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 void coda_write(struct coda_dev
*dev
, u32 data
, u32 reg
)
76 v4l2_dbg(2, coda_debug
, &dev
->v4l2_dev
,
77 "%s: data=0x%x, reg=0x%x\n", __func__
, data
, reg
);
78 writel(data
, dev
->regs_base
+ reg
);
81 unsigned int coda_read(struct coda_dev
*dev
, u32 reg
)
85 data
= readl(dev
->regs_base
+ reg
);
86 v4l2_dbg(2, coda_debug
, &dev
->v4l2_dev
,
87 "%s: data=0x%x, reg=0x%x\n", __func__
, data
, reg
);
91 void coda_write_base(struct coda_ctx
*ctx
, struct coda_q_data
*q_data
,
92 struct vb2_v4l2_buffer
*buf
, unsigned int reg_y
)
94 u32 base_y
= vb2_dma_contig_plane_dma_addr(&buf
->vb2_buf
, 0);
97 switch (q_data
->fourcc
) {
98 case V4L2_PIX_FMT_YUYV
:
99 /* Fallthrough: IN -H264-> CODA -NV12 MB-> VDOA -YUYV-> OUT */
100 case V4L2_PIX_FMT_NV12
:
101 case V4L2_PIX_FMT_YUV420
:
103 base_cb
= base_y
+ q_data
->bytesperline
* q_data
->height
;
104 base_cr
= base_cb
+ q_data
->bytesperline
* q_data
->height
/ 4;
106 case V4L2_PIX_FMT_YVU420
:
107 /* Switch Cb and Cr for YVU420 format */
108 base_cr
= base_y
+ q_data
->bytesperline
* q_data
->height
;
109 base_cb
= base_cr
+ q_data
->bytesperline
* q_data
->height
/ 4;
111 case V4L2_PIX_FMT_YUV422P
:
112 base_cb
= base_y
+ q_data
->bytesperline
* q_data
->height
;
113 base_cr
= base_cb
+ q_data
->bytesperline
* q_data
->height
/ 2;
116 coda_write(ctx
->dev
, base_y
, reg_y
);
117 coda_write(ctx
->dev
, base_cb
, reg_y
+ 4);
118 coda_write(ctx
->dev
, base_cr
, reg_y
+ 8);
121 #define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
122 { mode, src_fourcc, dst_fourcc, max_w, max_h }
125 * Arrays of codecs supported by each given version of Coda:
129 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
131 static const struct coda_codec codadx6_codecs
[] = {
132 CODA_CODEC(CODADX6_MODE_ENCODE_H264
, V4L2_PIX_FMT_YUV420
, V4L2_PIX_FMT_H264
, 720, 576),
133 CODA_CODEC(CODADX6_MODE_ENCODE_MP4
, V4L2_PIX_FMT_YUV420
, V4L2_PIX_FMT_MPEG4
, 720, 576),
136 static const struct coda_codec coda7_codecs
[] = {
137 CODA_CODEC(CODA7_MODE_ENCODE_H264
, V4L2_PIX_FMT_YUV420
, V4L2_PIX_FMT_H264
, 1280, 720),
138 CODA_CODEC(CODA7_MODE_ENCODE_MP4
, V4L2_PIX_FMT_YUV420
, V4L2_PIX_FMT_MPEG4
, 1280, 720),
139 CODA_CODEC(CODA7_MODE_ENCODE_MJPG
, V4L2_PIX_FMT_YUV420
, V4L2_PIX_FMT_JPEG
, 8192, 8192),
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
, 1920, 1088),
143 CODA_CODEC(CODA7_MODE_DECODE_MJPG
, V4L2_PIX_FMT_JPEG
, V4L2_PIX_FMT_YUV420
, 8192, 8192),
146 static const struct coda_codec coda9_codecs
[] = {
147 CODA_CODEC(CODA9_MODE_ENCODE_H264
, V4L2_PIX_FMT_YUV420
, V4L2_PIX_FMT_H264
, 1920, 1088),
148 CODA_CODEC(CODA9_MODE_ENCODE_MP4
, V4L2_PIX_FMT_YUV420
, V4L2_PIX_FMT_MPEG4
, 1920, 1088),
149 CODA_CODEC(CODA9_MODE_DECODE_H264
, V4L2_PIX_FMT_H264
, V4L2_PIX_FMT_YUV420
, 1920, 1088),
150 CODA_CODEC(CODA9_MODE_DECODE_MP2
, V4L2_PIX_FMT_MPEG2
, V4L2_PIX_FMT_YUV420
, 1920, 1088),
151 CODA_CODEC(CODA9_MODE_DECODE_MP4
, V4L2_PIX_FMT_MPEG4
, V4L2_PIX_FMT_YUV420
, 1920, 1088),
154 struct coda_video_device
{
156 enum coda_inst_type type
;
157 const struct coda_context_ops
*ops
;
159 u32 src_formats
[CODA_MAX_FORMATS
];
160 u32 dst_formats
[CODA_MAX_FORMATS
];
163 static const struct coda_video_device coda_bit_encoder
= {
164 .name
= "coda-encoder",
165 .type
= CODA_INST_ENCODER
,
166 .ops
= &coda_bit_encode_ops
,
178 static const struct coda_video_device coda_bit_jpeg_encoder
= {
179 .name
= "coda-jpeg-encoder",
180 .type
= CODA_INST_ENCODER
,
181 .ops
= &coda_bit_encode_ops
,
186 V4L2_PIX_FMT_YUV422P
,
193 static const struct coda_video_device coda_bit_decoder
= {
194 .name
= "coda-decoder",
195 .type
= CODA_INST_DECODER
,
196 .ops
= &coda_bit_decode_ops
,
207 * If V4L2_PIX_FMT_YUYV should be default,
208 * set_default_params() must be adjusted.
214 static const struct coda_video_device coda_bit_jpeg_decoder
= {
215 .name
= "coda-jpeg-decoder",
216 .type
= CODA_INST_DECODER
,
217 .ops
= &coda_bit_decode_ops
,
225 V4L2_PIX_FMT_YUV422P
,
229 static const struct coda_video_device
*codadx6_video_devices
[] = {
233 static const struct coda_video_device
*coda7_video_devices
[] = {
234 &coda_bit_jpeg_encoder
,
235 &coda_bit_jpeg_decoder
,
240 static const struct coda_video_device
*coda9_video_devices
[] = {
246 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
249 static u32
coda_format_normalize_yuv(u32 fourcc
)
252 case V4L2_PIX_FMT_NV12
:
253 case V4L2_PIX_FMT_YUV420
:
254 case V4L2_PIX_FMT_YVU420
:
255 case V4L2_PIX_FMT_YUV422P
:
256 case V4L2_PIX_FMT_YUYV
:
257 return V4L2_PIX_FMT_YUV420
;
263 static const struct coda_codec
*coda_find_codec(struct coda_dev
*dev
,
264 int src_fourcc
, int dst_fourcc
)
266 const struct coda_codec
*codecs
= dev
->devtype
->codecs
;
267 int num_codecs
= dev
->devtype
->num_codecs
;
270 src_fourcc
= coda_format_normalize_yuv(src_fourcc
);
271 dst_fourcc
= coda_format_normalize_yuv(dst_fourcc
);
272 if (src_fourcc
== dst_fourcc
)
275 for (k
= 0; k
< num_codecs
; k
++) {
276 if (codecs
[k
].src_fourcc
== src_fourcc
&&
277 codecs
[k
].dst_fourcc
== dst_fourcc
)
287 static void coda_get_max_dimensions(struct coda_dev
*dev
,
288 const struct coda_codec
*codec
,
289 int *max_w
, int *max_h
)
291 const struct coda_codec
*codecs
= dev
->devtype
->codecs
;
292 int num_codecs
= dev
->devtype
->num_codecs
;
300 for (k
= 0, w
= 0, h
= 0; k
< num_codecs
; k
++) {
301 w
= max(w
, codecs
[k
].max_w
);
302 h
= max(h
, codecs
[k
].max_h
);
312 static const struct coda_video_device
*to_coda_video_device(struct video_device
315 struct coda_dev
*dev
= video_get_drvdata(vdev
);
316 unsigned int i
= vdev
- dev
->vfd
;
318 if (i
>= dev
->devtype
->num_vdevs
)
321 return dev
->devtype
->vdevs
[i
];
324 const char *coda_product_name(int product
)
336 snprintf(buf
, sizeof(buf
), "(0x%04x)", product
);
341 static struct vdoa_data
*coda_get_vdoa_data(void)
343 struct device_node
*vdoa_node
;
344 struct platform_device
*vdoa_pdev
;
345 struct vdoa_data
*vdoa_data
= NULL
;
347 vdoa_node
= of_find_compatible_node(NULL
, NULL
, "fsl,imx6q-vdoa");
351 vdoa_pdev
= of_find_device_by_node(vdoa_node
);
355 vdoa_data
= platform_get_drvdata(vdoa_pdev
);
357 vdoa_data
= ERR_PTR(-EPROBE_DEFER
);
361 of_node_put(vdoa_node
);
367 * V4L2 ioctl() operations.
369 static int coda_querycap(struct file
*file
, void *priv
,
370 struct v4l2_capability
*cap
)
372 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
374 strlcpy(cap
->driver
, CODA_NAME
, sizeof(cap
->driver
));
375 strlcpy(cap
->card
, coda_product_name(ctx
->dev
->devtype
->product
),
377 strlcpy(cap
->bus_info
, "platform:" CODA_NAME
, sizeof(cap
->bus_info
));
378 cap
->device_caps
= V4L2_CAP_VIDEO_M2M
| V4L2_CAP_STREAMING
;
379 cap
->capabilities
= cap
->device_caps
| V4L2_CAP_DEVICE_CAPS
;
384 static int coda_enum_fmt(struct file
*file
, void *priv
,
385 struct v4l2_fmtdesc
*f
)
387 struct video_device
*vdev
= video_devdata(file
);
388 const struct coda_video_device
*cvd
= to_coda_video_device(vdev
);
391 if (f
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
)
392 formats
= cvd
->src_formats
;
393 else if (f
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
394 formats
= cvd
->dst_formats
;
398 if (f
->index
>= CODA_MAX_FORMATS
|| formats
[f
->index
] == 0)
401 f
->pixelformat
= formats
[f
->index
];
406 static int coda_g_fmt(struct file
*file
, void *priv
,
407 struct v4l2_format
*f
)
409 struct coda_q_data
*q_data
;
410 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
412 q_data
= get_q_data(ctx
, f
->type
);
416 f
->fmt
.pix
.field
= V4L2_FIELD_NONE
;
417 f
->fmt
.pix
.pixelformat
= q_data
->fourcc
;
418 f
->fmt
.pix
.width
= q_data
->width
;
419 f
->fmt
.pix
.height
= q_data
->height
;
420 f
->fmt
.pix
.bytesperline
= q_data
->bytesperline
;
422 f
->fmt
.pix
.sizeimage
= q_data
->sizeimage
;
423 if (f
->fmt
.pix
.pixelformat
== V4L2_PIX_FMT_JPEG
)
424 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_JPEG
;
426 f
->fmt
.pix
.colorspace
= ctx
->colorspace
;
431 static int coda_try_pixelformat(struct coda_ctx
*ctx
, struct v4l2_format
*f
)
433 struct coda_q_data
*q_data
;
437 if (f
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
)
438 formats
= ctx
->cvd
->src_formats
;
439 else if (f
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
440 formats
= ctx
->cvd
->dst_formats
;
444 for (i
= 0; i
< CODA_MAX_FORMATS
; i
++) {
445 /* Skip YUYV if the vdoa is not available */
446 if (!ctx
->vdoa
&& f
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
447 formats
[i
] == V4L2_PIX_FMT_YUYV
)
450 if (formats
[i
] == f
->fmt
.pix
.pixelformat
) {
451 f
->fmt
.pix
.pixelformat
= formats
[i
];
456 /* Fall back to currently set pixelformat */
457 q_data
= get_q_data(ctx
, f
->type
);
458 f
->fmt
.pix
.pixelformat
= q_data
->fourcc
;
463 static int coda_try_fmt_vdoa(struct coda_ctx
*ctx
, struct v4l2_format
*f
,
468 if (f
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
479 err
= vdoa_context_configure(NULL
, f
->fmt
.pix
.width
, f
->fmt
.pix
.height
,
480 f
->fmt
.pix
.pixelformat
);
490 static unsigned int coda_estimate_sizeimage(struct coda_ctx
*ctx
, u32 sizeimage
,
491 u32 width
, u32 height
)
494 * This is a rough estimate for sensible compressed buffer
495 * sizes (between 1 and 16 bits per pixel). This could be
496 * improved by better format specific worst case estimates.
498 return round_up(clamp(sizeimage
, width
* height
/ 8,
499 width
* height
* 2), PAGE_SIZE
);
502 static int coda_try_fmt(struct coda_ctx
*ctx
, const struct coda_codec
*codec
,
503 struct v4l2_format
*f
)
505 struct coda_dev
*dev
= ctx
->dev
;
506 unsigned int max_w
, max_h
;
507 enum v4l2_field field
;
509 field
= f
->fmt
.pix
.field
;
510 if (field
== V4L2_FIELD_ANY
)
511 field
= V4L2_FIELD_NONE
;
512 else if (V4L2_FIELD_NONE
!= field
)
515 /* V4L2 specification suggests the driver corrects the format struct
516 * if any of the dimensions is unsupported */
517 f
->fmt
.pix
.field
= field
;
519 coda_get_max_dimensions(dev
, codec
, &max_w
, &max_h
);
520 v4l_bound_align_image(&f
->fmt
.pix
.width
, MIN_W
, max_w
, W_ALIGN
,
521 &f
->fmt
.pix
.height
, MIN_H
, max_h
, H_ALIGN
,
524 switch (f
->fmt
.pix
.pixelformat
) {
525 case V4L2_PIX_FMT_NV12
:
526 case V4L2_PIX_FMT_YUV420
:
527 case V4L2_PIX_FMT_YVU420
:
529 * Frame stride must be at least multiple of 8,
530 * but multiple of 16 for h.264 or JPEG 4:2:x
532 f
->fmt
.pix
.bytesperline
= round_up(f
->fmt
.pix
.width
, 16);
533 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.bytesperline
*
534 f
->fmt
.pix
.height
* 3 / 2;
536 case V4L2_PIX_FMT_YUYV
:
537 f
->fmt
.pix
.bytesperline
= round_up(f
->fmt
.pix
.width
, 16) * 2;
538 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.bytesperline
*
541 case V4L2_PIX_FMT_YUV422P
:
542 f
->fmt
.pix
.bytesperline
= round_up(f
->fmt
.pix
.width
, 16);
543 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.bytesperline
*
544 f
->fmt
.pix
.height
* 2;
546 case V4L2_PIX_FMT_JPEG
:
547 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_JPEG
;
549 case V4L2_PIX_FMT_H264
:
550 case V4L2_PIX_FMT_MPEG4
:
551 case V4L2_PIX_FMT_MPEG2
:
552 f
->fmt
.pix
.bytesperline
= 0;
553 f
->fmt
.pix
.sizeimage
= coda_estimate_sizeimage(ctx
,
554 f
->fmt
.pix
.sizeimage
,
565 static int coda_try_fmt_vid_cap(struct file
*file
, void *priv
,
566 struct v4l2_format
*f
)
568 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
569 const struct coda_q_data
*q_data_src
;
570 const struct coda_codec
*codec
;
571 struct vb2_queue
*src_vq
;
575 ret
= coda_try_pixelformat(ctx
, f
);
579 q_data_src
= get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_OUTPUT
);
582 * If the source format is already fixed, only allow the same output
585 src_vq
= v4l2_m2m_get_vq(ctx
->fh
.m2m_ctx
, V4L2_BUF_TYPE_VIDEO_OUTPUT
);
586 if (vb2_is_streaming(src_vq
)) {
587 f
->fmt
.pix
.width
= q_data_src
->width
;
588 f
->fmt
.pix
.height
= q_data_src
->height
;
591 f
->fmt
.pix
.colorspace
= ctx
->colorspace
;
593 q_data_src
= get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_OUTPUT
);
594 codec
= coda_find_codec(ctx
->dev
, q_data_src
->fourcc
,
595 f
->fmt
.pix
.pixelformat
);
599 ret
= coda_try_fmt(ctx
, codec
, f
);
603 /* The h.264 decoder only returns complete 16x16 macroblocks */
604 if (codec
&& codec
->src_fourcc
== V4L2_PIX_FMT_H264
) {
605 f
->fmt
.pix
.width
= f
->fmt
.pix
.width
;
606 f
->fmt
.pix
.height
= round_up(f
->fmt
.pix
.height
, 16);
607 f
->fmt
.pix
.bytesperline
= round_up(f
->fmt
.pix
.width
, 16);
608 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.bytesperline
*
609 f
->fmt
.pix
.height
* 3 / 2;
611 ret
= coda_try_fmt_vdoa(ctx
, f
, &use_vdoa
);
615 if (f
->fmt
.pix
.pixelformat
== V4L2_PIX_FMT_YUYV
) {
619 f
->fmt
.pix
.bytesperline
= round_up(f
->fmt
.pix
.width
, 16) * 2;
620 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.bytesperline
*
628 static int coda_try_fmt_vid_out(struct file
*file
, void *priv
,
629 struct v4l2_format
*f
)
631 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
632 struct coda_dev
*dev
= ctx
->dev
;
633 const struct coda_q_data
*q_data_dst
;
634 const struct coda_codec
*codec
;
637 ret
= coda_try_pixelformat(ctx
, f
);
641 switch (f
->fmt
.pix
.colorspace
) {
642 case V4L2_COLORSPACE_REC709
:
643 case V4L2_COLORSPACE_JPEG
:
646 if (f
->fmt
.pix
.pixelformat
== V4L2_PIX_FMT_JPEG
)
647 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_JPEG
;
649 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_REC709
;
652 q_data_dst
= get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_CAPTURE
);
653 codec
= coda_find_codec(dev
, f
->fmt
.pix
.pixelformat
, q_data_dst
->fourcc
);
655 return coda_try_fmt(ctx
, codec
, f
);
658 static int coda_s_fmt(struct coda_ctx
*ctx
, struct v4l2_format
*f
,
661 struct coda_q_data
*q_data
;
662 struct vb2_queue
*vq
;
664 vq
= v4l2_m2m_get_vq(ctx
->fh
.m2m_ctx
, f
->type
);
668 q_data
= get_q_data(ctx
, f
->type
);
672 if (vb2_is_busy(vq
)) {
673 v4l2_err(&ctx
->dev
->v4l2_dev
, "%s queue busy\n", __func__
);
677 q_data
->fourcc
= f
->fmt
.pix
.pixelformat
;
678 q_data
->width
= f
->fmt
.pix
.width
;
679 q_data
->height
= f
->fmt
.pix
.height
;
680 q_data
->bytesperline
= f
->fmt
.pix
.bytesperline
;
681 q_data
->sizeimage
= f
->fmt
.pix
.sizeimage
;
685 q_data
->rect
.left
= 0;
686 q_data
->rect
.top
= 0;
687 q_data
->rect
.width
= f
->fmt
.pix
.width
;
688 q_data
->rect
.height
= f
->fmt
.pix
.height
;
691 switch (f
->fmt
.pix
.pixelformat
) {
692 case V4L2_PIX_FMT_YUYV
:
693 ctx
->tiled_map_type
= GDI_TILED_FRAME_MB_RASTER_MAP
;
695 case V4L2_PIX_FMT_NV12
:
696 ctx
->tiled_map_type
= GDI_TILED_FRAME_MB_RASTER_MAP
;
699 /* else fall through */
700 case V4L2_PIX_FMT_YUV420
:
701 case V4L2_PIX_FMT_YVU420
:
702 ctx
->tiled_map_type
= GDI_LINEAR_FRAME_MAP
;
708 if (ctx
->tiled_map_type
== GDI_TILED_FRAME_MB_RASTER_MAP
&&
709 !coda_try_fmt_vdoa(ctx
, f
, &ctx
->use_vdoa
) &&
711 vdoa_context_configure(ctx
->vdoa
, f
->fmt
.pix
.width
,
713 f
->fmt
.pix
.pixelformat
);
715 ctx
->use_vdoa
= false;
717 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
718 "Setting format for type %d, wxh: %dx%d, fmt: %4.4s %c\n",
719 f
->type
, q_data
->width
, q_data
->height
,
720 (char *)&q_data
->fourcc
,
721 (ctx
->tiled_map_type
== GDI_LINEAR_FRAME_MAP
) ? 'L' : 'T');
726 static int coda_s_fmt_vid_cap(struct file
*file
, void *priv
,
727 struct v4l2_format
*f
)
729 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
730 struct coda_q_data
*q_data_src
;
734 ret
= coda_try_fmt_vid_cap(file
, priv
, f
);
738 q_data_src
= get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_OUTPUT
);
741 r
.width
= q_data_src
->width
;
742 r
.height
= q_data_src
->height
;
744 return coda_s_fmt(ctx
, f
, &r
);
747 static int coda_s_fmt_vid_out(struct file
*file
, void *priv
,
748 struct v4l2_format
*f
)
750 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
751 struct coda_q_data
*q_data_src
;
752 struct v4l2_format f_cap
;
756 ret
= coda_try_fmt_vid_out(file
, priv
, f
);
760 ret
= coda_s_fmt(ctx
, f
, NULL
);
764 ctx
->colorspace
= f
->fmt
.pix
.colorspace
;
766 memset(&f_cap
, 0, sizeof(f_cap
));
767 f_cap
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
768 coda_g_fmt(file
, priv
, &f_cap
);
769 f_cap
.fmt
.pix
.width
= f
->fmt
.pix
.width
;
770 f_cap
.fmt
.pix
.height
= f
->fmt
.pix
.height
;
772 ret
= coda_try_fmt_vid_cap(file
, priv
, &f_cap
);
776 q_data_src
= get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_OUTPUT
);
779 r
.width
= q_data_src
->width
;
780 r
.height
= q_data_src
->height
;
782 return coda_s_fmt(ctx
, &f_cap
, &r
);
785 static int coda_reqbufs(struct file
*file
, void *priv
,
786 struct v4l2_requestbuffers
*rb
)
788 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
791 ret
= v4l2_m2m_reqbufs(file
, ctx
->fh
.m2m_ctx
, rb
);
796 * Allow to allocate instance specific per-context buffers, such as
797 * bitstream ringbuffer, slice buffer, work buffer, etc. if needed.
799 if (rb
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
&& ctx
->ops
->reqbufs
)
800 return ctx
->ops
->reqbufs(ctx
, rb
);
805 static int coda_qbuf(struct file
*file
, void *priv
,
806 struct v4l2_buffer
*buf
)
808 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
810 return v4l2_m2m_qbuf(file
, ctx
->fh
.m2m_ctx
, buf
);
813 static bool coda_buf_is_end_of_stream(struct coda_ctx
*ctx
,
814 struct vb2_v4l2_buffer
*buf
)
816 struct vb2_queue
*src_vq
;
818 src_vq
= v4l2_m2m_get_vq(ctx
->fh
.m2m_ctx
, V4L2_BUF_TYPE_VIDEO_OUTPUT
);
820 return ((ctx
->bit_stream_param
& CODA_BIT_STREAM_END_FLAG
) &&
821 (buf
->sequence
== (ctx
->qsequence
- 1)));
824 void coda_m2m_buf_done(struct coda_ctx
*ctx
, struct vb2_v4l2_buffer
*buf
,
825 enum vb2_buffer_state state
)
827 const struct v4l2_event eos_event
= {
828 .type
= V4L2_EVENT_EOS
831 if (coda_buf_is_end_of_stream(ctx
, buf
)) {
832 buf
->flags
|= V4L2_BUF_FLAG_LAST
;
834 v4l2_event_queue_fh(&ctx
->fh
, &eos_event
);
837 v4l2_m2m_buf_done(buf
, state
);
840 static int coda_g_selection(struct file
*file
, void *fh
,
841 struct v4l2_selection
*s
)
843 struct coda_ctx
*ctx
= fh_to_ctx(fh
);
844 struct coda_q_data
*q_data
;
845 struct v4l2_rect r
, *rsel
;
847 q_data
= get_q_data(ctx
, s
->type
);
853 r
.width
= q_data
->width
;
854 r
.height
= q_data
->height
;
855 rsel
= &q_data
->rect
;
858 case V4L2_SEL_TGT_CROP_DEFAULT
:
859 case V4L2_SEL_TGT_CROP_BOUNDS
:
862 case V4L2_SEL_TGT_CROP
:
863 if (s
->type
!= V4L2_BUF_TYPE_VIDEO_OUTPUT
)
866 case V4L2_SEL_TGT_COMPOSE_BOUNDS
:
867 case V4L2_SEL_TGT_COMPOSE_PADDED
:
870 case V4L2_SEL_TGT_COMPOSE
:
871 case V4L2_SEL_TGT_COMPOSE_DEFAULT
:
872 if (s
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
884 static int coda_try_decoder_cmd(struct file
*file
, void *fh
,
885 struct v4l2_decoder_cmd
*dc
)
887 if (dc
->cmd
!= V4L2_DEC_CMD_STOP
)
890 if (dc
->flags
& V4L2_DEC_CMD_STOP_TO_BLACK
)
893 if (!(dc
->flags
& V4L2_DEC_CMD_STOP_IMMEDIATELY
) && (dc
->stop
.pts
!= 0))
899 static int coda_decoder_cmd(struct file
*file
, void *fh
,
900 struct v4l2_decoder_cmd
*dc
)
902 struct coda_ctx
*ctx
= fh_to_ctx(fh
);
905 ret
= coda_try_decoder_cmd(file
, fh
, dc
);
909 /* Ignore decoder stop command silently in encoder context */
910 if (ctx
->inst_type
!= CODA_INST_DECODER
)
913 /* Set the stream-end flag on this context */
914 coda_bit_stream_end_flag(ctx
);
916 v4l2_m2m_try_schedule(ctx
->fh
.m2m_ctx
);
921 static int coda_g_parm(struct file
*file
, void *fh
, struct v4l2_streamparm
*a
)
923 struct coda_ctx
*ctx
= fh_to_ctx(fh
);
924 struct v4l2_fract
*tpf
;
926 if (a
->type
!= V4L2_BUF_TYPE_VIDEO_OUTPUT
)
929 a
->parm
.output
.capability
= V4L2_CAP_TIMEPERFRAME
;
930 tpf
= &a
->parm
.output
.timeperframe
;
931 tpf
->denominator
= ctx
->params
.framerate
& CODA_FRATE_RES_MASK
;
932 tpf
->numerator
= 1 + (ctx
->params
.framerate
>>
933 CODA_FRATE_DIV_OFFSET
);
939 * Approximate timeperframe v4l2_fract with values that can be written
940 * into the 16-bit CODA_FRATE_DIV and CODA_FRATE_RES fields.
942 static void coda_approximate_timeperframe(struct v4l2_fract
*timeperframe
)
944 struct v4l2_fract s
= *timeperframe
;
945 struct v4l2_fract f0
;
946 struct v4l2_fract f1
= { 1, 0 };
947 struct v4l2_fract f2
= { 0, 1 };
948 unsigned int i
, div
, s_denominator
;
950 /* Lower bound is 1/65535 */
951 if (s
.numerator
== 0 || s
.denominator
/ s
.numerator
> 65535) {
952 timeperframe
->numerator
= 1;
953 timeperframe
->denominator
= 65535;
957 /* Upper bound is 65536/1, map everything above to infinity */
958 if (s
.denominator
== 0 || s
.numerator
/ s
.denominator
> 65536) {
959 timeperframe
->numerator
= 1;
960 timeperframe
->denominator
= 0;
964 /* Reduce fraction to lowest terms */
965 div
= gcd(s
.numerator
, s
.denominator
);
968 s
.denominator
/= div
;
971 if (s
.numerator
<= 65536 && s
.denominator
< 65536) {
976 /* Find successive convergents from continued fraction expansion */
977 while (f2
.numerator
<= 65536 && f2
.denominator
< 65536) {
981 /* Stop when f2 exactly equals timeperframe */
982 if (s
.numerator
== 0)
985 i
= s
.denominator
/ s
.numerator
;
987 f2
.numerator
= f0
.numerator
+ i
* f1
.numerator
;
988 f2
.denominator
= f0
.denominator
+ i
* f2
.denominator
;
990 s_denominator
= s
.numerator
;
991 s
.numerator
= s
.denominator
% s
.numerator
;
992 s
.denominator
= s_denominator
;
998 static uint32_t coda_timeperframe_to_frate(struct v4l2_fract
*timeperframe
)
1000 return ((timeperframe
->numerator
- 1) << CODA_FRATE_DIV_OFFSET
) |
1001 timeperframe
->denominator
;
1004 static int coda_s_parm(struct file
*file
, void *fh
, struct v4l2_streamparm
*a
)
1006 struct coda_ctx
*ctx
= fh_to_ctx(fh
);
1007 struct v4l2_fract
*tpf
;
1009 if (a
->type
!= V4L2_BUF_TYPE_VIDEO_OUTPUT
)
1012 tpf
= &a
->parm
.output
.timeperframe
;
1013 coda_approximate_timeperframe(tpf
);
1014 ctx
->params
.framerate
= coda_timeperframe_to_frate(tpf
);
1019 static int coda_subscribe_event(struct v4l2_fh
*fh
,
1020 const struct v4l2_event_subscription
*sub
)
1022 switch (sub
->type
) {
1023 case V4L2_EVENT_EOS
:
1024 return v4l2_event_subscribe(fh
, sub
, 0, NULL
);
1026 return v4l2_ctrl_subscribe_event(fh
, sub
);
1030 static const struct v4l2_ioctl_ops coda_ioctl_ops
= {
1031 .vidioc_querycap
= coda_querycap
,
1033 .vidioc_enum_fmt_vid_cap
= coda_enum_fmt
,
1034 .vidioc_g_fmt_vid_cap
= coda_g_fmt
,
1035 .vidioc_try_fmt_vid_cap
= coda_try_fmt_vid_cap
,
1036 .vidioc_s_fmt_vid_cap
= coda_s_fmt_vid_cap
,
1038 .vidioc_enum_fmt_vid_out
= coda_enum_fmt
,
1039 .vidioc_g_fmt_vid_out
= coda_g_fmt
,
1040 .vidioc_try_fmt_vid_out
= coda_try_fmt_vid_out
,
1041 .vidioc_s_fmt_vid_out
= coda_s_fmt_vid_out
,
1043 .vidioc_reqbufs
= coda_reqbufs
,
1044 .vidioc_querybuf
= v4l2_m2m_ioctl_querybuf
,
1046 .vidioc_qbuf
= coda_qbuf
,
1047 .vidioc_expbuf
= v4l2_m2m_ioctl_expbuf
,
1048 .vidioc_dqbuf
= v4l2_m2m_ioctl_dqbuf
,
1049 .vidioc_create_bufs
= v4l2_m2m_ioctl_create_bufs
,
1050 .vidioc_prepare_buf
= v4l2_m2m_ioctl_prepare_buf
,
1052 .vidioc_streamon
= v4l2_m2m_ioctl_streamon
,
1053 .vidioc_streamoff
= v4l2_m2m_ioctl_streamoff
,
1055 .vidioc_g_selection
= coda_g_selection
,
1057 .vidioc_try_decoder_cmd
= coda_try_decoder_cmd
,
1058 .vidioc_decoder_cmd
= coda_decoder_cmd
,
1060 .vidioc_g_parm
= coda_g_parm
,
1061 .vidioc_s_parm
= coda_s_parm
,
1063 .vidioc_subscribe_event
= coda_subscribe_event
,
1064 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
1068 * Mem-to-mem operations.
1071 static void coda_device_run(void *m2m_priv
)
1073 struct coda_ctx
*ctx
= m2m_priv
;
1074 struct coda_dev
*dev
= ctx
->dev
;
1076 queue_work(dev
->workqueue
, &ctx
->pic_run_work
);
1079 static void coda_pic_run_work(struct work_struct
*work
)
1081 struct coda_ctx
*ctx
= container_of(work
, struct coda_ctx
, pic_run_work
);
1082 struct coda_dev
*dev
= ctx
->dev
;
1085 mutex_lock(&ctx
->buffer_mutex
);
1086 mutex_lock(&dev
->coda_mutex
);
1088 ret
= ctx
->ops
->prepare_run(ctx
);
1089 if (ret
< 0 && ctx
->inst_type
== CODA_INST_DECODER
) {
1090 mutex_unlock(&dev
->coda_mutex
);
1091 mutex_unlock(&ctx
->buffer_mutex
);
1092 /* job_finish scheduled by prepare_decode */
1096 if (!wait_for_completion_timeout(&ctx
->completion
,
1097 msecs_to_jiffies(1000))) {
1098 dev_err(&dev
->plat_dev
->dev
, "CODA PIC_RUN timeout\n");
1103 } else if (!ctx
->aborting
) {
1104 ctx
->ops
->finish_run(ctx
);
1107 if ((ctx
->aborting
|| (!ctx
->streamon_cap
&& !ctx
->streamon_out
)) &&
1108 ctx
->ops
->seq_end_work
)
1109 queue_work(dev
->workqueue
, &ctx
->seq_end_work
);
1111 mutex_unlock(&dev
->coda_mutex
);
1112 mutex_unlock(&ctx
->buffer_mutex
);
1114 v4l2_m2m_job_finish(ctx
->dev
->m2m_dev
, ctx
->fh
.m2m_ctx
);
1117 static int coda_job_ready(void *m2m_priv
)
1119 struct coda_ctx
*ctx
= m2m_priv
;
1120 int src_bufs
= v4l2_m2m_num_src_bufs_ready(ctx
->fh
.m2m_ctx
);
1123 * For both 'P' and 'key' frame cases 1 picture
1124 * and 1 frame are needed. In the decoder case,
1125 * the compressed frame can be in the bitstream.
1127 if (!src_bufs
&& ctx
->inst_type
!= CODA_INST_DECODER
) {
1128 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
1129 "not ready: not enough video buffers.\n");
1133 if (!v4l2_m2m_num_dst_bufs_ready(ctx
->fh
.m2m_ctx
)) {
1134 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
1135 "not ready: not enough video capture buffers.\n");
1139 if (ctx
->inst_type
== CODA_INST_DECODER
&& ctx
->use_bit
) {
1140 bool stream_end
= ctx
->bit_stream_param
&
1141 CODA_BIT_STREAM_END_FLAG
;
1142 int num_metas
= ctx
->num_metas
;
1145 count
= hweight32(ctx
->frm_dis_flg
);
1146 if (ctx
->use_vdoa
&& count
>= (ctx
->num_internal_frames
- 1)) {
1147 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
1148 "%d: not ready: all internal buffers in use: %d/%d (0x%x)",
1149 ctx
->idx
, count
, ctx
->num_internal_frames
,
1154 if (ctx
->hold
&& !src_bufs
) {
1155 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
1156 "%d: not ready: on hold for more buffers.\n",
1161 if (!stream_end
&& (num_metas
+ src_bufs
) < 2) {
1162 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
1163 "%d: not ready: need 2 buffers available (%d, %d)\n",
1164 ctx
->idx
, num_metas
, src_bufs
);
1169 if (!src_bufs
&& !stream_end
&&
1170 (coda_get_bitstream_payload(ctx
) < 512)) {
1171 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
1172 "%d: not ready: not enough bitstream data (%d).\n",
1173 ctx
->idx
, coda_get_bitstream_payload(ctx
));
1178 if (ctx
->aborting
) {
1179 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
1180 "not ready: aborting\n");
1184 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
1190 static void coda_job_abort(void *priv
)
1192 struct coda_ctx
*ctx
= priv
;
1196 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
1200 static void coda_lock(void *m2m_priv
)
1202 struct coda_ctx
*ctx
= m2m_priv
;
1203 struct coda_dev
*pcdev
= ctx
->dev
;
1205 mutex_lock(&pcdev
->dev_mutex
);
1208 static void coda_unlock(void *m2m_priv
)
1210 struct coda_ctx
*ctx
= m2m_priv
;
1211 struct coda_dev
*pcdev
= ctx
->dev
;
1213 mutex_unlock(&pcdev
->dev_mutex
);
1216 static const struct v4l2_m2m_ops coda_m2m_ops
= {
1217 .device_run
= coda_device_run
,
1218 .job_ready
= coda_job_ready
,
1219 .job_abort
= coda_job_abort
,
1221 .unlock
= coda_unlock
,
1224 static void set_default_params(struct coda_ctx
*ctx
)
1226 unsigned int max_w
, max_h
, usize
, csize
;
1228 ctx
->codec
= coda_find_codec(ctx
->dev
, ctx
->cvd
->src_formats
[0],
1229 ctx
->cvd
->dst_formats
[0]);
1230 max_w
= min(ctx
->codec
->max_w
, 1920U);
1231 max_h
= min(ctx
->codec
->max_h
, 1088U);
1232 usize
= max_w
* max_h
* 3 / 2;
1233 csize
= coda_estimate_sizeimage(ctx
, usize
, max_w
, max_h
);
1235 ctx
->params
.codec_mode
= ctx
->codec
->mode
;
1236 ctx
->colorspace
= V4L2_COLORSPACE_REC709
;
1237 ctx
->params
.framerate
= 30;
1239 /* Default formats for output and input queues */
1240 ctx
->q_data
[V4L2_M2M_SRC
].fourcc
= ctx
->cvd
->src_formats
[0];
1241 ctx
->q_data
[V4L2_M2M_DST
].fourcc
= ctx
->cvd
->dst_formats
[0];
1242 ctx
->q_data
[V4L2_M2M_SRC
].width
= max_w
;
1243 ctx
->q_data
[V4L2_M2M_SRC
].height
= max_h
;
1244 ctx
->q_data
[V4L2_M2M_DST
].width
= max_w
;
1245 ctx
->q_data
[V4L2_M2M_DST
].height
= max_h
;
1246 if (ctx
->codec
->src_fourcc
== V4L2_PIX_FMT_YUV420
) {
1247 ctx
->q_data
[V4L2_M2M_SRC
].bytesperline
= max_w
;
1248 ctx
->q_data
[V4L2_M2M_SRC
].sizeimage
= usize
;
1249 ctx
->q_data
[V4L2_M2M_DST
].bytesperline
= 0;
1250 ctx
->q_data
[V4L2_M2M_DST
].sizeimage
= csize
;
1252 ctx
->q_data
[V4L2_M2M_SRC
].bytesperline
= 0;
1253 ctx
->q_data
[V4L2_M2M_SRC
].sizeimage
= csize
;
1254 ctx
->q_data
[V4L2_M2M_DST
].bytesperline
= max_w
;
1255 ctx
->q_data
[V4L2_M2M_DST
].sizeimage
= usize
;
1257 ctx
->q_data
[V4L2_M2M_SRC
].rect
.width
= max_w
;
1258 ctx
->q_data
[V4L2_M2M_SRC
].rect
.height
= max_h
;
1259 ctx
->q_data
[V4L2_M2M_DST
].rect
.width
= max_w
;
1260 ctx
->q_data
[V4L2_M2M_DST
].rect
.height
= max_h
;
1263 * Since the RBC2AXI logic only supports a single chroma plane,
1264 * macroblock tiling only works for to NV12 pixel format.
1266 ctx
->tiled_map_type
= GDI_LINEAR_FRAME_MAP
;
1272 static int coda_queue_setup(struct vb2_queue
*vq
,
1273 unsigned int *nbuffers
, unsigned int *nplanes
,
1274 unsigned int sizes
[], struct device
*alloc_devs
[])
1276 struct coda_ctx
*ctx
= vb2_get_drv_priv(vq
);
1277 struct coda_q_data
*q_data
;
1280 q_data
= get_q_data(ctx
, vq
->type
);
1281 size
= q_data
->sizeimage
;
1286 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
1287 "get %d buffer(s) of size %d each.\n", *nbuffers
, size
);
1292 static int coda_buf_prepare(struct vb2_buffer
*vb
)
1294 struct coda_ctx
*ctx
= vb2_get_drv_priv(vb
->vb2_queue
);
1295 struct coda_q_data
*q_data
;
1297 q_data
= get_q_data(ctx
, vb
->vb2_queue
->type
);
1299 if (vb2_plane_size(vb
, 0) < q_data
->sizeimage
) {
1300 v4l2_warn(&ctx
->dev
->v4l2_dev
,
1301 "%s data will not fit into plane (%lu < %lu)\n",
1302 __func__
, vb2_plane_size(vb
, 0),
1303 (long)q_data
->sizeimage
);
1310 static void coda_buf_queue(struct vb2_buffer
*vb
)
1312 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
1313 struct coda_ctx
*ctx
= vb2_get_drv_priv(vb
->vb2_queue
);
1314 struct vb2_queue
*vq
= vb
->vb2_queue
;
1315 struct coda_q_data
*q_data
;
1317 q_data
= get_q_data(ctx
, vb
->vb2_queue
->type
);
1320 * In the decoder case, immediately try to copy the buffer into the
1321 * bitstream ringbuffer and mark it as ready to be dequeued.
1323 if (ctx
->bitstream
.size
&& vq
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
) {
1325 * For backwards compatibility, queuing an empty buffer marks
1328 if (vb2_get_plane_payload(vb
, 0) == 0)
1329 coda_bit_stream_end_flag(ctx
);
1330 mutex_lock(&ctx
->bitstream_mutex
);
1331 v4l2_m2m_buf_queue(ctx
->fh
.m2m_ctx
, vbuf
);
1332 if (vb2_is_streaming(vb
->vb2_queue
))
1333 coda_fill_bitstream(ctx
, true);
1334 mutex_unlock(&ctx
->bitstream_mutex
);
1336 v4l2_m2m_buf_queue(ctx
->fh
.m2m_ctx
, vbuf
);
1340 int coda_alloc_aux_buf(struct coda_dev
*dev
, struct coda_aux_buf
*buf
,
1341 size_t size
, const char *name
, struct dentry
*parent
)
1343 buf
->vaddr
= dma_alloc_coherent(&dev
->plat_dev
->dev
, size
, &buf
->paddr
,
1346 v4l2_err(&dev
->v4l2_dev
,
1347 "Failed to allocate %s buffer of size %u\n",
1354 if (name
&& parent
) {
1355 buf
->blob
.data
= buf
->vaddr
;
1356 buf
->blob
.size
= size
;
1357 buf
->dentry
= debugfs_create_blob(name
, 0644, parent
,
1360 dev_warn(&dev
->plat_dev
->dev
,
1361 "failed to create debugfs entry %s\n", name
);
1367 void coda_free_aux_buf(struct coda_dev
*dev
,
1368 struct coda_aux_buf
*buf
)
1371 dma_free_coherent(&dev
->plat_dev
->dev
, buf
->size
,
1372 buf
->vaddr
, buf
->paddr
);
1375 debugfs_remove(buf
->dentry
);
1380 static int coda_start_streaming(struct vb2_queue
*q
, unsigned int count
)
1382 struct coda_ctx
*ctx
= vb2_get_drv_priv(q
);
1383 struct v4l2_device
*v4l2_dev
= &ctx
->dev
->v4l2_dev
;
1384 struct coda_q_data
*q_data_src
, *q_data_dst
;
1385 struct vb2_v4l2_buffer
*buf
;
1391 q_data_src
= get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_OUTPUT
);
1392 if (q
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
) {
1393 if (ctx
->inst_type
== CODA_INST_DECODER
&& ctx
->use_bit
) {
1394 /* copy the buffers that were queued before streamon */
1395 mutex_lock(&ctx
->bitstream_mutex
);
1396 coda_fill_bitstream(ctx
, false);
1397 mutex_unlock(&ctx
->bitstream_mutex
);
1399 if (coda_get_bitstream_payload(ctx
) < 512) {
1405 ctx
->streamon_out
= 1;
1407 ctx
->streamon_cap
= 1;
1410 /* Don't start the coda unless both queues are on */
1411 if (!(ctx
->streamon_out
& ctx
->streamon_cap
))
1414 q_data_dst
= get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_CAPTURE
);
1415 if ((q_data_src
->width
!= q_data_dst
->width
&&
1416 round_up(q_data_src
->width
, 16) != q_data_dst
->width
) ||
1417 (q_data_src
->height
!= q_data_dst
->height
&&
1418 round_up(q_data_src
->height
, 16) != q_data_dst
->height
)) {
1419 v4l2_err(v4l2_dev
, "can't convert %dx%d to %dx%d\n",
1420 q_data_src
->width
, q_data_src
->height
,
1421 q_data_dst
->width
, q_data_dst
->height
);
1426 /* Allow BIT decoder device_run with no new buffers queued */
1427 if (ctx
->inst_type
== CODA_INST_DECODER
&& ctx
->use_bit
)
1428 v4l2_m2m_set_src_buffered(ctx
->fh
.m2m_ctx
, true);
1430 ctx
->gopcounter
= ctx
->params
.gop_size
- 1;
1432 ctx
->codec
= coda_find_codec(ctx
->dev
, q_data_src
->fourcc
,
1433 q_data_dst
->fourcc
);
1435 v4l2_err(v4l2_dev
, "couldn't tell instance type.\n");
1440 if (q_data_dst
->fourcc
== V4L2_PIX_FMT_JPEG
)
1441 ctx
->params
.gop_size
= 1;
1442 ctx
->gopcounter
= ctx
->params
.gop_size
- 1;
1444 ret
= ctx
->ops
->start_streaming(ctx
);
1445 if (ctx
->inst_type
== CODA_INST_DECODER
) {
1455 if (q
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
) {
1456 while ((buf
= v4l2_m2m_src_buf_remove(ctx
->fh
.m2m_ctx
)))
1457 v4l2_m2m_buf_done(buf
, VB2_BUF_STATE_QUEUED
);
1459 while ((buf
= v4l2_m2m_dst_buf_remove(ctx
->fh
.m2m_ctx
)))
1460 v4l2_m2m_buf_done(buf
, VB2_BUF_STATE_QUEUED
);
1465 static void coda_stop_streaming(struct vb2_queue
*q
)
1467 struct coda_ctx
*ctx
= vb2_get_drv_priv(q
);
1468 struct coda_dev
*dev
= ctx
->dev
;
1469 struct vb2_v4l2_buffer
*buf
;
1470 unsigned long flags
;
1473 stop
= ctx
->streamon_out
&& ctx
->streamon_cap
;
1475 if (q
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
) {
1476 v4l2_dbg(1, coda_debug
, &dev
->v4l2_dev
,
1477 "%s: output\n", __func__
);
1478 ctx
->streamon_out
= 0;
1480 coda_bit_stream_end_flag(ctx
);
1484 while ((buf
= v4l2_m2m_src_buf_remove(ctx
->fh
.m2m_ctx
)))
1485 v4l2_m2m_buf_done(buf
, VB2_BUF_STATE_ERROR
);
1487 v4l2_dbg(1, coda_debug
, &dev
->v4l2_dev
,
1488 "%s: capture\n", __func__
);
1489 ctx
->streamon_cap
= 0;
1492 ctx
->sequence_offset
= 0;
1494 while ((buf
= v4l2_m2m_dst_buf_remove(ctx
->fh
.m2m_ctx
)))
1495 v4l2_m2m_buf_done(buf
, VB2_BUF_STATE_ERROR
);
1499 struct coda_buffer_meta
*meta
;
1501 if (ctx
->ops
->seq_end_work
) {
1502 queue_work(dev
->workqueue
, &ctx
->seq_end_work
);
1503 flush_work(&ctx
->seq_end_work
);
1505 spin_lock_irqsave(&ctx
->buffer_meta_lock
, flags
);
1506 while (!list_empty(&ctx
->buffer_meta_list
)) {
1507 meta
= list_first_entry(&ctx
->buffer_meta_list
,
1508 struct coda_buffer_meta
, list
);
1509 list_del(&meta
->list
);
1513 spin_unlock_irqrestore(&ctx
->buffer_meta_lock
, flags
);
1514 kfifo_init(&ctx
->bitstream_fifo
,
1515 ctx
->bitstream
.vaddr
, ctx
->bitstream
.size
);
1516 ctx
->runcounter
= 0;
1520 if (!ctx
->streamon_out
&& !ctx
->streamon_cap
)
1521 ctx
->bit_stream_param
&= ~CODA_BIT_STREAM_END_FLAG
;
1524 static const struct vb2_ops coda_qops
= {
1525 .queue_setup
= coda_queue_setup
,
1526 .buf_prepare
= coda_buf_prepare
,
1527 .buf_queue
= coda_buf_queue
,
1528 .start_streaming
= coda_start_streaming
,
1529 .stop_streaming
= coda_stop_streaming
,
1530 .wait_prepare
= vb2_ops_wait_prepare
,
1531 .wait_finish
= vb2_ops_wait_finish
,
1534 static int coda_s_ctrl(struct v4l2_ctrl
*ctrl
)
1536 struct coda_ctx
*ctx
=
1537 container_of(ctrl
->handler
, struct coda_ctx
, ctrls
);
1539 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
1540 "s_ctrl: id = %d, val = %d\n", ctrl
->id
, ctrl
->val
);
1543 case V4L2_CID_HFLIP
:
1545 ctx
->params
.rot_mode
|= CODA_MIR_HOR
;
1547 ctx
->params
.rot_mode
&= ~CODA_MIR_HOR
;
1549 case V4L2_CID_VFLIP
:
1551 ctx
->params
.rot_mode
|= CODA_MIR_VER
;
1553 ctx
->params
.rot_mode
&= ~CODA_MIR_VER
;
1555 case V4L2_CID_MPEG_VIDEO_BITRATE
:
1556 ctx
->params
.bitrate
= ctrl
->val
/ 1000;
1558 case V4L2_CID_MPEG_VIDEO_GOP_SIZE
:
1559 ctx
->params
.gop_size
= ctrl
->val
;
1561 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP
:
1562 ctx
->params
.h264_intra_qp
= ctrl
->val
;
1564 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP
:
1565 ctx
->params
.h264_inter_qp
= ctrl
->val
;
1567 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP
:
1568 ctx
->params
.h264_min_qp
= ctrl
->val
;
1570 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP
:
1571 ctx
->params
.h264_max_qp
= ctrl
->val
;
1573 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA
:
1574 ctx
->params
.h264_deblk_alpha
= ctrl
->val
;
1576 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA
:
1577 ctx
->params
.h264_deblk_beta
= ctrl
->val
;
1579 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE
:
1580 ctx
->params
.h264_deblk_enabled
= (ctrl
->val
==
1581 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED
);
1583 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP
:
1584 ctx
->params
.mpeg4_intra_qp
= ctrl
->val
;
1586 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP
:
1587 ctx
->params
.mpeg4_inter_qp
= ctrl
->val
;
1589 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE
:
1590 ctx
->params
.slice_mode
= ctrl
->val
;
1592 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB
:
1593 ctx
->params
.slice_max_mb
= ctrl
->val
;
1595 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES
:
1596 ctx
->params
.slice_max_bits
= ctrl
->val
* 8;
1598 case V4L2_CID_MPEG_VIDEO_HEADER_MODE
:
1600 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB
:
1601 ctx
->params
.intra_refresh
= ctrl
->val
;
1603 case V4L2_CID_JPEG_COMPRESSION_QUALITY
:
1604 coda_set_jpeg_compression_quality(ctx
, ctrl
->val
);
1606 case V4L2_CID_JPEG_RESTART_INTERVAL
:
1607 ctx
->params
.jpeg_restart_interval
= ctrl
->val
;
1609 case V4L2_CID_MPEG_VIDEO_VBV_DELAY
:
1610 ctx
->params
.vbv_delay
= ctrl
->val
;
1612 case V4L2_CID_MPEG_VIDEO_VBV_SIZE
:
1613 ctx
->params
.vbv_size
= min(ctrl
->val
* 8192, 0x7fffffff);
1616 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
1617 "Invalid control, id=%d, val=%d\n",
1618 ctrl
->id
, ctrl
->val
);
1625 static const struct v4l2_ctrl_ops coda_ctrl_ops
= {
1626 .s_ctrl
= coda_s_ctrl
,
1629 static void coda_encode_ctrls(struct coda_ctx
*ctx
)
1631 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1632 V4L2_CID_MPEG_VIDEO_BITRATE
, 0, 32767000, 1000, 0);
1633 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1634 V4L2_CID_MPEG_VIDEO_GOP_SIZE
, 1, 60, 1, 16);
1635 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1636 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP
, 0, 51, 1, 25);
1637 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1638 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP
, 0, 51, 1, 25);
1639 if (ctx
->dev
->devtype
->product
!= CODA_960
) {
1640 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1641 V4L2_CID_MPEG_VIDEO_H264_MIN_QP
, 0, 51, 1, 12);
1643 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1644 V4L2_CID_MPEG_VIDEO_H264_MAX_QP
, 0, 51, 1, 51);
1645 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1646 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA
, 0, 15, 1, 0);
1647 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1648 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA
, 0, 15, 1, 0);
1649 v4l2_ctrl_new_std_menu(&ctx
->ctrls
, &coda_ctrl_ops
,
1650 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE
,
1651 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED
, 0x0,
1652 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED
);
1653 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1654 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP
, 1, 31, 1, 2);
1655 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1656 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP
, 1, 31, 1, 2);
1657 v4l2_ctrl_new_std_menu(&ctx
->ctrls
, &coda_ctrl_ops
,
1658 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE
,
1659 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES
, 0x0,
1660 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE
);
1661 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1662 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB
, 1, 0x3fffffff, 1, 1);
1663 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1664 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES
, 1, 0x3fffffff, 1,
1666 v4l2_ctrl_new_std_menu(&ctx
->ctrls
, &coda_ctrl_ops
,
1667 V4L2_CID_MPEG_VIDEO_HEADER_MODE
,
1668 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME
,
1669 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE
),
1670 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME
);
1671 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1672 V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB
, 0,
1673 1920 * 1088 / 256, 1, 0);
1674 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1675 V4L2_CID_MPEG_VIDEO_VBV_DELAY
, 0, 0x7fff, 1, 0);
1677 * The maximum VBV size value is 0x7fffffff bits,
1678 * one bit less than 262144 KiB
1680 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1681 V4L2_CID_MPEG_VIDEO_VBV_SIZE
, 0, 262144, 1, 0);
1684 static void coda_jpeg_encode_ctrls(struct coda_ctx
*ctx
)
1686 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1687 V4L2_CID_JPEG_COMPRESSION_QUALITY
, 5, 100, 1, 50);
1688 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1689 V4L2_CID_JPEG_RESTART_INTERVAL
, 0, 100, 1, 0);
1692 static int coda_ctrls_setup(struct coda_ctx
*ctx
)
1694 v4l2_ctrl_handler_init(&ctx
->ctrls
, 2);
1696 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1697 V4L2_CID_HFLIP
, 0, 1, 1, 0);
1698 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1699 V4L2_CID_VFLIP
, 0, 1, 1, 0);
1700 if (ctx
->inst_type
== CODA_INST_ENCODER
) {
1701 if (ctx
->cvd
->dst_formats
[0] == V4L2_PIX_FMT_JPEG
)
1702 coda_jpeg_encode_ctrls(ctx
);
1704 coda_encode_ctrls(ctx
);
1707 if (ctx
->ctrls
.error
) {
1708 v4l2_err(&ctx
->dev
->v4l2_dev
,
1709 "control initialization error (%d)",
1714 return v4l2_ctrl_handler_setup(&ctx
->ctrls
);
1717 static int coda_queue_init(struct coda_ctx
*ctx
, struct vb2_queue
*vq
)
1720 vq
->ops
= &coda_qops
;
1721 vq
->buf_struct_size
= sizeof(struct v4l2_m2m_buffer
);
1722 vq
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_COPY
;
1723 vq
->lock
= &ctx
->dev
->dev_mutex
;
1724 /* One way to indicate end-of-stream for coda is to set the
1725 * bytesused == 0. However by default videobuf2 handles bytesused
1726 * equal to 0 as a special case and changes its value to the size
1727 * of the buffer. Set the allow_zero_bytesused flag, so
1728 * that videobuf2 will keep the value of bytesused intact.
1730 vq
->allow_zero_bytesused
= 1;
1731 vq
->dev
= &ctx
->dev
->plat_dev
->dev
;
1733 return vb2_queue_init(vq
);
1736 int coda_encoder_queue_init(void *priv
, struct vb2_queue
*src_vq
,
1737 struct vb2_queue
*dst_vq
)
1741 src_vq
->type
= V4L2_BUF_TYPE_VIDEO_OUTPUT
;
1742 src_vq
->io_modes
= VB2_DMABUF
| VB2_MMAP
;
1743 src_vq
->mem_ops
= &vb2_dma_contig_memops
;
1745 ret
= coda_queue_init(priv
, src_vq
);
1749 dst_vq
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1750 dst_vq
->io_modes
= VB2_DMABUF
| VB2_MMAP
;
1751 dst_vq
->mem_ops
= &vb2_dma_contig_memops
;
1753 return coda_queue_init(priv
, dst_vq
);
1756 int coda_decoder_queue_init(void *priv
, struct vb2_queue
*src_vq
,
1757 struct vb2_queue
*dst_vq
)
1761 src_vq
->type
= V4L2_BUF_TYPE_VIDEO_OUTPUT
;
1762 src_vq
->io_modes
= VB2_DMABUF
| VB2_MMAP
| VB2_USERPTR
;
1763 src_vq
->mem_ops
= &vb2_vmalloc_memops
;
1765 ret
= coda_queue_init(priv
, src_vq
);
1769 dst_vq
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1770 dst_vq
->io_modes
= VB2_DMABUF
| VB2_MMAP
;
1771 dst_vq
->mem_ops
= &vb2_dma_contig_memops
;
1773 return coda_queue_init(priv
, dst_vq
);
1776 static int coda_next_free_instance(struct coda_dev
*dev
)
1778 int idx
= ffz(dev
->instance_mask
);
1781 (dev
->devtype
->product
== CODA_DX6
&& idx
> CODADX6_MAX_INSTANCES
))
1791 static int coda_open(struct file
*file
)
1793 struct video_device
*vdev
= video_devdata(file
);
1794 struct coda_dev
*dev
= video_get_drvdata(vdev
);
1795 struct coda_ctx
*ctx
= NULL
;
1800 ctx
= kzalloc(sizeof(*ctx
), GFP_KERNEL
);
1804 idx
= coda_next_free_instance(dev
);
1809 set_bit(idx
, &dev
->instance_mask
);
1811 name
= kasprintf(GFP_KERNEL
, "context%d", idx
);
1814 goto err_coda_name_init
;
1817 ctx
->debugfs_entry
= debugfs_create_dir(name
, dev
->debugfs_root
);
1820 ctx
->cvd
= to_coda_video_device(vdev
);
1821 ctx
->inst_type
= ctx
->cvd
->type
;
1822 ctx
->ops
= ctx
->cvd
->ops
;
1823 ctx
->use_bit
= !ctx
->cvd
->direct
;
1824 init_completion(&ctx
->completion
);
1825 INIT_WORK(&ctx
->pic_run_work
, coda_pic_run_work
);
1826 if (ctx
->ops
->seq_end_work
)
1827 INIT_WORK(&ctx
->seq_end_work
, ctx
->ops
->seq_end_work
);
1828 v4l2_fh_init(&ctx
->fh
, video_devdata(file
));
1829 file
->private_data
= &ctx
->fh
;
1830 v4l2_fh_add(&ctx
->fh
);
1833 switch (dev
->devtype
->product
) {
1835 ctx
->frame_mem_ctrl
= 1 << 12;
1843 if (ctx
->dev
->vdoa
&& !disable_vdoa
) {
1844 ctx
->vdoa
= vdoa_context_create(dev
->vdoa
);
1846 v4l2_warn(&dev
->v4l2_dev
,
1847 "Failed to create vdoa context: not using vdoa");
1849 ctx
->use_vdoa
= false;
1851 /* Power up and upload firmware if necessary */
1852 ret
= pm_runtime_get_sync(&dev
->plat_dev
->dev
);
1854 v4l2_err(&dev
->v4l2_dev
, "failed to power up: %d\n", ret
);
1858 ret
= clk_prepare_enable(dev
->clk_per
);
1862 ret
= clk_prepare_enable(dev
->clk_ahb
);
1866 set_default_params(ctx
);
1867 ctx
->fh
.m2m_ctx
= v4l2_m2m_ctx_init(dev
->m2m_dev
, ctx
,
1868 ctx
->ops
->queue_init
);
1869 if (IS_ERR(ctx
->fh
.m2m_ctx
)) {
1870 ret
= PTR_ERR(ctx
->fh
.m2m_ctx
);
1872 v4l2_err(&dev
->v4l2_dev
, "%s return error (%d)\n",
1877 ret
= coda_ctrls_setup(ctx
);
1879 v4l2_err(&dev
->v4l2_dev
, "failed to setup coda controls\n");
1880 goto err_ctrls_setup
;
1883 ctx
->fh
.ctrl_handler
= &ctx
->ctrls
;
1885 mutex_init(&ctx
->bitstream_mutex
);
1886 mutex_init(&ctx
->buffer_mutex
);
1887 INIT_LIST_HEAD(&ctx
->buffer_meta_list
);
1888 spin_lock_init(&ctx
->buffer_meta_lock
);
1891 list_add(&ctx
->list
, &dev
->instances
);
1894 v4l2_dbg(1, coda_debug
, &dev
->v4l2_dev
, "Created instance %d (%p)\n",
1900 v4l2_m2m_ctx_release(ctx
->fh
.m2m_ctx
);
1902 clk_disable_unprepare(dev
->clk_ahb
);
1904 clk_disable_unprepare(dev
->clk_per
);
1906 pm_runtime_put_sync(&dev
->plat_dev
->dev
);
1908 v4l2_fh_del(&ctx
->fh
);
1909 v4l2_fh_exit(&ctx
->fh
);
1910 clear_bit(ctx
->idx
, &dev
->instance_mask
);
1917 static int coda_release(struct file
*file
)
1919 struct coda_dev
*dev
= video_drvdata(file
);
1920 struct coda_ctx
*ctx
= fh_to_ctx(file
->private_data
);
1922 v4l2_dbg(1, coda_debug
, &dev
->v4l2_dev
, "Releasing instance %p\n",
1925 if (ctx
->inst_type
== CODA_INST_DECODER
&& ctx
->use_bit
)
1926 coda_bit_stream_end_flag(ctx
);
1928 /* If this instance is running, call .job_abort and wait for it to end */
1929 v4l2_m2m_ctx_release(ctx
->fh
.m2m_ctx
);
1932 vdoa_context_destroy(ctx
->vdoa
);
1934 /* In case the instance was not running, we still need to call SEQ_END */
1935 if (ctx
->ops
->seq_end_work
) {
1936 queue_work(dev
->workqueue
, &ctx
->seq_end_work
);
1937 flush_work(&ctx
->seq_end_work
);
1941 list_del(&ctx
->list
);
1944 if (ctx
->dev
->devtype
->product
== CODA_DX6
)
1945 coda_free_aux_buf(dev
, &ctx
->workbuf
);
1947 v4l2_ctrl_handler_free(&ctx
->ctrls
);
1948 clk_disable_unprepare(dev
->clk_ahb
);
1949 clk_disable_unprepare(dev
->clk_per
);
1950 pm_runtime_put_sync(&dev
->plat_dev
->dev
);
1951 v4l2_fh_del(&ctx
->fh
);
1952 v4l2_fh_exit(&ctx
->fh
);
1953 clear_bit(ctx
->idx
, &dev
->instance_mask
);
1954 if (ctx
->ops
->release
)
1955 ctx
->ops
->release(ctx
);
1956 debugfs_remove_recursive(ctx
->debugfs_entry
);
1962 static const struct v4l2_file_operations coda_fops
= {
1963 .owner
= THIS_MODULE
,
1965 .release
= coda_release
,
1966 .poll
= v4l2_m2m_fop_poll
,
1967 .unlocked_ioctl
= video_ioctl2
,
1968 .mmap
= v4l2_m2m_fop_mmap
,
1971 static int coda_hw_init(struct coda_dev
*dev
)
1977 ret
= clk_prepare_enable(dev
->clk_per
);
1981 ret
= clk_prepare_enable(dev
->clk_ahb
);
1986 reset_control_reset(dev
->rstc
);
1989 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
1990 * The 16-bit chars in the code buffer are in memory access
1991 * order, re-sort them to CODA order for register download.
1992 * Data in this SRAM survives a reboot.
1994 p
= (u16
*)dev
->codebuf
.vaddr
;
1995 if (dev
->devtype
->product
== CODA_DX6
) {
1996 for (i
= 0; i
< (CODA_ISRAM_SIZE
/ 2); i
++) {
1997 data
= CODA_DOWN_ADDRESS_SET(i
) |
1998 CODA_DOWN_DATA_SET(p
[i
^ 1]);
1999 coda_write(dev
, data
, CODA_REG_BIT_CODE_DOWN
);
2002 for (i
= 0; i
< (CODA_ISRAM_SIZE
/ 2); i
++) {
2003 data
= CODA_DOWN_ADDRESS_SET(i
) |
2004 CODA_DOWN_DATA_SET(p
[round_down(i
, 4) +
2006 coda_write(dev
, data
, CODA_REG_BIT_CODE_DOWN
);
2010 /* Clear registers */
2011 for (i
= 0; i
< 64; i
++)
2012 coda_write(dev
, 0, CODA_REG_BIT_CODE_BUF_ADDR
+ i
* 4);
2014 /* Tell the BIT where to find everything it needs */
2015 if (dev
->devtype
->product
== CODA_960
||
2016 dev
->devtype
->product
== CODA_7541
) {
2017 coda_write(dev
, dev
->tempbuf
.paddr
,
2018 CODA_REG_BIT_TEMP_BUF_ADDR
);
2019 coda_write(dev
, 0, CODA_REG_BIT_BIT_STREAM_PARAM
);
2021 coda_write(dev
, dev
->workbuf
.paddr
,
2022 CODA_REG_BIT_WORK_BUF_ADDR
);
2024 coda_write(dev
, dev
->codebuf
.paddr
,
2025 CODA_REG_BIT_CODE_BUF_ADDR
);
2026 coda_write(dev
, 0, CODA_REG_BIT_CODE_RUN
);
2028 /* Set default values */
2029 switch (dev
->devtype
->product
) {
2031 coda_write(dev
, CODADX6_STREAM_BUF_PIC_FLUSH
,
2032 CODA_REG_BIT_STREAM_CTRL
);
2035 coda_write(dev
, CODA7_STREAM_BUF_PIC_FLUSH
,
2036 CODA_REG_BIT_STREAM_CTRL
);
2038 if (dev
->devtype
->product
== CODA_960
)
2039 coda_write(dev
, 1 << 12, CODA_REG_BIT_FRAME_MEM_CTRL
);
2041 coda_write(dev
, 0, CODA_REG_BIT_FRAME_MEM_CTRL
);
2043 if (dev
->devtype
->product
!= CODA_DX6
)
2044 coda_write(dev
, 0, CODA7_REG_BIT_AXI_SRAM_USE
);
2046 coda_write(dev
, CODA_INT_INTERRUPT_ENABLE
,
2047 CODA_REG_BIT_INT_ENABLE
);
2049 /* Reset VPU and start processor */
2050 data
= coda_read(dev
, CODA_REG_BIT_CODE_RESET
);
2051 data
|= CODA_REG_RESET_ENABLE
;
2052 coda_write(dev
, data
, CODA_REG_BIT_CODE_RESET
);
2054 data
&= ~CODA_REG_RESET_ENABLE
;
2055 coda_write(dev
, data
, CODA_REG_BIT_CODE_RESET
);
2056 coda_write(dev
, CODA_REG_RUN_ENABLE
, CODA_REG_BIT_CODE_RUN
);
2058 clk_disable_unprepare(dev
->clk_ahb
);
2059 clk_disable_unprepare(dev
->clk_per
);
2064 clk_disable_unprepare(dev
->clk_per
);
2069 static int coda_register_device(struct coda_dev
*dev
, int i
)
2071 struct video_device
*vfd
= &dev
->vfd
[i
];
2073 if (i
>= dev
->devtype
->num_vdevs
)
2076 strlcpy(vfd
->name
, dev
->devtype
->vdevs
[i
]->name
, sizeof(vfd
->name
));
2077 vfd
->fops
= &coda_fops
;
2078 vfd
->ioctl_ops
= &coda_ioctl_ops
;
2079 vfd
->release
= video_device_release_empty
,
2080 vfd
->lock
= &dev
->dev_mutex
;
2081 vfd
->v4l2_dev
= &dev
->v4l2_dev
;
2082 vfd
->vfl_dir
= VFL_DIR_M2M
;
2083 video_set_drvdata(vfd
, dev
);
2085 /* Not applicable, use the selection API instead */
2086 v4l2_disable_ioctl(vfd
, VIDIOC_CROPCAP
);
2087 v4l2_disable_ioctl(vfd
, VIDIOC_G_CROP
);
2088 v4l2_disable_ioctl(vfd
, VIDIOC_S_CROP
);
2090 return video_register_device(vfd
, VFL_TYPE_GRABBER
, 0);
2093 static void coda_copy_firmware(struct coda_dev
*dev
, const u8
* const buf
,
2096 u32
*src
= (u32
*)buf
;
2098 /* Check if the firmware has a 16-byte Freescale header, skip it */
2099 if (buf
[0] == 'M' && buf
[1] == 'X')
2102 * Check whether the firmware is in native order or pre-reordered for
2103 * memory access. The first instruction opcode always is 0xe40e.
2105 if (__le16_to_cpup((__le16
*)src
) == 0xe40e) {
2106 u32
*dst
= dev
->codebuf
.vaddr
;
2109 /* Firmware in native order, reorder while copying */
2110 if (dev
->devtype
->product
== CODA_DX6
) {
2111 for (i
= 0; i
< (size
- 16) / 4; i
++)
2112 dst
[i
] = (src
[i
] << 16) | (src
[i
] >> 16);
2114 for (i
= 0; i
< (size
- 16) / 4; i
+= 2) {
2115 dst
[i
] = (src
[i
+ 1] << 16) | (src
[i
+ 1] >> 16);
2116 dst
[i
+ 1] = (src
[i
] << 16) | (src
[i
] >> 16);
2120 /* Copy the already reordered firmware image */
2121 memcpy(dev
->codebuf
.vaddr
, src
, size
);
2125 static void coda_fw_callback(const struct firmware
*fw
, void *context
);
2127 static int coda_firmware_request(struct coda_dev
*dev
)
2131 if (dev
->firmware
>= ARRAY_SIZE(dev
->devtype
->firmware
))
2134 fw
= dev
->devtype
->firmware
[dev
->firmware
];
2136 dev_dbg(&dev
->plat_dev
->dev
, "requesting firmware '%s' for %s\n", fw
,
2137 coda_product_name(dev
->devtype
->product
));
2139 return request_firmware_nowait(THIS_MODULE
, true, fw
,
2140 &dev
->plat_dev
->dev
, GFP_KERNEL
, dev
,
2144 static void coda_fw_callback(const struct firmware
*fw
, void *context
)
2146 struct coda_dev
*dev
= context
;
2147 struct platform_device
*pdev
= dev
->plat_dev
;
2152 ret
= coda_firmware_request(dev
);
2154 v4l2_err(&dev
->v4l2_dev
, "firmware request failed\n");
2159 if (dev
->firmware
> 0) {
2161 * Since we can't suppress warnings for failed asynchronous
2162 * firmware requests, report that the fallback firmware was
2165 dev_info(&pdev
->dev
, "Using fallback firmware %s\n",
2166 dev
->devtype
->firmware
[dev
->firmware
]);
2169 /* allocate auxiliary per-device code buffer for the BIT processor */
2170 ret
= coda_alloc_aux_buf(dev
, &dev
->codebuf
, fw
->size
, "codebuf",
2175 coda_copy_firmware(dev
, fw
->data
, fw
->size
);
2176 release_firmware(fw
);
2178 ret
= coda_hw_init(dev
);
2180 v4l2_err(&dev
->v4l2_dev
, "HW initialization failed\n");
2184 ret
= coda_check_firmware(dev
);
2188 dev
->m2m_dev
= v4l2_m2m_init(&coda_m2m_ops
);
2189 if (IS_ERR(dev
->m2m_dev
)) {
2190 v4l2_err(&dev
->v4l2_dev
, "Failed to init mem2mem device\n");
2194 for (i
= 0; i
< dev
->devtype
->num_vdevs
; i
++) {
2195 ret
= coda_register_device(dev
, i
);
2197 v4l2_err(&dev
->v4l2_dev
,
2198 "Failed to register %s video device: %d\n",
2199 dev
->devtype
->vdevs
[i
]->name
, ret
);
2204 v4l2_info(&dev
->v4l2_dev
, "codec registered as /dev/video[%d-%d]\n",
2205 dev
->vfd
[0].num
, dev
->vfd
[i
- 1].num
);
2207 pm_runtime_put_sync(&pdev
->dev
);
2212 video_unregister_device(&dev
->vfd
[i
]);
2213 v4l2_m2m_release(dev
->m2m_dev
);
2215 pm_runtime_put_sync(&pdev
->dev
);
2218 enum coda_platform
{
2225 static const struct coda_devtype coda_devdata
[] = {
2228 "vpu_fw_imx27_TO2.bin",
2229 "vpu/vpu_fw_imx27_TO2.bin",
2230 "v4l-codadx6-imx27.bin"
2232 .product
= CODA_DX6
,
2233 .codecs
= codadx6_codecs
,
2234 .num_codecs
= ARRAY_SIZE(codadx6_codecs
),
2235 .vdevs
= codadx6_video_devices
,
2236 .num_vdevs
= ARRAY_SIZE(codadx6_video_devices
),
2237 .workbuf_size
= 288 * 1024 + FMO_SLICE_SAVE_BUF_SIZE
* 8 * 1024,
2238 .iram_size
= 0xb000,
2243 "vpu/vpu_fw_imx53.bin",
2244 "v4l-coda7541-imx53.bin"
2246 .product
= CODA_7541
,
2247 .codecs
= coda7_codecs
,
2248 .num_codecs
= ARRAY_SIZE(coda7_codecs
),
2249 .vdevs
= coda7_video_devices
,
2250 .num_vdevs
= ARRAY_SIZE(coda7_video_devices
),
2251 .workbuf_size
= 128 * 1024,
2252 .tempbuf_size
= 304 * 1024,
2253 .iram_size
= 0x14000,
2258 "vpu/vpu_fw_imx6q.bin",
2259 "v4l-coda960-imx6q.bin"
2261 .product
= CODA_960
,
2262 .codecs
= coda9_codecs
,
2263 .num_codecs
= ARRAY_SIZE(coda9_codecs
),
2264 .vdevs
= coda9_video_devices
,
2265 .num_vdevs
= ARRAY_SIZE(coda9_video_devices
),
2266 .workbuf_size
= 80 * 1024,
2267 .tempbuf_size
= 204 * 1024,
2268 .iram_size
= 0x21000,
2273 "vpu/vpu_fw_imx6d.bin",
2274 "v4l-coda960-imx6dl.bin"
2276 .product
= CODA_960
,
2277 .codecs
= coda9_codecs
,
2278 .num_codecs
= ARRAY_SIZE(coda9_codecs
),
2279 .vdevs
= coda9_video_devices
,
2280 .num_vdevs
= ARRAY_SIZE(coda9_video_devices
),
2281 .workbuf_size
= 80 * 1024,
2282 .tempbuf_size
= 204 * 1024,
2283 .iram_size
= 0x20000,
2287 static struct platform_device_id coda_platform_ids
[] = {
2288 { .name
= "coda-imx27", .driver_data
= CODA_IMX27
},
2291 MODULE_DEVICE_TABLE(platform
, coda_platform_ids
);
2294 static const struct of_device_id coda_dt_ids
[] = {
2295 { .compatible
= "fsl,imx27-vpu", .data
= &coda_devdata
[CODA_IMX27
] },
2296 { .compatible
= "fsl,imx53-vpu", .data
= &coda_devdata
[CODA_IMX53
] },
2297 { .compatible
= "fsl,imx6q-vpu", .data
= &coda_devdata
[CODA_IMX6Q
] },
2298 { .compatible
= "fsl,imx6dl-vpu", .data
= &coda_devdata
[CODA_IMX6DL
] },
2301 MODULE_DEVICE_TABLE(of
, coda_dt_ids
);
2304 static int coda_probe(struct platform_device
*pdev
)
2306 const struct of_device_id
*of_id
=
2307 of_match_device(of_match_ptr(coda_dt_ids
), &pdev
->dev
);
2308 const struct platform_device_id
*pdev_id
;
2309 struct coda_platform_data
*pdata
= pdev
->dev
.platform_data
;
2310 struct device_node
*np
= pdev
->dev
.of_node
;
2311 struct gen_pool
*pool
;
2312 struct coda_dev
*dev
;
2313 struct resource
*res
;
2316 dev
= devm_kzalloc(&pdev
->dev
, sizeof(*dev
), GFP_KERNEL
);
2320 pdev_id
= of_id
? of_id
->data
: platform_get_device_id(pdev
);
2323 dev
->devtype
= of_id
->data
;
2325 dev
->devtype
= &coda_devdata
[pdev_id
->driver_data
];
2329 spin_lock_init(&dev
->irqlock
);
2330 INIT_LIST_HEAD(&dev
->instances
);
2332 dev
->plat_dev
= pdev
;
2333 dev
->clk_per
= devm_clk_get(&pdev
->dev
, "per");
2334 if (IS_ERR(dev
->clk_per
)) {
2335 dev_err(&pdev
->dev
, "Could not get per clock\n");
2336 return PTR_ERR(dev
->clk_per
);
2339 dev
->clk_ahb
= devm_clk_get(&pdev
->dev
, "ahb");
2340 if (IS_ERR(dev
->clk_ahb
)) {
2341 dev_err(&pdev
->dev
, "Could not get ahb clock\n");
2342 return PTR_ERR(dev
->clk_ahb
);
2345 /* Get memory for physical registers */
2346 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2347 dev
->regs_base
= devm_ioremap_resource(&pdev
->dev
, res
);
2348 if (IS_ERR(dev
->regs_base
))
2349 return PTR_ERR(dev
->regs_base
);
2352 irq
= platform_get_irq_byname(pdev
, "bit");
2354 irq
= platform_get_irq(pdev
, 0);
2356 dev_err(&pdev
->dev
, "failed to get irq resource\n");
2360 ret
= devm_request_threaded_irq(&pdev
->dev
, irq
, NULL
, coda_irq_handler
,
2361 IRQF_ONESHOT
, dev_name(&pdev
->dev
), dev
);
2363 dev_err(&pdev
->dev
, "failed to request irq: %d\n", ret
);
2367 dev
->rstc
= devm_reset_control_get_optional(&pdev
->dev
, NULL
);
2368 if (IS_ERR(dev
->rstc
)) {
2369 ret
= PTR_ERR(dev
->rstc
);
2370 if (ret
== -ENOENT
|| ret
== -ENOTSUPP
) {
2373 dev_err(&pdev
->dev
, "failed get reset control: %d\n",
2379 /* Get IRAM pool from device tree or platform data */
2380 pool
= of_gen_pool_get(np
, "iram", 0);
2382 pool
= gen_pool_get(pdata
->iram_dev
, NULL
);
2384 dev_err(&pdev
->dev
, "iram pool not available\n");
2387 dev
->iram_pool
= pool
;
2389 /* Get vdoa_data if supported by the platform */
2390 dev
->vdoa
= coda_get_vdoa_data();
2391 if (PTR_ERR(dev
->vdoa
) == -EPROBE_DEFER
)
2392 return -EPROBE_DEFER
;
2394 ret
= v4l2_device_register(&pdev
->dev
, &dev
->v4l2_dev
);
2398 mutex_init(&dev
->dev_mutex
);
2399 mutex_init(&dev
->coda_mutex
);
2401 dev
->debugfs_root
= debugfs_create_dir("coda", NULL
);
2402 if (!dev
->debugfs_root
)
2403 dev_warn(&pdev
->dev
, "failed to create debugfs root\n");
2405 /* allocate auxiliary per-device buffers for the BIT processor */
2406 if (dev
->devtype
->product
== CODA_DX6
) {
2407 ret
= coda_alloc_aux_buf(dev
, &dev
->workbuf
,
2408 dev
->devtype
->workbuf_size
, "workbuf",
2411 goto err_v4l2_register
;
2414 if (dev
->devtype
->tempbuf_size
) {
2415 ret
= coda_alloc_aux_buf(dev
, &dev
->tempbuf
,
2416 dev
->devtype
->tempbuf_size
, "tempbuf",
2419 goto err_v4l2_register
;
2422 dev
->iram
.size
= dev
->devtype
->iram_size
;
2423 dev
->iram
.vaddr
= gen_pool_dma_alloc(dev
->iram_pool
, dev
->iram
.size
,
2425 if (!dev
->iram
.vaddr
) {
2426 dev_warn(&pdev
->dev
, "unable to alloc iram\n");
2428 memset(dev
->iram
.vaddr
, 0, dev
->iram
.size
);
2429 dev
->iram
.blob
.data
= dev
->iram
.vaddr
;
2430 dev
->iram
.blob
.size
= dev
->iram
.size
;
2431 dev
->iram
.dentry
= debugfs_create_blob("iram", 0644,
2436 dev
->workqueue
= alloc_workqueue("coda", WQ_UNBOUND
| WQ_MEM_RECLAIM
, 1);
2437 if (!dev
->workqueue
) {
2438 dev_err(&pdev
->dev
, "unable to alloc workqueue\n");
2440 goto err_v4l2_register
;
2443 platform_set_drvdata(pdev
, dev
);
2446 * Start activated so we can directly call coda_hw_init in
2447 * coda_fw_callback regardless of whether CONFIG_PM is
2448 * enabled or whether the device is associated with a PM domain.
2450 pm_runtime_get_noresume(&pdev
->dev
);
2451 pm_runtime_set_active(&pdev
->dev
);
2452 pm_runtime_enable(&pdev
->dev
);
2454 ret
= coda_firmware_request(dev
);
2456 goto err_alloc_workqueue
;
2459 err_alloc_workqueue
:
2460 destroy_workqueue(dev
->workqueue
);
2462 v4l2_device_unregister(&dev
->v4l2_dev
);
2466 static int coda_remove(struct platform_device
*pdev
)
2468 struct coda_dev
*dev
= platform_get_drvdata(pdev
);
2471 for (i
= 0; i
< ARRAY_SIZE(dev
->vfd
); i
++) {
2472 if (video_get_drvdata(&dev
->vfd
[i
]))
2473 video_unregister_device(&dev
->vfd
[i
]);
2476 v4l2_m2m_release(dev
->m2m_dev
);
2477 pm_runtime_disable(&pdev
->dev
);
2478 v4l2_device_unregister(&dev
->v4l2_dev
);
2479 destroy_workqueue(dev
->workqueue
);
2480 if (dev
->iram
.vaddr
)
2481 gen_pool_free(dev
->iram_pool
, (unsigned long)dev
->iram
.vaddr
,
2483 coda_free_aux_buf(dev
, &dev
->codebuf
);
2484 coda_free_aux_buf(dev
, &dev
->tempbuf
);
2485 coda_free_aux_buf(dev
, &dev
->workbuf
);
2486 debugfs_remove_recursive(dev
->debugfs_root
);
2491 static int coda_runtime_resume(struct device
*dev
)
2493 struct coda_dev
*cdev
= dev_get_drvdata(dev
);
2496 if (dev
->pm_domain
&& cdev
->codebuf
.vaddr
) {
2497 ret
= coda_hw_init(cdev
);
2499 v4l2_err(&cdev
->v4l2_dev
, "HW initialization failed\n");
2506 static const struct dev_pm_ops coda_pm_ops
= {
2507 SET_RUNTIME_PM_OPS(NULL
, coda_runtime_resume
, NULL
)
2510 static struct platform_driver coda_driver
= {
2511 .probe
= coda_probe
,
2512 .remove
= coda_remove
,
2515 .of_match_table
= of_match_ptr(coda_dt_ids
),
2518 .id_table
= coda_platform_ids
,
2521 module_platform_driver(coda_driver
);
2523 MODULE_LICENSE("GPL");
2524 MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
2525 MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");