xref: /rk3399_rockchip-uboot/drivers/phy/phy-rockchip-samsung-hdptx.c (revision 6cef3c7b7cc45a9c882fc64e5496a416c99313b8)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Rockchip HDMI/DP Combo PHY with Samsung IP block
4  *
5  * Copyright (c) 2021 Rockchip Electronics Co. Ltd.
6  */
7 
8 #include <common.h>
9 #include <dm.h>
10 #include <generic-phy.h>
11 #include <reset.h>
12 #include <regmap.h>
13 #include <syscon.h>
14 #include <asm/io.h>
15 #include <linux/bitfield.h>
16 #include <linux/iopoll.h>
17 #include <asm/arch/clock.h>
18 
19 #define HDPTXPHY_GRF_CON0			0x0000
20 #define RO_REF_CLK_SEL				GENMASK(11, 10)
21 #define LC_REF_CLK_SEL				GENMASK(9, 8)
22 #define PLL_EN					BIT(7)
23 #define BIAS_EN					BIT(6)
24 #define BGR_EN					BIT(5)
25 #define HDPTX_MODE_SEL				BIT(0)
26 #define HDPTXPHY_GRF_STATUS0			0x0080
27 #define PLL_LOCK_DONE				BIT(3)
28 #define PHY_CLK_RDY				BIT(2)
29 #define PHY_RDY					BIT(1)
30 #define SB_RDY					BIT(0)
31 
32 /* cmn_reg0008 */
33 #define OVRD_LCPLL_EN				BIT(7)
34 #define LCPLL_EN				BIT(6)
35 
36 /* cmn_reg003C */
37 #define ANA_LCPLL_RESERVED7			BIT(7)
38 
39 /* cmn_reg003D */
40 #define OVRD_ROPLL_EN				BIT(7)
41 #define ROPLL_EN				BIT(6)
42 
43 /* cmn_reg0046 */
44 #define ROPLL_ANA_CPP_CTRL_COARSE		GENMASK(7, 4)
45 #define ROPLL_ANA_CPP_CTRL_FINE			GENMASK(3, 0)
46 
47 /* cmn_reg0047 */
48 #define ROPLL_ANA_LPF_C_SEL_COARSE		GENMASK(5, 3)
49 #define ROPLL_ANA_LPF_C_SEL_FINE		GENMASK(2, 0)
50 
51 /* cmn_reg004E */
52 #define ANA_ROPLL_PI_EN				BIT(5)
53 
54 /* cmn_reg0051 */
55 #define ROPLL_PMS_MDIV				GENMASK(7, 0)
56 
57 /* cmn_reg0055 */
58 #define ROPLL_PMS_MDIV_AFC			GENMASK(7, 0)
59 
60 /* cmn_reg0059 */
61 #define ANA_ROPLL_PMS_PDIV			GENMASK(7, 4)
62 #define ANA_ROPLL_PMS_REFDIV			GENMASK(3, 0)
63 
64 /* cmn_reg005A */
65 #define ROPLL_PMS_SDIV_RBR			GENMASK(7, 4)
66 #define ROPLL_PMS_SDIV_HBR			GENMASK(3, 0)
67 
68 /* cmn_reg005B */
69 #define ROPLL_PMS_SDIV_HBR2			GENMASK(7, 4)
70 #define ROPLL_PMS_SDIV_HBR3			GENMASK(3, 0)
71 
72 /* cmn_reg005D */
73 #define OVRD_ROPLL_REF_CLK_SEL			BIT(5)
74 #define ROPLL_REF_CLK_SEL			GENMASK(4, 3)
75 
76 /* cmn_reg005E */
77 #define ANA_ROPLL_SDM_EN			BIT(6)
78 #define OVRD_ROPLL_SDM_RSTN			BIT(5)
79 #define ROPLL_SDM_RSTN				BIT(4)
80 #define ROPLL_SDC_FRACTIONAL_EN_RBR		BIT(3)
81 #define ROPLL_SDC_FRACTIONAL_EN_HBR		BIT(2)
82 #define ROPLL_SDC_FRACTIONAL_EN_HBR2		BIT(1)
83 #define ROPLL_SDC_FRACTIONAL_EN_HBR3		BIT(0)
84 
85 /* cmn_reg005F */
86 #define OVRD_ROPLL_SDC_RSTN			BIT(5)
87 #define ROPLL_SDC_RSTN				BIT(4)
88 
89 /* cmn_reg0060 */
90 #define ROPLL_SDM_DENOMINATOR			GENMASK(7, 0)
91 
92 /* cmn_reg0064 */
93 #define ROPLL_SDM_NUMERATOR_SIGN_RBR		BIT(3)
94 #define ROPLL_SDM_NUMERATOR_SIGN_HBR		BIT(2)
95 #define ROPLL_SDM_NUMERATOR_SIGN_HBR2		BIT(1)
96 #define ROPLL_SDM_NUMERATOR_SIGN_HBR3		BIT(0)
97 
98 /* cmn_reg0065 */
99 #define ROPLL_SDM_NUMERATOR			GENMASK(7, 0)
100 
101 /* cmn_reg0069 */
102 #define ROPLL_SDC_N_RBR				GENMASK(2, 0)
103 
104 /* cmn_reg006A */
105 #define ROPLL_SDC_N_HBR				GENMASK(5, 3)
106 #define ROPLL_SDC_N_HBR2			GENMASK(2, 0)
107 
108 /* cmn_reg006B */
109 #define ROPLL_SDC_N_HBR3			GENMASK(3, 1)
110 
111 /* cmn_reg006C */
112 #define ROPLL_SDC_NUMERATOR			GENMASK(5, 0)
113 
114 /* cmn_reg0070 */
115 #define ROPLL_SDC_DENOMINATOR			GENMASK(5, 0)
116 
117 /* cmn_reg0074 */
118 #define OVRD_ROPLL_SDC_NDIV_RSTN		BIT(3)
119 #define ROPLL_SDC_NDIV_RSTN			BIT(2)
120 #define OVRD_ROPLL_SSC_EN			BIT(1)
121 #define ROPLL_SSC_EN				BIT(0)
122 
123 /* cmn_reg0075 */
124 #define ANA_ROPLL_SSC_FM_DEVIATION		GENMASK(5, 0)
125 
126 /* cmn_reg0076 */
127 #define ANA_ROPLL_SSC_FM_FREQ			GENMASK(6, 2)
128 
129 /* cmn_reg0077 */
130 #define ANA_ROPLL_SSC_CLK_DIV_SEL		GENMASK(6, 3)
131 
132 /* cmn_reg0081 */
133 #define ANA_PLL_CD_TX_SER_RATE_SEL		BIT(3)
134 #define ANA_PLL_CD_HSCLK_WEST_EN		BIT(1)
135 #define ANA_PLL_CD_HSCLK_EAST_EN		BIT(0)
136 
137 /* cmn_reg0082 */
138 #define ANA_PLL_CD_VREG_GAIN_CTRL		GENMASK(3, 0)
139 
140 /* cmn_reg0083 */
141 #define ANA_PLL_CD_VREG_ICTRL			GENMASK(6, 5)
142 
143 /* cmn_reg0084 */
144 #define PLL_LCRO_CLK_SEL			BIT(5)
145 
146 /* cmn_reg0085 */
147 #define ANA_PLL_SYNC_LOSS_DET_MODE		GENMASK(1, 0)
148 
149 /* cmn_reg0087 */
150 #define ANA_PLL_TX_HS_CLK_EN			BIT(2)
151 
152 /* cmn_reg0095 */
153 #define DP_TX_LINK_BW				GENMASK(1, 0)
154 
155 /* cmn_reg0097 */
156 #define DIG_CLK_SEL				BIT(1)
157 
158 /* cmn_reg0099 */
159 #define SSC_EN					GENMASK(7, 6)
160 #define CMN_ROPLL_ALONE_MODE			BIT(2)
161 
162 /* cmn_reg009A */
163 #define HS_SPEED_SEL				BIT(0)
164 
165 /* cmn_reg009B */
166 #define LS_SPEED_SEL				BIT(4)
167 
168 /* sb_reg0102 */
169 #define OVRD_SB_RXTERM_EN			BIT(5)
170 #define SB_RXRERM_EN				BIT(4)
171 #define ANA_SB_RXTERM_OFFSP			GENMASK(3, 0)
172 
173 /* sb_reg0103 */
174 #define ANA_SB_RXTERM_OFFSN			GENMASK(6, 3)
175 #define OVRD_SB_RX_RESCAL_DONE			BIT(1)
176 #define SB_RX_RESCAL_DONE			BIT(0)
177 
178 /* sb_reg0104 */
179 #define OVRD_SB_EN				BIT(5)
180 #define SB_EN					BIT(4)
181 #define OVRD_SB_AUX_EN				BIT(1)
182 #define SB_AUX_EN				BIT(0)
183 
184 /* sb_reg0105 */
185 #define ANA_SB_TX_HLVL_PROG			GENMASK(2, 0)
186 
187 /* sb_reg0106 */
188 #define ANA_SB_TX_LLVL_PROG			GENMASK(6, 4)
189 
190 /* sb_reg010D */
191 #define ANA_SB_DMRX_LPBK_DATA			BIT(4)
192 
193 /* sb_reg010F */
194 #define OVRD_SB_VREG_EN				BIT(7)
195 #define SB_VREG_EN				BIT(6)
196 #define ANA_SB_VREG_GAIN_CTRL			GENMASK(3, 0)
197 
198 /* sb_reg0110 */
199 #define ANA_SB_VREG_OUT_SEL			BIT(1)
200 #define ANA_SB_VREG_REF_SEL			BIT(0)
201 
202 /* sb_reg0113 */
203 #define SB_RX_RCAL_OPT_CODE			GENMASK(5, 4)
204 #define SB_RX_RTERM_CTRL			GENMASK(3, 0)
205 
206 /* sb_reg0114 */
207 #define SB_TG_SB_EN_DELAY_TIME			GENMASK(5, 3)
208 #define SB_TG_RXTERN_EN_DELAY_TIME		GENMASK(2, 0)
209 
210 /* sb_reg0115 */
211 #define SB_READY_DELAY_TIME			GENMASK(5, 3)
212 #define SB_TG_OSC_EN_DELAY_TIME			GENMASK(2, 0)
213 
214 /* sb_reg0116 */
215 #define SB_TG_OSC_EN_TO_AFC_RSTN_DELAT_TIME	GENMASK(6, 4)
216 
217 /* sb_reg0117 */
218 #define SB_TG_PLL_CD_VREG_FAST_PULSE_TIME	GENMASK(3, 0)
219 
220 /* sb_reg0118 */
221 #define SB_TG_EARC_DMRX_RECVRD_CLK_CNT		GENMASK(7, 0)
222 
223 /* sb_reg011A */
224 #define SB_TG_CNT_RUN_NO_7_0			GENMASK(7, 0)
225 
226 /* sb_reg011B */
227 #define SB_EARC_SIG_DET_BYPASS			BIT(4)
228 #define SB_AFC_TOL				GENMASK(3, 0)
229 
230 /* sb_reg011C */
231 #define SB_AFC_STB_NUM				GENMASK(3, 0)
232 
233 /* sb_reg011D */
234 #define SB_TG_OSC_CNT_MIN			GENMASK(7, 0)
235 
236 /* sb_reg011E */
237 #define SB_TG_OSC_CNT_MAX			GENMASK(7, 0)
238 
239 /* sb_reg011F */
240 #define SB_PWM_AFC_CTRL				GENMASK(7, 2)
241 #define SB_RCAL_RSTN				BIT(1)
242 
243 /* sb_reg0120 */
244 #define SB_AUX_EN_IN				BIT(7)
245 
246 /* sb_reg0123 */
247 #define OVRD_SB_READY				BIT(5)
248 #define SB_READY				BIT(4)
249 
250 /* lntop_reg0200 */
251 #define PROTOCOL_SEL				BIT(2)
252 
253 /* lntop_reg0206 */
254 #define DATA_BUS_WIDTH				GENMASK(2, 1)
255 #define BUS_WIDTH_SEL				BIT(0)
256 
257 /* lntop_reg0207 */
258 #define LANE_EN					GENMASK(3, 0)
259 
260 /* lane_reg0301 */
261 #define OVRD_LN_TX_DRV_EI_EN			BIT(7)
262 #define LN_TX_DRV_EI_EN				BIT(6)
263 
264 /* lane_reg0303 */
265 #define OVRD_LN_TX_DRV_LVL_CTRL			BIT(5)
266 #define LN_TX_DRV_LVL_CTRL			GENMASK(4, 0)
267 
268 /* lane_reg0304 */
269 #define OVRD_LN_TX_DRV_POST_LVL_CTRL		BIT(4)
270 #define LN_TX_DRV_POST_LVL_CTRL			GENMASK(3, 0)
271 
272 /* lane_reg0305 */
273 #define OVRD_LN_TX_DRV_PRE_LVL_CTRL		BIT(6)
274 #define LN_TX_DRV_PRE_LVL_CTRL			GENMASK(5, 2)
275 
276 /* lane_reg0306 */
277 #define LN_ANA_TX_DRV_IDRV_IDN_CTRL		GENMASK(7, 5)
278 #define LN_ANA_TX_DRV_IDRV_IUP_CTRL		GENMASK(4, 2)
279 #define LN_ANA_TX_DRV_ACCDRV_EN			BIT(0)
280 
281 /* lane_reg0307 */
282 #define LN_ANA_TX_DRV_ACCDRV_POL_SEL		BIT(6)
283 #define LN_ANA_TX_DRV_ACCDRV_CTRL		GENMASK(5, 3)
284 
285 /* lane_reg030A */
286 #define LN_ANA_TX_JEQ_EN			BIT(4)
287 #define LN_TX_JEQ_EVEN_CTRL_RBR			GENMASK(3, 0)
288 
289 /* lane_reg030B */
290 #define LN_TX_JEQ_EVEN_CTRL_HBR			GENMASK(7, 4)
291 #define LN_TX_JEQ_EVEN_CTRL_HBR2		GENMASK(3, 0)
292 
293 /* lane_reg030C */
294 #define LN_TX_JEQ_EVEN_CTRL_HBR3		GENMASK(7, 4)
295 #define LN_TX_JEQ_ODD_CTRL_RBR			GENMASK(3, 0)
296 
297 /* lane_reg030D */
298 #define LN_TX_JEQ_ODD_CTRL_HBR			GENMASK(7, 4)
299 #define LN_TX_JEQ_ODD_CTRL_HBR2			GENMASK(3, 0)
300 
301 /* lane_reg030E */
302 #define LN_TX_JEQ_ODD_CTRL_HBR3			GENMASK(7, 4)
303 
304 /* lane_reg0310 */
305 #define LN_ANA_TX_SYNC_LOSS_DET_MODE		GENMASK(1, 0)
306 
307 /* lane_reg0311 */
308 #define LN_TX_SER_40BIT_EN_RBR			BIT(3)
309 #define LN_TX_SER_40BIT_EN_HBR			BIT(2)
310 #define LN_TX_SER_40BIT_EN_HBR2			BIT(1)
311 #define LN_TX_SER_40BIT_EN_HBR3			BIT(0)
312 
313 /* lane_reg0316 */
314 #define LN_ANA_TX_SER_VREG_GAIN_CTRL		GENMASK(3, 0)
315 
316 /* lane_reg031B */
317 #define LN_ANA_TX_RESERVED			GENMASK(7, 0)
318 
319 /* lane_reg031E */
320 #define LN_POLARITY_INV				BIT(2)
321 #define LN_LANE_MODE				BIT(1)
322 
323 #define LANE_REG(lane, offset)			(0x400 * (lane) + (offset))
324 
325 struct rockchip_hdptx_phy {
326 	struct udevice *dev;
327 	struct regmap *regmap;
328 	struct regmap *grf;
329 
330 	struct reset_ctl apb_reset;
331 	struct reset_ctl cmn_reset;
332 	struct reset_ctl init_reset;
333 	struct reset_ctl lane_reset;
334 	u32 lane_polarity_invert[4];
335 };
336 
337 enum {
338 	DP_BW_RBR,
339 	DP_BW_HBR,
340 	DP_BW_HBR2,
341 	DP_BW_HBR3,
342 };
343 
344 enum {
345 	EDP_BW_2_16,
346 	EDP_BW_2_43,
347 	EDP_BW_3_24,
348 	EDP_BW_4_32,
349 };
350 
351 struct tx_drv_ctrl {
352 	u8 tx_drv_lvl_ctrl;
353 	u8 tx_drv_post_lvl_ctrl;
354 	u8 ana_tx_drv_idrv_idn_ctrl;
355 	u8 ana_tx_drv_idrv_iup_ctrl;
356 	u8 ana_tx_drv_accdrv_en;
357 	u8 ana_tx_drv_accdrv_ctrl;
358 	u8 tx_drv_pre_lvl_ctrl;
359 	u8 ana_tx_jeq_en;
360 	u8 tx_jeq_even_ctrl;
361 	u8 tx_jeq_odd_ctrl;
362 } __packed;
363 
364 struct tx_pll_ctrl {
365 	u8 mdiv;
366 	u8 sdiv;
367 	u8 sdm_denominator;
368 	u8 sdm_numerator_sign;
369 	u8 sdm_numerator;
370 	u8 sdc_clock_div;
371 	u8 sdc_numerator;
372 	u8 sdc_denominator;
373 	u8 ssc_deviation;
374 	u8 ssc_freq;
375 } __packed;
376 
377 static struct tx_drv_ctrl tx_drv_ctrl_rbr[4][4] = {
378 	/* voltage swing 0, pre-emphasis 0->3 */
379 	{
380 		{ 0x2, 0x0, 0x4, 0x6, 0x0, 0x4, 0x1, 0x1, 0x7, 0x7 },
381 		{ 0x4, 0x3, 0x4, 0x6, 0x0, 0x4, 0x0, 0x1, 0x7, 0x7 },
382 		{ 0x7, 0x6, 0x4, 0x6, 0x0, 0x4, 0x0, 0x1, 0x7, 0x7 },
383 		{ 0xd, 0xc, 0x7, 0x7, 0x1, 0x7, 0x0, 0x1, 0x7, 0x7 },
384 	},
385 
386 	/* voltage swing 1, pre-emphasis 0->2 */
387 	{
388 		{ 0x4, 0x0, 0x4, 0x6, 0x0, 0x4, 0x1, 0x1, 0x7, 0x7 },
389 		{ 0x9, 0x5, 0x4, 0x6, 0x0, 0x4, 0x0, 0x1, 0x7, 0x7 },
390 		{ 0xc, 0x8, 0x7, 0x7, 0x1, 0x7, 0x0, 0x1, 0x7, 0x7 },
391 	},
392 
393 	/* voltage swing 2, pre-emphasis 0->1 */
394 	{
395 		{ 0x8, 0x0, 0x4, 0x6, 0x0, 0x4, 0x1, 0x1, 0x7, 0x7 },
396 		{ 0xc, 0x5, 0x7, 0x7, 0x1, 0x7, 0x0, 0x1, 0x7, 0x7 },
397 	},
398 
399 	/* voltage swing 3, pre-emphasis 0 */
400 	{
401 		{ 0xb, 0x0, 0x7, 0x7, 0x1, 0x4, 0x1, 0x1, 0x7, 0x7 },
402 	}
403 };
404 
405 static struct tx_drv_ctrl tx_drv_ctrl_hbr[4][4] = {
406 	/* voltage swing 0, pre-emphasis 0->3 */
407 	{
408 		{ 0x2, 0x0, 0x4, 0x6, 0x0, 0x4, 0x1, 0x1, 0x7, 0x7 },
409 		{ 0x5, 0x4, 0x4, 0x6, 0x0, 0x4, 0x0, 0x1, 0x7, 0x7 },
410 		{ 0x9, 0x8, 0x4, 0x6, 0x0, 0x4, 0x0, 0x1, 0x7, 0x7 },
411 		{ 0xd, 0xc, 0x7, 0x7, 0x1, 0x7, 0x0, 0x1, 0x7, 0x7 },
412 	},
413 
414 	/* voltage swing 1, pre-emphasis 0->2 */
415 	{
416 		{ 0x6, 0x1, 0x4, 0x6, 0x0, 0x4, 0x1, 0x1, 0x7, 0x7 },
417 		{ 0xa, 0x6, 0x4, 0x6, 0x0, 0x4, 0x0, 0x1, 0x7, 0x7 },
418 		{ 0xc, 0x8, 0x7, 0x7, 0x1, 0x7, 0x0, 0x1, 0x7, 0x7 },
419 	},
420 
421 	/* voltage swing 2, pre-emphasis 0->1 */
422 	{
423 		{ 0x9, 0x1, 0x4, 0x6, 0x0, 0x4, 0x1, 0x1, 0x7, 0x7 },
424 		{ 0xd, 0x6, 0x7, 0x7, 0x1, 0x7, 0x0, 0x1, 0x7, 0x7 },
425 	},
426 
427 	/* voltage swing 3, pre-emphasis 0 */
428 	{
429 		{ 0xc, 0x1, 0x7, 0x7, 0x1, 0x4, 0x1, 0x1, 0x7, 0x7 },
430 	}
431 };
432 
433 static struct tx_drv_ctrl tx_drv_ctrl_hbr2[4][4] = {
434 	/* voltage swing 0, pre-emphasis 0->3 */
435 	{
436 		{ 0x2, 0x1, 0x4, 0x6, 0x0, 0x4, 0x0, 0x1, 0x7, 0x7 },
437 		{ 0x5, 0x4, 0x4, 0x6, 0x0, 0x4, 0x0, 0x1, 0x7, 0x7 },
438 		{ 0x9, 0x8, 0x4, 0x6, 0x1, 0x4, 0x0, 0x1, 0x7, 0x7 },
439 		{ 0xd, 0xc, 0x7, 0x7, 0x1, 0x7, 0x0, 0x1, 0x7, 0x7 },
440 	},
441 
442 	/* voltage swing 1, pre-emphasis 0->2 */
443 	{
444 		{ 0x6, 0x1, 0x4, 0x6, 0x0, 0x4, 0x1, 0x1, 0x7, 0x7 },
445 		{ 0xb, 0x7, 0x4, 0x6, 0x0, 0x4, 0x0, 0x1, 0x7, 0x7 },
446 		{ 0xd, 0x9, 0x7, 0x7, 0x1, 0x7, 0x0, 0x1, 0x7, 0x7 },
447 	},
448 
449 	/* voltage swing 2, pre-emphasis 0->1 */
450 	{
451 		{ 0x8, 0x1, 0x4, 0x6, 0x0, 0x4, 0x1, 0x1, 0x7, 0x7 },
452 		{ 0xc, 0x6, 0x7, 0x7, 0x1, 0x7, 0x0, 0x1, 0x7, 0x7 },
453 	},
454 
455 	/* voltage swing 3, pre-emphasis 0 */
456 	{
457 		{ 0xb, 0x0, 0x7, 0x7, 0x1, 0x4, 0x1, 0x1, 0x7, 0x7 },
458 	}
459 };
460 
461 static struct tx_drv_ctrl tx_drv_ctrl_r216_r243[4][4] = {
462 	/* voltage swing 0, pre-emphasis 0->3 */
463 	{
464 		{ 0x0, 0x1, 0x1, 0x1, 0x0, 0x4, 0x0, 0x1, 0x7, 0x7 },
465 		{ 0x1, 0x2, 0x4, 0x4, 0x0, 0x4, 0x1, 0x1, 0x7, 0x7 },
466 		{ 0x1, 0x3, 0x4, 0x4, 0x0, 0x4, 0x0, 0x1, 0x7, 0x7 },
467 		{ 0x3, 0x5, 0x4, 0x4, 0x0, 0x7, 0x0, 0x1, 0x7, 0x7 },
468 	},
469 
470 	/* voltage swing 1, pre-emphasis 0->2 */
471 	{
472 		{ 0x1, 0x1, 0x4, 0x4, 0x0, 0x4, 0x1, 0x1, 0x7, 0x7 },
473 		{ 0x2, 0x3, 0x4, 0x4, 0x0, 0x4, 0x0, 0x1, 0x7, 0x7 },
474 		{ 0x3, 0x4, 0x4, 0x4, 0x0, 0x7, 0x0, 0x1, 0x7, 0x7 },
475 	},
476 
477 	/* voltage swing 2, pre-emphasis 0->1 */
478 	{
479 		{ 0x1, 0x1, 0x4, 0x4, 0x0, 0x4, 0x0, 0x1, 0x7, 0x7 },
480 		{ 0x1, 0x2, 0x7, 0x7, 0x1, 0x7, 0x0, 0x1, 0x7, 0x7 },
481 	},
482 
483 	/* voltage swing 3, pre-emphasis 0 */
484 	{
485 		{ 0x3, 0x2, 0x2, 0x2, 0x0, 0x4, 0x0, 0x1, 0x7, 0x7 },
486 	}
487 };
488 
489 static struct tx_drv_ctrl tx_drv_ctrl_r324[4][4] = {
490 	/* voltage swing 0, pre-emphasis 0->3 */
491 	{
492 		{ 0x1, 0x2, 0x1, 0x1, 0x0, 0x4, 0x0, 0x1, 0x7, 0x7 },
493 		{ 0x2, 0x3, 0x1, 0x1, 0x0, 0x4, 0x0, 0x1, 0x7, 0x7 },
494 		{ 0x2, 0x4, 0x5, 0x5, 0x0, 0x4, 0x0, 0x1, 0x7, 0x7 },
495 		{ 0x4, 0x6, 0x4, 0x4, 0x0, 0x7, 0x0, 0x1, 0x7, 0x7 },
496 	},
497 
498 	/* voltage swing 1, pre-emphasis 0->2 */
499 	{
500 		{ 0x2, 0x2, 0x4, 0x4, 0x0, 0x4, 0x1, 0x1, 0x7, 0x7 },
501 		{ 0x2, 0x3, 0x4, 0x4, 0x0, 0x4, 0x0, 0x1, 0x7, 0x7 },
502 		{ 0x4, 0x5, 0x4, 0x4, 0x0, 0x7, 0x0, 0x1, 0x7, 0x7 },
503 	},
504 
505 	/* voltage swing 2, pre-emphasis 0->1 */
506 	{
507 		{ 0x2, 0x2, 0x4, 0x4, 0x0, 0x4, 0x0, 0x1, 0x7, 0x7 },
508 		{ 0x4, 0x4, 0x4, 0x4, 0x0, 0x7, 0x0, 0x1, 0x7, 0x7 },
509 	},
510 
511 	/* voltage swing 3, pre-emphasis 0 */
512 	{
513 		{ 0x3, 0x2, 0x4, 0x4, 0x0, 0x4, 0x0, 0x1, 0x7, 0x7 },
514 	}
515 };
516 
517 static struct tx_drv_ctrl tx_drv_ctrl_r432[4][4] = {
518 	/* voltage swing 0, pre-emphasis 0->3 */
519 	{
520 		{ 0x1, 0x2, 0x1, 0x1, 0x0, 0x4, 0x0, 0x1, 0x7, 0x7 },
521 		{ 0x2, 0x3, 0x1, 0x1, 0x0, 0x4, 0x0, 0x1, 0x7, 0x7 },
522 		{ 0x2, 0x4, 0x6, 0x6, 0x0, 0x4, 0x0, 0x1, 0x7, 0x7 },
523 		{ 0x4, 0x6, 0x6, 0x6, 0x0, 0x7, 0x0, 0x1, 0x7, 0x7 },
524 	},
525 
526 	/* voltage swing 1, pre-emphasis 0->2 */
527 	{
528 		{ 0x2, 0x2, 0x4, 0x4, 0x0, 0x4, 0x1, 0x1, 0x7, 0x7 },
529 		{ 0x3, 0x4, 0x4, 0x4, 0x0, 0x4, 0x0, 0x1, 0x7, 0x7 },
530 		{ 0x5, 0x6, 0x4, 0x4, 0x0, 0x7, 0x0, 0x1, 0x7, 0x7 },
531 	},
532 
533 	/* voltage swing 2, pre-emphasis 0->1 */
534 	{
535 		{ 0x2, 0x2, 0x4, 0x4, 0x0, 0x4, 0x0, 0x1, 0x7, 0x7 },
536 		{ 0x4, 0x4, 0x4, 0x4, 0x0, 0x7, 0x0, 0x1, 0x7, 0x7 },
537 	},
538 
539 	/* voltage swing 3, pre-emphasis 0 */
540 	{
541 		{ 0x5, 0x3, 0x1, 0x1, 0x0, 0x4, 0x0, 0x1, 0x7, 0x7 },
542 	}
543 };
544 
545 /* pll configurations for link rate R216/R243/R324/R432 */
546 static const struct tx_pll_ctrl tx_pll_ctrl_extra[4] = {
547 	{ 0x5a, 0x01, 0x32, 0x00, 0x00, 0x00, 0x01, 0x04, 0x0d, 0x1d }, /* R216 */
548 	{ 0x65, 0x01, 0x60, 0x00, 0x10, 0x01, 0x13, 0x18, 0x1c, 0x0d }, /* R243 */
549 	{ 0x87, 0x01, 0x21, 0x00, 0x00, 0x02, 0x03, 0x08, 0x0d, 0x1c }, /* R324 */
550 	{ 0x5a, 0x00, 0x32, 0x00, 0x00, 0x01, 0x01, 0x01, 0x0e, 0x1a }, /* R432 */
551 };
552 
553 static int rockchip_hdptx_phy_parse_training_table(struct udevice *dev)
554 {
555 	int size = sizeof(struct tx_drv_ctrl) * 10;
556 	const uint8_t *prop;
557 	u8 *buf, *training_table;
558 	int i, j;
559 
560 	prop = dev_read_u8_array_ptr(dev, "training-table", size);
561 	if (!prop)
562 		return 0;
563 
564 	buf = kzalloc(size, GFP_KERNEL);
565 	if (!buf)
566 		return -ENOMEM;
567 
568 	memcpy(buf, prop, size);
569 
570 	training_table = buf;
571 
572 	for (i = 0; i < 4; i++) {
573 		for (j = 0; j < 4; j++) {
574 			struct tx_drv_ctrl *ctrl;
575 
576 			if (i + j > 3)
577 				continue;
578 
579 			ctrl = (struct tx_drv_ctrl *)training_table;
580 			tx_drv_ctrl_rbr[i][j] = *ctrl;
581 			tx_drv_ctrl_hbr[i][j] = *ctrl;
582 			tx_drv_ctrl_hbr2[i][j] = *ctrl;
583 			training_table += sizeof(*ctrl);
584 		}
585 	}
586 
587 	kfree(buf);
588 
589 	return 0;
590 }
591 
592 static inline void rockchip_grf_write(struct regmap *grf, uint reg, uint mask,
593 				      uint val)
594 {
595 	regmap_write(grf, reg, (mask << 16) | (val & mask));
596 }
597 
598 static int rockchip_hdptx_phy_set_mode(struct phy *phy, enum phy_mode mode,
599 				       int submode)
600 {
601 	return 0;
602 }
603 
604 static int rockchip_hdptx_phy_verify_config(struct rockchip_hdptx_phy *hdptx,
605 					    struct phy_configure_opts_dp *dp)
606 {
607 	int i;
608 
609 	if (dp->set_rate) {
610 		switch (dp->link_rate) {
611 		case 1620:
612 		case 2160:
613 		case 2430:
614 		case 2700:
615 		case 3240:
616 		case 4320:
617 		case 5400:
618 			break;
619 		default:
620 			return -EINVAL;
621 		}
622 	}
623 
624 	switch (dp->lanes) {
625 	case 1:
626 	case 2:
627 	case 4:
628 		break;
629 	default:
630 		return -EINVAL;
631 	}
632 
633 	if (dp->set_voltages) {
634 		for (i = 0; i < dp->lanes; i++) {
635 			if (dp->voltage[i] > 3 || dp->pre[i] > 3)
636 				return -EINVAL;
637 
638 			if (dp->voltage[i] + dp->pre[i] > 3)
639 				return -EINVAL;
640 		}
641 	}
642 
643 	return 0;
644 }
645 
646 static void rockchip_hdptx_phy_set_voltage(struct rockchip_hdptx_phy *hdptx,
647 					   struct phy_configure_opts_dp *dp,
648 					   u8 lane)
649 {
650 	const struct tx_drv_ctrl *ctrl;
651 
652 	switch (dp->link_rate) {
653 	case 1620:
654 		ctrl = &tx_drv_ctrl_rbr[dp->voltage[lane]][dp->pre[lane]];
655 		regmap_update_bits(hdptx->regmap, LANE_REG(lane, 0x0c44),
656 				   LN_TX_SER_40BIT_EN_RBR,
657 				   FIELD_PREP(LN_TX_SER_40BIT_EN_RBR, 0x1));
658 		regmap_update_bits(hdptx->regmap, LANE_REG(lane, 0x0c28),
659 				   LN_TX_JEQ_EVEN_CTRL_RBR,
660 				   FIELD_PREP(LN_TX_JEQ_EVEN_CTRL_RBR, ctrl->tx_jeq_even_ctrl));
661 		regmap_update_bits(hdptx->regmap, LANE_REG(lane, 0x0c30),
662 				   LN_TX_JEQ_ODD_CTRL_RBR,
663 				   FIELD_PREP(LN_TX_JEQ_ODD_CTRL_RBR, ctrl->tx_jeq_odd_ctrl));
664 		break;
665 	case 2160:
666 	case 2430:
667 		ctrl = &tx_drv_ctrl_r216_r243[dp->voltage[lane]][dp->pre[lane]];
668 		regmap_update_bits(hdptx->regmap, LANE_REG(lane, 0x0c44),
669 				   LN_TX_SER_40BIT_EN_HBR,
670 				   FIELD_PREP(LN_TX_SER_40BIT_EN_HBR, 0x1));
671 		regmap_update_bits(hdptx->regmap, LANE_REG(lane, 0x0c2c),
672 				   LN_TX_JEQ_EVEN_CTRL_HBR,
673 				   FIELD_PREP(LN_TX_JEQ_EVEN_CTRL_HBR, ctrl->tx_jeq_even_ctrl));
674 		regmap_update_bits(hdptx->regmap, LANE_REG(lane, 0x0c34),
675 				   LN_TX_JEQ_ODD_CTRL_HBR,
676 				   FIELD_PREP(LN_TX_JEQ_ODD_CTRL_HBR, ctrl->tx_jeq_odd_ctrl));
677 		break;
678 	case 2700:
679 		ctrl = &tx_drv_ctrl_hbr[dp->voltage[lane]][dp->pre[lane]];
680 		regmap_update_bits(hdptx->regmap, LANE_REG(lane, 0x0c44),
681 				   LN_TX_SER_40BIT_EN_HBR,
682 				   FIELD_PREP(LN_TX_SER_40BIT_EN_HBR, 0x1));
683 		regmap_update_bits(hdptx->regmap, LANE_REG(lane, 0x0c2c),
684 				   LN_TX_JEQ_EVEN_CTRL_HBR,
685 				   FIELD_PREP(LN_TX_JEQ_EVEN_CTRL_HBR, ctrl->tx_jeq_even_ctrl));
686 		regmap_update_bits(hdptx->regmap, LANE_REG(lane, 0x0c34),
687 				   LN_TX_JEQ_ODD_CTRL_HBR,
688 				   FIELD_PREP(LN_TX_JEQ_ODD_CTRL_HBR, ctrl->tx_jeq_odd_ctrl));
689 		break;
690 	case 3240:
691 		ctrl = &tx_drv_ctrl_r324[dp->voltage[lane]][dp->pre[lane]];
692 		regmap_update_bits(hdptx->regmap, LANE_REG(lane, 0x0c44),
693 				   LN_TX_SER_40BIT_EN_HBR2,
694 				   FIELD_PREP(LN_TX_SER_40BIT_EN_HBR2, 0x1));
695 		regmap_update_bits(hdptx->regmap, LANE_REG(lane, 0x0c2c),
696 				   LN_TX_JEQ_EVEN_CTRL_HBR2,
697 				   FIELD_PREP(LN_TX_JEQ_EVEN_CTRL_HBR2, ctrl->tx_jeq_even_ctrl));
698 		regmap_update_bits(hdptx->regmap, LANE_REG(lane, 0x0c34),
699 				   LN_TX_JEQ_ODD_CTRL_HBR2,
700 				   FIELD_PREP(LN_TX_JEQ_ODD_CTRL_HBR2, ctrl->tx_jeq_odd_ctrl));
701 		break;
702 	case 4320:
703 		ctrl = &tx_drv_ctrl_r432[dp->voltage[lane]][dp->pre[lane]];
704 		regmap_update_bits(hdptx->regmap, LANE_REG(lane, 0x0c44),
705 				   LN_TX_SER_40BIT_EN_HBR2,
706 				   FIELD_PREP(LN_TX_SER_40BIT_EN_HBR2, 0x1));
707 		regmap_update_bits(hdptx->regmap, LANE_REG(lane, 0x0c2c),
708 				   LN_TX_JEQ_EVEN_CTRL_HBR2,
709 				   FIELD_PREP(LN_TX_JEQ_EVEN_CTRL_HBR2, ctrl->tx_jeq_even_ctrl));
710 		regmap_update_bits(hdptx->regmap, LANE_REG(lane, 0x0c34),
711 				   LN_TX_JEQ_ODD_CTRL_HBR2,
712 				   FIELD_PREP(LN_TX_JEQ_ODD_CTRL_HBR2, ctrl->tx_jeq_odd_ctrl));
713 		break;
714 	case 5400:
715 	default:
716 		ctrl = &tx_drv_ctrl_hbr2[dp->voltage[lane]][dp->pre[lane]];
717 		regmap_update_bits(hdptx->regmap, LANE_REG(lane, 0x0c44),
718 				   LN_TX_SER_40BIT_EN_HBR2,
719 				   FIELD_PREP(LN_TX_SER_40BIT_EN_HBR2, 0x1));
720 		regmap_update_bits(hdptx->regmap, LANE_REG(lane, 0x0c2c),
721 				   LN_TX_JEQ_EVEN_CTRL_HBR2,
722 				   FIELD_PREP(LN_TX_JEQ_EVEN_CTRL_HBR2, ctrl->tx_jeq_even_ctrl));
723 		regmap_update_bits(hdptx->regmap, LANE_REG(lane, 0x0c34),
724 				   LN_TX_JEQ_ODD_CTRL_HBR2,
725 				   FIELD_PREP(LN_TX_JEQ_ODD_CTRL_HBR2, ctrl->tx_jeq_odd_ctrl));
726 		break;
727 	}
728 
729 	regmap_update_bits(hdptx->regmap, LANE_REG(lane, 0x0c0c),
730 			   OVRD_LN_TX_DRV_LVL_CTRL | LN_TX_DRV_LVL_CTRL,
731 			   FIELD_PREP(OVRD_LN_TX_DRV_LVL_CTRL, 0x1) |
732 			   FIELD_PREP(LN_TX_DRV_LVL_CTRL,
733 				      ctrl->tx_drv_lvl_ctrl));
734 	regmap_update_bits(hdptx->regmap, LANE_REG(lane, 0x0c10),
735 			   OVRD_LN_TX_DRV_POST_LVL_CTRL |
736 			   LN_TX_DRV_POST_LVL_CTRL,
737 			   FIELD_PREP(OVRD_LN_TX_DRV_POST_LVL_CTRL, 0x1) |
738 			   FIELD_PREP(LN_TX_DRV_POST_LVL_CTRL,
739 				      ctrl->tx_drv_post_lvl_ctrl));
740 	regmap_update_bits(hdptx->regmap, LANE_REG(lane, 0x0c14),
741 			   OVRD_LN_TX_DRV_PRE_LVL_CTRL |
742 			   LN_TX_DRV_PRE_LVL_CTRL,
743 			   FIELD_PREP(OVRD_LN_TX_DRV_PRE_LVL_CTRL, 0x1) |
744 			   FIELD_PREP(LN_TX_DRV_PRE_LVL_CTRL,
745 				      ctrl->tx_drv_pre_lvl_ctrl));
746 	regmap_update_bits(hdptx->regmap, LANE_REG(lane, 0x0c18),
747 			   LN_ANA_TX_DRV_IDRV_IDN_CTRL |
748 			   LN_ANA_TX_DRV_IDRV_IUP_CTRL |
749 			   LN_ANA_TX_DRV_ACCDRV_EN,
750 			   FIELD_PREP(LN_ANA_TX_DRV_IDRV_IDN_CTRL,
751 				      ctrl->ana_tx_drv_idrv_idn_ctrl) |
752 			   FIELD_PREP(LN_ANA_TX_DRV_IDRV_IUP_CTRL,
753 				      ctrl->ana_tx_drv_idrv_iup_ctrl) |
754 			   FIELD_PREP(LN_ANA_TX_DRV_ACCDRV_EN,
755 				      ctrl->ana_tx_drv_accdrv_en));
756 	regmap_update_bits(hdptx->regmap, LANE_REG(lane, 0x0c1c),
757 			   LN_ANA_TX_DRV_ACCDRV_POL_SEL |
758 			   LN_ANA_TX_DRV_ACCDRV_CTRL,
759 			   FIELD_PREP(LN_ANA_TX_DRV_ACCDRV_POL_SEL, 0x1) |
760 			   FIELD_PREP(LN_ANA_TX_DRV_ACCDRV_CTRL,
761 				      ctrl->ana_tx_drv_accdrv_ctrl));
762 	regmap_update_bits(hdptx->regmap, LANE_REG(lane, 0x0c28),
763 			   LN_ANA_TX_JEQ_EN,
764 			   FIELD_PREP(LN_ANA_TX_JEQ_EN, ctrl->ana_tx_jeq_en));
765 	regmap_update_bits(hdptx->regmap, LANE_REG(lane, 0x0c6c),
766 			   LN_ANA_TX_RESERVED,
767 			   FIELD_PREP(LN_ANA_TX_RESERVED, 0x1));
768 	regmap_update_bits(hdptx->regmap, LANE_REG(lane, 0x0c58),
769 			   LN_ANA_TX_SER_VREG_GAIN_CTRL,
770 			   FIELD_PREP(LN_ANA_TX_SER_VREG_GAIN_CTRL, 0x2));
771 	regmap_update_bits(hdptx->regmap, LANE_REG(lane, 0x0c40),
772 			   LN_ANA_TX_SYNC_LOSS_DET_MODE,
773 			   FIELD_PREP(LN_ANA_TX_SYNC_LOSS_DET_MODE, 0x3));
774 }
775 
776 static int rockchip_hdptx_phy_set_voltages(struct rockchip_hdptx_phy *hdptx,
777 					   struct phy_configure_opts_dp *dp)
778 {
779 	u8 lane;
780 	u32 status;
781 	int ret;
782 
783 	for (lane = 0; lane < dp->lanes; lane++)
784 		rockchip_hdptx_phy_set_voltage(hdptx, dp, lane);
785 
786 	reset_deassert(&hdptx->lane_reset);
787 
788 	ret = regmap_read_poll_timeout(hdptx->grf, HDPTXPHY_GRF_STATUS0,
789 				       status, FIELD_GET(PHY_RDY, status),
790 				       50, 5000);
791 	if (ret) {
792 		dev_err(hdptx->dev, "timeout waiting for phy_rdy\n");
793 		return ret;
794 	}
795 
796 	return 0;
797 }
798 
799 static bool is_extra_recommended_link_rate(u32 link_rate)
800 {
801 	switch (link_rate) {
802 	case 2160:
803 	case 2430:
804 	case 3240:
805 	case 4320:
806 		return true;
807 	}
808 
809 	return false;
810 }
811 
812 static int rockchip_hdptx_phy_set_rate(struct rockchip_hdptx_phy *hdptx,
813 				       struct phy_configure_opts_dp *dp)
814 {
815 	u32 bw, status;
816 	u32 bw_extra = 0;
817 	int ret;
818 
819 	reset_assert(&hdptx->lane_reset);
820 	udelay(10);
821 	reset_assert(&hdptx->cmn_reset);
822 	udelay(10);
823 	rockchip_grf_write(hdptx->grf, HDPTXPHY_GRF_CON0, PLL_EN,
824 			   FIELD_PREP(PLL_EN, 0x0));
825 	udelay(10);
826 	regmap_update_bits(hdptx->regmap, 0x081c, LANE_EN,
827 			   FIELD_PREP(LANE_EN, 0x0));
828 
829 	switch (dp->link_rate) {
830 	case 1620:
831 		bw = DP_BW_RBR;
832 		break;
833 	case 2160:
834 		bw_extra = EDP_BW_2_16;
835 		bw = DP_BW_HBR;
836 		break;
837 	case 2430:
838 		bw_extra = EDP_BW_2_43;
839 		bw = DP_BW_HBR;
840 		break;
841 	case 2700:
842 		bw = DP_BW_HBR;
843 		break;
844 	case 3240:
845 		bw_extra = EDP_BW_3_24;
846 		bw = DP_BW_HBR2;
847 		break;
848 	case 4320:
849 		bw_extra = EDP_BW_4_32;
850 		bw = DP_BW_HBR2;
851 		break;
852 	case 5400:
853 		bw = DP_BW_HBR2;
854 		break;
855 	default:
856 		return -EINVAL;
857 	}
858 
859 	if (is_extra_recommended_link_rate(dp->link_rate)) {
860 		const struct tx_pll_ctrl *pll_ctrl = &tx_pll_ctrl_extra[bw_extra];
861 
862 		regmap_write(hdptx->regmap, 0x0144 + bw * 0x4,
863 			     FIELD_PREP(ROPLL_PMS_MDIV, pll_ctrl->mdiv));
864 		regmap_write(hdptx->regmap, 0x0180 + bw * 0x4,
865 			     FIELD_PREP(ROPLL_SDM_DENOMINATOR, pll_ctrl->sdm_denominator));
866 		regmap_write(hdptx->regmap, 0x0194 + bw * 0x4,
867 			     FIELD_PREP(ROPLL_SDM_NUMERATOR, pll_ctrl->sdm_numerator));
868 		regmap_write(hdptx->regmap, 0x01b0 + bw * 0x4,
869 			     FIELD_PREP(ROPLL_SDC_NUMERATOR, pll_ctrl->sdc_numerator));
870 		regmap_write(hdptx->regmap, 0x01c0 + bw * 0x4,
871 			     FIELD_PREP(ROPLL_SDC_DENOMINATOR, pll_ctrl->sdc_denominator));
872 
873 		if (bw == DP_BW_RBR) {
874 			regmap_update_bits(hdptx->regmap, 0x0168, ROPLL_PMS_SDIV_RBR,
875 					   FIELD_PREP(ROPLL_PMS_SDIV_RBR, pll_ctrl->sdiv));
876 			regmap_update_bits(hdptx->regmap, 0x0190, ROPLL_SDM_NUMERATOR_SIGN_RBR,
877 					   FIELD_PREP(ROPLL_SDM_NUMERATOR_SIGN_RBR,
878 						      pll_ctrl->sdm_numerator_sign));
879 			regmap_update_bits(hdptx->regmap, 0x01a4, ROPLL_SDC_N_RBR,
880 					   FIELD_PREP(ROPLL_SDC_N_RBR, pll_ctrl->sdc_clock_div));
881 		} else if (bw == DP_BW_HBR) {
882 			regmap_update_bits(hdptx->regmap, 0x0168, ROPLL_PMS_SDIV_HBR,
883 					   FIELD_PREP(ROPLL_PMS_SDIV_HBR, pll_ctrl->sdiv));
884 			regmap_update_bits(hdptx->regmap, 0x0190, ROPLL_SDM_NUMERATOR_SIGN_HBR,
885 					   FIELD_PREP(ROPLL_SDM_NUMERATOR_SIGN_HBR,
886 						      pll_ctrl->sdm_numerator_sign));
887 			regmap_update_bits(hdptx->regmap, 0x01a8, ROPLL_SDC_N_HBR,
888 					   FIELD_PREP(ROPLL_SDC_N_HBR, pll_ctrl->sdc_clock_div));
889 		} else {
890 			regmap_update_bits(hdptx->regmap, 0x016c, ROPLL_PMS_SDIV_HBR2,
891 					   FIELD_PREP(ROPLL_PMS_SDIV_HBR2, pll_ctrl->sdiv));
892 			regmap_update_bits(hdptx->regmap, 0x0190, ROPLL_SDM_NUMERATOR_SIGN_HBR2,
893 					   FIELD_PREP(ROPLL_SDM_NUMERATOR_SIGN_HBR2,
894 						      pll_ctrl->sdm_numerator_sign));
895 			regmap_update_bits(hdptx->regmap, 0x01a8, ROPLL_SDC_N_HBR2,
896 					   FIELD_PREP(ROPLL_SDC_N_HBR2, pll_ctrl->sdc_clock_div));
897 		}
898 	}
899 
900 	regmap_update_bits(hdptx->regmap, 0x0254, DP_TX_LINK_BW,
901 			   FIELD_PREP(DP_TX_LINK_BW, bw));
902 
903 	if (dp->ssc) {
904 		regmap_update_bits(hdptx->regmap, 0x01d0,
905 				   OVRD_ROPLL_SSC_EN | ROPLL_SSC_EN,
906 				   FIELD_PREP(OVRD_ROPLL_SSC_EN, 0x1) |
907 				   FIELD_PREP(ROPLL_SSC_EN, 0x1));
908 		if (is_extra_recommended_link_rate(dp->link_rate)) {
909 			const struct tx_pll_ctrl *pll_ctrl = &tx_pll_ctrl_extra[bw_extra];
910 
911 			regmap_write(hdptx->regmap, 0x01d4,
912 				     FIELD_PREP(ANA_ROPLL_SSC_FM_DEVIATION,
913 						pll_ctrl->ssc_deviation));
914 			regmap_update_bits(hdptx->regmap, 0x01d8,
915 					   ANA_ROPLL_SSC_FM_FREQ,
916 					   FIELD_PREP(ANA_ROPLL_SSC_FM_FREQ,
917 						      pll_ctrl->ssc_freq));
918 		} else {
919 			regmap_write(hdptx->regmap, 0x01d4,
920 				     FIELD_PREP(ANA_ROPLL_SSC_FM_DEVIATION, 0xc));
921 			regmap_update_bits(hdptx->regmap, 0x01d8,
922 					   ANA_ROPLL_SSC_FM_FREQ,
923 					   FIELD_PREP(ANA_ROPLL_SSC_FM_FREQ, 0x1f));
924 		}
925 		regmap_update_bits(hdptx->regmap, 0x0264, SSC_EN,
926 				   FIELD_PREP(SSC_EN, 0x2));
927 	} else {
928 		regmap_update_bits(hdptx->regmap, 0x01d0,
929 				   OVRD_ROPLL_SSC_EN | ROPLL_SSC_EN,
930 				   FIELD_PREP(OVRD_ROPLL_SSC_EN, 0x1) |
931 				   FIELD_PREP(ROPLL_SSC_EN, 0x0));
932 		regmap_update_bits(hdptx->regmap, 0x01d4,
933 				   ANA_ROPLL_SSC_FM_DEVIATION,
934 				   FIELD_PREP(ANA_ROPLL_SSC_FM_DEVIATION, 0x20));
935 		regmap_update_bits(hdptx->regmap, 0x01d8,
936 				   ANA_ROPLL_SSC_FM_FREQ,
937 				   FIELD_PREP(ANA_ROPLL_SSC_FM_FREQ, 0xc));
938 		regmap_update_bits(hdptx->regmap, 0x0264, SSC_EN,
939 				   FIELD_PREP(SSC_EN, 0x0));
940 	}
941 
942 	rockchip_grf_write(hdptx->grf, HDPTXPHY_GRF_CON0, PLL_EN,
943 			   FIELD_PREP(PLL_EN, 0x1));
944 	udelay(10);
945 	reset_deassert(&hdptx->cmn_reset);
946 	udelay(10);
947 
948 	ret = regmap_read_poll_timeout(hdptx->grf, HDPTXPHY_GRF_STATUS0,
949 				       status, FIELD_GET(PLL_LOCK_DONE, status),
950 				       50, 1000);
951 	if (ret) {
952 		dev_err(hdptx->dev, "timeout waiting for pll_lock_done\n");
953 		return ret;
954 	}
955 
956 	regmap_update_bits(hdptx->regmap, 0x081c, LANE_EN,
957 			   FIELD_PREP(LANE_EN, GENMASK(dp->lanes - 1, 0)));
958 
959 	return 0;
960 }
961 
962 static int rockchip_hdptx_phy_configure(struct phy *phy,
963 					union phy_configure_opts *opts)
964 {
965 	struct rockchip_hdptx_phy *hdptx = dev_get_priv(phy->dev);
966 	enum phy_mode mode = generic_phy_get_mode(phy);
967 	int ret;
968 
969 	if (mode != PHY_MODE_DP)
970 		return -EINVAL;
971 
972 	ret = rockchip_hdptx_phy_verify_config(hdptx, &opts->dp);
973 	if (ret) {
974 		dev_err(hdptx->dev, "invalid params for phy configure\n");
975 		return ret;
976 	}
977 
978 	if (opts->dp.set_rate) {
979 		ret = rockchip_hdptx_phy_set_rate(hdptx, &opts->dp);
980 		if (ret) {
981 			dev_err(hdptx->dev, "failed to set rate: %d\n", ret);
982 			return ret;
983 		}
984 	}
985 
986 	if (opts->dp.set_voltages) {
987 		ret = rockchip_hdptx_phy_set_voltages(hdptx, &opts->dp);
988 		if (ret) {
989 			dev_err(hdptx->dev, "failed to set voltages: %d\n",
990 				ret);
991 			return ret;
992 		}
993 	}
994 
995 	return 0;
996 }
997 
998 static void rockchip_hdptx_phy_dp_pll_init(struct rockchip_hdptx_phy *hdptx)
999 {
1000 	regmap_update_bits(hdptx->regmap, 0x0020, OVRD_LCPLL_EN | LCPLL_EN,
1001 			   FIELD_PREP(OVRD_LCPLL_EN, 0x1) |
1002 			   FIELD_PREP(LCPLL_EN, 0x0));
1003 	regmap_update_bits(hdptx->regmap, 0x00f4, OVRD_ROPLL_EN | ROPLL_EN,
1004 			   FIELD_PREP(OVRD_ROPLL_EN, 0x1) |
1005 			   FIELD_PREP(ROPLL_EN, 0x1));
1006 	regmap_update_bits(hdptx->regmap, 0x0138, ANA_ROPLL_PI_EN,
1007 			   FIELD_PREP(ANA_ROPLL_PI_EN, 0x1));
1008 	regmap_write(hdptx->regmap, 0x0144, FIELD_PREP(ROPLL_PMS_MDIV, 0x87));
1009 	regmap_write(hdptx->regmap, 0x0148, FIELD_PREP(ROPLL_PMS_MDIV, 0x71));
1010 	regmap_write(hdptx->regmap, 0x014c, FIELD_PREP(ROPLL_PMS_MDIV, 0x71));
1011 	regmap_write(hdptx->regmap, 0x0154,
1012 		     FIELD_PREP(ROPLL_PMS_MDIV_AFC, 0x87));
1013 	regmap_write(hdptx->regmap, 0x0158,
1014 		     FIELD_PREP(ROPLL_PMS_MDIV_AFC, 0x71));
1015 	regmap_write(hdptx->regmap, 0x015c,
1016 		     FIELD_PREP(ROPLL_PMS_MDIV_AFC, 0x71));
1017 	regmap_write(hdptx->regmap, 0x0164,
1018 		     FIELD_PREP(ANA_ROPLL_PMS_PDIV, 0x1) |
1019 		     FIELD_PREP(ANA_ROPLL_PMS_REFDIV, 0x1));
1020 	regmap_write(hdptx->regmap, 0x0168,
1021 		     FIELD_PREP(ROPLL_PMS_SDIV_RBR, 0x3) |
1022 		     FIELD_PREP(ROPLL_PMS_SDIV_HBR, 0x1));
1023 	regmap_update_bits(hdptx->regmap, 0x016c, ROPLL_PMS_SDIV_HBR2,
1024 			   FIELD_PREP(ROPLL_PMS_SDIV_HBR2, 0x0));
1025 	regmap_update_bits(hdptx->regmap, 0x0178, ANA_ROPLL_SDM_EN,
1026 			   FIELD_PREP(ANA_ROPLL_SDM_EN, 0x1));
1027 	regmap_update_bits(hdptx->regmap, 0x0178,
1028 			   OVRD_ROPLL_SDM_RSTN | ROPLL_SDM_RSTN,
1029 			   FIELD_PREP(OVRD_ROPLL_SDM_RSTN, 0x1) |
1030 			   FIELD_PREP(ROPLL_SDM_RSTN, 0x1));
1031 	regmap_update_bits(hdptx->regmap, 0x0178, ROPLL_SDC_FRACTIONAL_EN_RBR,
1032 			   FIELD_PREP(ROPLL_SDC_FRACTIONAL_EN_RBR, 0x1));
1033 	regmap_update_bits(hdptx->regmap, 0x0178, ROPLL_SDC_FRACTIONAL_EN_HBR,
1034 			   FIELD_PREP(ROPLL_SDC_FRACTIONAL_EN_HBR, 0x1));
1035 	regmap_update_bits(hdptx->regmap, 0x0178, ROPLL_SDC_FRACTIONAL_EN_HBR2,
1036 			   FIELD_PREP(ROPLL_SDC_FRACTIONAL_EN_HBR2, 0x1));
1037 	regmap_update_bits(hdptx->regmap, 0x017c,
1038 			   OVRD_ROPLL_SDC_RSTN | ROPLL_SDC_RSTN,
1039 			   FIELD_PREP(OVRD_ROPLL_SDC_RSTN, 0x1) |
1040 			   FIELD_PREP(ROPLL_SDC_RSTN, 0x1));
1041 	regmap_write(hdptx->regmap, 0x0180,
1042 		     FIELD_PREP(ROPLL_SDM_DENOMINATOR, 0x21));
1043 	regmap_write(hdptx->regmap, 0x0184,
1044 		     FIELD_PREP(ROPLL_SDM_DENOMINATOR, 0x27));
1045 	regmap_write(hdptx->regmap, 0x0188,
1046 		     FIELD_PREP(ROPLL_SDM_DENOMINATOR, 0x27));
1047 	regmap_update_bits(hdptx->regmap, 0x0190,
1048 			   ROPLL_SDM_NUMERATOR_SIGN_RBR |
1049 			   ROPLL_SDM_NUMERATOR_SIGN_HBR |
1050 			   ROPLL_SDM_NUMERATOR_SIGN_HBR2,
1051 			   FIELD_PREP(ROPLL_SDM_NUMERATOR_SIGN_RBR, 0x0) |
1052 			   FIELD_PREP(ROPLL_SDM_NUMERATOR_SIGN_HBR, 0x1) |
1053 			   FIELD_PREP(ROPLL_SDM_NUMERATOR_SIGN_HBR2, 0x1));
1054 	regmap_write(hdptx->regmap, 0x0194,
1055 		     FIELD_PREP(ROPLL_SDM_NUMERATOR, 0x0));
1056 	regmap_write(hdptx->regmap, 0x0198,
1057 		     FIELD_PREP(ROPLL_SDM_NUMERATOR, 0xd));
1058 	regmap_write(hdptx->regmap, 0x019c,
1059 		     FIELD_PREP(ROPLL_SDM_NUMERATOR, 0xd));
1060 	regmap_update_bits(hdptx->regmap, 0x01a4, ROPLL_SDC_N_RBR,
1061 			   FIELD_PREP(ROPLL_SDC_N_RBR, 0x2));
1062 	regmap_update_bits(hdptx->regmap, 0x01a8,
1063 			   ROPLL_SDC_N_HBR | ROPLL_SDC_N_HBR2,
1064 			   FIELD_PREP(ROPLL_SDC_N_HBR, 0x2) |
1065 			   FIELD_PREP(ROPLL_SDC_N_HBR2, 0x2));
1066 	regmap_write(hdptx->regmap, 0x01b0,
1067 		     FIELD_PREP(ROPLL_SDC_NUMERATOR, 0x3));
1068 	regmap_write(hdptx->regmap, 0x01b4,
1069 		     FIELD_PREP(ROPLL_SDC_NUMERATOR, 0x7));
1070 	regmap_write(hdptx->regmap, 0x01b8,
1071 		     FIELD_PREP(ROPLL_SDC_NUMERATOR, 0x7));
1072 	regmap_write(hdptx->regmap, 0x01c0,
1073 		     FIELD_PREP(ROPLL_SDC_DENOMINATOR, 0x8));
1074 	regmap_write(hdptx->regmap, 0x01c4,
1075 		     FIELD_PREP(ROPLL_SDC_DENOMINATOR, 0x18));
1076 	regmap_write(hdptx->regmap, 0x01c8,
1077 		     FIELD_PREP(ROPLL_SDC_DENOMINATOR, 0x18));
1078 	regmap_update_bits(hdptx->regmap, 0x01d0,
1079 			   OVRD_ROPLL_SDC_NDIV_RSTN | ROPLL_SDC_NDIV_RSTN,
1080 			   FIELD_PREP(OVRD_ROPLL_SDC_NDIV_RSTN, 0x1) |
1081 			   FIELD_PREP(ROPLL_SDC_NDIV_RSTN, 0x1));
1082 	regmap_update_bits(hdptx->regmap, 0x01dc, ANA_ROPLL_SSC_CLK_DIV_SEL,
1083 			   FIELD_PREP(ANA_ROPLL_SSC_CLK_DIV_SEL, 0x1));
1084 	regmap_update_bits(hdptx->regmap, 0x0118,
1085 			   ROPLL_ANA_CPP_CTRL_COARSE | ROPLL_ANA_CPP_CTRL_FINE,
1086 			   FIELD_PREP(ROPLL_ANA_CPP_CTRL_COARSE, 0xe) |
1087 			   FIELD_PREP(ROPLL_ANA_CPP_CTRL_FINE, 0xe));
1088 	regmap_update_bits(hdptx->regmap, 0x011c,
1089 			   ROPLL_ANA_LPF_C_SEL_COARSE |
1090 			   ROPLL_ANA_LPF_C_SEL_FINE,
1091 			   FIELD_PREP(ROPLL_ANA_LPF_C_SEL_COARSE, 0x4) |
1092 			   FIELD_PREP(ROPLL_ANA_LPF_C_SEL_FINE, 0x4));
1093 	regmap_update_bits(hdptx->regmap, 0x0204, ANA_PLL_CD_TX_SER_RATE_SEL,
1094 			   FIELD_PREP(ANA_PLL_CD_TX_SER_RATE_SEL, 0x0));
1095 	regmap_update_bits(hdptx->regmap, 0x025c, DIG_CLK_SEL,
1096 			   FIELD_PREP(DIG_CLK_SEL, 0x1));
1097 	regmap_update_bits(hdptx->regmap, 0x021c, ANA_PLL_TX_HS_CLK_EN,
1098 			   FIELD_PREP(ANA_PLL_TX_HS_CLK_EN, 0x1));
1099 	regmap_update_bits(hdptx->regmap, 0x0204,
1100 			   ANA_PLL_CD_HSCLK_EAST_EN | ANA_PLL_CD_HSCLK_WEST_EN,
1101 			   FIELD_PREP(ANA_PLL_CD_HSCLK_EAST_EN, 0x1) |
1102 			   FIELD_PREP(ANA_PLL_CD_HSCLK_WEST_EN, 0x0));
1103 	regmap_update_bits(hdptx->regmap, 0x0264, CMN_ROPLL_ALONE_MODE,
1104 			   FIELD_PREP(CMN_ROPLL_ALONE_MODE, 0x1));
1105 	regmap_update_bits(hdptx->regmap, 0x0208, ANA_PLL_CD_VREG_GAIN_CTRL,
1106 			   FIELD_PREP(ANA_PLL_CD_VREG_GAIN_CTRL, 0x4));
1107 	regmap_update_bits(hdptx->regmap, 0x00f0, ANA_LCPLL_RESERVED7,
1108 			   FIELD_PREP(ANA_LCPLL_RESERVED7, 0x1));
1109 	regmap_update_bits(hdptx->regmap, 0x020c, ANA_PLL_CD_VREG_ICTRL,
1110 			   FIELD_PREP(ANA_PLL_CD_VREG_ICTRL, 0x1));
1111 	regmap_update_bits(hdptx->regmap, 0x0214, ANA_PLL_SYNC_LOSS_DET_MODE,
1112 			   FIELD_PREP(ANA_PLL_SYNC_LOSS_DET_MODE, 0x3));
1113 	regmap_update_bits(hdptx->regmap, 0x0210, PLL_LCRO_CLK_SEL,
1114 			   FIELD_PREP(PLL_LCRO_CLK_SEL, 0x1));
1115 	regmap_update_bits(hdptx->regmap, 0x0268, HS_SPEED_SEL,
1116 			   FIELD_PREP(HS_SPEED_SEL, 0x1));
1117 	regmap_update_bits(hdptx->regmap, 0x026c, LS_SPEED_SEL,
1118 			   FIELD_PREP(LS_SPEED_SEL, 0x1));
1119 }
1120 
1121 static int rockchip_hdptx_phy_dp_aux_init(struct rockchip_hdptx_phy *hdptx)
1122 {
1123 	u32 status;
1124 	int ret;
1125 
1126 	regmap_update_bits(hdptx->regmap, 0x0414, ANA_SB_TX_HLVL_PROG,
1127 			   FIELD_PREP(ANA_SB_TX_HLVL_PROG, 0x7));
1128 	regmap_update_bits(hdptx->regmap, 0x0418, ANA_SB_TX_LLVL_PROG,
1129 			   FIELD_PREP(ANA_SB_TX_LLVL_PROG, 0x7));
1130 	regmap_update_bits(hdptx->regmap, 0x044c,
1131 			   SB_RX_RCAL_OPT_CODE | SB_RX_RTERM_CTRL,
1132 			   FIELD_PREP(SB_RX_RCAL_OPT_CODE, 0x1) |
1133 			   FIELD_PREP(SB_RX_RTERM_CTRL, 0x3));
1134 	regmap_update_bits(hdptx->regmap, 0x0450,
1135 			   SB_TG_SB_EN_DELAY_TIME | SB_TG_RXTERN_EN_DELAY_TIME,
1136 			   FIELD_PREP(SB_TG_SB_EN_DELAY_TIME, 0x2) |
1137 			   FIELD_PREP(SB_TG_RXTERN_EN_DELAY_TIME, 0x2));
1138 	regmap_update_bits(hdptx->regmap, 0x0454,
1139 			   SB_READY_DELAY_TIME | SB_TG_OSC_EN_DELAY_TIME,
1140 			   FIELD_PREP(SB_READY_DELAY_TIME, 0x2) |
1141 			   FIELD_PREP(SB_TG_OSC_EN_DELAY_TIME, 0x2));
1142 	regmap_update_bits(hdptx->regmap, 0x0458,
1143 			   SB_TG_OSC_EN_TO_AFC_RSTN_DELAT_TIME,
1144 			   FIELD_PREP(SB_TG_OSC_EN_TO_AFC_RSTN_DELAT_TIME, 0x2));
1145 	regmap_update_bits(hdptx->regmap, 0x045c,
1146 			   SB_TG_PLL_CD_VREG_FAST_PULSE_TIME,
1147 			   FIELD_PREP(SB_TG_PLL_CD_VREG_FAST_PULSE_TIME, 0x4));
1148 	regmap_update_bits(hdptx->regmap, 0x0460,
1149 			   SB_TG_EARC_DMRX_RECVRD_CLK_CNT,
1150 			   FIELD_PREP(SB_TG_EARC_DMRX_RECVRD_CLK_CNT, 0xa));
1151 	regmap_update_bits(hdptx->regmap, 0x0468, SB_TG_CNT_RUN_NO_7_0,
1152 			   FIELD_PREP(SB_TG_CNT_RUN_NO_7_0, 0x3));
1153 	regmap_update_bits(hdptx->regmap, 0x046c,
1154 			   SB_EARC_SIG_DET_BYPASS | SB_AFC_TOL,
1155 			   FIELD_PREP(SB_EARC_SIG_DET_BYPASS, 0x1) |
1156 			   FIELD_PREP(SB_AFC_TOL, 0x3));
1157 	regmap_update_bits(hdptx->regmap, 0x0470, SB_AFC_STB_NUM,
1158 			   FIELD_PREP(SB_AFC_STB_NUM, 0x4));
1159 	regmap_update_bits(hdptx->regmap, 0x0474, SB_TG_OSC_CNT_MIN,
1160 			   FIELD_PREP(SB_TG_OSC_CNT_MIN, 0x67));
1161 	regmap_update_bits(hdptx->regmap, 0x0478, SB_TG_OSC_CNT_MAX,
1162 			   FIELD_PREP(SB_TG_OSC_CNT_MAX, 0x6a));
1163 	regmap_update_bits(hdptx->regmap, 0x047c, SB_PWM_AFC_CTRL,
1164 			   FIELD_PREP(SB_PWM_AFC_CTRL, 0x5));
1165 	regmap_update_bits(hdptx->regmap, 0x0434, ANA_SB_DMRX_LPBK_DATA,
1166 			   FIELD_PREP(ANA_SB_DMRX_LPBK_DATA, 0x1));
1167 	regmap_update_bits(hdptx->regmap, 0x0440,
1168 			   ANA_SB_VREG_OUT_SEL | ANA_SB_VREG_REF_SEL,
1169 			   FIELD_PREP(ANA_SB_VREG_OUT_SEL, 0x1) |
1170 			   FIELD_PREP(ANA_SB_VREG_REF_SEL, 0x1));
1171 	regmap_update_bits(hdptx->regmap, 0x043c, ANA_SB_VREG_GAIN_CTRL,
1172 			   FIELD_PREP(ANA_SB_VREG_GAIN_CTRL, 0x0));
1173 	regmap_update_bits(hdptx->regmap, 0x0408, ANA_SB_RXTERM_OFFSP,
1174 			   FIELD_PREP(ANA_SB_RXTERM_OFFSP, 0x3));
1175 	regmap_update_bits(hdptx->regmap, 0x040c, ANA_SB_RXTERM_OFFSN,
1176 			   FIELD_PREP(ANA_SB_RXTERM_OFFSN, 0x3));
1177 	regmap_update_bits(hdptx->regmap, 0x047c, SB_RCAL_RSTN,
1178 			   FIELD_PREP(SB_RCAL_RSTN, 0x1));
1179 	regmap_update_bits(hdptx->regmap, 0x0410, SB_AUX_EN,
1180 			   FIELD_PREP(SB_AUX_EN, 0x1));
1181 	regmap_update_bits(hdptx->regmap, 0x0480, SB_AUX_EN_IN,
1182 			   FIELD_PREP(SB_AUX_EN_IN, 0x1));
1183 	regmap_update_bits(hdptx->regmap, 0x040c, OVRD_SB_RX_RESCAL_DONE,
1184 			   FIELD_PREP(OVRD_SB_RX_RESCAL_DONE, 0x1));
1185 	regmap_update_bits(hdptx->regmap, 0x0410, OVRD_SB_EN,
1186 			   FIELD_PREP(OVRD_SB_EN, 0x1));
1187 	regmap_update_bits(hdptx->regmap, 0x0408, OVRD_SB_RXTERM_EN,
1188 			   FIELD_PREP(OVRD_SB_RXTERM_EN, 0x1));
1189 	regmap_update_bits(hdptx->regmap, 0x043c, OVRD_SB_VREG_EN,
1190 			   FIELD_PREP(OVRD_SB_VREG_EN, 0x1));
1191 	regmap_update_bits(hdptx->regmap, 0x0410, OVRD_SB_AUX_EN,
1192 			   FIELD_PREP(OVRD_SB_AUX_EN, 0x1));
1193 
1194 	rockchip_grf_write(hdptx->grf, HDPTXPHY_GRF_CON0, BGR_EN,
1195 			   FIELD_PREP(BGR_EN, 0x1));
1196 	rockchip_grf_write(hdptx->grf, HDPTXPHY_GRF_CON0, BIAS_EN,
1197 			   FIELD_PREP(BIAS_EN, 0x1));
1198 	udelay(10);
1199 	reset_deassert(&hdptx->init_reset);
1200 	udelay(1000);
1201 	reset_deassert(&hdptx->cmn_reset);
1202 	udelay(20);
1203 
1204 	regmap_update_bits(hdptx->regmap, 0x040c, SB_RX_RESCAL_DONE,
1205 			   FIELD_PREP(SB_RX_RESCAL_DONE, 0x1));
1206 	udelay(100);
1207 	regmap_update_bits(hdptx->regmap, 0x0410, SB_EN,
1208 			   FIELD_PREP(SB_EN, 0x1));
1209 	udelay(100);
1210 	regmap_update_bits(hdptx->regmap, 0x0408, SB_RXRERM_EN,
1211 			   FIELD_PREP(SB_RXRERM_EN, 0x1));
1212 	udelay(10);
1213 	regmap_update_bits(hdptx->regmap, 0x043c, SB_VREG_EN,
1214 			   FIELD_PREP(SB_VREG_EN, 0x1));
1215 	udelay(10);
1216 	regmap_update_bits(hdptx->regmap, 0x0410, SB_AUX_EN,
1217 			   FIELD_PREP(SB_AUX_EN, 0x1));
1218 	udelay(100);
1219 
1220 	ret = regmap_read_poll_timeout(hdptx->grf, HDPTXPHY_GRF_STATUS0,
1221 				       status, FIELD_GET(SB_RDY, status),
1222 				       50, 1000);
1223 	if (ret) {
1224 		dev_err(hdptx->dev, "timeout waiting for sb_rdy\n");
1225 		return ret;
1226 	}
1227 
1228 	return 0;
1229 }
1230 
1231 static void rockchip_hdptx_phy_reset(struct rockchip_hdptx_phy *hdptx)
1232 {
1233 	u32 lane;
1234 
1235 	reset_assert(&hdptx->lane_reset);
1236 	reset_assert(&hdptx->cmn_reset);
1237 	reset_assert(&hdptx->init_reset);
1238 
1239 	reset_assert(&hdptx->apb_reset);
1240 	udelay(10);
1241 	reset_deassert(&hdptx->apb_reset);
1242 
1243 	for (lane = 0; lane < 4; lane++)
1244 		regmap_update_bits(hdptx->regmap, LANE_REG(lane, 0x0c04),
1245 				   OVRD_LN_TX_DRV_EI_EN | LN_TX_DRV_EI_EN,
1246 				   FIELD_PREP(OVRD_LN_TX_DRV_EI_EN, 1) |
1247 				   FIELD_PREP(LN_TX_DRV_EI_EN, 0));
1248 
1249 	rockchip_grf_write(hdptx->grf, HDPTXPHY_GRF_CON0, PLL_EN,
1250 			   FIELD_PREP(PLL_EN, 0));
1251 	rockchip_grf_write(hdptx->grf, HDPTXPHY_GRF_CON0, BIAS_EN,
1252 			   FIELD_PREP(BIAS_EN, 0));
1253 	rockchip_grf_write(hdptx->grf, HDPTXPHY_GRF_CON0, BGR_EN,
1254 			   FIELD_PREP(BGR_EN, 0));
1255 }
1256 
1257 static int rockchip_hdptx_phy_power_on(struct phy *phy)
1258 {
1259 	struct rockchip_hdptx_phy *hdptx = dev_get_priv(phy->dev);
1260 	enum phy_mode mode = generic_phy_get_mode(phy);
1261 	u32 lane;
1262 
1263 	rockchip_hdptx_phy_reset(hdptx);
1264 
1265 	for (lane = 0; lane < 4; lane++) {
1266 		u32 invert = hdptx->lane_polarity_invert[lane];
1267 
1268 		regmap_update_bits(hdptx->regmap, LANE_REG(lane, 0x0c78),
1269 				   LN_POLARITY_INV | LN_LANE_MODE,
1270 				   FIELD_PREP(LN_POLARITY_INV, invert) |
1271 				   FIELD_PREP(LN_LANE_MODE, 1));
1272 	}
1273 
1274 	if (mode == PHY_MODE_DP) {
1275 		rockchip_grf_write(hdptx->grf, HDPTXPHY_GRF_CON0,
1276 				   HDPTX_MODE_SEL,
1277 				   FIELD_PREP(HDPTX_MODE_SEL, 0x1));
1278 
1279 		regmap_update_bits(hdptx->regmap, 0x0800, PROTOCOL_SEL,
1280 				   FIELD_PREP(PROTOCOL_SEL, 0x0));
1281 		regmap_update_bits(hdptx->regmap, 0x0818, DATA_BUS_WIDTH,
1282 				   FIELD_PREP(DATA_BUS_WIDTH, 0x1));
1283 		regmap_update_bits(hdptx->regmap, 0x0818, BUS_WIDTH_SEL,
1284 				   FIELD_PREP(BUS_WIDTH_SEL, 0x0));
1285 
1286 		rockchip_hdptx_phy_dp_pll_init(hdptx);
1287 		rockchip_hdptx_phy_dp_aux_init(hdptx);
1288 	} else {
1289 		rockchip_grf_write(hdptx->grf, HDPTXPHY_GRF_CON0,
1290 				   HDPTX_MODE_SEL,
1291 				   FIELD_PREP(HDPTX_MODE_SEL, 0x0));
1292 
1293 		regmap_update_bits(hdptx->regmap, 0x0800, PROTOCOL_SEL,
1294 				   FIELD_PREP(PROTOCOL_SEL, 0x1));
1295 	}
1296 
1297 	return 0;
1298 }
1299 
1300 static int rockchip_hdptx_phy_power_off(struct phy *phy)
1301 {
1302 	struct rockchip_hdptx_phy *hdptx = dev_get_priv(phy->dev);
1303 
1304 	rockchip_hdptx_phy_reset(hdptx);
1305 
1306 	return 0;
1307 }
1308 
1309 static const struct phy_ops rockchip_hdptx_phy_ops = {
1310 	.set_mode	= rockchip_hdptx_phy_set_mode,
1311 	.configure	= rockchip_hdptx_phy_configure,
1312 	.power_on	= rockchip_hdptx_phy_power_on,
1313 	.power_off	= rockchip_hdptx_phy_power_off,
1314 };
1315 
1316 static int rockchip_hdptx_phy_probe(struct udevice *dev)
1317 {
1318 	struct rockchip_hdptx_phy *hdptx = dev_get_priv(dev);
1319 	struct udevice *syscon;
1320 	u32 prop[4];
1321 	int ret;
1322 
1323 	ret = regmap_init_mem(dev, &hdptx->regmap);
1324 	if (ret)
1325 		return ret;
1326 
1327 	ret = uclass_get_device_by_phandle(UCLASS_SYSCON, dev, "rockchip,grf",
1328 					   &syscon);
1329 	if (ret)
1330 		return ret;
1331 
1332 	hdptx->grf = syscon_get_regmap(syscon);
1333 	if (IS_ERR(hdptx->grf)) {
1334 		ret = PTR_ERR(hdptx->grf);
1335 		dev_err(dev, "unable to find regmap: %d\n", ret);
1336 		return ret;
1337 	}
1338 
1339 	hdptx->dev = dev;
1340 
1341 	ret = reset_get_by_name(dev, "apb", &hdptx->apb_reset);
1342 	if (ret < 0) {
1343 		dev_err(dev, "failed to get apb reset: %d\n", ret);
1344 		return ret;
1345 	}
1346 
1347 	ret = reset_get_by_name(dev, "init", &hdptx->init_reset);
1348 	if (ret < 0) {
1349 		dev_err(dev, "failed to get init reset: %d\n", ret);
1350 		return ret;
1351 	}
1352 
1353 	ret = reset_get_by_name(dev, "cmn", &hdptx->cmn_reset);
1354 	if (ret < 0) {
1355 		dev_err(dev, "failed to get cmn reset: %d\n", ret);
1356 		return ret;
1357 	}
1358 
1359 	ret = reset_get_by_name(dev, "lane", &hdptx->lane_reset);
1360 	if (ret < 0) {
1361 		dev_err(dev, "failed to get lane reset: %d\n", ret);
1362 		return ret;
1363 	}
1364 
1365 	ret = rockchip_hdptx_phy_parse_training_table(dev);
1366 	if (ret) {
1367 		dev_err(dev, "failed to parse training table: %d\n", ret);
1368 		return ret;
1369 	}
1370 
1371 	if (!dev_read_u32_array(dev, "lane-polarity-invert", prop, ARRAY_SIZE(prop))) {
1372 		hdptx->lane_polarity_invert[0] = prop[0];
1373 		hdptx->lane_polarity_invert[1] = prop[1];
1374 		hdptx->lane_polarity_invert[2] = prop[2];
1375 		hdptx->lane_polarity_invert[3] = prop[3];
1376 	}
1377 
1378 	return 0;
1379 }
1380 
1381 static const struct udevice_id rockchip_hdptx_phy_ids[] = {
1382 	{ .compatible = "rockchip,rk3588-hdptx-phy", },
1383 	{}
1384 };
1385 
1386 U_BOOT_DRIVER(rockchip_hdptx_phy) = {
1387 	.name		= "rockchip_hdptx_phy",
1388 	.id		= UCLASS_PHY,
1389 	.ops		= &rockchip_hdptx_phy_ops,
1390 	.of_match	= rockchip_hdptx_phy_ids,
1391 	.probe		= rockchip_hdptx_phy_probe,
1392 	.priv_auto_alloc_size = sizeof(struct rockchip_hdptx_phy),
1393 };
1394