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>
42 #include "coda_regs.h"
44 #define CODA_NAME "coda"
46 #define CODADX6_MAX_INSTANCES 4
48 #define CODA_PARA_BUF_SIZE (10 * 1024)
49 #define CODA_ISRAM_SIZE (2048 * 2)
51 #define CODA7_PS_BUF_SIZE 0x28000
52 #define CODA9_PS_SAVE_SIZE (512 * 1024)
54 #define CODA_DEFAULT_GAMMA 4096
55 #define CODA9_DEFAULT_GAMMA 24576 /* 0.75 * 32768 */
60 #define S_ALIGN 1 /* multiple of 2 */
61 #define W_ALIGN 1 /* multiple of 2 */
62 #define H_ALIGN 1 /* multiple of 2 */
64 #define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh)
66 static int coda_debug
;
67 module_param(coda_debug
, int, 0644);
68 MODULE_PARM_DESC(coda_debug
, "Debug level (0-1)");
75 static const u8 coda_filler_nal
[14] = { 0x00, 0x00, 0x00, 0x01, 0x0c, 0xff,
76 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80 };
77 static const u8 coda_filler_size
[8] = { 0, 7, 14, 13, 12, 11, 10, 9 };
79 void coda_write(struct coda_dev
*dev
, u32 data
, u32 reg
)
81 v4l2_dbg(1, coda_debug
, &dev
->v4l2_dev
,
82 "%s: data=0x%x, reg=0x%x\n", __func__
, data
, reg
);
83 writel(data
, dev
->regs_base
+ reg
);
86 unsigned int coda_read(struct coda_dev
*dev
, u32 reg
)
89 data
= readl(dev
->regs_base
+ reg
);
90 v4l2_dbg(1, coda_debug
, &dev
->v4l2_dev
,
91 "%s: data=0x%x, reg=0x%x\n", __func__
, data
, reg
);
95 static inline unsigned long coda_isbusy(struct coda_dev
*dev
)
97 return coda_read(dev
, CODA_REG_BIT_BUSY
);
100 static inline int coda_is_initialized(struct coda_dev
*dev
)
102 return (coda_read(dev
, CODA_REG_BIT_CUR_PC
) != 0);
105 static int coda_wait_timeout(struct coda_dev
*dev
)
107 unsigned long timeout
= jiffies
+ msecs_to_jiffies(1000);
109 while (coda_isbusy(dev
)) {
110 if (time_after(jiffies
, timeout
))
116 static void coda_command_async(struct coda_ctx
*ctx
, int cmd
)
118 struct coda_dev
*dev
= ctx
->dev
;
120 if (dev
->devtype
->product
== CODA_960
||
121 dev
->devtype
->product
== CODA_7541
) {
122 /* Restore context related registers to CODA */
123 coda_write(dev
, ctx
->bit_stream_param
,
124 CODA_REG_BIT_BIT_STREAM_PARAM
);
125 coda_write(dev
, ctx
->frm_dis_flg
,
126 CODA_REG_BIT_FRM_DIS_FLG(ctx
->reg_idx
));
127 coda_write(dev
, ctx
->frame_mem_ctrl
,
128 CODA_REG_BIT_FRAME_MEM_CTRL
);
129 coda_write(dev
, ctx
->workbuf
.paddr
, CODA_REG_BIT_WORK_BUF_ADDR
);
132 if (dev
->devtype
->product
== CODA_960
) {
133 coda_write(dev
, 1, CODA9_GDI_WPROT_ERR_CLR
);
134 coda_write(dev
, 0, CODA9_GDI_WPROT_RGN_EN
);
137 coda_write(dev
, CODA_REG_BIT_BUSY_FLAG
, CODA_REG_BIT_BUSY
);
139 coda_write(dev
, ctx
->idx
, CODA_REG_BIT_RUN_INDEX
);
140 coda_write(dev
, ctx
->params
.codec_mode
, CODA_REG_BIT_RUN_COD_STD
);
141 coda_write(dev
, ctx
->params
.codec_mode_aux
, CODA7_REG_BIT_RUN_AUX_STD
);
143 coda_write(dev
, cmd
, CODA_REG_BIT_RUN_COMMAND
);
146 static int coda_command_sync(struct coda_ctx
*ctx
, int cmd
)
148 struct coda_dev
*dev
= ctx
->dev
;
150 coda_command_async(ctx
, cmd
);
151 return coda_wait_timeout(dev
);
154 static int coda_hw_reset(struct coda_ctx
*ctx
)
156 struct coda_dev
*dev
= ctx
->dev
;
157 unsigned long timeout
;
164 idx
= coda_read(dev
, CODA_REG_BIT_RUN_INDEX
);
166 if (dev
->devtype
->product
== CODA_960
) {
167 timeout
= jiffies
+ msecs_to_jiffies(100);
168 coda_write(dev
, 0x11, CODA9_GDI_BUS_CTRL
);
169 while (coda_read(dev
, CODA9_GDI_BUS_STATUS
) != 0x77) {
170 if (time_after(jiffies
, timeout
))
176 ret
= reset_control_reset(dev
->rstc
);
180 if (dev
->devtype
->product
== CODA_960
)
181 coda_write(dev
, 0x00, CODA9_GDI_BUS_CTRL
);
182 coda_write(dev
, CODA_REG_BIT_BUSY_FLAG
, CODA_REG_BIT_BUSY
);
183 coda_write(dev
, CODA_REG_RUN_ENABLE
, CODA_REG_BIT_CODE_RUN
);
184 ret
= coda_wait_timeout(dev
);
185 coda_write(dev
, idx
, CODA_REG_BIT_RUN_INDEX
);
190 static struct coda_q_data
*get_q_data(struct coda_ctx
*ctx
,
191 enum v4l2_buf_type type
)
194 case V4L2_BUF_TYPE_VIDEO_OUTPUT
:
195 return &(ctx
->q_data
[V4L2_M2M_SRC
]);
196 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
197 return &(ctx
->q_data
[V4L2_M2M_DST
]);
204 * Array of all formats supported by any version of Coda:
206 static const struct coda_fmt coda_formats
[] = {
208 .name
= "YUV 4:2:0 Planar, YCbCr",
209 .fourcc
= V4L2_PIX_FMT_YUV420
,
212 .name
= "YUV 4:2:0 Planar, YCrCb",
213 .fourcc
= V4L2_PIX_FMT_YVU420
,
216 .name
= "H264 Encoded Stream",
217 .fourcc
= V4L2_PIX_FMT_H264
,
220 .name
= "MPEG4 Encoded Stream",
221 .fourcc
= V4L2_PIX_FMT_MPEG4
,
225 #define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
226 { mode, src_fourcc, dst_fourcc, max_w, max_h }
229 * Arrays of codecs supported by each given version of Coda:
233 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
235 static const struct coda_codec codadx6_codecs
[] = {
236 CODA_CODEC(CODADX6_MODE_ENCODE_H264
, V4L2_PIX_FMT_YUV420
, V4L2_PIX_FMT_H264
, 720, 576),
237 CODA_CODEC(CODADX6_MODE_ENCODE_MP4
, V4L2_PIX_FMT_YUV420
, V4L2_PIX_FMT_MPEG4
, 720, 576),
240 static const struct coda_codec coda7_codecs
[] = {
241 CODA_CODEC(CODA7_MODE_ENCODE_H264
, V4L2_PIX_FMT_YUV420
, V4L2_PIX_FMT_H264
, 1280, 720),
242 CODA_CODEC(CODA7_MODE_ENCODE_MP4
, V4L2_PIX_FMT_YUV420
, V4L2_PIX_FMT_MPEG4
, 1280, 720),
243 CODA_CODEC(CODA7_MODE_DECODE_H264
, V4L2_PIX_FMT_H264
, V4L2_PIX_FMT_YUV420
, 1920, 1080),
244 CODA_CODEC(CODA7_MODE_DECODE_MP4
, V4L2_PIX_FMT_MPEG4
, V4L2_PIX_FMT_YUV420
, 1920, 1080),
247 static const struct coda_codec coda9_codecs
[] = {
248 CODA_CODEC(CODA9_MODE_ENCODE_H264
, V4L2_PIX_FMT_YUV420
, V4L2_PIX_FMT_H264
, 1920, 1080),
249 CODA_CODEC(CODA9_MODE_ENCODE_MP4
, V4L2_PIX_FMT_YUV420
, V4L2_PIX_FMT_MPEG4
, 1920, 1080),
250 CODA_CODEC(CODA9_MODE_DECODE_H264
, V4L2_PIX_FMT_H264
, V4L2_PIX_FMT_YUV420
, 1920, 1080),
251 CODA_CODEC(CODA9_MODE_DECODE_MP4
, V4L2_PIX_FMT_MPEG4
, V4L2_PIX_FMT_YUV420
, 1920, 1080),
254 static bool coda_format_is_yuv(u32 fourcc
)
257 case V4L2_PIX_FMT_YUV420
:
258 case V4L2_PIX_FMT_YVU420
:
266 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
269 static u32
coda_format_normalize_yuv(u32 fourcc
)
271 return coda_format_is_yuv(fourcc
) ? V4L2_PIX_FMT_YUV420
: fourcc
;
274 static const struct coda_codec
*coda_find_codec(struct coda_dev
*dev
,
275 int src_fourcc
, int dst_fourcc
)
277 const struct coda_codec
*codecs
= dev
->devtype
->codecs
;
278 int num_codecs
= dev
->devtype
->num_codecs
;
281 src_fourcc
= coda_format_normalize_yuv(src_fourcc
);
282 dst_fourcc
= coda_format_normalize_yuv(dst_fourcc
);
283 if (src_fourcc
== dst_fourcc
)
286 for (k
= 0; k
< num_codecs
; k
++) {
287 if (codecs
[k
].src_fourcc
== src_fourcc
&&
288 codecs
[k
].dst_fourcc
== dst_fourcc
)
298 static void coda_get_max_dimensions(struct coda_dev
*dev
,
299 const struct coda_codec
*codec
,
300 int *max_w
, int *max_h
)
302 const struct coda_codec
*codecs
= dev
->devtype
->codecs
;
303 int num_codecs
= dev
->devtype
->num_codecs
;
311 for (k
= 0, w
= 0, h
= 0; k
< num_codecs
; k
++) {
312 w
= max(w
, codecs
[k
].max_w
);
313 h
= max(h
, codecs
[k
].max_h
);
323 static char *coda_product_name(int product
)
335 snprintf(buf
, sizeof(buf
), "(0x%04x)", product
);
341 * V4L2 ioctl() operations.
343 static int coda_querycap(struct file
*file
, void *priv
,
344 struct v4l2_capability
*cap
)
346 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
348 strlcpy(cap
->driver
, CODA_NAME
, sizeof(cap
->driver
));
349 strlcpy(cap
->card
, coda_product_name(ctx
->dev
->devtype
->product
),
351 strlcpy(cap
->bus_info
, "platform:" CODA_NAME
, sizeof(cap
->bus_info
));
352 cap
->device_caps
= V4L2_CAP_VIDEO_M2M
| V4L2_CAP_STREAMING
;
353 cap
->capabilities
= cap
->device_caps
| V4L2_CAP_DEVICE_CAPS
;
358 static int coda_enum_fmt(struct file
*file
, void *priv
,
359 struct v4l2_fmtdesc
*f
)
361 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
362 const struct coda_codec
*codecs
= ctx
->dev
->devtype
->codecs
;
363 const struct coda_fmt
*formats
= coda_formats
;
364 const struct coda_fmt
*fmt
;
365 int num_codecs
= ctx
->dev
->devtype
->num_codecs
;
366 int num_formats
= ARRAY_SIZE(coda_formats
);
370 if (ctx
->inst_type
== CODA_INST_ENCODER
)
371 yuv
= (f
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
);
373 yuv
= (f
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
);
375 for (i
= 0; i
< num_formats
; i
++) {
376 /* Skip either raw or compressed formats */
377 if (yuv
!= coda_format_is_yuv(formats
[i
].fourcc
))
379 /* All uncompressed formats are always supported */
386 /* Compressed formats may be supported, check the codec list */
387 for (k
= 0; k
< num_codecs
; k
++) {
388 if (f
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
389 formats
[i
].fourcc
== codecs
[k
].dst_fourcc
)
391 if (f
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
&&
392 formats
[i
].fourcc
== codecs
[k
].src_fourcc
)
395 if (k
< num_codecs
) {
402 if (i
< num_formats
) {
404 strlcpy(f
->description
, fmt
->name
, sizeof(f
->description
));
405 f
->pixelformat
= fmt
->fourcc
;
407 f
->flags
|= V4L2_FMT_FLAG_COMPRESSED
;
411 /* Format not found */
415 static int coda_g_fmt(struct file
*file
, void *priv
,
416 struct v4l2_format
*f
)
418 struct coda_q_data
*q_data
;
419 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
421 q_data
= get_q_data(ctx
, f
->type
);
425 f
->fmt
.pix
.field
= V4L2_FIELD_NONE
;
426 f
->fmt
.pix
.pixelformat
= q_data
->fourcc
;
427 f
->fmt
.pix
.width
= q_data
->width
;
428 f
->fmt
.pix
.height
= q_data
->height
;
429 f
->fmt
.pix
.bytesperline
= q_data
->bytesperline
;
431 f
->fmt
.pix
.sizeimage
= q_data
->sizeimage
;
432 f
->fmt
.pix
.colorspace
= ctx
->colorspace
;
437 static int coda_try_fmt(struct coda_ctx
*ctx
, const struct coda_codec
*codec
,
438 struct v4l2_format
*f
)
440 struct coda_dev
*dev
= ctx
->dev
;
441 struct coda_q_data
*q_data
;
442 unsigned int max_w
, max_h
;
443 enum v4l2_field field
;
445 field
= f
->fmt
.pix
.field
;
446 if (field
== V4L2_FIELD_ANY
)
447 field
= V4L2_FIELD_NONE
;
448 else if (V4L2_FIELD_NONE
!= field
)
451 /* V4L2 specification suggests the driver corrects the format struct
452 * if any of the dimensions is unsupported */
453 f
->fmt
.pix
.field
= field
;
455 coda_get_max_dimensions(dev
, codec
, &max_w
, &max_h
);
456 v4l_bound_align_image(&f
->fmt
.pix
.width
, MIN_W
, max_w
, W_ALIGN
,
457 &f
->fmt
.pix
.height
, MIN_H
, max_h
, H_ALIGN
,
460 switch (f
->fmt
.pix
.pixelformat
) {
461 case V4L2_PIX_FMT_YUV420
:
462 case V4L2_PIX_FMT_YVU420
:
463 case V4L2_PIX_FMT_H264
:
464 case V4L2_PIX_FMT_MPEG4
:
465 case V4L2_PIX_FMT_JPEG
:
468 q_data
= get_q_data(ctx
, f
->type
);
471 f
->fmt
.pix
.pixelformat
= q_data
->fourcc
;
474 switch (f
->fmt
.pix
.pixelformat
) {
475 case V4L2_PIX_FMT_YUV420
:
476 case V4L2_PIX_FMT_YVU420
:
477 /* Frame stride must be multiple of 8, but 16 for h.264 */
478 f
->fmt
.pix
.bytesperline
= round_up(f
->fmt
.pix
.width
, 16);
479 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.bytesperline
*
480 f
->fmt
.pix
.height
* 3 / 2;
482 case V4L2_PIX_FMT_H264
:
483 case V4L2_PIX_FMT_MPEG4
:
484 case V4L2_PIX_FMT_JPEG
:
485 f
->fmt
.pix
.bytesperline
= 0;
486 f
->fmt
.pix
.sizeimage
= CODA_MAX_FRAME_SIZE
;
495 static int coda_try_fmt_vid_cap(struct file
*file
, void *priv
,
496 struct v4l2_format
*f
)
498 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
499 const struct coda_codec
*codec
= NULL
;
500 struct vb2_queue
*src_vq
;
504 * If the source format is already fixed, try to find a codec that
505 * converts to the given destination format
507 src_vq
= v4l2_m2m_get_vq(ctx
->fh
.m2m_ctx
, V4L2_BUF_TYPE_VIDEO_OUTPUT
);
508 if (vb2_is_streaming(src_vq
)) {
509 struct coda_q_data
*q_data_src
;
511 q_data_src
= get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_OUTPUT
);
512 codec
= coda_find_codec(ctx
->dev
, q_data_src
->fourcc
,
513 f
->fmt
.pix
.pixelformat
);
517 f
->fmt
.pix
.width
= q_data_src
->width
;
518 f
->fmt
.pix
.height
= q_data_src
->height
;
520 /* Otherwise determine codec by encoded format, if possible */
521 codec
= coda_find_codec(ctx
->dev
, V4L2_PIX_FMT_YUV420
,
522 f
->fmt
.pix
.pixelformat
);
525 f
->fmt
.pix
.colorspace
= ctx
->colorspace
;
527 ret
= coda_try_fmt(ctx
, codec
, f
);
531 /* The h.264 decoder only returns complete 16x16 macroblocks */
532 if (codec
&& codec
->src_fourcc
== V4L2_PIX_FMT_H264
) {
533 f
->fmt
.pix
.width
= f
->fmt
.pix
.width
;
534 f
->fmt
.pix
.height
= round_up(f
->fmt
.pix
.height
, 16);
535 f
->fmt
.pix
.bytesperline
= round_up(f
->fmt
.pix
.width
, 16);
536 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.bytesperline
*
537 f
->fmt
.pix
.height
* 3 / 2;
543 static int coda_try_fmt_vid_out(struct file
*file
, void *priv
,
544 struct v4l2_format
*f
)
546 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
547 const struct coda_codec
*codec
;
549 /* Determine codec by encoded format, returns NULL if raw or invalid */
550 codec
= coda_find_codec(ctx
->dev
, f
->fmt
.pix
.pixelformat
,
551 V4L2_PIX_FMT_YUV420
);
552 if (!codec
&& ctx
->inst_type
== CODA_INST_DECODER
) {
553 codec
= coda_find_codec(ctx
->dev
, V4L2_PIX_FMT_H264
,
554 V4L2_PIX_FMT_YUV420
);
559 if (!f
->fmt
.pix
.colorspace
)
560 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_REC709
;
562 return coda_try_fmt(ctx
, codec
, f
);
565 static int coda_s_fmt(struct coda_ctx
*ctx
, struct v4l2_format
*f
)
567 struct coda_q_data
*q_data
;
568 struct vb2_queue
*vq
;
570 vq
= v4l2_m2m_get_vq(ctx
->fh
.m2m_ctx
, f
->type
);
574 q_data
= get_q_data(ctx
, f
->type
);
578 if (vb2_is_busy(vq
)) {
579 v4l2_err(&ctx
->dev
->v4l2_dev
, "%s queue busy\n", __func__
);
583 q_data
->fourcc
= f
->fmt
.pix
.pixelformat
;
584 q_data
->width
= f
->fmt
.pix
.width
;
585 q_data
->height
= f
->fmt
.pix
.height
;
586 q_data
->bytesperline
= f
->fmt
.pix
.bytesperline
;
587 q_data
->sizeimage
= f
->fmt
.pix
.sizeimage
;
588 q_data
->rect
.left
= 0;
589 q_data
->rect
.top
= 0;
590 q_data
->rect
.width
= f
->fmt
.pix
.width
;
591 q_data
->rect
.height
= f
->fmt
.pix
.height
;
593 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
594 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
595 f
->type
, q_data
->width
, q_data
->height
, q_data
->fourcc
);
600 static int coda_s_fmt_vid_cap(struct file
*file
, void *priv
,
601 struct v4l2_format
*f
)
603 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
606 ret
= coda_try_fmt_vid_cap(file
, priv
, f
);
610 return coda_s_fmt(ctx
, f
);
613 static int coda_s_fmt_vid_out(struct file
*file
, void *priv
,
614 struct v4l2_format
*f
)
616 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
619 ret
= coda_try_fmt_vid_out(file
, priv
, f
);
623 ret
= coda_s_fmt(ctx
, f
);
625 ctx
->colorspace
= f
->fmt
.pix
.colorspace
;
630 static int coda_qbuf(struct file
*file
, void *priv
,
631 struct v4l2_buffer
*buf
)
633 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
635 return v4l2_m2m_qbuf(file
, ctx
->fh
.m2m_ctx
, buf
);
638 static bool coda_buf_is_end_of_stream(struct coda_ctx
*ctx
,
639 struct v4l2_buffer
*buf
)
641 struct vb2_queue
*src_vq
;
643 src_vq
= v4l2_m2m_get_vq(ctx
->fh
.m2m_ctx
, V4L2_BUF_TYPE_VIDEO_OUTPUT
);
645 return ((ctx
->bit_stream_param
& CODA_BIT_STREAM_END_FLAG
) &&
646 (buf
->sequence
== (ctx
->qsequence
- 1)));
649 static int coda_dqbuf(struct file
*file
, void *priv
,
650 struct v4l2_buffer
*buf
)
652 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
655 ret
= v4l2_m2m_dqbuf(file
, ctx
->fh
.m2m_ctx
, buf
);
657 /* If this is the last capture buffer, emit an end-of-stream event */
658 if (buf
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
659 coda_buf_is_end_of_stream(ctx
, buf
)) {
660 const struct v4l2_event eos_event
= {
661 .type
= V4L2_EVENT_EOS
664 v4l2_event_queue_fh(&ctx
->fh
, &eos_event
);
670 static int coda_g_selection(struct file
*file
, void *fh
,
671 struct v4l2_selection
*s
)
673 struct coda_ctx
*ctx
= fh_to_ctx(fh
);
674 struct coda_q_data
*q_data
;
675 struct v4l2_rect r
, *rsel
;
677 q_data
= get_q_data(ctx
, s
->type
);
683 r
.width
= q_data
->width
;
684 r
.height
= q_data
->height
;
685 rsel
= &q_data
->rect
;
688 case V4L2_SEL_TGT_CROP_DEFAULT
:
689 case V4L2_SEL_TGT_CROP_BOUNDS
:
692 case V4L2_SEL_TGT_CROP
:
693 if (s
->type
!= V4L2_BUF_TYPE_VIDEO_OUTPUT
)
696 case V4L2_SEL_TGT_COMPOSE_BOUNDS
:
697 case V4L2_SEL_TGT_COMPOSE_PADDED
:
700 case V4L2_SEL_TGT_COMPOSE
:
701 case V4L2_SEL_TGT_COMPOSE_DEFAULT
:
702 if (s
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
714 static int coda_try_decoder_cmd(struct file
*file
, void *fh
,
715 struct v4l2_decoder_cmd
*dc
)
717 if (dc
->cmd
!= V4L2_DEC_CMD_STOP
)
720 if (dc
->flags
& V4L2_DEC_CMD_STOP_TO_BLACK
)
723 if (!(dc
->flags
& V4L2_DEC_CMD_STOP_IMMEDIATELY
) && (dc
->stop
.pts
!= 0))
729 static int coda_decoder_cmd(struct file
*file
, void *fh
,
730 struct v4l2_decoder_cmd
*dc
)
732 struct coda_ctx
*ctx
= fh_to_ctx(fh
);
733 struct coda_dev
*dev
= ctx
->dev
;
736 ret
= coda_try_decoder_cmd(file
, fh
, dc
);
740 /* Ignore decoder stop command silently in encoder context */
741 if (ctx
->inst_type
!= CODA_INST_DECODER
)
744 /* Set the strem-end flag on this context */
745 ctx
->bit_stream_param
|= CODA_BIT_STREAM_END_FLAG
;
747 if ((dev
->devtype
->product
== CODA_960
) &&
749 (ctx
->idx
== coda_read(dev
, CODA_REG_BIT_RUN_INDEX
))) {
750 /* If this context is currently running, update the hardware flag */
751 coda_write(dev
, ctx
->bit_stream_param
, CODA_REG_BIT_BIT_STREAM_PARAM
);
754 v4l2_m2m_try_schedule(ctx
->fh
.m2m_ctx
);
759 static int coda_subscribe_event(struct v4l2_fh
*fh
,
760 const struct v4l2_event_subscription
*sub
)
764 return v4l2_event_subscribe(fh
, sub
, 0, NULL
);
766 return v4l2_ctrl_subscribe_event(fh
, sub
);
770 static const struct v4l2_ioctl_ops coda_ioctl_ops
= {
771 .vidioc_querycap
= coda_querycap
,
773 .vidioc_enum_fmt_vid_cap
= coda_enum_fmt
,
774 .vidioc_g_fmt_vid_cap
= coda_g_fmt
,
775 .vidioc_try_fmt_vid_cap
= coda_try_fmt_vid_cap
,
776 .vidioc_s_fmt_vid_cap
= coda_s_fmt_vid_cap
,
778 .vidioc_enum_fmt_vid_out
= coda_enum_fmt
,
779 .vidioc_g_fmt_vid_out
= coda_g_fmt
,
780 .vidioc_try_fmt_vid_out
= coda_try_fmt_vid_out
,
781 .vidioc_s_fmt_vid_out
= coda_s_fmt_vid_out
,
783 .vidioc_reqbufs
= v4l2_m2m_ioctl_reqbufs
,
784 .vidioc_querybuf
= v4l2_m2m_ioctl_querybuf
,
786 .vidioc_qbuf
= coda_qbuf
,
787 .vidioc_expbuf
= v4l2_m2m_ioctl_expbuf
,
788 .vidioc_dqbuf
= coda_dqbuf
,
789 .vidioc_create_bufs
= v4l2_m2m_ioctl_create_bufs
,
791 .vidioc_streamon
= v4l2_m2m_ioctl_streamon
,
792 .vidioc_streamoff
= v4l2_m2m_ioctl_streamoff
,
794 .vidioc_g_selection
= coda_g_selection
,
796 .vidioc_try_decoder_cmd
= coda_try_decoder_cmd
,
797 .vidioc_decoder_cmd
= coda_decoder_cmd
,
799 .vidioc_subscribe_event
= coda_subscribe_event
,
800 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
803 static int __coda_start_decoding(struct coda_ctx
*ctx
);
805 static inline int coda_get_bitstream_payload(struct coda_ctx
*ctx
)
807 return kfifo_len(&ctx
->bitstream_fifo
);
810 static void coda_kfifo_sync_from_device(struct coda_ctx
*ctx
)
812 struct __kfifo
*kfifo
= &ctx
->bitstream_fifo
.kfifo
;
813 struct coda_dev
*dev
= ctx
->dev
;
816 rd_ptr
= coda_read(dev
, CODA_REG_BIT_RD_PTR(ctx
->reg_idx
));
817 kfifo
->out
= (kfifo
->in
& ~kfifo
->mask
) |
818 (rd_ptr
- ctx
->bitstream
.paddr
);
819 if (kfifo
->out
> kfifo
->in
)
820 kfifo
->out
-= kfifo
->mask
+ 1;
823 static void coda_kfifo_sync_to_device_full(struct coda_ctx
*ctx
)
825 struct __kfifo
*kfifo
= &ctx
->bitstream_fifo
.kfifo
;
826 struct coda_dev
*dev
= ctx
->dev
;
829 rd_ptr
= ctx
->bitstream
.paddr
+ (kfifo
->out
& kfifo
->mask
);
830 coda_write(dev
, rd_ptr
, CODA_REG_BIT_RD_PTR(ctx
->reg_idx
));
831 wr_ptr
= ctx
->bitstream
.paddr
+ (kfifo
->in
& kfifo
->mask
);
832 coda_write(dev
, wr_ptr
, CODA_REG_BIT_WR_PTR(ctx
->reg_idx
));
835 static void coda_kfifo_sync_to_device_write(struct coda_ctx
*ctx
)
837 struct __kfifo
*kfifo
= &ctx
->bitstream_fifo
.kfifo
;
838 struct coda_dev
*dev
= ctx
->dev
;
841 wr_ptr
= ctx
->bitstream
.paddr
+ (kfifo
->in
& kfifo
->mask
);
842 coda_write(dev
, wr_ptr
, CODA_REG_BIT_WR_PTR(ctx
->reg_idx
));
845 static int coda_bitstream_queue(struct coda_ctx
*ctx
, struct vb2_buffer
*src_buf
)
847 u32 src_size
= vb2_get_plane_payload(src_buf
, 0);
850 n
= kfifo_in(&ctx
->bitstream_fifo
, vb2_plane_vaddr(src_buf
, 0), src_size
);
854 dma_sync_single_for_device(&ctx
->dev
->plat_dev
->dev
, ctx
->bitstream
.paddr
,
855 ctx
->bitstream
.size
, DMA_TO_DEVICE
);
857 src_buf
->v4l2_buf
.sequence
= ctx
->qsequence
++;
862 static bool coda_bitstream_try_queue(struct coda_ctx
*ctx
,
863 struct vb2_buffer
*src_buf
)
867 if (coda_get_bitstream_payload(ctx
) +
868 vb2_get_plane_payload(src_buf
, 0) + 512 >= ctx
->bitstream
.size
)
871 if (vb2_plane_vaddr(src_buf
, 0) == NULL
) {
872 v4l2_err(&ctx
->dev
->v4l2_dev
, "trying to queue empty buffer\n");
876 ret
= coda_bitstream_queue(ctx
, src_buf
);
878 v4l2_err(&ctx
->dev
->v4l2_dev
, "bitstream buffer overflow\n");
881 /* Sync read pointer to device */
882 if (ctx
== v4l2_m2m_get_curr_priv(ctx
->dev
->m2m_dev
))
883 coda_kfifo_sync_to_device_write(ctx
);
890 static void coda_fill_bitstream(struct coda_ctx
*ctx
)
892 struct vb2_buffer
*src_buf
;
893 struct coda_timestamp
*ts
;
895 while (v4l2_m2m_num_src_bufs_ready(ctx
->fh
.m2m_ctx
) > 0) {
896 src_buf
= v4l2_m2m_next_src_buf(ctx
->fh
.m2m_ctx
);
898 if (coda_bitstream_try_queue(ctx
, src_buf
)) {
900 * Source buffer is queued in the bitstream ringbuffer;
901 * queue the timestamp and mark source buffer as done
903 src_buf
= v4l2_m2m_src_buf_remove(ctx
->fh
.m2m_ctx
);
905 ts
= kmalloc(sizeof(*ts
), GFP_KERNEL
);
907 ts
->sequence
= src_buf
->v4l2_buf
.sequence
;
908 ts
->timecode
= src_buf
->v4l2_buf
.timecode
;
909 ts
->timestamp
= src_buf
->v4l2_buf
.timestamp
;
910 list_add_tail(&ts
->list
, &ctx
->timestamp_list
);
913 v4l2_m2m_buf_done(src_buf
, VB2_BUF_STATE_DONE
);
920 static void coda_set_gdi_regs(struct coda_ctx
*ctx
)
922 struct gdi_tiled_map
*tiled_map
= &ctx
->tiled_map
;
923 struct coda_dev
*dev
= ctx
->dev
;
926 for (i
= 0; i
< 16; i
++)
927 coda_write(dev
, tiled_map
->xy2ca_map
[i
],
928 CODA9_GDI_XY2_CAS_0
+ 4 * i
);
929 for (i
= 0; i
< 4; i
++)
930 coda_write(dev
, tiled_map
->xy2ba_map
[i
],
931 CODA9_GDI_XY2_BA_0
+ 4 * i
);
932 for (i
= 0; i
< 16; i
++)
933 coda_write(dev
, tiled_map
->xy2ra_map
[i
],
934 CODA9_GDI_XY2_RAS_0
+ 4 * i
);
935 coda_write(dev
, tiled_map
->xy2rbc_config
, CODA9_GDI_XY2_RBC_CONFIG
);
936 for (i
= 0; i
< 32; i
++)
937 coda_write(dev
, tiled_map
->rbc2axi_map
[i
],
938 CODA9_GDI_RBC2_AXI_0
+ 4 * i
);
942 * Mem-to-mem operations.
944 static int coda_prepare_decode(struct coda_ctx
*ctx
)
946 struct vb2_buffer
*dst_buf
;
947 struct coda_dev
*dev
= ctx
->dev
;
948 struct coda_q_data
*q_data_dst
;
950 u32 picture_y
, picture_cb
, picture_cr
;
952 dst_buf
= v4l2_m2m_next_dst_buf(ctx
->fh
.m2m_ctx
);
953 q_data_dst
= get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_CAPTURE
);
955 if (ctx
->params
.rot_mode
& CODA_ROT_90
) {
956 stridey
= q_data_dst
->height
;
957 height
= q_data_dst
->width
;
959 stridey
= q_data_dst
->width
;
960 height
= q_data_dst
->height
;
963 /* Try to copy source buffer contents into the bitstream ringbuffer */
964 mutex_lock(&ctx
->bitstream_mutex
);
965 coda_fill_bitstream(ctx
);
966 mutex_unlock(&ctx
->bitstream_mutex
);
968 if (coda_get_bitstream_payload(ctx
) < 512 &&
969 (!(ctx
->bit_stream_param
& CODA_BIT_STREAM_END_FLAG
))) {
970 v4l2_dbg(1, coda_debug
, &dev
->v4l2_dev
,
971 "bitstream payload: %d, skipping\n",
972 coda_get_bitstream_payload(ctx
));
973 v4l2_m2m_job_finish(ctx
->dev
->m2m_dev
, ctx
->fh
.m2m_ctx
);
977 /* Run coda_start_decoding (again) if not yet initialized */
978 if (!ctx
->initialized
) {
979 int ret
= __coda_start_decoding(ctx
);
981 v4l2_err(&dev
->v4l2_dev
, "failed to start decoding\n");
982 v4l2_m2m_job_finish(ctx
->dev
->m2m_dev
, ctx
->fh
.m2m_ctx
);
985 ctx
->initialized
= 1;
989 if (dev
->devtype
->product
== CODA_960
)
990 coda_set_gdi_regs(ctx
);
992 /* Set rotator output */
993 picture_y
= vb2_dma_contig_plane_dma_addr(dst_buf
, 0);
994 if (q_data_dst
->fourcc
== V4L2_PIX_FMT_YVU420
) {
995 /* Switch Cr and Cb for YVU420 format */
996 picture_cr
= picture_y
+ stridey
* height
;
997 picture_cb
= picture_cr
+ stridey
/ 2 * height
/ 2;
999 picture_cb
= picture_y
+ stridey
* height
;
1000 picture_cr
= picture_cb
+ stridey
/ 2 * height
/ 2;
1003 if (dev
->devtype
->product
== CODA_960
) {
1005 * The CODA960 seems to have an internal list of buffers with
1006 * 64 entries that includes the registered frame buffers as
1007 * well as the rotator buffer output.
1008 * ROT_INDEX needs to be < 0x40, but > ctx->num_internal_frames.
1010 coda_write(dev
, CODA_MAX_FRAMEBUFFERS
+ dst_buf
->v4l2_buf
.index
,
1011 CODA9_CMD_DEC_PIC_ROT_INDEX
);
1012 coda_write(dev
, picture_y
, CODA9_CMD_DEC_PIC_ROT_ADDR_Y
);
1013 coda_write(dev
, picture_cb
, CODA9_CMD_DEC_PIC_ROT_ADDR_CB
);
1014 coda_write(dev
, picture_cr
, CODA9_CMD_DEC_PIC_ROT_ADDR_CR
);
1015 coda_write(dev
, stridey
, CODA9_CMD_DEC_PIC_ROT_STRIDE
);
1017 coda_write(dev
, picture_y
, CODA_CMD_DEC_PIC_ROT_ADDR_Y
);
1018 coda_write(dev
, picture_cb
, CODA_CMD_DEC_PIC_ROT_ADDR_CB
);
1019 coda_write(dev
, picture_cr
, CODA_CMD_DEC_PIC_ROT_ADDR_CR
);
1020 coda_write(dev
, stridey
, CODA_CMD_DEC_PIC_ROT_STRIDE
);
1022 coda_write(dev
, CODA_ROT_MIR_ENABLE
| ctx
->params
.rot_mode
,
1023 CODA_CMD_DEC_PIC_ROT_MODE
);
1025 switch (dev
->devtype
->product
) {
1029 coda_write(dev
, CODA_PRE_SCAN_EN
, CODA_CMD_DEC_PIC_OPTION
);
1032 coda_write(dev
, (1 << 10), CODA_CMD_DEC_PIC_OPTION
); /* 'hardcode to use interrupt disable mode'? */
1036 coda_write(dev
, 0, CODA_CMD_DEC_PIC_SKIP_NUM
);
1038 coda_write(dev
, 0, CODA_CMD_DEC_PIC_BB_START
);
1039 coda_write(dev
, 0, CODA_CMD_DEC_PIC_START_BYTE
);
1044 static int coda_prepare_encode(struct coda_ctx
*ctx
)
1046 struct coda_q_data
*q_data_src
, *q_data_dst
;
1047 struct vb2_buffer
*src_buf
, *dst_buf
;
1048 struct coda_dev
*dev
= ctx
->dev
;
1050 int quant_param
= 0;
1051 u32 picture_y
, picture_cb
, picture_cr
;
1052 u32 pic_stream_buffer_addr
, pic_stream_buffer_size
;
1055 src_buf
= v4l2_m2m_next_src_buf(ctx
->fh
.m2m_ctx
);
1056 dst_buf
= v4l2_m2m_next_dst_buf(ctx
->fh
.m2m_ctx
);
1057 q_data_src
= get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_OUTPUT
);
1058 q_data_dst
= get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_CAPTURE
);
1059 dst_fourcc
= q_data_dst
->fourcc
;
1061 src_buf
->v4l2_buf
.sequence
= ctx
->osequence
;
1062 dst_buf
->v4l2_buf
.sequence
= ctx
->osequence
;
1066 * Workaround coda firmware BUG that only marks the first
1067 * frame as IDR. This is a problem for some decoders that can't
1068 * recover when a frame is lost.
1070 if (src_buf
->v4l2_buf
.sequence
% ctx
->params
.gop_size
) {
1071 src_buf
->v4l2_buf
.flags
|= V4L2_BUF_FLAG_PFRAME
;
1072 src_buf
->v4l2_buf
.flags
&= ~V4L2_BUF_FLAG_KEYFRAME
;
1074 src_buf
->v4l2_buf
.flags
|= V4L2_BUF_FLAG_KEYFRAME
;
1075 src_buf
->v4l2_buf
.flags
&= ~V4L2_BUF_FLAG_PFRAME
;
1078 if (dev
->devtype
->product
== CODA_960
)
1079 coda_set_gdi_regs(ctx
);
1082 * Copy headers at the beginning of the first frame for H.264 only.
1083 * In MPEG4 they are already copied by the coda.
1085 if (src_buf
->v4l2_buf
.sequence
== 0) {
1086 pic_stream_buffer_addr
=
1087 vb2_dma_contig_plane_dma_addr(dst_buf
, 0) +
1088 ctx
->vpu_header_size
[0] +
1089 ctx
->vpu_header_size
[1] +
1090 ctx
->vpu_header_size
[2];
1091 pic_stream_buffer_size
= CODA_MAX_FRAME_SIZE
-
1092 ctx
->vpu_header_size
[0] -
1093 ctx
->vpu_header_size
[1] -
1094 ctx
->vpu_header_size
[2];
1095 memcpy(vb2_plane_vaddr(dst_buf
, 0),
1096 &ctx
->vpu_header
[0][0], ctx
->vpu_header_size
[0]);
1097 memcpy(vb2_plane_vaddr(dst_buf
, 0) + ctx
->vpu_header_size
[0],
1098 &ctx
->vpu_header
[1][0], ctx
->vpu_header_size
[1]);
1099 memcpy(vb2_plane_vaddr(dst_buf
, 0) + ctx
->vpu_header_size
[0] +
1100 ctx
->vpu_header_size
[1], &ctx
->vpu_header
[2][0],
1101 ctx
->vpu_header_size
[2]);
1103 pic_stream_buffer_addr
=
1104 vb2_dma_contig_plane_dma_addr(dst_buf
, 0);
1105 pic_stream_buffer_size
= CODA_MAX_FRAME_SIZE
;
1108 if (src_buf
->v4l2_buf
.flags
& V4L2_BUF_FLAG_KEYFRAME
) {
1110 switch (dst_fourcc
) {
1111 case V4L2_PIX_FMT_H264
:
1112 quant_param
= ctx
->params
.h264_intra_qp
;
1114 case V4L2_PIX_FMT_MPEG4
:
1115 quant_param
= ctx
->params
.mpeg4_intra_qp
;
1118 v4l2_warn(&ctx
->dev
->v4l2_dev
,
1119 "cannot set intra qp, fmt not supported\n");
1124 switch (dst_fourcc
) {
1125 case V4L2_PIX_FMT_H264
:
1126 quant_param
= ctx
->params
.h264_inter_qp
;
1128 case V4L2_PIX_FMT_MPEG4
:
1129 quant_param
= ctx
->params
.mpeg4_inter_qp
;
1132 v4l2_warn(&ctx
->dev
->v4l2_dev
,
1133 "cannot set inter qp, fmt not supported\n");
1139 coda_write(dev
, CODA_ROT_MIR_ENABLE
| ctx
->params
.rot_mode
, CODA_CMD_ENC_PIC_ROT_MODE
);
1140 coda_write(dev
, quant_param
, CODA_CMD_ENC_PIC_QS
);
1143 picture_y
= vb2_dma_contig_plane_dma_addr(src_buf
, 0);
1144 switch (q_data_src
->fourcc
) {
1145 case V4L2_PIX_FMT_YVU420
:
1146 /* Switch Cb and Cr for YVU420 format */
1147 picture_cr
= picture_y
+ q_data_src
->bytesperline
*
1149 picture_cb
= picture_cr
+ q_data_src
->bytesperline
/ 2 *
1150 q_data_src
->height
/ 2;
1152 case V4L2_PIX_FMT_YUV420
:
1154 picture_cb
= picture_y
+ q_data_src
->bytesperline
*
1156 picture_cr
= picture_cb
+ q_data_src
->bytesperline
/ 2 *
1157 q_data_src
->height
/ 2;
1161 if (dev
->devtype
->product
== CODA_960
) {
1162 coda_write(dev
, 4/*FIXME: 0*/, CODA9_CMD_ENC_PIC_SRC_INDEX
);
1163 coda_write(dev
, q_data_src
->width
, CODA9_CMD_ENC_PIC_SRC_STRIDE
);
1164 coda_write(dev
, 0, CODA9_CMD_ENC_PIC_SUB_FRAME_SYNC
);
1166 coda_write(dev
, picture_y
, CODA9_CMD_ENC_PIC_SRC_ADDR_Y
);
1167 coda_write(dev
, picture_cb
, CODA9_CMD_ENC_PIC_SRC_ADDR_CB
);
1168 coda_write(dev
, picture_cr
, CODA9_CMD_ENC_PIC_SRC_ADDR_CR
);
1170 coda_write(dev
, picture_y
, CODA_CMD_ENC_PIC_SRC_ADDR_Y
);
1171 coda_write(dev
, picture_cb
, CODA_CMD_ENC_PIC_SRC_ADDR_CB
);
1172 coda_write(dev
, picture_cr
, CODA_CMD_ENC_PIC_SRC_ADDR_CR
);
1174 coda_write(dev
, force_ipicture
<< 1 & 0x2,
1175 CODA_CMD_ENC_PIC_OPTION
);
1177 coda_write(dev
, pic_stream_buffer_addr
, CODA_CMD_ENC_PIC_BB_START
);
1178 coda_write(dev
, pic_stream_buffer_size
/ 1024,
1179 CODA_CMD_ENC_PIC_BB_SIZE
);
1181 if (!ctx
->streamon_out
) {
1182 /* After streamoff on the output side, set the stream end flag */
1183 ctx
->bit_stream_param
|= CODA_BIT_STREAM_END_FLAG
;
1184 coda_write(dev
, ctx
->bit_stream_param
, CODA_REG_BIT_BIT_STREAM_PARAM
);
1190 static void coda_device_run(void *m2m_priv
)
1192 struct coda_ctx
*ctx
= m2m_priv
;
1193 struct coda_dev
*dev
= ctx
->dev
;
1195 queue_work(dev
->workqueue
, &ctx
->pic_run_work
);
1198 static void coda_free_framebuffers(struct coda_ctx
*ctx
);
1199 static void coda_free_context_buffers(struct coda_ctx
*ctx
);
1201 static void coda_seq_end_work(struct work_struct
*work
)
1203 struct coda_ctx
*ctx
= container_of(work
, struct coda_ctx
, seq_end_work
);
1204 struct coda_dev
*dev
= ctx
->dev
;
1206 mutex_lock(&ctx
->buffer_mutex
);
1207 mutex_lock(&dev
->coda_mutex
);
1209 v4l2_dbg(1, coda_debug
, &dev
->v4l2_dev
,
1210 "%d: %s: sent command 'SEQ_END' to coda\n", ctx
->idx
, __func__
);
1211 if (coda_command_sync(ctx
, CODA_COMMAND_SEQ_END
)) {
1212 v4l2_err(&dev
->v4l2_dev
,
1213 "CODA_COMMAND_SEQ_END failed\n");
1216 kfifo_init(&ctx
->bitstream_fifo
,
1217 ctx
->bitstream
.vaddr
, ctx
->bitstream
.size
);
1219 coda_free_framebuffers(ctx
);
1220 coda_free_context_buffers(ctx
);
1222 mutex_unlock(&dev
->coda_mutex
);
1223 mutex_unlock(&ctx
->buffer_mutex
);
1226 static void coda_finish_decode(struct coda_ctx
*ctx
);
1227 static void coda_finish_encode(struct coda_ctx
*ctx
);
1229 static void coda_pic_run_work(struct work_struct
*work
)
1231 struct coda_ctx
*ctx
= container_of(work
, struct coda_ctx
, pic_run_work
);
1232 struct coda_dev
*dev
= ctx
->dev
;
1235 mutex_lock(&ctx
->buffer_mutex
);
1236 mutex_lock(&dev
->coda_mutex
);
1238 ret
= ctx
->ops
->prepare_run(ctx
);
1239 if (ret
< 0 && ctx
->inst_type
== CODA_INST_DECODER
) {
1240 mutex_unlock(&dev
->coda_mutex
);
1241 mutex_unlock(&ctx
->buffer_mutex
);
1242 /* job_finish scheduled by prepare_decode */
1246 if (dev
->devtype
->product
!= CODA_DX6
)
1247 coda_write(dev
, ctx
->iram_info
.axi_sram_use
,
1248 CODA7_REG_BIT_AXI_SRAM_USE
);
1250 if (ctx
->inst_type
== CODA_INST_DECODER
)
1251 coda_kfifo_sync_to_device_full(ctx
);
1252 coda_command_async(ctx
, CODA_COMMAND_PIC_RUN
);
1254 if (!wait_for_completion_timeout(&ctx
->completion
, msecs_to_jiffies(1000))) {
1255 dev_err(&dev
->plat_dev
->dev
, "CODA PIC_RUN timeout\n");
1260 } else if (!ctx
->aborting
) {
1261 ctx
->ops
->finish_run(ctx
);
1264 if (ctx
->aborting
|| (!ctx
->streamon_cap
&& !ctx
->streamon_out
))
1265 queue_work(dev
->workqueue
, &ctx
->seq_end_work
);
1267 mutex_unlock(&dev
->coda_mutex
);
1268 mutex_unlock(&ctx
->buffer_mutex
);
1270 v4l2_m2m_job_finish(ctx
->dev
->m2m_dev
, ctx
->fh
.m2m_ctx
);
1273 static int coda_job_ready(void *m2m_priv
)
1275 struct coda_ctx
*ctx
= m2m_priv
;
1278 * For both 'P' and 'key' frame cases 1 picture
1279 * and 1 frame are needed. In the decoder case,
1280 * the compressed frame can be in the bitstream.
1282 if (!v4l2_m2m_num_src_bufs_ready(ctx
->fh
.m2m_ctx
) &&
1283 ctx
->inst_type
!= CODA_INST_DECODER
) {
1284 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
1285 "not ready: not enough video buffers.\n");
1289 if (!v4l2_m2m_num_dst_bufs_ready(ctx
->fh
.m2m_ctx
)) {
1290 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
1291 "not ready: not enough video capture buffers.\n");
1296 ((ctx
->inst_type
== CODA_INST_DECODER
) &&
1297 (coda_get_bitstream_payload(ctx
) < 512) &&
1298 !(ctx
->bit_stream_param
& CODA_BIT_STREAM_END_FLAG
))) {
1299 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
1300 "%d: not ready: not enough bitstream data.\n",
1305 if (ctx
->aborting
) {
1306 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
1307 "not ready: aborting\n");
1311 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
1316 static void coda_job_abort(void *priv
)
1318 struct coda_ctx
*ctx
= priv
;
1322 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
1326 static void coda_lock(void *m2m_priv
)
1328 struct coda_ctx
*ctx
= m2m_priv
;
1329 struct coda_dev
*pcdev
= ctx
->dev
;
1330 mutex_lock(&pcdev
->dev_mutex
);
1333 static void coda_unlock(void *m2m_priv
)
1335 struct coda_ctx
*ctx
= m2m_priv
;
1336 struct coda_dev
*pcdev
= ctx
->dev
;
1337 mutex_unlock(&pcdev
->dev_mutex
);
1340 static const struct v4l2_m2m_ops coda_m2m_ops
= {
1341 .device_run
= coda_device_run
,
1342 .job_ready
= coda_job_ready
,
1343 .job_abort
= coda_job_abort
,
1345 .unlock
= coda_unlock
,
1348 static void coda_set_tiled_map_type(struct coda_ctx
*ctx
, int tiled_map_type
)
1350 struct gdi_tiled_map
*tiled_map
= &ctx
->tiled_map
;
1351 int luma_map
, chro_map
, i
;
1353 memset(tiled_map
, 0, sizeof(*tiled_map
));
1357 tiled_map
->map_type
= tiled_map_type
;
1358 for (i
= 0; i
< 16; i
++)
1359 tiled_map
->xy2ca_map
[i
] = luma_map
<< 8 | chro_map
;
1360 for (i
= 0; i
< 4; i
++)
1361 tiled_map
->xy2ba_map
[i
] = luma_map
<< 8 | chro_map
;
1362 for (i
= 0; i
< 16; i
++)
1363 tiled_map
->xy2ra_map
[i
] = luma_map
<< 8 | chro_map
;
1365 if (tiled_map_type
== GDI_LINEAR_FRAME_MAP
) {
1366 tiled_map
->xy2rbc_config
= 0;
1368 dev_err(&ctx
->dev
->plat_dev
->dev
, "invalid map type: %d\n",
1374 static void set_default_params(struct coda_ctx
*ctx
)
1376 u32 src_fourcc
, dst_fourcc
;
1380 if (ctx
->inst_type
== CODA_INST_ENCODER
) {
1381 src_fourcc
= V4L2_PIX_FMT_YUV420
;
1382 dst_fourcc
= V4L2_PIX_FMT_H264
;
1384 src_fourcc
= V4L2_PIX_FMT_H264
;
1385 dst_fourcc
= V4L2_PIX_FMT_YUV420
;
1387 ctx
->codec
= coda_find_codec(ctx
->dev
, src_fourcc
, dst_fourcc
);
1388 max_w
= ctx
->codec
->max_w
;
1389 max_h
= ctx
->codec
->max_h
;
1391 ctx
->params
.codec_mode
= ctx
->codec
->mode
;
1392 ctx
->colorspace
= V4L2_COLORSPACE_REC709
;
1393 ctx
->params
.framerate
= 30;
1396 /* Default formats for output and input queues */
1397 ctx
->q_data
[V4L2_M2M_SRC
].fourcc
= ctx
->codec
->src_fourcc
;
1398 ctx
->q_data
[V4L2_M2M_DST
].fourcc
= ctx
->codec
->dst_fourcc
;
1399 ctx
->q_data
[V4L2_M2M_SRC
].width
= max_w
;
1400 ctx
->q_data
[V4L2_M2M_SRC
].height
= max_h
;
1401 ctx
->q_data
[V4L2_M2M_DST
].width
= max_w
;
1402 ctx
->q_data
[V4L2_M2M_DST
].height
= max_h
;
1403 if (ctx
->codec
->src_fourcc
== V4L2_PIX_FMT_YUV420
) {
1404 ctx
->q_data
[V4L2_M2M_SRC
].bytesperline
= max_w
;
1405 ctx
->q_data
[V4L2_M2M_SRC
].sizeimage
= (max_w
* max_h
* 3) / 2;
1406 ctx
->q_data
[V4L2_M2M_DST
].bytesperline
= 0;
1407 ctx
->q_data
[V4L2_M2M_DST
].sizeimage
= CODA_MAX_FRAME_SIZE
;
1409 ctx
->q_data
[V4L2_M2M_SRC
].bytesperline
= 0;
1410 ctx
->q_data
[V4L2_M2M_SRC
].sizeimage
= CODA_MAX_FRAME_SIZE
;
1411 ctx
->q_data
[V4L2_M2M_DST
].bytesperline
= max_w
;
1412 ctx
->q_data
[V4L2_M2M_DST
].sizeimage
= (max_w
* max_h
* 3) / 2;
1414 ctx
->q_data
[V4L2_M2M_SRC
].rect
.width
= max_w
;
1415 ctx
->q_data
[V4L2_M2M_SRC
].rect
.height
= max_h
;
1416 ctx
->q_data
[V4L2_M2M_DST
].rect
.width
= max_w
;
1417 ctx
->q_data
[V4L2_M2M_DST
].rect
.height
= max_h
;
1419 if (ctx
->dev
->devtype
->product
== CODA_960
)
1420 coda_set_tiled_map_type(ctx
, GDI_LINEAR_FRAME_MAP
);
1426 static int coda_queue_setup(struct vb2_queue
*vq
,
1427 const struct v4l2_format
*fmt
,
1428 unsigned int *nbuffers
, unsigned int *nplanes
,
1429 unsigned int sizes
[], void *alloc_ctxs
[])
1431 struct coda_ctx
*ctx
= vb2_get_drv_priv(vq
);
1432 struct coda_q_data
*q_data
;
1435 q_data
= get_q_data(ctx
, vq
->type
);
1436 size
= q_data
->sizeimage
;
1441 alloc_ctxs
[0] = ctx
->dev
->alloc_ctx
;
1443 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
1444 "get %d buffer(s) of size %d each.\n", *nbuffers
, size
);
1449 static int coda_buf_prepare(struct vb2_buffer
*vb
)
1451 struct coda_ctx
*ctx
= vb2_get_drv_priv(vb
->vb2_queue
);
1452 struct coda_q_data
*q_data
;
1454 q_data
= get_q_data(ctx
, vb
->vb2_queue
->type
);
1456 if (vb2_plane_size(vb
, 0) < q_data
->sizeimage
) {
1457 v4l2_warn(&ctx
->dev
->v4l2_dev
,
1458 "%s data will not fit into plane (%lu < %lu)\n",
1459 __func__
, vb2_plane_size(vb
, 0),
1460 (long)q_data
->sizeimage
);
1467 static void coda_buf_queue(struct vb2_buffer
*vb
)
1469 struct coda_ctx
*ctx
= vb2_get_drv_priv(vb
->vb2_queue
);
1470 struct coda_dev
*dev
= ctx
->dev
;
1471 struct coda_q_data
*q_data
;
1473 q_data
= get_q_data(ctx
, vb
->vb2_queue
->type
);
1476 * In the decoder case, immediately try to copy the buffer into the
1477 * bitstream ringbuffer and mark it as ready to be dequeued.
1479 if (q_data
->fourcc
== V4L2_PIX_FMT_H264
&&
1480 vb
->vb2_queue
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
) {
1482 * For backwards compatibility, queuing an empty buffer marks
1485 if (vb2_get_plane_payload(vb
, 0) == 0) {
1486 ctx
->bit_stream_param
|= CODA_BIT_STREAM_END_FLAG
;
1487 if ((dev
->devtype
->product
== CODA_960
) &&
1489 (ctx
->idx
== coda_read(dev
, CODA_REG_BIT_RUN_INDEX
))) {
1490 /* if this decoder instance is running, set the stream end flag */
1491 coda_write(dev
, ctx
->bit_stream_param
, CODA_REG_BIT_BIT_STREAM_PARAM
);
1494 mutex_lock(&ctx
->bitstream_mutex
);
1495 v4l2_m2m_buf_queue(ctx
->fh
.m2m_ctx
, vb
);
1496 if (vb2_is_streaming(vb
->vb2_queue
))
1497 coda_fill_bitstream(ctx
);
1498 mutex_unlock(&ctx
->bitstream_mutex
);
1500 v4l2_m2m_buf_queue(ctx
->fh
.m2m_ctx
, vb
);
1504 static void coda_parabuf_write(struct coda_ctx
*ctx
, int index
, u32 value
)
1506 struct coda_dev
*dev
= ctx
->dev
;
1507 u32
*p
= ctx
->parabuf
.vaddr
;
1509 if (dev
->devtype
->product
== CODA_DX6
)
1512 p
[index
^ 1] = value
;
1515 static int coda_alloc_aux_buf(struct coda_dev
*dev
,
1516 struct coda_aux_buf
*buf
, size_t size
,
1517 const char *name
, struct dentry
*parent
)
1519 buf
->vaddr
= dma_alloc_coherent(&dev
->plat_dev
->dev
, size
, &buf
->paddr
,
1526 if (name
&& parent
) {
1527 buf
->blob
.data
= buf
->vaddr
;
1528 buf
->blob
.size
= size
;
1529 buf
->dentry
= debugfs_create_blob(name
, 0644, parent
, &buf
->blob
);
1531 dev_warn(&dev
->plat_dev
->dev
,
1532 "failed to create debugfs entry %s\n", name
);
1538 static inline int coda_alloc_context_buf(struct coda_ctx
*ctx
,
1539 struct coda_aux_buf
*buf
, size_t size
,
1542 return coda_alloc_aux_buf(ctx
->dev
, buf
, size
, name
, ctx
->debugfs_entry
);
1545 static void coda_free_aux_buf(struct coda_dev
*dev
,
1546 struct coda_aux_buf
*buf
)
1549 dma_free_coherent(&dev
->plat_dev
->dev
, buf
->size
,
1550 buf
->vaddr
, buf
->paddr
);
1554 debugfs_remove(buf
->dentry
);
1557 static void coda_free_framebuffers(struct coda_ctx
*ctx
)
1561 for (i
= 0; i
< CODA_MAX_FRAMEBUFFERS
; i
++)
1562 coda_free_aux_buf(ctx
->dev
, &ctx
->internal_frames
[i
]);
1565 static int coda_alloc_framebuffers(struct coda_ctx
*ctx
, struct coda_q_data
*q_data
, u32 fourcc
)
1567 struct coda_dev
*dev
= ctx
->dev
;
1574 if (ctx
->codec
&& (ctx
->codec
->src_fourcc
== V4L2_PIX_FMT_H264
||
1575 ctx
->codec
->dst_fourcc
== V4L2_PIX_FMT_H264
)) {
1576 width
= round_up(q_data
->width
, 16);
1577 height
= round_up(q_data
->height
, 16);
1579 width
= round_up(q_data
->width
, 8);
1580 height
= q_data
->height
;
1582 ysize
= width
* height
;
1584 /* Allocate frame buffers */
1585 for (i
= 0; i
< ctx
->num_internal_frames
; i
++) {
1589 size
= ysize
+ ysize
/ 2;
1590 if (ctx
->codec
->src_fourcc
== V4L2_PIX_FMT_H264
&&
1591 dev
->devtype
->product
!= CODA_DX6
)
1593 name
= kasprintf(GFP_KERNEL
, "fb%d", i
);
1594 ret
= coda_alloc_context_buf(ctx
, &ctx
->internal_frames
[i
],
1598 coda_free_framebuffers(ctx
);
1603 /* Register frame buffers in the parameter buffer */
1604 for (i
= 0; i
< ctx
->num_internal_frames
; i
++) {
1605 paddr
= ctx
->internal_frames
[i
].paddr
;
1606 coda_parabuf_write(ctx
, i
* 3 + 0, paddr
); /* Y */
1607 coda_parabuf_write(ctx
, i
* 3 + 1, paddr
+ ysize
); /* Cb */
1608 coda_parabuf_write(ctx
, i
* 3 + 2, paddr
+ ysize
+ ysize
/4); /* Cr */
1610 /* mvcol buffer for h.264 */
1611 if (ctx
->codec
->src_fourcc
== V4L2_PIX_FMT_H264
&&
1612 dev
->devtype
->product
!= CODA_DX6
)
1613 coda_parabuf_write(ctx
, 96 + i
,
1614 ctx
->internal_frames
[i
].paddr
+
1615 ysize
+ ysize
/4 + ysize
/4);
1618 /* mvcol buffer for mpeg4 */
1619 if ((dev
->devtype
->product
!= CODA_DX6
) &&
1620 (ctx
->codec
->src_fourcc
== V4L2_PIX_FMT_MPEG4
))
1621 coda_parabuf_write(ctx
, 97, ctx
->internal_frames
[i
].paddr
+
1622 ysize
+ ysize
/4 + ysize
/4);
1627 static int coda_h264_padding(int size
, char *p
)
1632 diff
= size
- (size
& ~0x7);
1636 nal_size
= coda_filler_size
[diff
];
1637 memcpy(p
, coda_filler_nal
, nal_size
);
1639 /* Add rbsp stop bit and trailing at the end */
1640 *(p
+ nal_size
- 1) = 0x80;
1645 static phys_addr_t
coda_iram_alloc(struct coda_iram_info
*iram
, size_t size
)
1649 size
= round_up(size
, 1024);
1650 if (size
> iram
->remaining
)
1652 iram
->remaining
-= size
;
1654 ret
= iram
->next_paddr
;
1655 iram
->next_paddr
+= size
;
1660 static void coda_setup_iram(struct coda_ctx
*ctx
)
1662 struct coda_iram_info
*iram_info
= &ctx
->iram_info
;
1663 struct coda_dev
*dev
= ctx
->dev
;
1669 memset(iram_info
, 0, sizeof(*iram_info
));
1670 iram_info
->next_paddr
= dev
->iram
.paddr
;
1671 iram_info
->remaining
= dev
->iram
.size
;
1673 switch (dev
->devtype
->product
) {
1675 dbk_bits
= CODA7_USE_HOST_DBK_ENABLE
| CODA7_USE_DBK_ENABLE
;
1676 bit_bits
= CODA7_USE_HOST_BIT_ENABLE
| CODA7_USE_BIT_ENABLE
;
1677 ip_bits
= CODA7_USE_HOST_IP_ENABLE
| CODA7_USE_IP_ENABLE
;
1680 dbk_bits
= CODA9_USE_HOST_DBK_ENABLE
| CODA9_USE_DBK_ENABLE
;
1681 bit_bits
= CODA9_USE_HOST_BIT_ENABLE
| CODA7_USE_BIT_ENABLE
;
1682 ip_bits
= CODA9_USE_HOST_IP_ENABLE
| CODA7_USE_IP_ENABLE
;
1684 default: /* CODA_DX6 */
1688 if (ctx
->inst_type
== CODA_INST_ENCODER
) {
1689 struct coda_q_data
*q_data_src
;
1691 q_data_src
= get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_OUTPUT
);
1692 mb_width
= DIV_ROUND_UP(q_data_src
->width
, 16);
1694 /* Prioritize in case IRAM is too small for everything */
1695 if (dev
->devtype
->product
== CODA_7541
) {
1696 iram_info
->search_ram_size
= round_up(mb_width
* 16 *
1698 iram_info
->search_ram_paddr
= coda_iram_alloc(iram_info
,
1699 iram_info
->search_ram_size
);
1700 if (!iram_info
->search_ram_paddr
) {
1701 pr_err("IRAM is smaller than the search ram size\n");
1704 iram_info
->axi_sram_use
|= CODA7_USE_HOST_ME_ENABLE
|
1705 CODA7_USE_ME_ENABLE
;
1708 /* Only H.264BP and H.263P3 are considered */
1709 iram_info
->buf_dbk_y_use
= coda_iram_alloc(iram_info
, 64 * mb_width
);
1710 iram_info
->buf_dbk_c_use
= coda_iram_alloc(iram_info
, 64 * mb_width
);
1711 if (!iram_info
->buf_dbk_c_use
)
1713 iram_info
->axi_sram_use
|= dbk_bits
;
1715 iram_info
->buf_bit_use
= coda_iram_alloc(iram_info
, 128 * mb_width
);
1716 if (!iram_info
->buf_bit_use
)
1718 iram_info
->axi_sram_use
|= bit_bits
;
1720 iram_info
->buf_ip_ac_dc_use
= coda_iram_alloc(iram_info
, 128 * mb_width
);
1721 if (!iram_info
->buf_ip_ac_dc_use
)
1723 iram_info
->axi_sram_use
|= ip_bits
;
1725 /* OVL and BTP disabled for encoder */
1726 } else if (ctx
->inst_type
== CODA_INST_DECODER
) {
1727 struct coda_q_data
*q_data_dst
;
1729 q_data_dst
= get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_CAPTURE
);
1730 mb_width
= DIV_ROUND_UP(q_data_dst
->width
, 16);
1732 iram_info
->buf_dbk_y_use
= coda_iram_alloc(iram_info
, 128 * mb_width
);
1733 iram_info
->buf_dbk_c_use
= coda_iram_alloc(iram_info
, 128 * mb_width
);
1734 if (!iram_info
->buf_dbk_c_use
)
1736 iram_info
->axi_sram_use
|= dbk_bits
;
1738 iram_info
->buf_bit_use
= coda_iram_alloc(iram_info
, 128 * mb_width
);
1739 if (!iram_info
->buf_bit_use
)
1741 iram_info
->axi_sram_use
|= bit_bits
;
1743 iram_info
->buf_ip_ac_dc_use
= coda_iram_alloc(iram_info
, 128 * mb_width
);
1744 if (!iram_info
->buf_ip_ac_dc_use
)
1746 iram_info
->axi_sram_use
|= ip_bits
;
1748 /* OVL and BTP unused as there is no VC1 support yet */
1752 if (!(iram_info
->axi_sram_use
& CODA7_USE_HOST_IP_ENABLE
))
1753 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
1754 "IRAM smaller than needed\n");
1756 if (dev
->devtype
->product
== CODA_7541
) {
1757 /* TODO - Enabling these causes picture errors on CODA7541 */
1758 if (ctx
->inst_type
== CODA_INST_DECODER
) {
1760 iram_info
->axi_sram_use
&= ~(CODA7_USE_HOST_IP_ENABLE
|
1761 CODA7_USE_IP_ENABLE
);
1764 iram_info
->axi_sram_use
&= ~(CODA7_USE_HOST_IP_ENABLE
|
1765 CODA7_USE_HOST_DBK_ENABLE
|
1766 CODA7_USE_IP_ENABLE
|
1767 CODA7_USE_DBK_ENABLE
);
1772 static void coda_free_context_buffers(struct coda_ctx
*ctx
)
1774 struct coda_dev
*dev
= ctx
->dev
;
1776 coda_free_aux_buf(dev
, &ctx
->slicebuf
);
1777 coda_free_aux_buf(dev
, &ctx
->psbuf
);
1778 if (dev
->devtype
->product
!= CODA_DX6
)
1779 coda_free_aux_buf(dev
, &ctx
->workbuf
);
1782 static int coda_alloc_context_buffers(struct coda_ctx
*ctx
,
1783 struct coda_q_data
*q_data
)
1785 struct coda_dev
*dev
= ctx
->dev
;
1789 if (dev
->devtype
->product
== CODA_DX6
)
1792 if (ctx
->psbuf
.vaddr
) {
1793 v4l2_err(&dev
->v4l2_dev
, "psmembuf still allocated\n");
1796 if (ctx
->slicebuf
.vaddr
) {
1797 v4l2_err(&dev
->v4l2_dev
, "slicebuf still allocated\n");
1800 if (ctx
->workbuf
.vaddr
) {
1801 v4l2_err(&dev
->v4l2_dev
, "context buffer still allocated\n");
1806 if (q_data
->fourcc
== V4L2_PIX_FMT_H264
) {
1807 /* worst case slice size */
1808 size
= (DIV_ROUND_UP(q_data
->width
, 16) *
1809 DIV_ROUND_UP(q_data
->height
, 16)) * 3200 / 8 + 512;
1810 ret
= coda_alloc_context_buf(ctx
, &ctx
->slicebuf
, size
, "slicebuf");
1812 v4l2_err(&dev
->v4l2_dev
, "failed to allocate %d byte slice buffer",
1813 ctx
->slicebuf
.size
);
1818 if (dev
->devtype
->product
== CODA_7541
) {
1819 ret
= coda_alloc_context_buf(ctx
, &ctx
->psbuf
, CODA7_PS_BUF_SIZE
, "psbuf");
1821 v4l2_err(&dev
->v4l2_dev
, "failed to allocate psmem buffer");
1826 size
= dev
->devtype
->workbuf_size
;
1827 if (dev
->devtype
->product
== CODA_960
&&
1828 q_data
->fourcc
== V4L2_PIX_FMT_H264
)
1829 size
+= CODA9_PS_SAVE_SIZE
;
1830 ret
= coda_alloc_context_buf(ctx
, &ctx
->workbuf
, size
, "workbuf");
1832 v4l2_err(&dev
->v4l2_dev
, "failed to allocate %d byte context buffer",
1840 coda_free_context_buffers(ctx
);
1844 static int __coda_start_decoding(struct coda_ctx
*ctx
)
1846 struct coda_q_data
*q_data_src
, *q_data_dst
;
1847 u32 bitstream_buf
, bitstream_size
;
1848 struct coda_dev
*dev
= ctx
->dev
;
1854 /* Start decoding */
1855 q_data_src
= get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_OUTPUT
);
1856 q_data_dst
= get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_CAPTURE
);
1857 bitstream_buf
= ctx
->bitstream
.paddr
;
1858 bitstream_size
= ctx
->bitstream
.size
;
1859 src_fourcc
= q_data_src
->fourcc
;
1861 coda_write(dev
, ctx
->parabuf
.paddr
, CODA_REG_BIT_PARA_BUF_ADDR
);
1863 /* Update coda bitstream read and write pointers from kfifo */
1864 coda_kfifo_sync_to_device_full(ctx
);
1866 ctx
->display_idx
= -1;
1867 ctx
->frm_dis_flg
= 0;
1868 coda_write(dev
, 0, CODA_REG_BIT_FRM_DIS_FLG(ctx
->reg_idx
));
1870 coda_write(dev
, CODA_BIT_DEC_SEQ_INIT_ESCAPE
,
1871 CODA_REG_BIT_BIT_STREAM_PARAM
);
1873 coda_write(dev
, bitstream_buf
, CODA_CMD_DEC_SEQ_BB_START
);
1874 coda_write(dev
, bitstream_size
/ 1024, CODA_CMD_DEC_SEQ_BB_SIZE
);
1876 if ((dev
->devtype
->product
== CODA_7541
) ||
1877 (dev
->devtype
->product
== CODA_960
))
1878 val
|= CODA_REORDER_ENABLE
;
1879 coda_write(dev
, val
, CODA_CMD_DEC_SEQ_OPTION
);
1881 ctx
->params
.codec_mode
= ctx
->codec
->mode
;
1882 if (dev
->devtype
->product
== CODA_960
&&
1883 src_fourcc
== V4L2_PIX_FMT_MPEG4
)
1884 ctx
->params
.codec_mode_aux
= CODA_MP4_AUX_MPEG4
;
1886 ctx
->params
.codec_mode_aux
= 0;
1887 if (src_fourcc
== V4L2_PIX_FMT_H264
) {
1888 if (dev
->devtype
->product
== CODA_7541
) {
1889 coda_write(dev
, ctx
->psbuf
.paddr
,
1890 CODA_CMD_DEC_SEQ_PS_BB_START
);
1891 coda_write(dev
, (CODA7_PS_BUF_SIZE
/ 1024),
1892 CODA_CMD_DEC_SEQ_PS_BB_SIZE
);
1894 if (dev
->devtype
->product
== CODA_960
) {
1895 coda_write(dev
, 0, CODA_CMD_DEC_SEQ_X264_MV_EN
);
1896 coda_write(dev
, 512, CODA_CMD_DEC_SEQ_SPP_CHUNK_SIZE
);
1899 if (dev
->devtype
->product
!= CODA_960
) {
1900 coda_write(dev
, 0, CODA_CMD_DEC_SEQ_SRC_SIZE
);
1903 if (coda_command_sync(ctx
, CODA_COMMAND_SEQ_INIT
)) {
1904 v4l2_err(&dev
->v4l2_dev
, "CODA_COMMAND_SEQ_INIT timeout\n");
1905 coda_write(dev
, 0, CODA_REG_BIT_BIT_STREAM_PARAM
);
1909 /* Update kfifo out pointer from coda bitstream read pointer */
1910 coda_kfifo_sync_from_device(ctx
);
1912 coda_write(dev
, 0, CODA_REG_BIT_BIT_STREAM_PARAM
);
1914 if (coda_read(dev
, CODA_RET_DEC_SEQ_SUCCESS
) == 0) {
1915 v4l2_err(&dev
->v4l2_dev
,
1916 "CODA_COMMAND_SEQ_INIT failed, error code = %d\n",
1917 coda_read(dev
, CODA_RET_DEC_SEQ_ERR_REASON
));
1921 val
= coda_read(dev
, CODA_RET_DEC_SEQ_SRC_SIZE
);
1922 if (dev
->devtype
->product
== CODA_DX6
) {
1923 width
= (val
>> CODADX6_PICWIDTH_OFFSET
) & CODADX6_PICWIDTH_MASK
;
1924 height
= val
& CODADX6_PICHEIGHT_MASK
;
1926 width
= (val
>> CODA7_PICWIDTH_OFFSET
) & CODA7_PICWIDTH_MASK
;
1927 height
= val
& CODA7_PICHEIGHT_MASK
;
1930 if (width
> q_data_dst
->width
|| height
> q_data_dst
->height
) {
1931 v4l2_err(&dev
->v4l2_dev
, "stream is %dx%d, not %dx%d\n",
1932 width
, height
, q_data_dst
->width
, q_data_dst
->height
);
1936 width
= round_up(width
, 16);
1937 height
= round_up(height
, 16);
1939 v4l2_dbg(1, coda_debug
, &dev
->v4l2_dev
, "%s instance %d now: %dx%d\n",
1940 __func__
, ctx
->idx
, width
, height
);
1942 ctx
->num_internal_frames
= coda_read(dev
, CODA_RET_DEC_SEQ_FRAME_NEED
);
1943 if (ctx
->num_internal_frames
> CODA_MAX_FRAMEBUFFERS
) {
1944 v4l2_err(&dev
->v4l2_dev
,
1945 "not enough framebuffers to decode (%d < %d)\n",
1946 CODA_MAX_FRAMEBUFFERS
, ctx
->num_internal_frames
);
1950 if (src_fourcc
== V4L2_PIX_FMT_H264
) {
1954 left_right
= coda_read(dev
, CODA_RET_DEC_SEQ_CROP_LEFT_RIGHT
);
1955 top_bottom
= coda_read(dev
, CODA_RET_DEC_SEQ_CROP_TOP_BOTTOM
);
1957 q_data_dst
->rect
.left
= (left_right
>> 10) & 0x3ff;
1958 q_data_dst
->rect
.top
= (top_bottom
>> 10) & 0x3ff;
1959 q_data_dst
->rect
.width
= width
- q_data_dst
->rect
.left
-
1960 (left_right
& 0x3ff);
1961 q_data_dst
->rect
.height
= height
- q_data_dst
->rect
.top
-
1962 (top_bottom
& 0x3ff);
1965 ret
= coda_alloc_framebuffers(ctx
, q_data_dst
, src_fourcc
);
1969 /* Tell the decoder how many frame buffers we allocated. */
1970 coda_write(dev
, ctx
->num_internal_frames
, CODA_CMD_SET_FRAME_BUF_NUM
);
1971 coda_write(dev
, width
, CODA_CMD_SET_FRAME_BUF_STRIDE
);
1973 if (dev
->devtype
->product
!= CODA_DX6
) {
1974 /* Set secondary AXI IRAM */
1975 coda_setup_iram(ctx
);
1977 coda_write(dev
, ctx
->iram_info
.buf_bit_use
,
1978 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR
);
1979 coda_write(dev
, ctx
->iram_info
.buf_ip_ac_dc_use
,
1980 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR
);
1981 coda_write(dev
, ctx
->iram_info
.buf_dbk_y_use
,
1982 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR
);
1983 coda_write(dev
, ctx
->iram_info
.buf_dbk_c_use
,
1984 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR
);
1985 coda_write(dev
, ctx
->iram_info
.buf_ovl_use
,
1986 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR
);
1987 if (dev
->devtype
->product
== CODA_960
)
1988 coda_write(dev
, ctx
->iram_info
.buf_btp_use
,
1989 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR
);
1992 if (dev
->devtype
->product
== CODA_960
) {
1993 coda_write(dev
, -1, CODA9_CMD_SET_FRAME_DELAY
);
1995 coda_write(dev
, 0x20262024, CODA9_CMD_SET_FRAME_CACHE_SIZE
);
1996 coda_write(dev
, 2 << CODA9_CACHE_PAGEMERGE_OFFSET
|
1997 32 << CODA9_CACHE_LUMA_BUFFER_SIZE_OFFSET
|
1998 8 << CODA9_CACHE_CB_BUFFER_SIZE_OFFSET
|
1999 8 << CODA9_CACHE_CR_BUFFER_SIZE_OFFSET
,
2000 CODA9_CMD_SET_FRAME_CACHE_CONFIG
);
2003 if (src_fourcc
== V4L2_PIX_FMT_H264
) {
2004 coda_write(dev
, ctx
->slicebuf
.paddr
,
2005 CODA_CMD_SET_FRAME_SLICE_BB_START
);
2006 coda_write(dev
, ctx
->slicebuf
.size
/ 1024,
2007 CODA_CMD_SET_FRAME_SLICE_BB_SIZE
);
2010 if (dev
->devtype
->product
== CODA_7541
) {
2011 int max_mb_x
= 1920 / 16;
2012 int max_mb_y
= 1088 / 16;
2013 int max_mb_num
= max_mb_x
* max_mb_y
;
2015 coda_write(dev
, max_mb_num
<< 16 | max_mb_x
<< 8 | max_mb_y
,
2016 CODA7_CMD_SET_FRAME_MAX_DEC_SIZE
);
2017 } else if (dev
->devtype
->product
== CODA_960
) {
2018 int max_mb_x
= 1920 / 16;
2019 int max_mb_y
= 1088 / 16;
2020 int max_mb_num
= max_mb_x
* max_mb_y
;
2022 coda_write(dev
, max_mb_num
<< 16 | max_mb_x
<< 8 | max_mb_y
,
2023 CODA9_CMD_SET_FRAME_MAX_DEC_SIZE
);
2026 if (coda_command_sync(ctx
, CODA_COMMAND_SET_FRAME_BUF
)) {
2027 v4l2_err(&ctx
->dev
->v4l2_dev
,
2028 "CODA_COMMAND_SET_FRAME_BUF timeout\n");
2035 static int coda_start_decoding(struct coda_ctx
*ctx
)
2037 struct coda_dev
*dev
= ctx
->dev
;
2040 mutex_lock(&dev
->coda_mutex
);
2041 ret
= __coda_start_decoding(ctx
);
2042 mutex_unlock(&dev
->coda_mutex
);
2047 static int coda_encode_header(struct coda_ctx
*ctx
, struct vb2_buffer
*buf
,
2048 int header_code
, u8
*header
, int *size
)
2050 struct coda_dev
*dev
= ctx
->dev
;
2055 if (dev
->devtype
->product
== CODA_960
)
2056 memset(vb2_plane_vaddr(buf
, 0), 0, 64);
2058 coda_write(dev
, vb2_dma_contig_plane_dma_addr(buf
, 0),
2059 CODA_CMD_ENC_HEADER_BB_START
);
2060 bufsize
= vb2_plane_size(buf
, 0);
2061 if (dev
->devtype
->product
== CODA_960
)
2063 coda_write(dev
, bufsize
, CODA_CMD_ENC_HEADER_BB_SIZE
);
2064 coda_write(dev
, header_code
, CODA_CMD_ENC_HEADER_CODE
);
2065 ret
= coda_command_sync(ctx
, CODA_COMMAND_ENCODE_HEADER
);
2067 v4l2_err(&dev
->v4l2_dev
, "CODA_COMMAND_ENCODE_HEADER timeout\n");
2071 if (dev
->devtype
->product
== CODA_960
) {
2072 for (i
= 63; i
> 0; i
--)
2073 if (((char *)vb2_plane_vaddr(buf
, 0))[i
] != 0)
2077 *size
= coda_read(dev
, CODA_REG_BIT_WR_PTR(ctx
->reg_idx
)) -
2078 coda_read(dev
, CODA_CMD_ENC_HEADER_BB_START
);
2080 memcpy(header
, vb2_plane_vaddr(buf
, 0), *size
);
2085 static int coda_start_encoding(struct coda_ctx
*ctx
);
2087 static int coda_start_streaming(struct vb2_queue
*q
, unsigned int count
)
2089 struct coda_ctx
*ctx
= vb2_get_drv_priv(q
);
2090 struct v4l2_device
*v4l2_dev
= &ctx
->dev
->v4l2_dev
;
2091 struct coda_q_data
*q_data_src
, *q_data_dst
;
2095 q_data_src
= get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_OUTPUT
);
2096 if (q
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
) {
2097 if (q_data_src
->fourcc
== V4L2_PIX_FMT_H264
) {
2098 /* copy the buffers that where queued before streamon */
2099 mutex_lock(&ctx
->bitstream_mutex
);
2100 coda_fill_bitstream(ctx
);
2101 mutex_unlock(&ctx
->bitstream_mutex
);
2103 if (coda_get_bitstream_payload(ctx
) < 512)
2110 ctx
->streamon_out
= 1;
2115 ctx
->streamon_cap
= 1;
2118 /* Don't start the coda unless both queues are on */
2119 if (!(ctx
->streamon_out
& ctx
->streamon_cap
))
2122 /* Allow decoder device_run with no new buffers queued */
2123 if (ctx
->inst_type
== CODA_INST_DECODER
)
2124 v4l2_m2m_set_src_buffered(ctx
->fh
.m2m_ctx
, true);
2126 ctx
->gopcounter
= ctx
->params
.gop_size
- 1;
2127 q_data_dst
= get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_CAPTURE
);
2128 dst_fourcc
= q_data_dst
->fourcc
;
2130 ctx
->codec
= coda_find_codec(ctx
->dev
, q_data_src
->fourcc
,
2131 q_data_dst
->fourcc
);
2133 v4l2_err(v4l2_dev
, "couldn't tell instance type.\n");
2137 /* Allocate per-instance buffers */
2138 ret
= coda_alloc_context_buffers(ctx
, q_data_src
);
2142 ret
= ctx
->ops
->start_streaming(ctx
);
2143 if (ctx
->inst_type
== CODA_INST_DECODER
) {
2150 ctx
->initialized
= 1;
2154 static int coda_start_encoding(struct coda_ctx
*ctx
)
2156 struct coda_dev
*dev
= ctx
->dev
;
2157 struct v4l2_device
*v4l2_dev
= &dev
->v4l2_dev
;
2158 struct coda_q_data
*q_data_src
, *q_data_dst
;
2159 u32 bitstream_buf
, bitstream_size
;
2160 struct vb2_buffer
*buf
;
2161 int gamma
, ret
, value
;
2164 q_data_src
= get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_OUTPUT
);
2165 q_data_dst
= get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_CAPTURE
);
2166 dst_fourcc
= q_data_dst
->fourcc
;
2168 buf
= v4l2_m2m_next_dst_buf(ctx
->fh
.m2m_ctx
);
2169 bitstream_buf
= vb2_dma_contig_plane_dma_addr(buf
, 0);
2170 bitstream_size
= q_data_dst
->sizeimage
;
2172 if (!coda_is_initialized(dev
)) {
2173 v4l2_err(v4l2_dev
, "coda is not initialized.\n");
2177 mutex_lock(&dev
->coda_mutex
);
2179 coda_write(dev
, ctx
->parabuf
.paddr
, CODA_REG_BIT_PARA_BUF_ADDR
);
2180 coda_write(dev
, bitstream_buf
, CODA_REG_BIT_RD_PTR(ctx
->reg_idx
));
2181 coda_write(dev
, bitstream_buf
, CODA_REG_BIT_WR_PTR(ctx
->reg_idx
));
2182 switch (dev
->devtype
->product
) {
2184 coda_write(dev
, CODADX6_STREAM_BUF_DYNALLOC_EN
|
2185 CODADX6_STREAM_BUF_PIC_RESET
, CODA_REG_BIT_STREAM_CTRL
);
2188 coda_write(dev
, 0, CODA9_GDI_WPROT_RGN_EN
);
2191 coda_write(dev
, CODA7_STREAM_BUF_DYNALLOC_EN
|
2192 CODA7_STREAM_BUF_PIC_RESET
, CODA_REG_BIT_STREAM_CTRL
);
2196 value
= coda_read(dev
, CODA_REG_BIT_FRAME_MEM_CTRL
);
2197 value
&= ~(1 << 2 | 0x7 << 9);
2198 ctx
->frame_mem_ctrl
= value
;
2199 coda_write(dev
, value
, CODA_REG_BIT_FRAME_MEM_CTRL
);
2201 if (dev
->devtype
->product
== CODA_DX6
) {
2202 /* Configure the coda */
2203 coda_write(dev
, dev
->iram
.paddr
, CODADX6_REG_BIT_SEARCH_RAM_BASE_ADDR
);
2206 /* Could set rotation here if needed */
2207 switch (dev
->devtype
->product
) {
2209 value
= (q_data_src
->width
& CODADX6_PICWIDTH_MASK
) << CODADX6_PICWIDTH_OFFSET
;
2210 value
|= (q_data_src
->height
& CODADX6_PICHEIGHT_MASK
) << CODA_PICHEIGHT_OFFSET
;
2213 if (dst_fourcc
== V4L2_PIX_FMT_H264
) {
2214 value
= (round_up(q_data_src
->width
, 16) &
2215 CODA7_PICWIDTH_MASK
) << CODA7_PICWIDTH_OFFSET
;
2216 value
|= (round_up(q_data_src
->height
, 16) &
2217 CODA7_PICHEIGHT_MASK
) << CODA_PICHEIGHT_OFFSET
;
2222 value
= (q_data_src
->width
& CODA7_PICWIDTH_MASK
) << CODA7_PICWIDTH_OFFSET
;
2223 value
|= (q_data_src
->height
& CODA7_PICHEIGHT_MASK
) << CODA_PICHEIGHT_OFFSET
;
2225 coda_write(dev
, value
, CODA_CMD_ENC_SEQ_SRC_SIZE
);
2226 coda_write(dev
, ctx
->params
.framerate
,
2227 CODA_CMD_ENC_SEQ_SRC_F_RATE
);
2229 ctx
->params
.codec_mode
= ctx
->codec
->mode
;
2230 switch (dst_fourcc
) {
2231 case V4L2_PIX_FMT_MPEG4
:
2232 if (dev
->devtype
->product
== CODA_960
)
2233 coda_write(dev
, CODA9_STD_MPEG4
, CODA_CMD_ENC_SEQ_COD_STD
);
2235 coda_write(dev
, CODA_STD_MPEG4
, CODA_CMD_ENC_SEQ_COD_STD
);
2236 coda_write(dev
, 0, CODA_CMD_ENC_SEQ_MP4_PARA
);
2238 case V4L2_PIX_FMT_H264
:
2239 if (dev
->devtype
->product
== CODA_960
)
2240 coda_write(dev
, CODA9_STD_H264
, CODA_CMD_ENC_SEQ_COD_STD
);
2242 coda_write(dev
, CODA_STD_H264
, CODA_CMD_ENC_SEQ_COD_STD
);
2243 if (ctx
->params
.h264_deblk_enabled
) {
2244 value
= ((ctx
->params
.h264_deblk_alpha
&
2245 CODA_264PARAM_DEBLKFILTEROFFSETALPHA_MASK
) <<
2246 CODA_264PARAM_DEBLKFILTEROFFSETALPHA_OFFSET
) |
2247 ((ctx
->params
.h264_deblk_beta
&
2248 CODA_264PARAM_DEBLKFILTEROFFSETBETA_MASK
) <<
2249 CODA_264PARAM_DEBLKFILTEROFFSETBETA_OFFSET
);
2251 value
= 1 << CODA_264PARAM_DISABLEDEBLK_OFFSET
;
2253 coda_write(dev
, value
, CODA_CMD_ENC_SEQ_264_PARA
);
2257 "dst format (0x%08x) invalid.\n", dst_fourcc
);
2262 switch (ctx
->params
.slice_mode
) {
2263 case V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE
:
2266 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB
:
2267 value
= (ctx
->params
.slice_max_mb
& CODA_SLICING_SIZE_MASK
) << CODA_SLICING_SIZE_OFFSET
;
2268 value
|= (1 & CODA_SLICING_UNIT_MASK
) << CODA_SLICING_UNIT_OFFSET
;
2269 value
|= 1 & CODA_SLICING_MODE_MASK
;
2271 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES
:
2272 value
= (ctx
->params
.slice_max_bits
& CODA_SLICING_SIZE_MASK
) << CODA_SLICING_SIZE_OFFSET
;
2273 value
|= (0 & CODA_SLICING_UNIT_MASK
) << CODA_SLICING_UNIT_OFFSET
;
2274 value
|= 1 & CODA_SLICING_MODE_MASK
;
2277 coda_write(dev
, value
, CODA_CMD_ENC_SEQ_SLICE_MODE
);
2278 value
= ctx
->params
.gop_size
& CODA_GOP_SIZE_MASK
;
2279 coda_write(dev
, value
, CODA_CMD_ENC_SEQ_GOP_SIZE
);
2281 if (ctx
->params
.bitrate
) {
2282 /* Rate control enabled */
2283 value
= (ctx
->params
.bitrate
& CODA_RATECONTROL_BITRATE_MASK
) << CODA_RATECONTROL_BITRATE_OFFSET
;
2284 value
|= 1 & CODA_RATECONTROL_ENABLE_MASK
;
2285 if (dev
->devtype
->product
== CODA_960
)
2286 value
|= BIT(31); /* disable autoskip */
2290 coda_write(dev
, value
, CODA_CMD_ENC_SEQ_RC_PARA
);
2292 coda_write(dev
, 0, CODA_CMD_ENC_SEQ_RC_BUF_SIZE
);
2293 coda_write(dev
, ctx
->params
.intra_refresh
,
2294 CODA_CMD_ENC_SEQ_INTRA_REFRESH
);
2296 coda_write(dev
, bitstream_buf
, CODA_CMD_ENC_SEQ_BB_START
);
2297 coda_write(dev
, bitstream_size
/ 1024, CODA_CMD_ENC_SEQ_BB_SIZE
);
2301 if (dev
->devtype
->product
== CODA_960
)
2302 gamma
= CODA9_DEFAULT_GAMMA
;
2304 gamma
= CODA_DEFAULT_GAMMA
;
2306 coda_write(dev
, (gamma
& CODA_GAMMA_MASK
) << CODA_GAMMA_OFFSET
,
2307 CODA_CMD_ENC_SEQ_RC_GAMMA
);
2310 if (ctx
->params
.h264_min_qp
|| ctx
->params
.h264_max_qp
) {
2312 ctx
->params
.h264_min_qp
<< CODA_QPMIN_OFFSET
|
2313 ctx
->params
.h264_max_qp
<< CODA_QPMAX_OFFSET
,
2314 CODA_CMD_ENC_SEQ_RC_QP_MIN_MAX
);
2316 if (dev
->devtype
->product
== CODA_960
) {
2317 if (ctx
->params
.h264_max_qp
)
2318 value
|= 1 << CODA9_OPTION_RCQPMAX_OFFSET
;
2319 if (CODA_DEFAULT_GAMMA
> 0)
2320 value
|= 1 << CODA9_OPTION_GAMMA_OFFSET
;
2322 if (CODA_DEFAULT_GAMMA
> 0) {
2323 if (dev
->devtype
->product
== CODA_DX6
)
2324 value
|= 1 << CODADX6_OPTION_GAMMA_OFFSET
;
2326 value
|= 1 << CODA7_OPTION_GAMMA_OFFSET
;
2328 if (ctx
->params
.h264_min_qp
)
2329 value
|= 1 << CODA7_OPTION_RCQPMIN_OFFSET
;
2330 if (ctx
->params
.h264_max_qp
)
2331 value
|= 1 << CODA7_OPTION_RCQPMAX_OFFSET
;
2333 coda_write(dev
, value
, CODA_CMD_ENC_SEQ_OPTION
);
2335 coda_write(dev
, 0, CODA_CMD_ENC_SEQ_RC_INTERVAL_MODE
);
2337 coda_setup_iram(ctx
);
2339 if (dst_fourcc
== V4L2_PIX_FMT_H264
) {
2340 switch (dev
->devtype
->product
) {
2342 value
= FMO_SLICE_SAVE_BUF_SIZE
<< 7;
2343 coda_write(dev
, value
, CODADX6_CMD_ENC_SEQ_FMO
);
2346 coda_write(dev
, ctx
->iram_info
.search_ram_paddr
,
2347 CODA7_CMD_ENC_SEQ_SEARCH_BASE
);
2348 coda_write(dev
, ctx
->iram_info
.search_ram_size
,
2349 CODA7_CMD_ENC_SEQ_SEARCH_SIZE
);
2352 coda_write(dev
, 0, CODA9_CMD_ENC_SEQ_ME_OPTION
);
2353 coda_write(dev
, 0, CODA9_CMD_ENC_SEQ_INTRA_WEIGHT
);
2357 ret
= coda_command_sync(ctx
, CODA_COMMAND_SEQ_INIT
);
2359 v4l2_err(v4l2_dev
, "CODA_COMMAND_SEQ_INIT timeout\n");
2363 if (coda_read(dev
, CODA_RET_ENC_SEQ_SUCCESS
) == 0) {
2364 v4l2_err(v4l2_dev
, "CODA_COMMAND_SEQ_INIT failed\n");
2369 if (dev
->devtype
->product
== CODA_960
)
2370 ctx
->num_internal_frames
= 4;
2372 ctx
->num_internal_frames
= 2;
2373 ret
= coda_alloc_framebuffers(ctx
, q_data_src
, dst_fourcc
);
2375 v4l2_err(v4l2_dev
, "failed to allocate framebuffers\n");
2379 coda_write(dev
, ctx
->num_internal_frames
, CODA_CMD_SET_FRAME_BUF_NUM
);
2380 coda_write(dev
, q_data_src
->bytesperline
,
2381 CODA_CMD_SET_FRAME_BUF_STRIDE
);
2382 if (dev
->devtype
->product
== CODA_7541
) {
2383 coda_write(dev
, q_data_src
->bytesperline
,
2384 CODA7_CMD_SET_FRAME_SOURCE_BUF_STRIDE
);
2386 if (dev
->devtype
->product
!= CODA_DX6
) {
2387 coda_write(dev
, ctx
->iram_info
.buf_bit_use
,
2388 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR
);
2389 coda_write(dev
, ctx
->iram_info
.buf_ip_ac_dc_use
,
2390 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR
);
2391 coda_write(dev
, ctx
->iram_info
.buf_dbk_y_use
,
2392 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR
);
2393 coda_write(dev
, ctx
->iram_info
.buf_dbk_c_use
,
2394 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR
);
2395 coda_write(dev
, ctx
->iram_info
.buf_ovl_use
,
2396 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR
);
2397 if (dev
->devtype
->product
== CODA_960
) {
2398 coda_write(dev
, ctx
->iram_info
.buf_btp_use
,
2399 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR
);
2402 coda_write(dev
, ctx
->internal_frames
[2].paddr
, CODA9_CMD_SET_FRAME_SUBSAMP_A
);
2403 coda_write(dev
, ctx
->internal_frames
[3].paddr
, CODA9_CMD_SET_FRAME_SUBSAMP_B
);
2407 ret
= coda_command_sync(ctx
, CODA_COMMAND_SET_FRAME_BUF
);
2409 v4l2_err(v4l2_dev
, "CODA_COMMAND_SET_FRAME_BUF timeout\n");
2413 /* Save stream headers */
2414 buf
= v4l2_m2m_next_dst_buf(ctx
->fh
.m2m_ctx
);
2415 switch (dst_fourcc
) {
2416 case V4L2_PIX_FMT_H264
:
2418 * Get SPS in the first frame and copy it to an
2419 * intermediate buffer.
2421 ret
= coda_encode_header(ctx
, buf
, CODA_HEADER_H264_SPS
,
2422 &ctx
->vpu_header
[0][0],
2423 &ctx
->vpu_header_size
[0]);
2428 * Get PPS in the first frame and copy it to an
2429 * intermediate buffer.
2431 ret
= coda_encode_header(ctx
, buf
, CODA_HEADER_H264_PPS
,
2432 &ctx
->vpu_header
[1][0],
2433 &ctx
->vpu_header_size
[1]);
2438 * Length of H.264 headers is variable and thus it might not be
2439 * aligned for the coda to append the encoded frame. In that is
2440 * the case a filler NAL must be added to header 2.
2442 ctx
->vpu_header_size
[2] = coda_h264_padding(
2443 (ctx
->vpu_header_size
[0] +
2444 ctx
->vpu_header_size
[1]),
2445 ctx
->vpu_header
[2]);
2447 case V4L2_PIX_FMT_MPEG4
:
2449 * Get VOS in the first frame and copy it to an
2450 * intermediate buffer
2452 ret
= coda_encode_header(ctx
, buf
, CODA_HEADER_MP4V_VOS
,
2453 &ctx
->vpu_header
[0][0],
2454 &ctx
->vpu_header_size
[0]);
2458 ret
= coda_encode_header(ctx
, buf
, CODA_HEADER_MP4V_VIS
,
2459 &ctx
->vpu_header
[1][0],
2460 &ctx
->vpu_header_size
[1]);
2464 ret
= coda_encode_header(ctx
, buf
, CODA_HEADER_MP4V_VOL
,
2465 &ctx
->vpu_header
[2][0],
2466 &ctx
->vpu_header_size
[2]);
2471 /* No more formats need to save headers at the moment */
2476 mutex_unlock(&dev
->coda_mutex
);
2480 static void coda_stop_streaming(struct vb2_queue
*q
)
2482 struct coda_ctx
*ctx
= vb2_get_drv_priv(q
);
2483 struct coda_dev
*dev
= ctx
->dev
;
2485 if (q
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
) {
2486 v4l2_dbg(1, coda_debug
, &dev
->v4l2_dev
,
2487 "%s: output\n", __func__
);
2488 ctx
->streamon_out
= 0;
2490 if (ctx
->inst_type
== CODA_INST_DECODER
&&
2491 coda_isbusy(dev
) && ctx
->idx
== coda_read(dev
, CODA_REG_BIT_RUN_INDEX
)) {
2492 /* if this decoder instance is running, set the stream end flag */
2493 if (dev
->devtype
->product
== CODA_960
) {
2494 u32 val
= coda_read(dev
, CODA_REG_BIT_BIT_STREAM_PARAM
);
2496 val
|= CODA_BIT_STREAM_END_FLAG
;
2497 coda_write(dev
, val
, CODA_REG_BIT_BIT_STREAM_PARAM
);
2498 ctx
->bit_stream_param
= val
;
2501 ctx
->bit_stream_param
|= CODA_BIT_STREAM_END_FLAG
;
2505 v4l2_dbg(1, coda_debug
, &dev
->v4l2_dev
,
2506 "%s: capture\n", __func__
);
2507 ctx
->streamon_cap
= 0;
2510 ctx
->sequence_offset
= 0;
2513 if (!ctx
->streamon_out
&& !ctx
->streamon_cap
) {
2514 struct coda_timestamp
*ts
;
2516 while (!list_empty(&ctx
->timestamp_list
)) {
2517 ts
= list_first_entry(&ctx
->timestamp_list
,
2518 struct coda_timestamp
, list
);
2519 list_del(&ts
->list
);
2522 kfifo_init(&ctx
->bitstream_fifo
,
2523 ctx
->bitstream
.vaddr
, ctx
->bitstream
.size
);
2524 ctx
->runcounter
= 0;
2528 static const struct vb2_ops coda_qops
= {
2529 .queue_setup
= coda_queue_setup
,
2530 .buf_prepare
= coda_buf_prepare
,
2531 .buf_queue
= coda_buf_queue
,
2532 .start_streaming
= coda_start_streaming
,
2533 .stop_streaming
= coda_stop_streaming
,
2534 .wait_prepare
= vb2_ops_wait_prepare
,
2535 .wait_finish
= vb2_ops_wait_finish
,
2538 static int coda_s_ctrl(struct v4l2_ctrl
*ctrl
)
2540 struct coda_ctx
*ctx
=
2541 container_of(ctrl
->handler
, struct coda_ctx
, ctrls
);
2543 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
2544 "s_ctrl: id = %d, val = %d\n", ctrl
->id
, ctrl
->val
);
2547 case V4L2_CID_HFLIP
:
2549 ctx
->params
.rot_mode
|= CODA_MIR_HOR
;
2551 ctx
->params
.rot_mode
&= ~CODA_MIR_HOR
;
2553 case V4L2_CID_VFLIP
:
2555 ctx
->params
.rot_mode
|= CODA_MIR_VER
;
2557 ctx
->params
.rot_mode
&= ~CODA_MIR_VER
;
2559 case V4L2_CID_MPEG_VIDEO_BITRATE
:
2560 ctx
->params
.bitrate
= ctrl
->val
/ 1000;
2562 case V4L2_CID_MPEG_VIDEO_GOP_SIZE
:
2563 ctx
->params
.gop_size
= ctrl
->val
;
2565 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP
:
2566 ctx
->params
.h264_intra_qp
= ctrl
->val
;
2568 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP
:
2569 ctx
->params
.h264_inter_qp
= ctrl
->val
;
2571 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP
:
2572 ctx
->params
.h264_min_qp
= ctrl
->val
;
2574 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP
:
2575 ctx
->params
.h264_max_qp
= ctrl
->val
;
2577 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA
:
2578 ctx
->params
.h264_deblk_alpha
= ctrl
->val
;
2580 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA
:
2581 ctx
->params
.h264_deblk_beta
= ctrl
->val
;
2583 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE
:
2584 ctx
->params
.h264_deblk_enabled
= (ctrl
->val
==
2585 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED
);
2587 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP
:
2588 ctx
->params
.mpeg4_intra_qp
= ctrl
->val
;
2590 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP
:
2591 ctx
->params
.mpeg4_inter_qp
= ctrl
->val
;
2593 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE
:
2594 ctx
->params
.slice_mode
= ctrl
->val
;
2596 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB
:
2597 ctx
->params
.slice_max_mb
= ctrl
->val
;
2599 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES
:
2600 ctx
->params
.slice_max_bits
= ctrl
->val
* 8;
2602 case V4L2_CID_MPEG_VIDEO_HEADER_MODE
:
2604 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB
:
2605 ctx
->params
.intra_refresh
= ctrl
->val
;
2608 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
2609 "Invalid control, id=%d, val=%d\n",
2610 ctrl
->id
, ctrl
->val
);
2617 static const struct v4l2_ctrl_ops coda_ctrl_ops
= {
2618 .s_ctrl
= coda_s_ctrl
,
2621 static int coda_ctrls_setup(struct coda_ctx
*ctx
)
2623 v4l2_ctrl_handler_init(&ctx
->ctrls
, 9);
2625 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
2626 V4L2_CID_HFLIP
, 0, 1, 1, 0);
2627 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
2628 V4L2_CID_VFLIP
, 0, 1, 1, 0);
2629 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
2630 V4L2_CID_MPEG_VIDEO_BITRATE
, 0, 32767000, 1, 0);
2631 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
2632 V4L2_CID_MPEG_VIDEO_GOP_SIZE
, 1, 60, 1, 16);
2633 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
2634 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP
, 0, 51, 1, 25);
2635 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
2636 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP
, 0, 51, 1, 25);
2637 if (ctx
->dev
->devtype
->product
!= CODA_960
) {
2638 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
2639 V4L2_CID_MPEG_VIDEO_H264_MIN_QP
, 0, 51, 1, 12);
2641 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
2642 V4L2_CID_MPEG_VIDEO_H264_MAX_QP
, 0, 51, 1, 51);
2643 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
2644 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA
, 0, 15, 1, 0);
2645 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
2646 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA
, 0, 15, 1, 0);
2647 v4l2_ctrl_new_std_menu(&ctx
->ctrls
, &coda_ctrl_ops
,
2648 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE
,
2649 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED
, 0x0,
2650 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED
);
2651 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
2652 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP
, 1, 31, 1, 2);
2653 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
2654 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP
, 1, 31, 1, 2);
2655 v4l2_ctrl_new_std_menu(&ctx
->ctrls
, &coda_ctrl_ops
,
2656 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE
,
2657 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES
, 0x0,
2658 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE
);
2659 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
2660 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB
, 1, 0x3fffffff, 1, 1);
2661 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
2662 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES
, 1, 0x3fffffff, 1, 500);
2663 v4l2_ctrl_new_std_menu(&ctx
->ctrls
, &coda_ctrl_ops
,
2664 V4L2_CID_MPEG_VIDEO_HEADER_MODE
,
2665 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME
,
2666 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE
),
2667 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME
);
2668 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
2669 V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB
, 0, 1920 * 1088 / 256, 1, 0);
2671 if (ctx
->ctrls
.error
) {
2672 v4l2_err(&ctx
->dev
->v4l2_dev
, "control initialization error (%d)",
2677 return v4l2_ctrl_handler_setup(&ctx
->ctrls
);
2680 static int coda_queue_init(struct coda_ctx
*ctx
, struct vb2_queue
*vq
)
2683 vq
->ops
= &coda_qops
;
2684 vq
->buf_struct_size
= sizeof(struct v4l2_m2m_buffer
);
2685 vq
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_COPY
;
2686 vq
->lock
= &ctx
->dev
->dev_mutex
;
2688 return vb2_queue_init(vq
);
2691 static int coda_encoder_queue_init(void *priv
, struct vb2_queue
*src_vq
,
2692 struct vb2_queue
*dst_vq
)
2696 src_vq
->type
= V4L2_BUF_TYPE_VIDEO_OUTPUT
;
2697 src_vq
->io_modes
= VB2_DMABUF
| VB2_MMAP
;
2698 src_vq
->mem_ops
= &vb2_dma_contig_memops
;
2700 ret
= coda_queue_init(priv
, src_vq
);
2704 dst_vq
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
2705 dst_vq
->io_modes
= VB2_DMABUF
| VB2_MMAP
;
2706 dst_vq
->mem_ops
= &vb2_dma_contig_memops
;
2708 return coda_queue_init(priv
, dst_vq
);
2711 static int coda_decoder_queue_init(void *priv
, struct vb2_queue
*src_vq
,
2712 struct vb2_queue
*dst_vq
)
2716 src_vq
->type
= V4L2_BUF_TYPE_VIDEO_OUTPUT
;
2717 src_vq
->io_modes
= VB2_DMABUF
| VB2_MMAP
;
2718 src_vq
->mem_ops
= &vb2_dma_contig_memops
;
2720 ret
= coda_queue_init(priv
, src_vq
);
2724 dst_vq
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
2725 dst_vq
->io_modes
= VB2_DMABUF
| VB2_MMAP
;
2726 dst_vq
->mem_ops
= &vb2_dma_contig_memops
;
2728 return coda_queue_init(priv
, dst_vq
);
2731 static int coda_next_free_instance(struct coda_dev
*dev
)
2733 int idx
= ffz(dev
->instance_mask
);
2736 (dev
->devtype
->product
== CODA_DX6
&& idx
> CODADX6_MAX_INSTANCES
))
2742 static int coda_open(struct file
*file
, enum coda_inst_type inst_type
,
2743 const struct coda_context_ops
*ctx_ops
)
2745 struct coda_dev
*dev
= video_drvdata(file
);
2746 struct coda_ctx
*ctx
= NULL
;
2751 ctx
= kzalloc(sizeof *ctx
, GFP_KERNEL
);
2755 idx
= coda_next_free_instance(dev
);
2760 set_bit(idx
, &dev
->instance_mask
);
2762 name
= kasprintf(GFP_KERNEL
, "context%d", idx
);
2763 ctx
->debugfs_entry
= debugfs_create_dir(name
, dev
->debugfs_root
);
2766 ctx
->inst_type
= inst_type
;
2768 init_completion(&ctx
->completion
);
2769 INIT_WORK(&ctx
->pic_run_work
, coda_pic_run_work
);
2770 INIT_WORK(&ctx
->seq_end_work
, ctx
->ops
->seq_end_work
);
2771 v4l2_fh_init(&ctx
->fh
, video_devdata(file
));
2772 file
->private_data
= &ctx
->fh
;
2773 v4l2_fh_add(&ctx
->fh
);
2776 switch (dev
->devtype
->product
) {
2785 /* Power up and upload firmware if necessary */
2786 ret
= pm_runtime_get_sync(&dev
->plat_dev
->dev
);
2788 v4l2_err(&dev
->v4l2_dev
, "failed to power up: %d\n", ret
);
2792 ret
= clk_prepare_enable(dev
->clk_per
);
2796 ret
= clk_prepare_enable(dev
->clk_ahb
);
2800 set_default_params(ctx
);
2801 ctx
->fh
.m2m_ctx
= v4l2_m2m_ctx_init(dev
->m2m_dev
, ctx
,
2802 ctx
->ops
->queue_init
);
2803 if (IS_ERR(ctx
->fh
.m2m_ctx
)) {
2804 ret
= PTR_ERR(ctx
->fh
.m2m_ctx
);
2806 v4l2_err(&dev
->v4l2_dev
, "%s return error (%d)\n",
2811 ret
= coda_ctrls_setup(ctx
);
2813 v4l2_err(&dev
->v4l2_dev
, "failed to setup coda controls\n");
2814 goto err_ctrls_setup
;
2817 ctx
->fh
.ctrl_handler
= &ctx
->ctrls
;
2819 ret
= coda_alloc_context_buf(ctx
, &ctx
->parabuf
, CODA_PARA_BUF_SIZE
,
2822 v4l2_err(&dev
->v4l2_dev
, "failed to allocate parabuf");
2826 ctx
->bitstream
.size
= CODA_MAX_FRAME_SIZE
;
2827 ctx
->bitstream
.vaddr
= dma_alloc_writecombine(&dev
->plat_dev
->dev
,
2828 ctx
->bitstream
.size
, &ctx
->bitstream
.paddr
, GFP_KERNEL
);
2829 if (!ctx
->bitstream
.vaddr
) {
2830 v4l2_err(&dev
->v4l2_dev
, "failed to allocate bitstream ringbuffer");
2832 goto err_dma_writecombine
;
2834 kfifo_init(&ctx
->bitstream_fifo
,
2835 ctx
->bitstream
.vaddr
, ctx
->bitstream
.size
);
2836 mutex_init(&ctx
->bitstream_mutex
);
2837 mutex_init(&ctx
->buffer_mutex
);
2838 INIT_LIST_HEAD(&ctx
->timestamp_list
);
2841 list_add(&ctx
->list
, &dev
->instances
);
2844 v4l2_dbg(1, coda_debug
, &dev
->v4l2_dev
, "Created instance %d (%p)\n",
2849 err_dma_writecombine
:
2850 coda_free_context_buffers(ctx
);
2851 if (ctx
->dev
->devtype
->product
== CODA_DX6
)
2852 coda_free_aux_buf(dev
, &ctx
->workbuf
);
2853 coda_free_aux_buf(dev
, &ctx
->parabuf
);
2855 v4l2_ctrl_handler_free(&ctx
->ctrls
);
2857 v4l2_m2m_ctx_release(ctx
->fh
.m2m_ctx
);
2859 clk_disable_unprepare(dev
->clk_ahb
);
2861 clk_disable_unprepare(dev
->clk_per
);
2863 pm_runtime_put_sync(&dev
->plat_dev
->dev
);
2865 v4l2_fh_del(&ctx
->fh
);
2866 v4l2_fh_exit(&ctx
->fh
);
2867 clear_bit(ctx
->idx
, &dev
->instance_mask
);
2873 struct coda_context_ops coda_encode_ops
= {
2874 .queue_init
= coda_encoder_queue_init
,
2875 .start_streaming
= coda_start_encoding
,
2876 .prepare_run
= coda_prepare_encode
,
2877 .finish_run
= coda_finish_encode
,
2878 .seq_end_work
= coda_seq_end_work
,
2881 struct coda_context_ops coda_decode_ops
= {
2882 .queue_init
= coda_decoder_queue_init
,
2883 .start_streaming
= coda_start_decoding
,
2884 .prepare_run
= coda_prepare_decode
,
2885 .finish_run
= coda_finish_decode
,
2886 .seq_end_work
= coda_seq_end_work
2889 static int coda_encoder_open(struct file
*file
)
2891 return coda_open(file
, CODA_INST_ENCODER
, &coda_encode_ops
);
2894 static int coda_decoder_open(struct file
*file
)
2896 return coda_open(file
, CODA_INST_DECODER
, &coda_decode_ops
);
2899 static int coda_release(struct file
*file
)
2901 struct coda_dev
*dev
= video_drvdata(file
);
2902 struct coda_ctx
*ctx
= fh_to_ctx(file
->private_data
);
2904 v4l2_dbg(1, coda_debug
, &dev
->v4l2_dev
, "Releasing instance %p\n",
2907 debugfs_remove_recursive(ctx
->debugfs_entry
);
2909 /* If this instance is running, call .job_abort and wait for it to end */
2910 v4l2_m2m_ctx_release(ctx
->fh
.m2m_ctx
);
2912 /* In case the instance was not running, we still need to call SEQ_END */
2913 if (ctx
->initialized
) {
2914 queue_work(dev
->workqueue
, &ctx
->seq_end_work
);
2915 flush_work(&ctx
->seq_end_work
);
2918 coda_free_framebuffers(ctx
);
2921 list_del(&ctx
->list
);
2924 dma_free_writecombine(&dev
->plat_dev
->dev
, ctx
->bitstream
.size
,
2925 ctx
->bitstream
.vaddr
, ctx
->bitstream
.paddr
);
2926 coda_free_context_buffers(ctx
);
2927 if (ctx
->dev
->devtype
->product
== CODA_DX6
)
2928 coda_free_aux_buf(dev
, &ctx
->workbuf
);
2930 coda_free_aux_buf(dev
, &ctx
->parabuf
);
2931 v4l2_ctrl_handler_free(&ctx
->ctrls
);
2932 clk_disable_unprepare(dev
->clk_ahb
);
2933 clk_disable_unprepare(dev
->clk_per
);
2934 pm_runtime_put_sync(&dev
->plat_dev
->dev
);
2935 v4l2_fh_del(&ctx
->fh
);
2936 v4l2_fh_exit(&ctx
->fh
);
2937 clear_bit(ctx
->idx
, &dev
->instance_mask
);
2943 static const struct v4l2_file_operations coda_encoder_fops
= {
2944 .owner
= THIS_MODULE
,
2945 .open
= coda_encoder_open
,
2946 .release
= coda_release
,
2947 .poll
= v4l2_m2m_fop_poll
,
2948 .unlocked_ioctl
= video_ioctl2
,
2949 .mmap
= v4l2_m2m_fop_mmap
,
2952 static const struct v4l2_file_operations coda_decoder_fops
= {
2953 .owner
= THIS_MODULE
,
2954 .open
= coda_decoder_open
,
2955 .release
= coda_release
,
2956 .poll
= v4l2_m2m_fop_poll
,
2957 .unlocked_ioctl
= video_ioctl2
,
2958 .mmap
= v4l2_m2m_fop_mmap
,
2961 static void coda_finish_decode(struct coda_ctx
*ctx
)
2963 struct coda_dev
*dev
= ctx
->dev
;
2964 struct coda_q_data
*q_data_src
;
2965 struct coda_q_data
*q_data_dst
;
2966 struct vb2_buffer
*dst_buf
;
2967 struct coda_timestamp
*ts
;
2976 dst_buf
= v4l2_m2m_next_dst_buf(ctx
->fh
.m2m_ctx
);
2978 /* Update kfifo out pointer from coda bitstream read pointer */
2979 coda_kfifo_sync_from_device(ctx
);
2982 * in stream-end mode, the read pointer can overshoot the write pointer
2983 * by up to 512 bytes
2985 if (ctx
->bit_stream_param
& CODA_BIT_STREAM_END_FLAG
) {
2986 if (coda_get_bitstream_payload(ctx
) >= CODA_MAX_FRAME_SIZE
- 512)
2987 kfifo_init(&ctx
->bitstream_fifo
,
2988 ctx
->bitstream
.vaddr
, ctx
->bitstream
.size
);
2991 q_data_src
= get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_OUTPUT
);
2992 src_fourcc
= q_data_src
->fourcc
;
2994 val
= coda_read(dev
, CODA_RET_DEC_PIC_SUCCESS
);
2996 pr_err("DEC_PIC_SUCCESS = %d\n", val
);
2998 success
= val
& 0x1;
3000 v4l2_err(&dev
->v4l2_dev
, "decode failed\n");
3002 if (src_fourcc
== V4L2_PIX_FMT_H264
) {
3004 v4l2_err(&dev
->v4l2_dev
,
3005 "insufficient PS buffer space (%d bytes)\n",
3008 v4l2_err(&dev
->v4l2_dev
,
3009 "insufficient slice buffer space (%d bytes)\n",
3010 ctx
->slicebuf
.size
);
3013 val
= coda_read(dev
, CODA_RET_DEC_PIC_SIZE
);
3014 width
= (val
>> 16) & 0xffff;
3015 height
= val
& 0xffff;
3017 q_data_dst
= get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_CAPTURE
);
3019 /* frame crop information */
3020 if (src_fourcc
== V4L2_PIX_FMT_H264
) {
3024 left_right
= coda_read(dev
, CODA_RET_DEC_PIC_CROP_LEFT_RIGHT
);
3025 top_bottom
= coda_read(dev
, CODA_RET_DEC_PIC_CROP_TOP_BOTTOM
);
3027 if (left_right
== 0xffffffff && top_bottom
== 0xffffffff) {
3028 /* Keep current crop information */
3030 struct v4l2_rect
*rect
= &q_data_dst
->rect
;
3032 rect
->left
= left_right
>> 16 & 0xffff;
3033 rect
->top
= top_bottom
>> 16 & 0xffff;
3034 rect
->width
= width
- rect
->left
-
3035 (left_right
& 0xffff);
3036 rect
->height
= height
- rect
->top
-
3037 (top_bottom
& 0xffff);
3043 err_mb
= coda_read(dev
, CODA_RET_DEC_PIC_ERR_MB
);
3045 v4l2_err(&dev
->v4l2_dev
,
3046 "errors in %d macroblocks\n", err_mb
);
3048 if (dev
->devtype
->product
== CODA_7541
) {
3049 val
= coda_read(dev
, CODA_RET_DEC_PIC_OPTION
);
3051 /* not enough bitstream data */
3052 v4l2_dbg(1, coda_debug
, &dev
->v4l2_dev
,
3053 "prescan failed: %d\n", val
);
3059 ctx
->frm_dis_flg
= coda_read(dev
, CODA_REG_BIT_FRM_DIS_FLG(ctx
->reg_idx
));
3062 * The previous display frame was copied out by the rotator,
3063 * now it can be overwritten again
3065 if (ctx
->display_idx
>= 0 &&
3066 ctx
->display_idx
< ctx
->num_internal_frames
) {
3067 ctx
->frm_dis_flg
&= ~(1 << ctx
->display_idx
);
3068 coda_write(dev
, ctx
->frm_dis_flg
,
3069 CODA_REG_BIT_FRM_DIS_FLG(ctx
->reg_idx
));
3073 * The index of the last decoded frame, not necessarily in
3074 * display order, and the index of the next display frame.
3075 * The latter could have been decoded in a previous run.
3077 decoded_idx
= coda_read(dev
, CODA_RET_DEC_PIC_CUR_IDX
);
3078 display_idx
= coda_read(dev
, CODA_RET_DEC_PIC_FRAME_IDX
);
3080 if (decoded_idx
== -1) {
3081 /* no frame was decoded, but we might have a display frame */
3082 if (display_idx
>= 0 && display_idx
< ctx
->num_internal_frames
)
3083 ctx
->sequence_offset
++;
3084 else if (ctx
->display_idx
< 0)
3086 } else if (decoded_idx
== -2) {
3087 /* no frame was decoded, we still return the remaining buffers */
3088 } else if (decoded_idx
< 0 || decoded_idx
>= ctx
->num_internal_frames
) {
3089 v4l2_err(&dev
->v4l2_dev
,
3090 "decoded frame index out of range: %d\n", decoded_idx
);
3092 ts
= list_first_entry(&ctx
->timestamp_list
,
3093 struct coda_timestamp
, list
);
3094 list_del(&ts
->list
);
3095 val
= coda_read(dev
, CODA_RET_DEC_PIC_FRAME_NUM
) - 1;
3096 val
-= ctx
->sequence_offset
;
3097 if (val
!= (ts
->sequence
& 0xffff)) {
3098 v4l2_err(&dev
->v4l2_dev
,
3099 "sequence number mismatch (%d(%d) != %d)\n",
3100 val
, ctx
->sequence_offset
, ts
->sequence
);
3102 ctx
->frame_timestamps
[decoded_idx
] = *ts
;
3105 val
= coda_read(dev
, CODA_RET_DEC_PIC_TYPE
) & 0x7;
3107 ctx
->frame_types
[decoded_idx
] = V4L2_BUF_FLAG_KEYFRAME
;
3109 ctx
->frame_types
[decoded_idx
] = V4L2_BUF_FLAG_PFRAME
;
3111 ctx
->frame_types
[decoded_idx
] = V4L2_BUF_FLAG_BFRAME
;
3113 ctx
->frame_errors
[decoded_idx
] = err_mb
;
3116 if (display_idx
== -1) {
3118 * no more frames to be decoded, but there could still
3119 * be rotator output to dequeue
3122 } else if (display_idx
== -3) {
3123 /* possibly prescan failure */
3124 } else if (display_idx
< 0 || display_idx
>= ctx
->num_internal_frames
) {
3125 v4l2_err(&dev
->v4l2_dev
,
3126 "presentation frame index out of range: %d\n",
3130 /* If a frame was copied out, return it */
3131 if (ctx
->display_idx
>= 0 &&
3132 ctx
->display_idx
< ctx
->num_internal_frames
) {
3133 dst_buf
= v4l2_m2m_dst_buf_remove(ctx
->fh
.m2m_ctx
);
3134 dst_buf
->v4l2_buf
.sequence
= ctx
->osequence
++;
3136 dst_buf
->v4l2_buf
.flags
&= ~(V4L2_BUF_FLAG_KEYFRAME
|
3137 V4L2_BUF_FLAG_PFRAME
|
3138 V4L2_BUF_FLAG_BFRAME
);
3139 dst_buf
->v4l2_buf
.flags
|= ctx
->frame_types
[ctx
->display_idx
];
3140 ts
= &ctx
->frame_timestamps
[ctx
->display_idx
];
3141 dst_buf
->v4l2_buf
.timecode
= ts
->timecode
;
3142 dst_buf
->v4l2_buf
.timestamp
= ts
->timestamp
;
3144 vb2_set_plane_payload(dst_buf
, 0, width
* height
* 3 / 2);
3146 v4l2_m2m_buf_done(dst_buf
, ctx
->frame_errors
[display_idx
] ?
3147 VB2_BUF_STATE_ERROR
: VB2_BUF_STATE_DONE
);
3149 v4l2_dbg(1, coda_debug
, &dev
->v4l2_dev
,
3150 "job finished: decoding frame (%d) (%s)\n",
3151 dst_buf
->v4l2_buf
.sequence
,
3152 (dst_buf
->v4l2_buf
.flags
& V4L2_BUF_FLAG_KEYFRAME
) ?
3153 "KEYFRAME" : "PFRAME");
3155 v4l2_dbg(1, coda_debug
, &dev
->v4l2_dev
,
3156 "job finished: no frame decoded\n");
3159 /* The rotator will copy the current display frame next time */
3160 ctx
->display_idx
= display_idx
;
3163 static void coda_finish_encode(struct coda_ctx
*ctx
)
3165 struct vb2_buffer
*src_buf
, *dst_buf
;
3166 struct coda_dev
*dev
= ctx
->dev
;
3167 u32 wr_ptr
, start_ptr
;
3169 src_buf
= v4l2_m2m_src_buf_remove(ctx
->fh
.m2m_ctx
);
3170 dst_buf
= v4l2_m2m_next_dst_buf(ctx
->fh
.m2m_ctx
);
3172 /* Get results from the coda */
3173 start_ptr
= coda_read(dev
, CODA_CMD_ENC_PIC_BB_START
);
3174 wr_ptr
= coda_read(dev
, CODA_REG_BIT_WR_PTR(ctx
->reg_idx
));
3176 /* Calculate bytesused field */
3177 if (dst_buf
->v4l2_buf
.sequence
== 0) {
3178 vb2_set_plane_payload(dst_buf
, 0, wr_ptr
- start_ptr
+
3179 ctx
->vpu_header_size
[0] +
3180 ctx
->vpu_header_size
[1] +
3181 ctx
->vpu_header_size
[2]);
3183 vb2_set_plane_payload(dst_buf
, 0, wr_ptr
- start_ptr
);
3186 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
, "frame size = %u\n",
3187 wr_ptr
- start_ptr
);
3189 coda_read(dev
, CODA_RET_ENC_PIC_SLICE_NUM
);
3190 coda_read(dev
, CODA_RET_ENC_PIC_FLAG
);
3192 if (coda_read(dev
, CODA_RET_ENC_PIC_TYPE
) == 0) {
3193 dst_buf
->v4l2_buf
.flags
|= V4L2_BUF_FLAG_KEYFRAME
;
3194 dst_buf
->v4l2_buf
.flags
&= ~V4L2_BUF_FLAG_PFRAME
;
3196 dst_buf
->v4l2_buf
.flags
|= V4L2_BUF_FLAG_PFRAME
;
3197 dst_buf
->v4l2_buf
.flags
&= ~V4L2_BUF_FLAG_KEYFRAME
;
3200 dst_buf
->v4l2_buf
.timestamp
= src_buf
->v4l2_buf
.timestamp
;
3201 dst_buf
->v4l2_buf
.flags
&= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK
;
3202 dst_buf
->v4l2_buf
.flags
|=
3203 src_buf
->v4l2_buf
.flags
& V4L2_BUF_FLAG_TSTAMP_SRC_MASK
;
3204 dst_buf
->v4l2_buf
.timecode
= src_buf
->v4l2_buf
.timecode
;
3206 v4l2_m2m_buf_done(src_buf
, VB2_BUF_STATE_DONE
);
3208 dst_buf
= v4l2_m2m_dst_buf_remove(ctx
->fh
.m2m_ctx
);
3209 v4l2_m2m_buf_done(dst_buf
, VB2_BUF_STATE_DONE
);
3212 if (ctx
->gopcounter
< 0)
3213 ctx
->gopcounter
= ctx
->params
.gop_size
- 1;
3215 v4l2_dbg(1, coda_debug
, &dev
->v4l2_dev
,
3216 "job finished: encoding frame (%d) (%s)\n",
3217 dst_buf
->v4l2_buf
.sequence
,
3218 (dst_buf
->v4l2_buf
.flags
& V4L2_BUF_FLAG_KEYFRAME
) ?
3219 "KEYFRAME" : "PFRAME");
3222 static irqreturn_t
coda_irq_handler(int irq
, void *data
)
3224 struct coda_dev
*dev
= data
;
3225 struct coda_ctx
*ctx
;
3227 /* read status register to attend the IRQ */
3228 coda_read(dev
, CODA_REG_BIT_INT_STATUS
);
3229 coda_write(dev
, CODA_REG_BIT_INT_CLEAR_SET
,
3230 CODA_REG_BIT_INT_CLEAR
);
3232 ctx
= v4l2_m2m_get_curr_priv(dev
->m2m_dev
);
3234 v4l2_err(&dev
->v4l2_dev
, "Instance released before the end of transaction\n");
3235 mutex_unlock(&dev
->coda_mutex
);
3239 if (ctx
->aborting
) {
3240 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
3241 "task has been aborted\n");
3244 if (coda_isbusy(ctx
->dev
)) {
3245 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
3246 "coda is still busy!!!!\n");
3250 complete(&ctx
->completion
);
3255 static u32 coda_supported_firmwares
[] = {
3256 CODA_FIRMWARE_VERNUM(CODA_DX6
, 2, 2, 5),
3257 CODA_FIRMWARE_VERNUM(CODA_7541
, 1, 4, 50),
3258 CODA_FIRMWARE_VERNUM(CODA_960
, 2, 1, 5),
3261 static bool coda_firmware_supported(u32 vernum
)
3265 for (i
= 0; i
< ARRAY_SIZE(coda_supported_firmwares
); i
++)
3266 if (vernum
== coda_supported_firmwares
[i
])
3271 static int coda_hw_init(struct coda_dev
*dev
)
3277 ret
= clk_prepare_enable(dev
->clk_per
);
3281 ret
= clk_prepare_enable(dev
->clk_ahb
);
3286 reset_control_reset(dev
->rstc
);
3289 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
3290 * The 16-bit chars in the code buffer are in memory access
3291 * order, re-sort them to CODA order for register download.
3292 * Data in this SRAM survives a reboot.
3294 p
= (u16
*)dev
->codebuf
.vaddr
;
3295 if (dev
->devtype
->product
== CODA_DX6
) {
3296 for (i
= 0; i
< (CODA_ISRAM_SIZE
/ 2); i
++) {
3297 data
= CODA_DOWN_ADDRESS_SET(i
) |
3298 CODA_DOWN_DATA_SET(p
[i
^ 1]);
3299 coda_write(dev
, data
, CODA_REG_BIT_CODE_DOWN
);
3302 for (i
= 0; i
< (CODA_ISRAM_SIZE
/ 2); i
++) {
3303 data
= CODA_DOWN_ADDRESS_SET(i
) |
3304 CODA_DOWN_DATA_SET(p
[round_down(i
, 4) +
3306 coda_write(dev
, data
, CODA_REG_BIT_CODE_DOWN
);
3310 /* Clear registers */
3311 for (i
= 0; i
< 64; i
++)
3312 coda_write(dev
, 0, CODA_REG_BIT_CODE_BUF_ADDR
+ i
* 4);
3314 /* Tell the BIT where to find everything it needs */
3315 if (dev
->devtype
->product
== CODA_960
||
3316 dev
->devtype
->product
== CODA_7541
) {
3317 coda_write(dev
, dev
->tempbuf
.paddr
,
3318 CODA_REG_BIT_TEMP_BUF_ADDR
);
3319 coda_write(dev
, 0, CODA_REG_BIT_BIT_STREAM_PARAM
);
3321 coda_write(dev
, dev
->workbuf
.paddr
,
3322 CODA_REG_BIT_WORK_BUF_ADDR
);
3324 coda_write(dev
, dev
->codebuf
.paddr
,
3325 CODA_REG_BIT_CODE_BUF_ADDR
);
3326 coda_write(dev
, 0, CODA_REG_BIT_CODE_RUN
);
3328 /* Set default values */
3329 switch (dev
->devtype
->product
) {
3331 coda_write(dev
, CODADX6_STREAM_BUF_PIC_FLUSH
, CODA_REG_BIT_STREAM_CTRL
);
3334 coda_write(dev
, CODA7_STREAM_BUF_PIC_FLUSH
, CODA_REG_BIT_STREAM_CTRL
);
3336 if (dev
->devtype
->product
== CODA_960
)
3337 coda_write(dev
, 1 << 12, CODA_REG_BIT_FRAME_MEM_CTRL
);
3339 coda_write(dev
, 0, CODA_REG_BIT_FRAME_MEM_CTRL
);
3341 if (dev
->devtype
->product
!= CODA_DX6
)
3342 coda_write(dev
, 0, CODA7_REG_BIT_AXI_SRAM_USE
);
3344 coda_write(dev
, CODA_INT_INTERRUPT_ENABLE
,
3345 CODA_REG_BIT_INT_ENABLE
);
3347 /* Reset VPU and start processor */
3348 data
= coda_read(dev
, CODA_REG_BIT_CODE_RESET
);
3349 data
|= CODA_REG_RESET_ENABLE
;
3350 coda_write(dev
, data
, CODA_REG_BIT_CODE_RESET
);
3352 data
&= ~CODA_REG_RESET_ENABLE
;
3353 coda_write(dev
, data
, CODA_REG_BIT_CODE_RESET
);
3354 coda_write(dev
, CODA_REG_RUN_ENABLE
, CODA_REG_BIT_CODE_RUN
);
3356 clk_disable_unprepare(dev
->clk_ahb
);
3357 clk_disable_unprepare(dev
->clk_per
);
3362 clk_disable_unprepare(dev
->clk_per
);
3367 static int coda_check_firmware(struct coda_dev
*dev
)
3369 u16 product
, major
, minor
, release
;
3373 ret
= clk_prepare_enable(dev
->clk_per
);
3377 ret
= clk_prepare_enable(dev
->clk_ahb
);
3381 coda_write(dev
, 0, CODA_CMD_FIRMWARE_VERNUM
);
3382 coda_write(dev
, CODA_REG_BIT_BUSY_FLAG
, CODA_REG_BIT_BUSY
);
3383 coda_write(dev
, 0, CODA_REG_BIT_RUN_INDEX
);
3384 coda_write(dev
, 0, CODA_REG_BIT_RUN_COD_STD
);
3385 coda_write(dev
, CODA_COMMAND_FIRMWARE_GET
, CODA_REG_BIT_RUN_COMMAND
);
3386 if (coda_wait_timeout(dev
)) {
3387 v4l2_err(&dev
->v4l2_dev
, "firmware get command error\n");
3392 if (dev
->devtype
->product
== CODA_960
) {
3393 data
= coda_read(dev
, CODA9_CMD_FIRMWARE_CODE_REV
);
3394 v4l2_info(&dev
->v4l2_dev
, "Firmware code revision: %d\n",
3398 /* Check we are compatible with the loaded firmware */
3399 data
= coda_read(dev
, CODA_CMD_FIRMWARE_VERNUM
);
3400 product
= CODA_FIRMWARE_PRODUCT(data
);
3401 major
= CODA_FIRMWARE_MAJOR(data
);
3402 minor
= CODA_FIRMWARE_MINOR(data
);
3403 release
= CODA_FIRMWARE_RELEASE(data
);
3405 clk_disable_unprepare(dev
->clk_per
);
3406 clk_disable_unprepare(dev
->clk_ahb
);
3408 if (product
!= dev
->devtype
->product
) {
3409 v4l2_err(&dev
->v4l2_dev
, "Wrong firmware. Hw: %s, Fw: %s,"
3410 " Version: %u.%u.%u\n",
3411 coda_product_name(dev
->devtype
->product
),
3412 coda_product_name(product
), major
, minor
, release
);
3416 v4l2_info(&dev
->v4l2_dev
, "Initialized %s.\n",
3417 coda_product_name(product
));
3419 if (coda_firmware_supported(data
)) {
3420 v4l2_info(&dev
->v4l2_dev
, "Firmware version: %u.%u.%u\n",
3421 major
, minor
, release
);
3423 v4l2_warn(&dev
->v4l2_dev
, "Unsupported firmware version: "
3424 "%u.%u.%u\n", major
, minor
, release
);
3430 clk_disable_unprepare(dev
->clk_ahb
);
3432 clk_disable_unprepare(dev
->clk_per
);
3437 static int coda_register_device(struct coda_dev
*dev
, struct video_device
*vfd
)
3439 vfd
->release
= video_device_release_empty
,
3440 vfd
->lock
= &dev
->dev_mutex
;
3441 vfd
->v4l2_dev
= &dev
->v4l2_dev
;
3442 vfd
->vfl_dir
= VFL_DIR_M2M
;
3443 video_set_drvdata(vfd
, dev
);
3445 return video_register_device(vfd
, VFL_TYPE_GRABBER
, 0);
3448 static void coda_fw_callback(const struct firmware
*fw
, void *context
)
3450 struct coda_dev
*dev
= context
;
3451 struct platform_device
*pdev
= dev
->plat_dev
;
3455 v4l2_err(&dev
->v4l2_dev
, "firmware request failed\n");
3459 /* allocate auxiliary per-device code buffer for the BIT processor */
3460 ret
= coda_alloc_aux_buf(dev
, &dev
->codebuf
, fw
->size
, "codebuf",
3463 dev_err(&pdev
->dev
, "failed to allocate code buffer\n");
3467 /* Copy the whole firmware image to the code buffer */
3468 memcpy(dev
->codebuf
.vaddr
, fw
->data
, fw
->size
);
3469 release_firmware(fw
);
3471 if (pm_runtime_enabled(&pdev
->dev
) && pdev
->dev
.pm_domain
) {
3473 * Enabling power temporarily will cause coda_hw_init to be
3474 * called via coda_runtime_resume by the pm domain.
3476 ret
= pm_runtime_get_sync(&dev
->plat_dev
->dev
);
3478 v4l2_err(&dev
->v4l2_dev
, "failed to power on: %d\n",
3483 ret
= coda_check_firmware(dev
);
3487 pm_runtime_put_sync(&dev
->plat_dev
->dev
);
3490 * If runtime pm is disabled or pm_domain is not set,
3491 * initialize once manually.
3493 ret
= coda_hw_init(dev
);
3495 v4l2_err(&dev
->v4l2_dev
, "HW initialization failed\n");
3499 ret
= coda_check_firmware(dev
);
3504 dev
->alloc_ctx
= vb2_dma_contig_init_ctx(&pdev
->dev
);
3505 if (IS_ERR(dev
->alloc_ctx
)) {
3506 v4l2_err(&dev
->v4l2_dev
, "Failed to alloc vb2 context\n");
3510 dev
->m2m_dev
= v4l2_m2m_init(&coda_m2m_ops
);
3511 if (IS_ERR(dev
->m2m_dev
)) {
3512 v4l2_err(&dev
->v4l2_dev
, "Failed to init mem2mem device\n");
3516 dev
->vfd
[0].fops
= &coda_encoder_fops
,
3517 dev
->vfd
[0].ioctl_ops
= &coda_ioctl_ops
;
3518 snprintf(dev
->vfd
[0].name
, sizeof(dev
->vfd
[0].name
), "coda-encoder");
3519 ret
= coda_register_device(dev
, &dev
->vfd
[0]);
3521 v4l2_err(&dev
->v4l2_dev
,
3522 "Failed to register encoder video device\n");
3526 dev
->vfd
[1].fops
= &coda_decoder_fops
,
3527 dev
->vfd
[1].ioctl_ops
= &coda_ioctl_ops
;
3528 snprintf(dev
->vfd
[1].name
, sizeof(dev
->vfd
[1].name
), "coda-decoder");
3529 ret
= coda_register_device(dev
, &dev
->vfd
[1]);
3531 v4l2_err(&dev
->v4l2_dev
,
3532 "Failed to register decoder video device\n");
3536 v4l2_info(&dev
->v4l2_dev
, "codec registered as /dev/video[%d-%d]\n",
3537 dev
->vfd
[0].num
, dev
->vfd
[1].num
);
3542 v4l2_m2m_release(dev
->m2m_dev
);
3544 vb2_dma_contig_cleanup_ctx(dev
->alloc_ctx
);
3547 static int coda_firmware_request(struct coda_dev
*dev
)
3549 char *fw
= dev
->devtype
->firmware
;
3551 dev_dbg(&dev
->plat_dev
->dev
, "requesting firmware '%s' for %s\n", fw
,
3552 coda_product_name(dev
->devtype
->product
));
3554 return request_firmware_nowait(THIS_MODULE
, true,
3555 fw
, &dev
->plat_dev
->dev
, GFP_KERNEL
, dev
, coda_fw_callback
);
3558 enum coda_platform
{
3565 static const struct coda_devtype coda_devdata
[] = {
3567 .firmware
= "v4l-codadx6-imx27.bin",
3568 .product
= CODA_DX6
,
3569 .codecs
= codadx6_codecs
,
3570 .num_codecs
= ARRAY_SIZE(codadx6_codecs
),
3571 .workbuf_size
= 288 * 1024 + FMO_SLICE_SAVE_BUF_SIZE
* 8 * 1024,
3572 .iram_size
= 0xb000,
3575 .firmware
= "v4l-coda7541-imx53.bin",
3576 .product
= CODA_7541
,
3577 .codecs
= coda7_codecs
,
3578 .num_codecs
= ARRAY_SIZE(coda7_codecs
),
3579 .workbuf_size
= 128 * 1024,
3580 .tempbuf_size
= 304 * 1024,
3581 .iram_size
= 0x14000,
3584 .firmware
= "v4l-coda960-imx6q.bin",
3585 .product
= CODA_960
,
3586 .codecs
= coda9_codecs
,
3587 .num_codecs
= ARRAY_SIZE(coda9_codecs
),
3588 .workbuf_size
= 80 * 1024,
3589 .tempbuf_size
= 204 * 1024,
3590 .iram_size
= 0x21000,
3593 .firmware
= "v4l-coda960-imx6dl.bin",
3594 .product
= CODA_960
,
3595 .codecs
= coda9_codecs
,
3596 .num_codecs
= ARRAY_SIZE(coda9_codecs
),
3597 .workbuf_size
= 80 * 1024,
3598 .tempbuf_size
= 204 * 1024,
3599 .iram_size
= 0x20000,
3603 static struct platform_device_id coda_platform_ids
[] = {
3604 { .name
= "coda-imx27", .driver_data
= CODA_IMX27
},
3605 { .name
= "coda-imx53", .driver_data
= CODA_IMX53
},
3608 MODULE_DEVICE_TABLE(platform
, coda_platform_ids
);
3611 static const struct of_device_id coda_dt_ids
[] = {
3612 { .compatible
= "fsl,imx27-vpu", .data
= &coda_devdata
[CODA_IMX27
] },
3613 { .compatible
= "fsl,imx53-vpu", .data
= &coda_devdata
[CODA_IMX53
] },
3614 { .compatible
= "fsl,imx6q-vpu", .data
= &coda_devdata
[CODA_IMX6Q
] },
3615 { .compatible
= "fsl,imx6dl-vpu", .data
= &coda_devdata
[CODA_IMX6DL
] },
3618 MODULE_DEVICE_TABLE(of
, coda_dt_ids
);
3621 static int coda_probe(struct platform_device
*pdev
)
3623 const struct of_device_id
*of_id
=
3624 of_match_device(of_match_ptr(coda_dt_ids
), &pdev
->dev
);
3625 const struct platform_device_id
*pdev_id
;
3626 struct coda_platform_data
*pdata
= pdev
->dev
.platform_data
;
3627 struct device_node
*np
= pdev
->dev
.of_node
;
3628 struct gen_pool
*pool
;
3629 struct coda_dev
*dev
;
3630 struct resource
*res
;
3633 dev
= devm_kzalloc(&pdev
->dev
, sizeof *dev
, GFP_KERNEL
);
3635 dev_err(&pdev
->dev
, "Not enough memory for %s\n",
3640 spin_lock_init(&dev
->irqlock
);
3641 INIT_LIST_HEAD(&dev
->instances
);
3643 dev
->plat_dev
= pdev
;
3644 dev
->clk_per
= devm_clk_get(&pdev
->dev
, "per");
3645 if (IS_ERR(dev
->clk_per
)) {
3646 dev_err(&pdev
->dev
, "Could not get per clock\n");
3647 return PTR_ERR(dev
->clk_per
);
3650 dev
->clk_ahb
= devm_clk_get(&pdev
->dev
, "ahb");
3651 if (IS_ERR(dev
->clk_ahb
)) {
3652 dev_err(&pdev
->dev
, "Could not get ahb clock\n");
3653 return PTR_ERR(dev
->clk_ahb
);
3656 /* Get memory for physical registers */
3657 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
3658 dev
->regs_base
= devm_ioremap_resource(&pdev
->dev
, res
);
3659 if (IS_ERR(dev
->regs_base
))
3660 return PTR_ERR(dev
->regs_base
);
3663 irq
= platform_get_irq(pdev
, 0);
3665 dev_err(&pdev
->dev
, "failed to get irq resource\n");
3669 ret
= devm_request_threaded_irq(&pdev
->dev
, irq
, NULL
, coda_irq_handler
,
3670 IRQF_ONESHOT
, dev_name(&pdev
->dev
), dev
);
3672 dev_err(&pdev
->dev
, "failed to request irq: %d\n", ret
);
3676 dev
->rstc
= devm_reset_control_get_optional(&pdev
->dev
, NULL
);
3677 if (IS_ERR(dev
->rstc
)) {
3678 ret
= PTR_ERR(dev
->rstc
);
3679 if (ret
== -ENOENT
|| ret
== -ENOSYS
) {
3682 dev_err(&pdev
->dev
, "failed get reset control: %d\n", ret
);
3687 /* Get IRAM pool from device tree or platform data */
3688 pool
= of_get_named_gen_pool(np
, "iram", 0);
3690 pool
= dev_get_gen_pool(pdata
->iram_dev
);
3692 dev_err(&pdev
->dev
, "iram pool not available\n");
3695 dev
->iram_pool
= pool
;
3697 ret
= v4l2_device_register(&pdev
->dev
, &dev
->v4l2_dev
);
3701 mutex_init(&dev
->dev_mutex
);
3702 mutex_init(&dev
->coda_mutex
);
3704 pdev_id
= of_id
? of_id
->data
: platform_get_device_id(pdev
);
3707 dev
->devtype
= of_id
->data
;
3708 } else if (pdev_id
) {
3709 dev
->devtype
= &coda_devdata
[pdev_id
->driver_data
];
3711 v4l2_device_unregister(&dev
->v4l2_dev
);
3715 dev
->debugfs_root
= debugfs_create_dir("coda", NULL
);
3716 if (!dev
->debugfs_root
)
3717 dev_warn(&pdev
->dev
, "failed to create debugfs root\n");
3719 /* allocate auxiliary per-device buffers for the BIT processor */
3720 if (dev
->devtype
->product
== CODA_DX6
) {
3721 ret
= coda_alloc_aux_buf(dev
, &dev
->workbuf
,
3722 dev
->devtype
->workbuf_size
, "workbuf",
3725 dev_err(&pdev
->dev
, "failed to allocate work buffer\n");
3726 v4l2_device_unregister(&dev
->v4l2_dev
);
3731 if (dev
->devtype
->tempbuf_size
) {
3732 ret
= coda_alloc_aux_buf(dev
, &dev
->tempbuf
,
3733 dev
->devtype
->tempbuf_size
, "tempbuf",
3736 dev_err(&pdev
->dev
, "failed to allocate temp buffer\n");
3737 v4l2_device_unregister(&dev
->v4l2_dev
);
3742 dev
->iram
.size
= dev
->devtype
->iram_size
;
3743 dev
->iram
.vaddr
= gen_pool_dma_alloc(dev
->iram_pool
, dev
->iram
.size
,
3745 if (!dev
->iram
.vaddr
) {
3746 dev_err(&pdev
->dev
, "unable to alloc iram\n");
3750 dev
->iram
.blob
.data
= dev
->iram
.vaddr
;
3751 dev
->iram
.blob
.size
= dev
->iram
.size
;
3752 dev
->iram
.dentry
= debugfs_create_blob("iram", 0644, dev
->debugfs_root
,
3755 dev
->workqueue
= alloc_workqueue("coda", WQ_UNBOUND
| WQ_MEM_RECLAIM
, 1);
3756 if (!dev
->workqueue
) {
3757 dev_err(&pdev
->dev
, "unable to alloc workqueue\n");
3761 platform_set_drvdata(pdev
, dev
);
3763 pm_runtime_enable(&pdev
->dev
);
3765 return coda_firmware_request(dev
);
3768 static int coda_remove(struct platform_device
*pdev
)
3770 struct coda_dev
*dev
= platform_get_drvdata(pdev
);
3772 video_unregister_device(&dev
->vfd
[0]);
3773 video_unregister_device(&dev
->vfd
[1]);
3775 v4l2_m2m_release(dev
->m2m_dev
);
3776 pm_runtime_disable(&pdev
->dev
);
3778 vb2_dma_contig_cleanup_ctx(dev
->alloc_ctx
);
3779 v4l2_device_unregister(&dev
->v4l2_dev
);
3780 destroy_workqueue(dev
->workqueue
);
3781 if (dev
->iram
.vaddr
)
3782 gen_pool_free(dev
->iram_pool
, (unsigned long)dev
->iram
.vaddr
,
3784 coda_free_aux_buf(dev
, &dev
->codebuf
);
3785 coda_free_aux_buf(dev
, &dev
->tempbuf
);
3786 coda_free_aux_buf(dev
, &dev
->workbuf
);
3787 debugfs_remove_recursive(dev
->debugfs_root
);
3791 #ifdef CONFIG_PM_RUNTIME
3792 static int coda_runtime_resume(struct device
*dev
)
3794 struct coda_dev
*cdev
= dev_get_drvdata(dev
);
3797 if (dev
->pm_domain
&& cdev
->codebuf
.vaddr
) {
3798 ret
= coda_hw_init(cdev
);
3800 v4l2_err(&cdev
->v4l2_dev
, "HW initialization failed\n");
3807 static const struct dev_pm_ops coda_pm_ops
= {
3808 SET_RUNTIME_PM_OPS(NULL
, coda_runtime_resume
, NULL
)
3811 static struct platform_driver coda_driver
= {
3812 .probe
= coda_probe
,
3813 .remove
= coda_remove
,
3816 .owner
= THIS_MODULE
,
3817 .of_match_table
= of_match_ptr(coda_dt_ids
),
3820 .id_table
= coda_platform_ids
,
3823 module_platform_driver(coda_driver
);
3825 MODULE_LICENSE("GPL");
3826 MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
3827 MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");