2 * Coda multi-standard codec IP
4 * Copyright (C) 2012 Vista Silicon S.L.
5 * Javier Martin, <javier.martin@vista-silicon.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
14 #include <linux/clk.h>
15 #include <linux/debugfs.h>
16 #include <linux/delay.h>
17 #include <linux/firmware.h>
18 #include <linux/gcd.h>
19 #include <linux/genalloc.h>
20 #include <linux/interrupt.h>
22 #include <linux/irq.h>
23 #include <linux/kfifo.h>
24 #include <linux/module.h>
25 #include <linux/of_device.h>
26 #include <linux/platform_device.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/slab.h>
29 #include <linux/videodev2.h>
31 #include <linux/platform_data/media/coda.h>
32 #include <linux/reset.h>
34 #include <media/v4l2-ctrls.h>
35 #include <media/v4l2-device.h>
36 #include <media/v4l2-event.h>
37 #include <media/v4l2-ioctl.h>
38 #include <media/v4l2-mem2mem.h>
39 #include <media/videobuf2-v4l2.h>
40 #include <media/videobuf2-dma-contig.h>
41 #include <media/videobuf2-vmalloc.h>
46 #define CODA_NAME "coda"
48 #define CODADX6_MAX_INSTANCES 4
49 #define CODA_MAX_FORMATS 4
51 #define CODA_ISRAM_SIZE (2048 * 2)
56 #define S_ALIGN 1 /* multiple of 2 */
57 #define W_ALIGN 1 /* multiple of 2 */
58 #define H_ALIGN 1 /* multiple of 2 */
60 #define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh)
63 module_param(coda_debug
, int, 0644);
64 MODULE_PARM_DESC(coda_debug
, "Debug level (0-2)");
66 static int disable_tiling
;
67 module_param(disable_tiling
, int, 0644);
68 MODULE_PARM_DESC(disable_tiling
, "Disable tiled frame buffers");
70 static int disable_vdoa
;
71 module_param(disable_vdoa
, int, 0644);
72 MODULE_PARM_DESC(disable_vdoa
, "Disable Video Data Order Adapter tiled to raster-scan conversion");
74 static int enable_bwb
= 0;
75 module_param(enable_bwb
, int, 0644);
76 MODULE_PARM_DESC(enable_bwb
, "Enable BWB unit for decoding, may crash on certain streams");
78 void coda_write(struct coda_dev
*dev
, u32 data
, u32 reg
)
80 v4l2_dbg(2, coda_debug
, &dev
->v4l2_dev
,
81 "%s: data=0x%x, reg=0x%x\n", __func__
, data
, reg
);
82 writel(data
, dev
->regs_base
+ reg
);
85 unsigned int coda_read(struct coda_dev
*dev
, u32 reg
)
89 data
= readl(dev
->regs_base
+ reg
);
90 v4l2_dbg(2, coda_debug
, &dev
->v4l2_dev
,
91 "%s: data=0x%x, reg=0x%x\n", __func__
, data
, reg
);
95 void coda_write_base(struct coda_ctx
*ctx
, struct coda_q_data
*q_data
,
96 struct vb2_v4l2_buffer
*buf
, unsigned int reg_y
)
98 u32 base_y
= vb2_dma_contig_plane_dma_addr(&buf
->vb2_buf
, 0);
101 switch (q_data
->fourcc
) {
102 case V4L2_PIX_FMT_YUYV
:
103 /* Fallthrough: IN -H264-> CODA -NV12 MB-> VDOA -YUYV-> OUT */
104 case V4L2_PIX_FMT_NV12
:
105 case V4L2_PIX_FMT_YUV420
:
107 base_cb
= base_y
+ q_data
->bytesperline
* q_data
->height
;
108 base_cr
= base_cb
+ q_data
->bytesperline
* q_data
->height
/ 4;
110 case V4L2_PIX_FMT_YVU420
:
111 /* Switch Cb and Cr for YVU420 format */
112 base_cr
= base_y
+ q_data
->bytesperline
* q_data
->height
;
113 base_cb
= base_cr
+ q_data
->bytesperline
* q_data
->height
/ 4;
115 case V4L2_PIX_FMT_YUV422P
:
116 base_cb
= base_y
+ q_data
->bytesperline
* q_data
->height
;
117 base_cr
= base_cb
+ q_data
->bytesperline
* q_data
->height
/ 2;
120 coda_write(ctx
->dev
, base_y
, reg_y
);
121 coda_write(ctx
->dev
, base_cb
, reg_y
+ 4);
122 coda_write(ctx
->dev
, base_cr
, reg_y
+ 8);
125 #define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
126 { mode, src_fourcc, dst_fourcc, max_w, max_h }
129 * Arrays of codecs supported by each given version of Coda:
134 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
136 static const struct coda_codec codadx6_codecs
[] = {
137 CODA_CODEC(CODADX6_MODE_ENCODE_H264
, V4L2_PIX_FMT_YUV420
, V4L2_PIX_FMT_H264
, 720, 576),
138 CODA_CODEC(CODADX6_MODE_ENCODE_MP4
, V4L2_PIX_FMT_YUV420
, V4L2_PIX_FMT_MPEG4
, 720, 576),
141 static const struct coda_codec codahx4_codecs
[] = {
142 CODA_CODEC(CODA7_MODE_ENCODE_H264
, V4L2_PIX_FMT_YUV420
, V4L2_PIX_FMT_H264
, 720, 576),
143 CODA_CODEC(CODA7_MODE_DECODE_H264
, V4L2_PIX_FMT_H264
, V4L2_PIX_FMT_YUV420
, 1920, 1088),
144 CODA_CODEC(CODA7_MODE_DECODE_MP2
, V4L2_PIX_FMT_MPEG2
, V4L2_PIX_FMT_YUV420
, 1920, 1088),
145 CODA_CODEC(CODA7_MODE_DECODE_MP4
, V4L2_PIX_FMT_MPEG4
, V4L2_PIX_FMT_YUV420
, 1280, 720),
148 static const struct coda_codec coda7_codecs
[] = {
149 CODA_CODEC(CODA7_MODE_ENCODE_H264
, V4L2_PIX_FMT_YUV420
, V4L2_PIX_FMT_H264
, 1280, 720),
150 CODA_CODEC(CODA7_MODE_ENCODE_MP4
, V4L2_PIX_FMT_YUV420
, V4L2_PIX_FMT_MPEG4
, 1280, 720),
151 CODA_CODEC(CODA7_MODE_ENCODE_MJPG
, V4L2_PIX_FMT_YUV420
, V4L2_PIX_FMT_JPEG
, 8192, 8192),
152 CODA_CODEC(CODA7_MODE_DECODE_H264
, V4L2_PIX_FMT_H264
, V4L2_PIX_FMT_YUV420
, 1920, 1088),
153 CODA_CODEC(CODA7_MODE_DECODE_MP2
, V4L2_PIX_FMT_MPEG2
, V4L2_PIX_FMT_YUV420
, 1920, 1088),
154 CODA_CODEC(CODA7_MODE_DECODE_MP4
, V4L2_PIX_FMT_MPEG4
, V4L2_PIX_FMT_YUV420
, 1920, 1088),
155 CODA_CODEC(CODA7_MODE_DECODE_MJPG
, V4L2_PIX_FMT_JPEG
, V4L2_PIX_FMT_YUV420
, 8192, 8192),
158 static const struct coda_codec coda9_codecs
[] = {
159 CODA_CODEC(CODA9_MODE_ENCODE_H264
, V4L2_PIX_FMT_YUV420
, V4L2_PIX_FMT_H264
, 1920, 1088),
160 CODA_CODEC(CODA9_MODE_ENCODE_MP4
, V4L2_PIX_FMT_YUV420
, V4L2_PIX_FMT_MPEG4
, 1920, 1088),
161 CODA_CODEC(CODA9_MODE_DECODE_H264
, V4L2_PIX_FMT_H264
, V4L2_PIX_FMT_YUV420
, 1920, 1088),
162 CODA_CODEC(CODA9_MODE_DECODE_MP2
, V4L2_PIX_FMT_MPEG2
, V4L2_PIX_FMT_YUV420
, 1920, 1088),
163 CODA_CODEC(CODA9_MODE_DECODE_MP4
, V4L2_PIX_FMT_MPEG4
, V4L2_PIX_FMT_YUV420
, 1920, 1088),
166 struct coda_video_device
{
168 enum coda_inst_type type
;
169 const struct coda_context_ops
*ops
;
171 u32 src_formats
[CODA_MAX_FORMATS
];
172 u32 dst_formats
[CODA_MAX_FORMATS
];
175 static const struct coda_video_device coda_bit_encoder
= {
176 .name
= "coda-encoder",
177 .type
= CODA_INST_ENCODER
,
178 .ops
= &coda_bit_encode_ops
,
190 static const struct coda_video_device coda_bit_jpeg_encoder
= {
191 .name
= "coda-jpeg-encoder",
192 .type
= CODA_INST_ENCODER
,
193 .ops
= &coda_bit_encode_ops
,
198 V4L2_PIX_FMT_YUV422P
,
205 static const struct coda_video_device coda_bit_decoder
= {
206 .name
= "coda-decoder",
207 .type
= CODA_INST_DECODER
,
208 .ops
= &coda_bit_decode_ops
,
219 * If V4L2_PIX_FMT_YUYV should be default,
220 * set_default_params() must be adjusted.
226 static const struct coda_video_device coda_bit_jpeg_decoder
= {
227 .name
= "coda-jpeg-decoder",
228 .type
= CODA_INST_DECODER
,
229 .ops
= &coda_bit_decode_ops
,
237 V4L2_PIX_FMT_YUV422P
,
241 static const struct coda_video_device
*codadx6_video_devices
[] = {
245 static const struct coda_video_device
*codahx4_video_devices
[] = {
250 static const struct coda_video_device
*coda7_video_devices
[] = {
251 &coda_bit_jpeg_encoder
,
252 &coda_bit_jpeg_decoder
,
257 static const struct coda_video_device
*coda9_video_devices
[] = {
263 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
266 static u32
coda_format_normalize_yuv(u32 fourcc
)
269 case V4L2_PIX_FMT_NV12
:
270 case V4L2_PIX_FMT_YUV420
:
271 case V4L2_PIX_FMT_YVU420
:
272 case V4L2_PIX_FMT_YUV422P
:
273 case V4L2_PIX_FMT_YUYV
:
274 return V4L2_PIX_FMT_YUV420
;
280 static const struct coda_codec
*coda_find_codec(struct coda_dev
*dev
,
281 int src_fourcc
, int dst_fourcc
)
283 const struct coda_codec
*codecs
= dev
->devtype
->codecs
;
284 int num_codecs
= dev
->devtype
->num_codecs
;
287 src_fourcc
= coda_format_normalize_yuv(src_fourcc
);
288 dst_fourcc
= coda_format_normalize_yuv(dst_fourcc
);
289 if (src_fourcc
== dst_fourcc
)
292 for (k
= 0; k
< num_codecs
; k
++) {
293 if (codecs
[k
].src_fourcc
== src_fourcc
&&
294 codecs
[k
].dst_fourcc
== dst_fourcc
)
304 static void coda_get_max_dimensions(struct coda_dev
*dev
,
305 const struct coda_codec
*codec
,
306 int *max_w
, int *max_h
)
308 const struct coda_codec
*codecs
= dev
->devtype
->codecs
;
309 int num_codecs
= dev
->devtype
->num_codecs
;
317 for (k
= 0, w
= 0, h
= 0; k
< num_codecs
; k
++) {
318 w
= max(w
, codecs
[k
].max_w
);
319 h
= max(h
, codecs
[k
].max_h
);
329 static const struct coda_video_device
*to_coda_video_device(struct video_device
332 struct coda_dev
*dev
= video_get_drvdata(vdev
);
333 unsigned int i
= vdev
- dev
->vfd
;
335 if (i
>= dev
->devtype
->num_vdevs
)
338 return dev
->devtype
->vdevs
[i
];
341 const char *coda_product_name(int product
)
355 snprintf(buf
, sizeof(buf
), "(0x%04x)", product
);
360 static struct vdoa_data
*coda_get_vdoa_data(void)
362 struct device_node
*vdoa_node
;
363 struct platform_device
*vdoa_pdev
;
364 struct vdoa_data
*vdoa_data
= NULL
;
366 vdoa_node
= of_find_compatible_node(NULL
, NULL
, "fsl,imx6q-vdoa");
370 vdoa_pdev
= of_find_device_by_node(vdoa_node
);
374 vdoa_data
= platform_get_drvdata(vdoa_pdev
);
376 vdoa_data
= ERR_PTR(-EPROBE_DEFER
);
379 of_node_put(vdoa_node
);
385 * V4L2 ioctl() operations.
387 static int coda_querycap(struct file
*file
, void *priv
,
388 struct v4l2_capability
*cap
)
390 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
392 strscpy(cap
->driver
, CODA_NAME
, sizeof(cap
->driver
));
393 strscpy(cap
->card
, coda_product_name(ctx
->dev
->devtype
->product
),
395 strscpy(cap
->bus_info
, "platform:" CODA_NAME
, sizeof(cap
->bus_info
));
396 cap
->device_caps
= V4L2_CAP_VIDEO_M2M
| V4L2_CAP_STREAMING
;
397 cap
->capabilities
= cap
->device_caps
| V4L2_CAP_DEVICE_CAPS
;
402 static int coda_enum_fmt(struct file
*file
, void *priv
,
403 struct v4l2_fmtdesc
*f
)
405 struct video_device
*vdev
= video_devdata(file
);
406 const struct coda_video_device
*cvd
= to_coda_video_device(vdev
);
407 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
410 if (f
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
)
411 formats
= cvd
->src_formats
;
412 else if (f
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
413 formats
= cvd
->dst_formats
;
417 if (f
->index
>= CODA_MAX_FORMATS
|| formats
[f
->index
] == 0)
420 /* Skip YUYV if the vdoa is not available */
421 if (!ctx
->vdoa
&& f
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
422 formats
[f
->index
] == V4L2_PIX_FMT_YUYV
)
425 f
->pixelformat
= formats
[f
->index
];
430 static int coda_g_fmt(struct file
*file
, void *priv
,
431 struct v4l2_format
*f
)
433 struct coda_q_data
*q_data
;
434 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
436 q_data
= get_q_data(ctx
, f
->type
);
440 f
->fmt
.pix
.field
= V4L2_FIELD_NONE
;
441 f
->fmt
.pix
.pixelformat
= q_data
->fourcc
;
442 f
->fmt
.pix
.width
= q_data
->width
;
443 f
->fmt
.pix
.height
= q_data
->height
;
444 f
->fmt
.pix
.bytesperline
= q_data
->bytesperline
;
446 f
->fmt
.pix
.sizeimage
= q_data
->sizeimage
;
447 f
->fmt
.pix
.colorspace
= ctx
->colorspace
;
448 f
->fmt
.pix
.xfer_func
= ctx
->xfer_func
;
449 f
->fmt
.pix
.ycbcr_enc
= ctx
->ycbcr_enc
;
450 f
->fmt
.pix
.quantization
= ctx
->quantization
;
455 static int coda_try_pixelformat(struct coda_ctx
*ctx
, struct v4l2_format
*f
)
457 struct coda_q_data
*q_data
;
461 if (f
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
)
462 formats
= ctx
->cvd
->src_formats
;
463 else if (f
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
464 formats
= ctx
->cvd
->dst_formats
;
468 for (i
= 0; i
< CODA_MAX_FORMATS
; i
++) {
469 /* Skip YUYV if the vdoa is not available */
470 if (!ctx
->vdoa
&& f
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
471 formats
[i
] == V4L2_PIX_FMT_YUYV
)
474 if (formats
[i
] == f
->fmt
.pix
.pixelformat
) {
475 f
->fmt
.pix
.pixelformat
= formats
[i
];
480 /* Fall back to currently set pixelformat */
481 q_data
= get_q_data(ctx
, f
->type
);
482 f
->fmt
.pix
.pixelformat
= q_data
->fourcc
;
487 static int coda_try_fmt_vdoa(struct coda_ctx
*ctx
, struct v4l2_format
*f
,
492 if (f
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
503 err
= vdoa_context_configure(NULL
, round_up(f
->fmt
.pix
.width
, 16),
504 f
->fmt
.pix
.height
, f
->fmt
.pix
.pixelformat
);
514 static unsigned int coda_estimate_sizeimage(struct coda_ctx
*ctx
, u32 sizeimage
,
515 u32 width
, u32 height
)
518 * This is a rough estimate for sensible compressed buffer
519 * sizes (between 1 and 16 bits per pixel). This could be
520 * improved by better format specific worst case estimates.
522 return round_up(clamp(sizeimage
, width
* height
/ 8,
523 width
* height
* 2), PAGE_SIZE
);
526 static int coda_try_fmt(struct coda_ctx
*ctx
, const struct coda_codec
*codec
,
527 struct v4l2_format
*f
)
529 struct coda_dev
*dev
= ctx
->dev
;
530 unsigned int max_w
, max_h
;
531 enum v4l2_field field
;
533 field
= f
->fmt
.pix
.field
;
534 if (field
== V4L2_FIELD_ANY
)
535 field
= V4L2_FIELD_NONE
;
536 else if (V4L2_FIELD_NONE
!= field
)
539 /* V4L2 specification suggests the driver corrects the format struct
540 * if any of the dimensions is unsupported */
541 f
->fmt
.pix
.field
= field
;
543 coda_get_max_dimensions(dev
, codec
, &max_w
, &max_h
);
544 v4l_bound_align_image(&f
->fmt
.pix
.width
, MIN_W
, max_w
, W_ALIGN
,
545 &f
->fmt
.pix
.height
, MIN_H
, max_h
, H_ALIGN
,
548 switch (f
->fmt
.pix
.pixelformat
) {
549 case V4L2_PIX_FMT_NV12
:
550 case V4L2_PIX_FMT_YUV420
:
551 case V4L2_PIX_FMT_YVU420
:
553 * Frame stride must be at least multiple of 8,
554 * but multiple of 16 for h.264 or JPEG 4:2:x
556 f
->fmt
.pix
.bytesperline
= round_up(f
->fmt
.pix
.width
, 16);
557 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.bytesperline
*
558 f
->fmt
.pix
.height
* 3 / 2;
560 case V4L2_PIX_FMT_YUYV
:
561 f
->fmt
.pix
.bytesperline
= round_up(f
->fmt
.pix
.width
, 16) * 2;
562 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.bytesperline
*
565 case V4L2_PIX_FMT_YUV422P
:
566 f
->fmt
.pix
.bytesperline
= round_up(f
->fmt
.pix
.width
, 16);
567 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.bytesperline
*
568 f
->fmt
.pix
.height
* 2;
570 case V4L2_PIX_FMT_JPEG
:
571 case V4L2_PIX_FMT_H264
:
572 case V4L2_PIX_FMT_MPEG4
:
573 case V4L2_PIX_FMT_MPEG2
:
574 f
->fmt
.pix
.bytesperline
= 0;
575 f
->fmt
.pix
.sizeimage
= coda_estimate_sizeimage(ctx
,
576 f
->fmt
.pix
.sizeimage
,
587 static int coda_try_fmt_vid_cap(struct file
*file
, void *priv
,
588 struct v4l2_format
*f
)
590 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
591 const struct coda_q_data
*q_data_src
;
592 const struct coda_codec
*codec
;
593 struct vb2_queue
*src_vq
;
597 ret
= coda_try_pixelformat(ctx
, f
);
601 q_data_src
= get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_OUTPUT
);
604 * If the source format is already fixed, only allow the same output
607 src_vq
= v4l2_m2m_get_vq(ctx
->fh
.m2m_ctx
, V4L2_BUF_TYPE_VIDEO_OUTPUT
);
608 if (vb2_is_streaming(src_vq
)) {
609 f
->fmt
.pix
.width
= q_data_src
->width
;
610 f
->fmt
.pix
.height
= q_data_src
->height
;
613 f
->fmt
.pix
.colorspace
= ctx
->colorspace
;
614 f
->fmt
.pix
.xfer_func
= ctx
->xfer_func
;
615 f
->fmt
.pix
.ycbcr_enc
= ctx
->ycbcr_enc
;
616 f
->fmt
.pix
.quantization
= ctx
->quantization
;
618 q_data_src
= get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_OUTPUT
);
619 codec
= coda_find_codec(ctx
->dev
, q_data_src
->fourcc
,
620 f
->fmt
.pix
.pixelformat
);
624 ret
= coda_try_fmt(ctx
, codec
, f
);
628 /* The h.264 decoder only returns complete 16x16 macroblocks */
629 if (codec
&& codec
->src_fourcc
== V4L2_PIX_FMT_H264
) {
630 f
->fmt
.pix
.height
= round_up(f
->fmt
.pix
.height
, 16);
631 f
->fmt
.pix
.bytesperline
= round_up(f
->fmt
.pix
.width
, 16);
632 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.bytesperline
*
633 f
->fmt
.pix
.height
* 3 / 2;
635 ret
= coda_try_fmt_vdoa(ctx
, f
, &use_vdoa
);
639 if (f
->fmt
.pix
.pixelformat
== V4L2_PIX_FMT_YUYV
) {
643 f
->fmt
.pix
.bytesperline
= round_up(f
->fmt
.pix
.width
, 16) * 2;
644 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.bytesperline
*
652 static void coda_set_default_colorspace(struct v4l2_pix_format
*fmt
)
654 enum v4l2_colorspace colorspace
;
656 if (fmt
->pixelformat
== V4L2_PIX_FMT_JPEG
)
657 colorspace
= V4L2_COLORSPACE_JPEG
;
658 else if (fmt
->width
<= 720 && fmt
->height
<= 576)
659 colorspace
= V4L2_COLORSPACE_SMPTE170M
;
661 colorspace
= V4L2_COLORSPACE_REC709
;
663 fmt
->colorspace
= colorspace
;
664 fmt
->xfer_func
= V4L2_XFER_FUNC_DEFAULT
;
665 fmt
->ycbcr_enc
= V4L2_YCBCR_ENC_DEFAULT
;
666 fmt
->quantization
= V4L2_QUANTIZATION_DEFAULT
;
669 static int coda_try_fmt_vid_out(struct file
*file
, void *priv
,
670 struct v4l2_format
*f
)
672 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
673 struct coda_dev
*dev
= ctx
->dev
;
674 const struct coda_q_data
*q_data_dst
;
675 const struct coda_codec
*codec
;
678 ret
= coda_try_pixelformat(ctx
, f
);
682 if (f
->fmt
.pix
.colorspace
== V4L2_COLORSPACE_DEFAULT
)
683 coda_set_default_colorspace(&f
->fmt
.pix
);
685 q_data_dst
= get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_CAPTURE
);
686 codec
= coda_find_codec(dev
, f
->fmt
.pix
.pixelformat
, q_data_dst
->fourcc
);
688 return coda_try_fmt(ctx
, codec
, f
);
691 static int coda_s_fmt(struct coda_ctx
*ctx
, struct v4l2_format
*f
,
694 struct coda_q_data
*q_data
;
695 struct vb2_queue
*vq
;
697 vq
= v4l2_m2m_get_vq(ctx
->fh
.m2m_ctx
, f
->type
);
701 q_data
= get_q_data(ctx
, f
->type
);
705 if (vb2_is_busy(vq
)) {
706 v4l2_err(&ctx
->dev
->v4l2_dev
, "%s queue busy\n", __func__
);
710 q_data
->fourcc
= f
->fmt
.pix
.pixelformat
;
711 q_data
->width
= f
->fmt
.pix
.width
;
712 q_data
->height
= f
->fmt
.pix
.height
;
713 q_data
->bytesperline
= f
->fmt
.pix
.bytesperline
;
714 q_data
->sizeimage
= f
->fmt
.pix
.sizeimage
;
718 q_data
->rect
.left
= 0;
719 q_data
->rect
.top
= 0;
720 q_data
->rect
.width
= f
->fmt
.pix
.width
;
721 q_data
->rect
.height
= f
->fmt
.pix
.height
;
724 switch (f
->fmt
.pix
.pixelformat
) {
725 case V4L2_PIX_FMT_YUYV
:
726 ctx
->tiled_map_type
= GDI_TILED_FRAME_MB_RASTER_MAP
;
728 case V4L2_PIX_FMT_NV12
:
729 if (!disable_tiling
) {
730 ctx
->tiled_map_type
= GDI_TILED_FRAME_MB_RASTER_MAP
;
733 /* else fall through */
734 case V4L2_PIX_FMT_YUV420
:
735 case V4L2_PIX_FMT_YVU420
:
736 ctx
->tiled_map_type
= GDI_LINEAR_FRAME_MAP
;
742 if (ctx
->tiled_map_type
== GDI_TILED_FRAME_MB_RASTER_MAP
&&
743 !coda_try_fmt_vdoa(ctx
, f
, &ctx
->use_vdoa
) &&
745 vdoa_context_configure(ctx
->vdoa
,
746 round_up(f
->fmt
.pix
.width
, 16),
748 f
->fmt
.pix
.pixelformat
);
750 ctx
->use_vdoa
= false;
752 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
753 "Setting format for type %d, wxh: %dx%d, fmt: %4.4s %c\n",
754 f
->type
, q_data
->width
, q_data
->height
,
755 (char *)&q_data
->fourcc
,
756 (ctx
->tiled_map_type
== GDI_LINEAR_FRAME_MAP
) ? 'L' : 'T');
761 static int coda_s_fmt_vid_cap(struct file
*file
, void *priv
,
762 struct v4l2_format
*f
)
764 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
765 struct coda_q_data
*q_data_src
;
769 ret
= coda_try_fmt_vid_cap(file
, priv
, f
);
773 q_data_src
= get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_OUTPUT
);
776 r
.width
= q_data_src
->width
;
777 r
.height
= q_data_src
->height
;
779 ret
= coda_s_fmt(ctx
, f
, &r
);
783 if (ctx
->inst_type
!= CODA_INST_ENCODER
)
786 ctx
->colorspace
= f
->fmt
.pix
.colorspace
;
787 ctx
->xfer_func
= f
->fmt
.pix
.xfer_func
;
788 ctx
->ycbcr_enc
= f
->fmt
.pix
.ycbcr_enc
;
789 ctx
->quantization
= f
->fmt
.pix
.quantization
;
794 static int coda_s_fmt_vid_out(struct file
*file
, void *priv
,
795 struct v4l2_format
*f
)
797 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
798 struct v4l2_format f_cap
;
799 struct vb2_queue
*dst_vq
;
802 ret
= coda_try_fmt_vid_out(file
, priv
, f
);
806 ret
= coda_s_fmt(ctx
, f
, NULL
);
810 if (ctx
->inst_type
!= CODA_INST_DECODER
)
813 ctx
->colorspace
= f
->fmt
.pix
.colorspace
;
814 ctx
->xfer_func
= f
->fmt
.pix
.xfer_func
;
815 ctx
->ycbcr_enc
= f
->fmt
.pix
.ycbcr_enc
;
816 ctx
->quantization
= f
->fmt
.pix
.quantization
;
818 dst_vq
= v4l2_m2m_get_vq(ctx
->fh
.m2m_ctx
, V4L2_BUF_TYPE_VIDEO_CAPTURE
);
823 * Setting the capture queue format is not possible while the capture
824 * queue is still busy. This is not an error, but the user will have to
825 * make sure themselves that the capture format is set correctly before
826 * starting the output queue again.
828 if (vb2_is_busy(dst_vq
))
831 memset(&f_cap
, 0, sizeof(f_cap
));
832 f_cap
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
833 coda_g_fmt(file
, priv
, &f_cap
);
834 f_cap
.fmt
.pix
.width
= f
->fmt
.pix
.width
;
835 f_cap
.fmt
.pix
.height
= f
->fmt
.pix
.height
;
837 return coda_s_fmt_vid_cap(file
, priv
, &f_cap
);
840 static int coda_reqbufs(struct file
*file
, void *priv
,
841 struct v4l2_requestbuffers
*rb
)
843 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
846 ret
= v4l2_m2m_reqbufs(file
, ctx
->fh
.m2m_ctx
, rb
);
851 * Allow to allocate instance specific per-context buffers, such as
852 * bitstream ringbuffer, slice buffer, work buffer, etc. if needed.
854 if (rb
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
&& ctx
->ops
->reqbufs
)
855 return ctx
->ops
->reqbufs(ctx
, rb
);
860 static int coda_qbuf(struct file
*file
, void *priv
,
861 struct v4l2_buffer
*buf
)
863 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
865 return v4l2_m2m_qbuf(file
, ctx
->fh
.m2m_ctx
, buf
);
868 static bool coda_buf_is_end_of_stream(struct coda_ctx
*ctx
,
869 struct vb2_v4l2_buffer
*buf
)
871 return ((ctx
->bit_stream_param
& CODA_BIT_STREAM_END_FLAG
) &&
872 (buf
->sequence
== (ctx
->qsequence
- 1)));
875 void coda_m2m_buf_done(struct coda_ctx
*ctx
, struct vb2_v4l2_buffer
*buf
,
876 enum vb2_buffer_state state
)
878 const struct v4l2_event eos_event
= {
879 .type
= V4L2_EVENT_EOS
882 if (coda_buf_is_end_of_stream(ctx
, buf
)) {
883 buf
->flags
|= V4L2_BUF_FLAG_LAST
;
885 v4l2_event_queue_fh(&ctx
->fh
, &eos_event
);
888 v4l2_m2m_buf_done(buf
, state
);
891 static int coda_g_selection(struct file
*file
, void *fh
,
892 struct v4l2_selection
*s
)
894 struct coda_ctx
*ctx
= fh_to_ctx(fh
);
895 struct coda_q_data
*q_data
;
896 struct v4l2_rect r
, *rsel
;
898 q_data
= get_q_data(ctx
, s
->type
);
904 r
.width
= q_data
->width
;
905 r
.height
= q_data
->height
;
906 rsel
= &q_data
->rect
;
909 case V4L2_SEL_TGT_CROP_DEFAULT
:
910 case V4L2_SEL_TGT_CROP_BOUNDS
:
913 case V4L2_SEL_TGT_CROP
:
914 if (s
->type
!= V4L2_BUF_TYPE_VIDEO_OUTPUT
)
917 case V4L2_SEL_TGT_COMPOSE_BOUNDS
:
918 case V4L2_SEL_TGT_COMPOSE_PADDED
:
921 case V4L2_SEL_TGT_COMPOSE
:
922 case V4L2_SEL_TGT_COMPOSE_DEFAULT
:
923 if (s
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
935 static int coda_s_selection(struct file
*file
, void *fh
,
936 struct v4l2_selection
*s
)
938 struct coda_ctx
*ctx
= fh_to_ctx(fh
);
939 struct coda_q_data
*q_data
;
941 if (ctx
->inst_type
== CODA_INST_ENCODER
&&
942 s
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
&&
943 s
->target
== V4L2_SEL_TGT_CROP
) {
944 q_data
= get_q_data(ctx
, s
->type
);
950 s
->r
.width
= clamp(s
->r
.width
, 2U, q_data
->width
);
951 s
->r
.height
= clamp(s
->r
.height
, 2U, q_data
->height
);
953 if (s
->flags
& V4L2_SEL_FLAG_LE
) {
954 s
->r
.width
= round_up(s
->r
.width
, 2);
955 s
->r
.height
= round_up(s
->r
.height
, 2);
957 s
->r
.width
= round_down(s
->r
.width
, 2);
958 s
->r
.height
= round_down(s
->r
.height
, 2);
966 return coda_g_selection(file
, fh
, s
);
969 static int coda_try_encoder_cmd(struct file
*file
, void *fh
,
970 struct v4l2_encoder_cmd
*ec
)
972 if (ec
->cmd
!= V4L2_ENC_CMD_STOP
)
975 if (ec
->flags
& V4L2_ENC_CMD_STOP_AT_GOP_END
)
981 static int coda_encoder_cmd(struct file
*file
, void *fh
,
982 struct v4l2_encoder_cmd
*ec
)
984 struct coda_ctx
*ctx
= fh_to_ctx(fh
);
985 struct vb2_queue
*dst_vq
;
988 ret
= coda_try_encoder_cmd(file
, fh
, ec
);
992 /* Ignore encoder stop command silently in decoder context */
993 if (ctx
->inst_type
!= CODA_INST_ENCODER
)
996 /* Set the stream-end flag on this context */
997 ctx
->bit_stream_param
|= CODA_BIT_STREAM_END_FLAG
;
999 /* If there is no buffer in flight, wake up */
1000 if (!ctx
->streamon_out
|| ctx
->qsequence
== ctx
->osequence
) {
1001 dst_vq
= v4l2_m2m_get_vq(ctx
->fh
.m2m_ctx
,
1002 V4L2_BUF_TYPE_VIDEO_CAPTURE
);
1003 dst_vq
->last_buffer_dequeued
= true;
1004 wake_up(&dst_vq
->done_wq
);
1010 static int coda_try_decoder_cmd(struct file
*file
, void *fh
,
1011 struct v4l2_decoder_cmd
*dc
)
1013 if (dc
->cmd
!= V4L2_DEC_CMD_STOP
)
1016 if (dc
->flags
& V4L2_DEC_CMD_STOP_TO_BLACK
)
1019 if (!(dc
->flags
& V4L2_DEC_CMD_STOP_IMMEDIATELY
) && (dc
->stop
.pts
!= 0))
1025 static int coda_decoder_cmd(struct file
*file
, void *fh
,
1026 struct v4l2_decoder_cmd
*dc
)
1028 struct coda_ctx
*ctx
= fh_to_ctx(fh
);
1031 ret
= coda_try_decoder_cmd(file
, fh
, dc
);
1035 /* Ignore decoder stop command silently in encoder context */
1036 if (ctx
->inst_type
!= CODA_INST_DECODER
)
1039 /* Set the stream-end flag on this context */
1040 coda_bit_stream_end_flag(ctx
);
1042 v4l2_m2m_try_schedule(ctx
->fh
.m2m_ctx
);
1047 static int coda_g_parm(struct file
*file
, void *fh
, struct v4l2_streamparm
*a
)
1049 struct coda_ctx
*ctx
= fh_to_ctx(fh
);
1050 struct v4l2_fract
*tpf
;
1052 if (a
->type
!= V4L2_BUF_TYPE_VIDEO_OUTPUT
)
1055 a
->parm
.output
.capability
= V4L2_CAP_TIMEPERFRAME
;
1056 tpf
= &a
->parm
.output
.timeperframe
;
1057 tpf
->denominator
= ctx
->params
.framerate
& CODA_FRATE_RES_MASK
;
1058 tpf
->numerator
= 1 + (ctx
->params
.framerate
>>
1059 CODA_FRATE_DIV_OFFSET
);
1065 * Approximate timeperframe v4l2_fract with values that can be written
1066 * into the 16-bit CODA_FRATE_DIV and CODA_FRATE_RES fields.
1068 static void coda_approximate_timeperframe(struct v4l2_fract
*timeperframe
)
1070 struct v4l2_fract s
= *timeperframe
;
1071 struct v4l2_fract f0
;
1072 struct v4l2_fract f1
= { 1, 0 };
1073 struct v4l2_fract f2
= { 0, 1 };
1074 unsigned int i
, div
, s_denominator
;
1076 /* Lower bound is 1/65535 */
1077 if (s
.numerator
== 0 || s
.denominator
/ s
.numerator
> 65535) {
1078 timeperframe
->numerator
= 1;
1079 timeperframe
->denominator
= 65535;
1083 /* Upper bound is 65536/1, map everything above to infinity */
1084 if (s
.denominator
== 0 || s
.numerator
/ s
.denominator
> 65536) {
1085 timeperframe
->numerator
= 1;
1086 timeperframe
->denominator
= 0;
1090 /* Reduce fraction to lowest terms */
1091 div
= gcd(s
.numerator
, s
.denominator
);
1094 s
.denominator
/= div
;
1097 if (s
.numerator
<= 65536 && s
.denominator
< 65536) {
1102 /* Find successive convergents from continued fraction expansion */
1103 while (f2
.numerator
<= 65536 && f2
.denominator
< 65536) {
1107 /* Stop when f2 exactly equals timeperframe */
1108 if (s
.numerator
== 0)
1111 i
= s
.denominator
/ s
.numerator
;
1113 f2
.numerator
= f0
.numerator
+ i
* f1
.numerator
;
1114 f2
.denominator
= f0
.denominator
+ i
* f2
.denominator
;
1116 s_denominator
= s
.numerator
;
1117 s
.numerator
= s
.denominator
% s
.numerator
;
1118 s
.denominator
= s_denominator
;
1124 static uint32_t coda_timeperframe_to_frate(struct v4l2_fract
*timeperframe
)
1126 return ((timeperframe
->numerator
- 1) << CODA_FRATE_DIV_OFFSET
) |
1127 timeperframe
->denominator
;
1130 static int coda_s_parm(struct file
*file
, void *fh
, struct v4l2_streamparm
*a
)
1132 struct coda_ctx
*ctx
= fh_to_ctx(fh
);
1133 struct v4l2_fract
*tpf
;
1135 if (a
->type
!= V4L2_BUF_TYPE_VIDEO_OUTPUT
)
1138 tpf
= &a
->parm
.output
.timeperframe
;
1139 coda_approximate_timeperframe(tpf
);
1140 ctx
->params
.framerate
= coda_timeperframe_to_frate(tpf
);
1145 static int coda_subscribe_event(struct v4l2_fh
*fh
,
1146 const struct v4l2_event_subscription
*sub
)
1148 switch (sub
->type
) {
1149 case V4L2_EVENT_EOS
:
1150 return v4l2_event_subscribe(fh
, sub
, 0, NULL
);
1152 return v4l2_ctrl_subscribe_event(fh
, sub
);
1156 static const struct v4l2_ioctl_ops coda_ioctl_ops
= {
1157 .vidioc_querycap
= coda_querycap
,
1159 .vidioc_enum_fmt_vid_cap
= coda_enum_fmt
,
1160 .vidioc_g_fmt_vid_cap
= coda_g_fmt
,
1161 .vidioc_try_fmt_vid_cap
= coda_try_fmt_vid_cap
,
1162 .vidioc_s_fmt_vid_cap
= coda_s_fmt_vid_cap
,
1164 .vidioc_enum_fmt_vid_out
= coda_enum_fmt
,
1165 .vidioc_g_fmt_vid_out
= coda_g_fmt
,
1166 .vidioc_try_fmt_vid_out
= coda_try_fmt_vid_out
,
1167 .vidioc_s_fmt_vid_out
= coda_s_fmt_vid_out
,
1169 .vidioc_reqbufs
= coda_reqbufs
,
1170 .vidioc_querybuf
= v4l2_m2m_ioctl_querybuf
,
1172 .vidioc_qbuf
= coda_qbuf
,
1173 .vidioc_expbuf
= v4l2_m2m_ioctl_expbuf
,
1174 .vidioc_dqbuf
= v4l2_m2m_ioctl_dqbuf
,
1175 .vidioc_create_bufs
= v4l2_m2m_ioctl_create_bufs
,
1176 .vidioc_prepare_buf
= v4l2_m2m_ioctl_prepare_buf
,
1178 .vidioc_streamon
= v4l2_m2m_ioctl_streamon
,
1179 .vidioc_streamoff
= v4l2_m2m_ioctl_streamoff
,
1181 .vidioc_g_selection
= coda_g_selection
,
1182 .vidioc_s_selection
= coda_s_selection
,
1184 .vidioc_try_encoder_cmd
= coda_try_encoder_cmd
,
1185 .vidioc_encoder_cmd
= coda_encoder_cmd
,
1186 .vidioc_try_decoder_cmd
= coda_try_decoder_cmd
,
1187 .vidioc_decoder_cmd
= coda_decoder_cmd
,
1189 .vidioc_g_parm
= coda_g_parm
,
1190 .vidioc_s_parm
= coda_s_parm
,
1192 .vidioc_subscribe_event
= coda_subscribe_event
,
1193 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
1197 * Mem-to-mem operations.
1200 static void coda_device_run(void *m2m_priv
)
1202 struct coda_ctx
*ctx
= m2m_priv
;
1203 struct coda_dev
*dev
= ctx
->dev
;
1205 queue_work(dev
->workqueue
, &ctx
->pic_run_work
);
1208 static void coda_pic_run_work(struct work_struct
*work
)
1210 struct coda_ctx
*ctx
= container_of(work
, struct coda_ctx
, pic_run_work
);
1211 struct coda_dev
*dev
= ctx
->dev
;
1214 mutex_lock(&ctx
->buffer_mutex
);
1215 mutex_lock(&dev
->coda_mutex
);
1217 ret
= ctx
->ops
->prepare_run(ctx
);
1218 if (ret
< 0 && ctx
->inst_type
== CODA_INST_DECODER
) {
1219 mutex_unlock(&dev
->coda_mutex
);
1220 mutex_unlock(&ctx
->buffer_mutex
);
1221 /* job_finish scheduled by prepare_decode */
1225 if (!wait_for_completion_timeout(&ctx
->completion
,
1226 msecs_to_jiffies(1000))) {
1227 dev_err(&dev
->plat_dev
->dev
, "CODA PIC_RUN timeout\n");
1233 if (ctx
->ops
->run_timeout
)
1234 ctx
->ops
->run_timeout(ctx
);
1235 } else if (!ctx
->aborting
) {
1236 ctx
->ops
->finish_run(ctx
);
1239 if ((ctx
->aborting
|| (!ctx
->streamon_cap
&& !ctx
->streamon_out
)) &&
1240 ctx
->ops
->seq_end_work
)
1241 queue_work(dev
->workqueue
, &ctx
->seq_end_work
);
1243 mutex_unlock(&dev
->coda_mutex
);
1244 mutex_unlock(&ctx
->buffer_mutex
);
1246 v4l2_m2m_job_finish(ctx
->dev
->m2m_dev
, ctx
->fh
.m2m_ctx
);
1249 static int coda_job_ready(void *m2m_priv
)
1251 struct coda_ctx
*ctx
= m2m_priv
;
1252 int src_bufs
= v4l2_m2m_num_src_bufs_ready(ctx
->fh
.m2m_ctx
);
1255 * For both 'P' and 'key' frame cases 1 picture
1256 * and 1 frame are needed. In the decoder case,
1257 * the compressed frame can be in the bitstream.
1259 if (!src_bufs
&& ctx
->inst_type
!= CODA_INST_DECODER
) {
1260 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
1261 "not ready: not enough video buffers.\n");
1265 if (!v4l2_m2m_num_dst_bufs_ready(ctx
->fh
.m2m_ctx
)) {
1266 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
1267 "not ready: not enough video capture buffers.\n");
1271 if (ctx
->inst_type
== CODA_INST_DECODER
&& ctx
->use_bit
) {
1272 bool stream_end
= ctx
->bit_stream_param
&
1273 CODA_BIT_STREAM_END_FLAG
;
1274 int num_metas
= ctx
->num_metas
;
1277 count
= hweight32(ctx
->frm_dis_flg
);
1278 if (ctx
->use_vdoa
&& count
>= (ctx
->num_internal_frames
- 1)) {
1279 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
1280 "%d: not ready: all internal buffers in use: %d/%d (0x%x)",
1281 ctx
->idx
, count
, ctx
->num_internal_frames
,
1286 if (ctx
->hold
&& !src_bufs
) {
1287 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
1288 "%d: not ready: on hold for more buffers.\n",
1293 if (!stream_end
&& (num_metas
+ src_bufs
) < 2) {
1294 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
1295 "%d: not ready: need 2 buffers available (%d, %d)\n",
1296 ctx
->idx
, num_metas
, src_bufs
);
1301 if (!src_bufs
&& !stream_end
&&
1302 (coda_get_bitstream_payload(ctx
) < 512)) {
1303 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
1304 "%d: not ready: not enough bitstream data (%d).\n",
1305 ctx
->idx
, coda_get_bitstream_payload(ctx
));
1310 if (ctx
->aborting
) {
1311 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
1312 "not ready: aborting\n");
1316 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
1322 static void coda_job_abort(void *priv
)
1324 struct coda_ctx
*ctx
= priv
;
1328 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
1332 static const struct v4l2_m2m_ops coda_m2m_ops
= {
1333 .device_run
= coda_device_run
,
1334 .job_ready
= coda_job_ready
,
1335 .job_abort
= coda_job_abort
,
1338 static void set_default_params(struct coda_ctx
*ctx
)
1340 unsigned int max_w
, max_h
, usize
, csize
;
1342 ctx
->codec
= coda_find_codec(ctx
->dev
, ctx
->cvd
->src_formats
[0],
1343 ctx
->cvd
->dst_formats
[0]);
1344 max_w
= min(ctx
->codec
->max_w
, 1920U);
1345 max_h
= min(ctx
->codec
->max_h
, 1088U);
1346 usize
= max_w
* max_h
* 3 / 2;
1347 csize
= coda_estimate_sizeimage(ctx
, usize
, max_w
, max_h
);
1349 ctx
->params
.codec_mode
= ctx
->codec
->mode
;
1350 if (ctx
->cvd
->src_formats
[0] == V4L2_PIX_FMT_JPEG
)
1351 ctx
->colorspace
= V4L2_COLORSPACE_JPEG
;
1353 ctx
->colorspace
= V4L2_COLORSPACE_REC709
;
1354 ctx
->xfer_func
= V4L2_XFER_FUNC_DEFAULT
;
1355 ctx
->ycbcr_enc
= V4L2_YCBCR_ENC_DEFAULT
;
1356 ctx
->quantization
= V4L2_QUANTIZATION_DEFAULT
;
1357 ctx
->params
.framerate
= 30;
1359 /* Default formats for output and input queues */
1360 ctx
->q_data
[V4L2_M2M_SRC
].fourcc
= ctx
->cvd
->src_formats
[0];
1361 ctx
->q_data
[V4L2_M2M_DST
].fourcc
= ctx
->cvd
->dst_formats
[0];
1362 ctx
->q_data
[V4L2_M2M_SRC
].width
= max_w
;
1363 ctx
->q_data
[V4L2_M2M_SRC
].height
= max_h
;
1364 ctx
->q_data
[V4L2_M2M_DST
].width
= max_w
;
1365 ctx
->q_data
[V4L2_M2M_DST
].height
= max_h
;
1366 if (ctx
->codec
->src_fourcc
== V4L2_PIX_FMT_YUV420
) {
1367 ctx
->q_data
[V4L2_M2M_SRC
].bytesperline
= max_w
;
1368 ctx
->q_data
[V4L2_M2M_SRC
].sizeimage
= usize
;
1369 ctx
->q_data
[V4L2_M2M_DST
].bytesperline
= 0;
1370 ctx
->q_data
[V4L2_M2M_DST
].sizeimage
= csize
;
1372 ctx
->q_data
[V4L2_M2M_SRC
].bytesperline
= 0;
1373 ctx
->q_data
[V4L2_M2M_SRC
].sizeimage
= csize
;
1374 ctx
->q_data
[V4L2_M2M_DST
].bytesperline
= max_w
;
1375 ctx
->q_data
[V4L2_M2M_DST
].sizeimage
= usize
;
1377 ctx
->q_data
[V4L2_M2M_SRC
].rect
.width
= max_w
;
1378 ctx
->q_data
[V4L2_M2M_SRC
].rect
.height
= max_h
;
1379 ctx
->q_data
[V4L2_M2M_DST
].rect
.width
= max_w
;
1380 ctx
->q_data
[V4L2_M2M_DST
].rect
.height
= max_h
;
1383 * Since the RBC2AXI logic only supports a single chroma plane,
1384 * macroblock tiling only works for to NV12 pixel format.
1386 ctx
->tiled_map_type
= GDI_LINEAR_FRAME_MAP
;
1392 static int coda_queue_setup(struct vb2_queue
*vq
,
1393 unsigned int *nbuffers
, unsigned int *nplanes
,
1394 unsigned int sizes
[], struct device
*alloc_devs
[])
1396 struct coda_ctx
*ctx
= vb2_get_drv_priv(vq
);
1397 struct coda_q_data
*q_data
;
1400 q_data
= get_q_data(ctx
, vq
->type
);
1401 size
= q_data
->sizeimage
;
1406 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
1407 "get %d buffer(s) of size %d each.\n", *nbuffers
, size
);
1412 static int coda_buf_prepare(struct vb2_buffer
*vb
)
1414 struct coda_ctx
*ctx
= vb2_get_drv_priv(vb
->vb2_queue
);
1415 struct coda_q_data
*q_data
;
1417 q_data
= get_q_data(ctx
, vb
->vb2_queue
->type
);
1419 if (vb2_plane_size(vb
, 0) < q_data
->sizeimage
) {
1420 v4l2_warn(&ctx
->dev
->v4l2_dev
,
1421 "%s data will not fit into plane (%lu < %lu)\n",
1422 __func__
, vb2_plane_size(vb
, 0),
1423 (long)q_data
->sizeimage
);
1430 static void coda_update_menu_ctrl(struct v4l2_ctrl
*ctrl
, int value
)
1435 v4l2_ctrl_lock(ctrl
);
1438 * Extend the control range if the parsed stream contains a known but
1439 * unsupported value or level.
1441 if (value
> ctrl
->maximum
) {
1442 __v4l2_ctrl_modify_range(ctrl
, ctrl
->minimum
, value
,
1443 ctrl
->menu_skip_mask
& ~(1 << value
),
1444 ctrl
->default_value
);
1445 } else if (value
< ctrl
->minimum
) {
1446 __v4l2_ctrl_modify_range(ctrl
, value
, ctrl
->maximum
,
1447 ctrl
->menu_skip_mask
& ~(1 << value
),
1448 ctrl
->default_value
);
1451 __v4l2_ctrl_s_ctrl(ctrl
, value
);
1453 v4l2_ctrl_unlock(ctrl
);
1456 static void coda_update_h264_profile_ctrl(struct coda_ctx
*ctx
)
1458 const char * const *profile_names
;
1461 profile
= coda_h264_profile(ctx
->params
.h264_profile_idc
);
1463 v4l2_warn(&ctx
->dev
->v4l2_dev
, "Invalid H264 Profile: %u\n",
1464 ctx
->params
.h264_profile_idc
);
1468 coda_update_menu_ctrl(ctx
->h264_profile_ctrl
, profile
);
1470 profile_names
= v4l2_ctrl_get_menu(V4L2_CID_MPEG_VIDEO_H264_PROFILE
);
1472 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
, "Parsed H264 Profile: %s\n",
1473 profile_names
[profile
]);
1476 static void coda_update_h264_level_ctrl(struct coda_ctx
*ctx
)
1478 const char * const *level_names
;
1481 level
= coda_h264_level(ctx
->params
.h264_level_idc
);
1483 v4l2_warn(&ctx
->dev
->v4l2_dev
, "Invalid H264 Level: %u\n",
1484 ctx
->params
.h264_level_idc
);
1488 coda_update_menu_ctrl(ctx
->h264_level_ctrl
, level
);
1490 level_names
= v4l2_ctrl_get_menu(V4L2_CID_MPEG_VIDEO_H264_LEVEL
);
1492 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
, "Parsed H264 Level: %s\n",
1493 level_names
[level
]);
1496 static void coda_buf_queue(struct vb2_buffer
*vb
)
1498 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
1499 struct coda_ctx
*ctx
= vb2_get_drv_priv(vb
->vb2_queue
);
1500 struct vb2_queue
*vq
= vb
->vb2_queue
;
1501 struct coda_q_data
*q_data
;
1503 q_data
= get_q_data(ctx
, vb
->vb2_queue
->type
);
1506 * In the decoder case, immediately try to copy the buffer into the
1507 * bitstream ringbuffer and mark it as ready to be dequeued.
1509 if (ctx
->bitstream
.size
&& vq
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
) {
1511 * For backwards compatibility, queuing an empty buffer marks
1514 if (vb2_get_plane_payload(vb
, 0) == 0)
1515 coda_bit_stream_end_flag(ctx
);
1517 if (q_data
->fourcc
== V4L2_PIX_FMT_H264
) {
1519 * Unless already done, try to obtain profile_idc and
1520 * level_idc from the SPS header. This allows to decide
1521 * whether to enable reordering during sequence
1524 if (!ctx
->params
.h264_profile_idc
) {
1525 coda_sps_parse_profile(ctx
, vb
);
1526 coda_update_h264_profile_ctrl(ctx
);
1527 coda_update_h264_level_ctrl(ctx
);
1531 mutex_lock(&ctx
->bitstream_mutex
);
1532 v4l2_m2m_buf_queue(ctx
->fh
.m2m_ctx
, vbuf
);
1533 if (vb2_is_streaming(vb
->vb2_queue
))
1534 /* This set buf->sequence = ctx->qsequence++ */
1535 coda_fill_bitstream(ctx
, NULL
);
1536 mutex_unlock(&ctx
->bitstream_mutex
);
1538 if (ctx
->inst_type
== CODA_INST_ENCODER
&&
1539 vq
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
)
1540 vbuf
->sequence
= ctx
->qsequence
++;
1541 v4l2_m2m_buf_queue(ctx
->fh
.m2m_ctx
, vbuf
);
1545 int coda_alloc_aux_buf(struct coda_dev
*dev
, struct coda_aux_buf
*buf
,
1546 size_t size
, const char *name
, struct dentry
*parent
)
1548 buf
->vaddr
= dma_alloc_coherent(&dev
->plat_dev
->dev
, size
, &buf
->paddr
,
1551 v4l2_err(&dev
->v4l2_dev
,
1552 "Failed to allocate %s buffer of size %zu\n",
1559 if (name
&& parent
) {
1560 buf
->blob
.data
= buf
->vaddr
;
1561 buf
->blob
.size
= size
;
1562 buf
->dentry
= debugfs_create_blob(name
, 0644, parent
,
1565 dev_warn(&dev
->plat_dev
->dev
,
1566 "failed to create debugfs entry %s\n", name
);
1572 void coda_free_aux_buf(struct coda_dev
*dev
,
1573 struct coda_aux_buf
*buf
)
1576 dma_free_coherent(&dev
->plat_dev
->dev
, buf
->size
,
1577 buf
->vaddr
, buf
->paddr
);
1580 debugfs_remove(buf
->dentry
);
1585 static int coda_start_streaming(struct vb2_queue
*q
, unsigned int count
)
1587 struct coda_ctx
*ctx
= vb2_get_drv_priv(q
);
1588 struct v4l2_device
*v4l2_dev
= &ctx
->dev
->v4l2_dev
;
1589 struct coda_q_data
*q_data_src
, *q_data_dst
;
1590 struct v4l2_m2m_buffer
*m2m_buf
, *tmp
;
1591 struct vb2_v4l2_buffer
*buf
;
1592 struct list_head list
;
1598 INIT_LIST_HEAD(&list
);
1600 q_data_src
= get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_OUTPUT
);
1601 if (q
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
) {
1602 if (ctx
->inst_type
== CODA_INST_DECODER
&& ctx
->use_bit
) {
1603 /* copy the buffers that were queued before streamon */
1604 mutex_lock(&ctx
->bitstream_mutex
);
1605 coda_fill_bitstream(ctx
, &list
);
1606 mutex_unlock(&ctx
->bitstream_mutex
);
1608 if (coda_get_bitstream_payload(ctx
) < 512) {
1614 ctx
->streamon_out
= 1;
1616 ctx
->streamon_cap
= 1;
1619 /* Don't start the coda unless both queues are on */
1620 if (!(ctx
->streamon_out
&& ctx
->streamon_cap
))
1623 q_data_dst
= get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_CAPTURE
);
1624 if ((q_data_src
->rect
.width
!= q_data_dst
->width
&&
1625 round_up(q_data_src
->rect
.width
, 16) != q_data_dst
->width
) ||
1626 (q_data_src
->rect
.height
!= q_data_dst
->height
&&
1627 round_up(q_data_src
->rect
.height
, 16) != q_data_dst
->height
)) {
1628 v4l2_err(v4l2_dev
, "can't convert %dx%d to %dx%d\n",
1629 q_data_src
->rect
.width
, q_data_src
->rect
.height
,
1630 q_data_dst
->width
, q_data_dst
->height
);
1635 /* Allow BIT decoder device_run with no new buffers queued */
1636 if (ctx
->inst_type
== CODA_INST_DECODER
&& ctx
->use_bit
)
1637 v4l2_m2m_set_src_buffered(ctx
->fh
.m2m_ctx
, true);
1639 ctx
->gopcounter
= ctx
->params
.gop_size
- 1;
1641 ctx
->codec
= coda_find_codec(ctx
->dev
, q_data_src
->fourcc
,
1642 q_data_dst
->fourcc
);
1644 v4l2_err(v4l2_dev
, "couldn't tell instance type.\n");
1649 if (q_data_dst
->fourcc
== V4L2_PIX_FMT_JPEG
)
1650 ctx
->params
.gop_size
= 1;
1651 ctx
->gopcounter
= ctx
->params
.gop_size
- 1;
1653 ret
= ctx
->ops
->start_streaming(ctx
);
1654 if (ctx
->inst_type
== CODA_INST_DECODER
) {
1662 if (q
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
) {
1663 list_for_each_entry_safe(m2m_buf
, tmp
, &list
, list
) {
1664 list_del(&m2m_buf
->list
);
1665 v4l2_m2m_buf_done(&m2m_buf
->vb
, VB2_BUF_STATE_DONE
);
1671 if (q
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
) {
1672 list_for_each_entry_safe(m2m_buf
, tmp
, &list
, list
) {
1673 list_del(&m2m_buf
->list
);
1674 v4l2_m2m_buf_done(&m2m_buf
->vb
, VB2_BUF_STATE_QUEUED
);
1676 while ((buf
= v4l2_m2m_src_buf_remove(ctx
->fh
.m2m_ctx
)))
1677 v4l2_m2m_buf_done(buf
, VB2_BUF_STATE_QUEUED
);
1679 while ((buf
= v4l2_m2m_dst_buf_remove(ctx
->fh
.m2m_ctx
)))
1680 v4l2_m2m_buf_done(buf
, VB2_BUF_STATE_QUEUED
);
1685 static void coda_stop_streaming(struct vb2_queue
*q
)
1687 struct coda_ctx
*ctx
= vb2_get_drv_priv(q
);
1688 struct coda_dev
*dev
= ctx
->dev
;
1689 struct vb2_v4l2_buffer
*buf
;
1690 unsigned long flags
;
1693 stop
= ctx
->streamon_out
&& ctx
->streamon_cap
;
1695 if (q
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
) {
1696 v4l2_dbg(1, coda_debug
, &dev
->v4l2_dev
,
1697 "%s: output\n", __func__
);
1698 ctx
->streamon_out
= 0;
1700 coda_bit_stream_end_flag(ctx
);
1704 while ((buf
= v4l2_m2m_src_buf_remove(ctx
->fh
.m2m_ctx
)))
1705 v4l2_m2m_buf_done(buf
, VB2_BUF_STATE_ERROR
);
1707 v4l2_dbg(1, coda_debug
, &dev
->v4l2_dev
,
1708 "%s: capture\n", __func__
);
1709 ctx
->streamon_cap
= 0;
1712 ctx
->sequence_offset
= 0;
1714 while ((buf
= v4l2_m2m_dst_buf_remove(ctx
->fh
.m2m_ctx
)))
1715 v4l2_m2m_buf_done(buf
, VB2_BUF_STATE_ERROR
);
1719 struct coda_buffer_meta
*meta
;
1721 if (ctx
->ops
->seq_end_work
) {
1722 queue_work(dev
->workqueue
, &ctx
->seq_end_work
);
1723 flush_work(&ctx
->seq_end_work
);
1725 spin_lock_irqsave(&ctx
->buffer_meta_lock
, flags
);
1726 while (!list_empty(&ctx
->buffer_meta_list
)) {
1727 meta
= list_first_entry(&ctx
->buffer_meta_list
,
1728 struct coda_buffer_meta
, list
);
1729 list_del(&meta
->list
);
1733 spin_unlock_irqrestore(&ctx
->buffer_meta_lock
, flags
);
1734 kfifo_init(&ctx
->bitstream_fifo
,
1735 ctx
->bitstream
.vaddr
, ctx
->bitstream
.size
);
1736 ctx
->runcounter
= 0;
1741 if (!ctx
->streamon_out
&& !ctx
->streamon_cap
)
1742 ctx
->bit_stream_param
&= ~CODA_BIT_STREAM_END_FLAG
;
1745 static const struct vb2_ops coda_qops
= {
1746 .queue_setup
= coda_queue_setup
,
1747 .buf_prepare
= coda_buf_prepare
,
1748 .buf_queue
= coda_buf_queue
,
1749 .start_streaming
= coda_start_streaming
,
1750 .stop_streaming
= coda_stop_streaming
,
1751 .wait_prepare
= vb2_ops_wait_prepare
,
1752 .wait_finish
= vb2_ops_wait_finish
,
1755 static int coda_s_ctrl(struct v4l2_ctrl
*ctrl
)
1757 struct coda_ctx
*ctx
=
1758 container_of(ctrl
->handler
, struct coda_ctx
, ctrls
);
1760 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
1761 "s_ctrl: id = %d, val = %d\n", ctrl
->id
, ctrl
->val
);
1764 case V4L2_CID_HFLIP
:
1766 ctx
->params
.rot_mode
|= CODA_MIR_HOR
;
1768 ctx
->params
.rot_mode
&= ~CODA_MIR_HOR
;
1770 case V4L2_CID_VFLIP
:
1772 ctx
->params
.rot_mode
|= CODA_MIR_VER
;
1774 ctx
->params
.rot_mode
&= ~CODA_MIR_VER
;
1776 case V4L2_CID_MPEG_VIDEO_BITRATE
:
1777 ctx
->params
.bitrate
= ctrl
->val
/ 1000;
1779 case V4L2_CID_MPEG_VIDEO_GOP_SIZE
:
1780 ctx
->params
.gop_size
= ctrl
->val
;
1782 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP
:
1783 ctx
->params
.h264_intra_qp
= ctrl
->val
;
1785 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP
:
1786 ctx
->params
.h264_inter_qp
= ctrl
->val
;
1788 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP
:
1789 ctx
->params
.h264_min_qp
= ctrl
->val
;
1791 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP
:
1792 ctx
->params
.h264_max_qp
= ctrl
->val
;
1794 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA
:
1795 ctx
->params
.h264_deblk_alpha
= ctrl
->val
;
1797 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA
:
1798 ctx
->params
.h264_deblk_beta
= ctrl
->val
;
1800 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE
:
1801 ctx
->params
.h264_deblk_enabled
= (ctrl
->val
==
1802 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED
);
1804 case V4L2_CID_MPEG_VIDEO_H264_PROFILE
:
1805 /* TODO: switch between baseline and constrained baseline */
1806 if (ctx
->inst_type
== CODA_INST_ENCODER
)
1807 ctx
->params
.h264_profile_idc
= 66;
1809 case V4L2_CID_MPEG_VIDEO_H264_LEVEL
:
1810 /* nothing to do, this is set by the encoder */
1812 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP
:
1813 ctx
->params
.mpeg4_intra_qp
= ctrl
->val
;
1815 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP
:
1816 ctx
->params
.mpeg4_inter_qp
= ctrl
->val
;
1818 case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE
:
1819 case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL
:
1820 /* nothing to do, these are fixed */
1822 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE
:
1823 ctx
->params
.slice_mode
= ctrl
->val
;
1825 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB
:
1826 ctx
->params
.slice_max_mb
= ctrl
->val
;
1828 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES
:
1829 ctx
->params
.slice_max_bits
= ctrl
->val
* 8;
1831 case V4L2_CID_MPEG_VIDEO_HEADER_MODE
:
1833 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB
:
1834 ctx
->params
.intra_refresh
= ctrl
->val
;
1836 case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME
:
1837 ctx
->params
.force_ipicture
= true;
1839 case V4L2_CID_JPEG_COMPRESSION_QUALITY
:
1840 coda_set_jpeg_compression_quality(ctx
, ctrl
->val
);
1842 case V4L2_CID_JPEG_RESTART_INTERVAL
:
1843 ctx
->params
.jpeg_restart_interval
= ctrl
->val
;
1845 case V4L2_CID_MPEG_VIDEO_VBV_DELAY
:
1846 ctx
->params
.vbv_delay
= ctrl
->val
;
1848 case V4L2_CID_MPEG_VIDEO_VBV_SIZE
:
1849 ctx
->params
.vbv_size
= min(ctrl
->val
* 8192, 0x7fffffff);
1852 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
1853 "Invalid control, id=%d, val=%d\n",
1854 ctrl
->id
, ctrl
->val
);
1861 static const struct v4l2_ctrl_ops coda_ctrl_ops
= {
1862 .s_ctrl
= coda_s_ctrl
,
1865 static void coda_encode_ctrls(struct coda_ctx
*ctx
)
1867 int max_gop_size
= (ctx
->dev
->devtype
->product
== CODA_DX6
) ? 60 : 99;
1869 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1870 V4L2_CID_MPEG_VIDEO_BITRATE
, 0, 32767000, 1000, 0);
1871 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1872 V4L2_CID_MPEG_VIDEO_GOP_SIZE
, 0, max_gop_size
, 1, 16);
1873 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1874 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP
, 0, 51, 1, 25);
1875 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1876 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP
, 0, 51, 1, 25);
1877 if (ctx
->dev
->devtype
->product
!= CODA_960
) {
1878 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1879 V4L2_CID_MPEG_VIDEO_H264_MIN_QP
, 0, 51, 1, 12);
1881 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1882 V4L2_CID_MPEG_VIDEO_H264_MAX_QP
, 0, 51, 1, 51);
1883 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1884 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA
, 0, 15, 1, 0);
1885 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1886 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA
, 0, 15, 1, 0);
1887 v4l2_ctrl_new_std_menu(&ctx
->ctrls
, &coda_ctrl_ops
,
1888 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE
,
1889 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED
, 0x0,
1890 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED
);
1891 v4l2_ctrl_new_std_menu(&ctx
->ctrls
, &coda_ctrl_ops
,
1892 V4L2_CID_MPEG_VIDEO_H264_PROFILE
,
1893 V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE
, 0x0,
1894 V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE
);
1895 if (ctx
->dev
->devtype
->product
== CODA_HX4
||
1896 ctx
->dev
->devtype
->product
== CODA_7541
) {
1897 v4l2_ctrl_new_std_menu(&ctx
->ctrls
, &coda_ctrl_ops
,
1898 V4L2_CID_MPEG_VIDEO_H264_LEVEL
,
1899 V4L2_MPEG_VIDEO_H264_LEVEL_3_1
,
1900 ~((1 << V4L2_MPEG_VIDEO_H264_LEVEL_2_0
) |
1901 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_0
) |
1902 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_1
)),
1903 V4L2_MPEG_VIDEO_H264_LEVEL_3_1
);
1905 if (ctx
->dev
->devtype
->product
== CODA_960
) {
1906 v4l2_ctrl_new_std_menu(&ctx
->ctrls
, &coda_ctrl_ops
,
1907 V4L2_CID_MPEG_VIDEO_H264_LEVEL
,
1908 V4L2_MPEG_VIDEO_H264_LEVEL_4_0
,
1909 ~((1 << V4L2_MPEG_VIDEO_H264_LEVEL_2_0
) |
1910 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_0
) |
1911 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_1
) |
1912 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_2
) |
1913 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_4_0
)),
1914 V4L2_MPEG_VIDEO_H264_LEVEL_4_0
);
1916 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1917 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP
, 1, 31, 1, 2);
1918 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1919 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP
, 1, 31, 1, 2);
1920 v4l2_ctrl_new_std_menu(&ctx
->ctrls
, &coda_ctrl_ops
,
1921 V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE
,
1922 V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE
, 0x0,
1923 V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE
);
1924 if (ctx
->dev
->devtype
->product
== CODA_HX4
||
1925 ctx
->dev
->devtype
->product
== CODA_7541
||
1926 ctx
->dev
->devtype
->product
== CODA_960
) {
1927 v4l2_ctrl_new_std_menu(&ctx
->ctrls
, &coda_ctrl_ops
,
1928 V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL
,
1929 V4L2_MPEG_VIDEO_MPEG4_LEVEL_5
,
1930 ~(1 << V4L2_MPEG_VIDEO_MPEG4_LEVEL_5
),
1931 V4L2_MPEG_VIDEO_MPEG4_LEVEL_5
);
1933 v4l2_ctrl_new_std_menu(&ctx
->ctrls
, &coda_ctrl_ops
,
1934 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE
,
1935 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES
, 0x0,
1936 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE
);
1937 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1938 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB
, 1, 0x3fffffff, 1, 1);
1939 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1940 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES
, 1, 0x3fffffff, 1,
1942 v4l2_ctrl_new_std_menu(&ctx
->ctrls
, &coda_ctrl_ops
,
1943 V4L2_CID_MPEG_VIDEO_HEADER_MODE
,
1944 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME
,
1945 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE
),
1946 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME
);
1947 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1948 V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB
, 0,
1949 1920 * 1088 / 256, 1, 0);
1950 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1951 V4L2_CID_MPEG_VIDEO_VBV_DELAY
, 0, 0x7fff, 1, 0);
1953 * The maximum VBV size value is 0x7fffffff bits,
1954 * one bit less than 262144 KiB
1956 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1957 V4L2_CID_MPEG_VIDEO_VBV_SIZE
, 0, 262144, 1, 0);
1960 static void coda_jpeg_encode_ctrls(struct coda_ctx
*ctx
)
1962 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1963 V4L2_CID_JPEG_COMPRESSION_QUALITY
, 5, 100, 1, 50);
1964 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1965 V4L2_CID_JPEG_RESTART_INTERVAL
, 0, 100, 1, 0);
1968 static void coda_decode_ctrls(struct coda_ctx
*ctx
)
1973 ctx
->h264_profile_ctrl
= v4l2_ctrl_new_std_menu(&ctx
->ctrls
,
1974 &coda_ctrl_ops
, V4L2_CID_MPEG_VIDEO_H264_PROFILE
,
1975 V4L2_MPEG_VIDEO_H264_PROFILE_HIGH
,
1976 ~((1 << V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE
) |
1977 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_MAIN
) |
1978 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_HIGH
)),
1979 V4L2_MPEG_VIDEO_H264_PROFILE_HIGH
);
1980 if (ctx
->h264_profile_ctrl
)
1981 ctx
->h264_profile_ctrl
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
1983 if (ctx
->dev
->devtype
->product
== CODA_HX4
||
1984 ctx
->dev
->devtype
->product
== CODA_7541
) {
1985 max
= V4L2_MPEG_VIDEO_H264_LEVEL_4_0
;
1986 mask
= ~((1 << V4L2_MPEG_VIDEO_H264_LEVEL_2_0
) |
1987 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_0
) |
1988 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_1
) |
1989 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_2
) |
1990 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_4_0
));
1991 } else if (ctx
->dev
->devtype
->product
== CODA_960
) {
1992 max
= V4L2_MPEG_VIDEO_H264_LEVEL_4_1
;
1993 mask
= ~((1 << V4L2_MPEG_VIDEO_H264_LEVEL_2_0
) |
1994 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_0
) |
1995 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_1
) |
1996 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_2
) |
1997 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_4_0
) |
1998 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_4_1
));
2002 ctx
->h264_level_ctrl
= v4l2_ctrl_new_std_menu(&ctx
->ctrls
,
2003 &coda_ctrl_ops
, V4L2_CID_MPEG_VIDEO_H264_LEVEL
, max
, mask
,
2005 if (ctx
->h264_level_ctrl
)
2006 ctx
->h264_level_ctrl
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
2009 static int coda_ctrls_setup(struct coda_ctx
*ctx
)
2011 v4l2_ctrl_handler_init(&ctx
->ctrls
, 2);
2013 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
2014 V4L2_CID_HFLIP
, 0, 1, 1, 0);
2015 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
2016 V4L2_CID_VFLIP
, 0, 1, 1, 0);
2017 if (ctx
->inst_type
== CODA_INST_ENCODER
) {
2018 if (ctx
->cvd
->dst_formats
[0] == V4L2_PIX_FMT_JPEG
)
2019 coda_jpeg_encode_ctrls(ctx
);
2021 coda_encode_ctrls(ctx
);
2023 if (ctx
->cvd
->src_formats
[0] == V4L2_PIX_FMT_H264
)
2024 coda_decode_ctrls(ctx
);
2027 if (ctx
->ctrls
.error
) {
2028 v4l2_err(&ctx
->dev
->v4l2_dev
,
2029 "control initialization error (%d)",
2034 return v4l2_ctrl_handler_setup(&ctx
->ctrls
);
2037 static int coda_queue_init(struct coda_ctx
*ctx
, struct vb2_queue
*vq
)
2040 vq
->ops
= &coda_qops
;
2041 vq
->buf_struct_size
= sizeof(struct v4l2_m2m_buffer
);
2042 vq
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_COPY
;
2043 vq
->lock
= &ctx
->dev
->dev_mutex
;
2044 /* One way to indicate end-of-stream for coda is to set the
2045 * bytesused == 0. However by default videobuf2 handles bytesused
2046 * equal to 0 as a special case and changes its value to the size
2047 * of the buffer. Set the allow_zero_bytesused flag, so
2048 * that videobuf2 will keep the value of bytesused intact.
2050 vq
->allow_zero_bytesused
= 1;
2052 * We might be fine with no buffers on some of the queues, but that
2053 * would need to be reflected in job_ready(). Currently we expect all
2054 * queues to have at least one buffer queued.
2056 vq
->min_buffers_needed
= 1;
2057 vq
->dev
= &ctx
->dev
->plat_dev
->dev
;
2059 return vb2_queue_init(vq
);
2062 int coda_encoder_queue_init(void *priv
, struct vb2_queue
*src_vq
,
2063 struct vb2_queue
*dst_vq
)
2067 src_vq
->type
= V4L2_BUF_TYPE_VIDEO_OUTPUT
;
2068 src_vq
->io_modes
= VB2_DMABUF
| VB2_MMAP
;
2069 src_vq
->mem_ops
= &vb2_dma_contig_memops
;
2071 ret
= coda_queue_init(priv
, src_vq
);
2075 dst_vq
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
2076 dst_vq
->io_modes
= VB2_DMABUF
| VB2_MMAP
;
2077 dst_vq
->mem_ops
= &vb2_dma_contig_memops
;
2079 return coda_queue_init(priv
, dst_vq
);
2082 int coda_decoder_queue_init(void *priv
, struct vb2_queue
*src_vq
,
2083 struct vb2_queue
*dst_vq
)
2087 src_vq
->type
= V4L2_BUF_TYPE_VIDEO_OUTPUT
;
2088 src_vq
->io_modes
= VB2_DMABUF
| VB2_MMAP
| VB2_USERPTR
;
2089 src_vq
->mem_ops
= &vb2_vmalloc_memops
;
2091 ret
= coda_queue_init(priv
, src_vq
);
2095 dst_vq
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
2096 dst_vq
->io_modes
= VB2_DMABUF
| VB2_MMAP
;
2097 dst_vq
->mem_ops
= &vb2_dma_contig_memops
;
2099 return coda_queue_init(priv
, dst_vq
);
2102 static int coda_next_free_instance(struct coda_dev
*dev
)
2104 int idx
= ffz(dev
->instance_mask
);
2107 (dev
->devtype
->product
== CODA_DX6
&& idx
> CODADX6_MAX_INSTANCES
))
2117 static int coda_open(struct file
*file
)
2119 struct video_device
*vdev
= video_devdata(file
);
2120 struct coda_dev
*dev
= video_get_drvdata(vdev
);
2121 struct coda_ctx
*ctx
= NULL
;
2126 ctx
= kzalloc(sizeof(*ctx
), GFP_KERNEL
);
2130 idx
= coda_next_free_instance(dev
);
2135 set_bit(idx
, &dev
->instance_mask
);
2137 name
= kasprintf(GFP_KERNEL
, "context%d", idx
);
2140 goto err_coda_name_init
;
2143 ctx
->debugfs_entry
= debugfs_create_dir(name
, dev
->debugfs_root
);
2146 ctx
->cvd
= to_coda_video_device(vdev
);
2147 ctx
->inst_type
= ctx
->cvd
->type
;
2148 ctx
->ops
= ctx
->cvd
->ops
;
2149 ctx
->use_bit
= !ctx
->cvd
->direct
;
2150 init_completion(&ctx
->completion
);
2151 INIT_WORK(&ctx
->pic_run_work
, coda_pic_run_work
);
2152 if (ctx
->ops
->seq_end_work
)
2153 INIT_WORK(&ctx
->seq_end_work
, ctx
->ops
->seq_end_work
);
2154 v4l2_fh_init(&ctx
->fh
, video_devdata(file
));
2155 file
->private_data
= &ctx
->fh
;
2156 v4l2_fh_add(&ctx
->fh
);
2159 switch (dev
->devtype
->product
) {
2162 * Enabling the BWB when decoding can hang the firmware with
2163 * certain streams. The issue was tracked as ENGR00293425 by
2164 * Freescale. As a workaround, disable BWB for all decoders.
2165 * The enable_bwb module parameter allows to override this.
2167 if (enable_bwb
|| ctx
->inst_type
== CODA_INST_ENCODER
)
2168 ctx
->frame_mem_ctrl
= CODA9_FRAME_ENABLE_BWB
;
2177 if (ctx
->dev
->vdoa
&& !disable_vdoa
) {
2178 ctx
->vdoa
= vdoa_context_create(dev
->vdoa
);
2180 v4l2_warn(&dev
->v4l2_dev
,
2181 "Failed to create vdoa context: not using vdoa");
2183 ctx
->use_vdoa
= false;
2185 /* Power up and upload firmware if necessary */
2186 ret
= pm_runtime_get_sync(&dev
->plat_dev
->dev
);
2188 v4l2_err(&dev
->v4l2_dev
, "failed to power up: %d\n", ret
);
2192 ret
= clk_prepare_enable(dev
->clk_per
);
2196 ret
= clk_prepare_enable(dev
->clk_ahb
);
2200 set_default_params(ctx
);
2201 ctx
->fh
.m2m_ctx
= v4l2_m2m_ctx_init(dev
->m2m_dev
, ctx
,
2202 ctx
->ops
->queue_init
);
2203 if (IS_ERR(ctx
->fh
.m2m_ctx
)) {
2204 ret
= PTR_ERR(ctx
->fh
.m2m_ctx
);
2206 v4l2_err(&dev
->v4l2_dev
, "%s return error (%d)\n",
2211 ret
= coda_ctrls_setup(ctx
);
2213 v4l2_err(&dev
->v4l2_dev
, "failed to setup coda controls\n");
2214 goto err_ctrls_setup
;
2217 ctx
->fh
.ctrl_handler
= &ctx
->ctrls
;
2219 mutex_init(&ctx
->bitstream_mutex
);
2220 mutex_init(&ctx
->buffer_mutex
);
2221 INIT_LIST_HEAD(&ctx
->buffer_meta_list
);
2222 spin_lock_init(&ctx
->buffer_meta_lock
);
2224 mutex_lock(&dev
->dev_mutex
);
2225 list_add(&ctx
->list
, &dev
->instances
);
2226 mutex_unlock(&dev
->dev_mutex
);
2228 v4l2_dbg(1, coda_debug
, &dev
->v4l2_dev
, "Created instance %d (%p)\n",
2234 v4l2_m2m_ctx_release(ctx
->fh
.m2m_ctx
);
2236 clk_disable_unprepare(dev
->clk_ahb
);
2238 clk_disable_unprepare(dev
->clk_per
);
2240 pm_runtime_put_sync(&dev
->plat_dev
->dev
);
2242 v4l2_fh_del(&ctx
->fh
);
2243 v4l2_fh_exit(&ctx
->fh
);
2244 clear_bit(ctx
->idx
, &dev
->instance_mask
);
2251 static int coda_release(struct file
*file
)
2253 struct coda_dev
*dev
= video_drvdata(file
);
2254 struct coda_ctx
*ctx
= fh_to_ctx(file
->private_data
);
2256 v4l2_dbg(1, coda_debug
, &dev
->v4l2_dev
, "Releasing instance %p\n",
2259 if (ctx
->inst_type
== CODA_INST_DECODER
&& ctx
->use_bit
)
2260 coda_bit_stream_end_flag(ctx
);
2262 /* If this instance is running, call .job_abort and wait for it to end */
2263 v4l2_m2m_ctx_release(ctx
->fh
.m2m_ctx
);
2266 vdoa_context_destroy(ctx
->vdoa
);
2268 /* In case the instance was not running, we still need to call SEQ_END */
2269 if (ctx
->ops
->seq_end_work
) {
2270 queue_work(dev
->workqueue
, &ctx
->seq_end_work
);
2271 flush_work(&ctx
->seq_end_work
);
2274 mutex_lock(&dev
->dev_mutex
);
2275 list_del(&ctx
->list
);
2276 mutex_unlock(&dev
->dev_mutex
);
2278 if (ctx
->dev
->devtype
->product
== CODA_DX6
)
2279 coda_free_aux_buf(dev
, &ctx
->workbuf
);
2281 v4l2_ctrl_handler_free(&ctx
->ctrls
);
2282 clk_disable_unprepare(dev
->clk_ahb
);
2283 clk_disable_unprepare(dev
->clk_per
);
2284 pm_runtime_put_sync(&dev
->plat_dev
->dev
);
2285 v4l2_fh_del(&ctx
->fh
);
2286 v4l2_fh_exit(&ctx
->fh
);
2287 clear_bit(ctx
->idx
, &dev
->instance_mask
);
2288 if (ctx
->ops
->release
)
2289 ctx
->ops
->release(ctx
);
2290 debugfs_remove_recursive(ctx
->debugfs_entry
);
2296 static const struct v4l2_file_operations coda_fops
= {
2297 .owner
= THIS_MODULE
,
2299 .release
= coda_release
,
2300 .poll
= v4l2_m2m_fop_poll
,
2301 .unlocked_ioctl
= video_ioctl2
,
2302 .mmap
= v4l2_m2m_fop_mmap
,
2305 static int coda_hw_init(struct coda_dev
*dev
)
2311 ret
= clk_prepare_enable(dev
->clk_per
);
2315 ret
= clk_prepare_enable(dev
->clk_ahb
);
2319 reset_control_reset(dev
->rstc
);
2322 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
2323 * The 16-bit chars in the code buffer are in memory access
2324 * order, re-sort them to CODA order for register download.
2325 * Data in this SRAM survives a reboot.
2327 p
= (u16
*)dev
->codebuf
.vaddr
;
2328 if (dev
->devtype
->product
== CODA_DX6
) {
2329 for (i
= 0; i
< (CODA_ISRAM_SIZE
/ 2); i
++) {
2330 data
= CODA_DOWN_ADDRESS_SET(i
) |
2331 CODA_DOWN_DATA_SET(p
[i
^ 1]);
2332 coda_write(dev
, data
, CODA_REG_BIT_CODE_DOWN
);
2335 for (i
= 0; i
< (CODA_ISRAM_SIZE
/ 2); i
++) {
2336 data
= CODA_DOWN_ADDRESS_SET(i
) |
2337 CODA_DOWN_DATA_SET(p
[round_down(i
, 4) +
2339 coda_write(dev
, data
, CODA_REG_BIT_CODE_DOWN
);
2343 /* Clear registers */
2344 for (i
= 0; i
< 64; i
++)
2345 coda_write(dev
, 0, CODA_REG_BIT_CODE_BUF_ADDR
+ i
* 4);
2347 /* Tell the BIT where to find everything it needs */
2348 if (dev
->devtype
->product
== CODA_960
||
2349 dev
->devtype
->product
== CODA_7541
||
2350 dev
->devtype
->product
== CODA_HX4
) {
2351 coda_write(dev
, dev
->tempbuf
.paddr
,
2352 CODA_REG_BIT_TEMP_BUF_ADDR
);
2353 coda_write(dev
, 0, CODA_REG_BIT_BIT_STREAM_PARAM
);
2355 coda_write(dev
, dev
->workbuf
.paddr
,
2356 CODA_REG_BIT_WORK_BUF_ADDR
);
2358 coda_write(dev
, dev
->codebuf
.paddr
,
2359 CODA_REG_BIT_CODE_BUF_ADDR
);
2360 coda_write(dev
, 0, CODA_REG_BIT_CODE_RUN
);
2362 /* Set default values */
2363 switch (dev
->devtype
->product
) {
2365 coda_write(dev
, CODADX6_STREAM_BUF_PIC_FLUSH
,
2366 CODA_REG_BIT_STREAM_CTRL
);
2369 coda_write(dev
, CODA7_STREAM_BUF_PIC_FLUSH
,
2370 CODA_REG_BIT_STREAM_CTRL
);
2372 if (dev
->devtype
->product
== CODA_960
)
2373 coda_write(dev
, CODA9_FRAME_ENABLE_BWB
,
2374 CODA_REG_BIT_FRAME_MEM_CTRL
);
2376 coda_write(dev
, 0, CODA_REG_BIT_FRAME_MEM_CTRL
);
2378 if (dev
->devtype
->product
!= CODA_DX6
)
2379 coda_write(dev
, 0, CODA7_REG_BIT_AXI_SRAM_USE
);
2381 coda_write(dev
, CODA_INT_INTERRUPT_ENABLE
,
2382 CODA_REG_BIT_INT_ENABLE
);
2384 /* Reset VPU and start processor */
2385 data
= coda_read(dev
, CODA_REG_BIT_CODE_RESET
);
2386 data
|= CODA_REG_RESET_ENABLE
;
2387 coda_write(dev
, data
, CODA_REG_BIT_CODE_RESET
);
2389 data
&= ~CODA_REG_RESET_ENABLE
;
2390 coda_write(dev
, data
, CODA_REG_BIT_CODE_RESET
);
2391 coda_write(dev
, CODA_REG_RUN_ENABLE
, CODA_REG_BIT_CODE_RUN
);
2393 clk_disable_unprepare(dev
->clk_ahb
);
2394 clk_disable_unprepare(dev
->clk_per
);
2399 clk_disable_unprepare(dev
->clk_per
);
2404 static int coda_register_device(struct coda_dev
*dev
, int i
)
2406 struct video_device
*vfd
= &dev
->vfd
[i
];
2408 if (i
>= dev
->devtype
->num_vdevs
)
2411 strscpy(vfd
->name
, dev
->devtype
->vdevs
[i
]->name
, sizeof(vfd
->name
));
2412 vfd
->fops
= &coda_fops
;
2413 vfd
->ioctl_ops
= &coda_ioctl_ops
;
2414 vfd
->release
= video_device_release_empty
,
2415 vfd
->lock
= &dev
->dev_mutex
;
2416 vfd
->v4l2_dev
= &dev
->v4l2_dev
;
2417 vfd
->vfl_dir
= VFL_DIR_M2M
;
2418 video_set_drvdata(vfd
, dev
);
2420 /* Not applicable, use the selection API instead */
2421 v4l2_disable_ioctl(vfd
, VIDIOC_CROPCAP
);
2422 v4l2_disable_ioctl(vfd
, VIDIOC_G_CROP
);
2423 v4l2_disable_ioctl(vfd
, VIDIOC_S_CROP
);
2425 return video_register_device(vfd
, VFL_TYPE_GRABBER
, 0);
2428 static void coda_copy_firmware(struct coda_dev
*dev
, const u8
* const buf
,
2431 u32
*src
= (u32
*)buf
;
2433 /* Check if the firmware has a 16-byte Freescale header, skip it */
2434 if (buf
[0] == 'M' && buf
[1] == 'X')
2437 * Check whether the firmware is in native order or pre-reordered for
2438 * memory access. The first instruction opcode always is 0xe40e.
2440 if (__le16_to_cpup((__le16
*)src
) == 0xe40e) {
2441 u32
*dst
= dev
->codebuf
.vaddr
;
2444 /* Firmware in native order, reorder while copying */
2445 if (dev
->devtype
->product
== CODA_DX6
) {
2446 for (i
= 0; i
< (size
- 16) / 4; i
++)
2447 dst
[i
] = (src
[i
] << 16) | (src
[i
] >> 16);
2449 for (i
= 0; i
< (size
- 16) / 4; i
+= 2) {
2450 dst
[i
] = (src
[i
+ 1] << 16) | (src
[i
+ 1] >> 16);
2451 dst
[i
+ 1] = (src
[i
] << 16) | (src
[i
] >> 16);
2455 /* Copy the already reordered firmware image */
2456 memcpy(dev
->codebuf
.vaddr
, src
, size
);
2460 static void coda_fw_callback(const struct firmware
*fw
, void *context
);
2462 static int coda_firmware_request(struct coda_dev
*dev
)
2466 if (dev
->firmware
>= ARRAY_SIZE(dev
->devtype
->firmware
))
2469 fw
= dev
->devtype
->firmware
[dev
->firmware
];
2471 dev_dbg(&dev
->plat_dev
->dev
, "requesting firmware '%s' for %s\n", fw
,
2472 coda_product_name(dev
->devtype
->product
));
2474 return request_firmware_nowait(THIS_MODULE
, true, fw
,
2475 &dev
->plat_dev
->dev
, GFP_KERNEL
, dev
,
2479 static void coda_fw_callback(const struct firmware
*fw
, void *context
)
2481 struct coda_dev
*dev
= context
;
2482 struct platform_device
*pdev
= dev
->plat_dev
;
2487 ret
= coda_firmware_request(dev
);
2489 v4l2_err(&dev
->v4l2_dev
, "firmware request failed\n");
2494 if (dev
->firmware
> 0) {
2496 * Since we can't suppress warnings for failed asynchronous
2497 * firmware requests, report that the fallback firmware was
2500 dev_info(&pdev
->dev
, "Using fallback firmware %s\n",
2501 dev
->devtype
->firmware
[dev
->firmware
]);
2504 /* allocate auxiliary per-device code buffer for the BIT processor */
2505 ret
= coda_alloc_aux_buf(dev
, &dev
->codebuf
, fw
->size
, "codebuf",
2510 coda_copy_firmware(dev
, fw
->data
, fw
->size
);
2511 release_firmware(fw
);
2513 ret
= coda_hw_init(dev
);
2515 v4l2_err(&dev
->v4l2_dev
, "HW initialization failed\n");
2519 ret
= coda_check_firmware(dev
);
2523 dev
->m2m_dev
= v4l2_m2m_init(&coda_m2m_ops
);
2524 if (IS_ERR(dev
->m2m_dev
)) {
2525 v4l2_err(&dev
->v4l2_dev
, "Failed to init mem2mem device\n");
2529 for (i
= 0; i
< dev
->devtype
->num_vdevs
; i
++) {
2530 ret
= coda_register_device(dev
, i
);
2532 v4l2_err(&dev
->v4l2_dev
,
2533 "Failed to register %s video device: %d\n",
2534 dev
->devtype
->vdevs
[i
]->name
, ret
);
2539 v4l2_info(&dev
->v4l2_dev
, "codec registered as /dev/video[%d-%d]\n",
2540 dev
->vfd
[0].num
, dev
->vfd
[i
- 1].num
);
2542 pm_runtime_put_sync(&pdev
->dev
);
2547 video_unregister_device(&dev
->vfd
[i
]);
2548 v4l2_m2m_release(dev
->m2m_dev
);
2550 pm_runtime_put_sync(&pdev
->dev
);
2553 enum coda_platform
{
2561 static const struct coda_devtype coda_devdata
[] = {
2564 "vpu_fw_imx27_TO2.bin",
2565 "vpu/vpu_fw_imx27_TO2.bin",
2566 "v4l-codadx6-imx27.bin"
2568 .product
= CODA_DX6
,
2569 .codecs
= codadx6_codecs
,
2570 .num_codecs
= ARRAY_SIZE(codadx6_codecs
),
2571 .vdevs
= codadx6_video_devices
,
2572 .num_vdevs
= ARRAY_SIZE(codadx6_video_devices
),
2573 .workbuf_size
= 288 * 1024 + FMO_SLICE_SAVE_BUF_SIZE
* 8 * 1024,
2574 .iram_size
= 0xb000,
2579 "vpu/vpu_fw_imx51.bin",
2580 "v4l-codahx4-imx51.bin"
2582 .product
= CODA_HX4
,
2583 .codecs
= codahx4_codecs
,
2584 .num_codecs
= ARRAY_SIZE(codahx4_codecs
),
2585 .vdevs
= codahx4_video_devices
,
2586 .num_vdevs
= ARRAY_SIZE(codahx4_video_devices
),
2587 .workbuf_size
= 128 * 1024,
2588 .tempbuf_size
= 304 * 1024,
2589 .iram_size
= 0x14000,
2594 "vpu/vpu_fw_imx53.bin",
2595 "v4l-coda7541-imx53.bin"
2597 .product
= CODA_7541
,
2598 .codecs
= coda7_codecs
,
2599 .num_codecs
= ARRAY_SIZE(coda7_codecs
),
2600 .vdevs
= coda7_video_devices
,
2601 .num_vdevs
= ARRAY_SIZE(coda7_video_devices
),
2602 .workbuf_size
= 128 * 1024,
2603 .tempbuf_size
= 304 * 1024,
2604 .iram_size
= 0x14000,
2609 "vpu/vpu_fw_imx6q.bin",
2610 "v4l-coda960-imx6q.bin"
2612 .product
= CODA_960
,
2613 .codecs
= coda9_codecs
,
2614 .num_codecs
= ARRAY_SIZE(coda9_codecs
),
2615 .vdevs
= coda9_video_devices
,
2616 .num_vdevs
= ARRAY_SIZE(coda9_video_devices
),
2617 .workbuf_size
= 80 * 1024,
2618 .tempbuf_size
= 204 * 1024,
2619 .iram_size
= 0x21000,
2624 "vpu/vpu_fw_imx6d.bin",
2625 "v4l-coda960-imx6dl.bin"
2627 .product
= CODA_960
,
2628 .codecs
= coda9_codecs
,
2629 .num_codecs
= ARRAY_SIZE(coda9_codecs
),
2630 .vdevs
= coda9_video_devices
,
2631 .num_vdevs
= ARRAY_SIZE(coda9_video_devices
),
2632 .workbuf_size
= 80 * 1024,
2633 .tempbuf_size
= 204 * 1024,
2634 .iram_size
= 0x1f000, /* leave 4k for suspend code */
2638 static const struct platform_device_id coda_platform_ids
[] = {
2639 { .name
= "coda-imx27", .driver_data
= CODA_IMX27
},
2642 MODULE_DEVICE_TABLE(platform
, coda_platform_ids
);
2645 static const struct of_device_id coda_dt_ids
[] = {
2646 { .compatible
= "fsl,imx27-vpu", .data
= &coda_devdata
[CODA_IMX27
] },
2647 { .compatible
= "fsl,imx51-vpu", .data
= &coda_devdata
[CODA_IMX51
] },
2648 { .compatible
= "fsl,imx53-vpu", .data
= &coda_devdata
[CODA_IMX53
] },
2649 { .compatible
= "fsl,imx6q-vpu", .data
= &coda_devdata
[CODA_IMX6Q
] },
2650 { .compatible
= "fsl,imx6dl-vpu", .data
= &coda_devdata
[CODA_IMX6DL
] },
2653 MODULE_DEVICE_TABLE(of
, coda_dt_ids
);
2656 static int coda_probe(struct platform_device
*pdev
)
2658 const struct of_device_id
*of_id
=
2659 of_match_device(of_match_ptr(coda_dt_ids
), &pdev
->dev
);
2660 const struct platform_device_id
*pdev_id
;
2661 struct coda_platform_data
*pdata
= pdev
->dev
.platform_data
;
2662 struct device_node
*np
= pdev
->dev
.of_node
;
2663 struct gen_pool
*pool
;
2664 struct coda_dev
*dev
;
2665 struct resource
*res
;
2668 dev
= devm_kzalloc(&pdev
->dev
, sizeof(*dev
), GFP_KERNEL
);
2672 pdev_id
= of_id
? of_id
->data
: platform_get_device_id(pdev
);
2675 dev
->devtype
= of_id
->data
;
2677 dev
->devtype
= &coda_devdata
[pdev_id
->driver_data
];
2681 spin_lock_init(&dev
->irqlock
);
2682 INIT_LIST_HEAD(&dev
->instances
);
2684 dev
->plat_dev
= pdev
;
2685 dev
->clk_per
= devm_clk_get(&pdev
->dev
, "per");
2686 if (IS_ERR(dev
->clk_per
)) {
2687 dev_err(&pdev
->dev
, "Could not get per clock\n");
2688 return PTR_ERR(dev
->clk_per
);
2691 dev
->clk_ahb
= devm_clk_get(&pdev
->dev
, "ahb");
2692 if (IS_ERR(dev
->clk_ahb
)) {
2693 dev_err(&pdev
->dev
, "Could not get ahb clock\n");
2694 return PTR_ERR(dev
->clk_ahb
);
2697 /* Get memory for physical registers */
2698 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2699 dev
->regs_base
= devm_ioremap_resource(&pdev
->dev
, res
);
2700 if (IS_ERR(dev
->regs_base
))
2701 return PTR_ERR(dev
->regs_base
);
2704 irq
= platform_get_irq_byname(pdev
, "bit");
2706 irq
= platform_get_irq(pdev
, 0);
2708 dev_err(&pdev
->dev
, "failed to get irq resource\n");
2712 ret
= devm_request_threaded_irq(&pdev
->dev
, irq
, NULL
, coda_irq_handler
,
2713 IRQF_ONESHOT
, dev_name(&pdev
->dev
), dev
);
2715 dev_err(&pdev
->dev
, "failed to request irq: %d\n", ret
);
2719 dev
->rstc
= devm_reset_control_get_optional_exclusive(&pdev
->dev
,
2721 if (IS_ERR(dev
->rstc
)) {
2722 ret
= PTR_ERR(dev
->rstc
);
2723 dev_err(&pdev
->dev
, "failed get reset control: %d\n", ret
);
2727 /* Get IRAM pool from device tree or platform data */
2728 pool
= of_gen_pool_get(np
, "iram", 0);
2730 pool
= gen_pool_get(pdata
->iram_dev
, NULL
);
2732 dev_err(&pdev
->dev
, "iram pool not available\n");
2735 dev
->iram_pool
= pool
;
2737 /* Get vdoa_data if supported by the platform */
2738 dev
->vdoa
= coda_get_vdoa_data();
2739 if (PTR_ERR(dev
->vdoa
) == -EPROBE_DEFER
)
2740 return -EPROBE_DEFER
;
2742 ret
= v4l2_device_register(&pdev
->dev
, &dev
->v4l2_dev
);
2746 mutex_init(&dev
->dev_mutex
);
2747 mutex_init(&dev
->coda_mutex
);
2749 dev
->debugfs_root
= debugfs_create_dir("coda", NULL
);
2750 if (!dev
->debugfs_root
)
2751 dev_warn(&pdev
->dev
, "failed to create debugfs root\n");
2753 /* allocate auxiliary per-device buffers for the BIT processor */
2754 if (dev
->devtype
->product
== CODA_DX6
) {
2755 ret
= coda_alloc_aux_buf(dev
, &dev
->workbuf
,
2756 dev
->devtype
->workbuf_size
, "workbuf",
2759 goto err_v4l2_register
;
2762 if (dev
->devtype
->tempbuf_size
) {
2763 ret
= coda_alloc_aux_buf(dev
, &dev
->tempbuf
,
2764 dev
->devtype
->tempbuf_size
, "tempbuf",
2767 goto err_v4l2_register
;
2770 dev
->iram
.size
= dev
->devtype
->iram_size
;
2771 dev
->iram
.vaddr
= gen_pool_dma_alloc(dev
->iram_pool
, dev
->iram
.size
,
2773 if (!dev
->iram
.vaddr
) {
2774 dev_warn(&pdev
->dev
, "unable to alloc iram\n");
2776 memset(dev
->iram
.vaddr
, 0, dev
->iram
.size
);
2777 dev
->iram
.blob
.data
= dev
->iram
.vaddr
;
2778 dev
->iram
.blob
.size
= dev
->iram
.size
;
2779 dev
->iram
.dentry
= debugfs_create_blob("iram", 0644,
2784 dev
->workqueue
= alloc_workqueue("coda", WQ_UNBOUND
| WQ_MEM_RECLAIM
, 1);
2785 if (!dev
->workqueue
) {
2786 dev_err(&pdev
->dev
, "unable to alloc workqueue\n");
2788 goto err_v4l2_register
;
2791 platform_set_drvdata(pdev
, dev
);
2794 * Start activated so we can directly call coda_hw_init in
2795 * coda_fw_callback regardless of whether CONFIG_PM is
2796 * enabled or whether the device is associated with a PM domain.
2798 pm_runtime_get_noresume(&pdev
->dev
);
2799 pm_runtime_set_active(&pdev
->dev
);
2800 pm_runtime_enable(&pdev
->dev
);
2802 ret
= coda_firmware_request(dev
);
2804 goto err_alloc_workqueue
;
2807 err_alloc_workqueue
:
2808 destroy_workqueue(dev
->workqueue
);
2810 v4l2_device_unregister(&dev
->v4l2_dev
);
2814 static int coda_remove(struct platform_device
*pdev
)
2816 struct coda_dev
*dev
= platform_get_drvdata(pdev
);
2819 for (i
= 0; i
< ARRAY_SIZE(dev
->vfd
); i
++) {
2820 if (video_get_drvdata(&dev
->vfd
[i
]))
2821 video_unregister_device(&dev
->vfd
[i
]);
2824 v4l2_m2m_release(dev
->m2m_dev
);
2825 pm_runtime_disable(&pdev
->dev
);
2826 v4l2_device_unregister(&dev
->v4l2_dev
);
2827 destroy_workqueue(dev
->workqueue
);
2828 if (dev
->iram
.vaddr
)
2829 gen_pool_free(dev
->iram_pool
, (unsigned long)dev
->iram
.vaddr
,
2831 coda_free_aux_buf(dev
, &dev
->codebuf
);
2832 coda_free_aux_buf(dev
, &dev
->tempbuf
);
2833 coda_free_aux_buf(dev
, &dev
->workbuf
);
2834 debugfs_remove_recursive(dev
->debugfs_root
);
2839 static int coda_runtime_resume(struct device
*dev
)
2841 struct coda_dev
*cdev
= dev_get_drvdata(dev
);
2844 if (dev
->pm_domain
&& cdev
->codebuf
.vaddr
) {
2845 ret
= coda_hw_init(cdev
);
2847 v4l2_err(&cdev
->v4l2_dev
, "HW initialization failed\n");
2854 static const struct dev_pm_ops coda_pm_ops
= {
2855 SET_RUNTIME_PM_OPS(NULL
, coda_runtime_resume
, NULL
)
2858 static struct platform_driver coda_driver
= {
2859 .probe
= coda_probe
,
2860 .remove
= coda_remove
,
2863 .of_match_table
= of_match_ptr(coda_dt_ids
),
2866 .id_table
= coda_platform_ids
,
2869 module_platform_driver(coda_driver
);
2871 MODULE_LICENSE("GPL");
2872 MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
2873 MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");