1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2018 MediaTek Inc.
4 * Author: Chunhui Dai <chunhui.dai@mediatek.com>
7 #include "phy-mtk-hdmi.h"
10 #define RG_HDMITX_DRV_IBIAS 0
11 #define RG_HDMITX_DRV_IBIAS_MASK (0x3f << 0)
12 #define RG_HDMITX_EN_SER 12
13 #define RG_HDMITX_EN_SER_MASK (0x0f << 12)
14 #define RG_HDMITX_EN_SLDO 16
15 #define RG_HDMITX_EN_SLDO_MASK (0x0f << 16)
16 #define RG_HDMITX_EN_PRED 20
17 #define RG_HDMITX_EN_PRED_MASK (0x0f << 20)
18 #define RG_HDMITX_EN_IMP 24
19 #define RG_HDMITX_EN_IMP_MASK (0x0f << 24)
20 #define RG_HDMITX_EN_DRV 28
21 #define RG_HDMITX_EN_DRV_MASK (0x0f << 28)
23 #define HDMI_CON1 0x04
24 #define RG_HDMITX_PRED_IBIAS 18
25 #define RG_HDMITX_PRED_IBIAS_MASK (0x0f << 18)
26 #define RG_HDMITX_PRED_IMP (0x01 << 22)
27 #define RG_HDMITX_DRV_IMP 26
28 #define RG_HDMITX_DRV_IMP_MASK (0x3f << 26)
30 #define HDMI_CON2 0x08
31 #define RG_HDMITX_EN_TX_CKLDO (0x01 << 0)
32 #define RG_HDMITX_EN_TX_POSDIV (0x01 << 1)
33 #define RG_HDMITX_TX_POSDIV 3
34 #define RG_HDMITX_TX_POSDIV_MASK (0x03 << 3)
35 #define RG_HDMITX_EN_MBIAS (0x01 << 6)
36 #define RG_HDMITX_MBIAS_LPF_EN (0x01 << 7)
38 #define HDMI_CON4 0x10
39 #define RG_HDMITX_RESERVE_MASK (0xffffffff << 0)
41 #define HDMI_CON6 0x18
43 #define RG_HTPLL_BR_MASK (0x03 << 0)
45 #define RG_HTPLL_BC_MASK (0x03 << 2)
47 #define RG_HTPLL_BP_MASK (0x0f << 4)
49 #define RG_HTPLL_IR_MASK (0x0f << 8)
50 #define RG_HTPLL_IC 12
51 #define RG_HTPLL_IC_MASK (0x0f << 12)
52 #define RG_HTPLL_POSDIV 16
53 #define RG_HTPLL_POSDIV_MASK (0x03 << 16)
54 #define RG_HTPLL_PREDIV 18
55 #define RG_HTPLL_PREDIV_MASK (0x03 << 18)
56 #define RG_HTPLL_FBKSEL 20
57 #define RG_HTPLL_FBKSEL_MASK (0x03 << 20)
58 #define RG_HTPLL_RLH_EN (0x01 << 22)
59 #define RG_HTPLL_FBKDIV 24
60 #define RG_HTPLL_FBKDIV_MASK (0x7f << 24)
61 #define RG_HTPLL_EN (0x01 << 31)
63 #define HDMI_CON7 0x1c
64 #define RG_HTPLL_AUTOK_EN (0x01 << 23)
65 #define RG_HTPLL_DIVEN 28
66 #define RG_HTPLL_DIVEN_MASK (0x07 << 28)
68 static int mtk_hdmi_pll_prepare(struct clk_hw
*hw
)
70 struct mtk_hdmi_phy
*hdmi_phy
= to_mtk_hdmi_phy(hw
);
72 mtk_hdmi_phy_set_bits(hdmi_phy
, HDMI_CON7
, RG_HTPLL_AUTOK_EN
);
73 mtk_hdmi_phy_clear_bits(hdmi_phy
, HDMI_CON6
, RG_HTPLL_RLH_EN
);
74 mtk_hdmi_phy_set_bits(hdmi_phy
, HDMI_CON6
, RG_HTPLL_POSDIV_MASK
);
75 mtk_hdmi_phy_set_bits(hdmi_phy
, HDMI_CON2
, RG_HDMITX_EN_MBIAS
);
76 usleep_range(80, 100);
77 mtk_hdmi_phy_set_bits(hdmi_phy
, HDMI_CON6
, RG_HTPLL_EN
);
78 mtk_hdmi_phy_set_bits(hdmi_phy
, HDMI_CON2
, RG_HDMITX_EN_TX_CKLDO
);
79 mtk_hdmi_phy_set_bits(hdmi_phy
, HDMI_CON0
, RG_HDMITX_EN_SLDO_MASK
);
80 usleep_range(80, 100);
81 mtk_hdmi_phy_set_bits(hdmi_phy
, HDMI_CON2
, RG_HDMITX_MBIAS_LPF_EN
);
82 mtk_hdmi_phy_set_bits(hdmi_phy
, HDMI_CON0
, RG_HDMITX_EN_SER_MASK
);
83 mtk_hdmi_phy_set_bits(hdmi_phy
, HDMI_CON0
, RG_HDMITX_EN_PRED_MASK
);
84 mtk_hdmi_phy_set_bits(hdmi_phy
, HDMI_CON0
, RG_HDMITX_EN_DRV_MASK
);
85 usleep_range(80, 100);
89 static void mtk_hdmi_pll_unprepare(struct clk_hw
*hw
)
91 struct mtk_hdmi_phy
*hdmi_phy
= to_mtk_hdmi_phy(hw
);
93 mtk_hdmi_phy_clear_bits(hdmi_phy
, HDMI_CON0
, RG_HDMITX_EN_DRV_MASK
);
94 mtk_hdmi_phy_clear_bits(hdmi_phy
, HDMI_CON0
, RG_HDMITX_EN_PRED_MASK
);
95 mtk_hdmi_phy_clear_bits(hdmi_phy
, HDMI_CON0
, RG_HDMITX_EN_SER_MASK
);
96 mtk_hdmi_phy_clear_bits(hdmi_phy
, HDMI_CON2
, RG_HDMITX_MBIAS_LPF_EN
);
97 usleep_range(80, 100);
98 mtk_hdmi_phy_clear_bits(hdmi_phy
, HDMI_CON0
, RG_HDMITX_EN_SLDO_MASK
);
99 mtk_hdmi_phy_clear_bits(hdmi_phy
, HDMI_CON2
, RG_HDMITX_EN_TX_CKLDO
);
100 mtk_hdmi_phy_clear_bits(hdmi_phy
, HDMI_CON6
, RG_HTPLL_EN
);
101 usleep_range(80, 100);
102 mtk_hdmi_phy_clear_bits(hdmi_phy
, HDMI_CON2
, RG_HDMITX_EN_MBIAS
);
103 mtk_hdmi_phy_clear_bits(hdmi_phy
, HDMI_CON6
, RG_HTPLL_POSDIV_MASK
);
104 mtk_hdmi_phy_clear_bits(hdmi_phy
, HDMI_CON6
, RG_HTPLL_RLH_EN
);
105 mtk_hdmi_phy_clear_bits(hdmi_phy
, HDMI_CON7
, RG_HTPLL_AUTOK_EN
);
106 usleep_range(80, 100);
109 static long mtk_hdmi_pll_round_rate(struct clk_hw
*hw
, unsigned long rate
,
110 unsigned long *parent_rate
)
115 static int mtk_hdmi_pll_set_rate(struct clk_hw
*hw
, unsigned long rate
,
116 unsigned long parent_rate
)
118 struct mtk_hdmi_phy
*hdmi_phy
= to_mtk_hdmi_phy(hw
);
121 if (rate
<= 64000000)
123 else if (rate
<= 128000000)
128 mtk_hdmi_phy_set_bits(hdmi_phy
, HDMI_CON6
, RG_HTPLL_PREDIV_MASK
);
129 mtk_hdmi_phy_set_bits(hdmi_phy
, HDMI_CON6
, RG_HTPLL_POSDIV_MASK
);
130 mtk_hdmi_phy_set_bits(hdmi_phy
, HDMI_CON2
, RG_HDMITX_EN_TX_POSDIV
);
131 mtk_hdmi_phy_mask(hdmi_phy
, HDMI_CON6
, (0x1 << RG_HTPLL_IC
),
133 mtk_hdmi_phy_mask(hdmi_phy
, HDMI_CON6
, (0x1 << RG_HTPLL_IR
),
135 mtk_hdmi_phy_mask(hdmi_phy
, HDMI_CON2
, (pos_div
<< RG_HDMITX_TX_POSDIV
),
136 RG_HDMITX_TX_POSDIV_MASK
);
137 mtk_hdmi_phy_mask(hdmi_phy
, HDMI_CON6
, (1 << RG_HTPLL_FBKSEL
),
138 RG_HTPLL_FBKSEL_MASK
);
139 mtk_hdmi_phy_mask(hdmi_phy
, HDMI_CON6
, (19 << RG_HTPLL_FBKDIV
),
140 RG_HTPLL_FBKDIV_MASK
);
141 mtk_hdmi_phy_mask(hdmi_phy
, HDMI_CON7
, (0x2 << RG_HTPLL_DIVEN
),
142 RG_HTPLL_DIVEN_MASK
);
143 mtk_hdmi_phy_mask(hdmi_phy
, HDMI_CON6
, (0xc << RG_HTPLL_BP
),
145 mtk_hdmi_phy_mask(hdmi_phy
, HDMI_CON6
, (0x2 << RG_HTPLL_BC
),
147 mtk_hdmi_phy_mask(hdmi_phy
, HDMI_CON6
, (0x1 << RG_HTPLL_BR
),
150 mtk_hdmi_phy_clear_bits(hdmi_phy
, HDMI_CON1
, RG_HDMITX_PRED_IMP
);
151 mtk_hdmi_phy_mask(hdmi_phy
, HDMI_CON1
, (0x3 << RG_HDMITX_PRED_IBIAS
),
152 RG_HDMITX_PRED_IBIAS_MASK
);
153 mtk_hdmi_phy_clear_bits(hdmi_phy
, HDMI_CON0
, RG_HDMITX_EN_IMP_MASK
);
154 mtk_hdmi_phy_mask(hdmi_phy
, HDMI_CON1
, (0x28 << RG_HDMITX_DRV_IMP
),
155 RG_HDMITX_DRV_IMP_MASK
);
156 mtk_hdmi_phy_mask(hdmi_phy
, HDMI_CON4
, 0x28, RG_HDMITX_RESERVE_MASK
);
157 mtk_hdmi_phy_mask(hdmi_phy
, HDMI_CON0
, (0xa << RG_HDMITX_DRV_IBIAS
),
158 RG_HDMITX_DRV_IBIAS_MASK
);
162 static unsigned long mtk_hdmi_pll_recalc_rate(struct clk_hw
*hw
,
163 unsigned long parent_rate
)
165 struct mtk_hdmi_phy
*hdmi_phy
= to_mtk_hdmi_phy(hw
);
166 unsigned long out_rate
, val
;
168 val
= (readl(hdmi_phy
->regs
+ HDMI_CON6
)
169 & RG_HTPLL_PREDIV_MASK
) >> RG_HTPLL_PREDIV
;
172 out_rate
= parent_rate
;
175 out_rate
= parent_rate
/ 2;
178 out_rate
= parent_rate
/ 4;
182 val
= (readl(hdmi_phy
->regs
+ HDMI_CON6
)
183 & RG_HTPLL_FBKDIV_MASK
) >> RG_HTPLL_FBKDIV
;
184 out_rate
*= (val
+ 1) * 2;
185 val
= (readl(hdmi_phy
->regs
+ HDMI_CON2
)
186 & RG_HDMITX_TX_POSDIV_MASK
);
187 out_rate
>>= (val
>> RG_HDMITX_TX_POSDIV
);
189 if (readl(hdmi_phy
->regs
+ HDMI_CON2
) & RG_HDMITX_EN_TX_POSDIV
)
195 static const struct clk_ops mtk_hdmi_phy_pll_ops
= {
196 .prepare
= mtk_hdmi_pll_prepare
,
197 .unprepare
= mtk_hdmi_pll_unprepare
,
198 .set_rate
= mtk_hdmi_pll_set_rate
,
199 .round_rate
= mtk_hdmi_pll_round_rate
,
200 .recalc_rate
= mtk_hdmi_pll_recalc_rate
,
203 static void mtk_hdmi_phy_enable_tmds(struct mtk_hdmi_phy
*hdmi_phy
)
205 mtk_hdmi_phy_set_bits(hdmi_phy
, HDMI_CON7
, RG_HTPLL_AUTOK_EN
);
206 mtk_hdmi_phy_clear_bits(hdmi_phy
, HDMI_CON6
, RG_HTPLL_RLH_EN
);
207 mtk_hdmi_phy_set_bits(hdmi_phy
, HDMI_CON6
, RG_HTPLL_POSDIV_MASK
);
208 mtk_hdmi_phy_set_bits(hdmi_phy
, HDMI_CON2
, RG_HDMITX_EN_MBIAS
);
209 usleep_range(80, 100);
210 mtk_hdmi_phy_set_bits(hdmi_phy
, HDMI_CON6
, RG_HTPLL_EN
);
211 mtk_hdmi_phy_set_bits(hdmi_phy
, HDMI_CON2
, RG_HDMITX_EN_TX_CKLDO
);
212 mtk_hdmi_phy_set_bits(hdmi_phy
, HDMI_CON0
, RG_HDMITX_EN_SLDO_MASK
);
213 usleep_range(80, 100);
214 mtk_hdmi_phy_set_bits(hdmi_phy
, HDMI_CON2
, RG_HDMITX_MBIAS_LPF_EN
);
215 mtk_hdmi_phy_set_bits(hdmi_phy
, HDMI_CON0
, RG_HDMITX_EN_SER_MASK
);
216 mtk_hdmi_phy_set_bits(hdmi_phy
, HDMI_CON0
, RG_HDMITX_EN_PRED_MASK
);
217 mtk_hdmi_phy_set_bits(hdmi_phy
, HDMI_CON0
, RG_HDMITX_EN_DRV_MASK
);
218 usleep_range(80, 100);
221 static void mtk_hdmi_phy_disable_tmds(struct mtk_hdmi_phy
*hdmi_phy
)
223 mtk_hdmi_phy_clear_bits(hdmi_phy
, HDMI_CON0
, RG_HDMITX_EN_DRV_MASK
);
224 mtk_hdmi_phy_clear_bits(hdmi_phy
, HDMI_CON0
, RG_HDMITX_EN_PRED_MASK
);
225 mtk_hdmi_phy_clear_bits(hdmi_phy
, HDMI_CON0
, RG_HDMITX_EN_SER_MASK
);
226 mtk_hdmi_phy_clear_bits(hdmi_phy
, HDMI_CON2
, RG_HDMITX_MBIAS_LPF_EN
);
227 usleep_range(80, 100);
228 mtk_hdmi_phy_clear_bits(hdmi_phy
, HDMI_CON0
, RG_HDMITX_EN_SLDO_MASK
);
229 mtk_hdmi_phy_clear_bits(hdmi_phy
, HDMI_CON2
, RG_HDMITX_EN_TX_CKLDO
);
230 mtk_hdmi_phy_clear_bits(hdmi_phy
, HDMI_CON6
, RG_HTPLL_EN
);
231 usleep_range(80, 100);
232 mtk_hdmi_phy_clear_bits(hdmi_phy
, HDMI_CON2
, RG_HDMITX_EN_MBIAS
);
233 mtk_hdmi_phy_clear_bits(hdmi_phy
, HDMI_CON6
, RG_HTPLL_POSDIV_MASK
);
234 mtk_hdmi_phy_clear_bits(hdmi_phy
, HDMI_CON6
, RG_HTPLL_RLH_EN
);
235 mtk_hdmi_phy_clear_bits(hdmi_phy
, HDMI_CON7
, RG_HTPLL_AUTOK_EN
);
236 usleep_range(80, 100);
239 struct mtk_hdmi_phy_conf mtk_hdmi_phy_2701_conf
= {
240 .flags
= CLK_SET_RATE_GATE
,
241 .pll_default_off
= true,
242 .hdmi_phy_clk_ops
= &mtk_hdmi_phy_pll_ops
,
243 .hdmi_phy_enable_tmds
= mtk_hdmi_phy_enable_tmds
,
244 .hdmi_phy_disable_tmds
= mtk_hdmi_phy_disable_tmds
,
247 MODULE_AUTHOR("Chunhui Dai <chunhui.dai@mediatek.com>");
248 MODULE_DESCRIPTION("MediaTek HDMI PHY Driver");
249 MODULE_LICENSE("GPL v2");