blob: 3c6bc1c6fdcc91d22fee5bbc22574617a57a6c8a [file] [log] [blame]
Javier Martin186b2502012-07-26 05:53:35 -03001/*
2 * Coda multi-standard codec IP
3 *
4 * Copyright (C) 2012 Vista Silicon S.L.
5 * Javier Martin, <javier.martin@vista-silicon.com>
6 * Xavier Duret
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
14#include <linux/clk.h>
Philipp Zabelf61c0b12014-07-11 06:36:40 -030015#include <linux/debugfs.h>
Javier Martin186b2502012-07-26 05:53:35 -030016#include <linux/delay.h>
17#include <linux/firmware.h>
Philipp Zabelcde29ef2015-07-16 13:13:24 -030018#include <linux/gcd.h>
Philipp Zabel657eee72013-04-29 16:17:14 -070019#include <linux/genalloc.h>
Javier Martin186b2502012-07-26 05:53:35 -030020#include <linux/interrupt.h>
21#include <linux/io.h>
22#include <linux/irq.h>
Philipp Zabel9082a7c2013-06-21 03:55:31 -030023#include <linux/kfifo.h>
Javier Martin186b2502012-07-26 05:53:35 -030024#include <linux/module.h>
25#include <linux/of_device.h>
26#include <linux/platform_device.h>
Philipp Zabel1e172732014-07-11 06:36:23 -030027#include <linux/pm_runtime.h>
Javier Martin186b2502012-07-26 05:53:35 -030028#include <linux/slab.h>
29#include <linux/videodev2.h>
30#include <linux/of.h>
Mauro Carvalho Chehaba71daaa2015-11-17 07:11:13 -020031#include <linux/platform_data/media/coda.h>
Philipp Zabel8f452842014-07-11 06:36:35 -030032#include <linux/reset.h>
Javier Martin186b2502012-07-26 05:53:35 -030033
34#include <media/v4l2-ctrls.h>
35#include <media/v4l2-device.h>
Philipp Zabel918c66f2013-06-27 06:59:01 -030036#include <media/v4l2-event.h>
Javier Martin186b2502012-07-26 05:53:35 -030037#include <media/v4l2-ioctl.h>
38#include <media/v4l2-mem2mem.h>
Junghak Sungc1399902015-09-22 10:30:29 -030039#include <media/videobuf2-v4l2.h>
Javier Martin186b2502012-07-26 05:53:35 -030040#include <media/videobuf2-dma-contig.h>
Philipp Zabelbb04aa62015-01-23 13:51:30 -030041#include <media/videobuf2-vmalloc.h>
Javier Martin186b2502012-07-26 05:53:35 -030042
Philipp Zabela2b3e462014-07-23 12:28:39 -030043#include "coda.h"
Javier Martin186b2502012-07-26 05:53:35 -030044
45#define CODA_NAME "coda"
46
Philipp Zabel0cddc7e2013-09-30 10:34:44 -030047#define CODADX6_MAX_INSTANCES 4
Philipp Zabel2c11d1b2014-10-02 14:08:28 -030048#define CODA_MAX_FORMATS 4
Javier Martin186b2502012-07-26 05:53:35 -030049
Javier Martin186b2502012-07-26 05:53:35 -030050#define CODA_ISRAM_SIZE (2048 * 2)
51
Javier Martin186b2502012-07-26 05:53:35 -030052#define MIN_W 176
53#define MIN_H 144
Javier Martin186b2502012-07-26 05:53:35 -030054
55#define S_ALIGN 1 /* multiple of 2 */
56#define W_ALIGN 1 /* multiple of 2 */
57#define H_ALIGN 1 /* multiple of 2 */
58
59#define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh)
60
Philipp Zabel79924ca2014-07-23 12:28:45 -030061int coda_debug;
Philipp Zabelc4eb1bfc2013-05-21 04:24:16 -030062module_param(coda_debug, int, 0644);
Philipp Zabeld60b18b2014-08-05 14:00:15 -030063MODULE_PARM_DESC(coda_debug, "Debug level (0-2)");
Javier Martin186b2502012-07-26 05:53:35 -030064
Philipp Zabela269e532015-07-16 13:19:38 -030065static int disable_tiling;
66module_param(disable_tiling, int, 0644);
67MODULE_PARM_DESC(disable_tiling, "Disable tiled frame buffers");
Philipp Zabelb96904e2013-05-23 10:42:58 -030068
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,
Junghak Sung2d700712015-09-22 10:30:30 -030087 struct vb2_v4l2_buffer *buf, unsigned int reg_y)
Philipp Zabel856d7d92014-09-29 09:53:44 -030088{
Junghak Sung2d700712015-09-22 10:30:30 -030089 u32 base_y = vb2_dma_contig_plane_dma_addr(&buf->vb2_buf, 0);
Philipp Zabel856d7d92014-09-29 09:53:44 -030090 u32 base_cb, base_cr;
91
92 switch (q_data->fourcc) {
Philipp Zabel6727d4f2015-07-16 13:19:39 -030093 case V4L2_PIX_FMT_NV12:
94 case V4L2_PIX_FMT_YUV420:
95 default:
96 base_cb = base_y + q_data->bytesperline * q_data->height;
97 base_cr = base_cb + q_data->bytesperline * q_data->height / 4;
98 break;
Philipp Zabel856d7d92014-09-29 09:53:44 -030099 case V4L2_PIX_FMT_YVU420:
100 /* Switch Cb and Cr for YVU420 format */
101 base_cr = base_y + q_data->bytesperline * q_data->height;
102 base_cb = base_cr + 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
Philipp Zabelb96904e2013-05-23 10:42:58 -0300114#define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
115 { mode, src_fourcc, dst_fourcc, max_w, max_h }
116
117/*
118 * Arrays of codecs supported by each given version of Coda:
119 * i.MX27 -> codadx6
120 * i.MX5x -> coda7
121 * i.MX6 -> coda960
122 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
123 */
Philipp Zabel814c3762014-07-18 07:22:45 -0300124static const struct coda_codec codadx6_codecs[] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300125 CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
126 CODA_CODEC(CODADX6_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -0300127};
128
Philipp Zabel814c3762014-07-18 07:22:45 -0300129static const struct coda_codec coda7_codecs[] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300130 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720),
131 CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720),
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300132 CODA_CODEC(CODA7_MODE_ENCODE_MJPG, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_JPEG, 8192, 8192),
Philipp Zabelb0ed05b2014-08-05 14:00:14 -0300133 CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
134 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088),
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300135 CODA_CODEC(CODA7_MODE_DECODE_MJPG, V4L2_PIX_FMT_JPEG, V4L2_PIX_FMT_YUV420, 8192, 8192),
Philipp Zabelb96904e2013-05-23 10:42:58 -0300136};
137
Philipp Zabel814c3762014-07-18 07:22:45 -0300138static const struct coda_codec coda9_codecs[] = {
Philipp Zabelb0ed05b2014-08-05 14:00:14 -0300139 CODA_CODEC(CODA9_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1920, 1088),
140 CODA_CODEC(CODA9_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1920, 1088),
141 CODA_CODEC(CODA9_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
142 CODA_CODEC(CODA9_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088),
Philipp Zabel89548442014-07-11 06:36:17 -0300143};
144
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300145struct coda_video_device {
146 const char *name;
147 enum coda_inst_type type;
148 const struct coda_context_ops *ops;
Philipp Zabela22496c2015-01-23 13:51:33 -0300149 bool direct;
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300150 u32 src_formats[CODA_MAX_FORMATS];
151 u32 dst_formats[CODA_MAX_FORMATS];
152};
153
154static const struct coda_video_device coda_bit_encoder = {
155 .name = "coda-encoder",
156 .type = CODA_INST_ENCODER,
157 .ops = &coda_bit_encode_ops,
158 .src_formats = {
Philipp Zabel6727d4f2015-07-16 13:19:39 -0300159 V4L2_PIX_FMT_NV12,
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300160 V4L2_PIX_FMT_YUV420,
161 V4L2_PIX_FMT_YVU420,
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300162 },
163 .dst_formats = {
164 V4L2_PIX_FMT_H264,
165 V4L2_PIX_FMT_MPEG4,
166 },
167};
168
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300169static const struct coda_video_device coda_bit_jpeg_encoder = {
170 .name = "coda-jpeg-encoder",
171 .type = CODA_INST_ENCODER,
172 .ops = &coda_bit_encode_ops,
173 .src_formats = {
Philipp Zabel6727d4f2015-07-16 13:19:39 -0300174 V4L2_PIX_FMT_NV12,
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300175 V4L2_PIX_FMT_YUV420,
176 V4L2_PIX_FMT_YVU420,
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300177 V4L2_PIX_FMT_YUV422P,
178 },
179 .dst_formats = {
180 V4L2_PIX_FMT_JPEG,
181 },
182};
183
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300184static const struct coda_video_device coda_bit_decoder = {
185 .name = "coda-decoder",
186 .type = CODA_INST_DECODER,
187 .ops = &coda_bit_decode_ops,
188 .src_formats = {
189 V4L2_PIX_FMT_H264,
190 V4L2_PIX_FMT_MPEG4,
191 },
192 .dst_formats = {
Philipp Zabel6727d4f2015-07-16 13:19:39 -0300193 V4L2_PIX_FMT_NV12,
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300194 V4L2_PIX_FMT_YUV420,
195 V4L2_PIX_FMT_YVU420,
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300196 },
197};
198
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300199static const struct coda_video_device coda_bit_jpeg_decoder = {
200 .name = "coda-jpeg-decoder",
201 .type = CODA_INST_DECODER,
202 .ops = &coda_bit_decode_ops,
203 .src_formats = {
204 V4L2_PIX_FMT_JPEG,
205 },
206 .dst_formats = {
Philipp Zabel6727d4f2015-07-16 13:19:39 -0300207 V4L2_PIX_FMT_NV12,
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300208 V4L2_PIX_FMT_YUV420,
209 V4L2_PIX_FMT_YVU420,
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300210 V4L2_PIX_FMT_YUV422P,
211 },
212};
213
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300214static const struct coda_video_device *codadx6_video_devices[] = {
215 &coda_bit_encoder,
216};
217
218static const struct coda_video_device *coda7_video_devices[] = {
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300219 &coda_bit_jpeg_encoder,
220 &coda_bit_jpeg_decoder,
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300221 &coda_bit_encoder,
222 &coda_bit_decoder,
223};
224
225static const struct coda_video_device *coda9_video_devices[] = {
226 &coda_bit_encoder,
227 &coda_bit_decoder,
228};
229
Philipp Zabelb96904e2013-05-23 10:42:58 -0300230/*
231 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
232 * tables.
233 */
234static u32 coda_format_normalize_yuv(u32 fourcc)
235{
Philipp Zabel8076c7e2015-07-09 07:10:18 -0300236 switch (fourcc) {
Philipp Zabel6727d4f2015-07-16 13:19:39 -0300237 case V4L2_PIX_FMT_NV12:
Philipp Zabel8076c7e2015-07-09 07:10:18 -0300238 case V4L2_PIX_FMT_YUV420:
239 case V4L2_PIX_FMT_YVU420:
Philipp Zabel8076c7e2015-07-09 07:10:18 -0300240 case V4L2_PIX_FMT_YUV422P:
241 return V4L2_PIX_FMT_YUV420;
242 default:
243 return fourcc;
244 }
Philipp Zabelb96904e2013-05-23 10:42:58 -0300245}
246
Philipp Zabel814c3762014-07-18 07:22:45 -0300247static const struct coda_codec *coda_find_codec(struct coda_dev *dev,
248 int src_fourcc, int dst_fourcc)
Philipp Zabelb96904e2013-05-23 10:42:58 -0300249{
Philipp Zabel814c3762014-07-18 07:22:45 -0300250 const struct coda_codec *codecs = dev->devtype->codecs;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300251 int num_codecs = dev->devtype->num_codecs;
252 int k;
253
254 src_fourcc = coda_format_normalize_yuv(src_fourcc);
255 dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
256 if (src_fourcc == dst_fourcc)
257 return NULL;
258
259 for (k = 0; k < num_codecs; k++) {
260 if (codecs[k].src_fourcc == src_fourcc &&
261 codecs[k].dst_fourcc == dst_fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300262 break;
263 }
264
Philipp Zabelb96904e2013-05-23 10:42:58 -0300265 if (k == num_codecs)
Javier Martin186b2502012-07-26 05:53:35 -0300266 return NULL;
267
Philipp Zabelb96904e2013-05-23 10:42:58 -0300268 return &codecs[k];
Javier Martin186b2502012-07-26 05:53:35 -0300269}
270
Philipp Zabel57625592013-09-30 10:34:51 -0300271static void coda_get_max_dimensions(struct coda_dev *dev,
Philipp Zabel814c3762014-07-18 07:22:45 -0300272 const struct coda_codec *codec,
Philipp Zabel57625592013-09-30 10:34:51 -0300273 int *max_w, int *max_h)
274{
Philipp Zabel814c3762014-07-18 07:22:45 -0300275 const struct coda_codec *codecs = dev->devtype->codecs;
Philipp Zabel57625592013-09-30 10:34:51 -0300276 int num_codecs = dev->devtype->num_codecs;
277 unsigned int w, h;
278 int k;
279
280 if (codec) {
281 w = codec->max_w;
282 h = codec->max_h;
283 } else {
284 for (k = 0, w = 0, h = 0; k < num_codecs; k++) {
285 w = max(w, codecs[k].max_w);
286 h = max(h, codecs[k].max_h);
287 }
288 }
289
290 if (max_w)
291 *max_w = w;
292 if (max_h)
293 *max_h = h;
294}
295
Philipp Zabel55425702015-12-02 14:58:50 -0200296static const struct coda_video_device *to_coda_video_device(struct video_device
297 *vdev)
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300298{
299 struct coda_dev *dev = video_get_drvdata(vdev);
300 unsigned int i = vdev - dev->vfd;
301
302 if (i >= dev->devtype->num_vdevs)
303 return NULL;
304
305 return dev->devtype->vdevs[i];
306}
307
Philipp Zabel79924ca2014-07-23 12:28:45 -0300308const char *coda_product_name(int product)
Philipp Zabel927933f2013-09-30 10:34:48 -0300309{
310 static char buf[9];
311
312 switch (product) {
313 case CODA_DX6:
314 return "CodaDx6";
315 case CODA_7541:
316 return "CODA7541";
Philipp Zabel89548442014-07-11 06:36:17 -0300317 case CODA_960:
318 return "CODA960";
Philipp Zabel927933f2013-09-30 10:34:48 -0300319 default:
320 snprintf(buf, sizeof(buf), "(0x%04x)", product);
321 return buf;
322 }
323}
324
Javier Martin186b2502012-07-26 05:53:35 -0300325/*
326 * V4L2 ioctl() operations.
327 */
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300328static int coda_querycap(struct file *file, void *priv,
329 struct v4l2_capability *cap)
Javier Martin186b2502012-07-26 05:53:35 -0300330{
Philipp Zabel927933f2013-09-30 10:34:48 -0300331 struct coda_ctx *ctx = fh_to_ctx(priv);
332
Javier Martin186b2502012-07-26 05:53:35 -0300333 strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver));
Philipp Zabel927933f2013-09-30 10:34:48 -0300334 strlcpy(cap->card, coda_product_name(ctx->dev->devtype->product),
335 sizeof(cap->card));
Philipp Zabeldad0e1c2013-05-21 04:18:22 -0300336 strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
Philipp Zabel3898e7a2014-07-18 07:22:37 -0300337 cap->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
Javier Martin186b2502012-07-26 05:53:35 -0300338 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
339
340 return 0;
341}
342
Philipp Zabel22e244b2014-07-18 07:22:43 -0300343static int coda_enum_fmt(struct file *file, void *priv,
344 struct v4l2_fmtdesc *f)
Javier Martin186b2502012-07-26 05:53:35 -0300345{
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300346 struct video_device *vdev = video_devdata(file);
347 const struct coda_video_device *cvd = to_coda_video_device(vdev);
348 const u32 *formats;
Philipp Zabel22e244b2014-07-18 07:22:43 -0300349
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300350 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
351 formats = cvd->src_formats;
352 else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
353 formats = cvd->dst_formats;
Philipp Zabel22e244b2014-07-18 07:22:43 -0300354 else
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300355 return -EINVAL;
Javier Martin186b2502012-07-26 05:53:35 -0300356
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300357 if (f->index >= CODA_MAX_FORMATS || formats[f->index] == 0)
358 return -EINVAL;
Javier Martin186b2502012-07-26 05:53:35 -0300359
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300360 f->pixelformat = formats[f->index];
Javier Martin186b2502012-07-26 05:53:35 -0300361
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300362 return 0;
Javier Martin186b2502012-07-26 05:53:35 -0300363}
364
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300365static int coda_g_fmt(struct file *file, void *priv,
366 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300367{
Javier Martin186b2502012-07-26 05:53:35 -0300368 struct coda_q_data *q_data;
369 struct coda_ctx *ctx = fh_to_ctx(priv);
370
Javier Martin186b2502012-07-26 05:53:35 -0300371 q_data = get_q_data(ctx, f->type);
Philipp Zabelb9736292014-07-11 06:36:18 -0300372 if (!q_data)
373 return -EINVAL;
Javier Martin186b2502012-07-26 05:53:35 -0300374
375 f->fmt.pix.field = V4L2_FIELD_NONE;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300376 f->fmt.pix.pixelformat = q_data->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -0300377 f->fmt.pix.width = q_data->width;
378 f->fmt.pix.height = q_data->height;
Philipp Zabel2fd6a372014-07-11 06:36:36 -0300379 f->fmt.pix.bytesperline = q_data->bytesperline;
Javier Martin186b2502012-07-26 05:53:35 -0300380
381 f->fmt.pix.sizeimage = q_data->sizeimage;
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300382 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_JPEG)
383 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
384 else
385 f->fmt.pix.colorspace = ctx->colorspace;
Javier Martin186b2502012-07-26 05:53:35 -0300386
387 return 0;
388}
389
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300390static int coda_try_pixelformat(struct coda_ctx *ctx, struct v4l2_format *f)
391{
392 struct coda_q_data *q_data;
393 const u32 *formats;
394 int i;
395
396 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
397 formats = ctx->cvd->src_formats;
398 else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
399 formats = ctx->cvd->dst_formats;
400 else
401 return -EINVAL;
402
403 for (i = 0; i < CODA_MAX_FORMATS; i++) {
404 if (formats[i] == f->fmt.pix.pixelformat) {
405 f->fmt.pix.pixelformat = formats[i];
406 return 0;
407 }
408 }
409
410 /* Fall back to currently set pixelformat */
411 q_data = get_q_data(ctx, f->type);
412 f->fmt.pix.pixelformat = q_data->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -0300413
414 return 0;
415}
416
Philipp Zabel8e428d52015-01-23 13:51:29 -0300417static unsigned int coda_estimate_sizeimage(struct coda_ctx *ctx, u32 sizeimage,
418 u32 width, u32 height)
419{
420 /*
421 * This is a rough estimate for sensible compressed buffer
422 * sizes (between 1 and 16 bits per pixel). This could be
423 * improved by better format specific worst case estimates.
424 */
425 return round_up(clamp(sizeimage, width * height / 8,
426 width * height * 2), PAGE_SIZE);
427}
428
Philipp Zabel814c3762014-07-18 07:22:45 -0300429static int coda_try_fmt(struct coda_ctx *ctx, const struct coda_codec *codec,
Philipp Zabel57625592013-09-30 10:34:51 -0300430 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300431{
Philipp Zabel57625592013-09-30 10:34:51 -0300432 struct coda_dev *dev = ctx->dev;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300433 unsigned int max_w, max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300434 enum v4l2_field field;
435
436 field = f->fmt.pix.field;
437 if (field == V4L2_FIELD_ANY)
438 field = V4L2_FIELD_NONE;
439 else if (V4L2_FIELD_NONE != field)
440 return -EINVAL;
441
442 /* V4L2 specification suggests the driver corrects the format struct
443 * if any of the dimensions is unsupported */
444 f->fmt.pix.field = field;
445
Philipp Zabel57625592013-09-30 10:34:51 -0300446 coda_get_max_dimensions(dev, codec, &max_w, &max_h);
447 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN,
448 &f->fmt.pix.height, MIN_H, max_h, H_ALIGN,
449 S_ALIGN);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300450
Philipp Zabel57625592013-09-30 10:34:51 -0300451 switch (f->fmt.pix.pixelformat) {
Philipp Zabel6727d4f2015-07-16 13:19:39 -0300452 case V4L2_PIX_FMT_NV12:
Philipp Zabel57625592013-09-30 10:34:51 -0300453 case V4L2_PIX_FMT_YUV420:
454 case V4L2_PIX_FMT_YVU420:
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300455 /*
456 * Frame stride must be at least multiple of 8,
457 * but multiple of 16 for h.264 or JPEG 4:2:x
458 */
Philipp Zabel451dfe52014-07-11 06:36:39 -0300459 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
Philipp Zabel47cf0c62013-05-23 10:42:54 -0300460 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
Philipp Zabel451d43a2012-07-26 09:18:27 -0300461 f->fmt.pix.height * 3 / 2;
Philipp Zabel57625592013-09-30 10:34:51 -0300462 break;
Philipp Zabel4de69312014-10-02 14:08:26 -0300463 case V4L2_PIX_FMT_YUV422P:
464 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
465 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
466 f->fmt.pix.height * 2;
467 break;
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300468 case V4L2_PIX_FMT_JPEG:
469 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
470 /* fallthrough */
Philipp Zabel57625592013-09-30 10:34:51 -0300471 case V4L2_PIX_FMT_H264:
472 case V4L2_PIX_FMT_MPEG4:
Javier Martin186b2502012-07-26 05:53:35 -0300473 f->fmt.pix.bytesperline = 0;
Philipp Zabel8e428d52015-01-23 13:51:29 -0300474 f->fmt.pix.sizeimage = coda_estimate_sizeimage(ctx,
475 f->fmt.pix.sizeimage,
476 f->fmt.pix.width,
477 f->fmt.pix.height);
Philipp Zabel57625592013-09-30 10:34:51 -0300478 break;
479 default:
480 BUG();
Javier Martin186b2502012-07-26 05:53:35 -0300481 }
482
483 return 0;
484}
485
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300486static int coda_try_fmt_vid_cap(struct file *file, void *priv,
487 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300488{
Javier Martin186b2502012-07-26 05:53:35 -0300489 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300490 const struct coda_q_data *q_data_src;
491 const struct coda_codec *codec;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300492 struct vb2_queue *src_vq;
493 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300494
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300495 ret = coda_try_pixelformat(ctx, f);
496 if (ret < 0)
497 return ret;
498
499 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
500
Philipp Zabel918c66f2013-06-27 06:59:01 -0300501 /*
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300502 * If the source format is already fixed, only allow the same output
503 * resolution
Philipp Zabel918c66f2013-06-27 06:59:01 -0300504 */
Philipp Zabel14604e32014-07-11 06:36:22 -0300505 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300506 if (vb2_is_streaming(src_vq)) {
Philipp Zabel91b5841e2014-07-18 07:22:41 -0300507 f->fmt.pix.width = q_data_src->width;
508 f->fmt.pix.height = q_data_src->height;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300509 }
Javier Martin186b2502012-07-26 05:53:35 -0300510
511 f->fmt.pix.colorspace = ctx->colorspace;
512
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300513 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
514 codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
515 f->fmt.pix.pixelformat);
516 if (!codec)
517 return -EINVAL;
518
Philipp Zabel57625592013-09-30 10:34:51 -0300519 ret = coda_try_fmt(ctx, codec, f);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300520 if (ret < 0)
521 return ret;
522
523 /* The h.264 decoder only returns complete 16x16 macroblocks */
524 if (codec && codec->src_fourcc == V4L2_PIX_FMT_H264) {
Philipp Zabel1b0ed7b2014-07-11 06:36:37 -0300525 f->fmt.pix.width = f->fmt.pix.width;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300526 f->fmt.pix.height = round_up(f->fmt.pix.height, 16);
Philipp Zabel1b0ed7b2014-07-11 06:36:37 -0300527 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300528 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
529 f->fmt.pix.height * 3 / 2;
530 }
531
532 return 0;
Javier Martin186b2502012-07-26 05:53:35 -0300533}
534
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300535static int coda_try_fmt_vid_out(struct file *file, void *priv,
536 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300537{
538 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300539 struct coda_dev *dev = ctx->dev;
540 const struct coda_q_data *q_data_dst;
541 const struct coda_codec *codec;
542 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300543
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300544 ret = coda_try_pixelformat(ctx, f);
545 if (ret < 0)
546 return ret;
Javier Martin186b2502012-07-26 05:53:35 -0300547
Philipp Zabel5f9826e2015-01-23 13:51:21 -0300548 switch (f->fmt.pix.colorspace) {
549 case V4L2_COLORSPACE_REC709:
550 case V4L2_COLORSPACE_JPEG:
551 break;
552 default:
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300553 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_JPEG)
554 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
555 else
556 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
Javier Martin186b2502012-07-26 05:53:35 -0300557 }
558
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300559 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
560 codec = coda_find_codec(dev, f->fmt.pix.pixelformat, q_data_dst->fourcc);
Javier Martin186b2502012-07-26 05:53:35 -0300561
562 return coda_try_fmt(ctx, codec, f);
563}
564
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300565static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300566{
567 struct coda_q_data *q_data;
568 struct vb2_queue *vq;
Javier Martin186b2502012-07-26 05:53:35 -0300569
Philipp Zabel14604e32014-07-11 06:36:22 -0300570 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
Javier Martin186b2502012-07-26 05:53:35 -0300571 if (!vq)
572 return -EINVAL;
573
574 q_data = get_q_data(ctx, f->type);
575 if (!q_data)
576 return -EINVAL;
577
578 if (vb2_is_busy(vq)) {
579 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
580 return -EBUSY;
581 }
582
Philipp Zabelb96904e2013-05-23 10:42:58 -0300583 q_data->fourcc = f->fmt.pix.pixelformat;
Javier Martin186b2502012-07-26 05:53:35 -0300584 q_data->width = f->fmt.pix.width;
585 q_data->height = f->fmt.pix.height;
Philipp Zabel2fd6a372014-07-11 06:36:36 -0300586 q_data->bytesperline = f->fmt.pix.bytesperline;
Philipp Zabel451d43a2012-07-26 09:18:27 -0300587 q_data->sizeimage = f->fmt.pix.sizeimage;
Philipp Zabel52c41672014-07-11 06:36:19 -0300588 q_data->rect.left = 0;
589 q_data->rect.top = 0;
590 q_data->rect.width = f->fmt.pix.width;
591 q_data->rect.height = f->fmt.pix.height;
Javier Martin186b2502012-07-26 05:53:35 -0300592
Philipp Zabela269e532015-07-16 13:19:38 -0300593 switch (f->fmt.pix.pixelformat) {
594 case V4L2_PIX_FMT_NV12:
595 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
596 ctx->tiled_map_type = GDI_TILED_FRAME_MB_RASTER_MAP;
597 if (!disable_tiling)
598 break;
599 }
600 /* else fall through */
601 case V4L2_PIX_FMT_YUV420:
602 case V4L2_PIX_FMT_YVU420:
603 ctx->tiled_map_type = GDI_LINEAR_FRAME_MAP;
604 break;
605 default:
606 break;
607 }
608
Javier Martin186b2502012-07-26 05:53:35 -0300609 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
610 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
Philipp Zabelb96904e2013-05-23 10:42:58 -0300611 f->type, q_data->width, q_data->height, q_data->fourcc);
Javier Martin186b2502012-07-26 05:53:35 -0300612
613 return 0;
614}
615
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300616static int coda_s_fmt_vid_cap(struct file *file, void *priv,
617 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300618{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300619 struct coda_ctx *ctx = fh_to_ctx(priv);
Javier Martin186b2502012-07-26 05:53:35 -0300620 int ret;
621
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300622 ret = coda_try_fmt_vid_cap(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300623 if (ret)
624 return ret;
625
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300626 return coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300627}
628
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300629static int coda_s_fmt_vid_out(struct file *file, void *priv,
630 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300631{
632 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelf95a6ce2014-08-05 14:00:19 -0300633 struct v4l2_format f_cap;
Javier Martin186b2502012-07-26 05:53:35 -0300634 int ret;
635
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300636 ret = coda_try_fmt_vid_out(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300637 if (ret)
638 return ret;
639
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300640 ret = coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300641 if (ret)
Philipp Zabel2dc546d2014-08-05 14:00:18 -0300642 return ret;
643
644 ctx->colorspace = f->fmt.pix.colorspace;
Javier Martin186b2502012-07-26 05:53:35 -0300645
Philipp Zabel1c2f6a92015-01-23 13:51:22 -0300646 memset(&f_cap, 0, sizeof(f_cap));
Philipp Zabelf95a6ce2014-08-05 14:00:19 -0300647 f_cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
648 coda_g_fmt(file, priv, &f_cap);
649 f_cap.fmt.pix.width = f->fmt.pix.width;
650 f_cap.fmt.pix.height = f->fmt.pix.height;
651
652 ret = coda_try_fmt_vid_cap(file, priv, &f_cap);
653 if (ret)
654 return ret;
655
656 return coda_s_fmt(ctx, &f_cap);
Javier Martin186b2502012-07-26 05:53:35 -0300657}
658
Philipp Zabel73751da2015-03-24 14:30:51 -0300659static int coda_reqbufs(struct file *file, void *priv,
660 struct v4l2_requestbuffers *rb)
661{
662 struct coda_ctx *ctx = fh_to_ctx(priv);
663 int ret;
664
665 ret = v4l2_m2m_reqbufs(file, ctx->fh.m2m_ctx, rb);
666 if (ret)
667 return ret;
668
669 /*
670 * Allow to allocate instance specific per-context buffers, such as
671 * bitstream ringbuffer, slice buffer, work buffer, etc. if needed.
672 */
673 if (rb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT && ctx->ops->reqbufs)
674 return ctx->ops->reqbufs(ctx, rb);
675
676 return 0;
677}
678
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300679static int coda_qbuf(struct file *file, void *priv,
680 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300681{
682 struct coda_ctx *ctx = fh_to_ctx(priv);
683
Philipp Zabel14604e32014-07-11 06:36:22 -0300684 return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
Javier Martin186b2502012-07-26 05:53:35 -0300685}
686
Philipp Zabel918c66f2013-06-27 06:59:01 -0300687static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx,
Junghak Sung2d700712015-09-22 10:30:30 -0300688 struct vb2_v4l2_buffer *buf)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300689{
690 struct vb2_queue *src_vq;
691
Philipp Zabel14604e32014-07-11 06:36:22 -0300692 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300693
694 return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) &&
Junghak Sung2d700712015-09-22 10:30:30 -0300695 (buf->sequence == (ctx->qsequence - 1)));
Philipp Zabel918c66f2013-06-27 06:59:01 -0300696}
697
Junghak Sung2d700712015-09-22 10:30:30 -0300698void coda_m2m_buf_done(struct coda_ctx *ctx, struct vb2_v4l2_buffer *buf,
Philipp Zabel9f2bfb32015-05-04 07:51:07 -0300699 enum vb2_buffer_state state)
Javier Martin186b2502012-07-26 05:53:35 -0300700{
Philipp Zabel9f2bfb32015-05-04 07:51:07 -0300701 const struct v4l2_event eos_event = {
702 .type = V4L2_EVENT_EOS
703 };
Javier Martin186b2502012-07-26 05:53:35 -0300704
Philipp Zabel9f2bfb32015-05-04 07:51:07 -0300705 if (coda_buf_is_end_of_stream(ctx, buf)) {
Junghak Sung2d700712015-09-22 10:30:30 -0300706 buf->flags |= V4L2_BUF_FLAG_LAST;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300707
708 v4l2_event_queue_fh(&ctx->fh, &eos_event);
709 }
710
Philipp Zabel9f2bfb32015-05-04 07:51:07 -0300711 v4l2_m2m_buf_done(buf, state);
Javier Martin186b2502012-07-26 05:53:35 -0300712}
713
Philipp Zabel52c41672014-07-11 06:36:19 -0300714static int coda_g_selection(struct file *file, void *fh,
715 struct v4l2_selection *s)
716{
717 struct coda_ctx *ctx = fh_to_ctx(fh);
718 struct coda_q_data *q_data;
719 struct v4l2_rect r, *rsel;
720
721 q_data = get_q_data(ctx, s->type);
722 if (!q_data)
723 return -EINVAL;
724
725 r.left = 0;
726 r.top = 0;
727 r.width = q_data->width;
728 r.height = q_data->height;
729 rsel = &q_data->rect;
730
731 switch (s->target) {
732 case V4L2_SEL_TGT_CROP_DEFAULT:
733 case V4L2_SEL_TGT_CROP_BOUNDS:
734 rsel = &r;
735 /* fallthrough */
736 case V4L2_SEL_TGT_CROP:
737 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
738 return -EINVAL;
739 break;
740 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
741 case V4L2_SEL_TGT_COMPOSE_PADDED:
742 rsel = &r;
743 /* fallthrough */
744 case V4L2_SEL_TGT_COMPOSE:
745 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
746 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
747 return -EINVAL;
748 break;
749 default:
750 return -EINVAL;
751 }
752
753 s->r = *rsel;
754
755 return 0;
756}
757
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300758static int coda_try_decoder_cmd(struct file *file, void *fh,
759 struct v4l2_decoder_cmd *dc)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300760{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300761 if (dc->cmd != V4L2_DEC_CMD_STOP)
762 return -EINVAL;
763
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300764 if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300765 return -EINVAL;
766
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300767 if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0))
Philipp Zabel918c66f2013-06-27 06:59:01 -0300768 return -EINVAL;
769
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300770 return 0;
771}
772
773static int coda_decoder_cmd(struct file *file, void *fh,
774 struct v4l2_decoder_cmd *dc)
775{
776 struct coda_ctx *ctx = fh_to_ctx(fh);
777 int ret;
778
779 ret = coda_try_decoder_cmd(file, fh, dc);
780 if (ret < 0)
781 return ret;
782
783 /* Ignore decoder stop command silently in encoder context */
Philipp Zabel918c66f2013-06-27 06:59:01 -0300784 if (ctx->inst_type != CODA_INST_DECODER)
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300785 return 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300786
Philipp Zabel347bb7f2014-07-23 12:28:42 -0300787 /* Set the stream-end flag on this context */
788 coda_bit_stream_end_flag(ctx);
Philipp Zabel84e23652014-07-11 06:36:34 -0300789 ctx->hold = false;
Michael Olbrichf3497da2014-07-11 06:36:30 -0300790 v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
Philipp Zabel89548442014-07-11 06:36:17 -0300791
Philipp Zabel918c66f2013-06-27 06:59:01 -0300792 return 0;
793}
794
Philipp Zabelcde29ef2015-07-16 13:13:24 -0300795static int coda_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
796{
797 struct coda_ctx *ctx = fh_to_ctx(fh);
798 struct v4l2_fract *tpf;
799
800 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
801 return -EINVAL;
802
803 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
804 tpf = &a->parm.output.timeperframe;
805 tpf->denominator = ctx->params.framerate & CODA_FRATE_RES_MASK;
806 tpf->numerator = 1 + (ctx->params.framerate >>
807 CODA_FRATE_DIV_OFFSET);
808
809 return 0;
810}
811
812/*
813 * Approximate timeperframe v4l2_fract with values that can be written
814 * into the 16-bit CODA_FRATE_DIV and CODA_FRATE_RES fields.
815 */
816static void coda_approximate_timeperframe(struct v4l2_fract *timeperframe)
817{
818 struct v4l2_fract s = *timeperframe;
819 struct v4l2_fract f0;
820 struct v4l2_fract f1 = { 1, 0 };
821 struct v4l2_fract f2 = { 0, 1 };
822 unsigned int i, div, s_denominator;
823
824 /* Lower bound is 1/65535 */
825 if (s.numerator == 0 || s.denominator / s.numerator > 65535) {
826 timeperframe->numerator = 1;
827 timeperframe->denominator = 65535;
828 return;
829 }
830
831 /* Upper bound is 65536/1, map everything above to infinity */
832 if (s.denominator == 0 || s.numerator / s.denominator > 65536) {
833 timeperframe->numerator = 1;
834 timeperframe->denominator = 0;
835 return;
836 }
837
838 /* Reduce fraction to lowest terms */
839 div = gcd(s.numerator, s.denominator);
840 if (div > 1) {
841 s.numerator /= div;
842 s.denominator /= div;
843 }
844
845 if (s.numerator <= 65536 && s.denominator < 65536) {
846 *timeperframe = s;
847 return;
848 }
849
850 /* Find successive convergents from continued fraction expansion */
851 while (f2.numerator <= 65536 && f2.denominator < 65536) {
852 f0 = f1;
853 f1 = f2;
854
855 /* Stop when f2 exactly equals timeperframe */
856 if (s.numerator == 0)
857 break;
858
859 i = s.denominator / s.numerator;
860
861 f2.numerator = f0.numerator + i * f1.numerator;
862 f2.denominator = f0.denominator + i * f2.denominator;
863
864 s_denominator = s.numerator;
865 s.numerator = s.denominator % s.numerator;
866 s.denominator = s_denominator;
867 }
868
869 *timeperframe = f1;
870}
871
872static uint32_t coda_timeperframe_to_frate(struct v4l2_fract *timeperframe)
873{
874 return ((timeperframe->numerator - 1) << CODA_FRATE_DIV_OFFSET) |
875 timeperframe->denominator;
876}
877
878static int coda_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
879{
880 struct coda_ctx *ctx = fh_to_ctx(fh);
881 struct v4l2_fract *tpf;
882
883 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
884 return -EINVAL;
885
886 tpf = &a->parm.output.timeperframe;
887 coda_approximate_timeperframe(tpf);
888 ctx->params.framerate = coda_timeperframe_to_frate(tpf);
889
890 return 0;
891}
892
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300893static int coda_subscribe_event(struct v4l2_fh *fh,
894 const struct v4l2_event_subscription *sub)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300895{
896 switch (sub->type) {
897 case V4L2_EVENT_EOS:
898 return v4l2_event_subscribe(fh, sub, 0, NULL);
899 default:
900 return v4l2_ctrl_subscribe_event(fh, sub);
901 }
Javier Martin186b2502012-07-26 05:53:35 -0300902}
903
904static const struct v4l2_ioctl_ops coda_ioctl_ops = {
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300905 .vidioc_querycap = coda_querycap,
Javier Martin186b2502012-07-26 05:53:35 -0300906
Philipp Zabel22e244b2014-07-18 07:22:43 -0300907 .vidioc_enum_fmt_vid_cap = coda_enum_fmt,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300908 .vidioc_g_fmt_vid_cap = coda_g_fmt,
909 .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
910 .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap,
Javier Martin186b2502012-07-26 05:53:35 -0300911
Philipp Zabel22e244b2014-07-18 07:22:43 -0300912 .vidioc_enum_fmt_vid_out = coda_enum_fmt,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300913 .vidioc_g_fmt_vid_out = coda_g_fmt,
914 .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
915 .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out,
Javier Martin186b2502012-07-26 05:53:35 -0300916
Philipp Zabel73751da2015-03-24 14:30:51 -0300917 .vidioc_reqbufs = coda_reqbufs,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300918 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
Javier Martin186b2502012-07-26 05:53:35 -0300919
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300920 .vidioc_qbuf = coda_qbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300921 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
Philipp Zabel9f2bfb32015-05-04 07:51:07 -0300922 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300923 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
Javier Martin186b2502012-07-26 05:53:35 -0300924
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300925 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
926 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300927
Philipp Zabel52c41672014-07-11 06:36:19 -0300928 .vidioc_g_selection = coda_g_selection,
929
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300930 .vidioc_try_decoder_cmd = coda_try_decoder_cmd,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300931 .vidioc_decoder_cmd = coda_decoder_cmd,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300932
Philipp Zabelcde29ef2015-07-16 13:13:24 -0300933 .vidioc_g_parm = coda_g_parm,
934 .vidioc_s_parm = coda_s_parm,
935
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300936 .vidioc_subscribe_event = coda_subscribe_event,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300937 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Javier Martin186b2502012-07-26 05:53:35 -0300938};
939
940/*
941 * Mem-to-mem operations.
942 */
Philipp Zabel477c1cf2013-06-21 03:55:33 -0300943
944static void coda_device_run(void *m2m_priv)
945{
946 struct coda_ctx *ctx = m2m_priv;
947 struct coda_dev *dev = ctx->dev;
Philipp Zabel32b6f202014-07-11 06:36:20 -0300948
949 queue_work(dev->workqueue, &ctx->pic_run_work);
950}
951
Philipp Zabel32b6f202014-07-11 06:36:20 -0300952static void coda_pic_run_work(struct work_struct *work)
953{
954 struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
955 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300956 int ret;
957
958 mutex_lock(&ctx->buffer_mutex);
Philipp Zabel477c1cf2013-06-21 03:55:33 -0300959 mutex_lock(&dev->coda_mutex);
960
Philipp Zabela1192a12014-07-23 12:28:40 -0300961 ret = ctx->ops->prepare_run(ctx);
962 if (ret < 0 && ctx->inst_type == CODA_INST_DECODER) {
963 mutex_unlock(&dev->coda_mutex);
964 mutex_unlock(&ctx->buffer_mutex);
965 /* job_finish scheduled by prepare_decode */
966 return;
Philipp Zabel10436672012-07-02 09:03:55 -0300967 }
968
Philipp Zabelf23797b2014-08-06 08:02:23 -0300969 if (!wait_for_completion_timeout(&ctx->completion,
970 msecs_to_jiffies(1000))) {
Philipp Zabel32b6f202014-07-11 06:36:20 -0300971 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout\n");
Philipp Zabel84e23652014-07-11 06:36:34 -0300972
973 ctx->hold = true;
Philipp Zabel8f452842014-07-11 06:36:35 -0300974
975 coda_hw_reset(ctx);
Philipp Zabel32b6f202014-07-11 06:36:20 -0300976 } else if (!ctx->aborting) {
Philipp Zabela1192a12014-07-23 12:28:40 -0300977 ctx->ops->finish_run(ctx);
Philipp Zabel32b6f202014-07-11 06:36:20 -0300978 }
979
Philipp Zabel747d7642015-01-23 13:51:31 -0300980 if ((ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out)) &&
981 ctx->ops->seq_end_work)
Philipp Zabel32b6f202014-07-11 06:36:20 -0300982 queue_work(dev->workqueue, &ctx->seq_end_work);
983
984 mutex_unlock(&dev->coda_mutex);
985 mutex_unlock(&ctx->buffer_mutex);
986
Philipp Zabel14604e32014-07-11 06:36:22 -0300987 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -0300988}
989
990static int coda_job_ready(void *m2m_priv)
991{
992 struct coda_ctx *ctx = m2m_priv;
Philipp Zabel2cf251c2015-07-09 07:10:20 -0300993 int src_bufs = v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -0300994
995 /*
996 * For both 'P' and 'key' frame cases 1 picture
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300997 * and 1 frame are needed. In the decoder case,
998 * the compressed frame can be in the bitstream.
Javier Martin186b2502012-07-26 05:53:35 -0300999 */
Philipp Zabel2cf251c2015-07-09 07:10:20 -03001000 if (!src_bufs && ctx->inst_type != CODA_INST_DECODER) {
Javier Martin186b2502012-07-26 05:53:35 -03001001 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1002 "not ready: not enough video buffers.\n");
1003 return 0;
1004 }
1005
Philipp Zabel14604e32014-07-11 06:36:22 -03001006 if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001007 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1008 "not ready: not enough video capture buffers.\n");
1009 return 0;
1010 }
1011
Philipp Zabela22496c2015-01-23 13:51:33 -03001012 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
Philipp Zabel47f3fa62015-07-09 07:10:21 -03001013 bool stream_end = ctx->bit_stream_param &
1014 CODA_BIT_STREAM_END_FLAG;
1015 int num_metas = ctx->num_metas;
Philipp Zabelf77fd8a2015-01-23 13:51:20 -03001016
Philipp Zabel2cf251c2015-07-09 07:10:20 -03001017 if (ctx->hold && !src_bufs) {
Philipp Zabelf77fd8a2015-01-23 13:51:20 -03001018 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1019 "%d: not ready: on hold for more buffers.\n",
1020 ctx->idx);
1021 return 0;
1022 }
1023
Philipp Zabelf77fd8a2015-01-23 13:51:20 -03001024 if (!stream_end && (num_metas + src_bufs) < 2) {
1025 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1026 "%d: not ready: need 2 buffers available (%d, %d)\n",
1027 ctx->idx, num_metas, src_bufs);
1028 return 0;
1029 }
1030
1031
Philipp Zabel2cf251c2015-07-09 07:10:20 -03001032 if (!src_bufs && !stream_end &&
1033 (coda_get_bitstream_payload(ctx) < 512)) {
Philipp Zabelf77fd8a2015-01-23 13:51:20 -03001034 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1035 "%d: not ready: not enough bitstream data (%d).\n",
1036 ctx->idx, coda_get_bitstream_payload(ctx));
1037 return 0;
1038 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001039 }
1040
Philipp Zabel3e748262013-05-23 10:43:01 -03001041 if (ctx->aborting) {
1042 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1043 "not ready: aborting\n");
1044 return 0;
1045 }
1046
Javier Martin186b2502012-07-26 05:53:35 -03001047 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1048 "job ready\n");
Philipp Zabel47f3fa62015-07-09 07:10:21 -03001049
Javier Martin186b2502012-07-26 05:53:35 -03001050 return 1;
1051}
1052
1053static void coda_job_abort(void *priv)
1054{
1055 struct coda_ctx *ctx = priv;
Javier Martin186b2502012-07-26 05:53:35 -03001056
1057 ctx->aborting = 1;
1058
1059 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1060 "Aborting task\n");
Javier Martin186b2502012-07-26 05:53:35 -03001061}
1062
1063static void coda_lock(void *m2m_priv)
1064{
1065 struct coda_ctx *ctx = m2m_priv;
1066 struct coda_dev *pcdev = ctx->dev;
Philipp Zabelf23797b2014-08-06 08:02:23 -03001067
Javier Martin186b2502012-07-26 05:53:35 -03001068 mutex_lock(&pcdev->dev_mutex);
1069}
1070
1071static void coda_unlock(void *m2m_priv)
1072{
1073 struct coda_ctx *ctx = m2m_priv;
1074 struct coda_dev *pcdev = ctx->dev;
Philipp Zabelf23797b2014-08-06 08:02:23 -03001075
Javier Martin186b2502012-07-26 05:53:35 -03001076 mutex_unlock(&pcdev->dev_mutex);
1077}
1078
Philipp Zabel814c3762014-07-18 07:22:45 -03001079static const struct v4l2_m2m_ops coda_m2m_ops = {
Javier Martin186b2502012-07-26 05:53:35 -03001080 .device_run = coda_device_run,
1081 .job_ready = coda_job_ready,
1082 .job_abort = coda_job_abort,
1083 .lock = coda_lock,
1084 .unlock = coda_unlock,
1085};
1086
1087static void set_default_params(struct coda_ctx *ctx)
1088{
Philipp Zabel8e428d52015-01-23 13:51:29 -03001089 unsigned int max_w, max_h, usize, csize;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001090
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001091 ctx->codec = coda_find_codec(ctx->dev, ctx->cvd->src_formats[0],
1092 ctx->cvd->dst_formats[0]);
Philipp Zabeld4c6a412014-10-02 14:08:35 -03001093 max_w = min(ctx->codec->max_w, 1920U);
1094 max_h = min(ctx->codec->max_h, 1088U);
Philipp Zabel8e428d52015-01-23 13:51:29 -03001095 usize = max_w * max_h * 3 / 2;
1096 csize = coda_estimate_sizeimage(ctx, usize, max_w, max_h);
Javier Martin186b2502012-07-26 05:53:35 -03001097
Philipp Zabel121cacf2014-07-18 07:22:42 -03001098 ctx->params.codec_mode = ctx->codec->mode;
Javier Martin186b2502012-07-26 05:53:35 -03001099 ctx->colorspace = V4L2_COLORSPACE_REC709;
1100 ctx->params.framerate = 30;
Javier Martin186b2502012-07-26 05:53:35 -03001101
1102 /* Default formats for output and input queues */
Philipp Zabel6727d4f2015-07-16 13:19:39 -03001103 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->cvd->src_formats[0];
1104 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->cvd->dst_formats[0];
Philipp Zabelb96904e2013-05-23 10:42:58 -03001105 ctx->q_data[V4L2_M2M_SRC].width = max_w;
1106 ctx->q_data[V4L2_M2M_SRC].height = max_h;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001107 ctx->q_data[V4L2_M2M_DST].width = max_w;
1108 ctx->q_data[V4L2_M2M_DST].height = max_h;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001109 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_YUV420) {
1110 ctx->q_data[V4L2_M2M_SRC].bytesperline = max_w;
Philipp Zabel8e428d52015-01-23 13:51:29 -03001111 ctx->q_data[V4L2_M2M_SRC].sizeimage = usize;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001112 ctx->q_data[V4L2_M2M_DST].bytesperline = 0;
Philipp Zabel8e428d52015-01-23 13:51:29 -03001113 ctx->q_data[V4L2_M2M_DST].sizeimage = csize;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001114 } else {
1115 ctx->q_data[V4L2_M2M_SRC].bytesperline = 0;
Philipp Zabel8e428d52015-01-23 13:51:29 -03001116 ctx->q_data[V4L2_M2M_SRC].sizeimage = csize;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001117 ctx->q_data[V4L2_M2M_DST].bytesperline = max_w;
Philipp Zabel8e428d52015-01-23 13:51:29 -03001118 ctx->q_data[V4L2_M2M_DST].sizeimage = usize;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001119 }
Philipp Zabel52c41672014-07-11 06:36:19 -03001120 ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
1121 ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
1122 ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
1123 ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
Philipp Zabel89548442014-07-11 06:36:17 -03001124
Philipp Zabela269e532015-07-16 13:19:38 -03001125 /*
1126 * Since the RBC2AXI logic only supports a single chroma plane,
1127 * macroblock tiling only works for to NV12 pixel format.
1128 */
1129 ctx->tiled_map_type = GDI_LINEAR_FRAME_MAP;
Javier Martin186b2502012-07-26 05:53:35 -03001130}
1131
1132/*
1133 * Queue operations
1134 */
Hans Verkuildf9ecb02015-10-28 00:50:37 -02001135static int coda_queue_setup(struct vb2_queue *vq,
Javier Martin186b2502012-07-26 05:53:35 -03001136 unsigned int *nbuffers, unsigned int *nplanes,
1137 unsigned int sizes[], void *alloc_ctxs[])
1138{
1139 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
Philipp Zabele34db062012-08-29 08:22:00 -03001140 struct coda_q_data *q_data;
Javier Martin186b2502012-07-26 05:53:35 -03001141 unsigned int size;
1142
Philipp Zabele34db062012-08-29 08:22:00 -03001143 q_data = get_q_data(ctx, vq->type);
1144 size = q_data->sizeimage;
Javier Martin186b2502012-07-26 05:53:35 -03001145
1146 *nplanes = 1;
1147 sizes[0] = size;
1148
Philipp Zabelbb04aa62015-01-23 13:51:30 -03001149 /* Set to vb2-dma-contig allocator context, ignored by vb2-vmalloc */
Javier Martin186b2502012-07-26 05:53:35 -03001150 alloc_ctxs[0] = ctx->dev->alloc_ctx;
1151
1152 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1153 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
1154
1155 return 0;
1156}
1157
1158static int coda_buf_prepare(struct vb2_buffer *vb)
1159{
1160 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1161 struct coda_q_data *q_data;
1162
1163 q_data = get_q_data(ctx, vb->vb2_queue->type);
1164
1165 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
1166 v4l2_warn(&ctx->dev->v4l2_dev,
1167 "%s data will not fit into plane (%lu < %lu)\n",
1168 __func__, vb2_plane_size(vb, 0),
1169 (long)q_data->sizeimage);
1170 return -EINVAL;
1171 }
1172
Javier Martin186b2502012-07-26 05:53:35 -03001173 return 0;
1174}
1175
1176static void coda_buf_queue(struct vb2_buffer *vb)
1177{
Junghak Sung2d700712015-09-22 10:30:30 -03001178 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
Javier Martin186b2502012-07-26 05:53:35 -03001179 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
Philipp Zabelcb06b702015-01-23 13:51:35 -03001180 struct vb2_queue *vq = vb->vb2_queue;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001181 struct coda_q_data *q_data;
1182
1183 q_data = get_q_data(ctx, vb->vb2_queue->type);
1184
1185 /*
1186 * In the decoder case, immediately try to copy the buffer into the
1187 * bitstream ringbuffer and mark it as ready to be dequeued.
1188 */
Philipp Zabelcb06b702015-01-23 13:51:35 -03001189 if (ctx->bitstream.size && vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001190 /*
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03001191 * For backwards compatibility, queuing an empty buffer marks
Philipp Zabel918c66f2013-06-27 06:59:01 -03001192 * the stream end
1193 */
Philipp Zabel347bb7f2014-07-23 12:28:42 -03001194 if (vb2_get_plane_payload(vb, 0) == 0)
1195 coda_bit_stream_end_flag(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001196 mutex_lock(&ctx->bitstream_mutex);
Junghak Sung2d700712015-09-22 10:30:30 -03001197 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
Michael Olbricheabed932014-07-18 07:22:40 -03001198 if (vb2_is_streaming(vb->vb2_queue))
Philipp Zabel582d88722015-03-24 14:30:57 -03001199 coda_fill_bitstream(ctx, true);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001200 mutex_unlock(&ctx->bitstream_mutex);
1201 } else {
Junghak Sung2d700712015-09-22 10:30:30 -03001202 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001203 }
Javier Martin186b2502012-07-26 05:53:35 -03001204}
1205
Philipp Zabel79924ca2014-07-23 12:28:45 -03001206int coda_alloc_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf,
1207 size_t size, const char *name, struct dentry *parent)
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001208{
1209 buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
1210 GFP_KERNEL);
Philipp Zabel68fc31c2014-08-05 14:00:16 -03001211 if (!buf->vaddr) {
1212 v4l2_err(&dev->v4l2_dev,
1213 "Failed to allocate %s buffer of size %u\n",
1214 name, size);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001215 return -ENOMEM;
Philipp Zabel68fc31c2014-08-05 14:00:16 -03001216 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001217
1218 buf->size = size;
1219
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001220 if (name && parent) {
1221 buf->blob.data = buf->vaddr;
1222 buf->blob.size = size;
Philipp Zabelf23797b2014-08-06 08:02:23 -03001223 buf->dentry = debugfs_create_blob(name, 0644, parent,
1224 &buf->blob);
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001225 if (!buf->dentry)
1226 dev_warn(&dev->plat_dev->dev,
1227 "failed to create debugfs entry %s\n", name);
1228 }
1229
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001230 return 0;
1231}
1232
Philipp Zabel79924ca2014-07-23 12:28:45 -03001233void coda_free_aux_buf(struct coda_dev *dev,
1234 struct coda_aux_buf *buf)
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001235{
1236 if (buf->vaddr) {
1237 dma_free_coherent(&dev->plat_dev->dev, buf->size,
1238 buf->vaddr, buf->paddr);
1239 buf->vaddr = NULL;
1240 buf->size = 0;
Peter Seidererd446ec82015-03-24 14:30:48 -03001241 debugfs_remove(buf->dentry);
1242 buf->dentry = NULL;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001243 }
1244}
1245
Javier Martin186b2502012-07-26 05:53:35 -03001246static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
1247{
1248 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1249 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
Javier Martin186b2502012-07-26 05:53:35 -03001250 struct coda_q_data *q_data_src, *q_data_dst;
Junghak Sung2d700712015-09-22 10:30:30 -03001251 struct vb2_v4l2_buffer *buf;
Philipp Zabeld35167a2013-05-23 10:42:59 -03001252 int ret = 0;
Javier Martin186b2502012-07-26 05:53:35 -03001253
Philipp Zabelb96904e2013-05-23 10:42:58 -03001254 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001255 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabel58bc7ed2015-07-09 07:10:14 -03001256 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
Philipp Zabel582d88722015-03-24 14:30:57 -03001257 /* copy the buffers that were queued before streamon */
Michael Olbricheabed932014-07-18 07:22:40 -03001258 mutex_lock(&ctx->bitstream_mutex);
Philipp Zabel582d88722015-03-24 14:30:57 -03001259 coda_fill_bitstream(ctx, false);
Michael Olbricheabed932014-07-18 07:22:40 -03001260 mutex_unlock(&ctx->bitstream_mutex);
1261
Philipp Zabelb9063522014-08-05 14:00:10 -03001262 if (coda_get_bitstream_payload(ctx) < 512) {
1263 ret = -EINVAL;
1264 goto err;
1265 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001266 } else {
Philipp Zabelb9063522014-08-05 14:00:10 -03001267 if (count < 1) {
1268 ret = -EINVAL;
1269 goto err;
1270 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001271 }
1272
1273 ctx->streamon_out = 1;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001274 } else {
Philipp Zabelb9063522014-08-05 14:00:10 -03001275 if (count < 1) {
1276 ret = -EINVAL;
1277 goto err;
1278 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001279
1280 ctx->streamon_cap = 1;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001281 }
Javier Martin186b2502012-07-26 05:53:35 -03001282
Philipp Zabelb96904e2013-05-23 10:42:58 -03001283 /* Don't start the coda unless both queues are on */
1284 if (!(ctx->streamon_out & ctx->streamon_cap))
1285 return 0;
Javier Martin186b2502012-07-26 05:53:35 -03001286
Philipp Zabel23b6ee52015-03-24 14:30:55 -03001287 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1288 if ((q_data_src->width != q_data_dst->width &&
1289 round_up(q_data_src->width, 16) != q_data_dst->width) ||
1290 (q_data_src->height != q_data_dst->height &&
1291 round_up(q_data_src->height, 16) != q_data_dst->height)) {
1292 v4l2_err(v4l2_dev, "can't convert %dx%d to %dx%d\n",
1293 q_data_src->width, q_data_src->height,
1294 q_data_dst->width, q_data_dst->height);
1295 ret = -EINVAL;
1296 goto err;
1297 }
1298
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001299 /* Allow BIT decoder device_run with no new buffers queued */
Philipp Zabela22496c2015-01-23 13:51:33 -03001300 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
Philipp Zabel14604e32014-07-11 06:36:22 -03001301 v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001302
Philipp Zabelb96904e2013-05-23 10:42:58 -03001303 ctx->gopcounter = ctx->params.gop_size - 1;
Javier Martin186b2502012-07-26 05:53:35 -03001304
Philipp Zabelb96904e2013-05-23 10:42:58 -03001305 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
1306 q_data_dst->fourcc);
1307 if (!ctx->codec) {
Javier Martin186b2502012-07-26 05:53:35 -03001308 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
Philipp Zabelb9063522014-08-05 14:00:10 -03001309 ret = -EINVAL;
1310 goto err;
Javier Martin186b2502012-07-26 05:53:35 -03001311 }
1312
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001313 if (q_data_dst->fourcc == V4L2_PIX_FMT_JPEG)
1314 ctx->params.gop_size = 1;
1315 ctx->gopcounter = ctx->params.gop_size - 1;
1316
Philipp Zabela1192a12014-07-23 12:28:40 -03001317 ret = ctx->ops->start_streaming(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001318 if (ctx->inst_type == CODA_INST_DECODER) {
Philipp Zabel89548442014-07-11 06:36:17 -03001319 if (ret == -EAGAIN)
Philipp Zabel918c66f2013-06-27 06:59:01 -03001320 return 0;
Philipp Zabel89548442014-07-11 06:36:17 -03001321 else if (ret < 0)
Philipp Zabelb9063522014-08-05 14:00:10 -03001322 goto err;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001323 }
1324
Philipp Zabel89548442014-07-11 06:36:17 -03001325 return ret;
Philipp Zabelb9063522014-08-05 14:00:10 -03001326
1327err:
1328 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1329 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
Philipp Zabelb8b1b58c52014-10-21 13:25:52 -03001330 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
Philipp Zabelb9063522014-08-05 14:00:10 -03001331 } else {
1332 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
Philipp Zabelb8b1b58c52014-10-21 13:25:52 -03001333 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
Philipp Zabelb9063522014-08-05 14:00:10 -03001334 }
1335 return ret;
Philipp Zabel89548442014-07-11 06:36:17 -03001336}
1337
Hans Verkuile37559b2014-04-17 02:47:21 -03001338static void coda_stop_streaming(struct vb2_queue *q)
Javier Martin186b2502012-07-26 05:53:35 -03001339{
1340 struct coda_ctx *ctx = vb2_get_drv_priv(q);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03001341 struct coda_dev *dev = ctx->dev;
Junghak Sung2d700712015-09-22 10:30:30 -03001342 struct vb2_v4l2_buffer *buf;
Philipp Zabel47f3fa62015-07-09 07:10:21 -03001343 unsigned long flags;
Philipp Zabel5c76c2c2015-03-24 14:30:56 -03001344 bool stop;
1345
1346 stop = ctx->streamon_out && ctx->streamon_cap;
Javier Martin186b2502012-07-26 05:53:35 -03001347
1348 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001349 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03001350 "%s: output\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001351 ctx->streamon_out = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001352
Philipp Zabel347bb7f2014-07-23 12:28:42 -03001353 coda_bit_stream_end_flag(ctx);
Philipp Zabel4a31b522014-08-05 14:00:11 -03001354
Philipp Zabel6dd5ef52015-01-23 13:51:26 -03001355 ctx->qsequence = 0;
Philipp Zabel4a31b522014-08-05 14:00:11 -03001356
1357 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1358 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
Javier Martin186b2502012-07-26 05:53:35 -03001359 } else {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001360 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03001361 "%s: capture\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001362 ctx->streamon_cap = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001363
1364 ctx->osequence = 0;
Philipp Zabelcb2c0282014-07-11 06:36:33 -03001365 ctx->sequence_offset = 0;
Philipp Zabel4a31b522014-08-05 14:00:11 -03001366
1367 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
1368 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
Javier Martin186b2502012-07-26 05:53:35 -03001369 }
1370
Philipp Zabel5c76c2c2015-03-24 14:30:56 -03001371 if (stop) {
Philipp Zabel7cbb1052014-10-02 14:08:32 -03001372 struct coda_buffer_meta *meta;
Philipp Zabel846ced92014-07-11 06:36:31 -03001373
Philipp Zabelf4706d62015-01-23 13:51:27 -03001374 if (ctx->ops->seq_end_work) {
1375 queue_work(dev->workqueue, &ctx->seq_end_work);
1376 flush_work(&ctx->seq_end_work);
1377 }
Philipp Zabel47f3fa62015-07-09 07:10:21 -03001378 spin_lock_irqsave(&ctx->buffer_meta_lock, flags);
Philipp Zabel7cbb1052014-10-02 14:08:32 -03001379 while (!list_empty(&ctx->buffer_meta_list)) {
1380 meta = list_first_entry(&ctx->buffer_meta_list,
1381 struct coda_buffer_meta, list);
1382 list_del(&meta->list);
1383 kfree(meta);
Philipp Zabel846ced92014-07-11 06:36:31 -03001384 }
Philipp Zabel47f3fa62015-07-09 07:10:21 -03001385 ctx->num_metas = 0;
1386 spin_unlock_irqrestore(&ctx->buffer_meta_lock, flags);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001387 kfifo_init(&ctx->bitstream_fifo,
1388 ctx->bitstream.vaddr, ctx->bitstream.size);
1389 ctx->runcounter = 0;
Philipp Zabelf157cf42014-09-29 09:53:42 -03001390 ctx->aborting = 0;
Philipp Zabel62bed142012-07-25 10:40:39 -03001391 }
Philipp Zabelc1ae0b22015-07-09 07:10:17 -03001392
1393 if (!ctx->streamon_out && !ctx->streamon_cap)
1394 ctx->bit_stream_param &= ~CODA_BIT_STREAM_END_FLAG;
Javier Martin186b2502012-07-26 05:53:35 -03001395}
1396
Philipp Zabel121cacf2014-07-18 07:22:42 -03001397static const struct vb2_ops coda_qops = {
Javier Martin186b2502012-07-26 05:53:35 -03001398 .queue_setup = coda_queue_setup,
1399 .buf_prepare = coda_buf_prepare,
1400 .buf_queue = coda_buf_queue,
Javier Martin186b2502012-07-26 05:53:35 -03001401 .start_streaming = coda_start_streaming,
1402 .stop_streaming = coda_stop_streaming,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001403 .wait_prepare = vb2_ops_wait_prepare,
1404 .wait_finish = vb2_ops_wait_finish,
Javier Martin186b2502012-07-26 05:53:35 -03001405};
1406
1407static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
1408{
1409 struct coda_ctx *ctx =
1410 container_of(ctrl->handler, struct coda_ctx, ctrls);
1411
1412 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1413 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
1414
1415 switch (ctrl->id) {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001416 case V4L2_CID_HFLIP:
1417 if (ctrl->val)
1418 ctx->params.rot_mode |= CODA_MIR_HOR;
1419 else
1420 ctx->params.rot_mode &= ~CODA_MIR_HOR;
1421 break;
1422 case V4L2_CID_VFLIP:
1423 if (ctrl->val)
1424 ctx->params.rot_mode |= CODA_MIR_VER;
1425 else
1426 ctx->params.rot_mode &= ~CODA_MIR_VER;
1427 break;
Javier Martin186b2502012-07-26 05:53:35 -03001428 case V4L2_CID_MPEG_VIDEO_BITRATE:
1429 ctx->params.bitrate = ctrl->val / 1000;
1430 break;
1431 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
1432 ctx->params.gop_size = ctrl->val;
1433 break;
1434 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
1435 ctx->params.h264_intra_qp = ctrl->val;
1436 break;
1437 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
1438 ctx->params.h264_inter_qp = ctrl->val;
1439 break;
Philipp Zabel1a5567e2014-07-11 06:36:26 -03001440 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
1441 ctx->params.h264_min_qp = ctrl->val;
1442 break;
1443 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
1444 ctx->params.h264_max_qp = ctrl->val;
1445 break;
Philipp Zabelde23b1d2014-07-11 06:36:27 -03001446 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
1447 ctx->params.h264_deblk_alpha = ctrl->val;
1448 break;
1449 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
1450 ctx->params.h264_deblk_beta = ctrl->val;
1451 break;
1452 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
1453 ctx->params.h264_deblk_enabled = (ctrl->val ==
1454 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
1455 break;
Javier Martin186b2502012-07-26 05:53:35 -03001456 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
1457 ctx->params.mpeg4_intra_qp = ctrl->val;
1458 break;
1459 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
1460 ctx->params.mpeg4_inter_qp = ctrl->val;
1461 break;
1462 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
1463 ctx->params.slice_mode = ctrl->val;
1464 break;
1465 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
1466 ctx->params.slice_max_mb = ctrl->val;
1467 break;
Philipp Zabelc566c782012-08-08 11:59:38 -03001468 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
1469 ctx->params.slice_max_bits = ctrl->val * 8;
1470 break;
Javier Martin186b2502012-07-26 05:53:35 -03001471 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
1472 break;
Philipp Zabelf38f79d2014-07-11 06:36:28 -03001473 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
1474 ctx->params.intra_refresh = ctrl->val;
1475 break;
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001476 case V4L2_CID_JPEG_COMPRESSION_QUALITY:
1477 coda_set_jpeg_compression_quality(ctx, ctrl->val);
1478 break;
1479 case V4L2_CID_JPEG_RESTART_INTERVAL:
1480 ctx->params.jpeg_restart_interval = ctrl->val;
1481 break;
Philipp Zabelda2b3b32015-07-10 10:37:52 -03001482 case V4L2_CID_MPEG_VIDEO_VBV_DELAY:
1483 ctx->params.vbv_delay = ctrl->val;
1484 break;
1485 case V4L2_CID_MPEG_VIDEO_VBV_SIZE:
1486 ctx->params.vbv_size = min(ctrl->val * 8192, 0x7fffffff);
1487 break;
Javier Martin186b2502012-07-26 05:53:35 -03001488 default:
1489 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1490 "Invalid control, id=%d, val=%d\n",
1491 ctrl->id, ctrl->val);
1492 return -EINVAL;
1493 }
1494
1495 return 0;
1496}
1497
Philipp Zabel814c3762014-07-18 07:22:45 -03001498static const struct v4l2_ctrl_ops coda_ctrl_ops = {
Javier Martin186b2502012-07-26 05:53:35 -03001499 .s_ctrl = coda_s_ctrl,
1500};
1501
Philipp Zabelbfc732f2014-10-02 14:08:29 -03001502static void coda_encode_ctrls(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03001503{
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001504 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel92b042e2015-03-18 08:15:36 -03001505 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1000, 0);
Javier Martin186b2502012-07-26 05:53:35 -03001506 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1507 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
1508 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03001509 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
Javier Martin186b2502012-07-26 05:53:35 -03001510 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03001511 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
Philipp Zabel1a5567e2014-07-11 06:36:26 -03001512 if (ctx->dev->devtype->product != CODA_960) {
1513 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1514 V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12);
1515 }
1516 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1517 V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51);
Javier Martin186b2502012-07-26 05:53:35 -03001518 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabelde23b1d2014-07-11 06:36:27 -03001519 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, 0, 15, 1, 0);
1520 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1521 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, 0, 15, 1, 0);
1522 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1523 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
1524 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED, 0x0,
1525 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
1526 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03001527 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
1528 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1529 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
1530 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1531 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
Philipp Zabelc566c782012-08-08 11:59:38 -03001532 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
1533 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
Javier Martin186b2502012-07-26 05:53:35 -03001534 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1535 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
Philipp Zabelc566c782012-08-08 11:59:38 -03001536 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabelf23797b2014-08-06 08:02:23 -03001537 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1,
1538 500);
Javier Martin186b2502012-07-26 05:53:35 -03001539 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1540 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
1541 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
1542 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
1543 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
Philipp Zabelf38f79d2014-07-11 06:36:28 -03001544 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabelf23797b2014-08-06 08:02:23 -03001545 V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0,
1546 1920 * 1088 / 256, 1, 0);
Philipp Zabelda2b3b32015-07-10 10:37:52 -03001547 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1548 V4L2_CID_MPEG_VIDEO_VBV_DELAY, 0, 0x7fff, 1, 0);
1549 /*
1550 * The maximum VBV size value is 0x7fffffff bits,
1551 * one bit less than 262144 KiB
1552 */
1553 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1554 V4L2_CID_MPEG_VIDEO_VBV_SIZE, 0, 262144, 1, 0);
Philipp Zabelbfc732f2014-10-02 14:08:29 -03001555}
1556
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001557static void coda_jpeg_encode_ctrls(struct coda_ctx *ctx)
1558{
1559 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1560 V4L2_CID_JPEG_COMPRESSION_QUALITY, 5, 100, 1, 50);
1561 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1562 V4L2_CID_JPEG_RESTART_INTERVAL, 0, 100, 1, 0);
1563}
1564
Philipp Zabelbfc732f2014-10-02 14:08:29 -03001565static int coda_ctrls_setup(struct coda_ctx *ctx)
1566{
1567 v4l2_ctrl_handler_init(&ctx->ctrls, 2);
1568
1569 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1570 V4L2_CID_HFLIP, 0, 1, 1, 0);
1571 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1572 V4L2_CID_VFLIP, 0, 1, 1, 0);
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001573 if (ctx->inst_type == CODA_INST_ENCODER) {
1574 if (ctx->cvd->dst_formats[0] == V4L2_PIX_FMT_JPEG)
1575 coda_jpeg_encode_ctrls(ctx);
1576 else
1577 coda_encode_ctrls(ctx);
1578 }
Javier Martin186b2502012-07-26 05:53:35 -03001579
1580 if (ctx->ctrls.error) {
Philipp Zabelf23797b2014-08-06 08:02:23 -03001581 v4l2_err(&ctx->dev->v4l2_dev,
1582 "control initialization error (%d)",
Javier Martin186b2502012-07-26 05:53:35 -03001583 ctx->ctrls.error);
1584 return -EINVAL;
1585 }
1586
1587 return v4l2_ctrl_handler_setup(&ctx->ctrls);
1588}
1589
Philipp Zabel121cacf2014-07-18 07:22:42 -03001590static int coda_queue_init(struct coda_ctx *ctx, struct vb2_queue *vq)
Javier Martin186b2502012-07-26 05:53:35 -03001591{
Philipp Zabel121cacf2014-07-18 07:22:42 -03001592 vq->drv_priv = ctx;
1593 vq->ops = &coda_qops;
1594 vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1595 vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1596 vq->lock = &ctx->dev->dev_mutex;
Kamil Debskie4af23d2015-02-23 09:26:18 -03001597 /* One way to indicate end-of-stream for coda is to set the
1598 * bytesused == 0. However by default videobuf2 handles bytesused
1599 * equal to 0 as a special case and changes its value to the size
1600 * of the buffer. Set the allow_zero_bytesused flag, so
1601 * that videobuf2 will keep the value of bytesused intact.
1602 */
1603 vq->allow_zero_bytesused = 1;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001604
1605 return vb2_queue_init(vq);
1606}
1607
Philipp Zabel79924ca2014-07-23 12:28:45 -03001608int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq,
1609 struct vb2_queue *dst_vq)
Philipp Zabel121cacf2014-07-18 07:22:42 -03001610{
Javier Martin186b2502012-07-26 05:53:35 -03001611 int ret;
1612
Javier Martin186b2502012-07-26 05:53:35 -03001613 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabeld29a8cf2014-07-18 07:22:38 -03001614 src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
Javier Martin186b2502012-07-26 05:53:35 -03001615 src_vq->mem_ops = &vb2_dma_contig_memops;
1616
Philipp Zabel121cacf2014-07-18 07:22:42 -03001617 ret = coda_queue_init(priv, src_vq);
Javier Martin186b2502012-07-26 05:53:35 -03001618 if (ret)
1619 return ret;
1620
Javier Martin186b2502012-07-26 05:53:35 -03001621 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Philipp Zabeld29a8cf2014-07-18 07:22:38 -03001622 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
Javier Martin186b2502012-07-26 05:53:35 -03001623 dst_vq->mem_ops = &vb2_dma_contig_memops;
1624
Philipp Zabel121cacf2014-07-18 07:22:42 -03001625 return coda_queue_init(priv, dst_vq);
1626}
1627
Philipp Zabel79924ca2014-07-23 12:28:45 -03001628int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq,
1629 struct vb2_queue *dst_vq)
Philipp Zabel121cacf2014-07-18 07:22:42 -03001630{
1631 int ret;
1632
1633 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabelbb04aa62015-01-23 13:51:30 -03001634 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
1635 src_vq->mem_ops = &vb2_vmalloc_memops;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001636
1637 ret = coda_queue_init(priv, src_vq);
1638 if (ret)
1639 return ret;
1640
1641 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1642 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1643 dst_vq->mem_ops = &vb2_dma_contig_memops;
1644
1645 return coda_queue_init(priv, dst_vq);
Javier Martin186b2502012-07-26 05:53:35 -03001646}
1647
Philipp Zabele11f3e62012-07-25 09:16:58 -03001648static int coda_next_free_instance(struct coda_dev *dev)
1649{
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03001650 int idx = ffz(dev->instance_mask);
1651
1652 if ((idx < 0) ||
1653 (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES))
1654 return -EBUSY;
1655
1656 return idx;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001657}
1658
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001659/*
1660 * File operations
1661 */
1662
1663static int coda_open(struct file *file)
Javier Martin186b2502012-07-26 05:53:35 -03001664{
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001665 struct video_device *vdev = video_devdata(file);
1666 struct coda_dev *dev = video_get_drvdata(vdev);
Javier Martin186b2502012-07-26 05:53:35 -03001667 struct coda_ctx *ctx = NULL;
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001668 char *name;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001669 int ret;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001670 int idx;
Javier Martin186b2502012-07-26 05:53:35 -03001671
Philipp Zabelf23797b2014-08-06 08:02:23 -03001672 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
Javier Martin186b2502012-07-26 05:53:35 -03001673 if (!ctx)
1674 return -ENOMEM;
1675
Fabio Estevamf82bc202013-08-21 11:14:16 -03001676 idx = coda_next_free_instance(dev);
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03001677 if (idx < 0) {
1678 ret = idx;
Fabio Estevamf82bc202013-08-21 11:14:16 -03001679 goto err_coda_max;
1680 }
1681 set_bit(idx, &dev->instance_mask);
1682
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001683 name = kasprintf(GFP_KERNEL, "context%d", idx);
Peter Seiderera7f933a2015-03-24 14:30:47 -03001684 if (!name) {
1685 ret = -ENOMEM;
1686 goto err_coda_name_init;
1687 }
1688
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001689 ctx->debugfs_entry = debugfs_create_dir(name, dev->debugfs_root);
1690 kfree(name);
1691
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001692 ctx->cvd = to_coda_video_device(vdev);
1693 ctx->inst_type = ctx->cvd->type;
1694 ctx->ops = ctx->cvd->ops;
Philipp Zabela22496c2015-01-23 13:51:33 -03001695 ctx->use_bit = !ctx->cvd->direct;
Philipp Zabel32b6f202014-07-11 06:36:20 -03001696 init_completion(&ctx->completion);
1697 INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
Philipp Zabel747d7642015-01-23 13:51:31 -03001698 if (ctx->ops->seq_end_work)
1699 INIT_WORK(&ctx->seq_end_work, ctx->ops->seq_end_work);
Javier Martin186b2502012-07-26 05:53:35 -03001700 v4l2_fh_init(&ctx->fh, video_devdata(file));
1701 file->private_data = &ctx->fh;
1702 v4l2_fh_add(&ctx->fh);
1703 ctx->dev = dev;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001704 ctx->idx = idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001705 switch (dev->devtype->product) {
Philipp Zabel89548442014-07-11 06:36:17 -03001706 case CODA_960:
Philipp Zabel2bf299c2014-09-29 09:53:46 -03001707 ctx->frame_mem_ctrl = 1 << 12;
1708 /* fallthrough */
1709 case CODA_7541:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001710 ctx->reg_idx = 0;
1711 break;
1712 default:
1713 ctx->reg_idx = idx;
1714 }
Fabio Estevamf82bc202013-08-21 11:14:16 -03001715
Philipp Zabel1e172732014-07-11 06:36:23 -03001716 /* Power up and upload firmware if necessary */
1717 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
1718 if (ret < 0) {
1719 v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
1720 goto err_pm_get;
1721 }
1722
Fabio Estevam79830db2013-08-21 11:14:17 -03001723 ret = clk_prepare_enable(dev->clk_per);
1724 if (ret)
1725 goto err_clk_per;
1726
1727 ret = clk_prepare_enable(dev->clk_ahb);
1728 if (ret)
1729 goto err_clk_ahb;
1730
Javier Martin186b2502012-07-26 05:53:35 -03001731 set_default_params(ctx);
Philipp Zabela1192a12014-07-23 12:28:40 -03001732 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
1733 ctx->ops->queue_init);
Philipp Zabel14604e32014-07-11 06:36:22 -03001734 if (IS_ERR(ctx->fh.m2m_ctx)) {
1735 ret = PTR_ERR(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001736
1737 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
1738 __func__, ret);
Fabio Estevamf82bc202013-08-21 11:14:16 -03001739 goto err_ctx_init;
Javier Martin186b2502012-07-26 05:53:35 -03001740 }
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001741
Javier Martin186b2502012-07-26 05:53:35 -03001742 ret = coda_ctrls_setup(ctx);
1743 if (ret) {
1744 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
Fabio Estevamf82bc202013-08-21 11:14:16 -03001745 goto err_ctrls_setup;
Javier Martin186b2502012-07-26 05:53:35 -03001746 }
1747
1748 ctx->fh.ctrl_handler = &ctx->ctrls;
1749
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001750 mutex_init(&ctx->bitstream_mutex);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001751 mutex_init(&ctx->buffer_mutex);
Philipp Zabel7cbb1052014-10-02 14:08:32 -03001752 INIT_LIST_HEAD(&ctx->buffer_meta_list);
Philipp Zabel47f3fa62015-07-09 07:10:21 -03001753 spin_lock_init(&ctx->buffer_meta_lock);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001754
Javier Martin186b2502012-07-26 05:53:35 -03001755 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001756 list_add(&ctx->list, &dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03001757 coda_unlock(ctx);
1758
Javier Martin186b2502012-07-26 05:53:35 -03001759 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
1760 ctx->idx, ctx);
1761
1762 return 0;
1763
Fabio Estevamf82bc202013-08-21 11:14:16 -03001764err_ctrls_setup:
Philipp Zabel14604e32014-07-11 06:36:22 -03001765 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Fabio Estevamf82bc202013-08-21 11:14:16 -03001766err_ctx_init:
1767 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03001768err_clk_ahb:
Fabio Estevamf82bc202013-08-21 11:14:16 -03001769 clk_disable_unprepare(dev->clk_per);
Fabio Estevam79830db2013-08-21 11:14:17 -03001770err_clk_per:
Philipp Zabel1e172732014-07-11 06:36:23 -03001771 pm_runtime_put_sync(&dev->plat_dev->dev);
1772err_pm_get:
Javier Martin186b2502012-07-26 05:53:35 -03001773 v4l2_fh_del(&ctx->fh);
1774 v4l2_fh_exit(&ctx->fh);
Fabio Estevamf82bc202013-08-21 11:14:16 -03001775 clear_bit(ctx->idx, &dev->instance_mask);
Peter Seiderera7f933a2015-03-24 14:30:47 -03001776err_coda_name_init:
Fabio Estevamf82bc202013-08-21 11:14:16 -03001777err_coda_max:
Javier Martin186b2502012-07-26 05:53:35 -03001778 kfree(ctx);
1779 return ret;
1780}
1781
1782static int coda_release(struct file *file)
1783{
1784 struct coda_dev *dev = video_drvdata(file);
1785 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
1786
1787 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
1788 ctx);
1789
Philipp Zabela22496c2015-01-23 13:51:33 -03001790 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
Philipp Zabeld4bb75f2014-10-08 13:09:11 -03001791 coda_bit_stream_end_flag(ctx);
1792
Philipp Zabel918c66f2013-06-27 06:59:01 -03001793 /* If this instance is running, call .job_abort and wait for it to end */
Philipp Zabel14604e32014-07-11 06:36:22 -03001794 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001795
1796 /* In case the instance was not running, we still need to call SEQ_END */
Philipp Zabel5c718bb2015-07-09 07:10:16 -03001797 if (ctx->ops->seq_end_work) {
Philipp Zabel32b6f202014-07-11 06:36:20 -03001798 queue_work(dev->workqueue, &ctx->seq_end_work);
1799 flush_work(&ctx->seq_end_work);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001800 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001801
Javier Martin186b2502012-07-26 05:53:35 -03001802 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001803 list_del(&ctx->list);
Javier Martin186b2502012-07-26 05:53:35 -03001804 coda_unlock(ctx);
1805
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001806 if (ctx->dev->devtype->product == CODA_DX6)
1807 coda_free_aux_buf(dev, &ctx->workbuf);
1808
Javier Martin186b2502012-07-26 05:53:35 -03001809 v4l2_ctrl_handler_free(&ctx->ctrls);
Javier Martin186b2502012-07-26 05:53:35 -03001810 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevamf82bc202013-08-21 11:14:16 -03001811 clk_disable_unprepare(dev->clk_per);
Philipp Zabel1e172732014-07-11 06:36:23 -03001812 pm_runtime_put_sync(&dev->plat_dev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03001813 v4l2_fh_del(&ctx->fh);
1814 v4l2_fh_exit(&ctx->fh);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001815 clear_bit(ctx->idx, &dev->instance_mask);
Philipp Zabel58b76772014-07-23 12:28:43 -03001816 if (ctx->ops->release)
1817 ctx->ops->release(ctx);
Philipp Zabele1519e82015-01-23 13:51:17 -03001818 debugfs_remove_recursive(ctx->debugfs_entry);
Javier Martin186b2502012-07-26 05:53:35 -03001819 kfree(ctx);
1820
1821 return 0;
1822}
1823
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001824static const struct v4l2_file_operations coda_fops = {
Javier Martin186b2502012-07-26 05:53:35 -03001825 .owner = THIS_MODULE,
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001826 .open = coda_open,
Javier Martin186b2502012-07-26 05:53:35 -03001827 .release = coda_release,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001828 .poll = v4l2_m2m_fop_poll,
Javier Martin186b2502012-07-26 05:53:35 -03001829 .unlocked_ioctl = video_ioctl2,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001830 .mmap = v4l2_m2m_fop_mmap,
Javier Martin186b2502012-07-26 05:53:35 -03001831};
1832
Philipp Zabel87048bb2012-07-02 07:03:43 -03001833static int coda_hw_init(struct coda_dev *dev)
Javier Martin186b2502012-07-26 05:53:35 -03001834{
Javier Martin186b2502012-07-26 05:53:35 -03001835 u32 data;
1836 u16 *p;
Fabio Estevam79830db2013-08-21 11:14:17 -03001837 int i, ret;
Javier Martin186b2502012-07-26 05:53:35 -03001838
Fabio Estevam79830db2013-08-21 11:14:17 -03001839 ret = clk_prepare_enable(dev->clk_per);
1840 if (ret)
Philipp Zabel1e172732014-07-11 06:36:23 -03001841 goto err_clk_per;
Fabio Estevam79830db2013-08-21 11:14:17 -03001842
1843 ret = clk_prepare_enable(dev->clk_ahb);
1844 if (ret)
1845 goto err_clk_ahb;
Javier Martin186b2502012-07-26 05:53:35 -03001846
Philipp Zabel8f452842014-07-11 06:36:35 -03001847 if (dev->rstc)
1848 reset_control_reset(dev->rstc);
1849
Javier Martin186b2502012-07-26 05:53:35 -03001850 /*
1851 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
Philipp Zabel87048bb2012-07-02 07:03:43 -03001852 * The 16-bit chars in the code buffer are in memory access
1853 * order, re-sort them to CODA order for register download.
Javier Martin186b2502012-07-26 05:53:35 -03001854 * Data in this SRAM survives a reboot.
1855 */
Philipp Zabel87048bb2012-07-02 07:03:43 -03001856 p = (u16 *)dev->codebuf.vaddr;
1857 if (dev->devtype->product == CODA_DX6) {
1858 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
1859 data = CODA_DOWN_ADDRESS_SET(i) |
1860 CODA_DOWN_DATA_SET(p[i ^ 1]);
1861 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
1862 }
1863 } else {
1864 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
1865 data = CODA_DOWN_ADDRESS_SET(i) |
1866 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
1867 3 - (i % 4)]);
1868 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
1869 }
Javier Martin186b2502012-07-26 05:53:35 -03001870 }
Javier Martin186b2502012-07-26 05:53:35 -03001871
Philipp Zabel9acf7692013-05-23 10:42:56 -03001872 /* Clear registers */
1873 for (i = 0; i < 64; i++)
1874 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
1875
Javier Martin186b2502012-07-26 05:53:35 -03001876 /* Tell the BIT where to find everything it needs */
Philipp Zabel89548442014-07-11 06:36:17 -03001877 if (dev->devtype->product == CODA_960 ||
1878 dev->devtype->product == CODA_7541) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001879 coda_write(dev, dev->tempbuf.paddr,
1880 CODA_REG_BIT_TEMP_BUF_ADDR);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001881 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001882 } else {
1883 coda_write(dev, dev->workbuf.paddr,
1884 CODA_REG_BIT_WORK_BUF_ADDR);
1885 }
Javier Martin186b2502012-07-26 05:53:35 -03001886 coda_write(dev, dev->codebuf.paddr,
1887 CODA_REG_BIT_CODE_BUF_ADDR);
1888 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
1889
1890 /* Set default values */
1891 switch (dev->devtype->product) {
1892 case CODA_DX6:
Philipp Zabelf23797b2014-08-06 08:02:23 -03001893 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH,
1894 CODA_REG_BIT_STREAM_CTRL);
Javier Martin186b2502012-07-26 05:53:35 -03001895 break;
1896 default:
Philipp Zabelf23797b2014-08-06 08:02:23 -03001897 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH,
1898 CODA_REG_BIT_STREAM_CTRL);
Javier Martin186b2502012-07-26 05:53:35 -03001899 }
Philipp Zabel89548442014-07-11 06:36:17 -03001900 if (dev->devtype->product == CODA_960)
1901 coda_write(dev, 1 << 12, CODA_REG_BIT_FRAME_MEM_CTRL);
1902 else
1903 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel10436672012-07-02 09:03:55 -03001904
1905 if (dev->devtype->product != CODA_DX6)
1906 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
1907
Javier Martin186b2502012-07-26 05:53:35 -03001908 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
1909 CODA_REG_BIT_INT_ENABLE);
1910
1911 /* Reset VPU and start processor */
1912 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
1913 data |= CODA_REG_RESET_ENABLE;
1914 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
1915 udelay(10);
1916 data &= ~CODA_REG_RESET_ENABLE;
1917 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
1918 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
1919
Philipp Zabelfe7554e2014-07-11 06:36:24 -03001920 clk_disable_unprepare(dev->clk_ahb);
1921 clk_disable_unprepare(dev->clk_per);
1922
1923 return 0;
1924
1925err_clk_ahb:
1926 clk_disable_unprepare(dev->clk_per);
1927err_clk_per:
1928 return ret;
1929}
1930
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001931static int coda_register_device(struct coda_dev *dev, int i)
Philipp Zabel121cacf2014-07-18 07:22:42 -03001932{
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001933 struct video_device *vfd = &dev->vfd[i];
1934
Dan Carpenter88ca3af2015-01-08 07:07:08 -03001935 if (i >= dev->devtype->num_vdevs)
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001936 return -EINVAL;
1937
Philipp Zabelbf249da2015-03-24 14:30:50 -03001938 strlcpy(vfd->name, dev->devtype->vdevs[i]->name, sizeof(vfd->name));
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001939 vfd->fops = &coda_fops;
1940 vfd->ioctl_ops = &coda_ioctl_ops;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001941 vfd->release = video_device_release_empty,
1942 vfd->lock = &dev->dev_mutex;
1943 vfd->v4l2_dev = &dev->v4l2_dev;
1944 vfd->vfl_dir = VFL_DIR_M2M;
1945 video_set_drvdata(vfd, dev);
1946
Philipp Zabela188a662014-08-05 14:00:20 -03001947 /* Not applicable, use the selection API instead */
1948 v4l2_disable_ioctl(vfd, VIDIOC_CROPCAP);
1949 v4l2_disable_ioctl(vfd, VIDIOC_G_CROP);
1950 v4l2_disable_ioctl(vfd, VIDIOC_S_CROP);
1951
Philipp Zabel121cacf2014-07-18 07:22:42 -03001952 return video_register_device(vfd, VFL_TYPE_GRABBER, 0);
1953}
1954
Javier Martin186b2502012-07-26 05:53:35 -03001955static void coda_fw_callback(const struct firmware *fw, void *context)
1956{
1957 struct coda_dev *dev = context;
1958 struct platform_device *pdev = dev->plat_dev;
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001959 int i, ret;
Javier Martin186b2502012-07-26 05:53:35 -03001960
1961 if (!fw) {
1962 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03001963 goto put_pm;
Javier Martin186b2502012-07-26 05:53:35 -03001964 }
1965
1966 /* allocate auxiliary per-device code buffer for the BIT processor */
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001967 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size, "codebuf",
1968 dev->debugfs_root);
Philipp Zabel6ba53b82015-03-24 14:30:54 -03001969 if (ret < 0)
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03001970 goto put_pm;
Javier Martin186b2502012-07-26 05:53:35 -03001971
Philipp Zabel87048bb2012-07-02 07:03:43 -03001972 /* Copy the whole firmware image to the code buffer */
1973 memcpy(dev->codebuf.vaddr, fw->data, fw->size);
1974 release_firmware(fw);
1975
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03001976 ret = coda_hw_init(dev);
1977 if (ret < 0) {
1978 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
1979 goto put_pm;
Javier Martin186b2502012-07-26 05:53:35 -03001980 }
1981
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03001982 ret = coda_check_firmware(dev);
1983 if (ret < 0)
1984 goto put_pm;
1985
Javier Martin186b2502012-07-26 05:53:35 -03001986 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
1987 if (IS_ERR(dev->alloc_ctx)) {
1988 v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03001989 goto put_pm;
Javier Martin186b2502012-07-26 05:53:35 -03001990 }
1991
1992 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
1993 if (IS_ERR(dev->m2m_dev)) {
1994 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
1995 goto rel_ctx;
1996 }
1997
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001998 for (i = 0; i < dev->devtype->num_vdevs; i++) {
1999 ret = coda_register_device(dev, i);
2000 if (ret) {
2001 v4l2_err(&dev->v4l2_dev,
2002 "Failed to register %s video device: %d\n",
2003 dev->devtype->vdevs[i]->name, ret);
2004 goto rel_vfd;
2005 }
Philipp Zabel121cacf2014-07-18 07:22:42 -03002006 }
2007
2008 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video[%d-%d]\n",
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002009 dev->vfd[0].num, dev->vfd[i - 1].num);
Javier Martin186b2502012-07-26 05:53:35 -03002010
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03002011 pm_runtime_put_sync(&pdev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03002012 return;
2013
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002014rel_vfd:
2015 while (--i >= 0)
2016 video_unregister_device(&dev->vfd[i]);
Javier Martin186b2502012-07-26 05:53:35 -03002017 v4l2_m2m_release(dev->m2m_dev);
2018rel_ctx:
2019 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03002020put_pm:
2021 pm_runtime_put_sync(&pdev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03002022}
2023
2024static int coda_firmware_request(struct coda_dev *dev)
2025{
2026 char *fw = dev->devtype->firmware;
2027
2028 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
2029 coda_product_name(dev->devtype->product));
2030
2031 return request_firmware_nowait(THIS_MODULE, true,
2032 fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback);
2033}
2034
2035enum coda_platform {
2036 CODA_IMX27,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03002037 CODA_IMX53,
Philipp Zabel89548442014-07-11 06:36:17 -03002038 CODA_IMX6Q,
2039 CODA_IMX6DL,
Javier Martin186b2502012-07-26 05:53:35 -03002040};
2041
Emil Goodec06d8752012-08-14 17:44:42 -03002042static const struct coda_devtype coda_devdata[] = {
Javier Martin186b2502012-07-26 05:53:35 -03002043 [CODA_IMX27] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002044 .firmware = "v4l-codadx6-imx27.bin",
2045 .product = CODA_DX6,
2046 .codecs = codadx6_codecs,
2047 .num_codecs = ARRAY_SIZE(codadx6_codecs),
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002048 .vdevs = codadx6_video_devices,
2049 .num_vdevs = ARRAY_SIZE(codadx6_video_devices),
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002050 .workbuf_size = 288 * 1024 + FMO_SLICE_SAVE_BUF_SIZE * 8 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03002051 .iram_size = 0xb000,
Javier Martin186b2502012-07-26 05:53:35 -03002052 },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03002053 [CODA_IMX53] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002054 .firmware = "v4l-coda7541-imx53.bin",
2055 .product = CODA_7541,
2056 .codecs = coda7_codecs,
2057 .num_codecs = ARRAY_SIZE(coda7_codecs),
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002058 .vdevs = coda7_video_devices,
2059 .num_vdevs = ARRAY_SIZE(coda7_video_devices),
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002060 .workbuf_size = 128 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03002061 .tempbuf_size = 304 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03002062 .iram_size = 0x14000,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03002063 },
Philipp Zabel89548442014-07-11 06:36:17 -03002064 [CODA_IMX6Q] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002065 .firmware = "v4l-coda960-imx6q.bin",
2066 .product = CODA_960,
2067 .codecs = coda9_codecs,
2068 .num_codecs = ARRAY_SIZE(coda9_codecs),
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002069 .vdevs = coda9_video_devices,
2070 .num_vdevs = ARRAY_SIZE(coda9_video_devices),
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002071 .workbuf_size = 80 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03002072 .tempbuf_size = 204 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03002073 .iram_size = 0x21000,
Philipp Zabel89548442014-07-11 06:36:17 -03002074 },
2075 [CODA_IMX6DL] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002076 .firmware = "v4l-coda960-imx6dl.bin",
2077 .product = CODA_960,
2078 .codecs = coda9_codecs,
2079 .num_codecs = ARRAY_SIZE(coda9_codecs),
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002080 .vdevs = coda9_video_devices,
2081 .num_vdevs = ARRAY_SIZE(coda9_video_devices),
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002082 .workbuf_size = 80 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03002083 .tempbuf_size = 204 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03002084 .iram_size = 0x20000,
Philipp Zabel89548442014-07-11 06:36:17 -03002085 },
Javier Martin186b2502012-07-26 05:53:35 -03002086};
2087
2088static struct platform_device_id coda_platform_ids[] = {
2089 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
2090 { /* sentinel */ }
2091};
2092MODULE_DEVICE_TABLE(platform, coda_platform_ids);
2093
2094#ifdef CONFIG_OF
2095static const struct of_device_id coda_dt_ids[] = {
Alexander Shiyan7b0dd9e2013-06-15 08:09:57 -03002096 { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03002097 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
Philipp Zabel89548442014-07-11 06:36:17 -03002098 { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
2099 { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
Javier Martin186b2502012-07-26 05:53:35 -03002100 { /* sentinel */ }
2101};
2102MODULE_DEVICE_TABLE(of, coda_dt_ids);
2103#endif
2104
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08002105static int coda_probe(struct platform_device *pdev)
Javier Martin186b2502012-07-26 05:53:35 -03002106{
2107 const struct of_device_id *of_id =
2108 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
2109 const struct platform_device_id *pdev_id;
Philipp Zabel657eee72013-04-29 16:17:14 -07002110 struct coda_platform_data *pdata = pdev->dev.platform_data;
2111 struct device_node *np = pdev->dev.of_node;
2112 struct gen_pool *pool;
Javier Martin186b2502012-07-26 05:53:35 -03002113 struct coda_dev *dev;
2114 struct resource *res;
2115 int ret, irq;
2116
Philipp Zabelf23797b2014-08-06 08:02:23 -03002117 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
Philipp Zabel6da999d2014-09-29 09:53:43 -03002118 if (!dev)
Javier Martin186b2502012-07-26 05:53:35 -03002119 return -ENOMEM;
Javier Martin186b2502012-07-26 05:53:35 -03002120
Philipp Zabelb2f91ae2014-10-02 14:08:27 -03002121 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
2122
Fabio Estevamb7bd6602014-10-04 16:40:50 -03002123 if (of_id) {
Philipp Zabelb2f91ae2014-10-02 14:08:27 -03002124 dev->devtype = of_id->data;
Fabio Estevamb7bd6602014-10-04 16:40:50 -03002125 } else if (pdev_id) {
Philipp Zabelb2f91ae2014-10-02 14:08:27 -03002126 dev->devtype = &coda_devdata[pdev_id->driver_data];
Fabio Estevamb7bd6602014-10-04 16:40:50 -03002127 } else {
2128 ret = -EINVAL;
2129 goto err_v4l2_register;
Javier Martin186b2502012-07-26 05:53:35 -03002130 }
2131
2132 spin_lock_init(&dev->irqlock);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002133 INIT_LIST_HEAD(&dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03002134
2135 dev->plat_dev = pdev;
2136 dev->clk_per = devm_clk_get(&pdev->dev, "per");
2137 if (IS_ERR(dev->clk_per)) {
2138 dev_err(&pdev->dev, "Could not get per clock\n");
2139 return PTR_ERR(dev->clk_per);
2140 }
2141
2142 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
2143 if (IS_ERR(dev->clk_ahb)) {
2144 dev_err(&pdev->dev, "Could not get ahb clock\n");
2145 return PTR_ERR(dev->clk_ahb);
2146 }
2147
2148 /* Get memory for physical registers */
2149 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Philipp Zabel611cbbf2013-05-21 04:19:27 -03002150 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
2151 if (IS_ERR(dev->regs_base))
2152 return PTR_ERR(dev->regs_base);
Javier Martin186b2502012-07-26 05:53:35 -03002153
2154 /* IRQ */
Philipp Zabel540b72e2014-08-05 14:00:09 -03002155 irq = platform_get_irq_byname(pdev, "bit");
2156 if (irq < 0)
2157 irq = platform_get_irq(pdev, 0);
Javier Martin186b2502012-07-26 05:53:35 -03002158 if (irq < 0) {
2159 dev_err(&pdev->dev, "failed to get irq resource\n");
Fabio Estevam7d377432014-06-04 15:46:23 -03002160 return irq;
Javier Martin186b2502012-07-26 05:53:35 -03002161 }
2162
Fabio Estevam08c8d142014-06-04 15:46:24 -03002163 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
2164 IRQF_ONESHOT, dev_name(&pdev->dev), dev);
2165 if (ret < 0) {
2166 dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
2167 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03002168 }
2169
Philipp Zabelee27aa92014-07-24 16:50:03 -03002170 dev->rstc = devm_reset_control_get_optional(&pdev->dev, NULL);
Philipp Zabel8f452842014-07-11 06:36:35 -03002171 if (IS_ERR(dev->rstc)) {
2172 ret = PTR_ERR(dev->rstc);
Philipp Zabelee27aa92014-07-24 16:50:03 -03002173 if (ret == -ENOENT || ret == -ENOSYS) {
Philipp Zabel8f452842014-07-11 06:36:35 -03002174 dev->rstc = NULL;
2175 } else {
Philipp Zabelf23797b2014-08-06 08:02:23 -03002176 dev_err(&pdev->dev, "failed get reset control: %d\n",
2177 ret);
Philipp Zabel8f452842014-07-11 06:36:35 -03002178 return ret;
2179 }
2180 }
2181
Philipp Zabel657eee72013-04-29 16:17:14 -07002182 /* Get IRAM pool from device tree or platform data */
Vladimir Zapolskiyabdd4a72015-06-30 15:00:07 -07002183 pool = of_gen_pool_get(np, "iram", 0);
Philipp Zabel657eee72013-04-29 16:17:14 -07002184 if (!pool && pdata)
Vladimir Zapolskiy73858172015-09-04 15:47:43 -07002185 pool = gen_pool_get(pdata->iram_dev, NULL);
Philipp Zabel657eee72013-04-29 16:17:14 -07002186 if (!pool) {
2187 dev_err(&pdev->dev, "iram pool not available\n");
2188 return -ENOMEM;
2189 }
2190 dev->iram_pool = pool;
2191
Javier Martin186b2502012-07-26 05:53:35 -03002192 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
2193 if (ret)
2194 return ret;
2195
2196 mutex_init(&dev->dev_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002197 mutex_init(&dev->coda_mutex);
Javier Martin186b2502012-07-26 05:53:35 -03002198
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002199 dev->debugfs_root = debugfs_create_dir("coda", NULL);
2200 if (!dev->debugfs_root)
2201 dev_warn(&pdev->dev, "failed to create debugfs root\n");
2202
Javier Martin186b2502012-07-26 05:53:35 -03002203 /* allocate auxiliary per-device buffers for the BIT processor */
Philipp Zabel5d73fad2014-07-11 06:36:42 -03002204 if (dev->devtype->product == CODA_DX6) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002205 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002206 dev->devtype->workbuf_size, "workbuf",
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002207 dev->debugfs_root);
Philipp Zabel6ba53b82015-03-24 14:30:54 -03002208 if (ret < 0)
Fabio Estevamb7bd6602014-10-04 16:40:50 -03002209 goto err_v4l2_register;
Javier Martin186b2502012-07-26 05:53:35 -03002210 }
Philipp Zabel5d73fad2014-07-11 06:36:42 -03002211
2212 if (dev->devtype->tempbuf_size) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002213 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03002214 dev->devtype->tempbuf_size, "tempbuf",
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002215 dev->debugfs_root);
Philipp Zabel6ba53b82015-03-24 14:30:54 -03002216 if (ret < 0)
Fabio Estevamb7bd6602014-10-04 16:40:50 -03002217 goto err_v4l2_register;
Javier Martin186b2502012-07-26 05:53:35 -03002218 }
2219
Philipp Zabel401e9722014-07-11 06:36:43 -03002220 dev->iram.size = dev->devtype->iram_size;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03002221 dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
2222 &dev->iram.paddr);
2223 if (!dev->iram.vaddr) {
Philipp Zabel8be31c82014-08-05 14:00:13 -03002224 dev_warn(&pdev->dev, "unable to alloc iram\n");
2225 } else {
Philipp Zabel811a6932015-01-23 13:51:23 -03002226 memset(dev->iram.vaddr, 0, dev->iram.size);
Philipp Zabel8be31c82014-08-05 14:00:13 -03002227 dev->iram.blob.data = dev->iram.vaddr;
2228 dev->iram.blob.size = dev->iram.size;
2229 dev->iram.dentry = debugfs_create_blob("iram", 0644,
2230 dev->debugfs_root,
2231 &dev->iram.blob);
Philipp Zabel10436672012-07-02 09:03:55 -03002232 }
2233
Philipp Zabel32b6f202014-07-11 06:36:20 -03002234 dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
2235 if (!dev->workqueue) {
2236 dev_err(&pdev->dev, "unable to alloc workqueue\n");
Fabio Estevam74d08d52014-10-04 16:40:51 -03002237 ret = -ENOMEM;
2238 goto err_v4l2_register;
Philipp Zabel32b6f202014-07-11 06:36:20 -03002239 }
2240
Javier Martin186b2502012-07-26 05:53:35 -03002241 platform_set_drvdata(pdev, dev);
2242
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03002243 /*
2244 * Start activated so we can directly call coda_hw_init in
Rafael J. Wysockie243c7c2014-12-04 01:10:10 +01002245 * coda_fw_callback regardless of whether CONFIG_PM is
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03002246 * enabled or whether the device is associated with a PM domain.
2247 */
2248 pm_runtime_get_noresume(&pdev->dev);
2249 pm_runtime_set_active(&pdev->dev);
Philipp Zabel1e172732014-07-11 06:36:23 -03002250 pm_runtime_enable(&pdev->dev);
2251
Javier Martin186b2502012-07-26 05:53:35 -03002252 return coda_firmware_request(dev);
Fabio Estevamb7bd6602014-10-04 16:40:50 -03002253
2254err_v4l2_register:
2255 v4l2_device_unregister(&dev->v4l2_dev);
2256 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03002257}
2258
2259static int coda_remove(struct platform_device *pdev)
2260{
2261 struct coda_dev *dev = platform_get_drvdata(pdev);
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002262 int i;
Javier Martin186b2502012-07-26 05:53:35 -03002263
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002264 for (i = 0; i < ARRAY_SIZE(dev->vfd); i++) {
2265 if (video_get_drvdata(&dev->vfd[i]))
2266 video_unregister_device(&dev->vfd[i]);
2267 }
Javier Martin186b2502012-07-26 05:53:35 -03002268 if (dev->m2m_dev)
2269 v4l2_m2m_release(dev->m2m_dev);
Philipp Zabel1e172732014-07-11 06:36:23 -03002270 pm_runtime_disable(&pdev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03002271 if (dev->alloc_ctx)
2272 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
2273 v4l2_device_unregister(&dev->v4l2_dev);
Philipp Zabel32b6f202014-07-11 06:36:20 -03002274 destroy_workqueue(dev->workqueue);
Philipp Zabelb313bcc2014-07-11 06:36:16 -03002275 if (dev->iram.vaddr)
2276 gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
2277 dev->iram.size);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002278 coda_free_aux_buf(dev, &dev->codebuf);
2279 coda_free_aux_buf(dev, &dev->tempbuf);
2280 coda_free_aux_buf(dev, &dev->workbuf);
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002281 debugfs_remove_recursive(dev->debugfs_root);
Javier Martin186b2502012-07-26 05:53:35 -03002282 return 0;
2283}
2284
Rafael J. Wysockie243c7c2014-12-04 01:10:10 +01002285#ifdef CONFIG_PM
Philipp Zabel1e172732014-07-11 06:36:23 -03002286static int coda_runtime_resume(struct device *dev)
2287{
2288 struct coda_dev *cdev = dev_get_drvdata(dev);
2289 int ret = 0;
2290
Philipp Zabel65919e62014-07-18 07:22:36 -03002291 if (dev->pm_domain && cdev->codebuf.vaddr) {
Philipp Zabel1e172732014-07-11 06:36:23 -03002292 ret = coda_hw_init(cdev);
2293 if (ret)
2294 v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
2295 }
2296
2297 return ret;
2298}
2299#endif
2300
2301static const struct dev_pm_ops coda_pm_ops = {
2302 SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
2303};
2304
Javier Martin186b2502012-07-26 05:53:35 -03002305static struct platform_driver coda_driver = {
2306 .probe = coda_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08002307 .remove = coda_remove,
Javier Martin186b2502012-07-26 05:53:35 -03002308 .driver = {
2309 .name = CODA_NAME,
Javier Martin186b2502012-07-26 05:53:35 -03002310 .of_match_table = of_match_ptr(coda_dt_ids),
Philipp Zabel1e172732014-07-11 06:36:23 -03002311 .pm = &coda_pm_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002312 },
2313 .id_table = coda_platform_ids,
2314};
2315
2316module_platform_driver(coda_driver);
2317
2318MODULE_LICENSE("GPL");
2319MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
2320MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");