1*2ec9c210SAngeloGioacchino Del Regno // SPDX-License-Identifier: GPL-2.0
2*2ec9c210SAngeloGioacchino Del Regno /*
3*2ec9c210SAngeloGioacchino Del Regno * Copyright (c) 2019 MediaTek Inc.
4*2ec9c210SAngeloGioacchino Del Regno * Author: jitao.shi <jitao.shi@mediatek.com>
5*2ec9c210SAngeloGioacchino Del Regno *
6*2ec9c210SAngeloGioacchino Del Regno * Copyright (c) 2026 Collabora Ltd.
7*2ec9c210SAngeloGioacchino Del Regno * AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>
8*2ec9c210SAngeloGioacchino Del Regno */
9*2ec9c210SAngeloGioacchino Del Regno
10*2ec9c210SAngeloGioacchino Del Regno #include "phy-mtk-io.h"
11*2ec9c210SAngeloGioacchino Del Regno #include "phy-mtk-mipi-dsi.h"
12*2ec9c210SAngeloGioacchino Del Regno
13*2ec9c210SAngeloGioacchino Del Regno #define MIPITX_LANE_CON 0x0004
14*2ec9c210SAngeloGioacchino Del Regno #define RG_DSI_CPHY_T1DRV_EN BIT(0)
15*2ec9c210SAngeloGioacchino Del Regno #define RG_DSI_ANA_CK_SEL BIT(1)
16*2ec9c210SAngeloGioacchino Del Regno #define RG_DSI_PHY_CK_SEL BIT(2)
17*2ec9c210SAngeloGioacchino Del Regno #define RG_DSI_CPHY_EN BIT(3)
18*2ec9c210SAngeloGioacchino Del Regno #define RG_DSI_PHYCK_INV_EN BIT(4)
19*2ec9c210SAngeloGioacchino Del Regno #define RG_DSI_PWR04_EN BIT(5)
20*2ec9c210SAngeloGioacchino Del Regno #define RG_DSI_BG_LPF_EN BIT(6)
21*2ec9c210SAngeloGioacchino Del Regno #define RG_DSI_BG_CORE_EN BIT(7)
22*2ec9c210SAngeloGioacchino Del Regno #define RG_DSI_PAD_TIEL_SEL BIT(8)
23*2ec9c210SAngeloGioacchino Del Regno
24*2ec9c210SAngeloGioacchino Del Regno #define MIPITX_VOLTAGE_SEL 0x0008
25*2ec9c210SAngeloGioacchino Del Regno #define RG_DSI_HSTX_LDO_REF_SEL GENMASK(9, 6)
26*2ec9c210SAngeloGioacchino Del Regno #define RG_DSI_PRD_REF_SEL GENMASK(5, 0)
27*2ec9c210SAngeloGioacchino Del Regno #define RG_DSI_PRD_REF_MINI 0
28*2ec9c210SAngeloGioacchino Del Regno #define RG_DSI_PRD_REF_DEF 4
29*2ec9c210SAngeloGioacchino Del Regno #define RG_DSI_PRD_REF_MAX 7
30*2ec9c210SAngeloGioacchino Del Regno
31*2ec9c210SAngeloGioacchino Del Regno #define MIPITX_PRESERVED 0x000c
32*2ec9c210SAngeloGioacchino Del Regno #define MIPITX_PRESERVED_DEF 0xffff0040
33*2ec9c210SAngeloGioacchino Del Regno #define MIPITX_PRESERVED_MINI 0xffff00f0
34*2ec9c210SAngeloGioacchino Del Regno
35*2ec9c210SAngeloGioacchino Del Regno #define MIPITX_PLL_PWR 0x0028
36*2ec9c210SAngeloGioacchino Del Regno #define AD_DSI_PLL_SDM_PWR_ON BIT(0)
37*2ec9c210SAngeloGioacchino Del Regno #define AD_DSI_PLL_SDM_ISO_EN BIT(1)
38*2ec9c210SAngeloGioacchino Del Regno #define MIPITX_PLL_CON0 0x002c
39*2ec9c210SAngeloGioacchino Del Regno #define MIPITX_PLL_CON1 0x0030
40*2ec9c210SAngeloGioacchino Del Regno #define RG_DSI_PLL_EN BIT(0)
41*2ec9c210SAngeloGioacchino Del Regno #define RG_DSI_PLL_POSDIV GENMASK(10, 8)
42*2ec9c210SAngeloGioacchino Del Regno #define MIPITX_PLL_CON2 0x0034
43*2ec9c210SAngeloGioacchino Del Regno #define MIPITX_PLL_CON3 0x0038
44*2ec9c210SAngeloGioacchino Del Regno #define MIPITX_PLL_CON4 0x003c
45*2ec9c210SAngeloGioacchino Del Regno #define RG_DSI_PLL_IBIAS GENMASK(11, 10)
46*2ec9c210SAngeloGioacchino Del Regno
47*2ec9c210SAngeloGioacchino Del Regno #define MIPITX_D2_SW_CTL_EN 0x015c
48*2ec9c210SAngeloGioacchino Del Regno #define MIPITX_D0_SW_CTL_EN 0x025c
49*2ec9c210SAngeloGioacchino Del Regno #define MIPITX_CK_CKMODE_EN 0x0320
50*2ec9c210SAngeloGioacchino Del Regno #define DSI_CK_CKMODE_EN BIT(0)
51*2ec9c210SAngeloGioacchino Del Regno #define MIPITX_CK_SW_CTL_EN 0x035c
52*2ec9c210SAngeloGioacchino Del Regno #define MIPITX_D1_SW_CTL_EN 0x045c
53*2ec9c210SAngeloGioacchino Del Regno #define MIPITX_D3_SW_CTL_EN 0x055c
54*2ec9c210SAngeloGioacchino Del Regno #define DSI_SW_CTL_EN BIT(0)
55*2ec9c210SAngeloGioacchino Del Regno
56*2ec9c210SAngeloGioacchino Del Regno #define DSI_PHY_XTAL_CLK_HZ 26000000
57*2ec9c210SAngeloGioacchino Del Regno #define DSI_PHY_PLL_MIN_RATE_HZ 125000000
58*2ec9c210SAngeloGioacchino Del Regno #define DSI_PHY_PLL_MAX_RATE_HZ 2000000000
59*2ec9c210SAngeloGioacchino Del Regno
mtk_mipi_tx_pll_enable(struct clk_hw * hw)60*2ec9c210SAngeloGioacchino Del Regno static int mtk_mipi_tx_pll_enable(struct clk_hw *hw)
61*2ec9c210SAngeloGioacchino Del Regno {
62*2ec9c210SAngeloGioacchino Del Regno struct mtk_mipi_tx *mipi_tx = mtk_mipi_tx_from_clk_hw(hw);
63*2ec9c210SAngeloGioacchino Del Regno void __iomem *base = mipi_tx->regs;
64*2ec9c210SAngeloGioacchino Del Regno u32 voltage = RG_DSI_PRD_REF_MINI;
65*2ec9c210SAngeloGioacchino Del Regno u32 pres = MIPITX_PRESERVED_MINI;
66*2ec9c210SAngeloGioacchino Del Regno unsigned long long pcw_calc;
67*2ec9c210SAngeloGioacchino Del Regno unsigned int txdiv, txdiv0;
68*2ec9c210SAngeloGioacchino Del Regno u32 pcw;
69*2ec9c210SAngeloGioacchino Del Regno
70*2ec9c210SAngeloGioacchino Del Regno dev_dbg(mipi_tx->dev, "enable: %u bps\n", mipi_tx->data_rate);
71*2ec9c210SAngeloGioacchino Del Regno
72*2ec9c210SAngeloGioacchino Del Regno if (mipi_tx->data_rate >= DSI_PHY_PLL_MAX_RATE_HZ) {
73*2ec9c210SAngeloGioacchino Del Regno /* Select higher signaling voltage for fast data rates */
74*2ec9c210SAngeloGioacchino Del Regno voltage = RG_DSI_PRD_REF_DEF;
75*2ec9c210SAngeloGioacchino Del Regno pres = MIPITX_PRESERVED_DEF;
76*2ec9c210SAngeloGioacchino Del Regno txdiv = 1;
77*2ec9c210SAngeloGioacchino Del Regno txdiv0 = 0;
78*2ec9c210SAngeloGioacchino Del Regno } else if (mipi_tx->data_rate >= 1000000000) {
79*2ec9c210SAngeloGioacchino Del Regno txdiv = 2;
80*2ec9c210SAngeloGioacchino Del Regno txdiv0 = 1;
81*2ec9c210SAngeloGioacchino Del Regno } else if (mipi_tx->data_rate >= 500000000) {
82*2ec9c210SAngeloGioacchino Del Regno txdiv = 4;
83*2ec9c210SAngeloGioacchino Del Regno txdiv0 = 2;
84*2ec9c210SAngeloGioacchino Del Regno } else if (mipi_tx->data_rate > 250000000) {
85*2ec9c210SAngeloGioacchino Del Regno txdiv = 8;
86*2ec9c210SAngeloGioacchino Del Regno txdiv0 = 3;
87*2ec9c210SAngeloGioacchino Del Regno } else if (mipi_tx->data_rate >= 125000000) {
88*2ec9c210SAngeloGioacchino Del Regno txdiv = 16;
89*2ec9c210SAngeloGioacchino Del Regno txdiv0 = 4;
90*2ec9c210SAngeloGioacchino Del Regno } else {
91*2ec9c210SAngeloGioacchino Del Regno return -EINVAL;
92*2ec9c210SAngeloGioacchino Del Regno }
93*2ec9c210SAngeloGioacchino Del Regno
94*2ec9c210SAngeloGioacchino Del Regno pcw_calc = ((u64)(mipi_tx->data_rate / 2) * txdiv) << 24;
95*2ec9c210SAngeloGioacchino Del Regno pcw_calc = div_u64(pcw_calc, DSI_PHY_XTAL_CLK_HZ);
96*2ec9c210SAngeloGioacchino Del Regno
97*2ec9c210SAngeloGioacchino Del Regno if (pcw_calc > U32_MAX) {
98*2ec9c210SAngeloGioacchino Del Regno dev_err(mipi_tx->dev, "Calculated PCW=%llu overflow!\n", pcw_calc);
99*2ec9c210SAngeloGioacchino Del Regno return -EINVAL;
100*2ec9c210SAngeloGioacchino Del Regno }
101*2ec9c210SAngeloGioacchino Del Regno pcw = (u32)pcw_calc;
102*2ec9c210SAngeloGioacchino Del Regno
103*2ec9c210SAngeloGioacchino Del Regno mtk_phy_update_field(base + MIPITX_VOLTAGE_SEL, RG_DSI_PRD_REF_SEL, voltage);
104*2ec9c210SAngeloGioacchino Del Regno writel(pres, base + MIPITX_PRESERVED);
105*2ec9c210SAngeloGioacchino Del Regno
106*2ec9c210SAngeloGioacchino Del Regno /* Enable the SDM and wait for power to stabilize */
107*2ec9c210SAngeloGioacchino Del Regno mtk_phy_set_bits(base + MIPITX_PLL_PWR, AD_DSI_PLL_SDM_PWR_ON);
108*2ec9c210SAngeloGioacchino Del Regno mtk_phy_clear_bits(base + MIPITX_PLL_CON1, RG_DSI_PLL_EN);
109*2ec9c210SAngeloGioacchino Del Regno udelay(30);
110*2ec9c210SAngeloGioacchino Del Regno
111*2ec9c210SAngeloGioacchino Del Regno /* Disable isolation and program PLL's PCW and dividers */
112*2ec9c210SAngeloGioacchino Del Regno mtk_phy_clear_bits(base + MIPITX_PLL_PWR, AD_DSI_PLL_SDM_ISO_EN);
113*2ec9c210SAngeloGioacchino Del Regno writel(pcw, base + MIPITX_PLL_CON0);
114*2ec9c210SAngeloGioacchino Del Regno mtk_phy_update_field(base + MIPITX_PLL_CON1, RG_DSI_PLL_POSDIV, txdiv0);
115*2ec9c210SAngeloGioacchino Del Regno
116*2ec9c210SAngeloGioacchino Del Regno /* Enable the PLL and wait for the clock output to stabilize */
117*2ec9c210SAngeloGioacchino Del Regno mtk_phy_set_bits(base + MIPITX_PLL_CON1, RG_DSI_PLL_EN);
118*2ec9c210SAngeloGioacchino Del Regno udelay(30);
119*2ec9c210SAngeloGioacchino Del Regno
120*2ec9c210SAngeloGioacchino Del Regno return 0;
121*2ec9c210SAngeloGioacchino Del Regno }
122*2ec9c210SAngeloGioacchino Del Regno
mtk_mipi_tx_pll_disable(struct clk_hw * hw)123*2ec9c210SAngeloGioacchino Del Regno static void mtk_mipi_tx_pll_disable(struct clk_hw *hw)
124*2ec9c210SAngeloGioacchino Del Regno {
125*2ec9c210SAngeloGioacchino Del Regno struct mtk_mipi_tx *mipi_tx = mtk_mipi_tx_from_clk_hw(hw);
126*2ec9c210SAngeloGioacchino Del Regno void __iomem *base = mipi_tx->regs;
127*2ec9c210SAngeloGioacchino Del Regno
128*2ec9c210SAngeloGioacchino Del Regno mtk_phy_clear_bits(base + MIPITX_PLL_CON1, RG_DSI_PLL_EN);
129*2ec9c210SAngeloGioacchino Del Regno
130*2ec9c210SAngeloGioacchino Del Regno mtk_phy_set_bits(base + MIPITX_PLL_PWR, AD_DSI_PLL_SDM_ISO_EN);
131*2ec9c210SAngeloGioacchino Del Regno mtk_phy_clear_bits(base + MIPITX_PLL_PWR, AD_DSI_PLL_SDM_PWR_ON);
132*2ec9c210SAngeloGioacchino Del Regno }
133*2ec9c210SAngeloGioacchino Del Regno
mtk_mipi_tx_pll_determine_rate(struct clk_hw * hw,struct clk_rate_request * req)134*2ec9c210SAngeloGioacchino Del Regno static int mtk_mipi_tx_pll_determine_rate(struct clk_hw *hw,
135*2ec9c210SAngeloGioacchino Del Regno struct clk_rate_request *req)
136*2ec9c210SAngeloGioacchino Del Regno {
137*2ec9c210SAngeloGioacchino Del Regno req->rate = clamp_val(req->rate,
138*2ec9c210SAngeloGioacchino Del Regno DSI_PHY_PLL_MIN_RATE_HZ, DSI_PHY_PLL_MAX_RATE_HZ);
139*2ec9c210SAngeloGioacchino Del Regno
140*2ec9c210SAngeloGioacchino Del Regno return 0;
141*2ec9c210SAngeloGioacchino Del Regno }
142*2ec9c210SAngeloGioacchino Del Regno
143*2ec9c210SAngeloGioacchino Del Regno static const struct clk_ops mtk_mipi_tx_pll_ops = {
144*2ec9c210SAngeloGioacchino Del Regno .enable = mtk_mipi_tx_pll_enable,
145*2ec9c210SAngeloGioacchino Del Regno .disable = mtk_mipi_tx_pll_disable,
146*2ec9c210SAngeloGioacchino Del Regno .determine_rate = mtk_mipi_tx_pll_determine_rate,
147*2ec9c210SAngeloGioacchino Del Regno .set_rate = mtk_mipi_tx_pll_set_rate,
148*2ec9c210SAngeloGioacchino Del Regno .recalc_rate = mtk_mipi_tx_pll_recalc_rate,
149*2ec9c210SAngeloGioacchino Del Regno };
150*2ec9c210SAngeloGioacchino Del Regno
mtk_mipi_tx_power_on_signal(struct phy * phy)151*2ec9c210SAngeloGioacchino Del Regno static void mtk_mipi_tx_power_on_signal(struct phy *phy)
152*2ec9c210SAngeloGioacchino Del Regno {
153*2ec9c210SAngeloGioacchino Del Regno struct mtk_mipi_tx *mipi_tx = phy_get_drvdata(phy);
154*2ec9c210SAngeloGioacchino Del Regno void __iomem *base = mipi_tx->regs;
155*2ec9c210SAngeloGioacchino Del Regno
156*2ec9c210SAngeloGioacchino Del Regno /* BG_LPF_EN / BG_CORE_EN */
157*2ec9c210SAngeloGioacchino Del Regno writel(RG_DSI_PAD_TIEL_SEL | RG_DSI_BG_CORE_EN, base + MIPITX_LANE_CON);
158*2ec9c210SAngeloGioacchino Del Regno /* Wait for MIPI core to enable */
159*2ec9c210SAngeloGioacchino Del Regno usleep_range(30, 100);
160*2ec9c210SAngeloGioacchino Del Regno writel(RG_DSI_BG_CORE_EN | RG_DSI_BG_LPF_EN, base + MIPITX_LANE_CON);
161*2ec9c210SAngeloGioacchino Del Regno
162*2ec9c210SAngeloGioacchino Del Regno /* Switch OFF each Lane */
163*2ec9c210SAngeloGioacchino Del Regno mtk_phy_clear_bits(base + MIPITX_D0_SW_CTL_EN, DSI_SW_CTL_EN);
164*2ec9c210SAngeloGioacchino Del Regno mtk_phy_clear_bits(base + MIPITX_D1_SW_CTL_EN, DSI_SW_CTL_EN);
165*2ec9c210SAngeloGioacchino Del Regno mtk_phy_clear_bits(base + MIPITX_D2_SW_CTL_EN, DSI_SW_CTL_EN);
166*2ec9c210SAngeloGioacchino Del Regno mtk_phy_clear_bits(base + MIPITX_D3_SW_CTL_EN, DSI_SW_CTL_EN);
167*2ec9c210SAngeloGioacchino Del Regno mtk_phy_clear_bits(base + MIPITX_CK_SW_CTL_EN, DSI_SW_CTL_EN);
168*2ec9c210SAngeloGioacchino Del Regno
169*2ec9c210SAngeloGioacchino Del Regno /*
170*2ec9c210SAngeloGioacchino Del Regno * The MIPI TX drive strength is in the range of 3000 ~ 6000 microamps:
171*2ec9c210SAngeloGioacchino Del Regno * RG_DSI_HSTX_LDO_REF_SEL expresses an offset from the minimum drive
172*2ec9c210SAngeloGioacchino Del Regno * strength (3000uA) and can add a maximum offset of 3000uA, reaching a
173*2ec9c210SAngeloGioacchino Del Regno * maximum drive strength of 3000+3000=6000uA.
174*2ec9c210SAngeloGioacchino Del Regno */
175*2ec9c210SAngeloGioacchino Del Regno mtk_phy_update_field(base + MIPITX_VOLTAGE_SEL, RG_DSI_HSTX_LDO_REF_SEL,
176*2ec9c210SAngeloGioacchino Del Regno (mipi_tx->mipitx_drive - 3000) / 200);
177*2ec9c210SAngeloGioacchino Del Regno
178*2ec9c210SAngeloGioacchino Del Regno mtk_phy_set_bits(base + MIPITX_CK_CKMODE_EN, DSI_CK_CKMODE_EN);
179*2ec9c210SAngeloGioacchino Del Regno }
180*2ec9c210SAngeloGioacchino Del Regno
mtk_mipi_tx_power_off_signal(struct phy * phy)181*2ec9c210SAngeloGioacchino Del Regno static void mtk_mipi_tx_power_off_signal(struct phy *phy)
182*2ec9c210SAngeloGioacchino Del Regno {
183*2ec9c210SAngeloGioacchino Del Regno struct mtk_mipi_tx *mipi_tx = phy_get_drvdata(phy);
184*2ec9c210SAngeloGioacchino Del Regno void __iomem *base = mipi_tx->regs;
185*2ec9c210SAngeloGioacchino Del Regno
186*2ec9c210SAngeloGioacchino Del Regno /* Switch ON each lane one by one */
187*2ec9c210SAngeloGioacchino Del Regno mtk_phy_set_bits(base + MIPITX_D0_SW_CTL_EN, DSI_SW_CTL_EN);
188*2ec9c210SAngeloGioacchino Del Regno mtk_phy_set_bits(base + MIPITX_D1_SW_CTL_EN, DSI_SW_CTL_EN);
189*2ec9c210SAngeloGioacchino Del Regno mtk_phy_set_bits(base + MIPITX_D2_SW_CTL_EN, DSI_SW_CTL_EN);
190*2ec9c210SAngeloGioacchino Del Regno mtk_phy_set_bits(base + MIPITX_D3_SW_CTL_EN, DSI_SW_CTL_EN);
191*2ec9c210SAngeloGioacchino Del Regno mtk_phy_set_bits(base + MIPITX_CK_SW_CTL_EN, DSI_SW_CTL_EN);
192*2ec9c210SAngeloGioacchino Del Regno
193*2ec9c210SAngeloGioacchino Del Regno writel(RG_DSI_PAD_TIEL_SEL | RG_DSI_BG_CORE_EN, base + MIPITX_LANE_CON);
194*2ec9c210SAngeloGioacchino Del Regno writel(RG_DSI_PAD_TIEL_SEL, base + MIPITX_LANE_CON);
195*2ec9c210SAngeloGioacchino Del Regno }
196*2ec9c210SAngeloGioacchino Del Regno
197*2ec9c210SAngeloGioacchino Del Regno const struct mtk_mipitx_data mt8196_mipitx_data = {
198*2ec9c210SAngeloGioacchino Del Regno .mipi_tx_clk_ops = &mtk_mipi_tx_pll_ops,
199*2ec9c210SAngeloGioacchino Del Regno .mipi_tx_enable_signal = mtk_mipi_tx_power_on_signal,
200*2ec9c210SAngeloGioacchino Del Regno .mipi_tx_disable_signal = mtk_mipi_tx_power_off_signal,
201*2ec9c210SAngeloGioacchino Del Regno };
202