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