xref: /linux/drivers/phy/rockchip/phy-rockchip-samsung-hdptx.c (revision 16e6a1a3cb3fa5fa11cd76a9d71235d927923329)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2021-2022 Rockchip Electronics Co., Ltd.
4  * Copyright (c) 2024-2026 Collabora Ltd.
5  *
6  * Author: Algea Cao <algea.cao@rock-chips.com>
7  * Author: Cristian Ciocaltea <cristian.ciocaltea@collabora.com>
8  */
9 #include <linux/bitfield.h>
10 #include <linux/clk.h>
11 #include <linux/clk-provider.h>
12 #include <linux/delay.h>
13 #include <linux/hw_bitfield.h>
14 #include <linux/mfd/syscon.h>
15 #include <linux/module.h>
16 #include <linux/of.h>
17 #include <linux/of_platform.h>
18 #include <linux/phy/phy.h>
19 #include <linux/platform_device.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/rational.h>
22 #include <linux/regmap.h>
23 #include <linux/reset.h>
24 
25 #define GRF_HDPTX_CON0			0x00
26 #define LC_REF_CLK_SEL			BIT(11)
27 #define HDPTX_I_PLL_EN			BIT(7)
28 #define HDPTX_I_BIAS_EN			BIT(6)
29 #define HDPTX_I_BGR_EN			BIT(5)
30 #define HDPTX_MODE_SEL			BIT(0)
31 #define GRF_HDPTX_STATUS		0x80
32 #define HDPTX_O_PLL_LOCK_DONE		BIT(3)
33 #define HDPTX_O_PHY_CLK_RDY		BIT(2)
34 #define HDPTX_O_PHY_RDY			BIT(1)
35 #define HDPTX_O_SB_RDY			BIT(0)
36 
37 #define HDPTX_REG(_n, _min, _max)				\
38 	(							\
39 		BUILD_BUG_ON_ZERO((0x##_n) < (0x##_min)) +	\
40 		BUILD_BUG_ON_ZERO((0x##_n) > (0x##_max)) +	\
41 		((0x##_n) * 4)					\
42 	)
43 
44 #define CMN_REG(n)			HDPTX_REG(n, 0000, 00a7)
45 #define SB_REG(n)			HDPTX_REG(n, 0100, 0129)
46 #define LNTOP_REG(n)			HDPTX_REG(n, 0200, 0229)
47 #define LANE_REG(n)			HDPTX_REG(n, 0300, 062d)
48 
49 /* CMN_REG(0008) */
50 #define OVRD_LCPLL_EN_MASK		BIT(7)
51 #define LCPLL_EN_MASK			BIT(6)
52 #define LCPLL_LCVCO_MODE_EN_MASK	BIT(4)
53 /* CMN_REG(001e) */
54 #define LCPLL_PI_EN_MASK		BIT(5)
55 #define LCPLL_100M_CLK_EN_MASK		BIT(0)
56 /* CMN_REG(0022) */
57 #define ANA_LCPLL_PMS_PDIV_MASK		GENMASK(7, 4)
58 #define ANA_LCPLL_PMS_REFDIV_MASK	GENMASK(3, 0)
59 /* CMN_REG(0023) */
60 #define LCPLL_PMS_SDIV_RBR_MASK		GENMASK(7, 4)
61 #define LCPLL_PMS_SDIV_HBR_MASK		GENMASK(3, 0)
62 /* CMN_REG(0025) */
63 #define LCPLL_PMS_IQDIV_RSTN_MASK	BIT(4)
64 /* CMN_REG(0028) */
65 #define LCPLL_SDC_FRAC_EN_MASK		BIT(2)
66 #define LCPLL_SDC_FRAC_RSTN_MASK	BIT(0)
67 /* CMN_REG(002d) */
68 #define LCPLL_SDC_N_MASK		GENMASK(3, 1)
69 /* CMN_REG(002e) */
70 #define LCPLL_SDC_NUMBERATOR_MASK	GENMASK(5, 0)
71 /* CMN_REG(002f) */
72 #define LCPLL_SDC_DENOMINATOR_MASK	GENMASK(7, 2)
73 #define LCPLL_SDC_NDIV_RSTN_MASK	BIT(0)
74 /* CMN_REG(003c) */
75 #define ANA_LCPLL_RESERVED7_MASK	BIT(7)
76 /* CMN_REG(003d) */
77 #define OVRD_ROPLL_EN_MASK		BIT(7)
78 #define ROPLL_EN_MASK			BIT(6)
79 #define ROPLL_LCVCO_EN_MASK		BIT(4)
80 /* CMN_REG(0046) */
81 #define ROPLL_ANA_CPP_CTRL_COARSE_MASK	GENMASK(7, 4)
82 #define ROPLL_ANA_CPP_CTRL_FINE_MASK	GENMASK(3, 0)
83 /* CMN_REG(0047) */
84 #define ROPLL_ANA_LPF_C_SEL_COARSE_MASK	GENMASK(5, 3)
85 #define ROPLL_ANA_LPF_C_SEL_FINE_MASK	GENMASK(2, 0)
86 /* CMN_REG(004e) */
87 #define ROPLL_PI_EN_MASK		BIT(5)
88 /* CMN_REG(0051) */
89 #define ROPLL_PMS_MDIV_MASK		GENMASK(7, 0)
90 /* CMN_REG(0055) */
91 #define ROPLL_PMS_MDIV_AFC_MASK		GENMASK(7, 0)
92 /* CMN_REG(0059) */
93 #define ANA_ROPLL_PMS_PDIV_MASK		GENMASK(7, 4)
94 #define ANA_ROPLL_PMS_REFDIV_MASK	GENMASK(3, 0)
95 /* CMN_REG(005a) */
96 #define ROPLL_PMS_SDIV_RBR_MASK		GENMASK(7, 4)
97 #define ROPLL_PMS_SDIV_HBR_MASK		GENMASK(3, 0)
98 /* CMN_REG(005b) */
99 #define ROPLL_PMS_SDIV_HBR2_MASK	GENMASK(7, 4)
100 /* CMN_REG(005c) */
101 #define ROPLL_PMS_IQDIV_RSTN_MASK	BIT(5)
102 /* CMN_REG(005e) */
103 #define ROPLL_SDM_EN_MASK		BIT(6)
104 #define OVRD_ROPLL_SDM_RSTN_MASK	BIT(5)
105 #define ROPLL_SDM_RSTN_MASK		BIT(4)
106 #define ROPLL_SDC_FRAC_EN_RBR_MASK	BIT(3)
107 #define ROPLL_SDC_FRAC_EN_HBR_MASK	BIT(2)
108 #define ROPLL_SDC_FRAC_EN_HBR2_MASK	BIT(1)
109 #define ROPLL_SDM_FRAC_EN_HBR3_MASK	BIT(0)
110 /* CMN_REG(005f) */
111 #define OVRD_ROPLL_SDC_RSTN_MASK	BIT(5)
112 #define ROPLL_SDC_RSTN_MASK		BIT(4)
113 /* CMN_REG(0060)  */
114 #define ROPLL_SDM_DENOMINATOR_MASK	GENMASK(7, 0)
115 /* CMN_REG(0064) */
116 #define ROPLL_SDM_NUM_SIGN_RBR_MASK	BIT(3)
117 #define ROPLL_SDM_NUM_SIGN_HBR_MASK	BIT(2)
118 #define ROPLL_SDM_NUM_SIGN_HBR2_MASK	BIT(1)
119 /* CMN_REG(0065) */
120 #define ROPLL_SDM_NUM_MASK		GENMASK(7, 0)
121 /* CMN_REG(0069) */
122 #define ROPLL_SDC_N_RBR_MASK		GENMASK(2, 0)
123 /* CMN_REG(006a) */
124 #define ROPLL_SDC_N_HBR_MASK		GENMASK(5, 3)
125 #define ROPLL_SDC_N_HBR2_MASK		GENMASK(2, 0)
126 /* CMN_REG(006b) */
127 #define ROPLL_SDC_N_HBR3_MASK		GENMASK(3, 1)
128 /* CMN_REG(006c) */
129 #define ROPLL_SDC_NUM_MASK		GENMASK(5, 0)
130 /* cmn_reg0070 */
131 #define ROPLL_SDC_DENO_MASK		GENMASK(5, 0)
132 /* CMN_REG(0074) */
133 #define OVRD_ROPLL_SDC_NDIV_RSTN_MASK	BIT(3)
134 #define ROPLL_SDC_NDIV_RSTN_MASK	BIT(2)
135 #define OVRD_ROPLL_SSC_EN_MASK		BIT(1)
136 #define ROPLL_SSC_EN_MASK		BIT(0)
137 /* CMN_REG(0075) */
138 #define ANA_ROPLL_SSC_FM_DEVIATION_MASK	GENMASK(5, 0)
139 /* CMN_REG(0076) */
140 #define ANA_ROPLL_SSC_FM_FREQ_MASK	GENMASK(6, 2)
141 /* CMN_REG(0077) */
142 #define ANA_ROPLL_SSC_CLK_DIV_SEL_MASK	GENMASK(6, 3)
143 /* CMN_REG(0081) */
144 #define OVRD_PLL_CD_CLK_EN_MASK		BIT(8)
145 #define ANA_PLL_CD_TX_SER_RATE_SEL_MASK	BIT(3)
146 #define ANA_PLL_CD_HSCLK_WEST_EN_MASK	BIT(1)
147 #define ANA_PLL_CD_HSCLK_EAST_EN_MASK	BIT(0)
148 /* CMN_REG(0082) */
149 #define ANA_PLL_CD_VREG_GAIN_CTRL_MASK	GENMASK(3, 0)
150 /* CMN_REG(0083) */
151 #define ANA_PLL_CD_VREG_ICTRL_MASK	GENMASK(6, 5)
152 /* CMN_REG(0084) */
153 #define PLL_LCRO_CLK_SEL_MASK		BIT(5)
154 /* CMN_REG(0085) */
155 #define ANA_PLL_SYNC_LOSS_DET_MODE_MASK	GENMASK(1, 0)
156 /* CMN_REG(0086) */
157 #define PLL_PCG_POSTDIV_SEL_MASK	GENMASK(7, 4)
158 #define PLL_PCG_CLK_SEL_MASK		GENMASK(3, 1)
159 #define PLL_PCG_CLK_EN_MASK		BIT(0)
160 /* CMN_REG(0087) */
161 #define ANA_PLL_FRL_MODE_EN_MASK	BIT(3)
162 #define ANA_PLL_TX_HS_CLK_EN_MASK	BIT(2)
163 /* CMN_REG(0089) */
164 #define LCPLL_ALONE_MODE_MASK		BIT(1)
165 /* CMN_REG(0095) */
166 #define DP_TX_LINK_BW_MASK		GENMASK(1, 0)
167 /* CMN_REG(0097) */
168 #define DIG_CLK_SEL_MASK		BIT(1)
169 #define LCPLL_REF			BIT(1)
170 #define ROPLL_REF			0
171 /* CMN_REG(0099) */
172 #define SSC_EN_MASK			GENMASK(7, 6)
173 #define CMN_ROPLL_ALONE_MODE_MASK	BIT(2)
174 #define ROPLL_ALONE_MODE		BIT(2)
175 /* CMN_REG(009a) */
176 #define HS_SPEED_SEL_MASK		BIT(0)
177 #define DIV_10_CLOCK			BIT(0)
178 /* CMN_REG(009b) */
179 #define LS_SPEED_SEL_MASK		BIT(4)
180 #define LINK_SYMBOL_CLOCK		BIT(4)
181 #define LINK_SYMBOL_CLOCK1_2		0
182 
183 /* SB_REG(0102) */
184 #define OVRD_SB_RXTERM_EN_MASK		BIT(5)
185 #define SB_RXTERM_EN_MASK		BIT(4)
186 #define ANA_SB_RXTERM_OFFSP_MASK	GENMASK(3, 0)
187 /* SB_REG(0103) */
188 #define ANA_SB_RXTERM_OFFSN_MASK	GENMASK(6, 3)
189 #define OVRD_SB_RX_RESCAL_DONE_MASK	BIT(1)
190 #define SB_RX_RESCAL_DONE_MASK		BIT(0)
191 /* SB_REG(0104) */
192 #define OVRD_SB_EN_MASK			BIT(5)
193 #define SB_EN_MASK			BIT(4)
194 #define OVRD_SB_AUX_EN_MASK		BIT(1)
195 #define SB_AUX_EN_MASK			BIT(0)
196 /* SB_REG(0105) */
197 #define OVRD_SB_EARC_CMDC_EN_MASK	BIT(6)
198 #define SB_EARC_CMDC_EN_MASK		BIT(5)
199 #define ANA_SB_TX_HLVL_PROG_MASK	GENMASK(2, 0)
200 /* SB_REG(0106) */
201 #define ANA_SB_TX_LLVL_PROG_MASK	GENMASK(6, 4)
202 /* SB_REG(0109) */
203 #define ANA_SB_DMRX_AFC_DIV_RATIO_MASK	GENMASK(2, 0)
204 /* SB_REG(010d) */
205 #define ANA_SB_DMRX_LPBK_DATA_MASK	BIT(4)
206 /* SB_REG(010f) */
207 #define OVRD_SB_VREG_EN_MASK		BIT(7)
208 #define SB_VREG_EN_MASK			BIT(6)
209 #define OVRD_SB_VREG_LPF_BYPASS_MASK	BIT(5)
210 #define SB_VREG_LPF_BYPASS_MASK		BIT(4)
211 #define ANA_SB_VREG_GAIN_CTRL_MASK	GENMASK(3, 0)
212 /* SB_REG(0110) */
213 #define ANA_SB_VREG_OUT_SEL_MASK	BIT(1)
214 #define ANA_SB_VREG_REF_SEL_MASK	BIT(0)
215 /* SB_REG(0113) */
216 #define SB_RX_RCAL_OPT_CODE_MASK	GENMASK(5, 4)
217 #define SB_RX_RTERM_CTRL_MASK		GENMASK(3, 0)
218 /* SB_REG(0114) */
219 #define SB_TG_SB_EN_DELAY_TIME_MASK	GENMASK(5, 3)
220 #define SB_TG_RXTERM_EN_DELAY_TIME_MASK	GENMASK(2, 0)
221 /* SB_REG(0115) */
222 #define SB_READY_DELAY_TIME_MASK	GENMASK(5, 3)
223 #define SB_TG_OSC_EN_DELAY_TIME_MASK	GENMASK(2, 0)
224 /* SB_REG(0116) */
225 #define AFC_RSTN_DELAY_TIME_MASK	GENMASK(6, 4)
226 /* SB_REG(0117) */
227 #define FAST_PULSE_TIME_MASK		GENMASK(3, 0)
228 /* SB_REG(0118) */
229 #define SB_TG_EARC_DMRX_RECVRD_CLK_CNT_MASK	GENMASK(7, 0)
230 /* SB_REG(011a) */
231 #define SB_TG_CNT_RUN_NO_7_0_MASK	GENMASK(7, 0)
232 /* SB_REG(011b) */
233 #define SB_EARC_SIG_DET_BYPASS_MASK	BIT(4)
234 #define SB_AFC_TOL_MASK			GENMASK(3, 0)
235 /* SB_REG(011c) */
236 #define SB_AFC_STB_NUM_MASK		GENMASK(3, 0)
237 /* SB_REG(011d) */
238 #define SB_TG_OSC_CNT_MIN_MASK		GENMASK(7, 0)
239 /* SB_REG(011e) */
240 #define SB_TG_OSC_CNT_MAX_MASK		GENMASK(7, 0)
241 /* SB_REG(011f) */
242 #define SB_PWM_AFC_CTRL_MASK		GENMASK(7, 2)
243 #define SB_RCAL_RSTN_MASK		BIT(1)
244 /* SB_REG(0120) */
245 #define SB_AUX_EN_IN_MASK		BIT(7)
246 #define SB_EARC_EN_MASK			BIT(1)
247 #define SB_EARC_AFC_EN_MASK		BIT(2)
248 /* SB_REG(0123) */
249 #define OVRD_SB_READY_MASK		BIT(5)
250 #define SB_READY_MASK			BIT(4)
251 
252 /* LNTOP_REG(0200) */
253 #define PROTOCOL_SEL_MASK		BIT(2)
254 #define HDMI_MODE			BIT(2)
255 #define HDMI_TMDS_FRL_SEL		BIT(1)
256 /* LNTOP_REG(0206) */
257 #define DATA_BUS_WIDTH_MASK		GENMASK(2, 1)
258 #define DATA_BUS_WIDTH_SEL_MASK		BIT(0)
259 #define DATA_BUS_36_40			BIT(0)
260 /* LNTOP_REG(0207) */
261 #define LANE_EN_MASK			0xf
262 #define ALL_LANE_EN			0xf
263 
264 /* LANE_REG(0301) */
265 #define OVRD_LN_TX_DRV_EI_EN_MASK	BIT(7)
266 #define LN_TX_DRV_EI_EN_MASK		BIT(6)
267 /* LANE_REG(0303) */
268 #define OVRD_LN_TX_DRV_LVL_CTRL_MASK	BIT(5)
269 #define LN_TX_DRV_LVL_CTRL_MASK		GENMASK(4, 0)
270 /* LANE_REG(0304)  */
271 #define OVRD_LN_TX_DRV_POST_LVL_CTRL_MASK	BIT(4)
272 #define LN_TX_DRV_POST_LVL_CTRL_MASK	GENMASK(3, 0)
273 /* LANE_REG(0305) */
274 #define OVRD_LN_TX_DRV_PRE_LVL_CTRL_MASK	BIT(6)
275 #define LN_TX_DRV_PRE_LVL_CTRL_MASK	GENMASK(5, 2)
276 /* LANE_REG(0306) */
277 #define LN_ANA_TX_DRV_IDRV_IDN_CTRL_MASK	GENMASK(7, 5)
278 #define LN_ANA_TX_DRV_IDRV_IUP_CTRL_MASK	GENMASK(4, 2)
279 #define LN_ANA_TX_DRV_ACCDRV_EN_MASK	BIT(0)
280 /* LANE_REG(0307) */
281 #define LN_ANA_TX_DRV_ACCDRV_POL_SEL_MASK	BIT(6)
282 #define LN_ANA_TX_DRV_ACCDRV_CTRL_MASK	GENMASK(5, 3)
283 /* LANE_REG(030a) */
284 #define LN_ANA_TX_JEQ_EN_MASK		BIT(4)
285 #define LN_TX_JEQ_EVEN_CTRL_RBR_MASK	GENMASK(3, 0)
286 /* LANE_REG(030b) */
287 #define LN_TX_JEQ_EVEN_CTRL_HBR_MASK	GENMASK(7, 4)
288 #define LN_TX_JEQ_EVEN_CTRL_HBR2_MASK	GENMASK(3, 0)
289 /* LANE_REG(030c) */
290 #define LN_TX_JEQ_ODD_CTRL_RBR_MASK	GENMASK(3, 0)
291 /* LANE_REG(030d) */
292 #define LN_TX_JEQ_ODD_CTRL_HBR_MASK	GENMASK(7, 4)
293 #define LN_TX_JEQ_ODD_CTRL_HBR2_MASK	GENMASK(3, 0)
294 /* LANE_REG(0310) */
295 #define LN_ANA_TX_SYNC_LOSS_DET_MODE_MASK	GENMASK(1, 0)
296 /* LANE_REG(0311) */
297 #define LN_TX_SER_40BIT_EN_RBR_MASK	BIT(3)
298 #define LN_TX_SER_40BIT_EN_HBR_MASK	BIT(2)
299 #define LN_TX_SER_40BIT_EN_HBR2_MASK	BIT(1)
300 /* LANE_REG(0312) */
301 #define LN0_TX_SER_RATE_SEL_RBR_MASK	BIT(5)
302 #define LN0_TX_SER_RATE_SEL_HBR_MASK	BIT(4)
303 #define LN0_TX_SER_RATE_SEL_HBR2_MASK	BIT(3)
304 #define LN0_TX_SER_RATE_SEL_HBR3_MASK	BIT(2)
305 /* LANE_REG(0316) */
306 #define LN_ANA_TX_SER_VREG_GAIN_CTRL_MASK	GENMASK(3, 0)
307 /* LANE_REG(031B) */
308 #define LN_ANA_TX_RESERVED_MASK		GENMASK(7, 0)
309 /* LANE_REG(031e) */
310 #define LN_POLARITY_INV_MASK		BIT(2)
311 #define LN_LANE_MODE_MASK		BIT(1)
312 
313 /* LANE_REG(0412) */
314 #define LN1_TX_SER_RATE_SEL_RBR_MASK	BIT(5)
315 #define LN1_TX_SER_RATE_SEL_HBR_MASK	BIT(4)
316 #define LN1_TX_SER_RATE_SEL_HBR2_MASK	BIT(3)
317 #define LN1_TX_SER_RATE_SEL_HBR3_MASK	BIT(2)
318 
319 /* LANE_REG(0512) */
320 #define LN2_TX_SER_RATE_SEL_RBR_MASK	BIT(5)
321 #define LN2_TX_SER_RATE_SEL_HBR_MASK	BIT(4)
322 #define LN2_TX_SER_RATE_SEL_HBR2_MASK	BIT(3)
323 #define LN2_TX_SER_RATE_SEL_HBR3_MASK	BIT(2)
324 
325 /* LANE_REG(0612) */
326 #define LN3_TX_SER_RATE_SEL_RBR_MASK	BIT(5)
327 #define LN3_TX_SER_RATE_SEL_HBR_MASK	BIT(4)
328 #define LN3_TX_SER_RATE_SEL_HBR2_MASK	BIT(3)
329 #define LN3_TX_SER_RATE_SEL_HBR3_MASK	BIT(2)
330 
331 #define HDMI14_MAX_RATE			340000000
332 #define HDMI20_MAX_RATE			600000000
333 #define FRL_3G3L_RATE			900000000
334 #define FRL_6G3L_RATE			1800000000
335 #define FRL_8G4L_RATE			3200000000
336 
337 enum dp_link_rate {
338 	DP_BW_RBR,
339 	DP_BW_HBR,
340 	DP_BW_HBR2,
341 };
342 
343 struct lcpll_config {
344 	unsigned long long rate;
345 	u8 lcvco_mode_en;
346 	u8 pi_en;
347 	u8 clk_en_100m;
348 	u8 pms_mdiv;
349 	u8 pms_mdiv_afc;
350 	u8 pms_pdiv;
351 	u8 pms_refdiv;
352 	u8 pms_sdiv;
353 	u8 sdm_deno;
354 	u8 sdm_num_sign;
355 	u8 sdm_num;
356 	u8 sdc_n;
357 };
358 
359 struct ropll_config {
360 	unsigned long long rate;
361 	u8 pms_mdiv;
362 	u8 pms_mdiv_afc;
363 	u8 pms_pdiv;
364 	u8 pms_refdiv;
365 	u8 pms_sdiv;
366 	u8 sdm_en;
367 	u8 sdm_deno;
368 	u8 sdm_num_sign;
369 	u8 sdm_num;
370 	u8 sdc_n;
371 	u8 sdc_num;
372 	u8 sdc_deno;
373 };
374 
375 struct tx_drv_ctrl {
376 	u8 tx_drv_lvl_ctrl;
377 	u8 tx_drv_post_lvl_ctrl;
378 	u8 ana_tx_drv_idrv_idn_ctrl;
379 	u8 ana_tx_drv_idrv_iup_ctrl;
380 	u8 ana_tx_drv_accdrv_en;
381 	u8 ana_tx_drv_accdrv_ctrl;
382 	u8 tx_drv_pre_lvl_ctrl;
383 	u8 ana_tx_jeq_en;
384 	u8 tx_jeq_even_ctrl;
385 	u8 tx_jeq_odd_ctrl;
386 };
387 
388 enum rk_hdptx_reset {
389 	RST_APB = 0,
390 	RST_INIT,
391 	RST_CMN,
392 	RST_LANE,
393 	RST_MAX
394 };
395 
396 #define MAX_HDPTX_PHY_NUM	2
397 
398 struct rk_hdptx_phy_cfg {
399 	unsigned int num_phys;
400 	unsigned int phy_ids[MAX_HDPTX_PHY_NUM];
401 };
402 
403 struct rk_hdptx_hdmi_cfg {
404 	enum phy_hdmi_mode mode;
405 	unsigned long long rate;
406 	unsigned int bpc;
407 };
408 
409 struct rk_hdptx_phy {
410 	struct device *dev;
411 	struct regmap *regmap;
412 	struct regmap *grf;
413 
414 	int phy_id;
415 	struct phy *phy;
416 	struct rk_hdptx_hdmi_cfg hdmi_cfg;
417 	struct clk_bulk_data *clks;
418 	int nr_clks;
419 	struct reset_control_bulk_data rsts[RST_MAX];
420 
421 	/* clk provider */
422 	struct clk_hw hw;
423 	bool pll_config_dirty;
424 
425 	atomic_t usage_count;
426 
427 	/* used for dp mode */
428 	unsigned int link_rate;
429 	unsigned int lanes;
430 };
431 
432 static const struct lcpll_config rk_hdptx_frl_lcpll_cfg[] = {
433 	/*                                |              pms             |      sdm       |      */
434 	/*         rate, lcen, pien, cken, mdiv, mdafc, pdiv, rdiv, sdiv, deno, nsig, num, sdcn, */
435 	{ 4800000000ULL,    1,    0,    0,  125,   125,    1,    1,    0,    1,    0,   0,    2, },
436 	{ 4000000000ULL,    1,    1,    0,  104,   104,    1,    1,    0,    9,    0,   1,    1, },
437 	{ 2400000000ULL,    1,    0,    0,  125,   125,    1,    1,    1,    1,    0,   0,    2, },
438 	{ 1800000000ULL,    1,    0,    0,  125,   125,    1,    1,    1,    1,    0,   0,    2, },
439 	{  900000000ULL,    1,    0,    0,  125,   125,    1,    1,    3,    1,    0,   0,    2, },
440 };
441 
442 static const struct ropll_config rk_hdptx_tmds_ropll_cfg[] = {
443 	/*             |              pms             |        sdm         |     sdc     | */
444 	/*        rate, mdiv, mdafc, pdiv, rdiv, sdiv, en, deno, nsig, num,  n, num, deno, */
445 	{ 594000000ULL,  124,   124,    1,    1,    0,  1,   62,    1,  16,  5,   0,    1, },
446 	{ 461101250ULL,   97,    97,    1,    1,    0,  1,   71,    1,  53,  2,   6,   35, },
447 	{ 371250000ULL,  155,   155,    1,    1,    1,  1,   62,    1,  16,  5,   0,    1, },
448 	{ 297000000ULL,  124,   124,    1,    1,    1,  1,   62,    1,  16,  5,   0,    1, },
449 	{ 185625000ULL,  155,   155,    1,    1,    3,  1,   62,    1,  16,  5,   0,    1, },
450 	{ 162000000ULL,  135,   135,    1,    1,    3,  0,    4,    0,   3,  5,   5,   16, },
451 	{ 154000000ULL,  193,   193,    1,    1,    5,  1,  193,    1,  32,  2,   1,    1, },
452 	{ 148500000ULL,  123,   123,    1,    1,    3,  1,    4,    0,   3,  5,   5,   16, },
453 	{ 146250000ULL,  122,   122,    1,    1,    3,  1,  244,    1,  16,  2,   1,    1, },
454 	{ 119000000ULL,  149,   149,    1,    1,    5,  1,  149,    1,  16,  2,   1,    1, },
455 	{ 108000000ULL,  135,   135,    1,    1,    5,  0,    9,    0,   5,  0,  20,   24, },
456 	{ 106500000ULL,   89,    89,    1,    1,    3,  1,   89,    1,  16,  1,   0,    1, },
457 	{  92812500ULL,  155,   155,    1,    1,    7,  1,   62,    1,  16,  5,   0,    1, },
458 	{  85500000ULL,  214,   214,    1,    1,   11,  1,  214,    1,  16,  2,   1,    1, },
459 	{  83500000ULL,  105,   105,    1,    1,    5,  1,   42,    1,  16,  1,   0,    1, },
460 	{  74250000ULL,  124,   124,    1,    1,    7,  1,   62,    1,  16,  5,   0,    1, },
461 	{  65000000ULL,  162,   162,    1,    1,   11,  1,   54,    0,  16,  4,   1,    1, },
462 	{  50250000ULL,   84,    84,    1,    1,    7,  1,   11,    1,   4,  5,   4,   11, },
463 	{  40000000ULL,  100,   100,    1,    1,   11,  0,    9,    0,   5,  0,  20,   24, },
464 	{  33750000ULL,  112,   112,    1,    1,   15,  1,    2,    0,   1,  5,   1,    1, },
465 	{  27000000ULL,   90,    90,    1,    1,   15,  0,    9,    0,   5,  0,  20,   24, },
466 	{  25175000ULL,   84,    84,    1,    1,   15,  1,  168,    1,  16,  4,   1,    1, },
467 };
468 
469 static const struct reg_sequence rk_hdptx_common_cmn_init_seq[] = {
470 	REG_SEQ0(CMN_REG(0009), 0x0c),
471 	REG_SEQ0(CMN_REG(000a), 0x83),
472 	REG_SEQ0(CMN_REG(000b), 0x06),
473 	REG_SEQ0(CMN_REG(000c), 0x20),
474 	REG_SEQ0(CMN_REG(000d), 0xb8),
475 	REG_SEQ0(CMN_REG(000e), 0x0f),
476 	REG_SEQ0(CMN_REG(000f), 0x0f),
477 	REG_SEQ0(CMN_REG(0010), 0x04),
478 	REG_SEQ0(CMN_REG(0012), 0x26),
479 	REG_SEQ0(CMN_REG(0013), 0x22),
480 	REG_SEQ0(CMN_REG(0014), 0x24),
481 	REG_SEQ0(CMN_REG(0015), 0x77),
482 	REG_SEQ0(CMN_REG(0016), 0x08),
483 	REG_SEQ0(CMN_REG(0018), 0x04),
484 	REG_SEQ0(CMN_REG(0019), 0x48),
485 	REG_SEQ0(CMN_REG(001a), 0x01),
486 	REG_SEQ0(CMN_REG(001b), 0x00),
487 	REG_SEQ0(CMN_REG(001c), 0x01),
488 	REG_SEQ0(CMN_REG(001d), 0x64),
489 	REG_SEQ0(CMN_REG(001f), 0x00),
490 	REG_SEQ0(CMN_REG(0029), 0x01),
491 	REG_SEQ0(CMN_REG(0035), 0x00),
492 	REG_SEQ0(CMN_REG(0038), 0x00),
493 	REG_SEQ0(CMN_REG(0039), 0x00),
494 	REG_SEQ0(CMN_REG(003a), 0x00),
495 	REG_SEQ0(CMN_REG(003b), 0x00),
496 	REG_SEQ0(CMN_REG(003c), 0x80),
497 	REG_SEQ0(CMN_REG(003e), 0x0c),
498 	REG_SEQ0(CMN_REG(003f), 0x83),
499 	REG_SEQ0(CMN_REG(0040), 0x06),
500 	REG_SEQ0(CMN_REG(0041), 0x20),
501 	REG_SEQ0(CMN_REG(0043), 0x00),
502 	REG_SEQ0(CMN_REG(0044), 0x46),
503 	REG_SEQ0(CMN_REG(0045), 0x24),
504 	REG_SEQ0(CMN_REG(0047), 0x00),
505 	REG_SEQ0(CMN_REG(0049), 0xfa),
506 	REG_SEQ0(CMN_REG(004a), 0x08),
507 	REG_SEQ0(CMN_REG(004b), 0x00),
508 	REG_SEQ0(CMN_REG(004c), 0x01),
509 	REG_SEQ0(CMN_REG(004d), 0x64),
510 	REG_SEQ0(CMN_REG(004f), 0x00),
511 	REG_SEQ0(CMN_REG(0050), 0x00),
512 	REG_SEQ0(CMN_REG(005f), 0x01),
513 	REG_SEQ0(CMN_REG(0075), 0x20),
514 	REG_SEQ0(CMN_REG(0076), 0x30),
515 	REG_SEQ0(CMN_REG(0077), 0x08),
516 	REG_SEQ0(CMN_REG(0078), 0x0c),
517 	REG_SEQ0(CMN_REG(0079), 0x00),
518 	REG_SEQ0(CMN_REG(007b), 0x00),
519 	REG_SEQ0(CMN_REG(007c), 0x00),
520 	REG_SEQ0(CMN_REG(007d), 0x00),
521 	REG_SEQ0(CMN_REG(007e), 0x00),
522 	REG_SEQ0(CMN_REG(007f), 0x00),
523 	REG_SEQ0(CMN_REG(0080), 0x00),
524 	REG_SEQ0(CMN_REG(0082), 0x04),
525 	REG_SEQ0(CMN_REG(0083), 0x24),
526 	REG_SEQ0(CMN_REG(0084), 0x20),
527 	REG_SEQ0(CMN_REG(0085), 0x03),
528 	REG_SEQ0(CMN_REG(008a), 0x55),
529 	REG_SEQ0(CMN_REG(008b), 0x25),
530 	REG_SEQ0(CMN_REG(008c), 0x2c),
531 	REG_SEQ0(CMN_REG(008d), 0x22),
532 	REG_SEQ0(CMN_REG(008e), 0x14),
533 	REG_SEQ0(CMN_REG(008f), 0x20),
534 	REG_SEQ0(CMN_REG(0090), 0x00),
535 	REG_SEQ0(CMN_REG(0091), 0x00),
536 	REG_SEQ0(CMN_REG(0092), 0x00),
537 	REG_SEQ0(CMN_REG(0093), 0x00),
538 	REG_SEQ0(CMN_REG(009a), 0x11),
539 };
540 
541 static const struct reg_sequence rk_hdptx_frl_lcpll_cmn_init_seq[] = {
542 	REG_SEQ0(CMN_REG(0011), 0x00),
543 	REG_SEQ0(CMN_REG(0017), 0x00),
544 	REG_SEQ0(CMN_REG(0025), 0x10),
545 	REG_SEQ0(CMN_REG(0026), 0x53),
546 	REG_SEQ0(CMN_REG(0027), 0x01),
547 	REG_SEQ0(CMN_REG(0028), 0x0d),
548 	REG_SEQ0(CMN_REG(002e), 0x02),
549 	REG_SEQ0(CMN_REG(002f), 0x0d),
550 	REG_SEQ0(CMN_REG(0030), 0x00),
551 	REG_SEQ0(CMN_REG(0031), 0x20),
552 	REG_SEQ0(CMN_REG(0032), 0x30),
553 	REG_SEQ0(CMN_REG(0033), 0x0b),
554 	REG_SEQ0(CMN_REG(0034), 0x23),
555 	REG_SEQ0(CMN_REG(003d), 0x00),
556 	REG_SEQ0(CMN_REG(0042), 0xb8),
557 	REG_SEQ0(CMN_REG(0046), 0xff),
558 	REG_SEQ0(CMN_REG(0048), 0x44),
559 	REG_SEQ0(CMN_REG(004e), 0x14),
560 	REG_SEQ0(CMN_REG(0051), 0x00),
561 	REG_SEQ0(CMN_REG(0055), 0x00),
562 	REG_SEQ0(CMN_REG(0059), 0x11),
563 	REG_SEQ0(CMN_REG(005a), 0x03),
564 	REG_SEQ0(CMN_REG(005c), 0x05),
565 	REG_SEQ0(CMN_REG(005d), 0x0c),
566 	REG_SEQ0(CMN_REG(005e), 0x07),
567 	REG_SEQ0(CMN_REG(0060), 0x01),
568 	REG_SEQ0(CMN_REG(0064), 0x07),
569 	REG_SEQ0(CMN_REG(0065), 0x00),
570 	REG_SEQ0(CMN_REG(0069), 0x00),
571 	REG_SEQ0(CMN_REG(006b), 0x04),
572 	REG_SEQ0(CMN_REG(006c), 0x00),
573 	REG_SEQ0(CMN_REG(0070), 0x01),
574 	REG_SEQ0(CMN_REG(0073), 0x30),
575 	REG_SEQ0(CMN_REG(0074), 0x00),
576 	REG_SEQ0(CMN_REG(0081), 0x09),
577 	REG_SEQ0(CMN_REG(0086), 0x01),
578 	REG_SEQ0(CMN_REG(0087), 0x0c),
579 	REG_SEQ0(CMN_REG(0089), 0x02),
580 	REG_SEQ0(CMN_REG(0095), 0x00),
581 	REG_SEQ0(CMN_REG(0097), 0x00),
582 	REG_SEQ0(CMN_REG(0099), 0x00),
583 	REG_SEQ0(CMN_REG(009b), 0x10),
584 };
585 
586 static const struct reg_sequence rk_hdptx_frl_lcpll_ropll_cmn_init_seq[] = {
587 	REG_SEQ0(CMN_REG(0008), 0xd0),
588 	REG_SEQ0(CMN_REG(0011), 0x00),
589 	REG_SEQ0(CMN_REG(0017), 0x00),
590 	REG_SEQ0(CMN_REG(001e), 0x35),
591 	REG_SEQ0(CMN_REG(0020), 0x6b),
592 	REG_SEQ0(CMN_REG(0021), 0x6b),
593 	REG_SEQ0(CMN_REG(0022), 0x11),
594 	REG_SEQ0(CMN_REG(0024), 0x00),
595 	REG_SEQ0(CMN_REG(0025), 0x10),
596 	REG_SEQ0(CMN_REG(0026), 0x53),
597 	REG_SEQ0(CMN_REG(0027), 0x15),
598 	REG_SEQ0(CMN_REG(0028), 0x0d),
599 	REG_SEQ0(CMN_REG(002a), 0x09),
600 	REG_SEQ0(CMN_REG(002b), 0x01),
601 	REG_SEQ0(CMN_REG(002c), 0x02),
602 	REG_SEQ0(CMN_REG(002d), 0x02),
603 	REG_SEQ0(CMN_REG(002e), 0x0d),
604 	REG_SEQ0(CMN_REG(002f), 0x61),
605 	REG_SEQ0(CMN_REG(0030), 0x00),
606 	REG_SEQ0(CMN_REG(0031), 0x20),
607 	REG_SEQ0(CMN_REG(0032), 0x30),
608 	REG_SEQ0(CMN_REG(0033), 0x0b),
609 	REG_SEQ0(CMN_REG(0034), 0x23),
610 	REG_SEQ0(CMN_REG(0037), 0x00),
611 	REG_SEQ0(CMN_REG(003d), 0xc0),
612 	REG_SEQ0(CMN_REG(0042), 0xb8),
613 	REG_SEQ0(CMN_REG(0046), 0xff),
614 	REG_SEQ0(CMN_REG(0048), 0x44),
615 	REG_SEQ0(CMN_REG(004e), 0x14),
616 	REG_SEQ0(CMN_REG(0054), 0x19),
617 	REG_SEQ0(CMN_REG(0058), 0x19),
618 	REG_SEQ0(CMN_REG(0059), 0x11),
619 	REG_SEQ0(CMN_REG(005b), 0x30),
620 	REG_SEQ0(CMN_REG(005c), 0x25),
621 	REG_SEQ0(CMN_REG(005d), 0x14),
622 	REG_SEQ0(CMN_REG(005e), 0x0e),
623 	REG_SEQ0(CMN_REG(0063), 0x01),
624 	REG_SEQ0(CMN_REG(0064), 0x0e),
625 	REG_SEQ0(CMN_REG(0068), 0x00),
626 	REG_SEQ0(CMN_REG(0069), 0x02),
627 	REG_SEQ0(CMN_REG(006b), 0x00),
628 	REG_SEQ0(CMN_REG(006f), 0x00),
629 	REG_SEQ0(CMN_REG(0073), 0x02),
630 	REG_SEQ0(CMN_REG(0074), 0x00),
631 	REG_SEQ0(CMN_REG(007a), 0x00),
632 	REG_SEQ0(CMN_REG(0081), 0x09),
633 	REG_SEQ0(CMN_REG(0086), 0x11),
634 	REG_SEQ0(CMN_REG(0087), 0x0c),
635 	REG_SEQ0(CMN_REG(0089), 0x00),
636 	REG_SEQ0(CMN_REG(0095), 0x03),
637 	REG_SEQ0(CMN_REG(0097), 0x00),
638 	REG_SEQ0(CMN_REG(0099), 0x00),
639 	REG_SEQ0(CMN_REG(009b), 0x10),
640 	REG_SEQ0(CMN_REG(009e), 0x03),
641 	REG_SEQ0(CMN_REG(009f), 0xff),
642 	REG_SEQ0(CMN_REG(00a0), 0x60),
643 };
644 
645 static const struct reg_sequence rk_hdptx_tmds_cmn_init_seq[] = {
646 	REG_SEQ0(CMN_REG(0008), 0x00),
647 	REG_SEQ0(CMN_REG(0011), 0x01),
648 	REG_SEQ0(CMN_REG(0017), 0x20),
649 	REG_SEQ0(CMN_REG(001e), 0x14),
650 	REG_SEQ0(CMN_REG(0020), 0x00),
651 	REG_SEQ0(CMN_REG(0021), 0x00),
652 	REG_SEQ0(CMN_REG(0022), 0x11),
653 	REG_SEQ0(CMN_REG(0023), 0x00),
654 	REG_SEQ0(CMN_REG(0024), 0x00),
655 	REG_SEQ0(CMN_REG(0025), 0x53),
656 	REG_SEQ0(CMN_REG(0026), 0x00),
657 	REG_SEQ0(CMN_REG(0027), 0x00),
658 	REG_SEQ0(CMN_REG(0028), 0x01),
659 	REG_SEQ0(CMN_REG(002a), 0x00),
660 	REG_SEQ0(CMN_REG(002b), 0x00),
661 	REG_SEQ0(CMN_REG(002c), 0x00),
662 	REG_SEQ0(CMN_REG(002d), 0x00),
663 	REG_SEQ0(CMN_REG(002e), 0x04),
664 	REG_SEQ0(CMN_REG(002f), 0x00),
665 	REG_SEQ0(CMN_REG(0030), 0x20),
666 	REG_SEQ0(CMN_REG(0031), 0x30),
667 	REG_SEQ0(CMN_REG(0032), 0x0b),
668 	REG_SEQ0(CMN_REG(0033), 0x23),
669 	REG_SEQ0(CMN_REG(0034), 0x00),
670 	REG_SEQ0(CMN_REG(003d), 0x40),
671 	REG_SEQ0(CMN_REG(0042), 0x78),
672 	REG_SEQ0(CMN_REG(0046), 0xdd),
673 	REG_SEQ0(CMN_REG(0048), 0x11),
674 	REG_SEQ0(CMN_REG(004e), 0x34),
675 	REG_SEQ0(CMN_REG(005c), 0x25),
676 	REG_SEQ0(CMN_REG(005d), 0x0c),
677 	REG_SEQ0(CMN_REG(005e), 0x4f),
678 	REG_SEQ0(CMN_REG(006b), 0x04),
679 	REG_SEQ0(CMN_REG(0073), 0x30),
680 	REG_SEQ0(CMN_REG(0074), 0x04),
681 	REG_SEQ0(CMN_REG(0081), 0x01),
682 	REG_SEQ0(CMN_REG(0086), 0x01),
683 	REG_SEQ0(CMN_REG(0087), 0x04),
684 	REG_SEQ0(CMN_REG(0089), 0x00),
685 	REG_SEQ0(CMN_REG(0095), 0x00),
686 	REG_SEQ0(CMN_REG(0097), 0x02),
687 	REG_SEQ0(CMN_REG(0099), 0x04),
688 	REG_SEQ0(CMN_REG(009b), 0x00),
689 };
690 
691 static const struct reg_sequence rk_hdptx_common_sb_init_seq[] = {
692 	REG_SEQ0(SB_REG(0114), 0x00),
693 	REG_SEQ0(SB_REG(0115), 0x00),
694 	REG_SEQ0(SB_REG(0116), 0x00),
695 	REG_SEQ0(SB_REG(0117), 0x00),
696 };
697 
698 static const struct reg_sequence rk_hdptx_frl_lntop_init_seq[] = {
699 	REG_SEQ0(LNTOP_REG(0200), 0x04),
700 	REG_SEQ0(LNTOP_REG(0201), 0x00),
701 	REG_SEQ0(LNTOP_REG(0202), 0x00),
702 	REG_SEQ0(LNTOP_REG(0203), 0xf0),
703 	REG_SEQ0(LNTOP_REG(0204), 0xff),
704 	REG_SEQ0(LNTOP_REG(0205), 0xff),
705 	REG_SEQ0(LNTOP_REG(0206), 0x05),
706 };
707 
708 static const struct reg_sequence rk_hdptx_tmds_lntop_highbr_seq[] = {
709 	REG_SEQ0(LNTOP_REG(0201), 0x00),
710 	REG_SEQ0(LNTOP_REG(0202), 0x00),
711 	REG_SEQ0(LNTOP_REG(0203), 0x0f),
712 	REG_SEQ0(LNTOP_REG(0204), 0xff),
713 	REG_SEQ0(LNTOP_REG(0205), 0xff),
714 };
715 
716 static const struct reg_sequence rk_hdptx_tmds_lntop_lowbr_seq[] = {
717 	REG_SEQ0(LNTOP_REG(0201), 0x07),
718 	REG_SEQ0(LNTOP_REG(0202), 0xc1),
719 	REG_SEQ0(LNTOP_REG(0203), 0xf0),
720 	REG_SEQ0(LNTOP_REG(0204), 0x7c),
721 	REG_SEQ0(LNTOP_REG(0205), 0x1f),
722 };
723 
724 static const struct reg_sequence rk_hdptx_common_lane_init_seq[] = {
725 	REG_SEQ0(LANE_REG(0303), 0x0c),
726 	REG_SEQ0(LANE_REG(0307), 0x20),
727 	REG_SEQ0(LANE_REG(030a), 0x17),
728 	REG_SEQ0(LANE_REG(030b), 0x77),
729 	REG_SEQ0(LANE_REG(030c), 0x77),
730 	REG_SEQ0(LANE_REG(030d), 0x77),
731 	REG_SEQ0(LANE_REG(030e), 0x38),
732 	REG_SEQ0(LANE_REG(0310), 0x03),
733 	REG_SEQ0(LANE_REG(0311), 0x0f),
734 	REG_SEQ0(LANE_REG(0316), 0x02),
735 	REG_SEQ0(LANE_REG(031b), 0x01),
736 	REG_SEQ0(LANE_REG(031f), 0x15),
737 	REG_SEQ0(LANE_REG(0320), 0xa0),
738 	REG_SEQ0(LANE_REG(0403), 0x0c),
739 	REG_SEQ0(LANE_REG(0407), 0x20),
740 	REG_SEQ0(LANE_REG(040a), 0x17),
741 	REG_SEQ0(LANE_REG(040b), 0x77),
742 	REG_SEQ0(LANE_REG(040c), 0x77),
743 	REG_SEQ0(LANE_REG(040d), 0x77),
744 	REG_SEQ0(LANE_REG(040e), 0x38),
745 	REG_SEQ0(LANE_REG(0410), 0x03),
746 	REG_SEQ0(LANE_REG(0411), 0x0f),
747 	REG_SEQ0(LANE_REG(0416), 0x02),
748 	REG_SEQ0(LANE_REG(041b), 0x01),
749 	REG_SEQ0(LANE_REG(041f), 0x15),
750 	REG_SEQ0(LANE_REG(0420), 0xa0),
751 	REG_SEQ0(LANE_REG(0503), 0x0c),
752 	REG_SEQ0(LANE_REG(0507), 0x20),
753 	REG_SEQ0(LANE_REG(050a), 0x17),
754 	REG_SEQ0(LANE_REG(050b), 0x77),
755 	REG_SEQ0(LANE_REG(050c), 0x77),
756 	REG_SEQ0(LANE_REG(050d), 0x77),
757 	REG_SEQ0(LANE_REG(050e), 0x38),
758 	REG_SEQ0(LANE_REG(0510), 0x03),
759 	REG_SEQ0(LANE_REG(0511), 0x0f),
760 	REG_SEQ0(LANE_REG(0516), 0x02),
761 	REG_SEQ0(LANE_REG(051b), 0x01),
762 	REG_SEQ0(LANE_REG(051f), 0x15),
763 	REG_SEQ0(LANE_REG(0520), 0xa0),
764 	REG_SEQ0(LANE_REG(0603), 0x0c),
765 	REG_SEQ0(LANE_REG(0607), 0x20),
766 	REG_SEQ0(LANE_REG(060a), 0x17),
767 	REG_SEQ0(LANE_REG(060b), 0x77),
768 	REG_SEQ0(LANE_REG(060c), 0x77),
769 	REG_SEQ0(LANE_REG(060d), 0x77),
770 	REG_SEQ0(LANE_REG(060e), 0x38),
771 	REG_SEQ0(LANE_REG(0610), 0x03),
772 	REG_SEQ0(LANE_REG(0611), 0x0f),
773 	REG_SEQ0(LANE_REG(0616), 0x02),
774 	REG_SEQ0(LANE_REG(061b), 0x01),
775 	REG_SEQ0(LANE_REG(061f), 0x15),
776 	REG_SEQ0(LANE_REG(0620), 0xa0),
777 };
778 
779 static const struct reg_sequence rk_hdptx_frl_lane_init_seq[] = {
780 	REG_SEQ0(LANE_REG(0312), 0x3c),
781 	REG_SEQ0(LANE_REG(0412), 0x3c),
782 	REG_SEQ0(LANE_REG(0512), 0x3c),
783 	REG_SEQ0(LANE_REG(0612), 0x3c),
784 	REG_SEQ0(LANE_REG(0303), 0x2f),
785 	REG_SEQ0(LANE_REG(0403), 0x2f),
786 	REG_SEQ0(LANE_REG(0503), 0x2f),
787 	REG_SEQ0(LANE_REG(0603), 0x2f),
788 	REG_SEQ0(LANE_REG(0305), 0x03),
789 	REG_SEQ0(LANE_REG(0405), 0x03),
790 	REG_SEQ0(LANE_REG(0505), 0x03),
791 	REG_SEQ0(LANE_REG(0605), 0x03),
792 	REG_SEQ0(LANE_REG(0306), 0xfc),
793 	REG_SEQ0(LANE_REG(0406), 0xfc),
794 	REG_SEQ0(LANE_REG(0506), 0xfc),
795 	REG_SEQ0(LANE_REG(0606), 0xfc),
796 	REG_SEQ0(LANE_REG(0305), 0x4f),
797 	REG_SEQ0(LANE_REG(0405), 0x4f),
798 	REG_SEQ0(LANE_REG(0505), 0x4f),
799 	REG_SEQ0(LANE_REG(0605), 0x4f),
800 	REG_SEQ0(LANE_REG(0304), 0x14),
801 	REG_SEQ0(LANE_REG(0404), 0x14),
802 	REG_SEQ0(LANE_REG(0504), 0x14),
803 	REG_SEQ0(LANE_REG(0604), 0x14),
804 	/* Keep Inter-Pair Skew in the limits */
805 	REG_SEQ0(LANE_REG(031e), 0x02),
806 	REG_SEQ0(LANE_REG(041e), 0x02),
807 	REG_SEQ0(LANE_REG(051e), 0x02),
808 	REG_SEQ0(LANE_REG(061e), 0x02),
809 };
810 
811 static const struct reg_sequence rk_hdptx_tmds_lane_init_seq[] = {
812 	REG_SEQ0(LANE_REG(0312), 0x00),
813 	REG_SEQ0(LANE_REG(0412), 0x00),
814 	REG_SEQ0(LANE_REG(0512), 0x00),
815 	REG_SEQ0(LANE_REG(0612), 0x00),
816 	REG_SEQ0(LANE_REG(0303), 0x2f),
817 	REG_SEQ0(LANE_REG(0403), 0x2f),
818 	REG_SEQ0(LANE_REG(0503), 0x2f),
819 	REG_SEQ0(LANE_REG(0603), 0x2f),
820 	REG_SEQ0(LANE_REG(0305), 0x03),
821 	REG_SEQ0(LANE_REG(0405), 0x03),
822 	REG_SEQ0(LANE_REG(0505), 0x03),
823 	REG_SEQ0(LANE_REG(0605), 0x03),
824 	REG_SEQ0(LANE_REG(0306), 0x1c),
825 	REG_SEQ0(LANE_REG(0406), 0x1c),
826 	REG_SEQ0(LANE_REG(0506), 0x1c),
827 	REG_SEQ0(LANE_REG(0606), 0x1c),
828 	/* Keep Inter-Pair Skew in the limits */
829 	REG_SEQ0(LANE_REG(031e), 0x02),
830 	REG_SEQ0(LANE_REG(041e), 0x02),
831 	REG_SEQ0(LANE_REG(051e), 0x02),
832 	REG_SEQ0(LANE_REG(061e), 0x0a),
833 };
834 
835 static struct tx_drv_ctrl tx_drv_ctrl_rbr[4][4] = {
836 	/* voltage swing 0, pre-emphasis 0->3 */
837 	{
838 		{ 0x2, 0x0, 0x4, 0x6, 0x0, 0x4, 0x1, 0x1, 0x7, 0x7 },
839 		{ 0x4, 0x3, 0x4, 0x6, 0x0, 0x4, 0x0, 0x1, 0x7, 0x7 },
840 		{ 0x7, 0x6, 0x4, 0x6, 0x0, 0x4, 0x0, 0x1, 0x7, 0x7 },
841 		{ 0xd, 0xc, 0x7, 0x7, 0x1, 0x7, 0x0, 0x1, 0x7, 0x7 },
842 	},
843 
844 	/* voltage swing 1, pre-emphasis 0->2 */
845 	{
846 		{ 0x4, 0x0, 0x4, 0x6, 0x0, 0x4, 0x1, 0x1, 0x7, 0x7 },
847 		{ 0x9, 0x5, 0x4, 0x6, 0x0, 0x4, 0x0, 0x1, 0x7, 0x7 },
848 		{ 0xc, 0x8, 0x7, 0x7, 0x1, 0x7, 0x0, 0x1, 0x7, 0x7 },
849 	},
850 
851 	/* voltage swing 2, pre-emphasis 0->1 */
852 	{
853 		{ 0x8, 0x0, 0x4, 0x6, 0x0, 0x4, 0x1, 0x1, 0x7, 0x7 },
854 		{ 0xc, 0x5, 0x7, 0x7, 0x1, 0x7, 0x0, 0x1, 0x7, 0x7 },
855 	},
856 
857 	/* voltage swing 3, pre-emphasis 0 */
858 	{
859 		{ 0xb, 0x0, 0x7, 0x7, 0x1, 0x4, 0x1, 0x1, 0x7, 0x7 },
860 	}
861 };
862 
863 static struct tx_drv_ctrl tx_drv_ctrl_hbr[4][4] = {
864 	/* voltage swing 0, pre-emphasis 0->3 */
865 	{
866 		{ 0x2, 0x0, 0x4, 0x6, 0x0, 0x4, 0x1, 0x1, 0x7, 0x7 },
867 		{ 0x5, 0x4, 0x4, 0x6, 0x0, 0x4, 0x0, 0x1, 0x7, 0x7 },
868 		{ 0x9, 0x8, 0x4, 0x6, 0x0, 0x4, 0x0, 0x1, 0x7, 0x7 },
869 		{ 0xd, 0xc, 0x7, 0x7, 0x1, 0x7, 0x0, 0x1, 0x7, 0x7 },
870 	},
871 
872 	/* voltage swing 1, pre-emphasis 0->2 */
873 	{
874 		{ 0x6, 0x1, 0x4, 0x6, 0x0, 0x4, 0x1, 0x1, 0x7, 0x7 },
875 		{ 0xa, 0x6, 0x4, 0x6, 0x0, 0x4, 0x0, 0x1, 0x7, 0x7 },
876 		{ 0xc, 0x8, 0x7, 0x7, 0x1, 0x7, 0x0, 0x1, 0x7, 0x7 },
877 	},
878 
879 	/* voltage swing 2, pre-emphasis 0->1 */
880 	{
881 		{ 0x9, 0x1, 0x4, 0x6, 0x0, 0x4, 0x1, 0x1, 0x7, 0x7 },
882 		{ 0xd, 0x6, 0x7, 0x7, 0x1, 0x7, 0x0, 0x1, 0x7, 0x7 },
883 	},
884 
885 	/* voltage swing 3, pre-emphasis 0 */
886 	{
887 		{ 0xc, 0x1, 0x7, 0x7, 0x1, 0x4, 0x1, 0x1, 0x7, 0x7 },
888 	}
889 };
890 
891 static struct tx_drv_ctrl tx_drv_ctrl_hbr2[4][4] = {
892 	/* voltage swing 0, pre-emphasis 0->3 */
893 	{
894 		{ 0x2, 0x1, 0x4, 0x6, 0x0, 0x4, 0x0, 0x1, 0x7, 0x7 },
895 		{ 0x5, 0x4, 0x4, 0x6, 0x0, 0x4, 0x0, 0x1, 0x7, 0x7 },
896 		{ 0x9, 0x8, 0x4, 0x6, 0x1, 0x4, 0x0, 0x1, 0x7, 0x7 },
897 		{ 0xd, 0xc, 0x7, 0x7, 0x1, 0x7, 0x0, 0x1, 0x7, 0x7 },
898 	},
899 
900 	/* voltage swing 1, pre-emphasis 0->2 */
901 	{
902 		{ 0x6, 0x1, 0x4, 0x6, 0x0, 0x4, 0x1, 0x1, 0x7, 0x7 },
903 		{ 0xb, 0x7, 0x4, 0x6, 0x0, 0x4, 0x0, 0x1, 0x7, 0x7 },
904 		{ 0xd, 0x9, 0x7, 0x7, 0x1, 0x7, 0x0, 0x1, 0x7, 0x7 },
905 	},
906 
907 	/* voltage swing 2, pre-emphasis 0->1 */
908 	{
909 		{ 0x8, 0x1, 0x4, 0x6, 0x0, 0x4, 0x1, 0x1, 0x7, 0x7 },
910 		{ 0xc, 0x6, 0x7, 0x7, 0x1, 0x7, 0x0, 0x1, 0x7, 0x7 },
911 	},
912 
913 	/* voltage swing 3, pre-emphasis 0 */
914 	{
915 		{ 0xb, 0x0, 0x7, 0x7, 0x1, 0x4, 0x1, 0x1, 0x7, 0x7 },
916 	}
917 };
918 
rk_hdptx_phy_is_rw_reg(struct device * dev,unsigned int reg)919 static bool rk_hdptx_phy_is_rw_reg(struct device *dev, unsigned int reg)
920 {
921 	switch (reg) {
922 	case 0x0000 ... 0x029c: /* CMN Register */
923 	case 0x0400 ... 0x04a4: /* Sideband Register */
924 	case 0x0800 ... 0x08a4: /* Lane Top Register */
925 	case 0x0c00 ... 0x0cb4: /* Lane 0 Register */
926 	case 0x1000 ... 0x10b4: /* Lane 1 Register */
927 	case 0x1400 ... 0x14b4: /* Lane 2 Register */
928 	case 0x1800 ... 0x18b4: /* Lane 3 Register */
929 		return true;
930 	}
931 
932 	return false;
933 }
934 
935 static const struct regmap_config rk_hdptx_phy_regmap_config = {
936 	.reg_bits = 32,
937 	.reg_stride = 4,
938 	.val_bits = 32,
939 	.writeable_reg = rk_hdptx_phy_is_rw_reg,
940 	.readable_reg = rk_hdptx_phy_is_rw_reg,
941 	.max_register = 0x18b4,
942 };
943 
944 #define rk_hdptx_multi_reg_write(hdptx, seq) \
945 	regmap_multi_reg_write((hdptx)->regmap, seq, ARRAY_SIZE(seq))
946 
rk_hdptx_pre_power_up(struct rk_hdptx_phy * hdptx)947 static void rk_hdptx_pre_power_up(struct rk_hdptx_phy *hdptx)
948 {
949 	u32 val;
950 
951 	reset_control_assert(hdptx->rsts[RST_APB].rstc);
952 	usleep_range(20, 25);
953 	reset_control_deassert(hdptx->rsts[RST_APB].rstc);
954 
955 	reset_control_assert(hdptx->rsts[RST_LANE].rstc);
956 	reset_control_assert(hdptx->rsts[RST_CMN].rstc);
957 	reset_control_assert(hdptx->rsts[RST_INIT].rstc);
958 
959 	val = (FIELD_PREP_WM16(HDPTX_I_PLL_EN, 0) |
960 	       FIELD_PREP_WM16(HDPTX_I_BIAS_EN, 0) |
961 	       FIELD_PREP_WM16(HDPTX_I_BGR_EN, 0));
962 	regmap_write(hdptx->grf, GRF_HDPTX_CON0, val);
963 }
964 
rk_hdptx_post_enable_lane(struct rk_hdptx_phy * hdptx)965 static int rk_hdptx_post_enable_lane(struct rk_hdptx_phy *hdptx)
966 {
967 	u32 val;
968 	int ret;
969 
970 	reset_control_deassert(hdptx->rsts[RST_LANE].rstc);
971 
972 	val = (FIELD_PREP_WM16(HDPTX_I_BIAS_EN, 1) |
973 	       FIELD_PREP_WM16(HDPTX_I_BGR_EN, 1));
974 	regmap_write(hdptx->grf, GRF_HDPTX_CON0, val);
975 
976 	/* 3 lanes FRL mode */
977 	if (hdptx->hdmi_cfg.rate == FRL_6G3L_RATE ||
978 	    hdptx->hdmi_cfg.rate == FRL_3G3L_RATE)
979 		regmap_write(hdptx->regmap, LNTOP_REG(0207), 0x07);
980 	else
981 		regmap_write(hdptx->regmap, LNTOP_REG(0207), 0x0f);
982 
983 	ret = regmap_read_poll_timeout(hdptx->grf, GRF_HDPTX_STATUS, val,
984 				       (val & HDPTX_O_PHY_RDY) &&
985 				       (val & HDPTX_O_PLL_LOCK_DONE),
986 				       100, 5000);
987 	if (ret) {
988 		dev_err(hdptx->dev, "Failed to get PHY lane lock: %d\n", ret);
989 		return ret;
990 	}
991 
992 	dev_dbg(hdptx->dev, "PHY lane locked\n");
993 
994 	return 0;
995 }
996 
rk_hdptx_post_enable_pll(struct rk_hdptx_phy * hdptx)997 static int rk_hdptx_post_enable_pll(struct rk_hdptx_phy *hdptx)
998 {
999 	u32 val;
1000 	int ret;
1001 
1002 	val = (FIELD_PREP_WM16(HDPTX_I_BIAS_EN, 1) |
1003 	       FIELD_PREP_WM16(HDPTX_I_BGR_EN, 1));
1004 	regmap_write(hdptx->grf, GRF_HDPTX_CON0, val);
1005 
1006 	usleep_range(10, 15);
1007 	reset_control_deassert(hdptx->rsts[RST_INIT].rstc);
1008 
1009 	usleep_range(10, 15);
1010 	regmap_write(hdptx->grf, GRF_HDPTX_CON0, FIELD_PREP_WM16(HDPTX_I_PLL_EN, 1));
1011 
1012 	usleep_range(10, 15);
1013 	reset_control_deassert(hdptx->rsts[RST_CMN].rstc);
1014 
1015 	ret = regmap_read_poll_timeout(hdptx->grf, GRF_HDPTX_STATUS, val,
1016 				       val & HDPTX_O_PHY_CLK_RDY, 20, 400);
1017 	if (ret) {
1018 		dev_err(hdptx->dev, "Failed to get PHY clk ready: %d\n", ret);
1019 		return ret;
1020 	}
1021 
1022 	dev_dbg(hdptx->dev, "PHY clk ready\n");
1023 
1024 	return 0;
1025 }
1026 
rk_hdptx_phy_disable(struct rk_hdptx_phy * hdptx)1027 static void rk_hdptx_phy_disable(struct rk_hdptx_phy *hdptx)
1028 {
1029 	u32 val;
1030 
1031 	reset_control_assert(hdptx->rsts[RST_APB].rstc);
1032 	usleep_range(20, 30);
1033 	reset_control_deassert(hdptx->rsts[RST_APB].rstc);
1034 
1035 	regmap_write(hdptx->regmap, LNTOP_REG(0207), 0x0);
1036 	regmap_write(hdptx->regmap, LANE_REG(0300), 0x82);
1037 	regmap_write(hdptx->regmap, SB_REG(010f), 0xc1);
1038 	regmap_write(hdptx->regmap, SB_REG(0110), 0x1);
1039 	regmap_write(hdptx->regmap, LANE_REG(0301), 0x80);
1040 	regmap_write(hdptx->regmap, LANE_REG(0401), 0x80);
1041 	regmap_write(hdptx->regmap, LANE_REG(0501), 0x80);
1042 	regmap_write(hdptx->regmap, LANE_REG(0601), 0x80);
1043 
1044 	reset_control_assert(hdptx->rsts[RST_LANE].rstc);
1045 	reset_control_assert(hdptx->rsts[RST_CMN].rstc);
1046 	reset_control_assert(hdptx->rsts[RST_INIT].rstc);
1047 
1048 	val = (FIELD_PREP_WM16(HDPTX_I_PLL_EN, 0) |
1049 	       FIELD_PREP_WM16(HDPTX_I_BIAS_EN, 0) |
1050 	       FIELD_PREP_WM16(HDPTX_I_BGR_EN, 0));
1051 	regmap_write(hdptx->grf, GRF_HDPTX_CON0, val);
1052 }
1053 
rk_hdptx_phy_clk_pll_calc(unsigned long long rate,struct ropll_config * cfg)1054 static bool rk_hdptx_phy_clk_pll_calc(unsigned long long rate,
1055 				      struct ropll_config *cfg)
1056 {
1057 	const unsigned int fout = div_u64(rate, 200), fref = 24000;
1058 	unsigned long k = 0, lc, k_sub, lc_sub;
1059 	unsigned int fvco, sdc;
1060 	u32 mdiv, sdiv, n = 8;
1061 
1062 	if (fout > 0xfffffff)
1063 		return false;
1064 
1065 	for (sdiv = 16; sdiv >= 1; sdiv--) {
1066 		if (sdiv % 2 && sdiv != 1)
1067 			continue;
1068 
1069 		fvco = fout * sdiv;
1070 
1071 		if (fvco < 2000000 || fvco > 4000000)
1072 			continue;
1073 
1074 		mdiv = DIV_ROUND_UP(fvco, fref);
1075 		if (mdiv < 20 || mdiv > 255)
1076 			continue;
1077 
1078 		if (fref * mdiv - fvco) {
1079 			for (sdc = 264000; sdc <= 750000; sdc += fref)
1080 				if (sdc * n > fref * mdiv)
1081 					break;
1082 
1083 			if (sdc > 750000)
1084 				continue;
1085 
1086 			rational_best_approximation(fref * mdiv - fvco,
1087 						    sdc / 16,
1088 						    GENMASK(6, 0),
1089 						    GENMASK(7, 0),
1090 						    &k, &lc);
1091 
1092 			rational_best_approximation(sdc * n - fref * mdiv,
1093 						    sdc,
1094 						    GENMASK(6, 0),
1095 						    GENMASK(7, 0),
1096 						    &k_sub, &lc_sub);
1097 		}
1098 
1099 		break;
1100 	}
1101 
1102 	if (sdiv < 1)
1103 		return false;
1104 
1105 	if (cfg) {
1106 		cfg->pms_mdiv = mdiv;
1107 		cfg->pms_mdiv_afc = mdiv;
1108 		cfg->pms_pdiv = 1;
1109 		cfg->pms_refdiv = 1;
1110 		cfg->pms_sdiv = sdiv - 1;
1111 
1112 		cfg->sdm_en = k > 0 ? 1 : 0;
1113 		if (cfg->sdm_en) {
1114 			cfg->sdm_deno = lc;
1115 			cfg->sdm_num_sign = 1;
1116 			cfg->sdm_num = k;
1117 			cfg->sdc_n = n - 3;
1118 			cfg->sdc_num = k_sub;
1119 			cfg->sdc_deno = lc_sub;
1120 		}
1121 	}
1122 
1123 	return true;
1124 }
1125 
rk_hdptx_frl_lcpll_cmn_config(struct rk_hdptx_phy * hdptx)1126 static int rk_hdptx_frl_lcpll_cmn_config(struct rk_hdptx_phy *hdptx)
1127 {
1128 	const struct lcpll_config *cfg = NULL;
1129 	int i;
1130 
1131 	dev_dbg(hdptx->dev, "%s rate=%llu\n", __func__, hdptx->hdmi_cfg.rate);
1132 
1133 	for (i = 0; i < ARRAY_SIZE(rk_hdptx_frl_lcpll_cfg); i++) {
1134 		if (hdptx->hdmi_cfg.rate == rk_hdptx_frl_lcpll_cfg[i].rate) {
1135 			cfg = &rk_hdptx_frl_lcpll_cfg[i];
1136 			break;
1137 		}
1138 	}
1139 
1140 	if (!cfg) {
1141 		dev_err(hdptx->dev, "%s cannot find pll cfg for rate=%llu\n",
1142 			__func__, hdptx->hdmi_cfg.rate);
1143 		return -EINVAL;
1144 	}
1145 
1146 	rk_hdptx_pre_power_up(hdptx);
1147 
1148 	regmap_write(hdptx->grf, GRF_HDPTX_CON0, FIELD_PREP_WM16(LC_REF_CLK_SEL, 0));
1149 
1150 	rk_hdptx_multi_reg_write(hdptx, rk_hdptx_common_cmn_init_seq);
1151 	rk_hdptx_multi_reg_write(hdptx, rk_hdptx_frl_lcpll_cmn_init_seq);
1152 
1153 	regmap_update_bits(hdptx->regmap, CMN_REG(0008),
1154 			   LCPLL_EN_MASK | LCPLL_LCVCO_MODE_EN_MASK,
1155 			   FIELD_PREP(LCPLL_EN_MASK, 1) |
1156 			   FIELD_PREP(LCPLL_LCVCO_MODE_EN_MASK, cfg->lcvco_mode_en));
1157 
1158 	regmap_update_bits(hdptx->regmap, CMN_REG(001e),
1159 			   LCPLL_PI_EN_MASK | LCPLL_100M_CLK_EN_MASK,
1160 			   FIELD_PREP(LCPLL_PI_EN_MASK, cfg->pi_en) |
1161 			   FIELD_PREP(LCPLL_100M_CLK_EN_MASK, cfg->clk_en_100m));
1162 
1163 	regmap_write(hdptx->regmap, CMN_REG(0020), cfg->pms_mdiv);
1164 	regmap_write(hdptx->regmap, CMN_REG(0021), cfg->pms_mdiv_afc);
1165 	regmap_write(hdptx->regmap, CMN_REG(0022),
1166 		     FIELD_PREP(ANA_LCPLL_PMS_PDIV_MASK, cfg->pms_pdiv) |
1167 		     FIELD_PREP(ANA_LCPLL_PMS_REFDIV_MASK, cfg->pms_refdiv));
1168 	regmap_write(hdptx->regmap, CMN_REG(0023),
1169 		     FIELD_PREP(LCPLL_PMS_SDIV_RBR_MASK, cfg->pms_sdiv) |
1170 		     FIELD_PREP(LCPLL_PMS_SDIV_HBR_MASK, cfg->pms_sdiv));
1171 	regmap_write(hdptx->regmap, CMN_REG(002a), cfg->sdm_deno);
1172 	regmap_write(hdptx->regmap, CMN_REG(002b), cfg->sdm_num_sign);
1173 	regmap_write(hdptx->regmap, CMN_REG(002c), cfg->sdm_num);
1174 
1175 	regmap_update_bits(hdptx->regmap, CMN_REG(002d), LCPLL_SDC_N_MASK,
1176 			   FIELD_PREP(LCPLL_SDC_N_MASK, cfg->sdc_n));
1177 
1178 	regmap_update_bits(hdptx->regmap, CMN_REG(0086), PLL_PCG_POSTDIV_SEL_MASK,
1179 			   FIELD_PREP(PLL_PCG_POSTDIV_SEL_MASK, cfg->pms_sdiv));
1180 	regmap_update_bits(hdptx->regmap, CMN_REG(0086), PLL_PCG_CLK_SEL_MASK,
1181 			   FIELD_PREP(PLL_PCG_CLK_SEL_MASK, (hdptx->hdmi_cfg.bpc - 8) >> 1));
1182 
1183 	return rk_hdptx_post_enable_pll(hdptx);
1184 }
1185 
rk_hdptx_frl_lcpll_ropll_cmn_config(struct rk_hdptx_phy * hdptx)1186 static int rk_hdptx_frl_lcpll_ropll_cmn_config(struct rk_hdptx_phy *hdptx)
1187 {
1188 	dev_dbg(hdptx->dev, "%s rate=%llu\n", __func__, hdptx->hdmi_cfg.rate);
1189 
1190 	rk_hdptx_pre_power_up(hdptx);
1191 
1192 	/* ROPLL input reference clock from LCPLL (cascade mode) */
1193 	regmap_write(hdptx->grf, GRF_HDPTX_CON0, FIELD_PREP_WM16(LC_REF_CLK_SEL, 1));
1194 
1195 	rk_hdptx_multi_reg_write(hdptx, rk_hdptx_common_cmn_init_seq);
1196 	rk_hdptx_multi_reg_write(hdptx, rk_hdptx_frl_lcpll_ropll_cmn_init_seq);
1197 
1198 	return rk_hdptx_post_enable_pll(hdptx);
1199 }
1200 
rk_hdptx_tmds_ropll_cmn_config(struct rk_hdptx_phy * hdptx)1201 static int rk_hdptx_tmds_ropll_cmn_config(struct rk_hdptx_phy *hdptx)
1202 {
1203 	const struct ropll_config *cfg = NULL;
1204 	struct ropll_config rc = {0};
1205 	int i;
1206 
1207 	if (!hdptx->hdmi_cfg.rate)
1208 		return 0;
1209 
1210 	for (i = 0; i < ARRAY_SIZE(rk_hdptx_tmds_ropll_cfg); i++)
1211 		if (hdptx->hdmi_cfg.rate == rk_hdptx_tmds_ropll_cfg[i].rate) {
1212 			cfg = &rk_hdptx_tmds_ropll_cfg[i];
1213 			break;
1214 		}
1215 
1216 	if (!cfg) {
1217 		if (!rk_hdptx_phy_clk_pll_calc(hdptx->hdmi_cfg.rate, &rc)) {
1218 			dev_err(hdptx->dev, "%s cannot find pll cfg for rate=%llu\n",
1219 				__func__, hdptx->hdmi_cfg.rate);
1220 			return -EINVAL;
1221 		}
1222 
1223 		cfg = &rc;
1224 	}
1225 
1226 	dev_dbg(hdptx->dev, "%s rate=%llu mdiv=%u sdiv=%u sdm_en=%u k_sign=%u k=%u lc=%u\n",
1227 		__func__, hdptx->hdmi_cfg.rate, cfg->pms_mdiv, cfg->pms_sdiv + 1,
1228 		cfg->sdm_en, cfg->sdm_num_sign, cfg->sdm_num, cfg->sdm_deno);
1229 
1230 	rk_hdptx_pre_power_up(hdptx);
1231 
1232 	regmap_write(hdptx->grf, GRF_HDPTX_CON0, FIELD_PREP_WM16(LC_REF_CLK_SEL, 0));
1233 
1234 	rk_hdptx_multi_reg_write(hdptx, rk_hdptx_common_cmn_init_seq);
1235 	rk_hdptx_multi_reg_write(hdptx, rk_hdptx_tmds_cmn_init_seq);
1236 
1237 	regmap_write(hdptx->regmap, CMN_REG(0051), cfg->pms_mdiv);
1238 	regmap_write(hdptx->regmap, CMN_REG(0055), cfg->pms_mdiv_afc);
1239 	regmap_write(hdptx->regmap, CMN_REG(0059),
1240 		     FIELD_PREP(ANA_ROPLL_PMS_PDIV_MASK, cfg->pms_pdiv) |
1241 		     FIELD_PREP(ANA_ROPLL_PMS_REFDIV_MASK, cfg->pms_refdiv));
1242 	regmap_write(hdptx->regmap, CMN_REG(005a),
1243 		     FIELD_PREP(ROPLL_PMS_SDIV_RBR_MASK, cfg->pms_sdiv));
1244 
1245 	regmap_update_bits(hdptx->regmap, CMN_REG(005e), ROPLL_SDM_EN_MASK,
1246 			   FIELD_PREP(ROPLL_SDM_EN_MASK, cfg->sdm_en));
1247 	if (!cfg->sdm_en)
1248 		regmap_update_bits(hdptx->regmap, CMN_REG(005e), 0xf, 0);
1249 
1250 	regmap_update_bits(hdptx->regmap, CMN_REG(0064), ROPLL_SDM_NUM_SIGN_RBR_MASK,
1251 			   FIELD_PREP(ROPLL_SDM_NUM_SIGN_RBR_MASK, cfg->sdm_num_sign));
1252 
1253 	regmap_write(hdptx->regmap, CMN_REG(0060), cfg->sdm_deno);
1254 	regmap_write(hdptx->regmap, CMN_REG(0065), cfg->sdm_num);
1255 
1256 	regmap_update_bits(hdptx->regmap, CMN_REG(0069), ROPLL_SDC_N_RBR_MASK,
1257 			   FIELD_PREP(ROPLL_SDC_N_RBR_MASK, cfg->sdc_n));
1258 
1259 	regmap_write(hdptx->regmap, CMN_REG(006c), cfg->sdc_num);
1260 	regmap_write(hdptx->regmap, CMN_REG(0070), cfg->sdc_deno);
1261 
1262 	regmap_update_bits(hdptx->regmap, CMN_REG(0086), PLL_PCG_POSTDIV_SEL_MASK,
1263 			   FIELD_PREP(PLL_PCG_POSTDIV_SEL_MASK, cfg->pms_sdiv));
1264 
1265 	regmap_update_bits(hdptx->regmap, CMN_REG(0086), PLL_PCG_CLK_SEL_MASK,
1266 			   FIELD_PREP(PLL_PCG_CLK_SEL_MASK, (hdptx->hdmi_cfg.bpc - 8) >> 1));
1267 
1268 	regmap_update_bits(hdptx->regmap, CMN_REG(0086), PLL_PCG_CLK_EN_MASK,
1269 			   FIELD_PREP(PLL_PCG_CLK_EN_MASK, 0x1));
1270 
1271 	return rk_hdptx_post_enable_pll(hdptx);
1272 }
1273 
rk_hdptx_pll_cmn_config(struct rk_hdptx_phy * hdptx)1274 static int rk_hdptx_pll_cmn_config(struct rk_hdptx_phy *hdptx)
1275 {
1276 	int ret;
1277 
1278 	if (hdptx->hdmi_cfg.rate <= HDMI20_MAX_RATE)
1279 		ret = rk_hdptx_tmds_ropll_cmn_config(hdptx);
1280 	else if (hdptx->hdmi_cfg.rate == FRL_8G4L_RATE)
1281 		ret = rk_hdptx_frl_lcpll_ropll_cmn_config(hdptx);
1282 	else
1283 		ret = rk_hdptx_frl_lcpll_cmn_config(hdptx);
1284 
1285 	if (!ret)
1286 		hdptx->pll_config_dirty = false;
1287 
1288 	return ret;
1289 }
1290 
rk_hdptx_frl_lcpll_mode_config(struct rk_hdptx_phy * hdptx)1291 static int rk_hdptx_frl_lcpll_mode_config(struct rk_hdptx_phy *hdptx)
1292 {
1293 	rk_hdptx_multi_reg_write(hdptx, rk_hdptx_common_sb_init_seq);
1294 	rk_hdptx_multi_reg_write(hdptx, rk_hdptx_frl_lntop_init_seq);
1295 
1296 	rk_hdptx_multi_reg_write(hdptx, rk_hdptx_common_lane_init_seq);
1297 	rk_hdptx_multi_reg_write(hdptx, rk_hdptx_frl_lane_init_seq);
1298 
1299 	return rk_hdptx_post_enable_lane(hdptx);
1300 }
1301 
rk_hdptx_tmds_ropll_mode_config(struct rk_hdptx_phy * hdptx)1302 static int rk_hdptx_tmds_ropll_mode_config(struct rk_hdptx_phy *hdptx)
1303 {
1304 	rk_hdptx_multi_reg_write(hdptx, rk_hdptx_common_sb_init_seq);
1305 
1306 	regmap_write(hdptx->regmap, LNTOP_REG(0200), 0x06);
1307 
1308 	if (hdptx->hdmi_cfg.rate > HDMI14_MAX_RATE) {
1309 		/* For 1/40 bitrate clk */
1310 		rk_hdptx_multi_reg_write(hdptx, rk_hdptx_tmds_lntop_highbr_seq);
1311 	} else {
1312 		/* For 1/10 bitrate clk */
1313 		rk_hdptx_multi_reg_write(hdptx, rk_hdptx_tmds_lntop_lowbr_seq);
1314 	}
1315 
1316 	regmap_write(hdptx->regmap, LNTOP_REG(0206), 0x07);
1317 
1318 	rk_hdptx_multi_reg_write(hdptx, rk_hdptx_common_lane_init_seq);
1319 	rk_hdptx_multi_reg_write(hdptx, rk_hdptx_tmds_lane_init_seq);
1320 
1321 	return rk_hdptx_post_enable_lane(hdptx);
1322 }
1323 
rk_hdptx_dp_reset(struct rk_hdptx_phy * hdptx)1324 static void rk_hdptx_dp_reset(struct rk_hdptx_phy *hdptx)
1325 {
1326 	reset_control_assert(hdptx->rsts[RST_LANE].rstc);
1327 	reset_control_assert(hdptx->rsts[RST_CMN].rstc);
1328 	reset_control_assert(hdptx->rsts[RST_INIT].rstc);
1329 
1330 	reset_control_assert(hdptx->rsts[RST_APB].rstc);
1331 	usleep_range(10, 15);
1332 	reset_control_deassert(hdptx->rsts[RST_APB].rstc);
1333 
1334 	regmap_update_bits(hdptx->regmap, LANE_REG(0301),
1335 			   OVRD_LN_TX_DRV_EI_EN_MASK | LN_TX_DRV_EI_EN_MASK,
1336 			   FIELD_PREP(OVRD_LN_TX_DRV_EI_EN_MASK, 1) |
1337 			   FIELD_PREP(LN_TX_DRV_EI_EN_MASK, 0));
1338 	regmap_update_bits(hdptx->regmap, LANE_REG(0401),
1339 			   OVRD_LN_TX_DRV_EI_EN_MASK | LN_TX_DRV_EI_EN_MASK,
1340 			   FIELD_PREP(OVRD_LN_TX_DRV_EI_EN_MASK, 1) |
1341 			   FIELD_PREP(LN_TX_DRV_EI_EN_MASK, 0));
1342 	regmap_update_bits(hdptx->regmap, LANE_REG(0501),
1343 			   OVRD_LN_TX_DRV_EI_EN_MASK | LN_TX_DRV_EI_EN_MASK,
1344 			   FIELD_PREP(OVRD_LN_TX_DRV_EI_EN_MASK, 1) |
1345 			   FIELD_PREP(LN_TX_DRV_EI_EN_MASK, 0));
1346 	regmap_update_bits(hdptx->regmap, LANE_REG(0601),
1347 			   OVRD_LN_TX_DRV_EI_EN_MASK | LN_TX_DRV_EI_EN_MASK,
1348 			   FIELD_PREP(OVRD_LN_TX_DRV_EI_EN_MASK, 1) |
1349 			   FIELD_PREP(LN_TX_DRV_EI_EN_MASK, 0));
1350 
1351 	regmap_write(hdptx->grf, GRF_HDPTX_CON0,
1352 		     FIELD_PREP_WM16(HDPTX_I_PLL_EN, 0) |
1353 		     FIELD_PREP_WM16(HDPTX_I_BIAS_EN, 0) |
1354 		     FIELD_PREP_WM16(HDPTX_I_BGR_EN, 0));
1355 }
1356 
rk_hdptx_phy_consumer_get(struct rk_hdptx_phy * hdptx)1357 static int rk_hdptx_phy_consumer_get(struct rk_hdptx_phy *hdptx)
1358 {
1359 	enum phy_mode mode = phy_get_mode(hdptx->phy);
1360 	u32 status;
1361 	int ret;
1362 
1363 	if (atomic_inc_return(&hdptx->usage_count) > 1)
1364 		return 0;
1365 
1366 	ret = regmap_read(hdptx->grf, GRF_HDPTX_STATUS, &status);
1367 	if (ret) {
1368 		atomic_dec(&hdptx->usage_count);
1369 		return ret;
1370 	}
1371 
1372 	if (mode == PHY_MODE_DP) {
1373 		rk_hdptx_dp_reset(hdptx);
1374 	} else {
1375 		/*
1376 		 * Ignore PLL config errors at this point as pll_config_dirty
1377 		 * was not reset and, therefore, operation will be retried.
1378 		 */
1379 		rk_hdptx_pll_cmn_config(hdptx);
1380 	}
1381 
1382 	return 0;
1383 }
1384 
rk_hdptx_phy_consumer_put(struct rk_hdptx_phy * hdptx,bool force)1385 static int rk_hdptx_phy_consumer_put(struct rk_hdptx_phy *hdptx, bool force)
1386 {
1387 	enum phy_mode mode = phy_get_mode(hdptx->phy);
1388 	u32 status;
1389 	int ret;
1390 
1391 	ret = atomic_dec_return(&hdptx->usage_count);
1392 	if (ret > 0)
1393 		return 0;
1394 
1395 	if (ret < 0) {
1396 		dev_warn(hdptx->dev, "Usage count underflow!\n");
1397 		ret = -EINVAL;
1398 	} else {
1399 		ret = regmap_read(hdptx->grf, GRF_HDPTX_STATUS, &status);
1400 		if (!ret) {
1401 			if (status & HDPTX_O_PLL_LOCK_DONE) {
1402 				if (mode == PHY_MODE_DP)
1403 					rk_hdptx_dp_reset(hdptx);
1404 				else
1405 					rk_hdptx_phy_disable(hdptx);
1406 			}
1407 			return 0;
1408 		} else if (force) {
1409 			return 0;
1410 		}
1411 	}
1412 
1413 	atomic_inc(&hdptx->usage_count);
1414 	return ret;
1415 }
1416 
rk_hdptx_dp_pll_init(struct rk_hdptx_phy * hdptx)1417 static void rk_hdptx_dp_pll_init(struct rk_hdptx_phy *hdptx)
1418 {
1419 	regmap_update_bits(hdptx->regmap, CMN_REG(003c), ANA_LCPLL_RESERVED7_MASK,
1420 			   FIELD_PREP(ANA_LCPLL_RESERVED7_MASK, 0x1));
1421 
1422 	regmap_update_bits(hdptx->regmap, CMN_REG(0046),
1423 			   ROPLL_ANA_CPP_CTRL_COARSE_MASK | ROPLL_ANA_CPP_CTRL_FINE_MASK,
1424 			   FIELD_PREP(ROPLL_ANA_CPP_CTRL_COARSE_MASK, 0xe) |
1425 			   FIELD_PREP(ROPLL_ANA_CPP_CTRL_FINE_MASK, 0xe));
1426 	regmap_update_bits(hdptx->regmap, CMN_REG(0047),
1427 			   ROPLL_ANA_LPF_C_SEL_COARSE_MASK |
1428 			   ROPLL_ANA_LPF_C_SEL_FINE_MASK,
1429 			   FIELD_PREP(ROPLL_ANA_LPF_C_SEL_COARSE_MASK, 0x4) |
1430 			   FIELD_PREP(ROPLL_ANA_LPF_C_SEL_FINE_MASK, 0x4));
1431 
1432 	regmap_write(hdptx->regmap, CMN_REG(0051), FIELD_PREP(ROPLL_PMS_MDIV_MASK, 0x87));
1433 	regmap_write(hdptx->regmap, CMN_REG(0052), FIELD_PREP(ROPLL_PMS_MDIV_MASK, 0x71));
1434 	regmap_write(hdptx->regmap, CMN_REG(0053), FIELD_PREP(ROPLL_PMS_MDIV_MASK, 0x71));
1435 
1436 	regmap_write(hdptx->regmap, CMN_REG(0055),
1437 		     FIELD_PREP(ROPLL_PMS_MDIV_AFC_MASK, 0x87));
1438 	regmap_write(hdptx->regmap, CMN_REG(0056),
1439 		     FIELD_PREP(ROPLL_PMS_MDIV_AFC_MASK, 0x71));
1440 	regmap_write(hdptx->regmap, CMN_REG(0057),
1441 		     FIELD_PREP(ROPLL_PMS_MDIV_AFC_MASK, 0x71));
1442 
1443 	regmap_write(hdptx->regmap, CMN_REG(0059),
1444 		     FIELD_PREP(ANA_ROPLL_PMS_PDIV_MASK, 0x1) |
1445 		     FIELD_PREP(ANA_ROPLL_PMS_REFDIV_MASK, 0x1));
1446 	regmap_write(hdptx->regmap, CMN_REG(005a),
1447 		     FIELD_PREP(ROPLL_PMS_SDIV_RBR_MASK, 0x3) |
1448 		     FIELD_PREP(ROPLL_PMS_SDIV_HBR_MASK, 0x1));
1449 	regmap_update_bits(hdptx->regmap, CMN_REG(005b), ROPLL_PMS_SDIV_HBR2_MASK,
1450 			   FIELD_PREP(ROPLL_PMS_SDIV_HBR2_MASK, 0x0));
1451 
1452 	regmap_update_bits(hdptx->regmap, CMN_REG(005e), ROPLL_SDM_EN_MASK,
1453 			   FIELD_PREP(ROPLL_SDM_EN_MASK, 0x1));
1454 	regmap_update_bits(hdptx->regmap, CMN_REG(005e),
1455 			   OVRD_ROPLL_SDM_RSTN_MASK | ROPLL_SDM_RSTN_MASK,
1456 			   FIELD_PREP(OVRD_ROPLL_SDM_RSTN_MASK, 0x1) |
1457 			   FIELD_PREP(ROPLL_SDM_RSTN_MASK, 0x1));
1458 	regmap_update_bits(hdptx->regmap, CMN_REG(005e), ROPLL_SDC_FRAC_EN_RBR_MASK,
1459 			   FIELD_PREP(ROPLL_SDC_FRAC_EN_RBR_MASK, 0x1));
1460 	regmap_update_bits(hdptx->regmap, CMN_REG(005e), ROPLL_SDC_FRAC_EN_HBR_MASK,
1461 			   FIELD_PREP(ROPLL_SDC_FRAC_EN_HBR_MASK, 0x1));
1462 	regmap_update_bits(hdptx->regmap, CMN_REG(005e), ROPLL_SDC_FRAC_EN_HBR2_MASK,
1463 			   FIELD_PREP(ROPLL_SDC_FRAC_EN_HBR2_MASK, 0x1));
1464 
1465 	regmap_update_bits(hdptx->regmap, CMN_REG(005f),
1466 			   OVRD_ROPLL_SDC_RSTN_MASK | ROPLL_SDC_RSTN_MASK,
1467 			   FIELD_PREP(OVRD_ROPLL_SDC_RSTN_MASK, 0x1) |
1468 			   FIELD_PREP(ROPLL_SDC_RSTN_MASK, 0x1));
1469 	regmap_write(hdptx->regmap, CMN_REG(0060),
1470 		     FIELD_PREP(ROPLL_SDM_DENOMINATOR_MASK, 0x21));
1471 	regmap_write(hdptx->regmap, CMN_REG(0061),
1472 		     FIELD_PREP(ROPLL_SDM_DENOMINATOR_MASK, 0x27));
1473 	regmap_write(hdptx->regmap, CMN_REG(0062),
1474 		     FIELD_PREP(ROPLL_SDM_DENOMINATOR_MASK, 0x27));
1475 
1476 	regmap_update_bits(hdptx->regmap, CMN_REG(0064),
1477 			   ROPLL_SDM_NUM_SIGN_RBR_MASK |
1478 			   ROPLL_SDM_NUM_SIGN_HBR_MASK |
1479 			   ROPLL_SDM_NUM_SIGN_HBR2_MASK,
1480 			   FIELD_PREP(ROPLL_SDM_NUM_SIGN_RBR_MASK, 0x0) |
1481 			   FIELD_PREP(ROPLL_SDM_NUM_SIGN_HBR_MASK, 0x1) |
1482 			   FIELD_PREP(ROPLL_SDM_NUM_SIGN_HBR2_MASK, 0x1));
1483 	regmap_write(hdptx->regmap, CMN_REG(0065),
1484 		     FIELD_PREP(ROPLL_SDM_NUM_MASK, 0x0));
1485 	regmap_write(hdptx->regmap, CMN_REG(0066),
1486 		     FIELD_PREP(ROPLL_SDM_NUM_MASK, 0xd));
1487 	regmap_write(hdptx->regmap, CMN_REG(0067),
1488 		     FIELD_PREP(ROPLL_SDM_NUM_MASK, 0xd));
1489 
1490 	regmap_update_bits(hdptx->regmap, CMN_REG(0069), ROPLL_SDC_N_RBR_MASK,
1491 			   FIELD_PREP(ROPLL_SDC_N_RBR_MASK, 0x2));
1492 
1493 	regmap_update_bits(hdptx->regmap, CMN_REG(006a),
1494 			   ROPLL_SDC_N_HBR_MASK | ROPLL_SDC_N_HBR2_MASK,
1495 			   FIELD_PREP(ROPLL_SDC_N_HBR_MASK, 0x1) |
1496 			   FIELD_PREP(ROPLL_SDC_N_HBR2_MASK, 0x1));
1497 
1498 	regmap_write(hdptx->regmap, CMN_REG(006c),
1499 		     FIELD_PREP(ROPLL_SDC_NUM_MASK, 0x3));
1500 	regmap_write(hdptx->regmap, CMN_REG(006d),
1501 		     FIELD_PREP(ROPLL_SDC_NUM_MASK, 0x7));
1502 	regmap_write(hdptx->regmap, CMN_REG(006e),
1503 		     FIELD_PREP(ROPLL_SDC_NUM_MASK, 0x7));
1504 
1505 	regmap_write(hdptx->regmap, CMN_REG(0070),
1506 		     FIELD_PREP(ROPLL_SDC_DENO_MASK, 0x8));
1507 	regmap_write(hdptx->regmap, CMN_REG(0071),
1508 		     FIELD_PREP(ROPLL_SDC_DENO_MASK, 0x18));
1509 	regmap_write(hdptx->regmap, CMN_REG(0072),
1510 		     FIELD_PREP(ROPLL_SDC_DENO_MASK, 0x18));
1511 
1512 	regmap_update_bits(hdptx->regmap, CMN_REG(0074),
1513 			   OVRD_ROPLL_SDC_NDIV_RSTN_MASK | ROPLL_SDC_NDIV_RSTN_MASK,
1514 			   FIELD_PREP(OVRD_ROPLL_SDC_NDIV_RSTN_MASK, 0x1) |
1515 			   FIELD_PREP(ROPLL_SDC_NDIV_RSTN_MASK, 0x1));
1516 
1517 	regmap_update_bits(hdptx->regmap, CMN_REG(0077), ANA_ROPLL_SSC_CLK_DIV_SEL_MASK,
1518 			   FIELD_PREP(ANA_ROPLL_SSC_CLK_DIV_SEL_MASK, 0x1));
1519 
1520 	regmap_update_bits(hdptx->regmap, CMN_REG(0081), ANA_PLL_CD_TX_SER_RATE_SEL_MASK,
1521 			   FIELD_PREP(ANA_PLL_CD_TX_SER_RATE_SEL_MASK, 0x0));
1522 	regmap_update_bits(hdptx->regmap, CMN_REG(0081),
1523 			   ANA_PLL_CD_HSCLK_EAST_EN_MASK | ANA_PLL_CD_HSCLK_WEST_EN_MASK,
1524 			   FIELD_PREP(ANA_PLL_CD_HSCLK_EAST_EN_MASK, 0x1) |
1525 			   FIELD_PREP(ANA_PLL_CD_HSCLK_WEST_EN_MASK, 0x0));
1526 
1527 	regmap_update_bits(hdptx->regmap, CMN_REG(0082), ANA_PLL_CD_VREG_GAIN_CTRL_MASK,
1528 			   FIELD_PREP(ANA_PLL_CD_VREG_GAIN_CTRL_MASK, 0x4));
1529 	regmap_update_bits(hdptx->regmap, CMN_REG(0083), ANA_PLL_CD_VREG_ICTRL_MASK,
1530 			   FIELD_PREP(ANA_PLL_CD_VREG_ICTRL_MASK, 0x1));
1531 	regmap_update_bits(hdptx->regmap, CMN_REG(0084), PLL_LCRO_CLK_SEL_MASK,
1532 			   FIELD_PREP(PLL_LCRO_CLK_SEL_MASK, 0x1));
1533 	regmap_update_bits(hdptx->regmap, CMN_REG(0085), ANA_PLL_SYNC_LOSS_DET_MODE_MASK,
1534 			   FIELD_PREP(ANA_PLL_SYNC_LOSS_DET_MODE_MASK, 0x3));
1535 
1536 	regmap_update_bits(hdptx->regmap, CMN_REG(0087), ANA_PLL_TX_HS_CLK_EN_MASK,
1537 			   FIELD_PREP(ANA_PLL_TX_HS_CLK_EN_MASK, 0x1));
1538 
1539 	regmap_update_bits(hdptx->regmap, CMN_REG(0097), DIG_CLK_SEL_MASK,
1540 			   FIELD_PREP(DIG_CLK_SEL_MASK, 0x1));
1541 
1542 	regmap_update_bits(hdptx->regmap, CMN_REG(0099), CMN_ROPLL_ALONE_MODE_MASK,
1543 			   FIELD_PREP(CMN_ROPLL_ALONE_MODE_MASK, 0x1));
1544 	regmap_update_bits(hdptx->regmap, CMN_REG(009a), HS_SPEED_SEL_MASK,
1545 			   FIELD_PREP(HS_SPEED_SEL_MASK, 0x1));
1546 	regmap_update_bits(hdptx->regmap, CMN_REG(009b), LS_SPEED_SEL_MASK,
1547 			   FIELD_PREP(LS_SPEED_SEL_MASK, 0x1));
1548 }
1549 
rk_hdptx_dp_aux_init(struct rk_hdptx_phy * hdptx)1550 static int rk_hdptx_dp_aux_init(struct rk_hdptx_phy *hdptx)
1551 {
1552 	u32 status;
1553 	int ret;
1554 
1555 	regmap_update_bits(hdptx->regmap, SB_REG(0102), ANA_SB_RXTERM_OFFSP_MASK,
1556 			   FIELD_PREP(ANA_SB_RXTERM_OFFSP_MASK, 0x3));
1557 	regmap_update_bits(hdptx->regmap, SB_REG(0103), ANA_SB_RXTERM_OFFSN_MASK,
1558 			   FIELD_PREP(ANA_SB_RXTERM_OFFSN_MASK, 0x3));
1559 	regmap_update_bits(hdptx->regmap, SB_REG(0104), SB_AUX_EN_MASK,
1560 			   FIELD_PREP(SB_AUX_EN_MASK, 0x1));
1561 	regmap_update_bits(hdptx->regmap, SB_REG(0105), ANA_SB_TX_HLVL_PROG_MASK,
1562 			   FIELD_PREP(ANA_SB_TX_HLVL_PROG_MASK, 0x7));
1563 	regmap_update_bits(hdptx->regmap, SB_REG(0106), ANA_SB_TX_LLVL_PROG_MASK,
1564 			   FIELD_PREP(ANA_SB_TX_LLVL_PROG_MASK, 0x7));
1565 
1566 	regmap_update_bits(hdptx->regmap, SB_REG(010d), ANA_SB_DMRX_LPBK_DATA_MASK,
1567 			   FIELD_PREP(ANA_SB_DMRX_LPBK_DATA_MASK, 0x1));
1568 
1569 	regmap_update_bits(hdptx->regmap, SB_REG(010f), ANA_SB_VREG_GAIN_CTRL_MASK,
1570 			   FIELD_PREP(ANA_SB_VREG_GAIN_CTRL_MASK, 0x0));
1571 	regmap_update_bits(hdptx->regmap, SB_REG(0110),
1572 			   ANA_SB_VREG_OUT_SEL_MASK | ANA_SB_VREG_REF_SEL_MASK,
1573 			   FIELD_PREP(ANA_SB_VREG_OUT_SEL_MASK, 0x1) |
1574 			   FIELD_PREP(ANA_SB_VREG_REF_SEL_MASK, 0x1));
1575 
1576 	regmap_update_bits(hdptx->regmap, SB_REG(0113),
1577 			   SB_RX_RCAL_OPT_CODE_MASK | SB_RX_RTERM_CTRL_MASK,
1578 			   FIELD_PREP(SB_RX_RCAL_OPT_CODE_MASK, 0x1) |
1579 			   FIELD_PREP(SB_RX_RTERM_CTRL_MASK, 0x3));
1580 	regmap_update_bits(hdptx->regmap, SB_REG(0114),
1581 			   SB_TG_SB_EN_DELAY_TIME_MASK | SB_TG_RXTERM_EN_DELAY_TIME_MASK,
1582 			   FIELD_PREP(SB_TG_SB_EN_DELAY_TIME_MASK, 0x2) |
1583 			   FIELD_PREP(SB_TG_RXTERM_EN_DELAY_TIME_MASK, 0x2));
1584 	regmap_update_bits(hdptx->regmap, SB_REG(0115),
1585 			   SB_READY_DELAY_TIME_MASK | SB_TG_OSC_EN_DELAY_TIME_MASK,
1586 			   FIELD_PREP(SB_READY_DELAY_TIME_MASK, 0x2) |
1587 			   FIELD_PREP(SB_TG_OSC_EN_DELAY_TIME_MASK, 0x2));
1588 	regmap_update_bits(hdptx->regmap, SB_REG(0116),
1589 			   AFC_RSTN_DELAY_TIME_MASK,
1590 			   FIELD_PREP(AFC_RSTN_DELAY_TIME_MASK, 0x2));
1591 	regmap_update_bits(hdptx->regmap, SB_REG(0117),
1592 			   FAST_PULSE_TIME_MASK,
1593 			   FIELD_PREP(FAST_PULSE_TIME_MASK, 0x4));
1594 	regmap_update_bits(hdptx->regmap, SB_REG(0118),
1595 			   SB_TG_EARC_DMRX_RECVRD_CLK_CNT_MASK,
1596 			   FIELD_PREP(SB_TG_EARC_DMRX_RECVRD_CLK_CNT_MASK, 0xa));
1597 
1598 	regmap_update_bits(hdptx->regmap, SB_REG(011a), SB_TG_CNT_RUN_NO_7_0_MASK,
1599 			   FIELD_PREP(SB_TG_CNT_RUN_NO_7_0_MASK, 0x3));
1600 	regmap_update_bits(hdptx->regmap, SB_REG(011b),
1601 			   SB_EARC_SIG_DET_BYPASS_MASK | SB_AFC_TOL_MASK,
1602 			   FIELD_PREP(SB_EARC_SIG_DET_BYPASS_MASK, 0x1) |
1603 			   FIELD_PREP(SB_AFC_TOL_MASK, 0x3));
1604 	regmap_update_bits(hdptx->regmap, SB_REG(011c), SB_AFC_STB_NUM_MASK,
1605 			   FIELD_PREP(SB_AFC_STB_NUM_MASK, 0x4));
1606 	regmap_update_bits(hdptx->regmap, SB_REG(011d), SB_TG_OSC_CNT_MIN_MASK,
1607 			   FIELD_PREP(SB_TG_OSC_CNT_MIN_MASK, 0x67));
1608 	regmap_update_bits(hdptx->regmap, SB_REG(011e), SB_TG_OSC_CNT_MAX_MASK,
1609 			   FIELD_PREP(SB_TG_OSC_CNT_MAX_MASK, 0x6a));
1610 	regmap_update_bits(hdptx->regmap, SB_REG(011f), SB_PWM_AFC_CTRL_MASK,
1611 			   FIELD_PREP(SB_PWM_AFC_CTRL_MASK, 0x5));
1612 	regmap_update_bits(hdptx->regmap, SB_REG(011f), SB_RCAL_RSTN_MASK,
1613 			   FIELD_PREP(SB_RCAL_RSTN_MASK, 0x1));
1614 	regmap_update_bits(hdptx->regmap, SB_REG(0120), SB_AUX_EN_IN_MASK,
1615 			   FIELD_PREP(SB_AUX_EN_IN_MASK, 0x1));
1616 
1617 	regmap_update_bits(hdptx->regmap, SB_REG(0102), OVRD_SB_RXTERM_EN_MASK,
1618 			   FIELD_PREP(OVRD_SB_RXTERM_EN_MASK, 0x1));
1619 	regmap_update_bits(hdptx->regmap, SB_REG(0103), OVRD_SB_RX_RESCAL_DONE_MASK,
1620 			   FIELD_PREP(OVRD_SB_RX_RESCAL_DONE_MASK, 0x1));
1621 	regmap_update_bits(hdptx->regmap, SB_REG(0104), OVRD_SB_EN_MASK,
1622 			   FIELD_PREP(OVRD_SB_EN_MASK, 0x1));
1623 	regmap_update_bits(hdptx->regmap, SB_REG(0104), OVRD_SB_AUX_EN_MASK,
1624 			   FIELD_PREP(OVRD_SB_AUX_EN_MASK, 0x1));
1625 
1626 	regmap_update_bits(hdptx->regmap, SB_REG(010f), OVRD_SB_VREG_EN_MASK,
1627 			   FIELD_PREP(OVRD_SB_VREG_EN_MASK, 0x1));
1628 
1629 	regmap_write(hdptx->grf, GRF_HDPTX_CON0,
1630 		     FIELD_PREP_WM16(HDPTX_I_BGR_EN, 1) |
1631 		     FIELD_PREP_WM16(HDPTX_I_BIAS_EN, 1));
1632 	usleep_range(20, 25);
1633 
1634 	reset_control_deassert(hdptx->rsts[RST_INIT].rstc);
1635 	usleep_range(20, 25);
1636 	reset_control_deassert(hdptx->rsts[RST_CMN].rstc);
1637 	usleep_range(20, 25);
1638 
1639 	regmap_update_bits(hdptx->regmap, SB_REG(0103), OVRD_SB_RX_RESCAL_DONE_MASK,
1640 			   FIELD_PREP(OVRD_SB_RX_RESCAL_DONE_MASK, 0x1));
1641 	usleep_range(100, 110);
1642 	regmap_update_bits(hdptx->regmap, SB_REG(0104), SB_EN_MASK,
1643 			   FIELD_PREP(SB_EN_MASK, 0x1));
1644 	usleep_range(100, 110);
1645 	regmap_update_bits(hdptx->regmap, SB_REG(0102), SB_RXTERM_EN_MASK,
1646 			   FIELD_PREP(SB_RXTERM_EN_MASK, 0x1));
1647 	usleep_range(20, 25);
1648 	regmap_update_bits(hdptx->regmap, SB_REG(010f), SB_VREG_EN_MASK,
1649 			   FIELD_PREP(SB_VREG_EN_MASK, 0x1));
1650 	usleep_range(20, 25);
1651 	regmap_update_bits(hdptx->regmap, SB_REG(0104), SB_AUX_EN_MASK,
1652 			   FIELD_PREP(SB_AUX_EN_MASK, 0x1));
1653 	usleep_range(100, 110);
1654 
1655 	ret = regmap_read_poll_timeout(hdptx->grf, GRF_HDPTX_STATUS,
1656 				       status, FIELD_GET(HDPTX_O_SB_RDY, status),
1657 				       50, 1000);
1658 	if (ret) {
1659 		dev_err(hdptx->dev, "Failed to get phy sb ready: %d\n", ret);
1660 		return ret;
1661 	}
1662 
1663 	return 0;
1664 }
1665 
rk_hdptx_phy_power_on(struct phy * phy)1666 static int rk_hdptx_phy_power_on(struct phy *phy)
1667 {
1668 	struct rk_hdptx_phy *hdptx = phy_get_drvdata(phy);
1669 	enum phy_mode mode = phy_get_mode(phy);
1670 	int ret, lane;
1671 
1672 	ret = rk_hdptx_phy_consumer_get(hdptx);
1673 	if (ret)
1674 		return ret;
1675 
1676 	if (mode == PHY_MODE_DP) {
1677 		regmap_write(hdptx->grf, GRF_HDPTX_CON0,
1678 			     FIELD_PREP_WM16(HDPTX_MODE_SEL, 1));
1679 
1680 		for (lane = 0; lane < 4; lane++) {
1681 			regmap_update_bits(hdptx->regmap, LANE_REG(031e) + 0x400 * lane,
1682 					   LN_POLARITY_INV_MASK | LN_LANE_MODE_MASK,
1683 					   FIELD_PREP(LN_POLARITY_INV_MASK, 0) |
1684 					   FIELD_PREP(LN_LANE_MODE_MASK, 1));
1685 		}
1686 
1687 		regmap_update_bits(hdptx->regmap, LNTOP_REG(0200), PROTOCOL_SEL_MASK,
1688 				   FIELD_PREP(PROTOCOL_SEL_MASK, 0x0));
1689 		regmap_update_bits(hdptx->regmap, LNTOP_REG(0206), DATA_BUS_WIDTH_MASK,
1690 				   FIELD_PREP(DATA_BUS_WIDTH_MASK, 0x1));
1691 		regmap_update_bits(hdptx->regmap, LNTOP_REG(0206), DATA_BUS_WIDTH_SEL_MASK,
1692 				   FIELD_PREP(DATA_BUS_WIDTH_SEL_MASK, 0x0));
1693 
1694 		rk_hdptx_dp_pll_init(hdptx);
1695 
1696 		ret = rk_hdptx_dp_aux_init(hdptx);
1697 	} else {
1698 		dev_dbg(hdptx->dev, "%s rate=%llu bpc=%u\n", __func__,
1699 			hdptx->hdmi_cfg.rate, hdptx->hdmi_cfg.bpc);
1700 
1701 		if (hdptx->pll_config_dirty)
1702 			ret = rk_hdptx_pll_cmn_config(hdptx);
1703 
1704 		if (!ret) {
1705 			regmap_write(hdptx->grf, GRF_HDPTX_CON0,
1706 				     FIELD_PREP_WM16(HDPTX_MODE_SEL, 0));
1707 
1708 			if (hdptx->hdmi_cfg.mode == PHY_HDMI_MODE_FRL)
1709 				ret = rk_hdptx_frl_lcpll_mode_config(hdptx);
1710 			else
1711 				ret = rk_hdptx_tmds_ropll_mode_config(hdptx);
1712 		}
1713 	}
1714 
1715 	if (ret)
1716 		rk_hdptx_phy_consumer_put(hdptx, true);
1717 
1718 	return ret;
1719 }
1720 
rk_hdptx_phy_power_off(struct phy * phy)1721 static int rk_hdptx_phy_power_off(struct phy *phy)
1722 {
1723 	struct rk_hdptx_phy *hdptx = phy_get_drvdata(phy);
1724 
1725 	return rk_hdptx_phy_consumer_put(hdptx, false);
1726 }
1727 
rk_hdptx_phy_verify_hdmi_config(struct rk_hdptx_phy * hdptx,struct phy_configure_opts_hdmi * hdmi_in,struct rk_hdptx_hdmi_cfg * hdmi_out)1728 static int rk_hdptx_phy_verify_hdmi_config(struct rk_hdptx_phy *hdptx,
1729 					   struct phy_configure_opts_hdmi *hdmi_in,
1730 					   struct rk_hdptx_hdmi_cfg *hdmi_out)
1731 {
1732 	int i;
1733 
1734 	if (hdptx->hdmi_cfg.mode == PHY_HDMI_MODE_FRL) {
1735 		unsigned long long frl_rate = 100000000ULL * hdmi_in->frl.lanes *
1736 					      hdmi_in->frl.rate_per_lane;
1737 
1738 		switch (hdmi_in->frl.rate_per_lane) {
1739 		case 3:
1740 		case 6:
1741 		case 8:
1742 		case 10:
1743 		case 12:
1744 			break;
1745 		default:
1746 			return -EINVAL;
1747 		}
1748 
1749 		if (!hdmi_in->frl.lanes || hdmi_in->frl.lanes > 4)
1750 			return -EINVAL;
1751 
1752 		if (frl_rate != FRL_8G4L_RATE) {
1753 			for (i = 0; i < ARRAY_SIZE(rk_hdptx_frl_lcpll_cfg); i++)
1754 				if (frl_rate == rk_hdptx_frl_lcpll_cfg[i].rate)
1755 					break;
1756 			if (i == ARRAY_SIZE(rk_hdptx_frl_lcpll_cfg))
1757 				return -EINVAL;
1758 		}
1759 
1760 		if (hdmi_out)
1761 			hdmi_out->rate = frl_rate;
1762 	} else {
1763 		if (!hdmi_in->tmds_char_rate || hdmi_in->tmds_char_rate > HDMI20_MAX_RATE)
1764 			return -EINVAL;
1765 
1766 		for (i = 0; i < ARRAY_SIZE(rk_hdptx_tmds_ropll_cfg); i++)
1767 			if (hdmi_in->tmds_char_rate == rk_hdptx_tmds_ropll_cfg[i].rate)
1768 				break;
1769 
1770 		if (i == ARRAY_SIZE(rk_hdptx_tmds_ropll_cfg) &&
1771 		    !rk_hdptx_phy_clk_pll_calc(hdmi_in->tmds_char_rate, NULL))
1772 			return -EINVAL;
1773 
1774 		if (hdmi_out)
1775 			hdmi_out->rate = hdmi_in->tmds_char_rate;
1776 	}
1777 
1778 	switch (hdmi_in->bpc) {
1779 	case 0:
1780 	case 8:
1781 	case 10:
1782 	case 12:
1783 	case 16:
1784 		break;
1785 	default:
1786 		return -EINVAL;
1787 	}
1788 
1789 	if (hdmi_out)
1790 		hdmi_out->bpc = hdmi_in->bpc ?: 8;
1791 
1792 	return 0;
1793 }
1794 
rk_hdptx_phy_verify_dp_config(struct rk_hdptx_phy * hdptx,struct phy_configure_opts_dp * dp)1795 static int rk_hdptx_phy_verify_dp_config(struct rk_hdptx_phy *hdptx,
1796 					 struct phy_configure_opts_dp *dp)
1797 {
1798 	int i;
1799 
1800 	if (dp->set_rate) {
1801 		switch (dp->link_rate) {
1802 		case 1620:
1803 		case 2700:
1804 		case 5400:
1805 			break;
1806 		default:
1807 			return -EINVAL;
1808 		}
1809 	}
1810 
1811 	if (dp->set_lanes) {
1812 		switch (dp->lanes) {
1813 		case 1:
1814 		case 2:
1815 		case 4:
1816 			break;
1817 		default:
1818 			return -EINVAL;
1819 		}
1820 	}
1821 
1822 	if (dp->set_voltages) {
1823 		for (i = 0; i < hdptx->lanes; i++) {
1824 			if (dp->voltage[i] > 3 || dp->pre[i] > 3)
1825 				return -EINVAL;
1826 
1827 			if (dp->voltage[i] + dp->pre[i] > 3)
1828 				return -EINVAL;
1829 		}
1830 	}
1831 
1832 	return 0;
1833 }
1834 
rk_hdptx_phy_set_rate(struct rk_hdptx_phy * hdptx,struct phy_configure_opts_dp * dp)1835 static int rk_hdptx_phy_set_rate(struct rk_hdptx_phy *hdptx,
1836 				 struct phy_configure_opts_dp *dp)
1837 {
1838 	u32 bw, status;
1839 	int ret;
1840 
1841 	regmap_write(hdptx->grf, GRF_HDPTX_CON0, FIELD_PREP_WM16(HDPTX_I_PLL_EN, 0));
1842 
1843 	switch (dp->link_rate) {
1844 	case 1620:
1845 		bw = DP_BW_RBR;
1846 		break;
1847 	case 2700:
1848 		bw = DP_BW_HBR;
1849 		break;
1850 	case 5400:
1851 		bw = DP_BW_HBR2;
1852 		break;
1853 	default:
1854 		return -EINVAL;
1855 	}
1856 	hdptx->link_rate = dp->link_rate;
1857 
1858 	regmap_update_bits(hdptx->regmap, CMN_REG(0008), OVRD_LCPLL_EN_MASK | LCPLL_EN_MASK,
1859 			   FIELD_PREP(OVRD_LCPLL_EN_MASK, 0x1) |
1860 			   FIELD_PREP(LCPLL_EN_MASK, 0x0));
1861 
1862 	regmap_update_bits(hdptx->regmap, CMN_REG(003d), OVRD_ROPLL_EN_MASK | ROPLL_EN_MASK,
1863 			   FIELD_PREP(OVRD_ROPLL_EN_MASK, 0x1) |
1864 			   FIELD_PREP(ROPLL_EN_MASK, 0x1));
1865 
1866 	if (dp->ssc) {
1867 		regmap_update_bits(hdptx->regmap, CMN_REG(0074),
1868 				   OVRD_ROPLL_SSC_EN_MASK | ROPLL_SSC_EN_MASK,
1869 				   FIELD_PREP(OVRD_ROPLL_SSC_EN_MASK, 0x1) |
1870 				   FIELD_PREP(ROPLL_SSC_EN_MASK, 0x1));
1871 		regmap_write(hdptx->regmap, CMN_REG(0075),
1872 			     FIELD_PREP(ANA_ROPLL_SSC_FM_DEVIATION_MASK, 0xc));
1873 		regmap_update_bits(hdptx->regmap, CMN_REG(0076),
1874 				   ANA_ROPLL_SSC_FM_FREQ_MASK,
1875 				   FIELD_PREP(ANA_ROPLL_SSC_FM_FREQ_MASK, 0x1f));
1876 
1877 		regmap_update_bits(hdptx->regmap, CMN_REG(0099), SSC_EN_MASK,
1878 				   FIELD_PREP(SSC_EN_MASK, 0x2));
1879 	} else {
1880 		regmap_update_bits(hdptx->regmap, CMN_REG(0074),
1881 				   OVRD_ROPLL_SSC_EN_MASK | ROPLL_SSC_EN_MASK,
1882 				   FIELD_PREP(OVRD_ROPLL_SSC_EN_MASK, 0x1) |
1883 				   FIELD_PREP(ROPLL_SSC_EN_MASK, 0x0));
1884 		regmap_write(hdptx->regmap, CMN_REG(0075),
1885 			     FIELD_PREP(ANA_ROPLL_SSC_FM_DEVIATION_MASK, 0x20));
1886 		regmap_update_bits(hdptx->regmap, CMN_REG(0076),
1887 				   ANA_ROPLL_SSC_FM_FREQ_MASK,
1888 				   FIELD_PREP(ANA_ROPLL_SSC_FM_FREQ_MASK, 0xc));
1889 
1890 		regmap_update_bits(hdptx->regmap, CMN_REG(0099), SSC_EN_MASK,
1891 				   FIELD_PREP(SSC_EN_MASK, 0x0));
1892 	}
1893 
1894 	regmap_update_bits(hdptx->regmap, CMN_REG(0095), DP_TX_LINK_BW_MASK,
1895 			   FIELD_PREP(DP_TX_LINK_BW_MASK, bw));
1896 
1897 	regmap_write(hdptx->grf, GRF_HDPTX_CON0, FIELD_PREP_WM16(HDPTX_I_PLL_EN, 1));
1898 
1899 	ret = regmap_read_poll_timeout(hdptx->grf, GRF_HDPTX_STATUS,
1900 				       status, FIELD_GET(HDPTX_O_PLL_LOCK_DONE, status),
1901 				       50, 1000);
1902 	if (ret) {
1903 		dev_err(hdptx->dev, "Failed to get phy pll lock: %d\n", ret);
1904 		return ret;
1905 	}
1906 
1907 	return 0;
1908 }
1909 
rk_hdptx_phy_set_lanes(struct rk_hdptx_phy * hdptx,struct phy_configure_opts_dp * dp)1910 static int rk_hdptx_phy_set_lanes(struct rk_hdptx_phy *hdptx,
1911 				  struct phy_configure_opts_dp *dp)
1912 {
1913 	hdptx->lanes = dp->lanes;
1914 
1915 	regmap_update_bits(hdptx->regmap, LNTOP_REG(0207), LANE_EN_MASK,
1916 			   FIELD_PREP(LANE_EN_MASK, GENMASK(hdptx->lanes - 1, 0)));
1917 
1918 	return 0;
1919 }
1920 
rk_hdptx_phy_set_voltage(struct rk_hdptx_phy * hdptx,struct phy_configure_opts_dp * dp,u8 lane)1921 static void rk_hdptx_phy_set_voltage(struct rk_hdptx_phy *hdptx,
1922 				     struct phy_configure_opts_dp *dp,
1923 				     u8 lane)
1924 {
1925 	const struct tx_drv_ctrl *ctrl;
1926 	u32 offset = lane * 0x400;
1927 
1928 	switch (hdptx->link_rate) {
1929 	case 1620:
1930 		ctrl = &tx_drv_ctrl_rbr[dp->voltage[lane]][dp->pre[lane]];
1931 		regmap_update_bits(hdptx->regmap, LANE_REG(030a) + offset,
1932 				   LN_TX_JEQ_EVEN_CTRL_RBR_MASK,
1933 				   FIELD_PREP(LN_TX_JEQ_EVEN_CTRL_RBR_MASK,
1934 					      ctrl->tx_jeq_even_ctrl));
1935 		regmap_update_bits(hdptx->regmap, LANE_REG(030c) + offset,
1936 				   LN_TX_JEQ_ODD_CTRL_RBR_MASK,
1937 				   FIELD_PREP(LN_TX_JEQ_ODD_CTRL_RBR_MASK,
1938 					      ctrl->tx_jeq_odd_ctrl));
1939 		regmap_update_bits(hdptx->regmap, LANE_REG(0311) + offset,
1940 				   LN_TX_SER_40BIT_EN_RBR_MASK,
1941 				   FIELD_PREP(LN_TX_SER_40BIT_EN_RBR_MASK, 0x1));
1942 		break;
1943 	case 2700:
1944 		ctrl = &tx_drv_ctrl_hbr[dp->voltage[lane]][dp->pre[lane]];
1945 		regmap_update_bits(hdptx->regmap, LANE_REG(030b) + offset,
1946 				   LN_TX_JEQ_EVEN_CTRL_HBR_MASK,
1947 				   FIELD_PREP(LN_TX_JEQ_EVEN_CTRL_HBR_MASK,
1948 					      ctrl->tx_jeq_even_ctrl));
1949 		regmap_update_bits(hdptx->regmap, LANE_REG(030d) + offset,
1950 				   LN_TX_JEQ_ODD_CTRL_HBR_MASK,
1951 				   FIELD_PREP(LN_TX_JEQ_ODD_CTRL_HBR_MASK,
1952 					      ctrl->tx_jeq_odd_ctrl));
1953 		regmap_update_bits(hdptx->regmap, LANE_REG(0311) + offset,
1954 				   LN_TX_SER_40BIT_EN_HBR_MASK,
1955 				   FIELD_PREP(LN_TX_SER_40BIT_EN_HBR_MASK, 0x1));
1956 		break;
1957 	case 5400:
1958 	default:
1959 		ctrl = &tx_drv_ctrl_hbr2[dp->voltage[lane]][dp->pre[lane]];
1960 		regmap_update_bits(hdptx->regmap, LANE_REG(030b) + offset,
1961 				   LN_TX_JEQ_EVEN_CTRL_HBR2_MASK,
1962 				   FIELD_PREP(LN_TX_JEQ_EVEN_CTRL_HBR2_MASK,
1963 					      ctrl->tx_jeq_even_ctrl));
1964 		regmap_update_bits(hdptx->regmap, LANE_REG(030d) + offset,
1965 				   LN_TX_JEQ_ODD_CTRL_HBR2_MASK,
1966 				   FIELD_PREP(LN_TX_JEQ_ODD_CTRL_HBR2_MASK,
1967 					      ctrl->tx_jeq_odd_ctrl));
1968 		regmap_update_bits(hdptx->regmap, LANE_REG(0311) + offset,
1969 				   LN_TX_SER_40BIT_EN_HBR2_MASK,
1970 				   FIELD_PREP(LN_TX_SER_40BIT_EN_HBR2_MASK, 0x1));
1971 		break;
1972 	}
1973 
1974 	regmap_update_bits(hdptx->regmap, LANE_REG(0303) + offset,
1975 			   OVRD_LN_TX_DRV_LVL_CTRL_MASK | LN_TX_DRV_LVL_CTRL_MASK,
1976 			   FIELD_PREP(OVRD_LN_TX_DRV_LVL_CTRL_MASK, 0x1) |
1977 			   FIELD_PREP(LN_TX_DRV_LVL_CTRL_MASK,
1978 				      ctrl->tx_drv_lvl_ctrl));
1979 	regmap_update_bits(hdptx->regmap, LANE_REG(0304) + offset,
1980 			   OVRD_LN_TX_DRV_POST_LVL_CTRL_MASK |
1981 			   LN_TX_DRV_POST_LVL_CTRL_MASK,
1982 			   FIELD_PREP(OVRD_LN_TX_DRV_POST_LVL_CTRL_MASK, 0x1) |
1983 			   FIELD_PREP(LN_TX_DRV_POST_LVL_CTRL_MASK,
1984 				      ctrl->tx_drv_post_lvl_ctrl));
1985 	regmap_update_bits(hdptx->regmap, LANE_REG(0305) + offset,
1986 			   OVRD_LN_TX_DRV_PRE_LVL_CTRL_MASK |
1987 			   LN_TX_DRV_PRE_LVL_CTRL_MASK,
1988 			   FIELD_PREP(OVRD_LN_TX_DRV_PRE_LVL_CTRL_MASK, 0x1) |
1989 			   FIELD_PREP(LN_TX_DRV_PRE_LVL_CTRL_MASK,
1990 				      ctrl->tx_drv_pre_lvl_ctrl));
1991 	regmap_update_bits(hdptx->regmap, LANE_REG(0306) + offset,
1992 			   LN_ANA_TX_DRV_IDRV_IDN_CTRL_MASK |
1993 			   LN_ANA_TX_DRV_IDRV_IUP_CTRL_MASK |
1994 			   LN_ANA_TX_DRV_ACCDRV_EN_MASK,
1995 			   FIELD_PREP(LN_ANA_TX_DRV_IDRV_IDN_CTRL_MASK,
1996 				      ctrl->ana_tx_drv_idrv_idn_ctrl) |
1997 			   FIELD_PREP(LN_ANA_TX_DRV_IDRV_IUP_CTRL_MASK,
1998 				      ctrl->ana_tx_drv_idrv_iup_ctrl) |
1999 			   FIELD_PREP(LN_ANA_TX_DRV_ACCDRV_EN_MASK,
2000 				      ctrl->ana_tx_drv_accdrv_en));
2001 	regmap_update_bits(hdptx->regmap, LANE_REG(0307) + offset,
2002 			   LN_ANA_TX_DRV_ACCDRV_POL_SEL_MASK |
2003 			   LN_ANA_TX_DRV_ACCDRV_CTRL_MASK,
2004 			   FIELD_PREP(LN_ANA_TX_DRV_ACCDRV_POL_SEL_MASK, 0x1) |
2005 			   FIELD_PREP(LN_ANA_TX_DRV_ACCDRV_CTRL_MASK,
2006 				      ctrl->ana_tx_drv_accdrv_ctrl));
2007 
2008 	regmap_update_bits(hdptx->regmap, LANE_REG(030a) + offset,
2009 			   LN_ANA_TX_JEQ_EN_MASK,
2010 			   FIELD_PREP(LN_ANA_TX_JEQ_EN_MASK, ctrl->ana_tx_jeq_en));
2011 
2012 	regmap_update_bits(hdptx->regmap, LANE_REG(0310) + offset,
2013 			   LN_ANA_TX_SYNC_LOSS_DET_MODE_MASK,
2014 			   FIELD_PREP(LN_ANA_TX_SYNC_LOSS_DET_MODE_MASK, 0x3));
2015 
2016 	regmap_update_bits(hdptx->regmap, LANE_REG(0316) + offset,
2017 			   LN_ANA_TX_SER_VREG_GAIN_CTRL_MASK,
2018 			   FIELD_PREP(LN_ANA_TX_SER_VREG_GAIN_CTRL_MASK, 0x2));
2019 
2020 	regmap_update_bits(hdptx->regmap, LANE_REG(031b) + offset,
2021 			   LN_ANA_TX_RESERVED_MASK,
2022 			   FIELD_PREP(LN_ANA_TX_RESERVED_MASK, 0x1));
2023 }
2024 
rk_hdptx_phy_set_voltages(struct rk_hdptx_phy * hdptx,struct phy_configure_opts_dp * dp)2025 static int rk_hdptx_phy_set_voltages(struct rk_hdptx_phy *hdptx,
2026 				     struct phy_configure_opts_dp *dp)
2027 {
2028 	u8 lane;
2029 	u32 status;
2030 	int ret;
2031 
2032 	for (lane = 0; lane < hdptx->lanes; lane++)
2033 		rk_hdptx_phy_set_voltage(hdptx, dp, lane);
2034 
2035 	reset_control_deassert(hdptx->rsts[RST_LANE].rstc);
2036 
2037 	ret = regmap_read_poll_timeout(hdptx->grf, GRF_HDPTX_STATUS,
2038 				       status, FIELD_GET(HDPTX_O_PHY_RDY, status),
2039 				       50, 5000);
2040 	if (ret) {
2041 		dev_err(hdptx->dev, "Failed to get phy ready: %d\n", ret);
2042 		return ret;
2043 	}
2044 
2045 	return 0;
2046 }
2047 
rk_hdptx_phy_set_mode(struct phy * phy,enum phy_mode mode,int submode)2048 static int rk_hdptx_phy_set_mode(struct phy *phy, enum phy_mode mode, int submode)
2049 {
2050 	struct rk_hdptx_phy *hdptx = phy_get_drvdata(phy);
2051 
2052 	if (mode == PHY_MODE_DP)
2053 		return 0;
2054 
2055 	if (mode != PHY_MODE_HDMI) {
2056 		dev_err(&phy->dev, "invalid PHY mode: %d\n", mode);
2057 		return -EINVAL;
2058 	}
2059 
2060 	switch (submode) {
2061 	case PHY_HDMI_MODE_TMDS:
2062 	case PHY_HDMI_MODE_FRL:
2063 		hdptx->hdmi_cfg.mode = submode;
2064 		break;
2065 	default:
2066 		dev_err(&phy->dev, "invalid HDMI mode: %d\n", submode);
2067 		return -EINVAL;
2068 	}
2069 
2070 	return 0;
2071 }
2072 
rk_hdptx_phy_configure(struct phy * phy,union phy_configure_opts * opts)2073 static int rk_hdptx_phy_configure(struct phy *phy, union phy_configure_opts *opts)
2074 {
2075 	struct rk_hdptx_phy *hdptx = phy_get_drvdata(phy);
2076 	enum phy_mode mode = phy_get_mode(phy);
2077 	int ret;
2078 
2079 	if (mode != PHY_MODE_DP) {
2080 		ret = rk_hdptx_phy_verify_hdmi_config(hdptx, &opts->hdmi, &hdptx->hdmi_cfg);
2081 		if (ret) {
2082 			dev_err(hdptx->dev, "invalid hdmi params for phy configure\n");
2083 		} else {
2084 			hdptx->pll_config_dirty = true;
2085 
2086 			dev_dbg(hdptx->dev, "%s %s rate=%llu bpc=%u\n", __func__,
2087 				hdptx->hdmi_cfg.mode ? "FRL" : "TMDS",
2088 				hdptx->hdmi_cfg.rate, hdptx->hdmi_cfg.bpc);
2089 		}
2090 
2091 		return ret;
2092 	}
2093 
2094 	ret = rk_hdptx_phy_verify_dp_config(hdptx, &opts->dp);
2095 	if (ret) {
2096 		dev_err(hdptx->dev, "invalid dp params for phy configure\n");
2097 		return ret;
2098 	}
2099 
2100 	if (opts->dp.set_rate) {
2101 		ret = rk_hdptx_phy_set_rate(hdptx, &opts->dp);
2102 		if (ret) {
2103 			dev_err(hdptx->dev, "failed to set rate: %d\n", ret);
2104 			return ret;
2105 		}
2106 	}
2107 
2108 	if (opts->dp.set_lanes) {
2109 		ret = rk_hdptx_phy_set_lanes(hdptx, &opts->dp);
2110 		if (ret) {
2111 			dev_err(hdptx->dev, "failed to set lanes: %d\n", ret);
2112 			return ret;
2113 		}
2114 	}
2115 
2116 	if (opts->dp.set_voltages) {
2117 		ret = rk_hdptx_phy_set_voltages(hdptx, &opts->dp);
2118 		if (ret) {
2119 			dev_err(hdptx->dev, "failed to set voltages: %d\n",
2120 				ret);
2121 			return ret;
2122 		}
2123 	}
2124 
2125 	return 0;
2126 }
2127 
rk_hdptx_phy_validate(struct phy * phy,enum phy_mode mode,int submode,union phy_configure_opts * opts)2128 static int rk_hdptx_phy_validate(struct phy *phy, enum phy_mode mode,
2129 				 int submode, union phy_configure_opts *opts)
2130 {
2131 	struct rk_hdptx_phy *hdptx = phy_get_drvdata(phy);
2132 
2133 	if (mode != PHY_MODE_DP)
2134 		return rk_hdptx_phy_verify_hdmi_config(hdptx, &opts->hdmi, NULL);
2135 
2136 	return rk_hdptx_phy_verify_dp_config(hdptx, &opts->dp);
2137 }
2138 
2139 static const struct phy_ops rk_hdptx_phy_ops = {
2140 	.power_on  = rk_hdptx_phy_power_on,
2141 	.power_off = rk_hdptx_phy_power_off,
2142 	.set_mode = rk_hdptx_phy_set_mode,
2143 	.configure = rk_hdptx_phy_configure,
2144 	.validate  = rk_hdptx_phy_validate,
2145 	.owner	   = THIS_MODULE,
2146 };
2147 
to_rk_hdptx_phy(struct clk_hw * hw)2148 static struct rk_hdptx_phy *to_rk_hdptx_phy(struct clk_hw *hw)
2149 {
2150 	return container_of(hw, struct rk_hdptx_phy, hw);
2151 }
2152 
rk_hdptx_phy_clk_prepare(struct clk_hw * hw)2153 static int rk_hdptx_phy_clk_prepare(struct clk_hw *hw)
2154 {
2155 	struct rk_hdptx_phy *hdptx = to_rk_hdptx_phy(hw);
2156 
2157 	return rk_hdptx_phy_consumer_get(hdptx);
2158 }
2159 
rk_hdptx_phy_clk_unprepare(struct clk_hw * hw)2160 static void rk_hdptx_phy_clk_unprepare(struct clk_hw *hw)
2161 {
2162 	struct rk_hdptx_phy *hdptx = to_rk_hdptx_phy(hw);
2163 
2164 	rk_hdptx_phy_consumer_put(hdptx, true);
2165 }
2166 
2167 #define PLL_REF_CLK 24000000ULL
2168 
rk_hdptx_phy_clk_calc_rate_from_pll_cfg(struct rk_hdptx_phy * hdptx)2169 static u64 rk_hdptx_phy_clk_calc_rate_from_pll_cfg(struct rk_hdptx_phy *hdptx)
2170 {
2171 	struct lcpll_config lcpll_hw;
2172 	struct ropll_config ropll_hw;
2173 	u64 fout, sdm;
2174 	u32 mode, bpc, val;
2175 	int ret, i;
2176 
2177 	ret = regmap_read(hdptx->regmap, CMN_REG(0008), &mode);
2178 	if (ret)
2179 		return 0;
2180 
2181 	if (mode & LCPLL_LCVCO_MODE_EN_MASK) {
2182 		ret = regmap_read(hdptx->regmap, CMN_REG(0020), &val);
2183 		if (ret)
2184 			return 0;
2185 		lcpll_hw.pms_mdiv = val;
2186 
2187 		ret = regmap_read(hdptx->regmap, CMN_REG(0023), &val);
2188 		if (ret)
2189 			return 0;
2190 		lcpll_hw.pms_sdiv = FIELD_GET(LCPLL_PMS_SDIV_HBR_MASK, val);
2191 
2192 		ret = regmap_read(hdptx->regmap, CMN_REG(002B), &val);
2193 		if (ret)
2194 			return 0;
2195 		lcpll_hw.sdm_num_sign = val;
2196 
2197 		ret = regmap_read(hdptx->regmap, CMN_REG(002C), &val);
2198 		if (ret)
2199 			return 0;
2200 		lcpll_hw.sdm_num = val;
2201 
2202 		ret = regmap_read(hdptx->regmap, CMN_REG(002A), &val);
2203 		if (ret)
2204 			return 0;
2205 		lcpll_hw.sdm_deno = val;
2206 
2207 		ret = regmap_read(hdptx->regmap, CMN_REG(002D), &val);
2208 		if (ret)
2209 			return 0;
2210 		lcpll_hw.sdc_n = FIELD_GET(LCPLL_SDC_N_MASK, val);
2211 
2212 		ret = regmap_read(hdptx->grf, GRF_HDPTX_CON0, &val);
2213 		if (ret)
2214 			return 0;
2215 
2216 		if (val & LC_REF_CLK_SEL) {
2217 			if (lcpll_hw.pms_mdiv == 0x6b &&
2218 			    lcpll_hw.sdm_num_sign == 0x01 &&
2219 			    lcpll_hw.sdm_num == 0x02 &&
2220 			    lcpll_hw.sdm_deno == 0x09 &&
2221 			    lcpll_hw.sdc_n == FIELD_GET(LCPLL_SDC_N_MASK, 0x02))
2222 				return FRL_8G4L_RATE;
2223 		} else {
2224 			const struct lcpll_config *cfg;
2225 
2226 			for (i = 0; i < ARRAY_SIZE(rk_hdptx_frl_lcpll_cfg); i++) {
2227 				cfg = &rk_hdptx_frl_lcpll_cfg[i];
2228 
2229 				if (cfg->pms_mdiv == lcpll_hw.pms_mdiv &&
2230 				    cfg->pms_sdiv == lcpll_hw.pms_sdiv &&
2231 				    cfg->sdm_num_sign == lcpll_hw.sdm_num_sign &&
2232 				    cfg->sdm_num == lcpll_hw.sdm_num &&
2233 				    cfg->sdm_deno == lcpll_hw.sdm_deno &&
2234 				    cfg->sdc_n == lcpll_hw.sdc_n)
2235 					return cfg->rate;
2236 			}
2237 		}
2238 
2239 		dev_dbg(hdptx->dev, "%s no FRL match found\n", __func__);
2240 		return 0;
2241 	}
2242 
2243 	ret = regmap_read(hdptx->regmap, CMN_REG(0051), &val);
2244 	if (ret)
2245 		return 0;
2246 	ropll_hw.pms_mdiv = val;
2247 
2248 	ret = regmap_read(hdptx->regmap, CMN_REG(005E), &val);
2249 	if (ret)
2250 		return 0;
2251 	ropll_hw.sdm_en = FIELD_GET(ROPLL_SDM_EN_MASK, val);
2252 
2253 	ret = regmap_read(hdptx->regmap, CMN_REG(0064), &val);
2254 	if (ret)
2255 		return 0;
2256 	ropll_hw.sdm_num_sign = FIELD_GET(ROPLL_SDM_NUM_SIGN_RBR_MASK, val);
2257 
2258 	ret = regmap_read(hdptx->regmap, CMN_REG(0065), &val);
2259 	if (ret)
2260 		return 0;
2261 	ropll_hw.sdm_num = val;
2262 
2263 	ret = regmap_read(hdptx->regmap, CMN_REG(0060), &val);
2264 	if (ret)
2265 		return 0;
2266 	ropll_hw.sdm_deno = val;
2267 
2268 	ret = regmap_read(hdptx->regmap, CMN_REG(0069), &val);
2269 	if (ret)
2270 		return 0;
2271 	ropll_hw.sdc_n = FIELD_GET(ROPLL_SDC_N_RBR_MASK, val) + 3;
2272 
2273 	ret = regmap_read(hdptx->regmap, CMN_REG(006c), &val);
2274 	if (ret)
2275 		return 0;
2276 	ropll_hw.sdc_num = val;
2277 
2278 	ret = regmap_read(hdptx->regmap, CMN_REG(0070), &val);
2279 	if (ret)
2280 		return 0;
2281 	ropll_hw.sdc_deno = val;
2282 
2283 	ret = regmap_read(hdptx->regmap, CMN_REG(0086), &val);
2284 	if (ret)
2285 		return 0;
2286 	ropll_hw.pms_sdiv = FIELD_GET(PLL_PCG_POSTDIV_SEL_MASK, val) + 1;
2287 	bpc = (FIELD_GET(PLL_PCG_CLK_SEL_MASK, val) << 1) + 8;
2288 
2289 	fout = PLL_REF_CLK * ropll_hw.pms_mdiv;
2290 	if (ropll_hw.sdm_en) {
2291 		val = 16U * ropll_hw.sdm_deno *
2292 		      (ropll_hw.sdc_deno * ropll_hw.sdc_n - ropll_hw.sdc_num);
2293 		if (!val) {
2294 			/*
2295 			 * The PLL config currently stored in hardware can't be
2296 			 * translated into a rate. The next .set_rate() should
2297 			 * program a valid configuration and help with recovery.
2298 			 */
2299 			dev_dbg(hdptx->dev, "Invalid ROPLL hw state: deno == 0\n");
2300 			return 0;
2301 		}
2302 
2303 		sdm = div_u64(PLL_REF_CLK * ropll_hw.sdc_deno *
2304 			      ropll_hw.pms_mdiv * ropll_hw.sdm_num, val);
2305 
2306 		if (ropll_hw.sdm_num_sign) {
2307 			if (sdm > fout) {
2308 				/*
2309 				 * Similarly to the case above, it is expected
2310 				 * the next .set_rate() will help with recovery.
2311 				 */
2312 				dev_dbg(hdptx->dev, "Invalid ROPLL hw state: sdm > fout\n");
2313 				return 0;
2314 			}
2315 
2316 			fout = fout - sdm;
2317 		} else {
2318 			fout = fout + sdm;
2319 		}
2320 	}
2321 
2322 	return DIV_ROUND_CLOSEST_ULL(fout * 2 * 8, ropll_hw.pms_sdiv * 10 * bpc);
2323 }
2324 
rk_hdptx_phy_clk_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)2325 static unsigned long rk_hdptx_phy_clk_recalc_rate(struct clk_hw *hw,
2326 						  unsigned long parent_rate)
2327 {
2328 	struct rk_hdptx_phy *hdptx = to_rk_hdptx_phy(hw);
2329 	u32 status;
2330 	int ret;
2331 
2332 	ret = regmap_read(hdptx->grf, GRF_HDPTX_CON0, &status);
2333 	if (ret || !(status & HDPTX_I_PLL_EN))
2334 		return 0;
2335 
2336 	return rk_hdptx_phy_clk_calc_rate_from_pll_cfg(hdptx);
2337 }
2338 
rk_hdptx_phy_clk_determine_rate(struct clk_hw * hw,struct clk_rate_request * req)2339 static int rk_hdptx_phy_clk_determine_rate(struct clk_hw *hw,
2340 					   struct clk_rate_request *req)
2341 {
2342 	struct rk_hdptx_phy *hdptx = to_rk_hdptx_phy(hw);
2343 
2344 	/*
2345 	 * For uncommitted PLL configuration, invalidate the current clock rate
2346 	 * to ensure rk_hdptx_phy_clk_set_rate() will be always invoked.
2347 	 * Otherwise, restrict the rate according to the PHY link setup.
2348 	 */
2349 	if (hdptx->pll_config_dirty)
2350 		req->rate = 0;
2351 	else if (hdptx->hdmi_cfg.mode == PHY_HDMI_MODE_FRL)
2352 		req->rate = hdptx->hdmi_cfg.rate;
2353 	else
2354 		req->rate = DIV_ROUND_CLOSEST_ULL(hdptx->hdmi_cfg.rate * 8,
2355 						  hdptx->hdmi_cfg.bpc);
2356 
2357 	return 0;
2358 }
2359 
rk_hdptx_phy_clk_set_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)2360 static int rk_hdptx_phy_clk_set_rate(struct clk_hw *hw, unsigned long rate,
2361 				     unsigned long parent_rate)
2362 {
2363 	struct rk_hdptx_phy *hdptx = to_rk_hdptx_phy(hw);
2364 
2365 	/*
2366 	 * The link rate would be normally programmed in HW during
2367 	 * phy_ops.power_on() or clk_ops.prepare() callbacks, but it might
2368 	 * happen that the former gets fired too late, i.e. after this call,
2369 	 * while the latter being executed only once, i.e. when clock remains
2370 	 * in the prepared state during rate changes.
2371 	 */
2372 	return rk_hdptx_pll_cmn_config(hdptx);
2373 }
2374 
2375 static const struct clk_ops hdptx_phy_clk_ops = {
2376 	.prepare = rk_hdptx_phy_clk_prepare,
2377 	.unprepare = rk_hdptx_phy_clk_unprepare,
2378 	.recalc_rate = rk_hdptx_phy_clk_recalc_rate,
2379 	.determine_rate = rk_hdptx_phy_clk_determine_rate,
2380 	.set_rate = rk_hdptx_phy_clk_set_rate,
2381 };
2382 
rk_hdptx_phy_clk_register(struct rk_hdptx_phy * hdptx)2383 static int rk_hdptx_phy_clk_register(struct rk_hdptx_phy *hdptx)
2384 {
2385 	struct device *dev = hdptx->dev;
2386 	const char *name, *pname;
2387 	struct clk *refclk;
2388 	int ret;
2389 
2390 	refclk = devm_clk_get(dev, "ref");
2391 	if (IS_ERR(refclk))
2392 		return dev_err_probe(dev, PTR_ERR(refclk),
2393 				     "Failed to get ref clock\n");
2394 
2395 	name = hdptx->phy_id > 0 ? "clk_hdmiphy_pixel1" : "clk_hdmiphy_pixel0";
2396 	pname = __clk_get_name(refclk);
2397 
2398 	hdptx->hw.init = CLK_HW_INIT(name, pname, &hdptx_phy_clk_ops,
2399 				     CLK_GET_RATE_NOCACHE);
2400 
2401 	ret = devm_clk_hw_register(dev, &hdptx->hw);
2402 	if (ret)
2403 		return dev_err_probe(dev, ret, "Failed to register clock\n");
2404 
2405 	ret = devm_of_clk_add_hw_provider(dev, of_clk_hw_simple_get, &hdptx->hw);
2406 	if (ret)
2407 		return dev_err_probe(dev, ret,
2408 				     "Failed to register clk provider\n");
2409 	return 0;
2410 }
2411 
rk_hdptx_phy_runtime_suspend(struct device * dev)2412 static int rk_hdptx_phy_runtime_suspend(struct device *dev)
2413 {
2414 	struct rk_hdptx_phy *hdptx = dev_get_drvdata(dev);
2415 
2416 	clk_bulk_disable_unprepare(hdptx->nr_clks, hdptx->clks);
2417 
2418 	return 0;
2419 }
2420 
rk_hdptx_phy_runtime_resume(struct device * dev)2421 static int rk_hdptx_phy_runtime_resume(struct device *dev)
2422 {
2423 	struct rk_hdptx_phy *hdptx = dev_get_drvdata(dev);
2424 	int ret;
2425 
2426 	ret = clk_bulk_prepare_enable(hdptx->nr_clks, hdptx->clks);
2427 	if (ret)
2428 		dev_err(hdptx->dev, "Failed to enable clocks: %d\n", ret);
2429 
2430 	return ret;
2431 }
2432 
rk_hdptx_phy_probe(struct platform_device * pdev)2433 static int rk_hdptx_phy_probe(struct platform_device *pdev)
2434 {
2435 	const struct rk_hdptx_phy_cfg *cfgs;
2436 	struct phy_provider *phy_provider;
2437 	struct device *dev = &pdev->dev;
2438 	struct rk_hdptx_phy *hdptx;
2439 	struct resource *res;
2440 	void __iomem *regs;
2441 	int ret, id;
2442 
2443 	hdptx = devm_kzalloc(dev, sizeof(*hdptx), GFP_KERNEL);
2444 	if (!hdptx)
2445 		return -ENOMEM;
2446 
2447 	hdptx->dev = dev;
2448 	hdptx->hdmi_cfg.bpc = 8;
2449 
2450 	regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
2451 	if (IS_ERR(regs))
2452 		return dev_err_probe(dev, PTR_ERR(regs),
2453 				     "Failed to ioremap resource\n");
2454 
2455 	cfgs = device_get_match_data(dev);
2456 	if (!cfgs)
2457 		return dev_err_probe(dev, -EINVAL, "missing match data\n");
2458 
2459 	/* find the phy-id from the io address */
2460 	hdptx->phy_id = -ENODEV;
2461 	for (id = 0; id < cfgs->num_phys; id++) {
2462 		if (res->start == cfgs->phy_ids[id]) {
2463 			hdptx->phy_id = id;
2464 			break;
2465 		}
2466 	}
2467 
2468 	if (hdptx->phy_id < 0)
2469 		return dev_err_probe(dev, -ENODEV, "no matching device found\n");
2470 
2471 	ret = devm_clk_bulk_get_all(dev, &hdptx->clks);
2472 	if (ret < 0)
2473 		return dev_err_probe(dev, ret, "Failed to get clocks\n");
2474 	if (ret == 0)
2475 		return dev_err_probe(dev, -EINVAL, "Missing clocks\n");
2476 
2477 	hdptx->nr_clks = ret;
2478 
2479 	hdptx->regmap = devm_regmap_init_mmio(dev, regs,
2480 					      &rk_hdptx_phy_regmap_config);
2481 	if (IS_ERR(hdptx->regmap))
2482 		return dev_err_probe(dev, PTR_ERR(hdptx->regmap),
2483 				     "Failed to init regmap\n");
2484 
2485 	hdptx->rsts[RST_APB].id = "apb";
2486 	hdptx->rsts[RST_INIT].id = "init";
2487 	hdptx->rsts[RST_CMN].id = "cmn";
2488 	hdptx->rsts[RST_LANE].id = "lane";
2489 
2490 	ret = devm_reset_control_bulk_get_exclusive(dev, RST_MAX, hdptx->rsts);
2491 	if (ret)
2492 		return dev_err_probe(dev, ret, "Failed to get resets\n");
2493 
2494 	hdptx->grf = syscon_regmap_lookup_by_phandle(dev->of_node,
2495 						     "rockchip,grf");
2496 	if (IS_ERR(hdptx->grf))
2497 		return dev_err_probe(dev, PTR_ERR(hdptx->grf),
2498 				     "Could not get GRF syscon\n");
2499 
2500 	platform_set_drvdata(pdev, hdptx);
2501 
2502 	ret = devm_pm_runtime_enable(dev);
2503 	if (ret)
2504 		return dev_err_probe(dev, ret, "Failed to enable runtime PM\n");
2505 
2506 	hdptx->phy = devm_phy_create(dev, NULL, &rk_hdptx_phy_ops);
2507 	if (IS_ERR(hdptx->phy))
2508 		return dev_err_probe(dev, PTR_ERR(hdptx->phy),
2509 				     "Failed to create HDMI PHY\n");
2510 
2511 	phy_set_drvdata(hdptx->phy, hdptx);
2512 	phy_set_bus_width(hdptx->phy, 8);
2513 
2514 	phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
2515 	if (IS_ERR(phy_provider))
2516 		return dev_err_probe(dev, PTR_ERR(phy_provider),
2517 				     "Failed to register PHY provider\n");
2518 
2519 	reset_control_deassert(hdptx->rsts[RST_APB].rstc);
2520 	reset_control_deassert(hdptx->rsts[RST_CMN].rstc);
2521 	reset_control_deassert(hdptx->rsts[RST_INIT].rstc);
2522 
2523 	return rk_hdptx_phy_clk_register(hdptx);
2524 }
2525 
2526 static const struct dev_pm_ops rk_hdptx_phy_pm_ops = {
2527 	RUNTIME_PM_OPS(rk_hdptx_phy_runtime_suspend,
2528 		       rk_hdptx_phy_runtime_resume, NULL)
2529 };
2530 
2531 static const struct rk_hdptx_phy_cfg rk3576_hdptx_phy_cfgs = {
2532 	.num_phys = 1,
2533 	.phy_ids = {
2534 		0x2b000000,
2535 	},
2536 };
2537 
2538 static const struct rk_hdptx_phy_cfg rk3588_hdptx_phy_cfgs = {
2539 	.num_phys = 2,
2540 	.phy_ids = {
2541 		0xfed60000,
2542 		0xfed70000,
2543 	},
2544 };
2545 
2546 static const struct of_device_id rk_hdptx_phy_of_match[] = {
2547 	{
2548 		.compatible = "rockchip,rk3576-hdptx-phy",
2549 		.data = &rk3576_hdptx_phy_cfgs
2550 	},
2551 	{
2552 		.compatible = "rockchip,rk3588-hdptx-phy",
2553 		.data = &rk3588_hdptx_phy_cfgs
2554 	},
2555 	{}
2556 };
2557 MODULE_DEVICE_TABLE(of, rk_hdptx_phy_of_match);
2558 
2559 static struct platform_driver rk_hdptx_phy_driver = {
2560 	.probe  = rk_hdptx_phy_probe,
2561 	.driver = {
2562 		.name = "rockchip-hdptx-phy",
2563 		.pm = &rk_hdptx_phy_pm_ops,
2564 		.of_match_table = rk_hdptx_phy_of_match,
2565 	},
2566 };
2567 module_platform_driver(rk_hdptx_phy_driver);
2568 
2569 MODULE_AUTHOR("Algea Cao <algea.cao@rock-chips.com>");
2570 MODULE_AUTHOR("Cristian Ciocaltea <cristian.ciocaltea@collabora.com>");
2571 MODULE_AUTHOR("Damon Ding <damon.ding@rock-chips.com>");
2572 MODULE_DESCRIPTION("Samsung HDMI/eDP Transmitter Combo PHY Driver");
2573 MODULE_LICENSE("GPL");
2574