xref: /OK3568_Linux_fs/kernel/drivers/phy/rockchip/phy-rockchip-typec.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
4  * Author: Chris Zhong <zyw@rock-chips.com>
5  *         Kever Yang <kever.yang@rock-chips.com>
6  *
7  * The ROCKCHIP Type-C PHY has two PLL clocks. The first PLL clock
8  * is used for USB3, the second PLL clock is used for DP. This Type-C PHY has
9  * 3 working modes: USB3 only mode, DP only mode, and USB3+DP mode.
10  * At USB3 only mode, both PLL clocks need to be initialized, this allows the
11  * PHY to switch mode between USB3 and USB3+DP, without disconnecting the USB
12  * device.
13  * In The DP only mode, only the DP PLL needs to be powered on, and the 4 lanes
14  * are all used for DP.
15  *
16  * This driver gets extcon cable state and property, then decides which mode to
17  * select:
18  *
19  * 1. USB3 only mode:
20  *    EXTCON_USB or EXTCON_USB_HOST state is true, and
21  *    EXTCON_PROP_USB_SS property is true.
22  *    EXTCON_DISP_DP state is false.
23  *
24  * 2. DP only mode:
25  *    EXTCON_DISP_DP state is true, and
26  *    EXTCON_PROP_USB_SS property is false.
27  *    If EXTCON_USB_HOST state is true, it is DP + USB2 mode, since the USB2 phy
28  *    is a separate phy, so this case is still DP only mode.
29  *
30  * 3. USB3+DP mode:
31  *    EXTCON_USB_HOST and EXTCON_DISP_DP are both true, and
32  *    EXTCON_PROP_USB_SS property is true.
33  *
34  * This Type-C PHY driver supports normal and flip orientation. The orientation
35  * is reported by the EXTCON_PROP_USB_TYPEC_POLARITY property: true is flip
36  * orientation, false is normal orientation.
37  */
38 
39 #include <linux/clk.h>
40 #include <linux/clk-provider.h>
41 #include <linux/delay.h>
42 #include <linux/extcon.h>
43 #include <linux/io.h>
44 #include <linux/iopoll.h>
45 #include <linux/kernel.h>
46 #include <linux/module.h>
47 #include <linux/mutex.h>
48 #include <linux/of.h>
49 #include <linux/of_address.h>
50 #include <linux/of_platform.h>
51 #include <linux/platform_device.h>
52 #include <linux/regmap.h>
53 #include <linux/reset.h>
54 #include <linux/usb/typec_dp.h>
55 #include <linux/usb/typec_mux.h>
56 
57 #include <linux/mfd/syscon.h>
58 #include <linux/phy/phy.h>
59 #include <linux/phy/phy-rockchip-typec.h>
60 
61 #define CMN_SSM_BANDGAP			(0x21 << 2)
62 #define CMN_SSM_BIAS			(0x22 << 2)
63 #define CMN_PLLSM0_PLLEN		(0x29 << 2)
64 #define CMN_PLLSM0_PLLPRE		(0x2a << 2)
65 #define CMN_PLLSM0_PLLVREF		(0x2b << 2)
66 #define CMN_PLLSM0_PLLLOCK		(0x2c << 2)
67 #define CMN_PLLSM1_PLLEN		(0x31 << 2)
68 #define CMN_PLLSM1_PLLPRE		(0x32 << 2)
69 #define CMN_PLLSM1_PLLVREF		(0x33 << 2)
70 #define CMN_PLLSM1_PLLLOCK		(0x34 << 2)
71 #define CMN_PLLSM1_USER_DEF_CTRL	(0x37 << 2)
72 #define CMN_ICAL_OVRD			(0xc1 << 2)
73 #define CMN_PLL0_VCOCAL_OVRD		(0x83 << 2)
74 #define CMN_PLL0_VCOCAL_INIT		(0x84 << 2)
75 #define CMN_PLL0_VCOCAL_ITER		(0x85 << 2)
76 #define CMN_PLL0_LOCK_REFCNT_START	(0x90 << 2)
77 #define CMN_PLL0_LOCK_PLLCNT_START	(0x92 << 2)
78 #define CMN_PLL0_LOCK_PLLCNT_THR	(0x93 << 2)
79 #define CMN_PLL0_INTDIV			(0x94 << 2)
80 #define CMN_PLL0_FRACDIV		(0x95 << 2)
81 #define CMN_PLL0_HIGH_THR		(0x96 << 2)
82 #define CMN_PLL0_DSM_DIAG		(0x97 << 2)
83 #define CMN_PLL0_SS_CTRL1		(0x98 << 2)
84 #define CMN_PLL0_SS_CTRL2		(0x99 << 2)
85 #define CMN_PLL1_VCOCAL_START		(0xa1 << 2)
86 #define CMN_PLL1_VCOCAL_OVRD		(0xa3 << 2)
87 #define CMN_PLL1_VCOCAL_INIT		(0xa4 << 2)
88 #define CMN_PLL1_VCOCAL_ITER		(0xa5 << 2)
89 #define CMN_PLL1_LOCK_REFCNT_START	(0xb0 << 2)
90 #define CMN_PLL1_LOCK_PLLCNT_START	(0xb2 << 2)
91 #define CMN_PLL1_LOCK_PLLCNT_THR	(0xb3 << 2)
92 #define CMN_PLL1_INTDIV			(0xb4 << 2)
93 #define CMN_PLL1_FRACDIV		(0xb5 << 2)
94 #define CMN_PLL1_HIGH_THR		(0xb6 << 2)
95 #define CMN_PLL1_DSM_DIAG		(0xb7 << 2)
96 #define CMN_PLL1_SS_CTRL1		(0xb8 << 2)
97 #define CMN_PLL1_SS_CTRL2		(0xb9 << 2)
98 #define CMN_RXCAL_OVRD			(0xd1 << 2)
99 
100 #define CMN_TXPUCAL_CTRL		(0xe0 << 2)
101 #define CMN_TXPUCAL_OVRD		(0xe1 << 2)
102 #define CMN_TXPDCAL_CTRL		(0xf0 << 2)
103 #define CMN_TXPDCAL_OVRD		(0xf1 << 2)
104 
105 /* For CMN_TXPUCAL_CTRL, CMN_TXPDCAL_CTRL */
106 #define CMN_TXPXCAL_START		BIT(15)
107 #define CMN_TXPXCAL_DONE		BIT(14)
108 #define CMN_TXPXCAL_NO_RESPONSE		BIT(13)
109 #define CMN_TXPXCAL_CURRENT_RESPONSE	BIT(12)
110 
111 #define CMN_TXPU_ADJ_CTRL		(0x108 << 2)
112 #define CMN_TXPD_ADJ_CTRL		(0x10c << 2)
113 
114 /*
115  * For CMN_TXPUCAL_CTRL, CMN_TXPDCAL_CTRL,
116  *     CMN_TXPU_ADJ_CTRL, CMN_TXPDCAL_CTRL
117  *
118  * NOTE: some of these registers are documented to be 2's complement
119  * signed numbers, but then documented to be always positive.  Weird.
120  * In such a case, using CMN_CALIB_CODE_POS() avoids the unnecessary
121  * sign extension.
122  */
123 #define CMN_CALIB_CODE_WIDTH	7
124 #define CMN_CALIB_CODE_OFFSET	0
125 #define CMN_CALIB_CODE_MASK	GENMASK(CMN_CALIB_CODE_WIDTH, 0)
126 #define CMN_CALIB_CODE(x)	\
127 	sign_extend32((x) >> CMN_CALIB_CODE_OFFSET, CMN_CALIB_CODE_WIDTH)
128 
129 #define CMN_CALIB_CODE_POS_MASK	GENMASK(CMN_CALIB_CODE_WIDTH - 1, 0)
130 #define CMN_CALIB_CODE_POS(x)	\
131 	(((x) >> CMN_CALIB_CODE_OFFSET) & CMN_CALIB_CODE_POS_MASK)
132 
133 #define CMN_DIAG_PLL0_FBH_OVRD		(0x1c0 << 2)
134 #define CMN_DIAG_PLL0_FBL_OVRD		(0x1c1 << 2)
135 #define CMN_DIAG_PLL0_OVRD		(0x1c2 << 2)
136 #define CMN_DIAG_PLL0_V2I_TUNE		(0x1c5 << 2)
137 #define CMN_DIAG_PLL0_CP_TUNE		(0x1c6 << 2)
138 #define CMN_DIAG_PLL0_LF_PROG		(0x1c7 << 2)
139 #define CMN_DIAG_PLL1_FBH_OVRD		(0x1d0 << 2)
140 #define CMN_DIAG_PLL1_FBL_OVRD		(0x1d1 << 2)
141 #define CMN_DIAG_PLL1_OVRD		(0x1d2 << 2)
142 #define CMN_DIAG_PLL1_V2I_TUNE		(0x1d5 << 2)
143 #define CMN_DIAG_PLL1_CP_TUNE		(0x1d6 << 2)
144 #define CMN_DIAG_PLL1_LF_PROG		(0x1d7 << 2)
145 #define CMN_DIAG_PLL1_PTATIS_TUNE1	(0x1d8 << 2)
146 #define CMN_DIAG_PLL1_PTATIS_TUNE2	(0x1d9 << 2)
147 #define CMN_DIAG_PLL1_INCLK_CTRL	(0x1da << 2)
148 #define CMN_DIAG_HSCLK_SEL		(0x1e0 << 2)
149 
150 #define XCVR_PSM_RCTRL(n)		((0x4001 | ((n) << 9)) << 2)
151 #define XCVR_PSM_CAL_TMR(n)		((0x4002 | ((n) << 9)) << 2)
152 #define XCVR_PSM_A0IN_TMR(n)		((0x4003 | ((n) << 9)) << 2)
153 #define TX_TXCC_CAL_SCLR_MULT(n)	((0x4047 | ((n) << 9)) << 2)
154 #define TX_TXCC_CPOST_MULT_00(n)	((0x404c | ((n) << 9)) << 2)
155 #define TX_TXCC_CPOST_MULT_01(n)	((0x404d | ((n) << 9)) << 2)
156 #define TX_TXCC_CPOST_MULT_10(n)	((0x404e | ((n) << 9)) << 2)
157 #define TX_TXCC_CPOST_MULT_11(n)	((0x404f | ((n) << 9)) << 2)
158 #define TX_TXCC_MGNFS_MULT_000(n)	((0x4050 | ((n) << 9)) << 2)
159 #define TX_TXCC_MGNFS_MULT_001(n)	((0x4051 | ((n) << 9)) << 2)
160 #define TX_TXCC_MGNFS_MULT_010(n)	((0x4052 | ((n) << 9)) << 2)
161 #define TX_TXCC_MGNFS_MULT_011(n)	((0x4053 | ((n) << 9)) << 2)
162 #define TX_TXCC_MGNFS_MULT_100(n)	((0x4054 | ((n) << 9)) << 2)
163 #define TX_TXCC_MGNFS_MULT_101(n)	((0x4055 | ((n) << 9)) << 2)
164 #define TX_TXCC_MGNFS_MULT_110(n)	((0x4056 | ((n) << 9)) << 2)
165 #define TX_TXCC_MGNFS_MULT_111(n)	((0x4057 | ((n) << 9)) << 2)
166 #define TX_TXCC_MGNLS_MULT_000(n)	((0x4058 | ((n) << 9)) << 2)
167 #define TX_TXCC_MGNLS_MULT_001(n)	((0x4059 | ((n) << 9)) << 2)
168 #define TX_TXCC_MGNLS_MULT_010(n)	((0x405a | ((n) << 9)) << 2)
169 #define TX_TXCC_MGNLS_MULT_011(n)	((0x405b | ((n) << 9)) << 2)
170 #define TX_TXCC_MGNLS_MULT_100(n)	((0x405c | ((n) << 9)) << 2)
171 #define TX_TXCC_MGNLS_MULT_101(n)	((0x405d | ((n) << 9)) << 2)
172 #define TX_TXCC_MGNLS_MULT_110(n)	((0x405e | ((n) << 9)) << 2)
173 #define TX_TXCC_MGNLS_MULT_111(n)	((0x405f | ((n) << 9)) << 2)
174 
175 #define XCVR_DIAG_PLLDRC_CTRL(n)	((0x40e0 | ((n) << 9)) << 2)
176 #define XCVR_DIAG_BIDI_CTRL(n)		((0x40e8 | ((n) << 9)) << 2)
177 #define XCVR_DIAG_LANE_FCM_EN_MGN(n)	((0x40f2 | ((n) << 9)) << 2)
178 #define TX_PSC_A0(n)			((0x4100 | ((n) << 9)) << 2)
179 #define TX_PSC_A1(n)			((0x4101 | ((n) << 9)) << 2)
180 #define TX_PSC_A2(n)			((0x4102 | ((n) << 9)) << 2)
181 #define TX_PSC_A3(n)			((0x4103 | ((n) << 9)) << 2)
182 #define TX_RCVDET_CTRL(n)		((0x4120 | ((n) << 9)) << 2)
183 #define TX_RCVDET_EN_TMR(n)		((0x4122 | ((n) << 9)) << 2)
184 #define TX_RCVDET_ST_TMR(n)		((0x4123 | ((n) << 9)) << 2)
185 #define TX_DIAG_TX_DRV(n)		((0x41e1 | ((n) << 9)) << 2)
186 #define TX_DIAG_BGREF_PREDRV_DELAY	(0x41e7 << 2)
187 
188 /* Use this for "n" in macros like "_MULT_XXX" to target the aux channel */
189 #define AUX_CH_LANE			8
190 
191 #define TX_ANA_CTRL_REG_1		(0x5020 << 2)
192 
193 #define TXDA_DP_AUX_EN			BIT(15)
194 #define AUXDA_SE_EN			BIT(14)
195 #define TXDA_CAL_LATCH_EN		BIT(13)
196 #define AUXDA_POLARITY			BIT(12)
197 #define TXDA_DRV_POWER_ISOLATION_EN	BIT(11)
198 #define TXDA_DRV_POWER_EN_PH_2_N	BIT(10)
199 #define TXDA_DRV_POWER_EN_PH_1_N	BIT(9)
200 #define TXDA_BGREF_EN			BIT(8)
201 #define TXDA_DRV_LDO_EN			BIT(7)
202 #define TXDA_DECAP_EN_DEL		BIT(6)
203 #define TXDA_DECAP_EN			BIT(5)
204 #define TXDA_UPHY_SUPPLY_EN_DEL		BIT(4)
205 #define TXDA_UPHY_SUPPLY_EN		BIT(3)
206 #define TXDA_LOW_LEAKAGE_EN		BIT(2)
207 #define TXDA_DRV_IDLE_LOWI_EN		BIT(1)
208 #define TXDA_DRV_CMN_MODE_EN		BIT(0)
209 
210 #define TX_ANA_CTRL_REG_2		(0x5021 << 2)
211 
212 #define AUXDA_DEBOUNCING_CLK		BIT(15)
213 #define TXDA_LPBK_RECOVERED_CLK_EN	BIT(14)
214 #define TXDA_LPBK_ISI_GEN_EN		BIT(13)
215 #define TXDA_LPBK_SERIAL_EN		BIT(12)
216 #define TXDA_LPBK_LINE_EN		BIT(11)
217 #define TXDA_DRV_LDO_REDC_SINKIQ	BIT(10)
218 #define XCVR_DECAP_EN_DEL		BIT(9)
219 #define XCVR_DECAP_EN			BIT(8)
220 #define TXDA_MPHY_ENABLE_HS_NT		BIT(7)
221 #define TXDA_MPHY_SA_MODE		BIT(6)
222 #define TXDA_DRV_LDO_RBYR_FB_EN		BIT(5)
223 #define TXDA_DRV_RST_PULL_DOWN		BIT(4)
224 #define TXDA_DRV_LDO_BG_FB_EN		BIT(3)
225 #define TXDA_DRV_LDO_BG_REF_EN		BIT(2)
226 #define TXDA_DRV_PREDRV_EN_DEL		BIT(1)
227 #define TXDA_DRV_PREDRV_EN		BIT(0)
228 
229 #define TXDA_COEFF_CALC_CTRL		(0x5022 << 2)
230 
231 #define TX_HIGH_Z			BIT(6)
232 #define TX_VMARGIN_OFFSET		3
233 #define TX_VMARGIN_MASK			0x7
234 #define LOW_POWER_SWING_EN		BIT(2)
235 #define TX_FCM_DRV_MAIN_EN		BIT(1)
236 #define TX_FCM_FULL_MARGIN		BIT(0)
237 
238 #define TX_DIG_CTRL_REG_2		(0x5024 << 2)
239 
240 #define TX_HIGH_Z_TM_EN			BIT(15)
241 #define TX_RESCAL_CODE_OFFSET		0
242 #define TX_RESCAL_CODE_MASK		0x3f
243 
244 #define TXDA_CYA_AUXDA_CYA		(0x5025 << 2)
245 #define TX_ANA_CTRL_REG_3		(0x5026 << 2)
246 #define TX_ANA_CTRL_REG_4		(0x5027 << 2)
247 #define TX_ANA_CTRL_REG_5		(0x5029 << 2)
248 
249 #define RX_PSC_A0(n)			((0x8000 | ((n) << 9)) << 2)
250 #define RX_PSC_A1(n)			((0x8001 | ((n) << 9)) << 2)
251 #define RX_PSC_A2(n)			((0x8002 | ((n) << 9)) << 2)
252 #define RX_PSC_A3(n)			((0x8003 | ((n) << 9)) << 2)
253 #define RX_PSC_CAL(n)			((0x8006 | ((n) << 9)) << 2)
254 #define RX_PSC_RDY(n)			((0x8007 | ((n) << 9)) << 2)
255 #define RX_IQPI_ILL_CAL_OVRD		(0x8023 << 2)
256 #define RX_EPI_ILL_CAL_OVRD		(0x8033 << 2)
257 #define RX_SDCAL0_OVRD			(0x8041 << 2)
258 #define RX_SDCAL1_OVRD			(0x8049 << 2)
259 #define RX_SLC_INIT			(0x806d << 2)
260 #define RX_SLC_RUN			(0x806e << 2)
261 #define RX_CDRLF_CNFG2			(0x8081 << 2)
262 #define RX_SIGDET_HL_FILT_TMR(n)	((0x8090 | ((n) << 9)) << 2)
263 #define RX_SLC_IOP0_OVRD		(0x8101 << 2)
264 #define RX_SLC_IOP1_OVRD		(0x8105 << 2)
265 #define RX_SLC_QOP0_OVRD		(0x8109 << 2)
266 #define RX_SLC_QOP1_OVRD		(0x810d << 2)
267 #define RX_SLC_EOP0_OVRD		(0x8111 << 2)
268 #define RX_SLC_EOP1_OVRD		(0x8115 << 2)
269 #define RX_SLC_ION0_OVRD		(0x8119 << 2)
270 #define RX_SLC_ION1_OVRD		(0x811d << 2)
271 #define RX_SLC_QON0_OVRD		(0x8121 << 2)
272 #define RX_SLC_QON1_OVRD		(0x8125 << 2)
273 #define RX_SLC_EON0_OVRD		(0x8129 << 2)
274 #define RX_SLC_EON1_OVRD		(0x812d << 2)
275 #define RX_SLC_IEP0_OVRD		(0x8131 << 2)
276 #define RX_SLC_IEP1_OVRD		(0x8135 << 2)
277 #define RX_SLC_QEP0_OVRD		(0x8139 << 2)
278 #define RX_SLC_QEP1_OVRD		(0x813d << 2)
279 #define RX_SLC_EEP0_OVRD		(0x8141 << 2)
280 #define RX_SLC_EEP1_OVRD		(0x8145 << 2)
281 #define RX_SLC_IEN0_OVRD		(0x8149 << 2)
282 #define RX_SLC_IEN1_OVRD		(0x814d << 2)
283 #define RX_SLC_QEN0_OVRD		(0x8151 << 2)
284 #define RX_SLC_QEN1_OVRD		(0x8155 << 2)
285 #define RX_SLC_EEN0_OVRD		(0x8159 << 2)
286 #define RX_SLC_EEN1_OVRD		(0x815d << 2)
287 #define RX_REE_CTRL_DATA_MASK(n)	((0x81bb | ((n) << 9)) << 2)
288 #define RX_DIAG_SIGDET_TUNE(n)		((0x81dc | ((n) << 9)) << 2)
289 #define RX_DIAG_SC2C_DELAY		(0x81e1 << 2)
290 
291 #define PHY_PMA_LANE_CFG		(0xc000 << 2)
292 #define PMA_LANE3_DP_LANE_SEL(x)	(((x) & 0x3) << 14)
293 #define PMA_LANE3_INTERFACE_SEL(x)	(((x) & 0x1) << 12)
294 #define PMA_LANE2_DP_LANE_SEL(x)	(((x) & 0x3) << 10)
295 #define PMA_LANE2_INTERFACE_SEL(x)	(((x) & 0x1) << 8)
296 #define PMA_LANE1_DP_LANE_SEL(x)	(((x) & 0x3) << 6)
297 #define PMA_LANE1_INTERFACE_SEL(x)	(((x) & 0x1) << 4)
298 #define PMA_LANE0_DP_LANE_SEL(x)	(((x) & 0x3) << 2)
299 #define PMA_LANE0_INTERFACE_SEL(x)	(((x) & 0x1) << 0)
300 #define PIPE_CMN_CTRL1			(0xc001 << 2)
301 #define PIPE_CMN_CTRL2			(0xc002 << 2)
302 #define PIPE_COM_LOCK_CFG1		(0xc003 << 2)
303 #define PIPE_COM_LOCK_CFG2		(0xc004 << 2)
304 #define PIPE_RCV_DET_INH		(0xc005 << 2)
305 #define PHY_DP_MODE_CTL			(0xc008 << 2)
306 #define PHY_DP_LANE_DISABLE		GENMASK(15, 12)
307 #define PHY_DP_LANE_3_DISABLE		BIT(15)
308 #define PHY_DP_LANE_2_DISABLE		BIT(14)
309 #define PHY_DP_LANE_1_DISABLE		BIT(13)
310 #define PHY_DP_LANE_0_DISABLE		BIT(12)
311 #define PHY_DP_POWER_STATE_ACK_MASK	GENMASK(7, 4)
312 #define PHY_DP_POWER_STATE_ACK_SHIFT	4
313 #define PHY_DP_POWER_STATE_MASK		GENMASK(3, 0)
314 #define PHY_DP_CLK_CTL			(0xc009 << 2)
315 #define DP_PLL_CLOCK_ENABLE_ACK		BIT(3)
316 #define DP_PLL_CLOCK_ENABLE_MASK	BIT(2)
317 #define DP_PLL_CLOCK_DISABLE		0
318 #define DP_PLL_READY			BIT(1)
319 #define DP_PLL_ENABLE_MASK		BIT(0)
320 #define DP_PLL_ENABLE			BIT(0)
321 #define DP_PLL_DISABLE			0
322 #define DP_CLK_CTL			(0xc009 << 2)
323 #define STS				(0xc00F << 2)
324 #define PHY_ISO_CMN_CTRL		(0xc010 << 2)
325 #define PHY_DP_TX_CTL			(0xc408 << 2)
326 #define PMA_CMN_CTRL1			(0xc800 << 2)
327 #define PHY_PMA_ISO_CMN_CTRL		(0xc810 << 2)
328 #define PHY_ISOLATION_CTRL		(0xc81f << 2)
329 #define PHY_PMA_ISO_XCVR_CTRL(n)	((0xcc11 | ((n) << 6)) << 2)
330 #define PHY_PMA_ISO_LINK_MODE(n)	((0xcc12 | ((n) << 6)) << 2)
331 #define PHY_PMA_ISO_PWRST_CTRL(n)	((0xcc13 | ((n) << 6)) << 2)
332 #define PHY_PMA_ISO_TX_DATA_LO(n)	((0xcc14 | ((n) << 6)) << 2)
333 #define PHY_PMA_ISO_TX_DATA_HI(n)	((0xcc15 | ((n) << 6)) << 2)
334 #define PHY_PMA_ISO_RX_DATA_LO(n)	((0xcc16 | ((n) << 6)) << 2)
335 #define PHY_PMA_ISO_RX_DATA_HI(n)	((0xcc17 | ((n) << 6)) << 2)
336 #define TX_BIST_CTRL(n)			((0x4140 | ((n) << 9)) << 2)
337 #define TX_BIST_UDDWR(n)		((0x4141 | ((n) << 9)) << 2)
338 
339 /*
340  * Selects which PLL clock will be driven on the analog high speed
341  * clock 0: PLL 0 div 1
342  * clock 1: PLL 1 div 2
343  */
344 #define CLK_PLL1_DIV1			0x20
345 #define CLK_PLL1_DIV2			0x30
346 #define CLK_PLL_MASK			0x33
347 
348 #define CMN_READY			BIT(0)
349 
350 #define DP_PLL_CLOCK_ENABLE_ACK		BIT(3)
351 #define DP_PLL_CLOCK_ENABLE		BIT(2)
352 #define DP_PLL_ENABLE_ACK		BIT(1)
353 #define DP_PLL_ENABLE			BIT(0)
354 #define DP_PLL_DATA_RATE_RBR		((2 << 12) | (4 << 8))
355 #define DP_PLL_DATA_RATE_HBR		((2 << 12) | (4 << 8))
356 #define DP_PLL_DATA_RATE_HBR2		((1 << 12) | (2 << 8))
357 #define DP_PLL_DATA_RATE_MASK		0xff00
358 
359 #define DP_MODE_MASK			0xf
360 #define DP_MODE_ENTER_A0		BIT(0)
361 #define DP_MODE_ENTER_A2		BIT(2)
362 #define DP_MODE_ENTER_A3		BIT(3)
363 #define DP_MODE_A0_ACK			BIT(4)
364 #define DP_MODE_A2_ACK			BIT(6)
365 #define DP_MODE_A3_ACK			BIT(7)
366 #define DP_LINK_RESET_DEASSERTED	BIT(8)
367 
368 #define PHY_MODE_SET_TIMEOUT		100000
369 
370 #define PIN_ASSIGN_C_E			0x51d9
371 #define PIN_ASSIGN_D_F			0x5100
372 
373 #define MODE_DISCONNECT			0
374 #define MODE_UFP_USB			BIT(0)
375 #define MODE_DFP_USB			BIT(1)
376 #define MODE_DFP_DP			BIT(2)
377 
378 #define DP_DEFAULT_RATE			162000
379 
380 #define POWER_ON_TRIES			5
381 
382 struct usb3phy_reg {
383 	u32 offset;
384 	u32 enable_bit;
385 	u32 write_enable;
386 };
387 
388 /**
389  * struct rockchip_usb3phy_port_cfg - usb3-phy port configuration.
390  * @reg: the base address for usb3-phy config.
391  * @typec_conn_dir: the register of type-c connector direction.
392  * @usb3tousb2_en: the register of type-c force usb2 to usb2 enable.
393  * @external_psm: the register of type-c phy external psm clock.
394  * @pipe_status: the register of type-c phy pipe status.
395  * @usb3_host_disable: the register of type-c usb3 host disable.
396  * @usb3_host_port: the register of type-c usb3 host port.
397  * @uphy_dp_sel: the register of type-c phy DP select control.
398  */
399 struct rockchip_usb3phy_port_cfg {
400 	unsigned int reg;
401 	struct usb3phy_reg typec_conn_dir;
402 	struct usb3phy_reg usb3tousb2_en;
403 	struct usb3phy_reg external_psm;
404 	struct usb3phy_reg pipe_status;
405 	struct usb3phy_reg usb3_host_disable;
406 	struct usb3phy_reg usb3_host_port;
407 	struct usb3phy_reg uphy_dp_sel;
408 };
409 
410 struct phy_config {
411 	int swing;
412 	int pe;
413 };
414 
415 enum {
416 	TYPEC_PHY_USB,
417 	TYPEC_PHY_DP,
418 	TYPEC_PHY_MAX,
419 };
420 
421 struct rockchip_typec_phy {
422 	struct device *dev;
423 	void __iomem *base;
424 	struct typec_mux *mux;
425 	struct typec_switch *sw;
426 	struct regmap *grf_regs;
427 	struct clk *clk_core;
428 	struct clk *clk_ref;
429 	struct reset_control *uphy_rst;
430 	struct reset_control *pipe_rst;
431 	struct reset_control *tcphy_rst;
432 	struct phy *phys[TYPEC_PHY_MAX];
433 	const struct rockchip_usb3phy_port_cfg *port_cfgs;
434 	/* mutex to protect access to individual PHYs */
435 	struct mutex lock;
436 
437 	bool flip;
438 	u8 mode;
439 	u8 new_mode;
440 	struct phy_config config[3][4];
441 };
442 
443 struct phy_reg {
444 	u16 value;
445 	u32 addr;
446 };
447 
448 static struct phy_reg usb3_pll_cfg[] = {
449 	{ 0xf0,		CMN_PLL0_VCOCAL_INIT },
450 	{ 0x18,		CMN_PLL0_VCOCAL_ITER },
451 	{ 0xd0,		CMN_PLL0_INTDIV },
452 	{ 0x4a4a,	CMN_PLL0_FRACDIV },
453 	{ 0x34,		CMN_PLL0_HIGH_THR },
454 	{ 0x1ee,	CMN_PLL0_SS_CTRL1 },
455 	{ 0x7f03,	CMN_PLL0_SS_CTRL2 },
456 	{ 0x20,		CMN_PLL0_DSM_DIAG },
457 	{ 0,		CMN_DIAG_PLL0_OVRD },
458 	{ 0,		CMN_DIAG_PLL0_FBH_OVRD },
459 	{ 0,		CMN_DIAG_PLL0_FBL_OVRD },
460 	{ 0x7,		CMN_DIAG_PLL0_V2I_TUNE },
461 	{ 0x45,		CMN_DIAG_PLL0_CP_TUNE },
462 	{ 0x8,		CMN_DIAG_PLL0_LF_PROG },
463 };
464 
465 static const struct phy_reg dp_pll_rbr_cfg[] = {
466 	{ 0x00f0, CMN_PLL1_VCOCAL_INIT },
467 	{ 0x0018, CMN_PLL1_VCOCAL_ITER },
468 	{ 0x30b9, CMN_PLL1_VCOCAL_START },
469 	{ 0x0087, CMN_PLL1_INTDIV },
470 	{ 0x0000, CMN_PLL1_FRACDIV },
471 	{ 0x0022, CMN_PLL1_HIGH_THR },
472 	{ 0x8000, CMN_PLL1_SS_CTRL1 },
473 	{ 0x0000, CMN_PLL1_SS_CTRL2 },
474 	{ 0x0020, CMN_PLL1_DSM_DIAG },
475 	{ 0x0000, CMN_PLLSM1_USER_DEF_CTRL },
476 	{ 0x0000, CMN_DIAG_PLL1_OVRD },
477 	{ 0x0000, CMN_DIAG_PLL1_FBH_OVRD },
478 	{ 0x0000, CMN_DIAG_PLL1_FBL_OVRD },
479 	{ 0x0006, CMN_DIAG_PLL1_V2I_TUNE },
480 	{ 0x0045, CMN_DIAG_PLL1_CP_TUNE },
481 	{ 0x0008, CMN_DIAG_PLL1_LF_PROG },
482 	{ 0x0100, CMN_DIAG_PLL1_PTATIS_TUNE1 },
483 	{ 0x0007, CMN_DIAG_PLL1_PTATIS_TUNE2 },
484 	{ 0x0001, CMN_DIAG_PLL1_INCLK_CTRL },
485 };
486 
487 static const struct phy_reg dp_pll_rbr_ssc_cfg[] = {
488 	{ 0x00f0, CMN_PLL1_VCOCAL_INIT },
489 	{ 0x0018, CMN_PLL1_VCOCAL_ITER },
490 	{ 0x30b9, CMN_PLL1_VCOCAL_START },
491 	{ 0x0086, CMN_PLL1_INTDIV },
492 	{ 0xf915, CMN_PLL1_FRACDIV },
493 	{ 0x0022, CMN_PLL1_HIGH_THR },
494 	{ 0x0140, CMN_PLL1_SS_CTRL1 },
495 	{ 0x7f03, CMN_PLL1_SS_CTRL2 },
496 	{ 0x0020, CMN_PLL1_DSM_DIAG },
497 	{ 0x0000, CMN_PLLSM1_USER_DEF_CTRL },
498 	{ 0x0000, CMN_DIAG_PLL1_OVRD },
499 	{ 0x0000, CMN_DIAG_PLL1_FBH_OVRD },
500 	{ 0x0000, CMN_DIAG_PLL1_FBL_OVRD },
501 	{ 0x0006, CMN_DIAG_PLL1_V2I_TUNE },
502 	{ 0x0045, CMN_DIAG_PLL1_CP_TUNE },
503 	{ 0x0008, CMN_DIAG_PLL1_LF_PROG },
504 	{ 0x0100, CMN_DIAG_PLL1_PTATIS_TUNE1 },
505 	{ 0x0007, CMN_DIAG_PLL1_PTATIS_TUNE2 },
506 	{ 0x0001, CMN_DIAG_PLL1_INCLK_CTRL },
507 };
508 
509 static const struct phy_reg dp_pll_hbr_cfg[] = {
510 	{ 0x00f0, CMN_PLL1_VCOCAL_INIT },
511 	{ 0x0018, CMN_PLL1_VCOCAL_ITER },
512 	{ 0x30b4, CMN_PLL1_VCOCAL_START },
513 	{ 0x00e1, CMN_PLL1_INTDIV },
514 	{ 0x0000, CMN_PLL1_FRACDIV },
515 	{ 0x0005, CMN_PLL1_HIGH_THR },
516 	{ 0x8000, CMN_PLL1_SS_CTRL1 },
517 	{ 0x0000, CMN_PLL1_SS_CTRL2 },
518 	{ 0x0020, CMN_PLL1_DSM_DIAG },
519 	{ 0x1000, CMN_PLLSM1_USER_DEF_CTRL },
520 	{ 0x0000, CMN_DIAG_PLL1_OVRD },
521 	{ 0x0000, CMN_DIAG_PLL1_FBH_OVRD },
522 	{ 0x0000, CMN_DIAG_PLL1_FBL_OVRD },
523 	{ 0x0007, CMN_DIAG_PLL1_V2I_TUNE },
524 	{ 0x0045, CMN_DIAG_PLL1_CP_TUNE },
525 	{ 0x0008, CMN_DIAG_PLL1_LF_PROG },
526 	{ 0x0001, CMN_DIAG_PLL1_PTATIS_TUNE1 },
527 	{ 0x0001, CMN_DIAG_PLL1_PTATIS_TUNE2 },
528 	{ 0x0001, CMN_DIAG_PLL1_INCLK_CTRL },
529 };
530 
531 static const struct phy_reg dp_pll_hbr_ssc_cfg[] = {
532 	{ 0x00f0, CMN_PLL1_VCOCAL_INIT },
533 	{ 0x0018, CMN_PLL1_VCOCAL_ITER },
534 	{ 0x30b4, CMN_PLL1_VCOCAL_START },
535 	{ 0x00e0, CMN_PLL1_INTDIV },
536 	{ 0xf479, CMN_PLL1_FRACDIV },
537 	{ 0x0038, CMN_PLL1_HIGH_THR },
538 	{ 0x0204, CMN_PLL1_SS_CTRL1 },
539 	{ 0x7f03, CMN_PLL1_SS_CTRL2 },
540 	{ 0x0020, CMN_PLL1_DSM_DIAG },
541 	{ 0x1000, CMN_PLLSM1_USER_DEF_CTRL },
542 	{ 0x0000, CMN_DIAG_PLL1_OVRD },
543 	{ 0x0000, CMN_DIAG_PLL1_FBH_OVRD },
544 	{ 0x0000, CMN_DIAG_PLL1_FBL_OVRD },
545 	{ 0x0007, CMN_DIAG_PLL1_V2I_TUNE },
546 	{ 0x0045, CMN_DIAG_PLL1_CP_TUNE },
547 	{ 0x0008, CMN_DIAG_PLL1_LF_PROG },
548 	{ 0x0001, CMN_DIAG_PLL1_PTATIS_TUNE1 },
549 	{ 0x0001, CMN_DIAG_PLL1_PTATIS_TUNE2 },
550 	{ 0x0001, CMN_DIAG_PLL1_INCLK_CTRL },
551 };
552 
553 static const struct phy_reg dp_pll_hbr2_cfg[] = {
554 	{ 0x00f0, CMN_PLL1_VCOCAL_INIT },
555 	{ 0x0018, CMN_PLL1_VCOCAL_ITER },
556 	{ 0x30b4, CMN_PLL1_VCOCAL_START },
557 	{ 0x00e1, CMN_PLL1_INTDIV },
558 	{ 0x0000, CMN_PLL1_FRACDIV },
559 	{ 0x0005, CMN_PLL1_HIGH_THR },
560 	{ 0x8000, CMN_PLL1_SS_CTRL1 },
561 	{ 0x0000, CMN_PLL1_SS_CTRL2 },
562 	{ 0x0020, CMN_PLL1_DSM_DIAG },
563 	{ 0x1000, CMN_PLLSM1_USER_DEF_CTRL },
564 	{ 0x0000, CMN_DIAG_PLL1_OVRD },
565 	{ 0x0000, CMN_DIAG_PLL1_FBH_OVRD },
566 	{ 0x0000, CMN_DIAG_PLL1_FBL_OVRD },
567 	{ 0x0007, CMN_DIAG_PLL1_V2I_TUNE },
568 	{ 0x0045, CMN_DIAG_PLL1_CP_TUNE },
569 	{ 0x0008, CMN_DIAG_PLL1_LF_PROG },
570 	{ 0x0001, CMN_DIAG_PLL1_PTATIS_TUNE1 },
571 	{ 0x0001, CMN_DIAG_PLL1_PTATIS_TUNE2 },
572 	{ 0x0001, CMN_DIAG_PLL1_INCLK_CTRL },
573 };
574 
575 static const struct phy_reg dp_pll_hbr2_ssc_cfg[] = {
576 	{ 0x00f0, CMN_PLL1_VCOCAL_INIT },
577 	{ 0x0018, CMN_PLL1_VCOCAL_ITER },
578 	{ 0x30b4, CMN_PLL1_VCOCAL_START },
579 	{ 0x00e0, CMN_PLL1_INTDIV },
580 	{ 0xf479, CMN_PLL1_FRACDIV },
581 	{ 0x0038, CMN_PLL1_HIGH_THR },
582 	{ 0x0204, CMN_PLL1_SS_CTRL1 },
583 	{ 0x7f03, CMN_PLL1_SS_CTRL2 },
584 	{ 0x0020, CMN_PLL1_DSM_DIAG },
585 	{ 0x1000, CMN_PLLSM1_USER_DEF_CTRL },
586 	{ 0x0000, CMN_DIAG_PLL1_OVRD },
587 	{ 0x0000, CMN_DIAG_PLL1_FBH_OVRD },
588 	{ 0x0000, CMN_DIAG_PLL1_FBL_OVRD },
589 	{ 0x0007, CMN_DIAG_PLL1_V2I_TUNE },
590 	{ 0x0045, CMN_DIAG_PLL1_CP_TUNE },
591 	{ 0x0008, CMN_DIAG_PLL1_LF_PROG },
592 	{ 0x0001, CMN_DIAG_PLL1_PTATIS_TUNE1 },
593 	{ 0x0001, CMN_DIAG_PLL1_PTATIS_TUNE2 },
594 	{ 0x0001, CMN_DIAG_PLL1_INCLK_CTRL },
595 };
596 
597 static const struct rockchip_usb3phy_port_cfg rk3399_usb3phy_port_cfgs[] = {
598 	{
599 		.reg = 0xff7c0000,
600 		.typec_conn_dir	= { 0xe580, 0, 16 },
601 		.usb3tousb2_en	= { 0xe580, 3, 19 },
602 		.external_psm	= { 0xe588, 14, 30 },
603 		.pipe_status	= { 0xe5c0, 0, 0 },
604 		.usb3_host_disable = { 0x2434, 0, 16 },
605 		.usb3_host_port = { 0x2434, 12, 28 },
606 		.uphy_dp_sel	= { 0x6268, 19, 19 },
607 	},
608 	{
609 		.reg = 0xff800000,
610 		.typec_conn_dir	= { 0xe58c, 0, 16 },
611 		.usb3tousb2_en	= { 0xe58c, 3, 19 },
612 		.external_psm	= { 0xe594, 14, 30 },
613 		.pipe_status	= { 0xe5c0, 16, 16 },
614 		.usb3_host_disable = { 0x2444, 0, 16 },
615 		.usb3_host_port = { 0x2444, 12, 28 },
616 		.uphy_dp_sel	= { 0x6268, 3, 19 },
617 	},
618 	{ /* sentinel */ }
619 };
620 
621 /* default phy config */
622 static const struct phy_config tcphy_default_config[3][4] = {
623 	{{ .swing = 0x2a, .pe = 0x00 },
624 	 { .swing = 0x1f, .pe = 0x15 },
625 	 { .swing = 0x14, .pe = 0x22 },
626 	 { .swing = 0x02, .pe = 0x2b } },
627 
628 	{{ .swing = 0x21, .pe = 0x00 },
629 	 { .swing = 0x12, .pe = 0x15 },
630 	 { .swing = 0x02, .pe = 0x22 },
631 	 { .swing = 0,    .pe = 0 } },
632 
633 	{{ .swing = 0x15, .pe = 0x00 },
634 	 { .swing = 0x00, .pe = 0x15 },
635 	 { .swing = 0,    .pe = 0 },
636 	 { .swing = 0,    .pe = 0 } },
637 };
638 
639 enum phy_dp_power_state {
640 	PHY_DP_POWER_STATE_A0,
641 	PHY_DP_POWER_STATE_A1,
642 	PHY_DP_POWER_STATE_A2,
643 	PHY_DP_POWER_STATE_A3,
644 };
645 
tcphy_dp_set_power_state(struct rockchip_typec_phy * tcphy,enum phy_dp_power_state state)646 static int tcphy_dp_set_power_state(struct rockchip_typec_phy *tcphy,
647 				    enum phy_dp_power_state state)
648 {
649 	u32 ack, reg, sts = BIT(state);
650 	int ret;
651 
652 	/*
653 	 * Power state changes must not be requested until after the cmn_ready
654 	 * signal has gone active.
655 	 */
656 	reg = readl(tcphy->base + PMA_CMN_CTRL1);
657 	if (!(reg & CMN_READY)) {
658 		dev_err(tcphy->dev, "cmn_ready in the inactive state\n");
659 		return -EINVAL;
660 	}
661 
662 	reg = readl(tcphy->base + PHY_DP_MODE_CTL);
663 	reg &= ~PHY_DP_POWER_STATE_MASK;
664 	reg |= sts;
665 	writel(reg, tcphy->base + PHY_DP_MODE_CTL);
666 
667 	ret = readl_poll_timeout(tcphy->base + PHY_DP_MODE_CTL,
668 				 ack, (((ack & PHY_DP_POWER_STATE_ACK_MASK) >>
669 				 PHY_DP_POWER_STATE_ACK_SHIFT) == sts), 10,
670 				 PHY_MODE_SET_TIMEOUT);
671 	if (ret < 0) {
672 		dev_err(tcphy->dev, "failed to enter power state %d\n", state);
673 		return ret;
674 	}
675 
676 	return 0;
677 }
678 
679 enum {
680 	PHY_DP_LANE_0,
681 	PHY_DP_LANE_1,
682 	PHY_DP_LANE_2,
683 	PHY_DP_LANE_3,
684 };
685 
686 enum {
687 	PMA_IF_PIPE_PCS,
688 	PMA_IF_PHY_DP,
689 };
690 
691 /*
692  * For the TypeC PHY, the 4 lanes are mapping to the USB TypeC receptacle pins
693  * as follows:
694  *   -------------------------------------------------------------------
695  *	PHY Lanes/Module Pins			TypeC Receptacle Pins
696  *   -------------------------------------------------------------------
697  *	Lane0 (tx_p/m_ln_0)			TX1+/TX1- (pins A2/A3)
698  *	Lane1 (tx_rx_p/m_ln_1)			RX1+/RX1- (pins B11/B10)
699  *	Lane2 (tx_rx_p/m_ln_2)			RX2+/RX2- (pins A11/A10)
700  *	Lane3 (tx_p/m_ln_3)			TX2+/TX2- (pins B2/B3)
701  *   -------------------------------------------------------------------
702  *
703  * USB and DP lanes mapping to TypeC PHY lanes for each of pin assignment
704  * options (normal connector orientation) described in the VESA DisplayPort
705  * Alt Mode on USB TypeC Standard as follows:
706  *
707  * ----------------------------------------------------------------------
708  *	PHY Lanes	A	B	C	D	E	F
709  * ----------------------------------------------------------------------
710  *	  0	       ML1     SSTX    ML2     SSTX    ML2     SSTX
711  *	  1	       ML3     SSRX    ML3     SSRX    ML3     SSRX
712  *	  2	       ML2     ML1     ML0     ML0     ML0     ML0
713  *	  3	       ML0     ML0     ML1     ML1     ML1     ML1
714  * ----------------------------------------------------------------------
715  */
tcphy_set_lane_mapping(struct rockchip_typec_phy * tcphy,u8 mode)716 static void tcphy_set_lane_mapping(struct rockchip_typec_phy *tcphy, u8 mode)
717 {
718 	/*
719 	 * The PHY_PMA_LANE_CFG register is used to select whether a PMA lane
720 	 * is mapped for USB or PHY DP. The PHY_PMA_LANE_CFG register is
721 	 * configured based on a normal connector orientation. Logic in the
722 	 * PHY automatically handles the flipped connector case based on the
723 	 * setting of orientation of TypeC PHY.
724 	 */
725 	if (mode == MODE_DFP_DP) {
726 		/* This maps to VESA DP Alt Mode pin assignments C and E. */
727 		writel(PMA_LANE3_DP_LANE_SEL(PHY_DP_LANE_1) |
728 		       PMA_LANE3_INTERFACE_SEL(PMA_IF_PHY_DP) |
729 		       PMA_LANE2_DP_LANE_SEL(PHY_DP_LANE_0) |
730 		       PMA_LANE2_INTERFACE_SEL(PMA_IF_PHY_DP) |
731 		       PMA_LANE1_DP_LANE_SEL(PHY_DP_LANE_3) |
732 		       PMA_LANE1_INTERFACE_SEL(PMA_IF_PHY_DP) |
733 		       PMA_LANE0_DP_LANE_SEL(PHY_DP_LANE_2) |
734 		       PMA_LANE0_INTERFACE_SEL(PMA_IF_PHY_DP),
735 		       tcphy->base + PHY_PMA_LANE_CFG);
736 	} else {
737 		/* This maps to VESA DP Alt Mode pin assignments D and F. */
738 		writel(PMA_LANE3_DP_LANE_SEL(PHY_DP_LANE_1) |
739 		       PMA_LANE3_INTERFACE_SEL(PMA_IF_PHY_DP) |
740 		       PMA_LANE2_DP_LANE_SEL(PHY_DP_LANE_0) |
741 		       PMA_LANE2_INTERFACE_SEL(PMA_IF_PHY_DP) |
742 		       PMA_LANE1_INTERFACE_SEL(PMA_IF_PIPE_PCS) |
743 		       PMA_LANE0_INTERFACE_SEL(PMA_IF_PIPE_PCS),
744 		       tcphy->base + PHY_PMA_LANE_CFG);
745 	}
746 }
747 
tcphy_cfg_24m(struct rockchip_typec_phy * tcphy)748 static void tcphy_cfg_24m(struct rockchip_typec_phy *tcphy)
749 {
750 	u32 i, rdata;
751 
752 	/*
753 	 * cmn_ref_clk_sel = 3, select the 24Mhz for clk parent
754 	 * cmn_psm_clk_dig_div = 2, set the clk division to 2
755 	 */
756 	writel(0x830, tcphy->base + PMA_CMN_CTRL1);
757 	for (i = 0; i < 4; i++) {
758 		/*
759 		 * The following PHY configuration assumes a 24 MHz reference
760 		 * clock.
761 		 */
762 		writel(0x90, tcphy->base + XCVR_DIAG_LANE_FCM_EN_MGN(i));
763 		writel(0x960, tcphy->base + TX_RCVDET_EN_TMR(i));
764 		writel(0x30, tcphy->base + TX_RCVDET_ST_TMR(i));
765 	}
766 
767 	rdata = readl(tcphy->base + CMN_DIAG_HSCLK_SEL);
768 	rdata &= ~CLK_PLL_MASK;
769 	rdata |= CLK_PLL1_DIV2;
770 	writel(rdata, tcphy->base + CMN_DIAG_HSCLK_SEL);
771 }
772 
tcphy_cfg_usb3_pll(struct rockchip_typec_phy * tcphy)773 static void tcphy_cfg_usb3_pll(struct rockchip_typec_phy *tcphy)
774 {
775 	u32 i;
776 
777 	/* load the configuration of PLL0 */
778 	for (i = 0; i < ARRAY_SIZE(usb3_pll_cfg); i++)
779 		writel(usb3_pll_cfg[i].value,
780 		       tcphy->base + usb3_pll_cfg[i].addr);
781 }
782 
tcphy_cfg_dp_pll(struct rockchip_typec_phy * tcphy,int link_rate)783 static void tcphy_cfg_dp_pll(struct rockchip_typec_phy *tcphy, int link_rate)
784 {
785 	const struct phy_reg *phy_cfg;
786 	u32 clk_ctrl;
787 	u32 i, cfg_size, hsclk_sel;
788 
789 	hsclk_sel = readl(tcphy->base + CMN_DIAG_HSCLK_SEL);
790 	hsclk_sel &= ~CLK_PLL_MASK;
791 
792 	switch (link_rate) {
793 	case 540000:
794 		clk_ctrl = DP_PLL_DATA_RATE_HBR2;
795 		hsclk_sel |= CLK_PLL1_DIV1;
796 		phy_cfg = dp_pll_hbr2_cfg;
797 		cfg_size = ARRAY_SIZE(dp_pll_hbr2_cfg);
798 		break;
799 	case 270000:
800 		clk_ctrl = DP_PLL_DATA_RATE_HBR;
801 		hsclk_sel |= CLK_PLL1_DIV2;
802 		phy_cfg = dp_pll_hbr_cfg;
803 		cfg_size = ARRAY_SIZE(dp_pll_hbr_cfg);
804 		break;
805 	case 162000:
806 	default:
807 		clk_ctrl = DP_PLL_DATA_RATE_RBR;
808 		hsclk_sel |= CLK_PLL1_DIV2;
809 		phy_cfg = dp_pll_rbr_cfg;
810 		cfg_size = ARRAY_SIZE(dp_pll_rbr_cfg);
811 		break;
812 	}
813 
814 	clk_ctrl |= DP_PLL_CLOCK_ENABLE | DP_PLL_ENABLE;
815 	writel(clk_ctrl, tcphy->base + PHY_DP_CLK_CTL);
816 	writel(hsclk_sel, tcphy->base + CMN_DIAG_HSCLK_SEL);
817 
818 	/* load the configuration of PLL1 */
819 	for (i = 0; i < cfg_size; i++)
820 		writel(phy_cfg[i].value, tcphy->base + phy_cfg[i].addr);
821 }
822 
tcphy_tx_usb3_cfg_lane(struct rockchip_typec_phy * tcphy,u32 lane)823 static void tcphy_tx_usb3_cfg_lane(struct rockchip_typec_phy *tcphy, u32 lane)
824 {
825 	writel(0x7799, tcphy->base + TX_PSC_A0(lane));
826 	writel(0x7798, tcphy->base + TX_PSC_A1(lane));
827 	writel(0x5098, tcphy->base + TX_PSC_A2(lane));
828 	writel(0x5098, tcphy->base + TX_PSC_A3(lane));
829 	writel(0, tcphy->base + TX_TXCC_MGNFS_MULT_000(lane));
830 	writel(0xbf, tcphy->base + XCVR_DIAG_BIDI_CTRL(lane));
831 }
832 
tcphy_rx_usb3_cfg_lane(struct rockchip_typec_phy * tcphy,u32 lane)833 static void tcphy_rx_usb3_cfg_lane(struct rockchip_typec_phy *tcphy, u32 lane)
834 {
835 	writel(0xa6fd, tcphy->base + RX_PSC_A0(lane));
836 	writel(0xa6fd, tcphy->base + RX_PSC_A1(lane));
837 	writel(0xa410, tcphy->base + RX_PSC_A2(lane));
838 	writel(0x2410, tcphy->base + RX_PSC_A3(lane));
839 	writel(0x23ff, tcphy->base + RX_PSC_CAL(lane));
840 	writel(0x13, tcphy->base + RX_SIGDET_HL_FILT_TMR(lane));
841 	writel(0x03e7, tcphy->base + RX_REE_CTRL_DATA_MASK(lane));
842 	writel(0x1004, tcphy->base + RX_DIAG_SIGDET_TUNE(lane));
843 	writel(0x2010, tcphy->base + RX_PSC_RDY(lane));
844 	writel(0xfb, tcphy->base + XCVR_DIAG_BIDI_CTRL(lane));
845 }
846 
tcphy_dp_cfg_lane(struct rockchip_typec_phy * tcphy,int link_rate,u8 swing,u8 pre_emp,u32 lane)847 static void tcphy_dp_cfg_lane(struct rockchip_typec_phy *tcphy, int link_rate,
848 			      u8 swing, u8 pre_emp, u32 lane)
849 {
850 	u16 val;
851 
852 	writel(0xbefc, tcphy->base + XCVR_PSM_RCTRL(lane));
853 	writel(0x6799, tcphy->base + TX_PSC_A0(lane));
854 	writel(0x6798, tcphy->base + TX_PSC_A1(lane));
855 	writel(0x98, tcphy->base + TX_PSC_A2(lane));
856 	writel(0x98, tcphy->base + TX_PSC_A3(lane));
857 
858 	writel(tcphy->config[swing][pre_emp].swing,
859 	       tcphy->base + TX_TXCC_MGNFS_MULT_000(lane));
860 	writel(tcphy->config[swing][pre_emp].pe,
861 	       tcphy->base + TX_TXCC_CPOST_MULT_00(lane));
862 
863 	if (swing == 2 && pre_emp == 0 && link_rate != 540000) {
864 		writel(0x700, tcphy->base + TX_DIAG_TX_DRV(lane));
865 		writel(0x13c, tcphy->base + TX_TXCC_CAL_SCLR_MULT(lane));
866 	} else {
867 		writel(0x128, tcphy->base + TX_TXCC_CAL_SCLR_MULT(lane));
868 		writel(0x0400, tcphy->base + TX_DIAG_TX_DRV(lane));
869 	}
870 
871 	val = readl(tcphy->base + XCVR_DIAG_PLLDRC_CTRL(lane));
872 	val = val & 0x8fff;
873 	switch (link_rate) {
874 	case 540000:
875 		val |= (5 << 12);
876 		break;
877 	case 162000:
878 	case 270000:
879 	default:
880 		val |= (6 << 12);
881 		break;
882 	}
883 	writel(val, tcphy->base + XCVR_DIAG_PLLDRC_CTRL(lane));
884 }
885 
tcphy_dp_set_phy_config(struct phy * phy,int link_rate,int lane_count,u8 swing,u8 pre_emp)886 int tcphy_dp_set_phy_config(struct phy *phy, int link_rate,
887 			    int lane_count, u8 swing, u8 pre_emp)
888 {
889 	struct rockchip_typec_phy *tcphy = phy_get_drvdata(phy);
890 	u8 i;
891 
892 	if (!phy->power_count)
893 		return -EPERM;
894 
895 	if (tcphy->mode == MODE_DFP_DP) {
896 		for (i = 0; i < 4; i++)
897 			tcphy_dp_cfg_lane(tcphy, link_rate, swing, pre_emp, i);
898 	} else {
899 		if (tcphy->flip) {
900 			tcphy_dp_cfg_lane(tcphy, link_rate, swing, pre_emp, 0);
901 			tcphy_dp_cfg_lane(tcphy, link_rate, swing, pre_emp, 1);
902 		} else {
903 			tcphy_dp_cfg_lane(tcphy, link_rate, swing, pre_emp, 2);
904 			tcphy_dp_cfg_lane(tcphy, link_rate, swing, pre_emp, 3);
905 		}
906 	}
907 
908 	return 0;
909 }
910 EXPORT_SYMBOL(tcphy_dp_set_phy_config);
911 
tcphy_dp_set_lane_count(struct phy * phy,u8 lane_count)912 int tcphy_dp_set_lane_count(struct phy *phy, u8 lane_count)
913 {
914 	struct rockchip_typec_phy *tcphy = phy_get_drvdata(phy);
915 	u32 reg;
916 
917 	if (!phy->power_count)
918 		return -EPERM;
919 
920 	/*
921 	 * In cases where fewer than the configured number of DP lanes are
922 	 * being used. PHY_DP_MODE_CTL[15:12] must be set to disable and
923 	 * power-down the unused PHY DP lanes (and their mapped PMA lanes).
924 	 * Set the bit ([15:12]) associated with each DP PHY lane(s) to be
925 	 * disabled.
926 	 */
927 	reg = readl(tcphy->base + PHY_DP_MODE_CTL);
928 	reg |= PHY_DP_LANE_DISABLE;
929 
930 	switch (lane_count) {
931 	case 4:
932 		reg &= ~(PHY_DP_LANE_3_DISABLE | PHY_DP_LANE_2_DISABLE |
933 			 PHY_DP_LANE_1_DISABLE | PHY_DP_LANE_0_DISABLE);
934 		break;
935 	case 2:
936 		reg &= ~(PHY_DP_LANE_1_DISABLE | PHY_DP_LANE_0_DISABLE);
937 		break;
938 	case 1:
939 		reg &= ~PHY_DP_LANE_0_DISABLE;
940 		break;
941 	default:
942 		return -EINVAL;
943 	}
944 
945 	writel(reg, tcphy->base + PHY_DP_MODE_CTL);
946 
947 	return 0;
948 }
949 EXPORT_SYMBOL(tcphy_dp_set_lane_count);
950 
tcphy_dp_set_link_rate(struct phy * phy,int link_rate,bool ssc_on)951 int tcphy_dp_set_link_rate(struct phy *phy, int link_rate, bool ssc_on)
952 {
953 	struct rockchip_typec_phy *tcphy = phy_get_drvdata(phy);
954 	const struct phy_reg *phy_cfg;
955 	u32 cmn_diag_hsclk_sel, phy_dp_clk_ctl, reg;
956 	u32 i, cfg_size;
957 	int ret;
958 
959 	if (!phy->power_count)
960 		return -EPERM;
961 
962 	/* Place the PHY lanes in the A3 power state. */
963 	ret = tcphy_dp_set_power_state(tcphy, PHY_DP_POWER_STATE_A3);
964 	if (ret) {
965 		dev_err(tcphy->dev, "failed to enter A3 state: %d\n", ret);
966 		return ret;
967 	}
968 
969 	/* Gate the PLL clocks from PMA */
970 	reg = readl(tcphy->base + PHY_DP_CLK_CTL);
971 	reg &= ~DP_PLL_CLOCK_ENABLE_MASK;
972 	reg |= DP_PLL_CLOCK_DISABLE;
973 	writel(reg, tcphy->base + PHY_DP_CLK_CTL);
974 
975 	ret = readl_poll_timeout(tcphy->base + PHY_DP_CLK_CTL, reg,
976 				 !(reg & DP_PLL_CLOCK_ENABLE_ACK),
977 				 10, PHY_MODE_SET_TIMEOUT);
978 	if (ret) {
979 		dev_err(tcphy->dev, "wait DP PLL clock disabled timeout\n");
980 		return ret;
981 	}
982 
983 	/* Disable the PLL */
984 	reg = readl(tcphy->base + PHY_DP_CLK_CTL);
985 	reg &= ~DP_PLL_ENABLE_MASK;
986 	reg |= DP_PLL_DISABLE;
987 	writel(reg, tcphy->base + PHY_DP_CLK_CTL);
988 
989 	ret = readl_poll_timeout(tcphy->base + PHY_DP_CLK_CTL, reg,
990 				 !(reg & DP_PLL_READY),
991 				 10, PHY_MODE_SET_TIMEOUT);
992 	if (ret) {
993 		dev_err(tcphy->dev, "wait DP PLL not ready timeout\n");
994 		return ret;
995 	}
996 
997 	/* Re-configure PHY registers for the new data rate */
998 	cmn_diag_hsclk_sel = readl(tcphy->base + CMN_DIAG_HSCLK_SEL);
999 	cmn_diag_hsclk_sel &= ~(GENMASK(5, 4) | GENMASK(1, 0));
1000 
1001 	phy_dp_clk_ctl = readl(tcphy->base + PHY_DP_CLK_CTL);
1002 	phy_dp_clk_ctl &= ~(GENMASK(15, 12) | GENMASK(11, 8));
1003 
1004 	switch (link_rate) {
1005 	case 162000:
1006 		cmn_diag_hsclk_sel |= (3 << 4) | (0 << 0);
1007 		phy_dp_clk_ctl |= (2 << 12) | (4 << 8);
1008 
1009 		phy_cfg = ssc_on ? dp_pll_rbr_ssc_cfg : dp_pll_rbr_cfg;
1010 		cfg_size = ssc_on ? ARRAY_SIZE(dp_pll_rbr_ssc_cfg) :
1011 				    ARRAY_SIZE(dp_pll_rbr_cfg);
1012 		break;
1013 	case 270000:
1014 		cmn_diag_hsclk_sel |= (3 << 4) | (0 << 0);
1015 		phy_dp_clk_ctl |= (2 << 12) | (4 << 8);
1016 
1017 		phy_cfg = ssc_on ? dp_pll_hbr_ssc_cfg : dp_pll_hbr_cfg;
1018 		cfg_size = ssc_on ? ARRAY_SIZE(dp_pll_hbr_ssc_cfg) :
1019 				    ARRAY_SIZE(dp_pll_hbr_cfg);
1020 		break;
1021 	case 540000:
1022 		cmn_diag_hsclk_sel |= (2 << 4) | (0 << 0);
1023 		phy_dp_clk_ctl |= (1 << 12) | (2 << 8);
1024 
1025 		phy_cfg = ssc_on ? dp_pll_hbr2_ssc_cfg : dp_pll_hbr2_cfg;
1026 		cfg_size = ssc_on ? ARRAY_SIZE(dp_pll_hbr2_ssc_cfg) :
1027 				    ARRAY_SIZE(dp_pll_hbr2_cfg);
1028 		break;
1029 	default:
1030 		return -EINVAL;
1031 	}
1032 
1033 	writel(cmn_diag_hsclk_sel, tcphy->base + CMN_DIAG_HSCLK_SEL);
1034 	writel(phy_dp_clk_ctl, tcphy->base + PHY_DP_CLK_CTL);
1035 
1036 	/* load the configuration of PLL1 */
1037 	for (i = 0; i < cfg_size; i++)
1038 		writel(phy_cfg[i].value, tcphy->base + phy_cfg[i].addr);
1039 
1040 	/* Enable the PLL */
1041 	reg = readl(tcphy->base + PHY_DP_CLK_CTL);
1042 	reg &= ~DP_PLL_ENABLE_MASK;
1043 	reg |= DP_PLL_ENABLE;
1044 	writel(reg, tcphy->base + PHY_DP_CLK_CTL);
1045 
1046 	ret = readl_poll_timeout(tcphy->base + PHY_DP_CLK_CTL, reg,
1047 				 reg & DP_PLL_READY,
1048 				 10, PHY_MODE_SET_TIMEOUT);
1049 	if (ret < 0) {
1050 		dev_err(tcphy->dev, "wait DP PLL ready timeout\n");
1051 		return ret;
1052 	}
1053 
1054 	/* Enable PMA PLL clocks */
1055 	reg = readl(tcphy->base + PHY_DP_CLK_CTL);
1056 	reg &= ~DP_PLL_CLOCK_ENABLE_MASK;
1057 	reg |= DP_PLL_CLOCK_ENABLE;
1058 	writel(reg, tcphy->base + PHY_DP_CLK_CTL);
1059 
1060 	ret = readl_poll_timeout(tcphy->base + PHY_DP_CLK_CTL, reg,
1061 				 reg & DP_PLL_CLOCK_ENABLE_ACK,
1062 				 10, PHY_MODE_SET_TIMEOUT);
1063 	if (ret) {
1064 		dev_err(tcphy->dev, "wait DP PLL clock enabled timeout\n");
1065 		return ret;
1066 	}
1067 
1068 	/* The PMA must go through the A2 power state upon a data rate change */
1069 	ret = tcphy_dp_set_power_state(tcphy, PHY_DP_POWER_STATE_A2);
1070 	if (ret) {
1071 		dev_err(tcphy->dev, "failed to enter A2 state: %d\n", ret);
1072 		return ret;
1073 	}
1074 
1075 	/* change the PHY power state to A0 */
1076 	ret = tcphy_dp_set_power_state(tcphy, PHY_DP_POWER_STATE_A0);
1077 	if (ret) {
1078 		dev_err(tcphy->dev, "failed to enter A0 state: %d\n", ret);
1079 		return ret;
1080 	}
1081 
1082 	return 0;
1083 }
1084 EXPORT_SYMBOL(tcphy_dp_set_link_rate);
1085 
property_enable(struct rockchip_typec_phy * tcphy,const struct usb3phy_reg * reg,bool en)1086 static inline int property_enable(struct rockchip_typec_phy *tcphy,
1087 				  const struct usb3phy_reg *reg, bool en)
1088 {
1089 	u32 mask = 1 << reg->write_enable;
1090 	u32 val = en << reg->enable_bit;
1091 
1092 	return regmap_write(tcphy->grf_regs, reg->offset, val | mask);
1093 }
1094 
tcphy_dp_aux_set_flip(struct rockchip_typec_phy * tcphy)1095 static void tcphy_dp_aux_set_flip(struct rockchip_typec_phy *tcphy)
1096 {
1097 	u16 tx_ana_ctrl_reg_1;
1098 
1099 	/*
1100 	 * Select the polarity of the xcvr:
1101 	 * 1, Reverses the polarity (If TYPEC, Pulls ups aux_p and pull
1102 	 * down aux_m)
1103 	 * 0, Normal polarity (if TYPEC, pulls up aux_m and pulls down
1104 	 * aux_p)
1105 	 */
1106 	tx_ana_ctrl_reg_1 = readl(tcphy->base + TX_ANA_CTRL_REG_1);
1107 	if (!tcphy->flip)
1108 		tx_ana_ctrl_reg_1 |= AUXDA_POLARITY;
1109 	else
1110 		tx_ana_ctrl_reg_1 &= ~AUXDA_POLARITY;
1111 	writel(tx_ana_ctrl_reg_1, tcphy->base + TX_ANA_CTRL_REG_1);
1112 }
1113 
tcphy_dp_aux_calibration(struct rockchip_typec_phy * tcphy)1114 static void tcphy_dp_aux_calibration(struct rockchip_typec_phy *tcphy)
1115 {
1116 	u16 val;
1117 	u16 tx_ana_ctrl_reg_1;
1118 	u16 tx_ana_ctrl_reg_2;
1119 	s32 pu_calib_code, pd_calib_code;
1120 	s32 pu_adj, pd_adj;
1121 	u16 calib;
1122 
1123 	/*
1124 	 * Calculate calibration code as per docs: use an average of the
1125 	 * pull down and pull up.  Then add in adjustments.
1126 	 */
1127 	val = readl(tcphy->base + CMN_TXPUCAL_CTRL);
1128 	pu_calib_code = CMN_CALIB_CODE_POS(val);
1129 	val = readl(tcphy->base + CMN_TXPDCAL_CTRL);
1130 	pd_calib_code = CMN_CALIB_CODE_POS(val);
1131 	val = readl(tcphy->base + CMN_TXPU_ADJ_CTRL);
1132 	pu_adj = CMN_CALIB_CODE(val);
1133 	val = readl(tcphy->base + CMN_TXPD_ADJ_CTRL);
1134 	pd_adj = CMN_CALIB_CODE(val);
1135 	calib = (pu_calib_code + pd_calib_code) / 2 + pu_adj + pd_adj;
1136 
1137 	/* disable txda_cal_latch_en for rewrite the calibration values */
1138 	tx_ana_ctrl_reg_1 = readl(tcphy->base + TX_ANA_CTRL_REG_1);
1139 	tx_ana_ctrl_reg_1 &= ~TXDA_CAL_LATCH_EN;
1140 	writel(tx_ana_ctrl_reg_1, tcphy->base + TX_ANA_CTRL_REG_1);
1141 
1142 	/* write the calibration, then delay 10 ms as sample in docs */
1143 	val = readl(tcphy->base + TX_DIG_CTRL_REG_2);
1144 	val &= ~(TX_RESCAL_CODE_MASK << TX_RESCAL_CODE_OFFSET);
1145 	val |= calib << TX_RESCAL_CODE_OFFSET;
1146 	writel(val, tcphy->base + TX_DIG_CTRL_REG_2);
1147 	usleep_range(10000, 10050);
1148 
1149 	/*
1150 	 * Enable signal for latch that sample and holds calibration values.
1151 	 * Activate this signal for 1 clock cycle to sample new calibration
1152 	 * values.
1153 	 */
1154 	tx_ana_ctrl_reg_1 |= TXDA_CAL_LATCH_EN;
1155 	writel(tx_ana_ctrl_reg_1, tcphy->base + TX_ANA_CTRL_REG_1);
1156 	usleep_range(150, 200);
1157 
1158 	/* set TX Voltage Level and TX Deemphasis to 0 */
1159 	writel(0, tcphy->base + PHY_DP_TX_CTL);
1160 
1161 	/* re-enable decap */
1162 	tx_ana_ctrl_reg_2 = XCVR_DECAP_EN;
1163 	writel(tx_ana_ctrl_reg_2, tcphy->base + TX_ANA_CTRL_REG_2);
1164 	udelay(1);
1165 	tx_ana_ctrl_reg_2 |= XCVR_DECAP_EN_DEL;
1166 	writel(tx_ana_ctrl_reg_2, tcphy->base + TX_ANA_CTRL_REG_2);
1167 
1168 	writel(0, tcphy->base + TX_ANA_CTRL_REG_3);
1169 
1170 	tx_ana_ctrl_reg_1 |= TXDA_UPHY_SUPPLY_EN;
1171 	writel(tx_ana_ctrl_reg_1, tcphy->base + TX_ANA_CTRL_REG_1);
1172 	udelay(1);
1173 	tx_ana_ctrl_reg_1 |= TXDA_UPHY_SUPPLY_EN_DEL;
1174 	writel(tx_ana_ctrl_reg_1, tcphy->base + TX_ANA_CTRL_REG_1);
1175 
1176 	writel(0, tcphy->base + TX_ANA_CTRL_REG_5);
1177 
1178 	/*
1179 	 * Programs txda_drv_ldo_prog[15:0], Sets driver LDO
1180 	 * voltage 16'h1001 for DP-AUX-TX and RX
1181 	 */
1182 	writel(0x1001, tcphy->base + TX_ANA_CTRL_REG_4);
1183 
1184 	/* re-enables Bandgap reference for LDO */
1185 	tx_ana_ctrl_reg_1 |= TXDA_DRV_LDO_EN;
1186 	writel(tx_ana_ctrl_reg_1, tcphy->base + TX_ANA_CTRL_REG_1);
1187 	udelay(5);
1188 	tx_ana_ctrl_reg_1 |= TXDA_BGREF_EN;
1189 	writel(tx_ana_ctrl_reg_1, tcphy->base + TX_ANA_CTRL_REG_1);
1190 
1191 	/*
1192 	 * re-enables the transmitter pre-driver, driver data selection MUX,
1193 	 * and receiver detect circuits.
1194 	 */
1195 	tx_ana_ctrl_reg_2 |= TXDA_DRV_PREDRV_EN;
1196 	writel(tx_ana_ctrl_reg_2, tcphy->base + TX_ANA_CTRL_REG_2);
1197 	udelay(1);
1198 	tx_ana_ctrl_reg_2 |= TXDA_DRV_PREDRV_EN_DEL;
1199 	writel(tx_ana_ctrl_reg_2, tcphy->base + TX_ANA_CTRL_REG_2);
1200 
1201 	/*
1202 	 * Do all the undocumented magic:
1203 	 * - Turn on TXDA_DP_AUX_EN, whatever that is, even though sample
1204 	 *   never shows this going on.
1205 	 * - Turn on TXDA_DECAP_EN (and TXDA_DECAP_EN_DEL) even though
1206 	 *   docs say for aux it's always 0.
1207 	 * - Turn off the LDO and BGREF, which we just spent time turning
1208 	 *   on above (???).
1209 	 *
1210 	 * Without this magic, things seem worse.
1211 	 */
1212 	tx_ana_ctrl_reg_1 |= TXDA_DP_AUX_EN;
1213 	tx_ana_ctrl_reg_1 |= TXDA_DECAP_EN;
1214 	tx_ana_ctrl_reg_1 &= ~TXDA_DRV_LDO_EN;
1215 	tx_ana_ctrl_reg_1 &= ~TXDA_BGREF_EN;
1216 	writel(tx_ana_ctrl_reg_1, tcphy->base + TX_ANA_CTRL_REG_1);
1217 	udelay(1);
1218 	tx_ana_ctrl_reg_1 |= TXDA_DECAP_EN_DEL;
1219 	writel(tx_ana_ctrl_reg_1, tcphy->base + TX_ANA_CTRL_REG_1);
1220 
1221 	/*
1222 	 * Undo the work we did to set the LDO voltage.
1223 	 * This doesn't seem to help nor hurt, but it kinda goes with the
1224 	 * undocumented magic above.
1225 	 */
1226 	writel(0, tcphy->base + TX_ANA_CTRL_REG_4);
1227 
1228 	/* Don't set voltage swing to 400 mV peak to peak (differential) */
1229 	writel(0, tcphy->base + TXDA_COEFF_CALC_CTRL);
1230 
1231 	/* Init TXDA_CYA_AUXDA_CYA for unknown magic reasons */
1232 	writel(0, tcphy->base + TXDA_CYA_AUXDA_CYA);
1233 
1234 	/*
1235 	 * More undocumented magic, presumably the goal of which is to
1236 	 * make the "auxda_source_aux_oen" be ignored and instead to decide
1237 	 * about "high impedance state" based on what software puts in the
1238 	 * register TXDA_COEFF_CALC_CTRL (see TX_HIGH_Z).  Since we only
1239 	 * program that register once and we don't set the bit TX_HIGH_Z,
1240 	 * presumably the goal here is that we should never put the analog
1241 	 * driver in high impedance state.
1242 	 */
1243 	val = readl(tcphy->base + TX_DIG_CTRL_REG_2);
1244 	val |= TX_HIGH_Z_TM_EN;
1245 	writel(val, tcphy->base + TX_DIG_CTRL_REG_2);
1246 }
1247 
tcphy_cfg_usb3_to_usb2_only(struct rockchip_typec_phy * tcphy,bool value)1248 static int tcphy_cfg_usb3_to_usb2_only(struct rockchip_typec_phy *tcphy,
1249 				       bool value)
1250 {
1251 	const struct rockchip_usb3phy_port_cfg *cfg = tcphy->port_cfgs;
1252 
1253 	property_enable(tcphy, &cfg->usb3tousb2_en, value);
1254 	property_enable(tcphy, &cfg->usb3_host_disable, value);
1255 	property_enable(tcphy, &cfg->usb3_host_port, !value);
1256 
1257 	return 0;
1258 }
1259 
tcphy_phy_init(struct rockchip_typec_phy * tcphy,u8 mode)1260 static int tcphy_phy_init(struct rockchip_typec_phy *tcphy, u8 mode)
1261 {
1262 	const struct rockchip_usb3phy_port_cfg *cfg = tcphy->port_cfgs;
1263 	int ret, i;
1264 	u32 val;
1265 
1266 	ret = clk_prepare_enable(tcphy->clk_core);
1267 	if (ret) {
1268 		dev_err(tcphy->dev, "Failed to prepare_enable core clock\n");
1269 		return ret;
1270 	}
1271 
1272 	ret = clk_prepare_enable(tcphy->clk_ref);
1273 	if (ret) {
1274 		dev_err(tcphy->dev, "Failed to prepare_enable ref clock\n");
1275 		goto err_clk_core;
1276 	}
1277 
1278 	reset_control_deassert(tcphy->tcphy_rst);
1279 
1280 	property_enable(tcphy, &cfg->typec_conn_dir, tcphy->flip);
1281 	tcphy_dp_aux_set_flip(tcphy);
1282 
1283 	tcphy_cfg_24m(tcphy);
1284 	tcphy_set_lane_mapping(tcphy, mode);
1285 
1286 	if (mode == MODE_DFP_DP) {
1287 		tcphy_cfg_usb3_to_usb2_only(tcphy, true);
1288 		tcphy_cfg_dp_pll(tcphy, DP_DEFAULT_RATE);
1289 		for (i = 0; i < 4; i++)
1290 			tcphy_dp_cfg_lane(tcphy, DP_DEFAULT_RATE, 0, 0, i);
1291 	} else {
1292 		tcphy_cfg_usb3_pll(tcphy);
1293 		tcphy_cfg_dp_pll(tcphy, DP_DEFAULT_RATE);
1294 		if (tcphy->flip) {
1295 			tcphy_tx_usb3_cfg_lane(tcphy, 3);
1296 			tcphy_rx_usb3_cfg_lane(tcphy, 2);
1297 			tcphy_dp_cfg_lane(tcphy, DP_DEFAULT_RATE, 0, 0, 0);
1298 			tcphy_dp_cfg_lane(tcphy, DP_DEFAULT_RATE, 0, 0, 1);
1299 		} else {
1300 			tcphy_tx_usb3_cfg_lane(tcphy, 0);
1301 			tcphy_rx_usb3_cfg_lane(tcphy, 1);
1302 			tcphy_dp_cfg_lane(tcphy, DP_DEFAULT_RATE, 0, 0, 2);
1303 			tcphy_dp_cfg_lane(tcphy, DP_DEFAULT_RATE, 0, 0, 3);
1304 		}
1305 	}
1306 
1307 	val = readl(tcphy->base + PHY_DP_MODE_CTL);
1308 	val &= ~DP_MODE_MASK;
1309 	val |= DP_MODE_ENTER_A2 | DP_LINK_RESET_DEASSERTED;
1310 	writel(val, tcphy->base + PHY_DP_MODE_CTL);
1311 
1312 	reset_control_deassert(tcphy->uphy_rst);
1313 
1314 	ret = readx_poll_timeout(readl, tcphy->base + PMA_CMN_CTRL1,
1315 				 val, val & CMN_READY, 10,
1316 				 PHY_MODE_SET_TIMEOUT);
1317 	if (ret < 0) {
1318 		dev_err(tcphy->dev, "wait pma ready timeout\n");
1319 		ret = -ETIMEDOUT;
1320 		goto err_wait_pma;
1321 	}
1322 
1323 	reset_control_deassert(tcphy->pipe_rst);
1324 
1325 	return 0;
1326 
1327 err_wait_pma:
1328 	reset_control_assert(tcphy->uphy_rst);
1329 	reset_control_assert(tcphy->tcphy_rst);
1330 	clk_disable_unprepare(tcphy->clk_ref);
1331 err_clk_core:
1332 	clk_disable_unprepare(tcphy->clk_core);
1333 	return ret;
1334 }
1335 
tcphy_phy_deinit(struct rockchip_typec_phy * tcphy)1336 static void tcphy_phy_deinit(struct rockchip_typec_phy *tcphy)
1337 {
1338 	reset_control_assert(tcphy->tcphy_rst);
1339 	reset_control_assert(tcphy->uphy_rst);
1340 	reset_control_assert(tcphy->pipe_rst);
1341 	clk_disable_unprepare(tcphy->clk_core);
1342 	clk_disable_unprepare(tcphy->clk_ref);
1343 }
1344 
tcphy_get_mode(struct rockchip_typec_phy * tcphy)1345 static int tcphy_get_mode(struct rockchip_typec_phy *tcphy)
1346 {
1347 	return tcphy->new_mode;
1348 }
1349 
tcphy_orien_sw_set(struct typec_switch * sw,enum typec_orientation orien)1350 static int tcphy_orien_sw_set(struct typec_switch *sw,
1351 			      enum typec_orientation orien)
1352 {
1353 	struct rockchip_typec_phy *tcphy = typec_switch_get_drvdata(sw);
1354 
1355 	mutex_lock(&tcphy->lock);
1356 
1357 	if (orien == TYPEC_ORIENTATION_NONE) {
1358 		tcphy->new_mode = MODE_DISCONNECT;
1359 		goto unlock_ret;
1360 	}
1361 
1362 	tcphy->flip = (orien == TYPEC_ORIENTATION_REVERSE) ? true : false;
1363 	tcphy->new_mode = MODE_DFP_USB;
1364 
1365 unlock_ret:
1366 	mutex_unlock(&tcphy->lock);
1367 	return 0;
1368 }
1369 
tcphy_setup_orien_switch(struct rockchip_typec_phy * tcphy)1370 static int tcphy_setup_orien_switch(struct rockchip_typec_phy *tcphy)
1371 {
1372 	struct typec_switch_desc sw_desc = { };
1373 
1374 	sw_desc.drvdata = tcphy;
1375 	sw_desc.fwnode = dev_fwnode(tcphy->dev);
1376 	sw_desc.set = tcphy_orien_sw_set;
1377 
1378 	tcphy->sw = typec_switch_register(tcphy->dev, &sw_desc);
1379 	if (IS_ERR(tcphy->sw)) {
1380 		dev_err(tcphy->dev, "Error register typec orientation switch: %ld\n",
1381 			PTR_ERR(tcphy->sw));
1382 		return PTR_ERR(tcphy->sw);
1383 	}
1384 
1385 	return 0;
1386 }
1387 
udphy_orien_switch_unregister(void * data)1388 static void udphy_orien_switch_unregister(void *data)
1389 {
1390 	struct rockchip_typec_phy *tcphy = data;
1391 
1392 	typec_switch_unregister(tcphy->sw);
1393 }
1394 
_rockchip_usb3_phy_power_on(struct rockchip_typec_phy * tcphy)1395 static int _rockchip_usb3_phy_power_on(struct rockchip_typec_phy *tcphy)
1396 {
1397 	const struct rockchip_usb3phy_port_cfg *cfg = tcphy->port_cfgs;
1398 	const struct usb3phy_reg *reg = &cfg->pipe_status;
1399 	int timeout, new_mode, ret = 0;
1400 	u32 val;
1401 
1402 	mutex_lock(&tcphy->lock);
1403 
1404 	new_mode = tcphy_get_mode(tcphy);
1405 	if (new_mode < 0) {
1406 		ret = new_mode;
1407 		goto unlock_ret;
1408 	}
1409 
1410 	/* DP-only mode; fall back to USB2 */
1411 	if (!(new_mode & (MODE_DFP_USB | MODE_UFP_USB))) {
1412 		tcphy_cfg_usb3_to_usb2_only(tcphy, true);
1413 		goto unlock_ret;
1414 	}
1415 
1416 	if (tcphy->mode == new_mode)
1417 		goto unlock_ret;
1418 
1419 	if (tcphy->mode == MODE_DISCONNECT) {
1420 		ret = tcphy_phy_init(tcphy, new_mode);
1421 		if (ret)
1422 			goto unlock_ret;
1423 	}
1424 
1425 	/* wait TCPHY for pipe ready */
1426 	for (timeout = 0; timeout < 100; timeout++) {
1427 		regmap_read(tcphy->grf_regs, reg->offset, &val);
1428 		if (!(val & BIT(reg->enable_bit))) {
1429 			tcphy->mode |= new_mode & (MODE_DFP_USB | MODE_UFP_USB);
1430 			/* enable usb3 host */
1431 			tcphy_cfg_usb3_to_usb2_only(tcphy, false);
1432 			goto unlock_ret;
1433 		}
1434 		usleep_range(10, 20);
1435 	}
1436 
1437 	if (tcphy->mode == MODE_DISCONNECT)
1438 		tcphy_phy_deinit(tcphy);
1439 
1440 	ret = -ETIMEDOUT;
1441 
1442 unlock_ret:
1443 	mutex_unlock(&tcphy->lock);
1444 	return ret;
1445 }
1446 
rockchip_usb3_phy_power_on(struct phy * phy)1447 static int rockchip_usb3_phy_power_on(struct phy *phy)
1448 {
1449 	struct rockchip_typec_phy *tcphy = phy_get_drvdata(phy);
1450 	int ret;
1451 	int tries;
1452 
1453 	for (tries = 0; tries < POWER_ON_TRIES; tries++) {
1454 		ret = _rockchip_usb3_phy_power_on(tcphy);
1455 		if (!ret)
1456 			break;
1457 	}
1458 
1459 	if (tries && !ret)
1460 		dev_info(tcphy->dev, "Needed %d loops to turn on\n", tries);
1461 
1462 	return ret;
1463 }
1464 
rockchip_usb3_phy_power_off(struct phy * phy)1465 static int rockchip_usb3_phy_power_off(struct phy *phy)
1466 {
1467 	struct rockchip_typec_phy *tcphy = phy_get_drvdata(phy);
1468 
1469 	mutex_lock(&tcphy->lock);
1470 	tcphy_cfg_usb3_to_usb2_only(tcphy, false);
1471 
1472 	if (tcphy->mode == MODE_DISCONNECT)
1473 		goto unlock;
1474 
1475 	tcphy->mode &= ~(MODE_UFP_USB | MODE_DFP_USB);
1476 	if (tcphy->mode == MODE_DISCONNECT)
1477 		tcphy_phy_deinit(tcphy);
1478 
1479 unlock:
1480 	mutex_unlock(&tcphy->lock);
1481 	return 0;
1482 }
1483 
1484 static const struct phy_ops rockchip_usb3_phy_ops = {
1485 	.power_on	= rockchip_usb3_phy_power_on,
1486 	.power_off	= rockchip_usb3_phy_power_off,
1487 	.owner		= THIS_MODULE,
1488 };
1489 
rockchip_dp_phy_power_on(struct phy * phy)1490 static int rockchip_dp_phy_power_on(struct phy *phy)
1491 {
1492 	struct rockchip_typec_phy *tcphy = phy_get_drvdata(phy);
1493 	const struct rockchip_usb3phy_port_cfg *cfg = tcphy->port_cfgs;
1494 	int new_mode, ret = 0;
1495 	u32 val;
1496 
1497 	mutex_lock(&tcphy->lock);
1498 
1499 	new_mode = tcphy_get_mode(tcphy);
1500 	if (new_mode < 0) {
1501 		ret = new_mode;
1502 		goto unlock_ret;
1503 	}
1504 
1505 	if (!(new_mode & MODE_DFP_DP)) {
1506 		ret = -ENODEV;
1507 		goto unlock_ret;
1508 	}
1509 
1510 	if (tcphy->mode == new_mode)
1511 		goto unlock_ret;
1512 
1513 	/*
1514 	 * If the PHY has been power on, but the mode is not DP only mode,
1515 	 * re-init the PHY for setting all of 4 lanes to DP.
1516 	 */
1517 	if (new_mode == MODE_DFP_DP && tcphy->mode != MODE_DISCONNECT) {
1518 		tcphy_phy_deinit(tcphy);
1519 		ret = tcphy_phy_init(tcphy, new_mode);
1520 	} else if (tcphy->mode == MODE_DISCONNECT) {
1521 		ret = tcphy_phy_init(tcphy, new_mode);
1522 	}
1523 	if (ret)
1524 		goto unlock_ret;
1525 
1526 	property_enable(tcphy, &cfg->uphy_dp_sel, 1);
1527 
1528 	ret = readx_poll_timeout(readl, tcphy->base + PHY_DP_MODE_CTL,
1529 				 val, val & DP_MODE_A2_ACK, 1000,
1530 				 PHY_MODE_SET_TIMEOUT);
1531 	if (ret < 0) {
1532 		dev_err(tcphy->dev, "failed to wait TCPHY enter A2\n");
1533 		goto power_on_finish;
1534 	}
1535 
1536 	tcphy_dp_aux_calibration(tcphy);
1537 
1538 	/* enter A0 mode */
1539 	ret = tcphy_dp_set_power_state(tcphy, PHY_DP_POWER_STATE_A0);
1540 	if (ret) {
1541 		dev_err(tcphy->dev, "failed to enter A0 power state\n");
1542 		goto power_on_finish;
1543 	}
1544 
1545 	tcphy->mode |= MODE_DFP_DP;
1546 
1547 power_on_finish:
1548 	if (tcphy->mode == MODE_DISCONNECT)
1549 		tcphy_phy_deinit(tcphy);
1550 unlock_ret:
1551 	mutex_unlock(&tcphy->lock);
1552 	return ret;
1553 }
1554 
rockchip_dp_phy_power_off(struct phy * phy)1555 static int rockchip_dp_phy_power_off(struct phy *phy)
1556 {
1557 	struct rockchip_typec_phy *tcphy = phy_get_drvdata(phy);
1558 	int ret;
1559 
1560 	mutex_lock(&tcphy->lock);
1561 
1562 	if (tcphy->mode == MODE_DISCONNECT)
1563 		goto unlock;
1564 
1565 	tcphy->mode &= ~MODE_DFP_DP;
1566 
1567 	ret = tcphy_dp_set_power_state(tcphy, PHY_DP_POWER_STATE_A2);
1568 	if (ret) {
1569 		dev_err(tcphy->dev, "failed to enter A2 power state\n");
1570 		goto unlock;
1571 	}
1572 
1573 	if (tcphy->mode == MODE_DISCONNECT)
1574 		tcphy_phy_deinit(tcphy);
1575 
1576 unlock:
1577 	mutex_unlock(&tcphy->lock);
1578 	return 0;
1579 }
1580 
1581 static const struct phy_ops rockchip_dp_phy_ops = {
1582 	.power_on	= rockchip_dp_phy_power_on,
1583 	.power_off	= rockchip_dp_phy_power_off,
1584 	.owner		= THIS_MODULE,
1585 };
1586 
tcphy_typec_mux_set(struct typec_mux * mux,struct typec_mux_state * state)1587 static int tcphy_typec_mux_set(struct typec_mux *mux, struct typec_mux_state *state)
1588 {
1589 	struct rockchip_typec_phy *tcphy = typec_mux_get_drvdata(mux);
1590 	struct typec_displayport_data *data;
1591 	int hpd = 0;
1592 
1593 	mutex_lock(&tcphy->lock);
1594 
1595 	switch (state->mode) {
1596 	case TYPEC_STATE_SAFE:
1597 		fallthrough;
1598 	case TYPEC_STATE_USB:
1599 		tcphy->new_mode = MODE_DFP_USB;
1600 		phy_set_bus_width(tcphy->phys[TYPEC_PHY_DP], 0);
1601 		break;
1602 	case TYPEC_DP_STATE_C:
1603 	case TYPEC_DP_STATE_E:
1604 		tcphy->new_mode = MODE_DFP_DP;
1605 		data = state->data;
1606 		hpd = !!(data->status & DP_STATUS_HPD_STATE);
1607 		phy_set_bus_width(tcphy->phys[TYPEC_PHY_DP], hpd ? 4 : 0);
1608 		break;
1609 	case TYPEC_DP_STATE_D:
1610 		tcphy->new_mode = MODE_DFP_DP | MODE_DFP_USB;
1611 		data = state->data;
1612 		hpd = !!(data->status & DP_STATUS_HPD_STATE);
1613 		phy_set_bus_width(tcphy->phys[TYPEC_PHY_DP], hpd ? 2 : 0);
1614 		break;
1615 	default:
1616 		break;
1617 	}
1618 
1619 	mutex_unlock(&tcphy->lock);
1620 
1621 	return 0;
1622 }
1623 
tcphy_setup_typec_mux(struct rockchip_typec_phy * tcphy)1624 static int tcphy_setup_typec_mux(struct rockchip_typec_phy *tcphy)
1625 {
1626 	struct typec_mux_desc mux_desc = {};
1627 
1628 	mux_desc.drvdata = tcphy;
1629 	mux_desc.fwnode = dev_fwnode(tcphy->dev);
1630 	mux_desc.set = tcphy_typec_mux_set;
1631 
1632 	tcphy->mux = typec_mux_register(tcphy->dev, &mux_desc);
1633 	if (IS_ERR(tcphy->mux)) {
1634 		dev_err(tcphy->dev, "Error register typec mux: %ld\n",
1635 			PTR_ERR(tcphy->mux));
1636 		return PTR_ERR(tcphy->mux);
1637 	}
1638 
1639 	return 0;
1640 }
1641 
tcphy_typec_mux_unregister(void * data)1642 static void tcphy_typec_mux_unregister(void *data)
1643 {
1644 	struct rockchip_typec_phy *tcphy = data;
1645 
1646 	typec_mux_unregister(tcphy->mux);
1647 }
1648 
tcphy_parse_dt(struct rockchip_typec_phy * tcphy,struct device * dev)1649 static int tcphy_parse_dt(struct rockchip_typec_phy *tcphy,
1650 			  struct device *dev)
1651 {
1652 	int ret;
1653 
1654 	tcphy->grf_regs = syscon_regmap_lookup_by_phandle(dev->of_node,
1655 							  "rockchip,grf");
1656 	if (IS_ERR(tcphy->grf_regs)) {
1657 		dev_err(dev, "could not find grf dt node\n");
1658 		return PTR_ERR(tcphy->grf_regs);
1659 	}
1660 
1661 	tcphy->clk_core = devm_clk_get(dev, "tcpdcore");
1662 	if (IS_ERR(tcphy->clk_core)) {
1663 		dev_err(dev, "could not get uphy core clock\n");
1664 		return PTR_ERR(tcphy->clk_core);
1665 	}
1666 
1667 	tcphy->clk_ref = devm_clk_get(dev, "tcpdphy-ref");
1668 	if (IS_ERR(tcphy->clk_ref)) {
1669 		dev_err(dev, "could not get uphy ref clock\n");
1670 		return PTR_ERR(tcphy->clk_ref);
1671 	}
1672 
1673 	tcphy->uphy_rst = devm_reset_control_get(dev, "uphy");
1674 	if (IS_ERR(tcphy->uphy_rst)) {
1675 		dev_err(dev, "no uphy_rst reset control found\n");
1676 		return PTR_ERR(tcphy->uphy_rst);
1677 	}
1678 
1679 	tcphy->pipe_rst = devm_reset_control_get(dev, "uphy-pipe");
1680 	if (IS_ERR(tcphy->pipe_rst)) {
1681 		dev_err(dev, "no pipe_rst reset control found\n");
1682 		return PTR_ERR(tcphy->pipe_rst);
1683 	}
1684 
1685 	tcphy->tcphy_rst = devm_reset_control_get(dev, "uphy-tcphy");
1686 	if (IS_ERR(tcphy->tcphy_rst)) {
1687 		dev_err(dev, "no tcphy_rst reset control found\n");
1688 		return PTR_ERR(tcphy->tcphy_rst);
1689 	}
1690 
1691 	/*
1692 	 * check if phy_config pass from dts, if no,
1693 	 * use default phy config value.
1694 	 */
1695 	ret = of_property_read_u32_array(dev->of_node, "rockchip,phy-config",
1696 		(u32 *)tcphy->config, sizeof(tcphy->config) / sizeof(u32));
1697 	if (ret)
1698 		memcpy(tcphy->config, tcphy_default_config,
1699 		       sizeof(tcphy->config));
1700 
1701 	return 0;
1702 }
1703 
typec_phy_pre_init(struct rockchip_typec_phy * tcphy)1704 static void typec_phy_pre_init(struct rockchip_typec_phy *tcphy)
1705 {
1706 	const struct rockchip_usb3phy_port_cfg *cfg = tcphy->port_cfgs;
1707 
1708 	reset_control_assert(tcphy->tcphy_rst);
1709 	reset_control_assert(tcphy->uphy_rst);
1710 	reset_control_assert(tcphy->pipe_rst);
1711 
1712 	/* select external psm clock */
1713 	property_enable(tcphy, &cfg->external_psm, 1);
1714 	property_enable(tcphy, &cfg->usb3tousb2_en, 0);
1715 
1716 	tcphy->mode = MODE_DISCONNECT;
1717 }
1718 
rockchip_typec_phy_probe(struct platform_device * pdev)1719 static int rockchip_typec_phy_probe(struct platform_device *pdev)
1720 {
1721 	struct device *dev = &pdev->dev;
1722 	struct device_node *np = dev->of_node;
1723 	struct device_node *child_np;
1724 	struct rockchip_typec_phy *tcphy;
1725 	struct phy_provider *phy_provider;
1726 	struct resource *res;
1727 	const struct rockchip_usb3phy_port_cfg *phy_cfgs;
1728 	const struct of_device_id *match;
1729 	int index, ret;
1730 
1731 	tcphy = devm_kzalloc(dev, sizeof(*tcphy), GFP_KERNEL);
1732 	if (!tcphy)
1733 		return -ENOMEM;
1734 
1735 	match = of_match_device(dev->driver->of_match_table, dev);
1736 	if (!match || !match->data) {
1737 		dev_err(dev, "phy configs are not assigned!\n");
1738 		return -EINVAL;
1739 	}
1740 
1741 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1742 	tcphy->base = devm_ioremap_resource(dev, res);
1743 	if (IS_ERR(tcphy->base))
1744 		return PTR_ERR(tcphy->base);
1745 
1746 	phy_cfgs = match->data;
1747 	/* find out a proper config which can be matched with dt. */
1748 	index = 0;
1749 	while (phy_cfgs[index].reg) {
1750 		if (phy_cfgs[index].reg == res->start) {
1751 			tcphy->port_cfgs = &phy_cfgs[index];
1752 			break;
1753 		}
1754 
1755 		++index;
1756 	}
1757 
1758 	if (!tcphy->port_cfgs) {
1759 		dev_err(dev, "no phy-config can be matched with %pOFn node\n",
1760 			np);
1761 		return -EINVAL;
1762 	}
1763 
1764 	ret = tcphy_parse_dt(tcphy, dev);
1765 	if (ret)
1766 		return ret;
1767 
1768 	tcphy->dev = dev;
1769 	tcphy->new_mode = MODE_DFP_USB;
1770 	platform_set_drvdata(pdev, tcphy);
1771 	mutex_init(&tcphy->lock);
1772 
1773 	typec_phy_pre_init(tcphy);
1774 
1775 	if (device_property_present(dev, "orientation-switch")) {
1776 		ret = tcphy_setup_orien_switch(tcphy);
1777 		if (ret)
1778 			return ret;
1779 		ret = devm_add_action_or_reset(dev, udphy_orien_switch_unregister,
1780 					       tcphy);
1781 		if (ret)
1782 			return ret;
1783 	}
1784 
1785 	if (device_property_present(dev, "svid")) {
1786 		ret = tcphy_setup_typec_mux(tcphy);
1787 		if (ret)
1788 			return ret;
1789 
1790 		ret = devm_add_action_or_reset(dev, tcphy_typec_mux_unregister, tcphy);
1791 		if (ret)
1792 			return ret;
1793 	}
1794 
1795 	pm_runtime_enable(dev);
1796 
1797 	for_each_available_child_of_node(np, child_np) {
1798 		struct phy *phy;
1799 
1800 		if (!of_node_cmp(child_np->name, "dp-port")) {
1801 			phy = devm_phy_create(dev, child_np,
1802 					      &rockchip_dp_phy_ops);
1803 			if (IS_ERR(phy)) {
1804 				dev_err(dev, "failed to create phy: %s\n",
1805 					child_np->name);
1806 				of_node_put(child_np);
1807 				ret = PTR_ERR(phy);
1808 				goto error;
1809 			}
1810 			tcphy->phys[TYPEC_PHY_DP] = phy;
1811 		} else if (!of_node_cmp(child_np->name, "usb3-port")) {
1812 			phy = devm_phy_create(dev, child_np,
1813 					      &rockchip_usb3_phy_ops);
1814 			if (IS_ERR(phy)) {
1815 				dev_err(dev, "failed to create phy: %s\n",
1816 					child_np->name);
1817 				of_node_put(child_np);
1818 				ret = PTR_ERR(phy);
1819 				goto error;
1820 			}
1821 			tcphy->phys[TYPEC_PHY_USB] = phy;
1822 		} else {
1823 			continue;
1824 
1825 		}
1826 
1827 		phy_set_drvdata(phy, tcphy);
1828 	}
1829 
1830 	phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
1831 	if (IS_ERR(phy_provider)) {
1832 		dev_err(dev, "Failed to register phy provider\n");
1833 		ret = PTR_ERR(phy_provider);
1834 		goto error;
1835 	}
1836 
1837 	return 0;
1838 
1839 error:
1840 	pm_runtime_disable(dev);
1841 	return ret;
1842 }
1843 
rockchip_typec_phy_remove(struct platform_device * pdev)1844 static int rockchip_typec_phy_remove(struct platform_device *pdev)
1845 {
1846 	pm_runtime_disable(&pdev->dev);
1847 
1848 	return 0;
1849 }
1850 
1851 static const struct of_device_id rockchip_typec_phy_dt_ids[] = {
1852 	{
1853 		.compatible = "rockchip,rk3399-typec-phy",
1854 		.data = &rk3399_usb3phy_port_cfgs
1855 	},
1856 	{ /* sentinel */ }
1857 };
1858 
1859 MODULE_DEVICE_TABLE(of, rockchip_typec_phy_dt_ids);
1860 
1861 static struct platform_driver rockchip_typec_phy_driver = {
1862 	.probe		= rockchip_typec_phy_probe,
1863 	.remove		= rockchip_typec_phy_remove,
1864 	.driver		= {
1865 		.name	= "rockchip-typec-phy",
1866 		.of_match_table = rockchip_typec_phy_dt_ids,
1867 	},
1868 };
1869 
1870 module_platform_driver(rockchip_typec_phy_driver);
1871 
1872 MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
1873 MODULE_AUTHOR("Kever Yang <kever.yang@rock-chips.com>");
1874 MODULE_DESCRIPTION("Rockchip USB TYPE-C PHY driver");
1875 MODULE_LICENSE("GPL v2");
1876