]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/media/platform/coda/coda-common.c
treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 152
[mirror_ubuntu-jammy-kernel.git] / drivers / media / platform / coda / coda-common.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
186b250a
JM
2/*
3 * Coda multi-standard codec IP
4 *
5 * Copyright (C) 2012 Vista Silicon S.L.
6 * Javier Martin, <javier.martin@vista-silicon.com>
7 * Xavier Duret
186b250a
JM
8 */
9
10#include <linux/clk.h>
f61c0b14 11#include <linux/debugfs.h>
186b250a
JM
12#include <linux/delay.h>
13#include <linux/firmware.h>
cde29ef3 14#include <linux/gcd.h>
657eee7d 15#include <linux/genalloc.h>
649cfc2b 16#include <linux/idr.h>
186b250a
JM
17#include <linux/interrupt.h>
18#include <linux/io.h>
19#include <linux/irq.h>
9082a7c6 20#include <linux/kfifo.h>
186b250a
JM
21#include <linux/module.h>
22#include <linux/of_device.h>
23#include <linux/platform_device.h>
1e172731 24#include <linux/pm_runtime.h>
186b250a
JM
25#include <linux/slab.h>
26#include <linux/videodev2.h>
27#include <linux/of.h>
a71daaa1 28#include <linux/platform_data/media/coda.h>
8f45284c 29#include <linux/reset.h>
186b250a
JM
30
31#include <media/v4l2-ctrls.h>
32#include <media/v4l2-device.h>
918c66fd 33#include <media/v4l2-event.h>
186b250a
JM
34#include <media/v4l2-ioctl.h>
35#include <media/v4l2-mem2mem.h>
c139990e 36#include <media/videobuf2-v4l2.h>
186b250a 37#include <media/videobuf2-dma-contig.h>
bb04aa6f 38#include <media/videobuf2-vmalloc.h>
186b250a 39
a2b3e46a 40#include "coda.h"
e7f3c548 41#include "imx-vdoa.h"
186b250a
JM
42
43#define CODA_NAME "coda"
44
0cddc7e9 45#define CODADX6_MAX_INSTANCES 4
2c11d1bd 46#define CODA_MAX_FORMATS 4
186b250a 47
186b250a
JM
48#define CODA_ISRAM_SIZE (2048 * 2)
49
074e8db0
PZ
50#define MIN_W 48
51#define MIN_H 16
186b250a
JM
52
53#define S_ALIGN 1 /* multiple of 2 */
54#define W_ALIGN 1 /* multiple of 2 */
55#define H_ALIGN 1 /* multiple of 2 */
56
57#define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh)
58
79924ca9 59int coda_debug;
c4eb1bfc 60module_param(coda_debug, int, 0644);
d60b18ba 61MODULE_PARM_DESC(coda_debug, "Debug level (0-2)");
186b250a 62
a269e53b
PZ
63static int disable_tiling;
64module_param(disable_tiling, int, 0644);
65MODULE_PARM_DESC(disable_tiling, "Disable tiled frame buffers");
b96904e5 66
e7f3c548
MT
67static int disable_vdoa;
68module_param(disable_vdoa, int, 0644);
69MODULE_PARM_DESC(disable_vdoa, "Disable Video Data Order Adapter tiled to raster-scan conversion");
70
89ed025d
PZ
71static int enable_bwb = 0;
72module_param(enable_bwb, int, 0644);
87a420c8 73MODULE_PARM_DESC(enable_bwb, "Enable BWB unit for decoding, may crash on certain streams");
89ed025d 74
a2b3e46a 75void coda_write(struct coda_dev *dev, u32 data, u32 reg)
186b250a 76{
d60b18ba 77 v4l2_dbg(2, coda_debug, &dev->v4l2_dev,
186b250a
JM
78 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
79 writel(data, dev->regs_base + reg);
80}
81
a2b3e46a 82unsigned int coda_read(struct coda_dev *dev, u32 reg)
186b250a
JM
83{
84 u32 data;
f23797b6 85
186b250a 86 data = readl(dev->regs_base + reg);
d60b18ba 87 v4l2_dbg(2, coda_debug, &dev->v4l2_dev,
186b250a
JM
88 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
89 return data;
90}
91
856d7d93 92void coda_write_base(struct coda_ctx *ctx, struct coda_q_data *q_data,
2d700715 93 struct vb2_v4l2_buffer *buf, unsigned int reg_y)
856d7d93 94{
2d700715 95 u32 base_y = vb2_dma_contig_plane_dma_addr(&buf->vb2_buf, 0);
856d7d93
PZ
96 u32 base_cb, base_cr;
97
98 switch (q_data->fourcc) {
d40e98c1
MT
99 case V4L2_PIX_FMT_YUYV:
100 /* Fallthrough: IN -H264-> CODA -NV12 MB-> VDOA -YUYV-> OUT */
1cb12cf3 101 case V4L2_PIX_FMT_NV12:
6727d4fc 102 case V4L2_PIX_FMT_YUV420:
856d7d93
PZ
103 default:
104 base_cb = base_y + q_data->bytesperline * q_data->height;
105 base_cr = base_cb + q_data->bytesperline * q_data->height / 4;
106 break;
6727d4fc
PZ
107 case V4L2_PIX_FMT_YVU420:
108 /* Switch Cb and Cr for YVU420 format */
109 base_cr = base_y + q_data->bytesperline * q_data->height;
110 base_cb = base_cr + q_data->bytesperline * q_data->height / 4;
111 break;
4de69319
PZ
112 case V4L2_PIX_FMT_YUV422P:
113 base_cb = base_y + q_data->bytesperline * q_data->height;
114 base_cr = base_cb + q_data->bytesperline * q_data->height / 2;
856d7d93
PZ
115 }
116
117 coda_write(ctx->dev, base_y, reg_y);
118 coda_write(ctx->dev, base_cb, reg_y + 4);
119 coda_write(ctx->dev, base_cr, reg_y + 8);
120}
121
b96904e5
PZ
122#define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
123 { mode, src_fourcc, dst_fourcc, max_w, max_h }
124
125/*
126 * Arrays of codecs supported by each given version of Coda:
127 * i.MX27 -> codadx6
1d1ca23b
PZ
128 * i.MX51 -> codahx4
129 * i.MX53 -> coda7
b96904e5
PZ
130 * i.MX6 -> coda960
131 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
132 */
814c3762 133static const struct coda_codec codadx6_codecs[] = {
b96904e5
PZ
134 CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
135 CODA_CODEC(CODADX6_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
136};
137
1d1ca23b
PZ
138static const struct coda_codec codahx4_codecs[] = {
139 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
140 CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
141 CODA_CODEC(CODA7_MODE_DECODE_MP2, V4L2_PIX_FMT_MPEG2, V4L2_PIX_FMT_YUV420, 1920, 1088),
142 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1280, 720),
143};
144
814c3762 145static const struct coda_codec coda7_codecs[] = {
b96904e5
PZ
146 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720),
147 CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720),
cb1d3a33 148 CODA_CODEC(CODA7_MODE_ENCODE_MJPG, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_JPEG, 8192, 8192),
b0ed05b9 149 CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
95847f40 150 CODA_CODEC(CODA7_MODE_DECODE_MP2, V4L2_PIX_FMT_MPEG2, V4L2_PIX_FMT_YUV420, 1920, 1088),
b0ed05b9 151 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088),
cb1d3a33 152 CODA_CODEC(CODA7_MODE_DECODE_MJPG, V4L2_PIX_FMT_JPEG, V4L2_PIX_FMT_YUV420, 8192, 8192),
b96904e5
PZ
153};
154
814c3762 155static const struct coda_codec coda9_codecs[] = {
b0ed05b9
PZ
156 CODA_CODEC(CODA9_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1920, 1088),
157 CODA_CODEC(CODA9_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1920, 1088),
158 CODA_CODEC(CODA9_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
95847f40 159 CODA_CODEC(CODA9_MODE_DECODE_MP2, V4L2_PIX_FMT_MPEG2, V4L2_PIX_FMT_YUV420, 1920, 1088),
b0ed05b9 160 CODA_CODEC(CODA9_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088),
89548441
PZ
161};
162
2c11d1bd
PZ
163struct coda_video_device {
164 const char *name;
165 enum coda_inst_type type;
166 const struct coda_context_ops *ops;
a22496c6 167 bool direct;
2c11d1bd
PZ
168 u32 src_formats[CODA_MAX_FORMATS];
169 u32 dst_formats[CODA_MAX_FORMATS];
170};
171
172static const struct coda_video_device coda_bit_encoder = {
173 .name = "coda-encoder",
174 .type = CODA_INST_ENCODER,
175 .ops = &coda_bit_encode_ops,
176 .src_formats = {
6727d4fc 177 V4L2_PIX_FMT_NV12,
2c11d1bd
PZ
178 V4L2_PIX_FMT_YUV420,
179 V4L2_PIX_FMT_YVU420,
2c11d1bd
PZ
180 },
181 .dst_formats = {
182 V4L2_PIX_FMT_H264,
183 V4L2_PIX_FMT_MPEG4,
184 },
185};
186
cb1d3a33
PZ
187static const struct coda_video_device coda_bit_jpeg_encoder = {
188 .name = "coda-jpeg-encoder",
189 .type = CODA_INST_ENCODER,
190 .ops = &coda_bit_encode_ops,
191 .src_formats = {
6727d4fc 192 V4L2_PIX_FMT_NV12,
cb1d3a33
PZ
193 V4L2_PIX_FMT_YUV420,
194 V4L2_PIX_FMT_YVU420,
cb1d3a33
PZ
195 V4L2_PIX_FMT_YUV422P,
196 },
197 .dst_formats = {
198 V4L2_PIX_FMT_JPEG,
199 },
200};
201
2c11d1bd
PZ
202static const struct coda_video_device coda_bit_decoder = {
203 .name = "coda-decoder",
204 .type = CODA_INST_DECODER,
205 .ops = &coda_bit_decode_ops,
206 .src_formats = {
207 V4L2_PIX_FMT_H264,
95847f40 208 V4L2_PIX_FMT_MPEG2,
2c11d1bd
PZ
209 V4L2_PIX_FMT_MPEG4,
210 },
211 .dst_formats = {
6727d4fc 212 V4L2_PIX_FMT_NV12,
2c11d1bd
PZ
213 V4L2_PIX_FMT_YUV420,
214 V4L2_PIX_FMT_YVU420,
d40e98c1
MT
215 /*
216 * If V4L2_PIX_FMT_YUYV should be default,
217 * set_default_params() must be adjusted.
218 */
219 V4L2_PIX_FMT_YUYV,
2c11d1bd
PZ
220 },
221};
222
cb1d3a33
PZ
223static const struct coda_video_device coda_bit_jpeg_decoder = {
224 .name = "coda-jpeg-decoder",
225 .type = CODA_INST_DECODER,
226 .ops = &coda_bit_decode_ops,
227 .src_formats = {
228 V4L2_PIX_FMT_JPEG,
229 },
230 .dst_formats = {
6727d4fc 231 V4L2_PIX_FMT_NV12,
cb1d3a33
PZ
232 V4L2_PIX_FMT_YUV420,
233 V4L2_PIX_FMT_YVU420,
cb1d3a33
PZ
234 V4L2_PIX_FMT_YUV422P,
235 },
236};
237
2c11d1bd
PZ
238static const struct coda_video_device *codadx6_video_devices[] = {
239 &coda_bit_encoder,
240};
241
1d1ca23b
PZ
242static const struct coda_video_device *codahx4_video_devices[] = {
243 &coda_bit_encoder,
244 &coda_bit_decoder,
245};
246
2c11d1bd 247static const struct coda_video_device *coda7_video_devices[] = {
cb1d3a33
PZ
248 &coda_bit_jpeg_encoder,
249 &coda_bit_jpeg_decoder,
2c11d1bd
PZ
250 &coda_bit_encoder,
251 &coda_bit_decoder,
252};
253
254static const struct coda_video_device *coda9_video_devices[] = {
255 &coda_bit_encoder,
256 &coda_bit_decoder,
257};
258
8076c7e3
PZ
259/*
260 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
261 * tables.
262 */
263static u32 coda_format_normalize_yuv(u32 fourcc)
b96904e5
PZ
264{
265 switch (fourcc) {
6727d4fc 266 case V4L2_PIX_FMT_NV12:
b96904e5
PZ
267 case V4L2_PIX_FMT_YUV420:
268 case V4L2_PIX_FMT_YVU420:
4de69319 269 case V4L2_PIX_FMT_YUV422P:
d40e98c1 270 case V4L2_PIX_FMT_YUYV:
8076c7e3 271 return V4L2_PIX_FMT_YUV420;
b96904e5 272 default:
8076c7e3 273 return fourcc;
2c11d1bd 274 }
b96904e5
PZ
275}
276
814c3762
PZ
277static const struct coda_codec *coda_find_codec(struct coda_dev *dev,
278 int src_fourcc, int dst_fourcc)
186b250a 279{
814c3762 280 const struct coda_codec *codecs = dev->devtype->codecs;
b96904e5
PZ
281 int num_codecs = dev->devtype->num_codecs;
282 int k;
283
284 src_fourcc = coda_format_normalize_yuv(src_fourcc);
285 dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
286 if (src_fourcc == dst_fourcc)
287 return NULL;
186b250a 288
b96904e5
PZ
289 for (k = 0; k < num_codecs; k++) {
290 if (codecs[k].src_fourcc == src_fourcc &&
291 codecs[k].dst_fourcc == dst_fourcc)
186b250a
JM
292 break;
293 }
294
b96904e5 295 if (k == num_codecs)
186b250a
JM
296 return NULL;
297
b96904e5 298 return &codecs[k];
186b250a
JM
299}
300
57625593 301static void coda_get_max_dimensions(struct coda_dev *dev,
814c3762 302 const struct coda_codec *codec,
57625593
PZ
303 int *max_w, int *max_h)
304{
814c3762 305 const struct coda_codec *codecs = dev->devtype->codecs;
57625593
PZ
306 int num_codecs = dev->devtype->num_codecs;
307 unsigned int w, h;
308 int k;
309
310 if (codec) {
311 w = codec->max_w;
312 h = codec->max_h;
313 } else {
314 for (k = 0, w = 0, h = 0; k < num_codecs; k++) {
315 w = max(w, codecs[k].max_w);
316 h = max(h, codecs[k].max_h);
317 }
318 }
319
320 if (max_w)
321 *max_w = w;
322 if (max_h)
323 *max_h = h;
324}
325
5542570e
PZ
326static const struct coda_video_device *to_coda_video_device(struct video_device
327 *vdev)
2c11d1bd
PZ
328{
329 struct coda_dev *dev = video_get_drvdata(vdev);
330 unsigned int i = vdev - dev->vfd;
331
332 if (i >= dev->devtype->num_vdevs)
333 return NULL;
334
335 return dev->devtype->vdevs[i];
336}
337
79924ca9 338const char *coda_product_name(int product)
927933f7
PZ
339{
340 static char buf[9];
341
342 switch (product) {
343 case CODA_DX6:
344 return "CodaDx6";
1d1ca23b
PZ
345 case CODA_HX4:
346 return "CodaHx4";
927933f7
PZ
347 case CODA_7541:
348 return "CODA7541";
89548441
PZ
349 case CODA_960:
350 return "CODA960";
927933f7
PZ
351 default:
352 snprintf(buf, sizeof(buf), "(0x%04x)", product);
353 return buf;
354 }
355}
356
e7f3c548
MT
357static struct vdoa_data *coda_get_vdoa_data(void)
358{
359 struct device_node *vdoa_node;
360 struct platform_device *vdoa_pdev;
361 struct vdoa_data *vdoa_data = NULL;
362
363 vdoa_node = of_find_compatible_node(NULL, NULL, "fsl,imx6q-vdoa");
364 if (!vdoa_node)
365 return NULL;
366
367 vdoa_pdev = of_find_device_by_node(vdoa_node);
368 if (!vdoa_pdev)
369 goto out;
370
371 vdoa_data = platform_get_drvdata(vdoa_pdev);
372 if (!vdoa_data)
373 vdoa_data = ERR_PTR(-EPROBE_DEFER);
374
375out:
94a426c5 376 of_node_put(vdoa_node);
e7f3c548
MT
377
378 return vdoa_data;
379}
380
186b250a
JM
381/*
382 * V4L2 ioctl() operations.
383 */
2e9e4f11
PZ
384static int coda_querycap(struct file *file, void *priv,
385 struct v4l2_capability *cap)
186b250a 386{
927933f7
PZ
387 struct coda_ctx *ctx = fh_to_ctx(priv);
388
c0decac1
MCC
389 strscpy(cap->driver, CODA_NAME, sizeof(cap->driver));
390 strscpy(cap->card, coda_product_name(ctx->dev->devtype->product),
927933f7 391 sizeof(cap->card));
c0decac1 392 strscpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
3898e7a7 393 cap->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
186b250a
JM
394 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
395
396 return 0;
397}
398
22e244b8
PZ
399static int coda_enum_fmt(struct file *file, void *priv,
400 struct v4l2_fmtdesc *f)
186b250a 401{
2c11d1bd
PZ
402 struct video_device *vdev = video_devdata(file);
403 const struct coda_video_device *cvd = to_coda_video_device(vdev);
9d826ce8 404 struct coda_ctx *ctx = fh_to_ctx(priv);
2c11d1bd 405 const u32 *formats;
2c11d1bd
PZ
406
407 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
408 formats = cvd->src_formats;
409 else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
410 formats = cvd->dst_formats;
22e244b8 411 else
2c11d1bd 412 return -EINVAL;
186b250a 413
2c11d1bd
PZ
414 if (f->index >= CODA_MAX_FORMATS || formats[f->index] == 0)
415 return -EINVAL;
416
9d826ce8
PZ
417 /* Skip YUYV if the vdoa is not available */
418 if (!ctx->vdoa && f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
419 formats[f->index] == V4L2_PIX_FMT_YUYV)
420 return -EINVAL;
421
2c11d1bd 422 f->pixelformat = formats[f->index];
186b250a 423
2c11d1bd 424 return 0;
186b250a
JM
425}
426
2e9e4f11
PZ
427static int coda_g_fmt(struct file *file, void *priv,
428 struct v4l2_format *f)
186b250a 429{
186b250a
JM
430 struct coda_q_data *q_data;
431 struct coda_ctx *ctx = fh_to_ctx(priv);
432
186b250a 433 q_data = get_q_data(ctx, f->type);
b9736299
PZ
434 if (!q_data)
435 return -EINVAL;
186b250a
JM
436
437 f->fmt.pix.field = V4L2_FIELD_NONE;
b96904e5 438 f->fmt.pix.pixelformat = q_data->fourcc;
186b250a
JM
439 f->fmt.pix.width = q_data->width;
440 f->fmt.pix.height = q_data->height;
2fd6a370 441 f->fmt.pix.bytesperline = q_data->bytesperline;
186b250a
JM
442
443 f->fmt.pix.sizeimage = q_data->sizeimage;
b14ac545
PZ
444 f->fmt.pix.colorspace = ctx->colorspace;
445 f->fmt.pix.xfer_func = ctx->xfer_func;
446 f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
447 f->fmt.pix.quantization = ctx->quantization;
186b250a
JM
448
449 return 0;
450}
451
2c11d1bd
PZ
452static int coda_try_pixelformat(struct coda_ctx *ctx, struct v4l2_format *f)
453{
454 struct coda_q_data *q_data;
455 const u32 *formats;
456 int i;
457
458 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
459 formats = ctx->cvd->src_formats;
460 else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
461 formats = ctx->cvd->dst_formats;
462 else
463 return -EINVAL;
464
465 for (i = 0; i < CODA_MAX_FORMATS; i++) {
d40e98c1
MT
466 /* Skip YUYV if the vdoa is not available */
467 if (!ctx->vdoa && f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
468 formats[i] == V4L2_PIX_FMT_YUYV)
469 continue;
470
2c11d1bd
PZ
471 if (formats[i] == f->fmt.pix.pixelformat) {
472 f->fmt.pix.pixelformat = formats[i];
473 return 0;
474 }
475 }
476
477 /* Fall back to currently set pixelformat */
478 q_data = get_q_data(ctx, f->type);
479 f->fmt.pix.pixelformat = q_data->fourcc;
186b250a
JM
480
481 return 0;
482}
483
e7f3c548
MT
484static int coda_try_fmt_vdoa(struct coda_ctx *ctx, struct v4l2_format *f,
485 bool *use_vdoa)
486{
487 int err;
488
489 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
490 return -EINVAL;
491
492 if (!use_vdoa)
493 return -EINVAL;
494
495 if (!ctx->vdoa) {
496 *use_vdoa = false;
497 return 0;
498 }
499
1403bc4c
PZ
500 err = vdoa_context_configure(NULL, round_up(f->fmt.pix.width, 16),
501 f->fmt.pix.height, f->fmt.pix.pixelformat);
e7f3c548
MT
502 if (err) {
503 *use_vdoa = false;
504 return 0;
505 }
506
507 *use_vdoa = true;
508 return 0;
509}
510
8e428d50
PZ
511static unsigned int coda_estimate_sizeimage(struct coda_ctx *ctx, u32 sizeimage,
512 u32 width, u32 height)
513{
514 /*
515 * This is a rough estimate for sensible compressed buffer
516 * sizes (between 1 and 16 bits per pixel). This could be
517 * improved by better format specific worst case estimates.
518 */
519 return round_up(clamp(sizeimage, width * height / 8,
520 width * height * 2), PAGE_SIZE);
521}
522
814c3762 523static int coda_try_fmt(struct coda_ctx *ctx, const struct coda_codec *codec,
57625593 524 struct v4l2_format *f)
186b250a 525{
57625593 526 struct coda_dev *dev = ctx->dev;
b96904e5 527 unsigned int max_w, max_h;
186b250a
JM
528 enum v4l2_field field;
529
530 field = f->fmt.pix.field;
531 if (field == V4L2_FIELD_ANY)
532 field = V4L2_FIELD_NONE;
533 else if (V4L2_FIELD_NONE != field)
534 return -EINVAL;
535
536 /* V4L2 specification suggests the driver corrects the format struct
537 * if any of the dimensions is unsupported */
538 f->fmt.pix.field = field;
539
57625593
PZ
540 coda_get_max_dimensions(dev, codec, &max_w, &max_h);
541 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN,
542 &f->fmt.pix.height, MIN_H, max_h, H_ALIGN,
543 S_ALIGN);
544
545 switch (f->fmt.pix.pixelformat) {
6727d4fc 546 case V4L2_PIX_FMT_NV12:
57625593
PZ
547 case V4L2_PIX_FMT_YUV420:
548 case V4L2_PIX_FMT_YVU420:
cb1d3a33
PZ
549 /*
550 * Frame stride must be at least multiple of 8,
551 * but multiple of 16 for h.264 or JPEG 4:2:x
552 */
451dfe5d 553 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
47cf0c61 554 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
451d43ad 555 f->fmt.pix.height * 3 / 2;
57625593 556 break;
d40e98c1
MT
557 case V4L2_PIX_FMT_YUYV:
558 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16) * 2;
559 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
560 f->fmt.pix.height;
561 break;
4de69319
PZ
562 case V4L2_PIX_FMT_YUV422P:
563 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
564 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
565 f->fmt.pix.height * 2;
566 break;
cb1d3a33 567 case V4L2_PIX_FMT_JPEG:
57625593
PZ
568 case V4L2_PIX_FMT_H264:
569 case V4L2_PIX_FMT_MPEG4:
95847f40 570 case V4L2_PIX_FMT_MPEG2:
186b250a 571 f->fmt.pix.bytesperline = 0;
8e428d50
PZ
572 f->fmt.pix.sizeimage = coda_estimate_sizeimage(ctx,
573 f->fmt.pix.sizeimage,
574 f->fmt.pix.width,
575 f->fmt.pix.height);
57625593
PZ
576 break;
577 default:
578 BUG();
186b250a
JM
579 }
580
581 return 0;
582}
583
2e9e4f11
PZ
584static int coda_try_fmt_vid_cap(struct file *file, void *priv,
585 struct v4l2_format *f)
186b250a 586{
186b250a 587 struct coda_ctx *ctx = fh_to_ctx(priv);
2c11d1bd
PZ
588 const struct coda_q_data *q_data_src;
589 const struct coda_codec *codec;
918c66fd
PZ
590 struct vb2_queue *src_vq;
591 int ret;
e7f3c548 592 bool use_vdoa;
918c66fd 593
2c11d1bd
PZ
594 ret = coda_try_pixelformat(ctx, f);
595 if (ret < 0)
596 return ret;
597
598 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
599
918c66fd 600 /*
2c11d1bd
PZ
601 * If the source format is already fixed, only allow the same output
602 * resolution
918c66fd 603 */
14604e3a 604 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
918c66fd 605 if (vb2_is_streaming(src_vq)) {
91b5841e
PZ
606 f->fmt.pix.width = q_data_src->width;
607 f->fmt.pix.height = q_data_src->height;
918c66fd 608 }
186b250a
JM
609
610 f->fmt.pix.colorspace = ctx->colorspace;
b14ac545
PZ
611 f->fmt.pix.xfer_func = ctx->xfer_func;
612 f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
613 f->fmt.pix.quantization = ctx->quantization;
186b250a 614
2c11d1bd
PZ
615 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
616 codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
617 f->fmt.pix.pixelformat);
618 if (!codec)
619 return -EINVAL;
620
57625593 621 ret = coda_try_fmt(ctx, codec, f);
918c66fd
PZ
622 if (ret < 0)
623 return ret;
624
625 /* The h.264 decoder only returns complete 16x16 macroblocks */
626 if (codec && codec->src_fourcc == V4L2_PIX_FMT_H264) {
918c66fd 627 f->fmt.pix.height = round_up(f->fmt.pix.height, 16);
1b0ed7be 628 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
918c66fd
PZ
629 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
630 f->fmt.pix.height * 3 / 2;
e7f3c548
MT
631
632 ret = coda_try_fmt_vdoa(ctx, f, &use_vdoa);
633 if (ret < 0)
634 return ret;
d40e98c1
MT
635
636 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV) {
637 if (!use_vdoa)
638 return -EINVAL;
639
640 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16) * 2;
641 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
642 f->fmt.pix.height;
643 }
918c66fd
PZ
644 }
645
646 return 0;
186b250a
JM
647}
648
b14ac545
PZ
649static void coda_set_default_colorspace(struct v4l2_pix_format *fmt)
650{
651 enum v4l2_colorspace colorspace;
652
653 if (fmt->pixelformat == V4L2_PIX_FMT_JPEG)
654 colorspace = V4L2_COLORSPACE_JPEG;
655 else if (fmt->width <= 720 && fmt->height <= 576)
656 colorspace = V4L2_COLORSPACE_SMPTE170M;
657 else
658 colorspace = V4L2_COLORSPACE_REC709;
659
660 fmt->colorspace = colorspace;
661 fmt->xfer_func = V4L2_XFER_FUNC_DEFAULT;
662 fmt->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
663 fmt->quantization = V4L2_QUANTIZATION_DEFAULT;
664}
665
2e9e4f11
PZ
666static int coda_try_fmt_vid_out(struct file *file, void *priv,
667 struct v4l2_format *f)
186b250a
JM
668{
669 struct coda_ctx *ctx = fh_to_ctx(priv);
2c11d1bd
PZ
670 struct coda_dev *dev = ctx->dev;
671 const struct coda_q_data *q_data_dst;
672 const struct coda_codec *codec;
673 int ret;
186b250a 674
2c11d1bd
PZ
675 ret = coda_try_pixelformat(ctx, f);
676 if (ret < 0)
677 return ret;
186b250a 678
b14ac545
PZ
679 if (f->fmt.pix.colorspace == V4L2_COLORSPACE_DEFAULT)
680 coda_set_default_colorspace(&f->fmt.pix);
186b250a 681
2c11d1bd
PZ
682 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
683 codec = coda_find_codec(dev, f->fmt.pix.pixelformat, q_data_dst->fourcc);
186b250a 684
57625593 685 return coda_try_fmt(ctx, codec, f);
186b250a
JM
686}
687
4ec319eb
PZ
688static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f,
689 struct v4l2_rect *r)
186b250a
JM
690{
691 struct coda_q_data *q_data;
692 struct vb2_queue *vq;
186b250a 693
14604e3a 694 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
186b250a
JM
695 if (!vq)
696 return -EINVAL;
697
698 q_data = get_q_data(ctx, f->type);
699 if (!q_data)
700 return -EINVAL;
701
702 if (vb2_is_busy(vq)) {
299cc318
PZ
703 v4l2_err(&ctx->dev->v4l2_dev, "%s: %s queue busy: %d\n",
704 __func__, v4l2_type_names[f->type], vq->num_buffers);
186b250a
JM
705 return -EBUSY;
706 }
707
b96904e5 708 q_data->fourcc = f->fmt.pix.pixelformat;
186b250a
JM
709 q_data->width = f->fmt.pix.width;
710 q_data->height = f->fmt.pix.height;
2fd6a370 711 q_data->bytesperline = f->fmt.pix.bytesperline;
451d43ad 712 q_data->sizeimage = f->fmt.pix.sizeimage;
4ec319eb
PZ
713 if (r) {
714 q_data->rect = *r;
715 } else {
716 q_data->rect.left = 0;
717 q_data->rect.top = 0;
718 q_data->rect.width = f->fmt.pix.width;
719 q_data->rect.height = f->fmt.pix.height;
720 }
186b250a 721
a269e53b 722 switch (f->fmt.pix.pixelformat) {
d40e98c1
MT
723 case V4L2_PIX_FMT_YUYV:
724 ctx->tiled_map_type = GDI_TILED_FRAME_MB_RASTER_MAP;
725 break;
a269e53b 726 case V4L2_PIX_FMT_NV12:
fbf9aa6a 727 if (!disable_tiling && ctx->dev->devtype->product == CODA_960) {
9c2a4559 728 ctx->tiled_map_type = GDI_TILED_FRAME_MB_RASTER_MAP;
e7f3c548 729 break;
9c2a4559 730 }
a269e53b
PZ
731 /* else fall through */
732 case V4L2_PIX_FMT_YUV420:
733 case V4L2_PIX_FMT_YVU420:
734 ctx->tiled_map_type = GDI_LINEAR_FRAME_MAP;
735 break;
736 default:
737 break;
738 }
739
e7f3c548
MT
740 if (ctx->tiled_map_type == GDI_TILED_FRAME_MB_RASTER_MAP &&
741 !coda_try_fmt_vdoa(ctx, f, &ctx->use_vdoa) &&
742 ctx->use_vdoa)
1403bc4c
PZ
743 vdoa_context_configure(ctx->vdoa,
744 round_up(f->fmt.pix.width, 16),
e7f3c548
MT
745 f->fmt.pix.height,
746 f->fmt.pix.pixelformat);
747 else
748 ctx->use_vdoa = false;
749
8f90d15e
PZ
750 coda_dbg(1, ctx, "Setting %s format, wxh: %dx%d, fmt: %4.4s %c\n",
751 v4l2_type_names[f->type], q_data->width, q_data->height,
752 (char *)&q_data->fourcc,
753 (ctx->tiled_map_type == GDI_LINEAR_FRAME_MAP) ? 'L' : 'T');
186b250a
JM
754
755 return 0;
756}
757
2e9e4f11
PZ
758static int coda_s_fmt_vid_cap(struct file *file, void *priv,
759 struct v4l2_format *f)
186b250a 760{
b96904e5 761 struct coda_ctx *ctx = fh_to_ctx(priv);
4ec319eb 762 struct coda_q_data *q_data_src;
75480ebf 763 const struct coda_codec *codec;
4ec319eb 764 struct v4l2_rect r;
186b250a
JM
765 int ret;
766
2e9e4f11 767 ret = coda_try_fmt_vid_cap(file, priv, f);
186b250a
JM
768 if (ret)
769 return ret;
770
4ec319eb
PZ
771 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
772 r.left = 0;
773 r.top = 0;
774 r.width = q_data_src->width;
775 r.height = q_data_src->height;
776
2c756c72
PZ
777 ret = coda_s_fmt(ctx, f, &r);
778 if (ret)
779 return ret;
780
781 if (ctx->inst_type != CODA_INST_ENCODER)
782 return 0;
783
75480ebf
PZ
784 /* Setting the coded format determines the selected codec */
785 codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
786 f->fmt.pix.pixelformat);
787 if (!codec) {
788 v4l2_err(&ctx->dev->v4l2_dev, "failed to determine codec\n");
789 return -EINVAL;
790 }
791 ctx->codec = codec;
792
2c756c72
PZ
793 ctx->colorspace = f->fmt.pix.colorspace;
794 ctx->xfer_func = f->fmt.pix.xfer_func;
795 ctx->ycbcr_enc = f->fmt.pix.ycbcr_enc;
796 ctx->quantization = f->fmt.pix.quantization;
797
798 return 0;
186b250a
JM
799}
800
2e9e4f11
PZ
801static int coda_s_fmt_vid_out(struct file *file, void *priv,
802 struct v4l2_format *f)
186b250a
JM
803{
804 struct coda_ctx *ctx = fh_to_ctx(priv);
75480ebf 805 const struct coda_codec *codec;
f95a6ce1 806 struct v4l2_format f_cap;
0f8529fd 807 struct vb2_queue *dst_vq;
186b250a
JM
808 int ret;
809
2e9e4f11 810 ret = coda_try_fmt_vid_out(file, priv, f);
186b250a
JM
811 if (ret)
812 return ret;
813
4ec319eb 814 ret = coda_s_fmt(ctx, f, NULL);
186b250a 815 if (ret)
2dc546d0
PZ
816 return ret;
817
01ba2e65
PZ
818 ctx->colorspace = f->fmt.pix.colorspace;
819 ctx->xfer_func = f->fmt.pix.xfer_func;
820 ctx->ycbcr_enc = f->fmt.pix.ycbcr_enc;
821 ctx->quantization = f->fmt.pix.quantization;
822
2c756c72
PZ
823 if (ctx->inst_type != CODA_INST_DECODER)
824 return 0;
825
75480ebf
PZ
826 /* Setting the coded format determines the selected codec */
827 codec = coda_find_codec(ctx->dev, f->fmt.pix.pixelformat,
828 V4L2_PIX_FMT_YUV420);
829 if (!codec) {
830 v4l2_err(&ctx->dev->v4l2_dev, "failed to determine codec\n");
831 return -EINVAL;
832 }
833 ctx->codec = codec;
834
0f8529fd
PZ
835 dst_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
836 if (!dst_vq)
837 return -EINVAL;
838
839 /*
840 * Setting the capture queue format is not possible while the capture
841 * queue is still busy. This is not an error, but the user will have to
842 * make sure themselves that the capture format is set correctly before
843 * starting the output queue again.
844 */
845 if (vb2_is_busy(dst_vq))
846 return 0;
847
1c2f6a99 848 memset(&f_cap, 0, sizeof(f_cap));
f95a6ce1
PZ
849 f_cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
850 coda_g_fmt(file, priv, &f_cap);
851 f_cap.fmt.pix.width = f->fmt.pix.width;
852 f_cap.fmt.pix.height = f->fmt.pix.height;
853
6dc8c115 854 return coda_s_fmt_vid_cap(file, priv, &f_cap);
186b250a
JM
855}
856
73751da6
PZ
857static int coda_reqbufs(struct file *file, void *priv,
858 struct v4l2_requestbuffers *rb)
859{
860 struct coda_ctx *ctx = fh_to_ctx(priv);
861 int ret;
862
863 ret = v4l2_m2m_reqbufs(file, ctx->fh.m2m_ctx, rb);
864 if (ret)
865 return ret;
866
867 /*
868 * Allow to allocate instance specific per-context buffers, such as
869 * bitstream ringbuffer, slice buffer, work buffer, etc. if needed.
870 */
871 if (rb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT && ctx->ops->reqbufs)
872 return ctx->ops->reqbufs(ctx, rb);
873
874 return 0;
875}
876
2e9e4f11
PZ
877static int coda_qbuf(struct file *file, void *priv,
878 struct v4l2_buffer *buf)
186b250a
JM
879{
880 struct coda_ctx *ctx = fh_to_ctx(priv);
881
14604e3a 882 return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
186b250a
JM
883}
884
918c66fd 885static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx,
2d700715 886 struct vb2_v4l2_buffer *buf)
918c66fd 887{
918c66fd 888 return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) &&
2d700715 889 (buf->sequence == (ctx->qsequence - 1)));
918c66fd
PZ
890}
891
2d700715 892void coda_m2m_buf_done(struct coda_ctx *ctx, struct vb2_v4l2_buffer *buf,
9f2bfb33 893 enum vb2_buffer_state state)
186b250a 894{
9f2bfb33
PZ
895 const struct v4l2_event eos_event = {
896 .type = V4L2_EVENT_EOS
897 };
186b250a 898
9f2bfb33 899 if (coda_buf_is_end_of_stream(ctx, buf)) {
2d700715 900 buf->flags |= V4L2_BUF_FLAG_LAST;
918c66fd
PZ
901
902 v4l2_event_queue_fh(&ctx->fh, &eos_event);
903 }
904
9f2bfb33 905 v4l2_m2m_buf_done(buf, state);
186b250a
JM
906}
907
52c41676
PZ
908static int coda_g_selection(struct file *file, void *fh,
909 struct v4l2_selection *s)
910{
911 struct coda_ctx *ctx = fh_to_ctx(fh);
912 struct coda_q_data *q_data;
913 struct v4l2_rect r, *rsel;
914
915 q_data = get_q_data(ctx, s->type);
916 if (!q_data)
917 return -EINVAL;
918
919 r.left = 0;
920 r.top = 0;
921 r.width = q_data->width;
922 r.height = q_data->height;
923 rsel = &q_data->rect;
924
925 switch (s->target) {
926 case V4L2_SEL_TGT_CROP_DEFAULT:
927 case V4L2_SEL_TGT_CROP_BOUNDS:
928 rsel = &r;
929 /* fallthrough */
930 case V4L2_SEL_TGT_CROP:
931 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
932 return -EINVAL;
933 break;
934 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
935 case V4L2_SEL_TGT_COMPOSE_PADDED:
936 rsel = &r;
937 /* fallthrough */
938 case V4L2_SEL_TGT_COMPOSE:
939 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
940 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
941 return -EINVAL;
942 break;
943 default:
944 return -EINVAL;
945 }
946
947 s->r = *rsel;
948
949 return 0;
950}
951
40fa8df6
PZ
952static int coda_s_selection(struct file *file, void *fh,
953 struct v4l2_selection *s)
954{
955 struct coda_ctx *ctx = fh_to_ctx(fh);
956 struct coda_q_data *q_data;
957
7e47c384
PZ
958 switch (s->target) {
959 case V4L2_SEL_TGT_CROP:
960 if (ctx->inst_type == CODA_INST_ENCODER &&
961 s->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
962 q_data = get_q_data(ctx, s->type);
963 if (!q_data)
964 return -EINVAL;
40fa8df6 965
7e47c384
PZ
966 s->r.left = 0;
967 s->r.top = 0;
968 s->r.width = clamp(s->r.width, 2U, q_data->width);
969 s->r.height = clamp(s->r.height, 2U, q_data->height);
40fa8df6 970
7e47c384
PZ
971 if (s->flags & V4L2_SEL_FLAG_LE) {
972 s->r.width = round_up(s->r.width, 2);
973 s->r.height = round_up(s->r.height, 2);
974 } else {
975 s->r.width = round_down(s->r.width, 2);
976 s->r.height = round_down(s->r.height, 2);
977 }
40fa8df6 978
7e47c384 979 q_data->rect = s->r;
40fa8df6 980
c229f5c7
PZ
981 coda_dbg(1, ctx, "Setting crop rectangle: %dx%d\n",
982 s->r.width, s->r.height);
983
7e47c384
PZ
984 return 0;
985 }
986 /* else fall through */
987 case V4L2_SEL_TGT_NATIVE_SIZE:
988 case V4L2_SEL_TGT_COMPOSE:
989 return coda_g_selection(file, fh, s);
990 default:
991 /* v4l2-compliance expects this to fail for read-only targets */
992 return -EINVAL;
993 }
40fa8df6
PZ
994}
995
59195ceb
PZ
996static int coda_try_encoder_cmd(struct file *file, void *fh,
997 struct v4l2_encoder_cmd *ec)
998{
784867d2
PZ
999 struct coda_ctx *ctx = fh_to_ctx(fh);
1000
1001 if (ctx->inst_type != CODA_INST_ENCODER)
1002 return -ENOTTY;
1003
59195ceb
PZ
1004 if (ec->cmd != V4L2_ENC_CMD_STOP)
1005 return -EINVAL;
1006
1007 if (ec->flags & V4L2_ENC_CMD_STOP_AT_GOP_END)
1008 return -EINVAL;
1009
1010 return 0;
1011}
1012
1013static int coda_encoder_cmd(struct file *file, void *fh,
1014 struct v4l2_encoder_cmd *ec)
1015{
1016 struct coda_ctx *ctx = fh_to_ctx(fh);
1017 struct vb2_queue *dst_vq;
1018 int ret;
1019
1020 ret = coda_try_encoder_cmd(file, fh, ec);
1021 if (ret < 0)
1022 return ret;
1023
59195ceb
PZ
1024 /* Set the stream-end flag on this context */
1025 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
1026
1027 /* If there is no buffer in flight, wake up */
8056402c 1028 if (!ctx->streamon_out || ctx->qsequence == ctx->osequence) {
59195ceb
PZ
1029 dst_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
1030 V4L2_BUF_TYPE_VIDEO_CAPTURE);
1031 dst_vq->last_buffer_dequeued = true;
1032 wake_up(&dst_vq->done_wq);
1033 }
1034
1035 return 0;
1036}
1037
64ba40ac
PZ
1038static int coda_try_decoder_cmd(struct file *file, void *fh,
1039 struct v4l2_decoder_cmd *dc)
918c66fd 1040{
784867d2
PZ
1041 struct coda_ctx *ctx = fh_to_ctx(fh);
1042
1043 if (ctx->inst_type != CODA_INST_DECODER)
1044 return -ENOTTY;
1045
918c66fd
PZ
1046 if (dc->cmd != V4L2_DEC_CMD_STOP)
1047 return -EINVAL;
1048
64ba40ac 1049 if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
918c66fd
PZ
1050 return -EINVAL;
1051
64ba40ac 1052 if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0))
918c66fd
PZ
1053 return -EINVAL;
1054
64ba40ac
PZ
1055 return 0;
1056}
1057
1058static int coda_decoder_cmd(struct file *file, void *fh,
1059 struct v4l2_decoder_cmd *dc)
1060{
1061 struct coda_ctx *ctx = fh_to_ctx(fh);
1062 int ret;
1063
1064 ret = coda_try_decoder_cmd(file, fh, dc);
1065 if (ret < 0)
1066 return ret;
1067
347bb7f0
PZ
1068 /* Set the stream-end flag on this context */
1069 coda_bit_stream_end_flag(ctx);
84e2365d 1070 ctx->hold = false;
f3497dad 1071 v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
89548441 1072
918c66fd
PZ
1073 return 0;
1074}
1075
b03abe53
PZ
1076static int coda_enum_framesizes(struct file *file, void *fh,
1077 struct v4l2_frmsizeenum *fsize)
1078{
1079 struct coda_ctx *ctx = fh_to_ctx(fh);
1080 struct coda_q_data *q_data_dst;
1081 const struct coda_codec *codec;
1082
1083 if (ctx->inst_type != CODA_INST_ENCODER)
1084 return -ENOTTY;
1085
1086 if (fsize->index)
1087 return -EINVAL;
1088
1089 if (coda_format_normalize_yuv(fsize->pixel_format) ==
1090 V4L2_PIX_FMT_YUV420) {
1091 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1092 codec = coda_find_codec(ctx->dev, fsize->pixel_format,
1093 q_data_dst->fourcc);
1094 } else {
1095 codec = coda_find_codec(ctx->dev, V4L2_PIX_FMT_YUV420,
1096 fsize->pixel_format);
1097 }
1098 if (!codec)
1099 return -EINVAL;
1100
1101 fsize->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
1102 fsize->stepwise.min_width = MIN_W;
1103 fsize->stepwise.max_width = codec->max_w;
1104 fsize->stepwise.step_width = 1;
1105 fsize->stepwise.min_height = MIN_H;
1106 fsize->stepwise.max_height = codec->max_h;
1107 fsize->stepwise.step_height = 1;
1108
1109 return 0;
1110}
1111
07b6080d
PZ
1112static int coda_enum_frameintervals(struct file *file, void *fh,
1113 struct v4l2_frmivalenum *f)
1114{
1115 struct coda_ctx *ctx = fh_to_ctx(fh);
1116 int i;
1117
1118 if (f->index)
1119 return -EINVAL;
1120
1121 /* Disallow YUYV if the vdoa is not available */
1122 if (!ctx->vdoa && f->pixel_format == V4L2_PIX_FMT_YUYV)
1123 return -EINVAL;
1124
1125 for (i = 0; i < CODA_MAX_FORMATS; i++) {
1126 if (f->pixel_format == ctx->cvd->src_formats[i] ||
1127 f->pixel_format == ctx->cvd->dst_formats[i])
1128 break;
1129 }
1130 if (i == CODA_MAX_FORMATS)
1131 return -EINVAL;
1132
1133 f->type = V4L2_FRMIVAL_TYPE_CONTINUOUS;
1134 f->stepwise.min.numerator = 1;
1135 f->stepwise.min.denominator = 65535;
1136 f->stepwise.max.numerator = 65536;
1137 f->stepwise.max.denominator = 1;
1138 f->stepwise.step.numerator = 1;
1139 f->stepwise.step.denominator = 1;
1140
1141 return 0;
1142}
1143
cde29ef3
PZ
1144static int coda_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
1145{
1146 struct coda_ctx *ctx = fh_to_ctx(fh);
1147 struct v4l2_fract *tpf;
1148
1149 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1150 return -EINVAL;
1151
1152 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
1153 tpf = &a->parm.output.timeperframe;
1154 tpf->denominator = ctx->params.framerate & CODA_FRATE_RES_MASK;
1155 tpf->numerator = 1 + (ctx->params.framerate >>
1156 CODA_FRATE_DIV_OFFSET);
1157
1158 return 0;
1159}
1160
1161/*
1162 * Approximate timeperframe v4l2_fract with values that can be written
1163 * into the 16-bit CODA_FRATE_DIV and CODA_FRATE_RES fields.
1164 */
1165static void coda_approximate_timeperframe(struct v4l2_fract *timeperframe)
1166{
1167 struct v4l2_fract s = *timeperframe;
1168 struct v4l2_fract f0;
1169 struct v4l2_fract f1 = { 1, 0 };
1170 struct v4l2_fract f2 = { 0, 1 };
1171 unsigned int i, div, s_denominator;
1172
1173 /* Lower bound is 1/65535 */
1174 if (s.numerator == 0 || s.denominator / s.numerator > 65535) {
1175 timeperframe->numerator = 1;
1176 timeperframe->denominator = 65535;
1177 return;
1178 }
1179
cf13135c 1180 /* Upper bound is 65536/1 */
cde29ef3 1181 if (s.denominator == 0 || s.numerator / s.denominator > 65536) {
cf13135c
PZ
1182 timeperframe->numerator = 65536;
1183 timeperframe->denominator = 1;
cde29ef3
PZ
1184 return;
1185 }
1186
1187 /* Reduce fraction to lowest terms */
1188 div = gcd(s.numerator, s.denominator);
1189 if (div > 1) {
1190 s.numerator /= div;
1191 s.denominator /= div;
1192 }
1193
1194 if (s.numerator <= 65536 && s.denominator < 65536) {
1195 *timeperframe = s;
1196 return;
1197 }
1198
1199 /* Find successive convergents from continued fraction expansion */
1200 while (f2.numerator <= 65536 && f2.denominator < 65536) {
1201 f0 = f1;
1202 f1 = f2;
1203
1204 /* Stop when f2 exactly equals timeperframe */
1205 if (s.numerator == 0)
1206 break;
1207
1208 i = s.denominator / s.numerator;
1209
1210 f2.numerator = f0.numerator + i * f1.numerator;
1211 f2.denominator = f0.denominator + i * f2.denominator;
1212
1213 s_denominator = s.numerator;
1214 s.numerator = s.denominator % s.numerator;
1215 s.denominator = s_denominator;
1216 }
1217
1218 *timeperframe = f1;
1219}
1220
1221static uint32_t coda_timeperframe_to_frate(struct v4l2_fract *timeperframe)
1222{
1223 return ((timeperframe->numerator - 1) << CODA_FRATE_DIV_OFFSET) |
1224 timeperframe->denominator;
1225}
1226
1227static int coda_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
1228{
1229 struct coda_ctx *ctx = fh_to_ctx(fh);
1230 struct v4l2_fract *tpf;
1231
1232 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1233 return -EINVAL;
1234
4c5ce24b 1235 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
cde29ef3
PZ
1236 tpf = &a->parm.output.timeperframe;
1237 coda_approximate_timeperframe(tpf);
1238 ctx->params.framerate = coda_timeperframe_to_frate(tpf);
1239
1240 return 0;
1241}
1242
2e9e4f11
PZ
1243static int coda_subscribe_event(struct v4l2_fh *fh,
1244 const struct v4l2_event_subscription *sub)
918c66fd
PZ
1245{
1246 switch (sub->type) {
1247 case V4L2_EVENT_EOS:
1248 return v4l2_event_subscribe(fh, sub, 0, NULL);
1249 default:
1250 return v4l2_ctrl_subscribe_event(fh, sub);
1251 }
186b250a
JM
1252}
1253
1254static const struct v4l2_ioctl_ops coda_ioctl_ops = {
2e9e4f11 1255 .vidioc_querycap = coda_querycap,
186b250a 1256
22e244b8 1257 .vidioc_enum_fmt_vid_cap = coda_enum_fmt,
2e9e4f11
PZ
1258 .vidioc_g_fmt_vid_cap = coda_g_fmt,
1259 .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
1260 .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap,
186b250a 1261
22e244b8 1262 .vidioc_enum_fmt_vid_out = coda_enum_fmt,
2e9e4f11
PZ
1263 .vidioc_g_fmt_vid_out = coda_g_fmt,
1264 .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
1265 .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out,
186b250a 1266
73751da6 1267 .vidioc_reqbufs = coda_reqbufs,
152ea1c8 1268 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
186b250a 1269
2e9e4f11 1270 .vidioc_qbuf = coda_qbuf,
152ea1c8 1271 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
9f2bfb33 1272 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
152ea1c8 1273 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
bb757d7b 1274 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
186b250a 1275
152ea1c8
PZ
1276 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
1277 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
918c66fd 1278
52c41676 1279 .vidioc_g_selection = coda_g_selection,
40fa8df6 1280 .vidioc_s_selection = coda_s_selection,
52c41676 1281
59195ceb
PZ
1282 .vidioc_try_encoder_cmd = coda_try_encoder_cmd,
1283 .vidioc_encoder_cmd = coda_encoder_cmd,
64ba40ac 1284 .vidioc_try_decoder_cmd = coda_try_decoder_cmd,
2e9e4f11 1285 .vidioc_decoder_cmd = coda_decoder_cmd,
918c66fd 1286
cde29ef3
PZ
1287 .vidioc_g_parm = coda_g_parm,
1288 .vidioc_s_parm = coda_s_parm,
1289
b03abe53 1290 .vidioc_enum_framesizes = coda_enum_framesizes,
07b6080d
PZ
1291 .vidioc_enum_frameintervals = coda_enum_frameintervals,
1292
2e9e4f11 1293 .vidioc_subscribe_event = coda_subscribe_event,
918c66fd 1294 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
186b250a
JM
1295};
1296
1297/*
1298 * Mem-to-mem operations.
1299 */
477c1cfe
PZ
1300
1301static void coda_device_run(void *m2m_priv)
1302{
1303 struct coda_ctx *ctx = m2m_priv;
1304 struct coda_dev *dev = ctx->dev;
32b6f206
PZ
1305
1306 queue_work(dev->workqueue, &ctx->pic_run_work);
1307}
1308
32b6f206
PZ
1309static void coda_pic_run_work(struct work_struct *work)
1310{
1311 struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
1312 struct coda_dev *dev = ctx->dev;
1313 int ret;
1314
1315 mutex_lock(&ctx->buffer_mutex);
918c66fd
PZ
1316 mutex_lock(&dev->coda_mutex);
1317
a1192a17
PZ
1318 ret = ctx->ops->prepare_run(ctx);
1319 if (ret < 0 && ctx->inst_type == CODA_INST_DECODER) {
1320 mutex_unlock(&dev->coda_mutex);
1321 mutex_unlock(&ctx->buffer_mutex);
1322 /* job_finish scheduled by prepare_decode */
1323 return;
1043667b
PZ
1324 }
1325
f23797b6
PZ
1326 if (!wait_for_completion_timeout(&ctx->completion,
1327 msecs_to_jiffies(1000))) {
32b6f206 1328 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout\n");
84e2365d
PZ
1329
1330 ctx->hold = true;
8f45284c
PZ
1331
1332 coda_hw_reset(ctx);
fb2be08f 1333
cb778f5c
PZ
1334 if (ctx->ops->run_timeout)
1335 ctx->ops->run_timeout(ctx);
32b6f206 1336 } else if (!ctx->aborting) {
a1192a17 1337 ctx->ops->finish_run(ctx);
32b6f206
PZ
1338 }
1339
747d7646
PZ
1340 if ((ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out)) &&
1341 ctx->ops->seq_end_work)
32b6f206
PZ
1342 queue_work(dev->workqueue, &ctx->seq_end_work);
1343
1344 mutex_unlock(&dev->coda_mutex);
1345 mutex_unlock(&ctx->buffer_mutex);
1346
14604e3a 1347 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
186b250a
JM
1348}
1349
1350static int coda_job_ready(void *m2m_priv)
1351{
1352 struct coda_ctx *ctx = m2m_priv;
2cf251c0 1353 int src_bufs = v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx);
186b250a
JM
1354
1355 /*
1356 * For both 'P' and 'key' frame cases 1 picture
9082a7c6
PZ
1357 * and 1 frame are needed. In the decoder case,
1358 * the compressed frame can be in the bitstream.
186b250a 1359 */
2cf251c0 1360 if (!src_bufs && ctx->inst_type != CODA_INST_DECODER) {
8f90d15e 1361 coda_dbg(1, ctx, "not ready: not enough vid-out buffers.\n");
186b250a
JM
1362 return 0;
1363 }
1364
14604e3a 1365 if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
8f90d15e 1366 coda_dbg(1, ctx, "not ready: not enough vid-cap buffers.\n");
9082a7c6
PZ
1367 return 0;
1368 }
1369
a22496c6 1370 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
47f3fa63
PZ
1371 bool stream_end = ctx->bit_stream_param &
1372 CODA_BIT_STREAM_END_FLAG;
1373 int num_metas = ctx->num_metas;
51407c2d 1374 struct coda_buffer_meta *meta;
e7f3c548
MT
1375 unsigned int count;
1376
1377 count = hweight32(ctx->frm_dis_flg);
1378 if (ctx->use_vdoa && count >= (ctx->num_internal_frames - 1)) {
8f90d15e
PZ
1379 coda_dbg(1, ctx,
1380 "not ready: all internal buffers in use: %d/%d (0x%x)",
1381 count, ctx->num_internal_frames,
e7f3c548
MT
1382 ctx->frm_dis_flg);
1383 return 0;
1384 }
f77fd8a9 1385
2cf251c0 1386 if (ctx->hold && !src_bufs) {
8f90d15e
PZ
1387 coda_dbg(1, ctx,
1388 "not ready: on hold for more buffers.\n");
f77fd8a9
PZ
1389 return 0;
1390 }
1391
f77fd8a9 1392 if (!stream_end && (num_metas + src_bufs) < 2) {
8f90d15e
PZ
1393 coda_dbg(1, ctx,
1394 "not ready: need 2 buffers available (queue:%d + bitstream:%d)\n",
1395 num_metas, src_bufs);
f77fd8a9
PZ
1396 return 0;
1397 }
1398
51407c2d
PZ
1399 meta = list_first_entry(&ctx->buffer_meta_list,
1400 struct coda_buffer_meta, list);
1401 if (!coda_bitstream_can_fetch_past(ctx, meta->end) &&
1402 !stream_end) {
8f90d15e 1403 coda_dbg(1, ctx,
51407c2d
PZ
1404 "not ready: not enough bitstream data to read past %u (%u)\n",
1405 meta->end, ctx->bitstream_fifo.kfifo.in);
f77fd8a9
PZ
1406 return 0;
1407 }
918c66fd
PZ
1408 }
1409
3e748268 1410 if (ctx->aborting) {
8f90d15e 1411 coda_dbg(1, ctx, "not ready: aborting\n");
3e748268
PZ
1412 return 0;
1413 }
1414
8f90d15e 1415 coda_dbg(1, ctx, "job ready\n");
47f3fa63 1416
186b250a
JM
1417 return 1;
1418}
1419
1420static void coda_job_abort(void *priv)
1421{
1422 struct coda_ctx *ctx = priv;
186b250a
JM
1423
1424 ctx->aborting = 1;
1425
8f90d15e 1426 coda_dbg(1, ctx, "job abort\n");
186b250a
JM
1427}
1428
814c3762 1429static const struct v4l2_m2m_ops coda_m2m_ops = {
186b250a
JM
1430 .device_run = coda_device_run,
1431 .job_ready = coda_job_ready,
1432 .job_abort = coda_job_abort,
186b250a
JM
1433};
1434
1435static void set_default_params(struct coda_ctx *ctx)
1436{
8e428d50 1437 unsigned int max_w, max_h, usize, csize;
b96904e5 1438
2c11d1bd
PZ
1439 ctx->codec = coda_find_codec(ctx->dev, ctx->cvd->src_formats[0],
1440 ctx->cvd->dst_formats[0]);
d4c6a416
PZ
1441 max_w = min(ctx->codec->max_w, 1920U);
1442 max_h = min(ctx->codec->max_h, 1088U);
8e428d50
PZ
1443 usize = max_w * max_h * 3 / 2;
1444 csize = coda_estimate_sizeimage(ctx, usize, max_w, max_h);
186b250a 1445
121cacf4 1446 ctx->params.codec_mode = ctx->codec->mode;
b14ac545
PZ
1447 if (ctx->cvd->src_formats[0] == V4L2_PIX_FMT_JPEG)
1448 ctx->colorspace = V4L2_COLORSPACE_JPEG;
1449 else
1450 ctx->colorspace = V4L2_COLORSPACE_REC709;
1451 ctx->xfer_func = V4L2_XFER_FUNC_DEFAULT;
1452 ctx->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
1453 ctx->quantization = V4L2_QUANTIZATION_DEFAULT;
186b250a 1454 ctx->params.framerate = 30;
186b250a
JM
1455
1456 /* Default formats for output and input queues */
6727d4fc
PZ
1457 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->cvd->src_formats[0];
1458 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->cvd->dst_formats[0];
b96904e5
PZ
1459 ctx->q_data[V4L2_M2M_SRC].width = max_w;
1460 ctx->q_data[V4L2_M2M_SRC].height = max_h;
b96904e5
PZ
1461 ctx->q_data[V4L2_M2M_DST].width = max_w;
1462 ctx->q_data[V4L2_M2M_DST].height = max_h;
121cacf4
PZ
1463 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_YUV420) {
1464 ctx->q_data[V4L2_M2M_SRC].bytesperline = max_w;
8e428d50 1465 ctx->q_data[V4L2_M2M_SRC].sizeimage = usize;
121cacf4 1466 ctx->q_data[V4L2_M2M_DST].bytesperline = 0;
8e428d50 1467 ctx->q_data[V4L2_M2M_DST].sizeimage = csize;
121cacf4
PZ
1468 } else {
1469 ctx->q_data[V4L2_M2M_SRC].bytesperline = 0;
8e428d50 1470 ctx->q_data[V4L2_M2M_SRC].sizeimage = csize;
121cacf4 1471 ctx->q_data[V4L2_M2M_DST].bytesperline = max_w;
8e428d50 1472 ctx->q_data[V4L2_M2M_DST].sizeimage = usize;
121cacf4 1473 }
52c41676
PZ
1474 ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
1475 ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
1476 ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
1477 ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
89548441 1478
a269e53b
PZ
1479 /*
1480 * Since the RBC2AXI logic only supports a single chroma plane,
1481 * macroblock tiling only works for to NV12 pixel format.
1482 */
1483 ctx->tiled_map_type = GDI_LINEAR_FRAME_MAP;
186b250a
JM
1484}
1485
1486/*
1487 * Queue operations
1488 */
df9ecb0c 1489static int coda_queue_setup(struct vb2_queue *vq,
186b250a 1490 unsigned int *nbuffers, unsigned int *nplanes,
36c0f8b3 1491 unsigned int sizes[], struct device *alloc_devs[])
186b250a
JM
1492{
1493 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
e34db066 1494 struct coda_q_data *q_data;
186b250a
JM
1495 unsigned int size;
1496
e34db066
PZ
1497 q_data = get_q_data(ctx, vq->type);
1498 size = q_data->sizeimage;
186b250a 1499
0eecc619
PZ
1500 if (*nplanes)
1501 return sizes[0] < size ? -EINVAL : 0;
1502
186b250a
JM
1503 *nplanes = 1;
1504 sizes[0] = size;
1505
8f90d15e
PZ
1506 coda_dbg(1, ctx, "get %d buffer(s) of size %d each.\n", *nbuffers,
1507 size);
186b250a
JM
1508
1509 return 0;
1510}
1511
1512static int coda_buf_prepare(struct vb2_buffer *vb)
1513{
583958cb 1514 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
186b250a
JM
1515 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1516 struct coda_q_data *q_data;
1517
1518 q_data = get_q_data(ctx, vb->vb2_queue->type);
583958cb
PZ
1519 if (V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) {
1520 if (vbuf->field == V4L2_FIELD_ANY)
1521 vbuf->field = V4L2_FIELD_NONE;
1522 if (vbuf->field != V4L2_FIELD_NONE) {
1523 v4l2_warn(&ctx->dev->v4l2_dev,
1524 "%s field isn't supported\n", __func__);
1525 return -EINVAL;
1526 }
1527 }
186b250a
JM
1528
1529 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
1530 v4l2_warn(&ctx->dev->v4l2_dev,
1531 "%s data will not fit into plane (%lu < %lu)\n",
1532 __func__, vb2_plane_size(vb, 0),
1533 (long)q_data->sizeimage);
1534 return -EINVAL;
1535 }
1536
186b250a
JM
1537 return 0;
1538}
1539
42a68012
PZ
1540static void coda_update_menu_ctrl(struct v4l2_ctrl *ctrl, int value)
1541{
1542 if (!ctrl)
1543 return;
1544
1545 v4l2_ctrl_lock(ctrl);
1546
1547 /*
1548 * Extend the control range if the parsed stream contains a known but
1549 * unsupported value or level.
1550 */
1551 if (value > ctrl->maximum) {
1552 __v4l2_ctrl_modify_range(ctrl, ctrl->minimum, value,
1553 ctrl->menu_skip_mask & ~(1 << value),
1554 ctrl->default_value);
1555 } else if (value < ctrl->minimum) {
1556 __v4l2_ctrl_modify_range(ctrl, value, ctrl->maximum,
1557 ctrl->menu_skip_mask & ~(1 << value),
1558 ctrl->default_value);
1559 }
1560
1561 __v4l2_ctrl_s_ctrl(ctrl, value);
1562
1563 v4l2_ctrl_unlock(ctrl);
1564}
1565
1566static void coda_update_h264_profile_ctrl(struct coda_ctx *ctx)
1567{
1568 const char * const *profile_names;
1569 int profile;
1570
1571 profile = coda_h264_profile(ctx->params.h264_profile_idc);
1572 if (profile < 0) {
1573 v4l2_warn(&ctx->dev->v4l2_dev, "Invalid H264 Profile: %u\n",
1574 ctx->params.h264_profile_idc);
1575 return;
1576 }
1577
1578 coda_update_menu_ctrl(ctx->h264_profile_ctrl, profile);
1579
1580 profile_names = v4l2_ctrl_get_menu(V4L2_CID_MPEG_VIDEO_H264_PROFILE);
1581
8f90d15e 1582 coda_dbg(1, ctx, "Parsed H264 Profile: %s\n", profile_names[profile]);
42a68012
PZ
1583}
1584
1585static void coda_update_h264_level_ctrl(struct coda_ctx *ctx)
1586{
1587 const char * const *level_names;
1588 int level;
1589
1590 level = coda_h264_level(ctx->params.h264_level_idc);
1591 if (level < 0) {
1592 v4l2_warn(&ctx->dev->v4l2_dev, "Invalid H264 Level: %u\n",
1593 ctx->params.h264_level_idc);
1594 return;
1595 }
1596
1597 coda_update_menu_ctrl(ctx->h264_level_ctrl, level);
1598
1599 level_names = v4l2_ctrl_get_menu(V4L2_CID_MPEG_VIDEO_H264_LEVEL);
1600
8f90d15e 1601 coda_dbg(1, ctx, "Parsed H264 Level: %s\n", level_names[level]);
42a68012
PZ
1602}
1603
186b250a
JM
1604static void coda_buf_queue(struct vb2_buffer *vb)
1605{
2d700715 1606 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
186b250a 1607 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
cb06b705 1608 struct vb2_queue *vq = vb->vb2_queue;
918c66fd
PZ
1609 struct coda_q_data *q_data;
1610
1611 q_data = get_q_data(ctx, vb->vb2_queue->type);
1612
1613 /*
1614 * In the decoder case, immediately try to copy the buffer into the
1615 * bitstream ringbuffer and mark it as ready to be dequeued.
1616 */
cb06b705 1617 if (ctx->bitstream.size && vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
918c66fd 1618 /*
39c1cb2b 1619 * For backwards compatibility, queuing an empty buffer marks
918c66fd
PZ
1620 * the stream end
1621 */
347bb7f0
PZ
1622 if (vb2_get_plane_payload(vb, 0) == 0)
1623 coda_bit_stream_end_flag(ctx);
69122271
PZ
1624
1625 if (q_data->fourcc == V4L2_PIX_FMT_H264) {
1626 /*
1627 * Unless already done, try to obtain profile_idc and
1628 * level_idc from the SPS header. This allows to decide
1629 * whether to enable reordering during sequence
1630 * initialization.
1631 */
42a68012 1632 if (!ctx->params.h264_profile_idc) {
69122271 1633 coda_sps_parse_profile(ctx, vb);
42a68012
PZ
1634 coda_update_h264_profile_ctrl(ctx);
1635 coda_update_h264_level_ctrl(ctx);
1636 }
69122271
PZ
1637 }
1638
918c66fd 1639 mutex_lock(&ctx->bitstream_mutex);
2d700715 1640 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
eabed931 1641 if (vb2_is_streaming(vb->vb2_queue))
59195ceb 1642 /* This set buf->sequence = ctx->qsequence++ */
331e7860 1643 coda_fill_bitstream(ctx, NULL);
918c66fd
PZ
1644 mutex_unlock(&ctx->bitstream_mutex);
1645 } else {
59195ceb
PZ
1646 if (ctx->inst_type == CODA_INST_ENCODER &&
1647 vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1648 vbuf->sequence = ctx->qsequence++;
2d700715 1649 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
918c66fd 1650 }
186b250a
JM
1651}
1652
79924ca9
PZ
1653int coda_alloc_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf,
1654 size_t size, const char *name, struct dentry *parent)
5677e3b0
PZ
1655{
1656 buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
1657 GFP_KERNEL);
68fc31c5
PZ
1658 if (!buf->vaddr) {
1659 v4l2_err(&dev->v4l2_dev,
d5798ee3 1660 "Failed to allocate %s buffer of size %zu\n",
68fc31c5 1661 name, size);
5677e3b0 1662 return -ENOMEM;
68fc31c5 1663 }
5677e3b0
PZ
1664
1665 buf->size = size;
1666
f61c0b14
PZ
1667 if (name && parent) {
1668 buf->blob.data = buf->vaddr;
1669 buf->blob.size = size;
f23797b6
PZ
1670 buf->dentry = debugfs_create_blob(name, 0644, parent,
1671 &buf->blob);
f61c0b14
PZ
1672 if (!buf->dentry)
1673 dev_warn(&dev->plat_dev->dev,
1674 "failed to create debugfs entry %s\n", name);
1675 }
1676
5677e3b0
PZ
1677 return 0;
1678}
1679
79924ca9
PZ
1680void coda_free_aux_buf(struct coda_dev *dev,
1681 struct coda_aux_buf *buf)
5677e3b0
PZ
1682{
1683 if (buf->vaddr) {
1684 dma_free_coherent(&dev->plat_dev->dev, buf->size,
1685 buf->vaddr, buf->paddr);
1686 buf->vaddr = NULL;
1687 buf->size = 0;
d446ec8e
PS
1688 debugfs_remove(buf->dentry);
1689 buf->dentry = NULL;
5677e3b0
PZ
1690 }
1691}
1692
79924ca9 1693static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
5677e3b0 1694{
79924ca9
PZ
1695 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1696 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
1697 struct coda_q_data *q_data_src, *q_data_dst;
331e7860 1698 struct v4l2_m2m_buffer *m2m_buf, *tmp;
2d700715 1699 struct vb2_v4l2_buffer *buf;
331e7860 1700 struct list_head list;
79924ca9 1701 int ret = 0;
5677e3b0 1702
49b966fa
PZ
1703 if (count < 1)
1704 return -EINVAL;
1705
8f90d15e
PZ
1706 coda_dbg(1, ctx, "start streaming %s\n", v4l2_type_names[q->type]);
1707
331e7860
PZ
1708 INIT_LIST_HEAD(&list);
1709
79924ca9
PZ
1710 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1711 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
58bc7edf 1712 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
582d8872 1713 /* copy the buffers that were queued before streamon */
79924ca9 1714 mutex_lock(&ctx->bitstream_mutex);
331e7860 1715 coda_fill_bitstream(ctx, &list);
79924ca9 1716 mutex_unlock(&ctx->bitstream_mutex);
eabed931 1717
b906352c
PZ
1718 if (coda_get_bitstream_payload(ctx) < 512) {
1719 ret = -EINVAL;
1720 goto err;
1721 }
918c66fd 1722 }
186b250a 1723
b96904e5 1724 ctx->streamon_out = 1;
918c66fd 1725 } else {
918c66fd 1726 ctx->streamon_cap = 1;
b96904e5 1727 }
186b250a 1728
b96904e5 1729 /* Don't start the coda unless both queues are on */
888708b1 1730 if (!(ctx->streamon_out && ctx->streamon_cap))
331e7860 1731 goto out;
186b250a 1732
23b6ee5c 1733 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
40fa8df6
PZ
1734 if ((q_data_src->rect.width != q_data_dst->width &&
1735 round_up(q_data_src->rect.width, 16) != q_data_dst->width) ||
1736 (q_data_src->rect.height != q_data_dst->height &&
1737 round_up(q_data_src->rect.height, 16) != q_data_dst->height)) {
23b6ee5c 1738 v4l2_err(v4l2_dev, "can't convert %dx%d to %dx%d\n",
40fa8df6 1739 q_data_src->rect.width, q_data_src->rect.height,
23b6ee5c
PZ
1740 q_data_dst->width, q_data_dst->height);
1741 ret = -EINVAL;
1742 goto err;
1743 }
1744
cb1d3a33 1745 /* Allow BIT decoder device_run with no new buffers queued */
a22496c6 1746 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
14604e3a 1747 v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
918c66fd 1748
b96904e5 1749 ctx->gopcounter = ctx->params.gop_size - 1;
b96904e5 1750
cb1d3a33
PZ
1751 if (q_data_dst->fourcc == V4L2_PIX_FMT_JPEG)
1752 ctx->params.gop_size = 1;
1753 ctx->gopcounter = ctx->params.gop_size - 1;
1754
a1192a17 1755 ret = ctx->ops->start_streaming(ctx);
918c66fd 1756 if (ctx->inst_type == CODA_INST_DECODER) {
89548441 1757 if (ret == -EAGAIN)
331e7860 1758 goto out;
918c66fd 1759 }
331e7860
PZ
1760 if (ret < 0)
1761 goto err;
918c66fd 1762
331e7860
PZ
1763out:
1764 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1765 list_for_each_entry_safe(m2m_buf, tmp, &list, list) {
1766 list_del(&m2m_buf->list);
1767 v4l2_m2m_buf_done(&m2m_buf->vb, VB2_BUF_STATE_DONE);
1768 }
1769 }
1770 return 0;
b906352c
PZ
1771
1772err:
1773 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
331e7860
PZ
1774 list_for_each_entry_safe(m2m_buf, tmp, &list, list) {
1775 list_del(&m2m_buf->list);
1776 v4l2_m2m_buf_done(&m2m_buf->vb, VB2_BUF_STATE_QUEUED);
1777 }
b906352c 1778 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
b8b1b58c 1779 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
b906352c
PZ
1780 } else {
1781 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
b8b1b58c 1782 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
b906352c
PZ
1783 }
1784 return ret;
89548441
PZ
1785}
1786
e37559b2 1787static void coda_stop_streaming(struct vb2_queue *q)
186b250a
JM
1788{
1789 struct coda_ctx *ctx = vb2_get_drv_priv(q);
2fb57f06 1790 struct coda_dev *dev = ctx->dev;
2d700715 1791 struct vb2_v4l2_buffer *buf;
5c76c2c3
PZ
1792 bool stop;
1793
1794 stop = ctx->streamon_out && ctx->streamon_cap;
186b250a 1795
8f90d15e
PZ
1796 coda_dbg(1, ctx, "stop streaming %s\n", v4l2_type_names[q->type]);
1797
186b250a 1798 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
b96904e5 1799 ctx->streamon_out = 0;
918c66fd 1800
347bb7f0 1801 coda_bit_stream_end_flag(ctx);
4a31b52f 1802
6dd5ef5b 1803 ctx->qsequence = 0;
4a31b52f
PZ
1804
1805 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1806 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
186b250a 1807 } else {
b96904e5 1808 ctx->streamon_cap = 0;
2fb57f06 1809
918c66fd 1810 ctx->osequence = 0;
cb2c0282 1811 ctx->sequence_offset = 0;
4a31b52f
PZ
1812
1813 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
1814 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
186b250a
JM
1815 }
1816
5c76c2c3 1817 if (stop) {
7cbb105f 1818 struct coda_buffer_meta *meta;
846ced9c 1819
f4706d6e
PZ
1820 if (ctx->ops->seq_end_work) {
1821 queue_work(dev->workqueue, &ctx->seq_end_work);
1822 flush_work(&ctx->seq_end_work);
1823 }
68d66a93 1824 spin_lock(&ctx->buffer_meta_lock);
7cbb105f
PZ
1825 while (!list_empty(&ctx->buffer_meta_list)) {
1826 meta = list_first_entry(&ctx->buffer_meta_list,
1827 struct coda_buffer_meta, list);
1828 list_del(&meta->list);
1829 kfree(meta);
846ced9c 1830 }
47f3fa63 1831 ctx->num_metas = 0;
68d66a93 1832 spin_unlock(&ctx->buffer_meta_lock);
918c66fd
PZ
1833 kfifo_init(&ctx->bitstream_fifo,
1834 ctx->bitstream.vaddr, ctx->bitstream.size);
1835 ctx->runcounter = 0;
f157cf49 1836 ctx->aborting = 0;
4d2096b4 1837 ctx->hold = false;
918c66fd 1838 }
c1ae0b28
PZ
1839
1840 if (!ctx->streamon_out && !ctx->streamon_cap)
1841 ctx->bit_stream_param &= ~CODA_BIT_STREAM_END_FLAG;
186b250a
JM
1842}
1843
121cacf4 1844static const struct vb2_ops coda_qops = {
186b250a
JM
1845 .queue_setup = coda_queue_setup,
1846 .buf_prepare = coda_buf_prepare,
1847 .buf_queue = coda_buf_queue,
186b250a
JM
1848 .start_streaming = coda_start_streaming,
1849 .stop_streaming = coda_stop_streaming,
152ea1c8
PZ
1850 .wait_prepare = vb2_ops_wait_prepare,
1851 .wait_finish = vb2_ops_wait_finish,
186b250a
JM
1852};
1853
1854static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
1855{
1856 struct coda_ctx *ctx =
1857 container_of(ctrl->handler, struct coda_ctx, ctrls);
1858
8f90d15e
PZ
1859 coda_dbg(1, ctx, "s_ctrl: id = 0x%x, name = \"%s\", val = %d\n",
1860 ctrl->id, ctrl->name, ctrl->val);
186b250a
JM
1861
1862 switch (ctrl->id) {
8f35c7bc
PZ
1863 case V4L2_CID_HFLIP:
1864 if (ctrl->val)
1865 ctx->params.rot_mode |= CODA_MIR_HOR;
1866 else
1867 ctx->params.rot_mode &= ~CODA_MIR_HOR;
1868 break;
1869 case V4L2_CID_VFLIP:
1870 if (ctrl->val)
1871 ctx->params.rot_mode |= CODA_MIR_VER;
1872 else
1873 ctx->params.rot_mode &= ~CODA_MIR_VER;
1874 break;
186b250a
JM
1875 case V4L2_CID_MPEG_VIDEO_BITRATE:
1876 ctx->params.bitrate = ctrl->val / 1000;
1877 break;
1878 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
1879 ctx->params.gop_size = ctrl->val;
1880 break;
1881 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
1882 ctx->params.h264_intra_qp = ctrl->val;
1883 break;
1884 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
1885 ctx->params.h264_inter_qp = ctrl->val;
1886 break;
1a5567e3
PZ
1887 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
1888 ctx->params.h264_min_qp = ctrl->val;
1889 break;
1890 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
1891 ctx->params.h264_max_qp = ctrl->val;
1892 break;
de23b1db 1893 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
75fa6e4f 1894 ctx->params.h264_slice_alpha_c0_offset_div2 = ctrl->val;
de23b1db
PZ
1895 break;
1896 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
75fa6e4f 1897 ctx->params.h264_slice_beta_offset_div2 = ctrl->val;
de23b1db
PZ
1898 break;
1899 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
75fa6e4f 1900 ctx->params.h264_disable_deblocking_filter_idc = ctrl->val;
de23b1db 1901 break;
31489f6b
PZ
1902 case V4L2_CID_MPEG_VIDEO_H264_CONSTRAINED_INTRA_PREDICTION:
1903 ctx->params.h264_constrained_intra_pred_flag = ctrl->val;
1904 break;
3943f059
PZ
1905 case V4L2_CID_MPEG_VIDEO_H264_CHROMA_QP_INDEX_OFFSET:
1906 ctx->params.h264_chroma_qp_index_offset = ctrl->val;
1907 break;
462e8ce0
PZ
1908 case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
1909 /* TODO: switch between baseline and constrained baseline */
1f32061e
LS
1910 if (ctx->inst_type == CODA_INST_ENCODER)
1911 ctx->params.h264_profile_idc = 66;
462e8ce0
PZ
1912 break;
1913 case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
1914 /* nothing to do, this is set by the encoder */
1915 break;
186b250a
JM
1916 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
1917 ctx->params.mpeg4_intra_qp = ctrl->val;
1918 break;
1919 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
1920 ctx->params.mpeg4_inter_qp = ctrl->val;
1921 break;
462e8ce0
PZ
1922 case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:
1923 case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:
1924 /* nothing to do, these are fixed */
1925 break;
186b250a
JM
1926 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
1927 ctx->params.slice_mode = ctrl->val;
1928 break;
1929 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
1930 ctx->params.slice_max_mb = ctrl->val;
1931 break;
c566c789
PZ
1932 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
1933 ctx->params.slice_max_bits = ctrl->val * 8;
1934 break;
186b250a
JM
1935 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
1936 break;
f38f79d5
PZ
1937 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
1938 ctx->params.intra_refresh = ctrl->val;
1939 break;
7e68c533
PZ
1940 case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME:
1941 ctx->params.force_ipicture = true;
1942 break;
cb1d3a33
PZ
1943 case V4L2_CID_JPEG_COMPRESSION_QUALITY:
1944 coda_set_jpeg_compression_quality(ctx, ctrl->val);
1945 break;
1946 case V4L2_CID_JPEG_RESTART_INTERVAL:
1947 ctx->params.jpeg_restart_interval = ctrl->val;
1948 break;
da2b3b3e
PZ
1949 case V4L2_CID_MPEG_VIDEO_VBV_DELAY:
1950 ctx->params.vbv_delay = ctrl->val;
1951 break;
1952 case V4L2_CID_MPEG_VIDEO_VBV_SIZE:
1953 ctx->params.vbv_size = min(ctrl->val * 8192, 0x7fffffff);
1954 break;
186b250a 1955 default:
8f90d15e
PZ
1956 coda_dbg(1, ctx, "Invalid control, id=%d, val=%d\n",
1957 ctrl->id, ctrl->val);
186b250a
JM
1958 return -EINVAL;
1959 }
1960
1961 return 0;
1962}
1963
814c3762 1964static const struct v4l2_ctrl_ops coda_ctrl_ops = {
186b250a
JM
1965 .s_ctrl = coda_s_ctrl,
1966};
1967
bfc732f6 1968static void coda_encode_ctrls(struct coda_ctx *ctx)
186b250a 1969{
0c62c679
PZ
1970 int max_gop_size = (ctx->dev->devtype->product == CODA_DX6) ? 60 : 99;
1971
186b250a 1972 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
92b042ec 1973 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1000, 0);
186b250a 1974 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
0c62c679 1975 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 0, max_gop_size, 1, 16);
186b250a 1976 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
594a750b 1977 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
186b250a 1978 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
594a750b 1979 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
1a5567e3
PZ
1980 if (ctx->dev->devtype->product != CODA_960) {
1981 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1982 V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12);
1983 }
1984 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1985 V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51);
de23b1db 1986 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
75fa6e4f 1987 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, -6, 6, 1, 0);
de23b1db 1988 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
75fa6e4f 1989 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, -6, 6, 1, 0);
de23b1db
PZ
1990 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1991 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
75fa6e4f
PZ
1992 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED_AT_SLICE_BOUNDARY,
1993 0x0, V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
31489f6b
PZ
1994 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1995 V4L2_CID_MPEG_VIDEO_H264_CONSTRAINED_INTRA_PREDICTION, 0, 1, 1,
1996 0);
3943f059
PZ
1997 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1998 V4L2_CID_MPEG_VIDEO_H264_CHROMA_QP_INDEX_OFFSET, -12, 12, 1, 0);
462e8ce0
PZ
1999 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2000 V4L2_CID_MPEG_VIDEO_H264_PROFILE,
2001 V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE, 0x0,
2002 V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE);
1d1ca23b
PZ
2003 if (ctx->dev->devtype->product == CODA_HX4 ||
2004 ctx->dev->devtype->product == CODA_7541) {
462e8ce0
PZ
2005 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2006 V4L2_CID_MPEG_VIDEO_H264_LEVEL,
2007 V4L2_MPEG_VIDEO_H264_LEVEL_3_1,
2008 ~((1 << V4L2_MPEG_VIDEO_H264_LEVEL_2_0) |
2009 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_0) |
2010 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_1)),
2011 V4L2_MPEG_VIDEO_H264_LEVEL_3_1);
2012 }
2013 if (ctx->dev->devtype->product == CODA_960) {
2014 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2015 V4L2_CID_MPEG_VIDEO_H264_LEVEL,
2016 V4L2_MPEG_VIDEO_H264_LEVEL_4_0,
2017 ~((1 << V4L2_MPEG_VIDEO_H264_LEVEL_2_0) |
2018 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_0) |
2019 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_1) |
2020 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_2) |
2021 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_4_0)),
2022 V4L2_MPEG_VIDEO_H264_LEVEL_4_0);
2023 }
186b250a
JM
2024 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2025 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
2026 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2027 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
462e8ce0
PZ
2028 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2029 V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE,
2030 V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE, 0x0,
2031 V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE);
1d1ca23b
PZ
2032 if (ctx->dev->devtype->product == CODA_HX4 ||
2033 ctx->dev->devtype->product == CODA_7541 ||
462e8ce0
PZ
2034 ctx->dev->devtype->product == CODA_960) {
2035 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2036 V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL,
2037 V4L2_MPEG_VIDEO_MPEG4_LEVEL_5,
2038 ~(1 << V4L2_MPEG_VIDEO_MPEG4_LEVEL_5),
2039 V4L2_MPEG_VIDEO_MPEG4_LEVEL_5);
2040 }
186b250a
JM
2041 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2042 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
c566c789
PZ
2043 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
2044 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
186b250a
JM
2045 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2046 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
c566c789 2047 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
f23797b6
PZ
2048 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1,
2049 500);
186b250a
JM
2050 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2051 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
2052 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
2053 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
2054 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
f38f79d5 2055 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
f23797b6
PZ
2056 V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0,
2057 1920 * 1088 / 256, 1, 0);
da2b3b3e
PZ
2058 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2059 V4L2_CID_MPEG_VIDEO_VBV_DELAY, 0, 0x7fff, 1, 0);
2060 /*
2061 * The maximum VBV size value is 0x7fffffff bits,
2062 * one bit less than 262144 KiB
2063 */
2064 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2065 V4L2_CID_MPEG_VIDEO_VBV_SIZE, 0, 262144, 1, 0);
bfc732f6
PZ
2066}
2067
cb1d3a33
PZ
2068static void coda_jpeg_encode_ctrls(struct coda_ctx *ctx)
2069{
2070 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2071 V4L2_CID_JPEG_COMPRESSION_QUALITY, 5, 100, 1, 50);
2072 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2073 V4L2_CID_JPEG_RESTART_INTERVAL, 0, 100, 1, 0);
2074}
2075
42a68012
PZ
2076static void coda_decode_ctrls(struct coda_ctx *ctx)
2077{
42a68012
PZ
2078 u8 max;
2079
2080 ctx->h264_profile_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
2081 &coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_H264_PROFILE,
2082 V4L2_MPEG_VIDEO_H264_PROFILE_HIGH,
2083 ~((1 << V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE) |
2084 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_MAIN) |
2085 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_HIGH)),
2086 V4L2_MPEG_VIDEO_H264_PROFILE_HIGH);
2087 if (ctx->h264_profile_ctrl)
2088 ctx->h264_profile_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2089
2090 if (ctx->dev->devtype->product == CODA_HX4 ||
be9dd094 2091 ctx->dev->devtype->product == CODA_7541)
42a68012 2092 max = V4L2_MPEG_VIDEO_H264_LEVEL_4_0;
be9dd094 2093 else if (ctx->dev->devtype->product == CODA_960)
42a68012 2094 max = V4L2_MPEG_VIDEO_H264_LEVEL_4_1;
be9dd094 2095 else
42a68012 2096 return;
42a68012 2097 ctx->h264_level_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
be9dd094 2098 &coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_H264_LEVEL, max, 0, max);
42a68012
PZ
2099 if (ctx->h264_level_ctrl)
2100 ctx->h264_level_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2101}
2102
bfc732f6
PZ
2103static int coda_ctrls_setup(struct coda_ctx *ctx)
2104{
2105 v4l2_ctrl_handler_init(&ctx->ctrls, 2);
2106
2107 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2108 V4L2_CID_HFLIP, 0, 1, 1, 0);
2109 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2110 V4L2_CID_VFLIP, 0, 1, 1, 0);
cb1d3a33 2111 if (ctx->inst_type == CODA_INST_ENCODER) {
c1b6761e
PZ
2112 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2113 V4L2_CID_MIN_BUFFERS_FOR_OUTPUT,
2114 1, 1, 1, 1);
cb1d3a33
PZ
2115 if (ctx->cvd->dst_formats[0] == V4L2_PIX_FMT_JPEG)
2116 coda_jpeg_encode_ctrls(ctx);
2117 else
2118 coda_encode_ctrls(ctx);
42a68012 2119 } else {
c1b6761e
PZ
2120 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2121 V4L2_CID_MIN_BUFFERS_FOR_CAPTURE,
2122 1, 1, 1, 1);
42a68012
PZ
2123 if (ctx->cvd->src_formats[0] == V4L2_PIX_FMT_H264)
2124 coda_decode_ctrls(ctx);
cb1d3a33 2125 }
186b250a
JM
2126
2127 if (ctx->ctrls.error) {
f23797b6
PZ
2128 v4l2_err(&ctx->dev->v4l2_dev,
2129 "control initialization error (%d)",
186b250a
JM
2130 ctx->ctrls.error);
2131 return -EINVAL;
2132 }
2133
2134 return v4l2_ctrl_handler_setup(&ctx->ctrls);
2135}
2136
121cacf4
PZ
2137static int coda_queue_init(struct coda_ctx *ctx, struct vb2_queue *vq)
2138{
2139 vq->drv_priv = ctx;
2140 vq->ops = &coda_qops;
2141 vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2142 vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
2143 vq->lock = &ctx->dev->dev_mutex;
e4af23d3
KD
2144 /* One way to indicate end-of-stream for coda is to set the
2145 * bytesused == 0. However by default videobuf2 handles bytesused
2146 * equal to 0 as a special case and changes its value to the size
2147 * of the buffer. Set the allow_zero_bytesused flag, so
2148 * that videobuf2 will keep the value of bytesused intact.
2149 */
2150 vq->allow_zero_bytesused = 1;
e112146f
LS
2151 /*
2152 * We might be fine with no buffers on some of the queues, but that
2153 * would need to be reflected in job_ready(). Currently we expect all
2154 * queues to have at least one buffer queued.
2155 */
2156 vq->min_buffers_needed = 1;
53ddcc68 2157 vq->dev = &ctx->dev->plat_dev->dev;
121cacf4
PZ
2158
2159 return vb2_queue_init(vq);
2160}
2161
79924ca9
PZ
2162int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq,
2163 struct vb2_queue *dst_vq)
186b250a 2164{
186b250a
JM
2165 int ret;
2166
186b250a 2167 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
d29a8cf2 2168 src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
186b250a
JM
2169 src_vq->mem_ops = &vb2_dma_contig_memops;
2170
121cacf4 2171 ret = coda_queue_init(priv, src_vq);
186b250a
JM
2172 if (ret)
2173 return ret;
2174
186b250a 2175 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
d29a8cf2 2176 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
186b250a
JM
2177 dst_vq->mem_ops = &vb2_dma_contig_memops;
2178
121cacf4
PZ
2179 return coda_queue_init(priv, dst_vq);
2180}
2181
79924ca9
PZ
2182int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq,
2183 struct vb2_queue *dst_vq)
121cacf4
PZ
2184{
2185 int ret;
2186
2187 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
bb04aa6f
PZ
2188 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
2189 src_vq->mem_ops = &vb2_vmalloc_memops;
121cacf4
PZ
2190
2191 ret = coda_queue_init(priv, src_vq);
2192 if (ret)
2193 return ret;
2194
2195 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2196 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
2197 dst_vq->mem_ops = &vb2_dma_contig_memops;
2198
2199 return coda_queue_init(priv, dst_vq);
186b250a
JM
2200}
2201
2c11d1bd
PZ
2202/*
2203 * File operations
2204 */
2205
2206static int coda_open(struct file *file)
186b250a 2207{
2c11d1bd
PZ
2208 struct video_device *vdev = video_devdata(file);
2209 struct coda_dev *dev = video_get_drvdata(vdev);
649cfc2b
PZ
2210 struct coda_ctx *ctx;
2211 unsigned int max = ~0;
f61c0b14 2212 char *name;
918c66fd 2213 int ret;
e11f3e6e 2214 int idx;
186b250a 2215
f23797b6 2216 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
186b250a
JM
2217 if (!ctx)
2218 return -ENOMEM;
2219
649cfc2b
PZ
2220 if (dev->devtype->product == CODA_DX6)
2221 max = CODADX6_MAX_INSTANCES - 1;
2222 idx = ida_alloc_max(&dev->ida, max, GFP_KERNEL);
0cddc7e9
PZ
2223 if (idx < 0) {
2224 ret = idx;
f82bc203
FE
2225 goto err_coda_max;
2226 }
f82bc203 2227
f61c0b14 2228 name = kasprintf(GFP_KERNEL, "context%d", idx);
a7f933a5
PS
2229 if (!name) {
2230 ret = -ENOMEM;
2231 goto err_coda_name_init;
2232 }
2233
f61c0b14
PZ
2234 ctx->debugfs_entry = debugfs_create_dir(name, dev->debugfs_root);
2235 kfree(name);
2236
2c11d1bd
PZ
2237 ctx->cvd = to_coda_video_device(vdev);
2238 ctx->inst_type = ctx->cvd->type;
2239 ctx->ops = ctx->cvd->ops;
a22496c6 2240 ctx->use_bit = !ctx->cvd->direct;
32b6f206
PZ
2241 init_completion(&ctx->completion);
2242 INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
747d7646
PZ
2243 if (ctx->ops->seq_end_work)
2244 INIT_WORK(&ctx->seq_end_work, ctx->ops->seq_end_work);
186b250a
JM
2245 v4l2_fh_init(&ctx->fh, video_devdata(file));
2246 file->private_data = &ctx->fh;
2247 v4l2_fh_add(&ctx->fh);
2248 ctx->dev = dev;
e11f3e6e 2249 ctx->idx = idx;
8f90d15e
PZ
2250
2251 coda_dbg(1, ctx, "open instance (%p)\n", ctx);
2252
5677e3b0 2253 switch (dev->devtype->product) {
89548441 2254 case CODA_960:
87a420c8
PZ
2255 /*
2256 * Enabling the BWB when decoding can hang the firmware with
2257 * certain streams. The issue was tracked as ENGR00293425 by
2258 * Freescale. As a workaround, disable BWB for all decoders.
2259 * The enable_bwb module parameter allows to override this.
2260 */
2261 if (enable_bwb || ctx->inst_type == CODA_INST_ENCODER)
89ed025d 2262 ctx->frame_mem_ctrl = CODA9_FRAME_ENABLE_BWB;
2bf299cd 2263 /* fallthrough */
1d1ca23b 2264 case CODA_HX4:
2bf299cd 2265 case CODA_7541:
5677e3b0
PZ
2266 ctx->reg_idx = 0;
2267 break;
2268 default:
2269 ctx->reg_idx = idx;
2270 }
e7f3c548
MT
2271 if (ctx->dev->vdoa && !disable_vdoa) {
2272 ctx->vdoa = vdoa_context_create(dev->vdoa);
2273 if (!ctx->vdoa)
2274 v4l2_warn(&dev->v4l2_dev,
2275 "Failed to create vdoa context: not using vdoa");
2276 }
2277 ctx->use_vdoa = false;
f82bc203 2278
1e172731
PZ
2279 /* Power up and upload firmware if necessary */
2280 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
2281 if (ret < 0) {
2282 v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
2283 goto err_pm_get;
2284 }
2285
79830db2
FE
2286 ret = clk_prepare_enable(dev->clk_per);
2287 if (ret)
2288 goto err_clk_per;
2289
2290 ret = clk_prepare_enable(dev->clk_ahb);
2291 if (ret)
2292 goto err_clk_ahb;
2293
186b250a 2294 set_default_params(ctx);
a1192a17
PZ
2295 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
2296 ctx->ops->queue_init);
14604e3a
PZ
2297 if (IS_ERR(ctx->fh.m2m_ctx)) {
2298 ret = PTR_ERR(ctx->fh.m2m_ctx);
186b250a
JM
2299
2300 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
2301 __func__, ret);
f82bc203 2302 goto err_ctx_init;
186b250a 2303 }
152ea1c8 2304
186b250a
JM
2305 ret = coda_ctrls_setup(ctx);
2306 if (ret) {
2307 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
f82bc203 2308 goto err_ctrls_setup;
186b250a
JM
2309 }
2310
2311 ctx->fh.ctrl_handler = &ctx->ctrls;
2312
9082a7c6 2313 mutex_init(&ctx->bitstream_mutex);
918c66fd 2314 mutex_init(&ctx->buffer_mutex);
7cbb105f 2315 INIT_LIST_HEAD(&ctx->buffer_meta_list);
47f3fa63 2316 spin_lock_init(&ctx->buffer_meta_lock);
9082a7c6 2317
186b250a
JM
2318 return 0;
2319
f82bc203 2320err_ctrls_setup:
14604e3a 2321 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
f82bc203
FE
2322err_ctx_init:
2323 clk_disable_unprepare(dev->clk_ahb);
79830db2 2324err_clk_ahb:
f82bc203 2325 clk_disable_unprepare(dev->clk_per);
79830db2 2326err_clk_per:
1e172731
PZ
2327 pm_runtime_put_sync(&dev->plat_dev->dev);
2328err_pm_get:
186b250a
JM
2329 v4l2_fh_del(&ctx->fh);
2330 v4l2_fh_exit(&ctx->fh);
a7f933a5 2331err_coda_name_init:
649cfc2b 2332 ida_free(&dev->ida, ctx->idx);
f82bc203 2333err_coda_max:
186b250a
JM
2334 kfree(ctx);
2335 return ret;
2336}
2337
2338static int coda_release(struct file *file)
2339{
2340 struct coda_dev *dev = video_drvdata(file);
2341 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
2342
8f90d15e 2343 coda_dbg(1, ctx, "release instance (%p)\n", ctx);
186b250a 2344
a22496c6 2345 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
d4bb75f6
PZ
2346 coda_bit_stream_end_flag(ctx);
2347
918c66fd 2348 /* If this instance is running, call .job_abort and wait for it to end */
14604e3a 2349 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
918c66fd 2350
e7f3c548
MT
2351 if (ctx->vdoa)
2352 vdoa_context_destroy(ctx->vdoa);
2353
918c66fd 2354 /* In case the instance was not running, we still need to call SEQ_END */
5c718bb3 2355 if (ctx->ops->seq_end_work) {
32b6f206
PZ
2356 queue_work(dev->workqueue, &ctx->seq_end_work);
2357 flush_work(&ctx->seq_end_work);
918c66fd 2358 }
918c66fd 2359
5677e3b0
PZ
2360 if (ctx->dev->devtype->product == CODA_DX6)
2361 coda_free_aux_buf(dev, &ctx->workbuf);
2362
186b250a 2363 v4l2_ctrl_handler_free(&ctx->ctrls);
186b250a 2364 clk_disable_unprepare(dev->clk_ahb);
f82bc203 2365 clk_disable_unprepare(dev->clk_per);
1e172731 2366 pm_runtime_put_sync(&dev->plat_dev->dev);
186b250a
JM
2367 v4l2_fh_del(&ctx->fh);
2368 v4l2_fh_exit(&ctx->fh);
649cfc2b 2369 ida_free(&dev->ida, ctx->idx);
58b7677d
PZ
2370 if (ctx->ops->release)
2371 ctx->ops->release(ctx);
e1519e86 2372 debugfs_remove_recursive(ctx->debugfs_entry);
186b250a
JM
2373 kfree(ctx);
2374
2375 return 0;
2376}
2377
2c11d1bd 2378static const struct v4l2_file_operations coda_fops = {
121cacf4 2379 .owner = THIS_MODULE,
2c11d1bd 2380 .open = coda_open,
186b250a 2381 .release = coda_release,
152ea1c8 2382 .poll = v4l2_m2m_fop_poll,
186b250a 2383 .unlocked_ioctl = video_ioctl2,
152ea1c8 2384 .mmap = v4l2_m2m_fop_mmap,
186b250a
JM
2385};
2386
87048bb4 2387static int coda_hw_init(struct coda_dev *dev)
186b250a 2388{
186b250a
JM
2389 u32 data;
2390 u16 *p;
79830db2
FE
2391 int i, ret;
2392
2393 ret = clk_prepare_enable(dev->clk_per);
2394 if (ret)
1e172731 2395 goto err_clk_per;
186b250a 2396
79830db2
FE
2397 ret = clk_prepare_enable(dev->clk_ahb);
2398 if (ret)
2399 goto err_clk_ahb;
186b250a 2400
834ebbc8 2401 reset_control_reset(dev->rstc);
8f45284c 2402
186b250a
JM
2403 /*
2404 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
87048bb4
PZ
2405 * The 16-bit chars in the code buffer are in memory access
2406 * order, re-sort them to CODA order for register download.
186b250a
JM
2407 * Data in this SRAM survives a reboot.
2408 */
87048bb4
PZ
2409 p = (u16 *)dev->codebuf.vaddr;
2410 if (dev->devtype->product == CODA_DX6) {
2411 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
2412 data = CODA_DOWN_ADDRESS_SET(i) |
2413 CODA_DOWN_DATA_SET(p[i ^ 1]);
2414 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
2415 }
2416 } else {
2417 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
2418 data = CODA_DOWN_ADDRESS_SET(i) |
2419 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
2420 3 - (i % 4)]);
2421 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
2422 }
186b250a 2423 }
186b250a 2424
9acf7693
PZ
2425 /* Clear registers */
2426 for (i = 0; i < 64; i++)
2427 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
2428
186b250a 2429 /* Tell the BIT where to find everything it needs */
89548441 2430 if (dev->devtype->product == CODA_960 ||
1d1ca23b
PZ
2431 dev->devtype->product == CODA_7541 ||
2432 dev->devtype->product == CODA_HX4) {
5677e3b0
PZ
2433 coda_write(dev, dev->tempbuf.paddr,
2434 CODA_REG_BIT_TEMP_BUF_ADDR);
918c66fd 2435 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
5677e3b0
PZ
2436 } else {
2437 coda_write(dev, dev->workbuf.paddr,
2438 CODA_REG_BIT_WORK_BUF_ADDR);
2439 }
186b250a
JM
2440 coda_write(dev, dev->codebuf.paddr,
2441 CODA_REG_BIT_CODE_BUF_ADDR);
2442 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
2443
2444 /* Set default values */
2445 switch (dev->devtype->product) {
2446 case CODA_DX6:
f23797b6
PZ
2447 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH,
2448 CODA_REG_BIT_STREAM_CTRL);
186b250a
JM
2449 break;
2450 default:
f23797b6
PZ
2451 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH,
2452 CODA_REG_BIT_STREAM_CTRL);
186b250a 2453 }
89548441 2454 if (dev->devtype->product == CODA_960)
87a420c8
PZ
2455 coda_write(dev, CODA9_FRAME_ENABLE_BWB,
2456 CODA_REG_BIT_FRAME_MEM_CTRL);
89548441
PZ
2457 else
2458 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
1043667b
PZ
2459
2460 if (dev->devtype->product != CODA_DX6)
2461 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
2462
186b250a
JM
2463 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
2464 CODA_REG_BIT_INT_ENABLE);
2465
2466 /* Reset VPU and start processor */
2467 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
2468 data |= CODA_REG_RESET_ENABLE;
2469 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
2470 udelay(10);
2471 data &= ~CODA_REG_RESET_ENABLE;
2472 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
2473 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
2474
fe7554e6
PZ
2475 clk_disable_unprepare(dev->clk_ahb);
2476 clk_disable_unprepare(dev->clk_per);
2477
2478 return 0;
2479
2480err_clk_ahb:
2481 clk_disable_unprepare(dev->clk_per);
2482err_clk_per:
2483 return ret;
2484}
2485
2c11d1bd 2486static int coda_register_device(struct coda_dev *dev, int i)
121cacf4 2487{
2c11d1bd
PZ
2488 struct video_device *vfd = &dev->vfd[i];
2489
88ca3af4 2490 if (i >= dev->devtype->num_vdevs)
2c11d1bd
PZ
2491 return -EINVAL;
2492
c0decac1 2493 strscpy(vfd->name, dev->devtype->vdevs[i]->name, sizeof(vfd->name));
2c11d1bd
PZ
2494 vfd->fops = &coda_fops;
2495 vfd->ioctl_ops = &coda_ioctl_ops;
121cacf4
PZ
2496 vfd->release = video_device_release_empty,
2497 vfd->lock = &dev->dev_mutex;
2498 vfd->v4l2_dev = &dev->v4l2_dev;
2499 vfd->vfl_dir = VFL_DIR_M2M;
2500 video_set_drvdata(vfd, dev);
2501
a188a668
PZ
2502 /* Not applicable, use the selection API instead */
2503 v4l2_disable_ioctl(vfd, VIDIOC_CROPCAP);
2504 v4l2_disable_ioctl(vfd, VIDIOC_G_CROP);
2505 v4l2_disable_ioctl(vfd, VIDIOC_S_CROP);
2506
121cacf4
PZ
2507 return video_register_device(vfd, VFL_TYPE_GRABBER, 0);
2508}
2509
a1a87fa3
PZ
2510static void coda_copy_firmware(struct coda_dev *dev, const u8 * const buf,
2511 size_t size)
2512{
2513 u32 *src = (u32 *)buf;
2514
2515 /* Check if the firmware has a 16-byte Freescale header, skip it */
2516 if (buf[0] == 'M' && buf[1] == 'X')
2517 src += 4;
2518 /*
2519 * Check whether the firmware is in native order or pre-reordered for
2520 * memory access. The first instruction opcode always is 0xe40e.
2521 */
2522 if (__le16_to_cpup((__le16 *)src) == 0xe40e) {
2523 u32 *dst = dev->codebuf.vaddr;
2524 int i;
2525
2526 /* Firmware in native order, reorder while copying */
2527 if (dev->devtype->product == CODA_DX6) {
2528 for (i = 0; i < (size - 16) / 4; i++)
2529 dst[i] = (src[i] << 16) | (src[i] >> 16);
2530 } else {
2531 for (i = 0; i < (size - 16) / 4; i += 2) {
2532 dst[i] = (src[i + 1] << 16) | (src[i + 1] >> 16);
2533 dst[i + 1] = (src[i] << 16) | (src[i] >> 16);
2534 }
2535 }
2536 } else {
2537 /* Copy the already reordered firmware image */
2538 memcpy(dev->codebuf.vaddr, src, size);
2539 }
2540}
2541
2ac7f08e
PZ
2542static void coda_fw_callback(const struct firmware *fw, void *context);
2543
2544static int coda_firmware_request(struct coda_dev *dev)
2545{
1e9b71d5
PZ
2546 char *fw;
2547
2548 if (dev->firmware >= ARRAY_SIZE(dev->devtype->firmware))
2549 return -EINVAL;
2550
2551 fw = dev->devtype->firmware[dev->firmware];
2ac7f08e
PZ
2552
2553 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
2554 coda_product_name(dev->devtype->product));
2555
2556 return request_firmware_nowait(THIS_MODULE, true, fw,
2557 &dev->plat_dev->dev, GFP_KERNEL, dev,
2558 coda_fw_callback);
2559}
2560
186b250a
JM
2561static void coda_fw_callback(const struct firmware *fw, void *context)
2562{
2563 struct coda_dev *dev = context;
2564 struct platform_device *pdev = dev->plat_dev;
2c11d1bd 2565 int i, ret;
186b250a 2566
2ac7f08e 2567 if (!fw) {
1e9b71d5
PZ
2568 dev->firmware++;
2569 ret = coda_firmware_request(dev);
2570 if (ret < 0) {
2571 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
2572 goto put_pm;
2573 }
2ac7f08e
PZ
2574 return;
2575 }
1e9b71d5 2576 if (dev->firmware > 0) {
2ac7f08e
PZ
2577 /*
2578 * Since we can't suppress warnings for failed asynchronous
2579 * firmware requests, report that the fallback firmware was
2580 * found.
2581 */
2582 dev_info(&pdev->dev, "Using fallback firmware %s\n",
2583 dev->devtype->firmware[dev->firmware]);
2584 }
186b250a
JM
2585
2586 /* allocate auxiliary per-device code buffer for the BIT processor */
f61c0b14
PZ
2587 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size, "codebuf",
2588 dev->debugfs_root);
6ba53b81 2589 if (ret < 0)
c5d28e29 2590 goto put_pm;
186b250a 2591
a1a87fa3 2592 coda_copy_firmware(dev, fw->data, fw->size);
87048bb4
PZ
2593 release_firmware(fw);
2594
c5d28e29
UH
2595 ret = coda_hw_init(dev);
2596 if (ret < 0) {
2597 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
2598 goto put_pm;
186b250a
JM
2599 }
2600
c5d28e29
UH
2601 ret = coda_check_firmware(dev);
2602 if (ret < 0)
2603 goto put_pm;
2604
186b250a
JM
2605 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
2606 if (IS_ERR(dev->m2m_dev)) {
2607 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
53ddcc68 2608 goto put_pm;
186b250a
JM
2609 }
2610
2c11d1bd
PZ
2611 for (i = 0; i < dev->devtype->num_vdevs; i++) {
2612 ret = coda_register_device(dev, i);
2613 if (ret) {
2614 v4l2_err(&dev->v4l2_dev,
2615 "Failed to register %s video device: %d\n",
2616 dev->devtype->vdevs[i]->name, ret);
2617 goto rel_vfd;
2618 }
121cacf4
PZ
2619 }
2620
2621 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video[%d-%d]\n",
2c11d1bd 2622 dev->vfd[0].num, dev->vfd[i - 1].num);
186b250a 2623
c5d28e29 2624 pm_runtime_put_sync(&pdev->dev);
186b250a
JM
2625 return;
2626
2c11d1bd
PZ
2627rel_vfd:
2628 while (--i >= 0)
2629 video_unregister_device(&dev->vfd[i]);
186b250a 2630 v4l2_m2m_release(dev->m2m_dev);
c5d28e29
UH
2631put_pm:
2632 pm_runtime_put_sync(&pdev->dev);
186b250a
JM
2633}
2634
186b250a
JM
2635enum coda_platform {
2636 CODA_IMX27,
1d1ca23b 2637 CODA_IMX51,
df1e74cc 2638 CODA_IMX53,
89548441
PZ
2639 CODA_IMX6Q,
2640 CODA_IMX6DL,
186b250a
JM
2641};
2642
c06d8752 2643static const struct coda_devtype coda_devdata[] = {
186b250a 2644 [CODA_IMX27] = {
2ac7f08e
PZ
2645 .firmware = {
2646 "vpu_fw_imx27_TO2.bin",
8af7779f 2647 "vpu/vpu_fw_imx27_TO2.bin",
2ac7f08e
PZ
2648 "v4l-codadx6-imx27.bin"
2649 },
e5b0d1c6
PZ
2650 .product = CODA_DX6,
2651 .codecs = codadx6_codecs,
2652 .num_codecs = ARRAY_SIZE(codadx6_codecs),
2c11d1bd
PZ
2653 .vdevs = codadx6_video_devices,
2654 .num_vdevs = ARRAY_SIZE(codadx6_video_devices),
e5b0d1c6 2655 .workbuf_size = 288 * 1024 + FMO_SLICE_SAVE_BUF_SIZE * 8 * 1024,
401e972f 2656 .iram_size = 0xb000,
186b250a 2657 },
1d1ca23b
PZ
2658 [CODA_IMX51] = {
2659 .firmware = {
2660 "vpu_fw_imx51.bin",
2661 "vpu/vpu_fw_imx51.bin",
2662 "v4l-codahx4-imx51.bin"
2663 },
2664 .product = CODA_HX4,
2665 .codecs = codahx4_codecs,
2666 .num_codecs = ARRAY_SIZE(codahx4_codecs),
2667 .vdevs = codahx4_video_devices,
2668 .num_vdevs = ARRAY_SIZE(codahx4_video_devices),
2669 .workbuf_size = 128 * 1024,
2670 .tempbuf_size = 304 * 1024,
2671 .iram_size = 0x14000,
2672 },
df1e74cc 2673 [CODA_IMX53] = {
2ac7f08e
PZ
2674 .firmware = {
2675 "vpu_fw_imx53.bin",
8af7779f 2676 "vpu/vpu_fw_imx53.bin",
2ac7f08e
PZ
2677 "v4l-coda7541-imx53.bin"
2678 },
e5b0d1c6
PZ
2679 .product = CODA_7541,
2680 .codecs = coda7_codecs,
2681 .num_codecs = ARRAY_SIZE(coda7_codecs),
2c11d1bd
PZ
2682 .vdevs = coda7_video_devices,
2683 .num_vdevs = ARRAY_SIZE(coda7_video_devices),
e5b0d1c6 2684 .workbuf_size = 128 * 1024,
5d73fad1 2685 .tempbuf_size = 304 * 1024,
401e972f 2686 .iram_size = 0x14000,
df1e74cc 2687 },
89548441 2688 [CODA_IMX6Q] = {
2ac7f08e
PZ
2689 .firmware = {
2690 "vpu_fw_imx6q.bin",
8af7779f 2691 "vpu/vpu_fw_imx6q.bin",
2ac7f08e
PZ
2692 "v4l-coda960-imx6q.bin"
2693 },
e5b0d1c6
PZ
2694 .product = CODA_960,
2695 .codecs = coda9_codecs,
2696 .num_codecs = ARRAY_SIZE(coda9_codecs),
2c11d1bd
PZ
2697 .vdevs = coda9_video_devices,
2698 .num_vdevs = ARRAY_SIZE(coda9_video_devices),
e5b0d1c6 2699 .workbuf_size = 80 * 1024,
5d73fad1 2700 .tempbuf_size = 204 * 1024,
401e972f 2701 .iram_size = 0x21000,
89548441
PZ
2702 },
2703 [CODA_IMX6DL] = {
2ac7f08e
PZ
2704 .firmware = {
2705 "vpu_fw_imx6d.bin",
8af7779f 2706 "vpu/vpu_fw_imx6d.bin",
2ac7f08e
PZ
2707 "v4l-coda960-imx6dl.bin"
2708 },
e5b0d1c6
PZ
2709 .product = CODA_960,
2710 .codecs = coda9_codecs,
2711 .num_codecs = ARRAY_SIZE(coda9_codecs),
2c11d1bd
PZ
2712 .vdevs = coda9_video_devices,
2713 .num_vdevs = ARRAY_SIZE(coda9_video_devices),
e5b0d1c6 2714 .workbuf_size = 80 * 1024,
5d73fad1 2715 .tempbuf_size = 204 * 1024,
e047c5e0 2716 .iram_size = 0x1f000, /* leave 4k for suspend code */
89548441 2717 },
186b250a
JM
2718};
2719
89ac34aa 2720static const struct platform_device_id coda_platform_ids[] = {
186b250a
JM
2721 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
2722 { /* sentinel */ }
2723};
2724MODULE_DEVICE_TABLE(platform, coda_platform_ids);
2725
2726#ifdef CONFIG_OF
2727static const struct of_device_id coda_dt_ids[] = {
7b0dd9e6 2728 { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
1d1ca23b 2729 { .compatible = "fsl,imx51-vpu", .data = &coda_devdata[CODA_IMX51] },
df1e74cc 2730 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
89548441
PZ
2731 { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
2732 { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
186b250a
JM
2733 { /* sentinel */ }
2734};
2735MODULE_DEVICE_TABLE(of, coda_dt_ids);
2736#endif
2737
4c62e976 2738static int coda_probe(struct platform_device *pdev)
186b250a
JM
2739{
2740 const struct of_device_id *of_id =
2741 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
2742 const struct platform_device_id *pdev_id;
657eee7d
PZ
2743 struct coda_platform_data *pdata = pdev->dev.platform_data;
2744 struct device_node *np = pdev->dev.of_node;
2745 struct gen_pool *pool;
186b250a
JM
2746 struct coda_dev *dev;
2747 struct resource *res;
2748 int ret, irq;
2749
f23797b6 2750 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
6da999d9 2751 if (!dev)
186b250a 2752 return -ENOMEM;
186b250a 2753
b2f91ae3
PZ
2754 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
2755
bc717d5e 2756 if (of_id)
b2f91ae3 2757 dev->devtype = of_id->data;
bc717d5e 2758 else if (pdev_id)
b2f91ae3 2759 dev->devtype = &coda_devdata[pdev_id->driver_data];
bc717d5e
PZ
2760 else
2761 return -EINVAL;
186b250a
JM
2762
2763 spin_lock_init(&dev->irqlock);
2764
2765 dev->plat_dev = pdev;
2766 dev->clk_per = devm_clk_get(&pdev->dev, "per");
2767 if (IS_ERR(dev->clk_per)) {
2768 dev_err(&pdev->dev, "Could not get per clock\n");
2769 return PTR_ERR(dev->clk_per);
2770 }
2771
2772 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
2773 if (IS_ERR(dev->clk_ahb)) {
2774 dev_err(&pdev->dev, "Could not get ahb clock\n");
2775 return PTR_ERR(dev->clk_ahb);
2776 }
2777
2778 /* Get memory for physical registers */
2779 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
611cbbfe
PZ
2780 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
2781 if (IS_ERR(dev->regs_base))
2782 return PTR_ERR(dev->regs_base);
186b250a
JM
2783
2784 /* IRQ */
540b72e4
PZ
2785 irq = platform_get_irq_byname(pdev, "bit");
2786 if (irq < 0)
2787 irq = platform_get_irq(pdev, 0);
186b250a
JM
2788 if (irq < 0) {
2789 dev_err(&pdev->dev, "failed to get irq resource\n");
7d37743a 2790 return irq;
186b250a
JM
2791 }
2792
08c8d14e
FE
2793 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
2794 IRQF_ONESHOT, dev_name(&pdev->dev), dev);
2795 if (ret < 0) {
2796 dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
2797 return ret;
186b250a
JM
2798 }
2799
9e6b1dae
PZ
2800 dev->rstc = devm_reset_control_get_optional_exclusive(&pdev->dev,
2801 NULL);
8f45284c
PZ
2802 if (IS_ERR(dev->rstc)) {
2803 ret = PTR_ERR(dev->rstc);
834ebbc8
PZ
2804 dev_err(&pdev->dev, "failed get reset control: %d\n", ret);
2805 return ret;
8f45284c
PZ
2806 }
2807
657eee7d 2808 /* Get IRAM pool from device tree or platform data */
abdd4a70 2809 pool = of_gen_pool_get(np, "iram", 0);
657eee7d 2810 if (!pool && pdata)
73858173 2811 pool = gen_pool_get(pdata->iram_dev, NULL);
657eee7d
PZ
2812 if (!pool) {
2813 dev_err(&pdev->dev, "iram pool not available\n");
2814 return -ENOMEM;
2815 }
2816 dev->iram_pool = pool;
2817
e7f3c548
MT
2818 /* Get vdoa_data if supported by the platform */
2819 dev->vdoa = coda_get_vdoa_data();
2820 if (PTR_ERR(dev->vdoa) == -EPROBE_DEFER)
2821 return -EPROBE_DEFER;
2822
186b250a
JM
2823 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
2824 if (ret)
2825 return ret;
2826
2827 mutex_init(&dev->dev_mutex);
fcb62825 2828 mutex_init(&dev->coda_mutex);
649cfc2b 2829 ida_init(&dev->ida);
186b250a 2830
f61c0b14
PZ
2831 dev->debugfs_root = debugfs_create_dir("coda", NULL);
2832 if (!dev->debugfs_root)
2833 dev_warn(&pdev->dev, "failed to create debugfs root\n");
2834
186b250a 2835 /* allocate auxiliary per-device buffers for the BIT processor */
5d73fad1 2836 if (dev->devtype->product == CODA_DX6) {
5677e3b0 2837 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
e5b0d1c6 2838 dev->devtype->workbuf_size, "workbuf",
f61c0b14 2839 dev->debugfs_root);
6ba53b81 2840 if (ret < 0)
b7bd660a 2841 goto err_v4l2_register;
186b250a 2842 }
5d73fad1
PZ
2843
2844 if (dev->devtype->tempbuf_size) {
5677e3b0 2845 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
5d73fad1 2846 dev->devtype->tempbuf_size, "tempbuf",
f61c0b14 2847 dev->debugfs_root);
6ba53b81 2848 if (ret < 0)
b7bd660a 2849 goto err_v4l2_register;
186b250a
JM
2850 }
2851
401e972f 2852 dev->iram.size = dev->devtype->iram_size;
b313bcc9
PZ
2853 dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
2854 &dev->iram.paddr);
2855 if (!dev->iram.vaddr) {
8be31c89
PZ
2856 dev_warn(&pdev->dev, "unable to alloc iram\n");
2857 } else {
811a6934 2858 memset(dev->iram.vaddr, 0, dev->iram.size);
8be31c89
PZ
2859 dev->iram.blob.data = dev->iram.vaddr;
2860 dev->iram.blob.size = dev->iram.size;
2861 dev->iram.dentry = debugfs_create_blob("iram", 0644,
2862 dev->debugfs_root,
2863 &dev->iram.blob);
1043667b
PZ
2864 }
2865
32b6f206
PZ
2866 dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
2867 if (!dev->workqueue) {
2868 dev_err(&pdev->dev, "unable to alloc workqueue\n");
74d08d55
FE
2869 ret = -ENOMEM;
2870 goto err_v4l2_register;
32b6f206
PZ
2871 }
2872
186b250a
JM
2873 platform_set_drvdata(pdev, dev);
2874
c5d28e29
UH
2875 /*
2876 * Start activated so we can directly call coda_hw_init in
e243c7c1 2877 * coda_fw_callback regardless of whether CONFIG_PM is
c5d28e29
UH
2878 * enabled or whether the device is associated with a PM domain.
2879 */
2880 pm_runtime_get_noresume(&pdev->dev);
2881 pm_runtime_set_active(&pdev->dev);
1e172731
PZ
2882 pm_runtime_enable(&pdev->dev);
2883
c762ff1f
FE
2884 ret = coda_firmware_request(dev);
2885 if (ret)
2886 goto err_alloc_workqueue;
2887 return 0;
b7bd660a 2888
c762ff1f
FE
2889err_alloc_workqueue:
2890 destroy_workqueue(dev->workqueue);
b7bd660a
FE
2891err_v4l2_register:
2892 v4l2_device_unregister(&dev->v4l2_dev);
2893 return ret;
186b250a
JM
2894}
2895
2896static int coda_remove(struct platform_device *pdev)
2897{
2898 struct coda_dev *dev = platform_get_drvdata(pdev);
2c11d1bd 2899 int i;
186b250a 2900
2c11d1bd
PZ
2901 for (i = 0; i < ARRAY_SIZE(dev->vfd); i++) {
2902 if (video_get_drvdata(&dev->vfd[i]))
2903 video_unregister_device(&dev->vfd[i]);
2904 }
186b250a
JM
2905 if (dev->m2m_dev)
2906 v4l2_m2m_release(dev->m2m_dev);
1e172731 2907 pm_runtime_disable(&pdev->dev);
186b250a 2908 v4l2_device_unregister(&dev->v4l2_dev);
32b6f206 2909 destroy_workqueue(dev->workqueue);
b313bcc9
PZ
2910 if (dev->iram.vaddr)
2911 gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
2912 dev->iram.size);
5677e3b0
PZ
2913 coda_free_aux_buf(dev, &dev->codebuf);
2914 coda_free_aux_buf(dev, &dev->tempbuf);
2915 coda_free_aux_buf(dev, &dev->workbuf);
f61c0b14 2916 debugfs_remove_recursive(dev->debugfs_root);
649cfc2b 2917 ida_destroy(&dev->ida);
186b250a
JM
2918 return 0;
2919}
2920
e243c7c1 2921#ifdef CONFIG_PM
1e172731
PZ
2922static int coda_runtime_resume(struct device *dev)
2923{
2924 struct coda_dev *cdev = dev_get_drvdata(dev);
2925 int ret = 0;
2926
65919e6b 2927 if (dev->pm_domain && cdev->codebuf.vaddr) {
1e172731
PZ
2928 ret = coda_hw_init(cdev);
2929 if (ret)
2930 v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
2931 }
2932
2933 return ret;
2934}
2935#endif
2936
2937static const struct dev_pm_ops coda_pm_ops = {
2938 SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
2939};
2940
186b250a
JM
2941static struct platform_driver coda_driver = {
2942 .probe = coda_probe,
4c62e976 2943 .remove = coda_remove,
186b250a
JM
2944 .driver = {
2945 .name = CODA_NAME,
186b250a 2946 .of_match_table = of_match_ptr(coda_dt_ids),
1e172731 2947 .pm = &coda_pm_ops,
186b250a
JM
2948 },
2949 .id_table = coda_platform_ids,
2950};
2951
2952module_platform_driver(coda_driver);
2953
2954MODULE_LICENSE("GPL");
2955MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
2956MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");