2 * TI VPE mem2mem driver, based on the virtual v4l2-mem2mem example driver
4 * Copyright (c) 2013 Texas Instruments Inc.
5 * David Griego, <dagriego@biglakesoftware.com>
6 * Dale Farnsworth, <dale@farnsworth.org>
7 * Archit Taneja, <archit@ti.com>
9 * Copyright (c) 2009-2010 Samsung Electronics Co., Ltd.
10 * Pawel Osciak, <pawel@osciak.com>
11 * Marek Szyprowski, <m.szyprowski@samsung.com>
13 * Based on the virtual v4l2-mem2mem example device
15 * This program is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU General Public License version 2 as published by
17 * the Free Software Foundation
20 #include <linux/delay.h>
21 #include <linux/dma-mapping.h>
22 #include <linux/err.h>
24 #include <linux/interrupt.h>
26 #include <linux/ioctl.h>
27 #include <linux/module.h>
29 #include <linux/platform_device.h>
30 #include <linux/pm_runtime.h>
31 #include <linux/sched.h>
32 #include <linux/slab.h>
33 #include <linux/videodev2.h>
34 #include <linux/log2.h>
35 #include <linux/sizes.h>
37 #include <media/v4l2-common.h>
38 #include <media/v4l2-ctrls.h>
39 #include <media/v4l2-device.h>
40 #include <media/v4l2-event.h>
41 #include <media/v4l2-ioctl.h>
42 #include <media/v4l2-mem2mem.h>
43 #include <media/videobuf2-v4l2.h>
44 #include <media/videobuf2-dma-contig.h>
47 #include "vpdma_priv.h"
52 #define VPE_MODULE_NAME "vpe"
54 /* minimum and maximum frame sizes */
60 /* required alignments */
61 #define S_ALIGN 0 /* multiple of 1 */
62 #define H_ALIGN 1 /* multiple of 2 */
64 /* flags that indicate a format can be used for capture/output */
65 #define VPE_FMT_TYPE_CAPTURE (1 << 0)
66 #define VPE_FMT_TYPE_OUTPUT (1 << 1)
68 /* used as plane indices */
69 #define VPE_MAX_PLANES 2
73 /* per m2m context info */
74 #define VPE_MAX_SRC_BUFS 3 /* need 3 src fields to de-interlace */
76 #define VPE_DEF_BUFS_PER_JOB 1 /* default one buffer per batch job */
79 * each VPE context can need up to 3 config descriptors, 7 input descriptors,
80 * 3 output descriptors, and 10 control descriptors
82 #define VPE_DESC_LIST_SIZE (10 * VPDMA_DTD_DESC_SIZE + \
83 13 * VPDMA_CFD_CTD_DESC_SIZE)
85 #define vpe_dbg(vpedev, fmt, arg...) \
86 dev_dbg((vpedev)->v4l2_dev.dev, fmt, ##arg)
87 #define vpe_err(vpedev, fmt, arg...) \
88 dev_err((vpedev)->v4l2_dev.dev, fmt, ##arg)
90 struct vpe_us_coeffs
{
91 unsigned short anchor_fid0_c0
;
92 unsigned short anchor_fid0_c1
;
93 unsigned short anchor_fid0_c2
;
94 unsigned short anchor_fid0_c3
;
95 unsigned short interp_fid0_c0
;
96 unsigned short interp_fid0_c1
;
97 unsigned short interp_fid0_c2
;
98 unsigned short interp_fid0_c3
;
99 unsigned short anchor_fid1_c0
;
100 unsigned short anchor_fid1_c1
;
101 unsigned short anchor_fid1_c2
;
102 unsigned short anchor_fid1_c3
;
103 unsigned short interp_fid1_c0
;
104 unsigned short interp_fid1_c1
;
105 unsigned short interp_fid1_c2
;
106 unsigned short interp_fid1_c3
;
110 * Default upsampler coefficients
112 static const struct vpe_us_coeffs us_coeffs
[] = {
114 /* Coefficients for progressive input */
115 0x00C8, 0x0348, 0x0018, 0x3FD8, 0x3FB8, 0x0378, 0x00E8, 0x3FE8,
116 0x00C8, 0x0348, 0x0018, 0x3FD8, 0x3FB8, 0x0378, 0x00E8, 0x3FE8,
119 /* Coefficients for Top Field Interlaced input */
120 0x0051, 0x03D5, 0x3FE3, 0x3FF7, 0x3FB5, 0x02E9, 0x018F, 0x3FD3,
121 /* Coefficients for Bottom Field Interlaced input */
122 0x016B, 0x0247, 0x00B1, 0x3F9D, 0x3FCF, 0x03DB, 0x005D, 0x3FF9,
127 * the following registers are for configuring some of the parameters of the
128 * motion and edge detection blocks inside DEI, these generally remain the same,
129 * these could be passed later via userspace if some one needs to tweak these.
131 struct vpe_dei_regs
{
132 unsigned long mdt_spacial_freq_thr_reg
; /* VPE_DEI_REG2 */
133 unsigned long edi_config_reg
; /* VPE_DEI_REG3 */
134 unsigned long edi_lut_reg0
; /* VPE_DEI_REG4 */
135 unsigned long edi_lut_reg1
; /* VPE_DEI_REG5 */
136 unsigned long edi_lut_reg2
; /* VPE_DEI_REG6 */
137 unsigned long edi_lut_reg3
; /* VPE_DEI_REG7 */
141 * default expert DEI register values, unlikely to be modified.
143 static const struct vpe_dei_regs dei_regs
= {
144 .mdt_spacial_freq_thr_reg
= 0x020C0804u
,
145 .edi_config_reg
= 0x0118100Cu
,
146 .edi_lut_reg0
= 0x08040200u
,
147 .edi_lut_reg1
= 0x1010100Cu
,
148 .edi_lut_reg2
= 0x10101010u
,
149 .edi_lut_reg3
= 0x10101010u
,
153 * The port_data structure contains per-port data.
155 struct vpe_port_data
{
156 enum vpdma_channel channel
; /* VPDMA channel */
157 u8 vb_index
; /* input frame f, f-1, f-2 index */
158 u8 vb_part
; /* plane index for co-panar formats */
162 * Define indices into the port_data tables
164 #define VPE_PORT_LUMA1_IN 0
165 #define VPE_PORT_CHROMA1_IN 1
166 #define VPE_PORT_LUMA2_IN 2
167 #define VPE_PORT_CHROMA2_IN 3
168 #define VPE_PORT_LUMA3_IN 4
169 #define VPE_PORT_CHROMA3_IN 5
170 #define VPE_PORT_MV_IN 6
171 #define VPE_PORT_MV_OUT 7
172 #define VPE_PORT_LUMA_OUT 8
173 #define VPE_PORT_CHROMA_OUT 9
174 #define VPE_PORT_RGB_OUT 10
176 static const struct vpe_port_data port_data
[11] = {
177 [VPE_PORT_LUMA1_IN
] = {
178 .channel
= VPE_CHAN_LUMA1_IN
,
182 [VPE_PORT_CHROMA1_IN
] = {
183 .channel
= VPE_CHAN_CHROMA1_IN
,
185 .vb_part
= VPE_CHROMA
,
187 [VPE_PORT_LUMA2_IN
] = {
188 .channel
= VPE_CHAN_LUMA2_IN
,
192 [VPE_PORT_CHROMA2_IN
] = {
193 .channel
= VPE_CHAN_CHROMA2_IN
,
195 .vb_part
= VPE_CHROMA
,
197 [VPE_PORT_LUMA3_IN
] = {
198 .channel
= VPE_CHAN_LUMA3_IN
,
202 [VPE_PORT_CHROMA3_IN
] = {
203 .channel
= VPE_CHAN_CHROMA3_IN
,
205 .vb_part
= VPE_CHROMA
,
208 .channel
= VPE_CHAN_MV_IN
,
210 [VPE_PORT_MV_OUT
] = {
211 .channel
= VPE_CHAN_MV_OUT
,
213 [VPE_PORT_LUMA_OUT
] = {
214 .channel
= VPE_CHAN_LUMA_OUT
,
217 [VPE_PORT_CHROMA_OUT
] = {
218 .channel
= VPE_CHAN_CHROMA_OUT
,
219 .vb_part
= VPE_CHROMA
,
221 [VPE_PORT_RGB_OUT
] = {
222 .channel
= VPE_CHAN_RGB_OUT
,
228 /* driver info for each of the supported video formats */
230 char *name
; /* human-readable name */
231 u32 fourcc
; /* standard format identifier */
232 u8 types
; /* CAPTURE and/or OUTPUT */
233 u8 coplanar
; /* set for unpacked Luma and Chroma */
234 /* vpdma format info for each plane */
235 struct vpdma_data_format
const *vpdma_fmt
[VPE_MAX_PLANES
];
238 static struct vpe_fmt vpe_formats
[] = {
240 .name
= "YUV 422 co-planar",
241 .fourcc
= V4L2_PIX_FMT_NV16
,
242 .types
= VPE_FMT_TYPE_CAPTURE
| VPE_FMT_TYPE_OUTPUT
,
244 .vpdma_fmt
= { &vpdma_yuv_fmts
[VPDMA_DATA_FMT_Y444
],
245 &vpdma_yuv_fmts
[VPDMA_DATA_FMT_C444
],
249 .name
= "YUV 420 co-planar",
250 .fourcc
= V4L2_PIX_FMT_NV12
,
251 .types
= VPE_FMT_TYPE_CAPTURE
| VPE_FMT_TYPE_OUTPUT
,
253 .vpdma_fmt
= { &vpdma_yuv_fmts
[VPDMA_DATA_FMT_Y420
],
254 &vpdma_yuv_fmts
[VPDMA_DATA_FMT_C420
],
258 .name
= "YUYV 422 packed",
259 .fourcc
= V4L2_PIX_FMT_YUYV
,
260 .types
= VPE_FMT_TYPE_CAPTURE
| VPE_FMT_TYPE_OUTPUT
,
262 .vpdma_fmt
= { &vpdma_yuv_fmts
[VPDMA_DATA_FMT_YC422
],
266 .name
= "UYVY 422 packed",
267 .fourcc
= V4L2_PIX_FMT_UYVY
,
268 .types
= VPE_FMT_TYPE_CAPTURE
| VPE_FMT_TYPE_OUTPUT
,
270 .vpdma_fmt
= { &vpdma_yuv_fmts
[VPDMA_DATA_FMT_CY422
],
274 .name
= "RGB888 packed",
275 .fourcc
= V4L2_PIX_FMT_RGB24
,
276 .types
= VPE_FMT_TYPE_CAPTURE
,
278 .vpdma_fmt
= { &vpdma_rgb_fmts
[VPDMA_DATA_FMT_RGB24
],
283 .fourcc
= V4L2_PIX_FMT_RGB32
,
284 .types
= VPE_FMT_TYPE_CAPTURE
,
286 .vpdma_fmt
= { &vpdma_rgb_fmts
[VPDMA_DATA_FMT_ARGB32
],
290 .name
= "BGR888 packed",
291 .fourcc
= V4L2_PIX_FMT_BGR24
,
292 .types
= VPE_FMT_TYPE_CAPTURE
,
294 .vpdma_fmt
= { &vpdma_rgb_fmts
[VPDMA_DATA_FMT_BGR24
],
299 .fourcc
= V4L2_PIX_FMT_BGR32
,
300 .types
= VPE_FMT_TYPE_CAPTURE
,
302 .vpdma_fmt
= { &vpdma_rgb_fmts
[VPDMA_DATA_FMT_ABGR32
],
307 .fourcc
= V4L2_PIX_FMT_RGB565
,
308 .types
= VPE_FMT_TYPE_CAPTURE
,
310 .vpdma_fmt
= { &vpdma_rgb_fmts
[VPDMA_DATA_FMT_RGB565
],
315 .fourcc
= V4L2_PIX_FMT_RGB555
,
316 .types
= VPE_FMT_TYPE_CAPTURE
,
318 .vpdma_fmt
= { &vpdma_rgb_fmts
[VPDMA_DATA_FMT_RGBA16_5551
],
324 * per-queue, driver-specific private data.
325 * there is one source queue and one destination queue for each m2m context.
328 unsigned int width
; /* frame width */
329 unsigned int height
; /* frame height */
330 unsigned int bytesperline
[VPE_MAX_PLANES
]; /* bytes per line in memory */
331 enum v4l2_colorspace colorspace
;
332 enum v4l2_field field
; /* supported field value */
334 unsigned int sizeimage
[VPE_MAX_PLANES
]; /* image size in memory */
335 struct v4l2_rect c_rect
; /* crop/compose rectangle */
336 struct vpe_fmt
*fmt
; /* format info */
339 /* vpe_q_data flag bits */
340 #define Q_DATA_FRAME_1D BIT(0)
341 #define Q_DATA_MODE_TILED BIT(1)
342 #define Q_DATA_INTERLACED_ALTERNATE BIT(2)
343 #define Q_DATA_INTERLACED_SEQ_TB BIT(3)
345 #define Q_IS_INTERLACED (Q_DATA_INTERLACED_ALTERNATE | \
346 Q_DATA_INTERLACED_SEQ_TB)
353 /* find our format description corresponding to the passed v4l2_format */
354 static struct vpe_fmt
*find_format(struct v4l2_format
*f
)
359 for (k
= 0; k
< ARRAY_SIZE(vpe_formats
); k
++) {
360 fmt
= &vpe_formats
[k
];
361 if (fmt
->fourcc
== f
->fmt
.pix
.pixelformat
)
369 * there is one vpe_dev structure in the driver, it is shared by
373 struct v4l2_device v4l2_dev
;
374 struct video_device vfd
;
375 struct v4l2_m2m_dev
*m2m_dev
;
377 atomic_t num_instances
; /* count of driver instances */
378 dma_addr_t loaded_mmrs
; /* shadow mmrs in device */
379 struct mutex dev_mutex
;
384 struct resource
*res
;
386 struct vpdma_data
*vpdma
; /* vpdma data handle */
387 struct sc_data
*sc
; /* scaler data handle */
388 struct csc_data
*csc
; /* csc data handle */
392 * There is one vpe_ctx structure for each m2m context.
397 struct v4l2_ctrl_handler hdl
;
399 unsigned int field
; /* current field */
400 unsigned int sequence
; /* current frame/field seq */
401 unsigned int aborting
; /* abort after next irq */
403 unsigned int bufs_per_job
; /* input buffers per batch */
404 unsigned int bufs_completed
; /* bufs done in this batch */
406 struct vpe_q_data q_data
[2]; /* src & dst queue data */
407 struct vb2_v4l2_buffer
*src_vbs
[VPE_MAX_SRC_BUFS
];
408 struct vb2_v4l2_buffer
*dst_vb
;
410 dma_addr_t mv_buf_dma
[2]; /* dma addrs of motion vector in/out bufs */
411 void *mv_buf
[2]; /* virtual addrs of motion vector bufs */
412 size_t mv_buf_size
; /* current motion vector buffer size */
413 struct vpdma_buf mmr_adb
; /* shadow reg addr/data block */
414 struct vpdma_buf sc_coeff_h
; /* h coeff buffer */
415 struct vpdma_buf sc_coeff_v
; /* v coeff buffer */
416 struct vpdma_desc_list desc_list
; /* DMA descriptor list */
418 bool deinterlacing
; /* using de-interlacer */
419 bool load_mmrs
; /* have new shadow reg values */
421 unsigned int src_mv_buf_selector
;
426 * M2M devices get 2 queues.
427 * Return the queue given the type.
429 static struct vpe_q_data
*get_q_data(struct vpe_ctx
*ctx
,
430 enum v4l2_buf_type type
)
433 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
:
434 case V4L2_BUF_TYPE_VIDEO_OUTPUT
:
435 return &ctx
->q_data
[Q_DATA_SRC
];
436 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
:
437 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
438 return &ctx
->q_data
[Q_DATA_DST
];
445 static u32
read_reg(struct vpe_dev
*dev
, int offset
)
447 return ioread32(dev
->base
+ offset
);
450 static void write_reg(struct vpe_dev
*dev
, int offset
, u32 value
)
452 iowrite32(value
, dev
->base
+ offset
);
455 /* register field read/write helpers */
456 static int get_field(u32 value
, u32 mask
, int shift
)
458 return (value
& (mask
<< shift
)) >> shift
;
461 static int read_field_reg(struct vpe_dev
*dev
, int offset
, u32 mask
, int shift
)
463 return get_field(read_reg(dev
, offset
), mask
, shift
);
466 static void write_field(u32
*valp
, u32 field
, u32 mask
, int shift
)
470 val
&= ~(mask
<< shift
);
471 val
|= (field
& mask
) << shift
;
475 static void write_field_reg(struct vpe_dev
*dev
, int offset
, u32 field
,
478 u32 val
= read_reg(dev
, offset
);
480 write_field(&val
, field
, mask
, shift
);
482 write_reg(dev
, offset
, val
);
486 * DMA address/data block for the shadow registers
489 struct vpdma_adb_hdr out_fmt_hdr
;
492 struct vpdma_adb_hdr us1_hdr
;
494 struct vpdma_adb_hdr us2_hdr
;
496 struct vpdma_adb_hdr us3_hdr
;
498 struct vpdma_adb_hdr dei_hdr
;
500 struct vpdma_adb_hdr sc_hdr0
;
503 struct vpdma_adb_hdr sc_hdr8
;
506 struct vpdma_adb_hdr sc_hdr17
;
509 struct vpdma_adb_hdr csc_hdr
;
514 #define GET_OFFSET_TOP(ctx, obj, reg) \
515 ((obj)->res->start - ctx->dev->res->start + reg)
517 #define VPE_SET_MMR_ADB_HDR(ctx, hdr, regs, offset_a) \
518 VPDMA_SET_MMR_ADB_HDR(ctx->mmr_adb, vpe_mmr_adb, hdr, regs, offset_a)
520 * Set the headers for all of the address/data block structures.
522 static void init_adb_hdrs(struct vpe_ctx
*ctx
)
524 VPE_SET_MMR_ADB_HDR(ctx
, out_fmt_hdr
, out_fmt_reg
, VPE_CLK_FORMAT_SELECT
);
525 VPE_SET_MMR_ADB_HDR(ctx
, us1_hdr
, us1_regs
, VPE_US1_R0
);
526 VPE_SET_MMR_ADB_HDR(ctx
, us2_hdr
, us2_regs
, VPE_US2_R0
);
527 VPE_SET_MMR_ADB_HDR(ctx
, us3_hdr
, us3_regs
, VPE_US3_R0
);
528 VPE_SET_MMR_ADB_HDR(ctx
, dei_hdr
, dei_regs
, VPE_DEI_FRAME_SIZE
);
529 VPE_SET_MMR_ADB_HDR(ctx
, sc_hdr0
, sc_regs0
,
530 GET_OFFSET_TOP(ctx
, ctx
->dev
->sc
, CFG_SC0
));
531 VPE_SET_MMR_ADB_HDR(ctx
, sc_hdr8
, sc_regs8
,
532 GET_OFFSET_TOP(ctx
, ctx
->dev
->sc
, CFG_SC8
));
533 VPE_SET_MMR_ADB_HDR(ctx
, sc_hdr17
, sc_regs17
,
534 GET_OFFSET_TOP(ctx
, ctx
->dev
->sc
, CFG_SC17
));
535 VPE_SET_MMR_ADB_HDR(ctx
, csc_hdr
, csc_regs
,
536 GET_OFFSET_TOP(ctx
, ctx
->dev
->csc
, CSC_CSC00
));
540 * Allocate or re-allocate the motion vector DMA buffers
541 * There are two buffers, one for input and one for output.
542 * However, the roles are reversed after each field is processed.
543 * In other words, after each field is processed, the previous
544 * output (dst) MV buffer becomes the new input (src) MV buffer.
546 static int realloc_mv_buffers(struct vpe_ctx
*ctx
, size_t size
)
548 struct device
*dev
= ctx
->dev
->v4l2_dev
.dev
;
550 if (ctx
->mv_buf_size
== size
)
554 dma_free_coherent(dev
, ctx
->mv_buf_size
, ctx
->mv_buf
[0],
558 dma_free_coherent(dev
, ctx
->mv_buf_size
, ctx
->mv_buf
[1],
564 ctx
->mv_buf
[0] = dma_alloc_coherent(dev
, size
, &ctx
->mv_buf_dma
[0],
566 if (!ctx
->mv_buf
[0]) {
567 vpe_err(ctx
->dev
, "failed to allocate motion vector buffer\n");
571 ctx
->mv_buf
[1] = dma_alloc_coherent(dev
, size
, &ctx
->mv_buf_dma
[1],
573 if (!ctx
->mv_buf
[1]) {
574 vpe_err(ctx
->dev
, "failed to allocate motion vector buffer\n");
575 dma_free_coherent(dev
, size
, ctx
->mv_buf
[0],
581 ctx
->mv_buf_size
= size
;
582 ctx
->src_mv_buf_selector
= 0;
587 static void free_mv_buffers(struct vpe_ctx
*ctx
)
589 realloc_mv_buffers(ctx
, 0);
593 * While de-interlacing, we keep the two most recent input buffers
594 * around. This function frees those two buffers when we have
595 * finished processing the current stream.
597 static void free_vbs(struct vpe_ctx
*ctx
)
599 struct vpe_dev
*dev
= ctx
->dev
;
602 if (ctx
->src_vbs
[2] == NULL
)
605 spin_lock_irqsave(&dev
->lock
, flags
);
606 if (ctx
->src_vbs
[2]) {
607 v4l2_m2m_buf_done(ctx
->src_vbs
[2], VB2_BUF_STATE_DONE
);
608 v4l2_m2m_buf_done(ctx
->src_vbs
[1], VB2_BUF_STATE_DONE
);
610 spin_unlock_irqrestore(&dev
->lock
, flags
);
614 * Enable or disable the VPE clocks
616 static void vpe_set_clock_enable(struct vpe_dev
*dev
, bool on
)
621 val
= VPE_DATA_PATH_CLK_ENABLE
| VPE_VPEDMA_CLK_ENABLE
;
622 write_reg(dev
, VPE_CLK_ENABLE
, val
);
625 static void vpe_top_reset(struct vpe_dev
*dev
)
628 write_field_reg(dev
, VPE_CLK_RESET
, 1, VPE_DATA_PATH_CLK_RESET_MASK
,
629 VPE_DATA_PATH_CLK_RESET_SHIFT
);
631 usleep_range(100, 150);
633 write_field_reg(dev
, VPE_CLK_RESET
, 0, VPE_DATA_PATH_CLK_RESET_MASK
,
634 VPE_DATA_PATH_CLK_RESET_SHIFT
);
637 static void vpe_top_vpdma_reset(struct vpe_dev
*dev
)
639 write_field_reg(dev
, VPE_CLK_RESET
, 1, VPE_VPDMA_CLK_RESET_MASK
,
640 VPE_VPDMA_CLK_RESET_SHIFT
);
642 usleep_range(100, 150);
644 write_field_reg(dev
, VPE_CLK_RESET
, 0, VPE_VPDMA_CLK_RESET_MASK
,
645 VPE_VPDMA_CLK_RESET_SHIFT
);
649 * Load the correct of upsampler coefficients into the shadow MMRs
651 static void set_us_coefficients(struct vpe_ctx
*ctx
)
653 struct vpe_mmr_adb
*mmr_adb
= ctx
->mmr_adb
.addr
;
654 struct vpe_q_data
*s_q_data
= &ctx
->q_data
[Q_DATA_SRC
];
655 u32
*us1_reg
= &mmr_adb
->us1_regs
[0];
656 u32
*us2_reg
= &mmr_adb
->us2_regs
[0];
657 u32
*us3_reg
= &mmr_adb
->us3_regs
[0];
658 const unsigned short *cp
, *end_cp
;
660 cp
= &us_coeffs
[0].anchor_fid0_c0
;
662 if (s_q_data
->flags
& Q_IS_INTERLACED
) /* interlaced */
663 cp
+= sizeof(us_coeffs
[0]) / sizeof(*cp
);
665 end_cp
= cp
+ sizeof(us_coeffs
[0]) / sizeof(*cp
);
667 while (cp
< end_cp
) {
668 write_field(us1_reg
, *cp
++, VPE_US_C0_MASK
, VPE_US_C0_SHIFT
);
669 write_field(us1_reg
, *cp
++, VPE_US_C1_MASK
, VPE_US_C1_SHIFT
);
670 *us2_reg
++ = *us1_reg
;
671 *us3_reg
++ = *us1_reg
++;
673 ctx
->load_mmrs
= true;
677 * Set the upsampler config mode and the VPDMA line mode in the shadow MMRs.
679 static void set_cfg_modes(struct vpe_ctx
*ctx
)
681 struct vpe_fmt
*fmt
= ctx
->q_data
[Q_DATA_SRC
].fmt
;
682 struct vpe_mmr_adb
*mmr_adb
= ctx
->mmr_adb
.addr
;
683 u32
*us1_reg0
= &mmr_adb
->us1_regs
[0];
684 u32
*us2_reg0
= &mmr_adb
->us2_regs
[0];
685 u32
*us3_reg0
= &mmr_adb
->us3_regs
[0];
689 * Cfg Mode 0: YUV420 source, enable upsampler, DEI is de-interlacing.
690 * Cfg Mode 1: YUV422 source, disable upsampler, DEI is de-interlacing.
693 if (fmt
->fourcc
== V4L2_PIX_FMT_NV12
)
696 write_field(us1_reg0
, cfg_mode
, VPE_US_MODE_MASK
, VPE_US_MODE_SHIFT
);
697 write_field(us2_reg0
, cfg_mode
, VPE_US_MODE_MASK
, VPE_US_MODE_SHIFT
);
698 write_field(us3_reg0
, cfg_mode
, VPE_US_MODE_MASK
, VPE_US_MODE_SHIFT
);
700 ctx
->load_mmrs
= true;
703 static void set_line_modes(struct vpe_ctx
*ctx
)
705 struct vpe_fmt
*fmt
= ctx
->q_data
[Q_DATA_SRC
].fmt
;
708 if (fmt
->fourcc
== V4L2_PIX_FMT_NV12
)
709 line_mode
= 0; /* double lines to line buffer */
712 vpdma_set_line_mode(ctx
->dev
->vpdma
, line_mode
, VPE_CHAN_CHROMA1_IN
);
713 vpdma_set_line_mode(ctx
->dev
->vpdma
, line_mode
, VPE_CHAN_CHROMA2_IN
);
714 vpdma_set_line_mode(ctx
->dev
->vpdma
, line_mode
, VPE_CHAN_CHROMA3_IN
);
716 /* frame start for input luma */
717 vpdma_set_frame_start_event(ctx
->dev
->vpdma
, VPDMA_FSEVENT_CHANNEL_ACTIVE
,
719 vpdma_set_frame_start_event(ctx
->dev
->vpdma
, VPDMA_FSEVENT_CHANNEL_ACTIVE
,
721 vpdma_set_frame_start_event(ctx
->dev
->vpdma
, VPDMA_FSEVENT_CHANNEL_ACTIVE
,
724 /* frame start for input chroma */
725 vpdma_set_frame_start_event(ctx
->dev
->vpdma
, VPDMA_FSEVENT_CHANNEL_ACTIVE
,
726 VPE_CHAN_CHROMA1_IN
);
727 vpdma_set_frame_start_event(ctx
->dev
->vpdma
, VPDMA_FSEVENT_CHANNEL_ACTIVE
,
728 VPE_CHAN_CHROMA2_IN
);
729 vpdma_set_frame_start_event(ctx
->dev
->vpdma
, VPDMA_FSEVENT_CHANNEL_ACTIVE
,
730 VPE_CHAN_CHROMA3_IN
);
732 /* frame start for MV in client */
733 vpdma_set_frame_start_event(ctx
->dev
->vpdma
, VPDMA_FSEVENT_CHANNEL_ACTIVE
,
738 * Set the shadow registers that are modified when the source
741 static void set_src_registers(struct vpe_ctx
*ctx
)
743 set_us_coefficients(ctx
);
747 * Set the shadow registers that are modified when the destination
750 static void set_dst_registers(struct vpe_ctx
*ctx
)
752 struct vpe_mmr_adb
*mmr_adb
= ctx
->mmr_adb
.addr
;
753 enum v4l2_colorspace clrspc
= ctx
->q_data
[Q_DATA_DST
].colorspace
;
754 struct vpe_fmt
*fmt
= ctx
->q_data
[Q_DATA_DST
].fmt
;
757 if (clrspc
== V4L2_COLORSPACE_SRGB
) {
758 val
|= VPE_RGB_OUT_SELECT
;
759 vpdma_set_bg_color(ctx
->dev
->vpdma
,
760 (struct vpdma_data_format
*)fmt
->vpdma_fmt
[0], 0xff);
761 } else if (fmt
->fourcc
== V4L2_PIX_FMT_NV16
)
762 val
|= VPE_COLOR_SEPARATE_422
;
765 * the source of CHR_DS and CSC is always the scaler, irrespective of
766 * whether it's used or not
768 val
|= VPE_DS_SRC_DEI_SCALER
| VPE_CSC_SRC_DEI_SCALER
;
770 if (fmt
->fourcc
!= V4L2_PIX_FMT_NV12
)
771 val
|= VPE_DS_BYPASS
;
773 mmr_adb
->out_fmt_reg
[0] = val
;
775 ctx
->load_mmrs
= true;
779 * Set the de-interlacer shadow register values
781 static void set_dei_regs(struct vpe_ctx
*ctx
)
783 struct vpe_mmr_adb
*mmr_adb
= ctx
->mmr_adb
.addr
;
784 struct vpe_q_data
*s_q_data
= &ctx
->q_data
[Q_DATA_SRC
];
785 unsigned int src_h
= s_q_data
->c_rect
.height
;
786 unsigned int src_w
= s_q_data
->c_rect
.width
;
787 u32
*dei_mmr0
= &mmr_adb
->dei_regs
[0];
788 bool deinterlace
= true;
792 * according to TRM, we should set DEI in progressive bypass mode when
793 * the input content is progressive, however, DEI is bypassed correctly
794 * for both progressive and interlace content in interlace bypass mode.
795 * It has been recommended not to use progressive bypass mode.
797 if (!(s_q_data
->flags
& Q_IS_INTERLACED
) || !ctx
->deinterlacing
) {
799 val
= VPE_DEI_INTERLACE_BYPASS
;
802 src_h
= deinterlace
? src_h
* 2 : src_h
;
804 val
|= (src_h
<< VPE_DEI_HEIGHT_SHIFT
) |
805 (src_w
<< VPE_DEI_WIDTH_SHIFT
) |
810 ctx
->load_mmrs
= true;
813 static void set_dei_shadow_registers(struct vpe_ctx
*ctx
)
815 struct vpe_mmr_adb
*mmr_adb
= ctx
->mmr_adb
.addr
;
816 u32
*dei_mmr
= &mmr_adb
->dei_regs
[0];
817 const struct vpe_dei_regs
*cur
= &dei_regs
;
819 dei_mmr
[2] = cur
->mdt_spacial_freq_thr_reg
;
820 dei_mmr
[3] = cur
->edi_config_reg
;
821 dei_mmr
[4] = cur
->edi_lut_reg0
;
822 dei_mmr
[5] = cur
->edi_lut_reg1
;
823 dei_mmr
[6] = cur
->edi_lut_reg2
;
824 dei_mmr
[7] = cur
->edi_lut_reg3
;
826 ctx
->load_mmrs
= true;
829 static void config_edi_input_mode(struct vpe_ctx
*ctx
, int mode
)
831 struct vpe_mmr_adb
*mmr_adb
= ctx
->mmr_adb
.addr
;
832 u32
*edi_config_reg
= &mmr_adb
->dei_regs
[3];
835 write_field(edi_config_reg
, 1, 1, 2); /* EDI_ENABLE_3D */
838 write_field(edi_config_reg
, 1, 1, 3); /* EDI_CHROMA_3D */
840 write_field(edi_config_reg
, mode
, VPE_EDI_INP_MODE_MASK
,
841 VPE_EDI_INP_MODE_SHIFT
);
843 ctx
->load_mmrs
= true;
847 * Set the shadow registers whose values are modified when either the
848 * source or destination format is changed.
850 static int set_srcdst_params(struct vpe_ctx
*ctx
)
852 struct vpe_q_data
*s_q_data
= &ctx
->q_data
[Q_DATA_SRC
];
853 struct vpe_q_data
*d_q_data
= &ctx
->q_data
[Q_DATA_DST
];
854 struct vpe_mmr_adb
*mmr_adb
= ctx
->mmr_adb
.addr
;
855 unsigned int src_w
= s_q_data
->c_rect
.width
;
856 unsigned int src_h
= s_q_data
->c_rect
.height
;
857 unsigned int dst_w
= d_q_data
->c_rect
.width
;
858 unsigned int dst_h
= d_q_data
->c_rect
.height
;
863 ctx
->field
= V4L2_FIELD_TOP
;
865 if ((s_q_data
->flags
& Q_IS_INTERLACED
) &&
866 !(d_q_data
->flags
& Q_IS_INTERLACED
)) {
868 const struct vpdma_data_format
*mv
=
869 &vpdma_misc_fmts
[VPDMA_DATA_FMT_MV
];
872 * we make sure that the source image has a 16 byte aligned
873 * stride, we need to do the same for the motion vector buffer
874 * by aligning it's stride to the next 16 byte boundry. this
875 * extra space will not be used by the de-interlacer, but will
876 * ensure that vpdma operates correctly
878 bytes_per_line
= ALIGN((s_q_data
->width
* mv
->depth
) >> 3,
880 mv_buf_size
= bytes_per_line
* s_q_data
->height
;
882 ctx
->deinterlacing
= true;
885 ctx
->deinterlacing
= false;
890 ctx
->src_vbs
[2] = ctx
->src_vbs
[1] = ctx
->src_vbs
[0] = NULL
;
892 ret
= realloc_mv_buffers(ctx
, mv_buf_size
);
899 csc_set_coeff(ctx
->dev
->csc
, &mmr_adb
->csc_regs
[0],
900 s_q_data
->colorspace
, d_q_data
->colorspace
);
902 sc_set_hs_coeffs(ctx
->dev
->sc
, ctx
->sc_coeff_h
.addr
, src_w
, dst_w
);
903 sc_set_vs_coeffs(ctx
->dev
->sc
, ctx
->sc_coeff_v
.addr
, src_h
, dst_h
);
905 sc_config_scaler(ctx
->dev
->sc
, &mmr_adb
->sc_regs0
[0],
906 &mmr_adb
->sc_regs8
[0], &mmr_adb
->sc_regs17
[0],
907 src_w
, src_h
, dst_w
, dst_h
);
913 * Return the vpe_ctx structure for a given struct file
915 static struct vpe_ctx
*file2ctx(struct file
*file
)
917 return container_of(file
->private_data
, struct vpe_ctx
, fh
);
925 * job_ready() - check whether an instance is ready to be scheduled to run
927 static int job_ready(void *priv
)
929 struct vpe_ctx
*ctx
= priv
;
932 * This check is needed as this might be called directly from driver
933 * When called by m2m framework, this will always satisfy, but when
934 * called from vpe_irq, this might fail. (src stream with zero buffers)
936 if (v4l2_m2m_num_src_bufs_ready(ctx
->fh
.m2m_ctx
) <= 0 ||
937 v4l2_m2m_num_dst_bufs_ready(ctx
->fh
.m2m_ctx
) <= 0)
943 static void job_abort(void *priv
)
945 struct vpe_ctx
*ctx
= priv
;
947 /* Will cancel the transaction in the next interrupt handler */
952 * Lock access to the device
954 static void vpe_lock(void *priv
)
956 struct vpe_ctx
*ctx
= priv
;
957 struct vpe_dev
*dev
= ctx
->dev
;
958 mutex_lock(&dev
->dev_mutex
);
961 static void vpe_unlock(void *priv
)
963 struct vpe_ctx
*ctx
= priv
;
964 struct vpe_dev
*dev
= ctx
->dev
;
965 mutex_unlock(&dev
->dev_mutex
);
968 static void vpe_dump_regs(struct vpe_dev
*dev
)
970 #define DUMPREG(r) vpe_dbg(dev, "%-35s %08x\n", #r, read_reg(dev, VPE_##r))
972 vpe_dbg(dev
, "VPE Registers:\n");
976 DUMPREG(INT0_STATUS0_RAW
);
977 DUMPREG(INT0_STATUS0
);
978 DUMPREG(INT0_ENABLE0
);
979 DUMPREG(INT0_STATUS1_RAW
);
980 DUMPREG(INT0_STATUS1
);
981 DUMPREG(INT0_ENABLE1
);
984 DUMPREG(CLK_FORMAT_SELECT
);
985 DUMPREG(CLK_RANGE_MAP
);
1010 DUMPREG(DEI_FRAME_SIZE
);
1011 DUMPREG(MDT_BYPASS
);
1012 DUMPREG(MDT_SF_THRESHOLD
);
1013 DUMPREG(EDI_CONFIG
);
1014 DUMPREG(DEI_EDI_LUT_R0
);
1015 DUMPREG(DEI_EDI_LUT_R1
);
1016 DUMPREG(DEI_EDI_LUT_R2
);
1017 DUMPREG(DEI_EDI_LUT_R3
);
1018 DUMPREG(DEI_FMD_WINDOW_R0
);
1019 DUMPREG(DEI_FMD_WINDOW_R1
);
1020 DUMPREG(DEI_FMD_CONTROL_R0
);
1021 DUMPREG(DEI_FMD_CONTROL_R1
);
1022 DUMPREG(DEI_FMD_STATUS_R0
);
1023 DUMPREG(DEI_FMD_STATUS_R1
);
1024 DUMPREG(DEI_FMD_STATUS_R2
);
1027 sc_dump_regs(dev
->sc
);
1028 csc_dump_regs(dev
->csc
);
1031 static void add_out_dtd(struct vpe_ctx
*ctx
, int port
)
1033 struct vpe_q_data
*q_data
= &ctx
->q_data
[Q_DATA_DST
];
1034 const struct vpe_port_data
*p_data
= &port_data
[port
];
1035 struct vb2_buffer
*vb
= &ctx
->dst_vb
->vb2_buf
;
1036 struct vpe_fmt
*fmt
= q_data
->fmt
;
1037 const struct vpdma_data_format
*vpdma_fmt
;
1038 int mv_buf_selector
= !ctx
->src_mv_buf_selector
;
1039 dma_addr_t dma_addr
;
1042 if (port
== VPE_PORT_MV_OUT
) {
1043 vpdma_fmt
= &vpdma_misc_fmts
[VPDMA_DATA_FMT_MV
];
1044 dma_addr
= ctx
->mv_buf_dma
[mv_buf_selector
];
1046 /* to incorporate interleaved formats */
1047 int plane
= fmt
->coplanar
? p_data
->vb_part
: 0;
1049 vpdma_fmt
= fmt
->vpdma_fmt
[plane
];
1050 dma_addr
= vb2_dma_contig_plane_dma_addr(vb
, plane
);
1053 "acquiring output buffer(%d) dma_addr failed\n",
1059 if (q_data
->flags
& Q_DATA_FRAME_1D
)
1060 flags
|= VPDMA_DATA_FRAME_1D
;
1061 if (q_data
->flags
& Q_DATA_MODE_TILED
)
1062 flags
|= VPDMA_DATA_MODE_TILED
;
1064 vpdma_set_max_size(ctx
->dev
->vpdma
, VPDMA_MAX_SIZE1
,
1067 vpdma_add_out_dtd(&ctx
->desc_list
, q_data
->width
, &q_data
->c_rect
,
1068 vpdma_fmt
, dma_addr
, MAX_OUT_WIDTH_REG1
,
1069 MAX_OUT_HEIGHT_REG1
, p_data
->channel
, flags
);
1072 static void add_in_dtd(struct vpe_ctx
*ctx
, int port
)
1074 struct vpe_q_data
*q_data
= &ctx
->q_data
[Q_DATA_SRC
];
1075 const struct vpe_port_data
*p_data
= &port_data
[port
];
1076 struct vb2_buffer
*vb
= &ctx
->src_vbs
[p_data
->vb_index
]->vb2_buf
;
1077 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
1078 struct vpe_fmt
*fmt
= q_data
->fmt
;
1079 const struct vpdma_data_format
*vpdma_fmt
;
1080 int mv_buf_selector
= ctx
->src_mv_buf_selector
;
1081 int field
= vbuf
->field
== V4L2_FIELD_BOTTOM
;
1082 int frame_width
, frame_height
;
1083 dma_addr_t dma_addr
;
1086 if (port
== VPE_PORT_MV_IN
) {
1087 vpdma_fmt
= &vpdma_misc_fmts
[VPDMA_DATA_FMT_MV
];
1088 dma_addr
= ctx
->mv_buf_dma
[mv_buf_selector
];
1090 /* to incorporate interleaved formats */
1091 int plane
= fmt
->coplanar
? p_data
->vb_part
: 0;
1093 vpdma_fmt
= fmt
->vpdma_fmt
[plane
];
1095 dma_addr
= vb2_dma_contig_plane_dma_addr(vb
, plane
);
1098 "acquiring input buffer(%d) dma_addr failed\n",
1103 if (q_data
->flags
& Q_DATA_INTERLACED_SEQ_TB
) {
1105 * Use top or bottom field from same vb alternately
1106 * f,f-1,f-2 = TBT when seq is even
1107 * f,f-1,f-2 = BTB when seq is odd
1109 field
= (p_data
->vb_index
+ (ctx
->sequence
% 2)) % 2;
1113 * bottom field of a SEQ_TB buffer
1114 * Skip the top field data by
1116 int height
= q_data
->height
/ 2;
1117 int bpp
= fmt
->fourcc
== V4L2_PIX_FMT_NV12
?
1118 1 : (vpdma_fmt
->depth
>> 3);
1121 dma_addr
+= q_data
->width
* height
* bpp
;
1126 if (q_data
->flags
& Q_DATA_FRAME_1D
)
1127 flags
|= VPDMA_DATA_FRAME_1D
;
1128 if (q_data
->flags
& Q_DATA_MODE_TILED
)
1129 flags
|= VPDMA_DATA_MODE_TILED
;
1131 frame_width
= q_data
->c_rect
.width
;
1132 frame_height
= q_data
->c_rect
.height
;
1134 if (p_data
->vb_part
&& fmt
->fourcc
== V4L2_PIX_FMT_NV12
)
1137 vpdma_add_in_dtd(&ctx
->desc_list
, q_data
->width
, &q_data
->c_rect
,
1138 vpdma_fmt
, dma_addr
, p_data
->channel
, field
, flags
, frame_width
,
1139 frame_height
, 0, 0);
1143 * Enable the expected IRQ sources
1145 static void enable_irqs(struct vpe_ctx
*ctx
)
1147 write_reg(ctx
->dev
, VPE_INT0_ENABLE0_SET
, VPE_INT0_LIST0_COMPLETE
);
1148 write_reg(ctx
->dev
, VPE_INT0_ENABLE1_SET
, VPE_DEI_ERROR_INT
|
1149 VPE_DS1_UV_ERROR_INT
);
1151 vpdma_enable_list_complete_irq(ctx
->dev
->vpdma
, 0, 0, true);
1154 static void disable_irqs(struct vpe_ctx
*ctx
)
1156 write_reg(ctx
->dev
, VPE_INT0_ENABLE0_CLR
, 0xffffffff);
1157 write_reg(ctx
->dev
, VPE_INT0_ENABLE1_CLR
, 0xffffffff);
1159 vpdma_enable_list_complete_irq(ctx
->dev
->vpdma
, 0, 0, false);
1162 /* device_run() - prepares and starts the device
1164 * This function is only called when both the source and destination
1165 * buffers are in place.
1167 static void device_run(void *priv
)
1169 struct vpe_ctx
*ctx
= priv
;
1170 struct sc_data
*sc
= ctx
->dev
->sc
;
1171 struct vpe_q_data
*d_q_data
= &ctx
->q_data
[Q_DATA_DST
];
1172 struct vpe_q_data
*s_q_data
= &ctx
->q_data
[Q_DATA_SRC
];
1174 if (ctx
->deinterlacing
&& s_q_data
->flags
& Q_DATA_INTERLACED_SEQ_TB
&&
1175 ctx
->sequence
% 2 == 0) {
1176 /* When using SEQ_TB buffers, When using it first time,
1177 * No need to remove the buffer as the next field is present
1178 * in the same buffer. (so that job_ready won't fail)
1179 * It will be removed when using bottom field
1181 ctx
->src_vbs
[0] = v4l2_m2m_next_src_buf(ctx
->fh
.m2m_ctx
);
1182 WARN_ON(ctx
->src_vbs
[0] == NULL
);
1184 ctx
->src_vbs
[0] = v4l2_m2m_src_buf_remove(ctx
->fh
.m2m_ctx
);
1185 WARN_ON(ctx
->src_vbs
[0] == NULL
);
1188 ctx
->dst_vb
= v4l2_m2m_dst_buf_remove(ctx
->fh
.m2m_ctx
);
1189 WARN_ON(ctx
->dst_vb
== NULL
);
1191 if (ctx
->deinterlacing
) {
1193 if (ctx
->src_vbs
[2] == NULL
) {
1194 ctx
->src_vbs
[2] = ctx
->src_vbs
[0];
1195 WARN_ON(ctx
->src_vbs
[2] == NULL
);
1196 ctx
->src_vbs
[1] = ctx
->src_vbs
[0];
1197 WARN_ON(ctx
->src_vbs
[1] == NULL
);
1201 * we have output the first 2 frames through line average, we
1202 * now switch to EDI de-interlacer
1204 if (ctx
->sequence
== 2)
1205 config_edi_input_mode(ctx
, 0x3); /* EDI (Y + UV) */
1208 /* config descriptors */
1209 if (ctx
->dev
->loaded_mmrs
!= ctx
->mmr_adb
.dma_addr
|| ctx
->load_mmrs
) {
1210 vpdma_map_desc_buf(ctx
->dev
->vpdma
, &ctx
->mmr_adb
);
1211 vpdma_add_cfd_adb(&ctx
->desc_list
, CFD_MMR_CLIENT
, &ctx
->mmr_adb
);
1213 set_line_modes(ctx
);
1215 ctx
->dev
->loaded_mmrs
= ctx
->mmr_adb
.dma_addr
;
1216 ctx
->load_mmrs
= false;
1219 if (sc
->loaded_coeff_h
!= ctx
->sc_coeff_h
.dma_addr
||
1221 vpdma_map_desc_buf(ctx
->dev
->vpdma
, &ctx
->sc_coeff_h
);
1222 vpdma_add_cfd_block(&ctx
->desc_list
, CFD_SC_CLIENT
,
1223 &ctx
->sc_coeff_h
, 0);
1225 sc
->loaded_coeff_h
= ctx
->sc_coeff_h
.dma_addr
;
1226 sc
->load_coeff_h
= false;
1229 if (sc
->loaded_coeff_v
!= ctx
->sc_coeff_v
.dma_addr
||
1231 vpdma_map_desc_buf(ctx
->dev
->vpdma
, &ctx
->sc_coeff_v
);
1232 vpdma_add_cfd_block(&ctx
->desc_list
, CFD_SC_CLIENT
,
1233 &ctx
->sc_coeff_v
, SC_COEF_SRAM_SIZE
>> 4);
1235 sc
->loaded_coeff_v
= ctx
->sc_coeff_v
.dma_addr
;
1236 sc
->load_coeff_v
= false;
1239 /* output data descriptors */
1240 if (ctx
->deinterlacing
)
1241 add_out_dtd(ctx
, VPE_PORT_MV_OUT
);
1243 if (d_q_data
->colorspace
== V4L2_COLORSPACE_SRGB
) {
1244 add_out_dtd(ctx
, VPE_PORT_RGB_OUT
);
1246 add_out_dtd(ctx
, VPE_PORT_LUMA_OUT
);
1247 if (d_q_data
->fmt
->coplanar
)
1248 add_out_dtd(ctx
, VPE_PORT_CHROMA_OUT
);
1251 /* input data descriptors */
1252 if (ctx
->deinterlacing
) {
1253 add_in_dtd(ctx
, VPE_PORT_LUMA3_IN
);
1254 add_in_dtd(ctx
, VPE_PORT_CHROMA3_IN
);
1256 add_in_dtd(ctx
, VPE_PORT_LUMA2_IN
);
1257 add_in_dtd(ctx
, VPE_PORT_CHROMA2_IN
);
1260 add_in_dtd(ctx
, VPE_PORT_LUMA1_IN
);
1261 add_in_dtd(ctx
, VPE_PORT_CHROMA1_IN
);
1263 if (ctx
->deinterlacing
)
1264 add_in_dtd(ctx
, VPE_PORT_MV_IN
);
1266 /* sync on channel control descriptors for input ports */
1267 vpdma_add_sync_on_channel_ctd(&ctx
->desc_list
, VPE_CHAN_LUMA1_IN
);
1268 vpdma_add_sync_on_channel_ctd(&ctx
->desc_list
, VPE_CHAN_CHROMA1_IN
);
1270 if (ctx
->deinterlacing
) {
1271 vpdma_add_sync_on_channel_ctd(&ctx
->desc_list
,
1273 vpdma_add_sync_on_channel_ctd(&ctx
->desc_list
,
1274 VPE_CHAN_CHROMA2_IN
);
1276 vpdma_add_sync_on_channel_ctd(&ctx
->desc_list
,
1278 vpdma_add_sync_on_channel_ctd(&ctx
->desc_list
,
1279 VPE_CHAN_CHROMA3_IN
);
1281 vpdma_add_sync_on_channel_ctd(&ctx
->desc_list
, VPE_CHAN_MV_IN
);
1284 /* sync on channel control descriptors for output ports */
1285 if (d_q_data
->colorspace
== V4L2_COLORSPACE_SRGB
) {
1286 vpdma_add_sync_on_channel_ctd(&ctx
->desc_list
,
1289 vpdma_add_sync_on_channel_ctd(&ctx
->desc_list
,
1291 if (d_q_data
->fmt
->coplanar
)
1292 vpdma_add_sync_on_channel_ctd(&ctx
->desc_list
,
1293 VPE_CHAN_CHROMA_OUT
);
1296 if (ctx
->deinterlacing
)
1297 vpdma_add_sync_on_channel_ctd(&ctx
->desc_list
, VPE_CHAN_MV_OUT
);
1301 vpdma_map_desc_buf(ctx
->dev
->vpdma
, &ctx
->desc_list
.buf
);
1302 vpdma_submit_descs(ctx
->dev
->vpdma
, &ctx
->desc_list
, 0);
1305 static void dei_error(struct vpe_ctx
*ctx
)
1307 dev_warn(ctx
->dev
->v4l2_dev
.dev
,
1308 "received DEI error interrupt\n");
1311 static void ds1_uv_error(struct vpe_ctx
*ctx
)
1313 dev_warn(ctx
->dev
->v4l2_dev
.dev
,
1314 "received downsampler error interrupt\n");
1317 static irqreturn_t
vpe_irq(int irq_vpe
, void *data
)
1319 struct vpe_dev
*dev
= (struct vpe_dev
*)data
;
1320 struct vpe_ctx
*ctx
;
1321 struct vpe_q_data
*d_q_data
;
1322 struct vb2_v4l2_buffer
*s_vb
, *d_vb
;
1323 unsigned long flags
;
1325 bool list_complete
= false;
1327 irqst0
= read_reg(dev
, VPE_INT0_STATUS0
);
1329 write_reg(dev
, VPE_INT0_STATUS0_CLR
, irqst0
);
1330 vpe_dbg(dev
, "INT0_STATUS0 = 0x%08x\n", irqst0
);
1333 irqst1
= read_reg(dev
, VPE_INT0_STATUS1
);
1335 write_reg(dev
, VPE_INT0_STATUS1_CLR
, irqst1
);
1336 vpe_dbg(dev
, "INT0_STATUS1 = 0x%08x\n", irqst1
);
1339 ctx
= v4l2_m2m_get_curr_priv(dev
->m2m_dev
);
1341 vpe_err(dev
, "instance released before end of transaction\n");
1346 if (irqst1
& VPE_DEI_ERROR_INT
) {
1347 irqst1
&= ~VPE_DEI_ERROR_INT
;
1350 if (irqst1
& VPE_DS1_UV_ERROR_INT
) {
1351 irqst1
&= ~VPE_DS1_UV_ERROR_INT
;
1357 if (irqst0
& VPE_INT0_LIST0_COMPLETE
)
1358 vpdma_clear_list_stat(ctx
->dev
->vpdma
, 0, 0);
1360 irqst0
&= ~(VPE_INT0_LIST0_COMPLETE
);
1361 list_complete
= true;
1364 if (irqst0
| irqst1
) {
1365 dev_warn(dev
->v4l2_dev
.dev
, "Unexpected interrupt: INT0_STATUS0 = 0x%08x, INT0_STATUS1 = 0x%08x\n",
1370 * Setup next operation only when list complete IRQ occurs
1371 * otherwise, skip the following code
1378 vpdma_unmap_desc_buf(dev
->vpdma
, &ctx
->desc_list
.buf
);
1379 vpdma_unmap_desc_buf(dev
->vpdma
, &ctx
->mmr_adb
);
1380 vpdma_unmap_desc_buf(dev
->vpdma
, &ctx
->sc_coeff_h
);
1381 vpdma_unmap_desc_buf(dev
->vpdma
, &ctx
->sc_coeff_v
);
1383 vpdma_reset_desc_list(&ctx
->desc_list
);
1385 /* the previous dst mv buffer becomes the next src mv buffer */
1386 ctx
->src_mv_buf_selector
= !ctx
->src_mv_buf_selector
;
1391 s_vb
= ctx
->src_vbs
[0];
1394 d_vb
->flags
= s_vb
->flags
;
1395 d_vb
->vb2_buf
.timestamp
= s_vb
->vb2_buf
.timestamp
;
1397 if (s_vb
->flags
& V4L2_BUF_FLAG_TIMECODE
)
1398 d_vb
->timecode
= s_vb
->timecode
;
1400 d_vb
->sequence
= ctx
->sequence
;
1402 d_q_data
= &ctx
->q_data
[Q_DATA_DST
];
1403 if (d_q_data
->flags
& Q_IS_INTERLACED
) {
1404 d_vb
->field
= ctx
->field
;
1405 if (ctx
->field
== V4L2_FIELD_BOTTOM
) {
1407 ctx
->field
= V4L2_FIELD_TOP
;
1409 WARN_ON(ctx
->field
!= V4L2_FIELD_TOP
);
1410 ctx
->field
= V4L2_FIELD_BOTTOM
;
1413 d_vb
->field
= V4L2_FIELD_NONE
;
1417 if (ctx
->deinterlacing
) {
1419 * Allow source buffer to be dequeued only if it won't be used
1420 * in the next iteration. All vbs are initialized to first
1421 * buffer and we are shifting buffers every iteration, for the
1422 * first two iterations, no buffer will be dequeued.
1423 * This ensures that driver will keep (n-2)th (n-1)th and (n)th
1424 * field when deinterlacing is enabled
1426 if (ctx
->src_vbs
[2] != ctx
->src_vbs
[1])
1427 s_vb
= ctx
->src_vbs
[2];
1432 spin_lock_irqsave(&dev
->lock
, flags
);
1435 v4l2_m2m_buf_done(s_vb
, VB2_BUF_STATE_DONE
);
1437 v4l2_m2m_buf_done(d_vb
, VB2_BUF_STATE_DONE
);
1439 spin_unlock_irqrestore(&dev
->lock
, flags
);
1441 if (ctx
->deinterlacing
) {
1442 ctx
->src_vbs
[2] = ctx
->src_vbs
[1];
1443 ctx
->src_vbs
[1] = ctx
->src_vbs
[0];
1446 ctx
->bufs_completed
++;
1447 if (ctx
->bufs_completed
< ctx
->bufs_per_job
&& job_ready(ctx
)) {
1453 vpe_dbg(ctx
->dev
, "finishing transaction\n");
1454 ctx
->bufs_completed
= 0;
1455 v4l2_m2m_job_finish(dev
->m2m_dev
, ctx
->fh
.m2m_ctx
);
1463 static int vpe_querycap(struct file
*file
, void *priv
,
1464 struct v4l2_capability
*cap
)
1466 strncpy(cap
->driver
, VPE_MODULE_NAME
, sizeof(cap
->driver
) - 1);
1467 strncpy(cap
->card
, VPE_MODULE_NAME
, sizeof(cap
->card
) - 1);
1468 snprintf(cap
->bus_info
, sizeof(cap
->bus_info
), "platform:%s",
1470 cap
->device_caps
= V4L2_CAP_VIDEO_M2M_MPLANE
| V4L2_CAP_STREAMING
;
1471 cap
->capabilities
= cap
->device_caps
| V4L2_CAP_DEVICE_CAPS
;
1475 static int __enum_fmt(struct v4l2_fmtdesc
*f
, u32 type
)
1478 struct vpe_fmt
*fmt
= NULL
;
1481 for (i
= 0; i
< ARRAY_SIZE(vpe_formats
); ++i
) {
1482 if (vpe_formats
[i
].types
& type
) {
1483 if (index
== f
->index
) {
1484 fmt
= &vpe_formats
[i
];
1494 strncpy(f
->description
, fmt
->name
, sizeof(f
->description
) - 1);
1495 f
->pixelformat
= fmt
->fourcc
;
1499 static int vpe_enum_fmt(struct file
*file
, void *priv
,
1500 struct v4l2_fmtdesc
*f
)
1502 if (V4L2_TYPE_IS_OUTPUT(f
->type
))
1503 return __enum_fmt(f
, VPE_FMT_TYPE_OUTPUT
);
1505 return __enum_fmt(f
, VPE_FMT_TYPE_CAPTURE
);
1508 static int vpe_g_fmt(struct file
*file
, void *priv
, struct v4l2_format
*f
)
1510 struct v4l2_pix_format_mplane
*pix
= &f
->fmt
.pix_mp
;
1511 struct vpe_ctx
*ctx
= file2ctx(file
);
1512 struct vb2_queue
*vq
;
1513 struct vpe_q_data
*q_data
;
1516 vq
= v4l2_m2m_get_vq(ctx
->fh
.m2m_ctx
, f
->type
);
1520 q_data
= get_q_data(ctx
, f
->type
);
1522 pix
->width
= q_data
->width
;
1523 pix
->height
= q_data
->height
;
1524 pix
->pixelformat
= q_data
->fmt
->fourcc
;
1525 pix
->field
= q_data
->field
;
1527 if (V4L2_TYPE_IS_OUTPUT(f
->type
)) {
1528 pix
->colorspace
= q_data
->colorspace
;
1530 struct vpe_q_data
*s_q_data
;
1532 /* get colorspace from the source queue */
1533 s_q_data
= get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
);
1535 pix
->colorspace
= s_q_data
->colorspace
;
1538 pix
->num_planes
= q_data
->fmt
->coplanar
? 2 : 1;
1540 for (i
= 0; i
< pix
->num_planes
; i
++) {
1541 pix
->plane_fmt
[i
].bytesperline
= q_data
->bytesperline
[i
];
1542 pix
->plane_fmt
[i
].sizeimage
= q_data
->sizeimage
[i
];
1548 static int __vpe_try_fmt(struct vpe_ctx
*ctx
, struct v4l2_format
*f
,
1549 struct vpe_fmt
*fmt
, int type
)
1551 struct v4l2_pix_format_mplane
*pix
= &f
->fmt
.pix_mp
;
1552 struct v4l2_plane_pix_format
*plane_fmt
;
1553 unsigned int w_align
;
1554 int i
, depth
, depth_bytes
, height
;
1556 if (!fmt
|| !(fmt
->types
& type
)) {
1557 vpe_err(ctx
->dev
, "Fourcc format (0x%08x) invalid.\n",
1562 if (pix
->field
!= V4L2_FIELD_NONE
&& pix
->field
!= V4L2_FIELD_ALTERNATE
1563 && pix
->field
!= V4L2_FIELD_SEQ_TB
)
1564 pix
->field
= V4L2_FIELD_NONE
;
1566 depth
= fmt
->vpdma_fmt
[VPE_LUMA
]->depth
;
1569 * the line stride should 16 byte aligned for VPDMA to work, based on
1570 * the bytes per pixel, figure out how much the width should be aligned
1571 * to make sure line stride is 16 byte aligned
1573 depth_bytes
= depth
>> 3;
1575 if (depth_bytes
== 3)
1577 * if bpp is 3(as in some RGB formats), the pixel width doesn't
1578 * really help in ensuring line stride is 16 byte aligned
1583 * for the remainder bpp(4, 2 and 1), the pixel width alignment
1584 * can ensure a line stride alignment of 16 bytes. For example,
1585 * if bpp is 2, then the line stride can be 16 byte aligned if
1586 * the width is 8 byte aligned
1588 w_align
= order_base_2(VPDMA_DESC_ALIGN
/ depth_bytes
);
1590 v4l_bound_align_image(&pix
->width
, MIN_W
, MAX_W
, w_align
,
1591 &pix
->height
, MIN_H
, MAX_H
, H_ALIGN
,
1594 pix
->num_planes
= fmt
->coplanar
? 2 : 1;
1595 pix
->pixelformat
= fmt
->fourcc
;
1598 * For the actual image parameters, we need to consider the field
1599 * height of the image for SEQ_TB buffers.
1601 if (pix
->field
== V4L2_FIELD_SEQ_TB
)
1602 height
= pix
->height
/ 2;
1604 height
= pix
->height
;
1606 if (!pix
->colorspace
) {
1607 if (fmt
->fourcc
== V4L2_PIX_FMT_RGB24
||
1608 fmt
->fourcc
== V4L2_PIX_FMT_BGR24
||
1609 fmt
->fourcc
== V4L2_PIX_FMT_RGB32
||
1610 fmt
->fourcc
== V4L2_PIX_FMT_BGR32
) {
1611 pix
->colorspace
= V4L2_COLORSPACE_SRGB
;
1613 if (height
> 1280) /* HD */
1614 pix
->colorspace
= V4L2_COLORSPACE_REC709
;
1616 pix
->colorspace
= V4L2_COLORSPACE_SMPTE170M
;
1620 memset(pix
->reserved
, 0, sizeof(pix
->reserved
));
1621 for (i
= 0; i
< pix
->num_planes
; i
++) {
1622 plane_fmt
= &pix
->plane_fmt
[i
];
1623 depth
= fmt
->vpdma_fmt
[i
]->depth
;
1626 plane_fmt
->bytesperline
= (pix
->width
* depth
) >> 3;
1628 plane_fmt
->bytesperline
= pix
->width
;
1630 plane_fmt
->sizeimage
=
1631 (pix
->height
* pix
->width
* depth
) >> 3;
1633 memset(plane_fmt
->reserved
, 0, sizeof(plane_fmt
->reserved
));
1639 static int vpe_try_fmt(struct file
*file
, void *priv
, struct v4l2_format
*f
)
1641 struct vpe_ctx
*ctx
= file2ctx(file
);
1642 struct vpe_fmt
*fmt
= find_format(f
);
1644 if (V4L2_TYPE_IS_OUTPUT(f
->type
))
1645 return __vpe_try_fmt(ctx
, f
, fmt
, VPE_FMT_TYPE_OUTPUT
);
1647 return __vpe_try_fmt(ctx
, f
, fmt
, VPE_FMT_TYPE_CAPTURE
);
1650 static int __vpe_s_fmt(struct vpe_ctx
*ctx
, struct v4l2_format
*f
)
1652 struct v4l2_pix_format_mplane
*pix
= &f
->fmt
.pix_mp
;
1653 struct v4l2_plane_pix_format
*plane_fmt
;
1654 struct vpe_q_data
*q_data
;
1655 struct vb2_queue
*vq
;
1658 vq
= v4l2_m2m_get_vq(ctx
->fh
.m2m_ctx
, f
->type
);
1662 if (vb2_is_busy(vq
)) {
1663 vpe_err(ctx
->dev
, "queue busy\n");
1667 q_data
= get_q_data(ctx
, f
->type
);
1671 q_data
->fmt
= find_format(f
);
1672 q_data
->width
= pix
->width
;
1673 q_data
->height
= pix
->height
;
1674 q_data
->colorspace
= pix
->colorspace
;
1675 q_data
->field
= pix
->field
;
1677 for (i
= 0; i
< pix
->num_planes
; i
++) {
1678 plane_fmt
= &pix
->plane_fmt
[i
];
1680 q_data
->bytesperline
[i
] = plane_fmt
->bytesperline
;
1681 q_data
->sizeimage
[i
] = plane_fmt
->sizeimage
;
1684 q_data
->c_rect
.left
= 0;
1685 q_data
->c_rect
.top
= 0;
1686 q_data
->c_rect
.width
= q_data
->width
;
1687 q_data
->c_rect
.height
= q_data
->height
;
1689 if (q_data
->field
== V4L2_FIELD_ALTERNATE
)
1690 q_data
->flags
|= Q_DATA_INTERLACED_ALTERNATE
;
1691 else if (q_data
->field
== V4L2_FIELD_SEQ_TB
)
1692 q_data
->flags
|= Q_DATA_INTERLACED_SEQ_TB
;
1694 q_data
->flags
&= ~Q_IS_INTERLACED
;
1696 /* the crop height is halved for the case of SEQ_TB buffers */
1697 if (q_data
->flags
& Q_DATA_INTERLACED_SEQ_TB
)
1698 q_data
->c_rect
.height
/= 2;
1700 vpe_dbg(ctx
->dev
, "Setting format for type %d, wxh: %dx%d, fmt: %d bpl_y %d",
1701 f
->type
, q_data
->width
, q_data
->height
, q_data
->fmt
->fourcc
,
1702 q_data
->bytesperline
[VPE_LUMA
]);
1703 if (q_data
->fmt
->coplanar
)
1704 vpe_dbg(ctx
->dev
, " bpl_uv %d\n",
1705 q_data
->bytesperline
[VPE_CHROMA
]);
1710 static int vpe_s_fmt(struct file
*file
, void *priv
, struct v4l2_format
*f
)
1713 struct vpe_ctx
*ctx
= file2ctx(file
);
1715 ret
= vpe_try_fmt(file
, priv
, f
);
1719 ret
= __vpe_s_fmt(ctx
, f
);
1723 if (V4L2_TYPE_IS_OUTPUT(f
->type
))
1724 set_src_registers(ctx
);
1726 set_dst_registers(ctx
);
1728 return set_srcdst_params(ctx
);
1731 static int __vpe_try_selection(struct vpe_ctx
*ctx
, struct v4l2_selection
*s
)
1733 struct vpe_q_data
*q_data
;
1736 if ((s
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) &&
1737 (s
->type
!= V4L2_BUF_TYPE_VIDEO_OUTPUT
))
1740 q_data
= get_q_data(ctx
, s
->type
);
1744 switch (s
->target
) {
1745 case V4L2_SEL_TGT_COMPOSE
:
1747 * COMPOSE target is only valid for capture buffer type, return
1748 * error for output buffer type
1750 if (s
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
)
1753 case V4L2_SEL_TGT_CROP
:
1755 * CROP target is only valid for output buffer type, return
1756 * error for capture buffer type
1758 if (s
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1762 * bound and default crop/compose targets are invalid targets to
1770 * For SEQ_TB buffers, crop height should be less than the height of
1771 * the field height, not the buffer height
1773 if (q_data
->flags
& Q_DATA_INTERLACED_SEQ_TB
)
1774 height
= q_data
->height
/ 2;
1776 height
= q_data
->height
;
1778 if (s
->r
.top
< 0 || s
->r
.left
< 0) {
1779 vpe_err(ctx
->dev
, "negative values for top and left\n");
1780 s
->r
.top
= s
->r
.left
= 0;
1783 v4l_bound_align_image(&s
->r
.width
, MIN_W
, q_data
->width
, 1,
1784 &s
->r
.height
, MIN_H
, height
, H_ALIGN
, S_ALIGN
);
1786 /* adjust left/top if cropping rectangle is out of bounds */
1787 if (s
->r
.left
+ s
->r
.width
> q_data
->width
)
1788 s
->r
.left
= q_data
->width
- s
->r
.width
;
1789 if (s
->r
.top
+ s
->r
.height
> q_data
->height
)
1790 s
->r
.top
= q_data
->height
- s
->r
.height
;
1795 static int vpe_g_selection(struct file
*file
, void *fh
,
1796 struct v4l2_selection
*s
)
1798 struct vpe_ctx
*ctx
= file2ctx(file
);
1799 struct vpe_q_data
*q_data
;
1800 bool use_c_rect
= false;
1802 if ((s
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) &&
1803 (s
->type
!= V4L2_BUF_TYPE_VIDEO_OUTPUT
))
1806 q_data
= get_q_data(ctx
, s
->type
);
1810 switch (s
->target
) {
1811 case V4L2_SEL_TGT_COMPOSE_DEFAULT
:
1812 case V4L2_SEL_TGT_COMPOSE_BOUNDS
:
1813 if (s
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
)
1816 case V4L2_SEL_TGT_CROP_BOUNDS
:
1817 case V4L2_SEL_TGT_CROP_DEFAULT
:
1818 if (s
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1821 case V4L2_SEL_TGT_COMPOSE
:
1822 if (s
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
)
1826 case V4L2_SEL_TGT_CROP
:
1827 if (s
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1837 * for CROP/COMPOSE target type, return c_rect params from the
1838 * respective buffer type
1840 s
->r
= q_data
->c_rect
;
1843 * for DEFAULT/BOUNDS target type, return width and height from
1844 * S_FMT of the respective buffer type
1848 s
->r
.width
= q_data
->width
;
1849 s
->r
.height
= q_data
->height
;
1856 static int vpe_s_selection(struct file
*file
, void *fh
,
1857 struct v4l2_selection
*s
)
1859 struct vpe_ctx
*ctx
= file2ctx(file
);
1860 struct vpe_q_data
*q_data
;
1861 struct v4l2_selection sel
= *s
;
1864 ret
= __vpe_try_selection(ctx
, &sel
);
1868 q_data
= get_q_data(ctx
, sel
.type
);
1872 if ((q_data
->c_rect
.left
== sel
.r
.left
) &&
1873 (q_data
->c_rect
.top
== sel
.r
.top
) &&
1874 (q_data
->c_rect
.width
== sel
.r
.width
) &&
1875 (q_data
->c_rect
.height
== sel
.r
.height
)) {
1877 "requested crop/compose values are already set\n");
1881 q_data
->c_rect
= sel
.r
;
1883 return set_srcdst_params(ctx
);
1887 * defines number of buffers/frames a context can process with VPE before
1888 * switching to a different context. default value is 1 buffer per context
1890 #define V4L2_CID_VPE_BUFS_PER_JOB (V4L2_CID_USER_TI_VPE_BASE + 0)
1892 static int vpe_s_ctrl(struct v4l2_ctrl
*ctrl
)
1894 struct vpe_ctx
*ctx
=
1895 container_of(ctrl
->handler
, struct vpe_ctx
, hdl
);
1898 case V4L2_CID_VPE_BUFS_PER_JOB
:
1899 ctx
->bufs_per_job
= ctrl
->val
;
1903 vpe_err(ctx
->dev
, "Invalid control\n");
1910 static const struct v4l2_ctrl_ops vpe_ctrl_ops
= {
1911 .s_ctrl
= vpe_s_ctrl
,
1914 static const struct v4l2_ioctl_ops vpe_ioctl_ops
= {
1915 .vidioc_querycap
= vpe_querycap
,
1917 .vidioc_enum_fmt_vid_cap_mplane
= vpe_enum_fmt
,
1918 .vidioc_g_fmt_vid_cap_mplane
= vpe_g_fmt
,
1919 .vidioc_try_fmt_vid_cap_mplane
= vpe_try_fmt
,
1920 .vidioc_s_fmt_vid_cap_mplane
= vpe_s_fmt
,
1922 .vidioc_enum_fmt_vid_out_mplane
= vpe_enum_fmt
,
1923 .vidioc_g_fmt_vid_out_mplane
= vpe_g_fmt
,
1924 .vidioc_try_fmt_vid_out_mplane
= vpe_try_fmt
,
1925 .vidioc_s_fmt_vid_out_mplane
= vpe_s_fmt
,
1927 .vidioc_g_selection
= vpe_g_selection
,
1928 .vidioc_s_selection
= vpe_s_selection
,
1930 .vidioc_reqbufs
= v4l2_m2m_ioctl_reqbufs
,
1931 .vidioc_querybuf
= v4l2_m2m_ioctl_querybuf
,
1932 .vidioc_qbuf
= v4l2_m2m_ioctl_qbuf
,
1933 .vidioc_dqbuf
= v4l2_m2m_ioctl_dqbuf
,
1934 .vidioc_streamon
= v4l2_m2m_ioctl_streamon
,
1935 .vidioc_streamoff
= v4l2_m2m_ioctl_streamoff
,
1937 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
1938 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
1944 static int vpe_queue_setup(struct vb2_queue
*vq
,
1945 unsigned int *nbuffers
, unsigned int *nplanes
,
1946 unsigned int sizes
[], struct device
*alloc_devs
[])
1949 struct vpe_ctx
*ctx
= vb2_get_drv_priv(vq
);
1950 struct vpe_q_data
*q_data
;
1952 q_data
= get_q_data(ctx
, vq
->type
);
1954 *nplanes
= q_data
->fmt
->coplanar
? 2 : 1;
1956 for (i
= 0; i
< *nplanes
; i
++)
1957 sizes
[i
] = q_data
->sizeimage
[i
];
1959 vpe_dbg(ctx
->dev
, "get %d buffer(s) of size %d", *nbuffers
,
1961 if (q_data
->fmt
->coplanar
)
1962 vpe_dbg(ctx
->dev
, " and %d\n", sizes
[VPE_CHROMA
]);
1967 static int vpe_buf_prepare(struct vb2_buffer
*vb
)
1969 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
1970 struct vpe_ctx
*ctx
= vb2_get_drv_priv(vb
->vb2_queue
);
1971 struct vpe_q_data
*q_data
;
1974 vpe_dbg(ctx
->dev
, "type: %d\n", vb
->vb2_queue
->type
);
1976 q_data
= get_q_data(ctx
, vb
->vb2_queue
->type
);
1977 num_planes
= q_data
->fmt
->coplanar
? 2 : 1;
1979 if (vb
->vb2_queue
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
1980 if (!(q_data
->flags
& Q_IS_INTERLACED
)) {
1981 vbuf
->field
= V4L2_FIELD_NONE
;
1983 if (vbuf
->field
!= V4L2_FIELD_TOP
&&
1984 vbuf
->field
!= V4L2_FIELD_BOTTOM
&&
1985 vbuf
->field
!= V4L2_FIELD_SEQ_TB
)
1990 for (i
= 0; i
< num_planes
; i
++) {
1991 if (vb2_plane_size(vb
, i
) < q_data
->sizeimage
[i
]) {
1993 "data will not fit into plane (%lu < %lu)\n",
1994 vb2_plane_size(vb
, i
),
1995 (long) q_data
->sizeimage
[i
]);
2000 for (i
= 0; i
< num_planes
; i
++)
2001 vb2_set_plane_payload(vb
, i
, q_data
->sizeimage
[i
]);
2006 static void vpe_buf_queue(struct vb2_buffer
*vb
)
2008 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
2009 struct vpe_ctx
*ctx
= vb2_get_drv_priv(vb
->vb2_queue
);
2011 v4l2_m2m_buf_queue(ctx
->fh
.m2m_ctx
, vbuf
);
2014 static int vpe_start_streaming(struct vb2_queue
*q
, unsigned int count
)
2016 struct vpe_ctx
*ctx
= vb2_get_drv_priv(q
);
2018 if (ctx
->deinterlacing
)
2019 config_edi_input_mode(ctx
, 0x0);
2021 if (ctx
->sequence
!= 0)
2022 set_srcdst_params(ctx
);
2027 static void vpe_stop_streaming(struct vb2_queue
*q
)
2029 struct vpe_ctx
*ctx
= vb2_get_drv_priv(q
);
2031 vpe_dump_regs(ctx
->dev
);
2032 vpdma_dump_regs(ctx
->dev
->vpdma
);
2035 static const struct vb2_ops vpe_qops
= {
2036 .queue_setup
= vpe_queue_setup
,
2037 .buf_prepare
= vpe_buf_prepare
,
2038 .buf_queue
= vpe_buf_queue
,
2039 .wait_prepare
= vb2_ops_wait_prepare
,
2040 .wait_finish
= vb2_ops_wait_finish
,
2041 .start_streaming
= vpe_start_streaming
,
2042 .stop_streaming
= vpe_stop_streaming
,
2045 static int queue_init(void *priv
, struct vb2_queue
*src_vq
,
2046 struct vb2_queue
*dst_vq
)
2048 struct vpe_ctx
*ctx
= priv
;
2049 struct vpe_dev
*dev
= ctx
->dev
;
2052 memset(src_vq
, 0, sizeof(*src_vq
));
2053 src_vq
->type
= V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
;
2054 src_vq
->io_modes
= VB2_MMAP
| VB2_DMABUF
;
2055 src_vq
->drv_priv
= ctx
;
2056 src_vq
->buf_struct_size
= sizeof(struct v4l2_m2m_buffer
);
2057 src_vq
->ops
= &vpe_qops
;
2058 src_vq
->mem_ops
= &vb2_dma_contig_memops
;
2059 src_vq
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_COPY
;
2060 src_vq
->lock
= &dev
->dev_mutex
;
2061 src_vq
->dev
= dev
->v4l2_dev
.dev
;
2063 ret
= vb2_queue_init(src_vq
);
2067 memset(dst_vq
, 0, sizeof(*dst_vq
));
2068 dst_vq
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
;
2069 dst_vq
->io_modes
= VB2_MMAP
| VB2_DMABUF
;
2070 dst_vq
->drv_priv
= ctx
;
2071 dst_vq
->buf_struct_size
= sizeof(struct v4l2_m2m_buffer
);
2072 dst_vq
->ops
= &vpe_qops
;
2073 dst_vq
->mem_ops
= &vb2_dma_contig_memops
;
2074 dst_vq
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_COPY
;
2075 dst_vq
->lock
= &dev
->dev_mutex
;
2076 dst_vq
->dev
= dev
->v4l2_dev
.dev
;
2078 return vb2_queue_init(dst_vq
);
2081 static const struct v4l2_ctrl_config vpe_bufs_per_job
= {
2082 .ops
= &vpe_ctrl_ops
,
2083 .id
= V4L2_CID_VPE_BUFS_PER_JOB
,
2084 .name
= "Buffers Per Transaction",
2085 .type
= V4L2_CTRL_TYPE_INTEGER
,
2086 .def
= VPE_DEF_BUFS_PER_JOB
,
2088 .max
= VIDEO_MAX_FRAME
,
2095 static int vpe_open(struct file
*file
)
2097 struct vpe_dev
*dev
= video_drvdata(file
);
2098 struct vpe_q_data
*s_q_data
;
2099 struct v4l2_ctrl_handler
*hdl
;
2100 struct vpe_ctx
*ctx
;
2103 vpe_dbg(dev
, "vpe_open\n");
2105 ctx
= kzalloc(sizeof(*ctx
), GFP_KERNEL
);
2111 if (mutex_lock_interruptible(&dev
->dev_mutex
)) {
2116 ret
= vpdma_create_desc_list(&ctx
->desc_list
, VPE_DESC_LIST_SIZE
,
2117 VPDMA_LIST_TYPE_NORMAL
);
2121 ret
= vpdma_alloc_desc_buf(&ctx
->mmr_adb
, sizeof(struct vpe_mmr_adb
));
2123 goto free_desc_list
;
2125 ret
= vpdma_alloc_desc_buf(&ctx
->sc_coeff_h
, SC_COEF_SRAM_SIZE
);
2129 ret
= vpdma_alloc_desc_buf(&ctx
->sc_coeff_v
, SC_COEF_SRAM_SIZE
);
2135 v4l2_fh_init(&ctx
->fh
, video_devdata(file
));
2136 file
->private_data
= &ctx
->fh
;
2139 v4l2_ctrl_handler_init(hdl
, 1);
2140 v4l2_ctrl_new_custom(hdl
, &vpe_bufs_per_job
, NULL
);
2145 ctx
->fh
.ctrl_handler
= hdl
;
2146 v4l2_ctrl_handler_setup(hdl
);
2148 s_q_data
= &ctx
->q_data
[Q_DATA_SRC
];
2149 s_q_data
->fmt
= &vpe_formats
[2];
2150 s_q_data
->width
= 1920;
2151 s_q_data
->height
= 1080;
2152 s_q_data
->bytesperline
[VPE_LUMA
] = (s_q_data
->width
*
2153 s_q_data
->fmt
->vpdma_fmt
[VPE_LUMA
]->depth
) >> 3;
2154 s_q_data
->sizeimage
[VPE_LUMA
] = (s_q_data
->bytesperline
[VPE_LUMA
] *
2156 s_q_data
->colorspace
= V4L2_COLORSPACE_REC709
;
2157 s_q_data
->field
= V4L2_FIELD_NONE
;
2158 s_q_data
->c_rect
.left
= 0;
2159 s_q_data
->c_rect
.top
= 0;
2160 s_q_data
->c_rect
.width
= s_q_data
->width
;
2161 s_q_data
->c_rect
.height
= s_q_data
->height
;
2162 s_q_data
->flags
= 0;
2164 ctx
->q_data
[Q_DATA_DST
] = *s_q_data
;
2166 set_dei_shadow_registers(ctx
);
2167 set_src_registers(ctx
);
2168 set_dst_registers(ctx
);
2169 ret
= set_srcdst_params(ctx
);
2173 ctx
->fh
.m2m_ctx
= v4l2_m2m_ctx_init(dev
->m2m_dev
, ctx
, &queue_init
);
2175 if (IS_ERR(ctx
->fh
.m2m_ctx
)) {
2176 ret
= PTR_ERR(ctx
->fh
.m2m_ctx
);
2180 v4l2_fh_add(&ctx
->fh
);
2183 * for now, just report the creation of the first instance, we can later
2184 * optimize the driver to enable or disable clocks when the first
2185 * instance is created or the last instance released
2187 if (atomic_inc_return(&dev
->num_instances
) == 1)
2188 vpe_dbg(dev
, "first instance created\n");
2190 ctx
->bufs_per_job
= VPE_DEF_BUFS_PER_JOB
;
2192 ctx
->load_mmrs
= true;
2194 vpe_dbg(dev
, "created instance %p, m2m_ctx: %p\n",
2195 ctx
, ctx
->fh
.m2m_ctx
);
2197 mutex_unlock(&dev
->dev_mutex
);
2201 v4l2_ctrl_handler_free(hdl
);
2202 v4l2_fh_exit(&ctx
->fh
);
2203 vpdma_free_desc_buf(&ctx
->sc_coeff_v
);
2205 vpdma_free_desc_buf(&ctx
->sc_coeff_h
);
2207 vpdma_free_desc_buf(&ctx
->mmr_adb
);
2209 vpdma_free_desc_list(&ctx
->desc_list
);
2211 mutex_unlock(&dev
->dev_mutex
);
2217 static int vpe_release(struct file
*file
)
2219 struct vpe_dev
*dev
= video_drvdata(file
);
2220 struct vpe_ctx
*ctx
= file2ctx(file
);
2222 vpe_dbg(dev
, "releasing instance %p\n", ctx
);
2224 mutex_lock(&dev
->dev_mutex
);
2226 free_mv_buffers(ctx
);
2227 vpdma_free_desc_list(&ctx
->desc_list
);
2228 vpdma_free_desc_buf(&ctx
->mmr_adb
);
2230 vpdma_free_desc_buf(&ctx
->sc_coeff_v
);
2231 vpdma_free_desc_buf(&ctx
->sc_coeff_h
);
2233 v4l2_fh_del(&ctx
->fh
);
2234 v4l2_fh_exit(&ctx
->fh
);
2235 v4l2_ctrl_handler_free(&ctx
->hdl
);
2236 v4l2_m2m_ctx_release(ctx
->fh
.m2m_ctx
);
2241 * for now, just report the release of the last instance, we can later
2242 * optimize the driver to enable or disable clocks when the first
2243 * instance is created or the last instance released
2245 if (atomic_dec_return(&dev
->num_instances
) == 0)
2246 vpe_dbg(dev
, "last instance released\n");
2248 mutex_unlock(&dev
->dev_mutex
);
2253 static const struct v4l2_file_operations vpe_fops
= {
2254 .owner
= THIS_MODULE
,
2256 .release
= vpe_release
,
2257 .poll
= v4l2_m2m_fop_poll
,
2258 .unlocked_ioctl
= video_ioctl2
,
2259 .mmap
= v4l2_m2m_fop_mmap
,
2262 static struct video_device vpe_videodev
= {
2263 .name
= VPE_MODULE_NAME
,
2265 .ioctl_ops
= &vpe_ioctl_ops
,
2267 .release
= video_device_release_empty
,
2268 .vfl_dir
= VFL_DIR_M2M
,
2271 static struct v4l2_m2m_ops m2m_ops
= {
2272 .device_run
= device_run
,
2273 .job_ready
= job_ready
,
2274 .job_abort
= job_abort
,
2276 .unlock
= vpe_unlock
,
2279 static int vpe_runtime_get(struct platform_device
*pdev
)
2283 dev_dbg(&pdev
->dev
, "vpe_runtime_get\n");
2285 r
= pm_runtime_get_sync(&pdev
->dev
);
2287 return r
< 0 ? r
: 0;
2290 static void vpe_runtime_put(struct platform_device
*pdev
)
2295 dev_dbg(&pdev
->dev
, "vpe_runtime_put\n");
2297 r
= pm_runtime_put_sync(&pdev
->dev
);
2298 WARN_ON(r
< 0 && r
!= -ENOSYS
);
2301 static void vpe_fw_cb(struct platform_device
*pdev
)
2303 struct vpe_dev
*dev
= platform_get_drvdata(pdev
);
2304 struct video_device
*vfd
;
2308 *vfd
= vpe_videodev
;
2309 vfd
->lock
= &dev
->dev_mutex
;
2310 vfd
->v4l2_dev
= &dev
->v4l2_dev
;
2312 ret
= video_register_device(vfd
, VFL_TYPE_GRABBER
, 0);
2314 vpe_err(dev
, "Failed to register video device\n");
2316 vpe_set_clock_enable(dev
, 0);
2317 vpe_runtime_put(pdev
);
2318 pm_runtime_disable(&pdev
->dev
);
2319 v4l2_m2m_release(dev
->m2m_dev
);
2320 v4l2_device_unregister(&dev
->v4l2_dev
);
2325 video_set_drvdata(vfd
, dev
);
2326 snprintf(vfd
->name
, sizeof(vfd
->name
), "%s", vpe_videodev
.name
);
2327 dev_info(dev
->v4l2_dev
.dev
, "Device registered as /dev/video%d\n",
2331 static int vpe_probe(struct platform_device
*pdev
)
2333 struct vpe_dev
*dev
;
2336 dev
= devm_kzalloc(&pdev
->dev
, sizeof(*dev
), GFP_KERNEL
);
2340 spin_lock_init(&dev
->lock
);
2342 ret
= v4l2_device_register(&pdev
->dev
, &dev
->v4l2_dev
);
2346 atomic_set(&dev
->num_instances
, 0);
2347 mutex_init(&dev
->dev_mutex
);
2349 dev
->res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
,
2352 * HACK: we get resource info from device tree in the form of a list of
2353 * VPE sub blocks, the driver currently uses only the base of vpe_top
2354 * for register access, the driver should be changed later to access
2355 * registers based on the sub block base addresses
2357 dev
->base
= devm_ioremap(&pdev
->dev
, dev
->res
->start
, SZ_32K
);
2360 goto v4l2_dev_unreg
;
2363 irq
= platform_get_irq(pdev
, 0);
2364 ret
= devm_request_irq(&pdev
->dev
, irq
, vpe_irq
, 0, VPE_MODULE_NAME
,
2367 goto v4l2_dev_unreg
;
2369 platform_set_drvdata(pdev
, dev
);
2371 dev
->m2m_dev
= v4l2_m2m_init(&m2m_ops
);
2372 if (IS_ERR(dev
->m2m_dev
)) {
2373 vpe_err(dev
, "Failed to init mem2mem device\n");
2374 ret
= PTR_ERR(dev
->m2m_dev
);
2375 goto v4l2_dev_unreg
;
2378 pm_runtime_enable(&pdev
->dev
);
2380 ret
= vpe_runtime_get(pdev
);
2384 /* Perform clk enable followed by reset */
2385 vpe_set_clock_enable(dev
, 1);
2389 func
= read_field_reg(dev
, VPE_PID
, VPE_PID_FUNC_MASK
,
2390 VPE_PID_FUNC_SHIFT
);
2391 vpe_dbg(dev
, "VPE PID function %x\n", func
);
2393 vpe_top_vpdma_reset(dev
);
2395 dev
->sc
= sc_create(pdev
);
2396 if (IS_ERR(dev
->sc
)) {
2397 ret
= PTR_ERR(dev
->sc
);
2401 dev
->csc
= csc_create(pdev
);
2402 if (IS_ERR(dev
->csc
)) {
2403 ret
= PTR_ERR(dev
->csc
);
2407 dev
->vpdma
= vpdma_create(pdev
, vpe_fw_cb
);
2408 if (IS_ERR(dev
->vpdma
)) {
2409 ret
= PTR_ERR(dev
->vpdma
);
2416 vpe_runtime_put(pdev
);
2418 pm_runtime_disable(&pdev
->dev
);
2419 v4l2_m2m_release(dev
->m2m_dev
);
2421 v4l2_device_unregister(&dev
->v4l2_dev
);
2426 static int vpe_remove(struct platform_device
*pdev
)
2428 struct vpe_dev
*dev
= platform_get_drvdata(pdev
);
2430 v4l2_info(&dev
->v4l2_dev
, "Removing " VPE_MODULE_NAME
);
2432 v4l2_m2m_release(dev
->m2m_dev
);
2433 video_unregister_device(&dev
->vfd
);
2434 v4l2_device_unregister(&dev
->v4l2_dev
);
2436 vpe_set_clock_enable(dev
, 0);
2437 vpe_runtime_put(pdev
);
2438 pm_runtime_disable(&pdev
->dev
);
2443 #if defined(CONFIG_OF)
2444 static const struct of_device_id vpe_of_match
[] = {
2446 .compatible
= "ti,vpe",
2452 static struct platform_driver vpe_pdrv
= {
2454 .remove
= vpe_remove
,
2456 .name
= VPE_MODULE_NAME
,
2457 .of_match_table
= of_match_ptr(vpe_of_match
),
2461 module_platform_driver(vpe_pdrv
);
2463 MODULE_DESCRIPTION("TI VPE driver");
2464 MODULE_AUTHOR("Dale Farnsworth, <dale@farnsworth.org>");
2465 MODULE_LICENSE("GPL");