1*3bed4220SNeil Armstrong // SPDX-License-Identifier: GPL-2.0
2*3bed4220SNeil Armstrong /*
3*3bed4220SNeil Armstrong * Amlogic Meson Video Processing Unit driver
4*3bed4220SNeil Armstrong *
5*3bed4220SNeil Armstrong * Copyright (c) 2018 BayLibre, SAS.
6*3bed4220SNeil Armstrong * Author: Neil Armstrong <narmstrong@baylibre.com>
7*3bed4220SNeil Armstrong */
8*3bed4220SNeil Armstrong
9*3bed4220SNeil Armstrong #include "meson_vpu.h"
10*3bed4220SNeil Armstrong
11*3bed4220SNeil Armstrong /* OSDx_BLKx_CFG */
12*3bed4220SNeil Armstrong #define OSD_CANVAS_SEL 16
13*3bed4220SNeil Armstrong
14*3bed4220SNeil Armstrong #define OSD_ENDIANNESS_LE BIT(15)
15*3bed4220SNeil Armstrong #define OSD_ENDIANNESS_BE (0)
16*3bed4220SNeil Armstrong
17*3bed4220SNeil Armstrong #define OSD_BLK_MODE_422 (0x03 << 8)
18*3bed4220SNeil Armstrong #define OSD_BLK_MODE_16 (0x04 << 8)
19*3bed4220SNeil Armstrong #define OSD_BLK_MODE_32 (0x05 << 8)
20*3bed4220SNeil Armstrong #define OSD_BLK_MODE_24 (0x07 << 8)
21*3bed4220SNeil Armstrong
22*3bed4220SNeil Armstrong #define OSD_OUTPUT_COLOR_RGB BIT(7)
23*3bed4220SNeil Armstrong #define OSD_OUTPUT_COLOR_YUV (0)
24*3bed4220SNeil Armstrong
25*3bed4220SNeil Armstrong #define OSD_COLOR_MATRIX_32_RGBA (0x00 << 2)
26*3bed4220SNeil Armstrong #define OSD_COLOR_MATRIX_32_ARGB (0x01 << 2)
27*3bed4220SNeil Armstrong #define OSD_COLOR_MATRIX_32_ABGR (0x02 << 2)
28*3bed4220SNeil Armstrong #define OSD_COLOR_MATRIX_32_BGRA (0x03 << 2)
29*3bed4220SNeil Armstrong
30*3bed4220SNeil Armstrong #define OSD_COLOR_MATRIX_24_RGB (0x00 << 2)
31*3bed4220SNeil Armstrong
32*3bed4220SNeil Armstrong #define OSD_COLOR_MATRIX_16_RGB655 (0x00 << 2)
33*3bed4220SNeil Armstrong #define OSD_COLOR_MATRIX_16_RGB565 (0x04 << 2)
34*3bed4220SNeil Armstrong
35*3bed4220SNeil Armstrong #define OSD_INTERLACE_ENABLED BIT(1)
36*3bed4220SNeil Armstrong #define OSD_INTERLACE_ODD BIT(0)
37*3bed4220SNeil Armstrong #define OSD_INTERLACE_EVEN (0)
38*3bed4220SNeil Armstrong
39*3bed4220SNeil Armstrong /* OSDx_CTRL_STAT */
40*3bed4220SNeil Armstrong #define OSD_ENABLE BIT(21)
41*3bed4220SNeil Armstrong #define OSD_BLK0_ENABLE BIT(0)
42*3bed4220SNeil Armstrong
43*3bed4220SNeil Armstrong #define OSD_GLOBAL_ALPHA_SHIFT 12
44*3bed4220SNeil Armstrong
45*3bed4220SNeil Armstrong /* OSDx_CTRL_STAT2 */
46*3bed4220SNeil Armstrong #define OSD_REPLACE_EN BIT(14)
47*3bed4220SNeil Armstrong #define OSD_REPLACE_SHIFT 6
48*3bed4220SNeil Armstrong
49*3bed4220SNeil Armstrong /*
50*3bed4220SNeil Armstrong * When the output is interlaced, the OSD must switch between
51*3bed4220SNeil Armstrong * each field using the INTERLACE_SEL_ODD (0) of VIU_OSD1_BLK0_CFG_W0
52*3bed4220SNeil Armstrong * at each vsync.
53*3bed4220SNeil Armstrong * But the vertical scaler can provide such funtionnality if
54*3bed4220SNeil Armstrong * is configured for 2:1 scaling with interlace options enabled.
55*3bed4220SNeil Armstrong */
meson_vpp_setup_interlace_vscaler_osd1(struct meson_vpu_priv * priv,struct video_priv * uc_priv)56*3bed4220SNeil Armstrong static void meson_vpp_setup_interlace_vscaler_osd1(struct meson_vpu_priv *priv,
57*3bed4220SNeil Armstrong struct video_priv *uc_priv)
58*3bed4220SNeil Armstrong {
59*3bed4220SNeil Armstrong writel(BIT(3) /* Enable scaler */ |
60*3bed4220SNeil Armstrong BIT(2), /* Select OSD1 */
61*3bed4220SNeil Armstrong priv->io_base + _REG(VPP_OSD_SC_CTRL0));
62*3bed4220SNeil Armstrong
63*3bed4220SNeil Armstrong writel(((uc_priv->xsize - 1) << 16) | (uc_priv->ysize - 1),
64*3bed4220SNeil Armstrong priv->io_base + _REG(VPP_OSD_SCI_WH_M1));
65*3bed4220SNeil Armstrong /* 2:1 scaling */
66*3bed4220SNeil Armstrong writel((0 << 16) | uc_priv->xsize,
67*3bed4220SNeil Armstrong priv->io_base + _REG(VPP_OSD_SCO_H_START_END));
68*3bed4220SNeil Armstrong writel(((0 >> 1) << 16) | (uc_priv->ysize >> 1),
69*3bed4220SNeil Armstrong priv->io_base + _REG(VPP_OSD_SCO_V_START_END));
70*3bed4220SNeil Armstrong
71*3bed4220SNeil Armstrong /* 2:1 scaling values */
72*3bed4220SNeil Armstrong writel(BIT(16), priv->io_base + _REG(VPP_OSD_VSC_INI_PHASE));
73*3bed4220SNeil Armstrong writel(BIT(25), priv->io_base + _REG(VPP_OSD_VSC_PHASE_STEP));
74*3bed4220SNeil Armstrong
75*3bed4220SNeil Armstrong writel(0, priv->io_base + _REG(VPP_OSD_HSC_CTRL0));
76*3bed4220SNeil Armstrong
77*3bed4220SNeil Armstrong writel((4 << 0) /* osd_vsc_bank_length */ |
78*3bed4220SNeil Armstrong (4 << 3) /* osd_vsc_top_ini_rcv_num0 */ |
79*3bed4220SNeil Armstrong (1 << 8) /* osd_vsc_top_rpt_p0_num0 */ |
80*3bed4220SNeil Armstrong (6 << 11) /* osd_vsc_bot_ini_rcv_num0 */ |
81*3bed4220SNeil Armstrong (2 << 16) /* osd_vsc_bot_rpt_p0_num0 */ |
82*3bed4220SNeil Armstrong BIT(23) /* osd_prog_interlace */ |
83*3bed4220SNeil Armstrong BIT(24), /* Enable vertical scaler */
84*3bed4220SNeil Armstrong priv->io_base + _REG(VPP_OSD_VSC_CTRL0));
85*3bed4220SNeil Armstrong }
86*3bed4220SNeil Armstrong
87*3bed4220SNeil Armstrong static void
meson_vpp_disable_interlace_vscaler_osd1(struct meson_vpu_priv * priv)88*3bed4220SNeil Armstrong meson_vpp_disable_interlace_vscaler_osd1(struct meson_vpu_priv *priv)
89*3bed4220SNeil Armstrong {
90*3bed4220SNeil Armstrong writel(0, priv->io_base + _REG(VPP_OSD_SC_CTRL0));
91*3bed4220SNeil Armstrong writel(0, priv->io_base + _REG(VPP_OSD_VSC_CTRL0));
92*3bed4220SNeil Armstrong writel(0, priv->io_base + _REG(VPP_OSD_HSC_CTRL0));
93*3bed4220SNeil Armstrong }
94*3bed4220SNeil Armstrong
meson_vpu_setup_plane(struct udevice * dev,bool is_interlaced)95*3bed4220SNeil Armstrong void meson_vpu_setup_plane(struct udevice *dev, bool is_interlaced)
96*3bed4220SNeil Armstrong {
97*3bed4220SNeil Armstrong struct video_uc_platdata *uc_plat = dev_get_uclass_platdata(dev);
98*3bed4220SNeil Armstrong struct video_priv *uc_priv = dev_get_uclass_priv(dev);
99*3bed4220SNeil Armstrong struct meson_vpu_priv *priv = dev_get_priv(dev);
100*3bed4220SNeil Armstrong u32 osd1_ctrl_stat;
101*3bed4220SNeil Armstrong u32 osd1_blk0_cfg[5];
102*3bed4220SNeil Armstrong bool osd1_interlace;
103*3bed4220SNeil Armstrong unsigned int src_x1, src_x2, src_y1, src_y2;
104*3bed4220SNeil Armstrong unsigned int dest_x1, dest_x2, dest_y1, dest_y2;
105*3bed4220SNeil Armstrong
106*3bed4220SNeil Armstrong dest_x1 = src_x1 = 0;
107*3bed4220SNeil Armstrong dest_x2 = src_x2 = uc_priv->xsize;
108*3bed4220SNeil Armstrong dest_y1 = src_y1 = 0;
109*3bed4220SNeil Armstrong dest_y2 = src_y2 = uc_priv->ysize;
110*3bed4220SNeil Armstrong
111*3bed4220SNeil Armstrong /* Enable VPP Postblend */
112*3bed4220SNeil Armstrong writel(uc_priv->xsize,
113*3bed4220SNeil Armstrong priv->io_base + _REG(VPP_POSTBLEND_H_SIZE));
114*3bed4220SNeil Armstrong
115*3bed4220SNeil Armstrong writel_bits(VPP_POSTBLEND_ENABLE, VPP_POSTBLEND_ENABLE,
116*3bed4220SNeil Armstrong priv->io_base + _REG(VPP_MISC));
117*3bed4220SNeil Armstrong
118*3bed4220SNeil Armstrong /* uc_plat->base is the framebuffer */
119*3bed4220SNeil Armstrong
120*3bed4220SNeil Armstrong /* Enable OSD and BLK0, set max global alpha */
121*3bed4220SNeil Armstrong osd1_ctrl_stat = OSD_ENABLE | (0xFF << OSD_GLOBAL_ALPHA_SHIFT) |
122*3bed4220SNeil Armstrong OSD_BLK0_ENABLE;
123*3bed4220SNeil Armstrong
124*3bed4220SNeil Armstrong /* Set up BLK0 to point to the right canvas */
125*3bed4220SNeil Armstrong osd1_blk0_cfg[0] = ((MESON_CANVAS_ID_OSD1 << OSD_CANVAS_SEL) |
126*3bed4220SNeil Armstrong OSD_ENDIANNESS_LE);
127*3bed4220SNeil Armstrong
128*3bed4220SNeil Armstrong /* On GXBB, Use the old non-HDR RGB2YUV converter */
129*3bed4220SNeil Armstrong if (meson_vpu_is_compatible(priv, VPU_COMPATIBLE_GXBB))
130*3bed4220SNeil Armstrong osd1_blk0_cfg[0] |= OSD_OUTPUT_COLOR_RGB;
131*3bed4220SNeil Armstrong
132*3bed4220SNeil Armstrong /* For XRGB, replace the pixel's alpha by 0xFF */
133*3bed4220SNeil Armstrong writel_bits(OSD_REPLACE_EN, OSD_REPLACE_EN,
134*3bed4220SNeil Armstrong priv->io_base + _REG(VIU_OSD1_CTRL_STAT2));
135*3bed4220SNeil Armstrong osd1_blk0_cfg[0] |= OSD_BLK_MODE_32 |
136*3bed4220SNeil Armstrong OSD_COLOR_MATRIX_32_ARGB;
137*3bed4220SNeil Armstrong
138*3bed4220SNeil Armstrong if (is_interlaced) {
139*3bed4220SNeil Armstrong osd1_interlace = true;
140*3bed4220SNeil Armstrong dest_y1 /= 2;
141*3bed4220SNeil Armstrong dest_y2 /= 2;
142*3bed4220SNeil Armstrong } else {
143*3bed4220SNeil Armstrong osd1_interlace = false;
144*3bed4220SNeil Armstrong }
145*3bed4220SNeil Armstrong
146*3bed4220SNeil Armstrong /*
147*3bed4220SNeil Armstrong * The format of these registers is (x2 << 16 | x1),
148*3bed4220SNeil Armstrong * where x2 is exclusive.
149*3bed4220SNeil Armstrong * e.g. +30x1920 would be (1919 << 16) | 30
150*3bed4220SNeil Armstrong */
151*3bed4220SNeil Armstrong osd1_blk0_cfg[1] = ((src_x2 - 1) << 16) | src_x1;
152*3bed4220SNeil Armstrong osd1_blk0_cfg[2] = ((src_y2 - 1) << 16) | src_y1;
153*3bed4220SNeil Armstrong osd1_blk0_cfg[3] = ((dest_x2 - 1) << 16) | dest_x1;
154*3bed4220SNeil Armstrong osd1_blk0_cfg[4] = ((dest_y2 - 1) << 16) | dest_y1;
155*3bed4220SNeil Armstrong
156*3bed4220SNeil Armstrong writel(osd1_ctrl_stat, priv->io_base + _REG(VIU_OSD1_CTRL_STAT));
157*3bed4220SNeil Armstrong writel(osd1_blk0_cfg[0], priv->io_base + _REG(VIU_OSD1_BLK0_CFG_W0));
158*3bed4220SNeil Armstrong writel(osd1_blk0_cfg[1], priv->io_base + _REG(VIU_OSD1_BLK0_CFG_W1));
159*3bed4220SNeil Armstrong writel(osd1_blk0_cfg[2], priv->io_base + _REG(VIU_OSD1_BLK0_CFG_W2));
160*3bed4220SNeil Armstrong writel(osd1_blk0_cfg[3], priv->io_base + _REG(VIU_OSD1_BLK0_CFG_W3));
161*3bed4220SNeil Armstrong writel(osd1_blk0_cfg[4], priv->io_base + _REG(VIU_OSD1_BLK0_CFG_W4));
162*3bed4220SNeil Armstrong
163*3bed4220SNeil Armstrong /* If output is interlace, make use of the Scaler */
164*3bed4220SNeil Armstrong if (osd1_interlace)
165*3bed4220SNeil Armstrong meson_vpp_setup_interlace_vscaler_osd1(priv, uc_priv);
166*3bed4220SNeil Armstrong else
167*3bed4220SNeil Armstrong meson_vpp_disable_interlace_vscaler_osd1(priv);
168*3bed4220SNeil Armstrong
169*3bed4220SNeil Armstrong meson_canvas_setup(priv, MESON_CANVAS_ID_OSD1,
170*3bed4220SNeil Armstrong uc_plat->base, uc_priv->xsize * 4,
171*3bed4220SNeil Armstrong uc_priv->ysize, MESON_CANVAS_WRAP_NONE,
172*3bed4220SNeil Armstrong MESON_CANVAS_BLKMODE_LINEAR);
173*3bed4220SNeil Armstrong
174*3bed4220SNeil Armstrong /* Enable OSD1 */
175*3bed4220SNeil Armstrong writel_bits(VPP_OSD1_POSTBLEND, VPP_OSD1_POSTBLEND,
176*3bed4220SNeil Armstrong priv->io_base + _REG(VPP_MISC));
177*3bed4220SNeil Armstrong }
178