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/delay.h>
16 #include <linux/firmware.h>
17 #include <linux/genalloc.h>
18 #include <linux/interrupt.h>
20 #include <linux/irq.h>
21 #include <linux/kfifo.h>
22 #include <linux/module.h>
23 #include <linux/of_device.h>
24 #include <linux/platform_device.h>
25 #include <linux/slab.h>
26 #include <linux/videodev2.h>
28 #include <linux/platform_data/coda.h>
30 #include <media/v4l2-ctrls.h>
31 #include <media/v4l2-device.h>
32 #include <media/v4l2-event.h>
33 #include <media/v4l2-ioctl.h>
34 #include <media/v4l2-mem2mem.h>
35 #include <media/videobuf2-core.h>
36 #include <media/videobuf2-dma-contig.h>
40 #define CODA_NAME "coda"
42 #define CODADX6_MAX_INSTANCES 4
44 #define CODA_FMO_BUF_SIZE 32
45 #define CODADX6_WORK_BUF_SIZE (288 * 1024 + CODA_FMO_BUF_SIZE * 8 * 1024)
46 #define CODA7_WORK_BUF_SIZE (128 * 1024)
47 #define CODA7_TEMP_BUF_SIZE (304 * 1024)
48 #define CODA_PARA_BUF_SIZE (10 * 1024)
49 #define CODA_ISRAM_SIZE (2048 * 2)
50 #define CODADX6_IRAM_SIZE 0xb000
51 #define CODA7_IRAM_SIZE 0x14000
53 #define CODA7_PS_BUF_SIZE 0x28000
55 #define CODA_MAX_FRAMEBUFFERS 8
59 #define CODA_MAX_FRAME_SIZE 0x100000
60 #define FMO_SLICE_SAVE_BUF_SIZE (32)
61 #define CODA_DEFAULT_GAMMA 4096
66 #define S_ALIGN 1 /* multiple of 2 */
67 #define W_ALIGN 1 /* multiple of 2 */
68 #define H_ALIGN 1 /* multiple of 2 */
70 #define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh)
72 static int coda_debug
;
73 module_param(coda_debug
, int, 0644);
74 MODULE_PARM_DESC(coda_debug
, "Debug level (0-1)");
104 struct coda_devtype
{
106 enum coda_product product
;
107 struct coda_codec
*codecs
;
108 unsigned int num_codecs
;
112 /* Per-queue, driver-specific private data */
116 unsigned int sizeimage
;
120 struct coda_aux_buf
{
127 struct v4l2_device v4l2_dev
;
128 struct video_device vfd
;
129 struct platform_device
*plat_dev
;
130 const struct coda_devtype
*devtype
;
132 void __iomem
*regs_base
;
136 struct coda_aux_buf codebuf
;
137 struct coda_aux_buf tempbuf
;
138 struct coda_aux_buf workbuf
;
139 struct gen_pool
*iram_pool
;
140 long unsigned int iram_vaddr
;
141 long unsigned int iram_paddr
;
142 unsigned long iram_size
;
145 struct mutex dev_mutex
;
146 struct mutex coda_mutex
;
147 struct v4l2_m2m_dev
*m2m_dev
;
148 struct vb2_alloc_ctx
*alloc_ctx
;
149 struct list_head instances
;
150 unsigned long instance_mask
;
151 struct delayed_work timeout
;
163 enum v4l2_mpeg_video_multi_slice_mode slice_mode
;
170 struct coda_iram_info
{
172 phys_addr_t buf_bit_use
;
173 phys_addr_t buf_ip_ac_dc_use
;
174 phys_addr_t buf_dbk_y_use
;
175 phys_addr_t buf_dbk_c_use
;
176 phys_addr_t buf_ovl_use
;
177 phys_addr_t buf_btp_use
;
178 phys_addr_t search_ram_paddr
;
183 struct coda_dev
*dev
;
184 struct mutex buffer_mutex
;
185 struct list_head list
;
186 struct work_struct skip_run
;
194 struct coda_q_data q_data
[2];
195 enum coda_inst_type inst_type
;
196 struct coda_codec
*codec
;
197 enum v4l2_colorspace colorspace
;
198 struct coda_params params
;
199 struct v4l2_m2m_ctx
*m2m_ctx
;
200 struct v4l2_ctrl_handler ctrls
;
204 char vpu_header
[3][64];
205 int vpu_header_size
[3];
206 struct kfifo bitstream_fifo
;
207 struct mutex bitstream_mutex
;
208 struct coda_aux_buf bitstream
;
210 struct coda_aux_buf parabuf
;
211 struct coda_aux_buf psbuf
;
212 struct coda_aux_buf slicebuf
;
213 struct coda_aux_buf internal_frames
[CODA_MAX_FRAMEBUFFERS
];
214 struct coda_aux_buf workbuf
;
215 int num_internal_frames
;
218 struct coda_iram_info iram_info
;
219 u32 bit_stream_param
;
224 static const u8 coda_filler_nal
[14] = { 0x00, 0x00, 0x00, 0x01, 0x0c, 0xff,
225 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80 };
226 static const u8 coda_filler_size
[8] = { 0, 7, 14, 13, 12, 11, 10, 9 };
228 static inline void coda_write(struct coda_dev
*dev
, u32 data
, u32 reg
)
230 v4l2_dbg(1, coda_debug
, &dev
->v4l2_dev
,
231 "%s: data=0x%x, reg=0x%x\n", __func__
, data
, reg
);
232 writel(data
, dev
->regs_base
+ reg
);
235 static inline unsigned int coda_read(struct coda_dev
*dev
, u32 reg
)
238 data
= readl(dev
->regs_base
+ reg
);
239 v4l2_dbg(1, coda_debug
, &dev
->v4l2_dev
,
240 "%s: data=0x%x, reg=0x%x\n", __func__
, data
, reg
);
244 static inline unsigned long coda_isbusy(struct coda_dev
*dev
)
246 return coda_read(dev
, CODA_REG_BIT_BUSY
);
249 static inline int coda_is_initialized(struct coda_dev
*dev
)
251 return (coda_read(dev
, CODA_REG_BIT_CUR_PC
) != 0);
254 static int coda_wait_timeout(struct coda_dev
*dev
)
256 unsigned long timeout
= jiffies
+ msecs_to_jiffies(1000);
258 while (coda_isbusy(dev
)) {
259 if (time_after(jiffies
, timeout
))
265 static void coda_command_async(struct coda_ctx
*ctx
, int cmd
)
267 struct coda_dev
*dev
= ctx
->dev
;
269 if (dev
->devtype
->product
== CODA_7541
) {
270 /* Restore context related registers to CODA */
271 coda_write(dev
, ctx
->bit_stream_param
,
272 CODA_REG_BIT_BIT_STREAM_PARAM
);
273 coda_write(dev
, ctx
->frm_dis_flg
,
274 CODA_REG_BIT_FRM_DIS_FLG(ctx
->reg_idx
));
275 coda_write(dev
, ctx
->workbuf
.paddr
, CODA_REG_BIT_WORK_BUF_ADDR
);
278 coda_write(dev
, CODA_REG_BIT_BUSY_FLAG
, CODA_REG_BIT_BUSY
);
280 coda_write(dev
, ctx
->idx
, CODA_REG_BIT_RUN_INDEX
);
281 coda_write(dev
, ctx
->params
.codec_mode
, CODA_REG_BIT_RUN_COD_STD
);
282 coda_write(dev
, ctx
->params
.codec_mode_aux
, CODA7_REG_BIT_RUN_AUX_STD
);
284 coda_write(dev
, cmd
, CODA_REG_BIT_RUN_COMMAND
);
287 static int coda_command_sync(struct coda_ctx
*ctx
, int cmd
)
289 struct coda_dev
*dev
= ctx
->dev
;
291 coda_command_async(ctx
, cmd
);
292 return coda_wait_timeout(dev
);
295 static struct coda_q_data
*get_q_data(struct coda_ctx
*ctx
,
296 enum v4l2_buf_type type
)
299 case V4L2_BUF_TYPE_VIDEO_OUTPUT
:
300 return &(ctx
->q_data
[V4L2_M2M_SRC
]);
301 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
302 return &(ctx
->q_data
[V4L2_M2M_DST
]);
310 * Array of all formats supported by any version of Coda:
312 static struct coda_fmt coda_formats
[] = {
314 .name
= "YUV 4:2:0 Planar, YCbCr",
315 .fourcc
= V4L2_PIX_FMT_YUV420
,
318 .name
= "YUV 4:2:0 Planar, YCrCb",
319 .fourcc
= V4L2_PIX_FMT_YVU420
,
322 .name
= "H264 Encoded Stream",
323 .fourcc
= V4L2_PIX_FMT_H264
,
326 .name
= "MPEG4 Encoded Stream",
327 .fourcc
= V4L2_PIX_FMT_MPEG4
,
331 #define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
332 { mode, src_fourcc, dst_fourcc, max_w, max_h }
335 * Arrays of codecs supported by each given version of Coda:
339 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
341 static struct coda_codec codadx6_codecs
[] = {
342 CODA_CODEC(CODADX6_MODE_ENCODE_H264
, V4L2_PIX_FMT_YUV420
, V4L2_PIX_FMT_H264
, 720, 576),
343 CODA_CODEC(CODADX6_MODE_ENCODE_MP4
, V4L2_PIX_FMT_YUV420
, V4L2_PIX_FMT_MPEG4
, 720, 576),
346 static struct coda_codec coda7_codecs
[] = {
347 CODA_CODEC(CODA7_MODE_ENCODE_H264
, V4L2_PIX_FMT_YUV420
, V4L2_PIX_FMT_H264
, 1280, 720),
348 CODA_CODEC(CODA7_MODE_ENCODE_MP4
, V4L2_PIX_FMT_YUV420
, V4L2_PIX_FMT_MPEG4
, 1280, 720),
349 CODA_CODEC(CODA7_MODE_DECODE_H264
, V4L2_PIX_FMT_H264
, V4L2_PIX_FMT_YUV420
, 1920, 1080),
350 CODA_CODEC(CODA7_MODE_DECODE_MP4
, V4L2_PIX_FMT_MPEG4
, V4L2_PIX_FMT_YUV420
, 1920, 1080),
353 static bool coda_format_is_yuv(u32 fourcc
)
356 case V4L2_PIX_FMT_YUV420
:
357 case V4L2_PIX_FMT_YVU420
:
365 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
368 static u32
coda_format_normalize_yuv(u32 fourcc
)
370 return coda_format_is_yuv(fourcc
) ? V4L2_PIX_FMT_YUV420
: fourcc
;
373 static struct coda_codec
*coda_find_codec(struct coda_dev
*dev
, int src_fourcc
,
376 struct coda_codec
*codecs
= dev
->devtype
->codecs
;
377 int num_codecs
= dev
->devtype
->num_codecs
;
380 src_fourcc
= coda_format_normalize_yuv(src_fourcc
);
381 dst_fourcc
= coda_format_normalize_yuv(dst_fourcc
);
382 if (src_fourcc
== dst_fourcc
)
385 for (k
= 0; k
< num_codecs
; k
++) {
386 if (codecs
[k
].src_fourcc
== src_fourcc
&&
387 codecs
[k
].dst_fourcc
== dst_fourcc
)
398 * V4L2 ioctl() operations.
400 static int vidioc_querycap(struct file
*file
, void *priv
,
401 struct v4l2_capability
*cap
)
403 strlcpy(cap
->driver
, CODA_NAME
, sizeof(cap
->driver
));
404 strlcpy(cap
->card
, CODA_NAME
, sizeof(cap
->card
));
405 strlcpy(cap
->bus_info
, "platform:" CODA_NAME
, sizeof(cap
->bus_info
));
407 * This is only a mem-to-mem video device. The capture and output
408 * device capability flags are left only for backward compatibility
409 * and are scheduled for removal.
411 cap
->device_caps
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_VIDEO_OUTPUT
|
412 V4L2_CAP_VIDEO_M2M
| V4L2_CAP_STREAMING
;
413 cap
->capabilities
= cap
->device_caps
| V4L2_CAP_DEVICE_CAPS
;
418 static int enum_fmt(void *priv
, struct v4l2_fmtdesc
*f
,
419 enum v4l2_buf_type type
, int src_fourcc
)
421 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
422 struct coda_codec
*codecs
= ctx
->dev
->devtype
->codecs
;
423 struct coda_fmt
*formats
= coda_formats
;
424 struct coda_fmt
*fmt
;
425 int num_codecs
= ctx
->dev
->devtype
->num_codecs
;
426 int num_formats
= ARRAY_SIZE(coda_formats
);
429 for (i
= 0; i
< num_formats
; i
++) {
430 /* Both uncompressed formats are always supported */
431 if (coda_format_is_yuv(formats
[i
].fourcc
) &&
432 !coda_format_is_yuv(src_fourcc
)) {
438 /* Compressed formats may be supported, check the codec list */
439 for (k
= 0; k
< num_codecs
; k
++) {
440 /* if src_fourcc is set, only consider matching codecs */
441 if (type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
442 formats
[i
].fourcc
== codecs
[k
].dst_fourcc
&&
443 (!src_fourcc
|| src_fourcc
== codecs
[k
].src_fourcc
))
445 if (type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
&&
446 formats
[i
].fourcc
== codecs
[k
].src_fourcc
)
449 if (k
< num_codecs
) {
456 if (i
< num_formats
) {
458 strlcpy(f
->description
, fmt
->name
, sizeof(f
->description
));
459 f
->pixelformat
= fmt
->fourcc
;
460 if (!coda_format_is_yuv(fmt
->fourcc
))
461 f
->flags
|= V4L2_FMT_FLAG_COMPRESSED
;
465 /* Format not found */
469 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
470 struct v4l2_fmtdesc
*f
)
472 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
473 struct vb2_queue
*src_vq
;
474 struct coda_q_data
*q_data_src
;
476 /* If the source format is already fixed, only list matching formats */
477 src_vq
= v4l2_m2m_get_vq(ctx
->m2m_ctx
, V4L2_BUF_TYPE_VIDEO_OUTPUT
);
478 if (vb2_is_streaming(src_vq
)) {
479 q_data_src
= get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_OUTPUT
);
481 return enum_fmt(priv
, f
, V4L2_BUF_TYPE_VIDEO_CAPTURE
,
485 return enum_fmt(priv
, f
, V4L2_BUF_TYPE_VIDEO_CAPTURE
, 0);
488 static int vidioc_enum_fmt_vid_out(struct file
*file
, void *priv
,
489 struct v4l2_fmtdesc
*f
)
491 return enum_fmt(priv
, f
, V4L2_BUF_TYPE_VIDEO_OUTPUT
, 0);
494 static int vidioc_g_fmt(struct file
*file
, void *priv
, struct v4l2_format
*f
)
496 struct vb2_queue
*vq
;
497 struct coda_q_data
*q_data
;
498 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
500 vq
= v4l2_m2m_get_vq(ctx
->m2m_ctx
, f
->type
);
504 q_data
= get_q_data(ctx
, f
->type
);
506 f
->fmt
.pix
.field
= V4L2_FIELD_NONE
;
507 f
->fmt
.pix
.pixelformat
= q_data
->fourcc
;
508 f
->fmt
.pix
.width
= q_data
->width
;
509 f
->fmt
.pix
.height
= q_data
->height
;
510 if (coda_format_is_yuv(f
->fmt
.pix
.pixelformat
))
511 f
->fmt
.pix
.bytesperline
= round_up(f
->fmt
.pix
.width
, 2);
512 else /* encoded formats h.264/mpeg4 */
513 f
->fmt
.pix
.bytesperline
= 0;
515 f
->fmt
.pix
.sizeimage
= q_data
->sizeimage
;
516 f
->fmt
.pix
.colorspace
= ctx
->colorspace
;
521 static int vidioc_try_fmt(struct coda_codec
*codec
, struct v4l2_format
*f
)
523 unsigned int max_w
, max_h
;
524 enum v4l2_field field
;
526 field
= f
->fmt
.pix
.field
;
527 if (field
== V4L2_FIELD_ANY
)
528 field
= V4L2_FIELD_NONE
;
529 else if (V4L2_FIELD_NONE
!= field
)
532 /* V4L2 specification suggests the driver corrects the format struct
533 * if any of the dimensions is unsupported */
534 f
->fmt
.pix
.field
= field
;
537 max_w
= codec
->max_w
;
538 max_h
= codec
->max_h
;
543 v4l_bound_align_image(&f
->fmt
.pix
.width
, MIN_W
, max_w
,
544 W_ALIGN
, &f
->fmt
.pix
.height
,
545 MIN_H
, max_h
, H_ALIGN
, S_ALIGN
);
547 if (coda_format_is_yuv(f
->fmt
.pix
.pixelformat
)) {
548 /* Frame stride must be multiple of 8 */
549 f
->fmt
.pix
.bytesperline
= round_up(f
->fmt
.pix
.width
, 8);
550 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.bytesperline
*
551 f
->fmt
.pix
.height
* 3 / 2;
552 } else { /*encoded formats h.264/mpeg4 */
553 f
->fmt
.pix
.bytesperline
= 0;
554 f
->fmt
.pix
.sizeimage
= CODA_MAX_FRAME_SIZE
;
560 static int vidioc_try_fmt_vid_cap(struct file
*file
, void *priv
,
561 struct v4l2_format
*f
)
563 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
564 struct coda_codec
*codec
;
565 struct vb2_queue
*src_vq
;
569 * If the source format is already fixed, try to find a codec that
570 * converts to the given destination format
572 src_vq
= v4l2_m2m_get_vq(ctx
->m2m_ctx
, V4L2_BUF_TYPE_VIDEO_OUTPUT
);
573 if (vb2_is_streaming(src_vq
)) {
574 struct coda_q_data
*q_data_src
;
576 q_data_src
= get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_OUTPUT
);
577 codec
= coda_find_codec(ctx
->dev
, q_data_src
->fourcc
,
578 f
->fmt
.pix
.pixelformat
);
582 /* Otherwise determine codec by encoded format, if possible */
583 codec
= coda_find_codec(ctx
->dev
, V4L2_PIX_FMT_YUV420
,
584 f
->fmt
.pix
.pixelformat
);
587 f
->fmt
.pix
.colorspace
= ctx
->colorspace
;
589 ret
= vidioc_try_fmt(codec
, f
);
593 /* The h.264 decoder only returns complete 16x16 macroblocks */
594 if (codec
&& codec
->src_fourcc
== V4L2_PIX_FMT_H264
) {
595 f
->fmt
.pix
.width
= round_up(f
->fmt
.pix
.width
, 16);
596 f
->fmt
.pix
.height
= round_up(f
->fmt
.pix
.height
, 16);
597 f
->fmt
.pix
.bytesperline
= f
->fmt
.pix
.width
;
598 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.bytesperline
*
599 f
->fmt
.pix
.height
* 3 / 2;
605 static int vidioc_try_fmt_vid_out(struct file
*file
, void *priv
,
606 struct v4l2_format
*f
)
608 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
609 struct coda_codec
*codec
;
611 /* Determine codec by encoded format, returns NULL if raw or invalid */
612 codec
= coda_find_codec(ctx
->dev
, f
->fmt
.pix
.pixelformat
,
613 V4L2_PIX_FMT_YUV420
);
615 if (!f
->fmt
.pix
.colorspace
)
616 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_REC709
;
618 return vidioc_try_fmt(codec
, f
);
621 static int vidioc_s_fmt(struct coda_ctx
*ctx
, struct v4l2_format
*f
)
623 struct coda_q_data
*q_data
;
624 struct vb2_queue
*vq
;
626 vq
= v4l2_m2m_get_vq(ctx
->m2m_ctx
, f
->type
);
630 q_data
= get_q_data(ctx
, f
->type
);
634 if (vb2_is_busy(vq
)) {
635 v4l2_err(&ctx
->dev
->v4l2_dev
, "%s queue busy\n", __func__
);
639 q_data
->fourcc
= f
->fmt
.pix
.pixelformat
;
640 q_data
->width
= f
->fmt
.pix
.width
;
641 q_data
->height
= f
->fmt
.pix
.height
;
642 q_data
->sizeimage
= f
->fmt
.pix
.sizeimage
;
644 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
645 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
646 f
->type
, q_data
->width
, q_data
->height
, q_data
->fourcc
);
651 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
652 struct v4l2_format
*f
)
654 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
657 ret
= vidioc_try_fmt_vid_cap(file
, priv
, f
);
661 return vidioc_s_fmt(ctx
, f
);
664 static int vidioc_s_fmt_vid_out(struct file
*file
, void *priv
,
665 struct v4l2_format
*f
)
667 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
670 ret
= vidioc_try_fmt_vid_out(file
, priv
, f
);
674 ret
= vidioc_s_fmt(ctx
, f
);
676 ctx
->colorspace
= f
->fmt
.pix
.colorspace
;
681 static int vidioc_reqbufs(struct file
*file
, void *priv
,
682 struct v4l2_requestbuffers
*reqbufs
)
684 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
686 return v4l2_m2m_reqbufs(file
, ctx
->m2m_ctx
, reqbufs
);
689 static int vidioc_querybuf(struct file
*file
, void *priv
,
690 struct v4l2_buffer
*buf
)
692 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
694 return v4l2_m2m_querybuf(file
, ctx
->m2m_ctx
, buf
);
697 static int vidioc_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*buf
)
699 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
701 return v4l2_m2m_qbuf(file
, ctx
->m2m_ctx
, buf
);
704 static int vidioc_expbuf(struct file
*file
, void *priv
,
705 struct v4l2_exportbuffer
*eb
)
707 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
709 return v4l2_m2m_expbuf(file
, ctx
->m2m_ctx
, eb
);
712 static bool coda_buf_is_end_of_stream(struct coda_ctx
*ctx
,
713 struct v4l2_buffer
*buf
)
715 struct vb2_queue
*src_vq
;
717 src_vq
= v4l2_m2m_get_vq(ctx
->m2m_ctx
, V4L2_BUF_TYPE_VIDEO_OUTPUT
);
719 return ((ctx
->bit_stream_param
& CODA_BIT_STREAM_END_FLAG
) &&
720 (buf
->sequence
== (ctx
->qsequence
- 1)));
723 static int vidioc_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*buf
)
725 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
728 ret
= v4l2_m2m_dqbuf(file
, ctx
->m2m_ctx
, buf
);
730 /* If this is the last capture buffer, emit an end-of-stream event */
731 if (buf
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
732 coda_buf_is_end_of_stream(ctx
, buf
)) {
733 const struct v4l2_event eos_event
= {
734 .type
= V4L2_EVENT_EOS
737 v4l2_event_queue_fh(&ctx
->fh
, &eos_event
);
743 static int vidioc_create_bufs(struct file
*file
, void *priv
,
744 struct v4l2_create_buffers
*create
)
746 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
748 return v4l2_m2m_create_bufs(file
, ctx
->m2m_ctx
, create
);
751 static int vidioc_streamon(struct file
*file
, void *priv
,
752 enum v4l2_buf_type type
)
754 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
756 return v4l2_m2m_streamon(file
, ctx
->m2m_ctx
, type
);
759 static int vidioc_streamoff(struct file
*file
, void *priv
,
760 enum v4l2_buf_type type
)
762 struct coda_ctx
*ctx
= fh_to_ctx(priv
);
766 * This indirectly calls __vb2_queue_cancel, which dequeues all buffers.
767 * We therefore have to lock it against running hardware in this context,
768 * which still needs the buffers.
770 mutex_lock(&ctx
->buffer_mutex
);
771 ret
= v4l2_m2m_streamoff(file
, ctx
->m2m_ctx
, type
);
772 mutex_unlock(&ctx
->buffer_mutex
);
777 static int vidioc_decoder_cmd(struct file
*file
, void *fh
,
778 struct v4l2_decoder_cmd
*dc
)
780 struct coda_ctx
*ctx
= fh_to_ctx(fh
);
782 if (dc
->cmd
!= V4L2_DEC_CMD_STOP
)
785 if ((dc
->flags
& V4L2_DEC_CMD_STOP_TO_BLACK
) ||
786 (dc
->flags
& V4L2_DEC_CMD_STOP_IMMEDIATELY
))
789 if (dc
->stop
.pts
!= 0)
792 if (ctx
->inst_type
!= CODA_INST_DECODER
)
795 /* Set the strem-end flag on this context */
796 ctx
->bit_stream_param
|= CODA_BIT_STREAM_END_FLAG
;
801 static int vidioc_subscribe_event(struct v4l2_fh
*fh
,
802 const struct v4l2_event_subscription
*sub
)
806 return v4l2_event_subscribe(fh
, sub
, 0, NULL
);
808 return v4l2_ctrl_subscribe_event(fh
, sub
);
812 static const struct v4l2_ioctl_ops coda_ioctl_ops
= {
813 .vidioc_querycap
= vidioc_querycap
,
815 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
816 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt
,
817 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
818 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
820 .vidioc_enum_fmt_vid_out
= vidioc_enum_fmt_vid_out
,
821 .vidioc_g_fmt_vid_out
= vidioc_g_fmt
,
822 .vidioc_try_fmt_vid_out
= vidioc_try_fmt_vid_out
,
823 .vidioc_s_fmt_vid_out
= vidioc_s_fmt_vid_out
,
825 .vidioc_reqbufs
= vidioc_reqbufs
,
826 .vidioc_querybuf
= vidioc_querybuf
,
828 .vidioc_qbuf
= vidioc_qbuf
,
829 .vidioc_expbuf
= vidioc_expbuf
,
830 .vidioc_dqbuf
= vidioc_dqbuf
,
831 .vidioc_create_bufs
= vidioc_create_bufs
,
833 .vidioc_streamon
= vidioc_streamon
,
834 .vidioc_streamoff
= vidioc_streamoff
,
836 .vidioc_decoder_cmd
= vidioc_decoder_cmd
,
838 .vidioc_subscribe_event
= vidioc_subscribe_event
,
839 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
842 static int coda_start_decoding(struct coda_ctx
*ctx
);
844 static void coda_skip_run(struct work_struct
*work
)
846 struct coda_ctx
*ctx
= container_of(work
, struct coda_ctx
, skip_run
);
848 v4l2_m2m_job_finish(ctx
->dev
->m2m_dev
, ctx
->m2m_ctx
);
851 static inline int coda_get_bitstream_payload(struct coda_ctx
*ctx
)
853 return kfifo_len(&ctx
->bitstream_fifo
);
856 static void coda_kfifo_sync_from_device(struct coda_ctx
*ctx
)
858 struct __kfifo
*kfifo
= &ctx
->bitstream_fifo
.kfifo
;
859 struct coda_dev
*dev
= ctx
->dev
;
862 rd_ptr
= coda_read(dev
, CODA_REG_BIT_RD_PTR(ctx
->reg_idx
));
863 kfifo
->out
= (kfifo
->in
& ~kfifo
->mask
) |
864 (rd_ptr
- ctx
->bitstream
.paddr
);
865 if (kfifo
->out
> kfifo
->in
)
866 kfifo
->out
-= kfifo
->mask
+ 1;
869 static void coda_kfifo_sync_to_device_full(struct coda_ctx
*ctx
)
871 struct __kfifo
*kfifo
= &ctx
->bitstream_fifo
.kfifo
;
872 struct coda_dev
*dev
= ctx
->dev
;
875 rd_ptr
= ctx
->bitstream
.paddr
+ (kfifo
->out
& kfifo
->mask
);
876 coda_write(dev
, rd_ptr
, CODA_REG_BIT_RD_PTR(ctx
->reg_idx
));
877 wr_ptr
= ctx
->bitstream
.paddr
+ (kfifo
->in
& kfifo
->mask
);
878 coda_write(dev
, wr_ptr
, CODA_REG_BIT_WR_PTR(ctx
->reg_idx
));
881 static void coda_kfifo_sync_to_device_write(struct coda_ctx
*ctx
)
883 struct __kfifo
*kfifo
= &ctx
->bitstream_fifo
.kfifo
;
884 struct coda_dev
*dev
= ctx
->dev
;
887 wr_ptr
= ctx
->bitstream
.paddr
+ (kfifo
->in
& kfifo
->mask
);
888 coda_write(dev
, wr_ptr
, CODA_REG_BIT_WR_PTR(ctx
->reg_idx
));
891 static int coda_bitstream_queue(struct coda_ctx
*ctx
, struct vb2_buffer
*src_buf
)
893 u32 src_size
= vb2_get_plane_payload(src_buf
, 0);
896 n
= kfifo_in(&ctx
->bitstream_fifo
, vb2_plane_vaddr(src_buf
, 0), src_size
);
900 dma_sync_single_for_device(&ctx
->dev
->plat_dev
->dev
, ctx
->bitstream
.paddr
,
901 ctx
->bitstream
.size
, DMA_TO_DEVICE
);
908 static bool coda_bitstream_try_queue(struct coda_ctx
*ctx
,
909 struct vb2_buffer
*src_buf
)
913 if (coda_get_bitstream_payload(ctx
) +
914 vb2_get_plane_payload(src_buf
, 0) + 512 >= ctx
->bitstream
.size
)
917 if (vb2_plane_vaddr(src_buf
, 0) == NULL
) {
918 v4l2_err(&ctx
->dev
->v4l2_dev
, "trying to queue empty buffer\n");
922 ret
= coda_bitstream_queue(ctx
, src_buf
);
924 v4l2_err(&ctx
->dev
->v4l2_dev
, "bitstream buffer overflow\n");
927 /* Sync read pointer to device */
928 if (ctx
== v4l2_m2m_get_curr_priv(ctx
->dev
->m2m_dev
))
929 coda_kfifo_sync_to_device_write(ctx
);
931 ctx
->prescan_failed
= false;
936 static void coda_fill_bitstream(struct coda_ctx
*ctx
)
938 struct vb2_buffer
*src_buf
;
940 while (v4l2_m2m_num_src_bufs_ready(ctx
->m2m_ctx
) > 0) {
941 src_buf
= v4l2_m2m_next_src_buf(ctx
->m2m_ctx
);
943 if (coda_bitstream_try_queue(ctx
, src_buf
)) {
944 src_buf
= v4l2_m2m_src_buf_remove(ctx
->m2m_ctx
);
945 v4l2_m2m_buf_done(src_buf
, VB2_BUF_STATE_DONE
);
953 * Mem-to-mem operations.
955 static int coda_prepare_decode(struct coda_ctx
*ctx
)
957 struct vb2_buffer
*dst_buf
;
958 struct coda_dev
*dev
= ctx
->dev
;
959 struct coda_q_data
*q_data_dst
;
961 u32 picture_y
, picture_cb
, picture_cr
;
963 dst_buf
= v4l2_m2m_next_dst_buf(ctx
->m2m_ctx
);
964 q_data_dst
= get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_CAPTURE
);
966 if (ctx
->params
.rot_mode
& CODA_ROT_90
) {
967 stridey
= q_data_dst
->height
;
968 height
= q_data_dst
->width
;
970 stridey
= q_data_dst
->width
;
971 height
= q_data_dst
->height
;
974 /* Try to copy source buffer contents into the bitstream ringbuffer */
975 mutex_lock(&ctx
->bitstream_mutex
);
976 coda_fill_bitstream(ctx
);
977 mutex_unlock(&ctx
->bitstream_mutex
);
979 if (coda_get_bitstream_payload(ctx
) < 512 &&
980 (!(ctx
->bit_stream_param
& CODA_BIT_STREAM_END_FLAG
))) {
981 v4l2_dbg(1, coda_debug
, &dev
->v4l2_dev
,
982 "bitstream payload: %d, skipping\n",
983 coda_get_bitstream_payload(ctx
));
984 schedule_work(&ctx
->skip_run
);
988 /* Run coda_start_decoding (again) if not yet initialized */
989 if (!ctx
->initialized
) {
990 int ret
= coda_start_decoding(ctx
);
992 v4l2_err(&dev
->v4l2_dev
, "failed to start decoding\n");
993 schedule_work(&ctx
->skip_run
);
996 ctx
->initialized
= 1;
1000 /* Set rotator output */
1001 picture_y
= vb2_dma_contig_plane_dma_addr(dst_buf
, 0);
1002 if (q_data_dst
->fourcc
== V4L2_PIX_FMT_YVU420
) {
1003 /* Switch Cr and Cb for YVU420 format */
1004 picture_cr
= picture_y
+ stridey
* height
;
1005 picture_cb
= picture_cr
+ stridey
/ 2 * height
/ 2;
1007 picture_cb
= picture_y
+ stridey
* height
;
1008 picture_cr
= picture_cb
+ stridey
/ 2 * height
/ 2;
1010 coda_write(dev
, picture_y
, CODA_CMD_DEC_PIC_ROT_ADDR_Y
);
1011 coda_write(dev
, picture_cb
, CODA_CMD_DEC_PIC_ROT_ADDR_CB
);
1012 coda_write(dev
, picture_cr
, CODA_CMD_DEC_PIC_ROT_ADDR_CR
);
1013 coda_write(dev
, stridey
, CODA_CMD_DEC_PIC_ROT_STRIDE
);
1014 coda_write(dev
, CODA_ROT_MIR_ENABLE
| ctx
->params
.rot_mode
,
1015 CODA_CMD_DEC_PIC_ROT_MODE
);
1017 switch (dev
->devtype
->product
) {
1021 coda_write(dev
, CODA_PRE_SCAN_EN
, CODA_CMD_DEC_PIC_OPTION
);
1025 coda_write(dev
, 0, CODA_CMD_DEC_PIC_SKIP_NUM
);
1027 coda_write(dev
, 0, CODA_CMD_DEC_PIC_BB_START
);
1028 coda_write(dev
, 0, CODA_CMD_DEC_PIC_START_BYTE
);
1033 static void coda_prepare_encode(struct coda_ctx
*ctx
)
1035 struct coda_q_data
*q_data_src
, *q_data_dst
;
1036 struct vb2_buffer
*src_buf
, *dst_buf
;
1037 struct coda_dev
*dev
= ctx
->dev
;
1039 int quant_param
= 0;
1040 u32 picture_y
, picture_cb
, picture_cr
;
1041 u32 pic_stream_buffer_addr
, pic_stream_buffer_size
;
1044 src_buf
= v4l2_m2m_next_src_buf(ctx
->m2m_ctx
);
1045 dst_buf
= v4l2_m2m_next_dst_buf(ctx
->m2m_ctx
);
1046 q_data_src
= get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_OUTPUT
);
1047 q_data_dst
= get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_CAPTURE
);
1048 dst_fourcc
= q_data_dst
->fourcc
;
1050 src_buf
->v4l2_buf
.sequence
= ctx
->osequence
;
1051 dst_buf
->v4l2_buf
.sequence
= ctx
->osequence
;
1055 * Workaround coda firmware BUG that only marks the first
1056 * frame as IDR. This is a problem for some decoders that can't
1057 * recover when a frame is lost.
1059 if (src_buf
->v4l2_buf
.sequence
% ctx
->params
.gop_size
) {
1060 src_buf
->v4l2_buf
.flags
|= V4L2_BUF_FLAG_PFRAME
;
1061 src_buf
->v4l2_buf
.flags
&= ~V4L2_BUF_FLAG_KEYFRAME
;
1063 src_buf
->v4l2_buf
.flags
|= V4L2_BUF_FLAG_KEYFRAME
;
1064 src_buf
->v4l2_buf
.flags
&= ~V4L2_BUF_FLAG_PFRAME
;
1068 * Copy headers at the beginning of the first frame for H.264 only.
1069 * In MPEG4 they are already copied by the coda.
1071 if (src_buf
->v4l2_buf
.sequence
== 0) {
1072 pic_stream_buffer_addr
=
1073 vb2_dma_contig_plane_dma_addr(dst_buf
, 0) +
1074 ctx
->vpu_header_size
[0] +
1075 ctx
->vpu_header_size
[1] +
1076 ctx
->vpu_header_size
[2];
1077 pic_stream_buffer_size
= CODA_MAX_FRAME_SIZE
-
1078 ctx
->vpu_header_size
[0] -
1079 ctx
->vpu_header_size
[1] -
1080 ctx
->vpu_header_size
[2];
1081 memcpy(vb2_plane_vaddr(dst_buf
, 0),
1082 &ctx
->vpu_header
[0][0], ctx
->vpu_header_size
[0]);
1083 memcpy(vb2_plane_vaddr(dst_buf
, 0) + ctx
->vpu_header_size
[0],
1084 &ctx
->vpu_header
[1][0], ctx
->vpu_header_size
[1]);
1085 memcpy(vb2_plane_vaddr(dst_buf
, 0) + ctx
->vpu_header_size
[0] +
1086 ctx
->vpu_header_size
[1], &ctx
->vpu_header
[2][0],
1087 ctx
->vpu_header_size
[2]);
1089 pic_stream_buffer_addr
=
1090 vb2_dma_contig_plane_dma_addr(dst_buf
, 0);
1091 pic_stream_buffer_size
= CODA_MAX_FRAME_SIZE
;
1094 if (src_buf
->v4l2_buf
.flags
& V4L2_BUF_FLAG_KEYFRAME
) {
1096 switch (dst_fourcc
) {
1097 case V4L2_PIX_FMT_H264
:
1098 quant_param
= ctx
->params
.h264_intra_qp
;
1100 case V4L2_PIX_FMT_MPEG4
:
1101 quant_param
= ctx
->params
.mpeg4_intra_qp
;
1104 v4l2_warn(&ctx
->dev
->v4l2_dev
,
1105 "cannot set intra qp, fmt not supported\n");
1110 switch (dst_fourcc
) {
1111 case V4L2_PIX_FMT_H264
:
1112 quant_param
= ctx
->params
.h264_inter_qp
;
1114 case V4L2_PIX_FMT_MPEG4
:
1115 quant_param
= ctx
->params
.mpeg4_inter_qp
;
1118 v4l2_warn(&ctx
->dev
->v4l2_dev
,
1119 "cannot set inter qp, fmt not supported\n");
1125 coda_write(dev
, CODA_ROT_MIR_ENABLE
| ctx
->params
.rot_mode
, CODA_CMD_ENC_PIC_ROT_MODE
);
1126 coda_write(dev
, quant_param
, CODA_CMD_ENC_PIC_QS
);
1129 picture_y
= vb2_dma_contig_plane_dma_addr(src_buf
, 0);
1130 switch (q_data_src
->fourcc
) {
1131 case V4L2_PIX_FMT_YVU420
:
1132 /* Switch Cb and Cr for YVU420 format */
1133 picture_cr
= picture_y
+ q_data_src
->width
* q_data_src
->height
;
1134 picture_cb
= picture_cr
+ q_data_src
->width
/ 2 *
1135 q_data_src
->height
/ 2;
1137 case V4L2_PIX_FMT_YUV420
:
1139 picture_cb
= picture_y
+ q_data_src
->width
* q_data_src
->height
;
1140 picture_cr
= picture_cb
+ q_data_src
->width
/ 2 *
1141 q_data_src
->height
/ 2;
1145 coda_write(dev
, picture_y
, CODA_CMD_ENC_PIC_SRC_ADDR_Y
);
1146 coda_write(dev
, picture_cb
, CODA_CMD_ENC_PIC_SRC_ADDR_CB
);
1147 coda_write(dev
, picture_cr
, CODA_CMD_ENC_PIC_SRC_ADDR_CR
);
1148 coda_write(dev
, force_ipicture
<< 1 & 0x2,
1149 CODA_CMD_ENC_PIC_OPTION
);
1151 coda_write(dev
, pic_stream_buffer_addr
, CODA_CMD_ENC_PIC_BB_START
);
1152 coda_write(dev
, pic_stream_buffer_size
/ 1024,
1153 CODA_CMD_ENC_PIC_BB_SIZE
);
1156 static void coda_device_run(void *m2m_priv
)
1158 struct coda_ctx
*ctx
= m2m_priv
;
1159 struct coda_dev
*dev
= ctx
->dev
;
1162 mutex_lock(&ctx
->buffer_mutex
);
1165 * If streamoff dequeued all buffers before we could get the lock,
1166 * just bail out immediately.
1168 if ((!v4l2_m2m_num_src_bufs_ready(ctx
->m2m_ctx
) &&
1169 ctx
->inst_type
!= CODA_INST_DECODER
) ||
1170 !v4l2_m2m_num_dst_bufs_ready(ctx
->m2m_ctx
)) {
1171 v4l2_dbg(1, coda_debug
, &dev
->v4l2_dev
,
1172 "%d: device_run without buffers\n", ctx
->idx
);
1173 mutex_unlock(&ctx
->buffer_mutex
);
1174 schedule_work(&ctx
->skip_run
);
1178 mutex_lock(&dev
->coda_mutex
);
1180 if (ctx
->inst_type
== CODA_INST_DECODER
) {
1181 ret
= coda_prepare_decode(ctx
);
1183 mutex_unlock(&dev
->coda_mutex
);
1184 mutex_unlock(&ctx
->buffer_mutex
);
1185 /* job_finish scheduled by prepare_decode */
1189 coda_prepare_encode(ctx
);
1192 if (dev
->devtype
->product
!= CODA_DX6
)
1193 coda_write(dev
, ctx
->iram_info
.axi_sram_use
,
1194 CODA7_REG_BIT_AXI_SRAM_USE
);
1196 /* 1 second timeout in case CODA locks up */
1197 schedule_delayed_work(&dev
->timeout
, HZ
);
1199 if (ctx
->inst_type
== CODA_INST_DECODER
)
1200 coda_kfifo_sync_to_device_full(ctx
);
1201 coda_command_async(ctx
, CODA_COMMAND_PIC_RUN
);
1204 static int coda_job_ready(void *m2m_priv
)
1206 struct coda_ctx
*ctx
= m2m_priv
;
1209 * For both 'P' and 'key' frame cases 1 picture
1210 * and 1 frame are needed. In the decoder case,
1211 * the compressed frame can be in the bitstream.
1213 if (!v4l2_m2m_num_src_bufs_ready(ctx
->m2m_ctx
) &&
1214 ctx
->inst_type
!= CODA_INST_DECODER
) {
1215 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
1216 "not ready: not enough video buffers.\n");
1220 if (!v4l2_m2m_num_dst_bufs_ready(ctx
->m2m_ctx
)) {
1221 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
1222 "not ready: not enough video capture buffers.\n");
1226 if (ctx
->prescan_failed
||
1227 ((ctx
->inst_type
== CODA_INST_DECODER
) &&
1228 (coda_get_bitstream_payload(ctx
) < 512) &&
1229 !(ctx
->bit_stream_param
& CODA_BIT_STREAM_END_FLAG
))) {
1230 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
1231 "%d: not ready: not enough bitstream data.\n",
1236 if (ctx
->aborting
) {
1237 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
1238 "not ready: aborting\n");
1242 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
1247 static void coda_job_abort(void *priv
)
1249 struct coda_ctx
*ctx
= priv
;
1253 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
1257 static void coda_lock(void *m2m_priv
)
1259 struct coda_ctx
*ctx
= m2m_priv
;
1260 struct coda_dev
*pcdev
= ctx
->dev
;
1261 mutex_lock(&pcdev
->dev_mutex
);
1264 static void coda_unlock(void *m2m_priv
)
1266 struct coda_ctx
*ctx
= m2m_priv
;
1267 struct coda_dev
*pcdev
= ctx
->dev
;
1268 mutex_unlock(&pcdev
->dev_mutex
);
1271 static struct v4l2_m2m_ops coda_m2m_ops
= {
1272 .device_run
= coda_device_run
,
1273 .job_ready
= coda_job_ready
,
1274 .job_abort
= coda_job_abort
,
1276 .unlock
= coda_unlock
,
1279 static void set_default_params(struct coda_ctx
*ctx
)
1284 ctx
->codec
= &ctx
->dev
->devtype
->codecs
[0];
1285 max_w
= ctx
->codec
->max_w
;
1286 max_h
= ctx
->codec
->max_h
;
1288 ctx
->params
.codec_mode
= CODA_MODE_INVALID
;
1289 ctx
->colorspace
= V4L2_COLORSPACE_REC709
;
1290 ctx
->params
.framerate
= 30;
1293 /* Default formats for output and input queues */
1294 ctx
->q_data
[V4L2_M2M_SRC
].fourcc
= ctx
->codec
->src_fourcc
;
1295 ctx
->q_data
[V4L2_M2M_DST
].fourcc
= ctx
->codec
->dst_fourcc
;
1296 ctx
->q_data
[V4L2_M2M_SRC
].width
= max_w
;
1297 ctx
->q_data
[V4L2_M2M_SRC
].height
= max_h
;
1298 ctx
->q_data
[V4L2_M2M_SRC
].sizeimage
= (max_w
* max_h
* 3) / 2;
1299 ctx
->q_data
[V4L2_M2M_DST
].width
= max_w
;
1300 ctx
->q_data
[V4L2_M2M_DST
].height
= max_h
;
1301 ctx
->q_data
[V4L2_M2M_DST
].sizeimage
= CODA_MAX_FRAME_SIZE
;
1307 static int coda_queue_setup(struct vb2_queue
*vq
,
1308 const struct v4l2_format
*fmt
,
1309 unsigned int *nbuffers
, unsigned int *nplanes
,
1310 unsigned int sizes
[], void *alloc_ctxs
[])
1312 struct coda_ctx
*ctx
= vb2_get_drv_priv(vq
);
1313 struct coda_q_data
*q_data
;
1316 q_data
= get_q_data(ctx
, vq
->type
);
1317 size
= q_data
->sizeimage
;
1322 alloc_ctxs
[0] = ctx
->dev
->alloc_ctx
;
1324 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
1325 "get %d buffer(s) of size %d each.\n", *nbuffers
, size
);
1330 static int coda_buf_prepare(struct vb2_buffer
*vb
)
1332 struct coda_ctx
*ctx
= vb2_get_drv_priv(vb
->vb2_queue
);
1333 struct coda_q_data
*q_data
;
1335 q_data
= get_q_data(ctx
, vb
->vb2_queue
->type
);
1337 if (vb2_plane_size(vb
, 0) < q_data
->sizeimage
) {
1338 v4l2_warn(&ctx
->dev
->v4l2_dev
,
1339 "%s data will not fit into plane (%lu < %lu)\n",
1340 __func__
, vb2_plane_size(vb
, 0),
1341 (long)q_data
->sizeimage
);
1348 static void coda_buf_queue(struct vb2_buffer
*vb
)
1350 struct coda_ctx
*ctx
= vb2_get_drv_priv(vb
->vb2_queue
);
1351 struct coda_q_data
*q_data
;
1353 q_data
= get_q_data(ctx
, vb
->vb2_queue
->type
);
1356 * In the decoder case, immediately try to copy the buffer into the
1357 * bitstream ringbuffer and mark it as ready to be dequeued.
1359 if (q_data
->fourcc
== V4L2_PIX_FMT_H264
&&
1360 vb
->vb2_queue
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
) {
1362 * For backwards compatiblity, queuing an empty buffer marks
1365 if (vb2_get_plane_payload(vb
, 0) == 0)
1366 ctx
->bit_stream_param
|= CODA_BIT_STREAM_END_FLAG
;
1367 mutex_lock(&ctx
->bitstream_mutex
);
1368 v4l2_m2m_buf_queue(ctx
->m2m_ctx
, vb
);
1369 coda_fill_bitstream(ctx
);
1370 mutex_unlock(&ctx
->bitstream_mutex
);
1372 v4l2_m2m_buf_queue(ctx
->m2m_ctx
, vb
);
1376 static void coda_wait_prepare(struct vb2_queue
*q
)
1378 struct coda_ctx
*ctx
= vb2_get_drv_priv(q
);
1382 static void coda_wait_finish(struct vb2_queue
*q
)
1384 struct coda_ctx
*ctx
= vb2_get_drv_priv(q
);
1388 static void coda_parabuf_write(struct coda_ctx
*ctx
, int index
, u32 value
)
1390 struct coda_dev
*dev
= ctx
->dev
;
1391 u32
*p
= ctx
->parabuf
.vaddr
;
1393 if (dev
->devtype
->product
== CODA_DX6
)
1396 p
[index
^ 1] = value
;
1399 static int coda_alloc_aux_buf(struct coda_dev
*dev
,
1400 struct coda_aux_buf
*buf
, size_t size
)
1402 buf
->vaddr
= dma_alloc_coherent(&dev
->plat_dev
->dev
, size
, &buf
->paddr
,
1412 static inline int coda_alloc_context_buf(struct coda_ctx
*ctx
,
1413 struct coda_aux_buf
*buf
, size_t size
)
1415 return coda_alloc_aux_buf(ctx
->dev
, buf
, size
);
1418 static void coda_free_aux_buf(struct coda_dev
*dev
,
1419 struct coda_aux_buf
*buf
)
1422 dma_free_coherent(&dev
->plat_dev
->dev
, buf
->size
,
1423 buf
->vaddr
, buf
->paddr
);
1429 static void coda_free_framebuffers(struct coda_ctx
*ctx
)
1433 for (i
= 0; i
< CODA_MAX_FRAMEBUFFERS
; i
++)
1434 coda_free_aux_buf(ctx
->dev
, &ctx
->internal_frames
[i
]);
1437 static int coda_alloc_framebuffers(struct coda_ctx
*ctx
, struct coda_q_data
*q_data
, u32 fourcc
)
1439 struct coda_dev
*dev
= ctx
->dev
;
1440 int height
= q_data
->height
;
1446 if (ctx
->codec
&& ctx
->codec
->src_fourcc
== V4L2_PIX_FMT_H264
)
1447 height
= round_up(height
, 16);
1448 ysize
= round_up(q_data
->width
, 8) * height
;
1450 /* Allocate frame buffers */
1451 for (i
= 0; i
< ctx
->num_internal_frames
; i
++) {
1454 size
= q_data
->sizeimage
;
1455 if (ctx
->codec
->src_fourcc
== V4L2_PIX_FMT_H264
&&
1456 dev
->devtype
->product
!= CODA_DX6
)
1457 ctx
->internal_frames
[i
].size
+= ysize
/4;
1458 ret
= coda_alloc_context_buf(ctx
, &ctx
->internal_frames
[i
], size
);
1460 coda_free_framebuffers(ctx
);
1465 /* Register frame buffers in the parameter buffer */
1466 for (i
= 0; i
< ctx
->num_internal_frames
; i
++) {
1467 paddr
= ctx
->internal_frames
[i
].paddr
;
1468 coda_parabuf_write(ctx
, i
* 3 + 0, paddr
); /* Y */
1469 coda_parabuf_write(ctx
, i
* 3 + 1, paddr
+ ysize
); /* Cb */
1470 coda_parabuf_write(ctx
, i
* 3 + 2, paddr
+ ysize
+ ysize
/4); /* Cr */
1472 /* mvcol buffer for h.264 */
1473 if (ctx
->codec
->src_fourcc
== V4L2_PIX_FMT_H264
&&
1474 dev
->devtype
->product
!= CODA_DX6
)
1475 coda_parabuf_write(ctx
, 96 + i
,
1476 ctx
->internal_frames
[i
].paddr
+
1477 ysize
+ ysize
/4 + ysize
/4);
1480 /* mvcol buffer for mpeg4 */
1481 if ((dev
->devtype
->product
!= CODA_DX6
) &&
1482 (ctx
->codec
->src_fourcc
== V4L2_PIX_FMT_MPEG4
))
1483 coda_parabuf_write(ctx
, 97, ctx
->internal_frames
[i
].paddr
+
1484 ysize
+ ysize
/4 + ysize
/4);
1489 static int coda_h264_padding(int size
, char *p
)
1494 diff
= size
- (size
& ~0x7);
1498 nal_size
= coda_filler_size
[diff
];
1499 memcpy(p
, coda_filler_nal
, nal_size
);
1501 /* Add rbsp stop bit and trailing at the end */
1502 *(p
+ nal_size
- 1) = 0x80;
1507 static void coda_setup_iram(struct coda_ctx
*ctx
)
1509 struct coda_iram_info
*iram_info
= &ctx
->iram_info
;
1510 struct coda_dev
*dev
= ctx
->dev
;
1519 memset(iram_info
, 0, sizeof(*iram_info
));
1520 size
= dev
->iram_size
;
1522 if (dev
->devtype
->product
== CODA_DX6
)
1525 if (ctx
->inst_type
== CODA_INST_ENCODER
) {
1526 struct coda_q_data
*q_data_src
;
1528 q_data_src
= get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_OUTPUT
);
1529 mb_width
= DIV_ROUND_UP(q_data_src
->width
, 16);
1531 /* Prioritize in case IRAM is too small for everything */
1532 me_size
= round_up(round_up(q_data_src
->width
, 16) * 36 + 2048,
1534 iram_info
->search_ram_size
= me_size
;
1535 if (size
>= iram_info
->search_ram_size
) {
1536 if (dev
->devtype
->product
== CODA_7541
)
1537 iram_info
->axi_sram_use
|= CODA7_USE_HOST_ME_ENABLE
;
1538 iram_info
->search_ram_paddr
= dev
->iram_paddr
;
1539 size
-= iram_info
->search_ram_size
;
1541 pr_err("IRAM is smaller than the search ram size\n");
1545 /* Only H.264BP and H.263P3 are considered */
1546 dbk_size
= round_up(128 * mb_width
, 1024);
1547 if (size
>= dbk_size
) {
1548 iram_info
->axi_sram_use
|= CODA7_USE_HOST_DBK_ENABLE
;
1549 iram_info
->buf_dbk_y_use
= dev
->iram_paddr
+
1550 iram_info
->search_ram_size
;
1551 iram_info
->buf_dbk_c_use
= iram_info
->buf_dbk_y_use
+
1558 bitram_size
= round_up(128 * mb_width
, 1024);
1559 if (size
>= bitram_size
) {
1560 iram_info
->axi_sram_use
|= CODA7_USE_HOST_BIT_ENABLE
;
1561 iram_info
->buf_bit_use
= iram_info
->buf_dbk_c_use
+
1563 size
-= bitram_size
;
1568 ipacdc_size
= round_up(128 * mb_width
, 1024);
1569 if (size
>= ipacdc_size
) {
1570 iram_info
->axi_sram_use
|= CODA7_USE_HOST_IP_ENABLE
;
1571 iram_info
->buf_ip_ac_dc_use
= iram_info
->buf_bit_use
+
1573 size
-= ipacdc_size
;
1576 /* OVL and BTP disabled for encoder */
1577 } else if (ctx
->inst_type
== CODA_INST_DECODER
) {
1578 struct coda_q_data
*q_data_dst
;
1581 q_data_dst
= get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_CAPTURE
);
1582 mb_width
= DIV_ROUND_UP(q_data_dst
->width
, 16);
1583 mb_height
= DIV_ROUND_UP(q_data_dst
->height
, 16);
1585 dbk_size
= round_up(256 * mb_width
, 1024);
1586 if (size
>= dbk_size
) {
1587 iram_info
->axi_sram_use
|= CODA7_USE_HOST_DBK_ENABLE
;
1588 iram_info
->buf_dbk_y_use
= dev
->iram_paddr
;
1589 iram_info
->buf_dbk_c_use
= dev
->iram_paddr
+
1596 bitram_size
= round_up(128 * mb_width
, 1024);
1597 if (size
>= bitram_size
) {
1598 iram_info
->axi_sram_use
|= CODA7_USE_HOST_BIT_ENABLE
;
1599 iram_info
->buf_bit_use
= iram_info
->buf_dbk_c_use
+
1601 size
-= bitram_size
;
1606 ipacdc_size
= round_up(128 * mb_width
, 1024);
1607 if (size
>= ipacdc_size
) {
1608 iram_info
->axi_sram_use
|= CODA7_USE_HOST_IP_ENABLE
;
1609 iram_info
->buf_ip_ac_dc_use
= iram_info
->buf_bit_use
+
1611 size
-= ipacdc_size
;
1616 ovl_size
= round_up(80 * mb_width
, 1024);
1620 switch (dev
->devtype
->product
) {
1624 /* i.MX53 uses secondary AXI for IRAM access */
1625 if (iram_info
->axi_sram_use
& CODA7_USE_HOST_BIT_ENABLE
)
1626 iram_info
->axi_sram_use
|= CODA7_USE_BIT_ENABLE
;
1627 if (iram_info
->axi_sram_use
& CODA7_USE_HOST_IP_ENABLE
)
1628 iram_info
->axi_sram_use
|= CODA7_USE_IP_ENABLE
;
1629 if (iram_info
->axi_sram_use
& CODA7_USE_HOST_DBK_ENABLE
)
1630 iram_info
->axi_sram_use
|= CODA7_USE_DBK_ENABLE
;
1631 if (iram_info
->axi_sram_use
& CODA7_USE_HOST_OVL_ENABLE
)
1632 iram_info
->axi_sram_use
|= CODA7_USE_OVL_ENABLE
;
1633 if (iram_info
->axi_sram_use
& CODA7_USE_HOST_ME_ENABLE
)
1634 iram_info
->axi_sram_use
|= CODA7_USE_ME_ENABLE
;
1637 if (!(iram_info
->axi_sram_use
& CODA7_USE_HOST_IP_ENABLE
))
1638 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
1639 "IRAM smaller than needed\n");
1641 if (dev
->devtype
->product
== CODA_7541
) {
1642 /* TODO - Enabling these causes picture errors on CODA7541 */
1643 if (ctx
->inst_type
== CODA_INST_DECODER
) {
1645 iram_info
->axi_sram_use
&= ~(CODA7_USE_HOST_IP_ENABLE
|
1646 CODA7_USE_IP_ENABLE
);
1649 iram_info
->axi_sram_use
&= ~(CODA7_USE_HOST_IP_ENABLE
|
1650 CODA7_USE_HOST_DBK_ENABLE
|
1651 CODA7_USE_IP_ENABLE
|
1652 CODA7_USE_DBK_ENABLE
);
1657 static void coda_free_context_buffers(struct coda_ctx
*ctx
)
1659 struct coda_dev
*dev
= ctx
->dev
;
1661 coda_free_aux_buf(dev
, &ctx
->slicebuf
);
1662 coda_free_aux_buf(dev
, &ctx
->psbuf
);
1663 if (dev
->devtype
->product
!= CODA_DX6
)
1664 coda_free_aux_buf(dev
, &ctx
->workbuf
);
1667 static int coda_alloc_context_buffers(struct coda_ctx
*ctx
,
1668 struct coda_q_data
*q_data
)
1670 struct coda_dev
*dev
= ctx
->dev
;
1674 switch (dev
->devtype
->product
) {
1676 size
= CODA7_WORK_BUF_SIZE
;
1682 if (ctx
->psbuf
.vaddr
) {
1683 v4l2_err(&dev
->v4l2_dev
, "psmembuf still allocated\n");
1686 if (ctx
->slicebuf
.vaddr
) {
1687 v4l2_err(&dev
->v4l2_dev
, "slicebuf still allocated\n");
1690 if (ctx
->workbuf
.vaddr
) {
1691 v4l2_err(&dev
->v4l2_dev
, "context buffer still allocated\n");
1696 if (q_data
->fourcc
== V4L2_PIX_FMT_H264
) {
1697 /* worst case slice size */
1698 size
= (DIV_ROUND_UP(q_data
->width
, 16) *
1699 DIV_ROUND_UP(q_data
->height
, 16)) * 3200 / 8 + 512;
1700 ret
= coda_alloc_context_buf(ctx
, &ctx
->slicebuf
, size
);
1702 v4l2_err(&dev
->v4l2_dev
, "failed to allocate %d byte slice buffer",
1703 ctx
->slicebuf
.size
);
1708 if (dev
->devtype
->product
== CODA_7541
) {
1709 ret
= coda_alloc_context_buf(ctx
, &ctx
->psbuf
, CODA7_PS_BUF_SIZE
);
1711 v4l2_err(&dev
->v4l2_dev
, "failed to allocate psmem buffer");
1716 ret
= coda_alloc_context_buf(ctx
, &ctx
->workbuf
, size
);
1718 v4l2_err(&dev
->v4l2_dev
, "failed to allocate %d byte context buffer",
1726 coda_free_context_buffers(ctx
);
1730 static int coda_start_decoding(struct coda_ctx
*ctx
)
1732 struct coda_q_data
*q_data_src
, *q_data_dst
;
1733 u32 bitstream_buf
, bitstream_size
;
1734 struct coda_dev
*dev
= ctx
->dev
;
1740 /* Start decoding */
1741 q_data_src
= get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_OUTPUT
);
1742 q_data_dst
= get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_CAPTURE
);
1743 bitstream_buf
= ctx
->bitstream
.paddr
;
1744 bitstream_size
= ctx
->bitstream
.size
;
1745 src_fourcc
= q_data_src
->fourcc
;
1747 coda_write(dev
, ctx
->parabuf
.paddr
, CODA_REG_BIT_PARA_BUF_ADDR
);
1749 /* Update coda bitstream read and write pointers from kfifo */
1750 coda_kfifo_sync_to_device_full(ctx
);
1752 ctx
->display_idx
= -1;
1753 ctx
->frm_dis_flg
= 0;
1754 coda_write(dev
, 0, CODA_REG_BIT_FRM_DIS_FLG(ctx
->reg_idx
));
1756 coda_write(dev
, CODA_BIT_DEC_SEQ_INIT_ESCAPE
,
1757 CODA_REG_BIT_BIT_STREAM_PARAM
);
1759 coda_write(dev
, bitstream_buf
, CODA_CMD_DEC_SEQ_BB_START
);
1760 coda_write(dev
, bitstream_size
/ 1024, CODA_CMD_DEC_SEQ_BB_SIZE
);
1762 if (dev
->devtype
->product
== CODA_7541
)
1763 val
|= CODA_REORDER_ENABLE
;
1764 coda_write(dev
, val
, CODA_CMD_DEC_SEQ_OPTION
);
1766 ctx
->params
.codec_mode
= ctx
->codec
->mode
;
1767 ctx
->params
.codec_mode_aux
= 0;
1768 if (src_fourcc
== V4L2_PIX_FMT_H264
) {
1769 if (dev
->devtype
->product
== CODA_7541
) {
1770 coda_write(dev
, ctx
->psbuf
.paddr
,
1771 CODA_CMD_DEC_SEQ_PS_BB_START
);
1772 coda_write(dev
, (CODA7_PS_BUF_SIZE
/ 1024),
1773 CODA_CMD_DEC_SEQ_PS_BB_SIZE
);
1777 if (coda_command_sync(ctx
, CODA_COMMAND_SEQ_INIT
)) {
1778 v4l2_err(&dev
->v4l2_dev
, "CODA_COMMAND_SEQ_INIT timeout\n");
1779 coda_write(dev
, 0, CODA_REG_BIT_BIT_STREAM_PARAM
);
1783 /* Update kfifo out pointer from coda bitstream read pointer */
1784 coda_kfifo_sync_from_device(ctx
);
1786 coda_write(dev
, 0, CODA_REG_BIT_BIT_STREAM_PARAM
);
1788 if (coda_read(dev
, CODA_RET_DEC_SEQ_SUCCESS
) == 0) {
1789 v4l2_err(&dev
->v4l2_dev
,
1790 "CODA_COMMAND_SEQ_INIT failed, error code = %d\n",
1791 coda_read(dev
, CODA_RET_DEC_SEQ_ERR_REASON
));
1795 val
= coda_read(dev
, CODA_RET_DEC_SEQ_SRC_SIZE
);
1796 if (dev
->devtype
->product
== CODA_DX6
) {
1797 width
= (val
>> CODADX6_PICWIDTH_OFFSET
) & CODADX6_PICWIDTH_MASK
;
1798 height
= val
& CODADX6_PICHEIGHT_MASK
;
1800 width
= (val
>> CODA7_PICWIDTH_OFFSET
) & CODA7_PICWIDTH_MASK
;
1801 height
= val
& CODA7_PICHEIGHT_MASK
;
1804 if (width
> q_data_dst
->width
|| height
> q_data_dst
->height
) {
1805 v4l2_err(&dev
->v4l2_dev
, "stream is %dx%d, not %dx%d\n",
1806 width
, height
, q_data_dst
->width
, q_data_dst
->height
);
1810 width
= round_up(width
, 16);
1811 height
= round_up(height
, 16);
1813 v4l2_dbg(1, coda_debug
, &dev
->v4l2_dev
, "%s instance %d now: %dx%d\n",
1814 __func__
, ctx
->idx
, width
, height
);
1816 ctx
->num_internal_frames
= coda_read(dev
, CODA_RET_DEC_SEQ_FRAME_NEED
) + 1;
1817 if (ctx
->num_internal_frames
> CODA_MAX_FRAMEBUFFERS
) {
1818 v4l2_err(&dev
->v4l2_dev
,
1819 "not enough framebuffers to decode (%d < %d)\n",
1820 CODA_MAX_FRAMEBUFFERS
, ctx
->num_internal_frames
);
1824 ret
= coda_alloc_framebuffers(ctx
, q_data_dst
, src_fourcc
);
1828 /* Tell the decoder how many frame buffers we allocated. */
1829 coda_write(dev
, ctx
->num_internal_frames
, CODA_CMD_SET_FRAME_BUF_NUM
);
1830 coda_write(dev
, width
, CODA_CMD_SET_FRAME_BUF_STRIDE
);
1832 if (dev
->devtype
->product
!= CODA_DX6
) {
1833 /* Set secondary AXI IRAM */
1834 coda_setup_iram(ctx
);
1836 coda_write(dev
, ctx
->iram_info
.buf_bit_use
,
1837 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR
);
1838 coda_write(dev
, ctx
->iram_info
.buf_ip_ac_dc_use
,
1839 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR
);
1840 coda_write(dev
, ctx
->iram_info
.buf_dbk_y_use
,
1841 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR
);
1842 coda_write(dev
, ctx
->iram_info
.buf_dbk_c_use
,
1843 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR
);
1844 coda_write(dev
, ctx
->iram_info
.buf_ovl_use
,
1845 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR
);
1848 if (src_fourcc
== V4L2_PIX_FMT_H264
) {
1849 coda_write(dev
, ctx
->slicebuf
.paddr
,
1850 CODA_CMD_SET_FRAME_SLICE_BB_START
);
1851 coda_write(dev
, ctx
->slicebuf
.size
/ 1024,
1852 CODA_CMD_SET_FRAME_SLICE_BB_SIZE
);
1855 if (dev
->devtype
->product
== CODA_7541
) {
1856 int max_mb_x
= 1920 / 16;
1857 int max_mb_y
= 1088 / 16;
1858 int max_mb_num
= max_mb_x
* max_mb_y
;
1859 coda_write(dev
, max_mb_num
<< 16 | max_mb_x
<< 8 | max_mb_y
,
1860 CODA7_CMD_SET_FRAME_MAX_DEC_SIZE
);
1863 if (coda_command_sync(ctx
, CODA_COMMAND_SET_FRAME_BUF
)) {
1864 v4l2_err(&ctx
->dev
->v4l2_dev
,
1865 "CODA_COMMAND_SET_FRAME_BUF timeout\n");
1872 static int coda_encode_header(struct coda_ctx
*ctx
, struct vb2_buffer
*buf
,
1873 int header_code
, u8
*header
, int *size
)
1875 struct coda_dev
*dev
= ctx
->dev
;
1878 coda_write(dev
, vb2_dma_contig_plane_dma_addr(buf
, 0),
1879 CODA_CMD_ENC_HEADER_BB_START
);
1880 coda_write(dev
, vb2_plane_size(buf
, 0), CODA_CMD_ENC_HEADER_BB_SIZE
);
1881 coda_write(dev
, header_code
, CODA_CMD_ENC_HEADER_CODE
);
1882 ret
= coda_command_sync(ctx
, CODA_COMMAND_ENCODE_HEADER
);
1884 v4l2_err(&dev
->v4l2_dev
, "CODA_COMMAND_ENCODE_HEADER timeout\n");
1887 *size
= coda_read(dev
, CODA_REG_BIT_WR_PTR(ctx
->reg_idx
)) -
1888 coda_read(dev
, CODA_CMD_ENC_HEADER_BB_START
);
1889 memcpy(header
, vb2_plane_vaddr(buf
, 0), *size
);
1894 static int coda_start_streaming(struct vb2_queue
*q
, unsigned int count
)
1896 struct coda_ctx
*ctx
= vb2_get_drv_priv(q
);
1897 struct v4l2_device
*v4l2_dev
= &ctx
->dev
->v4l2_dev
;
1898 u32 bitstream_buf
, bitstream_size
;
1899 struct coda_dev
*dev
= ctx
->dev
;
1900 struct coda_q_data
*q_data_src
, *q_data_dst
;
1901 struct vb2_buffer
*buf
;
1906 q_data_src
= get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_OUTPUT
);
1907 if (q
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
) {
1908 if (q_data_src
->fourcc
== V4L2_PIX_FMT_H264
) {
1909 if (coda_get_bitstream_payload(ctx
) < 512)
1916 ctx
->streamon_out
= 1;
1918 if (coda_format_is_yuv(q_data_src
->fourcc
))
1919 ctx
->inst_type
= CODA_INST_ENCODER
;
1921 ctx
->inst_type
= CODA_INST_DECODER
;
1926 ctx
->streamon_cap
= 1;
1929 /* Don't start the coda unless both queues are on */
1930 if (!(ctx
->streamon_out
& ctx
->streamon_cap
))
1933 /* Allow decoder device_run with no new buffers queued */
1934 if (ctx
->inst_type
== CODA_INST_DECODER
)
1935 v4l2_m2m_set_src_buffered(ctx
->m2m_ctx
, true);
1937 ctx
->gopcounter
= ctx
->params
.gop_size
- 1;
1938 buf
= v4l2_m2m_next_dst_buf(ctx
->m2m_ctx
);
1939 bitstream_buf
= vb2_dma_contig_plane_dma_addr(buf
, 0);
1940 q_data_dst
= get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_CAPTURE
);
1941 bitstream_size
= q_data_dst
->sizeimage
;
1942 dst_fourcc
= q_data_dst
->fourcc
;
1944 ctx
->codec
= coda_find_codec(ctx
->dev
, q_data_src
->fourcc
,
1945 q_data_dst
->fourcc
);
1947 v4l2_err(v4l2_dev
, "couldn't tell instance type.\n");
1951 /* Allocate per-instance buffers */
1952 ret
= coda_alloc_context_buffers(ctx
, q_data_src
);
1956 if (ctx
->inst_type
== CODA_INST_DECODER
) {
1957 mutex_lock(&dev
->coda_mutex
);
1958 ret
= coda_start_decoding(ctx
);
1959 mutex_unlock(&dev
->coda_mutex
);
1960 if (ret
== -EAGAIN
) {
1962 } else if (ret
< 0) {
1965 ctx
->initialized
= 1;
1970 if (!coda_is_initialized(dev
)) {
1971 v4l2_err(v4l2_dev
, "coda is not initialized.\n");
1975 mutex_lock(&dev
->coda_mutex
);
1977 coda_write(dev
, ctx
->parabuf
.paddr
, CODA_REG_BIT_PARA_BUF_ADDR
);
1978 coda_write(dev
, bitstream_buf
, CODA_REG_BIT_RD_PTR(ctx
->reg_idx
));
1979 coda_write(dev
, bitstream_buf
, CODA_REG_BIT_WR_PTR(ctx
->reg_idx
));
1980 switch (dev
->devtype
->product
) {
1982 coda_write(dev
, CODADX6_STREAM_BUF_DYNALLOC_EN
|
1983 CODADX6_STREAM_BUF_PIC_RESET
, CODA_REG_BIT_STREAM_CTRL
);
1986 coda_write(dev
, CODA7_STREAM_BUF_DYNALLOC_EN
|
1987 CODA7_STREAM_BUF_PIC_RESET
, CODA_REG_BIT_STREAM_CTRL
);
1990 if (dev
->devtype
->product
== CODA_DX6
) {
1991 /* Configure the coda */
1992 coda_write(dev
, dev
->iram_paddr
, CODADX6_REG_BIT_SEARCH_RAM_BASE_ADDR
);
1995 /* Could set rotation here if needed */
1996 switch (dev
->devtype
->product
) {
1998 value
= (q_data_src
->width
& CODADX6_PICWIDTH_MASK
) << CODADX6_PICWIDTH_OFFSET
;
1999 value
|= (q_data_src
->height
& CODADX6_PICHEIGHT_MASK
) << CODA_PICHEIGHT_OFFSET
;
2002 value
= (q_data_src
->width
& CODA7_PICWIDTH_MASK
) << CODA7_PICWIDTH_OFFSET
;
2003 value
|= (q_data_src
->height
& CODA7_PICHEIGHT_MASK
) << CODA_PICHEIGHT_OFFSET
;
2005 coda_write(dev
, value
, CODA_CMD_ENC_SEQ_SRC_SIZE
);
2006 coda_write(dev
, ctx
->params
.framerate
,
2007 CODA_CMD_ENC_SEQ_SRC_F_RATE
);
2009 ctx
->params
.codec_mode
= ctx
->codec
->mode
;
2010 switch (dst_fourcc
) {
2011 case V4L2_PIX_FMT_MPEG4
:
2012 coda_write(dev
, CODA_STD_MPEG4
, CODA_CMD_ENC_SEQ_COD_STD
);
2013 coda_write(dev
, 0, CODA_CMD_ENC_SEQ_MP4_PARA
);
2015 case V4L2_PIX_FMT_H264
:
2016 coda_write(dev
, CODA_STD_H264
, CODA_CMD_ENC_SEQ_COD_STD
);
2017 coda_write(dev
, 0, CODA_CMD_ENC_SEQ_264_PARA
);
2021 "dst format (0x%08x) invalid.\n", dst_fourcc
);
2026 switch (ctx
->params
.slice_mode
) {
2027 case V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE
:
2030 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB
:
2031 value
= (ctx
->params
.slice_max_mb
& CODA_SLICING_SIZE_MASK
) << CODA_SLICING_SIZE_OFFSET
;
2032 value
|= (1 & CODA_SLICING_UNIT_MASK
) << CODA_SLICING_UNIT_OFFSET
;
2033 value
|= 1 & CODA_SLICING_MODE_MASK
;
2035 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES
:
2036 value
= (ctx
->params
.slice_max_bits
& CODA_SLICING_SIZE_MASK
) << CODA_SLICING_SIZE_OFFSET
;
2037 value
|= (0 & CODA_SLICING_UNIT_MASK
) << CODA_SLICING_UNIT_OFFSET
;
2038 value
|= 1 & CODA_SLICING_MODE_MASK
;
2041 coda_write(dev
, value
, CODA_CMD_ENC_SEQ_SLICE_MODE
);
2042 value
= ctx
->params
.gop_size
& CODA_GOP_SIZE_MASK
;
2043 coda_write(dev
, value
, CODA_CMD_ENC_SEQ_GOP_SIZE
);
2045 if (ctx
->params
.bitrate
) {
2046 /* Rate control enabled */
2047 value
= (ctx
->params
.bitrate
& CODA_RATECONTROL_BITRATE_MASK
) << CODA_RATECONTROL_BITRATE_OFFSET
;
2048 value
|= 1 & CODA_RATECONTROL_ENABLE_MASK
;
2052 coda_write(dev
, value
, CODA_CMD_ENC_SEQ_RC_PARA
);
2054 coda_write(dev
, 0, CODA_CMD_ENC_SEQ_RC_BUF_SIZE
);
2055 coda_write(dev
, 0, CODA_CMD_ENC_SEQ_INTRA_REFRESH
);
2057 coda_write(dev
, bitstream_buf
, CODA_CMD_ENC_SEQ_BB_START
);
2058 coda_write(dev
, bitstream_size
/ 1024, CODA_CMD_ENC_SEQ_BB_SIZE
);
2060 /* set default gamma */
2061 value
= (CODA_DEFAULT_GAMMA
& CODA_GAMMA_MASK
) << CODA_GAMMA_OFFSET
;
2062 coda_write(dev
, value
, CODA_CMD_ENC_SEQ_RC_GAMMA
);
2064 if (CODA_DEFAULT_GAMMA
> 0) {
2065 if (dev
->devtype
->product
== CODA_DX6
)
2066 value
= 1 << CODADX6_OPTION_GAMMA_OFFSET
;
2068 value
= 1 << CODA7_OPTION_GAMMA_OFFSET
;
2072 coda_write(dev
, value
, CODA_CMD_ENC_SEQ_OPTION
);
2074 coda_setup_iram(ctx
);
2076 if (dst_fourcc
== V4L2_PIX_FMT_H264
) {
2077 value
= (FMO_SLICE_SAVE_BUF_SIZE
<< 7);
2078 value
|= (0 & CODA_FMOPARAM_TYPE_MASK
) << CODA_FMOPARAM_TYPE_OFFSET
;
2079 value
|= 0 & CODA_FMOPARAM_SLICENUM_MASK
;
2080 if (dev
->devtype
->product
== CODA_DX6
) {
2081 coda_write(dev
, value
, CODADX6_CMD_ENC_SEQ_FMO
);
2083 coda_write(dev
, ctx
->iram_info
.search_ram_paddr
,
2084 CODA7_CMD_ENC_SEQ_SEARCH_BASE
);
2085 coda_write(dev
, ctx
->iram_info
.search_ram_size
,
2086 CODA7_CMD_ENC_SEQ_SEARCH_SIZE
);
2090 ret
= coda_command_sync(ctx
, CODA_COMMAND_SEQ_INIT
);
2092 v4l2_err(v4l2_dev
, "CODA_COMMAND_SEQ_INIT timeout\n");
2096 if (coda_read(dev
, CODA_RET_ENC_SEQ_SUCCESS
) == 0) {
2097 v4l2_err(v4l2_dev
, "CODA_COMMAND_SEQ_INIT failed\n");
2102 ctx
->num_internal_frames
= 2;
2103 ret
= coda_alloc_framebuffers(ctx
, q_data_src
, dst_fourcc
);
2105 v4l2_err(v4l2_dev
, "failed to allocate framebuffers\n");
2109 coda_write(dev
, ctx
->num_internal_frames
, CODA_CMD_SET_FRAME_BUF_NUM
);
2110 coda_write(dev
, round_up(q_data_src
->width
, 8), CODA_CMD_SET_FRAME_BUF_STRIDE
);
2111 if (dev
->devtype
->product
== CODA_7541
)
2112 coda_write(dev
, round_up(q_data_src
->width
, 8),
2113 CODA7_CMD_SET_FRAME_SOURCE_BUF_STRIDE
);
2114 if (dev
->devtype
->product
!= CODA_DX6
) {
2115 coda_write(dev
, ctx
->iram_info
.buf_bit_use
,
2116 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR
);
2117 coda_write(dev
, ctx
->iram_info
.buf_ip_ac_dc_use
,
2118 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR
);
2119 coda_write(dev
, ctx
->iram_info
.buf_dbk_y_use
,
2120 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR
);
2121 coda_write(dev
, ctx
->iram_info
.buf_dbk_c_use
,
2122 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR
);
2123 coda_write(dev
, ctx
->iram_info
.buf_ovl_use
,
2124 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR
);
2126 ret
= coda_command_sync(ctx
, CODA_COMMAND_SET_FRAME_BUF
);
2128 v4l2_err(v4l2_dev
, "CODA_COMMAND_SET_FRAME_BUF timeout\n");
2132 /* Save stream headers */
2133 buf
= v4l2_m2m_next_dst_buf(ctx
->m2m_ctx
);
2134 switch (dst_fourcc
) {
2135 case V4L2_PIX_FMT_H264
:
2137 * Get SPS in the first frame and copy it to an
2138 * intermediate buffer.
2140 ret
= coda_encode_header(ctx
, buf
, CODA_HEADER_H264_SPS
,
2141 &ctx
->vpu_header
[0][0],
2142 &ctx
->vpu_header_size
[0]);
2147 * Get PPS in the first frame and copy it to an
2148 * intermediate buffer.
2150 ret
= coda_encode_header(ctx
, buf
, CODA_HEADER_H264_PPS
,
2151 &ctx
->vpu_header
[1][0],
2152 &ctx
->vpu_header_size
[1]);
2157 * Length of H.264 headers is variable and thus it might not be
2158 * aligned for the coda to append the encoded frame. In that is
2159 * the case a filler NAL must be added to header 2.
2161 ctx
->vpu_header_size
[2] = coda_h264_padding(
2162 (ctx
->vpu_header_size
[0] +
2163 ctx
->vpu_header_size
[1]),
2164 ctx
->vpu_header
[2]);
2166 case V4L2_PIX_FMT_MPEG4
:
2168 * Get VOS in the first frame and copy it to an
2169 * intermediate buffer
2171 ret
= coda_encode_header(ctx
, buf
, CODA_HEADER_MP4V_VOS
,
2172 &ctx
->vpu_header
[0][0],
2173 &ctx
->vpu_header_size
[0]);
2177 ret
= coda_encode_header(ctx
, buf
, CODA_HEADER_MP4V_VIS
,
2178 &ctx
->vpu_header
[1][0],
2179 &ctx
->vpu_header_size
[1]);
2183 ret
= coda_encode_header(ctx
, buf
, CODA_HEADER_MP4V_VOL
,
2184 &ctx
->vpu_header
[2][0],
2185 &ctx
->vpu_header_size
[2]);
2190 /* No more formats need to save headers at the moment */
2195 mutex_unlock(&dev
->coda_mutex
);
2199 static int coda_stop_streaming(struct vb2_queue
*q
)
2201 struct coda_ctx
*ctx
= vb2_get_drv_priv(q
);
2202 struct coda_dev
*dev
= ctx
->dev
;
2204 if (q
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
) {
2205 v4l2_dbg(1, coda_debug
, &dev
->v4l2_dev
,
2206 "%s: output\n", __func__
);
2207 ctx
->streamon_out
= 0;
2209 ctx
->bit_stream_param
|= CODA_BIT_STREAM_END_FLAG
;
2213 v4l2_dbg(1, coda_debug
, &dev
->v4l2_dev
,
2214 "%s: capture\n", __func__
);
2215 ctx
->streamon_cap
= 0;
2220 if (!ctx
->streamon_out
&& !ctx
->streamon_cap
) {
2221 kfifo_init(&ctx
->bitstream_fifo
,
2222 ctx
->bitstream
.vaddr
, ctx
->bitstream
.size
);
2223 ctx
->runcounter
= 0;
2229 static struct vb2_ops coda_qops
= {
2230 .queue_setup
= coda_queue_setup
,
2231 .buf_prepare
= coda_buf_prepare
,
2232 .buf_queue
= coda_buf_queue
,
2233 .wait_prepare
= coda_wait_prepare
,
2234 .wait_finish
= coda_wait_finish
,
2235 .start_streaming
= coda_start_streaming
,
2236 .stop_streaming
= coda_stop_streaming
,
2239 static int coda_s_ctrl(struct v4l2_ctrl
*ctrl
)
2241 struct coda_ctx
*ctx
=
2242 container_of(ctrl
->handler
, struct coda_ctx
, ctrls
);
2244 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
2245 "s_ctrl: id = %d, val = %d\n", ctrl
->id
, ctrl
->val
);
2248 case V4L2_CID_HFLIP
:
2250 ctx
->params
.rot_mode
|= CODA_MIR_HOR
;
2252 ctx
->params
.rot_mode
&= ~CODA_MIR_HOR
;
2254 case V4L2_CID_VFLIP
:
2256 ctx
->params
.rot_mode
|= CODA_MIR_VER
;
2258 ctx
->params
.rot_mode
&= ~CODA_MIR_VER
;
2260 case V4L2_CID_MPEG_VIDEO_BITRATE
:
2261 ctx
->params
.bitrate
= ctrl
->val
/ 1000;
2263 case V4L2_CID_MPEG_VIDEO_GOP_SIZE
:
2264 ctx
->params
.gop_size
= ctrl
->val
;
2266 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP
:
2267 ctx
->params
.h264_intra_qp
= ctrl
->val
;
2269 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP
:
2270 ctx
->params
.h264_inter_qp
= ctrl
->val
;
2272 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP
:
2273 ctx
->params
.mpeg4_intra_qp
= ctrl
->val
;
2275 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP
:
2276 ctx
->params
.mpeg4_inter_qp
= ctrl
->val
;
2278 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE
:
2279 ctx
->params
.slice_mode
= ctrl
->val
;
2281 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB
:
2282 ctx
->params
.slice_max_mb
= ctrl
->val
;
2284 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES
:
2285 ctx
->params
.slice_max_bits
= ctrl
->val
* 8;
2287 case V4L2_CID_MPEG_VIDEO_HEADER_MODE
:
2290 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
2291 "Invalid control, id=%d, val=%d\n",
2292 ctrl
->id
, ctrl
->val
);
2299 static struct v4l2_ctrl_ops coda_ctrl_ops
= {
2300 .s_ctrl
= coda_s_ctrl
,
2303 static int coda_ctrls_setup(struct coda_ctx
*ctx
)
2305 v4l2_ctrl_handler_init(&ctx
->ctrls
, 9);
2307 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
2308 V4L2_CID_HFLIP
, 0, 1, 1, 0);
2309 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
2310 V4L2_CID_VFLIP
, 0, 1, 1, 0);
2311 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
2312 V4L2_CID_MPEG_VIDEO_BITRATE
, 0, 32767000, 1, 0);
2313 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
2314 V4L2_CID_MPEG_VIDEO_GOP_SIZE
, 1, 60, 1, 16);
2315 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
2316 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP
, 1, 51, 1, 25);
2317 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
2318 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP
, 1, 51, 1, 25);
2319 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
2320 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP
, 1, 31, 1, 2);
2321 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
2322 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP
, 1, 31, 1, 2);
2323 v4l2_ctrl_new_std_menu(&ctx
->ctrls
, &coda_ctrl_ops
,
2324 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE
,
2325 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES
, 0x0,
2326 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE
);
2327 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
2328 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB
, 1, 0x3fffffff, 1, 1);
2329 v4l2_ctrl_new_std(&ctx
->ctrls
, &coda_ctrl_ops
,
2330 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES
, 1, 0x3fffffff, 1, 500);
2331 v4l2_ctrl_new_std_menu(&ctx
->ctrls
, &coda_ctrl_ops
,
2332 V4L2_CID_MPEG_VIDEO_HEADER_MODE
,
2333 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME
,
2334 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE
),
2335 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME
);
2337 if (ctx
->ctrls
.error
) {
2338 v4l2_err(&ctx
->dev
->v4l2_dev
, "control initialization error (%d)",
2343 return v4l2_ctrl_handler_setup(&ctx
->ctrls
);
2346 static int coda_queue_init(void *priv
, struct vb2_queue
*src_vq
,
2347 struct vb2_queue
*dst_vq
)
2349 struct coda_ctx
*ctx
= priv
;
2352 src_vq
->type
= V4L2_BUF_TYPE_VIDEO_OUTPUT
;
2353 src_vq
->io_modes
= VB2_DMABUF
| VB2_MMAP
| VB2_USERPTR
;
2354 src_vq
->drv_priv
= ctx
;
2355 src_vq
->buf_struct_size
= sizeof(struct v4l2_m2m_buffer
);
2356 src_vq
->ops
= &coda_qops
;
2357 src_vq
->mem_ops
= &vb2_dma_contig_memops
;
2358 src_vq
->timestamp_type
= V4L2_BUF_FLAG_TIMESTAMP_COPY
;
2360 ret
= vb2_queue_init(src_vq
);
2364 dst_vq
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
2365 dst_vq
->io_modes
= VB2_DMABUF
| VB2_MMAP
| VB2_USERPTR
;
2366 dst_vq
->drv_priv
= ctx
;
2367 dst_vq
->buf_struct_size
= sizeof(struct v4l2_m2m_buffer
);
2368 dst_vq
->ops
= &coda_qops
;
2369 dst_vq
->mem_ops
= &vb2_dma_contig_memops
;
2370 dst_vq
->timestamp_type
= V4L2_BUF_FLAG_TIMESTAMP_COPY
;
2372 return vb2_queue_init(dst_vq
);
2375 static int coda_next_free_instance(struct coda_dev
*dev
)
2377 int idx
= ffz(dev
->instance_mask
);
2380 (dev
->devtype
->product
== CODA_DX6
&& idx
> CODADX6_MAX_INSTANCES
))
2386 static int coda_open(struct file
*file
)
2388 struct coda_dev
*dev
= video_drvdata(file
);
2389 struct coda_ctx
*ctx
= NULL
;
2393 ctx
= kzalloc(sizeof *ctx
, GFP_KERNEL
);
2397 idx
= coda_next_free_instance(dev
);
2402 set_bit(idx
, &dev
->instance_mask
);
2404 INIT_WORK(&ctx
->skip_run
, coda_skip_run
);
2405 v4l2_fh_init(&ctx
->fh
, video_devdata(file
));
2406 file
->private_data
= &ctx
->fh
;
2407 v4l2_fh_add(&ctx
->fh
);
2410 switch (dev
->devtype
->product
) {
2418 ret
= clk_prepare_enable(dev
->clk_per
);
2422 ret
= clk_prepare_enable(dev
->clk_ahb
);
2426 set_default_params(ctx
);
2427 ctx
->m2m_ctx
= v4l2_m2m_ctx_init(dev
->m2m_dev
, ctx
,
2429 if (IS_ERR(ctx
->m2m_ctx
)) {
2430 ret
= PTR_ERR(ctx
->m2m_ctx
);
2432 v4l2_err(&dev
->v4l2_dev
, "%s return error (%d)\n",
2436 ret
= coda_ctrls_setup(ctx
);
2438 v4l2_err(&dev
->v4l2_dev
, "failed to setup coda controls\n");
2439 goto err_ctrls_setup
;
2442 ctx
->fh
.ctrl_handler
= &ctx
->ctrls
;
2444 ret
= coda_alloc_context_buf(ctx
, &ctx
->parabuf
, CODA_PARA_BUF_SIZE
);
2446 v4l2_err(&dev
->v4l2_dev
, "failed to allocate parabuf");
2450 ctx
->bitstream
.size
= CODA_MAX_FRAME_SIZE
;
2451 ctx
->bitstream
.vaddr
= dma_alloc_writecombine(&dev
->plat_dev
->dev
,
2452 ctx
->bitstream
.size
, &ctx
->bitstream
.paddr
, GFP_KERNEL
);
2453 if (!ctx
->bitstream
.vaddr
) {
2454 v4l2_err(&dev
->v4l2_dev
, "failed to allocate bitstream ringbuffer");
2456 goto err_dma_writecombine
;
2458 kfifo_init(&ctx
->bitstream_fifo
,
2459 ctx
->bitstream
.vaddr
, ctx
->bitstream
.size
);
2460 mutex_init(&ctx
->bitstream_mutex
);
2461 mutex_init(&ctx
->buffer_mutex
);
2464 list_add(&ctx
->list
, &dev
->instances
);
2467 v4l2_dbg(1, coda_debug
, &dev
->v4l2_dev
, "Created instance %d (%p)\n",
2472 err_dma_writecombine
:
2473 coda_free_context_buffers(ctx
);
2474 if (ctx
->dev
->devtype
->product
== CODA_DX6
)
2475 coda_free_aux_buf(dev
, &ctx
->workbuf
);
2476 coda_free_aux_buf(dev
, &ctx
->parabuf
);
2478 v4l2_ctrl_handler_free(&ctx
->ctrls
);
2480 v4l2_m2m_ctx_release(ctx
->m2m_ctx
);
2482 clk_disable_unprepare(dev
->clk_ahb
);
2484 clk_disable_unprepare(dev
->clk_per
);
2486 v4l2_fh_del(&ctx
->fh
);
2487 v4l2_fh_exit(&ctx
->fh
);
2488 clear_bit(ctx
->idx
, &dev
->instance_mask
);
2494 static int coda_release(struct file
*file
)
2496 struct coda_dev
*dev
= video_drvdata(file
);
2497 struct coda_ctx
*ctx
= fh_to_ctx(file
->private_data
);
2499 v4l2_dbg(1, coda_debug
, &dev
->v4l2_dev
, "Releasing instance %p\n",
2502 /* If this instance is running, call .job_abort and wait for it to end */
2503 v4l2_m2m_ctx_release(ctx
->m2m_ctx
);
2505 /* In case the instance was not running, we still need to call SEQ_END */
2506 mutex_lock(&dev
->coda_mutex
);
2507 v4l2_dbg(1, coda_debug
, &dev
->v4l2_dev
,
2508 "%s: sent command 'SEQ_END' to coda\n", __func__
);
2509 if (coda_command_sync(ctx
, CODA_COMMAND_SEQ_END
)) {
2510 v4l2_err(&dev
->v4l2_dev
,
2511 "CODA_COMMAND_SEQ_END failed\n");
2512 mutex_unlock(&dev
->coda_mutex
);
2515 mutex_unlock(&dev
->coda_mutex
);
2517 coda_free_framebuffers(ctx
);
2520 list_del(&ctx
->list
);
2523 dma_free_writecombine(&dev
->plat_dev
->dev
, ctx
->bitstream
.size
,
2524 ctx
->bitstream
.vaddr
, ctx
->bitstream
.paddr
);
2525 coda_free_context_buffers(ctx
);
2526 if (ctx
->dev
->devtype
->product
== CODA_DX6
)
2527 coda_free_aux_buf(dev
, &ctx
->workbuf
);
2529 coda_free_aux_buf(dev
, &ctx
->parabuf
);
2530 v4l2_ctrl_handler_free(&ctx
->ctrls
);
2531 clk_disable_unprepare(dev
->clk_ahb
);
2532 clk_disable_unprepare(dev
->clk_per
);
2533 v4l2_fh_del(&ctx
->fh
);
2534 v4l2_fh_exit(&ctx
->fh
);
2535 clear_bit(ctx
->idx
, &dev
->instance_mask
);
2541 static unsigned int coda_poll(struct file
*file
,
2542 struct poll_table_struct
*wait
)
2544 struct coda_ctx
*ctx
= fh_to_ctx(file
->private_data
);
2548 ret
= v4l2_m2m_poll(file
, ctx
->m2m_ctx
, wait
);
2553 static int coda_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2555 struct coda_ctx
*ctx
= fh_to_ctx(file
->private_data
);
2557 return v4l2_m2m_mmap(file
, ctx
->m2m_ctx
, vma
);
2560 static const struct v4l2_file_operations coda_fops
= {
2561 .owner
= THIS_MODULE
,
2563 .release
= coda_release
,
2565 .unlocked_ioctl
= video_ioctl2
,
2569 static void coda_finish_decode(struct coda_ctx
*ctx
)
2571 struct coda_dev
*dev
= ctx
->dev
;
2572 struct coda_q_data
*q_data_src
;
2573 struct coda_q_data
*q_data_dst
;
2574 struct vb2_buffer
*dst_buf
;
2582 dst_buf
= v4l2_m2m_next_dst_buf(ctx
->m2m_ctx
);
2584 /* Update kfifo out pointer from coda bitstream read pointer */
2585 coda_kfifo_sync_from_device(ctx
);
2588 * in stream-end mode, the read pointer can overshoot the write pointer
2589 * by up to 512 bytes
2591 if (ctx
->bit_stream_param
& CODA_BIT_STREAM_END_FLAG
) {
2592 if (coda_get_bitstream_payload(ctx
) >= 0x100000 - 512)
2593 kfifo_init(&ctx
->bitstream_fifo
,
2594 ctx
->bitstream
.vaddr
, ctx
->bitstream
.size
);
2597 q_data_src
= get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_OUTPUT
);
2598 src_fourcc
= q_data_src
->fourcc
;
2600 val
= coda_read(dev
, CODA_RET_DEC_PIC_SUCCESS
);
2602 pr_err("DEC_PIC_SUCCESS = %d\n", val
);
2604 success
= val
& 0x1;
2606 v4l2_err(&dev
->v4l2_dev
, "decode failed\n");
2608 if (src_fourcc
== V4L2_PIX_FMT_H264
) {
2610 v4l2_err(&dev
->v4l2_dev
,
2611 "insufficient PS buffer space (%d bytes)\n",
2614 v4l2_err(&dev
->v4l2_dev
,
2615 "insufficient slice buffer space (%d bytes)\n",
2616 ctx
->slicebuf
.size
);
2619 val
= coda_read(dev
, CODA_RET_DEC_PIC_SIZE
);
2620 width
= (val
>> 16) & 0xffff;
2621 height
= val
& 0xffff;
2623 q_data_dst
= get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_CAPTURE
);
2625 val
= coda_read(dev
, CODA_RET_DEC_PIC_TYPE
);
2626 if ((val
& 0x7) == 0) {
2627 dst_buf
->v4l2_buf
.flags
|= V4L2_BUF_FLAG_KEYFRAME
;
2628 dst_buf
->v4l2_buf
.flags
&= ~V4L2_BUF_FLAG_PFRAME
;
2630 dst_buf
->v4l2_buf
.flags
|= V4L2_BUF_FLAG_PFRAME
;
2631 dst_buf
->v4l2_buf
.flags
&= ~V4L2_BUF_FLAG_KEYFRAME
;
2634 val
= coda_read(dev
, CODA_RET_DEC_PIC_ERR_MB
);
2636 v4l2_err(&dev
->v4l2_dev
,
2637 "errors in %d macroblocks\n", val
);
2639 if (dev
->devtype
->product
== CODA_7541
) {
2640 val
= coda_read(dev
, CODA_RET_DEC_PIC_OPTION
);
2642 /* not enough bitstream data */
2643 v4l2_dbg(1, coda_debug
, &dev
->v4l2_dev
,
2644 "prescan failed: %d\n", val
);
2645 ctx
->prescan_failed
= true;
2650 ctx
->frm_dis_flg
= coda_read(dev
, CODA_REG_BIT_FRM_DIS_FLG(ctx
->reg_idx
));
2653 * The previous display frame was copied out by the rotator,
2654 * now it can be overwritten again
2656 if (ctx
->display_idx
>= 0 &&
2657 ctx
->display_idx
< ctx
->num_internal_frames
) {
2658 ctx
->frm_dis_flg
&= ~(1 << ctx
->display_idx
);
2659 coda_write(dev
, ctx
->frm_dis_flg
,
2660 CODA_REG_BIT_FRM_DIS_FLG(ctx
->reg_idx
));
2664 * The index of the last decoded frame, not necessarily in
2665 * display order, and the index of the next display frame.
2666 * The latter could have been decoded in a previous run.
2668 decoded_idx
= coda_read(dev
, CODA_RET_DEC_PIC_CUR_IDX
);
2669 display_idx
= coda_read(dev
, CODA_RET_DEC_PIC_FRAME_IDX
);
2671 if (decoded_idx
== -1) {
2672 /* no frame was decoded, but we might have a display frame */
2673 if (display_idx
< 0 && ctx
->display_idx
< 0)
2674 ctx
->prescan_failed
= true;
2675 } else if (decoded_idx
== -2) {
2676 /* no frame was decoded, we still return the remaining buffers */
2677 } else if (decoded_idx
< 0 || decoded_idx
>= ctx
->num_internal_frames
) {
2678 v4l2_err(&dev
->v4l2_dev
,
2679 "decoded frame index out of range: %d\n", decoded_idx
);
2682 if (display_idx
== -1) {
2684 * no more frames to be decoded, but there could still
2685 * be rotator output to dequeue
2687 ctx
->prescan_failed
= true;
2688 } else if (display_idx
== -3) {
2689 /* possibly prescan failure */
2690 } else if (display_idx
< 0 || display_idx
>= ctx
->num_internal_frames
) {
2691 v4l2_err(&dev
->v4l2_dev
,
2692 "presentation frame index out of range: %d\n",
2696 /* If a frame was copied out, return it */
2697 if (ctx
->display_idx
>= 0 &&
2698 ctx
->display_idx
< ctx
->num_internal_frames
) {
2699 dst_buf
= v4l2_m2m_dst_buf_remove(ctx
->m2m_ctx
);
2700 dst_buf
->v4l2_buf
.sequence
= ctx
->osequence
++;
2702 vb2_set_plane_payload(dst_buf
, 0, width
* height
* 3 / 2);
2704 v4l2_m2m_buf_done(dst_buf
, success
? VB2_BUF_STATE_DONE
:
2705 VB2_BUF_STATE_ERROR
);
2707 v4l2_dbg(1, coda_debug
, &dev
->v4l2_dev
,
2708 "job finished: decoding frame (%d) (%s)\n",
2709 dst_buf
->v4l2_buf
.sequence
,
2710 (dst_buf
->v4l2_buf
.flags
& V4L2_BUF_FLAG_KEYFRAME
) ?
2711 "KEYFRAME" : "PFRAME");
2713 v4l2_dbg(1, coda_debug
, &dev
->v4l2_dev
,
2714 "job finished: no frame decoded\n");
2717 /* The rotator will copy the current display frame next time */
2718 ctx
->display_idx
= display_idx
;
2721 static void coda_finish_encode(struct coda_ctx
*ctx
)
2723 struct vb2_buffer
*src_buf
, *dst_buf
;
2724 struct coda_dev
*dev
= ctx
->dev
;
2725 u32 wr_ptr
, start_ptr
;
2727 src_buf
= v4l2_m2m_src_buf_remove(ctx
->m2m_ctx
);
2728 dst_buf
= v4l2_m2m_dst_buf_remove(ctx
->m2m_ctx
);
2730 /* Get results from the coda */
2731 coda_read(dev
, CODA_RET_ENC_PIC_TYPE
);
2732 start_ptr
= coda_read(dev
, CODA_CMD_ENC_PIC_BB_START
);
2733 wr_ptr
= coda_read(dev
, CODA_REG_BIT_WR_PTR(ctx
->reg_idx
));
2735 /* Calculate bytesused field */
2736 if (dst_buf
->v4l2_buf
.sequence
== 0) {
2737 vb2_set_plane_payload(dst_buf
, 0, wr_ptr
- start_ptr
+
2738 ctx
->vpu_header_size
[0] +
2739 ctx
->vpu_header_size
[1] +
2740 ctx
->vpu_header_size
[2]);
2742 vb2_set_plane_payload(dst_buf
, 0, wr_ptr
- start_ptr
);
2745 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
, "frame size = %u\n",
2746 wr_ptr
- start_ptr
);
2748 coda_read(dev
, CODA_RET_ENC_PIC_SLICE_NUM
);
2749 coda_read(dev
, CODA_RET_ENC_PIC_FLAG
);
2751 if (src_buf
->v4l2_buf
.flags
& V4L2_BUF_FLAG_KEYFRAME
) {
2752 dst_buf
->v4l2_buf
.flags
|= V4L2_BUF_FLAG_KEYFRAME
;
2753 dst_buf
->v4l2_buf
.flags
&= ~V4L2_BUF_FLAG_PFRAME
;
2755 dst_buf
->v4l2_buf
.flags
|= V4L2_BUF_FLAG_PFRAME
;
2756 dst_buf
->v4l2_buf
.flags
&= ~V4L2_BUF_FLAG_KEYFRAME
;
2759 dst_buf
->v4l2_buf
.timestamp
= src_buf
->v4l2_buf
.timestamp
;
2760 dst_buf
->v4l2_buf
.timecode
= src_buf
->v4l2_buf
.timecode
;
2762 v4l2_m2m_buf_done(src_buf
, VB2_BUF_STATE_DONE
);
2763 v4l2_m2m_buf_done(dst_buf
, VB2_BUF_STATE_DONE
);
2766 if (ctx
->gopcounter
< 0)
2767 ctx
->gopcounter
= ctx
->params
.gop_size
- 1;
2769 v4l2_dbg(1, coda_debug
, &dev
->v4l2_dev
,
2770 "job finished: encoding frame (%d) (%s)\n",
2771 dst_buf
->v4l2_buf
.sequence
,
2772 (dst_buf
->v4l2_buf
.flags
& V4L2_BUF_FLAG_KEYFRAME
) ?
2773 "KEYFRAME" : "PFRAME");
2776 static irqreturn_t
coda_irq_handler(int irq
, void *data
)
2778 struct coda_dev
*dev
= data
;
2779 struct coda_ctx
*ctx
;
2781 cancel_delayed_work(&dev
->timeout
);
2783 /* read status register to attend the IRQ */
2784 coda_read(dev
, CODA_REG_BIT_INT_STATUS
);
2785 coda_write(dev
, CODA_REG_BIT_INT_CLEAR_SET
,
2786 CODA_REG_BIT_INT_CLEAR
);
2788 ctx
= v4l2_m2m_get_curr_priv(dev
->m2m_dev
);
2790 v4l2_err(&dev
->v4l2_dev
, "Instance released before the end of transaction\n");
2791 mutex_unlock(&dev
->coda_mutex
);
2795 if (ctx
->aborting
) {
2796 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
2797 "task has been aborted\n");
2801 if (coda_isbusy(ctx
->dev
)) {
2802 v4l2_dbg(1, coda_debug
, &ctx
->dev
->v4l2_dev
,
2803 "coda is still busy!!!!\n");
2807 if (ctx
->inst_type
== CODA_INST_DECODER
)
2808 coda_finish_decode(ctx
);
2810 coda_finish_encode(ctx
);
2813 if (ctx
->aborting
|| (!ctx
->streamon_cap
&& !ctx
->streamon_out
)) {
2814 v4l2_dbg(1, coda_debug
, &dev
->v4l2_dev
,
2815 "%s: sent command 'SEQ_END' to coda\n", __func__
);
2816 if (coda_command_sync(ctx
, CODA_COMMAND_SEQ_END
)) {
2817 v4l2_err(&dev
->v4l2_dev
,
2818 "CODA_COMMAND_SEQ_END failed\n");
2821 kfifo_init(&ctx
->bitstream_fifo
,
2822 ctx
->bitstream
.vaddr
, ctx
->bitstream
.size
);
2824 coda_free_framebuffers(ctx
);
2825 coda_free_context_buffers(ctx
);
2828 mutex_unlock(&dev
->coda_mutex
);
2829 mutex_unlock(&ctx
->buffer_mutex
);
2831 v4l2_m2m_job_finish(ctx
->dev
->m2m_dev
, ctx
->m2m_ctx
);
2836 static void coda_timeout(struct work_struct
*work
)
2838 struct coda_ctx
*ctx
;
2839 struct coda_dev
*dev
= container_of(to_delayed_work(work
),
2840 struct coda_dev
, timeout
);
2842 dev_err(&dev
->plat_dev
->dev
, "CODA PIC_RUN timeout, stopping all streams\n");
2844 mutex_lock(&dev
->dev_mutex
);
2845 list_for_each_entry(ctx
, &dev
->instances
, list
) {
2846 if (mutex_is_locked(&ctx
->buffer_mutex
))
2847 mutex_unlock(&ctx
->buffer_mutex
);
2848 v4l2_m2m_streamoff(NULL
, ctx
->m2m_ctx
, V4L2_BUF_TYPE_VIDEO_OUTPUT
);
2849 v4l2_m2m_streamoff(NULL
, ctx
->m2m_ctx
, V4L2_BUF_TYPE_VIDEO_CAPTURE
);
2851 mutex_unlock(&dev
->dev_mutex
);
2853 mutex_unlock(&dev
->coda_mutex
);
2854 ctx
= v4l2_m2m_get_curr_priv(dev
->m2m_dev
);
2855 v4l2_m2m_job_finish(ctx
->dev
->m2m_dev
, ctx
->m2m_ctx
);
2858 static u32 coda_supported_firmwares
[] = {
2859 CODA_FIRMWARE_VERNUM(CODA_DX6
, 2, 2, 5),
2860 CODA_FIRMWARE_VERNUM(CODA_7541
, 1, 4, 50),
2863 static bool coda_firmware_supported(u32 vernum
)
2867 for (i
= 0; i
< ARRAY_SIZE(coda_supported_firmwares
); i
++)
2868 if (vernum
== coda_supported_firmwares
[i
])
2873 static char *coda_product_name(int product
)
2883 snprintf(buf
, sizeof(buf
), "(0x%04x)", product
);
2888 static int coda_hw_init(struct coda_dev
*dev
)
2890 u16 product
, major
, minor
, release
;
2895 ret
= clk_prepare_enable(dev
->clk_per
);
2899 ret
= clk_prepare_enable(dev
->clk_ahb
);
2904 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
2905 * The 16-bit chars in the code buffer are in memory access
2906 * order, re-sort them to CODA order for register download.
2907 * Data in this SRAM survives a reboot.
2909 p
= (u16
*)dev
->codebuf
.vaddr
;
2910 if (dev
->devtype
->product
== CODA_DX6
) {
2911 for (i
= 0; i
< (CODA_ISRAM_SIZE
/ 2); i
++) {
2912 data
= CODA_DOWN_ADDRESS_SET(i
) |
2913 CODA_DOWN_DATA_SET(p
[i
^ 1]);
2914 coda_write(dev
, data
, CODA_REG_BIT_CODE_DOWN
);
2917 for (i
= 0; i
< (CODA_ISRAM_SIZE
/ 2); i
++) {
2918 data
= CODA_DOWN_ADDRESS_SET(i
) |
2919 CODA_DOWN_DATA_SET(p
[round_down(i
, 4) +
2921 coda_write(dev
, data
, CODA_REG_BIT_CODE_DOWN
);
2925 /* Clear registers */
2926 for (i
= 0; i
< 64; i
++)
2927 coda_write(dev
, 0, CODA_REG_BIT_CODE_BUF_ADDR
+ i
* 4);
2929 /* Tell the BIT where to find everything it needs */
2930 if (dev
->devtype
->product
== CODA_7541
) {
2931 coda_write(dev
, dev
->tempbuf
.paddr
,
2932 CODA_REG_BIT_TEMP_BUF_ADDR
);
2933 coda_write(dev
, 0, CODA_REG_BIT_BIT_STREAM_PARAM
);
2935 coda_write(dev
, dev
->workbuf
.paddr
,
2936 CODA_REG_BIT_WORK_BUF_ADDR
);
2938 coda_write(dev
, dev
->codebuf
.paddr
,
2939 CODA_REG_BIT_CODE_BUF_ADDR
);
2940 coda_write(dev
, 0, CODA_REG_BIT_CODE_RUN
);
2942 /* Set default values */
2943 switch (dev
->devtype
->product
) {
2945 coda_write(dev
, CODADX6_STREAM_BUF_PIC_FLUSH
, CODA_REG_BIT_STREAM_CTRL
);
2948 coda_write(dev
, CODA7_STREAM_BUF_PIC_FLUSH
, CODA_REG_BIT_STREAM_CTRL
);
2950 coda_write(dev
, 0, CODA_REG_BIT_FRAME_MEM_CTRL
);
2952 if (dev
->devtype
->product
!= CODA_DX6
)
2953 coda_write(dev
, 0, CODA7_REG_BIT_AXI_SRAM_USE
);
2955 coda_write(dev
, CODA_INT_INTERRUPT_ENABLE
,
2956 CODA_REG_BIT_INT_ENABLE
);
2958 /* Reset VPU and start processor */
2959 data
= coda_read(dev
, CODA_REG_BIT_CODE_RESET
);
2960 data
|= CODA_REG_RESET_ENABLE
;
2961 coda_write(dev
, data
, CODA_REG_BIT_CODE_RESET
);
2963 data
&= ~CODA_REG_RESET_ENABLE
;
2964 coda_write(dev
, data
, CODA_REG_BIT_CODE_RESET
);
2965 coda_write(dev
, CODA_REG_RUN_ENABLE
, CODA_REG_BIT_CODE_RUN
);
2968 coda_write(dev
, 0, CODA_CMD_FIRMWARE_VERNUM
);
2969 coda_write(dev
, CODA_REG_BIT_BUSY_FLAG
, CODA_REG_BIT_BUSY
);
2970 coda_write(dev
, 0, CODA_REG_BIT_RUN_INDEX
);
2971 coda_write(dev
, 0, CODA_REG_BIT_RUN_COD_STD
);
2972 coda_write(dev
, CODA_COMMAND_FIRMWARE_GET
, CODA_REG_BIT_RUN_COMMAND
);
2973 if (coda_wait_timeout(dev
)) {
2974 clk_disable_unprepare(dev
->clk_per
);
2975 clk_disable_unprepare(dev
->clk_ahb
);
2976 v4l2_err(&dev
->v4l2_dev
, "firmware get command error\n");
2980 /* Check we are compatible with the loaded firmware */
2981 data
= coda_read(dev
, CODA_CMD_FIRMWARE_VERNUM
);
2982 product
= CODA_FIRMWARE_PRODUCT(data
);
2983 major
= CODA_FIRMWARE_MAJOR(data
);
2984 minor
= CODA_FIRMWARE_MINOR(data
);
2985 release
= CODA_FIRMWARE_RELEASE(data
);
2987 clk_disable_unprepare(dev
->clk_per
);
2988 clk_disable_unprepare(dev
->clk_ahb
);
2990 if (product
!= dev
->devtype
->product
) {
2991 v4l2_err(&dev
->v4l2_dev
, "Wrong firmware. Hw: %s, Fw: %s,"
2992 " Version: %u.%u.%u\n",
2993 coda_product_name(dev
->devtype
->product
),
2994 coda_product_name(product
), major
, minor
, release
);
2998 v4l2_info(&dev
->v4l2_dev
, "Initialized %s.\n",
2999 coda_product_name(product
));
3001 if (coda_firmware_supported(data
)) {
3002 v4l2_info(&dev
->v4l2_dev
, "Firmware version: %u.%u.%u\n",
3003 major
, minor
, release
);
3005 v4l2_warn(&dev
->v4l2_dev
, "Unsupported firmware version: "
3006 "%u.%u.%u\n", major
, minor
, release
);
3012 clk_disable_unprepare(dev
->clk_per
);
3016 static void coda_fw_callback(const struct firmware
*fw
, void *context
)
3018 struct coda_dev
*dev
= context
;
3019 struct platform_device
*pdev
= dev
->plat_dev
;
3023 v4l2_err(&dev
->v4l2_dev
, "firmware request failed\n");
3027 /* allocate auxiliary per-device code buffer for the BIT processor */
3028 ret
= coda_alloc_aux_buf(dev
, &dev
->codebuf
, fw
->size
);
3030 dev_err(&pdev
->dev
, "failed to allocate code buffer\n");
3034 /* Copy the whole firmware image to the code buffer */
3035 memcpy(dev
->codebuf
.vaddr
, fw
->data
, fw
->size
);
3036 release_firmware(fw
);
3038 ret
= coda_hw_init(dev
);
3040 v4l2_err(&dev
->v4l2_dev
, "HW initialization failed\n");
3044 dev
->vfd
.fops
= &coda_fops
,
3045 dev
->vfd
.ioctl_ops
= &coda_ioctl_ops
;
3046 dev
->vfd
.release
= video_device_release_empty
,
3047 dev
->vfd
.lock
= &dev
->dev_mutex
;
3048 dev
->vfd
.v4l2_dev
= &dev
->v4l2_dev
;
3049 dev
->vfd
.vfl_dir
= VFL_DIR_M2M
;
3050 snprintf(dev
->vfd
.name
, sizeof(dev
->vfd
.name
), "%s", CODA_NAME
);
3051 video_set_drvdata(&dev
->vfd
, dev
);
3053 dev
->alloc_ctx
= vb2_dma_contig_init_ctx(&pdev
->dev
);
3054 if (IS_ERR(dev
->alloc_ctx
)) {
3055 v4l2_err(&dev
->v4l2_dev
, "Failed to alloc vb2 context\n");
3059 dev
->m2m_dev
= v4l2_m2m_init(&coda_m2m_ops
);
3060 if (IS_ERR(dev
->m2m_dev
)) {
3061 v4l2_err(&dev
->v4l2_dev
, "Failed to init mem2mem device\n");
3065 ret
= video_register_device(&dev
->vfd
, VFL_TYPE_GRABBER
, 0);
3067 v4l2_err(&dev
->v4l2_dev
, "Failed to register video device\n");
3070 v4l2_info(&dev
->v4l2_dev
, "codec registered as /dev/video%d\n",
3076 v4l2_m2m_release(dev
->m2m_dev
);
3078 vb2_dma_contig_cleanup_ctx(dev
->alloc_ctx
);
3081 static int coda_firmware_request(struct coda_dev
*dev
)
3083 char *fw
= dev
->devtype
->firmware
;
3085 dev_dbg(&dev
->plat_dev
->dev
, "requesting firmware '%s' for %s\n", fw
,
3086 coda_product_name(dev
->devtype
->product
));
3088 return request_firmware_nowait(THIS_MODULE
, true,
3089 fw
, &dev
->plat_dev
->dev
, GFP_KERNEL
, dev
, coda_fw_callback
);
3092 enum coda_platform
{
3097 static const struct coda_devtype coda_devdata
[] = {
3099 .firmware
= "v4l-codadx6-imx27.bin",
3100 .product
= CODA_DX6
,
3101 .codecs
= codadx6_codecs
,
3102 .num_codecs
= ARRAY_SIZE(codadx6_codecs
),
3105 .firmware
= "v4l-coda7541-imx53.bin",
3106 .product
= CODA_7541
,
3107 .codecs
= coda7_codecs
,
3108 .num_codecs
= ARRAY_SIZE(coda7_codecs
),
3112 static struct platform_device_id coda_platform_ids
[] = {
3113 { .name
= "coda-imx27", .driver_data
= CODA_IMX27
},
3114 { .name
= "coda-imx53", .driver_data
= CODA_IMX53
},
3117 MODULE_DEVICE_TABLE(platform
, coda_platform_ids
);
3120 static const struct of_device_id coda_dt_ids
[] = {
3121 { .compatible
= "fsl,imx27-vpu", .data
= &coda_devdata
[CODA_IMX27
] },
3122 { .compatible
= "fsl,imx53-vpu", .data
= &coda_devdata
[CODA_IMX53
] },
3125 MODULE_DEVICE_TABLE(of
, coda_dt_ids
);
3128 static int coda_probe(struct platform_device
*pdev
)
3130 const struct of_device_id
*of_id
=
3131 of_match_device(of_match_ptr(coda_dt_ids
), &pdev
->dev
);
3132 const struct platform_device_id
*pdev_id
;
3133 struct coda_platform_data
*pdata
= pdev
->dev
.platform_data
;
3134 struct device_node
*np
= pdev
->dev
.of_node
;
3135 struct gen_pool
*pool
;
3136 struct coda_dev
*dev
;
3137 struct resource
*res
;
3140 dev
= devm_kzalloc(&pdev
->dev
, sizeof *dev
, GFP_KERNEL
);
3142 dev_err(&pdev
->dev
, "Not enough memory for %s\n",
3147 spin_lock_init(&dev
->irqlock
);
3148 INIT_LIST_HEAD(&dev
->instances
);
3149 INIT_DELAYED_WORK(&dev
->timeout
, coda_timeout
);
3151 dev
->plat_dev
= pdev
;
3152 dev
->clk_per
= devm_clk_get(&pdev
->dev
, "per");
3153 if (IS_ERR(dev
->clk_per
)) {
3154 dev_err(&pdev
->dev
, "Could not get per clock\n");
3155 return PTR_ERR(dev
->clk_per
);
3158 dev
->clk_ahb
= devm_clk_get(&pdev
->dev
, "ahb");
3159 if (IS_ERR(dev
->clk_ahb
)) {
3160 dev_err(&pdev
->dev
, "Could not get ahb clock\n");
3161 return PTR_ERR(dev
->clk_ahb
);
3164 /* Get memory for physical registers */
3165 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
3166 dev
->regs_base
= devm_ioremap_resource(&pdev
->dev
, res
);
3167 if (IS_ERR(dev
->regs_base
))
3168 return PTR_ERR(dev
->regs_base
);
3171 irq
= platform_get_irq(pdev
, 0);
3173 dev_err(&pdev
->dev
, "failed to get irq resource\n");
3177 if (devm_request_threaded_irq(&pdev
->dev
, irq
, NULL
, coda_irq_handler
,
3178 IRQF_ONESHOT
, CODA_NAME
, dev
) < 0) {
3179 dev_err(&pdev
->dev
, "failed to request irq\n");
3183 /* Get IRAM pool from device tree or platform data */
3184 pool
= of_get_named_gen_pool(np
, "iram", 0);
3186 pool
= dev_get_gen_pool(pdata
->iram_dev
);
3188 dev_err(&pdev
->dev
, "iram pool not available\n");
3191 dev
->iram_pool
= pool
;
3193 ret
= v4l2_device_register(&pdev
->dev
, &dev
->v4l2_dev
);
3197 mutex_init(&dev
->dev_mutex
);
3198 mutex_init(&dev
->coda_mutex
);
3200 pdev_id
= of_id
? of_id
->data
: platform_get_device_id(pdev
);
3203 dev
->devtype
= of_id
->data
;
3204 } else if (pdev_id
) {
3205 dev
->devtype
= &coda_devdata
[pdev_id
->driver_data
];
3207 v4l2_device_unregister(&dev
->v4l2_dev
);
3211 /* allocate auxiliary per-device buffers for the BIT processor */
3212 switch (dev
->devtype
->product
) {
3214 ret
= coda_alloc_aux_buf(dev
, &dev
->workbuf
,
3215 CODADX6_WORK_BUF_SIZE
);
3217 dev_err(&pdev
->dev
, "failed to allocate work buffer\n");
3218 v4l2_device_unregister(&dev
->v4l2_dev
);
3223 dev
->tempbuf
.size
= CODA7_TEMP_BUF_SIZE
;
3226 if (dev
->tempbuf
.size
) {
3227 ret
= coda_alloc_aux_buf(dev
, &dev
->tempbuf
,
3230 dev_err(&pdev
->dev
, "failed to allocate temp buffer\n");
3231 v4l2_device_unregister(&dev
->v4l2_dev
);
3236 switch (dev
->devtype
->product
) {
3238 dev
->iram_size
= CODADX6_IRAM_SIZE
;
3241 dev
->iram_size
= CODA7_IRAM_SIZE
;
3244 dev
->iram_vaddr
= gen_pool_alloc(dev
->iram_pool
, dev
->iram_size
);
3245 if (!dev
->iram_vaddr
) {
3246 dev_err(&pdev
->dev
, "unable to alloc iram\n");
3249 dev
->iram_paddr
= gen_pool_virt_to_phys(dev
->iram_pool
,
3252 platform_set_drvdata(pdev
, dev
);
3254 return coda_firmware_request(dev
);
3257 static int coda_remove(struct platform_device
*pdev
)
3259 struct coda_dev
*dev
= platform_get_drvdata(pdev
);
3261 video_unregister_device(&dev
->vfd
);
3263 v4l2_m2m_release(dev
->m2m_dev
);
3265 vb2_dma_contig_cleanup_ctx(dev
->alloc_ctx
);
3266 v4l2_device_unregister(&dev
->v4l2_dev
);
3267 if (dev
->iram_vaddr
)
3268 gen_pool_free(dev
->iram_pool
, dev
->iram_vaddr
, dev
->iram_size
);
3269 coda_free_aux_buf(dev
, &dev
->codebuf
);
3270 coda_free_aux_buf(dev
, &dev
->tempbuf
);
3271 coda_free_aux_buf(dev
, &dev
->workbuf
);
3275 static struct platform_driver coda_driver
= {
3276 .probe
= coda_probe
,
3277 .remove
= coda_remove
,
3280 .owner
= THIS_MODULE
,
3281 .of_match_table
= of_match_ptr(coda_dt_ids
),
3283 .id_table
= coda_platform_ids
,
3286 module_platform_driver(coda_driver
);
3288 MODULE_LICENSE("GPL");
3289 MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
3290 MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");