blob: 2d782ce94a67bfec8be38d254559adebecad9c6b [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),
Philipp Zabel95847f42015-12-02 14:58:54 -0200134 CODA_CODEC(CODA7_MODE_DECODE_MP2, V4L2_PIX_FMT_MPEG2, V4L2_PIX_FMT_YUV420, 1920, 1088),
Philipp Zabelb0ed05b2014-08-05 14:00:14 -0300135 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088),
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300136 CODA_CODEC(CODA7_MODE_DECODE_MJPG, V4L2_PIX_FMT_JPEG, V4L2_PIX_FMT_YUV420, 8192, 8192),
Philipp Zabelb96904e2013-05-23 10:42:58 -0300137};
138
Philipp Zabel814c3762014-07-18 07:22:45 -0300139static const struct coda_codec coda9_codecs[] = {
Philipp Zabelb0ed05b2014-08-05 14:00:14 -0300140 CODA_CODEC(CODA9_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1920, 1088),
141 CODA_CODEC(CODA9_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1920, 1088),
142 CODA_CODEC(CODA9_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
Philipp Zabel95847f42015-12-02 14:58:54 -0200143 CODA_CODEC(CODA9_MODE_DECODE_MP2, V4L2_PIX_FMT_MPEG2, V4L2_PIX_FMT_YUV420, 1920, 1088),
Philipp Zabelb0ed05b2014-08-05 14:00:14 -0300144 CODA_CODEC(CODA9_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088),
Philipp Zabel89548442014-07-11 06:36:17 -0300145};
146
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300147struct coda_video_device {
148 const char *name;
149 enum coda_inst_type type;
150 const struct coda_context_ops *ops;
Philipp Zabela22496c2015-01-23 13:51:33 -0300151 bool direct;
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300152 u32 src_formats[CODA_MAX_FORMATS];
153 u32 dst_formats[CODA_MAX_FORMATS];
154};
155
156static const struct coda_video_device coda_bit_encoder = {
157 .name = "coda-encoder",
158 .type = CODA_INST_ENCODER,
159 .ops = &coda_bit_encode_ops,
160 .src_formats = {
Philipp Zabel6727d4f2015-07-16 13:19:39 -0300161 V4L2_PIX_FMT_NV12,
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300162 V4L2_PIX_FMT_YUV420,
163 V4L2_PIX_FMT_YVU420,
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300164 },
165 .dst_formats = {
166 V4L2_PIX_FMT_H264,
167 V4L2_PIX_FMT_MPEG4,
168 },
169};
170
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300171static const struct coda_video_device coda_bit_jpeg_encoder = {
172 .name = "coda-jpeg-encoder",
173 .type = CODA_INST_ENCODER,
174 .ops = &coda_bit_encode_ops,
175 .src_formats = {
Philipp Zabel6727d4f2015-07-16 13:19:39 -0300176 V4L2_PIX_FMT_NV12,
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300177 V4L2_PIX_FMT_YUV420,
178 V4L2_PIX_FMT_YVU420,
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300179 V4L2_PIX_FMT_YUV422P,
180 },
181 .dst_formats = {
182 V4L2_PIX_FMT_JPEG,
183 },
184};
185
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300186static const struct coda_video_device coda_bit_decoder = {
187 .name = "coda-decoder",
188 .type = CODA_INST_DECODER,
189 .ops = &coda_bit_decode_ops,
190 .src_formats = {
191 V4L2_PIX_FMT_H264,
Philipp Zabel95847f42015-12-02 14:58:54 -0200192 V4L2_PIX_FMT_MPEG2,
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300193 V4L2_PIX_FMT_MPEG4,
194 },
195 .dst_formats = {
Philipp Zabel6727d4f2015-07-16 13:19:39 -0300196 V4L2_PIX_FMT_NV12,
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300197 V4L2_PIX_FMT_YUV420,
198 V4L2_PIX_FMT_YVU420,
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300199 },
200};
201
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300202static const struct coda_video_device coda_bit_jpeg_decoder = {
203 .name = "coda-jpeg-decoder",
204 .type = CODA_INST_DECODER,
205 .ops = &coda_bit_decode_ops,
206 .src_formats = {
207 V4L2_PIX_FMT_JPEG,
208 },
209 .dst_formats = {
Philipp Zabel6727d4f2015-07-16 13:19:39 -0300210 V4L2_PIX_FMT_NV12,
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300211 V4L2_PIX_FMT_YUV420,
212 V4L2_PIX_FMT_YVU420,
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300213 V4L2_PIX_FMT_YUV422P,
214 },
215};
216
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300217static const struct coda_video_device *codadx6_video_devices[] = {
218 &coda_bit_encoder,
219};
220
221static const struct coda_video_device *coda7_video_devices[] = {
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300222 &coda_bit_jpeg_encoder,
223 &coda_bit_jpeg_decoder,
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300224 &coda_bit_encoder,
225 &coda_bit_decoder,
226};
227
228static const struct coda_video_device *coda9_video_devices[] = {
229 &coda_bit_encoder,
230 &coda_bit_decoder,
231};
232
Philipp Zabelb96904e2013-05-23 10:42:58 -0300233/*
234 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
235 * tables.
236 */
237static u32 coda_format_normalize_yuv(u32 fourcc)
238{
Philipp Zabel8076c7e2015-07-09 07:10:18 -0300239 switch (fourcc) {
Philipp Zabel6727d4f2015-07-16 13:19:39 -0300240 case V4L2_PIX_FMT_NV12:
Philipp Zabel8076c7e2015-07-09 07:10:18 -0300241 case V4L2_PIX_FMT_YUV420:
242 case V4L2_PIX_FMT_YVU420:
Philipp Zabel8076c7e2015-07-09 07:10:18 -0300243 case V4L2_PIX_FMT_YUV422P:
244 return V4L2_PIX_FMT_YUV420;
245 default:
246 return fourcc;
247 }
Philipp Zabelb96904e2013-05-23 10:42:58 -0300248}
249
Philipp Zabel814c3762014-07-18 07:22:45 -0300250static const struct coda_codec *coda_find_codec(struct coda_dev *dev,
251 int src_fourcc, int dst_fourcc)
Philipp Zabelb96904e2013-05-23 10:42:58 -0300252{
Philipp Zabel814c3762014-07-18 07:22:45 -0300253 const struct coda_codec *codecs = dev->devtype->codecs;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300254 int num_codecs = dev->devtype->num_codecs;
255 int k;
256
257 src_fourcc = coda_format_normalize_yuv(src_fourcc);
258 dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
259 if (src_fourcc == dst_fourcc)
260 return NULL;
261
262 for (k = 0; k < num_codecs; k++) {
263 if (codecs[k].src_fourcc == src_fourcc &&
264 codecs[k].dst_fourcc == dst_fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300265 break;
266 }
267
Philipp Zabelb96904e2013-05-23 10:42:58 -0300268 if (k == num_codecs)
Javier Martin186b2502012-07-26 05:53:35 -0300269 return NULL;
270
Philipp Zabelb96904e2013-05-23 10:42:58 -0300271 return &codecs[k];
Javier Martin186b2502012-07-26 05:53:35 -0300272}
273
Philipp Zabel57625592013-09-30 10:34:51 -0300274static void coda_get_max_dimensions(struct coda_dev *dev,
Philipp Zabel814c3762014-07-18 07:22:45 -0300275 const struct coda_codec *codec,
Philipp Zabel57625592013-09-30 10:34:51 -0300276 int *max_w, int *max_h)
277{
Philipp Zabel814c3762014-07-18 07:22:45 -0300278 const struct coda_codec *codecs = dev->devtype->codecs;
Philipp Zabel57625592013-09-30 10:34:51 -0300279 int num_codecs = dev->devtype->num_codecs;
280 unsigned int w, h;
281 int k;
282
283 if (codec) {
284 w = codec->max_w;
285 h = codec->max_h;
286 } else {
287 for (k = 0, w = 0, h = 0; k < num_codecs; k++) {
288 w = max(w, codecs[k].max_w);
289 h = max(h, codecs[k].max_h);
290 }
291 }
292
293 if (max_w)
294 *max_w = w;
295 if (max_h)
296 *max_h = h;
297}
298
Philipp Zabel55425702015-12-02 14:58:50 -0200299static const struct coda_video_device *to_coda_video_device(struct video_device
300 *vdev)
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300301{
302 struct coda_dev *dev = video_get_drvdata(vdev);
303 unsigned int i = vdev - dev->vfd;
304
305 if (i >= dev->devtype->num_vdevs)
306 return NULL;
307
308 return dev->devtype->vdevs[i];
309}
310
Philipp Zabel79924ca2014-07-23 12:28:45 -0300311const char *coda_product_name(int product)
Philipp Zabel927933f2013-09-30 10:34:48 -0300312{
313 static char buf[9];
314
315 switch (product) {
316 case CODA_DX6:
317 return "CodaDx6";
318 case CODA_7541:
319 return "CODA7541";
Philipp Zabel89548442014-07-11 06:36:17 -0300320 case CODA_960:
321 return "CODA960";
Philipp Zabel927933f2013-09-30 10:34:48 -0300322 default:
323 snprintf(buf, sizeof(buf), "(0x%04x)", product);
324 return buf;
325 }
326}
327
Javier Martin186b2502012-07-26 05:53:35 -0300328/*
329 * V4L2 ioctl() operations.
330 */
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300331static int coda_querycap(struct file *file, void *priv,
332 struct v4l2_capability *cap)
Javier Martin186b2502012-07-26 05:53:35 -0300333{
Philipp Zabel927933f2013-09-30 10:34:48 -0300334 struct coda_ctx *ctx = fh_to_ctx(priv);
335
Javier Martin186b2502012-07-26 05:53:35 -0300336 strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver));
Philipp Zabel927933f2013-09-30 10:34:48 -0300337 strlcpy(cap->card, coda_product_name(ctx->dev->devtype->product),
338 sizeof(cap->card));
Philipp Zabeldad0e1c2013-05-21 04:18:22 -0300339 strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
Philipp Zabel3898e7a2014-07-18 07:22:37 -0300340 cap->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
Javier Martin186b2502012-07-26 05:53:35 -0300341 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
342
343 return 0;
344}
345
Philipp Zabel22e244b2014-07-18 07:22:43 -0300346static int coda_enum_fmt(struct file *file, void *priv,
347 struct v4l2_fmtdesc *f)
Javier Martin186b2502012-07-26 05:53:35 -0300348{
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300349 struct video_device *vdev = video_devdata(file);
350 const struct coda_video_device *cvd = to_coda_video_device(vdev);
351 const u32 *formats;
Philipp Zabel22e244b2014-07-18 07:22:43 -0300352
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300353 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
354 formats = cvd->src_formats;
355 else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
356 formats = cvd->dst_formats;
Philipp Zabel22e244b2014-07-18 07:22:43 -0300357 else
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300358 return -EINVAL;
Javier Martin186b2502012-07-26 05:53:35 -0300359
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300360 if (f->index >= CODA_MAX_FORMATS || formats[f->index] == 0)
361 return -EINVAL;
Javier Martin186b2502012-07-26 05:53:35 -0300362
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300363 f->pixelformat = formats[f->index];
Javier Martin186b2502012-07-26 05:53:35 -0300364
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300365 return 0;
Javier Martin186b2502012-07-26 05:53:35 -0300366}
367
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300368static int coda_g_fmt(struct file *file, void *priv,
369 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300370{
Javier Martin186b2502012-07-26 05:53:35 -0300371 struct coda_q_data *q_data;
372 struct coda_ctx *ctx = fh_to_ctx(priv);
373
Javier Martin186b2502012-07-26 05:53:35 -0300374 q_data = get_q_data(ctx, f->type);
Philipp Zabelb9736292014-07-11 06:36:18 -0300375 if (!q_data)
376 return -EINVAL;
Javier Martin186b2502012-07-26 05:53:35 -0300377
378 f->fmt.pix.field = V4L2_FIELD_NONE;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300379 f->fmt.pix.pixelformat = q_data->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -0300380 f->fmt.pix.width = q_data->width;
381 f->fmt.pix.height = q_data->height;
Philipp Zabel2fd6a372014-07-11 06:36:36 -0300382 f->fmt.pix.bytesperline = q_data->bytesperline;
Javier Martin186b2502012-07-26 05:53:35 -0300383
384 f->fmt.pix.sizeimage = q_data->sizeimage;
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300385 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_JPEG)
386 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
387 else
388 f->fmt.pix.colorspace = ctx->colorspace;
Javier Martin186b2502012-07-26 05:53:35 -0300389
390 return 0;
391}
392
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300393static int coda_try_pixelformat(struct coda_ctx *ctx, struct v4l2_format *f)
394{
395 struct coda_q_data *q_data;
396 const u32 *formats;
397 int i;
398
399 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
400 formats = ctx->cvd->src_formats;
401 else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
402 formats = ctx->cvd->dst_formats;
403 else
404 return -EINVAL;
405
406 for (i = 0; i < CODA_MAX_FORMATS; i++) {
407 if (formats[i] == f->fmt.pix.pixelformat) {
408 f->fmt.pix.pixelformat = formats[i];
409 return 0;
410 }
411 }
412
413 /* Fall back to currently set pixelformat */
414 q_data = get_q_data(ctx, f->type);
415 f->fmt.pix.pixelformat = q_data->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -0300416
417 return 0;
418}
419
Philipp Zabel8e428d52015-01-23 13:51:29 -0300420static unsigned int coda_estimate_sizeimage(struct coda_ctx *ctx, u32 sizeimage,
421 u32 width, u32 height)
422{
423 /*
424 * This is a rough estimate for sensible compressed buffer
425 * sizes (between 1 and 16 bits per pixel). This could be
426 * improved by better format specific worst case estimates.
427 */
428 return round_up(clamp(sizeimage, width * height / 8,
429 width * height * 2), PAGE_SIZE);
430}
431
Philipp Zabel814c3762014-07-18 07:22:45 -0300432static int coda_try_fmt(struct coda_ctx *ctx, const struct coda_codec *codec,
Philipp Zabel57625592013-09-30 10:34:51 -0300433 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300434{
Philipp Zabel57625592013-09-30 10:34:51 -0300435 struct coda_dev *dev = ctx->dev;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300436 unsigned int max_w, max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300437 enum v4l2_field field;
438
439 field = f->fmt.pix.field;
440 if (field == V4L2_FIELD_ANY)
441 field = V4L2_FIELD_NONE;
442 else if (V4L2_FIELD_NONE != field)
443 return -EINVAL;
444
445 /* V4L2 specification suggests the driver corrects the format struct
446 * if any of the dimensions is unsupported */
447 f->fmt.pix.field = field;
448
Philipp Zabel57625592013-09-30 10:34:51 -0300449 coda_get_max_dimensions(dev, codec, &max_w, &max_h);
450 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN,
451 &f->fmt.pix.height, MIN_H, max_h, H_ALIGN,
452 S_ALIGN);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300453
Philipp Zabel57625592013-09-30 10:34:51 -0300454 switch (f->fmt.pix.pixelformat) {
Philipp Zabel6727d4f2015-07-16 13:19:39 -0300455 case V4L2_PIX_FMT_NV12:
Philipp Zabel57625592013-09-30 10:34:51 -0300456 case V4L2_PIX_FMT_YUV420:
457 case V4L2_PIX_FMT_YVU420:
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300458 /*
459 * Frame stride must be at least multiple of 8,
460 * but multiple of 16 for h.264 or JPEG 4:2:x
461 */
Philipp Zabel451dfe52014-07-11 06:36:39 -0300462 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
Philipp Zabel47cf0c62013-05-23 10:42:54 -0300463 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
Philipp Zabel451d43a2012-07-26 09:18:27 -0300464 f->fmt.pix.height * 3 / 2;
Philipp Zabel57625592013-09-30 10:34:51 -0300465 break;
Philipp Zabel4de69312014-10-02 14:08:26 -0300466 case V4L2_PIX_FMT_YUV422P:
467 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
468 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
469 f->fmt.pix.height * 2;
470 break;
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300471 case V4L2_PIX_FMT_JPEG:
472 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
473 /* fallthrough */
Philipp Zabel57625592013-09-30 10:34:51 -0300474 case V4L2_PIX_FMT_H264:
475 case V4L2_PIX_FMT_MPEG4:
Philipp Zabel95847f42015-12-02 14:58:54 -0200476 case V4L2_PIX_FMT_MPEG2:
Javier Martin186b2502012-07-26 05:53:35 -0300477 f->fmt.pix.bytesperline = 0;
Philipp Zabel8e428d52015-01-23 13:51:29 -0300478 f->fmt.pix.sizeimage = coda_estimate_sizeimage(ctx,
479 f->fmt.pix.sizeimage,
480 f->fmt.pix.width,
481 f->fmt.pix.height);
Philipp Zabel57625592013-09-30 10:34:51 -0300482 break;
483 default:
484 BUG();
Javier Martin186b2502012-07-26 05:53:35 -0300485 }
486
487 return 0;
488}
489
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300490static int coda_try_fmt_vid_cap(struct file *file, void *priv,
491 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300492{
Javier Martin186b2502012-07-26 05:53:35 -0300493 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300494 const struct coda_q_data *q_data_src;
495 const struct coda_codec *codec;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300496 struct vb2_queue *src_vq;
497 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300498
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300499 ret = coda_try_pixelformat(ctx, f);
500 if (ret < 0)
501 return ret;
502
503 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
504
Philipp Zabel918c66f2013-06-27 06:59:01 -0300505 /*
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300506 * If the source format is already fixed, only allow the same output
507 * resolution
Philipp Zabel918c66f2013-06-27 06:59:01 -0300508 */
Philipp Zabel14604e32014-07-11 06:36:22 -0300509 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300510 if (vb2_is_streaming(src_vq)) {
Philipp Zabel91b5841e2014-07-18 07:22:41 -0300511 f->fmt.pix.width = q_data_src->width;
512 f->fmt.pix.height = q_data_src->height;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300513 }
Javier Martin186b2502012-07-26 05:53:35 -0300514
515 f->fmt.pix.colorspace = ctx->colorspace;
516
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300517 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
518 codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
519 f->fmt.pix.pixelformat);
520 if (!codec)
521 return -EINVAL;
522
Philipp Zabel57625592013-09-30 10:34:51 -0300523 ret = coda_try_fmt(ctx, codec, f);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300524 if (ret < 0)
525 return ret;
526
527 /* The h.264 decoder only returns complete 16x16 macroblocks */
528 if (codec && codec->src_fourcc == V4L2_PIX_FMT_H264) {
Philipp Zabel1b0ed7b2014-07-11 06:36:37 -0300529 f->fmt.pix.width = f->fmt.pix.width;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300530 f->fmt.pix.height = round_up(f->fmt.pix.height, 16);
Philipp Zabel1b0ed7b2014-07-11 06:36:37 -0300531 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300532 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
533 f->fmt.pix.height * 3 / 2;
534 }
535
536 return 0;
Javier Martin186b2502012-07-26 05:53:35 -0300537}
538
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300539static int coda_try_fmt_vid_out(struct file *file, void *priv,
540 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300541{
542 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300543 struct coda_dev *dev = ctx->dev;
544 const struct coda_q_data *q_data_dst;
545 const struct coda_codec *codec;
546 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300547
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300548 ret = coda_try_pixelformat(ctx, f);
549 if (ret < 0)
550 return ret;
Javier Martin186b2502012-07-26 05:53:35 -0300551
Philipp Zabel5f9826e2015-01-23 13:51:21 -0300552 switch (f->fmt.pix.colorspace) {
553 case V4L2_COLORSPACE_REC709:
554 case V4L2_COLORSPACE_JPEG:
555 break;
556 default:
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300557 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_JPEG)
558 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
559 else
560 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
Javier Martin186b2502012-07-26 05:53:35 -0300561 }
562
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300563 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
564 codec = coda_find_codec(dev, f->fmt.pix.pixelformat, q_data_dst->fourcc);
Javier Martin186b2502012-07-26 05:53:35 -0300565
566 return coda_try_fmt(ctx, codec, f);
567}
568
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300569static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300570{
571 struct coda_q_data *q_data;
572 struct vb2_queue *vq;
Javier Martin186b2502012-07-26 05:53:35 -0300573
Philipp Zabel14604e32014-07-11 06:36:22 -0300574 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
Javier Martin186b2502012-07-26 05:53:35 -0300575 if (!vq)
576 return -EINVAL;
577
578 q_data = get_q_data(ctx, f->type);
579 if (!q_data)
580 return -EINVAL;
581
582 if (vb2_is_busy(vq)) {
583 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
584 return -EBUSY;
585 }
586
Philipp Zabelb96904e2013-05-23 10:42:58 -0300587 q_data->fourcc = f->fmt.pix.pixelformat;
Javier Martin186b2502012-07-26 05:53:35 -0300588 q_data->width = f->fmt.pix.width;
589 q_data->height = f->fmt.pix.height;
Philipp Zabel2fd6a372014-07-11 06:36:36 -0300590 q_data->bytesperline = f->fmt.pix.bytesperline;
Philipp Zabel451d43a2012-07-26 09:18:27 -0300591 q_data->sizeimage = f->fmt.pix.sizeimage;
Philipp Zabel52c41672014-07-11 06:36:19 -0300592 q_data->rect.left = 0;
593 q_data->rect.top = 0;
594 q_data->rect.width = f->fmt.pix.width;
595 q_data->rect.height = f->fmt.pix.height;
Javier Martin186b2502012-07-26 05:53:35 -0300596
Philipp Zabela269e532015-07-16 13:19:38 -0300597 switch (f->fmt.pix.pixelformat) {
598 case V4L2_PIX_FMT_NV12:
599 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
600 ctx->tiled_map_type = GDI_TILED_FRAME_MB_RASTER_MAP;
601 if (!disable_tiling)
602 break;
603 }
604 /* else fall through */
605 case V4L2_PIX_FMT_YUV420:
606 case V4L2_PIX_FMT_YVU420:
607 ctx->tiled_map_type = GDI_LINEAR_FRAME_MAP;
608 break;
609 default:
610 break;
611 }
612
Javier Martin186b2502012-07-26 05:53:35 -0300613 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
614 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
Philipp Zabelb96904e2013-05-23 10:42:58 -0300615 f->type, q_data->width, q_data->height, q_data->fourcc);
Javier Martin186b2502012-07-26 05:53:35 -0300616
617 return 0;
618}
619
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300620static int coda_s_fmt_vid_cap(struct file *file, void *priv,
621 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300622{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300623 struct coda_ctx *ctx = fh_to_ctx(priv);
Javier Martin186b2502012-07-26 05:53:35 -0300624 int ret;
625
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300626 ret = coda_try_fmt_vid_cap(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300627 if (ret)
628 return ret;
629
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300630 return coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300631}
632
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300633static int coda_s_fmt_vid_out(struct file *file, void *priv,
634 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300635{
636 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelf95a6ce2014-08-05 14:00:19 -0300637 struct v4l2_format f_cap;
Javier Martin186b2502012-07-26 05:53:35 -0300638 int ret;
639
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300640 ret = coda_try_fmt_vid_out(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300641 if (ret)
642 return ret;
643
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300644 ret = coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300645 if (ret)
Philipp Zabel2dc546d2014-08-05 14:00:18 -0300646 return ret;
647
648 ctx->colorspace = f->fmt.pix.colorspace;
Javier Martin186b2502012-07-26 05:53:35 -0300649
Philipp Zabel1c2f6a92015-01-23 13:51:22 -0300650 memset(&f_cap, 0, sizeof(f_cap));
Philipp Zabelf95a6ce2014-08-05 14:00:19 -0300651 f_cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
652 coda_g_fmt(file, priv, &f_cap);
653 f_cap.fmt.pix.width = f->fmt.pix.width;
654 f_cap.fmt.pix.height = f->fmt.pix.height;
655
656 ret = coda_try_fmt_vid_cap(file, priv, &f_cap);
657 if (ret)
658 return ret;
659
660 return coda_s_fmt(ctx, &f_cap);
Javier Martin186b2502012-07-26 05:53:35 -0300661}
662
Philipp Zabel73751da2015-03-24 14:30:51 -0300663static int coda_reqbufs(struct file *file, void *priv,
664 struct v4l2_requestbuffers *rb)
665{
666 struct coda_ctx *ctx = fh_to_ctx(priv);
667 int ret;
668
669 ret = v4l2_m2m_reqbufs(file, ctx->fh.m2m_ctx, rb);
670 if (ret)
671 return ret;
672
673 /*
674 * Allow to allocate instance specific per-context buffers, such as
675 * bitstream ringbuffer, slice buffer, work buffer, etc. if needed.
676 */
677 if (rb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT && ctx->ops->reqbufs)
678 return ctx->ops->reqbufs(ctx, rb);
679
680 return 0;
681}
682
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300683static int coda_qbuf(struct file *file, void *priv,
684 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300685{
686 struct coda_ctx *ctx = fh_to_ctx(priv);
687
Philipp Zabel14604e32014-07-11 06:36:22 -0300688 return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
Javier Martin186b2502012-07-26 05:53:35 -0300689}
690
Philipp Zabel918c66f2013-06-27 06:59:01 -0300691static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx,
Junghak Sung2d700712015-09-22 10:30:30 -0300692 struct vb2_v4l2_buffer *buf)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300693{
694 struct vb2_queue *src_vq;
695
Philipp Zabel14604e32014-07-11 06:36:22 -0300696 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300697
698 return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) &&
Junghak Sung2d700712015-09-22 10:30:30 -0300699 (buf->sequence == (ctx->qsequence - 1)));
Philipp Zabel918c66f2013-06-27 06:59:01 -0300700}
701
Junghak Sung2d700712015-09-22 10:30:30 -0300702void coda_m2m_buf_done(struct coda_ctx *ctx, struct vb2_v4l2_buffer *buf,
Philipp Zabel9f2bfb32015-05-04 07:51:07 -0300703 enum vb2_buffer_state state)
Javier Martin186b2502012-07-26 05:53:35 -0300704{
Philipp Zabel9f2bfb32015-05-04 07:51:07 -0300705 const struct v4l2_event eos_event = {
706 .type = V4L2_EVENT_EOS
707 };
Javier Martin186b2502012-07-26 05:53:35 -0300708
Philipp Zabel9f2bfb32015-05-04 07:51:07 -0300709 if (coda_buf_is_end_of_stream(ctx, buf)) {
Junghak Sung2d700712015-09-22 10:30:30 -0300710 buf->flags |= V4L2_BUF_FLAG_LAST;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300711
712 v4l2_event_queue_fh(&ctx->fh, &eos_event);
713 }
714
Philipp Zabel9f2bfb32015-05-04 07:51:07 -0300715 v4l2_m2m_buf_done(buf, state);
Javier Martin186b2502012-07-26 05:53:35 -0300716}
717
Philipp Zabel52c41672014-07-11 06:36:19 -0300718static int coda_g_selection(struct file *file, void *fh,
719 struct v4l2_selection *s)
720{
721 struct coda_ctx *ctx = fh_to_ctx(fh);
722 struct coda_q_data *q_data;
723 struct v4l2_rect r, *rsel;
724
725 q_data = get_q_data(ctx, s->type);
726 if (!q_data)
727 return -EINVAL;
728
729 r.left = 0;
730 r.top = 0;
731 r.width = q_data->width;
732 r.height = q_data->height;
733 rsel = &q_data->rect;
734
735 switch (s->target) {
736 case V4L2_SEL_TGT_CROP_DEFAULT:
737 case V4L2_SEL_TGT_CROP_BOUNDS:
738 rsel = &r;
739 /* fallthrough */
740 case V4L2_SEL_TGT_CROP:
741 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
742 return -EINVAL;
743 break;
744 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
745 case V4L2_SEL_TGT_COMPOSE_PADDED:
746 rsel = &r;
747 /* fallthrough */
748 case V4L2_SEL_TGT_COMPOSE:
749 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
750 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
751 return -EINVAL;
752 break;
753 default:
754 return -EINVAL;
755 }
756
757 s->r = *rsel;
758
759 return 0;
760}
761
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300762static int coda_try_decoder_cmd(struct file *file, void *fh,
763 struct v4l2_decoder_cmd *dc)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300764{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300765 if (dc->cmd != V4L2_DEC_CMD_STOP)
766 return -EINVAL;
767
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300768 if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300769 return -EINVAL;
770
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300771 if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0))
Philipp Zabel918c66f2013-06-27 06:59:01 -0300772 return -EINVAL;
773
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300774 return 0;
775}
776
777static int coda_decoder_cmd(struct file *file, void *fh,
778 struct v4l2_decoder_cmd *dc)
779{
780 struct coda_ctx *ctx = fh_to_ctx(fh);
781 int ret;
782
783 ret = coda_try_decoder_cmd(file, fh, dc);
784 if (ret < 0)
785 return ret;
786
787 /* Ignore decoder stop command silently in encoder context */
Philipp Zabel918c66f2013-06-27 06:59:01 -0300788 if (ctx->inst_type != CODA_INST_DECODER)
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300789 return 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300790
Philipp Zabel347bb7f2014-07-23 12:28:42 -0300791 /* Set the stream-end flag on this context */
792 coda_bit_stream_end_flag(ctx);
Philipp Zabel84e23652014-07-11 06:36:34 -0300793 ctx->hold = false;
Michael Olbrichf3497da2014-07-11 06:36:30 -0300794 v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
Philipp Zabel89548442014-07-11 06:36:17 -0300795
Philipp Zabel918c66f2013-06-27 06:59:01 -0300796 return 0;
797}
798
Philipp Zabelcde29ef2015-07-16 13:13:24 -0300799static int coda_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
800{
801 struct coda_ctx *ctx = fh_to_ctx(fh);
802 struct v4l2_fract *tpf;
803
804 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
805 return -EINVAL;
806
807 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
808 tpf = &a->parm.output.timeperframe;
809 tpf->denominator = ctx->params.framerate & CODA_FRATE_RES_MASK;
810 tpf->numerator = 1 + (ctx->params.framerate >>
811 CODA_FRATE_DIV_OFFSET);
812
813 return 0;
814}
815
816/*
817 * Approximate timeperframe v4l2_fract with values that can be written
818 * into the 16-bit CODA_FRATE_DIV and CODA_FRATE_RES fields.
819 */
820static void coda_approximate_timeperframe(struct v4l2_fract *timeperframe)
821{
822 struct v4l2_fract s = *timeperframe;
823 struct v4l2_fract f0;
824 struct v4l2_fract f1 = { 1, 0 };
825 struct v4l2_fract f2 = { 0, 1 };
826 unsigned int i, div, s_denominator;
827
828 /* Lower bound is 1/65535 */
829 if (s.numerator == 0 || s.denominator / s.numerator > 65535) {
830 timeperframe->numerator = 1;
831 timeperframe->denominator = 65535;
832 return;
833 }
834
835 /* Upper bound is 65536/1, map everything above to infinity */
836 if (s.denominator == 0 || s.numerator / s.denominator > 65536) {
837 timeperframe->numerator = 1;
838 timeperframe->denominator = 0;
839 return;
840 }
841
842 /* Reduce fraction to lowest terms */
843 div = gcd(s.numerator, s.denominator);
844 if (div > 1) {
845 s.numerator /= div;
846 s.denominator /= div;
847 }
848
849 if (s.numerator <= 65536 && s.denominator < 65536) {
850 *timeperframe = s;
851 return;
852 }
853
854 /* Find successive convergents from continued fraction expansion */
855 while (f2.numerator <= 65536 && f2.denominator < 65536) {
856 f0 = f1;
857 f1 = f2;
858
859 /* Stop when f2 exactly equals timeperframe */
860 if (s.numerator == 0)
861 break;
862
863 i = s.denominator / s.numerator;
864
865 f2.numerator = f0.numerator + i * f1.numerator;
866 f2.denominator = f0.denominator + i * f2.denominator;
867
868 s_denominator = s.numerator;
869 s.numerator = s.denominator % s.numerator;
870 s.denominator = s_denominator;
871 }
872
873 *timeperframe = f1;
874}
875
876static uint32_t coda_timeperframe_to_frate(struct v4l2_fract *timeperframe)
877{
878 return ((timeperframe->numerator - 1) << CODA_FRATE_DIV_OFFSET) |
879 timeperframe->denominator;
880}
881
882static int coda_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
883{
884 struct coda_ctx *ctx = fh_to_ctx(fh);
885 struct v4l2_fract *tpf;
886
887 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
888 return -EINVAL;
889
890 tpf = &a->parm.output.timeperframe;
891 coda_approximate_timeperframe(tpf);
892 ctx->params.framerate = coda_timeperframe_to_frate(tpf);
893
894 return 0;
895}
896
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300897static int coda_subscribe_event(struct v4l2_fh *fh,
898 const struct v4l2_event_subscription *sub)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300899{
900 switch (sub->type) {
901 case V4L2_EVENT_EOS:
902 return v4l2_event_subscribe(fh, sub, 0, NULL);
903 default:
904 return v4l2_ctrl_subscribe_event(fh, sub);
905 }
Javier Martin186b2502012-07-26 05:53:35 -0300906}
907
908static const struct v4l2_ioctl_ops coda_ioctl_ops = {
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300909 .vidioc_querycap = coda_querycap,
Javier Martin186b2502012-07-26 05:53:35 -0300910
Philipp Zabel22e244b2014-07-18 07:22:43 -0300911 .vidioc_enum_fmt_vid_cap = coda_enum_fmt,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300912 .vidioc_g_fmt_vid_cap = coda_g_fmt,
913 .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
914 .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap,
Javier Martin186b2502012-07-26 05:53:35 -0300915
Philipp Zabel22e244b2014-07-18 07:22:43 -0300916 .vidioc_enum_fmt_vid_out = coda_enum_fmt,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300917 .vidioc_g_fmt_vid_out = coda_g_fmt,
918 .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
919 .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out,
Javier Martin186b2502012-07-26 05:53:35 -0300920
Philipp Zabel73751da2015-03-24 14:30:51 -0300921 .vidioc_reqbufs = coda_reqbufs,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300922 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
Javier Martin186b2502012-07-26 05:53:35 -0300923
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300924 .vidioc_qbuf = coda_qbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300925 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
Philipp Zabel9f2bfb32015-05-04 07:51:07 -0300926 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300927 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
Philipp Zabelbb757d72015-12-02 14:58:52 -0200928 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
Javier Martin186b2502012-07-26 05:53:35 -0300929
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300930 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
931 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300932
Philipp Zabel52c41672014-07-11 06:36:19 -0300933 .vidioc_g_selection = coda_g_selection,
934
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300935 .vidioc_try_decoder_cmd = coda_try_decoder_cmd,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300936 .vidioc_decoder_cmd = coda_decoder_cmd,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300937
Philipp Zabelcde29ef2015-07-16 13:13:24 -0300938 .vidioc_g_parm = coda_g_parm,
939 .vidioc_s_parm = coda_s_parm,
940
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300941 .vidioc_subscribe_event = coda_subscribe_event,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300942 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Javier Martin186b2502012-07-26 05:53:35 -0300943};
944
945/*
946 * Mem-to-mem operations.
947 */
Philipp Zabel477c1cf2013-06-21 03:55:33 -0300948
949static void coda_device_run(void *m2m_priv)
950{
951 struct coda_ctx *ctx = m2m_priv;
952 struct coda_dev *dev = ctx->dev;
Philipp Zabel32b6f202014-07-11 06:36:20 -0300953
954 queue_work(dev->workqueue, &ctx->pic_run_work);
955}
956
Philipp Zabel32b6f202014-07-11 06:36:20 -0300957static void coda_pic_run_work(struct work_struct *work)
958{
959 struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
960 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300961 int ret;
962
963 mutex_lock(&ctx->buffer_mutex);
Philipp Zabel477c1cf2013-06-21 03:55:33 -0300964 mutex_lock(&dev->coda_mutex);
965
Philipp Zabela1192a12014-07-23 12:28:40 -0300966 ret = ctx->ops->prepare_run(ctx);
967 if (ret < 0 && ctx->inst_type == CODA_INST_DECODER) {
968 mutex_unlock(&dev->coda_mutex);
969 mutex_unlock(&ctx->buffer_mutex);
970 /* job_finish scheduled by prepare_decode */
971 return;
Philipp Zabel10436672012-07-02 09:03:55 -0300972 }
973
Philipp Zabelf23797b2014-08-06 08:02:23 -0300974 if (!wait_for_completion_timeout(&ctx->completion,
975 msecs_to_jiffies(1000))) {
Philipp Zabel32b6f202014-07-11 06:36:20 -0300976 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout\n");
Philipp Zabel84e23652014-07-11 06:36:34 -0300977
978 ctx->hold = true;
Philipp Zabel8f452842014-07-11 06:36:35 -0300979
980 coda_hw_reset(ctx);
Philipp Zabel32b6f202014-07-11 06:36:20 -0300981 } else if (!ctx->aborting) {
Philipp Zabela1192a12014-07-23 12:28:40 -0300982 ctx->ops->finish_run(ctx);
Philipp Zabel32b6f202014-07-11 06:36:20 -0300983 }
984
Philipp Zabel747d7642015-01-23 13:51:31 -0300985 if ((ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out)) &&
986 ctx->ops->seq_end_work)
Philipp Zabel32b6f202014-07-11 06:36:20 -0300987 queue_work(dev->workqueue, &ctx->seq_end_work);
988
989 mutex_unlock(&dev->coda_mutex);
990 mutex_unlock(&ctx->buffer_mutex);
991
Philipp Zabel14604e32014-07-11 06:36:22 -0300992 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -0300993}
994
995static int coda_job_ready(void *m2m_priv)
996{
997 struct coda_ctx *ctx = m2m_priv;
Philipp Zabel2cf251c2015-07-09 07:10:20 -0300998 int src_bufs = v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -0300999
1000 /*
1001 * For both 'P' and 'key' frame cases 1 picture
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001002 * and 1 frame are needed. In the decoder case,
1003 * the compressed frame can be in the bitstream.
Javier Martin186b2502012-07-26 05:53:35 -03001004 */
Philipp Zabel2cf251c2015-07-09 07:10:20 -03001005 if (!src_bufs && ctx->inst_type != CODA_INST_DECODER) {
Javier Martin186b2502012-07-26 05:53:35 -03001006 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1007 "not ready: not enough video buffers.\n");
1008 return 0;
1009 }
1010
Philipp Zabel14604e32014-07-11 06:36:22 -03001011 if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001012 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1013 "not ready: not enough video capture buffers.\n");
1014 return 0;
1015 }
1016
Philipp Zabela22496c2015-01-23 13:51:33 -03001017 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
Philipp Zabel47f3fa62015-07-09 07:10:21 -03001018 bool stream_end = ctx->bit_stream_param &
1019 CODA_BIT_STREAM_END_FLAG;
1020 int num_metas = ctx->num_metas;
Philipp Zabelf77fd8a2015-01-23 13:51:20 -03001021
Philipp Zabel2cf251c2015-07-09 07:10:20 -03001022 if (ctx->hold && !src_bufs) {
Philipp Zabelf77fd8a2015-01-23 13:51:20 -03001023 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1024 "%d: not ready: on hold for more buffers.\n",
1025 ctx->idx);
1026 return 0;
1027 }
1028
Philipp Zabelf77fd8a2015-01-23 13:51:20 -03001029 if (!stream_end && (num_metas + src_bufs) < 2) {
1030 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1031 "%d: not ready: need 2 buffers available (%d, %d)\n",
1032 ctx->idx, num_metas, src_bufs);
1033 return 0;
1034 }
1035
1036
Philipp Zabel2cf251c2015-07-09 07:10:20 -03001037 if (!src_bufs && !stream_end &&
1038 (coda_get_bitstream_payload(ctx) < 512)) {
Philipp Zabelf77fd8a2015-01-23 13:51:20 -03001039 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1040 "%d: not ready: not enough bitstream data (%d).\n",
1041 ctx->idx, coda_get_bitstream_payload(ctx));
1042 return 0;
1043 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001044 }
1045
Philipp Zabel3e748262013-05-23 10:43:01 -03001046 if (ctx->aborting) {
1047 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1048 "not ready: aborting\n");
1049 return 0;
1050 }
1051
Javier Martin186b2502012-07-26 05:53:35 -03001052 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1053 "job ready\n");
Philipp Zabel47f3fa62015-07-09 07:10:21 -03001054
Javier Martin186b2502012-07-26 05:53:35 -03001055 return 1;
1056}
1057
1058static void coda_job_abort(void *priv)
1059{
1060 struct coda_ctx *ctx = priv;
Javier Martin186b2502012-07-26 05:53:35 -03001061
1062 ctx->aborting = 1;
1063
1064 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1065 "Aborting task\n");
Javier Martin186b2502012-07-26 05:53:35 -03001066}
1067
1068static void coda_lock(void *m2m_priv)
1069{
1070 struct coda_ctx *ctx = m2m_priv;
1071 struct coda_dev *pcdev = ctx->dev;
Philipp Zabelf23797b2014-08-06 08:02:23 -03001072
Javier Martin186b2502012-07-26 05:53:35 -03001073 mutex_lock(&pcdev->dev_mutex);
1074}
1075
1076static void coda_unlock(void *m2m_priv)
1077{
1078 struct coda_ctx *ctx = m2m_priv;
1079 struct coda_dev *pcdev = ctx->dev;
Philipp Zabelf23797b2014-08-06 08:02:23 -03001080
Javier Martin186b2502012-07-26 05:53:35 -03001081 mutex_unlock(&pcdev->dev_mutex);
1082}
1083
Philipp Zabel814c3762014-07-18 07:22:45 -03001084static const struct v4l2_m2m_ops coda_m2m_ops = {
Javier Martin186b2502012-07-26 05:53:35 -03001085 .device_run = coda_device_run,
1086 .job_ready = coda_job_ready,
1087 .job_abort = coda_job_abort,
1088 .lock = coda_lock,
1089 .unlock = coda_unlock,
1090};
1091
1092static void set_default_params(struct coda_ctx *ctx)
1093{
Philipp Zabel8e428d52015-01-23 13:51:29 -03001094 unsigned int max_w, max_h, usize, csize;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001095
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001096 ctx->codec = coda_find_codec(ctx->dev, ctx->cvd->src_formats[0],
1097 ctx->cvd->dst_formats[0]);
Philipp Zabeld4c6a412014-10-02 14:08:35 -03001098 max_w = min(ctx->codec->max_w, 1920U);
1099 max_h = min(ctx->codec->max_h, 1088U);
Philipp Zabel8e428d52015-01-23 13:51:29 -03001100 usize = max_w * max_h * 3 / 2;
1101 csize = coda_estimate_sizeimage(ctx, usize, max_w, max_h);
Javier Martin186b2502012-07-26 05:53:35 -03001102
Philipp Zabel121cacf2014-07-18 07:22:42 -03001103 ctx->params.codec_mode = ctx->codec->mode;
Javier Martin186b2502012-07-26 05:53:35 -03001104 ctx->colorspace = V4L2_COLORSPACE_REC709;
1105 ctx->params.framerate = 30;
Javier Martin186b2502012-07-26 05:53:35 -03001106
1107 /* Default formats for output and input queues */
Philipp Zabel6727d4f2015-07-16 13:19:39 -03001108 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->cvd->src_formats[0];
1109 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->cvd->dst_formats[0];
Philipp Zabelb96904e2013-05-23 10:42:58 -03001110 ctx->q_data[V4L2_M2M_SRC].width = max_w;
1111 ctx->q_data[V4L2_M2M_SRC].height = max_h;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001112 ctx->q_data[V4L2_M2M_DST].width = max_w;
1113 ctx->q_data[V4L2_M2M_DST].height = max_h;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001114 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_YUV420) {
1115 ctx->q_data[V4L2_M2M_SRC].bytesperline = max_w;
Philipp Zabel8e428d52015-01-23 13:51:29 -03001116 ctx->q_data[V4L2_M2M_SRC].sizeimage = usize;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001117 ctx->q_data[V4L2_M2M_DST].bytesperline = 0;
Philipp Zabel8e428d52015-01-23 13:51:29 -03001118 ctx->q_data[V4L2_M2M_DST].sizeimage = csize;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001119 } else {
1120 ctx->q_data[V4L2_M2M_SRC].bytesperline = 0;
Philipp Zabel8e428d52015-01-23 13:51:29 -03001121 ctx->q_data[V4L2_M2M_SRC].sizeimage = csize;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001122 ctx->q_data[V4L2_M2M_DST].bytesperline = max_w;
Philipp Zabel8e428d52015-01-23 13:51:29 -03001123 ctx->q_data[V4L2_M2M_DST].sizeimage = usize;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001124 }
Philipp Zabel52c41672014-07-11 06:36:19 -03001125 ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
1126 ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
1127 ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
1128 ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
Philipp Zabel89548442014-07-11 06:36:17 -03001129
Philipp Zabela269e532015-07-16 13:19:38 -03001130 /*
1131 * Since the RBC2AXI logic only supports a single chroma plane,
1132 * macroblock tiling only works for to NV12 pixel format.
1133 */
1134 ctx->tiled_map_type = GDI_LINEAR_FRAME_MAP;
Javier Martin186b2502012-07-26 05:53:35 -03001135}
1136
1137/*
1138 * Queue operations
1139 */
Hans Verkuildf9ecb02015-10-28 00:50:37 -02001140static int coda_queue_setup(struct vb2_queue *vq,
Javier Martin186b2502012-07-26 05:53:35 -03001141 unsigned int *nbuffers, unsigned int *nplanes,
1142 unsigned int sizes[], void *alloc_ctxs[])
1143{
1144 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
Philipp Zabele34db062012-08-29 08:22:00 -03001145 struct coda_q_data *q_data;
Javier Martin186b2502012-07-26 05:53:35 -03001146 unsigned int size;
1147
Philipp Zabele34db062012-08-29 08:22:00 -03001148 q_data = get_q_data(ctx, vq->type);
1149 size = q_data->sizeimage;
Javier Martin186b2502012-07-26 05:53:35 -03001150
1151 *nplanes = 1;
1152 sizes[0] = size;
1153
Philipp Zabelbb04aa62015-01-23 13:51:30 -03001154 /* Set to vb2-dma-contig allocator context, ignored by vb2-vmalloc */
Javier Martin186b2502012-07-26 05:53:35 -03001155 alloc_ctxs[0] = ctx->dev->alloc_ctx;
1156
1157 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1158 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
1159
1160 return 0;
1161}
1162
1163static int coda_buf_prepare(struct vb2_buffer *vb)
1164{
1165 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1166 struct coda_q_data *q_data;
1167
1168 q_data = get_q_data(ctx, vb->vb2_queue->type);
1169
1170 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
1171 v4l2_warn(&ctx->dev->v4l2_dev,
1172 "%s data will not fit into plane (%lu < %lu)\n",
1173 __func__, vb2_plane_size(vb, 0),
1174 (long)q_data->sizeimage);
1175 return -EINVAL;
1176 }
1177
Javier Martin186b2502012-07-26 05:53:35 -03001178 return 0;
1179}
1180
1181static void coda_buf_queue(struct vb2_buffer *vb)
1182{
Junghak Sung2d700712015-09-22 10:30:30 -03001183 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
Javier Martin186b2502012-07-26 05:53:35 -03001184 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
Philipp Zabelcb06b702015-01-23 13:51:35 -03001185 struct vb2_queue *vq = vb->vb2_queue;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001186 struct coda_q_data *q_data;
1187
1188 q_data = get_q_data(ctx, vb->vb2_queue->type);
1189
1190 /*
1191 * In the decoder case, immediately try to copy the buffer into the
1192 * bitstream ringbuffer and mark it as ready to be dequeued.
1193 */
Philipp Zabelcb06b702015-01-23 13:51:35 -03001194 if (ctx->bitstream.size && vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001195 /*
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03001196 * For backwards compatibility, queuing an empty buffer marks
Philipp Zabel918c66f2013-06-27 06:59:01 -03001197 * the stream end
1198 */
Philipp Zabel347bb7f2014-07-23 12:28:42 -03001199 if (vb2_get_plane_payload(vb, 0) == 0)
1200 coda_bit_stream_end_flag(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001201 mutex_lock(&ctx->bitstream_mutex);
Junghak Sung2d700712015-09-22 10:30:30 -03001202 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
Michael Olbricheabed932014-07-18 07:22:40 -03001203 if (vb2_is_streaming(vb->vb2_queue))
Philipp Zabel582d88722015-03-24 14:30:57 -03001204 coda_fill_bitstream(ctx, true);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001205 mutex_unlock(&ctx->bitstream_mutex);
1206 } else {
Junghak Sung2d700712015-09-22 10:30:30 -03001207 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001208 }
Javier Martin186b2502012-07-26 05:53:35 -03001209}
1210
Philipp Zabel79924ca2014-07-23 12:28:45 -03001211int coda_alloc_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf,
1212 size_t size, const char *name, struct dentry *parent)
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001213{
1214 buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
1215 GFP_KERNEL);
Philipp Zabel68fc31c2014-08-05 14:00:16 -03001216 if (!buf->vaddr) {
1217 v4l2_err(&dev->v4l2_dev,
1218 "Failed to allocate %s buffer of size %u\n",
1219 name, size);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001220 return -ENOMEM;
Philipp Zabel68fc31c2014-08-05 14:00:16 -03001221 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001222
1223 buf->size = size;
1224
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001225 if (name && parent) {
1226 buf->blob.data = buf->vaddr;
1227 buf->blob.size = size;
Philipp Zabelf23797b2014-08-06 08:02:23 -03001228 buf->dentry = debugfs_create_blob(name, 0644, parent,
1229 &buf->blob);
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001230 if (!buf->dentry)
1231 dev_warn(&dev->plat_dev->dev,
1232 "failed to create debugfs entry %s\n", name);
1233 }
1234
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001235 return 0;
1236}
1237
Philipp Zabel79924ca2014-07-23 12:28:45 -03001238void coda_free_aux_buf(struct coda_dev *dev,
1239 struct coda_aux_buf *buf)
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001240{
1241 if (buf->vaddr) {
1242 dma_free_coherent(&dev->plat_dev->dev, buf->size,
1243 buf->vaddr, buf->paddr);
1244 buf->vaddr = NULL;
1245 buf->size = 0;
Peter Seidererd446ec82015-03-24 14:30:48 -03001246 debugfs_remove(buf->dentry);
1247 buf->dentry = NULL;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001248 }
1249}
1250
Javier Martin186b2502012-07-26 05:53:35 -03001251static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
1252{
1253 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1254 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
Javier Martin186b2502012-07-26 05:53:35 -03001255 struct coda_q_data *q_data_src, *q_data_dst;
Junghak Sung2d700712015-09-22 10:30:30 -03001256 struct vb2_v4l2_buffer *buf;
Philipp Zabeld35167a2013-05-23 10:42:59 -03001257 int ret = 0;
Javier Martin186b2502012-07-26 05:53:35 -03001258
Philipp Zabel49b966f2015-12-02 14:58:53 -02001259 if (count < 1)
1260 return -EINVAL;
1261
Philipp Zabelb96904e2013-05-23 10:42:58 -03001262 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001263 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabel58bc7ed2015-07-09 07:10:14 -03001264 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
Philipp Zabel582d88722015-03-24 14:30:57 -03001265 /* copy the buffers that were queued before streamon */
Michael Olbricheabed932014-07-18 07:22:40 -03001266 mutex_lock(&ctx->bitstream_mutex);
Philipp Zabel582d88722015-03-24 14:30:57 -03001267 coda_fill_bitstream(ctx, false);
Michael Olbricheabed932014-07-18 07:22:40 -03001268 mutex_unlock(&ctx->bitstream_mutex);
1269
Philipp Zabelb9063522014-08-05 14:00:10 -03001270 if (coda_get_bitstream_payload(ctx) < 512) {
1271 ret = -EINVAL;
1272 goto err;
1273 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001274 }
1275
1276 ctx->streamon_out = 1;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001277 } else {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001278 ctx->streamon_cap = 1;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001279 }
Javier Martin186b2502012-07-26 05:53:35 -03001280
Philipp Zabelb96904e2013-05-23 10:42:58 -03001281 /* Don't start the coda unless both queues are on */
1282 if (!(ctx->streamon_out & ctx->streamon_cap))
1283 return 0;
Javier Martin186b2502012-07-26 05:53:35 -03001284
Philipp Zabel23b6ee52015-03-24 14:30:55 -03001285 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1286 if ((q_data_src->width != q_data_dst->width &&
1287 round_up(q_data_src->width, 16) != q_data_dst->width) ||
1288 (q_data_src->height != q_data_dst->height &&
1289 round_up(q_data_src->height, 16) != q_data_dst->height)) {
1290 v4l2_err(v4l2_dev, "can't convert %dx%d to %dx%d\n",
1291 q_data_src->width, q_data_src->height,
1292 q_data_dst->width, q_data_dst->height);
1293 ret = -EINVAL;
1294 goto err;
1295 }
1296
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001297 /* Allow BIT decoder device_run with no new buffers queued */
Philipp Zabela22496c2015-01-23 13:51:33 -03001298 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
Philipp Zabel14604e32014-07-11 06:36:22 -03001299 v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001300
Philipp Zabelb96904e2013-05-23 10:42:58 -03001301 ctx->gopcounter = ctx->params.gop_size - 1;
Javier Martin186b2502012-07-26 05:53:35 -03001302
Philipp Zabelb96904e2013-05-23 10:42:58 -03001303 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
1304 q_data_dst->fourcc);
1305 if (!ctx->codec) {
Javier Martin186b2502012-07-26 05:53:35 -03001306 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
Philipp Zabelb9063522014-08-05 14:00:10 -03001307 ret = -EINVAL;
1308 goto err;
Javier Martin186b2502012-07-26 05:53:35 -03001309 }
1310
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001311 if (q_data_dst->fourcc == V4L2_PIX_FMT_JPEG)
1312 ctx->params.gop_size = 1;
1313 ctx->gopcounter = ctx->params.gop_size - 1;
1314
Philipp Zabela1192a12014-07-23 12:28:40 -03001315 ret = ctx->ops->start_streaming(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001316 if (ctx->inst_type == CODA_INST_DECODER) {
Philipp Zabel89548442014-07-11 06:36:17 -03001317 if (ret == -EAGAIN)
Philipp Zabel918c66f2013-06-27 06:59:01 -03001318 return 0;
Philipp Zabel89548442014-07-11 06:36:17 -03001319 else if (ret < 0)
Philipp Zabelb9063522014-08-05 14:00:10 -03001320 goto err;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001321 }
1322
Philipp Zabel89548442014-07-11 06:36:17 -03001323 return ret;
Philipp Zabelb9063522014-08-05 14:00:10 -03001324
1325err:
1326 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1327 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
Philipp Zabelb8b1b58c52014-10-21 13:25:52 -03001328 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
Philipp Zabelb9063522014-08-05 14:00:10 -03001329 } else {
1330 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
Philipp Zabelb8b1b58c52014-10-21 13:25:52 -03001331 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
Philipp Zabelb9063522014-08-05 14:00:10 -03001332 }
1333 return ret;
Philipp Zabel89548442014-07-11 06:36:17 -03001334}
1335
Hans Verkuile37559b2014-04-17 02:47:21 -03001336static void coda_stop_streaming(struct vb2_queue *q)
Javier Martin186b2502012-07-26 05:53:35 -03001337{
1338 struct coda_ctx *ctx = vb2_get_drv_priv(q);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03001339 struct coda_dev *dev = ctx->dev;
Junghak Sung2d700712015-09-22 10:30:30 -03001340 struct vb2_v4l2_buffer *buf;
Philipp Zabel47f3fa62015-07-09 07:10:21 -03001341 unsigned long flags;
Philipp Zabel5c76c2c2015-03-24 14:30:56 -03001342 bool stop;
1343
1344 stop = ctx->streamon_out && ctx->streamon_cap;
Javier Martin186b2502012-07-26 05:53:35 -03001345
1346 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001347 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03001348 "%s: output\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001349 ctx->streamon_out = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001350
Philipp Zabel347bb7f2014-07-23 12:28:42 -03001351 coda_bit_stream_end_flag(ctx);
Philipp Zabel4a31b522014-08-05 14:00:11 -03001352
Philipp Zabel6dd5ef52015-01-23 13:51:26 -03001353 ctx->qsequence = 0;
Philipp Zabel4a31b522014-08-05 14:00:11 -03001354
1355 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1356 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
Javier Martin186b2502012-07-26 05:53:35 -03001357 } else {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001358 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03001359 "%s: capture\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001360 ctx->streamon_cap = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001361
1362 ctx->osequence = 0;
Philipp Zabelcb2c0282014-07-11 06:36:33 -03001363 ctx->sequence_offset = 0;
Philipp Zabel4a31b522014-08-05 14:00:11 -03001364
1365 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
1366 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
Javier Martin186b2502012-07-26 05:53:35 -03001367 }
1368
Philipp Zabel5c76c2c2015-03-24 14:30:56 -03001369 if (stop) {
Philipp Zabel7cbb1052014-10-02 14:08:32 -03001370 struct coda_buffer_meta *meta;
Philipp Zabel846ced92014-07-11 06:36:31 -03001371
Philipp Zabelf4706d62015-01-23 13:51:27 -03001372 if (ctx->ops->seq_end_work) {
1373 queue_work(dev->workqueue, &ctx->seq_end_work);
1374 flush_work(&ctx->seq_end_work);
1375 }
Philipp Zabel47f3fa62015-07-09 07:10:21 -03001376 spin_lock_irqsave(&ctx->buffer_meta_lock, flags);
Philipp Zabel7cbb1052014-10-02 14:08:32 -03001377 while (!list_empty(&ctx->buffer_meta_list)) {
1378 meta = list_first_entry(&ctx->buffer_meta_list,
1379 struct coda_buffer_meta, list);
1380 list_del(&meta->list);
1381 kfree(meta);
Philipp Zabel846ced92014-07-11 06:36:31 -03001382 }
Philipp Zabel47f3fa62015-07-09 07:10:21 -03001383 ctx->num_metas = 0;
1384 spin_unlock_irqrestore(&ctx->buffer_meta_lock, flags);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001385 kfifo_init(&ctx->bitstream_fifo,
1386 ctx->bitstream.vaddr, ctx->bitstream.size);
1387 ctx->runcounter = 0;
Philipp Zabelf157cf42014-09-29 09:53:42 -03001388 ctx->aborting = 0;
Philipp Zabel62bed142012-07-25 10:40:39 -03001389 }
Philipp Zabelc1ae0b22015-07-09 07:10:17 -03001390
1391 if (!ctx->streamon_out && !ctx->streamon_cap)
1392 ctx->bit_stream_param &= ~CODA_BIT_STREAM_END_FLAG;
Javier Martin186b2502012-07-26 05:53:35 -03001393}
1394
Philipp Zabel121cacf2014-07-18 07:22:42 -03001395static const struct vb2_ops coda_qops = {
Javier Martin186b2502012-07-26 05:53:35 -03001396 .queue_setup = coda_queue_setup,
1397 .buf_prepare = coda_buf_prepare,
1398 .buf_queue = coda_buf_queue,
Javier Martin186b2502012-07-26 05:53:35 -03001399 .start_streaming = coda_start_streaming,
1400 .stop_streaming = coda_stop_streaming,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001401 .wait_prepare = vb2_ops_wait_prepare,
1402 .wait_finish = vb2_ops_wait_finish,
Javier Martin186b2502012-07-26 05:53:35 -03001403};
1404
1405static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
1406{
1407 struct coda_ctx *ctx =
1408 container_of(ctrl->handler, struct coda_ctx, ctrls);
1409
1410 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1411 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
1412
1413 switch (ctrl->id) {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001414 case V4L2_CID_HFLIP:
1415 if (ctrl->val)
1416 ctx->params.rot_mode |= CODA_MIR_HOR;
1417 else
1418 ctx->params.rot_mode &= ~CODA_MIR_HOR;
1419 break;
1420 case V4L2_CID_VFLIP:
1421 if (ctrl->val)
1422 ctx->params.rot_mode |= CODA_MIR_VER;
1423 else
1424 ctx->params.rot_mode &= ~CODA_MIR_VER;
1425 break;
Javier Martin186b2502012-07-26 05:53:35 -03001426 case V4L2_CID_MPEG_VIDEO_BITRATE:
1427 ctx->params.bitrate = ctrl->val / 1000;
1428 break;
1429 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
1430 ctx->params.gop_size = ctrl->val;
1431 break;
1432 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
1433 ctx->params.h264_intra_qp = ctrl->val;
1434 break;
1435 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
1436 ctx->params.h264_inter_qp = ctrl->val;
1437 break;
Philipp Zabel1a5567e2014-07-11 06:36:26 -03001438 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
1439 ctx->params.h264_min_qp = ctrl->val;
1440 break;
1441 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
1442 ctx->params.h264_max_qp = ctrl->val;
1443 break;
Philipp Zabelde23b1d2014-07-11 06:36:27 -03001444 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
1445 ctx->params.h264_deblk_alpha = ctrl->val;
1446 break;
1447 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
1448 ctx->params.h264_deblk_beta = ctrl->val;
1449 break;
1450 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
1451 ctx->params.h264_deblk_enabled = (ctrl->val ==
1452 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
1453 break;
Javier Martin186b2502012-07-26 05:53:35 -03001454 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
1455 ctx->params.mpeg4_intra_qp = ctrl->val;
1456 break;
1457 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
1458 ctx->params.mpeg4_inter_qp = ctrl->val;
1459 break;
1460 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
1461 ctx->params.slice_mode = ctrl->val;
1462 break;
1463 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
1464 ctx->params.slice_max_mb = ctrl->val;
1465 break;
Philipp Zabelc566c782012-08-08 11:59:38 -03001466 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
1467 ctx->params.slice_max_bits = ctrl->val * 8;
1468 break;
Javier Martin186b2502012-07-26 05:53:35 -03001469 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
1470 break;
Philipp Zabelf38f79d2014-07-11 06:36:28 -03001471 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
1472 ctx->params.intra_refresh = ctrl->val;
1473 break;
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001474 case V4L2_CID_JPEG_COMPRESSION_QUALITY:
1475 coda_set_jpeg_compression_quality(ctx, ctrl->val);
1476 break;
1477 case V4L2_CID_JPEG_RESTART_INTERVAL:
1478 ctx->params.jpeg_restart_interval = ctrl->val;
1479 break;
Philipp Zabelda2b3b32015-07-10 10:37:52 -03001480 case V4L2_CID_MPEG_VIDEO_VBV_DELAY:
1481 ctx->params.vbv_delay = ctrl->val;
1482 break;
1483 case V4L2_CID_MPEG_VIDEO_VBV_SIZE:
1484 ctx->params.vbv_size = min(ctrl->val * 8192, 0x7fffffff);
1485 break;
Javier Martin186b2502012-07-26 05:53:35 -03001486 default:
1487 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1488 "Invalid control, id=%d, val=%d\n",
1489 ctrl->id, ctrl->val);
1490 return -EINVAL;
1491 }
1492
1493 return 0;
1494}
1495
Philipp Zabel814c3762014-07-18 07:22:45 -03001496static const struct v4l2_ctrl_ops coda_ctrl_ops = {
Javier Martin186b2502012-07-26 05:53:35 -03001497 .s_ctrl = coda_s_ctrl,
1498};
1499
Philipp Zabelbfc732f2014-10-02 14:08:29 -03001500static void coda_encode_ctrls(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03001501{
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001502 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel92b042e2015-03-18 08:15:36 -03001503 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1000, 0);
Javier Martin186b2502012-07-26 05:53:35 -03001504 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1505 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
1506 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03001507 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
Javier Martin186b2502012-07-26 05:53:35 -03001508 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03001509 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
Philipp Zabel1a5567e2014-07-11 06:36:26 -03001510 if (ctx->dev->devtype->product != CODA_960) {
1511 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1512 V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12);
1513 }
1514 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1515 V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51);
Javier Martin186b2502012-07-26 05:53:35 -03001516 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabelde23b1d2014-07-11 06:36:27 -03001517 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, 0, 15, 1, 0);
1518 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1519 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, 0, 15, 1, 0);
1520 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1521 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
1522 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED, 0x0,
1523 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
1524 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03001525 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
1526 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1527 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
1528 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1529 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
Philipp Zabelc566c782012-08-08 11:59:38 -03001530 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
1531 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
Javier Martin186b2502012-07-26 05:53:35 -03001532 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1533 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
Philipp Zabelc566c782012-08-08 11:59:38 -03001534 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabelf23797b2014-08-06 08:02:23 -03001535 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1,
1536 500);
Javier Martin186b2502012-07-26 05:53:35 -03001537 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1538 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
1539 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
1540 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
1541 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
Philipp Zabelf38f79d2014-07-11 06:36:28 -03001542 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabelf23797b2014-08-06 08:02:23 -03001543 V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0,
1544 1920 * 1088 / 256, 1, 0);
Philipp Zabelda2b3b32015-07-10 10:37:52 -03001545 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1546 V4L2_CID_MPEG_VIDEO_VBV_DELAY, 0, 0x7fff, 1, 0);
1547 /*
1548 * The maximum VBV size value is 0x7fffffff bits,
1549 * one bit less than 262144 KiB
1550 */
1551 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1552 V4L2_CID_MPEG_VIDEO_VBV_SIZE, 0, 262144, 1, 0);
Philipp Zabelbfc732f2014-10-02 14:08:29 -03001553}
1554
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001555static void coda_jpeg_encode_ctrls(struct coda_ctx *ctx)
1556{
1557 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1558 V4L2_CID_JPEG_COMPRESSION_QUALITY, 5, 100, 1, 50);
1559 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1560 V4L2_CID_JPEG_RESTART_INTERVAL, 0, 100, 1, 0);
1561}
1562
Philipp Zabelbfc732f2014-10-02 14:08:29 -03001563static int coda_ctrls_setup(struct coda_ctx *ctx)
1564{
1565 v4l2_ctrl_handler_init(&ctx->ctrls, 2);
1566
1567 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1568 V4L2_CID_HFLIP, 0, 1, 1, 0);
1569 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1570 V4L2_CID_VFLIP, 0, 1, 1, 0);
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001571 if (ctx->inst_type == CODA_INST_ENCODER) {
1572 if (ctx->cvd->dst_formats[0] == V4L2_PIX_FMT_JPEG)
1573 coda_jpeg_encode_ctrls(ctx);
1574 else
1575 coda_encode_ctrls(ctx);
1576 }
Javier Martin186b2502012-07-26 05:53:35 -03001577
1578 if (ctx->ctrls.error) {
Philipp Zabelf23797b2014-08-06 08:02:23 -03001579 v4l2_err(&ctx->dev->v4l2_dev,
1580 "control initialization error (%d)",
Javier Martin186b2502012-07-26 05:53:35 -03001581 ctx->ctrls.error);
1582 return -EINVAL;
1583 }
1584
1585 return v4l2_ctrl_handler_setup(&ctx->ctrls);
1586}
1587
Philipp Zabel121cacf2014-07-18 07:22:42 -03001588static int coda_queue_init(struct coda_ctx *ctx, struct vb2_queue *vq)
Javier Martin186b2502012-07-26 05:53:35 -03001589{
Philipp Zabel121cacf2014-07-18 07:22:42 -03001590 vq->drv_priv = ctx;
1591 vq->ops = &coda_qops;
1592 vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1593 vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1594 vq->lock = &ctx->dev->dev_mutex;
Kamil Debskie4af23d2015-02-23 09:26:18 -03001595 /* One way to indicate end-of-stream for coda is to set the
1596 * bytesused == 0. However by default videobuf2 handles bytesused
1597 * equal to 0 as a special case and changes its value to the size
1598 * of the buffer. Set the allow_zero_bytesused flag, so
1599 * that videobuf2 will keep the value of bytesused intact.
1600 */
1601 vq->allow_zero_bytesused = 1;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001602
1603 return vb2_queue_init(vq);
1604}
1605
Philipp Zabel79924ca2014-07-23 12:28:45 -03001606int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq,
1607 struct vb2_queue *dst_vq)
Philipp Zabel121cacf2014-07-18 07:22:42 -03001608{
Javier Martin186b2502012-07-26 05:53:35 -03001609 int ret;
1610
Javier Martin186b2502012-07-26 05:53:35 -03001611 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabeld29a8cf2014-07-18 07:22:38 -03001612 src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
Javier Martin186b2502012-07-26 05:53:35 -03001613 src_vq->mem_ops = &vb2_dma_contig_memops;
1614
Philipp Zabel121cacf2014-07-18 07:22:42 -03001615 ret = coda_queue_init(priv, src_vq);
Javier Martin186b2502012-07-26 05:53:35 -03001616 if (ret)
1617 return ret;
1618
Javier Martin186b2502012-07-26 05:53:35 -03001619 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Philipp Zabeld29a8cf2014-07-18 07:22:38 -03001620 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
Javier Martin186b2502012-07-26 05:53:35 -03001621 dst_vq->mem_ops = &vb2_dma_contig_memops;
1622
Philipp Zabel121cacf2014-07-18 07:22:42 -03001623 return coda_queue_init(priv, dst_vq);
1624}
1625
Philipp Zabel79924ca2014-07-23 12:28:45 -03001626int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq,
1627 struct vb2_queue *dst_vq)
Philipp Zabel121cacf2014-07-18 07:22:42 -03001628{
1629 int ret;
1630
1631 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabelbb04aa62015-01-23 13:51:30 -03001632 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
1633 src_vq->mem_ops = &vb2_vmalloc_memops;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001634
1635 ret = coda_queue_init(priv, src_vq);
1636 if (ret)
1637 return ret;
1638
1639 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1640 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1641 dst_vq->mem_ops = &vb2_dma_contig_memops;
1642
1643 return coda_queue_init(priv, dst_vq);
Javier Martin186b2502012-07-26 05:53:35 -03001644}
1645
Philipp Zabele11f3e62012-07-25 09:16:58 -03001646static int coda_next_free_instance(struct coda_dev *dev)
1647{
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03001648 int idx = ffz(dev->instance_mask);
1649
1650 if ((idx < 0) ||
1651 (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES))
1652 return -EBUSY;
1653
1654 return idx;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001655}
1656
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001657/*
1658 * File operations
1659 */
1660
1661static int coda_open(struct file *file)
Javier Martin186b2502012-07-26 05:53:35 -03001662{
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001663 struct video_device *vdev = video_devdata(file);
1664 struct coda_dev *dev = video_get_drvdata(vdev);
Javier Martin186b2502012-07-26 05:53:35 -03001665 struct coda_ctx *ctx = NULL;
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001666 char *name;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001667 int ret;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001668 int idx;
Javier Martin186b2502012-07-26 05:53:35 -03001669
Philipp Zabelf23797b2014-08-06 08:02:23 -03001670 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
Javier Martin186b2502012-07-26 05:53:35 -03001671 if (!ctx)
1672 return -ENOMEM;
1673
Fabio Estevamf82bc202013-08-21 11:14:16 -03001674 idx = coda_next_free_instance(dev);
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03001675 if (idx < 0) {
1676 ret = idx;
Fabio Estevamf82bc202013-08-21 11:14:16 -03001677 goto err_coda_max;
1678 }
1679 set_bit(idx, &dev->instance_mask);
1680
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001681 name = kasprintf(GFP_KERNEL, "context%d", idx);
Peter Seiderera7f933a2015-03-24 14:30:47 -03001682 if (!name) {
1683 ret = -ENOMEM;
1684 goto err_coda_name_init;
1685 }
1686
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001687 ctx->debugfs_entry = debugfs_create_dir(name, dev->debugfs_root);
1688 kfree(name);
1689
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001690 ctx->cvd = to_coda_video_device(vdev);
1691 ctx->inst_type = ctx->cvd->type;
1692 ctx->ops = ctx->cvd->ops;
Philipp Zabela22496c2015-01-23 13:51:33 -03001693 ctx->use_bit = !ctx->cvd->direct;
Philipp Zabel32b6f202014-07-11 06:36:20 -03001694 init_completion(&ctx->completion);
1695 INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
Philipp Zabel747d7642015-01-23 13:51:31 -03001696 if (ctx->ops->seq_end_work)
1697 INIT_WORK(&ctx->seq_end_work, ctx->ops->seq_end_work);
Javier Martin186b2502012-07-26 05:53:35 -03001698 v4l2_fh_init(&ctx->fh, video_devdata(file));
1699 file->private_data = &ctx->fh;
1700 v4l2_fh_add(&ctx->fh);
1701 ctx->dev = dev;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001702 ctx->idx = idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001703 switch (dev->devtype->product) {
Philipp Zabel89548442014-07-11 06:36:17 -03001704 case CODA_960:
Philipp Zabel2bf299c2014-09-29 09:53:46 -03001705 ctx->frame_mem_ctrl = 1 << 12;
1706 /* fallthrough */
1707 case CODA_7541:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001708 ctx->reg_idx = 0;
1709 break;
1710 default:
1711 ctx->reg_idx = idx;
1712 }
Fabio Estevamf82bc202013-08-21 11:14:16 -03001713
Philipp Zabel1e172732014-07-11 06:36:23 -03001714 /* Power up and upload firmware if necessary */
1715 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
1716 if (ret < 0) {
1717 v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
1718 goto err_pm_get;
1719 }
1720
Fabio Estevam79830db2013-08-21 11:14:17 -03001721 ret = clk_prepare_enable(dev->clk_per);
1722 if (ret)
1723 goto err_clk_per;
1724
1725 ret = clk_prepare_enable(dev->clk_ahb);
1726 if (ret)
1727 goto err_clk_ahb;
1728
Javier Martin186b2502012-07-26 05:53:35 -03001729 set_default_params(ctx);
Philipp Zabela1192a12014-07-23 12:28:40 -03001730 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
1731 ctx->ops->queue_init);
Philipp Zabel14604e32014-07-11 06:36:22 -03001732 if (IS_ERR(ctx->fh.m2m_ctx)) {
1733 ret = PTR_ERR(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001734
1735 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
1736 __func__, ret);
Fabio Estevamf82bc202013-08-21 11:14:16 -03001737 goto err_ctx_init;
Javier Martin186b2502012-07-26 05:53:35 -03001738 }
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001739
Javier Martin186b2502012-07-26 05:53:35 -03001740 ret = coda_ctrls_setup(ctx);
1741 if (ret) {
1742 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
Fabio Estevamf82bc202013-08-21 11:14:16 -03001743 goto err_ctrls_setup;
Javier Martin186b2502012-07-26 05:53:35 -03001744 }
1745
1746 ctx->fh.ctrl_handler = &ctx->ctrls;
1747
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001748 mutex_init(&ctx->bitstream_mutex);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001749 mutex_init(&ctx->buffer_mutex);
Philipp Zabel7cbb1052014-10-02 14:08:32 -03001750 INIT_LIST_HEAD(&ctx->buffer_meta_list);
Philipp Zabel47f3fa62015-07-09 07:10:21 -03001751 spin_lock_init(&ctx->buffer_meta_lock);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001752
Javier Martin186b2502012-07-26 05:53:35 -03001753 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001754 list_add(&ctx->list, &dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03001755 coda_unlock(ctx);
1756
Javier Martin186b2502012-07-26 05:53:35 -03001757 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
1758 ctx->idx, ctx);
1759
1760 return 0;
1761
Fabio Estevamf82bc202013-08-21 11:14:16 -03001762err_ctrls_setup:
Philipp Zabel14604e32014-07-11 06:36:22 -03001763 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Fabio Estevamf82bc202013-08-21 11:14:16 -03001764err_ctx_init:
1765 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03001766err_clk_ahb:
Fabio Estevamf82bc202013-08-21 11:14:16 -03001767 clk_disable_unprepare(dev->clk_per);
Fabio Estevam79830db2013-08-21 11:14:17 -03001768err_clk_per:
Philipp Zabel1e172732014-07-11 06:36:23 -03001769 pm_runtime_put_sync(&dev->plat_dev->dev);
1770err_pm_get:
Javier Martin186b2502012-07-26 05:53:35 -03001771 v4l2_fh_del(&ctx->fh);
1772 v4l2_fh_exit(&ctx->fh);
Fabio Estevamf82bc202013-08-21 11:14:16 -03001773 clear_bit(ctx->idx, &dev->instance_mask);
Peter Seiderera7f933a2015-03-24 14:30:47 -03001774err_coda_name_init:
Fabio Estevamf82bc202013-08-21 11:14:16 -03001775err_coda_max:
Javier Martin186b2502012-07-26 05:53:35 -03001776 kfree(ctx);
1777 return ret;
1778}
1779
1780static int coda_release(struct file *file)
1781{
1782 struct coda_dev *dev = video_drvdata(file);
1783 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
1784
1785 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
1786 ctx);
1787
Philipp Zabela22496c2015-01-23 13:51:33 -03001788 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
Philipp Zabeld4bb75f2014-10-08 13:09:11 -03001789 coda_bit_stream_end_flag(ctx);
1790
Philipp Zabel918c66f2013-06-27 06:59:01 -03001791 /* If this instance is running, call .job_abort and wait for it to end */
Philipp Zabel14604e32014-07-11 06:36:22 -03001792 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001793
1794 /* In case the instance was not running, we still need to call SEQ_END */
Philipp Zabel5c718bb2015-07-09 07:10:16 -03001795 if (ctx->ops->seq_end_work) {
Philipp Zabel32b6f202014-07-11 06:36:20 -03001796 queue_work(dev->workqueue, &ctx->seq_end_work);
1797 flush_work(&ctx->seq_end_work);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001798 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001799
Javier Martin186b2502012-07-26 05:53:35 -03001800 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001801 list_del(&ctx->list);
Javier Martin186b2502012-07-26 05:53:35 -03001802 coda_unlock(ctx);
1803
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001804 if (ctx->dev->devtype->product == CODA_DX6)
1805 coda_free_aux_buf(dev, &ctx->workbuf);
1806
Javier Martin186b2502012-07-26 05:53:35 -03001807 v4l2_ctrl_handler_free(&ctx->ctrls);
Javier Martin186b2502012-07-26 05:53:35 -03001808 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevamf82bc202013-08-21 11:14:16 -03001809 clk_disable_unprepare(dev->clk_per);
Philipp Zabel1e172732014-07-11 06:36:23 -03001810 pm_runtime_put_sync(&dev->plat_dev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03001811 v4l2_fh_del(&ctx->fh);
1812 v4l2_fh_exit(&ctx->fh);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001813 clear_bit(ctx->idx, &dev->instance_mask);
Philipp Zabel58b76772014-07-23 12:28:43 -03001814 if (ctx->ops->release)
1815 ctx->ops->release(ctx);
Philipp Zabele1519e82015-01-23 13:51:17 -03001816 debugfs_remove_recursive(ctx->debugfs_entry);
Javier Martin186b2502012-07-26 05:53:35 -03001817 kfree(ctx);
1818
1819 return 0;
1820}
1821
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001822static const struct v4l2_file_operations coda_fops = {
Javier Martin186b2502012-07-26 05:53:35 -03001823 .owner = THIS_MODULE,
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001824 .open = coda_open,
Javier Martin186b2502012-07-26 05:53:35 -03001825 .release = coda_release,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001826 .poll = v4l2_m2m_fop_poll,
Javier Martin186b2502012-07-26 05:53:35 -03001827 .unlocked_ioctl = video_ioctl2,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001828 .mmap = v4l2_m2m_fop_mmap,
Javier Martin186b2502012-07-26 05:53:35 -03001829};
1830
Philipp Zabel87048bb2012-07-02 07:03:43 -03001831static int coda_hw_init(struct coda_dev *dev)
Javier Martin186b2502012-07-26 05:53:35 -03001832{
Javier Martin186b2502012-07-26 05:53:35 -03001833 u32 data;
1834 u16 *p;
Fabio Estevam79830db2013-08-21 11:14:17 -03001835 int i, ret;
Javier Martin186b2502012-07-26 05:53:35 -03001836
Fabio Estevam79830db2013-08-21 11:14:17 -03001837 ret = clk_prepare_enable(dev->clk_per);
1838 if (ret)
Philipp Zabel1e172732014-07-11 06:36:23 -03001839 goto err_clk_per;
Fabio Estevam79830db2013-08-21 11:14:17 -03001840
1841 ret = clk_prepare_enable(dev->clk_ahb);
1842 if (ret)
1843 goto err_clk_ahb;
Javier Martin186b2502012-07-26 05:53:35 -03001844
Philipp Zabel8f452842014-07-11 06:36:35 -03001845 if (dev->rstc)
1846 reset_control_reset(dev->rstc);
1847
Javier Martin186b2502012-07-26 05:53:35 -03001848 /*
1849 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
Philipp Zabel87048bb2012-07-02 07:03:43 -03001850 * The 16-bit chars in the code buffer are in memory access
1851 * order, re-sort them to CODA order for register download.
Javier Martin186b2502012-07-26 05:53:35 -03001852 * Data in this SRAM survives a reboot.
1853 */
Philipp Zabel87048bb2012-07-02 07:03:43 -03001854 p = (u16 *)dev->codebuf.vaddr;
1855 if (dev->devtype->product == CODA_DX6) {
1856 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
1857 data = CODA_DOWN_ADDRESS_SET(i) |
1858 CODA_DOWN_DATA_SET(p[i ^ 1]);
1859 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
1860 }
1861 } else {
1862 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
1863 data = CODA_DOWN_ADDRESS_SET(i) |
1864 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
1865 3 - (i % 4)]);
1866 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
1867 }
Javier Martin186b2502012-07-26 05:53:35 -03001868 }
Javier Martin186b2502012-07-26 05:53:35 -03001869
Philipp Zabel9acf7692013-05-23 10:42:56 -03001870 /* Clear registers */
1871 for (i = 0; i < 64; i++)
1872 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
1873
Javier Martin186b2502012-07-26 05:53:35 -03001874 /* Tell the BIT where to find everything it needs */
Philipp Zabel89548442014-07-11 06:36:17 -03001875 if (dev->devtype->product == CODA_960 ||
1876 dev->devtype->product == CODA_7541) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001877 coda_write(dev, dev->tempbuf.paddr,
1878 CODA_REG_BIT_TEMP_BUF_ADDR);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001879 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001880 } else {
1881 coda_write(dev, dev->workbuf.paddr,
1882 CODA_REG_BIT_WORK_BUF_ADDR);
1883 }
Javier Martin186b2502012-07-26 05:53:35 -03001884 coda_write(dev, dev->codebuf.paddr,
1885 CODA_REG_BIT_CODE_BUF_ADDR);
1886 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
1887
1888 /* Set default values */
1889 switch (dev->devtype->product) {
1890 case CODA_DX6:
Philipp Zabelf23797b2014-08-06 08:02:23 -03001891 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH,
1892 CODA_REG_BIT_STREAM_CTRL);
Javier Martin186b2502012-07-26 05:53:35 -03001893 break;
1894 default:
Philipp Zabelf23797b2014-08-06 08:02:23 -03001895 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH,
1896 CODA_REG_BIT_STREAM_CTRL);
Javier Martin186b2502012-07-26 05:53:35 -03001897 }
Philipp Zabel89548442014-07-11 06:36:17 -03001898 if (dev->devtype->product == CODA_960)
1899 coda_write(dev, 1 << 12, CODA_REG_BIT_FRAME_MEM_CTRL);
1900 else
1901 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel10436672012-07-02 09:03:55 -03001902
1903 if (dev->devtype->product != CODA_DX6)
1904 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
1905
Javier Martin186b2502012-07-26 05:53:35 -03001906 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
1907 CODA_REG_BIT_INT_ENABLE);
1908
1909 /* Reset VPU and start processor */
1910 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
1911 data |= CODA_REG_RESET_ENABLE;
1912 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
1913 udelay(10);
1914 data &= ~CODA_REG_RESET_ENABLE;
1915 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
1916 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
1917
Philipp Zabelfe7554e2014-07-11 06:36:24 -03001918 clk_disable_unprepare(dev->clk_ahb);
1919 clk_disable_unprepare(dev->clk_per);
1920
1921 return 0;
1922
1923err_clk_ahb:
1924 clk_disable_unprepare(dev->clk_per);
1925err_clk_per:
1926 return ret;
1927}
1928
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001929static int coda_register_device(struct coda_dev *dev, int i)
Philipp Zabel121cacf2014-07-18 07:22:42 -03001930{
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001931 struct video_device *vfd = &dev->vfd[i];
1932
Dan Carpenter88ca3af2015-01-08 07:07:08 -03001933 if (i >= dev->devtype->num_vdevs)
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001934 return -EINVAL;
1935
Philipp Zabelbf249da2015-03-24 14:30:50 -03001936 strlcpy(vfd->name, dev->devtype->vdevs[i]->name, sizeof(vfd->name));
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001937 vfd->fops = &coda_fops;
1938 vfd->ioctl_ops = &coda_ioctl_ops;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001939 vfd->release = video_device_release_empty,
1940 vfd->lock = &dev->dev_mutex;
1941 vfd->v4l2_dev = &dev->v4l2_dev;
1942 vfd->vfl_dir = VFL_DIR_M2M;
1943 video_set_drvdata(vfd, dev);
1944
Philipp Zabela188a662014-08-05 14:00:20 -03001945 /* Not applicable, use the selection API instead */
1946 v4l2_disable_ioctl(vfd, VIDIOC_CROPCAP);
1947 v4l2_disable_ioctl(vfd, VIDIOC_G_CROP);
1948 v4l2_disable_ioctl(vfd, VIDIOC_S_CROP);
1949
Philipp Zabel121cacf2014-07-18 07:22:42 -03001950 return video_register_device(vfd, VFL_TYPE_GRABBER, 0);
1951}
1952
Javier Martin186b2502012-07-26 05:53:35 -03001953static void coda_fw_callback(const struct firmware *fw, void *context)
1954{
1955 struct coda_dev *dev = context;
1956 struct platform_device *pdev = dev->plat_dev;
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001957 int i, ret;
Javier Martin186b2502012-07-26 05:53:35 -03001958
1959 if (!fw) {
1960 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03001961 goto put_pm;
Javier Martin186b2502012-07-26 05:53:35 -03001962 }
1963
1964 /* allocate auxiliary per-device code buffer for the BIT processor */
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001965 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size, "codebuf",
1966 dev->debugfs_root);
Philipp Zabel6ba53b82015-03-24 14:30:54 -03001967 if (ret < 0)
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03001968 goto put_pm;
Javier Martin186b2502012-07-26 05:53:35 -03001969
Philipp Zabel87048bb2012-07-02 07:03:43 -03001970 /* Copy the whole firmware image to the code buffer */
1971 memcpy(dev->codebuf.vaddr, fw->data, fw->size);
1972 release_firmware(fw);
1973
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03001974 ret = coda_hw_init(dev);
1975 if (ret < 0) {
1976 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
1977 goto put_pm;
Javier Martin186b2502012-07-26 05:53:35 -03001978 }
1979
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03001980 ret = coda_check_firmware(dev);
1981 if (ret < 0)
1982 goto put_pm;
1983
Javier Martin186b2502012-07-26 05:53:35 -03001984 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
1985 if (IS_ERR(dev->alloc_ctx)) {
1986 v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03001987 goto put_pm;
Javier Martin186b2502012-07-26 05:53:35 -03001988 }
1989
1990 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
1991 if (IS_ERR(dev->m2m_dev)) {
1992 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
1993 goto rel_ctx;
1994 }
1995
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001996 for (i = 0; i < dev->devtype->num_vdevs; i++) {
1997 ret = coda_register_device(dev, i);
1998 if (ret) {
1999 v4l2_err(&dev->v4l2_dev,
2000 "Failed to register %s video device: %d\n",
2001 dev->devtype->vdevs[i]->name, ret);
2002 goto rel_vfd;
2003 }
Philipp Zabel121cacf2014-07-18 07:22:42 -03002004 }
2005
2006 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video[%d-%d]\n",
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002007 dev->vfd[0].num, dev->vfd[i - 1].num);
Javier Martin186b2502012-07-26 05:53:35 -03002008
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03002009 pm_runtime_put_sync(&pdev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03002010 return;
2011
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002012rel_vfd:
2013 while (--i >= 0)
2014 video_unregister_device(&dev->vfd[i]);
Javier Martin186b2502012-07-26 05:53:35 -03002015 v4l2_m2m_release(dev->m2m_dev);
2016rel_ctx:
2017 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03002018put_pm:
2019 pm_runtime_put_sync(&pdev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03002020}
2021
2022static int coda_firmware_request(struct coda_dev *dev)
2023{
2024 char *fw = dev->devtype->firmware;
2025
2026 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
2027 coda_product_name(dev->devtype->product));
2028
2029 return request_firmware_nowait(THIS_MODULE, true,
2030 fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback);
2031}
2032
2033enum coda_platform {
2034 CODA_IMX27,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03002035 CODA_IMX53,
Philipp Zabel89548442014-07-11 06:36:17 -03002036 CODA_IMX6Q,
2037 CODA_IMX6DL,
Javier Martin186b2502012-07-26 05:53:35 -03002038};
2039
Emil Goodec06d8752012-08-14 17:44:42 -03002040static const struct coda_devtype coda_devdata[] = {
Javier Martin186b2502012-07-26 05:53:35 -03002041 [CODA_IMX27] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002042 .firmware = "v4l-codadx6-imx27.bin",
2043 .product = CODA_DX6,
2044 .codecs = codadx6_codecs,
2045 .num_codecs = ARRAY_SIZE(codadx6_codecs),
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002046 .vdevs = codadx6_video_devices,
2047 .num_vdevs = ARRAY_SIZE(codadx6_video_devices),
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002048 .workbuf_size = 288 * 1024 + FMO_SLICE_SAVE_BUF_SIZE * 8 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03002049 .iram_size = 0xb000,
Javier Martin186b2502012-07-26 05:53:35 -03002050 },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03002051 [CODA_IMX53] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002052 .firmware = "v4l-coda7541-imx53.bin",
2053 .product = CODA_7541,
2054 .codecs = coda7_codecs,
2055 .num_codecs = ARRAY_SIZE(coda7_codecs),
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002056 .vdevs = coda7_video_devices,
2057 .num_vdevs = ARRAY_SIZE(coda7_video_devices),
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002058 .workbuf_size = 128 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03002059 .tempbuf_size = 304 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03002060 .iram_size = 0x14000,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03002061 },
Philipp Zabel89548442014-07-11 06:36:17 -03002062 [CODA_IMX6Q] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002063 .firmware = "v4l-coda960-imx6q.bin",
2064 .product = CODA_960,
2065 .codecs = coda9_codecs,
2066 .num_codecs = ARRAY_SIZE(coda9_codecs),
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002067 .vdevs = coda9_video_devices,
2068 .num_vdevs = ARRAY_SIZE(coda9_video_devices),
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002069 .workbuf_size = 80 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03002070 .tempbuf_size = 204 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03002071 .iram_size = 0x21000,
Philipp Zabel89548442014-07-11 06:36:17 -03002072 },
2073 [CODA_IMX6DL] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002074 .firmware = "v4l-coda960-imx6dl.bin",
2075 .product = CODA_960,
2076 .codecs = coda9_codecs,
2077 .num_codecs = ARRAY_SIZE(coda9_codecs),
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002078 .vdevs = coda9_video_devices,
2079 .num_vdevs = ARRAY_SIZE(coda9_video_devices),
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002080 .workbuf_size = 80 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03002081 .tempbuf_size = 204 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03002082 .iram_size = 0x20000,
Philipp Zabel89548442014-07-11 06:36:17 -03002083 },
Javier Martin186b2502012-07-26 05:53:35 -03002084};
2085
2086static struct platform_device_id coda_platform_ids[] = {
2087 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
2088 { /* sentinel */ }
2089};
2090MODULE_DEVICE_TABLE(platform, coda_platform_ids);
2091
2092#ifdef CONFIG_OF
2093static const struct of_device_id coda_dt_ids[] = {
Alexander Shiyan7b0dd9e2013-06-15 08:09:57 -03002094 { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03002095 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
Philipp Zabel89548442014-07-11 06:36:17 -03002096 { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
2097 { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
Javier Martin186b2502012-07-26 05:53:35 -03002098 { /* sentinel */ }
2099};
2100MODULE_DEVICE_TABLE(of, coda_dt_ids);
2101#endif
2102
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08002103static int coda_probe(struct platform_device *pdev)
Javier Martin186b2502012-07-26 05:53:35 -03002104{
2105 const struct of_device_id *of_id =
2106 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
2107 const struct platform_device_id *pdev_id;
Philipp Zabel657eee72013-04-29 16:17:14 -07002108 struct coda_platform_data *pdata = pdev->dev.platform_data;
2109 struct device_node *np = pdev->dev.of_node;
2110 struct gen_pool *pool;
Javier Martin186b2502012-07-26 05:53:35 -03002111 struct coda_dev *dev;
2112 struct resource *res;
2113 int ret, irq;
2114
Philipp Zabelf23797b2014-08-06 08:02:23 -03002115 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
Philipp Zabel6da999d2014-09-29 09:53:43 -03002116 if (!dev)
Javier Martin186b2502012-07-26 05:53:35 -03002117 return -ENOMEM;
Javier Martin186b2502012-07-26 05:53:35 -03002118
Philipp Zabelb2f91ae2014-10-02 14:08:27 -03002119 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
2120
Fabio Estevamb7bd6602014-10-04 16:40:50 -03002121 if (of_id) {
Philipp Zabelb2f91ae2014-10-02 14:08:27 -03002122 dev->devtype = of_id->data;
Fabio Estevamb7bd6602014-10-04 16:40:50 -03002123 } else if (pdev_id) {
Philipp Zabelb2f91ae2014-10-02 14:08:27 -03002124 dev->devtype = &coda_devdata[pdev_id->driver_data];
Fabio Estevamb7bd6602014-10-04 16:40:50 -03002125 } else {
2126 ret = -EINVAL;
2127 goto err_v4l2_register;
Javier Martin186b2502012-07-26 05:53:35 -03002128 }
2129
2130 spin_lock_init(&dev->irqlock);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002131 INIT_LIST_HEAD(&dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03002132
2133 dev->plat_dev = pdev;
2134 dev->clk_per = devm_clk_get(&pdev->dev, "per");
2135 if (IS_ERR(dev->clk_per)) {
2136 dev_err(&pdev->dev, "Could not get per clock\n");
2137 return PTR_ERR(dev->clk_per);
2138 }
2139
2140 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
2141 if (IS_ERR(dev->clk_ahb)) {
2142 dev_err(&pdev->dev, "Could not get ahb clock\n");
2143 return PTR_ERR(dev->clk_ahb);
2144 }
2145
2146 /* Get memory for physical registers */
2147 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Philipp Zabel611cbbf2013-05-21 04:19:27 -03002148 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
2149 if (IS_ERR(dev->regs_base))
2150 return PTR_ERR(dev->regs_base);
Javier Martin186b2502012-07-26 05:53:35 -03002151
2152 /* IRQ */
Philipp Zabel540b72e2014-08-05 14:00:09 -03002153 irq = platform_get_irq_byname(pdev, "bit");
2154 if (irq < 0)
2155 irq = platform_get_irq(pdev, 0);
Javier Martin186b2502012-07-26 05:53:35 -03002156 if (irq < 0) {
2157 dev_err(&pdev->dev, "failed to get irq resource\n");
Fabio Estevam7d377432014-06-04 15:46:23 -03002158 return irq;
Javier Martin186b2502012-07-26 05:53:35 -03002159 }
2160
Fabio Estevam08c8d142014-06-04 15:46:24 -03002161 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
2162 IRQF_ONESHOT, dev_name(&pdev->dev), dev);
2163 if (ret < 0) {
2164 dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
2165 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03002166 }
2167
Philipp Zabelee27aa92014-07-24 16:50:03 -03002168 dev->rstc = devm_reset_control_get_optional(&pdev->dev, NULL);
Philipp Zabel8f452842014-07-11 06:36:35 -03002169 if (IS_ERR(dev->rstc)) {
2170 ret = PTR_ERR(dev->rstc);
Philipp Zabelee27aa92014-07-24 16:50:03 -03002171 if (ret == -ENOENT || ret == -ENOSYS) {
Philipp Zabel8f452842014-07-11 06:36:35 -03002172 dev->rstc = NULL;
2173 } else {
Philipp Zabelf23797b2014-08-06 08:02:23 -03002174 dev_err(&pdev->dev, "failed get reset control: %d\n",
2175 ret);
Philipp Zabel8f452842014-07-11 06:36:35 -03002176 return ret;
2177 }
2178 }
2179
Philipp Zabel657eee72013-04-29 16:17:14 -07002180 /* Get IRAM pool from device tree or platform data */
Vladimir Zapolskiyabdd4a72015-06-30 15:00:07 -07002181 pool = of_gen_pool_get(np, "iram", 0);
Philipp Zabel657eee72013-04-29 16:17:14 -07002182 if (!pool && pdata)
Vladimir Zapolskiy73858172015-09-04 15:47:43 -07002183 pool = gen_pool_get(pdata->iram_dev, NULL);
Philipp Zabel657eee72013-04-29 16:17:14 -07002184 if (!pool) {
2185 dev_err(&pdev->dev, "iram pool not available\n");
2186 return -ENOMEM;
2187 }
2188 dev->iram_pool = pool;
2189
Javier Martin186b2502012-07-26 05:53:35 -03002190 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
2191 if (ret)
2192 return ret;
2193
2194 mutex_init(&dev->dev_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002195 mutex_init(&dev->coda_mutex);
Javier Martin186b2502012-07-26 05:53:35 -03002196
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002197 dev->debugfs_root = debugfs_create_dir("coda", NULL);
2198 if (!dev->debugfs_root)
2199 dev_warn(&pdev->dev, "failed to create debugfs root\n");
2200
Javier Martin186b2502012-07-26 05:53:35 -03002201 /* allocate auxiliary per-device buffers for the BIT processor */
Philipp Zabel5d73fad2014-07-11 06:36:42 -03002202 if (dev->devtype->product == CODA_DX6) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002203 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002204 dev->devtype->workbuf_size, "workbuf",
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002205 dev->debugfs_root);
Philipp Zabel6ba53b82015-03-24 14:30:54 -03002206 if (ret < 0)
Fabio Estevamb7bd6602014-10-04 16:40:50 -03002207 goto err_v4l2_register;
Javier Martin186b2502012-07-26 05:53:35 -03002208 }
Philipp Zabel5d73fad2014-07-11 06:36:42 -03002209
2210 if (dev->devtype->tempbuf_size) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002211 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03002212 dev->devtype->tempbuf_size, "tempbuf",
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002213 dev->debugfs_root);
Philipp Zabel6ba53b82015-03-24 14:30:54 -03002214 if (ret < 0)
Fabio Estevamb7bd6602014-10-04 16:40:50 -03002215 goto err_v4l2_register;
Javier Martin186b2502012-07-26 05:53:35 -03002216 }
2217
Philipp Zabel401e9722014-07-11 06:36:43 -03002218 dev->iram.size = dev->devtype->iram_size;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03002219 dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
2220 &dev->iram.paddr);
2221 if (!dev->iram.vaddr) {
Philipp Zabel8be31c82014-08-05 14:00:13 -03002222 dev_warn(&pdev->dev, "unable to alloc iram\n");
2223 } else {
Philipp Zabel811a6932015-01-23 13:51:23 -03002224 memset(dev->iram.vaddr, 0, dev->iram.size);
Philipp Zabel8be31c82014-08-05 14:00:13 -03002225 dev->iram.blob.data = dev->iram.vaddr;
2226 dev->iram.blob.size = dev->iram.size;
2227 dev->iram.dentry = debugfs_create_blob("iram", 0644,
2228 dev->debugfs_root,
2229 &dev->iram.blob);
Philipp Zabel10436672012-07-02 09:03:55 -03002230 }
2231
Philipp Zabel32b6f202014-07-11 06:36:20 -03002232 dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
2233 if (!dev->workqueue) {
2234 dev_err(&pdev->dev, "unable to alloc workqueue\n");
Fabio Estevam74d08d52014-10-04 16:40:51 -03002235 ret = -ENOMEM;
2236 goto err_v4l2_register;
Philipp Zabel32b6f202014-07-11 06:36:20 -03002237 }
2238
Javier Martin186b2502012-07-26 05:53:35 -03002239 platform_set_drvdata(pdev, dev);
2240
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03002241 /*
2242 * Start activated so we can directly call coda_hw_init in
Rafael J. Wysockie243c7c2014-12-04 01:10:10 +01002243 * coda_fw_callback regardless of whether CONFIG_PM is
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03002244 * enabled or whether the device is associated with a PM domain.
2245 */
2246 pm_runtime_get_noresume(&pdev->dev);
2247 pm_runtime_set_active(&pdev->dev);
Philipp Zabel1e172732014-07-11 06:36:23 -03002248 pm_runtime_enable(&pdev->dev);
2249
Javier Martin186b2502012-07-26 05:53:35 -03002250 return coda_firmware_request(dev);
Fabio Estevamb7bd6602014-10-04 16:40:50 -03002251
2252err_v4l2_register:
2253 v4l2_device_unregister(&dev->v4l2_dev);
2254 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03002255}
2256
2257static int coda_remove(struct platform_device *pdev)
2258{
2259 struct coda_dev *dev = platform_get_drvdata(pdev);
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002260 int i;
Javier Martin186b2502012-07-26 05:53:35 -03002261
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002262 for (i = 0; i < ARRAY_SIZE(dev->vfd); i++) {
2263 if (video_get_drvdata(&dev->vfd[i]))
2264 video_unregister_device(&dev->vfd[i]);
2265 }
Javier Martin186b2502012-07-26 05:53:35 -03002266 if (dev->m2m_dev)
2267 v4l2_m2m_release(dev->m2m_dev);
Philipp Zabel1e172732014-07-11 06:36:23 -03002268 pm_runtime_disable(&pdev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03002269 if (dev->alloc_ctx)
2270 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
2271 v4l2_device_unregister(&dev->v4l2_dev);
Philipp Zabel32b6f202014-07-11 06:36:20 -03002272 destroy_workqueue(dev->workqueue);
Philipp Zabelb313bcc2014-07-11 06:36:16 -03002273 if (dev->iram.vaddr)
2274 gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
2275 dev->iram.size);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002276 coda_free_aux_buf(dev, &dev->codebuf);
2277 coda_free_aux_buf(dev, &dev->tempbuf);
2278 coda_free_aux_buf(dev, &dev->workbuf);
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002279 debugfs_remove_recursive(dev->debugfs_root);
Javier Martin186b2502012-07-26 05:53:35 -03002280 return 0;
2281}
2282
Rafael J. Wysockie243c7c2014-12-04 01:10:10 +01002283#ifdef CONFIG_PM
Philipp Zabel1e172732014-07-11 06:36:23 -03002284static int coda_runtime_resume(struct device *dev)
2285{
2286 struct coda_dev *cdev = dev_get_drvdata(dev);
2287 int ret = 0;
2288
Philipp Zabel65919e62014-07-18 07:22:36 -03002289 if (dev->pm_domain && cdev->codebuf.vaddr) {
Philipp Zabel1e172732014-07-11 06:36:23 -03002290 ret = coda_hw_init(cdev);
2291 if (ret)
2292 v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
2293 }
2294
2295 return ret;
2296}
2297#endif
2298
2299static const struct dev_pm_ops coda_pm_ops = {
2300 SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
2301};
2302
Javier Martin186b2502012-07-26 05:53:35 -03002303static struct platform_driver coda_driver = {
2304 .probe = coda_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08002305 .remove = coda_remove,
Javier Martin186b2502012-07-26 05:53:35 -03002306 .driver = {
2307 .name = CODA_NAME,
Javier Martin186b2502012-07-26 05:53:35 -03002308 .of_match_table = of_match_ptr(coda_dt_ids),
Philipp Zabel1e172732014-07-11 06:36:23 -03002309 .pm = &coda_pm_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002310 },
2311 .id_table = coda_platform_ids,
2312};
2313
2314module_platform_driver(coda_driver);
2315
2316MODULE_LICENSE("GPL");
2317MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
2318MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");