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_ISRAM_SIZE (2048 * 2)
54 #define S_ALIGN 1 /* multiple of 2 */
55 #define W_ALIGN 1 /* multiple of 2 */
56 #define H_ALIGN 1 /* multiple of 2 */
58 #define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh)
61 module_param(coda_debug
, int, 0644);
62 MODULE_PARM_DESC(coda_debug
, "Debug level (0-2)");
64 void coda_write(struct coda_dev
*dev
, u32 data
, u32 reg
)
66 v4l2_dbg(2, coda_debug
, &dev
->v4l2_dev
,
67 "%s: data=0x%x, reg=0x%x\n", __func__
, data
, reg
);
68 writel(data
, dev
->regs_base
+ reg
);
71 unsigned int coda_read(struct coda_dev
*dev
, u32 reg
)
75 data
= readl(dev
->regs_base
+ reg
);
76 v4l2_dbg(2, coda_debug
, &dev
->v4l2_dev
,
77 "%s: data=0x%x, reg=0x%x\n", __func__
, data
, reg
);
81 void coda_write_base(struct coda_ctx
*ctx
, struct coda_q_data
*q_data
,
82 struct vb2_buffer
*buf
, unsigned int reg_y
)
84 u32 base_y
= vb2_dma_contig_plane_dma_addr(buf
, 0);
87 switch (q_data
->fourcc
) {
88 case V4L2_PIX_FMT_YVU420
:
89 /* Switch Cb and Cr for YVU420 format */
90 base_cr
= base_y
+ q_data
->bytesperline
* q_data
->height
;
91 base_cb
= base_cr
+ q_data
->bytesperline
* q_data
->height
/ 4;
93 case V4L2_PIX_FMT_YUV420
:
94 case V4L2_PIX_FMT_NV12
:
96 base_cb
= base_y
+ q_data
->bytesperline
* q_data
->height
;
97 base_cr
= base_cb
+ q_data
->bytesperline
* q_data
->height
/ 4;
99 case V4L2_PIX_FMT_YUV422P
:
100 base_cb
= base_y
+ q_data
->bytesperline
* q_data
->height
;
101 base_cr
= base_cb
+ q_data
->bytesperline
* q_data
->height
/ 2;
104 coda_write(ctx
->dev
, base_y
, reg_y
);
105 coda_write(ctx
->dev
, base_cb
, reg_y
+ 4);
106 coda_write(ctx
->dev
, base_cr
, reg_y
+ 8);
109 #define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
110 { mode, src_fourcc, dst_fourcc, max_w, max_h }
113 * Arrays of codecs supported by each given version of Coda:
117 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
119 static const struct coda_codec codadx6_codecs
[] = {
120 CODA_CODEC(CODADX6_MODE_ENCODE_H264
, V4L2_PIX_FMT_YUV420
, V4L2_PIX_FMT_H264
, 720, 576),
121 CODA_CODEC(CODADX6_MODE_ENCODE_MP4
, V4L2_PIX_FMT_YUV420
, V4L2_PIX_FMT_MPEG4
, 720, 576),
124 static const struct coda_codec coda7_codecs
[] = {
125 CODA_CODEC(CODA7_MODE_ENCODE_H264
, V4L2_PIX_FMT_YUV420
, V4L2_PIX_FMT_H264
, 1280, 720),
126 CODA_CODEC(CODA7_MODE_ENCODE_MP4
, V4L2_PIX_FMT_YUV420
, V4L2_PIX_FMT_MPEG4
, 1280, 720),
127 CODA_CODEC(CODA7_MODE_ENCODE_MJPG
, V4L2_PIX_FMT_YUV420
, V4L2_PIX_FMT_JPEG
, 8192, 8192),
128 CODA_CODEC(CODA7_MODE_DECODE_H264
, V4L2_PIX_FMT_H264
, V4L2_PIX_FMT_YUV420
, 1920, 1088),
129 CODA_CODEC(CODA7_MODE_DECODE_MP4
, V4L2_PIX_FMT_MPEG4
, V4L2_PIX_FMT_YUV420
, 1920, 1088),
130 CODA_CODEC(CODA7_MODE_DECODE_MJPG
, V4L2_PIX_FMT_JPEG
, V4L2_PIX_FMT_YUV420
, 8192, 8192),
133 static const struct coda_codec coda9_codecs
[] = {
134 CODA_CODEC(CODA9_MODE_ENCODE_H264
, V4L2_PIX_FMT_YUV420
, V4L2_PIX_FMT_H264
, 1920, 1088),
135 CODA_CODEC(CODA9_MODE_ENCODE_MP4
, V4L2_PIX_FMT_YUV420
, V4L2_PIX_FMT_MPEG4
, 1920, 1088),
136 CODA_CODEC(CODA9_MODE_DECODE_H264
, V4L2_PIX_FMT_H264
, V4L2_PIX_FMT_YUV420
, 1920, 1088),
137 CODA_CODEC(CODA9_MODE_DECODE_MP4
, V4L2_PIX_FMT_MPEG4
, V4L2_PIX_FMT_YUV420
, 1920, 1088),
140 struct coda_video_device
{
142 enum coda_inst_type type
;
143 const struct coda_context_ops
*ops
;
145 u32 src_formats
[CODA_MAX_FORMATS
];
146 u32 dst_formats
[CODA_MAX_FORMATS
];
149 static const struct coda_video_device coda_bit_encoder
= {
150 .name
= "coda-encoder",
151 .type
= CODA_INST_ENCODER
,
152 .ops
= &coda_bit_encode_ops
,
164 static const struct coda_video_device coda_bit_jpeg_encoder
= {
165 .name
= "coda-jpeg-encoder",
166 .type
= CODA_INST_ENCODER
,
167 .ops
= &coda_bit_encode_ops
,
172 V4L2_PIX_FMT_YUV422P
,
179 static const struct coda_video_device coda_bit_decoder
= {
180 .name
= "coda-decoder",
181 .type
= CODA_INST_DECODER
,
182 .ops
= &coda_bit_decode_ops
,
194 static const struct coda_video_device coda_bit_jpeg_decoder
= {
195 .name
= "coda-jpeg-decoder",
196 .type
= CODA_INST_DECODER
,
197 .ops
= &coda_bit_decode_ops
,
205 V4L2_PIX_FMT_YUV422P
,
209 static const struct coda_video_device
*codadx6_video_devices
[] = {
213 static const struct coda_video_device
*coda7_video_devices
[] = {
214 &coda_bit_jpeg_encoder
,
215 &coda_bit_jpeg_decoder
,
220 static const struct coda_video_device
*coda9_video_devices
[] = {
226 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
229 static u32
coda_format_normalize_yuv(u32 fourcc
)
232 case V4L2_PIX_FMT_YUV420
:
233 case V4L2_PIX_FMT_YVU420
:
234 case V4L2_PIX_FMT_NV12
:
235 case V4L2_PIX_FMT_YUV422P
:
236 return V4L2_PIX_FMT_YUV420
;
242 static const struct coda_codec
*coda_find_codec(struct coda_dev
*dev
,
243 int src_fourcc
, int dst_fourcc
)
245 const struct coda_codec
*codecs
= dev
->devtype
->codecs
;
246 int num_codecs
= dev
->devtype
->num_codecs
;
249 src_fourcc
= coda_format_normalize_yuv(src_fourcc
);
250 dst_fourcc
= coda_format_normalize_yuv(dst_fourcc
);
251 if (src_fourcc
== dst_fourcc
)
254 for (k
= 0; k
< num_codecs
; k
++) {
255 if (codecs
[k
].src_fourcc
== src_fourcc
&&
256 codecs
[k
].dst_fourcc
== dst_fourcc
)
266 static void coda_get_max_dimensions(struct coda_dev
*dev
,
267 const struct coda_codec
*codec
,
268 int *max_w
, int *max_h
)
270 const struct coda_codec
*codecs
= dev
->devtype
->codecs
;
271 int num_codecs
= dev
->devtype
->num_codecs
;
279 for (k
= 0, w
= 0, h
= 0; k
< num_codecs
; k
++) {
280 w
= max(w
, codecs
[k
].max_w
);
281 h
= max(h
, codecs
[k
].max_h
);
291 const struct coda_video_device
*to_coda_video_device(struct video_device
*vdev
)
293 struct coda_dev
*dev
= video_get_drvdata(vdev
);
294 unsigned int i
= vdev
- dev
->vfd
;
296 if (i
>= dev
->devtype
->num_vdevs
)
299 return dev
->devtype
->vdevs
[i
];
302 const char *coda_product_name(int product
)
314 snprintf(buf
, sizeof(buf
), "(0x%04x)", product
);
320 * V4L2 ioctl() operations.
322 static int coda_querycap(struct file
*file
, void *priv
,
323 struct v4l2_capability
*cap
)
325 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
327 strlcpy(cap
->driver
, CODA_NAME
, sizeof(cap
->driver
));
328 strlcpy(cap
->card
, coda_product_name(ctx
->dev
->devtype
->product
),
330 strlcpy(cap
->bus_info
, "platform:" CODA_NAME
, sizeof(cap
->bus_info
));
331 cap
->device_caps
= V4L2_CAP_VIDEO_M2M
| V4L2_CAP_STREAMING
;
332 cap
->capabilities
= cap
->device_caps
| V4L2_CAP_DEVICE_CAPS
;
337 static int coda_enum_fmt(struct file
*file
, void *priv
,
338 struct v4l2_fmtdesc
*f
)
340 struct video_device
*vdev
= video_devdata(file
);
341 const struct coda_video_device
*cvd
= to_coda_video_device(vdev
);
344 if (f
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
)
345 formats
= cvd
->src_formats
;
346 else if (f
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
347 formats
= cvd
->dst_formats
;
351 if (f
->index
>= CODA_MAX_FORMATS
|| formats
[f
->index
] == 0)
354 f
->pixelformat
= formats
[f
->index
];
359 static int coda_g_fmt(struct file
*file
, void *priv
,
360 struct v4l2_format
*f
)
362 struct coda_q_data
*q_data
;
363 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
365 q_data
= get_q_data(ctx
, f
->type
);
369 f
->fmt
.pix
.field
= V4L2_FIELD_NONE
;
370 f
->fmt
.pix
.pixelformat
= q_data
->fourcc
;
371 f
->fmt
.pix
.width
= q_data
->width
;
372 f
->fmt
.pix
.height
= q_data
->height
;
373 f
->fmt
.pix
.bytesperline
= q_data
->bytesperline
;
375 f
->fmt
.pix
.sizeimage
= q_data
->sizeimage
;
376 if (f
->fmt
.pix
.pixelformat
== V4L2_PIX_FMT_JPEG
)
377 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_JPEG
;
379 f
->fmt
.pix
.colorspace
= ctx
->colorspace
;
384 static int coda_try_pixelformat(struct coda_ctx
*ctx
, struct v4l2_format
*f
)
386 struct coda_q_data
*q_data
;
390 if (f
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
)
391 formats
= ctx
->cvd
->src_formats
;
392 else if (f
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
393 formats
= ctx
->cvd
->dst_formats
;
397 for (i
= 0; i
< CODA_MAX_FORMATS
; i
++) {
398 if (formats
[i
] == f
->fmt
.pix
.pixelformat
) {
399 f
->fmt
.pix
.pixelformat
= formats
[i
];
404 /* Fall back to currently set pixelformat */
405 q_data
= get_q_data(ctx
, f
->type
);
406 f
->fmt
.pix
.pixelformat
= q_data
->fourcc
;
411 static unsigned int coda_estimate_sizeimage(struct coda_ctx
*ctx
, u32 sizeimage
,
412 u32 width
, u32 height
)
415 * This is a rough estimate for sensible compressed buffer
416 * sizes (between 1 and 16 bits per pixel). This could be
417 * improved by better format specific worst case estimates.
419 return round_up(clamp(sizeimage
, width
* height
/ 8,
420 width
* height
* 2), PAGE_SIZE
);
423 static int coda_try_fmt(struct coda_ctx
*ctx
, const struct coda_codec
*codec
,
424 struct v4l2_format
*f
)
426 struct coda_dev
*dev
= ctx
->dev
;
427 unsigned int max_w
, max_h
;
428 enum v4l2_field field
;
430 field
= f
->fmt
.pix
.field
;
431 if (field
== V4L2_FIELD_ANY
)
432 field
= V4L2_FIELD_NONE
;
433 else if (V4L2_FIELD_NONE
!= field
)
436 /* V4L2 specification suggests the driver corrects the format struct
437 * if any of the dimensions is unsupported */
438 f
->fmt
.pix
.field
= field
;
440 coda_get_max_dimensions(dev
, codec
, &max_w
, &max_h
);
441 v4l_bound_align_image(&f
->fmt
.pix
.width
, MIN_W
, max_w
, W_ALIGN
,
442 &f
->fmt
.pix
.height
, MIN_H
, max_h
, H_ALIGN
,
445 switch (f
->fmt
.pix
.pixelformat
) {
446 case V4L2_PIX_FMT_YUV420
:
447 case V4L2_PIX_FMT_YVU420
:
448 case V4L2_PIX_FMT_NV12
:
450 * Frame stride must be at least multiple of 8,
451 * but multiple of 16 for h.264 or JPEG 4:2:x
453 f
->fmt
.pix
.bytesperline
= round_up(f
->fmt
.pix
.width
, 16);
454 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.bytesperline
*
455 f
->fmt
.pix
.height
* 3 / 2;
457 case V4L2_PIX_FMT_YUV422P
:
458 f
->fmt
.pix
.bytesperline
= round_up(f
->fmt
.pix
.width
, 16);
459 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.bytesperline
*
460 f
->fmt
.pix
.height
* 2;
462 case V4L2_PIX_FMT_JPEG
:
463 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_JPEG
;
465 case V4L2_PIX_FMT_H264
:
466 case V4L2_PIX_FMT_MPEG4
:
467 f
->fmt
.pix
.bytesperline
= 0;
468 f
->fmt
.pix
.sizeimage
= coda_estimate_sizeimage(ctx
,
469 f
->fmt
.pix
.sizeimage
,
480 static int coda_try_fmt_vid_cap(struct file
*file
, void *priv
,
481 struct v4l2_format
*f
)
483 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
484 const struct coda_q_data
*q_data_src
;
485 const struct coda_codec
*codec
;
486 struct vb2_queue
*src_vq
;
489 ret
= coda_try_pixelformat(ctx
, f
);
493 q_data_src
= get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_OUTPUT
);
496 * If the source format is already fixed, only allow the same output
499 src_vq
= v4l2_m2m_get_vq(ctx
->fh
.m2m_ctx
, V4L2_BUF_TYPE_VIDEO_OUTPUT
);
500 if (vb2_is_streaming(src_vq
)) {
501 f
->fmt
.pix
.width
= q_data_src
->width
;
502 f
->fmt
.pix
.height
= q_data_src
->height
;
505 f
->fmt
.pix
.colorspace
= ctx
->colorspace
;
507 q_data_src
= get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_OUTPUT
);
508 codec
= coda_find_codec(ctx
->dev
, q_data_src
->fourcc
,
509 f
->fmt
.pix
.pixelformat
);
513 ret
= coda_try_fmt(ctx
, codec
, f
);
517 /* The h.264 decoder only returns complete 16x16 macroblocks */
518 if (codec
&& codec
->src_fourcc
== V4L2_PIX_FMT_H264
) {
519 f
->fmt
.pix
.width
= f
->fmt
.pix
.width
;
520 f
->fmt
.pix
.height
= round_up(f
->fmt
.pix
.height
, 16);
521 f
->fmt
.pix
.bytesperline
= round_up(f
->fmt
.pix
.width
, 16);
522 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.bytesperline
*
523 f
->fmt
.pix
.height
* 3 / 2;
529 static int coda_try_fmt_vid_out(struct file
*file
, void *priv
,
530 struct v4l2_format
*f
)
532 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
533 struct coda_dev
*dev
= ctx
->dev
;
534 const struct coda_q_data
*q_data_dst
;
535 const struct coda_codec
*codec
;
538 ret
= coda_try_pixelformat(ctx
, f
);
542 switch (f
->fmt
.pix
.colorspace
) {
543 case V4L2_COLORSPACE_REC709
:
544 case V4L2_COLORSPACE_JPEG
:
547 if (f
->fmt
.pix
.pixelformat
== V4L2_PIX_FMT_JPEG
)
548 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_JPEG
;
550 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_REC709
;
553 q_data_dst
= get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_CAPTURE
);
554 codec
= coda_find_codec(dev
, f
->fmt
.pix
.pixelformat
, q_data_dst
->fourcc
);
556 return coda_try_fmt(ctx
, codec
, f
);
559 static int coda_s_fmt(struct coda_ctx
*ctx
, struct v4l2_format
*f
)
561 struct coda_q_data
*q_data
;
562 struct vb2_queue
*vq
;
564 vq
= v4l2_m2m_get_vq(ctx
->fh
.m2m_ctx
, f
->type
);
568 q_data
= get_q_data(ctx
, f
->type
);
572 if (vb2_is_busy(vq
)) {
573 v4l2_err(&ctx
->dev
->v4l2_dev
, "%s queue busy\n", __func__
);
577 q_data
->fourcc
= f
->fmt
.pix
.pixelformat
;
578 q_data
->width
= f
->fmt
.pix
.width
;
579 q_data
->height
= f
->fmt
.pix
.height
;
580 q_data
->bytesperline
= f
->fmt
.pix
.bytesperline
;
581 q_data
->sizeimage
= f
->fmt
.pix
.sizeimage
;
582 q_data
->rect
.left
= 0;
583 q_data
->rect
.top
= 0;
584 q_data
->rect
.width
= f
->fmt
.pix
.width
;
585 q_data
->rect
.height
= f
->fmt
.pix
.height
;
587 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
588 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
589 f
->type
, q_data
->width
, q_data
->height
, q_data
->fourcc
);
594 static int coda_s_fmt_vid_cap(struct file
*file
, void *priv
,
595 struct v4l2_format
*f
)
597 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
600 ret
= coda_try_fmt_vid_cap(file
, priv
, f
);
604 return coda_s_fmt(ctx
, f
);
607 static int coda_s_fmt_vid_out(struct file
*file
, void *priv
,
608 struct v4l2_format
*f
)
610 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
611 struct v4l2_format f_cap
;
614 ret
= coda_try_fmt_vid_out(file
, priv
, f
);
618 ret
= coda_s_fmt(ctx
, f
);
622 ctx
->colorspace
= f
->fmt
.pix
.colorspace
;
624 memset(&f_cap
, 0, sizeof(f_cap
));
625 f_cap
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
626 coda_g_fmt(file
, priv
, &f_cap
);
627 f_cap
.fmt
.pix
.width
= f
->fmt
.pix
.width
;
628 f_cap
.fmt
.pix
.height
= f
->fmt
.pix
.height
;
630 ret
= coda_try_fmt_vid_cap(file
, priv
, &f_cap
);
634 return coda_s_fmt(ctx
, &f_cap
);
637 static int coda_reqbufs(struct file
*file
, void *priv
,
638 struct v4l2_requestbuffers
*rb
)
640 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
643 ret
= v4l2_m2m_reqbufs(file
, ctx
->fh
.m2m_ctx
, rb
);
648 * Allow to allocate instance specific per-context buffers, such as
649 * bitstream ringbuffer, slice buffer, work buffer, etc. if needed.
651 if (rb
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
&& ctx
->ops
->reqbufs
)
652 return ctx
->ops
->reqbufs(ctx
, rb
);
657 static int coda_qbuf(struct file
*file
, void *priv
,
658 struct v4l2_buffer
*buf
)
660 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
662 return v4l2_m2m_qbuf(file
, ctx
->fh
.m2m_ctx
, buf
);
665 static bool coda_buf_is_end_of_stream(struct coda_ctx
*ctx
,
666 struct vb2_buffer
*buf
)
668 struct vb2_queue
*src_vq
;
670 src_vq
= v4l2_m2m_get_vq(ctx
->fh
.m2m_ctx
, V4L2_BUF_TYPE_VIDEO_OUTPUT
);
672 return ((ctx
->bit_stream_param
& CODA_BIT_STREAM_END_FLAG
) &&
673 (buf
->v4l2_buf
.sequence
== (ctx
->qsequence
- 1)));
676 void coda_m2m_buf_done(struct coda_ctx
*ctx
, struct vb2_buffer
*buf
,
677 enum vb2_buffer_state state
)
679 const struct v4l2_event eos_event
= {
680 .type
= V4L2_EVENT_EOS
683 if (coda_buf_is_end_of_stream(ctx
, buf
)) {
684 buf
->v4l2_buf
.flags
|= V4L2_BUF_FLAG_LAST
;
686 v4l2_event_queue_fh(&ctx
->fh
, &eos_event
);
689 v4l2_m2m_buf_done(buf
, state
);
692 static int coda_g_selection(struct file
*file
, void *fh
,
693 struct v4l2_selection
*s
)
695 struct coda_ctx
*ctx
= fh_to_ctx(fh
);
696 struct coda_q_data
*q_data
;
697 struct v4l2_rect r
, *rsel
;
699 q_data
= get_q_data(ctx
, s
->type
);
705 r
.width
= q_data
->width
;
706 r
.height
= q_data
->height
;
707 rsel
= &q_data
->rect
;
710 case V4L2_SEL_TGT_CROP_DEFAULT
:
711 case V4L2_SEL_TGT_CROP_BOUNDS
:
714 case V4L2_SEL_TGT_CROP
:
715 if (s
->type
!= V4L2_BUF_TYPE_VIDEO_OUTPUT
)
718 case V4L2_SEL_TGT_COMPOSE_BOUNDS
:
719 case V4L2_SEL_TGT_COMPOSE_PADDED
:
722 case V4L2_SEL_TGT_COMPOSE
:
723 case V4L2_SEL_TGT_COMPOSE_DEFAULT
:
724 if (s
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
736 static int coda_try_decoder_cmd(struct file
*file
, void *fh
,
737 struct v4l2_decoder_cmd
*dc
)
739 if (dc
->cmd
!= V4L2_DEC_CMD_STOP
)
742 if (dc
->flags
& V4L2_DEC_CMD_STOP_TO_BLACK
)
745 if (!(dc
->flags
& V4L2_DEC_CMD_STOP_IMMEDIATELY
) && (dc
->stop
.pts
!= 0))
751 static int coda_decoder_cmd(struct file
*file
, void *fh
,
752 struct v4l2_decoder_cmd
*dc
)
754 struct coda_ctx
*ctx
= fh_to_ctx(fh
);
757 ret
= coda_try_decoder_cmd(file
, fh
, dc
);
761 /* Ignore decoder stop command silently in encoder context */
762 if (ctx
->inst_type
!= CODA_INST_DECODER
)
765 /* Set the stream-end flag on this context */
766 coda_bit_stream_end_flag(ctx
);
768 v4l2_m2m_try_schedule(ctx
->fh
.m2m_ctx
);
773 static int coda_subscribe_event(struct v4l2_fh
*fh
,
774 const struct v4l2_event_subscription
*sub
)
778 return v4l2_event_subscribe(fh
, sub
, 0, NULL
);
780 return v4l2_ctrl_subscribe_event(fh
, sub
);
784 static const struct v4l2_ioctl_ops coda_ioctl_ops
= {
785 .vidioc_querycap
= coda_querycap
,
787 .vidioc_enum_fmt_vid_cap
= coda_enum_fmt
,
788 .vidioc_g_fmt_vid_cap
= coda_g_fmt
,
789 .vidioc_try_fmt_vid_cap
= coda_try_fmt_vid_cap
,
790 .vidioc_s_fmt_vid_cap
= coda_s_fmt_vid_cap
,
792 .vidioc_enum_fmt_vid_out
= coda_enum_fmt
,
793 .vidioc_g_fmt_vid_out
= coda_g_fmt
,
794 .vidioc_try_fmt_vid_out
= coda_try_fmt_vid_out
,
795 .vidioc_s_fmt_vid_out
= coda_s_fmt_vid_out
,
797 .vidioc_reqbufs
= coda_reqbufs
,
798 .vidioc_querybuf
= v4l2_m2m_ioctl_querybuf
,
800 .vidioc_qbuf
= coda_qbuf
,
801 .vidioc_expbuf
= v4l2_m2m_ioctl_expbuf
,
802 .vidioc_dqbuf
= v4l2_m2m_ioctl_dqbuf
,
803 .vidioc_create_bufs
= v4l2_m2m_ioctl_create_bufs
,
805 .vidioc_streamon
= v4l2_m2m_ioctl_streamon
,
806 .vidioc_streamoff
= v4l2_m2m_ioctl_streamoff
,
808 .vidioc_g_selection
= coda_g_selection
,
810 .vidioc_try_decoder_cmd
= coda_try_decoder_cmd
,
811 .vidioc_decoder_cmd
= coda_decoder_cmd
,
813 .vidioc_subscribe_event
= coda_subscribe_event
,
814 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
817 void coda_set_gdi_regs(struct coda_ctx
*ctx
)
819 struct gdi_tiled_map
*tiled_map
= &ctx
->tiled_map
;
820 struct coda_dev
*dev
= ctx
->dev
;
823 for (i
= 0; i
< 16; i
++)
824 coda_write(dev
, tiled_map
->xy2ca_map
[i
],
825 CODA9_GDI_XY2_CAS_0
+ 4 * i
);
826 for (i
= 0; i
< 4; i
++)
827 coda_write(dev
, tiled_map
->xy2ba_map
[i
],
828 CODA9_GDI_XY2_BA_0
+ 4 * i
);
829 for (i
= 0; i
< 16; i
++)
830 coda_write(dev
, tiled_map
->xy2ra_map
[i
],
831 CODA9_GDI_XY2_RAS_0
+ 4 * i
);
832 coda_write(dev
, tiled_map
->xy2rbc_config
, CODA9_GDI_XY2_RBC_CONFIG
);
833 for (i
= 0; i
< 32; i
++)
834 coda_write(dev
, tiled_map
->rbc2axi_map
[i
],
835 CODA9_GDI_RBC2_AXI_0
+ 4 * i
);
839 * Mem-to-mem operations.
842 static void coda_device_run(void *m2m_priv
)
844 struct coda_ctx
*ctx
= m2m_priv
;
845 struct coda_dev
*dev
= ctx
->dev
;
847 queue_work(dev
->workqueue
, &ctx
->pic_run_work
);
850 static void coda_pic_run_work(struct work_struct
*work
)
852 struct coda_ctx
*ctx
= container_of(work
, struct coda_ctx
, pic_run_work
);
853 struct coda_dev
*dev
= ctx
->dev
;
856 mutex_lock(&ctx
->buffer_mutex
);
857 mutex_lock(&dev
->coda_mutex
);
859 ret
= ctx
->ops
->prepare_run(ctx
);
860 if (ret
< 0 && ctx
->inst_type
== CODA_INST_DECODER
) {
861 mutex_unlock(&dev
->coda_mutex
);
862 mutex_unlock(&ctx
->buffer_mutex
);
863 /* job_finish scheduled by prepare_decode */
867 if (!wait_for_completion_timeout(&ctx
->completion
,
868 msecs_to_jiffies(1000))) {
869 dev_err(&dev
->plat_dev
->dev
, "CODA PIC_RUN timeout\n");
874 } else if (!ctx
->aborting
) {
875 ctx
->ops
->finish_run(ctx
);
878 if ((ctx
->aborting
|| (!ctx
->streamon_cap
&& !ctx
->streamon_out
)) &&
879 ctx
->ops
->seq_end_work
)
880 queue_work(dev
->workqueue
, &ctx
->seq_end_work
);
882 mutex_unlock(&dev
->coda_mutex
);
883 mutex_unlock(&ctx
->buffer_mutex
);
885 v4l2_m2m_job_finish(ctx
->dev
->m2m_dev
, ctx
->fh
.m2m_ctx
);
888 static int coda_job_ready(void *m2m_priv
)
890 struct coda_ctx
*ctx
= m2m_priv
;
891 int src_bufs
= v4l2_m2m_num_src_bufs_ready(ctx
->fh
.m2m_ctx
);
894 * For both 'P' and 'key' frame cases 1 picture
895 * and 1 frame are needed. In the decoder case,
896 * the compressed frame can be in the bitstream.
898 if (!src_bufs
&& ctx
->inst_type
!= CODA_INST_DECODER
) {
899 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
900 "not ready: not enough video buffers.\n");
904 if (!v4l2_m2m_num_dst_bufs_ready(ctx
->fh
.m2m_ctx
)) {
905 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
906 "not ready: not enough video capture buffers.\n");
910 if (ctx
->inst_type
== CODA_INST_DECODER
&& ctx
->use_bit
) {
911 struct list_head
*meta
;
915 if (ctx
->hold
&& !src_bufs
) {
916 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
917 "%d: not ready: on hold for more buffers.\n",
922 stream_end
= ctx
->bit_stream_param
&
923 CODA_BIT_STREAM_END_FLAG
;
926 list_for_each(meta
, &ctx
->buffer_meta_list
)
929 if (!stream_end
&& (num_metas
+ src_bufs
) < 2) {
930 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
931 "%d: not ready: need 2 buffers available (%d, %d)\n",
932 ctx
->idx
, num_metas
, src_bufs
);
937 if (!src_bufs
&& !stream_end
&&
938 (coda_get_bitstream_payload(ctx
) < 512)) {
939 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
940 "%d: not ready: not enough bitstream data (%d).\n",
941 ctx
->idx
, coda_get_bitstream_payload(ctx
));
947 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
948 "not ready: aborting\n");
952 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
957 static void coda_job_abort(void *priv
)
959 struct coda_ctx
*ctx
= priv
;
963 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
967 static void coda_lock(void *m2m_priv
)
969 struct coda_ctx
*ctx
= m2m_priv
;
970 struct coda_dev
*pcdev
= ctx
->dev
;
972 mutex_lock(&pcdev
->dev_mutex
);
975 static void coda_unlock(void *m2m_priv
)
977 struct coda_ctx
*ctx
= m2m_priv
;
978 struct coda_dev
*pcdev
= ctx
->dev
;
980 mutex_unlock(&pcdev
->dev_mutex
);
983 static const struct v4l2_m2m_ops coda_m2m_ops
= {
984 .device_run
= coda_device_run
,
985 .job_ready
= coda_job_ready
,
986 .job_abort
= coda_job_abort
,
988 .unlock
= coda_unlock
,
991 static void coda_set_tiled_map_type(struct coda_ctx
*ctx
, int tiled_map_type
)
993 struct gdi_tiled_map
*tiled_map
= &ctx
->tiled_map
;
994 int luma_map
, chro_map
, i
;
996 memset(tiled_map
, 0, sizeof(*tiled_map
));
1000 tiled_map
->map_type
= tiled_map_type
;
1001 for (i
= 0; i
< 16; i
++)
1002 tiled_map
->xy2ca_map
[i
] = luma_map
<< 8 | chro_map
;
1003 for (i
= 0; i
< 4; i
++)
1004 tiled_map
->xy2ba_map
[i
] = luma_map
<< 8 | chro_map
;
1005 for (i
= 0; i
< 16; i
++)
1006 tiled_map
->xy2ra_map
[i
] = luma_map
<< 8 | chro_map
;
1008 if (tiled_map_type
== GDI_LINEAR_FRAME_MAP
) {
1009 tiled_map
->xy2rbc_config
= 0;
1011 dev_err(&ctx
->dev
->plat_dev
->dev
, "invalid map type: %d\n",
1017 static void set_default_params(struct coda_ctx
*ctx
)
1019 unsigned int max_w
, max_h
, usize
, csize
;
1021 ctx
->codec
= coda_find_codec(ctx
->dev
, ctx
->cvd
->src_formats
[0],
1022 ctx
->cvd
->dst_formats
[0]);
1023 max_w
= min(ctx
->codec
->max_w
, 1920U);
1024 max_h
= min(ctx
->codec
->max_h
, 1088U);
1025 usize
= max_w
* max_h
* 3 / 2;
1026 csize
= coda_estimate_sizeimage(ctx
, usize
, max_w
, max_h
);
1028 ctx
->params
.codec_mode
= ctx
->codec
->mode
;
1029 ctx
->colorspace
= V4L2_COLORSPACE_REC709
;
1030 ctx
->params
.framerate
= 30;
1032 /* Default formats for output and input queues */
1033 ctx
->q_data
[V4L2_M2M_SRC
].fourcc
= ctx
->codec
->src_fourcc
;
1034 ctx
->q_data
[V4L2_M2M_DST
].fourcc
= ctx
->codec
->dst_fourcc
;
1035 ctx
->q_data
[V4L2_M2M_SRC
].width
= max_w
;
1036 ctx
->q_data
[V4L2_M2M_SRC
].height
= max_h
;
1037 ctx
->q_data
[V4L2_M2M_DST
].width
= max_w
;
1038 ctx
->q_data
[V4L2_M2M_DST
].height
= max_h
;
1039 if (ctx
->codec
->src_fourcc
== V4L2_PIX_FMT_YUV420
) {
1040 ctx
->q_data
[V4L2_M2M_SRC
].bytesperline
= max_w
;
1041 ctx
->q_data
[V4L2_M2M_SRC
].sizeimage
= usize
;
1042 ctx
->q_data
[V4L2_M2M_DST
].bytesperline
= 0;
1043 ctx
->q_data
[V4L2_M2M_DST
].sizeimage
= csize
;
1045 ctx
->q_data
[V4L2_M2M_SRC
].bytesperline
= 0;
1046 ctx
->q_data
[V4L2_M2M_SRC
].sizeimage
= csize
;
1047 ctx
->q_data
[V4L2_M2M_DST
].bytesperline
= max_w
;
1048 ctx
->q_data
[V4L2_M2M_DST
].sizeimage
= usize
;
1050 ctx
->q_data
[V4L2_M2M_SRC
].rect
.width
= max_w
;
1051 ctx
->q_data
[V4L2_M2M_SRC
].rect
.height
= max_h
;
1052 ctx
->q_data
[V4L2_M2M_DST
].rect
.width
= max_w
;
1053 ctx
->q_data
[V4L2_M2M_DST
].rect
.height
= max_h
;
1055 if (ctx
->dev
->devtype
->product
== CODA_960
)
1056 coda_set_tiled_map_type(ctx
, GDI_LINEAR_FRAME_MAP
);
1062 static int coda_queue_setup(struct vb2_queue
*vq
,
1063 const struct v4l2_format
*fmt
,
1064 unsigned int *nbuffers
, unsigned int *nplanes
,
1065 unsigned int sizes
[], void *alloc_ctxs
[])
1067 struct coda_ctx
*ctx
= vb2_get_drv_priv(vq
);
1068 struct coda_q_data
*q_data
;
1071 q_data
= get_q_data(ctx
, vq
->type
);
1072 size
= q_data
->sizeimage
;
1077 /* Set to vb2-dma-contig allocator context, ignored by vb2-vmalloc */
1078 alloc_ctxs
[0] = ctx
->dev
->alloc_ctx
;
1080 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
1081 "get %d buffer(s) of size %d each.\n", *nbuffers
, size
);
1086 static int coda_buf_prepare(struct vb2_buffer
*vb
)
1088 struct coda_ctx
*ctx
= vb2_get_drv_priv(vb
->vb2_queue
);
1089 struct coda_q_data
*q_data
;
1091 q_data
= get_q_data(ctx
, vb
->vb2_queue
->type
);
1093 if (vb2_plane_size(vb
, 0) < q_data
->sizeimage
) {
1094 v4l2_warn(&ctx
->dev
->v4l2_dev
,
1095 "%s data will not fit into plane (%lu < %lu)\n",
1096 __func__
, vb2_plane_size(vb
, 0),
1097 (long)q_data
->sizeimage
);
1104 static void coda_buf_queue(struct vb2_buffer
*vb
)
1106 struct coda_ctx
*ctx
= vb2_get_drv_priv(vb
->vb2_queue
);
1107 struct vb2_queue
*vq
= vb
->vb2_queue
;
1108 struct coda_q_data
*q_data
;
1110 q_data
= get_q_data(ctx
, vb
->vb2_queue
->type
);
1113 * In the decoder case, immediately try to copy the buffer into the
1114 * bitstream ringbuffer and mark it as ready to be dequeued.
1116 if (ctx
->bitstream
.size
&& vq
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
) {
1118 * For backwards compatibility, queuing an empty buffer marks
1121 if (vb2_get_plane_payload(vb
, 0) == 0)
1122 coda_bit_stream_end_flag(ctx
);
1123 mutex_lock(&ctx
->bitstream_mutex
);
1124 v4l2_m2m_buf_queue(ctx
->fh
.m2m_ctx
, vb
);
1125 if (vb2_is_streaming(vb
->vb2_queue
))
1126 coda_fill_bitstream(ctx
, true);
1127 mutex_unlock(&ctx
->bitstream_mutex
);
1129 v4l2_m2m_buf_queue(ctx
->fh
.m2m_ctx
, vb
);
1133 int coda_alloc_aux_buf(struct coda_dev
*dev
, struct coda_aux_buf
*buf
,
1134 size_t size
, const char *name
, struct dentry
*parent
)
1136 buf
->vaddr
= dma_alloc_coherent(&dev
->plat_dev
->dev
, size
, &buf
->paddr
,
1139 v4l2_err(&dev
->v4l2_dev
,
1140 "Failed to allocate %s buffer of size %u\n",
1147 if (name
&& parent
) {
1148 buf
->blob
.data
= buf
->vaddr
;
1149 buf
->blob
.size
= size
;
1150 buf
->dentry
= debugfs_create_blob(name
, 0644, parent
,
1153 dev_warn(&dev
->plat_dev
->dev
,
1154 "failed to create debugfs entry %s\n", name
);
1160 void coda_free_aux_buf(struct coda_dev
*dev
,
1161 struct coda_aux_buf
*buf
)
1164 dma_free_coherent(&dev
->plat_dev
->dev
, buf
->size
,
1165 buf
->vaddr
, buf
->paddr
);
1168 debugfs_remove(buf
->dentry
);
1173 static int coda_start_streaming(struct vb2_queue
*q
, unsigned int count
)
1175 struct coda_ctx
*ctx
= vb2_get_drv_priv(q
);
1176 struct v4l2_device
*v4l2_dev
= &ctx
->dev
->v4l2_dev
;
1177 struct coda_q_data
*q_data_src
, *q_data_dst
;
1178 struct vb2_buffer
*buf
;
1181 q_data_src
= get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_OUTPUT
);
1182 if (q
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
) {
1183 if (ctx
->inst_type
== CODA_INST_DECODER
&& ctx
->use_bit
) {
1184 /* copy the buffers that were queued before streamon */
1185 mutex_lock(&ctx
->bitstream_mutex
);
1186 coda_fill_bitstream(ctx
, false);
1187 mutex_unlock(&ctx
->bitstream_mutex
);
1189 if (coda_get_bitstream_payload(ctx
) < 512) {
1200 ctx
->streamon_out
= 1;
1207 ctx
->streamon_cap
= 1;
1210 /* Don't start the coda unless both queues are on */
1211 if (!(ctx
->streamon_out
& ctx
->streamon_cap
))
1214 q_data_dst
= get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_CAPTURE
);
1215 if ((q_data_src
->width
!= q_data_dst
->width
&&
1216 round_up(q_data_src
->width
, 16) != q_data_dst
->width
) ||
1217 (q_data_src
->height
!= q_data_dst
->height
&&
1218 round_up(q_data_src
->height
, 16) != q_data_dst
->height
)) {
1219 v4l2_err(v4l2_dev
, "can't convert %dx%d to %dx%d\n",
1220 q_data_src
->width
, q_data_src
->height
,
1221 q_data_dst
->width
, q_data_dst
->height
);
1226 /* Allow BIT decoder device_run with no new buffers queued */
1227 if (ctx
->inst_type
== CODA_INST_DECODER
&& ctx
->use_bit
)
1228 v4l2_m2m_set_src_buffered(ctx
->fh
.m2m_ctx
, true);
1230 ctx
->gopcounter
= ctx
->params
.gop_size
- 1;
1232 ctx
->codec
= coda_find_codec(ctx
->dev
, q_data_src
->fourcc
,
1233 q_data_dst
->fourcc
);
1235 v4l2_err(v4l2_dev
, "couldn't tell instance type.\n");
1240 if (q_data_dst
->fourcc
== V4L2_PIX_FMT_JPEG
)
1241 ctx
->params
.gop_size
= 1;
1242 ctx
->gopcounter
= ctx
->params
.gop_size
- 1;
1244 ret
= ctx
->ops
->start_streaming(ctx
);
1245 if (ctx
->inst_type
== CODA_INST_DECODER
) {
1255 if (q
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
) {
1256 while ((buf
= v4l2_m2m_src_buf_remove(ctx
->fh
.m2m_ctx
)))
1257 v4l2_m2m_buf_done(buf
, VB2_BUF_STATE_QUEUED
);
1259 while ((buf
= v4l2_m2m_dst_buf_remove(ctx
->fh
.m2m_ctx
)))
1260 v4l2_m2m_buf_done(buf
, VB2_BUF_STATE_QUEUED
);
1265 static void coda_stop_streaming(struct vb2_queue
*q
)
1267 struct coda_ctx
*ctx
= vb2_get_drv_priv(q
);
1268 struct coda_dev
*dev
= ctx
->dev
;
1269 struct vb2_buffer
*buf
;
1272 stop
= ctx
->streamon_out
&& ctx
->streamon_cap
;
1274 if (q
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
) {
1275 v4l2_dbg(1, coda_debug
, &dev
->v4l2_dev
,
1276 "%s: output\n", __func__
);
1277 ctx
->streamon_out
= 0;
1279 coda_bit_stream_end_flag(ctx
);
1283 while ((buf
= v4l2_m2m_src_buf_remove(ctx
->fh
.m2m_ctx
)))
1284 v4l2_m2m_buf_done(buf
, VB2_BUF_STATE_ERROR
);
1286 v4l2_dbg(1, coda_debug
, &dev
->v4l2_dev
,
1287 "%s: capture\n", __func__
);
1288 ctx
->streamon_cap
= 0;
1291 ctx
->sequence_offset
= 0;
1293 while ((buf
= v4l2_m2m_dst_buf_remove(ctx
->fh
.m2m_ctx
)))
1294 v4l2_m2m_buf_done(buf
, VB2_BUF_STATE_ERROR
);
1298 struct coda_buffer_meta
*meta
;
1300 if (ctx
->ops
->seq_end_work
) {
1301 queue_work(dev
->workqueue
, &ctx
->seq_end_work
);
1302 flush_work(&ctx
->seq_end_work
);
1304 mutex_lock(&ctx
->bitstream_mutex
);
1305 while (!list_empty(&ctx
->buffer_meta_list
)) {
1306 meta
= list_first_entry(&ctx
->buffer_meta_list
,
1307 struct coda_buffer_meta
, list
);
1308 list_del(&meta
->list
);
1311 mutex_unlock(&ctx
->bitstream_mutex
);
1312 kfifo_init(&ctx
->bitstream_fifo
,
1313 ctx
->bitstream
.vaddr
, ctx
->bitstream
.size
);
1314 ctx
->runcounter
= 0;
1318 if (!ctx
->streamon_out
&& !ctx
->streamon_cap
)
1319 ctx
->bit_stream_param
&= ~CODA_BIT_STREAM_END_FLAG
;
1322 static const struct vb2_ops coda_qops
= {
1323 .queue_setup
= coda_queue_setup
,
1324 .buf_prepare
= coda_buf_prepare
,
1325 .buf_queue
= coda_buf_queue
,
1326 .start_streaming
= coda_start_streaming
,
1327 .stop_streaming
= coda_stop_streaming
,
1328 .wait_prepare
= vb2_ops_wait_prepare
,
1329 .wait_finish
= vb2_ops_wait_finish
,
1332 static int coda_s_ctrl(struct v4l2_ctrl
*ctrl
)
1334 struct coda_ctx
*ctx
=
1335 container_of(ctrl
->handler
, struct coda_ctx
, ctrls
);
1337 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
1338 "s_ctrl: id = %d, val = %d\n", ctrl
->id
, ctrl
->val
);
1341 case V4L2_CID_HFLIP
:
1343 ctx
->params
.rot_mode
|= CODA_MIR_HOR
;
1345 ctx
->params
.rot_mode
&= ~CODA_MIR_HOR
;
1347 case V4L2_CID_VFLIP
:
1349 ctx
->params
.rot_mode
|= CODA_MIR_VER
;
1351 ctx
->params
.rot_mode
&= ~CODA_MIR_VER
;
1353 case V4L2_CID_MPEG_VIDEO_BITRATE
:
1354 ctx
->params
.bitrate
= ctrl
->val
/ 1000;
1356 case V4L2_CID_MPEG_VIDEO_GOP_SIZE
:
1357 ctx
->params
.gop_size
= ctrl
->val
;
1359 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP
:
1360 ctx
->params
.h264_intra_qp
= ctrl
->val
;
1362 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP
:
1363 ctx
->params
.h264_inter_qp
= ctrl
->val
;
1365 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP
:
1366 ctx
->params
.h264_min_qp
= ctrl
->val
;
1368 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP
:
1369 ctx
->params
.h264_max_qp
= ctrl
->val
;
1371 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA
:
1372 ctx
->params
.h264_deblk_alpha
= ctrl
->val
;
1374 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA
:
1375 ctx
->params
.h264_deblk_beta
= ctrl
->val
;
1377 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE
:
1378 ctx
->params
.h264_deblk_enabled
= (ctrl
->val
==
1379 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED
);
1381 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP
:
1382 ctx
->params
.mpeg4_intra_qp
= ctrl
->val
;
1384 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP
:
1385 ctx
->params
.mpeg4_inter_qp
= ctrl
->val
;
1387 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE
:
1388 ctx
->params
.slice_mode
= ctrl
->val
;
1390 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB
:
1391 ctx
->params
.slice_max_mb
= ctrl
->val
;
1393 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES
:
1394 ctx
->params
.slice_max_bits
= ctrl
->val
* 8;
1396 case V4L2_CID_MPEG_VIDEO_HEADER_MODE
:
1398 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB
:
1399 ctx
->params
.intra_refresh
= ctrl
->val
;
1401 case V4L2_CID_JPEG_COMPRESSION_QUALITY
:
1402 coda_set_jpeg_compression_quality(ctx
, ctrl
->val
);
1404 case V4L2_CID_JPEG_RESTART_INTERVAL
:
1405 ctx
->params
.jpeg_restart_interval
= ctrl
->val
;
1408 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
1409 "Invalid control, id=%d, val=%d\n",
1410 ctrl
->id
, ctrl
->val
);
1417 static const struct v4l2_ctrl_ops coda_ctrl_ops
= {
1418 .s_ctrl
= coda_s_ctrl
,
1421 static void coda_encode_ctrls(struct coda_ctx
*ctx
)
1423 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1424 V4L2_CID_MPEG_VIDEO_BITRATE
, 0, 32767000, 1000, 0);
1425 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1426 V4L2_CID_MPEG_VIDEO_GOP_SIZE
, 1, 60, 1, 16);
1427 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1428 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP
, 0, 51, 1, 25);
1429 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1430 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP
, 0, 51, 1, 25);
1431 if (ctx
->dev
->devtype
->product
!= CODA_960
) {
1432 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1433 V4L2_CID_MPEG_VIDEO_H264_MIN_QP
, 0, 51, 1, 12);
1435 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1436 V4L2_CID_MPEG_VIDEO_H264_MAX_QP
, 0, 51, 1, 51);
1437 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1438 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA
, 0, 15, 1, 0);
1439 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1440 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA
, 0, 15, 1, 0);
1441 v4l2_ctrl_new_std_menu(&ctx
->ctrls
, &coda_ctrl_ops
,
1442 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE
,
1443 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED
, 0x0,
1444 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED
);
1445 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1446 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP
, 1, 31, 1, 2);
1447 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1448 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP
, 1, 31, 1, 2);
1449 v4l2_ctrl_new_std_menu(&ctx
->ctrls
, &coda_ctrl_ops
,
1450 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE
,
1451 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES
, 0x0,
1452 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE
);
1453 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1454 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB
, 1, 0x3fffffff, 1, 1);
1455 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1456 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES
, 1, 0x3fffffff, 1,
1458 v4l2_ctrl_new_std_menu(&ctx
->ctrls
, &coda_ctrl_ops
,
1459 V4L2_CID_MPEG_VIDEO_HEADER_MODE
,
1460 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME
,
1461 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE
),
1462 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME
);
1463 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1464 V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB
, 0,
1465 1920 * 1088 / 256, 1, 0);
1468 static void coda_jpeg_encode_ctrls(struct coda_ctx
*ctx
)
1470 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1471 V4L2_CID_JPEG_COMPRESSION_QUALITY
, 5, 100, 1, 50);
1472 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1473 V4L2_CID_JPEG_RESTART_INTERVAL
, 0, 100, 1, 0);
1476 static int coda_ctrls_setup(struct coda_ctx
*ctx
)
1478 v4l2_ctrl_handler_init(&ctx
->ctrls
, 2);
1480 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1481 V4L2_CID_HFLIP
, 0, 1, 1, 0);
1482 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
1483 V4L2_CID_VFLIP
, 0, 1, 1, 0);
1484 if (ctx
->inst_type
== CODA_INST_ENCODER
) {
1485 if (ctx
->cvd
->dst_formats
[0] == V4L2_PIX_FMT_JPEG
)
1486 coda_jpeg_encode_ctrls(ctx
);
1488 coda_encode_ctrls(ctx
);
1491 if (ctx
->ctrls
.error
) {
1492 v4l2_err(&ctx
->dev
->v4l2_dev
,
1493 "control initialization error (%d)",
1498 return v4l2_ctrl_handler_setup(&ctx
->ctrls
);
1501 static int coda_queue_init(struct coda_ctx
*ctx
, struct vb2_queue
*vq
)
1504 vq
->ops
= &coda_qops
;
1505 vq
->buf_struct_size
= sizeof(struct v4l2_m2m_buffer
);
1506 vq
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_COPY
;
1507 vq
->lock
= &ctx
->dev
->dev_mutex
;
1508 /* One way to indicate end-of-stream for coda is to set the
1509 * bytesused == 0. However by default videobuf2 handles bytesused
1510 * equal to 0 as a special case and changes its value to the size
1511 * of the buffer. Set the allow_zero_bytesused flag, so
1512 * that videobuf2 will keep the value of bytesused intact.
1514 vq
->allow_zero_bytesused
= 1;
1516 return vb2_queue_init(vq
);
1519 int coda_encoder_queue_init(void *priv
, struct vb2_queue
*src_vq
,
1520 struct vb2_queue
*dst_vq
)
1524 src_vq
->type
= V4L2_BUF_TYPE_VIDEO_OUTPUT
;
1525 src_vq
->io_modes
= VB2_DMABUF
| VB2_MMAP
;
1526 src_vq
->mem_ops
= &vb2_dma_contig_memops
;
1528 ret
= coda_queue_init(priv
, src_vq
);
1532 dst_vq
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1533 dst_vq
->io_modes
= VB2_DMABUF
| VB2_MMAP
;
1534 dst_vq
->mem_ops
= &vb2_dma_contig_memops
;
1536 return coda_queue_init(priv
, dst_vq
);
1539 int coda_decoder_queue_init(void *priv
, struct vb2_queue
*src_vq
,
1540 struct vb2_queue
*dst_vq
)
1544 src_vq
->type
= V4L2_BUF_TYPE_VIDEO_OUTPUT
;
1545 src_vq
->io_modes
= VB2_DMABUF
| VB2_MMAP
| VB2_USERPTR
;
1546 src_vq
->mem_ops
= &vb2_vmalloc_memops
;
1548 ret
= coda_queue_init(priv
, src_vq
);
1552 dst_vq
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1553 dst_vq
->io_modes
= VB2_DMABUF
| VB2_MMAP
;
1554 dst_vq
->mem_ops
= &vb2_dma_contig_memops
;
1556 return coda_queue_init(priv
, dst_vq
);
1559 static int coda_next_free_instance(struct coda_dev
*dev
)
1561 int idx
= ffz(dev
->instance_mask
);
1564 (dev
->devtype
->product
== CODA_DX6
&& idx
> CODADX6_MAX_INSTANCES
))
1574 static int coda_open(struct file
*file
)
1576 struct video_device
*vdev
= video_devdata(file
);
1577 struct coda_dev
*dev
= video_get_drvdata(vdev
);
1578 struct coda_ctx
*ctx
= NULL
;
1583 ctx
= kzalloc(sizeof(*ctx
), GFP_KERNEL
);
1587 idx
= coda_next_free_instance(dev
);
1592 set_bit(idx
, &dev
->instance_mask
);
1594 name
= kasprintf(GFP_KERNEL
, "context%d", idx
);
1597 goto err_coda_name_init
;
1600 ctx
->debugfs_entry
= debugfs_create_dir(name
, dev
->debugfs_root
);
1603 ctx
->cvd
= to_coda_video_device(vdev
);
1604 ctx
->inst_type
= ctx
->cvd
->type
;
1605 ctx
->ops
= ctx
->cvd
->ops
;
1606 ctx
->use_bit
= !ctx
->cvd
->direct
;
1607 init_completion(&ctx
->completion
);
1608 INIT_WORK(&ctx
->pic_run_work
, coda_pic_run_work
);
1609 if (ctx
->ops
->seq_end_work
)
1610 INIT_WORK(&ctx
->seq_end_work
, ctx
->ops
->seq_end_work
);
1611 v4l2_fh_init(&ctx
->fh
, video_devdata(file
));
1612 file
->private_data
= &ctx
->fh
;
1613 v4l2_fh_add(&ctx
->fh
);
1616 switch (dev
->devtype
->product
) {
1618 ctx
->frame_mem_ctrl
= 1 << 12;
1627 /* Power up and upload firmware if necessary */
1628 ret
= pm_runtime_get_sync(&dev
->plat_dev
->dev
);
1630 v4l2_err(&dev
->v4l2_dev
, "failed to power up: %d\n", ret
);
1634 ret
= clk_prepare_enable(dev
->clk_per
);
1638 ret
= clk_prepare_enable(dev
->clk_ahb
);
1642 set_default_params(ctx
);
1643 ctx
->fh
.m2m_ctx
= v4l2_m2m_ctx_init(dev
->m2m_dev
, ctx
,
1644 ctx
->ops
->queue_init
);
1645 if (IS_ERR(ctx
->fh
.m2m_ctx
)) {
1646 ret
= PTR_ERR(ctx
->fh
.m2m_ctx
);
1648 v4l2_err(&dev
->v4l2_dev
, "%s return error (%d)\n",
1653 ret
= coda_ctrls_setup(ctx
);
1655 v4l2_err(&dev
->v4l2_dev
, "failed to setup coda controls\n");
1656 goto err_ctrls_setup
;
1659 ctx
->fh
.ctrl_handler
= &ctx
->ctrls
;
1661 mutex_init(&ctx
->bitstream_mutex
);
1662 mutex_init(&ctx
->buffer_mutex
);
1663 INIT_LIST_HEAD(&ctx
->buffer_meta_list
);
1666 list_add(&ctx
->list
, &dev
->instances
);
1669 v4l2_dbg(1, coda_debug
, &dev
->v4l2_dev
, "Created instance %d (%p)\n",
1675 v4l2_m2m_ctx_release(ctx
->fh
.m2m_ctx
);
1677 clk_disable_unprepare(dev
->clk_ahb
);
1679 clk_disable_unprepare(dev
->clk_per
);
1681 pm_runtime_put_sync(&dev
->plat_dev
->dev
);
1683 v4l2_fh_del(&ctx
->fh
);
1684 v4l2_fh_exit(&ctx
->fh
);
1685 clear_bit(ctx
->idx
, &dev
->instance_mask
);
1692 static int coda_release(struct file
*file
)
1694 struct coda_dev
*dev
= video_drvdata(file
);
1695 struct coda_ctx
*ctx
= fh_to_ctx(file
->private_data
);
1697 v4l2_dbg(1, coda_debug
, &dev
->v4l2_dev
, "Releasing instance %p\n",
1700 if (ctx
->inst_type
== CODA_INST_DECODER
&& ctx
->use_bit
)
1701 coda_bit_stream_end_flag(ctx
);
1703 /* If this instance is running, call .job_abort and wait for it to end */
1704 v4l2_m2m_ctx_release(ctx
->fh
.m2m_ctx
);
1706 /* In case the instance was not running, we still need to call SEQ_END */
1707 if (ctx
->ops
->seq_end_work
) {
1708 queue_work(dev
->workqueue
, &ctx
->seq_end_work
);
1709 flush_work(&ctx
->seq_end_work
);
1713 list_del(&ctx
->list
);
1716 if (ctx
->dev
->devtype
->product
== CODA_DX6
)
1717 coda_free_aux_buf(dev
, &ctx
->workbuf
);
1719 v4l2_ctrl_handler_free(&ctx
->ctrls
);
1720 clk_disable_unprepare(dev
->clk_ahb
);
1721 clk_disable_unprepare(dev
->clk_per
);
1722 pm_runtime_put_sync(&dev
->plat_dev
->dev
);
1723 v4l2_fh_del(&ctx
->fh
);
1724 v4l2_fh_exit(&ctx
->fh
);
1725 clear_bit(ctx
->idx
, &dev
->instance_mask
);
1726 if (ctx
->ops
->release
)
1727 ctx
->ops
->release(ctx
);
1728 debugfs_remove_recursive(ctx
->debugfs_entry
);
1734 static const struct v4l2_file_operations coda_fops
= {
1735 .owner
= THIS_MODULE
,
1737 .release
= coda_release
,
1738 .poll
= v4l2_m2m_fop_poll
,
1739 .unlocked_ioctl
= video_ioctl2
,
1740 .mmap
= v4l2_m2m_fop_mmap
,
1743 static int coda_hw_init(struct coda_dev
*dev
)
1749 ret
= clk_prepare_enable(dev
->clk_per
);
1753 ret
= clk_prepare_enable(dev
->clk_ahb
);
1758 reset_control_reset(dev
->rstc
);
1761 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
1762 * The 16-bit chars in the code buffer are in memory access
1763 * order, re-sort them to CODA order for register download.
1764 * Data in this SRAM survives a reboot.
1766 p
= (u16
*)dev
->codebuf
.vaddr
;
1767 if (dev
->devtype
->product
== CODA_DX6
) {
1768 for (i
= 0; i
< (CODA_ISRAM_SIZE
/ 2); i
++) {
1769 data
= CODA_DOWN_ADDRESS_SET(i
) |
1770 CODA_DOWN_DATA_SET(p
[i
^ 1]);
1771 coda_write(dev
, data
, CODA_REG_BIT_CODE_DOWN
);
1774 for (i
= 0; i
< (CODA_ISRAM_SIZE
/ 2); i
++) {
1775 data
= CODA_DOWN_ADDRESS_SET(i
) |
1776 CODA_DOWN_DATA_SET(p
[round_down(i
, 4) +
1778 coda_write(dev
, data
, CODA_REG_BIT_CODE_DOWN
);
1782 /* Clear registers */
1783 for (i
= 0; i
< 64; i
++)
1784 coda_write(dev
, 0, CODA_REG_BIT_CODE_BUF_ADDR
+ i
* 4);
1786 /* Tell the BIT where to find everything it needs */
1787 if (dev
->devtype
->product
== CODA_960
||
1788 dev
->devtype
->product
== CODA_7541
) {
1789 coda_write(dev
, dev
->tempbuf
.paddr
,
1790 CODA_REG_BIT_TEMP_BUF_ADDR
);
1791 coda_write(dev
, 0, CODA_REG_BIT_BIT_STREAM_PARAM
);
1793 coda_write(dev
, dev
->workbuf
.paddr
,
1794 CODA_REG_BIT_WORK_BUF_ADDR
);
1796 coda_write(dev
, dev
->codebuf
.paddr
,
1797 CODA_REG_BIT_CODE_BUF_ADDR
);
1798 coda_write(dev
, 0, CODA_REG_BIT_CODE_RUN
);
1800 /* Set default values */
1801 switch (dev
->devtype
->product
) {
1803 coda_write(dev
, CODADX6_STREAM_BUF_PIC_FLUSH
,
1804 CODA_REG_BIT_STREAM_CTRL
);
1807 coda_write(dev
, CODA7_STREAM_BUF_PIC_FLUSH
,
1808 CODA_REG_BIT_STREAM_CTRL
);
1810 if (dev
->devtype
->product
== CODA_960
)
1811 coda_write(dev
, 1 << 12, CODA_REG_BIT_FRAME_MEM_CTRL
);
1813 coda_write(dev
, 0, CODA_REG_BIT_FRAME_MEM_CTRL
);
1815 if (dev
->devtype
->product
!= CODA_DX6
)
1816 coda_write(dev
, 0, CODA7_REG_BIT_AXI_SRAM_USE
);
1818 coda_write(dev
, CODA_INT_INTERRUPT_ENABLE
,
1819 CODA_REG_BIT_INT_ENABLE
);
1821 /* Reset VPU and start processor */
1822 data
= coda_read(dev
, CODA_REG_BIT_CODE_RESET
);
1823 data
|= CODA_REG_RESET_ENABLE
;
1824 coda_write(dev
, data
, CODA_REG_BIT_CODE_RESET
);
1826 data
&= ~CODA_REG_RESET_ENABLE
;
1827 coda_write(dev
, data
, CODA_REG_BIT_CODE_RESET
);
1828 coda_write(dev
, CODA_REG_RUN_ENABLE
, CODA_REG_BIT_CODE_RUN
);
1830 clk_disable_unprepare(dev
->clk_ahb
);
1831 clk_disable_unprepare(dev
->clk_per
);
1836 clk_disable_unprepare(dev
->clk_per
);
1841 static int coda_register_device(struct coda_dev
*dev
, int i
)
1843 struct video_device
*vfd
= &dev
->vfd
[i
];
1845 if (i
>= dev
->devtype
->num_vdevs
)
1848 strlcpy(vfd
->name
, dev
->devtype
->vdevs
[i
]->name
, sizeof(vfd
->name
));
1849 vfd
->fops
= &coda_fops
;
1850 vfd
->ioctl_ops
= &coda_ioctl_ops
;
1851 vfd
->release
= video_device_release_empty
,
1852 vfd
->lock
= &dev
->dev_mutex
;
1853 vfd
->v4l2_dev
= &dev
->v4l2_dev
;
1854 vfd
->vfl_dir
= VFL_DIR_M2M
;
1855 video_set_drvdata(vfd
, dev
);
1857 /* Not applicable, use the selection API instead */
1858 v4l2_disable_ioctl(vfd
, VIDIOC_CROPCAP
);
1859 v4l2_disable_ioctl(vfd
, VIDIOC_G_CROP
);
1860 v4l2_disable_ioctl(vfd
, VIDIOC_S_CROP
);
1862 return video_register_device(vfd
, VFL_TYPE_GRABBER
, 0);
1865 static void coda_fw_callback(const struct firmware
*fw
, void *context
)
1867 struct coda_dev
*dev
= context
;
1868 struct platform_device
*pdev
= dev
->plat_dev
;
1872 v4l2_err(&dev
->v4l2_dev
, "firmware request failed\n");
1876 /* allocate auxiliary per-device code buffer for the BIT processor */
1877 ret
= coda_alloc_aux_buf(dev
, &dev
->codebuf
, fw
->size
, "codebuf",
1882 /* Copy the whole firmware image to the code buffer */
1883 memcpy(dev
->codebuf
.vaddr
, fw
->data
, fw
->size
);
1884 release_firmware(fw
);
1886 ret
= coda_hw_init(dev
);
1888 v4l2_err(&dev
->v4l2_dev
, "HW initialization failed\n");
1892 ret
= coda_check_firmware(dev
);
1896 dev
->alloc_ctx
= vb2_dma_contig_init_ctx(&pdev
->dev
);
1897 if (IS_ERR(dev
->alloc_ctx
)) {
1898 v4l2_err(&dev
->v4l2_dev
, "Failed to alloc vb2 context\n");
1902 dev
->m2m_dev
= v4l2_m2m_init(&coda_m2m_ops
);
1903 if (IS_ERR(dev
->m2m_dev
)) {
1904 v4l2_err(&dev
->v4l2_dev
, "Failed to init mem2mem device\n");
1908 for (i
= 0; i
< dev
->devtype
->num_vdevs
; i
++) {
1909 ret
= coda_register_device(dev
, i
);
1911 v4l2_err(&dev
->v4l2_dev
,
1912 "Failed to register %s video device: %d\n",
1913 dev
->devtype
->vdevs
[i
]->name
, ret
);
1918 v4l2_info(&dev
->v4l2_dev
, "codec registered as /dev/video[%d-%d]\n",
1919 dev
->vfd
[0].num
, dev
->vfd
[i
- 1].num
);
1921 pm_runtime_put_sync(&pdev
->dev
);
1926 video_unregister_device(&dev
->vfd
[i
]);
1927 v4l2_m2m_release(dev
->m2m_dev
);
1929 vb2_dma_contig_cleanup_ctx(dev
->alloc_ctx
);
1931 pm_runtime_put_sync(&pdev
->dev
);
1934 static int coda_firmware_request(struct coda_dev
*dev
)
1936 char *fw
= dev
->devtype
->firmware
;
1938 dev_dbg(&dev
->plat_dev
->dev
, "requesting firmware '%s' for %s\n", fw
,
1939 coda_product_name(dev
->devtype
->product
));
1941 return request_firmware_nowait(THIS_MODULE
, true,
1942 fw
, &dev
->plat_dev
->dev
, GFP_KERNEL
, dev
, coda_fw_callback
);
1945 enum coda_platform
{
1952 static const struct coda_devtype coda_devdata
[] = {
1954 .firmware
= "v4l-codadx6-imx27.bin",
1955 .product
= CODA_DX6
,
1956 .codecs
= codadx6_codecs
,
1957 .num_codecs
= ARRAY_SIZE(codadx6_codecs
),
1958 .vdevs
= codadx6_video_devices
,
1959 .num_vdevs
= ARRAY_SIZE(codadx6_video_devices
),
1960 .workbuf_size
= 288 * 1024 + FMO_SLICE_SAVE_BUF_SIZE
* 8 * 1024,
1961 .iram_size
= 0xb000,
1964 .firmware
= "v4l-coda7541-imx53.bin",
1965 .product
= CODA_7541
,
1966 .codecs
= coda7_codecs
,
1967 .num_codecs
= ARRAY_SIZE(coda7_codecs
),
1968 .vdevs
= coda7_video_devices
,
1969 .num_vdevs
= ARRAY_SIZE(coda7_video_devices
),
1970 .workbuf_size
= 128 * 1024,
1971 .tempbuf_size
= 304 * 1024,
1972 .iram_size
= 0x14000,
1975 .firmware
= "v4l-coda960-imx6q.bin",
1976 .product
= CODA_960
,
1977 .codecs
= coda9_codecs
,
1978 .num_codecs
= ARRAY_SIZE(coda9_codecs
),
1979 .vdevs
= coda9_video_devices
,
1980 .num_vdevs
= ARRAY_SIZE(coda9_video_devices
),
1981 .workbuf_size
= 80 * 1024,
1982 .tempbuf_size
= 204 * 1024,
1983 .iram_size
= 0x21000,
1986 .firmware
= "v4l-coda960-imx6dl.bin",
1987 .product
= CODA_960
,
1988 .codecs
= coda9_codecs
,
1989 .num_codecs
= ARRAY_SIZE(coda9_codecs
),
1990 .vdevs
= coda9_video_devices
,
1991 .num_vdevs
= ARRAY_SIZE(coda9_video_devices
),
1992 .workbuf_size
= 80 * 1024,
1993 .tempbuf_size
= 204 * 1024,
1994 .iram_size
= 0x20000,
1998 static struct platform_device_id coda_platform_ids
[] = {
1999 { .name
= "coda-imx27", .driver_data
= CODA_IMX27
},
2002 MODULE_DEVICE_TABLE(platform
, coda_platform_ids
);
2005 static const struct of_device_id coda_dt_ids
[] = {
2006 { .compatible
= "fsl,imx27-vpu", .data
= &coda_devdata
[CODA_IMX27
] },
2007 { .compatible
= "fsl,imx53-vpu", .data
= &coda_devdata
[CODA_IMX53
] },
2008 { .compatible
= "fsl,imx6q-vpu", .data
= &coda_devdata
[CODA_IMX6Q
] },
2009 { .compatible
= "fsl,imx6dl-vpu", .data
= &coda_devdata
[CODA_IMX6DL
] },
2012 MODULE_DEVICE_TABLE(of
, coda_dt_ids
);
2015 static int coda_probe(struct platform_device
*pdev
)
2017 const struct of_device_id
*of_id
=
2018 of_match_device(of_match_ptr(coda_dt_ids
), &pdev
->dev
);
2019 const struct platform_device_id
*pdev_id
;
2020 struct coda_platform_data
*pdata
= pdev
->dev
.platform_data
;
2021 struct device_node
*np
= pdev
->dev
.of_node
;
2022 struct gen_pool
*pool
;
2023 struct coda_dev
*dev
;
2024 struct resource
*res
;
2027 dev
= devm_kzalloc(&pdev
->dev
, sizeof(*dev
), GFP_KERNEL
);
2031 pdev_id
= of_id
? of_id
->data
: platform_get_device_id(pdev
);
2034 dev
->devtype
= of_id
->data
;
2035 } else if (pdev_id
) {
2036 dev
->devtype
= &coda_devdata
[pdev_id
->driver_data
];
2039 goto err_v4l2_register
;
2042 spin_lock_init(&dev
->irqlock
);
2043 INIT_LIST_HEAD(&dev
->instances
);
2045 dev
->plat_dev
= pdev
;
2046 dev
->clk_per
= devm_clk_get(&pdev
->dev
, "per");
2047 if (IS_ERR(dev
->clk_per
)) {
2048 dev_err(&pdev
->dev
, "Could not get per clock\n");
2049 return PTR_ERR(dev
->clk_per
);
2052 dev
->clk_ahb
= devm_clk_get(&pdev
->dev
, "ahb");
2053 if (IS_ERR(dev
->clk_ahb
)) {
2054 dev_err(&pdev
->dev
, "Could not get ahb clock\n");
2055 return PTR_ERR(dev
->clk_ahb
);
2058 /* Get memory for physical registers */
2059 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2060 dev
->regs_base
= devm_ioremap_resource(&pdev
->dev
, res
);
2061 if (IS_ERR(dev
->regs_base
))
2062 return PTR_ERR(dev
->regs_base
);
2065 irq
= platform_get_irq_byname(pdev
, "bit");
2067 irq
= platform_get_irq(pdev
, 0);
2069 dev_err(&pdev
->dev
, "failed to get irq resource\n");
2073 ret
= devm_request_threaded_irq(&pdev
->dev
, irq
, NULL
, coda_irq_handler
,
2074 IRQF_ONESHOT
, dev_name(&pdev
->dev
), dev
);
2076 dev_err(&pdev
->dev
, "failed to request irq: %d\n", ret
);
2080 dev
->rstc
= devm_reset_control_get_optional(&pdev
->dev
, NULL
);
2081 if (IS_ERR(dev
->rstc
)) {
2082 ret
= PTR_ERR(dev
->rstc
);
2083 if (ret
== -ENOENT
|| ret
== -ENOSYS
) {
2086 dev_err(&pdev
->dev
, "failed get reset control: %d\n",
2092 /* Get IRAM pool from device tree or platform data */
2093 pool
= of_gen_pool_get(np
, "iram", 0);
2095 pool
= gen_pool_get(pdata
->iram_dev
);
2097 dev_err(&pdev
->dev
, "iram pool not available\n");
2100 dev
->iram_pool
= pool
;
2102 ret
= v4l2_device_register(&pdev
->dev
, &dev
->v4l2_dev
);
2106 mutex_init(&dev
->dev_mutex
);
2107 mutex_init(&dev
->coda_mutex
);
2109 dev
->debugfs_root
= debugfs_create_dir("coda", NULL
);
2110 if (!dev
->debugfs_root
)
2111 dev_warn(&pdev
->dev
, "failed to create debugfs root\n");
2113 /* allocate auxiliary per-device buffers for the BIT processor */
2114 if (dev
->devtype
->product
== CODA_DX6
) {
2115 ret
= coda_alloc_aux_buf(dev
, &dev
->workbuf
,
2116 dev
->devtype
->workbuf_size
, "workbuf",
2119 goto err_v4l2_register
;
2122 if (dev
->devtype
->tempbuf_size
) {
2123 ret
= coda_alloc_aux_buf(dev
, &dev
->tempbuf
,
2124 dev
->devtype
->tempbuf_size
, "tempbuf",
2127 goto err_v4l2_register
;
2130 dev
->iram
.size
= dev
->devtype
->iram_size
;
2131 dev
->iram
.vaddr
= gen_pool_dma_alloc(dev
->iram_pool
, dev
->iram
.size
,
2133 if (!dev
->iram
.vaddr
) {
2134 dev_warn(&pdev
->dev
, "unable to alloc iram\n");
2136 memset(dev
->iram
.vaddr
, 0, dev
->iram
.size
);
2137 dev
->iram
.blob
.data
= dev
->iram
.vaddr
;
2138 dev
->iram
.blob
.size
= dev
->iram
.size
;
2139 dev
->iram
.dentry
= debugfs_create_blob("iram", 0644,
2144 dev
->workqueue
= alloc_workqueue("coda", WQ_UNBOUND
| WQ_MEM_RECLAIM
, 1);
2145 if (!dev
->workqueue
) {
2146 dev_err(&pdev
->dev
, "unable to alloc workqueue\n");
2148 goto err_v4l2_register
;
2151 platform_set_drvdata(pdev
, dev
);
2154 * Start activated so we can directly call coda_hw_init in
2155 * coda_fw_callback regardless of whether CONFIG_PM is
2156 * enabled or whether the device is associated with a PM domain.
2158 pm_runtime_get_noresume(&pdev
->dev
);
2159 pm_runtime_set_active(&pdev
->dev
);
2160 pm_runtime_enable(&pdev
->dev
);
2162 return coda_firmware_request(dev
);
2165 v4l2_device_unregister(&dev
->v4l2_dev
);
2169 static int coda_remove(struct platform_device
*pdev
)
2171 struct coda_dev
*dev
= platform_get_drvdata(pdev
);
2174 for (i
= 0; i
< ARRAY_SIZE(dev
->vfd
); i
++) {
2175 if (video_get_drvdata(&dev
->vfd
[i
]))
2176 video_unregister_device(&dev
->vfd
[i
]);
2179 v4l2_m2m_release(dev
->m2m_dev
);
2180 pm_runtime_disable(&pdev
->dev
);
2182 vb2_dma_contig_cleanup_ctx(dev
->alloc_ctx
);
2183 v4l2_device_unregister(&dev
->v4l2_dev
);
2184 destroy_workqueue(dev
->workqueue
);
2185 if (dev
->iram
.vaddr
)
2186 gen_pool_free(dev
->iram_pool
, (unsigned long)dev
->iram
.vaddr
,
2188 coda_free_aux_buf(dev
, &dev
->codebuf
);
2189 coda_free_aux_buf(dev
, &dev
->tempbuf
);
2190 coda_free_aux_buf(dev
, &dev
->workbuf
);
2191 debugfs_remove_recursive(dev
->debugfs_root
);
2196 static int coda_runtime_resume(struct device
*dev
)
2198 struct coda_dev
*cdev
= dev_get_drvdata(dev
);
2201 if (dev
->pm_domain
&& cdev
->codebuf
.vaddr
) {
2202 ret
= coda_hw_init(cdev
);
2204 v4l2_err(&cdev
->v4l2_dev
, "HW initialization failed\n");
2211 static const struct dev_pm_ops coda_pm_ops
= {
2212 SET_RUNTIME_PM_OPS(NULL
, coda_runtime_resume
, NULL
)
2215 static struct platform_driver coda_driver
= {
2216 .probe
= coda_probe
,
2217 .remove
= coda_remove
,
2220 .of_match_table
= of_match_ptr(coda_dt_ids
),
2223 .id_table
= coda_platform_ids
,
2226 module_platform_driver(coda_driver
);
2228 MODULE_LICENSE("GPL");
2229 MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
2230 MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");