blob: fa3ed74af116c0310265fe28878b1348d1083057 [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 Zabel4ec319e2017-01-20 12:00:21 -0200569static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f,
570 struct v4l2_rect *r)
Javier Martin186b2502012-07-26 05:53:35 -0300571{
572 struct coda_q_data *q_data;
573 struct vb2_queue *vq;
Javier Martin186b2502012-07-26 05:53:35 -0300574
Philipp Zabel14604e32014-07-11 06:36:22 -0300575 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
Javier Martin186b2502012-07-26 05:53:35 -0300576 if (!vq)
577 return -EINVAL;
578
579 q_data = get_q_data(ctx, f->type);
580 if (!q_data)
581 return -EINVAL;
582
583 if (vb2_is_busy(vq)) {
584 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
585 return -EBUSY;
586 }
587
Philipp Zabelb96904e2013-05-23 10:42:58 -0300588 q_data->fourcc = f->fmt.pix.pixelformat;
Javier Martin186b2502012-07-26 05:53:35 -0300589 q_data->width = f->fmt.pix.width;
590 q_data->height = f->fmt.pix.height;
Philipp Zabel2fd6a372014-07-11 06:36:36 -0300591 q_data->bytesperline = f->fmt.pix.bytesperline;
Philipp Zabel451d43a2012-07-26 09:18:27 -0300592 q_data->sizeimage = f->fmt.pix.sizeimage;
Philipp Zabel4ec319e2017-01-20 12:00:21 -0200593 if (r) {
594 q_data->rect = *r;
595 } else {
596 q_data->rect.left = 0;
597 q_data->rect.top = 0;
598 q_data->rect.width = f->fmt.pix.width;
599 q_data->rect.height = f->fmt.pix.height;
600 }
Javier Martin186b2502012-07-26 05:53:35 -0300601
Philipp Zabela269e532015-07-16 13:19:38 -0300602 switch (f->fmt.pix.pixelformat) {
603 case V4L2_PIX_FMT_NV12:
604 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
605 ctx->tiled_map_type = GDI_TILED_FRAME_MB_RASTER_MAP;
606 if (!disable_tiling)
607 break;
608 }
609 /* else fall through */
610 case V4L2_PIX_FMT_YUV420:
611 case V4L2_PIX_FMT_YVU420:
612 ctx->tiled_map_type = GDI_LINEAR_FRAME_MAP;
613 break;
614 default:
615 break;
616 }
617
Javier Martin186b2502012-07-26 05:53:35 -0300618 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
619 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
Philipp Zabelb96904e2013-05-23 10:42:58 -0300620 f->type, q_data->width, q_data->height, q_data->fourcc);
Javier Martin186b2502012-07-26 05:53:35 -0300621
622 return 0;
623}
624
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300625static int coda_s_fmt_vid_cap(struct file *file, void *priv,
626 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300627{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300628 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel4ec319e2017-01-20 12:00:21 -0200629 struct coda_q_data *q_data_src;
630 struct v4l2_rect r;
Javier Martin186b2502012-07-26 05:53:35 -0300631 int ret;
632
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300633 ret = coda_try_fmt_vid_cap(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300634 if (ret)
635 return ret;
636
Philipp Zabel4ec319e2017-01-20 12:00:21 -0200637 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
638 r.left = 0;
639 r.top = 0;
640 r.width = q_data_src->width;
641 r.height = q_data_src->height;
642
643 return coda_s_fmt(ctx, f, &r);
Javier Martin186b2502012-07-26 05:53:35 -0300644}
645
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300646static int coda_s_fmt_vid_out(struct file *file, void *priv,
647 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300648{
649 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel4ec319e2017-01-20 12:00:21 -0200650 struct coda_q_data *q_data_src;
Philipp Zabelf95a6ce2014-08-05 14:00:19 -0300651 struct v4l2_format f_cap;
Philipp Zabel4ec319e2017-01-20 12:00:21 -0200652 struct v4l2_rect r;
Javier Martin186b2502012-07-26 05:53:35 -0300653 int ret;
654
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300655 ret = coda_try_fmt_vid_out(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300656 if (ret)
657 return ret;
658
Philipp Zabel4ec319e2017-01-20 12:00:21 -0200659 ret = coda_s_fmt(ctx, f, NULL);
Javier Martin186b2502012-07-26 05:53:35 -0300660 if (ret)
Philipp Zabel2dc546d2014-08-05 14:00:18 -0300661 return ret;
662
663 ctx->colorspace = f->fmt.pix.colorspace;
Javier Martin186b2502012-07-26 05:53:35 -0300664
Philipp Zabel1c2f6a92015-01-23 13:51:22 -0300665 memset(&f_cap, 0, sizeof(f_cap));
Philipp Zabelf95a6ce2014-08-05 14:00:19 -0300666 f_cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
667 coda_g_fmt(file, priv, &f_cap);
668 f_cap.fmt.pix.width = f->fmt.pix.width;
669 f_cap.fmt.pix.height = f->fmt.pix.height;
670
671 ret = coda_try_fmt_vid_cap(file, priv, &f_cap);
672 if (ret)
673 return ret;
674
Philipp Zabel4ec319e2017-01-20 12:00:21 -0200675 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
676 r.left = 0;
677 r.top = 0;
678 r.width = q_data_src->width;
679 r.height = q_data_src->height;
680
681 return coda_s_fmt(ctx, &f_cap, &r);
Javier Martin186b2502012-07-26 05:53:35 -0300682}
683
Philipp Zabel73751da2015-03-24 14:30:51 -0300684static int coda_reqbufs(struct file *file, void *priv,
685 struct v4l2_requestbuffers *rb)
686{
687 struct coda_ctx *ctx = fh_to_ctx(priv);
688 int ret;
689
690 ret = v4l2_m2m_reqbufs(file, ctx->fh.m2m_ctx, rb);
691 if (ret)
692 return ret;
693
694 /*
695 * Allow to allocate instance specific per-context buffers, such as
696 * bitstream ringbuffer, slice buffer, work buffer, etc. if needed.
697 */
698 if (rb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT && ctx->ops->reqbufs)
699 return ctx->ops->reqbufs(ctx, rb);
700
701 return 0;
702}
703
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300704static int coda_qbuf(struct file *file, void *priv,
705 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300706{
707 struct coda_ctx *ctx = fh_to_ctx(priv);
708
Philipp Zabel14604e32014-07-11 06:36:22 -0300709 return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
Javier Martin186b2502012-07-26 05:53:35 -0300710}
711
Philipp Zabel918c66f2013-06-27 06:59:01 -0300712static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx,
Junghak Sung2d700712015-09-22 10:30:30 -0300713 struct vb2_v4l2_buffer *buf)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300714{
715 struct vb2_queue *src_vq;
716
Philipp Zabel14604e32014-07-11 06:36:22 -0300717 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300718
719 return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) &&
Junghak Sung2d700712015-09-22 10:30:30 -0300720 (buf->sequence == (ctx->qsequence - 1)));
Philipp Zabel918c66f2013-06-27 06:59:01 -0300721}
722
Junghak Sung2d700712015-09-22 10:30:30 -0300723void coda_m2m_buf_done(struct coda_ctx *ctx, struct vb2_v4l2_buffer *buf,
Philipp Zabel9f2bfb32015-05-04 07:51:07 -0300724 enum vb2_buffer_state state)
Javier Martin186b2502012-07-26 05:53:35 -0300725{
Philipp Zabel9f2bfb32015-05-04 07:51:07 -0300726 const struct v4l2_event eos_event = {
727 .type = V4L2_EVENT_EOS
728 };
Javier Martin186b2502012-07-26 05:53:35 -0300729
Philipp Zabel9f2bfb32015-05-04 07:51:07 -0300730 if (coda_buf_is_end_of_stream(ctx, buf)) {
Junghak Sung2d700712015-09-22 10:30:30 -0300731 buf->flags |= V4L2_BUF_FLAG_LAST;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300732
733 v4l2_event_queue_fh(&ctx->fh, &eos_event);
734 }
735
Philipp Zabel9f2bfb32015-05-04 07:51:07 -0300736 v4l2_m2m_buf_done(buf, state);
Javier Martin186b2502012-07-26 05:53:35 -0300737}
738
Philipp Zabel52c41672014-07-11 06:36:19 -0300739static int coda_g_selection(struct file *file, void *fh,
740 struct v4l2_selection *s)
741{
742 struct coda_ctx *ctx = fh_to_ctx(fh);
743 struct coda_q_data *q_data;
744 struct v4l2_rect r, *rsel;
745
746 q_data = get_q_data(ctx, s->type);
747 if (!q_data)
748 return -EINVAL;
749
750 r.left = 0;
751 r.top = 0;
752 r.width = q_data->width;
753 r.height = q_data->height;
754 rsel = &q_data->rect;
755
756 switch (s->target) {
757 case V4L2_SEL_TGT_CROP_DEFAULT:
758 case V4L2_SEL_TGT_CROP_BOUNDS:
759 rsel = &r;
760 /* fallthrough */
761 case V4L2_SEL_TGT_CROP:
762 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
763 return -EINVAL;
764 break;
765 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
766 case V4L2_SEL_TGT_COMPOSE_PADDED:
767 rsel = &r;
768 /* fallthrough */
769 case V4L2_SEL_TGT_COMPOSE:
770 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
771 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
772 return -EINVAL;
773 break;
774 default:
775 return -EINVAL;
776 }
777
778 s->r = *rsel;
779
780 return 0;
781}
782
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300783static int coda_try_decoder_cmd(struct file *file, void *fh,
784 struct v4l2_decoder_cmd *dc)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300785{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300786 if (dc->cmd != V4L2_DEC_CMD_STOP)
787 return -EINVAL;
788
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300789 if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300790 return -EINVAL;
791
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300792 if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0))
Philipp Zabel918c66f2013-06-27 06:59:01 -0300793 return -EINVAL;
794
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300795 return 0;
796}
797
798static int coda_decoder_cmd(struct file *file, void *fh,
799 struct v4l2_decoder_cmd *dc)
800{
801 struct coda_ctx *ctx = fh_to_ctx(fh);
802 int ret;
803
804 ret = coda_try_decoder_cmd(file, fh, dc);
805 if (ret < 0)
806 return ret;
807
808 /* Ignore decoder stop command silently in encoder context */
Philipp Zabel918c66f2013-06-27 06:59:01 -0300809 if (ctx->inst_type != CODA_INST_DECODER)
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300810 return 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300811
Philipp Zabel347bb7f2014-07-23 12:28:42 -0300812 /* Set the stream-end flag on this context */
813 coda_bit_stream_end_flag(ctx);
Philipp Zabel84e23652014-07-11 06:36:34 -0300814 ctx->hold = false;
Michael Olbrichf3497da2014-07-11 06:36:30 -0300815 v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
Philipp Zabel89548442014-07-11 06:36:17 -0300816
Philipp Zabel918c66f2013-06-27 06:59:01 -0300817 return 0;
818}
819
Philipp Zabelcde29ef2015-07-16 13:13:24 -0300820static int coda_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
821{
822 struct coda_ctx *ctx = fh_to_ctx(fh);
823 struct v4l2_fract *tpf;
824
825 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
826 return -EINVAL;
827
828 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
829 tpf = &a->parm.output.timeperframe;
830 tpf->denominator = ctx->params.framerate & CODA_FRATE_RES_MASK;
831 tpf->numerator = 1 + (ctx->params.framerate >>
832 CODA_FRATE_DIV_OFFSET);
833
834 return 0;
835}
836
837/*
838 * Approximate timeperframe v4l2_fract with values that can be written
839 * into the 16-bit CODA_FRATE_DIV and CODA_FRATE_RES fields.
840 */
841static void coda_approximate_timeperframe(struct v4l2_fract *timeperframe)
842{
843 struct v4l2_fract s = *timeperframe;
844 struct v4l2_fract f0;
845 struct v4l2_fract f1 = { 1, 0 };
846 struct v4l2_fract f2 = { 0, 1 };
847 unsigned int i, div, s_denominator;
848
849 /* Lower bound is 1/65535 */
850 if (s.numerator == 0 || s.denominator / s.numerator > 65535) {
851 timeperframe->numerator = 1;
852 timeperframe->denominator = 65535;
853 return;
854 }
855
856 /* Upper bound is 65536/1, map everything above to infinity */
857 if (s.denominator == 0 || s.numerator / s.denominator > 65536) {
858 timeperframe->numerator = 1;
859 timeperframe->denominator = 0;
860 return;
861 }
862
863 /* Reduce fraction to lowest terms */
864 div = gcd(s.numerator, s.denominator);
865 if (div > 1) {
866 s.numerator /= div;
867 s.denominator /= div;
868 }
869
870 if (s.numerator <= 65536 && s.denominator < 65536) {
871 *timeperframe = s;
872 return;
873 }
874
875 /* Find successive convergents from continued fraction expansion */
876 while (f2.numerator <= 65536 && f2.denominator < 65536) {
877 f0 = f1;
878 f1 = f2;
879
880 /* Stop when f2 exactly equals timeperframe */
881 if (s.numerator == 0)
882 break;
883
884 i = s.denominator / s.numerator;
885
886 f2.numerator = f0.numerator + i * f1.numerator;
887 f2.denominator = f0.denominator + i * f2.denominator;
888
889 s_denominator = s.numerator;
890 s.numerator = s.denominator % s.numerator;
891 s.denominator = s_denominator;
892 }
893
894 *timeperframe = f1;
895}
896
897static uint32_t coda_timeperframe_to_frate(struct v4l2_fract *timeperframe)
898{
899 return ((timeperframe->numerator - 1) << CODA_FRATE_DIV_OFFSET) |
900 timeperframe->denominator;
901}
902
903static int coda_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
904{
905 struct coda_ctx *ctx = fh_to_ctx(fh);
906 struct v4l2_fract *tpf;
907
908 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
909 return -EINVAL;
910
911 tpf = &a->parm.output.timeperframe;
912 coda_approximate_timeperframe(tpf);
913 ctx->params.framerate = coda_timeperframe_to_frate(tpf);
914
915 return 0;
916}
917
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300918static int coda_subscribe_event(struct v4l2_fh *fh,
919 const struct v4l2_event_subscription *sub)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300920{
921 switch (sub->type) {
922 case V4L2_EVENT_EOS:
923 return v4l2_event_subscribe(fh, sub, 0, NULL);
924 default:
925 return v4l2_ctrl_subscribe_event(fh, sub);
926 }
Javier Martin186b2502012-07-26 05:53:35 -0300927}
928
929static const struct v4l2_ioctl_ops coda_ioctl_ops = {
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300930 .vidioc_querycap = coda_querycap,
Javier Martin186b2502012-07-26 05:53:35 -0300931
Philipp Zabel22e244b2014-07-18 07:22:43 -0300932 .vidioc_enum_fmt_vid_cap = coda_enum_fmt,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300933 .vidioc_g_fmt_vid_cap = coda_g_fmt,
934 .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
935 .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap,
Javier Martin186b2502012-07-26 05:53:35 -0300936
Philipp Zabel22e244b2014-07-18 07:22:43 -0300937 .vidioc_enum_fmt_vid_out = coda_enum_fmt,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300938 .vidioc_g_fmt_vid_out = coda_g_fmt,
939 .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
940 .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out,
Javier Martin186b2502012-07-26 05:53:35 -0300941
Philipp Zabel73751da2015-03-24 14:30:51 -0300942 .vidioc_reqbufs = coda_reqbufs,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300943 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
Javier Martin186b2502012-07-26 05:53:35 -0300944
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300945 .vidioc_qbuf = coda_qbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300946 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
Philipp Zabel9f2bfb32015-05-04 07:51:07 -0300947 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300948 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
Philipp Zabelbb757d72015-12-02 14:58:52 -0200949 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
Javier Martin186b2502012-07-26 05:53:35 -0300950
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300951 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
952 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300953
Philipp Zabel52c41672014-07-11 06:36:19 -0300954 .vidioc_g_selection = coda_g_selection,
955
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300956 .vidioc_try_decoder_cmd = coda_try_decoder_cmd,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300957 .vidioc_decoder_cmd = coda_decoder_cmd,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300958
Philipp Zabelcde29ef2015-07-16 13:13:24 -0300959 .vidioc_g_parm = coda_g_parm,
960 .vidioc_s_parm = coda_s_parm,
961
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300962 .vidioc_subscribe_event = coda_subscribe_event,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300963 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Javier Martin186b2502012-07-26 05:53:35 -0300964};
965
966/*
967 * Mem-to-mem operations.
968 */
Philipp Zabel477c1cf2013-06-21 03:55:33 -0300969
970static void coda_device_run(void *m2m_priv)
971{
972 struct coda_ctx *ctx = m2m_priv;
973 struct coda_dev *dev = ctx->dev;
Philipp Zabel32b6f202014-07-11 06:36:20 -0300974
975 queue_work(dev->workqueue, &ctx->pic_run_work);
976}
977
Philipp Zabel32b6f202014-07-11 06:36:20 -0300978static void coda_pic_run_work(struct work_struct *work)
979{
980 struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
981 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300982 int ret;
983
984 mutex_lock(&ctx->buffer_mutex);
Philipp Zabel477c1cf2013-06-21 03:55:33 -0300985 mutex_lock(&dev->coda_mutex);
986
Philipp Zabela1192a12014-07-23 12:28:40 -0300987 ret = ctx->ops->prepare_run(ctx);
988 if (ret < 0 && ctx->inst_type == CODA_INST_DECODER) {
989 mutex_unlock(&dev->coda_mutex);
990 mutex_unlock(&ctx->buffer_mutex);
991 /* job_finish scheduled by prepare_decode */
992 return;
Philipp Zabel10436672012-07-02 09:03:55 -0300993 }
994
Philipp Zabelf23797b2014-08-06 08:02:23 -0300995 if (!wait_for_completion_timeout(&ctx->completion,
996 msecs_to_jiffies(1000))) {
Philipp Zabel32b6f202014-07-11 06:36:20 -0300997 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout\n");
Philipp Zabel84e23652014-07-11 06:36:34 -0300998
999 ctx->hold = true;
Philipp Zabel8f452842014-07-11 06:36:35 -03001000
1001 coda_hw_reset(ctx);
Philipp Zabel32b6f202014-07-11 06:36:20 -03001002 } else if (!ctx->aborting) {
Philipp Zabela1192a12014-07-23 12:28:40 -03001003 ctx->ops->finish_run(ctx);
Philipp Zabel32b6f202014-07-11 06:36:20 -03001004 }
1005
Philipp Zabel747d7642015-01-23 13:51:31 -03001006 if ((ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out)) &&
1007 ctx->ops->seq_end_work)
Philipp Zabel32b6f202014-07-11 06:36:20 -03001008 queue_work(dev->workqueue, &ctx->seq_end_work);
1009
1010 mutex_unlock(&dev->coda_mutex);
1011 mutex_unlock(&ctx->buffer_mutex);
1012
Philipp Zabel14604e32014-07-11 06:36:22 -03001013 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001014}
1015
1016static int coda_job_ready(void *m2m_priv)
1017{
1018 struct coda_ctx *ctx = m2m_priv;
Philipp Zabel2cf251c2015-07-09 07:10:20 -03001019 int src_bufs = v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001020
1021 /*
1022 * For both 'P' and 'key' frame cases 1 picture
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001023 * and 1 frame are needed. In the decoder case,
1024 * the compressed frame can be in the bitstream.
Javier Martin186b2502012-07-26 05:53:35 -03001025 */
Philipp Zabel2cf251c2015-07-09 07:10:20 -03001026 if (!src_bufs && ctx->inst_type != CODA_INST_DECODER) {
Javier Martin186b2502012-07-26 05:53:35 -03001027 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1028 "not ready: not enough video buffers.\n");
1029 return 0;
1030 }
1031
Philipp Zabel14604e32014-07-11 06:36:22 -03001032 if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001033 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1034 "not ready: not enough video capture buffers.\n");
1035 return 0;
1036 }
1037
Philipp Zabela22496c2015-01-23 13:51:33 -03001038 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
Philipp Zabel47f3fa62015-07-09 07:10:21 -03001039 bool stream_end = ctx->bit_stream_param &
1040 CODA_BIT_STREAM_END_FLAG;
1041 int num_metas = ctx->num_metas;
Philipp Zabelf77fd8a2015-01-23 13:51:20 -03001042
Philipp Zabel2cf251c2015-07-09 07:10:20 -03001043 if (ctx->hold && !src_bufs) {
Philipp Zabelf77fd8a2015-01-23 13:51:20 -03001044 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1045 "%d: not ready: on hold for more buffers.\n",
1046 ctx->idx);
1047 return 0;
1048 }
1049
Philipp Zabelf77fd8a2015-01-23 13:51:20 -03001050 if (!stream_end && (num_metas + src_bufs) < 2) {
1051 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1052 "%d: not ready: need 2 buffers available (%d, %d)\n",
1053 ctx->idx, num_metas, src_bufs);
1054 return 0;
1055 }
1056
1057
Philipp Zabel2cf251c2015-07-09 07:10:20 -03001058 if (!src_bufs && !stream_end &&
1059 (coda_get_bitstream_payload(ctx) < 512)) {
Philipp Zabelf77fd8a2015-01-23 13:51:20 -03001060 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1061 "%d: not ready: not enough bitstream data (%d).\n",
1062 ctx->idx, coda_get_bitstream_payload(ctx));
1063 return 0;
1064 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001065 }
1066
Philipp Zabel3e748262013-05-23 10:43:01 -03001067 if (ctx->aborting) {
1068 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1069 "not ready: aborting\n");
1070 return 0;
1071 }
1072
Javier Martin186b2502012-07-26 05:53:35 -03001073 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1074 "job ready\n");
Philipp Zabel47f3fa62015-07-09 07:10:21 -03001075
Javier Martin186b2502012-07-26 05:53:35 -03001076 return 1;
1077}
1078
1079static void coda_job_abort(void *priv)
1080{
1081 struct coda_ctx *ctx = priv;
Javier Martin186b2502012-07-26 05:53:35 -03001082
1083 ctx->aborting = 1;
1084
1085 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1086 "Aborting task\n");
Javier Martin186b2502012-07-26 05:53:35 -03001087}
1088
1089static void coda_lock(void *m2m_priv)
1090{
1091 struct coda_ctx *ctx = m2m_priv;
1092 struct coda_dev *pcdev = ctx->dev;
Philipp Zabelf23797b2014-08-06 08:02:23 -03001093
Javier Martin186b2502012-07-26 05:53:35 -03001094 mutex_lock(&pcdev->dev_mutex);
1095}
1096
1097static void coda_unlock(void *m2m_priv)
1098{
1099 struct coda_ctx *ctx = m2m_priv;
1100 struct coda_dev *pcdev = ctx->dev;
Philipp Zabelf23797b2014-08-06 08:02:23 -03001101
Javier Martin186b2502012-07-26 05:53:35 -03001102 mutex_unlock(&pcdev->dev_mutex);
1103}
1104
Philipp Zabel814c3762014-07-18 07:22:45 -03001105static const struct v4l2_m2m_ops coda_m2m_ops = {
Javier Martin186b2502012-07-26 05:53:35 -03001106 .device_run = coda_device_run,
1107 .job_ready = coda_job_ready,
1108 .job_abort = coda_job_abort,
1109 .lock = coda_lock,
1110 .unlock = coda_unlock,
1111};
1112
1113static void set_default_params(struct coda_ctx *ctx)
1114{
Philipp Zabel8e428d52015-01-23 13:51:29 -03001115 unsigned int max_w, max_h, usize, csize;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001116
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001117 ctx->codec = coda_find_codec(ctx->dev, ctx->cvd->src_formats[0],
1118 ctx->cvd->dst_formats[0]);
Philipp Zabeld4c6a412014-10-02 14:08:35 -03001119 max_w = min(ctx->codec->max_w, 1920U);
1120 max_h = min(ctx->codec->max_h, 1088U);
Philipp Zabel8e428d52015-01-23 13:51:29 -03001121 usize = max_w * max_h * 3 / 2;
1122 csize = coda_estimate_sizeimage(ctx, usize, max_w, max_h);
Javier Martin186b2502012-07-26 05:53:35 -03001123
Philipp Zabel121cacf2014-07-18 07:22:42 -03001124 ctx->params.codec_mode = ctx->codec->mode;
Javier Martin186b2502012-07-26 05:53:35 -03001125 ctx->colorspace = V4L2_COLORSPACE_REC709;
1126 ctx->params.framerate = 30;
Javier Martin186b2502012-07-26 05:53:35 -03001127
1128 /* Default formats for output and input queues */
Philipp Zabel6727d4f2015-07-16 13:19:39 -03001129 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->cvd->src_formats[0];
1130 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->cvd->dst_formats[0];
Philipp Zabelb96904e2013-05-23 10:42:58 -03001131 ctx->q_data[V4L2_M2M_SRC].width = max_w;
1132 ctx->q_data[V4L2_M2M_SRC].height = max_h;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001133 ctx->q_data[V4L2_M2M_DST].width = max_w;
1134 ctx->q_data[V4L2_M2M_DST].height = max_h;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001135 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_YUV420) {
1136 ctx->q_data[V4L2_M2M_SRC].bytesperline = max_w;
Philipp Zabel8e428d52015-01-23 13:51:29 -03001137 ctx->q_data[V4L2_M2M_SRC].sizeimage = usize;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001138 ctx->q_data[V4L2_M2M_DST].bytesperline = 0;
Philipp Zabel8e428d52015-01-23 13:51:29 -03001139 ctx->q_data[V4L2_M2M_DST].sizeimage = csize;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001140 } else {
1141 ctx->q_data[V4L2_M2M_SRC].bytesperline = 0;
Philipp Zabel8e428d52015-01-23 13:51:29 -03001142 ctx->q_data[V4L2_M2M_SRC].sizeimage = csize;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001143 ctx->q_data[V4L2_M2M_DST].bytesperline = max_w;
Philipp Zabel8e428d52015-01-23 13:51:29 -03001144 ctx->q_data[V4L2_M2M_DST].sizeimage = usize;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001145 }
Philipp Zabel52c41672014-07-11 06:36:19 -03001146 ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
1147 ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
1148 ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
1149 ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
Philipp Zabel89548442014-07-11 06:36:17 -03001150
Philipp Zabela269e532015-07-16 13:19:38 -03001151 /*
1152 * Since the RBC2AXI logic only supports a single chroma plane,
1153 * macroblock tiling only works for to NV12 pixel format.
1154 */
1155 ctx->tiled_map_type = GDI_LINEAR_FRAME_MAP;
Javier Martin186b2502012-07-26 05:53:35 -03001156}
1157
1158/*
1159 * Queue operations
1160 */
Hans Verkuildf9ecb02015-10-28 00:50:37 -02001161static int coda_queue_setup(struct vb2_queue *vq,
Javier Martin186b2502012-07-26 05:53:35 -03001162 unsigned int *nbuffers, unsigned int *nplanes,
Hans Verkuil36c0f8b2016-04-15 09:15:05 -03001163 unsigned int sizes[], struct device *alloc_devs[])
Javier Martin186b2502012-07-26 05:53:35 -03001164{
1165 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
Philipp Zabele34db062012-08-29 08:22:00 -03001166 struct coda_q_data *q_data;
Javier Martin186b2502012-07-26 05:53:35 -03001167 unsigned int size;
1168
Philipp Zabele34db062012-08-29 08:22:00 -03001169 q_data = get_q_data(ctx, vq->type);
1170 size = q_data->sizeimage;
Javier Martin186b2502012-07-26 05:53:35 -03001171
1172 *nplanes = 1;
1173 sizes[0] = size;
1174
Javier Martin186b2502012-07-26 05:53:35 -03001175 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1176 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
1177
1178 return 0;
1179}
1180
1181static int coda_buf_prepare(struct vb2_buffer *vb)
1182{
1183 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1184 struct coda_q_data *q_data;
1185
1186 q_data = get_q_data(ctx, vb->vb2_queue->type);
1187
1188 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
1189 v4l2_warn(&ctx->dev->v4l2_dev,
1190 "%s data will not fit into plane (%lu < %lu)\n",
1191 __func__, vb2_plane_size(vb, 0),
1192 (long)q_data->sizeimage);
1193 return -EINVAL;
1194 }
1195
Javier Martin186b2502012-07-26 05:53:35 -03001196 return 0;
1197}
1198
1199static void coda_buf_queue(struct vb2_buffer *vb)
1200{
Junghak Sung2d700712015-09-22 10:30:30 -03001201 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
Javier Martin186b2502012-07-26 05:53:35 -03001202 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
Philipp Zabelcb06b702015-01-23 13:51:35 -03001203 struct vb2_queue *vq = vb->vb2_queue;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001204 struct coda_q_data *q_data;
1205
1206 q_data = get_q_data(ctx, vb->vb2_queue->type);
1207
1208 /*
1209 * In the decoder case, immediately try to copy the buffer into the
1210 * bitstream ringbuffer and mark it as ready to be dequeued.
1211 */
Philipp Zabelcb06b702015-01-23 13:51:35 -03001212 if (ctx->bitstream.size && vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001213 /*
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03001214 * For backwards compatibility, queuing an empty buffer marks
Philipp Zabel918c66f2013-06-27 06:59:01 -03001215 * the stream end
1216 */
Philipp Zabel347bb7f2014-07-23 12:28:42 -03001217 if (vb2_get_plane_payload(vb, 0) == 0)
1218 coda_bit_stream_end_flag(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001219 mutex_lock(&ctx->bitstream_mutex);
Junghak Sung2d700712015-09-22 10:30:30 -03001220 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
Michael Olbricheabed932014-07-18 07:22:40 -03001221 if (vb2_is_streaming(vb->vb2_queue))
Philipp Zabel582d88722015-03-24 14:30:57 -03001222 coda_fill_bitstream(ctx, true);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001223 mutex_unlock(&ctx->bitstream_mutex);
1224 } else {
Junghak Sung2d700712015-09-22 10:30:30 -03001225 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001226 }
Javier Martin186b2502012-07-26 05:53:35 -03001227}
1228
Philipp Zabel79924ca2014-07-23 12:28:45 -03001229int coda_alloc_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf,
1230 size_t size, const char *name, struct dentry *parent)
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001231{
1232 buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
1233 GFP_KERNEL);
Philipp Zabel68fc31c2014-08-05 14:00:16 -03001234 if (!buf->vaddr) {
1235 v4l2_err(&dev->v4l2_dev,
1236 "Failed to allocate %s buffer of size %u\n",
1237 name, size);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001238 return -ENOMEM;
Philipp Zabel68fc31c2014-08-05 14:00:16 -03001239 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001240
1241 buf->size = size;
1242
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001243 if (name && parent) {
1244 buf->blob.data = buf->vaddr;
1245 buf->blob.size = size;
Philipp Zabelf23797b2014-08-06 08:02:23 -03001246 buf->dentry = debugfs_create_blob(name, 0644, parent,
1247 &buf->blob);
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001248 if (!buf->dentry)
1249 dev_warn(&dev->plat_dev->dev,
1250 "failed to create debugfs entry %s\n", name);
1251 }
1252
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001253 return 0;
1254}
1255
Philipp Zabel79924ca2014-07-23 12:28:45 -03001256void coda_free_aux_buf(struct coda_dev *dev,
1257 struct coda_aux_buf *buf)
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001258{
1259 if (buf->vaddr) {
1260 dma_free_coherent(&dev->plat_dev->dev, buf->size,
1261 buf->vaddr, buf->paddr);
1262 buf->vaddr = NULL;
1263 buf->size = 0;
Peter Seidererd446ec82015-03-24 14:30:48 -03001264 debugfs_remove(buf->dentry);
1265 buf->dentry = NULL;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001266 }
1267}
1268
Javier Martin186b2502012-07-26 05:53:35 -03001269static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
1270{
1271 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1272 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
Javier Martin186b2502012-07-26 05:53:35 -03001273 struct coda_q_data *q_data_src, *q_data_dst;
Junghak Sung2d700712015-09-22 10:30:30 -03001274 struct vb2_v4l2_buffer *buf;
Philipp Zabeld35167a2013-05-23 10:42:59 -03001275 int ret = 0;
Javier Martin186b2502012-07-26 05:53:35 -03001276
Philipp Zabel49b966f2015-12-02 14:58:53 -02001277 if (count < 1)
1278 return -EINVAL;
1279
Philipp Zabelb96904e2013-05-23 10:42:58 -03001280 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001281 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabel58bc7ed2015-07-09 07:10:14 -03001282 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
Philipp Zabel582d88722015-03-24 14:30:57 -03001283 /* copy the buffers that were queued before streamon */
Michael Olbricheabed932014-07-18 07:22:40 -03001284 mutex_lock(&ctx->bitstream_mutex);
Philipp Zabel582d88722015-03-24 14:30:57 -03001285 coda_fill_bitstream(ctx, false);
Michael Olbricheabed932014-07-18 07:22:40 -03001286 mutex_unlock(&ctx->bitstream_mutex);
1287
Philipp Zabelb9063522014-08-05 14:00:10 -03001288 if (coda_get_bitstream_payload(ctx) < 512) {
1289 ret = -EINVAL;
1290 goto err;
1291 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001292 }
1293
1294 ctx->streamon_out = 1;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001295 } else {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001296 ctx->streamon_cap = 1;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001297 }
Javier Martin186b2502012-07-26 05:53:35 -03001298
Philipp Zabelb96904e2013-05-23 10:42:58 -03001299 /* Don't start the coda unless both queues are on */
1300 if (!(ctx->streamon_out & ctx->streamon_cap))
1301 return 0;
Javier Martin186b2502012-07-26 05:53:35 -03001302
Philipp Zabel23b6ee52015-03-24 14:30:55 -03001303 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1304 if ((q_data_src->width != q_data_dst->width &&
1305 round_up(q_data_src->width, 16) != q_data_dst->width) ||
1306 (q_data_src->height != q_data_dst->height &&
1307 round_up(q_data_src->height, 16) != q_data_dst->height)) {
1308 v4l2_err(v4l2_dev, "can't convert %dx%d to %dx%d\n",
1309 q_data_src->width, q_data_src->height,
1310 q_data_dst->width, q_data_dst->height);
1311 ret = -EINVAL;
1312 goto err;
1313 }
1314
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001315 /* Allow BIT decoder device_run with no new buffers queued */
Philipp Zabela22496c2015-01-23 13:51:33 -03001316 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
Philipp Zabel14604e32014-07-11 06:36:22 -03001317 v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001318
Philipp Zabelb96904e2013-05-23 10:42:58 -03001319 ctx->gopcounter = ctx->params.gop_size - 1;
Javier Martin186b2502012-07-26 05:53:35 -03001320
Philipp Zabelb96904e2013-05-23 10:42:58 -03001321 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
1322 q_data_dst->fourcc);
1323 if (!ctx->codec) {
Javier Martin186b2502012-07-26 05:53:35 -03001324 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
Philipp Zabelb9063522014-08-05 14:00:10 -03001325 ret = -EINVAL;
1326 goto err;
Javier Martin186b2502012-07-26 05:53:35 -03001327 }
1328
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001329 if (q_data_dst->fourcc == V4L2_PIX_FMT_JPEG)
1330 ctx->params.gop_size = 1;
1331 ctx->gopcounter = ctx->params.gop_size - 1;
1332
Philipp Zabela1192a12014-07-23 12:28:40 -03001333 ret = ctx->ops->start_streaming(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001334 if (ctx->inst_type == CODA_INST_DECODER) {
Philipp Zabel89548442014-07-11 06:36:17 -03001335 if (ret == -EAGAIN)
Philipp Zabel918c66f2013-06-27 06:59:01 -03001336 return 0;
Philipp Zabel89548442014-07-11 06:36:17 -03001337 else if (ret < 0)
Philipp Zabelb9063522014-08-05 14:00:10 -03001338 goto err;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001339 }
1340
Philipp Zabel89548442014-07-11 06:36:17 -03001341 return ret;
Philipp Zabelb9063522014-08-05 14:00:10 -03001342
1343err:
1344 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1345 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
Philipp Zabelb8b1b582014-10-21 13:25:52 -03001346 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
Philipp Zabelb9063522014-08-05 14:00:10 -03001347 } else {
1348 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
Philipp Zabelb8b1b582014-10-21 13:25:52 -03001349 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
Philipp Zabelb9063522014-08-05 14:00:10 -03001350 }
1351 return ret;
Philipp Zabel89548442014-07-11 06:36:17 -03001352}
1353
Hans Verkuile37559b2014-04-17 02:47:21 -03001354static void coda_stop_streaming(struct vb2_queue *q)
Javier Martin186b2502012-07-26 05:53:35 -03001355{
1356 struct coda_ctx *ctx = vb2_get_drv_priv(q);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03001357 struct coda_dev *dev = ctx->dev;
Junghak Sung2d700712015-09-22 10:30:30 -03001358 struct vb2_v4l2_buffer *buf;
Philipp Zabel47f3fa62015-07-09 07:10:21 -03001359 unsigned long flags;
Philipp Zabel5c76c2c2015-03-24 14:30:56 -03001360 bool stop;
1361
1362 stop = ctx->streamon_out && ctx->streamon_cap;
Javier Martin186b2502012-07-26 05:53:35 -03001363
1364 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001365 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03001366 "%s: output\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001367 ctx->streamon_out = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001368
Philipp Zabel347bb7f2014-07-23 12:28:42 -03001369 coda_bit_stream_end_flag(ctx);
Philipp Zabel4a31b522014-08-05 14:00:11 -03001370
Philipp Zabel6dd5ef52015-01-23 13:51:26 -03001371 ctx->qsequence = 0;
Philipp Zabel4a31b522014-08-05 14:00:11 -03001372
1373 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1374 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
Javier Martin186b2502012-07-26 05:53:35 -03001375 } else {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001376 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03001377 "%s: capture\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001378 ctx->streamon_cap = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001379
1380 ctx->osequence = 0;
Philipp Zabelcb2c0282014-07-11 06:36:33 -03001381 ctx->sequence_offset = 0;
Philipp Zabel4a31b522014-08-05 14:00:11 -03001382
1383 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
1384 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
Javier Martin186b2502012-07-26 05:53:35 -03001385 }
1386
Philipp Zabel5c76c2c2015-03-24 14:30:56 -03001387 if (stop) {
Philipp Zabel7cbb1052014-10-02 14:08:32 -03001388 struct coda_buffer_meta *meta;
Philipp Zabel846ced92014-07-11 06:36:31 -03001389
Philipp Zabelf4706d62015-01-23 13:51:27 -03001390 if (ctx->ops->seq_end_work) {
1391 queue_work(dev->workqueue, &ctx->seq_end_work);
1392 flush_work(&ctx->seq_end_work);
1393 }
Philipp Zabel47f3fa62015-07-09 07:10:21 -03001394 spin_lock_irqsave(&ctx->buffer_meta_lock, flags);
Philipp Zabel7cbb1052014-10-02 14:08:32 -03001395 while (!list_empty(&ctx->buffer_meta_list)) {
1396 meta = list_first_entry(&ctx->buffer_meta_list,
1397 struct coda_buffer_meta, list);
1398 list_del(&meta->list);
1399 kfree(meta);
Philipp Zabel846ced92014-07-11 06:36:31 -03001400 }
Philipp Zabel47f3fa62015-07-09 07:10:21 -03001401 ctx->num_metas = 0;
1402 spin_unlock_irqrestore(&ctx->buffer_meta_lock, flags);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001403 kfifo_init(&ctx->bitstream_fifo,
1404 ctx->bitstream.vaddr, ctx->bitstream.size);
1405 ctx->runcounter = 0;
Philipp Zabelf157cf42014-09-29 09:53:42 -03001406 ctx->aborting = 0;
Philipp Zabel62bed142012-07-25 10:40:39 -03001407 }
Philipp Zabelc1ae0b22015-07-09 07:10:17 -03001408
1409 if (!ctx->streamon_out && !ctx->streamon_cap)
1410 ctx->bit_stream_param &= ~CODA_BIT_STREAM_END_FLAG;
Javier Martin186b2502012-07-26 05:53:35 -03001411}
1412
Philipp Zabel121cacf2014-07-18 07:22:42 -03001413static const struct vb2_ops coda_qops = {
Javier Martin186b2502012-07-26 05:53:35 -03001414 .queue_setup = coda_queue_setup,
1415 .buf_prepare = coda_buf_prepare,
1416 .buf_queue = coda_buf_queue,
Javier Martin186b2502012-07-26 05:53:35 -03001417 .start_streaming = coda_start_streaming,
1418 .stop_streaming = coda_stop_streaming,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001419 .wait_prepare = vb2_ops_wait_prepare,
1420 .wait_finish = vb2_ops_wait_finish,
Javier Martin186b2502012-07-26 05:53:35 -03001421};
1422
1423static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
1424{
1425 struct coda_ctx *ctx =
1426 container_of(ctrl->handler, struct coda_ctx, ctrls);
1427
1428 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1429 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
1430
1431 switch (ctrl->id) {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001432 case V4L2_CID_HFLIP:
1433 if (ctrl->val)
1434 ctx->params.rot_mode |= CODA_MIR_HOR;
1435 else
1436 ctx->params.rot_mode &= ~CODA_MIR_HOR;
1437 break;
1438 case V4L2_CID_VFLIP:
1439 if (ctrl->val)
1440 ctx->params.rot_mode |= CODA_MIR_VER;
1441 else
1442 ctx->params.rot_mode &= ~CODA_MIR_VER;
1443 break;
Javier Martin186b2502012-07-26 05:53:35 -03001444 case V4L2_CID_MPEG_VIDEO_BITRATE:
1445 ctx->params.bitrate = ctrl->val / 1000;
1446 break;
1447 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
1448 ctx->params.gop_size = ctrl->val;
1449 break;
1450 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
1451 ctx->params.h264_intra_qp = ctrl->val;
1452 break;
1453 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
1454 ctx->params.h264_inter_qp = ctrl->val;
1455 break;
Philipp Zabel1a5567e2014-07-11 06:36:26 -03001456 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
1457 ctx->params.h264_min_qp = ctrl->val;
1458 break;
1459 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
1460 ctx->params.h264_max_qp = ctrl->val;
1461 break;
Philipp Zabelde23b1d2014-07-11 06:36:27 -03001462 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
1463 ctx->params.h264_deblk_alpha = ctrl->val;
1464 break;
1465 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
1466 ctx->params.h264_deblk_beta = ctrl->val;
1467 break;
1468 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
1469 ctx->params.h264_deblk_enabled = (ctrl->val ==
1470 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
1471 break;
Javier Martin186b2502012-07-26 05:53:35 -03001472 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
1473 ctx->params.mpeg4_intra_qp = ctrl->val;
1474 break;
1475 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
1476 ctx->params.mpeg4_inter_qp = ctrl->val;
1477 break;
1478 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
1479 ctx->params.slice_mode = ctrl->val;
1480 break;
1481 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
1482 ctx->params.slice_max_mb = ctrl->val;
1483 break;
Philipp Zabelc566c782012-08-08 11:59:38 -03001484 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
1485 ctx->params.slice_max_bits = ctrl->val * 8;
1486 break;
Javier Martin186b2502012-07-26 05:53:35 -03001487 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
1488 break;
Philipp Zabelf38f79d2014-07-11 06:36:28 -03001489 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
1490 ctx->params.intra_refresh = ctrl->val;
1491 break;
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001492 case V4L2_CID_JPEG_COMPRESSION_QUALITY:
1493 coda_set_jpeg_compression_quality(ctx, ctrl->val);
1494 break;
1495 case V4L2_CID_JPEG_RESTART_INTERVAL:
1496 ctx->params.jpeg_restart_interval = ctrl->val;
1497 break;
Philipp Zabelda2b3b32015-07-10 10:37:52 -03001498 case V4L2_CID_MPEG_VIDEO_VBV_DELAY:
1499 ctx->params.vbv_delay = ctrl->val;
1500 break;
1501 case V4L2_CID_MPEG_VIDEO_VBV_SIZE:
1502 ctx->params.vbv_size = min(ctrl->val * 8192, 0x7fffffff);
1503 break;
Javier Martin186b2502012-07-26 05:53:35 -03001504 default:
1505 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1506 "Invalid control, id=%d, val=%d\n",
1507 ctrl->id, ctrl->val);
1508 return -EINVAL;
1509 }
1510
1511 return 0;
1512}
1513
Philipp Zabel814c3762014-07-18 07:22:45 -03001514static const struct v4l2_ctrl_ops coda_ctrl_ops = {
Javier Martin186b2502012-07-26 05:53:35 -03001515 .s_ctrl = coda_s_ctrl,
1516};
1517
Philipp Zabelbfc732f2014-10-02 14:08:29 -03001518static void coda_encode_ctrls(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03001519{
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001520 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel92b042e2015-03-18 08:15:36 -03001521 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1000, 0);
Javier Martin186b2502012-07-26 05:53:35 -03001522 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1523 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
1524 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03001525 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
Javier Martin186b2502012-07-26 05:53:35 -03001526 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03001527 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
Philipp Zabel1a5567e2014-07-11 06:36:26 -03001528 if (ctx->dev->devtype->product != CODA_960) {
1529 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1530 V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12);
1531 }
1532 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1533 V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51);
Javier Martin186b2502012-07-26 05:53:35 -03001534 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabelde23b1d2014-07-11 06:36:27 -03001535 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, 0, 15, 1, 0);
1536 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1537 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, 0, 15, 1, 0);
1538 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1539 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
1540 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED, 0x0,
1541 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
1542 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03001543 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
1544 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1545 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
1546 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1547 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
Philipp Zabelc566c782012-08-08 11:59:38 -03001548 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
1549 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
Javier Martin186b2502012-07-26 05:53:35 -03001550 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1551 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
Philipp Zabelc566c782012-08-08 11:59:38 -03001552 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabelf23797b2014-08-06 08:02:23 -03001553 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1,
1554 500);
Javier Martin186b2502012-07-26 05:53:35 -03001555 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1556 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
1557 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
1558 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
1559 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
Philipp Zabelf38f79d2014-07-11 06:36:28 -03001560 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabelf23797b2014-08-06 08:02:23 -03001561 V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0,
1562 1920 * 1088 / 256, 1, 0);
Philipp Zabelda2b3b32015-07-10 10:37:52 -03001563 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1564 V4L2_CID_MPEG_VIDEO_VBV_DELAY, 0, 0x7fff, 1, 0);
1565 /*
1566 * The maximum VBV size value is 0x7fffffff bits,
1567 * one bit less than 262144 KiB
1568 */
1569 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1570 V4L2_CID_MPEG_VIDEO_VBV_SIZE, 0, 262144, 1, 0);
Philipp Zabelbfc732f2014-10-02 14:08:29 -03001571}
1572
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001573static void coda_jpeg_encode_ctrls(struct coda_ctx *ctx)
1574{
1575 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1576 V4L2_CID_JPEG_COMPRESSION_QUALITY, 5, 100, 1, 50);
1577 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1578 V4L2_CID_JPEG_RESTART_INTERVAL, 0, 100, 1, 0);
1579}
1580
Philipp Zabelbfc732f2014-10-02 14:08:29 -03001581static int coda_ctrls_setup(struct coda_ctx *ctx)
1582{
1583 v4l2_ctrl_handler_init(&ctx->ctrls, 2);
1584
1585 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1586 V4L2_CID_HFLIP, 0, 1, 1, 0);
1587 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1588 V4L2_CID_VFLIP, 0, 1, 1, 0);
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001589 if (ctx->inst_type == CODA_INST_ENCODER) {
1590 if (ctx->cvd->dst_formats[0] == V4L2_PIX_FMT_JPEG)
1591 coda_jpeg_encode_ctrls(ctx);
1592 else
1593 coda_encode_ctrls(ctx);
1594 }
Javier Martin186b2502012-07-26 05:53:35 -03001595
1596 if (ctx->ctrls.error) {
Philipp Zabelf23797b2014-08-06 08:02:23 -03001597 v4l2_err(&ctx->dev->v4l2_dev,
1598 "control initialization error (%d)",
Javier Martin186b2502012-07-26 05:53:35 -03001599 ctx->ctrls.error);
1600 return -EINVAL;
1601 }
1602
1603 return v4l2_ctrl_handler_setup(&ctx->ctrls);
1604}
1605
Philipp Zabel121cacf2014-07-18 07:22:42 -03001606static int coda_queue_init(struct coda_ctx *ctx, struct vb2_queue *vq)
Javier Martin186b2502012-07-26 05:53:35 -03001607{
Philipp Zabel121cacf2014-07-18 07:22:42 -03001608 vq->drv_priv = ctx;
1609 vq->ops = &coda_qops;
1610 vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1611 vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1612 vq->lock = &ctx->dev->dev_mutex;
Kamil Debskie4af23d2015-02-23 09:26:18 -03001613 /* One way to indicate end-of-stream for coda is to set the
1614 * bytesused == 0. However by default videobuf2 handles bytesused
1615 * equal to 0 as a special case and changes its value to the size
1616 * of the buffer. Set the allow_zero_bytesused flag, so
1617 * that videobuf2 will keep the value of bytesused intact.
1618 */
1619 vq->allow_zero_bytesused = 1;
Hans Verkuil53ddcc62016-02-15 13:09:10 -02001620 vq->dev = &ctx->dev->plat_dev->dev;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001621
1622 return vb2_queue_init(vq);
1623}
1624
Philipp Zabel79924ca2014-07-23 12:28:45 -03001625int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq,
1626 struct vb2_queue *dst_vq)
Philipp Zabel121cacf2014-07-18 07:22:42 -03001627{
Javier Martin186b2502012-07-26 05:53:35 -03001628 int ret;
1629
Javier Martin186b2502012-07-26 05:53:35 -03001630 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabeld29a8cf2014-07-18 07:22:38 -03001631 src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
Javier Martin186b2502012-07-26 05:53:35 -03001632 src_vq->mem_ops = &vb2_dma_contig_memops;
1633
Philipp Zabel121cacf2014-07-18 07:22:42 -03001634 ret = coda_queue_init(priv, src_vq);
Javier Martin186b2502012-07-26 05:53:35 -03001635 if (ret)
1636 return ret;
1637
Javier Martin186b2502012-07-26 05:53:35 -03001638 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Philipp Zabeld29a8cf2014-07-18 07:22:38 -03001639 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
Javier Martin186b2502012-07-26 05:53:35 -03001640 dst_vq->mem_ops = &vb2_dma_contig_memops;
1641
Philipp Zabel121cacf2014-07-18 07:22:42 -03001642 return coda_queue_init(priv, dst_vq);
1643}
1644
Philipp Zabel79924ca2014-07-23 12:28:45 -03001645int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq,
1646 struct vb2_queue *dst_vq)
Philipp Zabel121cacf2014-07-18 07:22:42 -03001647{
1648 int ret;
1649
1650 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabelbb04aa62015-01-23 13:51:30 -03001651 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
1652 src_vq->mem_ops = &vb2_vmalloc_memops;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001653
1654 ret = coda_queue_init(priv, src_vq);
1655 if (ret)
1656 return ret;
1657
1658 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1659 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1660 dst_vq->mem_ops = &vb2_dma_contig_memops;
1661
1662 return coda_queue_init(priv, dst_vq);
Javier Martin186b2502012-07-26 05:53:35 -03001663}
1664
Philipp Zabele11f3e62012-07-25 09:16:58 -03001665static int coda_next_free_instance(struct coda_dev *dev)
1666{
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03001667 int idx = ffz(dev->instance_mask);
1668
1669 if ((idx < 0) ||
1670 (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES))
1671 return -EBUSY;
1672
1673 return idx;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001674}
1675
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001676/*
1677 * File operations
1678 */
1679
1680static int coda_open(struct file *file)
Javier Martin186b2502012-07-26 05:53:35 -03001681{
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001682 struct video_device *vdev = video_devdata(file);
1683 struct coda_dev *dev = video_get_drvdata(vdev);
Javier Martin186b2502012-07-26 05:53:35 -03001684 struct coda_ctx *ctx = NULL;
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001685 char *name;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001686 int ret;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001687 int idx;
Javier Martin186b2502012-07-26 05:53:35 -03001688
Philipp Zabelf23797b2014-08-06 08:02:23 -03001689 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
Javier Martin186b2502012-07-26 05:53:35 -03001690 if (!ctx)
1691 return -ENOMEM;
1692
Fabio Estevamf82bc202013-08-21 11:14:16 -03001693 idx = coda_next_free_instance(dev);
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03001694 if (idx < 0) {
1695 ret = idx;
Fabio Estevamf82bc202013-08-21 11:14:16 -03001696 goto err_coda_max;
1697 }
1698 set_bit(idx, &dev->instance_mask);
1699
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001700 name = kasprintf(GFP_KERNEL, "context%d", idx);
Peter Seiderera7f933a2015-03-24 14:30:47 -03001701 if (!name) {
1702 ret = -ENOMEM;
1703 goto err_coda_name_init;
1704 }
1705
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001706 ctx->debugfs_entry = debugfs_create_dir(name, dev->debugfs_root);
1707 kfree(name);
1708
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001709 ctx->cvd = to_coda_video_device(vdev);
1710 ctx->inst_type = ctx->cvd->type;
1711 ctx->ops = ctx->cvd->ops;
Philipp Zabela22496c2015-01-23 13:51:33 -03001712 ctx->use_bit = !ctx->cvd->direct;
Philipp Zabel32b6f202014-07-11 06:36:20 -03001713 init_completion(&ctx->completion);
1714 INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
Philipp Zabel747d7642015-01-23 13:51:31 -03001715 if (ctx->ops->seq_end_work)
1716 INIT_WORK(&ctx->seq_end_work, ctx->ops->seq_end_work);
Javier Martin186b2502012-07-26 05:53:35 -03001717 v4l2_fh_init(&ctx->fh, video_devdata(file));
1718 file->private_data = &ctx->fh;
1719 v4l2_fh_add(&ctx->fh);
1720 ctx->dev = dev;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001721 ctx->idx = idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001722 switch (dev->devtype->product) {
Philipp Zabel89548442014-07-11 06:36:17 -03001723 case CODA_960:
Philipp Zabel2bf299c2014-09-29 09:53:46 -03001724 ctx->frame_mem_ctrl = 1 << 12;
1725 /* fallthrough */
1726 case CODA_7541:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001727 ctx->reg_idx = 0;
1728 break;
1729 default:
1730 ctx->reg_idx = idx;
1731 }
Fabio Estevamf82bc202013-08-21 11:14:16 -03001732
Philipp Zabel1e172732014-07-11 06:36:23 -03001733 /* Power up and upload firmware if necessary */
1734 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
1735 if (ret < 0) {
1736 v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
1737 goto err_pm_get;
1738 }
1739
Fabio Estevam79830db2013-08-21 11:14:17 -03001740 ret = clk_prepare_enable(dev->clk_per);
1741 if (ret)
1742 goto err_clk_per;
1743
1744 ret = clk_prepare_enable(dev->clk_ahb);
1745 if (ret)
1746 goto err_clk_ahb;
1747
Javier Martin186b2502012-07-26 05:53:35 -03001748 set_default_params(ctx);
Philipp Zabela1192a12014-07-23 12:28:40 -03001749 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
1750 ctx->ops->queue_init);
Philipp Zabel14604e32014-07-11 06:36:22 -03001751 if (IS_ERR(ctx->fh.m2m_ctx)) {
1752 ret = PTR_ERR(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001753
1754 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
1755 __func__, ret);
Fabio Estevamf82bc202013-08-21 11:14:16 -03001756 goto err_ctx_init;
Javier Martin186b2502012-07-26 05:53:35 -03001757 }
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001758
Javier Martin186b2502012-07-26 05:53:35 -03001759 ret = coda_ctrls_setup(ctx);
1760 if (ret) {
1761 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
Fabio Estevamf82bc202013-08-21 11:14:16 -03001762 goto err_ctrls_setup;
Javier Martin186b2502012-07-26 05:53:35 -03001763 }
1764
1765 ctx->fh.ctrl_handler = &ctx->ctrls;
1766
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001767 mutex_init(&ctx->bitstream_mutex);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001768 mutex_init(&ctx->buffer_mutex);
Philipp Zabel7cbb1052014-10-02 14:08:32 -03001769 INIT_LIST_HEAD(&ctx->buffer_meta_list);
Philipp Zabel47f3fa62015-07-09 07:10:21 -03001770 spin_lock_init(&ctx->buffer_meta_lock);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001771
Javier Martin186b2502012-07-26 05:53:35 -03001772 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001773 list_add(&ctx->list, &dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03001774 coda_unlock(ctx);
1775
Javier Martin186b2502012-07-26 05:53:35 -03001776 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
1777 ctx->idx, ctx);
1778
1779 return 0;
1780
Fabio Estevamf82bc202013-08-21 11:14:16 -03001781err_ctrls_setup:
Philipp Zabel14604e32014-07-11 06:36:22 -03001782 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Fabio Estevamf82bc202013-08-21 11:14:16 -03001783err_ctx_init:
1784 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03001785err_clk_ahb:
Fabio Estevamf82bc202013-08-21 11:14:16 -03001786 clk_disable_unprepare(dev->clk_per);
Fabio Estevam79830db2013-08-21 11:14:17 -03001787err_clk_per:
Philipp Zabel1e172732014-07-11 06:36:23 -03001788 pm_runtime_put_sync(&dev->plat_dev->dev);
1789err_pm_get:
Javier Martin186b2502012-07-26 05:53:35 -03001790 v4l2_fh_del(&ctx->fh);
1791 v4l2_fh_exit(&ctx->fh);
Fabio Estevamf82bc202013-08-21 11:14:16 -03001792 clear_bit(ctx->idx, &dev->instance_mask);
Peter Seiderera7f933a2015-03-24 14:30:47 -03001793err_coda_name_init:
Fabio Estevamf82bc202013-08-21 11:14:16 -03001794err_coda_max:
Javier Martin186b2502012-07-26 05:53:35 -03001795 kfree(ctx);
1796 return ret;
1797}
1798
1799static int coda_release(struct file *file)
1800{
1801 struct coda_dev *dev = video_drvdata(file);
1802 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
1803
1804 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
1805 ctx);
1806
Philipp Zabela22496c2015-01-23 13:51:33 -03001807 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
Philipp Zabeld4bb75f2014-10-08 13:09:11 -03001808 coda_bit_stream_end_flag(ctx);
1809
Philipp Zabel918c66f2013-06-27 06:59:01 -03001810 /* If this instance is running, call .job_abort and wait for it to end */
Philipp Zabel14604e32014-07-11 06:36:22 -03001811 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001812
1813 /* In case the instance was not running, we still need to call SEQ_END */
Philipp Zabel5c718bb2015-07-09 07:10:16 -03001814 if (ctx->ops->seq_end_work) {
Philipp Zabel32b6f202014-07-11 06:36:20 -03001815 queue_work(dev->workqueue, &ctx->seq_end_work);
1816 flush_work(&ctx->seq_end_work);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001817 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001818
Javier Martin186b2502012-07-26 05:53:35 -03001819 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001820 list_del(&ctx->list);
Javier Martin186b2502012-07-26 05:53:35 -03001821 coda_unlock(ctx);
1822
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001823 if (ctx->dev->devtype->product == CODA_DX6)
1824 coda_free_aux_buf(dev, &ctx->workbuf);
1825
Javier Martin186b2502012-07-26 05:53:35 -03001826 v4l2_ctrl_handler_free(&ctx->ctrls);
Javier Martin186b2502012-07-26 05:53:35 -03001827 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevamf82bc202013-08-21 11:14:16 -03001828 clk_disable_unprepare(dev->clk_per);
Philipp Zabel1e172732014-07-11 06:36:23 -03001829 pm_runtime_put_sync(&dev->plat_dev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03001830 v4l2_fh_del(&ctx->fh);
1831 v4l2_fh_exit(&ctx->fh);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001832 clear_bit(ctx->idx, &dev->instance_mask);
Philipp Zabel58b76772014-07-23 12:28:43 -03001833 if (ctx->ops->release)
1834 ctx->ops->release(ctx);
Philipp Zabele1519e82015-01-23 13:51:17 -03001835 debugfs_remove_recursive(ctx->debugfs_entry);
Javier Martin186b2502012-07-26 05:53:35 -03001836 kfree(ctx);
1837
1838 return 0;
1839}
1840
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001841static const struct v4l2_file_operations coda_fops = {
Javier Martin186b2502012-07-26 05:53:35 -03001842 .owner = THIS_MODULE,
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001843 .open = coda_open,
Javier Martin186b2502012-07-26 05:53:35 -03001844 .release = coda_release,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001845 .poll = v4l2_m2m_fop_poll,
Javier Martin186b2502012-07-26 05:53:35 -03001846 .unlocked_ioctl = video_ioctl2,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001847 .mmap = v4l2_m2m_fop_mmap,
Javier Martin186b2502012-07-26 05:53:35 -03001848};
1849
Philipp Zabel87048bb2012-07-02 07:03:43 -03001850static int coda_hw_init(struct coda_dev *dev)
Javier Martin186b2502012-07-26 05:53:35 -03001851{
Javier Martin186b2502012-07-26 05:53:35 -03001852 u32 data;
1853 u16 *p;
Fabio Estevam79830db2013-08-21 11:14:17 -03001854 int i, ret;
Javier Martin186b2502012-07-26 05:53:35 -03001855
Fabio Estevam79830db2013-08-21 11:14:17 -03001856 ret = clk_prepare_enable(dev->clk_per);
1857 if (ret)
Philipp Zabel1e172732014-07-11 06:36:23 -03001858 goto err_clk_per;
Fabio Estevam79830db2013-08-21 11:14:17 -03001859
1860 ret = clk_prepare_enable(dev->clk_ahb);
1861 if (ret)
1862 goto err_clk_ahb;
Javier Martin186b2502012-07-26 05:53:35 -03001863
Philipp Zabel8f452842014-07-11 06:36:35 -03001864 if (dev->rstc)
1865 reset_control_reset(dev->rstc);
1866
Javier Martin186b2502012-07-26 05:53:35 -03001867 /*
1868 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
Philipp Zabel87048bb2012-07-02 07:03:43 -03001869 * The 16-bit chars in the code buffer are in memory access
1870 * order, re-sort them to CODA order for register download.
Javier Martin186b2502012-07-26 05:53:35 -03001871 * Data in this SRAM survives a reboot.
1872 */
Philipp Zabel87048bb2012-07-02 07:03:43 -03001873 p = (u16 *)dev->codebuf.vaddr;
1874 if (dev->devtype->product == CODA_DX6) {
1875 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
1876 data = CODA_DOWN_ADDRESS_SET(i) |
1877 CODA_DOWN_DATA_SET(p[i ^ 1]);
1878 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
1879 }
1880 } else {
1881 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
1882 data = CODA_DOWN_ADDRESS_SET(i) |
1883 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
1884 3 - (i % 4)]);
1885 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
1886 }
Javier Martin186b2502012-07-26 05:53:35 -03001887 }
Javier Martin186b2502012-07-26 05:53:35 -03001888
Philipp Zabel9acf7692013-05-23 10:42:56 -03001889 /* Clear registers */
1890 for (i = 0; i < 64; i++)
1891 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
1892
Javier Martin186b2502012-07-26 05:53:35 -03001893 /* Tell the BIT where to find everything it needs */
Philipp Zabel89548442014-07-11 06:36:17 -03001894 if (dev->devtype->product == CODA_960 ||
1895 dev->devtype->product == CODA_7541) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001896 coda_write(dev, dev->tempbuf.paddr,
1897 CODA_REG_BIT_TEMP_BUF_ADDR);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001898 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001899 } else {
1900 coda_write(dev, dev->workbuf.paddr,
1901 CODA_REG_BIT_WORK_BUF_ADDR);
1902 }
Javier Martin186b2502012-07-26 05:53:35 -03001903 coda_write(dev, dev->codebuf.paddr,
1904 CODA_REG_BIT_CODE_BUF_ADDR);
1905 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
1906
1907 /* Set default values */
1908 switch (dev->devtype->product) {
1909 case CODA_DX6:
Philipp Zabelf23797b2014-08-06 08:02:23 -03001910 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH,
1911 CODA_REG_BIT_STREAM_CTRL);
Javier Martin186b2502012-07-26 05:53:35 -03001912 break;
1913 default:
Philipp Zabelf23797b2014-08-06 08:02:23 -03001914 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH,
1915 CODA_REG_BIT_STREAM_CTRL);
Javier Martin186b2502012-07-26 05:53:35 -03001916 }
Philipp Zabel89548442014-07-11 06:36:17 -03001917 if (dev->devtype->product == CODA_960)
1918 coda_write(dev, 1 << 12, CODA_REG_BIT_FRAME_MEM_CTRL);
1919 else
1920 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel10436672012-07-02 09:03:55 -03001921
1922 if (dev->devtype->product != CODA_DX6)
1923 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
1924
Javier Martin186b2502012-07-26 05:53:35 -03001925 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
1926 CODA_REG_BIT_INT_ENABLE);
1927
1928 /* Reset VPU and start processor */
1929 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
1930 data |= CODA_REG_RESET_ENABLE;
1931 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
1932 udelay(10);
1933 data &= ~CODA_REG_RESET_ENABLE;
1934 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
1935 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
1936
Philipp Zabelfe7554e2014-07-11 06:36:24 -03001937 clk_disable_unprepare(dev->clk_ahb);
1938 clk_disable_unprepare(dev->clk_per);
1939
1940 return 0;
1941
1942err_clk_ahb:
1943 clk_disable_unprepare(dev->clk_per);
1944err_clk_per:
1945 return ret;
1946}
1947
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001948static int coda_register_device(struct coda_dev *dev, int i)
Philipp Zabel121cacf2014-07-18 07:22:42 -03001949{
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001950 struct video_device *vfd = &dev->vfd[i];
1951
Dan Carpenter88ca3af2015-01-08 07:07:08 -03001952 if (i >= dev->devtype->num_vdevs)
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001953 return -EINVAL;
1954
Philipp Zabelbf249da2015-03-24 14:30:50 -03001955 strlcpy(vfd->name, dev->devtype->vdevs[i]->name, sizeof(vfd->name));
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001956 vfd->fops = &coda_fops;
1957 vfd->ioctl_ops = &coda_ioctl_ops;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001958 vfd->release = video_device_release_empty,
1959 vfd->lock = &dev->dev_mutex;
1960 vfd->v4l2_dev = &dev->v4l2_dev;
1961 vfd->vfl_dir = VFL_DIR_M2M;
1962 video_set_drvdata(vfd, dev);
1963
Philipp Zabela188a662014-08-05 14:00:20 -03001964 /* Not applicable, use the selection API instead */
1965 v4l2_disable_ioctl(vfd, VIDIOC_CROPCAP);
1966 v4l2_disable_ioctl(vfd, VIDIOC_G_CROP);
1967 v4l2_disable_ioctl(vfd, VIDIOC_S_CROP);
1968
Philipp Zabel121cacf2014-07-18 07:22:42 -03001969 return video_register_device(vfd, VFL_TYPE_GRABBER, 0);
1970}
1971
Philipp Zabela1a87fa2016-02-17 11:21:10 -02001972static void coda_copy_firmware(struct coda_dev *dev, const u8 * const buf,
1973 size_t size)
1974{
1975 u32 *src = (u32 *)buf;
1976
1977 /* Check if the firmware has a 16-byte Freescale header, skip it */
1978 if (buf[0] == 'M' && buf[1] == 'X')
1979 src += 4;
1980 /*
1981 * Check whether the firmware is in native order or pre-reordered for
1982 * memory access. The first instruction opcode always is 0xe40e.
1983 */
1984 if (__le16_to_cpup((__le16 *)src) == 0xe40e) {
1985 u32 *dst = dev->codebuf.vaddr;
1986 int i;
1987
1988 /* Firmware in native order, reorder while copying */
1989 if (dev->devtype->product == CODA_DX6) {
1990 for (i = 0; i < (size - 16) / 4; i++)
1991 dst[i] = (src[i] << 16) | (src[i] >> 16);
1992 } else {
1993 for (i = 0; i < (size - 16) / 4; i += 2) {
1994 dst[i] = (src[i + 1] << 16) | (src[i + 1] >> 16);
1995 dst[i + 1] = (src[i] << 16) | (src[i] >> 16);
1996 }
1997 }
1998 } else {
1999 /* Copy the already reordered firmware image */
2000 memcpy(dev->codebuf.vaddr, src, size);
2001 }
2002}
2003
Philipp Zabel2ac7f082016-02-19 07:18:57 -02002004static void coda_fw_callback(const struct firmware *fw, void *context);
2005
2006static int coda_firmware_request(struct coda_dev *dev)
2007{
2008 char *fw = dev->devtype->firmware[dev->firmware];
2009
2010 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
2011 coda_product_name(dev->devtype->product));
2012
2013 return request_firmware_nowait(THIS_MODULE, true, fw,
2014 &dev->plat_dev->dev, GFP_KERNEL, dev,
2015 coda_fw_callback);
2016}
2017
Javier Martin186b2502012-07-26 05:53:35 -03002018static void coda_fw_callback(const struct firmware *fw, void *context)
2019{
2020 struct coda_dev *dev = context;
2021 struct platform_device *pdev = dev->plat_dev;
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002022 int i, ret;
Javier Martin186b2502012-07-26 05:53:35 -03002023
Philipp Zabel2ac7f082016-02-19 07:18:57 -02002024 if (!fw && dev->firmware == 1) {
Javier Martin186b2502012-07-26 05:53:35 -03002025 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03002026 goto put_pm;
Javier Martin186b2502012-07-26 05:53:35 -03002027 }
Philipp Zabel2ac7f082016-02-19 07:18:57 -02002028 if (!fw) {
2029 dev->firmware = 1;
2030 coda_firmware_request(dev);
2031 return;
2032 }
2033 if (dev->firmware == 1) {
2034 /*
2035 * Since we can't suppress warnings for failed asynchronous
2036 * firmware requests, report that the fallback firmware was
2037 * found.
2038 */
2039 dev_info(&pdev->dev, "Using fallback firmware %s\n",
2040 dev->devtype->firmware[dev->firmware]);
2041 }
Javier Martin186b2502012-07-26 05:53:35 -03002042
2043 /* allocate auxiliary per-device code buffer for the BIT processor */
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002044 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size, "codebuf",
2045 dev->debugfs_root);
Philipp Zabel6ba53b82015-03-24 14:30:54 -03002046 if (ret < 0)
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03002047 goto put_pm;
Javier Martin186b2502012-07-26 05:53:35 -03002048
Philipp Zabela1a87fa2016-02-17 11:21:10 -02002049 coda_copy_firmware(dev, fw->data, fw->size);
Philipp Zabel87048bb2012-07-02 07:03:43 -03002050 release_firmware(fw);
2051
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03002052 ret = coda_hw_init(dev);
2053 if (ret < 0) {
2054 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
2055 goto put_pm;
Javier Martin186b2502012-07-26 05:53:35 -03002056 }
2057
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03002058 ret = coda_check_firmware(dev);
2059 if (ret < 0)
2060 goto put_pm;
2061
Javier Martin186b2502012-07-26 05:53:35 -03002062 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
2063 if (IS_ERR(dev->m2m_dev)) {
2064 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
Hans Verkuil53ddcc62016-02-15 13:09:10 -02002065 goto put_pm;
Javier Martin186b2502012-07-26 05:53:35 -03002066 }
2067
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002068 for (i = 0; i < dev->devtype->num_vdevs; i++) {
2069 ret = coda_register_device(dev, i);
2070 if (ret) {
2071 v4l2_err(&dev->v4l2_dev,
2072 "Failed to register %s video device: %d\n",
2073 dev->devtype->vdevs[i]->name, ret);
2074 goto rel_vfd;
2075 }
Philipp Zabel121cacf2014-07-18 07:22:42 -03002076 }
2077
2078 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video[%d-%d]\n",
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002079 dev->vfd[0].num, dev->vfd[i - 1].num);
Javier Martin186b2502012-07-26 05:53:35 -03002080
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03002081 pm_runtime_put_sync(&pdev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03002082 return;
2083
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002084rel_vfd:
2085 while (--i >= 0)
2086 video_unregister_device(&dev->vfd[i]);
Javier Martin186b2502012-07-26 05:53:35 -03002087 v4l2_m2m_release(dev->m2m_dev);
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03002088put_pm:
2089 pm_runtime_put_sync(&pdev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03002090}
2091
Javier Martin186b2502012-07-26 05:53:35 -03002092enum coda_platform {
2093 CODA_IMX27,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03002094 CODA_IMX53,
Philipp Zabel89548442014-07-11 06:36:17 -03002095 CODA_IMX6Q,
2096 CODA_IMX6DL,
Javier Martin186b2502012-07-26 05:53:35 -03002097};
2098
Emil Goodec06d8752012-08-14 17:44:42 -03002099static const struct coda_devtype coda_devdata[] = {
Javier Martin186b2502012-07-26 05:53:35 -03002100 [CODA_IMX27] = {
Philipp Zabel2ac7f082016-02-19 07:18:57 -02002101 .firmware = {
2102 "vpu_fw_imx27_TO2.bin",
2103 "v4l-codadx6-imx27.bin"
2104 },
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002105 .product = CODA_DX6,
2106 .codecs = codadx6_codecs,
2107 .num_codecs = ARRAY_SIZE(codadx6_codecs),
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002108 .vdevs = codadx6_video_devices,
2109 .num_vdevs = ARRAY_SIZE(codadx6_video_devices),
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002110 .workbuf_size = 288 * 1024 + FMO_SLICE_SAVE_BUF_SIZE * 8 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03002111 .iram_size = 0xb000,
Javier Martin186b2502012-07-26 05:53:35 -03002112 },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03002113 [CODA_IMX53] = {
Philipp Zabel2ac7f082016-02-19 07:18:57 -02002114 .firmware = {
2115 "vpu_fw_imx53.bin",
2116 "v4l-coda7541-imx53.bin"
2117 },
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002118 .product = CODA_7541,
2119 .codecs = coda7_codecs,
2120 .num_codecs = ARRAY_SIZE(coda7_codecs),
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002121 .vdevs = coda7_video_devices,
2122 .num_vdevs = ARRAY_SIZE(coda7_video_devices),
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002123 .workbuf_size = 128 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03002124 .tempbuf_size = 304 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03002125 .iram_size = 0x14000,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03002126 },
Philipp Zabel89548442014-07-11 06:36:17 -03002127 [CODA_IMX6Q] = {
Philipp Zabel2ac7f082016-02-19 07:18:57 -02002128 .firmware = {
2129 "vpu_fw_imx6q.bin",
2130 "v4l-coda960-imx6q.bin"
2131 },
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002132 .product = CODA_960,
2133 .codecs = coda9_codecs,
2134 .num_codecs = ARRAY_SIZE(coda9_codecs),
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002135 .vdevs = coda9_video_devices,
2136 .num_vdevs = ARRAY_SIZE(coda9_video_devices),
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002137 .workbuf_size = 80 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03002138 .tempbuf_size = 204 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03002139 .iram_size = 0x21000,
Philipp Zabel89548442014-07-11 06:36:17 -03002140 },
2141 [CODA_IMX6DL] = {
Philipp Zabel2ac7f082016-02-19 07:18:57 -02002142 .firmware = {
2143 "vpu_fw_imx6d.bin",
2144 "v4l-coda960-imx6dl.bin"
2145 },
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002146 .product = CODA_960,
2147 .codecs = coda9_codecs,
2148 .num_codecs = ARRAY_SIZE(coda9_codecs),
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002149 .vdevs = coda9_video_devices,
2150 .num_vdevs = ARRAY_SIZE(coda9_video_devices),
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002151 .workbuf_size = 80 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03002152 .tempbuf_size = 204 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03002153 .iram_size = 0x20000,
Philipp Zabel89548442014-07-11 06:36:17 -03002154 },
Javier Martin186b2502012-07-26 05:53:35 -03002155};
2156
2157static struct platform_device_id coda_platform_ids[] = {
2158 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
2159 { /* sentinel */ }
2160};
2161MODULE_DEVICE_TABLE(platform, coda_platform_ids);
2162
2163#ifdef CONFIG_OF
2164static const struct of_device_id coda_dt_ids[] = {
Alexander Shiyan7b0dd9e2013-06-15 08:09:57 -03002165 { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03002166 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
Philipp Zabel89548442014-07-11 06:36:17 -03002167 { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
2168 { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
Javier Martin186b2502012-07-26 05:53:35 -03002169 { /* sentinel */ }
2170};
2171MODULE_DEVICE_TABLE(of, coda_dt_ids);
2172#endif
2173
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08002174static int coda_probe(struct platform_device *pdev)
Javier Martin186b2502012-07-26 05:53:35 -03002175{
2176 const struct of_device_id *of_id =
2177 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
2178 const struct platform_device_id *pdev_id;
Philipp Zabel657eee72013-04-29 16:17:14 -07002179 struct coda_platform_data *pdata = pdev->dev.platform_data;
2180 struct device_node *np = pdev->dev.of_node;
2181 struct gen_pool *pool;
Javier Martin186b2502012-07-26 05:53:35 -03002182 struct coda_dev *dev;
2183 struct resource *res;
2184 int ret, irq;
2185
Philipp Zabelf23797b2014-08-06 08:02:23 -03002186 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
Philipp Zabel6da999d2014-09-29 09:53:43 -03002187 if (!dev)
Javier Martin186b2502012-07-26 05:53:35 -03002188 return -ENOMEM;
Javier Martin186b2502012-07-26 05:53:35 -03002189
Philipp Zabelb2f91ae2014-10-02 14:08:27 -03002190 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
2191
Philipp Zabelbc717d52016-02-26 08:21:35 -03002192 if (of_id)
Philipp Zabelb2f91ae2014-10-02 14:08:27 -03002193 dev->devtype = of_id->data;
Philipp Zabelbc717d52016-02-26 08:21:35 -03002194 else if (pdev_id)
Philipp Zabelb2f91ae2014-10-02 14:08:27 -03002195 dev->devtype = &coda_devdata[pdev_id->driver_data];
Philipp Zabelbc717d52016-02-26 08:21:35 -03002196 else
2197 return -EINVAL;
Javier Martin186b2502012-07-26 05:53:35 -03002198
2199 spin_lock_init(&dev->irqlock);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002200 INIT_LIST_HEAD(&dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03002201
2202 dev->plat_dev = pdev;
2203 dev->clk_per = devm_clk_get(&pdev->dev, "per");
2204 if (IS_ERR(dev->clk_per)) {
2205 dev_err(&pdev->dev, "Could not get per clock\n");
2206 return PTR_ERR(dev->clk_per);
2207 }
2208
2209 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
2210 if (IS_ERR(dev->clk_ahb)) {
2211 dev_err(&pdev->dev, "Could not get ahb clock\n");
2212 return PTR_ERR(dev->clk_ahb);
2213 }
2214
2215 /* Get memory for physical registers */
2216 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Philipp Zabel611cbbf2013-05-21 04:19:27 -03002217 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
2218 if (IS_ERR(dev->regs_base))
2219 return PTR_ERR(dev->regs_base);
Javier Martin186b2502012-07-26 05:53:35 -03002220
2221 /* IRQ */
Philipp Zabel540b72e2014-08-05 14:00:09 -03002222 irq = platform_get_irq_byname(pdev, "bit");
2223 if (irq < 0)
2224 irq = platform_get_irq(pdev, 0);
Javier Martin186b2502012-07-26 05:53:35 -03002225 if (irq < 0) {
2226 dev_err(&pdev->dev, "failed to get irq resource\n");
Fabio Estevam7d377432014-06-04 15:46:23 -03002227 return irq;
Javier Martin186b2502012-07-26 05:53:35 -03002228 }
2229
Fabio Estevam08c8d142014-06-04 15:46:24 -03002230 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
2231 IRQF_ONESHOT, dev_name(&pdev->dev), dev);
2232 if (ret < 0) {
2233 dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
2234 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03002235 }
2236
Philipp Zabelee27aa92014-07-24 16:50:03 -03002237 dev->rstc = devm_reset_control_get_optional(&pdev->dev, NULL);
Philipp Zabel8f452842014-07-11 06:36:35 -03002238 if (IS_ERR(dev->rstc)) {
2239 ret = PTR_ERR(dev->rstc);
Alexander Shiyan84345a22016-06-18 11:45:15 -03002240 if (ret == -ENOENT || ret == -ENOTSUPP) {
Philipp Zabel8f452842014-07-11 06:36:35 -03002241 dev->rstc = NULL;
2242 } else {
Philipp Zabelf23797b2014-08-06 08:02:23 -03002243 dev_err(&pdev->dev, "failed get reset control: %d\n",
2244 ret);
Philipp Zabel8f452842014-07-11 06:36:35 -03002245 return ret;
2246 }
2247 }
2248
Philipp Zabel657eee72013-04-29 16:17:14 -07002249 /* Get IRAM pool from device tree or platform data */
Vladimir Zapolskiyabdd4a72015-06-30 15:00:07 -07002250 pool = of_gen_pool_get(np, "iram", 0);
Philipp Zabel657eee72013-04-29 16:17:14 -07002251 if (!pool && pdata)
Vladimir Zapolskiy73858172015-09-04 15:47:43 -07002252 pool = gen_pool_get(pdata->iram_dev, NULL);
Philipp Zabel657eee72013-04-29 16:17:14 -07002253 if (!pool) {
2254 dev_err(&pdev->dev, "iram pool not available\n");
2255 return -ENOMEM;
2256 }
2257 dev->iram_pool = pool;
2258
Javier Martin186b2502012-07-26 05:53:35 -03002259 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
2260 if (ret)
2261 return ret;
2262
2263 mutex_init(&dev->dev_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002264 mutex_init(&dev->coda_mutex);
Javier Martin186b2502012-07-26 05:53:35 -03002265
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002266 dev->debugfs_root = debugfs_create_dir("coda", NULL);
2267 if (!dev->debugfs_root)
2268 dev_warn(&pdev->dev, "failed to create debugfs root\n");
2269
Javier Martin186b2502012-07-26 05:53:35 -03002270 /* allocate auxiliary per-device buffers for the BIT processor */
Philipp Zabel5d73fad2014-07-11 06:36:42 -03002271 if (dev->devtype->product == CODA_DX6) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002272 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002273 dev->devtype->workbuf_size, "workbuf",
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002274 dev->debugfs_root);
Philipp Zabel6ba53b82015-03-24 14:30:54 -03002275 if (ret < 0)
Fabio Estevamb7bd6602014-10-04 16:40:50 -03002276 goto err_v4l2_register;
Javier Martin186b2502012-07-26 05:53:35 -03002277 }
Philipp Zabel5d73fad2014-07-11 06:36:42 -03002278
2279 if (dev->devtype->tempbuf_size) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002280 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03002281 dev->devtype->tempbuf_size, "tempbuf",
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002282 dev->debugfs_root);
Philipp Zabel6ba53b82015-03-24 14:30:54 -03002283 if (ret < 0)
Fabio Estevamb7bd6602014-10-04 16:40:50 -03002284 goto err_v4l2_register;
Javier Martin186b2502012-07-26 05:53:35 -03002285 }
2286
Philipp Zabel401e9722014-07-11 06:36:43 -03002287 dev->iram.size = dev->devtype->iram_size;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03002288 dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
2289 &dev->iram.paddr);
2290 if (!dev->iram.vaddr) {
Philipp Zabel8be31c82014-08-05 14:00:13 -03002291 dev_warn(&pdev->dev, "unable to alloc iram\n");
2292 } else {
Philipp Zabel811a6932015-01-23 13:51:23 -03002293 memset(dev->iram.vaddr, 0, dev->iram.size);
Philipp Zabel8be31c82014-08-05 14:00:13 -03002294 dev->iram.blob.data = dev->iram.vaddr;
2295 dev->iram.blob.size = dev->iram.size;
2296 dev->iram.dentry = debugfs_create_blob("iram", 0644,
2297 dev->debugfs_root,
2298 &dev->iram.blob);
Philipp Zabel10436672012-07-02 09:03:55 -03002299 }
2300
Philipp Zabel32b6f202014-07-11 06:36:20 -03002301 dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
2302 if (!dev->workqueue) {
2303 dev_err(&pdev->dev, "unable to alloc workqueue\n");
Fabio Estevam74d08d52014-10-04 16:40:51 -03002304 ret = -ENOMEM;
2305 goto err_v4l2_register;
Philipp Zabel32b6f202014-07-11 06:36:20 -03002306 }
2307
Javier Martin186b2502012-07-26 05:53:35 -03002308 platform_set_drvdata(pdev, dev);
2309
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03002310 /*
2311 * Start activated so we can directly call coda_hw_init in
Rafael J. Wysockie243c7c2014-12-04 01:10:10 +01002312 * coda_fw_callback regardless of whether CONFIG_PM is
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03002313 * enabled or whether the device is associated with a PM domain.
2314 */
2315 pm_runtime_get_noresume(&pdev->dev);
2316 pm_runtime_set_active(&pdev->dev);
Philipp Zabel1e172732014-07-11 06:36:23 -03002317 pm_runtime_enable(&pdev->dev);
2318
Fabio Estevamc762ff12016-10-04 12:41:37 -03002319 ret = coda_firmware_request(dev);
2320 if (ret)
2321 goto err_alloc_workqueue;
2322 return 0;
Fabio Estevamb7bd6602014-10-04 16:40:50 -03002323
Fabio Estevamc762ff12016-10-04 12:41:37 -03002324err_alloc_workqueue:
2325 destroy_workqueue(dev->workqueue);
Fabio Estevamb7bd6602014-10-04 16:40:50 -03002326err_v4l2_register:
2327 v4l2_device_unregister(&dev->v4l2_dev);
2328 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03002329}
2330
2331static int coda_remove(struct platform_device *pdev)
2332{
2333 struct coda_dev *dev = platform_get_drvdata(pdev);
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002334 int i;
Javier Martin186b2502012-07-26 05:53:35 -03002335
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002336 for (i = 0; i < ARRAY_SIZE(dev->vfd); i++) {
2337 if (video_get_drvdata(&dev->vfd[i]))
2338 video_unregister_device(&dev->vfd[i]);
2339 }
Javier Martin186b2502012-07-26 05:53:35 -03002340 if (dev->m2m_dev)
2341 v4l2_m2m_release(dev->m2m_dev);
Philipp Zabel1e172732014-07-11 06:36:23 -03002342 pm_runtime_disable(&pdev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03002343 v4l2_device_unregister(&dev->v4l2_dev);
Philipp Zabel32b6f202014-07-11 06:36:20 -03002344 destroy_workqueue(dev->workqueue);
Philipp Zabelb313bcc2014-07-11 06:36:16 -03002345 if (dev->iram.vaddr)
2346 gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
2347 dev->iram.size);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002348 coda_free_aux_buf(dev, &dev->codebuf);
2349 coda_free_aux_buf(dev, &dev->tempbuf);
2350 coda_free_aux_buf(dev, &dev->workbuf);
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002351 debugfs_remove_recursive(dev->debugfs_root);
Javier Martin186b2502012-07-26 05:53:35 -03002352 return 0;
2353}
2354
Rafael J. Wysockie243c7c2014-12-04 01:10:10 +01002355#ifdef CONFIG_PM
Philipp Zabel1e172732014-07-11 06:36:23 -03002356static int coda_runtime_resume(struct device *dev)
2357{
2358 struct coda_dev *cdev = dev_get_drvdata(dev);
2359 int ret = 0;
2360
Philipp Zabel65919e62014-07-18 07:22:36 -03002361 if (dev->pm_domain && cdev->codebuf.vaddr) {
Philipp Zabel1e172732014-07-11 06:36:23 -03002362 ret = coda_hw_init(cdev);
2363 if (ret)
2364 v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
2365 }
2366
2367 return ret;
2368}
2369#endif
2370
2371static const struct dev_pm_ops coda_pm_ops = {
2372 SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
2373};
2374
Javier Martin186b2502012-07-26 05:53:35 -03002375static struct platform_driver coda_driver = {
2376 .probe = coda_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08002377 .remove = coda_remove,
Javier Martin186b2502012-07-26 05:53:35 -03002378 .driver = {
2379 .name = CODA_NAME,
Javier Martin186b2502012-07-26 05:53:35 -03002380 .of_match_table = of_match_ptr(coda_dt_ids),
Philipp Zabel1e172732014-07-11 06:36:23 -03002381 .pm = &coda_pm_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002382 },
2383 .id_table = coda_platform_ids,
2384};
2385
2386module_platform_driver(coda_driver);
2387
2388MODULE_LICENSE("GPL");
2389MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
2390MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");