blob: 800d2477f1a015ef3874a3da0105148a57bc64d3 [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"
Michael Trettere7f3c542017-01-20 12:00:24 -020044#include "imx-vdoa.h"
Javier Martin186b2502012-07-26 05:53:35 -030045
46#define CODA_NAME "coda"
47
Philipp Zabel0cddc7e2013-09-30 10:34:44 -030048#define CODADX6_MAX_INSTANCES 4
Philipp Zabel2c11d1b2014-10-02 14:08:28 -030049#define CODA_MAX_FORMATS 4
Javier Martin186b2502012-07-26 05:53:35 -030050
Javier Martin186b2502012-07-26 05:53:35 -030051#define CODA_ISRAM_SIZE (2048 * 2)
52
Javier Martin186b2502012-07-26 05:53:35 -030053#define MIN_W 176
54#define MIN_H 144
Javier Martin186b2502012-07-26 05:53:35 -030055
56#define S_ALIGN 1 /* multiple of 2 */
57#define W_ALIGN 1 /* multiple of 2 */
58#define H_ALIGN 1 /* multiple of 2 */
59
60#define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh)
61
Philipp Zabel79924ca2014-07-23 12:28:45 -030062int coda_debug;
Philipp Zabelc4eb1bfc2013-05-21 04:24:16 -030063module_param(coda_debug, int, 0644);
Philipp Zabeld60b18b2014-08-05 14:00:15 -030064MODULE_PARM_DESC(coda_debug, "Debug level (0-2)");
Javier Martin186b2502012-07-26 05:53:35 -030065
Philipp Zabela269e532015-07-16 13:19:38 -030066static int disable_tiling;
67module_param(disable_tiling, int, 0644);
68MODULE_PARM_DESC(disable_tiling, "Disable tiled frame buffers");
Philipp Zabelb96904e2013-05-23 10:42:58 -030069
Michael Trettere7f3c542017-01-20 12:00:24 -020070static int disable_vdoa;
71module_param(disable_vdoa, int, 0644);
72MODULE_PARM_DESC(disable_vdoa, "Disable Video Data Order Adapter tiled to raster-scan conversion");
73
Philipp Zabel89ed0252017-03-02 07:19:52 -030074static int enable_bwb = 0;
75module_param(enable_bwb, int, 0644);
76MODULE_PARM_DESC(enable_bwb, "Enable BWB unit, may crash on certain streams");
77
Philipp Zabela2b3e462014-07-23 12:28:39 -030078void coda_write(struct coda_dev *dev, u32 data, u32 reg)
Javier Martin186b2502012-07-26 05:53:35 -030079{
Philipp Zabeld60b18b2014-08-05 14:00:15 -030080 v4l2_dbg(2, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -030081 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
82 writel(data, dev->regs_base + reg);
83}
84
Philipp Zabela2b3e462014-07-23 12:28:39 -030085unsigned int coda_read(struct coda_dev *dev, u32 reg)
Javier Martin186b2502012-07-26 05:53:35 -030086{
87 u32 data;
Philipp Zabelf23797b2014-08-06 08:02:23 -030088
Javier Martin186b2502012-07-26 05:53:35 -030089 data = readl(dev->regs_base + reg);
Philipp Zabeld60b18b2014-08-05 14:00:15 -030090 v4l2_dbg(2, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -030091 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
92 return data;
93}
94
Philipp Zabel856d7d92014-09-29 09:53:44 -030095void coda_write_base(struct coda_ctx *ctx, struct coda_q_data *q_data,
Junghak Sung2d700712015-09-22 10:30:30 -030096 struct vb2_v4l2_buffer *buf, unsigned int reg_y)
Philipp Zabel856d7d92014-09-29 09:53:44 -030097{
Junghak Sung2d700712015-09-22 10:30:30 -030098 u32 base_y = vb2_dma_contig_plane_dma_addr(&buf->vb2_buf, 0);
Philipp Zabel856d7d92014-09-29 09:53:44 -030099 u32 base_cb, base_cr;
100
101 switch (q_data->fourcc) {
Michael Tretterd40e98c2017-01-20 12:00:25 -0200102 case V4L2_PIX_FMT_YUYV:
103 /* Fallthrough: IN -H264-> CODA -NV12 MB-> VDOA -YUYV-> OUT */
Philipp Zabel6727d4f2015-07-16 13:19:39 -0300104 case V4L2_PIX_FMT_NV12:
105 case V4L2_PIX_FMT_YUV420:
106 default:
107 base_cb = base_y + q_data->bytesperline * q_data->height;
108 base_cr = base_cb + q_data->bytesperline * q_data->height / 4;
109 break;
Philipp Zabel856d7d92014-09-29 09:53:44 -0300110 case V4L2_PIX_FMT_YVU420:
111 /* Switch Cb and Cr for YVU420 format */
112 base_cr = base_y + q_data->bytesperline * q_data->height;
113 base_cb = base_cr + q_data->bytesperline * q_data->height / 4;
114 break;
Philipp Zabel4de69312014-10-02 14:08:26 -0300115 case V4L2_PIX_FMT_YUV422P:
116 base_cb = base_y + q_data->bytesperline * q_data->height;
117 base_cr = base_cb + q_data->bytesperline * q_data->height / 2;
Philipp Zabel856d7d92014-09-29 09:53:44 -0300118 }
119
120 coda_write(ctx->dev, base_y, reg_y);
121 coda_write(ctx->dev, base_cb, reg_y + 4);
122 coda_write(ctx->dev, base_cr, reg_y + 8);
123}
124
Philipp Zabelb96904e2013-05-23 10:42:58 -0300125#define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
126 { mode, src_fourcc, dst_fourcc, max_w, max_h }
127
128/*
129 * Arrays of codecs supported by each given version of Coda:
130 * i.MX27 -> codadx6
131 * i.MX5x -> coda7
132 * i.MX6 -> coda960
133 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
134 */
Philipp Zabel814c3762014-07-18 07:22:45 -0300135static const struct coda_codec codadx6_codecs[] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300136 CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
137 CODA_CODEC(CODADX6_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -0300138};
139
Philipp Zabel814c3762014-07-18 07:22:45 -0300140static const struct coda_codec coda7_codecs[] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300141 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720),
142 CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720),
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300143 CODA_CODEC(CODA7_MODE_ENCODE_MJPG, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_JPEG, 8192, 8192),
Philipp Zabelb0ed05b2014-08-05 14:00:14 -0300144 CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
Philipp Zabel95847f42015-12-02 14:58:54 -0200145 CODA_CODEC(CODA7_MODE_DECODE_MP2, V4L2_PIX_FMT_MPEG2, V4L2_PIX_FMT_YUV420, 1920, 1088),
Philipp Zabelb0ed05b2014-08-05 14:00:14 -0300146 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088),
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300147 CODA_CODEC(CODA7_MODE_DECODE_MJPG, V4L2_PIX_FMT_JPEG, V4L2_PIX_FMT_YUV420, 8192, 8192),
Philipp Zabelb96904e2013-05-23 10:42:58 -0300148};
149
Philipp Zabel814c3762014-07-18 07:22:45 -0300150static const struct coda_codec coda9_codecs[] = {
Philipp Zabelb0ed05b2014-08-05 14:00:14 -0300151 CODA_CODEC(CODA9_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1920, 1088),
152 CODA_CODEC(CODA9_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1920, 1088),
153 CODA_CODEC(CODA9_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
Philipp Zabel95847f42015-12-02 14:58:54 -0200154 CODA_CODEC(CODA9_MODE_DECODE_MP2, V4L2_PIX_FMT_MPEG2, V4L2_PIX_FMT_YUV420, 1920, 1088),
Philipp Zabelb0ed05b2014-08-05 14:00:14 -0300155 CODA_CODEC(CODA9_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088),
Philipp Zabel89548442014-07-11 06:36:17 -0300156};
157
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300158struct coda_video_device {
159 const char *name;
160 enum coda_inst_type type;
161 const struct coda_context_ops *ops;
Philipp Zabela22496c2015-01-23 13:51:33 -0300162 bool direct;
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300163 u32 src_formats[CODA_MAX_FORMATS];
164 u32 dst_formats[CODA_MAX_FORMATS];
165};
166
167static const struct coda_video_device coda_bit_encoder = {
168 .name = "coda-encoder",
169 .type = CODA_INST_ENCODER,
170 .ops = &coda_bit_encode_ops,
171 .src_formats = {
Philipp Zabel6727d4f2015-07-16 13:19:39 -0300172 V4L2_PIX_FMT_NV12,
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300173 V4L2_PIX_FMT_YUV420,
174 V4L2_PIX_FMT_YVU420,
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300175 },
176 .dst_formats = {
177 V4L2_PIX_FMT_H264,
178 V4L2_PIX_FMT_MPEG4,
179 },
180};
181
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300182static const struct coda_video_device coda_bit_jpeg_encoder = {
183 .name = "coda-jpeg-encoder",
184 .type = CODA_INST_ENCODER,
185 .ops = &coda_bit_encode_ops,
186 .src_formats = {
Philipp Zabel6727d4f2015-07-16 13:19:39 -0300187 V4L2_PIX_FMT_NV12,
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300188 V4L2_PIX_FMT_YUV420,
189 V4L2_PIX_FMT_YVU420,
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300190 V4L2_PIX_FMT_YUV422P,
191 },
192 .dst_formats = {
193 V4L2_PIX_FMT_JPEG,
194 },
195};
196
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300197static const struct coda_video_device coda_bit_decoder = {
198 .name = "coda-decoder",
199 .type = CODA_INST_DECODER,
200 .ops = &coda_bit_decode_ops,
201 .src_formats = {
202 V4L2_PIX_FMT_H264,
Philipp Zabel95847f42015-12-02 14:58:54 -0200203 V4L2_PIX_FMT_MPEG2,
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300204 V4L2_PIX_FMT_MPEG4,
205 },
206 .dst_formats = {
Philipp Zabel6727d4f2015-07-16 13:19:39 -0300207 V4L2_PIX_FMT_NV12,
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300208 V4L2_PIX_FMT_YUV420,
209 V4L2_PIX_FMT_YVU420,
Michael Tretterd40e98c2017-01-20 12:00:25 -0200210 /*
211 * If V4L2_PIX_FMT_YUYV should be default,
212 * set_default_params() must be adjusted.
213 */
214 V4L2_PIX_FMT_YUYV,
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300215 },
216};
217
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300218static const struct coda_video_device coda_bit_jpeg_decoder = {
219 .name = "coda-jpeg-decoder",
220 .type = CODA_INST_DECODER,
221 .ops = &coda_bit_decode_ops,
222 .src_formats = {
223 V4L2_PIX_FMT_JPEG,
224 },
225 .dst_formats = {
Philipp Zabel6727d4f2015-07-16 13:19:39 -0300226 V4L2_PIX_FMT_NV12,
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300227 V4L2_PIX_FMT_YUV420,
228 V4L2_PIX_FMT_YVU420,
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300229 V4L2_PIX_FMT_YUV422P,
230 },
231};
232
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300233static const struct coda_video_device *codadx6_video_devices[] = {
234 &coda_bit_encoder,
235};
236
237static const struct coda_video_device *coda7_video_devices[] = {
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300238 &coda_bit_jpeg_encoder,
239 &coda_bit_jpeg_decoder,
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300240 &coda_bit_encoder,
241 &coda_bit_decoder,
242};
243
244static const struct coda_video_device *coda9_video_devices[] = {
245 &coda_bit_encoder,
246 &coda_bit_decoder,
247};
248
Philipp Zabelb96904e2013-05-23 10:42:58 -0300249/*
250 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
251 * tables.
252 */
253static u32 coda_format_normalize_yuv(u32 fourcc)
254{
Philipp Zabel8076c7e2015-07-09 07:10:18 -0300255 switch (fourcc) {
Philipp Zabel6727d4f2015-07-16 13:19:39 -0300256 case V4L2_PIX_FMT_NV12:
Philipp Zabel8076c7e2015-07-09 07:10:18 -0300257 case V4L2_PIX_FMT_YUV420:
258 case V4L2_PIX_FMT_YVU420:
Philipp Zabel8076c7e2015-07-09 07:10:18 -0300259 case V4L2_PIX_FMT_YUV422P:
Michael Tretterd40e98c2017-01-20 12:00:25 -0200260 case V4L2_PIX_FMT_YUYV:
Philipp Zabel8076c7e2015-07-09 07:10:18 -0300261 return V4L2_PIX_FMT_YUV420;
262 default:
263 return fourcc;
264 }
Philipp Zabelb96904e2013-05-23 10:42:58 -0300265}
266
Philipp Zabel814c3762014-07-18 07:22:45 -0300267static const struct coda_codec *coda_find_codec(struct coda_dev *dev,
268 int src_fourcc, int dst_fourcc)
Philipp Zabelb96904e2013-05-23 10:42:58 -0300269{
Philipp Zabel814c3762014-07-18 07:22:45 -0300270 const struct coda_codec *codecs = dev->devtype->codecs;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300271 int num_codecs = dev->devtype->num_codecs;
272 int k;
273
274 src_fourcc = coda_format_normalize_yuv(src_fourcc);
275 dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
276 if (src_fourcc == dst_fourcc)
277 return NULL;
278
279 for (k = 0; k < num_codecs; k++) {
280 if (codecs[k].src_fourcc == src_fourcc &&
281 codecs[k].dst_fourcc == dst_fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300282 break;
283 }
284
Philipp Zabelb96904e2013-05-23 10:42:58 -0300285 if (k == num_codecs)
Javier Martin186b2502012-07-26 05:53:35 -0300286 return NULL;
287
Philipp Zabelb96904e2013-05-23 10:42:58 -0300288 return &codecs[k];
Javier Martin186b2502012-07-26 05:53:35 -0300289}
290
Philipp Zabel57625592013-09-30 10:34:51 -0300291static void coda_get_max_dimensions(struct coda_dev *dev,
Philipp Zabel814c3762014-07-18 07:22:45 -0300292 const struct coda_codec *codec,
Philipp Zabel57625592013-09-30 10:34:51 -0300293 int *max_w, int *max_h)
294{
Philipp Zabel814c3762014-07-18 07:22:45 -0300295 const struct coda_codec *codecs = dev->devtype->codecs;
Philipp Zabel57625592013-09-30 10:34:51 -0300296 int num_codecs = dev->devtype->num_codecs;
297 unsigned int w, h;
298 int k;
299
300 if (codec) {
301 w = codec->max_w;
302 h = codec->max_h;
303 } else {
304 for (k = 0, w = 0, h = 0; k < num_codecs; k++) {
305 w = max(w, codecs[k].max_w);
306 h = max(h, codecs[k].max_h);
307 }
308 }
309
310 if (max_w)
311 *max_w = w;
312 if (max_h)
313 *max_h = h;
314}
315
Philipp Zabel55425702015-12-02 14:58:50 -0200316static const struct coda_video_device *to_coda_video_device(struct video_device
317 *vdev)
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300318{
319 struct coda_dev *dev = video_get_drvdata(vdev);
320 unsigned int i = vdev - dev->vfd;
321
322 if (i >= dev->devtype->num_vdevs)
323 return NULL;
324
325 return dev->devtype->vdevs[i];
326}
327
Philipp Zabel79924ca2014-07-23 12:28:45 -0300328const char *coda_product_name(int product)
Philipp Zabel927933f2013-09-30 10:34:48 -0300329{
330 static char buf[9];
331
332 switch (product) {
333 case CODA_DX6:
334 return "CodaDx6";
335 case CODA_7541:
336 return "CODA7541";
Philipp Zabel89548442014-07-11 06:36:17 -0300337 case CODA_960:
338 return "CODA960";
Philipp Zabel927933f2013-09-30 10:34:48 -0300339 default:
340 snprintf(buf, sizeof(buf), "(0x%04x)", product);
341 return buf;
342 }
343}
344
Michael Trettere7f3c542017-01-20 12:00:24 -0200345static struct vdoa_data *coda_get_vdoa_data(void)
346{
347 struct device_node *vdoa_node;
348 struct platform_device *vdoa_pdev;
349 struct vdoa_data *vdoa_data = NULL;
350
351 vdoa_node = of_find_compatible_node(NULL, NULL, "fsl,imx6q-vdoa");
352 if (!vdoa_node)
353 return NULL;
354
355 vdoa_pdev = of_find_device_by_node(vdoa_node);
356 if (!vdoa_pdev)
357 goto out;
358
359 vdoa_data = platform_get_drvdata(vdoa_pdev);
360 if (!vdoa_data)
361 vdoa_data = ERR_PTR(-EPROBE_DEFER);
362
363out:
364 if (vdoa_node)
365 of_node_put(vdoa_node);
366
367 return vdoa_data;
368}
369
Javier Martin186b2502012-07-26 05:53:35 -0300370/*
371 * V4L2 ioctl() operations.
372 */
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300373static int coda_querycap(struct file *file, void *priv,
374 struct v4l2_capability *cap)
Javier Martin186b2502012-07-26 05:53:35 -0300375{
Philipp Zabel927933f2013-09-30 10:34:48 -0300376 struct coda_ctx *ctx = fh_to_ctx(priv);
377
Javier Martin186b2502012-07-26 05:53:35 -0300378 strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver));
Philipp Zabel927933f2013-09-30 10:34:48 -0300379 strlcpy(cap->card, coda_product_name(ctx->dev->devtype->product),
380 sizeof(cap->card));
Philipp Zabeldad0e1c2013-05-21 04:18:22 -0300381 strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
Philipp Zabel3898e7a2014-07-18 07:22:37 -0300382 cap->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
Javier Martin186b2502012-07-26 05:53:35 -0300383 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
384
385 return 0;
386}
387
Philipp Zabel22e244b2014-07-18 07:22:43 -0300388static int coda_enum_fmt(struct file *file, void *priv,
389 struct v4l2_fmtdesc *f)
Javier Martin186b2502012-07-26 05:53:35 -0300390{
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300391 struct video_device *vdev = video_devdata(file);
392 const struct coda_video_device *cvd = to_coda_video_device(vdev);
393 const u32 *formats;
Philipp Zabel22e244b2014-07-18 07:22:43 -0300394
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300395 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
396 formats = cvd->src_formats;
397 else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
398 formats = cvd->dst_formats;
Philipp Zabel22e244b2014-07-18 07:22:43 -0300399 else
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300400 return -EINVAL;
Javier Martin186b2502012-07-26 05:53:35 -0300401
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300402 if (f->index >= CODA_MAX_FORMATS || formats[f->index] == 0)
403 return -EINVAL;
Javier Martin186b2502012-07-26 05:53:35 -0300404
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300405 f->pixelformat = formats[f->index];
Javier Martin186b2502012-07-26 05:53:35 -0300406
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300407 return 0;
Javier Martin186b2502012-07-26 05:53:35 -0300408}
409
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300410static int coda_g_fmt(struct file *file, void *priv,
411 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300412{
Javier Martin186b2502012-07-26 05:53:35 -0300413 struct coda_q_data *q_data;
414 struct coda_ctx *ctx = fh_to_ctx(priv);
415
Javier Martin186b2502012-07-26 05:53:35 -0300416 q_data = get_q_data(ctx, f->type);
Philipp Zabelb9736292014-07-11 06:36:18 -0300417 if (!q_data)
418 return -EINVAL;
Javier Martin186b2502012-07-26 05:53:35 -0300419
420 f->fmt.pix.field = V4L2_FIELD_NONE;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300421 f->fmt.pix.pixelformat = q_data->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -0300422 f->fmt.pix.width = q_data->width;
423 f->fmt.pix.height = q_data->height;
Philipp Zabel2fd6a372014-07-11 06:36:36 -0300424 f->fmt.pix.bytesperline = q_data->bytesperline;
Javier Martin186b2502012-07-26 05:53:35 -0300425
426 f->fmt.pix.sizeimage = q_data->sizeimage;
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300427 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_JPEG)
428 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
429 else
430 f->fmt.pix.colorspace = ctx->colorspace;
Javier Martin186b2502012-07-26 05:53:35 -0300431
432 return 0;
433}
434
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300435static int coda_try_pixelformat(struct coda_ctx *ctx, struct v4l2_format *f)
436{
437 struct coda_q_data *q_data;
438 const u32 *formats;
439 int i;
440
441 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
442 formats = ctx->cvd->src_formats;
443 else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
444 formats = ctx->cvd->dst_formats;
445 else
446 return -EINVAL;
447
448 for (i = 0; i < CODA_MAX_FORMATS; i++) {
Michael Tretterd40e98c2017-01-20 12:00:25 -0200449 /* Skip YUYV if the vdoa is not available */
450 if (!ctx->vdoa && f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
451 formats[i] == V4L2_PIX_FMT_YUYV)
452 continue;
453
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300454 if (formats[i] == f->fmt.pix.pixelformat) {
455 f->fmt.pix.pixelformat = formats[i];
456 return 0;
457 }
458 }
459
460 /* Fall back to currently set pixelformat */
461 q_data = get_q_data(ctx, f->type);
462 f->fmt.pix.pixelformat = q_data->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -0300463
464 return 0;
465}
466
Michael Trettere7f3c542017-01-20 12:00:24 -0200467static int coda_try_fmt_vdoa(struct coda_ctx *ctx, struct v4l2_format *f,
468 bool *use_vdoa)
469{
470 int err;
471
472 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
473 return -EINVAL;
474
475 if (!use_vdoa)
476 return -EINVAL;
477
478 if (!ctx->vdoa) {
479 *use_vdoa = false;
480 return 0;
481 }
482
483 err = vdoa_context_configure(NULL, f->fmt.pix.width, f->fmt.pix.height,
484 f->fmt.pix.pixelformat);
485 if (err) {
486 *use_vdoa = false;
487 return 0;
488 }
489
490 *use_vdoa = true;
491 return 0;
492}
493
Philipp Zabel8e428d52015-01-23 13:51:29 -0300494static unsigned int coda_estimate_sizeimage(struct coda_ctx *ctx, u32 sizeimage,
495 u32 width, u32 height)
496{
497 /*
498 * This is a rough estimate for sensible compressed buffer
499 * sizes (between 1 and 16 bits per pixel). This could be
500 * improved by better format specific worst case estimates.
501 */
502 return round_up(clamp(sizeimage, width * height / 8,
503 width * height * 2), PAGE_SIZE);
504}
505
Philipp Zabel814c3762014-07-18 07:22:45 -0300506static int coda_try_fmt(struct coda_ctx *ctx, const struct coda_codec *codec,
Philipp Zabel57625592013-09-30 10:34:51 -0300507 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300508{
Philipp Zabel57625592013-09-30 10:34:51 -0300509 struct coda_dev *dev = ctx->dev;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300510 unsigned int max_w, max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300511 enum v4l2_field field;
512
513 field = f->fmt.pix.field;
514 if (field == V4L2_FIELD_ANY)
515 field = V4L2_FIELD_NONE;
516 else if (V4L2_FIELD_NONE != field)
517 return -EINVAL;
518
519 /* V4L2 specification suggests the driver corrects the format struct
520 * if any of the dimensions is unsupported */
521 f->fmt.pix.field = field;
522
Philipp Zabel57625592013-09-30 10:34:51 -0300523 coda_get_max_dimensions(dev, codec, &max_w, &max_h);
524 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN,
525 &f->fmt.pix.height, MIN_H, max_h, H_ALIGN,
526 S_ALIGN);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300527
Philipp Zabel57625592013-09-30 10:34:51 -0300528 switch (f->fmt.pix.pixelformat) {
Philipp Zabel6727d4f2015-07-16 13:19:39 -0300529 case V4L2_PIX_FMT_NV12:
Philipp Zabel57625592013-09-30 10:34:51 -0300530 case V4L2_PIX_FMT_YUV420:
531 case V4L2_PIX_FMT_YVU420:
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300532 /*
533 * Frame stride must be at least multiple of 8,
534 * but multiple of 16 for h.264 or JPEG 4:2:x
535 */
Philipp Zabel451dfe52014-07-11 06:36:39 -0300536 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
Philipp Zabel47cf0c62013-05-23 10:42:54 -0300537 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
Philipp Zabel451d43a2012-07-26 09:18:27 -0300538 f->fmt.pix.height * 3 / 2;
Philipp Zabel57625592013-09-30 10:34:51 -0300539 break;
Michael Tretterd40e98c2017-01-20 12:00:25 -0200540 case V4L2_PIX_FMT_YUYV:
541 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16) * 2;
542 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
543 f->fmt.pix.height;
544 break;
Philipp Zabel4de69312014-10-02 14:08:26 -0300545 case V4L2_PIX_FMT_YUV422P:
546 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
547 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
548 f->fmt.pix.height * 2;
549 break;
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300550 case V4L2_PIX_FMT_JPEG:
551 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
552 /* fallthrough */
Philipp Zabel57625592013-09-30 10:34:51 -0300553 case V4L2_PIX_FMT_H264:
554 case V4L2_PIX_FMT_MPEG4:
Philipp Zabel95847f42015-12-02 14:58:54 -0200555 case V4L2_PIX_FMT_MPEG2:
Javier Martin186b2502012-07-26 05:53:35 -0300556 f->fmt.pix.bytesperline = 0;
Philipp Zabel8e428d52015-01-23 13:51:29 -0300557 f->fmt.pix.sizeimage = coda_estimate_sizeimage(ctx,
558 f->fmt.pix.sizeimage,
559 f->fmt.pix.width,
560 f->fmt.pix.height);
Philipp Zabel57625592013-09-30 10:34:51 -0300561 break;
562 default:
563 BUG();
Javier Martin186b2502012-07-26 05:53:35 -0300564 }
565
566 return 0;
567}
568
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300569static int coda_try_fmt_vid_cap(struct file *file, void *priv,
570 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300571{
Javier Martin186b2502012-07-26 05:53:35 -0300572 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300573 const struct coda_q_data *q_data_src;
574 const struct coda_codec *codec;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300575 struct vb2_queue *src_vq;
576 int ret;
Michael Trettere7f3c542017-01-20 12:00:24 -0200577 bool use_vdoa;
Javier Martin186b2502012-07-26 05:53:35 -0300578
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300579 ret = coda_try_pixelformat(ctx, f);
580 if (ret < 0)
581 return ret;
582
583 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
584
Philipp Zabel918c66f2013-06-27 06:59:01 -0300585 /*
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300586 * If the source format is already fixed, only allow the same output
587 * resolution
Philipp Zabel918c66f2013-06-27 06:59:01 -0300588 */
Philipp Zabel14604e32014-07-11 06:36:22 -0300589 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300590 if (vb2_is_streaming(src_vq)) {
Philipp Zabel91b5841e2014-07-18 07:22:41 -0300591 f->fmt.pix.width = q_data_src->width;
592 f->fmt.pix.height = q_data_src->height;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300593 }
Javier Martin186b2502012-07-26 05:53:35 -0300594
595 f->fmt.pix.colorspace = ctx->colorspace;
596
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300597 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
598 codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
599 f->fmt.pix.pixelformat);
600 if (!codec)
601 return -EINVAL;
602
Philipp Zabel57625592013-09-30 10:34:51 -0300603 ret = coda_try_fmt(ctx, codec, f);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300604 if (ret < 0)
605 return ret;
606
607 /* The h.264 decoder only returns complete 16x16 macroblocks */
608 if (codec && codec->src_fourcc == V4L2_PIX_FMT_H264) {
Philipp Zabel1b0ed7b2014-07-11 06:36:37 -0300609 f->fmt.pix.width = f->fmt.pix.width;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300610 f->fmt.pix.height = round_up(f->fmt.pix.height, 16);
Philipp Zabel1b0ed7b2014-07-11 06:36:37 -0300611 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300612 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
613 f->fmt.pix.height * 3 / 2;
Michael Trettere7f3c542017-01-20 12:00:24 -0200614
615 ret = coda_try_fmt_vdoa(ctx, f, &use_vdoa);
616 if (ret < 0)
617 return ret;
Michael Tretterd40e98c2017-01-20 12:00:25 -0200618
619 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV) {
620 if (!use_vdoa)
621 return -EINVAL;
622
623 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16) * 2;
624 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
625 f->fmt.pix.height;
626 }
Philipp Zabel918c66f2013-06-27 06:59:01 -0300627 }
628
629 return 0;
Javier Martin186b2502012-07-26 05:53:35 -0300630}
631
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300632static int coda_try_fmt_vid_out(struct file *file, void *priv,
633 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300634{
635 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300636 struct coda_dev *dev = ctx->dev;
637 const struct coda_q_data *q_data_dst;
638 const struct coda_codec *codec;
639 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300640
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300641 ret = coda_try_pixelformat(ctx, f);
642 if (ret < 0)
643 return ret;
Javier Martin186b2502012-07-26 05:53:35 -0300644
Philipp Zabel5f9826e2015-01-23 13:51:21 -0300645 switch (f->fmt.pix.colorspace) {
646 case V4L2_COLORSPACE_REC709:
647 case V4L2_COLORSPACE_JPEG:
648 break;
649 default:
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300650 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_JPEG)
651 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
652 else
653 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
Javier Martin186b2502012-07-26 05:53:35 -0300654 }
655
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300656 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
657 codec = coda_find_codec(dev, f->fmt.pix.pixelformat, q_data_dst->fourcc);
Javier Martin186b2502012-07-26 05:53:35 -0300658
659 return coda_try_fmt(ctx, codec, f);
660}
661
Philipp Zabel4ec319e2017-01-20 12:00:21 -0200662static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f,
663 struct v4l2_rect *r)
Javier Martin186b2502012-07-26 05:53:35 -0300664{
665 struct coda_q_data *q_data;
666 struct vb2_queue *vq;
Javier Martin186b2502012-07-26 05:53:35 -0300667
Philipp Zabel14604e32014-07-11 06:36:22 -0300668 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
Javier Martin186b2502012-07-26 05:53:35 -0300669 if (!vq)
670 return -EINVAL;
671
672 q_data = get_q_data(ctx, f->type);
673 if (!q_data)
674 return -EINVAL;
675
676 if (vb2_is_busy(vq)) {
677 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
678 return -EBUSY;
679 }
680
Philipp Zabelb96904e2013-05-23 10:42:58 -0300681 q_data->fourcc = f->fmt.pix.pixelformat;
Javier Martin186b2502012-07-26 05:53:35 -0300682 q_data->width = f->fmt.pix.width;
683 q_data->height = f->fmt.pix.height;
Philipp Zabel2fd6a372014-07-11 06:36:36 -0300684 q_data->bytesperline = f->fmt.pix.bytesperline;
Philipp Zabel451d43a2012-07-26 09:18:27 -0300685 q_data->sizeimage = f->fmt.pix.sizeimage;
Philipp Zabel4ec319e2017-01-20 12:00:21 -0200686 if (r) {
687 q_data->rect = *r;
688 } else {
689 q_data->rect.left = 0;
690 q_data->rect.top = 0;
691 q_data->rect.width = f->fmt.pix.width;
692 q_data->rect.height = f->fmt.pix.height;
693 }
Javier Martin186b2502012-07-26 05:53:35 -0300694
Philipp Zabela269e532015-07-16 13:19:38 -0300695 switch (f->fmt.pix.pixelformat) {
Michael Tretterd40e98c2017-01-20 12:00:25 -0200696 case V4L2_PIX_FMT_YUYV:
697 ctx->tiled_map_type = GDI_TILED_FRAME_MB_RASTER_MAP;
698 break;
Philipp Zabela269e532015-07-16 13:19:38 -0300699 case V4L2_PIX_FMT_NV12:
Michael Trettere7f3c542017-01-20 12:00:24 -0200700 ctx->tiled_map_type = GDI_TILED_FRAME_MB_RASTER_MAP;
701 if (!disable_tiling)
702 break;
Philipp Zabela269e532015-07-16 13:19:38 -0300703 /* else fall through */
704 case V4L2_PIX_FMT_YUV420:
705 case V4L2_PIX_FMT_YVU420:
706 ctx->tiled_map_type = GDI_LINEAR_FRAME_MAP;
707 break;
708 default:
709 break;
710 }
711
Michael Trettere7f3c542017-01-20 12:00:24 -0200712 if (ctx->tiled_map_type == GDI_TILED_FRAME_MB_RASTER_MAP &&
713 !coda_try_fmt_vdoa(ctx, f, &ctx->use_vdoa) &&
714 ctx->use_vdoa)
715 vdoa_context_configure(ctx->vdoa, f->fmt.pix.width,
716 f->fmt.pix.height,
717 f->fmt.pix.pixelformat);
718 else
719 ctx->use_vdoa = false;
720
Javier Martin186b2502012-07-26 05:53:35 -0300721 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
Philipp Zabeld677b642017-01-20 12:00:22 -0200722 "Setting format for type %d, wxh: %dx%d, fmt: %4.4s %c\n",
723 f->type, q_data->width, q_data->height,
724 (char *)&q_data->fourcc,
725 (ctx->tiled_map_type == GDI_LINEAR_FRAME_MAP) ? 'L' : 'T');
Javier Martin186b2502012-07-26 05:53:35 -0300726
727 return 0;
728}
729
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300730static int coda_s_fmt_vid_cap(struct file *file, void *priv,
731 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300732{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300733 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel4ec319e2017-01-20 12:00:21 -0200734 struct coda_q_data *q_data_src;
735 struct v4l2_rect r;
Javier Martin186b2502012-07-26 05:53:35 -0300736 int ret;
737
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300738 ret = coda_try_fmt_vid_cap(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300739 if (ret)
740 return ret;
741
Philipp Zabel4ec319e2017-01-20 12:00:21 -0200742 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
743 r.left = 0;
744 r.top = 0;
745 r.width = q_data_src->width;
746 r.height = q_data_src->height;
747
748 return coda_s_fmt(ctx, f, &r);
Javier Martin186b2502012-07-26 05:53:35 -0300749}
750
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300751static int coda_s_fmt_vid_out(struct file *file, void *priv,
752 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300753{
754 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel4ec319e2017-01-20 12:00:21 -0200755 struct coda_q_data *q_data_src;
Philipp Zabelf95a6ce2014-08-05 14:00:19 -0300756 struct v4l2_format f_cap;
Philipp Zabel4ec319e2017-01-20 12:00:21 -0200757 struct v4l2_rect r;
Javier Martin186b2502012-07-26 05:53:35 -0300758 int ret;
759
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300760 ret = coda_try_fmt_vid_out(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300761 if (ret)
762 return ret;
763
Philipp Zabel4ec319e2017-01-20 12:00:21 -0200764 ret = coda_s_fmt(ctx, f, NULL);
Javier Martin186b2502012-07-26 05:53:35 -0300765 if (ret)
Philipp Zabel2dc546d2014-08-05 14:00:18 -0300766 return ret;
767
768 ctx->colorspace = f->fmt.pix.colorspace;
Javier Martin186b2502012-07-26 05:53:35 -0300769
Philipp Zabel1c2f6a92015-01-23 13:51:22 -0300770 memset(&f_cap, 0, sizeof(f_cap));
Philipp Zabelf95a6ce2014-08-05 14:00:19 -0300771 f_cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
772 coda_g_fmt(file, priv, &f_cap);
773 f_cap.fmt.pix.width = f->fmt.pix.width;
774 f_cap.fmt.pix.height = f->fmt.pix.height;
775
776 ret = coda_try_fmt_vid_cap(file, priv, &f_cap);
777 if (ret)
778 return ret;
779
Philipp Zabel4ec319e2017-01-20 12:00:21 -0200780 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
781 r.left = 0;
782 r.top = 0;
783 r.width = q_data_src->width;
784 r.height = q_data_src->height;
785
786 return coda_s_fmt(ctx, &f_cap, &r);
Javier Martin186b2502012-07-26 05:53:35 -0300787}
788
Philipp Zabel73751da2015-03-24 14:30:51 -0300789static int coda_reqbufs(struct file *file, void *priv,
790 struct v4l2_requestbuffers *rb)
791{
792 struct coda_ctx *ctx = fh_to_ctx(priv);
793 int ret;
794
795 ret = v4l2_m2m_reqbufs(file, ctx->fh.m2m_ctx, rb);
796 if (ret)
797 return ret;
798
799 /*
800 * Allow to allocate instance specific per-context buffers, such as
801 * bitstream ringbuffer, slice buffer, work buffer, etc. if needed.
802 */
803 if (rb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT && ctx->ops->reqbufs)
804 return ctx->ops->reqbufs(ctx, rb);
805
806 return 0;
807}
808
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300809static int coda_qbuf(struct file *file, void *priv,
810 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300811{
812 struct coda_ctx *ctx = fh_to_ctx(priv);
813
Philipp Zabel14604e32014-07-11 06:36:22 -0300814 return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
Javier Martin186b2502012-07-26 05:53:35 -0300815}
816
Philipp Zabel918c66f2013-06-27 06:59:01 -0300817static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx,
Junghak Sung2d700712015-09-22 10:30:30 -0300818 struct vb2_v4l2_buffer *buf)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300819{
Mauro Carvalho Chehab155ee522017-03-09 17:06:49 -0300820 v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300821
822 return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) &&
Junghak Sung2d700712015-09-22 10:30:30 -0300823 (buf->sequence == (ctx->qsequence - 1)));
Philipp Zabel918c66f2013-06-27 06:59:01 -0300824}
825
Junghak Sung2d700712015-09-22 10:30:30 -0300826void coda_m2m_buf_done(struct coda_ctx *ctx, struct vb2_v4l2_buffer *buf,
Philipp Zabel9f2bfb32015-05-04 07:51:07 -0300827 enum vb2_buffer_state state)
Javier Martin186b2502012-07-26 05:53:35 -0300828{
Philipp Zabel9f2bfb32015-05-04 07:51:07 -0300829 const struct v4l2_event eos_event = {
830 .type = V4L2_EVENT_EOS
831 };
Javier Martin186b2502012-07-26 05:53:35 -0300832
Philipp Zabel9f2bfb32015-05-04 07:51:07 -0300833 if (coda_buf_is_end_of_stream(ctx, buf)) {
Junghak Sung2d700712015-09-22 10:30:30 -0300834 buf->flags |= V4L2_BUF_FLAG_LAST;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300835
836 v4l2_event_queue_fh(&ctx->fh, &eos_event);
837 }
838
Philipp Zabel9f2bfb32015-05-04 07:51:07 -0300839 v4l2_m2m_buf_done(buf, state);
Javier Martin186b2502012-07-26 05:53:35 -0300840}
841
Philipp Zabel52c41672014-07-11 06:36:19 -0300842static int coda_g_selection(struct file *file, void *fh,
843 struct v4l2_selection *s)
844{
845 struct coda_ctx *ctx = fh_to_ctx(fh);
846 struct coda_q_data *q_data;
847 struct v4l2_rect r, *rsel;
848
849 q_data = get_q_data(ctx, s->type);
850 if (!q_data)
851 return -EINVAL;
852
853 r.left = 0;
854 r.top = 0;
855 r.width = q_data->width;
856 r.height = q_data->height;
857 rsel = &q_data->rect;
858
859 switch (s->target) {
860 case V4L2_SEL_TGT_CROP_DEFAULT:
861 case V4L2_SEL_TGT_CROP_BOUNDS:
862 rsel = &r;
863 /* fallthrough */
864 case V4L2_SEL_TGT_CROP:
865 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
866 return -EINVAL;
867 break;
868 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
869 case V4L2_SEL_TGT_COMPOSE_PADDED:
870 rsel = &r;
871 /* fallthrough */
872 case V4L2_SEL_TGT_COMPOSE:
873 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
874 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
875 return -EINVAL;
876 break;
877 default:
878 return -EINVAL;
879 }
880
881 s->r = *rsel;
882
883 return 0;
884}
885
Philipp Zabel59195ce2017-03-02 06:51:44 -0300886static int coda_try_encoder_cmd(struct file *file, void *fh,
887 struct v4l2_encoder_cmd *ec)
888{
889 if (ec->cmd != V4L2_ENC_CMD_STOP)
890 return -EINVAL;
891
892 if (ec->flags & V4L2_ENC_CMD_STOP_AT_GOP_END)
893 return -EINVAL;
894
895 return 0;
896}
897
898static int coda_encoder_cmd(struct file *file, void *fh,
899 struct v4l2_encoder_cmd *ec)
900{
901 struct coda_ctx *ctx = fh_to_ctx(fh);
902 struct vb2_queue *dst_vq;
903 int ret;
904
905 ret = coda_try_encoder_cmd(file, fh, ec);
906 if (ret < 0)
907 return ret;
908
909 /* Ignore encoder stop command silently in decoder context */
910 if (ctx->inst_type != CODA_INST_ENCODER)
911 return 0;
912
913 /* Set the stream-end flag on this context */
914 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
915
916 /* If there is no buffer in flight, wake up */
917 if (ctx->qsequence == ctx->osequence) {
918 dst_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
919 V4L2_BUF_TYPE_VIDEO_CAPTURE);
920 dst_vq->last_buffer_dequeued = true;
921 wake_up(&dst_vq->done_wq);
922 }
923
924 return 0;
925}
926
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300927static int coda_try_decoder_cmd(struct file *file, void *fh,
928 struct v4l2_decoder_cmd *dc)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300929{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300930 if (dc->cmd != V4L2_DEC_CMD_STOP)
931 return -EINVAL;
932
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300933 if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300934 return -EINVAL;
935
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300936 if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0))
Philipp Zabel918c66f2013-06-27 06:59:01 -0300937 return -EINVAL;
938
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300939 return 0;
940}
941
942static int coda_decoder_cmd(struct file *file, void *fh,
943 struct v4l2_decoder_cmd *dc)
944{
945 struct coda_ctx *ctx = fh_to_ctx(fh);
946 int ret;
947
948 ret = coda_try_decoder_cmd(file, fh, dc);
949 if (ret < 0)
950 return ret;
951
952 /* Ignore decoder stop command silently in encoder context */
Philipp Zabel918c66f2013-06-27 06:59:01 -0300953 if (ctx->inst_type != CODA_INST_DECODER)
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300954 return 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300955
Philipp Zabel347bb7f2014-07-23 12:28:42 -0300956 /* Set the stream-end flag on this context */
957 coda_bit_stream_end_flag(ctx);
Philipp Zabel84e23652014-07-11 06:36:34 -0300958 ctx->hold = false;
Michael Olbrichf3497da2014-07-11 06:36:30 -0300959 v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
Philipp Zabel89548442014-07-11 06:36:17 -0300960
Philipp Zabel918c66f2013-06-27 06:59:01 -0300961 return 0;
962}
963
Philipp Zabelcde29ef2015-07-16 13:13:24 -0300964static int coda_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
965{
966 struct coda_ctx *ctx = fh_to_ctx(fh);
967 struct v4l2_fract *tpf;
968
969 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
970 return -EINVAL;
971
972 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
973 tpf = &a->parm.output.timeperframe;
974 tpf->denominator = ctx->params.framerate & CODA_FRATE_RES_MASK;
975 tpf->numerator = 1 + (ctx->params.framerate >>
976 CODA_FRATE_DIV_OFFSET);
977
978 return 0;
979}
980
981/*
982 * Approximate timeperframe v4l2_fract with values that can be written
983 * into the 16-bit CODA_FRATE_DIV and CODA_FRATE_RES fields.
984 */
985static void coda_approximate_timeperframe(struct v4l2_fract *timeperframe)
986{
987 struct v4l2_fract s = *timeperframe;
988 struct v4l2_fract f0;
989 struct v4l2_fract f1 = { 1, 0 };
990 struct v4l2_fract f2 = { 0, 1 };
991 unsigned int i, div, s_denominator;
992
993 /* Lower bound is 1/65535 */
994 if (s.numerator == 0 || s.denominator / s.numerator > 65535) {
995 timeperframe->numerator = 1;
996 timeperframe->denominator = 65535;
997 return;
998 }
999
1000 /* Upper bound is 65536/1, map everything above to infinity */
1001 if (s.denominator == 0 || s.numerator / s.denominator > 65536) {
1002 timeperframe->numerator = 1;
1003 timeperframe->denominator = 0;
1004 return;
1005 }
1006
1007 /* Reduce fraction to lowest terms */
1008 div = gcd(s.numerator, s.denominator);
1009 if (div > 1) {
1010 s.numerator /= div;
1011 s.denominator /= div;
1012 }
1013
1014 if (s.numerator <= 65536 && s.denominator < 65536) {
1015 *timeperframe = s;
1016 return;
1017 }
1018
1019 /* Find successive convergents from continued fraction expansion */
1020 while (f2.numerator <= 65536 && f2.denominator < 65536) {
1021 f0 = f1;
1022 f1 = f2;
1023
1024 /* Stop when f2 exactly equals timeperframe */
1025 if (s.numerator == 0)
1026 break;
1027
1028 i = s.denominator / s.numerator;
1029
1030 f2.numerator = f0.numerator + i * f1.numerator;
1031 f2.denominator = f0.denominator + i * f2.denominator;
1032
1033 s_denominator = s.numerator;
1034 s.numerator = s.denominator % s.numerator;
1035 s.denominator = s_denominator;
1036 }
1037
1038 *timeperframe = f1;
1039}
1040
1041static uint32_t coda_timeperframe_to_frate(struct v4l2_fract *timeperframe)
1042{
1043 return ((timeperframe->numerator - 1) << CODA_FRATE_DIV_OFFSET) |
1044 timeperframe->denominator;
1045}
1046
1047static int coda_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
1048{
1049 struct coda_ctx *ctx = fh_to_ctx(fh);
1050 struct v4l2_fract *tpf;
1051
1052 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1053 return -EINVAL;
1054
1055 tpf = &a->parm.output.timeperframe;
1056 coda_approximate_timeperframe(tpf);
1057 ctx->params.framerate = coda_timeperframe_to_frate(tpf);
1058
1059 return 0;
1060}
1061
Philipp Zabel2e9e4f12013-09-30 10:34:50 -03001062static int coda_subscribe_event(struct v4l2_fh *fh,
1063 const struct v4l2_event_subscription *sub)
Philipp Zabel918c66f2013-06-27 06:59:01 -03001064{
1065 switch (sub->type) {
1066 case V4L2_EVENT_EOS:
1067 return v4l2_event_subscribe(fh, sub, 0, NULL);
1068 default:
1069 return v4l2_ctrl_subscribe_event(fh, sub);
1070 }
Javier Martin186b2502012-07-26 05:53:35 -03001071}
1072
1073static const struct v4l2_ioctl_ops coda_ioctl_ops = {
Philipp Zabel2e9e4f12013-09-30 10:34:50 -03001074 .vidioc_querycap = coda_querycap,
Javier Martin186b2502012-07-26 05:53:35 -03001075
Philipp Zabel22e244b2014-07-18 07:22:43 -03001076 .vidioc_enum_fmt_vid_cap = coda_enum_fmt,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -03001077 .vidioc_g_fmt_vid_cap = coda_g_fmt,
1078 .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
1079 .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap,
Javier Martin186b2502012-07-26 05:53:35 -03001080
Philipp Zabel22e244b2014-07-18 07:22:43 -03001081 .vidioc_enum_fmt_vid_out = coda_enum_fmt,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -03001082 .vidioc_g_fmt_vid_out = coda_g_fmt,
1083 .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
1084 .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out,
Javier Martin186b2502012-07-26 05:53:35 -03001085
Philipp Zabel73751da2015-03-24 14:30:51 -03001086 .vidioc_reqbufs = coda_reqbufs,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001087 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
Javier Martin186b2502012-07-26 05:53:35 -03001088
Philipp Zabel2e9e4f12013-09-30 10:34:50 -03001089 .vidioc_qbuf = coda_qbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001090 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
Philipp Zabel9f2bfb32015-05-04 07:51:07 -03001091 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001092 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
Philipp Zabelbb757d72015-12-02 14:58:52 -02001093 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
Javier Martin186b2502012-07-26 05:53:35 -03001094
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001095 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
1096 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
Philipp Zabel918c66f2013-06-27 06:59:01 -03001097
Philipp Zabel52c41672014-07-11 06:36:19 -03001098 .vidioc_g_selection = coda_g_selection,
1099
Philipp Zabel59195ce2017-03-02 06:51:44 -03001100 .vidioc_try_encoder_cmd = coda_try_encoder_cmd,
1101 .vidioc_encoder_cmd = coda_encoder_cmd,
Philipp Zabel64ba40a2013-09-30 10:34:52 -03001102 .vidioc_try_decoder_cmd = coda_try_decoder_cmd,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -03001103 .vidioc_decoder_cmd = coda_decoder_cmd,
Philipp Zabel918c66f2013-06-27 06:59:01 -03001104
Philipp Zabelcde29ef2015-07-16 13:13:24 -03001105 .vidioc_g_parm = coda_g_parm,
1106 .vidioc_s_parm = coda_s_parm,
1107
Philipp Zabel2e9e4f12013-09-30 10:34:50 -03001108 .vidioc_subscribe_event = coda_subscribe_event,
Philipp Zabel918c66f2013-06-27 06:59:01 -03001109 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Javier Martin186b2502012-07-26 05:53:35 -03001110};
1111
1112/*
1113 * Mem-to-mem operations.
1114 */
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001115
1116static void coda_device_run(void *m2m_priv)
1117{
1118 struct coda_ctx *ctx = m2m_priv;
1119 struct coda_dev *dev = ctx->dev;
Philipp Zabel32b6f202014-07-11 06:36:20 -03001120
1121 queue_work(dev->workqueue, &ctx->pic_run_work);
1122}
1123
Philipp Zabel32b6f202014-07-11 06:36:20 -03001124static void coda_pic_run_work(struct work_struct *work)
1125{
1126 struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
1127 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001128 int ret;
1129
1130 mutex_lock(&ctx->buffer_mutex);
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001131 mutex_lock(&dev->coda_mutex);
1132
Philipp Zabela1192a12014-07-23 12:28:40 -03001133 ret = ctx->ops->prepare_run(ctx);
1134 if (ret < 0 && ctx->inst_type == CODA_INST_DECODER) {
1135 mutex_unlock(&dev->coda_mutex);
1136 mutex_unlock(&ctx->buffer_mutex);
1137 /* job_finish scheduled by prepare_decode */
1138 return;
Philipp Zabel10436672012-07-02 09:03:55 -03001139 }
1140
Philipp Zabelf23797b2014-08-06 08:02:23 -03001141 if (!wait_for_completion_timeout(&ctx->completion,
1142 msecs_to_jiffies(1000))) {
Philipp Zabel32b6f202014-07-11 06:36:20 -03001143 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout\n");
Philipp Zabel84e23652014-07-11 06:36:34 -03001144
1145 ctx->hold = true;
Philipp Zabel8f452842014-07-11 06:36:35 -03001146
1147 coda_hw_reset(ctx);
Philipp Zabel32b6f202014-07-11 06:36:20 -03001148 } else if (!ctx->aborting) {
Philipp Zabela1192a12014-07-23 12:28:40 -03001149 ctx->ops->finish_run(ctx);
Philipp Zabel32b6f202014-07-11 06:36:20 -03001150 }
1151
Philipp Zabel747d7642015-01-23 13:51:31 -03001152 if ((ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out)) &&
1153 ctx->ops->seq_end_work)
Philipp Zabel32b6f202014-07-11 06:36:20 -03001154 queue_work(dev->workqueue, &ctx->seq_end_work);
1155
1156 mutex_unlock(&dev->coda_mutex);
1157 mutex_unlock(&ctx->buffer_mutex);
1158
Philipp Zabel14604e32014-07-11 06:36:22 -03001159 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001160}
1161
1162static int coda_job_ready(void *m2m_priv)
1163{
1164 struct coda_ctx *ctx = m2m_priv;
Philipp Zabel2cf251c2015-07-09 07:10:20 -03001165 int src_bufs = v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001166
1167 /*
1168 * For both 'P' and 'key' frame cases 1 picture
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001169 * and 1 frame are needed. In the decoder case,
1170 * the compressed frame can be in the bitstream.
Javier Martin186b2502012-07-26 05:53:35 -03001171 */
Philipp Zabel2cf251c2015-07-09 07:10:20 -03001172 if (!src_bufs && ctx->inst_type != CODA_INST_DECODER) {
Javier Martin186b2502012-07-26 05:53:35 -03001173 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1174 "not ready: not enough video buffers.\n");
1175 return 0;
1176 }
1177
Philipp Zabel14604e32014-07-11 06:36:22 -03001178 if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001179 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1180 "not ready: not enough video capture buffers.\n");
1181 return 0;
1182 }
1183
Philipp Zabela22496c2015-01-23 13:51:33 -03001184 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
Philipp Zabel47f3fa62015-07-09 07:10:21 -03001185 bool stream_end = ctx->bit_stream_param &
1186 CODA_BIT_STREAM_END_FLAG;
1187 int num_metas = ctx->num_metas;
Michael Trettere7f3c542017-01-20 12:00:24 -02001188 unsigned int count;
1189
1190 count = hweight32(ctx->frm_dis_flg);
1191 if (ctx->use_vdoa && count >= (ctx->num_internal_frames - 1)) {
1192 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1193 "%d: not ready: all internal buffers in use: %d/%d (0x%x)",
1194 ctx->idx, count, ctx->num_internal_frames,
1195 ctx->frm_dis_flg);
1196 return 0;
1197 }
Philipp Zabelf77fd8a2015-01-23 13:51:20 -03001198
Philipp Zabel2cf251c2015-07-09 07:10:20 -03001199 if (ctx->hold && !src_bufs) {
Philipp Zabelf77fd8a2015-01-23 13:51:20 -03001200 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1201 "%d: not ready: on hold for more buffers.\n",
1202 ctx->idx);
1203 return 0;
1204 }
1205
Philipp Zabelf77fd8a2015-01-23 13:51:20 -03001206 if (!stream_end && (num_metas + src_bufs) < 2) {
1207 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1208 "%d: not ready: need 2 buffers available (%d, %d)\n",
1209 ctx->idx, num_metas, src_bufs);
1210 return 0;
1211 }
1212
1213
Philipp Zabel2cf251c2015-07-09 07:10:20 -03001214 if (!src_bufs && !stream_end &&
1215 (coda_get_bitstream_payload(ctx) < 512)) {
Philipp Zabelf77fd8a2015-01-23 13:51:20 -03001216 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1217 "%d: not ready: not enough bitstream data (%d).\n",
1218 ctx->idx, coda_get_bitstream_payload(ctx));
1219 return 0;
1220 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001221 }
1222
Philipp Zabel3e748262013-05-23 10:43:01 -03001223 if (ctx->aborting) {
1224 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1225 "not ready: aborting\n");
1226 return 0;
1227 }
1228
Javier Martin186b2502012-07-26 05:53:35 -03001229 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1230 "job ready\n");
Philipp Zabel47f3fa62015-07-09 07:10:21 -03001231
Javier Martin186b2502012-07-26 05:53:35 -03001232 return 1;
1233}
1234
1235static void coda_job_abort(void *priv)
1236{
1237 struct coda_ctx *ctx = priv;
Javier Martin186b2502012-07-26 05:53:35 -03001238
1239 ctx->aborting = 1;
1240
1241 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1242 "Aborting task\n");
Javier Martin186b2502012-07-26 05:53:35 -03001243}
1244
1245static void coda_lock(void *m2m_priv)
1246{
1247 struct coda_ctx *ctx = m2m_priv;
1248 struct coda_dev *pcdev = ctx->dev;
Philipp Zabelf23797b2014-08-06 08:02:23 -03001249
Javier Martin186b2502012-07-26 05:53:35 -03001250 mutex_lock(&pcdev->dev_mutex);
1251}
1252
1253static void coda_unlock(void *m2m_priv)
1254{
1255 struct coda_ctx *ctx = m2m_priv;
1256 struct coda_dev *pcdev = ctx->dev;
Philipp Zabelf23797b2014-08-06 08:02:23 -03001257
Javier Martin186b2502012-07-26 05:53:35 -03001258 mutex_unlock(&pcdev->dev_mutex);
1259}
1260
Philipp Zabel814c3762014-07-18 07:22:45 -03001261static const struct v4l2_m2m_ops coda_m2m_ops = {
Javier Martin186b2502012-07-26 05:53:35 -03001262 .device_run = coda_device_run,
1263 .job_ready = coda_job_ready,
1264 .job_abort = coda_job_abort,
1265 .lock = coda_lock,
1266 .unlock = coda_unlock,
1267};
1268
1269static void set_default_params(struct coda_ctx *ctx)
1270{
Philipp Zabel8e428d52015-01-23 13:51:29 -03001271 unsigned int max_w, max_h, usize, csize;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001272
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001273 ctx->codec = coda_find_codec(ctx->dev, ctx->cvd->src_formats[0],
1274 ctx->cvd->dst_formats[0]);
Philipp Zabeld4c6a412014-10-02 14:08:35 -03001275 max_w = min(ctx->codec->max_w, 1920U);
1276 max_h = min(ctx->codec->max_h, 1088U);
Philipp Zabel8e428d52015-01-23 13:51:29 -03001277 usize = max_w * max_h * 3 / 2;
1278 csize = coda_estimate_sizeimage(ctx, usize, max_w, max_h);
Javier Martin186b2502012-07-26 05:53:35 -03001279
Philipp Zabel121cacf2014-07-18 07:22:42 -03001280 ctx->params.codec_mode = ctx->codec->mode;
Javier Martin186b2502012-07-26 05:53:35 -03001281 ctx->colorspace = V4L2_COLORSPACE_REC709;
1282 ctx->params.framerate = 30;
Javier Martin186b2502012-07-26 05:53:35 -03001283
1284 /* Default formats for output and input queues */
Philipp Zabel6727d4f2015-07-16 13:19:39 -03001285 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->cvd->src_formats[0];
1286 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->cvd->dst_formats[0];
Philipp Zabelb96904e2013-05-23 10:42:58 -03001287 ctx->q_data[V4L2_M2M_SRC].width = max_w;
1288 ctx->q_data[V4L2_M2M_SRC].height = max_h;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001289 ctx->q_data[V4L2_M2M_DST].width = max_w;
1290 ctx->q_data[V4L2_M2M_DST].height = max_h;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001291 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_YUV420) {
1292 ctx->q_data[V4L2_M2M_SRC].bytesperline = max_w;
Philipp Zabel8e428d52015-01-23 13:51:29 -03001293 ctx->q_data[V4L2_M2M_SRC].sizeimage = usize;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001294 ctx->q_data[V4L2_M2M_DST].bytesperline = 0;
Philipp Zabel8e428d52015-01-23 13:51:29 -03001295 ctx->q_data[V4L2_M2M_DST].sizeimage = csize;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001296 } else {
1297 ctx->q_data[V4L2_M2M_SRC].bytesperline = 0;
Philipp Zabel8e428d52015-01-23 13:51:29 -03001298 ctx->q_data[V4L2_M2M_SRC].sizeimage = csize;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001299 ctx->q_data[V4L2_M2M_DST].bytesperline = max_w;
Philipp Zabel8e428d52015-01-23 13:51:29 -03001300 ctx->q_data[V4L2_M2M_DST].sizeimage = usize;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001301 }
Philipp Zabel52c41672014-07-11 06:36:19 -03001302 ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
1303 ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
1304 ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
1305 ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
Philipp Zabel89548442014-07-11 06:36:17 -03001306
Philipp Zabela269e532015-07-16 13:19:38 -03001307 /*
1308 * Since the RBC2AXI logic only supports a single chroma plane,
1309 * macroblock tiling only works for to NV12 pixel format.
1310 */
1311 ctx->tiled_map_type = GDI_LINEAR_FRAME_MAP;
Javier Martin186b2502012-07-26 05:53:35 -03001312}
1313
1314/*
1315 * Queue operations
1316 */
Hans Verkuildf9ecb02015-10-28 00:50:37 -02001317static int coda_queue_setup(struct vb2_queue *vq,
Javier Martin186b2502012-07-26 05:53:35 -03001318 unsigned int *nbuffers, unsigned int *nplanes,
Hans Verkuil36c0f8b2016-04-15 09:15:05 -03001319 unsigned int sizes[], struct device *alloc_devs[])
Javier Martin186b2502012-07-26 05:53:35 -03001320{
1321 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
Philipp Zabele34db062012-08-29 08:22:00 -03001322 struct coda_q_data *q_data;
Javier Martin186b2502012-07-26 05:53:35 -03001323 unsigned int size;
1324
Philipp Zabele34db062012-08-29 08:22:00 -03001325 q_data = get_q_data(ctx, vq->type);
1326 size = q_data->sizeimage;
Javier Martin186b2502012-07-26 05:53:35 -03001327
1328 *nplanes = 1;
1329 sizes[0] = size;
1330
Javier Martin186b2502012-07-26 05:53:35 -03001331 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1332 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
1333
1334 return 0;
1335}
1336
1337static int coda_buf_prepare(struct vb2_buffer *vb)
1338{
1339 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1340 struct coda_q_data *q_data;
1341
1342 q_data = get_q_data(ctx, vb->vb2_queue->type);
1343
1344 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
1345 v4l2_warn(&ctx->dev->v4l2_dev,
1346 "%s data will not fit into plane (%lu < %lu)\n",
1347 __func__, vb2_plane_size(vb, 0),
1348 (long)q_data->sizeimage);
1349 return -EINVAL;
1350 }
1351
Javier Martin186b2502012-07-26 05:53:35 -03001352 return 0;
1353}
1354
1355static void coda_buf_queue(struct vb2_buffer *vb)
1356{
Junghak Sung2d700712015-09-22 10:30:30 -03001357 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
Javier Martin186b2502012-07-26 05:53:35 -03001358 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
Philipp Zabelcb06b702015-01-23 13:51:35 -03001359 struct vb2_queue *vq = vb->vb2_queue;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001360 struct coda_q_data *q_data;
1361
1362 q_data = get_q_data(ctx, vb->vb2_queue->type);
1363
1364 /*
1365 * In the decoder case, immediately try to copy the buffer into the
1366 * bitstream ringbuffer and mark it as ready to be dequeued.
1367 */
Philipp Zabelcb06b702015-01-23 13:51:35 -03001368 if (ctx->bitstream.size && vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001369 /*
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03001370 * For backwards compatibility, queuing an empty buffer marks
Philipp Zabel918c66f2013-06-27 06:59:01 -03001371 * the stream end
1372 */
Philipp Zabel347bb7f2014-07-23 12:28:42 -03001373 if (vb2_get_plane_payload(vb, 0) == 0)
1374 coda_bit_stream_end_flag(ctx);
Philipp Zabel69122272017-03-03 09:12:50 -03001375
1376 if (q_data->fourcc == V4L2_PIX_FMT_H264) {
1377 /*
1378 * Unless already done, try to obtain profile_idc and
1379 * level_idc from the SPS header. This allows to decide
1380 * whether to enable reordering during sequence
1381 * initialization.
1382 */
1383 if (!ctx->params.h264_profile_idc)
1384 coda_sps_parse_profile(ctx, vb);
1385 }
1386
Philipp Zabel918c66f2013-06-27 06:59:01 -03001387 mutex_lock(&ctx->bitstream_mutex);
Junghak Sung2d700712015-09-22 10:30:30 -03001388 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
Michael Olbricheabed932014-07-18 07:22:40 -03001389 if (vb2_is_streaming(vb->vb2_queue))
Philipp Zabel59195ce2017-03-02 06:51:44 -03001390 /* This set buf->sequence = ctx->qsequence++ */
Philipp Zabel331e7862017-03-03 09:12:48 -03001391 coda_fill_bitstream(ctx, NULL);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001392 mutex_unlock(&ctx->bitstream_mutex);
1393 } else {
Philipp Zabel59195ce2017-03-02 06:51:44 -03001394 if (ctx->inst_type == CODA_INST_ENCODER &&
1395 vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1396 vbuf->sequence = ctx->qsequence++;
Junghak Sung2d700712015-09-22 10:30:30 -03001397 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001398 }
Javier Martin186b2502012-07-26 05:53:35 -03001399}
1400
Philipp Zabel79924ca2014-07-23 12:28:45 -03001401int coda_alloc_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf,
1402 size_t size, const char *name, struct dentry *parent)
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001403{
1404 buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
1405 GFP_KERNEL);
Philipp Zabel68fc31c2014-08-05 14:00:16 -03001406 if (!buf->vaddr) {
1407 v4l2_err(&dev->v4l2_dev,
Mauro Carvalho Chehabd5798ee2017-03-09 17:16:41 -03001408 "Failed to allocate %s buffer of size %zu\n",
Philipp Zabel68fc31c2014-08-05 14:00:16 -03001409 name, size);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001410 return -ENOMEM;
Philipp Zabel68fc31c2014-08-05 14:00:16 -03001411 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001412
1413 buf->size = size;
1414
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001415 if (name && parent) {
1416 buf->blob.data = buf->vaddr;
1417 buf->blob.size = size;
Philipp Zabelf23797b2014-08-06 08:02:23 -03001418 buf->dentry = debugfs_create_blob(name, 0644, parent,
1419 &buf->blob);
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001420 if (!buf->dentry)
1421 dev_warn(&dev->plat_dev->dev,
1422 "failed to create debugfs entry %s\n", name);
1423 }
1424
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001425 return 0;
1426}
1427
Philipp Zabel79924ca2014-07-23 12:28:45 -03001428void coda_free_aux_buf(struct coda_dev *dev,
1429 struct coda_aux_buf *buf)
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001430{
1431 if (buf->vaddr) {
1432 dma_free_coherent(&dev->plat_dev->dev, buf->size,
1433 buf->vaddr, buf->paddr);
1434 buf->vaddr = NULL;
1435 buf->size = 0;
Peter Seidererd446ec82015-03-24 14:30:48 -03001436 debugfs_remove(buf->dentry);
1437 buf->dentry = NULL;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001438 }
1439}
1440
Javier Martin186b2502012-07-26 05:53:35 -03001441static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
1442{
1443 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1444 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
Javier Martin186b2502012-07-26 05:53:35 -03001445 struct coda_q_data *q_data_src, *q_data_dst;
Philipp Zabel331e7862017-03-03 09:12:48 -03001446 struct v4l2_m2m_buffer *m2m_buf, *tmp;
Junghak Sung2d700712015-09-22 10:30:30 -03001447 struct vb2_v4l2_buffer *buf;
Philipp Zabel331e7862017-03-03 09:12:48 -03001448 struct list_head list;
Philipp Zabeld35167a2013-05-23 10:42:59 -03001449 int ret = 0;
Javier Martin186b2502012-07-26 05:53:35 -03001450
Philipp Zabel49b966f2015-12-02 14:58:53 -02001451 if (count < 1)
1452 return -EINVAL;
1453
Philipp Zabel331e7862017-03-03 09:12:48 -03001454 INIT_LIST_HEAD(&list);
1455
Philipp Zabelb96904e2013-05-23 10:42:58 -03001456 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001457 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabel58bc7ed2015-07-09 07:10:14 -03001458 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
Philipp Zabel582d88722015-03-24 14:30:57 -03001459 /* copy the buffers that were queued before streamon */
Michael Olbricheabed932014-07-18 07:22:40 -03001460 mutex_lock(&ctx->bitstream_mutex);
Philipp Zabel331e7862017-03-03 09:12:48 -03001461 coda_fill_bitstream(ctx, &list);
Michael Olbricheabed932014-07-18 07:22:40 -03001462 mutex_unlock(&ctx->bitstream_mutex);
1463
Philipp Zabelb9063522014-08-05 14:00:10 -03001464 if (coda_get_bitstream_payload(ctx) < 512) {
1465 ret = -EINVAL;
1466 goto err;
1467 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001468 }
1469
1470 ctx->streamon_out = 1;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001471 } else {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001472 ctx->streamon_cap = 1;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001473 }
Javier Martin186b2502012-07-26 05:53:35 -03001474
Philipp Zabelb96904e2013-05-23 10:42:58 -03001475 /* Don't start the coda unless both queues are on */
Michael Tretter888708b2017-03-03 09:12:47 -03001476 if (!(ctx->streamon_out && ctx->streamon_cap))
Philipp Zabel331e7862017-03-03 09:12:48 -03001477 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03001478
Philipp Zabel23b6ee52015-03-24 14:30:55 -03001479 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1480 if ((q_data_src->width != q_data_dst->width &&
1481 round_up(q_data_src->width, 16) != q_data_dst->width) ||
1482 (q_data_src->height != q_data_dst->height &&
1483 round_up(q_data_src->height, 16) != q_data_dst->height)) {
1484 v4l2_err(v4l2_dev, "can't convert %dx%d to %dx%d\n",
1485 q_data_src->width, q_data_src->height,
1486 q_data_dst->width, q_data_dst->height);
1487 ret = -EINVAL;
1488 goto err;
1489 }
1490
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001491 /* Allow BIT decoder device_run with no new buffers queued */
Philipp Zabela22496c2015-01-23 13:51:33 -03001492 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
Philipp Zabel14604e32014-07-11 06:36:22 -03001493 v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001494
Philipp Zabelb96904e2013-05-23 10:42:58 -03001495 ctx->gopcounter = ctx->params.gop_size - 1;
Javier Martin186b2502012-07-26 05:53:35 -03001496
Philipp Zabelb96904e2013-05-23 10:42:58 -03001497 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
1498 q_data_dst->fourcc);
1499 if (!ctx->codec) {
Javier Martin186b2502012-07-26 05:53:35 -03001500 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
Philipp Zabelb9063522014-08-05 14:00:10 -03001501 ret = -EINVAL;
1502 goto err;
Javier Martin186b2502012-07-26 05:53:35 -03001503 }
1504
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001505 if (q_data_dst->fourcc == V4L2_PIX_FMT_JPEG)
1506 ctx->params.gop_size = 1;
1507 ctx->gopcounter = ctx->params.gop_size - 1;
1508
Philipp Zabela1192a12014-07-23 12:28:40 -03001509 ret = ctx->ops->start_streaming(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001510 if (ctx->inst_type == CODA_INST_DECODER) {
Philipp Zabel89548442014-07-11 06:36:17 -03001511 if (ret == -EAGAIN)
Philipp Zabel331e7862017-03-03 09:12:48 -03001512 goto out;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001513 }
Philipp Zabel331e7862017-03-03 09:12:48 -03001514 if (ret < 0)
1515 goto err;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001516
Philipp Zabel331e7862017-03-03 09:12:48 -03001517out:
1518 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1519 list_for_each_entry_safe(m2m_buf, tmp, &list, list) {
1520 list_del(&m2m_buf->list);
1521 v4l2_m2m_buf_done(&m2m_buf->vb, VB2_BUF_STATE_DONE);
1522 }
1523 }
1524 return 0;
Philipp Zabelb9063522014-08-05 14:00:10 -03001525
1526err:
1527 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabel331e7862017-03-03 09:12:48 -03001528 list_for_each_entry_safe(m2m_buf, tmp, &list, list) {
1529 list_del(&m2m_buf->list);
1530 v4l2_m2m_buf_done(&m2m_buf->vb, VB2_BUF_STATE_QUEUED);
1531 }
Philipp Zabelb9063522014-08-05 14:00:10 -03001532 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
Philipp Zabelb8b1b582014-10-21 13:25:52 -03001533 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
Philipp Zabelb9063522014-08-05 14:00:10 -03001534 } else {
1535 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
Philipp Zabelb8b1b582014-10-21 13:25:52 -03001536 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
Philipp Zabelb9063522014-08-05 14:00:10 -03001537 }
1538 return ret;
Philipp Zabel89548442014-07-11 06:36:17 -03001539}
1540
Hans Verkuile37559b2014-04-17 02:47:21 -03001541static void coda_stop_streaming(struct vb2_queue *q)
Javier Martin186b2502012-07-26 05:53:35 -03001542{
1543 struct coda_ctx *ctx = vb2_get_drv_priv(q);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03001544 struct coda_dev *dev = ctx->dev;
Junghak Sung2d700712015-09-22 10:30:30 -03001545 struct vb2_v4l2_buffer *buf;
Philipp Zabel47f3fa62015-07-09 07:10:21 -03001546 unsigned long flags;
Philipp Zabel5c76c2c2015-03-24 14:30:56 -03001547 bool stop;
1548
1549 stop = ctx->streamon_out && ctx->streamon_cap;
Javier Martin186b2502012-07-26 05:53:35 -03001550
1551 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001552 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03001553 "%s: output\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001554 ctx->streamon_out = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001555
Philipp Zabel347bb7f2014-07-23 12:28:42 -03001556 coda_bit_stream_end_flag(ctx);
Philipp Zabel4a31b522014-08-05 14:00:11 -03001557
Philipp Zabel6dd5ef52015-01-23 13:51:26 -03001558 ctx->qsequence = 0;
Philipp Zabel4a31b522014-08-05 14:00:11 -03001559
1560 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1561 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
Javier Martin186b2502012-07-26 05:53:35 -03001562 } else {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001563 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03001564 "%s: capture\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001565 ctx->streamon_cap = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001566
1567 ctx->osequence = 0;
Philipp Zabelcb2c0282014-07-11 06:36:33 -03001568 ctx->sequence_offset = 0;
Philipp Zabel4a31b522014-08-05 14:00:11 -03001569
1570 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
1571 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
Javier Martin186b2502012-07-26 05:53:35 -03001572 }
1573
Philipp Zabel5c76c2c2015-03-24 14:30:56 -03001574 if (stop) {
Philipp Zabel7cbb1052014-10-02 14:08:32 -03001575 struct coda_buffer_meta *meta;
Philipp Zabel846ced92014-07-11 06:36:31 -03001576
Philipp Zabelf4706d62015-01-23 13:51:27 -03001577 if (ctx->ops->seq_end_work) {
1578 queue_work(dev->workqueue, &ctx->seq_end_work);
1579 flush_work(&ctx->seq_end_work);
1580 }
Philipp Zabel47f3fa62015-07-09 07:10:21 -03001581 spin_lock_irqsave(&ctx->buffer_meta_lock, flags);
Philipp Zabel7cbb1052014-10-02 14:08:32 -03001582 while (!list_empty(&ctx->buffer_meta_list)) {
1583 meta = list_first_entry(&ctx->buffer_meta_list,
1584 struct coda_buffer_meta, list);
1585 list_del(&meta->list);
1586 kfree(meta);
Philipp Zabel846ced92014-07-11 06:36:31 -03001587 }
Philipp Zabel47f3fa62015-07-09 07:10:21 -03001588 ctx->num_metas = 0;
1589 spin_unlock_irqrestore(&ctx->buffer_meta_lock, flags);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001590 kfifo_init(&ctx->bitstream_fifo,
1591 ctx->bitstream.vaddr, ctx->bitstream.size);
1592 ctx->runcounter = 0;
Philipp Zabelf157cf42014-09-29 09:53:42 -03001593 ctx->aborting = 0;
Philipp Zabel62bed142012-07-25 10:40:39 -03001594 }
Philipp Zabelc1ae0b22015-07-09 07:10:17 -03001595
1596 if (!ctx->streamon_out && !ctx->streamon_cap)
1597 ctx->bit_stream_param &= ~CODA_BIT_STREAM_END_FLAG;
Javier Martin186b2502012-07-26 05:53:35 -03001598}
1599
Philipp Zabel121cacf2014-07-18 07:22:42 -03001600static const struct vb2_ops coda_qops = {
Javier Martin186b2502012-07-26 05:53:35 -03001601 .queue_setup = coda_queue_setup,
1602 .buf_prepare = coda_buf_prepare,
1603 .buf_queue = coda_buf_queue,
Javier Martin186b2502012-07-26 05:53:35 -03001604 .start_streaming = coda_start_streaming,
1605 .stop_streaming = coda_stop_streaming,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001606 .wait_prepare = vb2_ops_wait_prepare,
1607 .wait_finish = vb2_ops_wait_finish,
Javier Martin186b2502012-07-26 05:53:35 -03001608};
1609
1610static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
1611{
1612 struct coda_ctx *ctx =
1613 container_of(ctrl->handler, struct coda_ctx, ctrls);
1614
1615 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1616 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
1617
1618 switch (ctrl->id) {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001619 case V4L2_CID_HFLIP:
1620 if (ctrl->val)
1621 ctx->params.rot_mode |= CODA_MIR_HOR;
1622 else
1623 ctx->params.rot_mode &= ~CODA_MIR_HOR;
1624 break;
1625 case V4L2_CID_VFLIP:
1626 if (ctrl->val)
1627 ctx->params.rot_mode |= CODA_MIR_VER;
1628 else
1629 ctx->params.rot_mode &= ~CODA_MIR_VER;
1630 break;
Javier Martin186b2502012-07-26 05:53:35 -03001631 case V4L2_CID_MPEG_VIDEO_BITRATE:
1632 ctx->params.bitrate = ctrl->val / 1000;
1633 break;
1634 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
1635 ctx->params.gop_size = ctrl->val;
1636 break;
1637 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
1638 ctx->params.h264_intra_qp = ctrl->val;
1639 break;
1640 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
1641 ctx->params.h264_inter_qp = ctrl->val;
1642 break;
Philipp Zabel1a5567e2014-07-11 06:36:26 -03001643 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
1644 ctx->params.h264_min_qp = ctrl->val;
1645 break;
1646 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
1647 ctx->params.h264_max_qp = ctrl->val;
1648 break;
Philipp Zabelde23b1d2014-07-11 06:36:27 -03001649 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
1650 ctx->params.h264_deblk_alpha = ctrl->val;
1651 break;
1652 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
1653 ctx->params.h264_deblk_beta = ctrl->val;
1654 break;
1655 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
1656 ctx->params.h264_deblk_enabled = (ctrl->val ==
1657 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
1658 break;
Javier Martin186b2502012-07-26 05:53:35 -03001659 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
1660 ctx->params.mpeg4_intra_qp = ctrl->val;
1661 break;
1662 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
1663 ctx->params.mpeg4_inter_qp = ctrl->val;
1664 break;
1665 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
1666 ctx->params.slice_mode = ctrl->val;
1667 break;
1668 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
1669 ctx->params.slice_max_mb = ctrl->val;
1670 break;
Philipp Zabelc566c782012-08-08 11:59:38 -03001671 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
1672 ctx->params.slice_max_bits = ctrl->val * 8;
1673 break;
Javier Martin186b2502012-07-26 05:53:35 -03001674 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
1675 break;
Philipp Zabelf38f79d2014-07-11 06:36:28 -03001676 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
1677 ctx->params.intra_refresh = ctrl->val;
1678 break;
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001679 case V4L2_CID_JPEG_COMPRESSION_QUALITY:
1680 coda_set_jpeg_compression_quality(ctx, ctrl->val);
1681 break;
1682 case V4L2_CID_JPEG_RESTART_INTERVAL:
1683 ctx->params.jpeg_restart_interval = ctrl->val;
1684 break;
Philipp Zabelda2b3b32015-07-10 10:37:52 -03001685 case V4L2_CID_MPEG_VIDEO_VBV_DELAY:
1686 ctx->params.vbv_delay = ctrl->val;
1687 break;
1688 case V4L2_CID_MPEG_VIDEO_VBV_SIZE:
1689 ctx->params.vbv_size = min(ctrl->val * 8192, 0x7fffffff);
1690 break;
Javier Martin186b2502012-07-26 05:53:35 -03001691 default:
1692 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1693 "Invalid control, id=%d, val=%d\n",
1694 ctrl->id, ctrl->val);
1695 return -EINVAL;
1696 }
1697
1698 return 0;
1699}
1700
Philipp Zabel814c3762014-07-18 07:22:45 -03001701static const struct v4l2_ctrl_ops coda_ctrl_ops = {
Javier Martin186b2502012-07-26 05:53:35 -03001702 .s_ctrl = coda_s_ctrl,
1703};
1704
Philipp Zabelbfc732f2014-10-02 14:08:29 -03001705static void coda_encode_ctrls(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03001706{
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001707 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel92b042e2015-03-18 08:15:36 -03001708 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1000, 0);
Javier Martin186b2502012-07-26 05:53:35 -03001709 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1710 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
1711 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03001712 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
Javier Martin186b2502012-07-26 05:53:35 -03001713 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03001714 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
Philipp Zabel1a5567e2014-07-11 06:36:26 -03001715 if (ctx->dev->devtype->product != CODA_960) {
1716 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1717 V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12);
1718 }
1719 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1720 V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51);
Javier Martin186b2502012-07-26 05:53:35 -03001721 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabelde23b1d2014-07-11 06:36:27 -03001722 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, 0, 15, 1, 0);
1723 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1724 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, 0, 15, 1, 0);
1725 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1726 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
1727 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED, 0x0,
1728 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
1729 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03001730 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
1731 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1732 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
1733 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1734 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
Philipp Zabelc566c782012-08-08 11:59:38 -03001735 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
1736 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
Javier Martin186b2502012-07-26 05:53:35 -03001737 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1738 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
Philipp Zabelc566c782012-08-08 11:59:38 -03001739 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabelf23797b2014-08-06 08:02:23 -03001740 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1,
1741 500);
Javier Martin186b2502012-07-26 05:53:35 -03001742 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1743 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
1744 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
1745 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
1746 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
Philipp Zabelf38f79d2014-07-11 06:36:28 -03001747 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabelf23797b2014-08-06 08:02:23 -03001748 V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0,
1749 1920 * 1088 / 256, 1, 0);
Philipp Zabelda2b3b32015-07-10 10:37:52 -03001750 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1751 V4L2_CID_MPEG_VIDEO_VBV_DELAY, 0, 0x7fff, 1, 0);
1752 /*
1753 * The maximum VBV size value is 0x7fffffff bits,
1754 * one bit less than 262144 KiB
1755 */
1756 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1757 V4L2_CID_MPEG_VIDEO_VBV_SIZE, 0, 262144, 1, 0);
Philipp Zabelbfc732f2014-10-02 14:08:29 -03001758}
1759
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001760static void coda_jpeg_encode_ctrls(struct coda_ctx *ctx)
1761{
1762 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1763 V4L2_CID_JPEG_COMPRESSION_QUALITY, 5, 100, 1, 50);
1764 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1765 V4L2_CID_JPEG_RESTART_INTERVAL, 0, 100, 1, 0);
1766}
1767
Philipp Zabelbfc732f2014-10-02 14:08:29 -03001768static int coda_ctrls_setup(struct coda_ctx *ctx)
1769{
1770 v4l2_ctrl_handler_init(&ctx->ctrls, 2);
1771
1772 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1773 V4L2_CID_HFLIP, 0, 1, 1, 0);
1774 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1775 V4L2_CID_VFLIP, 0, 1, 1, 0);
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001776 if (ctx->inst_type == CODA_INST_ENCODER) {
1777 if (ctx->cvd->dst_formats[0] == V4L2_PIX_FMT_JPEG)
1778 coda_jpeg_encode_ctrls(ctx);
1779 else
1780 coda_encode_ctrls(ctx);
1781 }
Javier Martin186b2502012-07-26 05:53:35 -03001782
1783 if (ctx->ctrls.error) {
Philipp Zabelf23797b2014-08-06 08:02:23 -03001784 v4l2_err(&ctx->dev->v4l2_dev,
1785 "control initialization error (%d)",
Javier Martin186b2502012-07-26 05:53:35 -03001786 ctx->ctrls.error);
1787 return -EINVAL;
1788 }
1789
1790 return v4l2_ctrl_handler_setup(&ctx->ctrls);
1791}
1792
Philipp Zabel121cacf2014-07-18 07:22:42 -03001793static int coda_queue_init(struct coda_ctx *ctx, struct vb2_queue *vq)
Javier Martin186b2502012-07-26 05:53:35 -03001794{
Philipp Zabel121cacf2014-07-18 07:22:42 -03001795 vq->drv_priv = ctx;
1796 vq->ops = &coda_qops;
1797 vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1798 vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1799 vq->lock = &ctx->dev->dev_mutex;
Kamil Debskie4af23d2015-02-23 09:26:18 -03001800 /* One way to indicate end-of-stream for coda is to set the
1801 * bytesused == 0. However by default videobuf2 handles bytesused
1802 * equal to 0 as a special case and changes its value to the size
1803 * of the buffer. Set the allow_zero_bytesused flag, so
1804 * that videobuf2 will keep the value of bytesused intact.
1805 */
1806 vq->allow_zero_bytesused = 1;
Hans Verkuil53ddcc62016-02-15 13:09:10 -02001807 vq->dev = &ctx->dev->plat_dev->dev;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001808
1809 return vb2_queue_init(vq);
1810}
1811
Philipp Zabel79924ca2014-07-23 12:28:45 -03001812int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq,
1813 struct vb2_queue *dst_vq)
Philipp Zabel121cacf2014-07-18 07:22:42 -03001814{
Javier Martin186b2502012-07-26 05:53:35 -03001815 int ret;
1816
Javier Martin186b2502012-07-26 05:53:35 -03001817 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabeld29a8cf2014-07-18 07:22:38 -03001818 src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
Javier Martin186b2502012-07-26 05:53:35 -03001819 src_vq->mem_ops = &vb2_dma_contig_memops;
1820
Philipp Zabel121cacf2014-07-18 07:22:42 -03001821 ret = coda_queue_init(priv, src_vq);
Javier Martin186b2502012-07-26 05:53:35 -03001822 if (ret)
1823 return ret;
1824
Javier Martin186b2502012-07-26 05:53:35 -03001825 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Philipp Zabeld29a8cf2014-07-18 07:22:38 -03001826 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
Javier Martin186b2502012-07-26 05:53:35 -03001827 dst_vq->mem_ops = &vb2_dma_contig_memops;
1828
Philipp Zabel121cacf2014-07-18 07:22:42 -03001829 return coda_queue_init(priv, dst_vq);
1830}
1831
Philipp Zabel79924ca2014-07-23 12:28:45 -03001832int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq,
1833 struct vb2_queue *dst_vq)
Philipp Zabel121cacf2014-07-18 07:22:42 -03001834{
1835 int ret;
1836
1837 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabelbb04aa62015-01-23 13:51:30 -03001838 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
1839 src_vq->mem_ops = &vb2_vmalloc_memops;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001840
1841 ret = coda_queue_init(priv, src_vq);
1842 if (ret)
1843 return ret;
1844
1845 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1846 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1847 dst_vq->mem_ops = &vb2_dma_contig_memops;
1848
1849 return coda_queue_init(priv, dst_vq);
Javier Martin186b2502012-07-26 05:53:35 -03001850}
1851
Philipp Zabele11f3e62012-07-25 09:16:58 -03001852static int coda_next_free_instance(struct coda_dev *dev)
1853{
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03001854 int idx = ffz(dev->instance_mask);
1855
1856 if ((idx < 0) ||
1857 (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES))
1858 return -EBUSY;
1859
1860 return idx;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001861}
1862
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001863/*
1864 * File operations
1865 */
1866
1867static int coda_open(struct file *file)
Javier Martin186b2502012-07-26 05:53:35 -03001868{
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001869 struct video_device *vdev = video_devdata(file);
1870 struct coda_dev *dev = video_get_drvdata(vdev);
Javier Martin186b2502012-07-26 05:53:35 -03001871 struct coda_ctx *ctx = NULL;
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001872 char *name;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001873 int ret;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001874 int idx;
Javier Martin186b2502012-07-26 05:53:35 -03001875
Philipp Zabelf23797b2014-08-06 08:02:23 -03001876 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
Javier Martin186b2502012-07-26 05:53:35 -03001877 if (!ctx)
1878 return -ENOMEM;
1879
Fabio Estevamf82bc202013-08-21 11:14:16 -03001880 idx = coda_next_free_instance(dev);
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03001881 if (idx < 0) {
1882 ret = idx;
Fabio Estevamf82bc202013-08-21 11:14:16 -03001883 goto err_coda_max;
1884 }
1885 set_bit(idx, &dev->instance_mask);
1886
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001887 name = kasprintf(GFP_KERNEL, "context%d", idx);
Peter Seiderera7f933a2015-03-24 14:30:47 -03001888 if (!name) {
1889 ret = -ENOMEM;
1890 goto err_coda_name_init;
1891 }
1892
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001893 ctx->debugfs_entry = debugfs_create_dir(name, dev->debugfs_root);
1894 kfree(name);
1895
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001896 ctx->cvd = to_coda_video_device(vdev);
1897 ctx->inst_type = ctx->cvd->type;
1898 ctx->ops = ctx->cvd->ops;
Philipp Zabela22496c2015-01-23 13:51:33 -03001899 ctx->use_bit = !ctx->cvd->direct;
Philipp Zabel32b6f202014-07-11 06:36:20 -03001900 init_completion(&ctx->completion);
1901 INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
Philipp Zabel747d7642015-01-23 13:51:31 -03001902 if (ctx->ops->seq_end_work)
1903 INIT_WORK(&ctx->seq_end_work, ctx->ops->seq_end_work);
Javier Martin186b2502012-07-26 05:53:35 -03001904 v4l2_fh_init(&ctx->fh, video_devdata(file));
1905 file->private_data = &ctx->fh;
1906 v4l2_fh_add(&ctx->fh);
1907 ctx->dev = dev;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001908 ctx->idx = idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001909 switch (dev->devtype->product) {
Philipp Zabel89548442014-07-11 06:36:17 -03001910 case CODA_960:
Philipp Zabel89ed0252017-03-02 07:19:52 -03001911 if (enable_bwb)
1912 ctx->frame_mem_ctrl = CODA9_FRAME_ENABLE_BWB;
Philipp Zabel2bf299c2014-09-29 09:53:46 -03001913 /* fallthrough */
1914 case CODA_7541:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001915 ctx->reg_idx = 0;
1916 break;
1917 default:
1918 ctx->reg_idx = idx;
1919 }
Michael Trettere7f3c542017-01-20 12:00:24 -02001920 if (ctx->dev->vdoa && !disable_vdoa) {
1921 ctx->vdoa = vdoa_context_create(dev->vdoa);
1922 if (!ctx->vdoa)
1923 v4l2_warn(&dev->v4l2_dev,
1924 "Failed to create vdoa context: not using vdoa");
1925 }
1926 ctx->use_vdoa = false;
Fabio Estevamf82bc202013-08-21 11:14:16 -03001927
Philipp Zabel1e172732014-07-11 06:36:23 -03001928 /* Power up and upload firmware if necessary */
1929 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
1930 if (ret < 0) {
1931 v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
1932 goto err_pm_get;
1933 }
1934
Fabio Estevam79830db2013-08-21 11:14:17 -03001935 ret = clk_prepare_enable(dev->clk_per);
1936 if (ret)
1937 goto err_clk_per;
1938
1939 ret = clk_prepare_enable(dev->clk_ahb);
1940 if (ret)
1941 goto err_clk_ahb;
1942
Javier Martin186b2502012-07-26 05:53:35 -03001943 set_default_params(ctx);
Philipp Zabela1192a12014-07-23 12:28:40 -03001944 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
1945 ctx->ops->queue_init);
Philipp Zabel14604e32014-07-11 06:36:22 -03001946 if (IS_ERR(ctx->fh.m2m_ctx)) {
1947 ret = PTR_ERR(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001948
1949 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
1950 __func__, ret);
Fabio Estevamf82bc202013-08-21 11:14:16 -03001951 goto err_ctx_init;
Javier Martin186b2502012-07-26 05:53:35 -03001952 }
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001953
Javier Martin186b2502012-07-26 05:53:35 -03001954 ret = coda_ctrls_setup(ctx);
1955 if (ret) {
1956 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
Fabio Estevamf82bc202013-08-21 11:14:16 -03001957 goto err_ctrls_setup;
Javier Martin186b2502012-07-26 05:53:35 -03001958 }
1959
1960 ctx->fh.ctrl_handler = &ctx->ctrls;
1961
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001962 mutex_init(&ctx->bitstream_mutex);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001963 mutex_init(&ctx->buffer_mutex);
Philipp Zabel7cbb1052014-10-02 14:08:32 -03001964 INIT_LIST_HEAD(&ctx->buffer_meta_list);
Philipp Zabel47f3fa62015-07-09 07:10:21 -03001965 spin_lock_init(&ctx->buffer_meta_lock);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001966
Javier Martin186b2502012-07-26 05:53:35 -03001967 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001968 list_add(&ctx->list, &dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03001969 coda_unlock(ctx);
1970
Javier Martin186b2502012-07-26 05:53:35 -03001971 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
1972 ctx->idx, ctx);
1973
1974 return 0;
1975
Fabio Estevamf82bc202013-08-21 11:14:16 -03001976err_ctrls_setup:
Philipp Zabel14604e32014-07-11 06:36:22 -03001977 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Fabio Estevamf82bc202013-08-21 11:14:16 -03001978err_ctx_init:
1979 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03001980err_clk_ahb:
Fabio Estevamf82bc202013-08-21 11:14:16 -03001981 clk_disable_unprepare(dev->clk_per);
Fabio Estevam79830db2013-08-21 11:14:17 -03001982err_clk_per:
Philipp Zabel1e172732014-07-11 06:36:23 -03001983 pm_runtime_put_sync(&dev->plat_dev->dev);
1984err_pm_get:
Javier Martin186b2502012-07-26 05:53:35 -03001985 v4l2_fh_del(&ctx->fh);
1986 v4l2_fh_exit(&ctx->fh);
Fabio Estevamf82bc202013-08-21 11:14:16 -03001987 clear_bit(ctx->idx, &dev->instance_mask);
Peter Seiderera7f933a2015-03-24 14:30:47 -03001988err_coda_name_init:
Fabio Estevamf82bc202013-08-21 11:14:16 -03001989err_coda_max:
Javier Martin186b2502012-07-26 05:53:35 -03001990 kfree(ctx);
1991 return ret;
1992}
1993
1994static int coda_release(struct file *file)
1995{
1996 struct coda_dev *dev = video_drvdata(file);
1997 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
1998
1999 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
2000 ctx);
2001
Philipp Zabela22496c2015-01-23 13:51:33 -03002002 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
Philipp Zabeld4bb75f2014-10-08 13:09:11 -03002003 coda_bit_stream_end_flag(ctx);
2004
Philipp Zabel918c66f2013-06-27 06:59:01 -03002005 /* If this instance is running, call .job_abort and wait for it to end */
Philipp Zabel14604e32014-07-11 06:36:22 -03002006 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002007
Michael Trettere7f3c542017-01-20 12:00:24 -02002008 if (ctx->vdoa)
2009 vdoa_context_destroy(ctx->vdoa);
2010
Philipp Zabel918c66f2013-06-27 06:59:01 -03002011 /* In case the instance was not running, we still need to call SEQ_END */
Philipp Zabel5c718bb2015-07-09 07:10:16 -03002012 if (ctx->ops->seq_end_work) {
Philipp Zabel32b6f202014-07-11 06:36:20 -03002013 queue_work(dev->workqueue, &ctx->seq_end_work);
2014 flush_work(&ctx->seq_end_work);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002015 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03002016
Javier Martin186b2502012-07-26 05:53:35 -03002017 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002018 list_del(&ctx->list);
Javier Martin186b2502012-07-26 05:53:35 -03002019 coda_unlock(ctx);
2020
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002021 if (ctx->dev->devtype->product == CODA_DX6)
2022 coda_free_aux_buf(dev, &ctx->workbuf);
2023
Javier Martin186b2502012-07-26 05:53:35 -03002024 v4l2_ctrl_handler_free(&ctx->ctrls);
Javier Martin186b2502012-07-26 05:53:35 -03002025 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002026 clk_disable_unprepare(dev->clk_per);
Philipp Zabel1e172732014-07-11 06:36:23 -03002027 pm_runtime_put_sync(&dev->plat_dev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03002028 v4l2_fh_del(&ctx->fh);
2029 v4l2_fh_exit(&ctx->fh);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002030 clear_bit(ctx->idx, &dev->instance_mask);
Philipp Zabel58b76772014-07-23 12:28:43 -03002031 if (ctx->ops->release)
2032 ctx->ops->release(ctx);
Philipp Zabele1519e82015-01-23 13:51:17 -03002033 debugfs_remove_recursive(ctx->debugfs_entry);
Javier Martin186b2502012-07-26 05:53:35 -03002034 kfree(ctx);
2035
2036 return 0;
2037}
2038
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002039static const struct v4l2_file_operations coda_fops = {
Javier Martin186b2502012-07-26 05:53:35 -03002040 .owner = THIS_MODULE,
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002041 .open = coda_open,
Javier Martin186b2502012-07-26 05:53:35 -03002042 .release = coda_release,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002043 .poll = v4l2_m2m_fop_poll,
Javier Martin186b2502012-07-26 05:53:35 -03002044 .unlocked_ioctl = video_ioctl2,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002045 .mmap = v4l2_m2m_fop_mmap,
Javier Martin186b2502012-07-26 05:53:35 -03002046};
2047
Philipp Zabel87048bb2012-07-02 07:03:43 -03002048static int coda_hw_init(struct coda_dev *dev)
Javier Martin186b2502012-07-26 05:53:35 -03002049{
Javier Martin186b2502012-07-26 05:53:35 -03002050 u32 data;
2051 u16 *p;
Fabio Estevam79830db2013-08-21 11:14:17 -03002052 int i, ret;
Javier Martin186b2502012-07-26 05:53:35 -03002053
Fabio Estevam79830db2013-08-21 11:14:17 -03002054 ret = clk_prepare_enable(dev->clk_per);
2055 if (ret)
Philipp Zabel1e172732014-07-11 06:36:23 -03002056 goto err_clk_per;
Fabio Estevam79830db2013-08-21 11:14:17 -03002057
2058 ret = clk_prepare_enable(dev->clk_ahb);
2059 if (ret)
2060 goto err_clk_ahb;
Javier Martin186b2502012-07-26 05:53:35 -03002061
Philipp Zabel8f452842014-07-11 06:36:35 -03002062 if (dev->rstc)
2063 reset_control_reset(dev->rstc);
2064
Javier Martin186b2502012-07-26 05:53:35 -03002065 /*
2066 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
Philipp Zabel87048bb2012-07-02 07:03:43 -03002067 * The 16-bit chars in the code buffer are in memory access
2068 * order, re-sort them to CODA order for register download.
Javier Martin186b2502012-07-26 05:53:35 -03002069 * Data in this SRAM survives a reboot.
2070 */
Philipp Zabel87048bb2012-07-02 07:03:43 -03002071 p = (u16 *)dev->codebuf.vaddr;
2072 if (dev->devtype->product == CODA_DX6) {
2073 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
2074 data = CODA_DOWN_ADDRESS_SET(i) |
2075 CODA_DOWN_DATA_SET(p[i ^ 1]);
2076 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
2077 }
2078 } else {
2079 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
2080 data = CODA_DOWN_ADDRESS_SET(i) |
2081 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
2082 3 - (i % 4)]);
2083 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
2084 }
Javier Martin186b2502012-07-26 05:53:35 -03002085 }
Javier Martin186b2502012-07-26 05:53:35 -03002086
Philipp Zabel9acf7692013-05-23 10:42:56 -03002087 /* Clear registers */
2088 for (i = 0; i < 64; i++)
2089 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
2090
Javier Martin186b2502012-07-26 05:53:35 -03002091 /* Tell the BIT where to find everything it needs */
Philipp Zabel89548442014-07-11 06:36:17 -03002092 if (dev->devtype->product == CODA_960 ||
2093 dev->devtype->product == CODA_7541) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002094 coda_write(dev, dev->tempbuf.paddr,
2095 CODA_REG_BIT_TEMP_BUF_ADDR);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002096 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002097 } else {
2098 coda_write(dev, dev->workbuf.paddr,
2099 CODA_REG_BIT_WORK_BUF_ADDR);
2100 }
Javier Martin186b2502012-07-26 05:53:35 -03002101 coda_write(dev, dev->codebuf.paddr,
2102 CODA_REG_BIT_CODE_BUF_ADDR);
2103 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
2104
2105 /* Set default values */
2106 switch (dev->devtype->product) {
2107 case CODA_DX6:
Philipp Zabelf23797b2014-08-06 08:02:23 -03002108 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH,
2109 CODA_REG_BIT_STREAM_CTRL);
Javier Martin186b2502012-07-26 05:53:35 -03002110 break;
2111 default:
Philipp Zabelf23797b2014-08-06 08:02:23 -03002112 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH,
2113 CODA_REG_BIT_STREAM_CTRL);
Javier Martin186b2502012-07-26 05:53:35 -03002114 }
Philipp Zabel89548442014-07-11 06:36:17 -03002115 if (dev->devtype->product == CODA_960)
2116 coda_write(dev, 1 << 12, CODA_REG_BIT_FRAME_MEM_CTRL);
2117 else
2118 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel10436672012-07-02 09:03:55 -03002119
2120 if (dev->devtype->product != CODA_DX6)
2121 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
2122
Javier Martin186b2502012-07-26 05:53:35 -03002123 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
2124 CODA_REG_BIT_INT_ENABLE);
2125
2126 /* Reset VPU and start processor */
2127 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
2128 data |= CODA_REG_RESET_ENABLE;
2129 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
2130 udelay(10);
2131 data &= ~CODA_REG_RESET_ENABLE;
2132 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
2133 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
2134
Philipp Zabelfe7554e2014-07-11 06:36:24 -03002135 clk_disable_unprepare(dev->clk_ahb);
2136 clk_disable_unprepare(dev->clk_per);
2137
2138 return 0;
2139
2140err_clk_ahb:
2141 clk_disable_unprepare(dev->clk_per);
2142err_clk_per:
2143 return ret;
2144}
2145
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002146static int coda_register_device(struct coda_dev *dev, int i)
Philipp Zabel121cacf2014-07-18 07:22:42 -03002147{
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002148 struct video_device *vfd = &dev->vfd[i];
2149
Dan Carpenter88ca3af2015-01-08 07:07:08 -03002150 if (i >= dev->devtype->num_vdevs)
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002151 return -EINVAL;
2152
Philipp Zabelbf249da2015-03-24 14:30:50 -03002153 strlcpy(vfd->name, dev->devtype->vdevs[i]->name, sizeof(vfd->name));
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002154 vfd->fops = &coda_fops;
2155 vfd->ioctl_ops = &coda_ioctl_ops;
Philipp Zabel121cacf2014-07-18 07:22:42 -03002156 vfd->release = video_device_release_empty,
2157 vfd->lock = &dev->dev_mutex;
2158 vfd->v4l2_dev = &dev->v4l2_dev;
2159 vfd->vfl_dir = VFL_DIR_M2M;
2160 video_set_drvdata(vfd, dev);
2161
Philipp Zabela188a662014-08-05 14:00:20 -03002162 /* Not applicable, use the selection API instead */
2163 v4l2_disable_ioctl(vfd, VIDIOC_CROPCAP);
2164 v4l2_disable_ioctl(vfd, VIDIOC_G_CROP);
2165 v4l2_disable_ioctl(vfd, VIDIOC_S_CROP);
2166
Philipp Zabel121cacf2014-07-18 07:22:42 -03002167 return video_register_device(vfd, VFL_TYPE_GRABBER, 0);
2168}
2169
Philipp Zabela1a87fa2016-02-17 11:21:10 -02002170static void coda_copy_firmware(struct coda_dev *dev, const u8 * const buf,
2171 size_t size)
2172{
2173 u32 *src = (u32 *)buf;
2174
2175 /* Check if the firmware has a 16-byte Freescale header, skip it */
2176 if (buf[0] == 'M' && buf[1] == 'X')
2177 src += 4;
2178 /*
2179 * Check whether the firmware is in native order or pre-reordered for
2180 * memory access. The first instruction opcode always is 0xe40e.
2181 */
2182 if (__le16_to_cpup((__le16 *)src) == 0xe40e) {
2183 u32 *dst = dev->codebuf.vaddr;
2184 int i;
2185
2186 /* Firmware in native order, reorder while copying */
2187 if (dev->devtype->product == CODA_DX6) {
2188 for (i = 0; i < (size - 16) / 4; i++)
2189 dst[i] = (src[i] << 16) | (src[i] >> 16);
2190 } else {
2191 for (i = 0; i < (size - 16) / 4; i += 2) {
2192 dst[i] = (src[i + 1] << 16) | (src[i + 1] >> 16);
2193 dst[i + 1] = (src[i] << 16) | (src[i] >> 16);
2194 }
2195 }
2196 } else {
2197 /* Copy the already reordered firmware image */
2198 memcpy(dev->codebuf.vaddr, src, size);
2199 }
2200}
2201
Philipp Zabel2ac7f082016-02-19 07:18:57 -02002202static void coda_fw_callback(const struct firmware *fw, void *context);
2203
2204static int coda_firmware_request(struct coda_dev *dev)
2205{
Philipp Zabel1e9b71d2017-03-08 09:30:50 -03002206 char *fw;
2207
2208 if (dev->firmware >= ARRAY_SIZE(dev->devtype->firmware))
2209 return -EINVAL;
2210
2211 fw = dev->devtype->firmware[dev->firmware];
Philipp Zabel2ac7f082016-02-19 07:18:57 -02002212
2213 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
2214 coda_product_name(dev->devtype->product));
2215
2216 return request_firmware_nowait(THIS_MODULE, true, fw,
2217 &dev->plat_dev->dev, GFP_KERNEL, dev,
2218 coda_fw_callback);
2219}
2220
Javier Martin186b2502012-07-26 05:53:35 -03002221static void coda_fw_callback(const struct firmware *fw, void *context)
2222{
2223 struct coda_dev *dev = context;
2224 struct platform_device *pdev = dev->plat_dev;
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002225 int i, ret;
Javier Martin186b2502012-07-26 05:53:35 -03002226
Philipp Zabel2ac7f082016-02-19 07:18:57 -02002227 if (!fw) {
Philipp Zabel1e9b71d2017-03-08 09:30:50 -03002228 dev->firmware++;
2229 ret = coda_firmware_request(dev);
2230 if (ret < 0) {
2231 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
2232 goto put_pm;
2233 }
Philipp Zabel2ac7f082016-02-19 07:18:57 -02002234 return;
2235 }
Philipp Zabel1e9b71d2017-03-08 09:30:50 -03002236 if (dev->firmware > 0) {
Philipp Zabel2ac7f082016-02-19 07:18:57 -02002237 /*
2238 * Since we can't suppress warnings for failed asynchronous
2239 * firmware requests, report that the fallback firmware was
2240 * found.
2241 */
2242 dev_info(&pdev->dev, "Using fallback firmware %s\n",
2243 dev->devtype->firmware[dev->firmware]);
2244 }
Javier Martin186b2502012-07-26 05:53:35 -03002245
2246 /* allocate auxiliary per-device code buffer for the BIT processor */
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002247 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size, "codebuf",
2248 dev->debugfs_root);
Philipp Zabel6ba53b82015-03-24 14:30:54 -03002249 if (ret < 0)
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03002250 goto put_pm;
Javier Martin186b2502012-07-26 05:53:35 -03002251
Philipp Zabela1a87fa2016-02-17 11:21:10 -02002252 coda_copy_firmware(dev, fw->data, fw->size);
Philipp Zabel87048bb2012-07-02 07:03:43 -03002253 release_firmware(fw);
2254
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03002255 ret = coda_hw_init(dev);
2256 if (ret < 0) {
2257 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
2258 goto put_pm;
Javier Martin186b2502012-07-26 05:53:35 -03002259 }
2260
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03002261 ret = coda_check_firmware(dev);
2262 if (ret < 0)
2263 goto put_pm;
2264
Javier Martin186b2502012-07-26 05:53:35 -03002265 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
2266 if (IS_ERR(dev->m2m_dev)) {
2267 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
Hans Verkuil53ddcc62016-02-15 13:09:10 -02002268 goto put_pm;
Javier Martin186b2502012-07-26 05:53:35 -03002269 }
2270
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002271 for (i = 0; i < dev->devtype->num_vdevs; i++) {
2272 ret = coda_register_device(dev, i);
2273 if (ret) {
2274 v4l2_err(&dev->v4l2_dev,
2275 "Failed to register %s video device: %d\n",
2276 dev->devtype->vdevs[i]->name, ret);
2277 goto rel_vfd;
2278 }
Philipp Zabel121cacf2014-07-18 07:22:42 -03002279 }
2280
2281 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video[%d-%d]\n",
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002282 dev->vfd[0].num, dev->vfd[i - 1].num);
Javier Martin186b2502012-07-26 05:53:35 -03002283
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03002284 pm_runtime_put_sync(&pdev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03002285 return;
2286
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002287rel_vfd:
2288 while (--i >= 0)
2289 video_unregister_device(&dev->vfd[i]);
Javier Martin186b2502012-07-26 05:53:35 -03002290 v4l2_m2m_release(dev->m2m_dev);
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03002291put_pm:
2292 pm_runtime_put_sync(&pdev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03002293}
2294
Javier Martin186b2502012-07-26 05:53:35 -03002295enum coda_platform {
2296 CODA_IMX27,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03002297 CODA_IMX53,
Philipp Zabel89548442014-07-11 06:36:17 -03002298 CODA_IMX6Q,
2299 CODA_IMX6DL,
Javier Martin186b2502012-07-26 05:53:35 -03002300};
2301
Emil Goodec06d8752012-08-14 17:44:42 -03002302static const struct coda_devtype coda_devdata[] = {
Javier Martin186b2502012-07-26 05:53:35 -03002303 [CODA_IMX27] = {
Philipp Zabel2ac7f082016-02-19 07:18:57 -02002304 .firmware = {
2305 "vpu_fw_imx27_TO2.bin",
Baruch Siach8af77792017-01-15 08:33:53 -02002306 "vpu/vpu_fw_imx27_TO2.bin",
Philipp Zabel2ac7f082016-02-19 07:18:57 -02002307 "v4l-codadx6-imx27.bin"
2308 },
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002309 .product = CODA_DX6,
2310 .codecs = codadx6_codecs,
2311 .num_codecs = ARRAY_SIZE(codadx6_codecs),
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002312 .vdevs = codadx6_video_devices,
2313 .num_vdevs = ARRAY_SIZE(codadx6_video_devices),
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002314 .workbuf_size = 288 * 1024 + FMO_SLICE_SAVE_BUF_SIZE * 8 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03002315 .iram_size = 0xb000,
Javier Martin186b2502012-07-26 05:53:35 -03002316 },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03002317 [CODA_IMX53] = {
Philipp Zabel2ac7f082016-02-19 07:18:57 -02002318 .firmware = {
2319 "vpu_fw_imx53.bin",
Baruch Siach8af77792017-01-15 08:33:53 -02002320 "vpu/vpu_fw_imx53.bin",
Philipp Zabel2ac7f082016-02-19 07:18:57 -02002321 "v4l-coda7541-imx53.bin"
2322 },
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002323 .product = CODA_7541,
2324 .codecs = coda7_codecs,
2325 .num_codecs = ARRAY_SIZE(coda7_codecs),
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002326 .vdevs = coda7_video_devices,
2327 .num_vdevs = ARRAY_SIZE(coda7_video_devices),
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002328 .workbuf_size = 128 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03002329 .tempbuf_size = 304 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03002330 .iram_size = 0x14000,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03002331 },
Philipp Zabel89548442014-07-11 06:36:17 -03002332 [CODA_IMX6Q] = {
Philipp Zabel2ac7f082016-02-19 07:18:57 -02002333 .firmware = {
2334 "vpu_fw_imx6q.bin",
Baruch Siach8af77792017-01-15 08:33:53 -02002335 "vpu/vpu_fw_imx6q.bin",
Philipp Zabel2ac7f082016-02-19 07:18:57 -02002336 "v4l-coda960-imx6q.bin"
2337 },
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002338 .product = CODA_960,
2339 .codecs = coda9_codecs,
2340 .num_codecs = ARRAY_SIZE(coda9_codecs),
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002341 .vdevs = coda9_video_devices,
2342 .num_vdevs = ARRAY_SIZE(coda9_video_devices),
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002343 .workbuf_size = 80 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03002344 .tempbuf_size = 204 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03002345 .iram_size = 0x21000,
Philipp Zabel89548442014-07-11 06:36:17 -03002346 },
2347 [CODA_IMX6DL] = {
Philipp Zabel2ac7f082016-02-19 07:18:57 -02002348 .firmware = {
2349 "vpu_fw_imx6d.bin",
Baruch Siach8af77792017-01-15 08:33:53 -02002350 "vpu/vpu_fw_imx6d.bin",
Philipp Zabel2ac7f082016-02-19 07:18:57 -02002351 "v4l-coda960-imx6dl.bin"
2352 },
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002353 .product = CODA_960,
2354 .codecs = coda9_codecs,
2355 .num_codecs = ARRAY_SIZE(coda9_codecs),
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002356 .vdevs = coda9_video_devices,
2357 .num_vdevs = ARRAY_SIZE(coda9_video_devices),
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002358 .workbuf_size = 80 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03002359 .tempbuf_size = 204 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03002360 .iram_size = 0x20000,
Philipp Zabel89548442014-07-11 06:36:17 -03002361 },
Javier Martin186b2502012-07-26 05:53:35 -03002362};
2363
2364static struct platform_device_id coda_platform_ids[] = {
2365 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
2366 { /* sentinel */ }
2367};
2368MODULE_DEVICE_TABLE(platform, coda_platform_ids);
2369
2370#ifdef CONFIG_OF
2371static const struct of_device_id coda_dt_ids[] = {
Alexander Shiyan7b0dd9e2013-06-15 08:09:57 -03002372 { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03002373 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
Philipp Zabel89548442014-07-11 06:36:17 -03002374 { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
2375 { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
Javier Martin186b2502012-07-26 05:53:35 -03002376 { /* sentinel */ }
2377};
2378MODULE_DEVICE_TABLE(of, coda_dt_ids);
2379#endif
2380
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08002381static int coda_probe(struct platform_device *pdev)
Javier Martin186b2502012-07-26 05:53:35 -03002382{
2383 const struct of_device_id *of_id =
2384 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
2385 const struct platform_device_id *pdev_id;
Philipp Zabel657eee72013-04-29 16:17:14 -07002386 struct coda_platform_data *pdata = pdev->dev.platform_data;
2387 struct device_node *np = pdev->dev.of_node;
2388 struct gen_pool *pool;
Javier Martin186b2502012-07-26 05:53:35 -03002389 struct coda_dev *dev;
2390 struct resource *res;
2391 int ret, irq;
2392
Philipp Zabelf23797b2014-08-06 08:02:23 -03002393 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
Philipp Zabel6da999d2014-09-29 09:53:43 -03002394 if (!dev)
Javier Martin186b2502012-07-26 05:53:35 -03002395 return -ENOMEM;
Javier Martin186b2502012-07-26 05:53:35 -03002396
Philipp Zabelb2f91ae2014-10-02 14:08:27 -03002397 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
2398
Philipp Zabelbc717d52016-02-26 08:21:35 -03002399 if (of_id)
Philipp Zabelb2f91ae2014-10-02 14:08:27 -03002400 dev->devtype = of_id->data;
Philipp Zabelbc717d52016-02-26 08:21:35 -03002401 else if (pdev_id)
Philipp Zabelb2f91ae2014-10-02 14:08:27 -03002402 dev->devtype = &coda_devdata[pdev_id->driver_data];
Philipp Zabelbc717d52016-02-26 08:21:35 -03002403 else
2404 return -EINVAL;
Javier Martin186b2502012-07-26 05:53:35 -03002405
2406 spin_lock_init(&dev->irqlock);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002407 INIT_LIST_HEAD(&dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03002408
2409 dev->plat_dev = pdev;
2410 dev->clk_per = devm_clk_get(&pdev->dev, "per");
2411 if (IS_ERR(dev->clk_per)) {
2412 dev_err(&pdev->dev, "Could not get per clock\n");
2413 return PTR_ERR(dev->clk_per);
2414 }
2415
2416 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
2417 if (IS_ERR(dev->clk_ahb)) {
2418 dev_err(&pdev->dev, "Could not get ahb clock\n");
2419 return PTR_ERR(dev->clk_ahb);
2420 }
2421
2422 /* Get memory for physical registers */
2423 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Philipp Zabel611cbbf2013-05-21 04:19:27 -03002424 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
2425 if (IS_ERR(dev->regs_base))
2426 return PTR_ERR(dev->regs_base);
Javier Martin186b2502012-07-26 05:53:35 -03002427
2428 /* IRQ */
Philipp Zabel540b72e2014-08-05 14:00:09 -03002429 irq = platform_get_irq_byname(pdev, "bit");
2430 if (irq < 0)
2431 irq = platform_get_irq(pdev, 0);
Javier Martin186b2502012-07-26 05:53:35 -03002432 if (irq < 0) {
2433 dev_err(&pdev->dev, "failed to get irq resource\n");
Fabio Estevam7d377432014-06-04 15:46:23 -03002434 return irq;
Javier Martin186b2502012-07-26 05:53:35 -03002435 }
2436
Fabio Estevam08c8d142014-06-04 15:46:24 -03002437 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
2438 IRQF_ONESHOT, dev_name(&pdev->dev), dev);
2439 if (ret < 0) {
2440 dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
2441 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03002442 }
2443
Philipp Zabelee27aa92014-07-24 16:50:03 -03002444 dev->rstc = devm_reset_control_get_optional(&pdev->dev, NULL);
Philipp Zabel8f452842014-07-11 06:36:35 -03002445 if (IS_ERR(dev->rstc)) {
2446 ret = PTR_ERR(dev->rstc);
Alexander Shiyan84345a22016-06-18 11:45:15 -03002447 if (ret == -ENOENT || ret == -ENOTSUPP) {
Philipp Zabel8f452842014-07-11 06:36:35 -03002448 dev->rstc = NULL;
2449 } else {
Philipp Zabelf23797b2014-08-06 08:02:23 -03002450 dev_err(&pdev->dev, "failed get reset control: %d\n",
2451 ret);
Philipp Zabel8f452842014-07-11 06:36:35 -03002452 return ret;
2453 }
2454 }
2455
Philipp Zabel657eee72013-04-29 16:17:14 -07002456 /* Get IRAM pool from device tree or platform data */
Vladimir Zapolskiyabdd4a72015-06-30 15:00:07 -07002457 pool = of_gen_pool_get(np, "iram", 0);
Philipp Zabel657eee72013-04-29 16:17:14 -07002458 if (!pool && pdata)
Vladimir Zapolskiy73858172015-09-04 15:47:43 -07002459 pool = gen_pool_get(pdata->iram_dev, NULL);
Philipp Zabel657eee72013-04-29 16:17:14 -07002460 if (!pool) {
2461 dev_err(&pdev->dev, "iram pool not available\n");
2462 return -ENOMEM;
2463 }
2464 dev->iram_pool = pool;
2465
Michael Trettere7f3c542017-01-20 12:00:24 -02002466 /* Get vdoa_data if supported by the platform */
2467 dev->vdoa = coda_get_vdoa_data();
2468 if (PTR_ERR(dev->vdoa) == -EPROBE_DEFER)
2469 return -EPROBE_DEFER;
2470
Javier Martin186b2502012-07-26 05:53:35 -03002471 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
2472 if (ret)
2473 return ret;
2474
2475 mutex_init(&dev->dev_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002476 mutex_init(&dev->coda_mutex);
Javier Martin186b2502012-07-26 05:53:35 -03002477
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002478 dev->debugfs_root = debugfs_create_dir("coda", NULL);
2479 if (!dev->debugfs_root)
2480 dev_warn(&pdev->dev, "failed to create debugfs root\n");
2481
Javier Martin186b2502012-07-26 05:53:35 -03002482 /* allocate auxiliary per-device buffers for the BIT processor */
Philipp Zabel5d73fad2014-07-11 06:36:42 -03002483 if (dev->devtype->product == CODA_DX6) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002484 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002485 dev->devtype->workbuf_size, "workbuf",
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002486 dev->debugfs_root);
Philipp Zabel6ba53b82015-03-24 14:30:54 -03002487 if (ret < 0)
Fabio Estevamb7bd6602014-10-04 16:40:50 -03002488 goto err_v4l2_register;
Javier Martin186b2502012-07-26 05:53:35 -03002489 }
Philipp Zabel5d73fad2014-07-11 06:36:42 -03002490
2491 if (dev->devtype->tempbuf_size) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002492 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03002493 dev->devtype->tempbuf_size, "tempbuf",
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002494 dev->debugfs_root);
Philipp Zabel6ba53b82015-03-24 14:30:54 -03002495 if (ret < 0)
Fabio Estevamb7bd6602014-10-04 16:40:50 -03002496 goto err_v4l2_register;
Javier Martin186b2502012-07-26 05:53:35 -03002497 }
2498
Philipp Zabel401e9722014-07-11 06:36:43 -03002499 dev->iram.size = dev->devtype->iram_size;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03002500 dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
2501 &dev->iram.paddr);
2502 if (!dev->iram.vaddr) {
Philipp Zabel8be31c82014-08-05 14:00:13 -03002503 dev_warn(&pdev->dev, "unable to alloc iram\n");
2504 } else {
Philipp Zabel811a6932015-01-23 13:51:23 -03002505 memset(dev->iram.vaddr, 0, dev->iram.size);
Philipp Zabel8be31c82014-08-05 14:00:13 -03002506 dev->iram.blob.data = dev->iram.vaddr;
2507 dev->iram.blob.size = dev->iram.size;
2508 dev->iram.dentry = debugfs_create_blob("iram", 0644,
2509 dev->debugfs_root,
2510 &dev->iram.blob);
Philipp Zabel10436672012-07-02 09:03:55 -03002511 }
2512
Philipp Zabel32b6f202014-07-11 06:36:20 -03002513 dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
2514 if (!dev->workqueue) {
2515 dev_err(&pdev->dev, "unable to alloc workqueue\n");
Fabio Estevam74d08d52014-10-04 16:40:51 -03002516 ret = -ENOMEM;
2517 goto err_v4l2_register;
Philipp Zabel32b6f202014-07-11 06:36:20 -03002518 }
2519
Javier Martin186b2502012-07-26 05:53:35 -03002520 platform_set_drvdata(pdev, dev);
2521
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03002522 /*
2523 * Start activated so we can directly call coda_hw_init in
Rafael J. Wysockie243c7c2014-12-04 01:10:10 +01002524 * coda_fw_callback regardless of whether CONFIG_PM is
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03002525 * enabled or whether the device is associated with a PM domain.
2526 */
2527 pm_runtime_get_noresume(&pdev->dev);
2528 pm_runtime_set_active(&pdev->dev);
Philipp Zabel1e172732014-07-11 06:36:23 -03002529 pm_runtime_enable(&pdev->dev);
2530
Fabio Estevamc762ff12016-10-04 12:41:37 -03002531 ret = coda_firmware_request(dev);
2532 if (ret)
2533 goto err_alloc_workqueue;
2534 return 0;
Fabio Estevamb7bd6602014-10-04 16:40:50 -03002535
Fabio Estevamc762ff12016-10-04 12:41:37 -03002536err_alloc_workqueue:
2537 destroy_workqueue(dev->workqueue);
Fabio Estevamb7bd6602014-10-04 16:40:50 -03002538err_v4l2_register:
2539 v4l2_device_unregister(&dev->v4l2_dev);
2540 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03002541}
2542
2543static int coda_remove(struct platform_device *pdev)
2544{
2545 struct coda_dev *dev = platform_get_drvdata(pdev);
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002546 int i;
Javier Martin186b2502012-07-26 05:53:35 -03002547
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002548 for (i = 0; i < ARRAY_SIZE(dev->vfd); i++) {
2549 if (video_get_drvdata(&dev->vfd[i]))
2550 video_unregister_device(&dev->vfd[i]);
2551 }
Javier Martin186b2502012-07-26 05:53:35 -03002552 if (dev->m2m_dev)
2553 v4l2_m2m_release(dev->m2m_dev);
Philipp Zabel1e172732014-07-11 06:36:23 -03002554 pm_runtime_disable(&pdev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03002555 v4l2_device_unregister(&dev->v4l2_dev);
Philipp Zabel32b6f202014-07-11 06:36:20 -03002556 destroy_workqueue(dev->workqueue);
Philipp Zabelb313bcc2014-07-11 06:36:16 -03002557 if (dev->iram.vaddr)
2558 gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
2559 dev->iram.size);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002560 coda_free_aux_buf(dev, &dev->codebuf);
2561 coda_free_aux_buf(dev, &dev->tempbuf);
2562 coda_free_aux_buf(dev, &dev->workbuf);
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002563 debugfs_remove_recursive(dev->debugfs_root);
Javier Martin186b2502012-07-26 05:53:35 -03002564 return 0;
2565}
2566
Rafael J. Wysockie243c7c2014-12-04 01:10:10 +01002567#ifdef CONFIG_PM
Philipp Zabel1e172732014-07-11 06:36:23 -03002568static int coda_runtime_resume(struct device *dev)
2569{
2570 struct coda_dev *cdev = dev_get_drvdata(dev);
2571 int ret = 0;
2572
Philipp Zabel65919e62014-07-18 07:22:36 -03002573 if (dev->pm_domain && cdev->codebuf.vaddr) {
Philipp Zabel1e172732014-07-11 06:36:23 -03002574 ret = coda_hw_init(cdev);
2575 if (ret)
2576 v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
2577 }
2578
2579 return ret;
2580}
2581#endif
2582
2583static const struct dev_pm_ops coda_pm_ops = {
2584 SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
2585};
2586
Javier Martin186b2502012-07-26 05:53:35 -03002587static struct platform_driver coda_driver = {
2588 .probe = coda_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08002589 .remove = coda_remove,
Javier Martin186b2502012-07-26 05:53:35 -03002590 .driver = {
2591 .name = CODA_NAME,
Javier Martin186b2502012-07-26 05:53:35 -03002592 .of_match_table = of_match_ptr(coda_dt_ids),
Philipp Zabel1e172732014-07-11 06:36:23 -03002593 .pm = &coda_pm_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002594 },
2595 .id_table = coda_platform_ids,
2596};
2597
2598module_platform_driver(coda_driver);
2599
2600MODULE_LICENSE("GPL");
2601MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
2602MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");