1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2022 MediaTek Inc.
4 * Copyright (c) 2022 BayLibre, SAS
6 #include <linux/delay.h>
8 #include <linux/mfd/syscon.h>
9 #include <linux/module.h>
10 #include <linux/phy/phy.h>
11 #include <linux/platform_device.h>
12 #include <linux/types.h>
13 #include <linux/units.h>
14 #include <linux/nvmem-consumer.h>
16 #include "phy-mtk-io.h"
17 #include "phy-mtk-hdmi.h"
18 #include "phy-mtk-hdmi-mt8195.h"
20 static void mtk_hdmi_ana_fifo_en(struct mtk_hdmi_phy
*hdmi_phy
)
22 /* make data fifo writable for hdmi2.0 */
23 mtk_phy_set_bits(hdmi_phy
->regs
+ HDMI_ANA_CTL
, REG_ANA_HDMI20_FIFO_EN
);
27 mtk_phy_tmds_clk_ratio(struct mtk_hdmi_phy
*hdmi_phy
, bool enable
)
29 void __iomem
*regs
= hdmi_phy
->regs
;
31 mtk_hdmi_ana_fifo_en(hdmi_phy
);
33 /* HDMI 2.0 specification, 3.4Gbps <= TMDS Bit Rate <= 6G,
34 * clock bit ratio 1:40, under 3.4Gbps, clock bit ratio 1:10
37 mtk_phy_update_field(regs
+ HDMI20_CLK_CFG
, REG_TXC_DIV
, 3);
39 mtk_phy_clear_bits(regs
+ HDMI20_CLK_CFG
, REG_TXC_DIV
);
42 static void mtk_hdmi_pll_sel_src(struct clk_hw
*hw
)
44 struct mtk_hdmi_phy
*hdmi_phy
= to_mtk_hdmi_phy(hw
);
45 void __iomem
*regs
= hdmi_phy
->regs
;
47 mtk_phy_clear_bits(regs
+ HDMI_CTL_3
, REG_HDMITX_REF_XTAL_SEL
);
48 mtk_phy_clear_bits(regs
+ HDMI_CTL_3
, REG_HDMITX_REF_RESPLL_SEL
);
50 /* DA_HDMITX21_REF_CK for TXPLL input source */
51 mtk_phy_clear_bits(regs
+ HDMI_1_CFG_10
, RG_HDMITXPLL_REF_CK_SEL
);
54 static void mtk_hdmi_pll_perf(struct clk_hw
*hw
)
56 struct mtk_hdmi_phy
*hdmi_phy
= to_mtk_hdmi_phy(hw
);
57 void __iomem
*regs
= hdmi_phy
->regs
;
59 mtk_phy_set_bits(regs
+ HDMI_1_PLL_CFG_0
, RG_HDMITXPLL_BP2
);
60 mtk_phy_set_bits(regs
+ HDMI_1_PLL_CFG_2
, RG_HDMITXPLL_BC
);
61 mtk_phy_update_field(regs
+ HDMI_1_PLL_CFG_2
, RG_HDMITXPLL_IC
, 0x1);
62 mtk_phy_update_field(regs
+ HDMI_1_PLL_CFG_2
, RG_HDMITXPLL_BR
, 0x2);
63 mtk_phy_update_field(regs
+ HDMI_1_PLL_CFG_2
, RG_HDMITXPLL_IR
, 0x2);
64 mtk_phy_set_bits(regs
+ HDMI_1_PLL_CFG_2
, RG_HDMITXPLL_BP
);
65 mtk_phy_clear_bits(regs
+ HDMI_1_PLL_CFG_0
, RG_HDMITXPLL_IBAND_FIX_EN
);
66 mtk_phy_clear_bits(regs
+ HDMI_1_PLL_CFG_1
, RG_HDMITXPLL_RESERVE_BIT14
);
67 mtk_phy_clear_bits(regs
+ HDMI_1_PLL_CFG_2
, RG_HDMITXPLL_HIKVCO
);
68 mtk_phy_update_field(regs
+ HDMI_1_PLL_CFG_0
, RG_HDMITXPLL_HREN
, 0x1);
69 mtk_phy_update_field(regs
+ HDMI_1_PLL_CFG_0
, RG_HDMITXPLL_LVR_SEL
, 0x1);
70 mtk_phy_set_bits(regs
+ HDMI_1_PLL_CFG_1
, RG_HDMITXPLL_RESERVE_BIT12_11
);
71 mtk_phy_set_bits(regs
+ HDMI_1_PLL_CFG_0
, RG_HDMITXPLL_TCL_EN
);
74 static int mtk_hdmi_pll_set_hw(struct clk_hw
*hw
, u8 prediv
,
77 u8 fbkdiv_hs3
, u8 posdiv1
,
78 u8 posdiv2
, u8 txprediv
,
89 struct mtk_hdmi_phy
*hdmi_phy
= to_mtk_hdmi_phy(hw
);
90 void __iomem
*regs
= hdmi_phy
->regs
;
92 mtk_hdmi_pll_sel_src(hw
);
94 mtk_hdmi_pll_perf(hw
);
96 mtk_phy_update_field(regs
+ HDMI_1_CFG_10
, RG_HDMITX21_BIAS_PE_BG_VREF_SEL
, 0x2);
97 mtk_phy_clear_bits(regs
+ HDMI_1_CFG_10
, RG_HDMITX21_VREF_SEL
);
98 mtk_phy_update_field(regs
+ HDMI_1_CFG_9
, RG_HDMITX21_SLDO_VREF_SEL
, 0x2);
99 mtk_phy_clear_bits(regs
+ HDMI_1_CFG_10
, RG_HDMITX21_BIAS_PE_VREF_SELB
);
100 mtk_phy_set_bits(regs
+ HDMI_1_CFG_3
, RG_HDMITX21_SLDOLPF_EN
);
101 mtk_phy_update_field(regs
+ HDMI_1_CFG_6
, RG_HDMITX21_INTR_CAL
, 0x11);
102 mtk_phy_set_bits(regs
+ HDMI_1_PLL_CFG_2
, RG_HDMITXPLL_PWD
);
105 txposdiv_value
= ilog2(txposdiv
);
107 mtk_phy_update_field(regs
+ HDMI_1_CFG_6
, RG_HDMITX21_TX_POSDIV
, txposdiv_value
);
108 mtk_phy_set_bits(regs
+ HDMI_1_CFG_6
, RG_HDMITX21_TX_POSDIV_EN
);
109 mtk_phy_clear_bits(regs
+ HDMI_1_CFG_6
, RG_HDMITX21_FRL_EN
);
114 div3_ctrl_value
= 0x0;
118 div3_ctrl_value
= 0x0;
122 div3_ctrl_value
= 0x1;
126 div3_ctrl_value
= 0x1;
133 mtk_phy_update_field(regs
+ HDMI_1_PLL_CFG_4
, RG_HDMITXPLL_POSDIV_DIV3_CTRL
, div3_ctrl_value
);
134 mtk_phy_update_field(regs
+ HDMI_1_PLL_CFG_4
, RG_HDMITXPLL_POSDIV
, posdiv_vallue
);
139 div_ctrl_value
= 0x0;
142 div_ctrl_value
= 0x1;
145 div_ctrl_value
= 0x2;
148 div_ctrl_value
= 0x3;
154 mtk_phy_update_field(regs
+ HDMI_1_PLL_CFG_4
, RG_HDMITXPLL_DIV_CTRL
, div_ctrl_value
);
156 /* DE add new setting */
157 mtk_phy_clear_bits(regs
+ HDMI_1_PLL_CFG_1
, RG_HDMITXPLL_RESERVE_BIT14
);
162 reserve_3_2_value
= 0x0;
165 reserve_3_2_value
= 0x1;
168 reserve_3_2_value
= 0x2;
171 reserve_3_2_value
= 0x3;
177 mtk_phy_update_field(regs
+ HDMI_1_PLL_CFG_1
, RG_HDMITXPLL_RESERVE_BIT3_2
, reserve_3_2_value
);
179 /* DE add new setting */
180 mtk_phy_update_field(regs
+ HDMI_1_PLL_CFG_1
, RG_HDMITXPLL_RESERVE_BIT1_0
, 0x2);
183 prediv_value
= ilog2(prediv
);
185 mtk_phy_update_field(regs
+ HDMI_1_PLL_CFG_4
, RG_HDMITXPLL_PREDIV
, prediv_value
);
188 reserve13_value
= ilog2(fbkdiv_hs3
);
190 mtk_phy_update_field(regs
+ HDMI_1_PLL_CFG_1
, RG_HDMITXPLL_RESERVE_BIT13
, reserve13_value
);
193 mtk_phy_update_field(regs
+ HDMI_1_PLL_CFG_4
, RG_HDMITXPLL_FBKDIV_HIGH
, fbkdiv_high
);
194 mtk_phy_update_field(regs
+ HDMI_1_PLL_CFG_3
, RG_HDMITXPLL_FBKDIV_LOW
, fbkdiv_low
);
196 /* Digital DIVIDER */
197 mtk_phy_clear_bits(regs
+ HDMI_CTL_3
, REG_PIXEL_CLOCK_SEL
);
199 if (digital_div
== 1) {
200 mtk_phy_clear_bits(regs
+ HDMI_CTL_3
, REG_HDMITX_PIXEL_CLOCK
);
202 mtk_phy_set_bits(regs
+ HDMI_CTL_3
, REG_HDMITX_PIXEL_CLOCK
);
203 mtk_phy_update_field(regs
+ HDMI_CTL_3
, REG_HDMITXPLL_DIV
, digital_div
- 1);
209 static int mtk_hdmi_pll_calc(struct mtk_hdmi_phy
*hdmi_phy
, struct clk_hw
*hw
,
210 unsigned long rate
, unsigned long parent_rate
)
212 u8 digital_div
, txprediv
, txposdiv
, fbkdiv_high
, posdiv1
, posdiv2
;
213 u64 tmds_clk
, pixel_clk
, da_hdmitx21_ref_ck
, ns_hdmipll_ck
, pcw
;
214 u8 txpredivs
[4] = { 2, 4, 6, 12 };
219 tmds_clk
= pixel_clk
;
221 if (tmds_clk
< 25 * MEGA
|| tmds_clk
> 594 * MEGA
)
224 if (tmds_clk
>= 340 * MEGA
)
225 hdmi_phy
->tmds_over_340M
= true;
227 hdmi_phy
->tmds_over_340M
= false;
230 da_hdmitx21_ref_ck
= 26 * MEGA
;
232 /* TXPOSDIV stage treatment:
233 * 0M < TMDS clk < 54M /8
234 * 54M <= TMDS clk < 148.35M /4
235 * 148.35M <=TMDS clk < 296.7M /2
236 * 296.7 <=TMDS clk <= 594M /1
238 if (tmds_clk
< 54 * MEGA
)
240 else if (tmds_clk
>= 54 * MEGA
&& (tmds_clk
* 100) < 14835 * MEGA
)
242 else if ((tmds_clk
* 100) >= 14835 * MEGA
&& (tmds_clk
* 10) < 2967 * MEGA
)
244 else if ((tmds_clk
* 10) >= 2967 * MEGA
&& tmds_clk
<= 594 * MEGA
)
249 /* calculate txprediv: can be 2, 4, 6, 12
250 * ICO clk = 5*TMDS_CLK*TXPOSDIV*TXPREDIV
251 * ICO clk constraint: 5G =< ICO clk <= 12G
253 for (i
= 0; i
< ARRAY_SIZE(txpredivs
); i
++) {
254 ns_hdmipll_ck
= 5 * tmds_clk
* txposdiv
* txpredivs
[i
];
255 if (ns_hdmipll_ck
>= 5 * GIGA
&&
256 ns_hdmipll_ck
<= 12 * GIGA
)
259 if (i
== (ARRAY_SIZE(txpredivs
) - 1) &&
260 (ns_hdmipll_ck
< 5 * GIGA
|| ns_hdmipll_ck
> 12 * GIGA
)) {
263 if (i
== ARRAY_SIZE(txpredivs
))
266 txprediv
= txpredivs
[i
];
268 /* PCW calculation: FBKDIV
269 * formula: pcw=(frequency_out*2^pcw_bit) / frequency_in / FBKDIV_HS3;
270 * RG_HDMITXPLL_FBKDIV[32:0]:
271 * [32,24] 9bit integer, [23,0]:24bit fraction
273 pcw
= div_u64(((u64
)ns_hdmipll_ck
) << PCW_DECIMAL_WIDTH
,
274 da_hdmitx21_ref_ck
* PLL_FBKDIV_HS3
);
276 if (pcw
> GENMASK_ULL(32, 0))
279 fbkdiv_high
= FIELD_GET(GENMASK_ULL(63, 32), pcw
);
280 fbkdiv_low
= FIELD_GET(GENMASK(31, 0), pcw
);
283 * posdiv1 stage treatment according to color_depth:
284 * 24bit -> posdiv1 /10, 30bit -> posdiv1 /12.5,
285 * 36bit -> posdiv1 /15, 48bit -> posdiv1 /10
290 /* Digital clk divider, max /32 */
291 digital_div
= div_u64(ns_hdmipll_ck
, posdiv1
* posdiv2
* pixel_clk
);
292 if (!(digital_div
<= 32 && digital_div
>= 1))
295 return mtk_hdmi_pll_set_hw(hw
, PLL_PREDIV
, fbkdiv_high
, fbkdiv_low
,
296 PLL_FBKDIV_HS3
, posdiv1
, posdiv2
, txprediv
,
297 txposdiv
, digital_div
);
300 static int mtk_hdmi_pll_drv_setting(struct clk_hw
*hw
)
302 struct mtk_hdmi_phy
*hdmi_phy
= to_mtk_hdmi_phy(hw
);
303 void __iomem
*regs
= hdmi_phy
->regs
;
304 u8 data_channel_bias
, clk_channel_bias
;
305 u8 impedance
, impedance_en
;
307 u32 pixel_clk
= hdmi_phy
->pll_rate
;
309 tmds_clk
= pixel_clk
;
311 /* bias & impedance setting:
312 * 3G < data rate <= 6G: enable impedance 100ohm,
313 * data channel bias 24mA, clock channel bias 20mA
314 * pixel clk >= HD, 74.175MHZ <= pixel clk <= 300MHZ:
315 * enalbe impedance 100ohm
316 * data channel 20mA, clock channel 16mA
317 * 27M =< pixel clk < 74.175: disable impedance
318 * data channel & clock channel bias 10mA
321 /* 3G < data rate <= 6G, 300M < tmds rate <= 594M */
322 if (tmds_clk
> 300 * MEGA
&& tmds_clk
<= 594 * MEGA
) {
323 data_channel_bias
= 0x3c; /* 24mA */
324 clk_channel_bias
= 0x34; /* 20mA */
326 impedance
= 0x36; /* 100ohm */
327 } else if (((u64
)pixel_clk
* 1000) >= 74175 * MEGA
&& pixel_clk
<= 300 * MEGA
) {
328 data_channel_bias
= 0x34; /* 20mA */
329 clk_channel_bias
= 0x2c; /* 16mA */
331 impedance
= 0x36; /* 100ohm */
332 } else if (pixel_clk
>= 27 * MEGA
&& ((u64
)pixel_clk
* 1000) < 74175 * MEGA
) {
333 data_channel_bias
= 0x14; /* 10mA */
334 clk_channel_bias
= 0x14; /* 10mA */
342 mtk_phy_update_field(regs
+ HDMI_1_CFG_1
, RG_HDMITX21_DRV_IBIAS_D0
, data_channel_bias
);
343 mtk_phy_update_field(regs
+ HDMI_1_CFG_1
, RG_HDMITX21_DRV_IBIAS_D1
, data_channel_bias
);
344 mtk_phy_update_field(regs
+ HDMI_1_CFG_1
, RG_HDMITX21_DRV_IBIAS_D2
, data_channel_bias
);
345 mtk_phy_update_field(regs
+ HDMI_1_CFG_0
, RG_HDMITX21_DRV_IBIAS_CLK
, clk_channel_bias
);
348 mtk_phy_update_field(regs
+ HDMI_1_CFG_0
, RG_HDMITX21_DRV_IMP_EN
, impedance_en
);
349 mtk_phy_update_field(regs
+ HDMI_1_CFG_2
, RG_HDMITX21_DRV_IMP_D0_EN1
, impedance
);
350 mtk_phy_update_field(regs
+ HDMI_1_CFG_2
, RG_HDMITX21_DRV_IMP_D1_EN1
, impedance
);
351 mtk_phy_update_field(regs
+ HDMI_1_CFG_2
, RG_HDMITX21_DRV_IMP_D2_EN1
, impedance
);
352 mtk_phy_update_field(regs
+ HDMI_1_CFG_2
, RG_HDMITX21_DRV_IMP_CLK_EN1
, impedance
);
357 static int mtk_hdmi_pll_prepare(struct clk_hw
*hw
)
359 struct mtk_hdmi_phy
*hdmi_phy
= to_mtk_hdmi_phy(hw
);
360 void __iomem
*regs
= hdmi_phy
->regs
;
362 mtk_phy_set_bits(regs
+ HDMI_1_CFG_6
, RG_HDMITX21_TX_POSDIV_EN
);
364 mtk_phy_set_bits(regs
+ HDMI_1_CFG_0
, RG_HDMITX21_SER_EN
);
365 mtk_phy_set_bits(regs
+ HDMI_1_CFG_6
, RG_HDMITX21_D0_DRV_OP_EN
);
366 mtk_phy_set_bits(regs
+ HDMI_1_CFG_6
, RG_HDMITX21_D1_DRV_OP_EN
);
367 mtk_phy_set_bits(regs
+ HDMI_1_CFG_6
, RG_HDMITX21_D2_DRV_OP_EN
);
368 mtk_phy_set_bits(regs
+ HDMI_1_CFG_6
, RG_HDMITX21_CK_DRV_OP_EN
);
370 mtk_phy_clear_bits(regs
+ HDMI_1_CFG_6
, RG_HDMITX21_FRL_D0_EN
);
371 mtk_phy_clear_bits(regs
+ HDMI_1_CFG_6
, RG_HDMITX21_FRL_D1_EN
);
372 mtk_phy_clear_bits(regs
+ HDMI_1_CFG_6
, RG_HDMITX21_FRL_D2_EN
);
373 mtk_phy_clear_bits(regs
+ HDMI_1_CFG_6
, RG_HDMITX21_FRL_CK_EN
);
375 mtk_hdmi_pll_drv_setting(hw
);
377 mtk_phy_clear_bits(regs
+ HDMI_1_CFG_10
, RG_HDMITX21_BG_PWD
);
378 mtk_phy_set_bits(regs
+ HDMI_1_CFG_6
, RG_HDMITX21_BIAS_EN
);
379 mtk_phy_set_bits(regs
+ HDMI_1_CFG_3
, RG_HDMITX21_CKLDO_EN
);
380 mtk_phy_set_bits(regs
+ HDMI_1_CFG_3
, RG_HDMITX21_SLDO_EN
);
382 mtk_phy_set_bits(regs
+ HDMI_1_PLL_CFG_4
, DA_HDMITXPLL_PWR_ON
);
384 mtk_phy_clear_bits(regs
+ HDMI_1_PLL_CFG_4
, DA_HDMITXPLL_ISO_EN
);
386 mtk_phy_clear_bits(regs
+ HDMI_1_PLL_CFG_2
, RG_HDMITXPLL_PWD
);
387 usleep_range(30, 50);
391 static void mtk_hdmi_pll_unprepare(struct clk_hw
*hw
)
393 struct mtk_hdmi_phy
*hdmi_phy
= to_mtk_hdmi_phy(hw
);
394 void __iomem
*regs
= hdmi_phy
->regs
;
396 mtk_phy_set_bits(regs
+ HDMI_1_CFG_10
, RG_HDMITX21_BG_PWD
);
397 mtk_phy_clear_bits(regs
+ HDMI_1_CFG_6
, RG_HDMITX21_BIAS_EN
);
398 mtk_phy_clear_bits(regs
+ HDMI_1_CFG_3
, RG_HDMITX21_CKLDO_EN
);
399 mtk_phy_clear_bits(regs
+ HDMI_1_CFG_3
, RG_HDMITX21_SLDO_EN
);
401 mtk_phy_set_bits(regs
+ HDMI_1_PLL_CFG_2
, RG_HDMITXPLL_PWD
);
402 usleep_range(10, 20);
403 mtk_phy_set_bits(regs
+ HDMI_1_PLL_CFG_4
, DA_HDMITXPLL_ISO_EN
);
404 usleep_range(10, 20);
405 mtk_phy_clear_bits(regs
+ HDMI_1_PLL_CFG_4
, DA_HDMITXPLL_PWR_ON
);
408 static int mtk_hdmi_pll_set_rate(struct clk_hw
*hw
, unsigned long rate
,
409 unsigned long parent_rate
)
411 struct mtk_hdmi_phy
*hdmi_phy
= to_mtk_hdmi_phy(hw
);
413 dev_dbg(hdmi_phy
->dev
, "%s: %lu Hz, parent: %lu Hz\n", __func__
, rate
,
416 return mtk_hdmi_pll_calc(hdmi_phy
, hw
, rate
, parent_rate
);
419 static long mtk_hdmi_pll_round_rate(struct clk_hw
*hw
, unsigned long rate
,
420 unsigned long *parent_rate
)
422 struct mtk_hdmi_phy
*hdmi_phy
= to_mtk_hdmi_phy(hw
);
424 hdmi_phy
->pll_rate
= rate
;
428 static unsigned long mtk_hdmi_pll_recalc_rate(struct clk_hw
*hw
,
429 unsigned long parent_rate
)
431 struct mtk_hdmi_phy
*hdmi_phy
= to_mtk_hdmi_phy(hw
);
433 return hdmi_phy
->pll_rate
;
436 static const struct clk_ops mtk_hdmi_pll_ops
= {
437 .prepare
= mtk_hdmi_pll_prepare
,
438 .unprepare
= mtk_hdmi_pll_unprepare
,
439 .set_rate
= mtk_hdmi_pll_set_rate
,
440 .round_rate
= mtk_hdmi_pll_round_rate
,
441 .recalc_rate
= mtk_hdmi_pll_recalc_rate
,
444 static void vtx_signal_en(struct mtk_hdmi_phy
*hdmi_phy
, bool on
)
446 void __iomem
*regs
= hdmi_phy
->regs
;
449 mtk_phy_set_bits(regs
+ HDMI_1_CFG_0
, RG_HDMITX21_DRV_EN
);
451 mtk_phy_clear_bits(regs
+ HDMI_1_CFG_0
, RG_HDMITX21_DRV_EN
);
454 static void mtk_hdmi_phy_enable_tmds(struct mtk_hdmi_phy
*hdmi_phy
)
456 vtx_signal_en(hdmi_phy
, true);
457 usleep_range(100, 150);
460 static void mtk_hdmi_phy_disable_tmds(struct mtk_hdmi_phy
*hdmi_phy
)
462 vtx_signal_en(hdmi_phy
, false);
465 static int mtk_hdmi_phy_configure(struct phy
*phy
, union phy_configure_opts
*opts
)
467 struct phy_configure_opts_dp
*dp_opts
= &opts
->dp
;
468 struct mtk_hdmi_phy
*hdmi_phy
= phy_get_drvdata(phy
);
471 ret
= clk_set_rate(hdmi_phy
->pll
, dp_opts
->link_rate
);
476 mtk_phy_tmds_clk_ratio(hdmi_phy
, hdmi_phy
->tmds_over_340M
);
481 struct mtk_hdmi_phy_conf mtk_hdmi_phy_8195_conf
= {
482 .flags
= CLK_SET_RATE_PARENT
| CLK_SET_RATE_GATE
,
483 .hdmi_phy_clk_ops
= &mtk_hdmi_pll_ops
,
484 .hdmi_phy_enable_tmds
= mtk_hdmi_phy_enable_tmds
,
485 .hdmi_phy_disable_tmds
= mtk_hdmi_phy_disable_tmds
,
486 .hdmi_phy_configure
= mtk_hdmi_phy_configure
,
489 MODULE_AUTHOR("Can Zeng <can.zeng@mediatek.com>");
490 MODULE_DESCRIPTION("MediaTek MT8195 HDMI PHY Driver");
491 MODULE_LICENSE("GPL v2");