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/genalloc.h>
19 #include <linux/interrupt.h>
21 #include <linux/irq.h>
22 #include <linux/kfifo.h>
23 #include <linux/module.h>
24 #include <linux/of_device.h>
25 #include <linux/platform_device.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/slab.h>
28 #include <linux/videodev2.h>
30 #include <linux/platform_data/coda.h>
31 #include <linux/reset.h>
33 #include <media/v4l2-ctrls.h>
34 #include <media/v4l2-device.h>
35 #include <media/v4l2-event.h>
36 #include <media/v4l2-ioctl.h>
37 #include <media/v4l2-mem2mem.h>
38 #include <media/videobuf2-core.h>
39 #include <media/videobuf2-dma-contig.h>
40 #include <media/videobuf2-vmalloc.h>
44 #define CODA_NAME "coda"
46 #define CODADX6_MAX_INSTANCES 4
47 #define CODA_MAX_FORMATS 4
49 #define CODA_PARA_BUF_SIZE (10 * 1024)
50 #define CODA_ISRAM_SIZE (2048 * 2)
55 #define S_ALIGN 1 /* multiple of 2 */
56 #define W_ALIGN 1 /* multiple of 2 */
57 #define H_ALIGN 1 /* multiple of 2 */
59 #define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh)
62 module_param(coda_debug
, int, 0644);
63 MODULE_PARM_DESC(coda_debug
, "Debug level (0-2)");
70 void coda_write(struct coda_dev
*dev
, u32 data
, u32 reg
)
72 v4l2_dbg(2, coda_debug
, &dev
->v4l2_dev
,
73 "%s: data=0x%x, reg=0x%x\n", __func__
, data
, reg
);
74 writel(data
, dev
->regs_base
+ reg
);
77 unsigned int coda_read(struct coda_dev
*dev
, u32 reg
)
81 data
= readl(dev
->regs_base
+ reg
);
82 v4l2_dbg(2, coda_debug
, &dev
->v4l2_dev
,
83 "%s: data=0x%x, reg=0x%x\n", __func__
, data
, reg
);
87 void coda_write_base(struct coda_ctx
*ctx
, struct coda_q_data
*q_data
,
88 struct vb2_buffer
*buf
, unsigned int reg_y
)
90 u32 base_y
= vb2_dma_contig_plane_dma_addr(buf
, 0);
93 switch (q_data
->fourcc
) {
94 case V4L2_PIX_FMT_YVU420
:
95 /* Switch Cb and Cr for YVU420 format */
96 base_cr
= base_y
+ q_data
->bytesperline
* q_data
->height
;
97 base_cb
= base_cr
+ q_data
->bytesperline
* q_data
->height
/ 4;
99 case V4L2_PIX_FMT_YUV420
:
100 case V4L2_PIX_FMT_NV12
:
102 base_cb
= base_y
+ q_data
->bytesperline
* q_data
->height
;
103 base_cr
= base_cb
+ q_data
->bytesperline
* q_data
->height
/ 4;
105 case V4L2_PIX_FMT_YUV422P
:
106 base_cb
= base_y
+ q_data
->bytesperline
* q_data
->height
;
107 base_cr
= base_cb
+ q_data
->bytesperline
* q_data
->height
/ 2;
110 coda_write(ctx
->dev
, base_y
, reg_y
);
111 coda_write(ctx
->dev
, base_cb
, reg_y
+ 4);
112 coda_write(ctx
->dev
, base_cr
, reg_y
+ 8);
116 * Array of all formats supported by any version of Coda:
118 static const struct coda_fmt coda_formats
[] = {
120 .name
= "YUV 4:2:0 Planar, YCbCr",
121 .fourcc
= V4L2_PIX_FMT_YUV420
,
124 .name
= "YUV 4:2:0 Planar, YCrCb",
125 .fourcc
= V4L2_PIX_FMT_YVU420
,
128 .name
= "YUV 4:2:0 Partial interleaved Y/CbCr",
129 .fourcc
= V4L2_PIX_FMT_NV12
,
132 .name
= "YUV 4:2:2 Planar, YCbCr",
133 .fourcc
= V4L2_PIX_FMT_YUV422P
,
136 .name
= "H264 Encoded Stream",
137 .fourcc
= V4L2_PIX_FMT_H264
,
140 .name
= "MPEG4 Encoded Stream",
141 .fourcc
= V4L2_PIX_FMT_MPEG4
,
144 .name
= "JPEG Encoded Images",
145 .fourcc
= V4L2_PIX_FMT_JPEG
,
149 #define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
150 { mode, src_fourcc, dst_fourcc, max_w, max_h }
153 * Arrays of codecs supported by each given version of Coda:
157 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
159 static const struct coda_codec codadx6_codecs
[] = {
160 CODA_CODEC(CODADX6_MODE_ENCODE_H264
, V4L2_PIX_FMT_YUV420
, V4L2_PIX_FMT_H264
, 720, 576),
161 CODA_CODEC(CODADX6_MODE_ENCODE_MP4
, V4L2_PIX_FMT_YUV420
, V4L2_PIX_FMT_MPEG4
, 720, 576),
164 static const struct coda_codec coda7_codecs
[] = {
165 CODA_CODEC(CODA7_MODE_ENCODE_H264
, V4L2_PIX_FMT_YUV420
, V4L2_PIX_FMT_H264
, 1280, 720),
166 CODA_CODEC(CODA7_MODE_ENCODE_MP4
, V4L2_PIX_FMT_YUV420
, V4L2_PIX_FMT_MPEG4
, 1280, 720),
167 CODA_CODEC(CODA7_MODE_ENCODE_MJPG
, V4L2_PIX_FMT_YUV420
, V4L2_PIX_FMT_JPEG
, 8192, 8192),
168 CODA_CODEC(CODA7_MODE_DECODE_H264
, V4L2_PIX_FMT_H264
, V4L2_PIX_FMT_YUV420
, 1920, 1088),
169 CODA_CODEC(CODA7_MODE_DECODE_MP4
, V4L2_PIX_FMT_MPEG4
, V4L2_PIX_FMT_YUV420
, 1920, 1088),
170 CODA_CODEC(CODA7_MODE_DECODE_MJPG
, V4L2_PIX_FMT_JPEG
, V4L2_PIX_FMT_YUV420
, 8192, 8192),
173 static const struct coda_codec coda9_codecs
[] = {
174 CODA_CODEC(CODA9_MODE_ENCODE_H264
, V4L2_PIX_FMT_YUV420
, V4L2_PIX_FMT_H264
, 1920, 1088),
175 CODA_CODEC(CODA9_MODE_ENCODE_MP4
, V4L2_PIX_FMT_YUV420
, V4L2_PIX_FMT_MPEG4
, 1920, 1088),
176 CODA_CODEC(CODA9_MODE_DECODE_H264
, V4L2_PIX_FMT_H264
, V4L2_PIX_FMT_YUV420
, 1920, 1088),
177 CODA_CODEC(CODA9_MODE_DECODE_MP4
, V4L2_PIX_FMT_MPEG4
, V4L2_PIX_FMT_YUV420
, 1920, 1088),
180 struct coda_video_device
{
182 enum coda_inst_type type
;
183 const struct coda_context_ops
*ops
;
185 u32 src_formats
[CODA_MAX_FORMATS
];
186 u32 dst_formats
[CODA_MAX_FORMATS
];
189 static const struct coda_video_device coda_bit_encoder
= {
190 .name
= "coda-encoder",
191 .type
= CODA_INST_ENCODER
,
192 .ops
= &coda_bit_encode_ops
,
204 static const struct coda_video_device coda_bit_jpeg_encoder
= {
205 .name
= "coda-jpeg-encoder",
206 .type
= CODA_INST_ENCODER
,
207 .ops
= &coda_bit_encode_ops
,
212 V4L2_PIX_FMT_YUV422P
,
219 static const struct coda_video_device coda_bit_decoder
= {
220 .name
= "coda-decoder",
221 .type
= CODA_INST_DECODER
,
222 .ops
= &coda_bit_decode_ops
,
234 static const struct coda_video_device coda_bit_jpeg_decoder
= {
235 .name
= "coda-jpeg-decoder",
236 .type
= CODA_INST_DECODER
,
237 .ops
= &coda_bit_decode_ops
,
245 V4L2_PIX_FMT_YUV422P
,
249 static const struct coda_video_device
*codadx6_video_devices
[] = {
253 static const struct coda_video_device
*coda7_video_devices
[] = {
254 &coda_bit_jpeg_encoder
,
255 &coda_bit_jpeg_decoder
,
260 static const struct coda_video_device
*coda9_video_devices
[] = {
265 static bool coda_format_is_yuv(u32 fourcc
)
268 case V4L2_PIX_FMT_YUV420
:
269 case V4L2_PIX_FMT_YVU420
:
270 case V4L2_PIX_FMT_NV12
:
271 case V4L2_PIX_FMT_YUV422P
:
278 static const char *coda_format_name(u32 fourcc
)
282 for (i
= 0; i
< ARRAY_SIZE(coda_formats
); i
++) {
283 if (coda_formats
[i
].fourcc
== fourcc
)
284 return coda_formats
[i
].name
;
291 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
294 static u32
coda_format_normalize_yuv(u32 fourcc
)
296 return coda_format_is_yuv(fourcc
) ? V4L2_PIX_FMT_YUV420
: fourcc
;
299 static const struct coda_codec
*coda_find_codec(struct coda_dev
*dev
,
300 int src_fourcc
, int dst_fourcc
)
302 const struct coda_codec
*codecs
= dev
->devtype
->codecs
;
303 int num_codecs
= dev
->devtype
->num_codecs
;
306 src_fourcc
= coda_format_normalize_yuv(src_fourcc
);
307 dst_fourcc
= coda_format_normalize_yuv(dst_fourcc
);
308 if (src_fourcc
== dst_fourcc
)
311 for (k
= 0; k
< num_codecs
; k
++) {
312 if (codecs
[k
].src_fourcc
== src_fourcc
&&
313 codecs
[k
].dst_fourcc
== dst_fourcc
)
323 static void coda_get_max_dimensions(struct coda_dev
*dev
,
324 const struct coda_codec
*codec
,
325 int *max_w
, int *max_h
)
327 const struct coda_codec
*codecs
= dev
->devtype
->codecs
;
328 int num_codecs
= dev
->devtype
->num_codecs
;
336 for (k
= 0, w
= 0, h
= 0; k
< num_codecs
; k
++) {
337 w
= max(w
, codecs
[k
].max_w
);
338 h
= max(h
, codecs
[k
].max_h
);
348 const struct coda_video_device
*to_coda_video_device(struct video_device
*vdev
)
350 struct coda_dev
*dev
= video_get_drvdata(vdev
);
351 unsigned int i
= vdev
- dev
->vfd
;
353 if (i
>= dev
->devtype
->num_vdevs
)
356 return dev
->devtype
->vdevs
[i
];
359 const char *coda_product_name(int product
)
371 snprintf(buf
, sizeof(buf
), "(0x%04x)", product
);
377 * V4L2 ioctl() operations.
379 static int coda_querycap(struct file
*file
, void *priv
,
380 struct v4l2_capability
*cap
)
382 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
384 strlcpy(cap
->driver
, CODA_NAME
, sizeof(cap
->driver
));
385 strlcpy(cap
->card
, coda_product_name(ctx
->dev
->devtype
->product
),
387 strlcpy(cap
->bus_info
, "platform:" CODA_NAME
, sizeof(cap
->bus_info
));
388 cap
->device_caps
= V4L2_CAP_VIDEO_M2M
| V4L2_CAP_STREAMING
;
389 cap
->capabilities
= cap
->device_caps
| V4L2_CAP_DEVICE_CAPS
;
394 static int coda_enum_fmt(struct file
*file
, void *priv
,
395 struct v4l2_fmtdesc
*f
)
397 struct video_device
*vdev
= video_devdata(file
);
398 const struct coda_video_device
*cvd
= to_coda_video_device(vdev
);
402 if (f
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
)
403 formats
= cvd
->src_formats
;
404 else if (f
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
405 formats
= cvd
->dst_formats
;
409 if (f
->index
>= CODA_MAX_FORMATS
|| formats
[f
->index
] == 0)
412 name
= coda_format_name(formats
[f
->index
]);
413 strlcpy(f
->description
, name
, sizeof(f
->description
));
414 f
->pixelformat
= formats
[f
->index
];
415 if (!coda_format_is_yuv(formats
[f
->index
]))
416 f
->flags
|= V4L2_FMT_FLAG_COMPRESSED
;
421 static int coda_g_fmt(struct file
*file
, void *priv
,
422 struct v4l2_format
*f
)
424 struct coda_q_data
*q_data
;
425 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
427 q_data
= get_q_data(ctx
, f
->type
);
431 f
->fmt
.pix
.field
= V4L2_FIELD_NONE
;
432 f
->fmt
.pix
.pixelformat
= q_data
->fourcc
;
433 f
->fmt
.pix
.width
= q_data
->width
;
434 f
->fmt
.pix
.height
= q_data
->height
;
435 f
->fmt
.pix
.bytesperline
= q_data
->bytesperline
;
437 f
->fmt
.pix
.sizeimage
= q_data
->sizeimage
;
438 if (f
->fmt
.pix
.pixelformat
== V4L2_PIX_FMT_JPEG
)
439 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_JPEG
;
441 f
->fmt
.pix
.colorspace
= ctx
->colorspace
;
446 static int coda_try_pixelformat(struct coda_ctx
*ctx
, struct v4l2_format
*f
)
448 struct coda_q_data
*q_data
;
452 if (f
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
)
453 formats
= ctx
->cvd
->src_formats
;
454 else if (f
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
455 formats
= ctx
->cvd
->dst_formats
;
459 for (i
= 0; i
< CODA_MAX_FORMATS
; i
++) {
460 if (formats
[i
] == f
->fmt
.pix
.pixelformat
) {
461 f
->fmt
.pix
.pixelformat
= formats
[i
];
466 /* Fall back to currently set pixelformat */
467 q_data
= get_q_data(ctx
, f
->type
);
468 f
->fmt
.pix
.pixelformat
= q_data
->fourcc
;
473 static unsigned int coda_estimate_sizeimage(struct coda_ctx
*ctx
, u32 sizeimage
,
474 u32 width
, u32 height
)
477 * This is a rough estimate for sensible compressed buffer
478 * sizes (between 1 and 16 bits per pixel). This could be
479 * improved by better format specific worst case estimates.
481 return round_up(clamp(sizeimage
, width
* height
/ 8,
482 width
* height
* 2), PAGE_SIZE
);
485 static int coda_try_fmt(struct coda_ctx
*ctx
, const struct coda_codec
*codec
,
486 struct v4l2_format
*f
)
488 struct coda_dev
*dev
= ctx
->dev
;
489 unsigned int max_w
, max_h
;
490 enum v4l2_field field
;
492 field
= f
->fmt
.pix
.field
;
493 if (field
== V4L2_FIELD_ANY
)
494 field
= V4L2_FIELD_NONE
;
495 else if (V4L2_FIELD_NONE
!= field
)
498 /* V4L2 specification suggests the driver corrects the format struct
499 * if any of the dimensions is unsupported */
500 f
->fmt
.pix
.field
= field
;
502 coda_get_max_dimensions(dev
, codec
, &max_w
, &max_h
);
503 v4l_bound_align_image(&f
->fmt
.pix
.width
, MIN_W
, max_w
, W_ALIGN
,
504 &f
->fmt
.pix
.height
, MIN_H
, max_h
, H_ALIGN
,
507 switch (f
->fmt
.pix
.pixelformat
) {
508 case V4L2_PIX_FMT_YUV420
:
509 case V4L2_PIX_FMT_YVU420
:
510 case V4L2_PIX_FMT_NV12
:
512 * Frame stride must be at least multiple of 8,
513 * but multiple of 16 for h.264 or JPEG 4:2:x
515 f
->fmt
.pix
.bytesperline
= round_up(f
->fmt
.pix
.width
, 16);
516 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.bytesperline
*
517 f
->fmt
.pix
.height
* 3 / 2;
519 case V4L2_PIX_FMT_YUV422P
:
520 f
->fmt
.pix
.bytesperline
= round_up(f
->fmt
.pix
.width
, 16);
521 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.bytesperline
*
522 f
->fmt
.pix
.height
* 2;
524 case V4L2_PIX_FMT_JPEG
:
525 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_JPEG
;
527 case V4L2_PIX_FMT_H264
:
528 case V4L2_PIX_FMT_MPEG4
:
529 f
->fmt
.pix
.bytesperline
= 0;
530 f
->fmt
.pix
.sizeimage
= coda_estimate_sizeimage(ctx
,
531 f
->fmt
.pix
.sizeimage
,
542 static int coda_try_fmt_vid_cap(struct file
*file
, void *priv
,
543 struct v4l2_format
*f
)
545 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
546 const struct coda_q_data
*q_data_src
;
547 const struct coda_codec
*codec
;
548 struct vb2_queue
*src_vq
;
551 ret
= coda_try_pixelformat(ctx
, f
);
555 q_data_src
= get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_OUTPUT
);
558 * If the source format is already fixed, only allow the same output
561 src_vq
= v4l2_m2m_get_vq(ctx
->fh
.m2m_ctx
, V4L2_BUF_TYPE_VIDEO_OUTPUT
);
562 if (vb2_is_streaming(src_vq
)) {
563 f
->fmt
.pix
.width
= q_data_src
->width
;
564 f
->fmt
.pix
.height
= q_data_src
->height
;
567 f
->fmt
.pix
.colorspace
= ctx
->colorspace
;
569 q_data_src
= get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_OUTPUT
);
570 codec
= coda_find_codec(ctx
->dev
, q_data_src
->fourcc
,
571 f
->fmt
.pix
.pixelformat
);
575 ret
= coda_try_fmt(ctx
, codec
, f
);
579 /* The h.264 decoder only returns complete 16x16 macroblocks */
580 if (codec
&& codec
->src_fourcc
== V4L2_PIX_FMT_H264
) {
581 f
->fmt
.pix
.width
= f
->fmt
.pix
.width
;
582 f
->fmt
.pix
.height
= round_up(f
->fmt
.pix
.height
, 16);
583 f
->fmt
.pix
.bytesperline
= round_up(f
->fmt
.pix
.width
, 16);
584 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.bytesperline
*
585 f
->fmt
.pix
.height
* 3 / 2;
591 static int coda_try_fmt_vid_out(struct file
*file
, void *priv
,
592 struct v4l2_format
*f
)
594 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
595 struct coda_dev
*dev
= ctx
->dev
;
596 const struct coda_q_data
*q_data_dst
;
597 const struct coda_codec
*codec
;
600 ret
= coda_try_pixelformat(ctx
, f
);
604 switch (f
->fmt
.pix
.colorspace
) {
605 case V4L2_COLORSPACE_REC709
:
606 case V4L2_COLORSPACE_JPEG
:
609 if (f
->fmt
.pix
.pixelformat
== V4L2_PIX_FMT_JPEG
)
610 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_JPEG
;
612 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_REC709
;
615 q_data_dst
= get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_CAPTURE
);
616 codec
= coda_find_codec(dev
, f
->fmt
.pix
.pixelformat
, q_data_dst
->fourcc
);
618 return coda_try_fmt(ctx
, codec
, f
);
621 static int coda_s_fmt(struct coda_ctx
*ctx
, struct v4l2_format
*f
)
623 struct coda_q_data
*q_data
;
624 struct vb2_queue
*vq
;
626 vq
= v4l2_m2m_get_vq(ctx
->fh
.m2m_ctx
, f
->type
);
630 q_data
= get_q_data(ctx
, f
->type
);
634 if (vb2_is_busy(vq
)) {
635 v4l2_err(&ctx
->dev
->v4l2_dev
, "%s queue busy\n", __func__
);
639 q_data
->fourcc
= f
->fmt
.pix
.pixelformat
;
640 q_data
->width
= f
->fmt
.pix
.width
;
641 q_data
->height
= f
->fmt
.pix
.height
;
642 q_data
->bytesperline
= f
->fmt
.pix
.bytesperline
;
643 q_data
->sizeimage
= f
->fmt
.pix
.sizeimage
;
644 q_data
->rect
.left
= 0;
645 q_data
->rect
.top
= 0;
646 q_data
->rect
.width
= f
->fmt
.pix
.width
;
647 q_data
->rect
.height
= f
->fmt
.pix
.height
;
649 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
650 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
651 f
->type
, q_data
->width
, q_data
->height
, q_data
->fourcc
);
656 static int coda_s_fmt_vid_cap(struct file
*file
, void *priv
,
657 struct v4l2_format
*f
)
659 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
662 ret
= coda_try_fmt_vid_cap(file
, priv
, f
);
666 return coda_s_fmt(ctx
, f
);
669 static int coda_s_fmt_vid_out(struct file
*file
, void *priv
,
670 struct v4l2_format
*f
)
672 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
673 struct v4l2_format f_cap
;
676 ret
= coda_try_fmt_vid_out(file
, priv
, f
);
680 ret
= coda_s_fmt(ctx
, f
);
684 ctx
->colorspace
= f
->fmt
.pix
.colorspace
;
686 memset(&f_cap
, 0, sizeof(f_cap
));
687 f_cap
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
688 coda_g_fmt(file
, priv
, &f_cap
);
689 f_cap
.fmt
.pix
.width
= f
->fmt
.pix
.width
;
690 f_cap
.fmt
.pix
.height
= f
->fmt
.pix
.height
;
692 ret
= coda_try_fmt_vid_cap(file
, priv
, &f_cap
);
696 return coda_s_fmt(ctx
, &f_cap
);
699 static int coda_qbuf(struct file
*file
, void *priv
,
700 struct v4l2_buffer
*buf
)
702 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
704 return v4l2_m2m_qbuf(file
, ctx
->fh
.m2m_ctx
, buf
);
707 static bool coda_buf_is_end_of_stream(struct coda_ctx
*ctx
,
708 struct v4l2_buffer
*buf
)
710 struct vb2_queue
*src_vq
;
712 src_vq
= v4l2_m2m_get_vq(ctx
->fh
.m2m_ctx
, V4L2_BUF_TYPE_VIDEO_OUTPUT
);
714 return ((ctx
->bit_stream_param
& CODA_BIT_STREAM_END_FLAG
) &&
715 (buf
->sequence
== (ctx
->qsequence
- 1)));
718 static int coda_dqbuf(struct file
*file
, void *priv
,
719 struct v4l2_buffer
*buf
)
721 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
724 ret
= v4l2_m2m_dqbuf(file
, ctx
->fh
.m2m_ctx
, buf
);
726 /* If this is the last capture buffer, emit an end-of-stream event */
727 if (buf
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
728 coda_buf_is_end_of_stream(ctx
, buf
)) {
729 const struct v4l2_event eos_event
= {
730 .type
= V4L2_EVENT_EOS
733 v4l2_event_queue_fh(&ctx
->fh
, &eos_event
);
739 static int coda_g_selection(struct file
*file
, void *fh
,
740 struct v4l2_selection
*s
)
742 struct coda_ctx
*ctx
= fh_to_ctx(fh
);
743 struct coda_q_data
*q_data
;
744 struct v4l2_rect r
, *rsel
;
746 q_data
= get_q_data(ctx
, s
->type
);
752 r
.width
= q_data
->width
;
753 r
.height
= q_data
->height
;
754 rsel
= &q_data
->rect
;
757 case V4L2_SEL_TGT_CROP_DEFAULT
:
758 case V4L2_SEL_TGT_CROP_BOUNDS
:
761 case V4L2_SEL_TGT_CROP
:
762 if (s
->type
!= V4L2_BUF_TYPE_VIDEO_OUTPUT
)
765 case V4L2_SEL_TGT_COMPOSE_BOUNDS
:
766 case V4L2_SEL_TGT_COMPOSE_PADDED
:
769 case V4L2_SEL_TGT_COMPOSE
:
770 case V4L2_SEL_TGT_COMPOSE_DEFAULT
:
771 if (s
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
783 static int coda_try_decoder_cmd(struct file
*file
, void *fh
,
784 struct v4l2_decoder_cmd
*dc
)
786 if (dc
->cmd
!= V4L2_DEC_CMD_STOP
)
789 if (dc
->flags
& V4L2_DEC_CMD_STOP_TO_BLACK
)
792 if (!(dc
->flags
& V4L2_DEC_CMD_STOP_IMMEDIATELY
) && (dc
->stop
.pts
!= 0))
798 static int coda_decoder_cmd(struct file
*file
, void *fh
,
799 struct v4l2_decoder_cmd
*dc
)
801 struct coda_ctx
*ctx
= fh_to_ctx(fh
);
804 ret
= coda_try_decoder_cmd(file
, fh
, dc
);
808 /* Ignore decoder stop command silently in encoder context */
809 if (ctx
->inst_type
!= CODA_INST_DECODER
)
812 /* Set the stream-end flag on this context */
813 coda_bit_stream_end_flag(ctx
);
815 v4l2_m2m_try_schedule(ctx
->fh
.m2m_ctx
);
820 static int coda_subscribe_event(struct v4l2_fh
*fh
,
821 const struct v4l2_event_subscription
*sub
)
825 return v4l2_event_subscribe(fh
, sub
, 0, NULL
);
827 return v4l2_ctrl_subscribe_event(fh
, sub
);
831 static const struct v4l2_ioctl_ops coda_ioctl_ops
= {
832 .vidioc_querycap
= coda_querycap
,
834 .vidioc_enum_fmt_vid_cap
= coda_enum_fmt
,
835 .vidioc_g_fmt_vid_cap
= coda_g_fmt
,
836 .vidioc_try_fmt_vid_cap
= coda_try_fmt_vid_cap
,
837 .vidioc_s_fmt_vid_cap
= coda_s_fmt_vid_cap
,
839 .vidioc_enum_fmt_vid_out
= coda_enum_fmt
,
840 .vidioc_g_fmt_vid_out
= coda_g_fmt
,
841 .vidioc_try_fmt_vid_out
= coda_try_fmt_vid_out
,
842 .vidioc_s_fmt_vid_out
= coda_s_fmt_vid_out
,
844 .vidioc_reqbufs
= v4l2_m2m_ioctl_reqbufs
,
845 .vidioc_querybuf
= v4l2_m2m_ioctl_querybuf
,
847 .vidioc_qbuf
= coda_qbuf
,
848 .vidioc_expbuf
= v4l2_m2m_ioctl_expbuf
,
849 .vidioc_dqbuf
= coda_dqbuf
,
850 .vidioc_create_bufs
= v4l2_m2m_ioctl_create_bufs
,
852 .vidioc_streamon
= v4l2_m2m_ioctl_streamon
,
853 .vidioc_streamoff
= v4l2_m2m_ioctl_streamoff
,
855 .vidioc_g_selection
= coda_g_selection
,
857 .vidioc_try_decoder_cmd
= coda_try_decoder_cmd
,
858 .vidioc_decoder_cmd
= coda_decoder_cmd
,
860 .vidioc_subscribe_event
= coda_subscribe_event
,
861 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
864 void coda_set_gdi_regs(struct coda_ctx
*ctx
)
866 struct gdi_tiled_map
*tiled_map
= &ctx
->tiled_map
;
867 struct coda_dev
*dev
= ctx
->dev
;
870 for (i
= 0; i
< 16; i
++)
871 coda_write(dev
, tiled_map
->xy2ca_map
[i
],
872 CODA9_GDI_XY2_CAS_0
+ 4 * i
);
873 for (i
= 0; i
< 4; i
++)
874 coda_write(dev
, tiled_map
->xy2ba_map
[i
],
875 CODA9_GDI_XY2_BA_0
+ 4 * i
);
876 for (i
= 0; i
< 16; i
++)
877 coda_write(dev
, tiled_map
->xy2ra_map
[i
],
878 CODA9_GDI_XY2_RAS_0
+ 4 * i
);
879 coda_write(dev
, tiled_map
->xy2rbc_config
, CODA9_GDI_XY2_RBC_CONFIG
);
880 for (i
= 0; i
< 32; i
++)
881 coda_write(dev
, tiled_map
->rbc2axi_map
[i
],
882 CODA9_GDI_RBC2_AXI_0
+ 4 * i
);
886 * Mem-to-mem operations.
889 static void coda_device_run(void *m2m_priv
)
891 struct coda_ctx
*ctx
= m2m_priv
;
892 struct coda_dev
*dev
= ctx
->dev
;
894 queue_work(dev
->workqueue
, &ctx
->pic_run_work
);
897 static void coda_pic_run_work(struct work_struct
*work
)
899 struct coda_ctx
*ctx
= container_of(work
, struct coda_ctx
, pic_run_work
);
900 struct coda_dev
*dev
= ctx
->dev
;
903 mutex_lock(&ctx
->buffer_mutex
);
904 mutex_lock(&dev
->coda_mutex
);
906 ret
= ctx
->ops
->prepare_run(ctx
);
907 if (ret
< 0 && ctx
->inst_type
== CODA_INST_DECODER
) {
908 mutex_unlock(&dev
->coda_mutex
);
909 mutex_unlock(&ctx
->buffer_mutex
);
910 /* job_finish scheduled by prepare_decode */
914 if (!wait_for_completion_timeout(&ctx
->completion
,
915 msecs_to_jiffies(1000))) {
916 dev_err(&dev
->plat_dev
->dev
, "CODA PIC_RUN timeout\n");
921 } else if (!ctx
->aborting
) {
922 ctx
->ops
->finish_run(ctx
);
925 if ((ctx
->aborting
|| (!ctx
->streamon_cap
&& !ctx
->streamon_out
)) &&
926 ctx
->ops
->seq_end_work
)
927 queue_work(dev
->workqueue
, &ctx
->seq_end_work
);
929 mutex_unlock(&dev
->coda_mutex
);
930 mutex_unlock(&ctx
->buffer_mutex
);
932 v4l2_m2m_job_finish(ctx
->dev
->m2m_dev
, ctx
->fh
.m2m_ctx
);
935 static int coda_job_ready(void *m2m_priv
)
937 struct coda_ctx
*ctx
= m2m_priv
;
940 * For both 'P' and 'key' frame cases 1 picture
941 * and 1 frame are needed. In the decoder case,
942 * the compressed frame can be in the bitstream.
944 if (!v4l2_m2m_num_src_bufs_ready(ctx
->fh
.m2m_ctx
) &&
945 ctx
->inst_type
!= CODA_INST_DECODER
) {
946 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
947 "not ready: not enough video buffers.\n");
951 if (!v4l2_m2m_num_dst_bufs_ready(ctx
->fh
.m2m_ctx
)) {
952 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
953 "not ready: not enough video capture buffers.\n");
957 if (ctx
->inst_type
== CODA_INST_DECODER
&& ctx
->use_bit
) {
958 struct list_head
*meta
;
963 if (ctx
->hold
&& !v4l2_m2m_num_src_bufs_ready(ctx
->fh
.m2m_ctx
)) {
964 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
965 "%d: not ready: on hold for more buffers.\n",
970 stream_end
= ctx
->bit_stream_param
&
971 CODA_BIT_STREAM_END_FLAG
;
974 list_for_each(meta
, &ctx
->buffer_meta_list
)
977 src_bufs
= v4l2_m2m_num_src_bufs_ready(ctx
->fh
.m2m_ctx
);
979 if (!stream_end
&& (num_metas
+ src_bufs
) < 2) {
980 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
981 "%d: not ready: need 2 buffers available (%d, %d)\n",
982 ctx
->idx
, num_metas
, src_bufs
);
987 if (!v4l2_m2m_num_src_bufs_ready(ctx
->fh
.m2m_ctx
) &&
988 !stream_end
&& (coda_get_bitstream_payload(ctx
) < 512)) {
989 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
990 "%d: not ready: not enough bitstream data (%d).\n",
991 ctx
->idx
, coda_get_bitstream_payload(ctx
));
997 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
998 "not ready: aborting\n");
1002 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
1007 static void coda_job_abort(void *priv
)
1009 struct coda_ctx
*ctx
= priv
;
1013 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
1017 static void coda_lock(void *m2m_priv
)
1019 struct coda_ctx
*ctx
= m2m_priv
;
1020 struct coda_dev
*pcdev
= ctx
->dev
;
1022 mutex_lock(&pcdev
->dev_mutex
);
1025 static void coda_unlock(void *m2m_priv
)
1027 struct coda_ctx
*ctx
= m2m_priv
;
1028 struct coda_dev
*pcdev
= ctx
->dev
;
1030 mutex_unlock(&pcdev
->dev_mutex
);
1033 static const struct v4l2_m2m_ops coda_m2m_ops
= {
1034 .device_run
= coda_device_run
,
1035 .job_ready
= coda_job_ready
,
1036 .job_abort
= coda_job_abort
,
1038 .unlock
= coda_unlock
,
1041 static void coda_set_tiled_map_type(struct coda_ctx
*ctx
, int tiled_map_type
)
1043 struct gdi_tiled_map
*tiled_map
= &ctx
->tiled_map
;
1044 int luma_map
, chro_map
, i
;
1046 memset(tiled_map
, 0, sizeof(*tiled_map
));
1050 tiled_map
->map_type
= tiled_map_type
;
1051 for (i
= 0; i
< 16; i
++)
1052 tiled_map
->xy2ca_map
[i
] = luma_map
<< 8 | chro_map
;
1053 for (i
= 0; i
< 4; i
++)
1054 tiled_map
->xy2ba_map
[i
] = luma_map
<< 8 | chro_map
;
1055 for (i
= 0; i
< 16; i
++)
1056 tiled_map
->xy2ra_map
[i
] = luma_map
<< 8 | chro_map
;
1058 if (tiled_map_type
== GDI_LINEAR_FRAME_MAP
) {
1059 tiled_map
->xy2rbc_config
= 0;
1061 dev_err(&ctx
->dev
->plat_dev
->dev
, "invalid map type: %d\n",
1067 static void set_default_params(struct coda_ctx
*ctx
)
1069 unsigned int max_w
, max_h
, usize
, csize
;
1071 ctx
->codec
= coda_find_codec(ctx
->dev
, ctx
->cvd
->src_formats
[0],
1072 ctx
->cvd
->dst_formats
[0]);
1073 max_w
= min(ctx
->codec
->max_w
, 1920U);
1074 max_h
= min(ctx
->codec
->max_h
, 1088U);
1075 usize
= max_w
* max_h
* 3 / 2;
1076 csize
= coda_estimate_sizeimage(ctx
, usize
, max_w
, max_h
);
1078 ctx
->params
.codec_mode
= ctx
->codec
->mode
;
1079 ctx
->colorspace
= V4L2_COLORSPACE_REC709
;
1080 ctx
->params
.framerate
= 30;
1082 /* Default formats for output and input queues */
1083 ctx
->q_data
[V4L2_M2M_SRC
].fourcc
= ctx
->codec
->src_fourcc
;
1084 ctx
->q_data
[V4L2_M2M_DST
].fourcc
= ctx
->codec
->dst_fourcc
;
1085 ctx
->q_data
[V4L2_M2M_SRC
].width
= max_w
;
1086 ctx
->q_data
[V4L2_M2M_SRC
].height
= max_h
;
1087 ctx
->q_data
[V4L2_M2M_DST
].width
= max_w
;
1088 ctx
->q_data
[V4L2_M2M_DST
].height
= max_h
;
1089 if (ctx
->codec
->src_fourcc
== V4L2_PIX_FMT_YUV420
) {
1090 ctx
->q_data
[V4L2_M2M_SRC
].bytesperline
= max_w
;
1091 ctx
->q_data
[V4L2_M2M_SRC
].sizeimage
= usize
;
1092 ctx
->q_data
[V4L2_M2M_DST
].bytesperline
= 0;
1093 ctx
->q_data
[V4L2_M2M_DST
].sizeimage
= csize
;
1095 ctx
->q_data
[V4L2_M2M_SRC
].bytesperline
= 0;
1096 ctx
->q_data
[V4L2_M2M_SRC
].sizeimage
= csize
;
1097 ctx
->q_data
[V4L2_M2M_DST
].bytesperline
= max_w
;
1098 ctx
->q_data
[V4L2_M2M_DST
].sizeimage
= usize
;
1100 ctx
->q_data
[V4L2_M2M_SRC
].rect
.width
= max_w
;
1101 ctx
->q_data
[V4L2_M2M_SRC
].rect
.height
= max_h
;
1102 ctx
->q_data
[V4L2_M2M_DST
].rect
.width
= max_w
;
1103 ctx
->q_data
[V4L2_M2M_DST
].rect
.height
= max_h
;
1105 if (ctx
->dev
->devtype
->product
== CODA_960
)
1106 coda_set_tiled_map_type(ctx
, GDI_LINEAR_FRAME_MAP
);
1112 static int coda_queue_setup(struct vb2_queue
*vq
,
1113 const struct v4l2_format
*fmt
,
1114 unsigned int *nbuffers
, unsigned int *nplanes
,
1115 unsigned int sizes
[], void *alloc_ctxs
[])
1117 struct coda_ctx
*ctx
= vb2_get_drv_priv(vq
);
1118 struct coda_q_data
*q_data
;
1121 q_data
= get_q_data(ctx
, vq
->type
);
1122 size
= q_data
->sizeimage
;
1127 /* Set to vb2-dma-contig allocator context, ignored by vb2-vmalloc */
1128 alloc_ctxs
[0] = ctx
->dev
->alloc_ctx
;
1130 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
1131 "get %d buffer(s) of size %d each.\n", *nbuffers
, size
);
1136 static int coda_buf_prepare(struct vb2_buffer
*vb
)
1138 struct coda_ctx
*ctx
= vb2_get_drv_priv(vb
->vb2_queue
);
1139 struct coda_q_data
*q_data
;
1141 q_data
= get_q_data(ctx
, vb
->vb2_queue
->type
);
1143 if (vb2_plane_size(vb
, 0) < q_data
->sizeimage
) {
1144 v4l2_warn(&ctx
->dev
->v4l2_dev
,
1145 "%s data will not fit into plane (%lu < %lu)\n",
1146 __func__
, vb2_plane_size(vb
, 0),
1147 (long)q_data
->sizeimage
);
1154 static void coda_buf_queue(struct vb2_buffer
*vb
)
1156 struct coda_ctx
*ctx
= vb2_get_drv_priv(vb
->vb2_queue
);
1157 struct vb2_queue
*vq
= vb
->vb2_queue
;
1158 struct coda_q_data
*q_data
;
1160 q_data
= get_q_data(ctx
, vb
->vb2_queue
->type
);
1163 * In the decoder case, immediately try to copy the buffer into the
1164 * bitstream ringbuffer and mark it as ready to be dequeued.
1166 if (ctx
->bitstream
.size
&& vq
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
) {
1168 * For backwards compatibility, queuing an empty buffer marks
1171 if (vb2_get_plane_payload(vb
, 0) == 0)
1172 coda_bit_stream_end_flag(ctx
);
1173 mutex_lock(&ctx
->bitstream_mutex
);
1174 v4l2_m2m_buf_queue(ctx
->fh
.m2m_ctx
, vb
);
1175 if (vb2_is_streaming(vb
->vb2_queue
))
1176 coda_fill_bitstream(ctx
);
1177 mutex_unlock(&ctx
->bitstream_mutex
);
1179 v4l2_m2m_buf_queue(ctx
->fh
.m2m_ctx
, vb
);
1183 int coda_alloc_aux_buf(struct coda_dev
*dev
, struct coda_aux_buf
*buf
,
1184 size_t size
, const char *name
, struct dentry
*parent
)
1186 buf
->vaddr
= dma_alloc_coherent(&dev
->plat_dev
->dev
, size
, &buf
->paddr
,
1189 v4l2_err(&dev
->v4l2_dev
,
1190 "Failed to allocate %s buffer of size %u\n",
1197 if (name
&& parent
) {
1198 buf
->blob
.data
= buf
->vaddr
;
1199 buf
->blob
.size
= size
;
1200 buf
->dentry
= debugfs_create_blob(name
, 0644, parent
,
1203 dev_warn(&dev
->plat_dev
->dev
,
1204 "failed to create debugfs entry %s\n", name
);
1210 void coda_free_aux_buf(struct coda_dev
*dev
,
1211 struct coda_aux_buf
*buf
)
1214 dma_free_coherent(&dev
->plat_dev
->dev
, buf
->size
,
1215 buf
->vaddr
, buf
->paddr
);
1218 debugfs_remove(buf
->dentry
);
1223 static int coda_start_streaming(struct vb2_queue
*q
, unsigned int count
)
1225 struct coda_ctx
*ctx
= vb2_get_drv_priv(q
);
1226 struct v4l2_device
*v4l2_dev
= &ctx
->dev
->v4l2_dev
;
1227 struct coda_q_data
*q_data_src
, *q_data_dst
;
1228 struct vb2_buffer
*buf
;
1231 q_data_src
= get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_OUTPUT
);
1232 if (q
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
) {
1233 if (q_data_src
->fourcc
== V4L2_PIX_FMT_H264
||
1234 (q_data_src
->fourcc
== V4L2_PIX_FMT_JPEG
&&
1235 ctx
->dev
->devtype
->product
== CODA_7541
)) {
1236 /* copy the buffers that where queued before streamon */
1237 mutex_lock(&ctx
->bitstream_mutex
);
1238 coda_fill_bitstream(ctx
);
1239 mutex_unlock(&ctx
->bitstream_mutex
);
1241 if (coda_get_bitstream_payload(ctx
) < 512) {
1252 ctx
->streamon_out
= 1;
1259 ctx
->streamon_cap
= 1;
1262 /* Don't start the coda unless both queues are on */
1263 if (!(ctx
->streamon_out
& ctx
->streamon_cap
))
1266 /* Allow BIT decoder device_run with no new buffers queued */
1267 if (ctx
->inst_type
== CODA_INST_DECODER
&& ctx
->use_bit
)
1268 v4l2_m2m_set_src_buffered(ctx
->fh
.m2m_ctx
, true);
1270 ctx
->gopcounter
= ctx
->params
.gop_size
- 1;
1271 q_data_dst
= get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_CAPTURE
);
1273 ctx
->codec
= coda_find_codec(ctx
->dev
, q_data_src
->fourcc
,
1274 q_data_dst
->fourcc
);
1276 v4l2_err(v4l2_dev
, "couldn't tell instance type.\n");
1281 if (q_data_dst
->fourcc
== V4L2_PIX_FMT_JPEG
)
1282 ctx
->params
.gop_size
= 1;
1283 ctx
->gopcounter
= ctx
->params
.gop_size
- 1;
1285 ret
= ctx
->ops
->start_streaming(ctx
);
1286 if (ctx
->inst_type
== CODA_INST_DECODER
) {
1293 ctx
->initialized
= 1;
1297 if (q
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
) {
1298 while ((buf
= v4l2_m2m_src_buf_remove(ctx
->fh
.m2m_ctx
)))
1299 v4l2_m2m_buf_done(buf
, VB2_BUF_STATE_QUEUED
);
1301 while ((buf
= v4l2_m2m_dst_buf_remove(ctx
->fh
.m2m_ctx
)))
1302 v4l2_m2m_buf_done(buf
, VB2_BUF_STATE_QUEUED
);
1307 static void coda_stop_streaming(struct vb2_queue
*q
)
1309 struct coda_ctx
*ctx
= vb2_get_drv_priv(q
);
1310 struct coda_dev
*dev
= ctx
->dev
;
1311 struct vb2_buffer
*buf
;
1313 if (q
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
) {
1314 v4l2_dbg(1, coda_debug
, &dev
->v4l2_dev
,
1315 "%s: output\n", __func__
);
1316 ctx
->streamon_out
= 0;
1318 coda_bit_stream_end_flag(ctx
);
1322 while ((buf
= v4l2_m2m_src_buf_remove(ctx
->fh
.m2m_ctx
)))
1323 v4l2_m2m_buf_done(buf
, VB2_BUF_STATE_ERROR
);
1325 v4l2_dbg(1, coda_debug
, &dev
->v4l2_dev
,
1326 "%s: capture\n", __func__
);
1327 ctx
->streamon_cap
= 0;
1330 ctx
->sequence_offset
= 0;
1332 while ((buf
= v4l2_m2m_dst_buf_remove(ctx
->fh
.m2m_ctx
)))
1333 v4l2_m2m_buf_done(buf
, VB2_BUF_STATE_ERROR
);
1336 if (!ctx
->streamon_out
&& !ctx
->streamon_cap
) {
1337 struct coda_buffer_meta
*meta
;
1339 if (ctx
->ops
->seq_end_work
) {
1340 queue_work(dev
->workqueue
, &ctx
->seq_end_work
);
1341 flush_work(&ctx
->seq_end_work
);
1343 mutex_lock(&ctx
->bitstream_mutex
);
1344 while (!list_empty(&ctx
->buffer_meta_list
)) {
1345 meta
= list_first_entry(&ctx
->buffer_meta_list
,
1346 struct coda_buffer_meta
, list
);
1347 list_del(&meta
->list
);
1350 mutex_unlock(&ctx
->bitstream_mutex
);
1351 kfifo_init(&ctx
->bitstream_fifo
,
1352 ctx
->bitstream
.vaddr
, ctx
->bitstream
.size
);
1353 ctx
->initialized
= 0;
1354 ctx
->runcounter
= 0;
1359 static const struct vb2_ops coda_qops
= {
1360 .queue_setup
= coda_queue_setup
,
1361 .buf_prepare
= coda_buf_prepare
,
1362 .buf_queue
= coda_buf_queue
,
1363 .start_streaming
= coda_start_streaming
,
1364 .stop_streaming
= coda_stop_streaming
,
1365 .wait_prepare
= vb2_ops_wait_prepare
,
1366 .wait_finish
= vb2_ops_wait_finish
,
1369 static int coda_s_ctrl(struct v4l2_ctrl
*ctrl
)
1371 struct coda_ctx
*ctx
=
1372 container_of(ctrl
->handler
, struct coda_ctx
, ctrls
);
1374 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
1375 "s_ctrl: id = %d, val = %d\n", ctrl
->id
, ctrl
->val
);
1378 case V4L2_CID_HFLIP
:
1380 ctx
->params
.rot_mode
|= CODA_MIR_HOR
;
1382 ctx
->params
.rot_mode
&= ~CODA_MIR_HOR
;
1384 case V4L2_CID_VFLIP
:
1386 ctx
->params
.rot_mode
|= CODA_MIR_VER
;
1388 ctx
->params
.rot_mode
&= ~CODA_MIR_VER
;
1390 case V4L2_CID_MPEG_VIDEO_BITRATE
:
1391 ctx
->params
.bitrate
= ctrl
->val
/ 1000;
1393 case V4L2_CID_MPEG_VIDEO_GOP_SIZE
:
1394 ctx
->params
.gop_size
= ctrl
->val
;
1396 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP
:
1397 ctx
->params
.h264_intra_qp
= ctrl
->val
;
1399 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP
:
1400 ctx
->params
.h264_inter_qp
= ctrl
->val
;
1402 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP
:
1403 ctx
->params
.h264_min_qp
= ctrl
->val
;
1405 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP
:
1406 ctx
->params
.h264_max_qp
= ctrl
->val
;
1408 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA
:
1409 ctx
->params
.h264_deblk_alpha
= ctrl
->val
;
1411 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA
:
1412 ctx
->params
.h264_deblk_beta
= ctrl
->val
;
1414 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE
:
1415 ctx
->params
.h264_deblk_enabled
= (ctrl
->val
==
1416 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED
);
1418 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP
:
1419 ctx
->params
.mpeg4_intra_qp
= ctrl
->val
;
1421 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP
:
1422 ctx
->params
.mpeg4_inter_qp
= ctrl
->val
;
1424 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE
:
1425 ctx
->params
.slice_mode
= ctrl
->val
;
1427 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB
:
1428 ctx
->params
.slice_max_mb
= ctrl
->val
;
1430 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES
:
1431 ctx
->params
.slice_max_bits
= ctrl
->val
* 8;
1433 case V4L2_CID_MPEG_VIDEO_HEADER_MODE
:
1435 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB
:
1436 ctx
->params
.intra_refresh
= ctrl
->val
;
1438 case V4L2_CID_JPEG_COMPRESSION_QUALITY
:
1439 coda_set_jpeg_compression_quality(ctx
, ctrl
->val
);
1441 case V4L2_CID_JPEG_RESTART_INTERVAL
:
1442 ctx
->params
.jpeg_restart_interval
= ctrl
->val
;
1445 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
1446 "Invalid control, id=%d, val=%d\n",
1447 ctrl
->id
, ctrl
->val
);
1454 static const struct v4l2_ctrl_ops coda_ctrl_ops
= {
1455 .s_ctrl
= coda_s_ctrl
,
1458 static void coda_encode_ctrls(struct coda_ctx
*ctx
)
1460 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1461 V4L2_CID_MPEG_VIDEO_BITRATE
, 0, 32767000, 1000, 0);
1462 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1463 V4L2_CID_MPEG_VIDEO_GOP_SIZE
, 1, 60, 1, 16);
1464 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1465 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP
, 0, 51, 1, 25);
1466 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1467 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP
, 0, 51, 1, 25);
1468 if (ctx
->dev
->devtype
->product
!= CODA_960
) {
1469 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1470 V4L2_CID_MPEG_VIDEO_H264_MIN_QP
, 0, 51, 1, 12);
1472 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1473 V4L2_CID_MPEG_VIDEO_H264_MAX_QP
, 0, 51, 1, 51);
1474 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1475 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA
, 0, 15, 1, 0);
1476 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1477 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA
, 0, 15, 1, 0);
1478 v4l2_ctrl_new_std_menu(&ctx
->ctrls
, &coda_ctrl_ops
,
1479 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE
,
1480 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED
, 0x0,
1481 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED
);
1482 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1483 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP
, 1, 31, 1, 2);
1484 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1485 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP
, 1, 31, 1, 2);
1486 v4l2_ctrl_new_std_menu(&ctx
->ctrls
, &coda_ctrl_ops
,
1487 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE
,
1488 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES
, 0x0,
1489 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE
);
1490 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1491 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB
, 1, 0x3fffffff, 1, 1);
1492 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1493 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES
, 1, 0x3fffffff, 1,
1495 v4l2_ctrl_new_std_menu(&ctx
->ctrls
, &coda_ctrl_ops
,
1496 V4L2_CID_MPEG_VIDEO_HEADER_MODE
,
1497 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME
,
1498 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE
),
1499 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME
);
1500 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1501 V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB
, 0,
1502 1920 * 1088 / 256, 1, 0);
1505 static void coda_jpeg_encode_ctrls(struct coda_ctx
*ctx
)
1507 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1508 V4L2_CID_JPEG_COMPRESSION_QUALITY
, 5, 100, 1, 50);
1509 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1510 V4L2_CID_JPEG_RESTART_INTERVAL
, 0, 100, 1, 0);
1513 static int coda_ctrls_setup(struct coda_ctx
*ctx
)
1515 v4l2_ctrl_handler_init(&ctx
->ctrls
, 2);
1517 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1518 V4L2_CID_HFLIP
, 0, 1, 1, 0);
1519 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1520 V4L2_CID_VFLIP
, 0, 1, 1, 0);
1521 if (ctx
->inst_type
== CODA_INST_ENCODER
) {
1522 if (ctx
->cvd
->dst_formats
[0] == V4L2_PIX_FMT_JPEG
)
1523 coda_jpeg_encode_ctrls(ctx
);
1525 coda_encode_ctrls(ctx
);
1528 if (ctx
->ctrls
.error
) {
1529 v4l2_err(&ctx
->dev
->v4l2_dev
,
1530 "control initialization error (%d)",
1535 return v4l2_ctrl_handler_setup(&ctx
->ctrls
);
1538 static int coda_queue_init(struct coda_ctx
*ctx
, struct vb2_queue
*vq
)
1541 vq
->ops
= &coda_qops
;
1542 vq
->buf_struct_size
= sizeof(struct v4l2_m2m_buffer
);
1543 vq
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_COPY
;
1544 vq
->lock
= &ctx
->dev
->dev_mutex
;
1545 /* One way to indicate end-of-stream for coda is to set the
1546 * bytesused == 0. However by default videobuf2 handles bytesused
1547 * equal to 0 as a special case and changes its value to the size
1548 * of the buffer. Set the allow_zero_bytesused flag, so
1549 * that videobuf2 will keep the value of bytesused intact.
1551 vq
->allow_zero_bytesused
= 1;
1553 return vb2_queue_init(vq
);
1556 int coda_encoder_queue_init(void *priv
, struct vb2_queue
*src_vq
,
1557 struct vb2_queue
*dst_vq
)
1561 src_vq
->type
= V4L2_BUF_TYPE_VIDEO_OUTPUT
;
1562 src_vq
->io_modes
= VB2_DMABUF
| VB2_MMAP
;
1563 src_vq
->mem_ops
= &vb2_dma_contig_memops
;
1565 ret
= coda_queue_init(priv
, src_vq
);
1569 dst_vq
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1570 dst_vq
->io_modes
= VB2_DMABUF
| VB2_MMAP
;
1571 dst_vq
->mem_ops
= &vb2_dma_contig_memops
;
1573 return coda_queue_init(priv
, dst_vq
);
1576 int coda_decoder_queue_init(void *priv
, struct vb2_queue
*src_vq
,
1577 struct vb2_queue
*dst_vq
)
1581 src_vq
->type
= V4L2_BUF_TYPE_VIDEO_OUTPUT
;
1582 src_vq
->io_modes
= VB2_DMABUF
| VB2_MMAP
| VB2_USERPTR
;
1583 src_vq
->mem_ops
= &vb2_vmalloc_memops
;
1585 ret
= coda_queue_init(priv
, src_vq
);
1589 dst_vq
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1590 dst_vq
->io_modes
= VB2_DMABUF
| VB2_MMAP
;
1591 dst_vq
->mem_ops
= &vb2_dma_contig_memops
;
1593 return coda_queue_init(priv
, dst_vq
);
1596 static int coda_next_free_instance(struct coda_dev
*dev
)
1598 int idx
= ffz(dev
->instance_mask
);
1601 (dev
->devtype
->product
== CODA_DX6
&& idx
> CODADX6_MAX_INSTANCES
))
1611 static int coda_open(struct file
*file
)
1613 struct video_device
*vdev
= video_devdata(file
);
1614 struct coda_dev
*dev
= video_get_drvdata(vdev
);
1615 struct coda_ctx
*ctx
= NULL
;
1620 ctx
= kzalloc(sizeof(*ctx
), GFP_KERNEL
);
1624 idx
= coda_next_free_instance(dev
);
1629 set_bit(idx
, &dev
->instance_mask
);
1631 name
= kasprintf(GFP_KERNEL
, "context%d", idx
);
1634 goto err_coda_name_init
;
1637 ctx
->debugfs_entry
= debugfs_create_dir(name
, dev
->debugfs_root
);
1640 ctx
->cvd
= to_coda_video_device(vdev
);
1641 ctx
->inst_type
= ctx
->cvd
->type
;
1642 ctx
->ops
= ctx
->cvd
->ops
;
1643 ctx
->use_bit
= !ctx
->cvd
->direct
;
1644 init_completion(&ctx
->completion
);
1645 INIT_WORK(&ctx
->pic_run_work
, coda_pic_run_work
);
1646 if (ctx
->ops
->seq_end_work
)
1647 INIT_WORK(&ctx
->seq_end_work
, ctx
->ops
->seq_end_work
);
1648 v4l2_fh_init(&ctx
->fh
, video_devdata(file
));
1649 file
->private_data
= &ctx
->fh
;
1650 v4l2_fh_add(&ctx
->fh
);
1653 switch (dev
->devtype
->product
) {
1655 ctx
->frame_mem_ctrl
= 1 << 12;
1664 /* Power up and upload firmware if necessary */
1665 ret
= pm_runtime_get_sync(&dev
->plat_dev
->dev
);
1667 v4l2_err(&dev
->v4l2_dev
, "failed to power up: %d\n", ret
);
1671 ret
= clk_prepare_enable(dev
->clk_per
);
1675 ret
= clk_prepare_enable(dev
->clk_ahb
);
1679 set_default_params(ctx
);
1680 ctx
->fh
.m2m_ctx
= v4l2_m2m_ctx_init(dev
->m2m_dev
, ctx
,
1681 ctx
->ops
->queue_init
);
1682 if (IS_ERR(ctx
->fh
.m2m_ctx
)) {
1683 ret
= PTR_ERR(ctx
->fh
.m2m_ctx
);
1685 v4l2_err(&dev
->v4l2_dev
, "%s return error (%d)\n",
1690 ret
= coda_ctrls_setup(ctx
);
1692 v4l2_err(&dev
->v4l2_dev
, "failed to setup coda controls\n");
1693 goto err_ctrls_setup
;
1696 ctx
->fh
.ctrl_handler
= &ctx
->ctrls
;
1699 ret
= coda_alloc_context_buf(ctx
, &ctx
->parabuf
,
1700 CODA_PARA_BUF_SIZE
, "parabuf");
1702 v4l2_err(&dev
->v4l2_dev
, "failed to allocate parabuf");
1706 if (ctx
->use_bit
&& ctx
->inst_type
== CODA_INST_DECODER
) {
1707 ctx
->bitstream
.size
= CODA_MAX_FRAME_SIZE
;
1708 ctx
->bitstream
.vaddr
= dma_alloc_writecombine(
1709 &dev
->plat_dev
->dev
, ctx
->bitstream
.size
,
1710 &ctx
->bitstream
.paddr
, GFP_KERNEL
);
1711 if (!ctx
->bitstream
.vaddr
) {
1712 v4l2_err(&dev
->v4l2_dev
,
1713 "failed to allocate bitstream ringbuffer");
1715 goto err_dma_writecombine
;
1718 kfifo_init(&ctx
->bitstream_fifo
,
1719 ctx
->bitstream
.vaddr
, ctx
->bitstream
.size
);
1720 mutex_init(&ctx
->bitstream_mutex
);
1721 mutex_init(&ctx
->buffer_mutex
);
1722 INIT_LIST_HEAD(&ctx
->buffer_meta_list
);
1725 list_add(&ctx
->list
, &dev
->instances
);
1728 v4l2_dbg(1, coda_debug
, &dev
->v4l2_dev
, "Created instance %d (%p)\n",
1733 err_dma_writecombine
:
1734 if (ctx
->dev
->devtype
->product
== CODA_DX6
)
1735 coda_free_aux_buf(dev
, &ctx
->workbuf
);
1736 coda_free_aux_buf(dev
, &ctx
->parabuf
);
1738 v4l2_ctrl_handler_free(&ctx
->ctrls
);
1740 v4l2_m2m_ctx_release(ctx
->fh
.m2m_ctx
);
1742 clk_disable_unprepare(dev
->clk_ahb
);
1744 clk_disable_unprepare(dev
->clk_per
);
1746 pm_runtime_put_sync(&dev
->plat_dev
->dev
);
1748 v4l2_fh_del(&ctx
->fh
);
1749 v4l2_fh_exit(&ctx
->fh
);
1750 clear_bit(ctx
->idx
, &dev
->instance_mask
);
1757 static int coda_release(struct file
*file
)
1759 struct coda_dev
*dev
= video_drvdata(file
);
1760 struct coda_ctx
*ctx
= fh_to_ctx(file
->private_data
);
1762 v4l2_dbg(1, coda_debug
, &dev
->v4l2_dev
, "Releasing instance %p\n",
1765 if (ctx
->inst_type
== CODA_INST_DECODER
&& ctx
->use_bit
)
1766 coda_bit_stream_end_flag(ctx
);
1768 /* If this instance is running, call .job_abort and wait for it to end */
1769 v4l2_m2m_ctx_release(ctx
->fh
.m2m_ctx
);
1771 /* In case the instance was not running, we still need to call SEQ_END */
1772 if (ctx
->initialized
&& ctx
->ops
->seq_end_work
) {
1773 queue_work(dev
->workqueue
, &ctx
->seq_end_work
);
1774 flush_work(&ctx
->seq_end_work
);
1778 list_del(&ctx
->list
);
1781 if (ctx
->bitstream
.vaddr
) {
1782 dma_free_writecombine(&dev
->plat_dev
->dev
, ctx
->bitstream
.size
,
1783 ctx
->bitstream
.vaddr
, ctx
->bitstream
.paddr
);
1785 if (ctx
->dev
->devtype
->product
== CODA_DX6
)
1786 coda_free_aux_buf(dev
, &ctx
->workbuf
);
1788 coda_free_aux_buf(dev
, &ctx
->parabuf
);
1789 v4l2_ctrl_handler_free(&ctx
->ctrls
);
1790 clk_disable_unprepare(dev
->clk_ahb
);
1791 clk_disable_unprepare(dev
->clk_per
);
1792 pm_runtime_put_sync(&dev
->plat_dev
->dev
);
1793 v4l2_fh_del(&ctx
->fh
);
1794 v4l2_fh_exit(&ctx
->fh
);
1795 clear_bit(ctx
->idx
, &dev
->instance_mask
);
1796 if (ctx
->ops
->release
)
1797 ctx
->ops
->release(ctx
);
1798 debugfs_remove_recursive(ctx
->debugfs_entry
);
1804 static const struct v4l2_file_operations coda_fops
= {
1805 .owner
= THIS_MODULE
,
1807 .release
= coda_release
,
1808 .poll
= v4l2_m2m_fop_poll
,
1809 .unlocked_ioctl
= video_ioctl2
,
1810 .mmap
= v4l2_m2m_fop_mmap
,
1813 static int coda_hw_init(struct coda_dev
*dev
)
1819 ret
= clk_prepare_enable(dev
->clk_per
);
1823 ret
= clk_prepare_enable(dev
->clk_ahb
);
1828 reset_control_reset(dev
->rstc
);
1831 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
1832 * The 16-bit chars in the code buffer are in memory access
1833 * order, re-sort them to CODA order for register download.
1834 * Data in this SRAM survives a reboot.
1836 p
= (u16
*)dev
->codebuf
.vaddr
;
1837 if (dev
->devtype
->product
== CODA_DX6
) {
1838 for (i
= 0; i
< (CODA_ISRAM_SIZE
/ 2); i
++) {
1839 data
= CODA_DOWN_ADDRESS_SET(i
) |
1840 CODA_DOWN_DATA_SET(p
[i
^ 1]);
1841 coda_write(dev
, data
, CODA_REG_BIT_CODE_DOWN
);
1844 for (i
= 0; i
< (CODA_ISRAM_SIZE
/ 2); i
++) {
1845 data
= CODA_DOWN_ADDRESS_SET(i
) |
1846 CODA_DOWN_DATA_SET(p
[round_down(i
, 4) +
1848 coda_write(dev
, data
, CODA_REG_BIT_CODE_DOWN
);
1852 /* Clear registers */
1853 for (i
= 0; i
< 64; i
++)
1854 coda_write(dev
, 0, CODA_REG_BIT_CODE_BUF_ADDR
+ i
* 4);
1856 /* Tell the BIT where to find everything it needs */
1857 if (dev
->devtype
->product
== CODA_960
||
1858 dev
->devtype
->product
== CODA_7541
) {
1859 coda_write(dev
, dev
->tempbuf
.paddr
,
1860 CODA_REG_BIT_TEMP_BUF_ADDR
);
1861 coda_write(dev
, 0, CODA_REG_BIT_BIT_STREAM_PARAM
);
1863 coda_write(dev
, dev
->workbuf
.paddr
,
1864 CODA_REG_BIT_WORK_BUF_ADDR
);
1866 coda_write(dev
, dev
->codebuf
.paddr
,
1867 CODA_REG_BIT_CODE_BUF_ADDR
);
1868 coda_write(dev
, 0, CODA_REG_BIT_CODE_RUN
);
1870 /* Set default values */
1871 switch (dev
->devtype
->product
) {
1873 coda_write(dev
, CODADX6_STREAM_BUF_PIC_FLUSH
,
1874 CODA_REG_BIT_STREAM_CTRL
);
1877 coda_write(dev
, CODA7_STREAM_BUF_PIC_FLUSH
,
1878 CODA_REG_BIT_STREAM_CTRL
);
1880 if (dev
->devtype
->product
== CODA_960
)
1881 coda_write(dev
, 1 << 12, CODA_REG_BIT_FRAME_MEM_CTRL
);
1883 coda_write(dev
, 0, CODA_REG_BIT_FRAME_MEM_CTRL
);
1885 if (dev
->devtype
->product
!= CODA_DX6
)
1886 coda_write(dev
, 0, CODA7_REG_BIT_AXI_SRAM_USE
);
1888 coda_write(dev
, CODA_INT_INTERRUPT_ENABLE
,
1889 CODA_REG_BIT_INT_ENABLE
);
1891 /* Reset VPU and start processor */
1892 data
= coda_read(dev
, CODA_REG_BIT_CODE_RESET
);
1893 data
|= CODA_REG_RESET_ENABLE
;
1894 coda_write(dev
, data
, CODA_REG_BIT_CODE_RESET
);
1896 data
&= ~CODA_REG_RESET_ENABLE
;
1897 coda_write(dev
, data
, CODA_REG_BIT_CODE_RESET
);
1898 coda_write(dev
, CODA_REG_RUN_ENABLE
, CODA_REG_BIT_CODE_RUN
);
1900 clk_disable_unprepare(dev
->clk_ahb
);
1901 clk_disable_unprepare(dev
->clk_per
);
1906 clk_disable_unprepare(dev
->clk_per
);
1911 static int coda_register_device(struct coda_dev
*dev
, int i
)
1913 struct video_device
*vfd
= &dev
->vfd
[i
];
1915 if (i
>= dev
->devtype
->num_vdevs
)
1918 strlcpy(vfd
->name
, dev
->devtype
->vdevs
[i
]->name
, sizeof(vfd
->name
));
1919 vfd
->fops
= &coda_fops
;
1920 vfd
->ioctl_ops
= &coda_ioctl_ops
;
1921 vfd
->release
= video_device_release_empty
,
1922 vfd
->lock
= &dev
->dev_mutex
;
1923 vfd
->v4l2_dev
= &dev
->v4l2_dev
;
1924 vfd
->vfl_dir
= VFL_DIR_M2M
;
1925 video_set_drvdata(vfd
, dev
);
1927 /* Not applicable, use the selection API instead */
1928 v4l2_disable_ioctl(vfd
, VIDIOC_CROPCAP
);
1929 v4l2_disable_ioctl(vfd
, VIDIOC_G_CROP
);
1930 v4l2_disable_ioctl(vfd
, VIDIOC_S_CROP
);
1932 return video_register_device(vfd
, VFL_TYPE_GRABBER
, 0);
1935 static void coda_fw_callback(const struct firmware
*fw
, void *context
)
1937 struct coda_dev
*dev
= context
;
1938 struct platform_device
*pdev
= dev
->plat_dev
;
1942 v4l2_err(&dev
->v4l2_dev
, "firmware request failed\n");
1946 /* allocate auxiliary per-device code buffer for the BIT processor */
1947 ret
= coda_alloc_aux_buf(dev
, &dev
->codebuf
, fw
->size
, "codebuf",
1950 dev_err(&pdev
->dev
, "failed to allocate code buffer\n");
1954 /* Copy the whole firmware image to the code buffer */
1955 memcpy(dev
->codebuf
.vaddr
, fw
->data
, fw
->size
);
1956 release_firmware(fw
);
1958 ret
= coda_hw_init(dev
);
1960 v4l2_err(&dev
->v4l2_dev
, "HW initialization failed\n");
1964 ret
= coda_check_firmware(dev
);
1968 dev
->alloc_ctx
= vb2_dma_contig_init_ctx(&pdev
->dev
);
1969 if (IS_ERR(dev
->alloc_ctx
)) {
1970 v4l2_err(&dev
->v4l2_dev
, "Failed to alloc vb2 context\n");
1974 dev
->m2m_dev
= v4l2_m2m_init(&coda_m2m_ops
);
1975 if (IS_ERR(dev
->m2m_dev
)) {
1976 v4l2_err(&dev
->v4l2_dev
, "Failed to init mem2mem device\n");
1980 for (i
= 0; i
< dev
->devtype
->num_vdevs
; i
++) {
1981 ret
= coda_register_device(dev
, i
);
1983 v4l2_err(&dev
->v4l2_dev
,
1984 "Failed to register %s video device: %d\n",
1985 dev
->devtype
->vdevs
[i
]->name
, ret
);
1990 v4l2_info(&dev
->v4l2_dev
, "codec registered as /dev/video[%d-%d]\n",
1991 dev
->vfd
[0].num
, dev
->vfd
[i
- 1].num
);
1993 pm_runtime_put_sync(&pdev
->dev
);
1998 video_unregister_device(&dev
->vfd
[i
]);
1999 v4l2_m2m_release(dev
->m2m_dev
);
2001 vb2_dma_contig_cleanup_ctx(dev
->alloc_ctx
);
2003 pm_runtime_put_sync(&pdev
->dev
);
2006 static int coda_firmware_request(struct coda_dev
*dev
)
2008 char *fw
= dev
->devtype
->firmware
;
2010 dev_dbg(&dev
->plat_dev
->dev
, "requesting firmware '%s' for %s\n", fw
,
2011 coda_product_name(dev
->devtype
->product
));
2013 return request_firmware_nowait(THIS_MODULE
, true,
2014 fw
, &dev
->plat_dev
->dev
, GFP_KERNEL
, dev
, coda_fw_callback
);
2017 enum coda_platform
{
2024 static const struct coda_devtype coda_devdata
[] = {
2026 .firmware
= "v4l-codadx6-imx27.bin",
2027 .product
= CODA_DX6
,
2028 .codecs
= codadx6_codecs
,
2029 .num_codecs
= ARRAY_SIZE(codadx6_codecs
),
2030 .vdevs
= codadx6_video_devices
,
2031 .num_vdevs
= ARRAY_SIZE(codadx6_video_devices
),
2032 .workbuf_size
= 288 * 1024 + FMO_SLICE_SAVE_BUF_SIZE
* 8 * 1024,
2033 .iram_size
= 0xb000,
2036 .firmware
= "v4l-coda7541-imx53.bin",
2037 .product
= CODA_7541
,
2038 .codecs
= coda7_codecs
,
2039 .num_codecs
= ARRAY_SIZE(coda7_codecs
),
2040 .vdevs
= coda7_video_devices
,
2041 .num_vdevs
= ARRAY_SIZE(coda7_video_devices
),
2042 .workbuf_size
= 128 * 1024,
2043 .tempbuf_size
= 304 * 1024,
2044 .iram_size
= 0x14000,
2047 .firmware
= "v4l-coda960-imx6q.bin",
2048 .product
= CODA_960
,
2049 .codecs
= coda9_codecs
,
2050 .num_codecs
= ARRAY_SIZE(coda9_codecs
),
2051 .vdevs
= coda9_video_devices
,
2052 .num_vdevs
= ARRAY_SIZE(coda9_video_devices
),
2053 .workbuf_size
= 80 * 1024,
2054 .tempbuf_size
= 204 * 1024,
2055 .iram_size
= 0x21000,
2058 .firmware
= "v4l-coda960-imx6dl.bin",
2059 .product
= CODA_960
,
2060 .codecs
= coda9_codecs
,
2061 .num_codecs
= ARRAY_SIZE(coda9_codecs
),
2062 .vdevs
= coda9_video_devices
,
2063 .num_vdevs
= ARRAY_SIZE(coda9_video_devices
),
2064 .workbuf_size
= 80 * 1024,
2065 .tempbuf_size
= 204 * 1024,
2066 .iram_size
= 0x20000,
2070 static struct platform_device_id coda_platform_ids
[] = {
2071 { .name
= "coda-imx27", .driver_data
= CODA_IMX27
},
2074 MODULE_DEVICE_TABLE(platform
, coda_platform_ids
);
2077 static const struct of_device_id coda_dt_ids
[] = {
2078 { .compatible
= "fsl,imx27-vpu", .data
= &coda_devdata
[CODA_IMX27
] },
2079 { .compatible
= "fsl,imx53-vpu", .data
= &coda_devdata
[CODA_IMX53
] },
2080 { .compatible
= "fsl,imx6q-vpu", .data
= &coda_devdata
[CODA_IMX6Q
] },
2081 { .compatible
= "fsl,imx6dl-vpu", .data
= &coda_devdata
[CODA_IMX6DL
] },
2084 MODULE_DEVICE_TABLE(of
, coda_dt_ids
);
2087 static int coda_probe(struct platform_device
*pdev
)
2089 const struct of_device_id
*of_id
=
2090 of_match_device(of_match_ptr(coda_dt_ids
), &pdev
->dev
);
2091 const struct platform_device_id
*pdev_id
;
2092 struct coda_platform_data
*pdata
= pdev
->dev
.platform_data
;
2093 struct device_node
*np
= pdev
->dev
.of_node
;
2094 struct gen_pool
*pool
;
2095 struct coda_dev
*dev
;
2096 struct resource
*res
;
2099 dev
= devm_kzalloc(&pdev
->dev
, sizeof(*dev
), GFP_KERNEL
);
2103 pdev_id
= of_id
? of_id
->data
: platform_get_device_id(pdev
);
2106 dev
->devtype
= of_id
->data
;
2107 } else if (pdev_id
) {
2108 dev
->devtype
= &coda_devdata
[pdev_id
->driver_data
];
2111 goto err_v4l2_register
;
2114 spin_lock_init(&dev
->irqlock
);
2115 INIT_LIST_HEAD(&dev
->instances
);
2117 dev
->plat_dev
= pdev
;
2118 dev
->clk_per
= devm_clk_get(&pdev
->dev
, "per");
2119 if (IS_ERR(dev
->clk_per
)) {
2120 dev_err(&pdev
->dev
, "Could not get per clock\n");
2121 return PTR_ERR(dev
->clk_per
);
2124 dev
->clk_ahb
= devm_clk_get(&pdev
->dev
, "ahb");
2125 if (IS_ERR(dev
->clk_ahb
)) {
2126 dev_err(&pdev
->dev
, "Could not get ahb clock\n");
2127 return PTR_ERR(dev
->clk_ahb
);
2130 /* Get memory for physical registers */
2131 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2132 dev
->regs_base
= devm_ioremap_resource(&pdev
->dev
, res
);
2133 if (IS_ERR(dev
->regs_base
))
2134 return PTR_ERR(dev
->regs_base
);
2137 irq
= platform_get_irq_byname(pdev
, "bit");
2139 irq
= platform_get_irq(pdev
, 0);
2141 dev_err(&pdev
->dev
, "failed to get irq resource\n");
2145 ret
= devm_request_threaded_irq(&pdev
->dev
, irq
, NULL
, coda_irq_handler
,
2146 IRQF_ONESHOT
, dev_name(&pdev
->dev
), dev
);
2148 dev_err(&pdev
->dev
, "failed to request irq: %d\n", ret
);
2152 dev
->rstc
= devm_reset_control_get_optional(&pdev
->dev
, NULL
);
2153 if (IS_ERR(dev
->rstc
)) {
2154 ret
= PTR_ERR(dev
->rstc
);
2155 if (ret
== -ENOENT
|| ret
== -ENOSYS
) {
2158 dev_err(&pdev
->dev
, "failed get reset control: %d\n",
2164 /* Get IRAM pool from device tree or platform data */
2165 pool
= of_get_named_gen_pool(np
, "iram", 0);
2167 pool
= dev_get_gen_pool(pdata
->iram_dev
);
2169 dev_err(&pdev
->dev
, "iram pool not available\n");
2172 dev
->iram_pool
= pool
;
2174 ret
= v4l2_device_register(&pdev
->dev
, &dev
->v4l2_dev
);
2178 mutex_init(&dev
->dev_mutex
);
2179 mutex_init(&dev
->coda_mutex
);
2181 dev
->debugfs_root
= debugfs_create_dir("coda", NULL
);
2182 if (!dev
->debugfs_root
)
2183 dev_warn(&pdev
->dev
, "failed to create debugfs root\n");
2185 /* allocate auxiliary per-device buffers for the BIT processor */
2186 if (dev
->devtype
->product
== CODA_DX6
) {
2187 ret
= coda_alloc_aux_buf(dev
, &dev
->workbuf
,
2188 dev
->devtype
->workbuf_size
, "workbuf",
2191 dev_err(&pdev
->dev
, "failed to allocate work buffer\n");
2192 goto err_v4l2_register
;
2196 if (dev
->devtype
->tempbuf_size
) {
2197 ret
= coda_alloc_aux_buf(dev
, &dev
->tempbuf
,
2198 dev
->devtype
->tempbuf_size
, "tempbuf",
2201 dev_err(&pdev
->dev
, "failed to allocate temp buffer\n");
2202 goto err_v4l2_register
;
2206 dev
->iram
.size
= dev
->devtype
->iram_size
;
2207 dev
->iram
.vaddr
= gen_pool_dma_alloc(dev
->iram_pool
, dev
->iram
.size
,
2209 if (!dev
->iram
.vaddr
) {
2210 dev_warn(&pdev
->dev
, "unable to alloc iram\n");
2212 memset(dev
->iram
.vaddr
, 0, dev
->iram
.size
);
2213 dev
->iram
.blob
.data
= dev
->iram
.vaddr
;
2214 dev
->iram
.blob
.size
= dev
->iram
.size
;
2215 dev
->iram
.dentry
= debugfs_create_blob("iram", 0644,
2220 dev
->workqueue
= alloc_workqueue("coda", WQ_UNBOUND
| WQ_MEM_RECLAIM
, 1);
2221 if (!dev
->workqueue
) {
2222 dev_err(&pdev
->dev
, "unable to alloc workqueue\n");
2224 goto err_v4l2_register
;
2227 platform_set_drvdata(pdev
, dev
);
2230 * Start activated so we can directly call coda_hw_init in
2231 * coda_fw_callback regardless of whether CONFIG_PM is
2232 * enabled or whether the device is associated with a PM domain.
2234 pm_runtime_get_noresume(&pdev
->dev
);
2235 pm_runtime_set_active(&pdev
->dev
);
2236 pm_runtime_enable(&pdev
->dev
);
2238 return coda_firmware_request(dev
);
2241 v4l2_device_unregister(&dev
->v4l2_dev
);
2245 static int coda_remove(struct platform_device
*pdev
)
2247 struct coda_dev
*dev
= platform_get_drvdata(pdev
);
2250 for (i
= 0; i
< ARRAY_SIZE(dev
->vfd
); i
++) {
2251 if (video_get_drvdata(&dev
->vfd
[i
]))
2252 video_unregister_device(&dev
->vfd
[i
]);
2255 v4l2_m2m_release(dev
->m2m_dev
);
2256 pm_runtime_disable(&pdev
->dev
);
2258 vb2_dma_contig_cleanup_ctx(dev
->alloc_ctx
);
2259 v4l2_device_unregister(&dev
->v4l2_dev
);
2260 destroy_workqueue(dev
->workqueue
);
2261 if (dev
->iram
.vaddr
)
2262 gen_pool_free(dev
->iram_pool
, (unsigned long)dev
->iram
.vaddr
,
2264 coda_free_aux_buf(dev
, &dev
->codebuf
);
2265 coda_free_aux_buf(dev
, &dev
->tempbuf
);
2266 coda_free_aux_buf(dev
, &dev
->workbuf
);
2267 debugfs_remove_recursive(dev
->debugfs_root
);
2272 static int coda_runtime_resume(struct device
*dev
)
2274 struct coda_dev
*cdev
= dev_get_drvdata(dev
);
2277 if (dev
->pm_domain
&& cdev
->codebuf
.vaddr
) {
2278 ret
= coda_hw_init(cdev
);
2280 v4l2_err(&cdev
->v4l2_dev
, "HW initialization failed\n");
2287 static const struct dev_pm_ops coda_pm_ops
= {
2288 SET_RUNTIME_PM_OPS(NULL
, coda_runtime_resume
, NULL
)
2291 static struct platform_driver coda_driver
= {
2292 .probe
= coda_probe
,
2293 .remove
= coda_remove
,
2296 .of_match_table
= of_match_ptr(coda_dt_ids
),
2299 .id_table
= coda_platform_ids
,
2302 module_platform_driver(coda_driver
);
2304 MODULE_LICENSE("GPL");
2305 MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
2306 MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");