1b28be59aSCK Hu // SPDX-License-Identifier: GPL-2.0-only
2b28be59aSCK Hu /*
3b28be59aSCK Hu * Copyright (c) 2014 MediaTek Inc.
4b28be59aSCK Hu * Author: Jie Qiu <jie.qiu@mediatek.com>
5b28be59aSCK Hu */
6b28be59aSCK Hu
7b28be59aSCK Hu #include "phy-mtk-hdmi.h"
8*0fb5e57eSChunfeng Yun #include "phy-mtk-io.h"
9b28be59aSCK Hu
10b28be59aSCK Hu #define HDMI_CON0 0x00
11b28be59aSCK Hu #define RG_HDMITX_PLL_EN BIT(31)
12a8a78274SChunfeng Yun #define RG_HDMITX_PLL_FBKDIV GENMASK(30, 24)
13a8a78274SChunfeng Yun #define RG_HDMITX_PLL_FBKSEL GENMASK(23, 22)
14a8a78274SChunfeng Yun #define RG_HDMITX_PLL_PREDIV GENMASK(21, 20)
15a8a78274SChunfeng Yun #define RG_HDMITX_PLL_POSDIV GENMASK(19, 18)
16a8a78274SChunfeng Yun #define RG_HDMITX_PLL_RST_DLY GENMASK(17, 16)
17a8a78274SChunfeng Yun #define RG_HDMITX_PLL_IR GENMASK(15, 12)
18a8a78274SChunfeng Yun #define RG_HDMITX_PLL_IC GENMASK(11, 8)
19a8a78274SChunfeng Yun #define RG_HDMITX_PLL_BP GENMASK(7, 4)
20a8a78274SChunfeng Yun #define RG_HDMITX_PLL_BR GENMASK(3, 2)
21a8a78274SChunfeng Yun #define RG_HDMITX_PLL_BC GENMASK(1, 0)
22b28be59aSCK Hu #define HDMI_CON1 0x04
23a8a78274SChunfeng Yun #define RG_HDMITX_PLL_DIVEN GENMASK(31, 29)
24b28be59aSCK Hu #define RG_HDMITX_PLL_AUTOK_EN BIT(28)
25a8a78274SChunfeng Yun #define RG_HDMITX_PLL_AUTOK_KF GENMASK(27, 26)
26a8a78274SChunfeng Yun #define RG_HDMITX_PLL_AUTOK_KS GENMASK(25, 24)
27b28be59aSCK Hu #define RG_HDMITX_PLL_AUTOK_LOAD BIT(23)
28a8a78274SChunfeng Yun #define RG_HDMITX_PLL_BAND GENMASK(21, 16)
29b28be59aSCK Hu #define RG_HDMITX_PLL_REF_SEL BIT(15)
30b28be59aSCK Hu #define RG_HDMITX_PLL_BIAS_EN BIT(14)
31b28be59aSCK Hu #define RG_HDMITX_PLL_BIAS_LPF_EN BIT(13)
32b28be59aSCK Hu #define RG_HDMITX_PLL_TXDIV_EN BIT(12)
33a8a78274SChunfeng Yun #define RG_HDMITX_PLL_TXDIV GENMASK(11, 10)
34b28be59aSCK Hu #define RG_HDMITX_PLL_LVROD_EN BIT(9)
35b28be59aSCK Hu #define RG_HDMITX_PLL_MONVC_EN BIT(8)
36b28be59aSCK Hu #define RG_HDMITX_PLL_MONCK_EN BIT(7)
37b28be59aSCK Hu #define RG_HDMITX_PLL_MONREF_EN BIT(6)
38b28be59aSCK Hu #define RG_HDMITX_PLL_TST_EN BIT(5)
39b28be59aSCK Hu #define RG_HDMITX_PLL_TST_CK_EN BIT(4)
40a8a78274SChunfeng Yun #define RG_HDMITX_PLL_TST_SEL GENMASK(3, 0)
41b28be59aSCK Hu #define HDMI_CON2 0x08
42a8a78274SChunfeng Yun #define RGS_HDMITX_PLL_AUTOK_BAND GENMASK(14, 8)
43b28be59aSCK Hu #define RGS_HDMITX_PLL_AUTOK_FAIL BIT(1)
44b28be59aSCK Hu #define RG_HDMITX_EN_TX_CKLDO BIT(0)
45b28be59aSCK Hu #define HDMI_CON3 0x0c
46a8a78274SChunfeng Yun #define RG_HDMITX_SER_EN GENMASK(31, 28)
47a8a78274SChunfeng Yun #define RG_HDMITX_PRD_EN GENMASK(27, 24)
48a8a78274SChunfeng Yun #define RG_HDMITX_PRD_IMP_EN GENMASK(23, 20)
49a8a78274SChunfeng Yun #define RG_HDMITX_DRV_EN GENMASK(19, 16)
50a8a78274SChunfeng Yun #define RG_HDMITX_DRV_IMP_EN GENMASK(15, 12)
51b28be59aSCK Hu #define RG_HDMITX_MHLCK_FORCE BIT(10)
52b28be59aSCK Hu #define RG_HDMITX_MHLCK_PPIX_EN BIT(9)
53b28be59aSCK Hu #define RG_HDMITX_MHLCK_EN BIT(8)
54a8a78274SChunfeng Yun #define RG_HDMITX_SER_DIN_SEL GENMASK(7, 4)
55b28be59aSCK Hu #define RG_HDMITX_SER_5T1_BIST_EN BIT(3)
56b28be59aSCK Hu #define RG_HDMITX_SER_BIST_TOG BIT(2)
57b28be59aSCK Hu #define RG_HDMITX_SER_DIN_TOG BIT(1)
58b28be59aSCK Hu #define RG_HDMITX_SER_CLKDIG_INV BIT(0)
59b28be59aSCK Hu #define HDMI_CON4 0x10
60a8a78274SChunfeng Yun #define RG_HDMITX_PRD_IBIAS_CLK GENMASK(27, 24)
61a8a78274SChunfeng Yun #define RG_HDMITX_PRD_IBIAS_D2 GENMASK(19, 16)
62a8a78274SChunfeng Yun #define RG_HDMITX_PRD_IBIAS_D1 GENMASK(11, 8)
63a8a78274SChunfeng Yun #define RG_HDMITX_PRD_IBIAS_D0 GENMASK(3, 0)
64b28be59aSCK Hu #define HDMI_CON5 0x14
65a8a78274SChunfeng Yun #define RG_HDMITX_DRV_IBIAS_CLK GENMASK(29, 24)
66a8a78274SChunfeng Yun #define RG_HDMITX_DRV_IBIAS_D2 GENMASK(21, 16)
67a8a78274SChunfeng Yun #define RG_HDMITX_DRV_IBIAS_D1 GENMASK(13, 8)
68a8a78274SChunfeng Yun #define RG_HDMITX_DRV_IBIAS_D0 GENMASK(5, 0)
69b28be59aSCK Hu #define HDMI_CON6 0x18
70a8a78274SChunfeng Yun #define RG_HDMITX_DRV_IMP_CLK GENMASK(29, 24)
71a8a78274SChunfeng Yun #define RG_HDMITX_DRV_IMP_D2 GENMASK(21, 16)
72a8a78274SChunfeng Yun #define RG_HDMITX_DRV_IMP_D1 GENMASK(13, 8)
73a8a78274SChunfeng Yun #define RG_HDMITX_DRV_IMP_D0 GENMASK(5, 0)
74b28be59aSCK Hu #define HDMI_CON7 0x1c
75a8a78274SChunfeng Yun #define RG_HDMITX_MHLCK_DRV_IBIAS GENMASK(31, 27)
76a8a78274SChunfeng Yun #define RG_HDMITX_SER_DIN GENMASK(25, 16)
77a8a78274SChunfeng Yun #define RG_HDMITX_CHLDC_TST GENMASK(15, 12)
78a8a78274SChunfeng Yun #define RG_HDMITX_CHLCK_TST GENMASK(11, 8)
79a8a78274SChunfeng Yun #define RG_HDMITX_RESERVE GENMASK(7, 0)
80b28be59aSCK Hu #define HDMI_CON8 0x20
81a8a78274SChunfeng Yun #define RGS_HDMITX_2T1_LEV GENMASK(19, 16)
82a8a78274SChunfeng Yun #define RGS_HDMITX_2T1_EDG GENMASK(15, 12)
83a8a78274SChunfeng Yun #define RGS_HDMITX_5T1_LEV GENMASK(11, 8)
84a8a78274SChunfeng Yun #define RGS_HDMITX_5T1_EDG GENMASK(7, 4)
85b28be59aSCK Hu #define RGS_HDMITX_PLUG_TST BIT(0)
86b28be59aSCK Hu
mtk_hdmi_pll_prepare(struct clk_hw * hw)87b28be59aSCK Hu static int mtk_hdmi_pll_prepare(struct clk_hw *hw)
88b28be59aSCK Hu {
89b28be59aSCK Hu struct mtk_hdmi_phy *hdmi_phy = to_mtk_hdmi_phy(hw);
90*0fb5e57eSChunfeng Yun void __iomem *base = hdmi_phy->regs;
91b28be59aSCK Hu
92*0fb5e57eSChunfeng Yun mtk_phy_set_bits(base + HDMI_CON1, RG_HDMITX_PLL_AUTOK_EN);
93*0fb5e57eSChunfeng Yun mtk_phy_set_bits(base + HDMI_CON0, RG_HDMITX_PLL_POSDIV);
94*0fb5e57eSChunfeng Yun mtk_phy_clear_bits(base + HDMI_CON3, RG_HDMITX_MHLCK_EN);
95*0fb5e57eSChunfeng Yun mtk_phy_set_bits(base + HDMI_CON1, RG_HDMITX_PLL_BIAS_EN);
96b28be59aSCK Hu usleep_range(100, 150);
97*0fb5e57eSChunfeng Yun mtk_phy_set_bits(base + HDMI_CON0, RG_HDMITX_PLL_EN);
98b28be59aSCK Hu usleep_range(100, 150);
99*0fb5e57eSChunfeng Yun mtk_phy_set_bits(base + HDMI_CON1, RG_HDMITX_PLL_BIAS_LPF_EN);
100*0fb5e57eSChunfeng Yun mtk_phy_set_bits(base + HDMI_CON1, RG_HDMITX_PLL_TXDIV_EN);
101b28be59aSCK Hu
102b28be59aSCK Hu return 0;
103b28be59aSCK Hu }
104b28be59aSCK Hu
mtk_hdmi_pll_unprepare(struct clk_hw * hw)105b28be59aSCK Hu static void mtk_hdmi_pll_unprepare(struct clk_hw *hw)
106b28be59aSCK Hu {
107b28be59aSCK Hu struct mtk_hdmi_phy *hdmi_phy = to_mtk_hdmi_phy(hw);
108*0fb5e57eSChunfeng Yun void __iomem *base = hdmi_phy->regs;
109b28be59aSCK Hu
110*0fb5e57eSChunfeng Yun mtk_phy_clear_bits(base + HDMI_CON1, RG_HDMITX_PLL_TXDIV_EN);
111*0fb5e57eSChunfeng Yun mtk_phy_clear_bits(base + HDMI_CON1, RG_HDMITX_PLL_BIAS_LPF_EN);
112b28be59aSCK Hu usleep_range(100, 150);
113*0fb5e57eSChunfeng Yun mtk_phy_clear_bits(base + HDMI_CON0, RG_HDMITX_PLL_EN);
114b28be59aSCK Hu usleep_range(100, 150);
115*0fb5e57eSChunfeng Yun mtk_phy_clear_bits(base + HDMI_CON1, RG_HDMITX_PLL_BIAS_EN);
116*0fb5e57eSChunfeng Yun mtk_phy_clear_bits(base + HDMI_CON0, RG_HDMITX_PLL_POSDIV);
117*0fb5e57eSChunfeng Yun mtk_phy_clear_bits(base + HDMI_CON1, RG_HDMITX_PLL_AUTOK_EN);
118b28be59aSCK Hu usleep_range(100, 150);
119b28be59aSCK Hu }
120b28be59aSCK Hu
mtk_hdmi_pll_round_rate(struct clk_hw * hw,unsigned long rate,unsigned long * parent_rate)121b28be59aSCK Hu static long mtk_hdmi_pll_round_rate(struct clk_hw *hw, unsigned long rate,
122b28be59aSCK Hu unsigned long *parent_rate)
123b28be59aSCK Hu {
124b28be59aSCK Hu struct mtk_hdmi_phy *hdmi_phy = to_mtk_hdmi_phy(hw);
125b28be59aSCK Hu
126b28be59aSCK Hu hdmi_phy->pll_rate = rate;
127b28be59aSCK Hu if (rate <= 74250000)
128b28be59aSCK Hu *parent_rate = rate;
129b28be59aSCK Hu else
130b28be59aSCK Hu *parent_rate = rate / 2;
131b28be59aSCK Hu
132b28be59aSCK Hu return rate;
133b28be59aSCK Hu }
134b28be59aSCK Hu
mtk_hdmi_pll_set_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)135b28be59aSCK Hu static int mtk_hdmi_pll_set_rate(struct clk_hw *hw, unsigned long rate,
136b28be59aSCK Hu unsigned long parent_rate)
137b28be59aSCK Hu {
138b28be59aSCK Hu struct mtk_hdmi_phy *hdmi_phy = to_mtk_hdmi_phy(hw);
139*0fb5e57eSChunfeng Yun void __iomem *base = hdmi_phy->regs;
140b28be59aSCK Hu unsigned int pre_div;
141b28be59aSCK Hu unsigned int div;
142b28be59aSCK Hu unsigned int pre_ibias;
143b28be59aSCK Hu unsigned int hdmi_ibias;
144b28be59aSCK Hu unsigned int imp_en;
145b28be59aSCK Hu
146b28be59aSCK Hu dev_dbg(hdmi_phy->dev, "%s: %lu Hz, parent: %lu Hz\n", __func__,
147b28be59aSCK Hu rate, parent_rate);
148b28be59aSCK Hu
149b28be59aSCK Hu if (rate <= 27000000) {
150b28be59aSCK Hu pre_div = 0;
151b28be59aSCK Hu div = 3;
152b28be59aSCK Hu } else if (rate <= 74250000) {
153b28be59aSCK Hu pre_div = 1;
154b28be59aSCK Hu div = 2;
155b28be59aSCK Hu } else {
156b28be59aSCK Hu pre_div = 1;
157b28be59aSCK Hu div = 1;
158b28be59aSCK Hu }
159b28be59aSCK Hu
160*0fb5e57eSChunfeng Yun mtk_phy_update_field(base + HDMI_CON0, RG_HDMITX_PLL_PREDIV, pre_div);
161*0fb5e57eSChunfeng Yun mtk_phy_set_bits(base + HDMI_CON0, RG_HDMITX_PLL_POSDIV);
162*0fb5e57eSChunfeng Yun mtk_phy_update_bits(base + HDMI_CON0,
163*0fb5e57eSChunfeng Yun RG_HDMITX_PLL_IC | RG_HDMITX_PLL_IR,
164309b4fecSChunfeng Yun FIELD_PREP(RG_HDMITX_PLL_IC, 0x1) |
165*0fb5e57eSChunfeng Yun FIELD_PREP(RG_HDMITX_PLL_IR, 0x1));
166*0fb5e57eSChunfeng Yun mtk_phy_update_field(base + HDMI_CON1, RG_HDMITX_PLL_TXDIV, div);
167*0fb5e57eSChunfeng Yun mtk_phy_update_bits(base + HDMI_CON0,
168*0fb5e57eSChunfeng Yun RG_HDMITX_PLL_FBKSEL | RG_HDMITX_PLL_FBKDIV,
169309b4fecSChunfeng Yun FIELD_PREP(RG_HDMITX_PLL_FBKSEL, 0x1) |
170*0fb5e57eSChunfeng Yun FIELD_PREP(RG_HDMITX_PLL_FBKDIV, 19));
171*0fb5e57eSChunfeng Yun mtk_phy_update_field(base + HDMI_CON1, RG_HDMITX_PLL_DIVEN, 0x2);
172*0fb5e57eSChunfeng Yun mtk_phy_update_bits(base + HDMI_CON0,
173*0fb5e57eSChunfeng Yun RG_HDMITX_PLL_BP | RG_HDMITX_PLL_BC |
174*0fb5e57eSChunfeng Yun RG_HDMITX_PLL_BR,
175309b4fecSChunfeng Yun FIELD_PREP(RG_HDMITX_PLL_BP, 0xc) |
176309b4fecSChunfeng Yun FIELD_PREP(RG_HDMITX_PLL_BC, 0x2) |
177*0fb5e57eSChunfeng Yun FIELD_PREP(RG_HDMITX_PLL_BR, 0x1));
178b28be59aSCK Hu if (rate < 165000000) {
179*0fb5e57eSChunfeng Yun mtk_phy_clear_bits(base + HDMI_CON3, RG_HDMITX_PRD_IMP_EN);
180b28be59aSCK Hu pre_ibias = 0x3;
181b28be59aSCK Hu imp_en = 0x0;
182b28be59aSCK Hu hdmi_ibias = hdmi_phy->ibias;
183b28be59aSCK Hu } else {
184*0fb5e57eSChunfeng Yun mtk_phy_set_bits(base + HDMI_CON3, RG_HDMITX_PRD_IMP_EN);
185b28be59aSCK Hu pre_ibias = 0x6;
186b28be59aSCK Hu imp_en = 0xf;
187b28be59aSCK Hu hdmi_ibias = hdmi_phy->ibias_up;
188b28be59aSCK Hu }
189*0fb5e57eSChunfeng Yun mtk_phy_update_bits(base + HDMI_CON4,
190*0fb5e57eSChunfeng Yun RG_HDMITX_PRD_IBIAS_CLK | RG_HDMITX_PRD_IBIAS_D2 |
191*0fb5e57eSChunfeng Yun RG_HDMITX_PRD_IBIAS_D1 | RG_HDMITX_PRD_IBIAS_D0,
192309b4fecSChunfeng Yun FIELD_PREP(RG_HDMITX_PRD_IBIAS_CLK, pre_ibias) |
193309b4fecSChunfeng Yun FIELD_PREP(RG_HDMITX_PRD_IBIAS_D2, pre_ibias) |
194309b4fecSChunfeng Yun FIELD_PREP(RG_HDMITX_PRD_IBIAS_D1, pre_ibias) |
195*0fb5e57eSChunfeng Yun FIELD_PREP(RG_HDMITX_PRD_IBIAS_D0, pre_ibias));
196*0fb5e57eSChunfeng Yun mtk_phy_update_field(base + HDMI_CON3, RG_HDMITX_DRV_IMP_EN, imp_en);
197*0fb5e57eSChunfeng Yun mtk_phy_update_bits(base + HDMI_CON6,
198*0fb5e57eSChunfeng Yun RG_HDMITX_DRV_IMP_CLK | RG_HDMITX_DRV_IMP_D2 |
199*0fb5e57eSChunfeng Yun RG_HDMITX_DRV_IMP_D1 | RG_HDMITX_DRV_IMP_D0,
200309b4fecSChunfeng Yun FIELD_PREP(RG_HDMITX_DRV_IMP_CLK, hdmi_phy->drv_imp_clk) |
201309b4fecSChunfeng Yun FIELD_PREP(RG_HDMITX_DRV_IMP_D2, hdmi_phy->drv_imp_d2) |
202309b4fecSChunfeng Yun FIELD_PREP(RG_HDMITX_DRV_IMP_D1, hdmi_phy->drv_imp_d1) |
203*0fb5e57eSChunfeng Yun FIELD_PREP(RG_HDMITX_DRV_IMP_D0, hdmi_phy->drv_imp_d0));
204*0fb5e57eSChunfeng Yun mtk_phy_update_bits(base + HDMI_CON5,
205*0fb5e57eSChunfeng Yun RG_HDMITX_DRV_IBIAS_CLK | RG_HDMITX_DRV_IBIAS_D2 |
206*0fb5e57eSChunfeng Yun RG_HDMITX_DRV_IBIAS_D1 | RG_HDMITX_DRV_IBIAS_D0,
207309b4fecSChunfeng Yun FIELD_PREP(RG_HDMITX_DRV_IBIAS_CLK, hdmi_ibias) |
208309b4fecSChunfeng Yun FIELD_PREP(RG_HDMITX_DRV_IBIAS_D2, hdmi_ibias) |
209309b4fecSChunfeng Yun FIELD_PREP(RG_HDMITX_DRV_IBIAS_D1, hdmi_ibias) |
210*0fb5e57eSChunfeng Yun FIELD_PREP(RG_HDMITX_DRV_IBIAS_D0, hdmi_ibias));
211b28be59aSCK Hu return 0;
212b28be59aSCK Hu }
213b28be59aSCK Hu
mtk_hdmi_pll_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)214b28be59aSCK Hu static unsigned long mtk_hdmi_pll_recalc_rate(struct clk_hw *hw,
215b28be59aSCK Hu unsigned long parent_rate)
216b28be59aSCK Hu {
217b28be59aSCK Hu struct mtk_hdmi_phy *hdmi_phy = to_mtk_hdmi_phy(hw);
218b28be59aSCK Hu
219b28be59aSCK Hu return hdmi_phy->pll_rate;
220b28be59aSCK Hu }
221b28be59aSCK Hu
222b28be59aSCK Hu static const struct clk_ops mtk_hdmi_phy_pll_ops = {
223b28be59aSCK Hu .prepare = mtk_hdmi_pll_prepare,
224b28be59aSCK Hu .unprepare = mtk_hdmi_pll_unprepare,
225b28be59aSCK Hu .set_rate = mtk_hdmi_pll_set_rate,
226b28be59aSCK Hu .round_rate = mtk_hdmi_pll_round_rate,
227b28be59aSCK Hu .recalc_rate = mtk_hdmi_pll_recalc_rate,
228b28be59aSCK Hu };
229b28be59aSCK Hu
mtk_hdmi_phy_enable_tmds(struct mtk_hdmi_phy * hdmi_phy)230b28be59aSCK Hu static void mtk_hdmi_phy_enable_tmds(struct mtk_hdmi_phy *hdmi_phy)
231b28be59aSCK Hu {
232*0fb5e57eSChunfeng Yun mtk_phy_set_bits(hdmi_phy->regs + HDMI_CON3,
233b28be59aSCK Hu RG_HDMITX_SER_EN | RG_HDMITX_PRD_EN |
234b28be59aSCK Hu RG_HDMITX_DRV_EN);
235b28be59aSCK Hu usleep_range(100, 150);
236b28be59aSCK Hu }
237b28be59aSCK Hu
mtk_hdmi_phy_disable_tmds(struct mtk_hdmi_phy * hdmi_phy)238b28be59aSCK Hu static void mtk_hdmi_phy_disable_tmds(struct mtk_hdmi_phy *hdmi_phy)
239b28be59aSCK Hu {
240*0fb5e57eSChunfeng Yun mtk_phy_clear_bits(hdmi_phy->regs + HDMI_CON3,
241b28be59aSCK Hu RG_HDMITX_DRV_EN | RG_HDMITX_PRD_EN |
242b28be59aSCK Hu RG_HDMITX_SER_EN);
243b28be59aSCK Hu }
244b28be59aSCK Hu
245b28be59aSCK Hu struct mtk_hdmi_phy_conf mtk_hdmi_phy_8173_conf = {
246b28be59aSCK Hu .flags = CLK_SET_RATE_PARENT | CLK_SET_RATE_GATE,
247b28be59aSCK Hu .hdmi_phy_clk_ops = &mtk_hdmi_phy_pll_ops,
248b28be59aSCK Hu .hdmi_phy_enable_tmds = mtk_hdmi_phy_enable_tmds,
249b28be59aSCK Hu .hdmi_phy_disable_tmds = mtk_hdmi_phy_disable_tmds,
250b28be59aSCK Hu };
251b28be59aSCK Hu
252b28be59aSCK Hu MODULE_AUTHOR("Jie Qiu <jie.qiu@mediatek.com>");
253b28be59aSCK Hu MODULE_DESCRIPTION("MediaTek MT8173 HDMI PHY Driver");
254b28be59aSCK Hu MODULE_LICENSE("GPL v2");
255