blob: 340011f1a08e223c247e6f95d71d0464e3180692 [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 Zabela2b3e462014-07-23 12:28:39 -030074void coda_write(struct coda_dev *dev, u32 data, u32 reg)
Javier Martin186b2502012-07-26 05:53:35 -030075{
Philipp Zabeld60b18b2014-08-05 14:00:15 -030076 v4l2_dbg(2, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -030077 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
78 writel(data, dev->regs_base + reg);
79}
80
Philipp Zabela2b3e462014-07-23 12:28:39 -030081unsigned int coda_read(struct coda_dev *dev, u32 reg)
Javier Martin186b2502012-07-26 05:53:35 -030082{
83 u32 data;
Philipp Zabelf23797b2014-08-06 08:02:23 -030084
Javier Martin186b2502012-07-26 05:53:35 -030085 data = readl(dev->regs_base + reg);
Philipp Zabeld60b18b2014-08-05 14:00:15 -030086 v4l2_dbg(2, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -030087 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
88 return data;
89}
90
Philipp Zabel856d7d92014-09-29 09:53:44 -030091void coda_write_base(struct coda_ctx *ctx, struct coda_q_data *q_data,
Junghak Sung2d700712015-09-22 10:30:30 -030092 struct vb2_v4l2_buffer *buf, unsigned int reg_y)
Philipp Zabel856d7d92014-09-29 09:53:44 -030093{
Junghak Sung2d700712015-09-22 10:30:30 -030094 u32 base_y = vb2_dma_contig_plane_dma_addr(&buf->vb2_buf, 0);
Philipp Zabel856d7d92014-09-29 09:53:44 -030095 u32 base_cb, base_cr;
96
97 switch (q_data->fourcc) {
Michael Tretterd40e98c2017-01-20 12:00:25 -020098 case V4L2_PIX_FMT_YUYV:
99 /* Fallthrough: IN -H264-> CODA -NV12 MB-> VDOA -YUYV-> OUT */
Philipp Zabel6727d4f2015-07-16 13:19:39 -0300100 case V4L2_PIX_FMT_NV12:
101 case V4L2_PIX_FMT_YUV420:
102 default:
103 base_cb = base_y + q_data->bytesperline * q_data->height;
104 base_cr = base_cb + q_data->bytesperline * q_data->height / 4;
105 break;
Philipp Zabel856d7d92014-09-29 09:53:44 -0300106 case V4L2_PIX_FMT_YVU420:
107 /* Switch Cb and Cr for YVU420 format */
108 base_cr = base_y + q_data->bytesperline * q_data->height;
109 base_cb = base_cr + q_data->bytesperline * q_data->height / 4;
110 break;
Philipp Zabel4de69312014-10-02 14:08:26 -0300111 case V4L2_PIX_FMT_YUV422P:
112 base_cb = base_y + q_data->bytesperline * q_data->height;
113 base_cr = base_cb + q_data->bytesperline * q_data->height / 2;
Philipp Zabel856d7d92014-09-29 09:53:44 -0300114 }
115
116 coda_write(ctx->dev, base_y, reg_y);
117 coda_write(ctx->dev, base_cb, reg_y + 4);
118 coda_write(ctx->dev, base_cr, reg_y + 8);
119}
120
Philipp Zabelb96904e2013-05-23 10:42:58 -0300121#define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
122 { mode, src_fourcc, dst_fourcc, max_w, max_h }
123
124/*
125 * Arrays of codecs supported by each given version of Coda:
126 * i.MX27 -> codadx6
127 * i.MX5x -> coda7
128 * i.MX6 -> coda960
129 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
130 */
Philipp Zabel814c3762014-07-18 07:22:45 -0300131static const struct coda_codec codadx6_codecs[] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300132 CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
133 CODA_CODEC(CODADX6_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -0300134};
135
Philipp Zabel814c3762014-07-18 07:22:45 -0300136static const struct coda_codec coda7_codecs[] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300137 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720),
138 CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720),
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300139 CODA_CODEC(CODA7_MODE_ENCODE_MJPG, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_JPEG, 8192, 8192),
Philipp Zabelb0ed05b2014-08-05 14:00:14 -0300140 CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
Philipp Zabel95847f42015-12-02 14:58:54 -0200141 CODA_CODEC(CODA7_MODE_DECODE_MP2, V4L2_PIX_FMT_MPEG2, V4L2_PIX_FMT_YUV420, 1920, 1088),
Philipp Zabelb0ed05b2014-08-05 14:00:14 -0300142 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088),
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300143 CODA_CODEC(CODA7_MODE_DECODE_MJPG, V4L2_PIX_FMT_JPEG, V4L2_PIX_FMT_YUV420, 8192, 8192),
Philipp Zabelb96904e2013-05-23 10:42:58 -0300144};
145
Philipp Zabel814c3762014-07-18 07:22:45 -0300146static const struct coda_codec coda9_codecs[] = {
Philipp Zabelb0ed05b2014-08-05 14:00:14 -0300147 CODA_CODEC(CODA9_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1920, 1088),
148 CODA_CODEC(CODA9_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1920, 1088),
149 CODA_CODEC(CODA9_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
Philipp Zabel95847f42015-12-02 14:58:54 -0200150 CODA_CODEC(CODA9_MODE_DECODE_MP2, V4L2_PIX_FMT_MPEG2, V4L2_PIX_FMT_YUV420, 1920, 1088),
Philipp Zabelb0ed05b2014-08-05 14:00:14 -0300151 CODA_CODEC(CODA9_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088),
Philipp Zabel89548442014-07-11 06:36:17 -0300152};
153
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300154struct coda_video_device {
155 const char *name;
156 enum coda_inst_type type;
157 const struct coda_context_ops *ops;
Philipp Zabela22496c2015-01-23 13:51:33 -0300158 bool direct;
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300159 u32 src_formats[CODA_MAX_FORMATS];
160 u32 dst_formats[CODA_MAX_FORMATS];
161};
162
163static const struct coda_video_device coda_bit_encoder = {
164 .name = "coda-encoder",
165 .type = CODA_INST_ENCODER,
166 .ops = &coda_bit_encode_ops,
167 .src_formats = {
Philipp Zabel6727d4f2015-07-16 13:19:39 -0300168 V4L2_PIX_FMT_NV12,
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300169 V4L2_PIX_FMT_YUV420,
170 V4L2_PIX_FMT_YVU420,
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300171 },
172 .dst_formats = {
173 V4L2_PIX_FMT_H264,
174 V4L2_PIX_FMT_MPEG4,
175 },
176};
177
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300178static const struct coda_video_device coda_bit_jpeg_encoder = {
179 .name = "coda-jpeg-encoder",
180 .type = CODA_INST_ENCODER,
181 .ops = &coda_bit_encode_ops,
182 .src_formats = {
Philipp Zabel6727d4f2015-07-16 13:19:39 -0300183 V4L2_PIX_FMT_NV12,
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300184 V4L2_PIX_FMT_YUV420,
185 V4L2_PIX_FMT_YVU420,
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300186 V4L2_PIX_FMT_YUV422P,
187 },
188 .dst_formats = {
189 V4L2_PIX_FMT_JPEG,
190 },
191};
192
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300193static const struct coda_video_device coda_bit_decoder = {
194 .name = "coda-decoder",
195 .type = CODA_INST_DECODER,
196 .ops = &coda_bit_decode_ops,
197 .src_formats = {
198 V4L2_PIX_FMT_H264,
Philipp Zabel95847f42015-12-02 14:58:54 -0200199 V4L2_PIX_FMT_MPEG2,
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300200 V4L2_PIX_FMT_MPEG4,
201 },
202 .dst_formats = {
Philipp Zabel6727d4f2015-07-16 13:19:39 -0300203 V4L2_PIX_FMT_NV12,
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300204 V4L2_PIX_FMT_YUV420,
205 V4L2_PIX_FMT_YVU420,
Michael Tretterd40e98c2017-01-20 12:00:25 -0200206 /*
207 * If V4L2_PIX_FMT_YUYV should be default,
208 * set_default_params() must be adjusted.
209 */
210 V4L2_PIX_FMT_YUYV,
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300211 },
212};
213
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300214static const struct coda_video_device coda_bit_jpeg_decoder = {
215 .name = "coda-jpeg-decoder",
216 .type = CODA_INST_DECODER,
217 .ops = &coda_bit_decode_ops,
218 .src_formats = {
219 V4L2_PIX_FMT_JPEG,
220 },
221 .dst_formats = {
Philipp Zabel6727d4f2015-07-16 13:19:39 -0300222 V4L2_PIX_FMT_NV12,
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300223 V4L2_PIX_FMT_YUV420,
224 V4L2_PIX_FMT_YVU420,
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300225 V4L2_PIX_FMT_YUV422P,
226 },
227};
228
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300229static const struct coda_video_device *codadx6_video_devices[] = {
230 &coda_bit_encoder,
231};
232
233static const struct coda_video_device *coda7_video_devices[] = {
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300234 &coda_bit_jpeg_encoder,
235 &coda_bit_jpeg_decoder,
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300236 &coda_bit_encoder,
237 &coda_bit_decoder,
238};
239
240static const struct coda_video_device *coda9_video_devices[] = {
241 &coda_bit_encoder,
242 &coda_bit_decoder,
243};
244
Philipp Zabelb96904e2013-05-23 10:42:58 -0300245/*
246 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
247 * tables.
248 */
249static u32 coda_format_normalize_yuv(u32 fourcc)
250{
Philipp Zabel8076c7e2015-07-09 07:10:18 -0300251 switch (fourcc) {
Philipp Zabel6727d4f2015-07-16 13:19:39 -0300252 case V4L2_PIX_FMT_NV12:
Philipp Zabel8076c7e2015-07-09 07:10:18 -0300253 case V4L2_PIX_FMT_YUV420:
254 case V4L2_PIX_FMT_YVU420:
Philipp Zabel8076c7e2015-07-09 07:10:18 -0300255 case V4L2_PIX_FMT_YUV422P:
Michael Tretterd40e98c2017-01-20 12:00:25 -0200256 case V4L2_PIX_FMT_YUYV:
Philipp Zabel8076c7e2015-07-09 07:10:18 -0300257 return V4L2_PIX_FMT_YUV420;
258 default:
259 return fourcc;
260 }
Philipp Zabelb96904e2013-05-23 10:42:58 -0300261}
262
Philipp Zabel814c3762014-07-18 07:22:45 -0300263static const struct coda_codec *coda_find_codec(struct coda_dev *dev,
264 int src_fourcc, int dst_fourcc)
Philipp Zabelb96904e2013-05-23 10:42:58 -0300265{
Philipp Zabel814c3762014-07-18 07:22:45 -0300266 const struct coda_codec *codecs = dev->devtype->codecs;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300267 int num_codecs = dev->devtype->num_codecs;
268 int k;
269
270 src_fourcc = coda_format_normalize_yuv(src_fourcc);
271 dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
272 if (src_fourcc == dst_fourcc)
273 return NULL;
274
275 for (k = 0; k < num_codecs; k++) {
276 if (codecs[k].src_fourcc == src_fourcc &&
277 codecs[k].dst_fourcc == dst_fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300278 break;
279 }
280
Philipp Zabelb96904e2013-05-23 10:42:58 -0300281 if (k == num_codecs)
Javier Martin186b2502012-07-26 05:53:35 -0300282 return NULL;
283
Philipp Zabelb96904e2013-05-23 10:42:58 -0300284 return &codecs[k];
Javier Martin186b2502012-07-26 05:53:35 -0300285}
286
Philipp Zabel57625592013-09-30 10:34:51 -0300287static void coda_get_max_dimensions(struct coda_dev *dev,
Philipp Zabel814c3762014-07-18 07:22:45 -0300288 const struct coda_codec *codec,
Philipp Zabel57625592013-09-30 10:34:51 -0300289 int *max_w, int *max_h)
290{
Philipp Zabel814c3762014-07-18 07:22:45 -0300291 const struct coda_codec *codecs = dev->devtype->codecs;
Philipp Zabel57625592013-09-30 10:34:51 -0300292 int num_codecs = dev->devtype->num_codecs;
293 unsigned int w, h;
294 int k;
295
296 if (codec) {
297 w = codec->max_w;
298 h = codec->max_h;
299 } else {
300 for (k = 0, w = 0, h = 0; k < num_codecs; k++) {
301 w = max(w, codecs[k].max_w);
302 h = max(h, codecs[k].max_h);
303 }
304 }
305
306 if (max_w)
307 *max_w = w;
308 if (max_h)
309 *max_h = h;
310}
311
Philipp Zabel55425702015-12-02 14:58:50 -0200312static const struct coda_video_device *to_coda_video_device(struct video_device
313 *vdev)
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300314{
315 struct coda_dev *dev = video_get_drvdata(vdev);
316 unsigned int i = vdev - dev->vfd;
317
318 if (i >= dev->devtype->num_vdevs)
319 return NULL;
320
321 return dev->devtype->vdevs[i];
322}
323
Philipp Zabel79924ca2014-07-23 12:28:45 -0300324const char *coda_product_name(int product)
Philipp Zabel927933f2013-09-30 10:34:48 -0300325{
326 static char buf[9];
327
328 switch (product) {
329 case CODA_DX6:
330 return "CodaDx6";
331 case CODA_7541:
332 return "CODA7541";
Philipp Zabel89548442014-07-11 06:36:17 -0300333 case CODA_960:
334 return "CODA960";
Philipp Zabel927933f2013-09-30 10:34:48 -0300335 default:
336 snprintf(buf, sizeof(buf), "(0x%04x)", product);
337 return buf;
338 }
339}
340
Michael Trettere7f3c542017-01-20 12:00:24 -0200341static struct vdoa_data *coda_get_vdoa_data(void)
342{
343 struct device_node *vdoa_node;
344 struct platform_device *vdoa_pdev;
345 struct vdoa_data *vdoa_data = NULL;
346
347 vdoa_node = of_find_compatible_node(NULL, NULL, "fsl,imx6q-vdoa");
348 if (!vdoa_node)
349 return NULL;
350
351 vdoa_pdev = of_find_device_by_node(vdoa_node);
352 if (!vdoa_pdev)
353 goto out;
354
355 vdoa_data = platform_get_drvdata(vdoa_pdev);
356 if (!vdoa_data)
357 vdoa_data = ERR_PTR(-EPROBE_DEFER);
358
359out:
360 if (vdoa_node)
361 of_node_put(vdoa_node);
362
363 return vdoa_data;
364}
365
Javier Martin186b2502012-07-26 05:53:35 -0300366/*
367 * V4L2 ioctl() operations.
368 */
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300369static int coda_querycap(struct file *file, void *priv,
370 struct v4l2_capability *cap)
Javier Martin186b2502012-07-26 05:53:35 -0300371{
Philipp Zabel927933f2013-09-30 10:34:48 -0300372 struct coda_ctx *ctx = fh_to_ctx(priv);
373
Javier Martin186b2502012-07-26 05:53:35 -0300374 strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver));
Philipp Zabel927933f2013-09-30 10:34:48 -0300375 strlcpy(cap->card, coda_product_name(ctx->dev->devtype->product),
376 sizeof(cap->card));
Philipp Zabeldad0e1c2013-05-21 04:18:22 -0300377 strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
Philipp Zabel3898e7a2014-07-18 07:22:37 -0300378 cap->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
Javier Martin186b2502012-07-26 05:53:35 -0300379 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
380
381 return 0;
382}
383
Philipp Zabel22e244b2014-07-18 07:22:43 -0300384static int coda_enum_fmt(struct file *file, void *priv,
385 struct v4l2_fmtdesc *f)
Javier Martin186b2502012-07-26 05:53:35 -0300386{
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300387 struct video_device *vdev = video_devdata(file);
388 const struct coda_video_device *cvd = to_coda_video_device(vdev);
389 const u32 *formats;
Philipp Zabel22e244b2014-07-18 07:22:43 -0300390
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300391 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
392 formats = cvd->src_formats;
393 else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
394 formats = cvd->dst_formats;
Philipp Zabel22e244b2014-07-18 07:22:43 -0300395 else
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300396 return -EINVAL;
Javier Martin186b2502012-07-26 05:53:35 -0300397
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300398 if (f->index >= CODA_MAX_FORMATS || formats[f->index] == 0)
399 return -EINVAL;
Javier Martin186b2502012-07-26 05:53:35 -0300400
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300401 f->pixelformat = formats[f->index];
Javier Martin186b2502012-07-26 05:53:35 -0300402
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300403 return 0;
Javier Martin186b2502012-07-26 05:53:35 -0300404}
405
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300406static int coda_g_fmt(struct file *file, void *priv,
407 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300408{
Javier Martin186b2502012-07-26 05:53:35 -0300409 struct coda_q_data *q_data;
410 struct coda_ctx *ctx = fh_to_ctx(priv);
411
Javier Martin186b2502012-07-26 05:53:35 -0300412 q_data = get_q_data(ctx, f->type);
Philipp Zabelb9736292014-07-11 06:36:18 -0300413 if (!q_data)
414 return -EINVAL;
Javier Martin186b2502012-07-26 05:53:35 -0300415
416 f->fmt.pix.field = V4L2_FIELD_NONE;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300417 f->fmt.pix.pixelformat = q_data->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -0300418 f->fmt.pix.width = q_data->width;
419 f->fmt.pix.height = q_data->height;
Philipp Zabel2fd6a372014-07-11 06:36:36 -0300420 f->fmt.pix.bytesperline = q_data->bytesperline;
Javier Martin186b2502012-07-26 05:53:35 -0300421
422 f->fmt.pix.sizeimage = q_data->sizeimage;
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300423 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_JPEG)
424 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
425 else
426 f->fmt.pix.colorspace = ctx->colorspace;
Javier Martin186b2502012-07-26 05:53:35 -0300427
428 return 0;
429}
430
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300431static int coda_try_pixelformat(struct coda_ctx *ctx, struct v4l2_format *f)
432{
433 struct coda_q_data *q_data;
434 const u32 *formats;
435 int i;
436
437 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
438 formats = ctx->cvd->src_formats;
439 else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
440 formats = ctx->cvd->dst_formats;
441 else
442 return -EINVAL;
443
444 for (i = 0; i < CODA_MAX_FORMATS; i++) {
Michael Tretterd40e98c2017-01-20 12:00:25 -0200445 /* Skip YUYV if the vdoa is not available */
446 if (!ctx->vdoa && f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
447 formats[i] == V4L2_PIX_FMT_YUYV)
448 continue;
449
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300450 if (formats[i] == f->fmt.pix.pixelformat) {
451 f->fmt.pix.pixelformat = formats[i];
452 return 0;
453 }
454 }
455
456 /* Fall back to currently set pixelformat */
457 q_data = get_q_data(ctx, f->type);
458 f->fmt.pix.pixelformat = q_data->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -0300459
460 return 0;
461}
462
Michael Trettere7f3c542017-01-20 12:00:24 -0200463static int coda_try_fmt_vdoa(struct coda_ctx *ctx, struct v4l2_format *f,
464 bool *use_vdoa)
465{
466 int err;
467
468 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
469 return -EINVAL;
470
471 if (!use_vdoa)
472 return -EINVAL;
473
474 if (!ctx->vdoa) {
475 *use_vdoa = false;
476 return 0;
477 }
478
479 err = vdoa_context_configure(NULL, f->fmt.pix.width, f->fmt.pix.height,
480 f->fmt.pix.pixelformat);
481 if (err) {
482 *use_vdoa = false;
483 return 0;
484 }
485
486 *use_vdoa = true;
487 return 0;
488}
489
Philipp Zabel8e428d52015-01-23 13:51:29 -0300490static unsigned int coda_estimate_sizeimage(struct coda_ctx *ctx, u32 sizeimage,
491 u32 width, u32 height)
492{
493 /*
494 * This is a rough estimate for sensible compressed buffer
495 * sizes (between 1 and 16 bits per pixel). This could be
496 * improved by better format specific worst case estimates.
497 */
498 return round_up(clamp(sizeimage, width * height / 8,
499 width * height * 2), PAGE_SIZE);
500}
501
Philipp Zabel814c3762014-07-18 07:22:45 -0300502static int coda_try_fmt(struct coda_ctx *ctx, const struct coda_codec *codec,
Philipp Zabel57625592013-09-30 10:34:51 -0300503 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300504{
Philipp Zabel57625592013-09-30 10:34:51 -0300505 struct coda_dev *dev = ctx->dev;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300506 unsigned int max_w, max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300507 enum v4l2_field field;
508
509 field = f->fmt.pix.field;
510 if (field == V4L2_FIELD_ANY)
511 field = V4L2_FIELD_NONE;
512 else if (V4L2_FIELD_NONE != field)
513 return -EINVAL;
514
515 /* V4L2 specification suggests the driver corrects the format struct
516 * if any of the dimensions is unsupported */
517 f->fmt.pix.field = field;
518
Philipp Zabel57625592013-09-30 10:34:51 -0300519 coda_get_max_dimensions(dev, codec, &max_w, &max_h);
520 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN,
521 &f->fmt.pix.height, MIN_H, max_h, H_ALIGN,
522 S_ALIGN);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300523
Philipp Zabel57625592013-09-30 10:34:51 -0300524 switch (f->fmt.pix.pixelformat) {
Philipp Zabel6727d4f2015-07-16 13:19:39 -0300525 case V4L2_PIX_FMT_NV12:
Philipp Zabel57625592013-09-30 10:34:51 -0300526 case V4L2_PIX_FMT_YUV420:
527 case V4L2_PIX_FMT_YVU420:
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300528 /*
529 * Frame stride must be at least multiple of 8,
530 * but multiple of 16 for h.264 or JPEG 4:2:x
531 */
Philipp Zabel451dfe52014-07-11 06:36:39 -0300532 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
Philipp Zabel47cf0c62013-05-23 10:42:54 -0300533 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
Philipp Zabel451d43a2012-07-26 09:18:27 -0300534 f->fmt.pix.height * 3 / 2;
Philipp Zabel57625592013-09-30 10:34:51 -0300535 break;
Michael Tretterd40e98c2017-01-20 12:00:25 -0200536 case V4L2_PIX_FMT_YUYV:
537 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16) * 2;
538 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
539 f->fmt.pix.height;
540 break;
Philipp Zabel4de69312014-10-02 14:08:26 -0300541 case V4L2_PIX_FMT_YUV422P:
542 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
543 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
544 f->fmt.pix.height * 2;
545 break;
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300546 case V4L2_PIX_FMT_JPEG:
547 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
548 /* fallthrough */
Philipp Zabel57625592013-09-30 10:34:51 -0300549 case V4L2_PIX_FMT_H264:
550 case V4L2_PIX_FMT_MPEG4:
Philipp Zabel95847f42015-12-02 14:58:54 -0200551 case V4L2_PIX_FMT_MPEG2:
Javier Martin186b2502012-07-26 05:53:35 -0300552 f->fmt.pix.bytesperline = 0;
Philipp Zabel8e428d52015-01-23 13:51:29 -0300553 f->fmt.pix.sizeimage = coda_estimate_sizeimage(ctx,
554 f->fmt.pix.sizeimage,
555 f->fmt.pix.width,
556 f->fmt.pix.height);
Philipp Zabel57625592013-09-30 10:34:51 -0300557 break;
558 default:
559 BUG();
Javier Martin186b2502012-07-26 05:53:35 -0300560 }
561
562 return 0;
563}
564
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300565static int coda_try_fmt_vid_cap(struct file *file, void *priv,
566 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300567{
Javier Martin186b2502012-07-26 05:53:35 -0300568 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300569 const struct coda_q_data *q_data_src;
570 const struct coda_codec *codec;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300571 struct vb2_queue *src_vq;
572 int ret;
Michael Trettere7f3c542017-01-20 12:00:24 -0200573 bool use_vdoa;
Javier Martin186b2502012-07-26 05:53:35 -0300574
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300575 ret = coda_try_pixelformat(ctx, f);
576 if (ret < 0)
577 return ret;
578
579 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
580
Philipp Zabel918c66f2013-06-27 06:59:01 -0300581 /*
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300582 * If the source format is already fixed, only allow the same output
583 * resolution
Philipp Zabel918c66f2013-06-27 06:59:01 -0300584 */
Philipp Zabel14604e32014-07-11 06:36:22 -0300585 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300586 if (vb2_is_streaming(src_vq)) {
Philipp Zabel91b5841e2014-07-18 07:22:41 -0300587 f->fmt.pix.width = q_data_src->width;
588 f->fmt.pix.height = q_data_src->height;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300589 }
Javier Martin186b2502012-07-26 05:53:35 -0300590
591 f->fmt.pix.colorspace = ctx->colorspace;
592
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300593 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
594 codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
595 f->fmt.pix.pixelformat);
596 if (!codec)
597 return -EINVAL;
598
Philipp Zabel57625592013-09-30 10:34:51 -0300599 ret = coda_try_fmt(ctx, codec, f);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300600 if (ret < 0)
601 return ret;
602
603 /* The h.264 decoder only returns complete 16x16 macroblocks */
604 if (codec && codec->src_fourcc == V4L2_PIX_FMT_H264) {
Philipp Zabel1b0ed7b2014-07-11 06:36:37 -0300605 f->fmt.pix.width = f->fmt.pix.width;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300606 f->fmt.pix.height = round_up(f->fmt.pix.height, 16);
Philipp Zabel1b0ed7b2014-07-11 06:36:37 -0300607 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300608 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
609 f->fmt.pix.height * 3 / 2;
Michael Trettere7f3c542017-01-20 12:00:24 -0200610
611 ret = coda_try_fmt_vdoa(ctx, f, &use_vdoa);
612 if (ret < 0)
613 return ret;
Michael Tretterd40e98c2017-01-20 12:00:25 -0200614
615 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV) {
616 if (!use_vdoa)
617 return -EINVAL;
618
619 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16) * 2;
620 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
621 f->fmt.pix.height;
622 }
Philipp Zabel918c66f2013-06-27 06:59:01 -0300623 }
624
625 return 0;
Javier Martin186b2502012-07-26 05:53:35 -0300626}
627
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300628static int coda_try_fmt_vid_out(struct file *file, void *priv,
629 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300630{
631 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300632 struct coda_dev *dev = ctx->dev;
633 const struct coda_q_data *q_data_dst;
634 const struct coda_codec *codec;
635 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300636
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300637 ret = coda_try_pixelformat(ctx, f);
638 if (ret < 0)
639 return ret;
Javier Martin186b2502012-07-26 05:53:35 -0300640
Philipp Zabel5f9826e2015-01-23 13:51:21 -0300641 switch (f->fmt.pix.colorspace) {
642 case V4L2_COLORSPACE_REC709:
643 case V4L2_COLORSPACE_JPEG:
644 break;
645 default:
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300646 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_JPEG)
647 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
648 else
649 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
Javier Martin186b2502012-07-26 05:53:35 -0300650 }
651
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300652 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
653 codec = coda_find_codec(dev, f->fmt.pix.pixelformat, q_data_dst->fourcc);
Javier Martin186b2502012-07-26 05:53:35 -0300654
655 return coda_try_fmt(ctx, codec, f);
656}
657
Philipp Zabel4ec319e2017-01-20 12:00:21 -0200658static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f,
659 struct v4l2_rect *r)
Javier Martin186b2502012-07-26 05:53:35 -0300660{
661 struct coda_q_data *q_data;
662 struct vb2_queue *vq;
Javier Martin186b2502012-07-26 05:53:35 -0300663
Philipp Zabel14604e32014-07-11 06:36:22 -0300664 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
Javier Martin186b2502012-07-26 05:53:35 -0300665 if (!vq)
666 return -EINVAL;
667
668 q_data = get_q_data(ctx, f->type);
669 if (!q_data)
670 return -EINVAL;
671
672 if (vb2_is_busy(vq)) {
673 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
674 return -EBUSY;
675 }
676
Philipp Zabelb96904e2013-05-23 10:42:58 -0300677 q_data->fourcc = f->fmt.pix.pixelformat;
Javier Martin186b2502012-07-26 05:53:35 -0300678 q_data->width = f->fmt.pix.width;
679 q_data->height = f->fmt.pix.height;
Philipp Zabel2fd6a372014-07-11 06:36:36 -0300680 q_data->bytesperline = f->fmt.pix.bytesperline;
Philipp Zabel451d43a2012-07-26 09:18:27 -0300681 q_data->sizeimage = f->fmt.pix.sizeimage;
Philipp Zabel4ec319e2017-01-20 12:00:21 -0200682 if (r) {
683 q_data->rect = *r;
684 } else {
685 q_data->rect.left = 0;
686 q_data->rect.top = 0;
687 q_data->rect.width = f->fmt.pix.width;
688 q_data->rect.height = f->fmt.pix.height;
689 }
Javier Martin186b2502012-07-26 05:53:35 -0300690
Philipp Zabela269e532015-07-16 13:19:38 -0300691 switch (f->fmt.pix.pixelformat) {
Michael Tretterd40e98c2017-01-20 12:00:25 -0200692 case V4L2_PIX_FMT_YUYV:
693 ctx->tiled_map_type = GDI_TILED_FRAME_MB_RASTER_MAP;
694 break;
Philipp Zabela269e532015-07-16 13:19:38 -0300695 case V4L2_PIX_FMT_NV12:
Michael Trettere7f3c542017-01-20 12:00:24 -0200696 ctx->tiled_map_type = GDI_TILED_FRAME_MB_RASTER_MAP;
697 if (!disable_tiling)
698 break;
Philipp Zabela269e532015-07-16 13:19:38 -0300699 /* else fall through */
700 case V4L2_PIX_FMT_YUV420:
701 case V4L2_PIX_FMT_YVU420:
702 ctx->tiled_map_type = GDI_LINEAR_FRAME_MAP;
703 break;
704 default:
705 break;
706 }
707
Michael Trettere7f3c542017-01-20 12:00:24 -0200708 if (ctx->tiled_map_type == GDI_TILED_FRAME_MB_RASTER_MAP &&
709 !coda_try_fmt_vdoa(ctx, f, &ctx->use_vdoa) &&
710 ctx->use_vdoa)
711 vdoa_context_configure(ctx->vdoa, f->fmt.pix.width,
712 f->fmt.pix.height,
713 f->fmt.pix.pixelformat);
714 else
715 ctx->use_vdoa = false;
716
Javier Martin186b2502012-07-26 05:53:35 -0300717 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
Philipp Zabeld677b642017-01-20 12:00:22 -0200718 "Setting format for type %d, wxh: %dx%d, fmt: %4.4s %c\n",
719 f->type, q_data->width, q_data->height,
720 (char *)&q_data->fourcc,
721 (ctx->tiled_map_type == GDI_LINEAR_FRAME_MAP) ? 'L' : 'T');
Javier Martin186b2502012-07-26 05:53:35 -0300722
723 return 0;
724}
725
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300726static int coda_s_fmt_vid_cap(struct file *file, void *priv,
727 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300728{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300729 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel4ec319e2017-01-20 12:00:21 -0200730 struct coda_q_data *q_data_src;
731 struct v4l2_rect r;
Javier Martin186b2502012-07-26 05:53:35 -0300732 int ret;
733
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300734 ret = coda_try_fmt_vid_cap(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300735 if (ret)
736 return ret;
737
Philipp Zabel4ec319e2017-01-20 12:00:21 -0200738 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
739 r.left = 0;
740 r.top = 0;
741 r.width = q_data_src->width;
742 r.height = q_data_src->height;
743
744 return coda_s_fmt(ctx, f, &r);
Javier Martin186b2502012-07-26 05:53:35 -0300745}
746
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300747static int coda_s_fmt_vid_out(struct file *file, void *priv,
748 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300749{
750 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel4ec319e2017-01-20 12:00:21 -0200751 struct coda_q_data *q_data_src;
Philipp Zabelf95a6ce2014-08-05 14:00:19 -0300752 struct v4l2_format f_cap;
Philipp Zabel4ec319e2017-01-20 12:00:21 -0200753 struct v4l2_rect r;
Javier Martin186b2502012-07-26 05:53:35 -0300754 int ret;
755
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300756 ret = coda_try_fmt_vid_out(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300757 if (ret)
758 return ret;
759
Philipp Zabel4ec319e2017-01-20 12:00:21 -0200760 ret = coda_s_fmt(ctx, f, NULL);
Javier Martin186b2502012-07-26 05:53:35 -0300761 if (ret)
Philipp Zabel2dc546d2014-08-05 14:00:18 -0300762 return ret;
763
764 ctx->colorspace = f->fmt.pix.colorspace;
Javier Martin186b2502012-07-26 05:53:35 -0300765
Philipp Zabel1c2f6a92015-01-23 13:51:22 -0300766 memset(&f_cap, 0, sizeof(f_cap));
Philipp Zabelf95a6ce2014-08-05 14:00:19 -0300767 f_cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
768 coda_g_fmt(file, priv, &f_cap);
769 f_cap.fmt.pix.width = f->fmt.pix.width;
770 f_cap.fmt.pix.height = f->fmt.pix.height;
771
772 ret = coda_try_fmt_vid_cap(file, priv, &f_cap);
773 if (ret)
774 return ret;
775
Philipp Zabel4ec319e2017-01-20 12:00:21 -0200776 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
777 r.left = 0;
778 r.top = 0;
779 r.width = q_data_src->width;
780 r.height = q_data_src->height;
781
782 return coda_s_fmt(ctx, &f_cap, &r);
Javier Martin186b2502012-07-26 05:53:35 -0300783}
784
Philipp Zabel73751da2015-03-24 14:30:51 -0300785static int coda_reqbufs(struct file *file, void *priv,
786 struct v4l2_requestbuffers *rb)
787{
788 struct coda_ctx *ctx = fh_to_ctx(priv);
789 int ret;
790
791 ret = v4l2_m2m_reqbufs(file, ctx->fh.m2m_ctx, rb);
792 if (ret)
793 return ret;
794
795 /*
796 * Allow to allocate instance specific per-context buffers, such as
797 * bitstream ringbuffer, slice buffer, work buffer, etc. if needed.
798 */
799 if (rb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT && ctx->ops->reqbufs)
800 return ctx->ops->reqbufs(ctx, rb);
801
802 return 0;
803}
804
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300805static int coda_qbuf(struct file *file, void *priv,
806 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300807{
808 struct coda_ctx *ctx = fh_to_ctx(priv);
809
Philipp Zabel14604e32014-07-11 06:36:22 -0300810 return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
Javier Martin186b2502012-07-26 05:53:35 -0300811}
812
Philipp Zabel918c66f2013-06-27 06:59:01 -0300813static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx,
Junghak Sung2d700712015-09-22 10:30:30 -0300814 struct vb2_v4l2_buffer *buf)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300815{
816 struct vb2_queue *src_vq;
817
Philipp Zabel14604e32014-07-11 06:36:22 -0300818 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300819
820 return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) &&
Junghak Sung2d700712015-09-22 10:30:30 -0300821 (buf->sequence == (ctx->qsequence - 1)));
Philipp Zabel918c66f2013-06-27 06:59:01 -0300822}
823
Junghak Sung2d700712015-09-22 10:30:30 -0300824void coda_m2m_buf_done(struct coda_ctx *ctx, struct vb2_v4l2_buffer *buf,
Philipp Zabel9f2bfb32015-05-04 07:51:07 -0300825 enum vb2_buffer_state state)
Javier Martin186b2502012-07-26 05:53:35 -0300826{
Philipp Zabel9f2bfb32015-05-04 07:51:07 -0300827 const struct v4l2_event eos_event = {
828 .type = V4L2_EVENT_EOS
829 };
Javier Martin186b2502012-07-26 05:53:35 -0300830
Philipp Zabel9f2bfb32015-05-04 07:51:07 -0300831 if (coda_buf_is_end_of_stream(ctx, buf)) {
Junghak Sung2d700712015-09-22 10:30:30 -0300832 buf->flags |= V4L2_BUF_FLAG_LAST;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300833
834 v4l2_event_queue_fh(&ctx->fh, &eos_event);
835 }
836
Philipp Zabel9f2bfb32015-05-04 07:51:07 -0300837 v4l2_m2m_buf_done(buf, state);
Javier Martin186b2502012-07-26 05:53:35 -0300838}
839
Philipp Zabel52c41672014-07-11 06:36:19 -0300840static int coda_g_selection(struct file *file, void *fh,
841 struct v4l2_selection *s)
842{
843 struct coda_ctx *ctx = fh_to_ctx(fh);
844 struct coda_q_data *q_data;
845 struct v4l2_rect r, *rsel;
846
847 q_data = get_q_data(ctx, s->type);
848 if (!q_data)
849 return -EINVAL;
850
851 r.left = 0;
852 r.top = 0;
853 r.width = q_data->width;
854 r.height = q_data->height;
855 rsel = &q_data->rect;
856
857 switch (s->target) {
858 case V4L2_SEL_TGT_CROP_DEFAULT:
859 case V4L2_SEL_TGT_CROP_BOUNDS:
860 rsel = &r;
861 /* fallthrough */
862 case V4L2_SEL_TGT_CROP:
863 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
864 return -EINVAL;
865 break;
866 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
867 case V4L2_SEL_TGT_COMPOSE_PADDED:
868 rsel = &r;
869 /* fallthrough */
870 case V4L2_SEL_TGT_COMPOSE:
871 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
872 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
873 return -EINVAL;
874 break;
875 default:
876 return -EINVAL;
877 }
878
879 s->r = *rsel;
880
881 return 0;
882}
883
Philipp Zabel59195ce2017-03-02 06:51:44 -0300884static int coda_try_encoder_cmd(struct file *file, void *fh,
885 struct v4l2_encoder_cmd *ec)
886{
887 if (ec->cmd != V4L2_ENC_CMD_STOP)
888 return -EINVAL;
889
890 if (ec->flags & V4L2_ENC_CMD_STOP_AT_GOP_END)
891 return -EINVAL;
892
893 return 0;
894}
895
896static int coda_encoder_cmd(struct file *file, void *fh,
897 struct v4l2_encoder_cmd *ec)
898{
899 struct coda_ctx *ctx = fh_to_ctx(fh);
900 struct vb2_queue *dst_vq;
901 int ret;
902
903 ret = coda_try_encoder_cmd(file, fh, ec);
904 if (ret < 0)
905 return ret;
906
907 /* Ignore encoder stop command silently in decoder context */
908 if (ctx->inst_type != CODA_INST_ENCODER)
909 return 0;
910
911 /* Set the stream-end flag on this context */
912 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
913
914 /* If there is no buffer in flight, wake up */
915 if (ctx->qsequence == ctx->osequence) {
916 dst_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
917 V4L2_BUF_TYPE_VIDEO_CAPTURE);
918 dst_vq->last_buffer_dequeued = true;
919 wake_up(&dst_vq->done_wq);
920 }
921
922 return 0;
923}
924
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300925static int coda_try_decoder_cmd(struct file *file, void *fh,
926 struct v4l2_decoder_cmd *dc)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300927{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300928 if (dc->cmd != V4L2_DEC_CMD_STOP)
929 return -EINVAL;
930
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300931 if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300932 return -EINVAL;
933
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300934 if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0))
Philipp Zabel918c66f2013-06-27 06:59:01 -0300935 return -EINVAL;
936
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300937 return 0;
938}
939
940static int coda_decoder_cmd(struct file *file, void *fh,
941 struct v4l2_decoder_cmd *dc)
942{
943 struct coda_ctx *ctx = fh_to_ctx(fh);
944 int ret;
945
946 ret = coda_try_decoder_cmd(file, fh, dc);
947 if (ret < 0)
948 return ret;
949
950 /* Ignore decoder stop command silently in encoder context */
Philipp Zabel918c66f2013-06-27 06:59:01 -0300951 if (ctx->inst_type != CODA_INST_DECODER)
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300952 return 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300953
Philipp Zabel347bb7f2014-07-23 12:28:42 -0300954 /* Set the stream-end flag on this context */
955 coda_bit_stream_end_flag(ctx);
Philipp Zabel84e23652014-07-11 06:36:34 -0300956 ctx->hold = false;
Michael Olbrichf3497da2014-07-11 06:36:30 -0300957 v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
Philipp Zabel89548442014-07-11 06:36:17 -0300958
Philipp Zabel918c66f2013-06-27 06:59:01 -0300959 return 0;
960}
961
Philipp Zabelcde29ef2015-07-16 13:13:24 -0300962static int coda_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
963{
964 struct coda_ctx *ctx = fh_to_ctx(fh);
965 struct v4l2_fract *tpf;
966
967 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
968 return -EINVAL;
969
970 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
971 tpf = &a->parm.output.timeperframe;
972 tpf->denominator = ctx->params.framerate & CODA_FRATE_RES_MASK;
973 tpf->numerator = 1 + (ctx->params.framerate >>
974 CODA_FRATE_DIV_OFFSET);
975
976 return 0;
977}
978
979/*
980 * Approximate timeperframe v4l2_fract with values that can be written
981 * into the 16-bit CODA_FRATE_DIV and CODA_FRATE_RES fields.
982 */
983static void coda_approximate_timeperframe(struct v4l2_fract *timeperframe)
984{
985 struct v4l2_fract s = *timeperframe;
986 struct v4l2_fract f0;
987 struct v4l2_fract f1 = { 1, 0 };
988 struct v4l2_fract f2 = { 0, 1 };
989 unsigned int i, div, s_denominator;
990
991 /* Lower bound is 1/65535 */
992 if (s.numerator == 0 || s.denominator / s.numerator > 65535) {
993 timeperframe->numerator = 1;
994 timeperframe->denominator = 65535;
995 return;
996 }
997
998 /* Upper bound is 65536/1, map everything above to infinity */
999 if (s.denominator == 0 || s.numerator / s.denominator > 65536) {
1000 timeperframe->numerator = 1;
1001 timeperframe->denominator = 0;
1002 return;
1003 }
1004
1005 /* Reduce fraction to lowest terms */
1006 div = gcd(s.numerator, s.denominator);
1007 if (div > 1) {
1008 s.numerator /= div;
1009 s.denominator /= div;
1010 }
1011
1012 if (s.numerator <= 65536 && s.denominator < 65536) {
1013 *timeperframe = s;
1014 return;
1015 }
1016
1017 /* Find successive convergents from continued fraction expansion */
1018 while (f2.numerator <= 65536 && f2.denominator < 65536) {
1019 f0 = f1;
1020 f1 = f2;
1021
1022 /* Stop when f2 exactly equals timeperframe */
1023 if (s.numerator == 0)
1024 break;
1025
1026 i = s.denominator / s.numerator;
1027
1028 f2.numerator = f0.numerator + i * f1.numerator;
1029 f2.denominator = f0.denominator + i * f2.denominator;
1030
1031 s_denominator = s.numerator;
1032 s.numerator = s.denominator % s.numerator;
1033 s.denominator = s_denominator;
1034 }
1035
1036 *timeperframe = f1;
1037}
1038
1039static uint32_t coda_timeperframe_to_frate(struct v4l2_fract *timeperframe)
1040{
1041 return ((timeperframe->numerator - 1) << CODA_FRATE_DIV_OFFSET) |
1042 timeperframe->denominator;
1043}
1044
1045static int coda_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
1046{
1047 struct coda_ctx *ctx = fh_to_ctx(fh);
1048 struct v4l2_fract *tpf;
1049
1050 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1051 return -EINVAL;
1052
1053 tpf = &a->parm.output.timeperframe;
1054 coda_approximate_timeperframe(tpf);
1055 ctx->params.framerate = coda_timeperframe_to_frate(tpf);
1056
1057 return 0;
1058}
1059
Philipp Zabel2e9e4f12013-09-30 10:34:50 -03001060static int coda_subscribe_event(struct v4l2_fh *fh,
1061 const struct v4l2_event_subscription *sub)
Philipp Zabel918c66f2013-06-27 06:59:01 -03001062{
1063 switch (sub->type) {
1064 case V4L2_EVENT_EOS:
1065 return v4l2_event_subscribe(fh, sub, 0, NULL);
1066 default:
1067 return v4l2_ctrl_subscribe_event(fh, sub);
1068 }
Javier Martin186b2502012-07-26 05:53:35 -03001069}
1070
1071static const struct v4l2_ioctl_ops coda_ioctl_ops = {
Philipp Zabel2e9e4f12013-09-30 10:34:50 -03001072 .vidioc_querycap = coda_querycap,
Javier Martin186b2502012-07-26 05:53:35 -03001073
Philipp Zabel22e244b2014-07-18 07:22:43 -03001074 .vidioc_enum_fmt_vid_cap = coda_enum_fmt,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -03001075 .vidioc_g_fmt_vid_cap = coda_g_fmt,
1076 .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
1077 .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap,
Javier Martin186b2502012-07-26 05:53:35 -03001078
Philipp Zabel22e244b2014-07-18 07:22:43 -03001079 .vidioc_enum_fmt_vid_out = coda_enum_fmt,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -03001080 .vidioc_g_fmt_vid_out = coda_g_fmt,
1081 .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
1082 .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out,
Javier Martin186b2502012-07-26 05:53:35 -03001083
Philipp Zabel73751da2015-03-24 14:30:51 -03001084 .vidioc_reqbufs = coda_reqbufs,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001085 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
Javier Martin186b2502012-07-26 05:53:35 -03001086
Philipp Zabel2e9e4f12013-09-30 10:34:50 -03001087 .vidioc_qbuf = coda_qbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001088 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
Philipp Zabel9f2bfb32015-05-04 07:51:07 -03001089 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001090 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
Philipp Zabelbb757d72015-12-02 14:58:52 -02001091 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
Javier Martin186b2502012-07-26 05:53:35 -03001092
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001093 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
1094 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
Philipp Zabel918c66f2013-06-27 06:59:01 -03001095
Philipp Zabel52c41672014-07-11 06:36:19 -03001096 .vidioc_g_selection = coda_g_selection,
1097
Philipp Zabel59195ce2017-03-02 06:51:44 -03001098 .vidioc_try_encoder_cmd = coda_try_encoder_cmd,
1099 .vidioc_encoder_cmd = coda_encoder_cmd,
Philipp Zabel64ba40a2013-09-30 10:34:52 -03001100 .vidioc_try_decoder_cmd = coda_try_decoder_cmd,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -03001101 .vidioc_decoder_cmd = coda_decoder_cmd,
Philipp Zabel918c66f2013-06-27 06:59:01 -03001102
Philipp Zabelcde29ef2015-07-16 13:13:24 -03001103 .vidioc_g_parm = coda_g_parm,
1104 .vidioc_s_parm = coda_s_parm,
1105
Philipp Zabel2e9e4f12013-09-30 10:34:50 -03001106 .vidioc_subscribe_event = coda_subscribe_event,
Philipp Zabel918c66f2013-06-27 06:59:01 -03001107 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Javier Martin186b2502012-07-26 05:53:35 -03001108};
1109
1110/*
1111 * Mem-to-mem operations.
1112 */
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001113
1114static void coda_device_run(void *m2m_priv)
1115{
1116 struct coda_ctx *ctx = m2m_priv;
1117 struct coda_dev *dev = ctx->dev;
Philipp Zabel32b6f202014-07-11 06:36:20 -03001118
1119 queue_work(dev->workqueue, &ctx->pic_run_work);
1120}
1121
Philipp Zabel32b6f202014-07-11 06:36:20 -03001122static void coda_pic_run_work(struct work_struct *work)
1123{
1124 struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
1125 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001126 int ret;
1127
1128 mutex_lock(&ctx->buffer_mutex);
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001129 mutex_lock(&dev->coda_mutex);
1130
Philipp Zabela1192a12014-07-23 12:28:40 -03001131 ret = ctx->ops->prepare_run(ctx);
1132 if (ret < 0 && ctx->inst_type == CODA_INST_DECODER) {
1133 mutex_unlock(&dev->coda_mutex);
1134 mutex_unlock(&ctx->buffer_mutex);
1135 /* job_finish scheduled by prepare_decode */
1136 return;
Philipp Zabel10436672012-07-02 09:03:55 -03001137 }
1138
Philipp Zabelf23797b2014-08-06 08:02:23 -03001139 if (!wait_for_completion_timeout(&ctx->completion,
1140 msecs_to_jiffies(1000))) {
Philipp Zabel32b6f202014-07-11 06:36:20 -03001141 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout\n");
Philipp Zabel84e23652014-07-11 06:36:34 -03001142
1143 ctx->hold = true;
Philipp Zabel8f452842014-07-11 06:36:35 -03001144
1145 coda_hw_reset(ctx);
Philipp Zabel32b6f202014-07-11 06:36:20 -03001146 } else if (!ctx->aborting) {
Philipp Zabela1192a12014-07-23 12:28:40 -03001147 ctx->ops->finish_run(ctx);
Philipp Zabel32b6f202014-07-11 06:36:20 -03001148 }
1149
Philipp Zabel747d7642015-01-23 13:51:31 -03001150 if ((ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out)) &&
1151 ctx->ops->seq_end_work)
Philipp Zabel32b6f202014-07-11 06:36:20 -03001152 queue_work(dev->workqueue, &ctx->seq_end_work);
1153
1154 mutex_unlock(&dev->coda_mutex);
1155 mutex_unlock(&ctx->buffer_mutex);
1156
Philipp Zabel14604e32014-07-11 06:36:22 -03001157 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001158}
1159
1160static int coda_job_ready(void *m2m_priv)
1161{
1162 struct coda_ctx *ctx = m2m_priv;
Philipp Zabel2cf251c2015-07-09 07:10:20 -03001163 int src_bufs = v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001164
1165 /*
1166 * For both 'P' and 'key' frame cases 1 picture
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001167 * and 1 frame are needed. In the decoder case,
1168 * the compressed frame can be in the bitstream.
Javier Martin186b2502012-07-26 05:53:35 -03001169 */
Philipp Zabel2cf251c2015-07-09 07:10:20 -03001170 if (!src_bufs && ctx->inst_type != CODA_INST_DECODER) {
Javier Martin186b2502012-07-26 05:53:35 -03001171 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1172 "not ready: not enough video buffers.\n");
1173 return 0;
1174 }
1175
Philipp Zabel14604e32014-07-11 06:36:22 -03001176 if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001177 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1178 "not ready: not enough video capture buffers.\n");
1179 return 0;
1180 }
1181
Philipp Zabela22496c2015-01-23 13:51:33 -03001182 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
Philipp Zabel47f3fa62015-07-09 07:10:21 -03001183 bool stream_end = ctx->bit_stream_param &
1184 CODA_BIT_STREAM_END_FLAG;
1185 int num_metas = ctx->num_metas;
Michael Trettere7f3c542017-01-20 12:00:24 -02001186 unsigned int count;
1187
1188 count = hweight32(ctx->frm_dis_flg);
1189 if (ctx->use_vdoa && count >= (ctx->num_internal_frames - 1)) {
1190 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1191 "%d: not ready: all internal buffers in use: %d/%d (0x%x)",
1192 ctx->idx, count, ctx->num_internal_frames,
1193 ctx->frm_dis_flg);
1194 return 0;
1195 }
Philipp Zabelf77fd8a2015-01-23 13:51:20 -03001196
Philipp Zabel2cf251c2015-07-09 07:10:20 -03001197 if (ctx->hold && !src_bufs) {
Philipp Zabelf77fd8a2015-01-23 13:51:20 -03001198 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1199 "%d: not ready: on hold for more buffers.\n",
1200 ctx->idx);
1201 return 0;
1202 }
1203
Philipp Zabelf77fd8a2015-01-23 13:51:20 -03001204 if (!stream_end && (num_metas + src_bufs) < 2) {
1205 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1206 "%d: not ready: need 2 buffers available (%d, %d)\n",
1207 ctx->idx, num_metas, src_bufs);
1208 return 0;
1209 }
1210
1211
Philipp Zabel2cf251c2015-07-09 07:10:20 -03001212 if (!src_bufs && !stream_end &&
1213 (coda_get_bitstream_payload(ctx) < 512)) {
Philipp Zabelf77fd8a2015-01-23 13:51:20 -03001214 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1215 "%d: not ready: not enough bitstream data (%d).\n",
1216 ctx->idx, coda_get_bitstream_payload(ctx));
1217 return 0;
1218 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001219 }
1220
Philipp Zabel3e748262013-05-23 10:43:01 -03001221 if (ctx->aborting) {
1222 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1223 "not ready: aborting\n");
1224 return 0;
1225 }
1226
Javier Martin186b2502012-07-26 05:53:35 -03001227 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1228 "job ready\n");
Philipp Zabel47f3fa62015-07-09 07:10:21 -03001229
Javier Martin186b2502012-07-26 05:53:35 -03001230 return 1;
1231}
1232
1233static void coda_job_abort(void *priv)
1234{
1235 struct coda_ctx *ctx = priv;
Javier Martin186b2502012-07-26 05:53:35 -03001236
1237 ctx->aborting = 1;
1238
1239 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1240 "Aborting task\n");
Javier Martin186b2502012-07-26 05:53:35 -03001241}
1242
1243static void coda_lock(void *m2m_priv)
1244{
1245 struct coda_ctx *ctx = m2m_priv;
1246 struct coda_dev *pcdev = ctx->dev;
Philipp Zabelf23797b2014-08-06 08:02:23 -03001247
Javier Martin186b2502012-07-26 05:53:35 -03001248 mutex_lock(&pcdev->dev_mutex);
1249}
1250
1251static void coda_unlock(void *m2m_priv)
1252{
1253 struct coda_ctx *ctx = m2m_priv;
1254 struct coda_dev *pcdev = ctx->dev;
Philipp Zabelf23797b2014-08-06 08:02:23 -03001255
Javier Martin186b2502012-07-26 05:53:35 -03001256 mutex_unlock(&pcdev->dev_mutex);
1257}
1258
Philipp Zabel814c3762014-07-18 07:22:45 -03001259static const struct v4l2_m2m_ops coda_m2m_ops = {
Javier Martin186b2502012-07-26 05:53:35 -03001260 .device_run = coda_device_run,
1261 .job_ready = coda_job_ready,
1262 .job_abort = coda_job_abort,
1263 .lock = coda_lock,
1264 .unlock = coda_unlock,
1265};
1266
1267static void set_default_params(struct coda_ctx *ctx)
1268{
Philipp Zabel8e428d52015-01-23 13:51:29 -03001269 unsigned int max_w, max_h, usize, csize;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001270
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001271 ctx->codec = coda_find_codec(ctx->dev, ctx->cvd->src_formats[0],
1272 ctx->cvd->dst_formats[0]);
Philipp Zabeld4c6a412014-10-02 14:08:35 -03001273 max_w = min(ctx->codec->max_w, 1920U);
1274 max_h = min(ctx->codec->max_h, 1088U);
Philipp Zabel8e428d52015-01-23 13:51:29 -03001275 usize = max_w * max_h * 3 / 2;
1276 csize = coda_estimate_sizeimage(ctx, usize, max_w, max_h);
Javier Martin186b2502012-07-26 05:53:35 -03001277
Philipp Zabel121cacf2014-07-18 07:22:42 -03001278 ctx->params.codec_mode = ctx->codec->mode;
Javier Martin186b2502012-07-26 05:53:35 -03001279 ctx->colorspace = V4L2_COLORSPACE_REC709;
1280 ctx->params.framerate = 30;
Javier Martin186b2502012-07-26 05:53:35 -03001281
1282 /* Default formats for output and input queues */
Philipp Zabel6727d4f2015-07-16 13:19:39 -03001283 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->cvd->src_formats[0];
1284 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->cvd->dst_formats[0];
Philipp Zabelb96904e2013-05-23 10:42:58 -03001285 ctx->q_data[V4L2_M2M_SRC].width = max_w;
1286 ctx->q_data[V4L2_M2M_SRC].height = max_h;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001287 ctx->q_data[V4L2_M2M_DST].width = max_w;
1288 ctx->q_data[V4L2_M2M_DST].height = max_h;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001289 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_YUV420) {
1290 ctx->q_data[V4L2_M2M_SRC].bytesperline = max_w;
Philipp Zabel8e428d52015-01-23 13:51:29 -03001291 ctx->q_data[V4L2_M2M_SRC].sizeimage = usize;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001292 ctx->q_data[V4L2_M2M_DST].bytesperline = 0;
Philipp Zabel8e428d52015-01-23 13:51:29 -03001293 ctx->q_data[V4L2_M2M_DST].sizeimage = csize;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001294 } else {
1295 ctx->q_data[V4L2_M2M_SRC].bytesperline = 0;
Philipp Zabel8e428d52015-01-23 13:51:29 -03001296 ctx->q_data[V4L2_M2M_SRC].sizeimage = csize;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001297 ctx->q_data[V4L2_M2M_DST].bytesperline = max_w;
Philipp Zabel8e428d52015-01-23 13:51:29 -03001298 ctx->q_data[V4L2_M2M_DST].sizeimage = usize;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001299 }
Philipp Zabel52c41672014-07-11 06:36:19 -03001300 ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
1301 ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
1302 ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
1303 ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
Philipp Zabel89548442014-07-11 06:36:17 -03001304
Philipp Zabela269e532015-07-16 13:19:38 -03001305 /*
1306 * Since the RBC2AXI logic only supports a single chroma plane,
1307 * macroblock tiling only works for to NV12 pixel format.
1308 */
1309 ctx->tiled_map_type = GDI_LINEAR_FRAME_MAP;
Javier Martin186b2502012-07-26 05:53:35 -03001310}
1311
1312/*
1313 * Queue operations
1314 */
Hans Verkuildf9ecb02015-10-28 00:50:37 -02001315static int coda_queue_setup(struct vb2_queue *vq,
Javier Martin186b2502012-07-26 05:53:35 -03001316 unsigned int *nbuffers, unsigned int *nplanes,
Hans Verkuil36c0f8b2016-04-15 09:15:05 -03001317 unsigned int sizes[], struct device *alloc_devs[])
Javier Martin186b2502012-07-26 05:53:35 -03001318{
1319 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
Philipp Zabele34db062012-08-29 08:22:00 -03001320 struct coda_q_data *q_data;
Javier Martin186b2502012-07-26 05:53:35 -03001321 unsigned int size;
1322
Philipp Zabele34db062012-08-29 08:22:00 -03001323 q_data = get_q_data(ctx, vq->type);
1324 size = q_data->sizeimage;
Javier Martin186b2502012-07-26 05:53:35 -03001325
1326 *nplanes = 1;
1327 sizes[0] = size;
1328
Javier Martin186b2502012-07-26 05:53:35 -03001329 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1330 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
1331
1332 return 0;
1333}
1334
1335static int coda_buf_prepare(struct vb2_buffer *vb)
1336{
1337 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1338 struct coda_q_data *q_data;
1339
1340 q_data = get_q_data(ctx, vb->vb2_queue->type);
1341
1342 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
1343 v4l2_warn(&ctx->dev->v4l2_dev,
1344 "%s data will not fit into plane (%lu < %lu)\n",
1345 __func__, vb2_plane_size(vb, 0),
1346 (long)q_data->sizeimage);
1347 return -EINVAL;
1348 }
1349
Javier Martin186b2502012-07-26 05:53:35 -03001350 return 0;
1351}
1352
1353static void coda_buf_queue(struct vb2_buffer *vb)
1354{
Junghak Sung2d700712015-09-22 10:30:30 -03001355 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
Javier Martin186b2502012-07-26 05:53:35 -03001356 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
Philipp Zabelcb06b702015-01-23 13:51:35 -03001357 struct vb2_queue *vq = vb->vb2_queue;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001358 struct coda_q_data *q_data;
1359
1360 q_data = get_q_data(ctx, vb->vb2_queue->type);
1361
1362 /*
1363 * In the decoder case, immediately try to copy the buffer into the
1364 * bitstream ringbuffer and mark it as ready to be dequeued.
1365 */
Philipp Zabelcb06b702015-01-23 13:51:35 -03001366 if (ctx->bitstream.size && vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001367 /*
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03001368 * For backwards compatibility, queuing an empty buffer marks
Philipp Zabel918c66f2013-06-27 06:59:01 -03001369 * the stream end
1370 */
Philipp Zabel347bb7f2014-07-23 12:28:42 -03001371 if (vb2_get_plane_payload(vb, 0) == 0)
1372 coda_bit_stream_end_flag(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001373 mutex_lock(&ctx->bitstream_mutex);
Junghak Sung2d700712015-09-22 10:30:30 -03001374 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
Michael Olbricheabed932014-07-18 07:22:40 -03001375 if (vb2_is_streaming(vb->vb2_queue))
Philipp Zabel59195ce2017-03-02 06:51:44 -03001376 /* This set buf->sequence = ctx->qsequence++ */
Philipp Zabel582d88722015-03-24 14:30:57 -03001377 coda_fill_bitstream(ctx, true);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001378 mutex_unlock(&ctx->bitstream_mutex);
1379 } else {
Philipp Zabel59195ce2017-03-02 06:51:44 -03001380 if (ctx->inst_type == CODA_INST_ENCODER &&
1381 vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1382 vbuf->sequence = ctx->qsequence++;
Junghak Sung2d700712015-09-22 10:30:30 -03001383 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001384 }
Javier Martin186b2502012-07-26 05:53:35 -03001385}
1386
Philipp Zabel79924ca2014-07-23 12:28:45 -03001387int coda_alloc_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf,
1388 size_t size, const char *name, struct dentry *parent)
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001389{
1390 buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
1391 GFP_KERNEL);
Philipp Zabel68fc31c2014-08-05 14:00:16 -03001392 if (!buf->vaddr) {
1393 v4l2_err(&dev->v4l2_dev,
1394 "Failed to allocate %s buffer of size %u\n",
1395 name, size);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001396 return -ENOMEM;
Philipp Zabel68fc31c2014-08-05 14:00:16 -03001397 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001398
1399 buf->size = size;
1400
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001401 if (name && parent) {
1402 buf->blob.data = buf->vaddr;
1403 buf->blob.size = size;
Philipp Zabelf23797b2014-08-06 08:02:23 -03001404 buf->dentry = debugfs_create_blob(name, 0644, parent,
1405 &buf->blob);
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001406 if (!buf->dentry)
1407 dev_warn(&dev->plat_dev->dev,
1408 "failed to create debugfs entry %s\n", name);
1409 }
1410
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001411 return 0;
1412}
1413
Philipp Zabel79924ca2014-07-23 12:28:45 -03001414void coda_free_aux_buf(struct coda_dev *dev,
1415 struct coda_aux_buf *buf)
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001416{
1417 if (buf->vaddr) {
1418 dma_free_coherent(&dev->plat_dev->dev, buf->size,
1419 buf->vaddr, buf->paddr);
1420 buf->vaddr = NULL;
1421 buf->size = 0;
Peter Seidererd446ec82015-03-24 14:30:48 -03001422 debugfs_remove(buf->dentry);
1423 buf->dentry = NULL;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001424 }
1425}
1426
Javier Martin186b2502012-07-26 05:53:35 -03001427static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
1428{
1429 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1430 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
Javier Martin186b2502012-07-26 05:53:35 -03001431 struct coda_q_data *q_data_src, *q_data_dst;
Junghak Sung2d700712015-09-22 10:30:30 -03001432 struct vb2_v4l2_buffer *buf;
Philipp Zabeld35167a2013-05-23 10:42:59 -03001433 int ret = 0;
Javier Martin186b2502012-07-26 05:53:35 -03001434
Philipp Zabel49b966f2015-12-02 14:58:53 -02001435 if (count < 1)
1436 return -EINVAL;
1437
Philipp Zabelb96904e2013-05-23 10:42:58 -03001438 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001439 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabel58bc7ed2015-07-09 07:10:14 -03001440 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
Philipp Zabel582d88722015-03-24 14:30:57 -03001441 /* copy the buffers that were queued before streamon */
Michael Olbricheabed932014-07-18 07:22:40 -03001442 mutex_lock(&ctx->bitstream_mutex);
Philipp Zabel582d88722015-03-24 14:30:57 -03001443 coda_fill_bitstream(ctx, false);
Michael Olbricheabed932014-07-18 07:22:40 -03001444 mutex_unlock(&ctx->bitstream_mutex);
1445
Philipp Zabelb9063522014-08-05 14:00:10 -03001446 if (coda_get_bitstream_payload(ctx) < 512) {
1447 ret = -EINVAL;
1448 goto err;
1449 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001450 }
1451
1452 ctx->streamon_out = 1;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001453 } else {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001454 ctx->streamon_cap = 1;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001455 }
Javier Martin186b2502012-07-26 05:53:35 -03001456
Philipp Zabelb96904e2013-05-23 10:42:58 -03001457 /* Don't start the coda unless both queues are on */
1458 if (!(ctx->streamon_out & ctx->streamon_cap))
1459 return 0;
Javier Martin186b2502012-07-26 05:53:35 -03001460
Philipp Zabel23b6ee52015-03-24 14:30:55 -03001461 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1462 if ((q_data_src->width != q_data_dst->width &&
1463 round_up(q_data_src->width, 16) != q_data_dst->width) ||
1464 (q_data_src->height != q_data_dst->height &&
1465 round_up(q_data_src->height, 16) != q_data_dst->height)) {
1466 v4l2_err(v4l2_dev, "can't convert %dx%d to %dx%d\n",
1467 q_data_src->width, q_data_src->height,
1468 q_data_dst->width, q_data_dst->height);
1469 ret = -EINVAL;
1470 goto err;
1471 }
1472
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001473 /* Allow BIT decoder device_run with no new buffers queued */
Philipp Zabela22496c2015-01-23 13:51:33 -03001474 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
Philipp Zabel14604e32014-07-11 06:36:22 -03001475 v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001476
Philipp Zabelb96904e2013-05-23 10:42:58 -03001477 ctx->gopcounter = ctx->params.gop_size - 1;
Javier Martin186b2502012-07-26 05:53:35 -03001478
Philipp Zabelb96904e2013-05-23 10:42:58 -03001479 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
1480 q_data_dst->fourcc);
1481 if (!ctx->codec) {
Javier Martin186b2502012-07-26 05:53:35 -03001482 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
Philipp Zabelb9063522014-08-05 14:00:10 -03001483 ret = -EINVAL;
1484 goto err;
Javier Martin186b2502012-07-26 05:53:35 -03001485 }
1486
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001487 if (q_data_dst->fourcc == V4L2_PIX_FMT_JPEG)
1488 ctx->params.gop_size = 1;
1489 ctx->gopcounter = ctx->params.gop_size - 1;
1490
Philipp Zabela1192a12014-07-23 12:28:40 -03001491 ret = ctx->ops->start_streaming(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001492 if (ctx->inst_type == CODA_INST_DECODER) {
Philipp Zabel89548442014-07-11 06:36:17 -03001493 if (ret == -EAGAIN)
Philipp Zabel918c66f2013-06-27 06:59:01 -03001494 return 0;
Philipp Zabel89548442014-07-11 06:36:17 -03001495 else if (ret < 0)
Philipp Zabelb9063522014-08-05 14:00:10 -03001496 goto err;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001497 }
1498
Philipp Zabel89548442014-07-11 06:36:17 -03001499 return ret;
Philipp Zabelb9063522014-08-05 14:00:10 -03001500
1501err:
1502 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1503 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
Philipp Zabelb8b1b582014-10-21 13:25:52 -03001504 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
Philipp Zabelb9063522014-08-05 14:00:10 -03001505 } else {
1506 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
Philipp Zabelb8b1b582014-10-21 13:25:52 -03001507 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
Philipp Zabelb9063522014-08-05 14:00:10 -03001508 }
1509 return ret;
Philipp Zabel89548442014-07-11 06:36:17 -03001510}
1511
Hans Verkuile37559b2014-04-17 02:47:21 -03001512static void coda_stop_streaming(struct vb2_queue *q)
Javier Martin186b2502012-07-26 05:53:35 -03001513{
1514 struct coda_ctx *ctx = vb2_get_drv_priv(q);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03001515 struct coda_dev *dev = ctx->dev;
Junghak Sung2d700712015-09-22 10:30:30 -03001516 struct vb2_v4l2_buffer *buf;
Philipp Zabel47f3fa62015-07-09 07:10:21 -03001517 unsigned long flags;
Philipp Zabel5c76c2c2015-03-24 14:30:56 -03001518 bool stop;
1519
1520 stop = ctx->streamon_out && ctx->streamon_cap;
Javier Martin186b2502012-07-26 05:53:35 -03001521
1522 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001523 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03001524 "%s: output\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001525 ctx->streamon_out = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001526
Philipp Zabel347bb7f2014-07-23 12:28:42 -03001527 coda_bit_stream_end_flag(ctx);
Philipp Zabel4a31b522014-08-05 14:00:11 -03001528
Philipp Zabel6dd5ef52015-01-23 13:51:26 -03001529 ctx->qsequence = 0;
Philipp Zabel4a31b522014-08-05 14:00:11 -03001530
1531 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1532 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
Javier Martin186b2502012-07-26 05:53:35 -03001533 } else {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001534 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03001535 "%s: capture\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001536 ctx->streamon_cap = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001537
1538 ctx->osequence = 0;
Philipp Zabelcb2c0282014-07-11 06:36:33 -03001539 ctx->sequence_offset = 0;
Philipp Zabel4a31b522014-08-05 14:00:11 -03001540
1541 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
1542 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
Javier Martin186b2502012-07-26 05:53:35 -03001543 }
1544
Philipp Zabel5c76c2c2015-03-24 14:30:56 -03001545 if (stop) {
Philipp Zabel7cbb1052014-10-02 14:08:32 -03001546 struct coda_buffer_meta *meta;
Philipp Zabel846ced92014-07-11 06:36:31 -03001547
Philipp Zabelf4706d62015-01-23 13:51:27 -03001548 if (ctx->ops->seq_end_work) {
1549 queue_work(dev->workqueue, &ctx->seq_end_work);
1550 flush_work(&ctx->seq_end_work);
1551 }
Philipp Zabel47f3fa62015-07-09 07:10:21 -03001552 spin_lock_irqsave(&ctx->buffer_meta_lock, flags);
Philipp Zabel7cbb1052014-10-02 14:08:32 -03001553 while (!list_empty(&ctx->buffer_meta_list)) {
1554 meta = list_first_entry(&ctx->buffer_meta_list,
1555 struct coda_buffer_meta, list);
1556 list_del(&meta->list);
1557 kfree(meta);
Philipp Zabel846ced92014-07-11 06:36:31 -03001558 }
Philipp Zabel47f3fa62015-07-09 07:10:21 -03001559 ctx->num_metas = 0;
1560 spin_unlock_irqrestore(&ctx->buffer_meta_lock, flags);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001561 kfifo_init(&ctx->bitstream_fifo,
1562 ctx->bitstream.vaddr, ctx->bitstream.size);
1563 ctx->runcounter = 0;
Philipp Zabelf157cf42014-09-29 09:53:42 -03001564 ctx->aborting = 0;
Philipp Zabel62bed142012-07-25 10:40:39 -03001565 }
Philipp Zabelc1ae0b22015-07-09 07:10:17 -03001566
1567 if (!ctx->streamon_out && !ctx->streamon_cap)
1568 ctx->bit_stream_param &= ~CODA_BIT_STREAM_END_FLAG;
Javier Martin186b2502012-07-26 05:53:35 -03001569}
1570
Philipp Zabel121cacf2014-07-18 07:22:42 -03001571static const struct vb2_ops coda_qops = {
Javier Martin186b2502012-07-26 05:53:35 -03001572 .queue_setup = coda_queue_setup,
1573 .buf_prepare = coda_buf_prepare,
1574 .buf_queue = coda_buf_queue,
Javier Martin186b2502012-07-26 05:53:35 -03001575 .start_streaming = coda_start_streaming,
1576 .stop_streaming = coda_stop_streaming,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001577 .wait_prepare = vb2_ops_wait_prepare,
1578 .wait_finish = vb2_ops_wait_finish,
Javier Martin186b2502012-07-26 05:53:35 -03001579};
1580
1581static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
1582{
1583 struct coda_ctx *ctx =
1584 container_of(ctrl->handler, struct coda_ctx, ctrls);
1585
1586 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1587 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
1588
1589 switch (ctrl->id) {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001590 case V4L2_CID_HFLIP:
1591 if (ctrl->val)
1592 ctx->params.rot_mode |= CODA_MIR_HOR;
1593 else
1594 ctx->params.rot_mode &= ~CODA_MIR_HOR;
1595 break;
1596 case V4L2_CID_VFLIP:
1597 if (ctrl->val)
1598 ctx->params.rot_mode |= CODA_MIR_VER;
1599 else
1600 ctx->params.rot_mode &= ~CODA_MIR_VER;
1601 break;
Javier Martin186b2502012-07-26 05:53:35 -03001602 case V4L2_CID_MPEG_VIDEO_BITRATE:
1603 ctx->params.bitrate = ctrl->val / 1000;
1604 break;
1605 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
1606 ctx->params.gop_size = ctrl->val;
1607 break;
1608 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
1609 ctx->params.h264_intra_qp = ctrl->val;
1610 break;
1611 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
1612 ctx->params.h264_inter_qp = ctrl->val;
1613 break;
Philipp Zabel1a5567e2014-07-11 06:36:26 -03001614 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
1615 ctx->params.h264_min_qp = ctrl->val;
1616 break;
1617 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
1618 ctx->params.h264_max_qp = ctrl->val;
1619 break;
Philipp Zabelde23b1d2014-07-11 06:36:27 -03001620 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
1621 ctx->params.h264_deblk_alpha = ctrl->val;
1622 break;
1623 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
1624 ctx->params.h264_deblk_beta = ctrl->val;
1625 break;
1626 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
1627 ctx->params.h264_deblk_enabled = (ctrl->val ==
1628 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
1629 break;
Javier Martin186b2502012-07-26 05:53:35 -03001630 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
1631 ctx->params.mpeg4_intra_qp = ctrl->val;
1632 break;
1633 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
1634 ctx->params.mpeg4_inter_qp = ctrl->val;
1635 break;
1636 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
1637 ctx->params.slice_mode = ctrl->val;
1638 break;
1639 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
1640 ctx->params.slice_max_mb = ctrl->val;
1641 break;
Philipp Zabelc566c782012-08-08 11:59:38 -03001642 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
1643 ctx->params.slice_max_bits = ctrl->val * 8;
1644 break;
Javier Martin186b2502012-07-26 05:53:35 -03001645 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
1646 break;
Philipp Zabelf38f79d2014-07-11 06:36:28 -03001647 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
1648 ctx->params.intra_refresh = ctrl->val;
1649 break;
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001650 case V4L2_CID_JPEG_COMPRESSION_QUALITY:
1651 coda_set_jpeg_compression_quality(ctx, ctrl->val);
1652 break;
1653 case V4L2_CID_JPEG_RESTART_INTERVAL:
1654 ctx->params.jpeg_restart_interval = ctrl->val;
1655 break;
Philipp Zabelda2b3b32015-07-10 10:37:52 -03001656 case V4L2_CID_MPEG_VIDEO_VBV_DELAY:
1657 ctx->params.vbv_delay = ctrl->val;
1658 break;
1659 case V4L2_CID_MPEG_VIDEO_VBV_SIZE:
1660 ctx->params.vbv_size = min(ctrl->val * 8192, 0x7fffffff);
1661 break;
Javier Martin186b2502012-07-26 05:53:35 -03001662 default:
1663 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1664 "Invalid control, id=%d, val=%d\n",
1665 ctrl->id, ctrl->val);
1666 return -EINVAL;
1667 }
1668
1669 return 0;
1670}
1671
Philipp Zabel814c3762014-07-18 07:22:45 -03001672static const struct v4l2_ctrl_ops coda_ctrl_ops = {
Javier Martin186b2502012-07-26 05:53:35 -03001673 .s_ctrl = coda_s_ctrl,
1674};
1675
Philipp Zabelbfc732f2014-10-02 14:08:29 -03001676static void coda_encode_ctrls(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03001677{
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001678 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel92b042e2015-03-18 08:15:36 -03001679 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1000, 0);
Javier Martin186b2502012-07-26 05:53:35 -03001680 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1681 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
1682 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03001683 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
Javier Martin186b2502012-07-26 05:53:35 -03001684 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03001685 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
Philipp Zabel1a5567e2014-07-11 06:36:26 -03001686 if (ctx->dev->devtype->product != CODA_960) {
1687 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1688 V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12);
1689 }
1690 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1691 V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51);
Javier Martin186b2502012-07-26 05:53:35 -03001692 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabelde23b1d2014-07-11 06:36:27 -03001693 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, 0, 15, 1, 0);
1694 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1695 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, 0, 15, 1, 0);
1696 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1697 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
1698 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED, 0x0,
1699 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
1700 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03001701 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
1702 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1703 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
1704 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1705 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
Philipp Zabelc566c782012-08-08 11:59:38 -03001706 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
1707 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
Javier Martin186b2502012-07-26 05:53:35 -03001708 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1709 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
Philipp Zabelc566c782012-08-08 11:59:38 -03001710 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabelf23797b2014-08-06 08:02:23 -03001711 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1,
1712 500);
Javier Martin186b2502012-07-26 05:53:35 -03001713 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1714 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
1715 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
1716 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
1717 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
Philipp Zabelf38f79d2014-07-11 06:36:28 -03001718 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabelf23797b2014-08-06 08:02:23 -03001719 V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0,
1720 1920 * 1088 / 256, 1, 0);
Philipp Zabelda2b3b32015-07-10 10:37:52 -03001721 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1722 V4L2_CID_MPEG_VIDEO_VBV_DELAY, 0, 0x7fff, 1, 0);
1723 /*
1724 * The maximum VBV size value is 0x7fffffff bits,
1725 * one bit less than 262144 KiB
1726 */
1727 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1728 V4L2_CID_MPEG_VIDEO_VBV_SIZE, 0, 262144, 1, 0);
Philipp Zabelbfc732f2014-10-02 14:08:29 -03001729}
1730
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001731static void coda_jpeg_encode_ctrls(struct coda_ctx *ctx)
1732{
1733 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1734 V4L2_CID_JPEG_COMPRESSION_QUALITY, 5, 100, 1, 50);
1735 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1736 V4L2_CID_JPEG_RESTART_INTERVAL, 0, 100, 1, 0);
1737}
1738
Philipp Zabelbfc732f2014-10-02 14:08:29 -03001739static int coda_ctrls_setup(struct coda_ctx *ctx)
1740{
1741 v4l2_ctrl_handler_init(&ctx->ctrls, 2);
1742
1743 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1744 V4L2_CID_HFLIP, 0, 1, 1, 0);
1745 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1746 V4L2_CID_VFLIP, 0, 1, 1, 0);
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001747 if (ctx->inst_type == CODA_INST_ENCODER) {
1748 if (ctx->cvd->dst_formats[0] == V4L2_PIX_FMT_JPEG)
1749 coda_jpeg_encode_ctrls(ctx);
1750 else
1751 coda_encode_ctrls(ctx);
1752 }
Javier Martin186b2502012-07-26 05:53:35 -03001753
1754 if (ctx->ctrls.error) {
Philipp Zabelf23797b2014-08-06 08:02:23 -03001755 v4l2_err(&ctx->dev->v4l2_dev,
1756 "control initialization error (%d)",
Javier Martin186b2502012-07-26 05:53:35 -03001757 ctx->ctrls.error);
1758 return -EINVAL;
1759 }
1760
1761 return v4l2_ctrl_handler_setup(&ctx->ctrls);
1762}
1763
Philipp Zabel121cacf2014-07-18 07:22:42 -03001764static int coda_queue_init(struct coda_ctx *ctx, struct vb2_queue *vq)
Javier Martin186b2502012-07-26 05:53:35 -03001765{
Philipp Zabel121cacf2014-07-18 07:22:42 -03001766 vq->drv_priv = ctx;
1767 vq->ops = &coda_qops;
1768 vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1769 vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1770 vq->lock = &ctx->dev->dev_mutex;
Kamil Debskie4af23d2015-02-23 09:26:18 -03001771 /* One way to indicate end-of-stream for coda is to set the
1772 * bytesused == 0. However by default videobuf2 handles bytesused
1773 * equal to 0 as a special case and changes its value to the size
1774 * of the buffer. Set the allow_zero_bytesused flag, so
1775 * that videobuf2 will keep the value of bytesused intact.
1776 */
1777 vq->allow_zero_bytesused = 1;
Hans Verkuil53ddcc62016-02-15 13:09:10 -02001778 vq->dev = &ctx->dev->plat_dev->dev;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001779
1780 return vb2_queue_init(vq);
1781}
1782
Philipp Zabel79924ca2014-07-23 12:28:45 -03001783int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq,
1784 struct vb2_queue *dst_vq)
Philipp Zabel121cacf2014-07-18 07:22:42 -03001785{
Javier Martin186b2502012-07-26 05:53:35 -03001786 int ret;
1787
Javier Martin186b2502012-07-26 05:53:35 -03001788 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabeld29a8cf2014-07-18 07:22:38 -03001789 src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
Javier Martin186b2502012-07-26 05:53:35 -03001790 src_vq->mem_ops = &vb2_dma_contig_memops;
1791
Philipp Zabel121cacf2014-07-18 07:22:42 -03001792 ret = coda_queue_init(priv, src_vq);
Javier Martin186b2502012-07-26 05:53:35 -03001793 if (ret)
1794 return ret;
1795
Javier Martin186b2502012-07-26 05:53:35 -03001796 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Philipp Zabeld29a8cf2014-07-18 07:22:38 -03001797 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
Javier Martin186b2502012-07-26 05:53:35 -03001798 dst_vq->mem_ops = &vb2_dma_contig_memops;
1799
Philipp Zabel121cacf2014-07-18 07:22:42 -03001800 return coda_queue_init(priv, dst_vq);
1801}
1802
Philipp Zabel79924ca2014-07-23 12:28:45 -03001803int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq,
1804 struct vb2_queue *dst_vq)
Philipp Zabel121cacf2014-07-18 07:22:42 -03001805{
1806 int ret;
1807
1808 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabelbb04aa62015-01-23 13:51:30 -03001809 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
1810 src_vq->mem_ops = &vb2_vmalloc_memops;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001811
1812 ret = coda_queue_init(priv, src_vq);
1813 if (ret)
1814 return ret;
1815
1816 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1817 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1818 dst_vq->mem_ops = &vb2_dma_contig_memops;
1819
1820 return coda_queue_init(priv, dst_vq);
Javier Martin186b2502012-07-26 05:53:35 -03001821}
1822
Philipp Zabele11f3e62012-07-25 09:16:58 -03001823static int coda_next_free_instance(struct coda_dev *dev)
1824{
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03001825 int idx = ffz(dev->instance_mask);
1826
1827 if ((idx < 0) ||
1828 (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES))
1829 return -EBUSY;
1830
1831 return idx;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001832}
1833
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001834/*
1835 * File operations
1836 */
1837
1838static int coda_open(struct file *file)
Javier Martin186b2502012-07-26 05:53:35 -03001839{
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001840 struct video_device *vdev = video_devdata(file);
1841 struct coda_dev *dev = video_get_drvdata(vdev);
Javier Martin186b2502012-07-26 05:53:35 -03001842 struct coda_ctx *ctx = NULL;
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001843 char *name;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001844 int ret;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001845 int idx;
Javier Martin186b2502012-07-26 05:53:35 -03001846
Philipp Zabelf23797b2014-08-06 08:02:23 -03001847 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
Javier Martin186b2502012-07-26 05:53:35 -03001848 if (!ctx)
1849 return -ENOMEM;
1850
Fabio Estevamf82bc202013-08-21 11:14:16 -03001851 idx = coda_next_free_instance(dev);
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03001852 if (idx < 0) {
1853 ret = idx;
Fabio Estevamf82bc202013-08-21 11:14:16 -03001854 goto err_coda_max;
1855 }
1856 set_bit(idx, &dev->instance_mask);
1857
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001858 name = kasprintf(GFP_KERNEL, "context%d", idx);
Peter Seiderera7f933a2015-03-24 14:30:47 -03001859 if (!name) {
1860 ret = -ENOMEM;
1861 goto err_coda_name_init;
1862 }
1863
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001864 ctx->debugfs_entry = debugfs_create_dir(name, dev->debugfs_root);
1865 kfree(name);
1866
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001867 ctx->cvd = to_coda_video_device(vdev);
1868 ctx->inst_type = ctx->cvd->type;
1869 ctx->ops = ctx->cvd->ops;
Philipp Zabela22496c2015-01-23 13:51:33 -03001870 ctx->use_bit = !ctx->cvd->direct;
Philipp Zabel32b6f202014-07-11 06:36:20 -03001871 init_completion(&ctx->completion);
1872 INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
Philipp Zabel747d7642015-01-23 13:51:31 -03001873 if (ctx->ops->seq_end_work)
1874 INIT_WORK(&ctx->seq_end_work, ctx->ops->seq_end_work);
Javier Martin186b2502012-07-26 05:53:35 -03001875 v4l2_fh_init(&ctx->fh, video_devdata(file));
1876 file->private_data = &ctx->fh;
1877 v4l2_fh_add(&ctx->fh);
1878 ctx->dev = dev;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001879 ctx->idx = idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001880 switch (dev->devtype->product) {
Philipp Zabel89548442014-07-11 06:36:17 -03001881 case CODA_960:
Philipp Zabel2bf299c2014-09-29 09:53:46 -03001882 ctx->frame_mem_ctrl = 1 << 12;
1883 /* fallthrough */
1884 case CODA_7541:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001885 ctx->reg_idx = 0;
1886 break;
1887 default:
1888 ctx->reg_idx = idx;
1889 }
Michael Trettere7f3c542017-01-20 12:00:24 -02001890 if (ctx->dev->vdoa && !disable_vdoa) {
1891 ctx->vdoa = vdoa_context_create(dev->vdoa);
1892 if (!ctx->vdoa)
1893 v4l2_warn(&dev->v4l2_dev,
1894 "Failed to create vdoa context: not using vdoa");
1895 }
1896 ctx->use_vdoa = false;
Fabio Estevamf82bc202013-08-21 11:14:16 -03001897
Philipp Zabel1e172732014-07-11 06:36:23 -03001898 /* Power up and upload firmware if necessary */
1899 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
1900 if (ret < 0) {
1901 v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
1902 goto err_pm_get;
1903 }
1904
Fabio Estevam79830db2013-08-21 11:14:17 -03001905 ret = clk_prepare_enable(dev->clk_per);
1906 if (ret)
1907 goto err_clk_per;
1908
1909 ret = clk_prepare_enable(dev->clk_ahb);
1910 if (ret)
1911 goto err_clk_ahb;
1912
Javier Martin186b2502012-07-26 05:53:35 -03001913 set_default_params(ctx);
Philipp Zabela1192a12014-07-23 12:28:40 -03001914 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
1915 ctx->ops->queue_init);
Philipp Zabel14604e32014-07-11 06:36:22 -03001916 if (IS_ERR(ctx->fh.m2m_ctx)) {
1917 ret = PTR_ERR(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001918
1919 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
1920 __func__, ret);
Fabio Estevamf82bc202013-08-21 11:14:16 -03001921 goto err_ctx_init;
Javier Martin186b2502012-07-26 05:53:35 -03001922 }
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001923
Javier Martin186b2502012-07-26 05:53:35 -03001924 ret = coda_ctrls_setup(ctx);
1925 if (ret) {
1926 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
Fabio Estevamf82bc202013-08-21 11:14:16 -03001927 goto err_ctrls_setup;
Javier Martin186b2502012-07-26 05:53:35 -03001928 }
1929
1930 ctx->fh.ctrl_handler = &ctx->ctrls;
1931
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001932 mutex_init(&ctx->bitstream_mutex);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001933 mutex_init(&ctx->buffer_mutex);
Philipp Zabel7cbb1052014-10-02 14:08:32 -03001934 INIT_LIST_HEAD(&ctx->buffer_meta_list);
Philipp Zabel47f3fa62015-07-09 07:10:21 -03001935 spin_lock_init(&ctx->buffer_meta_lock);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001936
Javier Martin186b2502012-07-26 05:53:35 -03001937 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001938 list_add(&ctx->list, &dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03001939 coda_unlock(ctx);
1940
Javier Martin186b2502012-07-26 05:53:35 -03001941 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
1942 ctx->idx, ctx);
1943
1944 return 0;
1945
Fabio Estevamf82bc202013-08-21 11:14:16 -03001946err_ctrls_setup:
Philipp Zabel14604e32014-07-11 06:36:22 -03001947 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Fabio Estevamf82bc202013-08-21 11:14:16 -03001948err_ctx_init:
1949 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03001950err_clk_ahb:
Fabio Estevamf82bc202013-08-21 11:14:16 -03001951 clk_disable_unprepare(dev->clk_per);
Fabio Estevam79830db2013-08-21 11:14:17 -03001952err_clk_per:
Philipp Zabel1e172732014-07-11 06:36:23 -03001953 pm_runtime_put_sync(&dev->plat_dev->dev);
1954err_pm_get:
Javier Martin186b2502012-07-26 05:53:35 -03001955 v4l2_fh_del(&ctx->fh);
1956 v4l2_fh_exit(&ctx->fh);
Fabio Estevamf82bc202013-08-21 11:14:16 -03001957 clear_bit(ctx->idx, &dev->instance_mask);
Peter Seiderera7f933a2015-03-24 14:30:47 -03001958err_coda_name_init:
Fabio Estevamf82bc202013-08-21 11:14:16 -03001959err_coda_max:
Javier Martin186b2502012-07-26 05:53:35 -03001960 kfree(ctx);
1961 return ret;
1962}
1963
1964static int coda_release(struct file *file)
1965{
1966 struct coda_dev *dev = video_drvdata(file);
1967 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
1968
1969 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
1970 ctx);
1971
Philipp Zabela22496c2015-01-23 13:51:33 -03001972 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
Philipp Zabeld4bb75f2014-10-08 13:09:11 -03001973 coda_bit_stream_end_flag(ctx);
1974
Philipp Zabel918c66f2013-06-27 06:59:01 -03001975 /* If this instance is running, call .job_abort and wait for it to end */
Philipp Zabel14604e32014-07-11 06:36:22 -03001976 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001977
Michael Trettere7f3c542017-01-20 12:00:24 -02001978 if (ctx->vdoa)
1979 vdoa_context_destroy(ctx->vdoa);
1980
Philipp Zabel918c66f2013-06-27 06:59:01 -03001981 /* In case the instance was not running, we still need to call SEQ_END */
Philipp Zabel5c718bb2015-07-09 07:10:16 -03001982 if (ctx->ops->seq_end_work) {
Philipp Zabel32b6f202014-07-11 06:36:20 -03001983 queue_work(dev->workqueue, &ctx->seq_end_work);
1984 flush_work(&ctx->seq_end_work);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001985 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001986
Javier Martin186b2502012-07-26 05:53:35 -03001987 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001988 list_del(&ctx->list);
Javier Martin186b2502012-07-26 05:53:35 -03001989 coda_unlock(ctx);
1990
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001991 if (ctx->dev->devtype->product == CODA_DX6)
1992 coda_free_aux_buf(dev, &ctx->workbuf);
1993
Javier Martin186b2502012-07-26 05:53:35 -03001994 v4l2_ctrl_handler_free(&ctx->ctrls);
Javier Martin186b2502012-07-26 05:53:35 -03001995 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevamf82bc202013-08-21 11:14:16 -03001996 clk_disable_unprepare(dev->clk_per);
Philipp Zabel1e172732014-07-11 06:36:23 -03001997 pm_runtime_put_sync(&dev->plat_dev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03001998 v4l2_fh_del(&ctx->fh);
1999 v4l2_fh_exit(&ctx->fh);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002000 clear_bit(ctx->idx, &dev->instance_mask);
Philipp Zabel58b76772014-07-23 12:28:43 -03002001 if (ctx->ops->release)
2002 ctx->ops->release(ctx);
Philipp Zabele1519e82015-01-23 13:51:17 -03002003 debugfs_remove_recursive(ctx->debugfs_entry);
Javier Martin186b2502012-07-26 05:53:35 -03002004 kfree(ctx);
2005
2006 return 0;
2007}
2008
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002009static const struct v4l2_file_operations coda_fops = {
Javier Martin186b2502012-07-26 05:53:35 -03002010 .owner = THIS_MODULE,
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002011 .open = coda_open,
Javier Martin186b2502012-07-26 05:53:35 -03002012 .release = coda_release,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002013 .poll = v4l2_m2m_fop_poll,
Javier Martin186b2502012-07-26 05:53:35 -03002014 .unlocked_ioctl = video_ioctl2,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002015 .mmap = v4l2_m2m_fop_mmap,
Javier Martin186b2502012-07-26 05:53:35 -03002016};
2017
Philipp Zabel87048bb2012-07-02 07:03:43 -03002018static int coda_hw_init(struct coda_dev *dev)
Javier Martin186b2502012-07-26 05:53:35 -03002019{
Javier Martin186b2502012-07-26 05:53:35 -03002020 u32 data;
2021 u16 *p;
Fabio Estevam79830db2013-08-21 11:14:17 -03002022 int i, ret;
Javier Martin186b2502012-07-26 05:53:35 -03002023
Fabio Estevam79830db2013-08-21 11:14:17 -03002024 ret = clk_prepare_enable(dev->clk_per);
2025 if (ret)
Philipp Zabel1e172732014-07-11 06:36:23 -03002026 goto err_clk_per;
Fabio Estevam79830db2013-08-21 11:14:17 -03002027
2028 ret = clk_prepare_enable(dev->clk_ahb);
2029 if (ret)
2030 goto err_clk_ahb;
Javier Martin186b2502012-07-26 05:53:35 -03002031
Philipp Zabel8f452842014-07-11 06:36:35 -03002032 if (dev->rstc)
2033 reset_control_reset(dev->rstc);
2034
Javier Martin186b2502012-07-26 05:53:35 -03002035 /*
2036 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
Philipp Zabel87048bb2012-07-02 07:03:43 -03002037 * The 16-bit chars in the code buffer are in memory access
2038 * order, re-sort them to CODA order for register download.
Javier Martin186b2502012-07-26 05:53:35 -03002039 * Data in this SRAM survives a reboot.
2040 */
Philipp Zabel87048bb2012-07-02 07:03:43 -03002041 p = (u16 *)dev->codebuf.vaddr;
2042 if (dev->devtype->product == CODA_DX6) {
2043 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
2044 data = CODA_DOWN_ADDRESS_SET(i) |
2045 CODA_DOWN_DATA_SET(p[i ^ 1]);
2046 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
2047 }
2048 } else {
2049 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
2050 data = CODA_DOWN_ADDRESS_SET(i) |
2051 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
2052 3 - (i % 4)]);
2053 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
2054 }
Javier Martin186b2502012-07-26 05:53:35 -03002055 }
Javier Martin186b2502012-07-26 05:53:35 -03002056
Philipp Zabel9acf7692013-05-23 10:42:56 -03002057 /* Clear registers */
2058 for (i = 0; i < 64; i++)
2059 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
2060
Javier Martin186b2502012-07-26 05:53:35 -03002061 /* Tell the BIT where to find everything it needs */
Philipp Zabel89548442014-07-11 06:36:17 -03002062 if (dev->devtype->product == CODA_960 ||
2063 dev->devtype->product == CODA_7541) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002064 coda_write(dev, dev->tempbuf.paddr,
2065 CODA_REG_BIT_TEMP_BUF_ADDR);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002066 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002067 } else {
2068 coda_write(dev, dev->workbuf.paddr,
2069 CODA_REG_BIT_WORK_BUF_ADDR);
2070 }
Javier Martin186b2502012-07-26 05:53:35 -03002071 coda_write(dev, dev->codebuf.paddr,
2072 CODA_REG_BIT_CODE_BUF_ADDR);
2073 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
2074
2075 /* Set default values */
2076 switch (dev->devtype->product) {
2077 case CODA_DX6:
Philipp Zabelf23797b2014-08-06 08:02:23 -03002078 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH,
2079 CODA_REG_BIT_STREAM_CTRL);
Javier Martin186b2502012-07-26 05:53:35 -03002080 break;
2081 default:
Philipp Zabelf23797b2014-08-06 08:02:23 -03002082 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH,
2083 CODA_REG_BIT_STREAM_CTRL);
Javier Martin186b2502012-07-26 05:53:35 -03002084 }
Philipp Zabel89548442014-07-11 06:36:17 -03002085 if (dev->devtype->product == CODA_960)
2086 coda_write(dev, 1 << 12, CODA_REG_BIT_FRAME_MEM_CTRL);
2087 else
2088 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel10436672012-07-02 09:03:55 -03002089
2090 if (dev->devtype->product != CODA_DX6)
2091 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
2092
Javier Martin186b2502012-07-26 05:53:35 -03002093 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
2094 CODA_REG_BIT_INT_ENABLE);
2095
2096 /* Reset VPU and start processor */
2097 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
2098 data |= CODA_REG_RESET_ENABLE;
2099 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
2100 udelay(10);
2101 data &= ~CODA_REG_RESET_ENABLE;
2102 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
2103 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
2104
Philipp Zabelfe7554e2014-07-11 06:36:24 -03002105 clk_disable_unprepare(dev->clk_ahb);
2106 clk_disable_unprepare(dev->clk_per);
2107
2108 return 0;
2109
2110err_clk_ahb:
2111 clk_disable_unprepare(dev->clk_per);
2112err_clk_per:
2113 return ret;
2114}
2115
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002116static int coda_register_device(struct coda_dev *dev, int i)
Philipp Zabel121cacf2014-07-18 07:22:42 -03002117{
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002118 struct video_device *vfd = &dev->vfd[i];
2119
Dan Carpenter88ca3af2015-01-08 07:07:08 -03002120 if (i >= dev->devtype->num_vdevs)
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002121 return -EINVAL;
2122
Philipp Zabelbf249da2015-03-24 14:30:50 -03002123 strlcpy(vfd->name, dev->devtype->vdevs[i]->name, sizeof(vfd->name));
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002124 vfd->fops = &coda_fops;
2125 vfd->ioctl_ops = &coda_ioctl_ops;
Philipp Zabel121cacf2014-07-18 07:22:42 -03002126 vfd->release = video_device_release_empty,
2127 vfd->lock = &dev->dev_mutex;
2128 vfd->v4l2_dev = &dev->v4l2_dev;
2129 vfd->vfl_dir = VFL_DIR_M2M;
2130 video_set_drvdata(vfd, dev);
2131
Philipp Zabela188a662014-08-05 14:00:20 -03002132 /* Not applicable, use the selection API instead */
2133 v4l2_disable_ioctl(vfd, VIDIOC_CROPCAP);
2134 v4l2_disable_ioctl(vfd, VIDIOC_G_CROP);
2135 v4l2_disable_ioctl(vfd, VIDIOC_S_CROP);
2136
Philipp Zabel121cacf2014-07-18 07:22:42 -03002137 return video_register_device(vfd, VFL_TYPE_GRABBER, 0);
2138}
2139
Philipp Zabela1a87fa2016-02-17 11:21:10 -02002140static void coda_copy_firmware(struct coda_dev *dev, const u8 * const buf,
2141 size_t size)
2142{
2143 u32 *src = (u32 *)buf;
2144
2145 /* Check if the firmware has a 16-byte Freescale header, skip it */
2146 if (buf[0] == 'M' && buf[1] == 'X')
2147 src += 4;
2148 /*
2149 * Check whether the firmware is in native order or pre-reordered for
2150 * memory access. The first instruction opcode always is 0xe40e.
2151 */
2152 if (__le16_to_cpup((__le16 *)src) == 0xe40e) {
2153 u32 *dst = dev->codebuf.vaddr;
2154 int i;
2155
2156 /* Firmware in native order, reorder while copying */
2157 if (dev->devtype->product == CODA_DX6) {
2158 for (i = 0; i < (size - 16) / 4; i++)
2159 dst[i] = (src[i] << 16) | (src[i] >> 16);
2160 } else {
2161 for (i = 0; i < (size - 16) / 4; i += 2) {
2162 dst[i] = (src[i + 1] << 16) | (src[i + 1] >> 16);
2163 dst[i + 1] = (src[i] << 16) | (src[i] >> 16);
2164 }
2165 }
2166 } else {
2167 /* Copy the already reordered firmware image */
2168 memcpy(dev->codebuf.vaddr, src, size);
2169 }
2170}
2171
Philipp Zabel2ac7f082016-02-19 07:18:57 -02002172static void coda_fw_callback(const struct firmware *fw, void *context);
2173
2174static int coda_firmware_request(struct coda_dev *dev)
2175{
2176 char *fw = dev->devtype->firmware[dev->firmware];
2177
2178 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
2179 coda_product_name(dev->devtype->product));
2180
2181 return request_firmware_nowait(THIS_MODULE, true, fw,
2182 &dev->plat_dev->dev, GFP_KERNEL, dev,
2183 coda_fw_callback);
2184}
2185
Javier Martin186b2502012-07-26 05:53:35 -03002186static void coda_fw_callback(const struct firmware *fw, void *context)
2187{
2188 struct coda_dev *dev = context;
2189 struct platform_device *pdev = dev->plat_dev;
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002190 int i, ret;
Javier Martin186b2502012-07-26 05:53:35 -03002191
Philipp Zabel2ac7f082016-02-19 07:18:57 -02002192 if (!fw && dev->firmware == 1) {
Javier Martin186b2502012-07-26 05:53:35 -03002193 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03002194 goto put_pm;
Javier Martin186b2502012-07-26 05:53:35 -03002195 }
Philipp Zabel2ac7f082016-02-19 07:18:57 -02002196 if (!fw) {
2197 dev->firmware = 1;
2198 coda_firmware_request(dev);
2199 return;
2200 }
2201 if (dev->firmware == 1) {
2202 /*
2203 * Since we can't suppress warnings for failed asynchronous
2204 * firmware requests, report that the fallback firmware was
2205 * found.
2206 */
2207 dev_info(&pdev->dev, "Using fallback firmware %s\n",
2208 dev->devtype->firmware[dev->firmware]);
2209 }
Javier Martin186b2502012-07-26 05:53:35 -03002210
2211 /* allocate auxiliary per-device code buffer for the BIT processor */
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002212 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size, "codebuf",
2213 dev->debugfs_root);
Philipp Zabel6ba53b82015-03-24 14:30:54 -03002214 if (ret < 0)
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03002215 goto put_pm;
Javier Martin186b2502012-07-26 05:53:35 -03002216
Philipp Zabela1a87fa2016-02-17 11:21:10 -02002217 coda_copy_firmware(dev, fw->data, fw->size);
Philipp Zabel87048bb2012-07-02 07:03:43 -03002218 release_firmware(fw);
2219
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03002220 ret = coda_hw_init(dev);
2221 if (ret < 0) {
2222 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
2223 goto put_pm;
Javier Martin186b2502012-07-26 05:53:35 -03002224 }
2225
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03002226 ret = coda_check_firmware(dev);
2227 if (ret < 0)
2228 goto put_pm;
2229
Javier Martin186b2502012-07-26 05:53:35 -03002230 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
2231 if (IS_ERR(dev->m2m_dev)) {
2232 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
Hans Verkuil53ddcc62016-02-15 13:09:10 -02002233 goto put_pm;
Javier Martin186b2502012-07-26 05:53:35 -03002234 }
2235
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002236 for (i = 0; i < dev->devtype->num_vdevs; i++) {
2237 ret = coda_register_device(dev, i);
2238 if (ret) {
2239 v4l2_err(&dev->v4l2_dev,
2240 "Failed to register %s video device: %d\n",
2241 dev->devtype->vdevs[i]->name, ret);
2242 goto rel_vfd;
2243 }
Philipp Zabel121cacf2014-07-18 07:22:42 -03002244 }
2245
2246 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video[%d-%d]\n",
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002247 dev->vfd[0].num, dev->vfd[i - 1].num);
Javier Martin186b2502012-07-26 05:53:35 -03002248
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03002249 pm_runtime_put_sync(&pdev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03002250 return;
2251
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002252rel_vfd:
2253 while (--i >= 0)
2254 video_unregister_device(&dev->vfd[i]);
Javier Martin186b2502012-07-26 05:53:35 -03002255 v4l2_m2m_release(dev->m2m_dev);
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03002256put_pm:
2257 pm_runtime_put_sync(&pdev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03002258}
2259
Javier Martin186b2502012-07-26 05:53:35 -03002260enum coda_platform {
2261 CODA_IMX27,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03002262 CODA_IMX53,
Philipp Zabel89548442014-07-11 06:36:17 -03002263 CODA_IMX6Q,
2264 CODA_IMX6DL,
Javier Martin186b2502012-07-26 05:53:35 -03002265};
2266
Emil Goodec06d8752012-08-14 17:44:42 -03002267static const struct coda_devtype coda_devdata[] = {
Javier Martin186b2502012-07-26 05:53:35 -03002268 [CODA_IMX27] = {
Philipp Zabel2ac7f082016-02-19 07:18:57 -02002269 .firmware = {
2270 "vpu_fw_imx27_TO2.bin",
Baruch Siach8af77792017-01-15 08:33:53 -02002271 "vpu/vpu_fw_imx27_TO2.bin",
Philipp Zabel2ac7f082016-02-19 07:18:57 -02002272 "v4l-codadx6-imx27.bin"
2273 },
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002274 .product = CODA_DX6,
2275 .codecs = codadx6_codecs,
2276 .num_codecs = ARRAY_SIZE(codadx6_codecs),
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002277 .vdevs = codadx6_video_devices,
2278 .num_vdevs = ARRAY_SIZE(codadx6_video_devices),
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002279 .workbuf_size = 288 * 1024 + FMO_SLICE_SAVE_BUF_SIZE * 8 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03002280 .iram_size = 0xb000,
Javier Martin186b2502012-07-26 05:53:35 -03002281 },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03002282 [CODA_IMX53] = {
Philipp Zabel2ac7f082016-02-19 07:18:57 -02002283 .firmware = {
2284 "vpu_fw_imx53.bin",
Baruch Siach8af77792017-01-15 08:33:53 -02002285 "vpu/vpu_fw_imx53.bin",
Philipp Zabel2ac7f082016-02-19 07:18:57 -02002286 "v4l-coda7541-imx53.bin"
2287 },
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002288 .product = CODA_7541,
2289 .codecs = coda7_codecs,
2290 .num_codecs = ARRAY_SIZE(coda7_codecs),
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002291 .vdevs = coda7_video_devices,
2292 .num_vdevs = ARRAY_SIZE(coda7_video_devices),
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002293 .workbuf_size = 128 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03002294 .tempbuf_size = 304 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03002295 .iram_size = 0x14000,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03002296 },
Philipp Zabel89548442014-07-11 06:36:17 -03002297 [CODA_IMX6Q] = {
Philipp Zabel2ac7f082016-02-19 07:18:57 -02002298 .firmware = {
2299 "vpu_fw_imx6q.bin",
Baruch Siach8af77792017-01-15 08:33:53 -02002300 "vpu/vpu_fw_imx6q.bin",
Philipp Zabel2ac7f082016-02-19 07:18:57 -02002301 "v4l-coda960-imx6q.bin"
2302 },
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002303 .product = CODA_960,
2304 .codecs = coda9_codecs,
2305 .num_codecs = ARRAY_SIZE(coda9_codecs),
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002306 .vdevs = coda9_video_devices,
2307 .num_vdevs = ARRAY_SIZE(coda9_video_devices),
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002308 .workbuf_size = 80 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03002309 .tempbuf_size = 204 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03002310 .iram_size = 0x21000,
Philipp Zabel89548442014-07-11 06:36:17 -03002311 },
2312 [CODA_IMX6DL] = {
Philipp Zabel2ac7f082016-02-19 07:18:57 -02002313 .firmware = {
2314 "vpu_fw_imx6d.bin",
Baruch Siach8af77792017-01-15 08:33:53 -02002315 "vpu/vpu_fw_imx6d.bin",
Philipp Zabel2ac7f082016-02-19 07:18:57 -02002316 "v4l-coda960-imx6dl.bin"
2317 },
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002318 .product = CODA_960,
2319 .codecs = coda9_codecs,
2320 .num_codecs = ARRAY_SIZE(coda9_codecs),
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002321 .vdevs = coda9_video_devices,
2322 .num_vdevs = ARRAY_SIZE(coda9_video_devices),
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002323 .workbuf_size = 80 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03002324 .tempbuf_size = 204 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03002325 .iram_size = 0x20000,
Philipp Zabel89548442014-07-11 06:36:17 -03002326 },
Javier Martin186b2502012-07-26 05:53:35 -03002327};
2328
2329static struct platform_device_id coda_platform_ids[] = {
2330 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
2331 { /* sentinel */ }
2332};
2333MODULE_DEVICE_TABLE(platform, coda_platform_ids);
2334
2335#ifdef CONFIG_OF
2336static const struct of_device_id coda_dt_ids[] = {
Alexander Shiyan7b0dd9e2013-06-15 08:09:57 -03002337 { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03002338 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
Philipp Zabel89548442014-07-11 06:36:17 -03002339 { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
2340 { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
Javier Martin186b2502012-07-26 05:53:35 -03002341 { /* sentinel */ }
2342};
2343MODULE_DEVICE_TABLE(of, coda_dt_ids);
2344#endif
2345
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08002346static int coda_probe(struct platform_device *pdev)
Javier Martin186b2502012-07-26 05:53:35 -03002347{
2348 const struct of_device_id *of_id =
2349 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
2350 const struct platform_device_id *pdev_id;
Philipp Zabel657eee72013-04-29 16:17:14 -07002351 struct coda_platform_data *pdata = pdev->dev.platform_data;
2352 struct device_node *np = pdev->dev.of_node;
2353 struct gen_pool *pool;
Javier Martin186b2502012-07-26 05:53:35 -03002354 struct coda_dev *dev;
2355 struct resource *res;
2356 int ret, irq;
2357
Philipp Zabelf23797b2014-08-06 08:02:23 -03002358 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
Philipp Zabel6da999d2014-09-29 09:53:43 -03002359 if (!dev)
Javier Martin186b2502012-07-26 05:53:35 -03002360 return -ENOMEM;
Javier Martin186b2502012-07-26 05:53:35 -03002361
Philipp Zabelb2f91ae2014-10-02 14:08:27 -03002362 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
2363
Philipp Zabelbc717d52016-02-26 08:21:35 -03002364 if (of_id)
Philipp Zabelb2f91ae2014-10-02 14:08:27 -03002365 dev->devtype = of_id->data;
Philipp Zabelbc717d52016-02-26 08:21:35 -03002366 else if (pdev_id)
Philipp Zabelb2f91ae2014-10-02 14:08:27 -03002367 dev->devtype = &coda_devdata[pdev_id->driver_data];
Philipp Zabelbc717d52016-02-26 08:21:35 -03002368 else
2369 return -EINVAL;
Javier Martin186b2502012-07-26 05:53:35 -03002370
2371 spin_lock_init(&dev->irqlock);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002372 INIT_LIST_HEAD(&dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03002373
2374 dev->plat_dev = pdev;
2375 dev->clk_per = devm_clk_get(&pdev->dev, "per");
2376 if (IS_ERR(dev->clk_per)) {
2377 dev_err(&pdev->dev, "Could not get per clock\n");
2378 return PTR_ERR(dev->clk_per);
2379 }
2380
2381 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
2382 if (IS_ERR(dev->clk_ahb)) {
2383 dev_err(&pdev->dev, "Could not get ahb clock\n");
2384 return PTR_ERR(dev->clk_ahb);
2385 }
2386
2387 /* Get memory for physical registers */
2388 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Philipp Zabel611cbbf2013-05-21 04:19:27 -03002389 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
2390 if (IS_ERR(dev->regs_base))
2391 return PTR_ERR(dev->regs_base);
Javier Martin186b2502012-07-26 05:53:35 -03002392
2393 /* IRQ */
Philipp Zabel540b72e2014-08-05 14:00:09 -03002394 irq = platform_get_irq_byname(pdev, "bit");
2395 if (irq < 0)
2396 irq = platform_get_irq(pdev, 0);
Javier Martin186b2502012-07-26 05:53:35 -03002397 if (irq < 0) {
2398 dev_err(&pdev->dev, "failed to get irq resource\n");
Fabio Estevam7d377432014-06-04 15:46:23 -03002399 return irq;
Javier Martin186b2502012-07-26 05:53:35 -03002400 }
2401
Fabio Estevam08c8d142014-06-04 15:46:24 -03002402 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
2403 IRQF_ONESHOT, dev_name(&pdev->dev), dev);
2404 if (ret < 0) {
2405 dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
2406 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03002407 }
2408
Philipp Zabelee27aa92014-07-24 16:50:03 -03002409 dev->rstc = devm_reset_control_get_optional(&pdev->dev, NULL);
Philipp Zabel8f452842014-07-11 06:36:35 -03002410 if (IS_ERR(dev->rstc)) {
2411 ret = PTR_ERR(dev->rstc);
Alexander Shiyan84345a22016-06-18 11:45:15 -03002412 if (ret == -ENOENT || ret == -ENOTSUPP) {
Philipp Zabel8f452842014-07-11 06:36:35 -03002413 dev->rstc = NULL;
2414 } else {
Philipp Zabelf23797b2014-08-06 08:02:23 -03002415 dev_err(&pdev->dev, "failed get reset control: %d\n",
2416 ret);
Philipp Zabel8f452842014-07-11 06:36:35 -03002417 return ret;
2418 }
2419 }
2420
Philipp Zabel657eee72013-04-29 16:17:14 -07002421 /* Get IRAM pool from device tree or platform data */
Vladimir Zapolskiyabdd4a72015-06-30 15:00:07 -07002422 pool = of_gen_pool_get(np, "iram", 0);
Philipp Zabel657eee72013-04-29 16:17:14 -07002423 if (!pool && pdata)
Vladimir Zapolskiy73858172015-09-04 15:47:43 -07002424 pool = gen_pool_get(pdata->iram_dev, NULL);
Philipp Zabel657eee72013-04-29 16:17:14 -07002425 if (!pool) {
2426 dev_err(&pdev->dev, "iram pool not available\n");
2427 return -ENOMEM;
2428 }
2429 dev->iram_pool = pool;
2430
Michael Trettere7f3c542017-01-20 12:00:24 -02002431 /* Get vdoa_data if supported by the platform */
2432 dev->vdoa = coda_get_vdoa_data();
2433 if (PTR_ERR(dev->vdoa) == -EPROBE_DEFER)
2434 return -EPROBE_DEFER;
2435
Javier Martin186b2502012-07-26 05:53:35 -03002436 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
2437 if (ret)
2438 return ret;
2439
2440 mutex_init(&dev->dev_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002441 mutex_init(&dev->coda_mutex);
Javier Martin186b2502012-07-26 05:53:35 -03002442
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002443 dev->debugfs_root = debugfs_create_dir("coda", NULL);
2444 if (!dev->debugfs_root)
2445 dev_warn(&pdev->dev, "failed to create debugfs root\n");
2446
Javier Martin186b2502012-07-26 05:53:35 -03002447 /* allocate auxiliary per-device buffers for the BIT processor */
Philipp Zabel5d73fad2014-07-11 06:36:42 -03002448 if (dev->devtype->product == CODA_DX6) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002449 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002450 dev->devtype->workbuf_size, "workbuf",
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002451 dev->debugfs_root);
Philipp Zabel6ba53b82015-03-24 14:30:54 -03002452 if (ret < 0)
Fabio Estevamb7bd6602014-10-04 16:40:50 -03002453 goto err_v4l2_register;
Javier Martin186b2502012-07-26 05:53:35 -03002454 }
Philipp Zabel5d73fad2014-07-11 06:36:42 -03002455
2456 if (dev->devtype->tempbuf_size) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002457 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03002458 dev->devtype->tempbuf_size, "tempbuf",
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002459 dev->debugfs_root);
Philipp Zabel6ba53b82015-03-24 14:30:54 -03002460 if (ret < 0)
Fabio Estevamb7bd6602014-10-04 16:40:50 -03002461 goto err_v4l2_register;
Javier Martin186b2502012-07-26 05:53:35 -03002462 }
2463
Philipp Zabel401e9722014-07-11 06:36:43 -03002464 dev->iram.size = dev->devtype->iram_size;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03002465 dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
2466 &dev->iram.paddr);
2467 if (!dev->iram.vaddr) {
Philipp Zabel8be31c82014-08-05 14:00:13 -03002468 dev_warn(&pdev->dev, "unable to alloc iram\n");
2469 } else {
Philipp Zabel811a6932015-01-23 13:51:23 -03002470 memset(dev->iram.vaddr, 0, dev->iram.size);
Philipp Zabel8be31c82014-08-05 14:00:13 -03002471 dev->iram.blob.data = dev->iram.vaddr;
2472 dev->iram.blob.size = dev->iram.size;
2473 dev->iram.dentry = debugfs_create_blob("iram", 0644,
2474 dev->debugfs_root,
2475 &dev->iram.blob);
Philipp Zabel10436672012-07-02 09:03:55 -03002476 }
2477
Philipp Zabel32b6f202014-07-11 06:36:20 -03002478 dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
2479 if (!dev->workqueue) {
2480 dev_err(&pdev->dev, "unable to alloc workqueue\n");
Fabio Estevam74d08d52014-10-04 16:40:51 -03002481 ret = -ENOMEM;
2482 goto err_v4l2_register;
Philipp Zabel32b6f202014-07-11 06:36:20 -03002483 }
2484
Javier Martin186b2502012-07-26 05:53:35 -03002485 platform_set_drvdata(pdev, dev);
2486
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03002487 /*
2488 * Start activated so we can directly call coda_hw_init in
Rafael J. Wysockie243c7c2014-12-04 01:10:10 +01002489 * coda_fw_callback regardless of whether CONFIG_PM is
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03002490 * enabled or whether the device is associated with a PM domain.
2491 */
2492 pm_runtime_get_noresume(&pdev->dev);
2493 pm_runtime_set_active(&pdev->dev);
Philipp Zabel1e172732014-07-11 06:36:23 -03002494 pm_runtime_enable(&pdev->dev);
2495
Fabio Estevamc762ff12016-10-04 12:41:37 -03002496 ret = coda_firmware_request(dev);
2497 if (ret)
2498 goto err_alloc_workqueue;
2499 return 0;
Fabio Estevamb7bd6602014-10-04 16:40:50 -03002500
Fabio Estevamc762ff12016-10-04 12:41:37 -03002501err_alloc_workqueue:
2502 destroy_workqueue(dev->workqueue);
Fabio Estevamb7bd6602014-10-04 16:40:50 -03002503err_v4l2_register:
2504 v4l2_device_unregister(&dev->v4l2_dev);
2505 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03002506}
2507
2508static int coda_remove(struct platform_device *pdev)
2509{
2510 struct coda_dev *dev = platform_get_drvdata(pdev);
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002511 int i;
Javier Martin186b2502012-07-26 05:53:35 -03002512
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002513 for (i = 0; i < ARRAY_SIZE(dev->vfd); i++) {
2514 if (video_get_drvdata(&dev->vfd[i]))
2515 video_unregister_device(&dev->vfd[i]);
2516 }
Javier Martin186b2502012-07-26 05:53:35 -03002517 if (dev->m2m_dev)
2518 v4l2_m2m_release(dev->m2m_dev);
Philipp Zabel1e172732014-07-11 06:36:23 -03002519 pm_runtime_disable(&pdev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03002520 v4l2_device_unregister(&dev->v4l2_dev);
Philipp Zabel32b6f202014-07-11 06:36:20 -03002521 destroy_workqueue(dev->workqueue);
Philipp Zabelb313bcc2014-07-11 06:36:16 -03002522 if (dev->iram.vaddr)
2523 gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
2524 dev->iram.size);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002525 coda_free_aux_buf(dev, &dev->codebuf);
2526 coda_free_aux_buf(dev, &dev->tempbuf);
2527 coda_free_aux_buf(dev, &dev->workbuf);
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002528 debugfs_remove_recursive(dev->debugfs_root);
Javier Martin186b2502012-07-26 05:53:35 -03002529 return 0;
2530}
2531
Rafael J. Wysockie243c7c2014-12-04 01:10:10 +01002532#ifdef CONFIG_PM
Philipp Zabel1e172732014-07-11 06:36:23 -03002533static int coda_runtime_resume(struct device *dev)
2534{
2535 struct coda_dev *cdev = dev_get_drvdata(dev);
2536 int ret = 0;
2537
Philipp Zabel65919e62014-07-18 07:22:36 -03002538 if (dev->pm_domain && cdev->codebuf.vaddr) {
Philipp Zabel1e172732014-07-11 06:36:23 -03002539 ret = coda_hw_init(cdev);
2540 if (ret)
2541 v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
2542 }
2543
2544 return ret;
2545}
2546#endif
2547
2548static const struct dev_pm_ops coda_pm_ops = {
2549 SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
2550};
2551
Javier Martin186b2502012-07-26 05:53:35 -03002552static struct platform_driver coda_driver = {
2553 .probe = coda_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08002554 .remove = coda_remove,
Javier Martin186b2502012-07-26 05:53:35 -03002555 .driver = {
2556 .name = CODA_NAME,
Javier Martin186b2502012-07-26 05:53:35 -03002557 .of_match_table = of_match_ptr(coda_dt_ids),
Philipp Zabel1e172732014-07-11 06:36:23 -03002558 .pm = &coda_pm_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002559 },
2560 .id_table = coda_platform_ids,
2561};
2562
2563module_platform_driver(coda_driver);
2564
2565MODULE_LICENSE("GPL");
2566MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
2567MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");