xref: /rk3399_rockchip-uboot/drivers/video/drm/phy-rockchip-samsung-hdptx-hdmi.c (revision b0852613211bdc72efa1e8e826e67e0b057c6931)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Rockchip HDMI/DP Combo PHY with Samsung IP block
4  *
5  * Copyright (c) 2021 Rockchip Electronics Co. Ltd.
6  */
7 
8 #include <common.h>
9 #include <clk-uclass.h>
10 #include <dm.h>
11 #include <reset.h>
12 #include <regmap.h>
13 #include <syscon.h>
14 #include <asm/io.h>
15 #include <linux/bitfield.h>
16 #include <linux/rational.h>
17 #include <linux/iopoll.h>
18 #include <asm/arch/clock.h>
19 #include <dm/lists.h>
20 #include <dm/of_access.h>
21 
22 #include "rockchip_display.h"
23 #include "rockchip_crtc.h"
24 #include "rockchip_phy.h"
25 
26 #define UPDATE(x, h, l)		(((x) << (l)) & GENMASK((h), (l)))
27 
28 #define GRF_HDPTX_CON0			0x00
29 #define LC_REF_CLK_SEL			BIT(11)
30 #define HDPTX_I_PLL_EN			BIT(7)
31 #define HDPTX_I_BIAS_EN			BIT(6)
32 #define HDPTX_I_BGR_EN			BIT(5)
33 #define GRF_HDPTX_STATUS		0x80
34 #define HDPTX_O_PLL_LOCK_DONE		BIT(3)
35 #define HDPTX_O_PHY_CLK_RDY		BIT(2)
36 #define HDPTX_O_PHY_RDY			BIT(1)
37 #define HDPTX_O_SB_RDY			BIT(0)
38 
39 #define CMN_REG0000			0x0000
40 #define CMN_REG0001			0x0004
41 #define CMN_REG0002			0x0008
42 #define CMN_REG0003			0x000C
43 #define CMN_REG0004			0x0010
44 #define CMN_REG0005			0x0014
45 #define CMN_REG0006			0x0018
46 #define CMN_REG0007			0x001C
47 #define CMN_REG0008			0x0020
48 #define LCPLL_EN_MASK			BIT(6)
49 #define LCPLL_EN(x)			UPDATE(x, 4, 4)
50 #define LCPLL_LCVCO_MODE_EN_MASK	BIT(4)
51 #define LCPLL_LCVCO_MODE_EN(x)		UPDATE(x, 4, 4)
52 #define CMN_REG0009			0x0024
53 #define CMN_REG000A			0x0028
54 #define CMN_REG000B			0x002C
55 #define CMN_REG000C			0x0030
56 #define CMN_REG000D			0x0034
57 #define CMN_REG000E			0x0038
58 #define CMN_REG000F			0x003C
59 #define CMN_REG0010			0x0040
60 #define CMN_REG0011			0x0044
61 #define CMN_REG0012			0x0048
62 #define CMN_REG0013			0x004C
63 #define CMN_REG0014			0x0050
64 #define CMN_REG0015			0x0054
65 #define CMN_REG0016			0x0058
66 #define CMN_REG0017			0x005C
67 #define CMN_REG0018			0x0060
68 #define CMN_REG0019			0x0064
69 #define CMN_REG001A			0x0068
70 #define CMN_REG001B			0x006C
71 #define CMN_REG001C			0x0070
72 #define CMN_REG001D			0x0074
73 #define CMN_REG001E			0x0078
74 #define LCPLL_PI_EN_MASK		BIT(5)
75 #define LCPLL_PI_EN(x)			UPDATE(x, 5, 5)
76 #define LCPLL_100M_CLK_EN_MASK		BIT(0)
77 #define LCPLL_100M_CLK_EN(x)		UPDATE(x, 0, 0)
78 #define CMN_REG001F			0x007C
79 #define CMN_REG0020			0x0080
80 #define CMN_REG0021			0x0084
81 #define CMN_REG0022			0x0088
82 #define CMN_REG0023			0x008C
83 #define CMN_REG0024			0x0090
84 #define CMN_REG0025			0x0094
85 #define LCPLL_PMS_IQDIV_RSTN		BIT(4)
86 #define CMN_REG0026			0x0098
87 #define CMN_REG0027			0x009C
88 #define CMN_REG0028			0x00A0
89 #define LCPLL_SDC_FRAC_EN		BIT(2)
90 #define LCPLL_SDC_FRAC_RSTN		BIT(0)
91 #define CMN_REG0029			0x00A4
92 #define CMN_REG002A			0x00A8
93 #define CMN_REG002B			0x00AC
94 #define CMN_REG002C			0x00B0
95 #define CMN_REG002D			0x00B4
96 #define LCPLL_SDC_N_MASK		GENMASK(3, 1)
97 #define LCPLL_SDC_N(x)			UPDATE(x, 3, 1)
98 #define CMN_REG002E			0x00B8
99 #define LCPLL_SDC_NUMBERATOR_MASK	GENMASK(5, 0)
100 #define LCPLL_SDC_NUMBERATOR(x)		UPDATE(x, 5, 0)
101 #define CMN_REG002F			0x00BC
102 #define LCPLL_SDC_DENOMINATOR_MASK	GENMASK(7, 2)
103 #define LCPLL_SDC_DENOMINATOR(x)	UPDATE(x, 7, 2)
104 #define LCPLL_SDC_NDIV_RSTN		BIT(0)
105 #define CMN_REG0030			0x00C0
106 #define CMN_REG0031			0x00C4
107 #define CMN_REG0032			0x00C8
108 #define CMN_REG0033			0x00CC
109 #define CMN_REG0034			0x00D0
110 #define CMN_REG0035			0x00D4
111 #define CMN_REG0036			0x00D8
112 #define CMN_REG0037			0x00DC
113 #define CMN_REG0038			0x00E0
114 #define CMN_REG0039			0x00E4
115 #define CMN_REG003A			0x00E8
116 #define CMN_REG003B			0x00EC
117 #define CMN_REG003C			0x00F0
118 #define CMN_REG003D			0x00F4
119 #define ROPLL_LCVCO_EN			BIT(4)
120 #define CMN_REG003E			0x00F8
121 #define CMN_REG003F			0x00FC
122 #define CMN_REG0040			0x0100
123 #define CMN_REG0041			0x0104
124 #define CMN_REG0042			0x0108
125 #define CMN_REG0043			0x010C
126 #define CMN_REG0044			0x0110
127 #define CMN_REG0045			0x0114
128 #define CMN_REG0046			0x0118
129 #define CMN_REG0047			0x011C
130 #define CMN_REG0048			0x0120
131 #define CMN_REG0049			0x0124
132 #define CMN_REG004A			0x0128
133 #define CMN_REG004B			0x012C
134 #define CMN_REG004C			0x0130
135 #define CMN_REG004D			0x0134
136 #define CMN_REG004E			0x0138
137 #define ROPLL_PI_EN			BIT(5)
138 #define CMN_REG004F			0x013C
139 #define CMN_REG0050			0x0140
140 #define CMN_REG0051			0x0144
141 #define CMN_REG0052			0x0148
142 #define CMN_REG0053			0x014C
143 #define CMN_REG0054			0x0150
144 #define CMN_REG0055			0x0154
145 #define CMN_REG0056			0x0158
146 #define CMN_REG0057			0x015C
147 #define CMN_REG0058			0x0160
148 #define CMN_REG0059			0x0164
149 #define CMN_REG005A			0x0168
150 #define CMN_REG005B			0x016C
151 #define CMN_REG005C			0x0170
152 #define ROPLL_PMS_IQDIV_RSTN		BIT(5)
153 #define CMN_REG005D			0x0174
154 #define CMN_REG005E			0x0178
155 #define ROPLL_SDM_EN_MASK		BIT(6)
156 #define ROPLL_SDM_EN(x)			UPDATE(x, 6, 6)
157 #define ROPLL_SDM_FRAC_EN_RBR		BIT(3)
158 #define ROPLL_SDM_FRAC_EN_HBR		BIT(2)
159 #define ROPLL_SDM_FRAC_EN_HBR2		BIT(1)
160 #define ROPLL_SDM_FRAC_EN_HBR3		BIT(0)
161 #define CMN_REG005F			0x017C
162 #define CMN_REG0060			0x0180
163 #define CMN_REG0061			0x0184
164 #define CMN_REG0062			0x0188
165 #define CMN_REG0063			0x018C
166 #define CMN_REG0064			0x0190
167 #define ROPLL_SDM_NUM_SIGN_RBR_MASK	BIT(3)
168 #define ROPLL_SDM_NUM_SIGN_RBR(x)	UPDATE(x, 3, 3)
169 #define CMN_REG0065			0x0194
170 #define CMN_REG0066			0x0198
171 #define CMN_REG0067			0x019C
172 #define CMN_REG0068			0x01A0
173 #define CMN_REG0069			0x01A4
174 #define ROPLL_SDC_N_RBR_MASK		GENMASK(2, 0)
175 #define ROPLL_SDC_N_RBR(x)		UPDATE(x, 2, 0)
176 #define CMN_REG006A			0x01A8
177 #define CMN_REG006B			0x01AC
178 #define CMN_REG006C			0x01B0
179 #define CMN_REG006D			0x01B4
180 #define CMN_REG006E			0x01B8
181 #define CMN_REG006F			0x01BC
182 #define CMN_REG0070			0x01C0
183 #define CMN_REG0071			0x01C4
184 #define CMN_REG0072			0x01C8
185 #define CMN_REG0073			0x01CC
186 #define CMN_REG0074			0x01D0
187 #define ROPLL_SDC_NDIV_RSTN		BIT(2)
188 #define ROPLL_SSC_EN			BIT(0)
189 #define CMN_REG0075			0x01D4
190 #define CMN_REG0076			0x01D8
191 #define CMN_REG0077			0x01DC
192 #define CMN_REG0078			0x01E0
193 #define CMN_REG0079			0x01E4
194 #define CMN_REG007A			0x01E8
195 #define CMN_REG007B			0x01EC
196 #define CMN_REG007C			0x01F0
197 #define CMN_REG007D			0x01F4
198 #define CMN_REG007E			0x01F8
199 #define CMN_REG007F			0x01FC
200 #define CMN_REG0080			0x0200
201 #define CMN_REG0081			0x0204
202 #define OVRD_PLL_CD_CLK_EN		BIT(8)
203 #define PLL_CD_HSCLK_EAST_EN		BIT(0)
204 #define CMN_REG0082			0x0208
205 #define CMN_REG0083			0x020C
206 #define CMN_REG0084			0x0210
207 #define CMN_REG0085			0x0214
208 #define CMN_REG0086			0x0218
209 #define PLL_PCG_POSTDIV_SEL_MASK	GENMASK(7, 4)
210 #define PLL_PCG_POSTDIV_SEL(x)		UPDATE(x, 7, 4)
211 #define PLL_PCG_CLK_SEL_MASK		GENMASK(3, 1)
212 #define PLL_PCG_CLK_SEL(x)		UPDATE(x, 3, 1)
213 #define PLL_PCG_CLK_EN			BIT(0)
214 #define CMN_REG0087			0x021C
215 #define PLL_FRL_MODE_EN			BIT(3)
216 #define PLL_TX_HS_CLK_EN		BIT(2)
217 #define CMN_REG0088			0x0220
218 #define CMN_REG0089			0x0224
219 #define LCPLL_ALONE_MODE		BIT(1)
220 #define CMN_REG008A			0x0228
221 #define CMN_REG008B			0x022C
222 #define CMN_REG008C			0x0230
223 #define CMN_REG008D			0x0234
224 #define CMN_REG008E			0x0238
225 #define CMN_REG008F			0x023C
226 #define CMN_REG0090			0x0240
227 #define CMN_REG0091			0x0244
228 #define CMN_REG0092			0x0248
229 #define CMN_REG0093			0x024C
230 #define CMN_REG0094			0x0250
231 #define CMN_REG0095			0x0254
232 #define CMN_REG0096			0x0258
233 #define CMN_REG0097			0x025C
234 #define DIG_CLK_SEL			BIT(1)
235 #define ROPLL_REF			BIT(1)
236 #define LCPLL_REF			0
237 #define CMN_REG0098			0x0260
238 #define CMN_REG0099			0x0264
239 #define CMN_ROPLL_ALONE_MODE		BIT(2)
240 #define ROPLL_ALONE_MODE		BIT(2)
241 #define CMN_REG009A			0x0268
242 #define HS_SPEED_SEL			BIT(0)
243 #define DIV_10_CLOCK			BIT(0)
244 #define CMN_REG009B			0x026C
245 #define IS_SPEED_SEL			BIT(4)
246 #define LINK_SYMBOL_CLOCK		BIT(4)
247 #define LINK_SYMBOL_CLOCK1_2		0
248 #define CMN_REG009C			0x0270
249 #define CMN_REG009D			0x0274
250 #define CMN_REG009E			0x0278
251 #define CMN_REG009F			0x027C
252 #define CMN_REG00A0			0x0280
253 #define CMN_REG00A1			0x0284
254 #define CMN_REG00A2			0x0288
255 #define CMN_REG00A3			0x028C
256 #define CMN_REG00AD			0x0290
257 #define CMN_REG00A5			0x0294
258 #define CMN_REG00A6			0x0298
259 #define CMN_REG00A7			0x029C
260 #define SB_REG0100			0x0400
261 #define SB_REG0101			0x0404
262 #define SB_REG0102			0x0408
263 #define OVRD_SB_RXTERM_EN_MASK		BIT(5)
264 #define OVRD_SB_RXTERM_EN(x)		UPDATE(x, 5, 5)
265 #define SB_RXTERM_EN_MASK		BIT(4)
266 #define SB_RXTERM_EN(x)			UPDATE(x, 4, 4)
267 #define ANA_SB_RXTERM_OFFSP_MASK	GENMASK(3, 0)
268 #define ANA_SB_RXTERM_OFFSP(x)		UPDATE(x, 3, 0)
269 #define SB_REG0103			0x040C
270 #define ANA_SB_RXTERM_OFFSN_MASK	GENMASK(6, 3)
271 #define ANA_SB_RXTERM_OFFSN(x)		UPDATE(x, 6, 3)
272 #define OVRD_SB_RX_RESCAL_DONE_MASK	BIT(1)
273 #define OVRD_SB_RX_RESCAL_DONE(x)	UPDATE(x, 1, 1)
274 #define SB_RX_RESCAL_DONE_MASK		BIT(0)
275 #define SB_RX_RESCAL_DONE(x)		UPDATE(x, 0, 0)
276 #define SB_REG0104			0x0410
277 #define OVRD_SB_EN_MASK			BIT(5)
278 #define OVRD_SB_EN(x)			UPDATE(x, 5, 5)
279 #define SB_EN_MASK			BIT(4)
280 #define SB_EN(x)			UPDATE(x, 4, 4)
281 #define SB_REG0105			0x0414
282 #define OVRD_SB_EARC_CMDC_EN_MASK	BIT(6)
283 #define OVRD_SB_EARC_CMDC_EN(x)		UPDATE(x, 6, 6)
284 #define SB_EARC_CMDC_EN_MASK		BIT(5)
285 #define SB_EARC_CMDC_EN(x)		UPDATE(x, 5, 5)
286 #define ANA_SB_TX_HLVL_PROG_MASK	GENMASK(2, 0)
287 #define ANA_SB_TX_HLVL_PROG(x)		UPDATE(x, 2, 0)
288 #define SB_REG0106			0x0418
289 #define ANA_SB_TX_LLVL_PROG_MASK	GENMASK(6, 4)
290 #define ANA_SB_TX_LLVL_PROG(x)		UPDATE(x, 6, 4)
291 #define SB_REG0107			0x041C
292 #define SB_REG0108			0x0420
293 #define SB_REG0109			0x0424
294 #define ANA_SB_DMRX_AFC_DIV_RATIO_MASK	GENMASK(2, 0)
295 #define ANA_SB_DMRX_AFC_DIV_RATIO(x)	UPDATE(x, 2, 0)
296 #define SB_REG010A			0x0428
297 #define SB_REG010B			0x042C
298 #define SB_REG010C			0x0430
299 #define SB_REG010D			0x0434
300 #define SB_REG010E			0x0438
301 #define SB_REG010F			0x043C
302 #define OVRD_SB_VREG_EN_MASK		BIT(7)
303 #define OVRD_SB_VREG_EN(x)		UPDATE(x, 7, 7)
304 #define SB_VREG_EN_MASK			BIT(6)
305 #define SB_VREG_EN(x)			UPDATE(x, 6, 6)
306 #define OVRD_SB_VREG_LPF_BYPASS_MASK	BIT(5)
307 #define OVRD_SB_VREG_LPF_BYPASS(x)	UPDATE(x, 5, 5)
308 #define SB_VREG_LPF_BYPASS_MASK		BIT(4)
309 #define SB_VREG_LPF_BYPASS(x)		UPDATE(x, 4, 4)
310 #define ANA_SB_VREG_GAIN_CTRL_MASK	GENMASK(3, 0)
311 #define ANA_SB_VREG_GAIN_CTRL(x)	UPDATE(x, 3, 0)
312 #define SB_REG0110			0x0440
313 #define ANA_SB_VREG_REF_SEL_MASK	BIT(0)
314 #define ANA_SB_VREG_REF_SEL(x)		UPDATE(x, 0, 0)
315 #define SB_REG0111			0x0444
316 #define SB_REG0112			0x0448
317 #define SB_REG0113			0x044C
318 #define SB_RX_RCAL_OPT_CODE_MASK	GENMASK(5, 4)
319 #define SB_RX_RCAL_OPT_CODE(x)		UPDATE(x, 5, 4)
320 #define SB_RX_RTERM_CTRL_MASK		GENMASK(3, 0)
321 #define SB_RX_RTERM_CTRL(x)		UPDATE(x, 3, 0)
322 #define SB_REG0114			0x0450
323 #define SB_TG_SB_EN_DELAY_TIME_MASK	GENMASK(5, 3)
324 #define SB_TG_SB_EN_DELAY_TIME(x)	UPDATE(x, 5, 3)
325 #define SB_TG_RXTERM_EN_DELAY_TIME_MASK	GENMASK(2, 0)
326 #define SB_TG_RXTERM_EN_DELAY_TIME(x)	UPDATE(x, 2, 0)
327 #define SB_REG0115			0x0454
328 #define SB_READY_DELAY_TIME_MASK	GENMASK(5, 3)
329 #define SB_READY_DELAY_TIME(x)		UPDATE(x, 5, 3)
330 #define SB_TG_OSC_EN_DELAY_TIME_MASK	GENMASK(2, 0)
331 #define SB_TG_OSC_EN_DELAY_TIME(x)	UPDATE(x, 2, 0)
332 #define SB_REG0116			0x0458
333 #define AFC_RSTN_DELAY_TIME_MASK	GENMASK(6, 4)
334 #define AFC_RSTN_DELAY_TIME(x)		UPDATE(x, 6, 4)
335 #define SB_REG0117			0x045C
336 #define FAST_PULSE_TIME_MASK		GENMASK(3, 0)
337 #define FAST_PULSE_TIME(x)		UPDATE(x, 3, 0)
338 #define SB_REG0118			0x0460
339 #define SB_REG0119			0x0464
340 #define SB_REG011A			0x0468
341 #define SB_REG011B			0x046C
342 #define SB_EARC_SIG_DET_BYPASS_MASK	BIT(4)
343 #define SB_EARC_SIG_DET_BYPASS(x)	UPDATE(x, 4, 4)
344 #define SB_AFC_TOL_MASK			GENMASK(3, 0)
345 #define SB_AFC_TOL(x)			UPDATE(x, 3, 0)
346 #define SB_REG011C			0x0470
347 #define SB_REG011D			0x0474
348 #define SB_REG011E			0x0478
349 #define SB_REG011F			0x047C
350 #define SB_PWM_AFC_CTRL_MASK		GENMASK(7, 2)
351 #define SB_PWM_AFC_CTRL(x)		UPDATE(x, 7, 2)
352 #define SB_RCAL_RSTN_MASK		BIT(1)
353 #define SB_RCAL_RSTN(x)			UPDATE(x, 1, 1)
354 #define SB_REG0120			0x0480
355 #define SB_EARC_EN_MASK			BIT(1)
356 #define SB_EARC_EN(x)			UPDATE(x, 1, 1)
357 #define SB_EARC_AFC_EN_MASK		BIT(2)
358 #define SB_EARC_AFC_EN(x)		UPDATE(x, 2, 2)
359 #define SB_REG0121			0x0484
360 #define SB_REG0122			0x0488
361 #define SB_REG0123			0x048C
362 #define OVRD_SB_READY_MASK		BIT(5)
363 #define OVRD_SB_READY(x)		UPDATE(x, 5, 5)
364 #define SB_READY_MASK			BIT(4)
365 #define SB_READY(x)			UPDATE(x, 4, 4)
366 #define SB_REG0124			0x0490
367 #define SB_REG0125			0x0494
368 #define SB_REG0126			0x0498
369 #define SB_REG0127			0x049C
370 #define SB_REG0128			0x04A0
371 #define SB_REG0129			0x04AD
372 #define LNTOP_REG0200			0x0800
373 #define PROTOCOL_SEL			BIT(2)
374 #define HDMI_MODE			BIT(2)
375 #define HDMI_TMDS_FRL_SEL		BIT(1)
376 #define LNTOP_REG0201			0x0804
377 #define LNTOP_REG0202			0x0808
378 #define LNTOP_REG0203			0x080C
379 #define LNTOP_REG0204			0x0810
380 #define LNTOP_REG0205			0x0814
381 #define LNTOP_REG0206			0x0818
382 #define DATA_BUS_WIDTH			(0x3 << 1)
383 #define WIDTH_40BIT			(0x3 << 1)
384 #define WIDTH_36BIT			(0x2 << 1)
385 #define DATA_BUS_SEL			BIT(0)
386 #define DATA_BUS_36_40			BIT(0)
387 #define LNTOP_REG0207			0x081C
388 #define LANE_EN				0xf
389 #define ALL_LANE_EN			0xf
390 #define LNTOP_REG0208			0x0820
391 #define LNTOP_REG0209			0x0824
392 #define LNTOP_REG020A			0x0828
393 #define LNTOP_REG020B			0x082C
394 #define LNTOP_REG020C			0x0830
395 #define LNTOP_REG020D			0x0834
396 #define LNTOP_REG020E			0x0838
397 #define LNTOP_REG020F			0x083C
398 #define LNTOP_REG0210			0x0840
399 #define LNTOP_REG0211			0x0844
400 #define LNTOP_REG0212			0x0848
401 #define LNTOP_REG0213			0x084C
402 #define LNTOP_REG0214			0x0850
403 #define LNTOP_REG0215			0x0854
404 #define LNTOP_REG0216			0x0858
405 #define LNTOP_REG0217			0x085C
406 #define LNTOP_REG0218			0x0860
407 #define LNTOP_REG0219			0x0864
408 #define LNTOP_REG021A			0x0868
409 #define LNTOP_REG021B			0x086C
410 #define LNTOP_REG021C			0x0870
411 #define LNTOP_REG021D			0x0874
412 #define LNTOP_REG021E			0x0878
413 #define LNTOP_REG021F			0x087C
414 #define LNTOP_REG0220			0x0880
415 #define LNTOP_REG0221			0x0884
416 #define LNTOP_REG0222			0x0888
417 #define LNTOP_REG0223			0x088C
418 #define LNTOP_REG0224			0x0890
419 #define LNTOP_REG0225			0x0894
420 #define LNTOP_REG0226			0x0898
421 #define LNTOP_REG0227			0x089C
422 #define LNTOP_REG0228			0x08A0
423 #define LNTOP_REG0229			0x08A4
424 #define LANE_REG0300			0x0C00
425 #define LANE_REG0301			0x0C04
426 #define LANE_REG0302			0x0C08
427 #define LANE_REG0303			0x0C0C
428 #define LANE_REG0304			0x0C10
429 #define LANE_REG0305			0x0C14
430 #define LANE_REG0306			0x0C18
431 #define LANE_REG0307			0x0C1C
432 #define LANE_REG0308			0x0C20
433 #define LANE_REG0309			0x0C24
434 #define LANE_REG030A			0x0C28
435 #define LANE_REG030B			0x0C2C
436 #define LANE_REG030C			0x0C30
437 #define LANE_REG030D			0x0C34
438 #define LANE_REG030E			0x0C38
439 #define LANE_REG030F			0x0C3C
440 #define LANE_REG0310			0x0C40
441 #define LANE_REG0311			0x0C44
442 #define LANE_REG0312			0x0C48
443 #define LN0_TX_SER_RATE_SEL_RBR		BIT(5)
444 #define LN0_TX_SER_RATE_SEL_HBR		BIT(4)
445 #define LN0_TX_SER_RATE_SEL_HBR2	BIT(3)
446 #define LN0_TX_SER_RATE_SEL_HBR3	BIT(2)
447 #define LANE_REG0313			0x0C4C
448 #define LANE_REG0314			0x0C50
449 #define LANE_REG0315			0x0C54
450 #define LANE_REG0316			0x0C58
451 #define LANE_REG0317			0x0C5C
452 #define LANE_REG0318			0x0C60
453 #define LANE_REG0319			0x0C64
454 #define LANE_REG031A			0x0C68
455 #define LANE_REG031B			0x0C6C
456 #define LANE_REG031C			0x0C70
457 #define LANE_REG031D			0x0C74
458 #define LANE_REG031E			0x0C78
459 #define LANE_REG031F			0x0C7C
460 #define LANE_REG0320			0x0C80
461 #define LANE_REG0321			0x0C84
462 #define LANE_REG0322			0x0C88
463 #define LANE_REG0323			0x0C8C
464 #define LANE_REG0324			0x0C90
465 #define LANE_REG0325			0x0C94
466 #define LANE_REG0326			0x0C98
467 #define LANE_REG0327			0x0C9C
468 #define LANE_REG0328			0x0CA0
469 #define LANE_REG0329			0x0CA4
470 #define LANE_REG032A			0x0CA8
471 #define LANE_REG032B			0x0CAC
472 #define LANE_REG032C			0x0CB0
473 #define LANE_REG032D			0x0CB4
474 #define LANE_REG0400			0x1000
475 #define LANE_REG0401			0x1004
476 #define LANE_REG0402			0x1008
477 #define LANE_REG0403			0x100C
478 #define LANE_REG0404			0x1010
479 #define LANE_REG0405			0x1014
480 #define LANE_REG0406			0x1018
481 #define LANE_REG0407			0x101C
482 #define LANE_REG0408			0x1020
483 #define LANE_REG0409			0x1024
484 #define LANE_REG040A			0x1028
485 #define LANE_REG040B			0x102C
486 #define LANE_REG040C			0x1030
487 #define LANE_REG040D			0x1034
488 #define LANE_REG040E			0x1038
489 #define LANE_REG040F			0x103C
490 #define LANE_REG0410			0x1040
491 #define LANE_REG0411			0x1044
492 #define LANE_REG0412			0x1048
493 #define LN1_TX_SER_RATE_SEL_RBR		BIT(5)
494 #define LN1_TX_SER_RATE_SEL_HBR		BIT(4)
495 #define LN1_TX_SER_RATE_SEL_HBR2	BIT(3)
496 #define LN1_TX_SER_RATE_SEL_HBR3	BIT(2)
497 #define LANE_REG0413			0x104C
498 #define LANE_REG0414			0x1050
499 #define LANE_REG0415			0x1054
500 #define LANE_REG0416			0x1058
501 #define LANE_REG0417			0x105C
502 #define LANE_REG0418			0x1060
503 #define LANE_REG0419			0x1064
504 #define LANE_REG041A			0x1068
505 #define LANE_REG041B			0x106C
506 #define LANE_REG041C			0x1070
507 #define LANE_REG041D			0x1074
508 #define LANE_REG041E			0x1078
509 #define LANE_REG041F			0x107C
510 #define LANE_REG0420			0x1080
511 #define LANE_REG0421			0x1084
512 #define LANE_REG0422			0x1088
513 #define LANE_REG0423			0x108C
514 #define LANE_REG0424			0x1090
515 #define LANE_REG0425			0x1094
516 #define LANE_REG0426			0x1098
517 #define LANE_REG0427			0x109C
518 #define LANE_REG0428			0x10A0
519 #define LANE_REG0429			0x10A4
520 #define LANE_REG042A			0x10A8
521 #define LANE_REG042B			0x10AC
522 #define LANE_REG042C			0x10B0
523 #define LANE_REG042D			0x10B4
524 #define LANE_REG0500			0x1400
525 #define LANE_REG0501			0x1404
526 #define LANE_REG0502			0x1408
527 #define LANE_REG0503			0x140C
528 #define LANE_REG0504			0x1410
529 #define LANE_REG0505			0x1414
530 #define LANE_REG0506			0x1418
531 #define LANE_REG0507			0x141C
532 #define LANE_REG0508			0x1420
533 #define LANE_REG0509			0x1424
534 #define LANE_REG050A			0x1428
535 #define LANE_REG050B			0x142C
536 #define LANE_REG050C			0x1430
537 #define LANE_REG050D			0x1434
538 #define LANE_REG050E			0x1438
539 #define LANE_REG050F			0x143C
540 #define LANE_REG0510			0x1440
541 #define LANE_REG0511			0x1444
542 #define LANE_REG0512			0x1448
543 #define LN2_TX_SER_RATE_SEL_RBR		BIT(5)
544 #define LN2_TX_SER_RATE_SEL_HBR		BIT(4)
545 #define LN2_TX_SER_RATE_SEL_HBR2	BIT(3)
546 #define LN2_TX_SER_RATE_SEL_HBR3	BIT(2)
547 #define LANE_REG0513			0x144C
548 #define LANE_REG0514			0x1450
549 #define LANE_REG0515			0x1454
550 #define LANE_REG0516			0x1458
551 #define LANE_REG0517			0x145C
552 #define LANE_REG0518			0x1460
553 #define LANE_REG0519			0x1464
554 #define LANE_REG051A			0x1468
555 #define LANE_REG051B			0x146C
556 #define LANE_REG051C			0x1470
557 #define LANE_REG051D			0x1474
558 #define LANE_REG051E			0x1478
559 #define LANE_REG051F			0x147C
560 #define LANE_REG0520			0x1480
561 #define LANE_REG0521			0x1484
562 #define LANE_REG0522			0x1488
563 #define LANE_REG0523			0x148C
564 #define LANE_REG0524			0x1490
565 #define LANE_REG0525			0x1494
566 #define LANE_REG0526			0x1498
567 #define LANE_REG0527			0x149C
568 #define LANE_REG0528			0x14A0
569 #define LANE_REG0529			0x14AD
570 #define LANE_REG052A			0x14A8
571 #define LANE_REG052B			0x14AC
572 #define LANE_REG052C			0x14B0
573 #define LANE_REG052D			0x14B4
574 #define LANE_REG0600			0x1800
575 #define LANE_REG0601			0x1804
576 #define LANE_REG0602			0x1808
577 #define LANE_REG0603			0x180C
578 #define LANE_REG0604			0x1810
579 #define LANE_REG0605			0x1814
580 #define LANE_REG0606			0x1818
581 #define LANE_REG0607			0x181C
582 #define LANE_REG0608			0x1820
583 #define LANE_REG0609			0x1824
584 #define LANE_REG060A			0x1828
585 #define LANE_REG060B			0x182C
586 #define LANE_REG060C			0x1830
587 #define LANE_REG060D			0x1834
588 #define LANE_REG060E			0x1838
589 #define LANE_REG060F			0x183C
590 #define LANE_REG0610			0x1840
591 #define LANE_REG0611			0x1844
592 #define LANE_REG0612			0x1848
593 #define LN3_TX_SER_RATE_SEL_RBR		BIT(5)
594 #define LN3_TX_SER_RATE_SEL_HBR		BIT(4)
595 #define LN3_TX_SER_RATE_SEL_HBR2	BIT(3)
596 #define LN3_TX_SER_RATE_SEL_HBR3	BIT(2)
597 #define LANE_REG0613			0x184C
598 #define LANE_REG0614			0x1850
599 #define LANE_REG0615			0x1854
600 #define LANE_REG0616			0x1858
601 #define LANE_REG0617			0x185C
602 #define LANE_REG0618			0x1860
603 #define LANE_REG0619			0x1864
604 #define LANE_REG061A			0x1868
605 #define LANE_REG061B			0x186C
606 #define LANE_REG061C			0x1870
607 #define LANE_REG061D			0x1874
608 #define LANE_REG061E			0x1878
609 #define LANE_REG061F			0x187C
610 #define LANE_REG0620			0x1880
611 #define LANE_REG0621			0x1884
612 #define LANE_REG0622			0x1888
613 #define LANE_REG0623			0x188C
614 #define LANE_REG0624			0x1890
615 #define LANE_REG0625			0x1894
616 #define LANE_REG0626			0x1898
617 #define LANE_REG0627			0x189C
618 #define LANE_REG0628			0x18A0
619 #define LANE_REG0629			0x18A4
620 #define LANE_REG062A			0x18A8
621 #define LANE_REG062B			0x18AC
622 #define LANE_REG062C			0x18B0
623 #define LANE_REG062D			0x18B4
624 
625 #define HDMI20_MAX_RATE 600000000
626 #define DATA_RATE_MASK 0xFFFFFFF
627 #define COLOR_DEPTH_MASK BIT(31)
628 #define HDMI_MODE_MASK BIT(30)
629 #define HDMI_EARC_MASK BIT(29)
630 
631 #define FRL_8G_4LANES 3200000000ULL
632 #define FRL_6G_3LANES 1800000000
633 #define FRL_3G_3LANES 900000000
634 
635 struct lcpll_config {
636 	u32 bit_rate;
637 	u8 lcvco_mode_en;
638 	u8 pi_en;
639 	u8 clk_en_100m;
640 	u8 pms_mdiv;
641 	u8 pms_mdiv_afc;
642 	u8 pms_pdiv;
643 	u8 pms_refdiv;
644 	u8 pms_sdiv;
645 	u8 pi_cdiv_rstn;
646 	u8 pi_cdiv_sel;
647 	u8 sdm_en;
648 	u8 sdm_rstn;
649 	u8 sdc_frac_en;
650 	u8 sdc_rstn;
651 	u8 sdm_deno;
652 	u8 sdm_num_sign;
653 	u8 sdm_num;
654 	u8 sdc_n;
655 	u8 sdc_n2;
656 	u8 sdc_num;
657 	u8 sdc_deno;
658 	u8 sdc_ndiv_rstn;
659 	u8 ssc_en;
660 	u8 ssc_fm_dev;
661 	u8 ssc_fm_freq;
662 	u8 ssc_clk_div_sel;
663 	u8 cd_tx_ser_rate_sel;
664 };
665 
666 struct ropll_config {
667 	u32 bit_rate;
668 	u8 pms_mdiv;
669 	u8 pms_mdiv_afc;
670 	u8 pms_pdiv;
671 	u8 pms_refdiv;
672 	u8 pms_sdiv;
673 	u8 pms_iqdiv_rstn;
674 	u8 ref_clk_sel;
675 	u8 sdm_en;
676 	u8 sdm_rstn;
677 	u8 sdc_frac_en;
678 	u8 sdc_rstn;
679 	u8 sdm_clk_div;
680 	u8 sdm_deno;
681 	u8 sdm_num_sign;
682 	u8 sdm_num;
683 	u8 sdc_n;
684 	u8 sdc_num;
685 	u8 sdc_deno;
686 	u8 sdc_ndiv_rstn;
687 	u8 ssc_en;
688 	u8 ssc_fm_dev;
689 	u8 ssc_fm_freq;
690 	u8 ssc_clk_div_sel;
691 	u8 ana_cpp_ctrl;
692 	u8 ana_lpf_c_sel;
693 	u8 cd_tx_ser_rate_sel;
694 };
695 
696 struct rockchip_hdptx_phy {
697 	struct udevice *dev;
698 	void __iomem *base;
699 	struct regmap *grf;
700 
701 	int id;
702 	bool dclk_en;
703 	bool pll_locked;
704 
705 	unsigned long rate;
706 	u32 bus_width;
707 
708 	struct reset_ctl apb_reset;
709 	struct reset_ctl cmn_reset;
710 	struct reset_ctl init_reset;
711 	struct reset_ctl lane_reset;
712 };
713 
714 struct clk_hdptx {
715 	struct udevice *dev;
716 	ulong rate;
717 };
718 
719 /* global variables are used to pass reource from phy drivers to clk driver */
720 static struct rockchip_hdptx_phy *g_hdptx0;
721 static struct rockchip_hdptx_phy *g_hdptx1;
722 
723 struct lcpll_config lcpll_cfg[] = {
724 	{ 48000000, 1, 0, 0, 0x7d, 0x7d, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 2,
725 		0, 0x13, 0x18, 1, 0, 0x20, 0x0c, 1, 0,
726 	},
727 	{ 40000000, 1, 1, 0, 0x68, 0x68, 1, 1, 0, 0, 0, 1, 1, 1, 1, 9, 0, 1, 1,
728 		0, 2, 3, 1, 0, 0x20, 0x0c, 1, 0,
729 	},
730 	{ 24000000, 1, 0, 0, 0x7d, 0x7d, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 2,
731 		0, 0x13, 0x18, 1, 0, 0x20, 0x0c, 1, 0,
732 	},
733 	{ 18000000, 1, 0, 0, 0x7d, 0x7d, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 2,
734 		0, 0x13, 0x18, 1, 0, 0x20, 0x0c, 1, 0,
735 	},
736 	{ 9000000, 1, 0, 0, 0x7d, 0x7d, 1, 1, 3, 0, 0, 0, 0, 1, 1, 1, 0, 0, 2,
737 		0, 0x13, 0x18, 1, 0, 0x20, 0x0c, 1, 0,
738 	},
739 	{ ~0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
740 		0, 0, 0, 0, 0, 0,
741 	},
742 };
743 
744 struct ropll_config ropll_frl_cfg[] = {
745 	{ 24000000, 0x19, 0x19, 1, 1, 0, 1, 2, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0,
746 		0, 0x20, 0x0c, 1, 0x0e, 0, 0,
747 	},
748 	{ 18000000, 0x7d, 0x7d, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0,
749 		0, 0x20, 0x0c, 1, 0x0e, 0, 0,
750 	},
751 	{ 9000000, 0x7d, 0x7d, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0,
752 		0, 0x20, 0x0c, 1, 0x0e, 0, 0,
753 	},
754 	{ ~0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
755 		0, 0, 0, 0,
756 	},
757 };
758 
759 struct ropll_config ropll_tmds_cfg[] = {
760 	{ 5940000, 124, 124, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 62, 1, 16, 5, 0,
761 		1, 1, 0, 0x20, 0x0c, 1, 0x0e, 0, 0,
762 	},
763 	{ 3712500, 155, 155, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 62, 1, 16, 5, 0,
764 		1, 1, 0, 0x20, 0x0c, 1, 0x0e, 0, 0,
765 	},
766 	{ 2970000, 124, 124, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 62, 1, 16, 5, 0,
767 		1, 1, 0, 0x20, 0x0c, 1, 0x0e, 0, 0,
768 	},
769 	{ 1620000, 135, 135, 1, 1, 3, 1, 1, 0, 1, 1, 1, 1, 4, 0, 3, 5, 5, 0x10,
770 		1, 0, 0x20, 0x0c, 1, 0x0e, 0, 0,
771 	},
772 	{ 1856250, 155, 155, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 62, 1, 16, 5, 0,
773 		1, 1, 0, 0x20, 0x0c, 1, 0x0e, 0, 0,
774 	},
775 	{ 1485000, 0x7b, 0x7b, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 4, 0, 3, 5, 5, 0x10,
776 		1, 0, 0x20, 0x0c, 1, 0x0e, 0, 0,
777 	},
778 	{ 1462500, 122, 122, 1, 1, 4, 1, 1, 1, 1, 1, 1, 1, 244, 1, 16, 1, 0, 1,
779 		1, 0, 0x20, 0x0c, 1, 0x0e, 0, 0,
780 	},
781 	{ 1065000, 89, 89, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 89, 1, 16, 1, 0, 1,
782 		1, 0, 0x20, 0x0c, 1, 0x0e, 0, 0,
783 	},
784 	{ 1080000, 135, 135, 1, 1, 5, 1, 1, 0, 1, 0, 1, 1, 0x9, 0, 0x05, 0, 0x14,
785 		0x18, 1, 0, 0x20, 0x0c, 1, 0x0e, 0, 0,
786 	},
787 	{ 855000, 125, 125, 1, 1, 6, 1, 1, 1, 1, 1, 1, 1, 80, 1, 16, 2, 0,
788 		1, 1, 0, 0x20, 0x0c, 1, 0x0e, 0, 0,
789 	},
790 	{ 835000, 105, 105, 1, 1, 5, 1, 1, 1, 1, 1, 1, 1, 42, 1, 16, 1, 0,
791 		1, 1, 0, 0x20, 0x0c, 1, 0x0e, 0, 0,
792 	},
793 	{ 928125, 155, 155, 1, 1, 7, 1, 1, 1, 1, 1, 1, 1, 62, 1, 16, 5, 0,
794 		1, 1, 0, 0x20, 0x0c, 1, 0x0e, 0, 0,
795 	},
796 	{ 742500, 124, 124, 1, 1, 7, 1, 1, 1, 1, 1, 1, 1, 62, 1, 16, 5, 0,
797 		1, 1, 0, 0x20, 0x0c, 1, 0x0e, 0, 0,
798 	},
799 	{ 650000, 162, 162, 1, 1, 11, 1, 1, 1, 1, 1, 1, 1, 54, 0, 16, 4, 1,
800 		1, 1, 0, 0x20, 0x0c, 1, 0x0e, 0, 0,
801 	},
802 	{ 337500, 0x70, 0x70, 1, 1, 0xf, 1, 1, 1, 1, 1, 1, 1, 0x2, 0, 0x01, 5, 1,
803 		1, 1, 0, 0x20, 0x0c, 1, 0x0e, 0, 0,
804 	},
805 	{ 400000, 100, 100, 1, 1, 11, 1, 1, 0, 1, 0, 1, 1, 0x9, 0, 0x05, 0, 0x14,
806 		0x18, 1, 0, 0x20, 0x0c, 1, 0x0e, 0, 0,
807 	},
808 	{ 270000, 0x5a, 0x5a, 1, 1, 0xf, 1, 1, 0, 1, 0, 1, 1, 0x9, 0, 0x05, 0, 0x14,
809 		0x18, 1, 0, 0x20, 0x0c, 1, 0x0e, 0, 0,
810 	},
811 	{ 251750, 84, 84, 1, 1, 0xf, 1, 1, 1, 1, 1, 1, 1, 168, 1, 16, 4, 1,
812 		1, 1, 0, 0x20, 0x0c, 1, 0x0e, 0, 0,
813 	},
814 	{ ~0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
815 		0, 0, 0, 0,
816 	},
817 };
818 
hdptx_write(struct rockchip_hdptx_phy * hdptx,uint reg,uint val)819 static inline void hdptx_write(struct rockchip_hdptx_phy *hdptx, uint reg,
820 			       uint val)
821 {
822 	writel(val, hdptx->base + reg);
823 }
824 
hdptx_read(struct rockchip_hdptx_phy * hdptx,uint reg)825 static inline uint hdptx_read(struct rockchip_hdptx_phy *hdptx, uint reg)
826 {
827 	return readl(hdptx->base + reg);
828 }
829 
hdptx_update_bits(struct rockchip_hdptx_phy * hdptx,uint reg,uint mask,uint val)830 static void hdptx_update_bits(struct rockchip_hdptx_phy *hdptx, uint reg,
831 			      uint mask, uint val)
832 {
833 	uint orig, tmp;
834 
835 	orig = hdptx_read(hdptx, reg);
836 	tmp = orig & ~mask;
837 	tmp |= val & mask;
838 	hdptx_write(hdptx, reg, tmp);
839 }
840 
hdptx_grf_write(struct rockchip_hdptx_phy * hdptx,uint reg,uint val)841 static void hdptx_grf_write(struct rockchip_hdptx_phy *hdptx, uint reg,
842 			    uint val)
843 {
844 	regmap_write(hdptx->grf, reg, val);
845 }
846 
hdptx_grf_read(struct rockchip_hdptx_phy * hdptx,uint reg)847 static uint hdptx_grf_read(struct rockchip_hdptx_phy *hdptx, uint reg)
848 {
849 	uint val;
850 	int ret;
851 
852 	ret = regmap_read(hdptx->grf, reg, &val);
853 	if (ret)
854 		dev_err(hdptx->dev, "regmap_read err:%d", ret);
855 
856 	return val;
857 }
858 
hdptx_pre_power_up(struct rockchip_hdptx_phy * hdptx)859 static void hdptx_pre_power_up(struct rockchip_hdptx_phy *hdptx)
860 {
861 	u32 val = 0;
862 
863 	reset_assert(&hdptx->apb_reset);
864 	udelay(20);
865 	reset_deassert(&hdptx->apb_reset);
866 
867 	reset_assert(&hdptx->lane_reset);
868 	reset_assert(&hdptx->cmn_reset);
869 	reset_assert(&hdptx->init_reset);
870 
871 	val = (HDPTX_I_PLL_EN | HDPTX_I_BIAS_EN | HDPTX_I_BGR_EN) << 16;
872 	hdptx_grf_write(hdptx, GRF_HDPTX_CON0, val);
873 }
874 
hdptx_post_enable_lane(struct rockchip_hdptx_phy * hdptx)875 static int hdptx_post_enable_lane(struct rockchip_hdptx_phy *hdptx)
876 {
877 	u32 val = 0;
878 	int i;
879 
880 	reset_deassert(&hdptx->lane_reset);
881 
882 	val = (HDPTX_I_BIAS_EN | HDPTX_I_BGR_EN) << 16 | HDPTX_I_BIAS_EN |
883 		HDPTX_I_BGR_EN;
884 	hdptx_grf_write(hdptx, GRF_HDPTX_CON0, val);
885 
886 	/* 3 lanes frl mode */
887 	if (hdptx->rate == FRL_6G_3LANES || hdptx->rate == FRL_3G_3LANES)
888 		hdptx_write(hdptx, LNTOP_REG0207, 0x07);
889 	else
890 		hdptx_write(hdptx, LNTOP_REG0207, 0x0f);
891 
892 	val = 0;
893 	for (i = 0; i < 50; i++) {
894 		val = hdptx_grf_read(hdptx, GRF_HDPTX_STATUS);
895 
896 		if (val & HDPTX_O_PHY_RDY && val & HDPTX_O_PLL_LOCK_DONE)
897 			break;
898 		udelay(100);
899 	}
900 
901 	if (i == 50) {
902 		dev_err(hdptx->dev, "hdptx phy lane can't ready!\n");
903 		return -EINVAL;
904 	}
905 
906 	dev_err(hdptx->dev, "hdptx phy lane locked!\n");
907 
908 	return 0;
909 }
910 
hdptx_post_enable_pll(struct rockchip_hdptx_phy * hdptx)911 static int hdptx_post_enable_pll(struct rockchip_hdptx_phy *hdptx)
912 {
913 	u32 val = 0;
914 	int i;
915 
916 	val = (HDPTX_I_BIAS_EN | HDPTX_I_BGR_EN) << 16 | HDPTX_I_BIAS_EN |
917 		HDPTX_I_BGR_EN;
918 	hdptx_grf_write(hdptx, GRF_HDPTX_CON0, val);
919 	udelay(10);
920 	reset_deassert(&hdptx->init_reset);
921 	udelay(10);
922 	val = HDPTX_I_PLL_EN << 16 | HDPTX_I_PLL_EN;
923 	hdptx_grf_write(hdptx, GRF_HDPTX_CON0, val);
924 	udelay(10);
925 	reset_deassert(&hdptx->cmn_reset);
926 
927 	val = 0;
928 	for (i = 0; i < 50; i++) {
929 		val = hdptx_grf_read(hdptx, GRF_HDPTX_STATUS);
930 
931 		if (val & HDPTX_O_PHY_CLK_RDY)
932 			break;
933 		udelay(20);
934 	}
935 
936 	if (i == 50) {
937 		dev_err(hdptx->dev, "hdptx phy pll can't lock!\n");
938 		return -EINVAL;
939 	}
940 
941 	hdptx->pll_locked = true;
942 	dev_err(hdptx->dev, "hdptx phy pll locked!\n");
943 
944 	return 0;
945 }
946 
hdptx_phy_clk_pll_calc(unsigned int data_rate,struct ropll_config * cfg)947 static bool hdptx_phy_clk_pll_calc(unsigned int data_rate,
948 				   struct ropll_config *cfg)
949 {
950 	unsigned int fref = 24000;
951 	unsigned int sdc;
952 	unsigned int fout = data_rate / 2;
953 	unsigned int fvco;
954 	u32 mdiv, sdiv, n = 8;
955 	unsigned long k = 0, lc, k_sub, lc_sub;
956 
957 	for (sdiv = 1; sdiv <= 16; sdiv++) {
958 		if (sdiv % 2 && sdiv != 1)
959 			continue;
960 
961 		fvco = fout * sdiv;
962 
963 		if (fvco < 2000000 || fvco > 4000000)
964 			continue;
965 
966 		mdiv = DIV_ROUND_UP(fvco, fref);
967 		if (mdiv < 20 || mdiv > 255)
968 			continue;
969 
970 		if (fref * mdiv - fvco) {
971 			for (sdc = 264000; sdc <= 750000; sdc += fref)
972 				if (sdc * n > fref * mdiv)
973 					break;
974 
975 			if (sdc > 750000)
976 				continue;
977 
978 			rational_best_approximation(fref * mdiv - fvco,
979 						    sdc / 16,
980 						    GENMASK(6, 0),
981 						    GENMASK(7, 0),
982 						    &k, &lc);
983 
984 			rational_best_approximation(sdc * n - fref * mdiv,
985 						    sdc,
986 						    GENMASK(6, 0),
987 						    GENMASK(7, 0),
988 						    &k_sub, &lc_sub);
989 		}
990 
991 		break;
992 	}
993 
994 	if (sdiv > 16)
995 		return false;
996 
997 	if (cfg) {
998 		cfg->pms_mdiv = mdiv;
999 		cfg->pms_mdiv_afc = mdiv;
1000 		cfg->pms_pdiv = 1;
1001 		cfg->pms_refdiv = 1;
1002 		cfg->pms_sdiv = sdiv - 1;
1003 
1004 		cfg->sdm_en = k > 0 ? 1 : 0;
1005 		if (cfg->sdm_en) {
1006 			cfg->sdm_deno = lc;
1007 			cfg->sdm_num_sign = 1;
1008 			cfg->sdm_num = k;
1009 			cfg->sdc_n = n - 3;
1010 			cfg->sdc_num = k_sub;
1011 			cfg->sdc_deno = lc_sub;
1012 		}
1013 	}
1014 
1015 	return true;
1016 }
1017 
hdptx_lcpll_cmn_config(struct rockchip_hdptx_phy * hdptx,unsigned long bit_rate)1018 static int hdptx_lcpll_cmn_config(struct rockchip_hdptx_phy *hdptx, unsigned long bit_rate)
1019 {
1020 	u8 color_depth = (bit_rate & COLOR_DEPTH_MASK) ? 1 : 0;
1021 	struct lcpll_config *cfg = lcpll_cfg;
1022 
1023 	printf("%s rate:%lu\n", __func__, bit_rate);
1024 	hdptx->rate = bit_rate * 100;
1025 
1026 	for (; cfg->bit_rate != ~0; cfg++)
1027 		if (bit_rate == cfg->bit_rate)
1028 			break;
1029 
1030 	if (cfg->bit_rate == ~0)
1031 		return -EINVAL;
1032 
1033 	hdptx_pre_power_up(hdptx);
1034 
1035 	hdptx_grf_write(hdptx, GRF_HDPTX_CON0, LC_REF_CLK_SEL << 16);
1036 
1037 	hdptx_update_bits(hdptx, CMN_REG0008, LCPLL_EN_MASK |
1038 		       LCPLL_LCVCO_MODE_EN_MASK, LCPLL_EN(1) |
1039 		       LCPLL_LCVCO_MODE_EN(cfg->lcvco_mode_en));
1040 	hdptx_write(hdptx, CMN_REG0009, 0x0c);
1041 	hdptx_write(hdptx, CMN_REG000A, 0x83);
1042 	hdptx_write(hdptx, CMN_REG000B, 0x06);
1043 	hdptx_write(hdptx, CMN_REG000C, 0x20);
1044 	hdptx_write(hdptx, CMN_REG000D, 0xb8);
1045 	hdptx_write(hdptx, CMN_REG000E, 0x0f);
1046 	hdptx_write(hdptx, CMN_REG000F, 0x0f);
1047 	hdptx_write(hdptx, CMN_REG0010, 0x04);
1048 	hdptx_write(hdptx, CMN_REG0011, 0x00);
1049 	hdptx_write(hdptx, CMN_REG0012, 0x26);
1050 	hdptx_write(hdptx, CMN_REG0013, 0x22);
1051 	hdptx_write(hdptx, CMN_REG0014, 0x24);
1052 	hdptx_write(hdptx, CMN_REG0015, 0x77);
1053 	hdptx_write(hdptx, CMN_REG0016, 0x08);
1054 	hdptx_write(hdptx, CMN_REG0017, 0x00);
1055 	hdptx_write(hdptx, CMN_REG0018, 0x04);
1056 	hdptx_write(hdptx, CMN_REG0019, 0x48);
1057 	hdptx_write(hdptx, CMN_REG001A, 0x01);
1058 	hdptx_write(hdptx, CMN_REG001B, 0x00);
1059 	hdptx_write(hdptx, CMN_REG001C, 0x01);
1060 	hdptx_write(hdptx, CMN_REG001D, 0x64);
1061 	hdptx_update_bits(hdptx, CMN_REG001E, LCPLL_PI_EN_MASK |
1062 		       LCPLL_100M_CLK_EN_MASK,
1063 		       LCPLL_PI_EN(cfg->pi_en) |
1064 		       LCPLL_100M_CLK_EN(cfg->clk_en_100m));
1065 	hdptx_write(hdptx, CMN_REG001F, 0x00);
1066 	hdptx_write(hdptx, CMN_REG0020, cfg->pms_mdiv);
1067 	hdptx_write(hdptx, CMN_REG0021, cfg->pms_mdiv_afc);
1068 	hdptx_write(hdptx, CMN_REG0022, (cfg->pms_pdiv << 4) | cfg->pms_refdiv);
1069 	hdptx_write(hdptx, CMN_REG0023, (cfg->pms_sdiv << 4) | cfg->pms_sdiv);
1070 	hdptx_write(hdptx, CMN_REG0025, 0x10);
1071 	hdptx_write(hdptx, CMN_REG0026, 0x53);
1072 	hdptx_write(hdptx, CMN_REG0027, 0x01);
1073 	hdptx_write(hdptx, CMN_REG0028, 0x0d);
1074 	hdptx_write(hdptx, CMN_REG0029, 0x01);
1075 	hdptx_write(hdptx, CMN_REG002A, cfg->sdm_deno);
1076 	hdptx_write(hdptx, CMN_REG002B, cfg->sdm_num_sign);
1077 	hdptx_write(hdptx, CMN_REG002C, cfg->sdm_num);
1078 	hdptx_update_bits(hdptx, CMN_REG002D, LCPLL_SDC_N_MASK,
1079 			  LCPLL_SDC_N(cfg->sdc_n));
1080 	hdptx_write(hdptx, CMN_REG002E, 0x02);
1081 	hdptx_write(hdptx, CMN_REG002F, 0x0d);
1082 	hdptx_write(hdptx, CMN_REG0030, 0x00);
1083 	hdptx_write(hdptx, CMN_REG0031, 0x20);
1084 	hdptx_write(hdptx, CMN_REG0032, 0x30);
1085 	hdptx_write(hdptx, CMN_REG0033, 0x0b);
1086 	hdptx_write(hdptx, CMN_REG0034, 0x23);
1087 	hdptx_write(hdptx, CMN_REG0035, 0x00);
1088 	hdptx_write(hdptx, CMN_REG0038, 0x00);
1089 	hdptx_write(hdptx, CMN_REG0039, 0x00);
1090 	hdptx_write(hdptx, CMN_REG003A, 0x00);
1091 	hdptx_write(hdptx, CMN_REG003B, 0x00);
1092 	hdptx_write(hdptx, CMN_REG003C, 0x80);
1093 	hdptx_write(hdptx, CMN_REG003D, 0x00);
1094 	hdptx_write(hdptx, CMN_REG003E, 0x0c);
1095 	hdptx_write(hdptx, CMN_REG003F, 0x83);
1096 	hdptx_write(hdptx, CMN_REG0040, 0x06);
1097 	hdptx_write(hdptx, CMN_REG0041, 0x20);
1098 	hdptx_write(hdptx, CMN_REG0042, 0xb8);
1099 	hdptx_write(hdptx, CMN_REG0043, 0x00);
1100 	hdptx_write(hdptx, CMN_REG0044, 0x46);
1101 	hdptx_write(hdptx, CMN_REG0045, 0x24);
1102 	hdptx_write(hdptx, CMN_REG0046, 0xff);
1103 	hdptx_write(hdptx, CMN_REG0047, 0x00);
1104 	hdptx_write(hdptx, CMN_REG0048, 0x44);
1105 	hdptx_write(hdptx, CMN_REG0049, 0xfa);
1106 	hdptx_write(hdptx, CMN_REG004A, 0x08);
1107 	hdptx_write(hdptx, CMN_REG004B, 0x00);
1108 	hdptx_write(hdptx, CMN_REG004C, 0x01);
1109 	hdptx_write(hdptx, CMN_REG004D, 0x64);
1110 	hdptx_write(hdptx, CMN_REG004E, 0x14);
1111 	hdptx_write(hdptx, CMN_REG004F, 0x00);
1112 	hdptx_write(hdptx, CMN_REG0050, 0x00);
1113 	hdptx_write(hdptx, CMN_REG0051, 0x00);
1114 	hdptx_write(hdptx, CMN_REG0055, 0x00);
1115 	hdptx_write(hdptx, CMN_REG0059, 0x11);
1116 	hdptx_write(hdptx, CMN_REG005A, 0x03);
1117 	hdptx_write(hdptx, CMN_REG005C, 0x05);
1118 	hdptx_write(hdptx, CMN_REG005D, 0x0c);
1119 	hdptx_write(hdptx, CMN_REG005E, 0x07);
1120 	hdptx_write(hdptx, CMN_REG005F, 0x01);
1121 	hdptx_write(hdptx, CMN_REG0060, 0x01);
1122 	hdptx_write(hdptx, CMN_REG0064, 0x07);
1123 	hdptx_write(hdptx, CMN_REG0065, 0x00);
1124 	hdptx_write(hdptx, CMN_REG0069, 0x00);
1125 	hdptx_write(hdptx, CMN_REG006B, 0x04);
1126 	hdptx_write(hdptx, CMN_REG006C, 0x00);
1127 	hdptx_write(hdptx, CMN_REG0070, 0x01);
1128 	hdptx_write(hdptx, CMN_REG0073, 0x30);
1129 	hdptx_write(hdptx, CMN_REG0074, 0x00);
1130 	hdptx_write(hdptx, CMN_REG0075, 0x20);
1131 	hdptx_write(hdptx, CMN_REG0076, 0x30);
1132 	hdptx_write(hdptx, CMN_REG0077, 0x08);
1133 	hdptx_write(hdptx, CMN_REG0078, 0x0c);
1134 	hdptx_write(hdptx, CMN_REG0079, 0x00);
1135 	hdptx_write(hdptx, CMN_REG007B, 0x00);
1136 	hdptx_write(hdptx, CMN_REG007C, 0x00);
1137 	hdptx_write(hdptx, CMN_REG007D, 0x00);
1138 	hdptx_write(hdptx, CMN_REG007E, 0x00);
1139 	hdptx_write(hdptx, CMN_REG007F, 0x00);
1140 	hdptx_write(hdptx, CMN_REG0080, 0x00);
1141 	hdptx_write(hdptx, CMN_REG0081, 0x09);
1142 	hdptx_write(hdptx, CMN_REG0082, 0x04);
1143 	hdptx_write(hdptx, CMN_REG0083, 0x24);
1144 	hdptx_write(hdptx, CMN_REG0084, 0x20);
1145 	hdptx_write(hdptx, CMN_REG0085, 0x03);
1146 	hdptx_write(hdptx, CMN_REG0086, 0x01);
1147 	hdptx_update_bits(hdptx, CMN_REG0086, PLL_PCG_POSTDIV_SEL_MASK,
1148 			  PLL_PCG_POSTDIV_SEL(cfg->pms_sdiv));
1149 	hdptx_update_bits(hdptx, CMN_REG0086, PLL_PCG_CLK_SEL_MASK,
1150 			  PLL_PCG_CLK_SEL(color_depth));
1151 	hdptx_write(hdptx, CMN_REG0087, 0x0c);
1152 	hdptx_write(hdptx, CMN_REG0089, 0x02);
1153 	hdptx_write(hdptx, CMN_REG008A, 0x55);
1154 	hdptx_write(hdptx, CMN_REG008B, 0x25);
1155 	hdptx_write(hdptx, CMN_REG008C, 0x2c);
1156 	hdptx_write(hdptx, CMN_REG008D, 0x22);
1157 	hdptx_write(hdptx, CMN_REG008E, 0x14);
1158 	hdptx_write(hdptx, CMN_REG008F, 0x20);
1159 	hdptx_write(hdptx, CMN_REG0090, 0x00);
1160 	hdptx_write(hdptx, CMN_REG0091, 0x00);
1161 	hdptx_write(hdptx, CMN_REG0092, 0x00);
1162 	hdptx_write(hdptx, CMN_REG0093, 0x00);
1163 	hdptx_write(hdptx, CMN_REG0095, 0x00);
1164 	hdptx_write(hdptx, CMN_REG0097, 0x00);
1165 	hdptx_write(hdptx, CMN_REG0099, 0x00);
1166 	hdptx_write(hdptx, CMN_REG009A, 0x11);
1167 	hdptx_write(hdptx, CMN_REG009B, 0x10);
1168 	hdptx_write(hdptx, SB_REG0114, 0x00);
1169 	hdptx_write(hdptx, SB_REG0115, 0x00);
1170 	hdptx_write(hdptx, SB_REG0116, 0x00);
1171 	hdptx_write(hdptx, SB_REG0117, 0x00);
1172 
1173 	return hdptx_post_enable_pll(hdptx);
1174 }
1175 
hdptx_ropll_cmn_config(struct rockchip_hdptx_phy * hdptx,unsigned long bit_rate)1176 static int hdptx_ropll_cmn_config(struct rockchip_hdptx_phy *hdptx, unsigned long bit_rate)
1177 {
1178 	int bus_width = hdptx->bus_width;
1179 	u8 color_depth = (bus_width & COLOR_DEPTH_MASK) ? 1 : 0;
1180 	struct ropll_config *cfg = ropll_tmds_cfg;
1181 	struct ropll_config rc = {0};
1182 
1183 	printf("%s bus_width:%x rate:%lu\n", __func__, bus_width, bit_rate);
1184 	hdptx->rate = bit_rate * 100;
1185 
1186 	for (; cfg->bit_rate != ~0; cfg++)
1187 		if (bit_rate == cfg->bit_rate)
1188 			break;
1189 
1190 	if (cfg->bit_rate == ~0) {
1191 		if (hdptx_phy_clk_pll_calc(bit_rate, &rc)) {
1192 			cfg = &rc;
1193 		} else {
1194 			dev_err(hdptx->dev, "%s can't find pll cfg\n", __func__);
1195 			return -EINVAL;
1196 		}
1197 	}
1198 
1199 	dev_dbg(hdptx->dev, "mdiv=%u, sdiv=%u\n",
1200 		cfg->pms_mdiv, cfg->pms_sdiv + 1);
1201 	dev_dbg(hdptx->dev, "sdm_en=%u, k_sign=%u, k=%u, lc=%u",
1202 		cfg->sdm_en, cfg->sdm_num_sign, cfg->sdm_num, cfg->sdm_deno);
1203 	dev_dbg(hdptx->dev, "n=%u, k_sub=%u, lc_sub=%u\n",
1204 		cfg->sdc_n + 3, cfg->sdc_num, cfg->sdc_deno);
1205 
1206 	hdptx_pre_power_up(hdptx);
1207 
1208 	hdptx_grf_write(hdptx, GRF_HDPTX_CON0, LC_REF_CLK_SEL << 16);
1209 
1210 	hdptx_write(hdptx, CMN_REG0008, 0x00);
1211 	hdptx_write(hdptx, CMN_REG0009, 0x0c);
1212 	hdptx_write(hdptx, CMN_REG000A, 0x83);
1213 	hdptx_write(hdptx, CMN_REG000B, 0x06);
1214 	hdptx_write(hdptx, CMN_REG000C, 0x20);
1215 	hdptx_write(hdptx, CMN_REG000D, 0xb8);
1216 	hdptx_write(hdptx, CMN_REG000E, 0x0f);
1217 	hdptx_write(hdptx, CMN_REG000F, 0x0f);
1218 	hdptx_write(hdptx, CMN_REG0010, 0x04);
1219 	hdptx_write(hdptx, CMN_REG0011, 0x01);
1220 	hdptx_write(hdptx, CMN_REG0012, 0x26);
1221 	hdptx_write(hdptx, CMN_REG0013, 0x22);
1222 	hdptx_write(hdptx, CMN_REG0014, 0x24);
1223 	hdptx_write(hdptx, CMN_REG0015, 0x77);
1224 	hdptx_write(hdptx, CMN_REG0016, 0x08);
1225 	hdptx_write(hdptx, CMN_REG0017, 0x20);
1226 	hdptx_write(hdptx, CMN_REG0018, 0x04);
1227 	hdptx_write(hdptx, CMN_REG0019, 0x48);
1228 	hdptx_write(hdptx, CMN_REG001A, 0x01);
1229 	hdptx_write(hdptx, CMN_REG001B, 0x00);
1230 	hdptx_write(hdptx, CMN_REG001C, 0x01);
1231 	hdptx_write(hdptx, CMN_REG001D, 0x64);
1232 	hdptx_write(hdptx, CMN_REG001E, 0x14);
1233 	hdptx_write(hdptx, CMN_REG001F, 0x00);
1234 	hdptx_write(hdptx, CMN_REG0020, 0x00);
1235 	hdptx_write(hdptx, CMN_REG0021, 0x00);
1236 	hdptx_write(hdptx, CMN_REG0022, 0x11);
1237 	hdptx_write(hdptx, CMN_REG0023, 0x00);
1238 	hdptx_write(hdptx, CMN_REG0024, 0x00);
1239 	hdptx_write(hdptx, CMN_REG0025, 0x53);
1240 	hdptx_write(hdptx, CMN_REG0026, 0x00);
1241 	hdptx_write(hdptx, CMN_REG0027, 0x00);
1242 	hdptx_write(hdptx, CMN_REG0028, 0x01);
1243 	hdptx_write(hdptx, CMN_REG0029, 0x01);
1244 	hdptx_write(hdptx, CMN_REG002A, 0x00);
1245 	hdptx_write(hdptx, CMN_REG002B, 0x00);
1246 	hdptx_write(hdptx, CMN_REG002C, 0x00);
1247 	hdptx_write(hdptx, CMN_REG002D, 0x00);
1248 	hdptx_write(hdptx, CMN_REG002E, 0x04);
1249 	hdptx_write(hdptx, CMN_REG002F, 0x00);
1250 	hdptx_write(hdptx, CMN_REG0030, 0x20);
1251 	hdptx_write(hdptx, CMN_REG0031, 0x30);
1252 	hdptx_write(hdptx, CMN_REG0032, 0x0b);
1253 	hdptx_write(hdptx, CMN_REG0033, 0x23);
1254 	hdptx_write(hdptx, CMN_REG0034, 0x00);
1255 	hdptx_write(hdptx, CMN_REG0035, 0x00);
1256 	hdptx_write(hdptx, CMN_REG0038, 0x00);
1257 	hdptx_write(hdptx, CMN_REG0039, 0x00);
1258 	hdptx_write(hdptx, CMN_REG003A, 0x00);
1259 	hdptx_write(hdptx, CMN_REG003B, 0x00);
1260 	hdptx_write(hdptx, CMN_REG003C, 0x80);
1261 	hdptx_write(hdptx, CMN_REG003D, 0x40);
1262 	hdptx_write(hdptx, CMN_REG003E, 0x0c);
1263 	hdptx_write(hdptx, CMN_REG003F, 0x83);
1264 	hdptx_write(hdptx, CMN_REG0040, 0x06);
1265 	hdptx_write(hdptx, CMN_REG0041, 0x20);
1266 	hdptx_write(hdptx, CMN_REG0042, 0x78);
1267 	hdptx_write(hdptx, CMN_REG0043, 0x00);
1268 	hdptx_write(hdptx, CMN_REG0044, 0x46);
1269 	hdptx_write(hdptx, CMN_REG0045, 0x24);
1270 	hdptx_write(hdptx, CMN_REG0046, 0xdd);
1271 	hdptx_write(hdptx, CMN_REG0047, 0x00);
1272 	hdptx_write(hdptx, CMN_REG0048, 0x11);
1273 	hdptx_write(hdptx, CMN_REG0049, 0xfa);
1274 	hdptx_write(hdptx, CMN_REG004A, 0x08);
1275 	hdptx_write(hdptx, CMN_REG004B, 0x00);
1276 	hdptx_write(hdptx, CMN_REG004C, 0x01);
1277 	hdptx_write(hdptx, CMN_REG004D, 0x64);
1278 	hdptx_write(hdptx, CMN_REG004E, 0x34);
1279 	hdptx_write(hdptx, CMN_REG004F, 0x00);
1280 	hdptx_write(hdptx, CMN_REG0050, 0x00);
1281 
1282 	hdptx_write(hdptx, CMN_REG0051, cfg->pms_mdiv);
1283 	hdptx_write(hdptx, CMN_REG0055, cfg->pms_mdiv_afc);
1284 
1285 	hdptx_write(hdptx, CMN_REG0059, (cfg->pms_pdiv << 4) | cfg->pms_refdiv);
1286 
1287 	hdptx_write(hdptx, CMN_REG005A, (cfg->pms_sdiv << 4));
1288 
1289 	hdptx_write(hdptx, CMN_REG005C, 0x25);
1290 	hdptx_write(hdptx, CMN_REG005D, 0x0c);
1291 	hdptx_write(hdptx, CMN_REG005E, 0x4f);
1292 	hdptx_update_bits(hdptx, CMN_REG005E, ROPLL_SDM_EN_MASK,
1293 			  ROPLL_SDM_EN(cfg->sdm_en));
1294 	if (!cfg->sdm_en)
1295 		hdptx_update_bits(hdptx, CMN_REG005E, 0xf, 0);
1296 
1297 	hdptx_write(hdptx, CMN_REG005F, 0x01);
1298 
1299 	hdptx_update_bits(hdptx, CMN_REG0064, ROPLL_SDM_NUM_SIGN_RBR_MASK,
1300 			  ROPLL_SDM_NUM_SIGN_RBR(cfg->sdm_num_sign));
1301 	hdptx_write(hdptx, CMN_REG0065, cfg->sdm_num);
1302 	hdptx_write(hdptx, CMN_REG0060, cfg->sdm_deno);
1303 
1304 	hdptx_update_bits(hdptx, CMN_REG0069, ROPLL_SDC_N_RBR_MASK,
1305 			  ROPLL_SDC_N_RBR(cfg->sdc_n));
1306 
1307 	hdptx_write(hdptx, CMN_REG006C, cfg->sdc_num);
1308 	hdptx_write(hdptx, CMN_REG0070, cfg->sdc_deno);
1309 
1310 	hdptx_write(hdptx, CMN_REG006B, 0x04);
1311 
1312 	hdptx_write(hdptx, CMN_REG0073, 0x30);
1313 	hdptx_write(hdptx, CMN_REG0074, 0x04);
1314 	hdptx_write(hdptx, CMN_REG0075, 0x20);
1315 	hdptx_write(hdptx, CMN_REG0076, 0x30);
1316 	hdptx_write(hdptx, CMN_REG0077, 0x08);
1317 	hdptx_write(hdptx, CMN_REG0078, 0x0c);
1318 	hdptx_write(hdptx, CMN_REG0079, 0x00);
1319 	hdptx_write(hdptx, CMN_REG007B, 0x00);
1320 	hdptx_write(hdptx, CMN_REG007C, 0x00);
1321 	hdptx_write(hdptx, CMN_REG007D, 0x00);
1322 	hdptx_write(hdptx, CMN_REG007E, 0x00);
1323 	hdptx_write(hdptx, CMN_REG007F, 0x00);
1324 	hdptx_write(hdptx, CMN_REG0080, 0x00);
1325 	hdptx_write(hdptx, CMN_REG0081, 0x01);
1326 	hdptx_write(hdptx, CMN_REG0082, 0x04);
1327 	hdptx_write(hdptx, CMN_REG0083, 0x24);
1328 	hdptx_write(hdptx, CMN_REG0084, 0x20);
1329 	hdptx_write(hdptx, CMN_REG0085, 0x03);
1330 
1331 	hdptx_update_bits(hdptx, CMN_REG0086, PLL_PCG_POSTDIV_SEL_MASK,
1332 			  PLL_PCG_POSTDIV_SEL(cfg->pms_sdiv));
1333 
1334 	hdptx_update_bits(hdptx, CMN_REG0086, PLL_PCG_CLK_SEL_MASK,
1335 			  PLL_PCG_CLK_SEL(color_depth));
1336 
1337 	hdptx_update_bits(hdptx, CMN_REG0086, PLL_PCG_CLK_EN, PLL_PCG_CLK_EN);
1338 
1339 	hdptx_write(hdptx, CMN_REG0087, 0x04);
1340 	hdptx_write(hdptx, CMN_REG0089, 0x00);
1341 	hdptx_write(hdptx, CMN_REG008A, 0x55);
1342 	hdptx_write(hdptx, CMN_REG008B, 0x25);
1343 	hdptx_write(hdptx, CMN_REG008C, 0x2c);
1344 	hdptx_write(hdptx, CMN_REG008D, 0x22);
1345 	hdptx_write(hdptx, CMN_REG008E, 0x14);
1346 	hdptx_write(hdptx, CMN_REG008F, 0x20);
1347 	hdptx_write(hdptx, CMN_REG0090, 0x00);
1348 	hdptx_write(hdptx, CMN_REG0091, 0x00);
1349 	hdptx_write(hdptx, CMN_REG0092, 0x00);
1350 	hdptx_write(hdptx, CMN_REG0093, 0x00);
1351 	hdptx_write(hdptx, CMN_REG0095, 0x00);
1352 	hdptx_write(hdptx, CMN_REG0097, 0x02);
1353 	hdptx_write(hdptx, CMN_REG0099, 0x04);
1354 	hdptx_write(hdptx, CMN_REG009A, 0x11);
1355 	hdptx_write(hdptx, CMN_REG009B, 0x00);
1356 
1357 	return hdptx_post_enable_pll(hdptx);
1358 }
1359 
hdptx_ropll_tmds_mode_config(struct rockchip_hdptx_phy * hdptx,u32 rate)1360 static int hdptx_ropll_tmds_mode_config(struct rockchip_hdptx_phy *hdptx, u32 rate)
1361 {
1362 	u32 bit_rate = rate & DATA_RATE_MASK;
1363 	u8 color_depth = (rate & COLOR_DEPTH_MASK) ? 1 : 0;
1364 
1365 	if (color_depth)
1366 		bit_rate = bit_rate * 5 / 4;
1367 
1368 	hdptx_write(hdptx, SB_REG0114, 0x00);
1369 	hdptx_write(hdptx, SB_REG0115, 0x00);
1370 	hdptx_write(hdptx, SB_REG0116, 0x00);
1371 	hdptx_write(hdptx, SB_REG0117, 0x00);
1372 	hdptx_write(hdptx, LNTOP_REG0200, 0x06);
1373 
1374 	if (bit_rate >= 3400000) {
1375 		/* For 1/40 bitrate clk */
1376 		hdptx_write(hdptx, LNTOP_REG0201, 0x00);
1377 		hdptx_write(hdptx, LNTOP_REG0202, 0x00);
1378 		hdptx_write(hdptx, LNTOP_REG0203, 0x0f);
1379 		hdptx_write(hdptx, LNTOP_REG0204, 0xff);
1380 		hdptx_write(hdptx, LNTOP_REG0205, 0xff);
1381 	} else {
1382 		/* For 1/10 bitrate clk */
1383 		hdptx_write(hdptx, LNTOP_REG0201, 0x07);
1384 		hdptx_write(hdptx, LNTOP_REG0202, 0xc1);
1385 		hdptx_write(hdptx, LNTOP_REG0203, 0xf0);
1386 		hdptx_write(hdptx, LNTOP_REG0204, 0x7c);
1387 		hdptx_write(hdptx, LNTOP_REG0205, 0x1f);
1388 	}
1389 
1390 	hdptx_write(hdptx, LNTOP_REG0206, 0x07);
1391 	hdptx_write(hdptx, LANE_REG0303, 0x0c);
1392 	hdptx_write(hdptx, LANE_REG0307, 0x20);
1393 	hdptx_write(hdptx, LANE_REG030A, 0x17);
1394 	hdptx_write(hdptx, LANE_REG030B, 0x77);
1395 	hdptx_write(hdptx, LANE_REG030C, 0x77);
1396 	hdptx_write(hdptx, LANE_REG030D, 0x77);
1397 	hdptx_write(hdptx, LANE_REG030E, 0x38);
1398 	hdptx_write(hdptx, LANE_REG0310, 0x03);
1399 	hdptx_write(hdptx, LANE_REG0311, 0x0f);
1400 	hdptx_write(hdptx, LANE_REG0312, 0x00);
1401 	hdptx_write(hdptx, LANE_REG0316, 0x02);
1402 	hdptx_write(hdptx, LANE_REG031B, 0x01);
1403 	hdptx_write(hdptx, LANE_REG031E, 0x00);
1404 	hdptx_write(hdptx, LANE_REG031F, 0x15);
1405 	hdptx_write(hdptx, LANE_REG0320, 0xa0);
1406 	hdptx_write(hdptx, LANE_REG0403, 0x0c);
1407 	hdptx_write(hdptx, LANE_REG0407, 0x20);
1408 	hdptx_write(hdptx, LANE_REG040A, 0x17);
1409 	hdptx_write(hdptx, LANE_REG040B, 0x77);
1410 	hdptx_write(hdptx, LANE_REG040C, 0x77);
1411 	hdptx_write(hdptx, LANE_REG040D, 0x77);
1412 	hdptx_write(hdptx, LANE_REG040E, 0x38);
1413 	hdptx_write(hdptx, LANE_REG0410, 0x03);
1414 	hdptx_write(hdptx, LANE_REG0411, 0x0f);
1415 	hdptx_write(hdptx, LANE_REG0412, 0x00);
1416 	hdptx_write(hdptx, LANE_REG0416, 0x02);
1417 	hdptx_write(hdptx, LANE_REG041B, 0x01);
1418 	hdptx_write(hdptx, LANE_REG041E, 0x00);
1419 	hdptx_write(hdptx, LANE_REG041F, 0x15);
1420 	hdptx_write(hdptx, LANE_REG0420, 0xa0);
1421 	hdptx_write(hdptx, LANE_REG0503, 0x0c);
1422 	hdptx_write(hdptx, LANE_REG0507, 0x20);
1423 	hdptx_write(hdptx, LANE_REG050A, 0x17);
1424 	hdptx_write(hdptx, LANE_REG050B, 0x77);
1425 	hdptx_write(hdptx, LANE_REG050C, 0x77);
1426 	hdptx_write(hdptx, LANE_REG050D, 0x77);
1427 	hdptx_write(hdptx, LANE_REG050E, 0x38);
1428 	hdptx_write(hdptx, LANE_REG0510, 0x03);
1429 	hdptx_write(hdptx, LANE_REG0511, 0x0f);
1430 	hdptx_write(hdptx, LANE_REG0512, 0x00);
1431 	hdptx_write(hdptx, LANE_REG0516, 0x02);
1432 	hdptx_write(hdptx, LANE_REG051B, 0x01);
1433 	hdptx_write(hdptx, LANE_REG051E, 0x00);
1434 	hdptx_write(hdptx, LANE_REG051F, 0x15);
1435 	hdptx_write(hdptx, LANE_REG0520, 0xa0);
1436 	hdptx_write(hdptx, LANE_REG0603, 0x0c);
1437 	hdptx_write(hdptx, LANE_REG0607, 0x20);
1438 	hdptx_write(hdptx, LANE_REG060A, 0x17);
1439 	hdptx_write(hdptx, LANE_REG060B, 0x77);
1440 	hdptx_write(hdptx, LANE_REG060C, 0x77);
1441 	hdptx_write(hdptx, LANE_REG060D, 0x77);
1442 	hdptx_write(hdptx, LANE_REG060E, 0x38);
1443 	hdptx_write(hdptx, LANE_REG0610, 0x03);
1444 	hdptx_write(hdptx, LANE_REG0611, 0x0f);
1445 	hdptx_write(hdptx, LANE_REG0612, 0x00);
1446 	hdptx_write(hdptx, LANE_REG0616, 0x02);
1447 	hdptx_write(hdptx, LANE_REG061B, 0x01);
1448 	hdptx_write(hdptx, LANE_REG061E, 0x08);
1449 
1450 	/* fix Inter-Pair Skew exceed the limits */
1451 	hdptx_write(hdptx, LANE_REG031E, 0x02);
1452 	hdptx_write(hdptx, LANE_REG041E, 0x02);
1453 	hdptx_write(hdptx, LANE_REG051E, 0x02);
1454 	hdptx_write(hdptx, LANE_REG061E, 0x0a);
1455 
1456 	hdptx_write(hdptx, LANE_REG061F, 0x15);
1457 	hdptx_write(hdptx, LANE_REG0620, 0xa0);
1458 
1459 	hdptx_write(hdptx, LANE_REG0303, 0x2f);
1460 	hdptx_write(hdptx, LANE_REG0403, 0x2f);
1461 	hdptx_write(hdptx, LANE_REG0503, 0x2f);
1462 	hdptx_write(hdptx, LANE_REG0603, 0x2f);
1463 	hdptx_write(hdptx, LANE_REG0305, 0x03);
1464 	hdptx_write(hdptx, LANE_REG0405, 0x03);
1465 	hdptx_write(hdptx, LANE_REG0505, 0x03);
1466 	hdptx_write(hdptx, LANE_REG0605, 0x03);
1467 	hdptx_write(hdptx, LANE_REG0306, 0x1c);
1468 	hdptx_write(hdptx, LANE_REG0406, 0x1c);
1469 	hdptx_write(hdptx, LANE_REG0506, 0x1c);
1470 	hdptx_write(hdptx, LANE_REG0606, 0x1c);
1471 
1472 	return hdptx_post_enable_lane(hdptx);
1473 }
1474 
1475 static int
hdptx_lcpll_ropll_cmn_config(struct rockchip_hdptx_phy * hdptx,unsigned long rate)1476 hdptx_lcpll_ropll_cmn_config(struct rockchip_hdptx_phy *hdptx,
1477 			     unsigned long rate)
1478 {
1479 	u32 val;
1480 
1481 	printf("%s rate:%lu\n", __func__, rate);
1482 
1483 	hdptx->rate = rate * 100;
1484 
1485 	hdptx_pre_power_up(hdptx);
1486 
1487 	/* ROPLL input reference clock from LCPLL (cascade mode) */
1488 	val = (LC_REF_CLK_SEL << 16) | LC_REF_CLK_SEL;
1489 	hdptx_grf_write(hdptx, GRF_HDPTX_CON0, val);
1490 
1491 	hdptx_write(hdptx, CMN_REG0008, 0xd0);
1492 	hdptx_write(hdptx, CMN_REG0009, 0x0c);
1493 	hdptx_write(hdptx, CMN_REG000A, 0x83);
1494 	hdptx_write(hdptx, CMN_REG000B, 0x06);
1495 	hdptx_write(hdptx, CMN_REG000C, 0x20);
1496 	hdptx_write(hdptx, CMN_REG000D, 0xb8);
1497 	hdptx_write(hdptx, CMN_REG000E, 0x0f);
1498 	hdptx_write(hdptx, CMN_REG000F, 0x0f);
1499 	hdptx_write(hdptx, CMN_REG0010, 0x04);
1500 	hdptx_write(hdptx, CMN_REG0011, 0x00);
1501 	hdptx_write(hdptx, CMN_REG0012, 0x26);
1502 	hdptx_write(hdptx, CMN_REG0013, 0x22);
1503 	hdptx_write(hdptx, CMN_REG0014, 0x24);
1504 	hdptx_write(hdptx, CMN_REG0015, 0x77);
1505 	hdptx_write(hdptx, CMN_REG0016, 0x08);
1506 	hdptx_write(hdptx, CMN_REG0017, 0x00);
1507 	hdptx_write(hdptx, CMN_REG0018, 0x04);
1508 	hdptx_write(hdptx, CMN_REG0019, 0x48);
1509 	hdptx_write(hdptx, CMN_REG001A, 0x01);
1510 	hdptx_write(hdptx, CMN_REG001B, 0x00);
1511 	hdptx_write(hdptx, CMN_REG001C, 0x01);
1512 	hdptx_write(hdptx, CMN_REG001D, 0x64);
1513 	hdptx_write(hdptx, CMN_REG001E, 0x35);
1514 	hdptx_write(hdptx, CMN_REG001F, 0x00);
1515 	hdptx_write(hdptx, CMN_REG0020, 0x6b);
1516 	hdptx_write(hdptx, CMN_REG0021, 0x6b);
1517 	hdptx_write(hdptx, CMN_REG0022, 0x11);
1518 	hdptx_write(hdptx, CMN_REG0024, 0x00);
1519 	hdptx_write(hdptx, CMN_REG0025, 0x10);
1520 	hdptx_write(hdptx, CMN_REG0026, 0x53);
1521 	hdptx_write(hdptx, CMN_REG0027, 0x15);
1522 	hdptx_write(hdptx, CMN_REG0028, 0x0d);
1523 	hdptx_write(hdptx, CMN_REG0029, 0x01);
1524 	hdptx_write(hdptx, CMN_REG002A, 0x09);
1525 	hdptx_write(hdptx, CMN_REG002B, 0x01);
1526 	hdptx_write(hdptx, CMN_REG002C, 0x02);
1527 	hdptx_write(hdptx, CMN_REG002D, 0x02);
1528 	hdptx_write(hdptx, CMN_REG002E, 0x0d);
1529 	hdptx_write(hdptx, CMN_REG002F, 0x61);
1530 	hdptx_write(hdptx, CMN_REG0030, 0x00);
1531 	hdptx_write(hdptx, CMN_REG0031, 0x20);
1532 	hdptx_write(hdptx, CMN_REG0032, 0x30);
1533 	hdptx_write(hdptx, CMN_REG0033, 0x0b);
1534 	hdptx_write(hdptx, CMN_REG0034, 0x23);
1535 	hdptx_write(hdptx, CMN_REG0035, 0x00);
1536 	hdptx_write(hdptx, CMN_REG0037, 0x00);
1537 	hdptx_write(hdptx, CMN_REG0038, 0x00);
1538 	hdptx_write(hdptx, CMN_REG0039, 0x00);
1539 	hdptx_write(hdptx, CMN_REG003A, 0x00);
1540 	hdptx_write(hdptx, CMN_REG003B, 0x00);
1541 	hdptx_write(hdptx, CMN_REG003C, 0x80);
1542 	hdptx_write(hdptx, CMN_REG003D, 0xc0);
1543 	hdptx_write(hdptx, CMN_REG003E, 0x0c);
1544 	hdptx_write(hdptx, CMN_REG003F, 0x83);
1545 	hdptx_write(hdptx, CMN_REG0040, 0x06);
1546 	hdptx_write(hdptx, CMN_REG0041, 0x20);
1547 	hdptx_write(hdptx, CMN_REG0042, 0xb8);
1548 	hdptx_write(hdptx, CMN_REG0043, 0x00);
1549 	hdptx_write(hdptx, CMN_REG0044, 0x46);
1550 	hdptx_write(hdptx, CMN_REG0045, 0x24);
1551 	hdptx_write(hdptx, CMN_REG0046, 0xff);
1552 	hdptx_write(hdptx, CMN_REG0047, 0x00);
1553 	hdptx_write(hdptx, CMN_REG0048, 0x44);
1554 	hdptx_write(hdptx, CMN_REG0049, 0xfa);
1555 	hdptx_write(hdptx, CMN_REG004A, 0x08);
1556 	hdptx_write(hdptx, CMN_REG004B, 0x00);
1557 	hdptx_write(hdptx, CMN_REG004C, 0x01);
1558 	hdptx_write(hdptx, CMN_REG004D, 0x64);
1559 	hdptx_write(hdptx, CMN_REG004E, 0x14);
1560 	hdptx_write(hdptx, CMN_REG004F, 0x00);
1561 	hdptx_write(hdptx, CMN_REG0050, 0x00);
1562 	hdptx_write(hdptx, CMN_REG0054, 0x19);
1563 	hdptx_write(hdptx, CMN_REG0058, 0x19);
1564 	hdptx_write(hdptx, CMN_REG0059, 0x11);
1565 	hdptx_write(hdptx, CMN_REG005B, 0x30);
1566 	hdptx_write(hdptx, CMN_REG005C, 0x25);
1567 	hdptx_write(hdptx, CMN_REG005D, 0x14);
1568 	hdptx_write(hdptx, CMN_REG005E, 0x0e);
1569 	hdptx_write(hdptx, CMN_REG005F, 0x01);
1570 	hdptx_write(hdptx, CMN_REG0063, 0x01);
1571 	hdptx_write(hdptx, CMN_REG0064, 0x0e);
1572 	hdptx_write(hdptx, CMN_REG0068, 0x00);
1573 	hdptx_write(hdptx, CMN_REG0069, 0x02);
1574 	hdptx_write(hdptx, CMN_REG006B, 0x00);
1575 	hdptx_write(hdptx, CMN_REG006F, 0x00);
1576 	hdptx_write(hdptx, CMN_REG0073, 0x02);
1577 	hdptx_write(hdptx, CMN_REG0074, 0x00);
1578 	hdptx_write(hdptx, CMN_REG0075, 0x20);
1579 	hdptx_write(hdptx, CMN_REG0076, 0x30);
1580 	hdptx_write(hdptx, CMN_REG0077, 0x08);
1581 	hdptx_write(hdptx, CMN_REG0078, 0x0c);
1582 	hdptx_write(hdptx, CMN_REG007A, 0x00);
1583 	hdptx_write(hdptx, CMN_REG007B, 0x00);
1584 	hdptx_write(hdptx, CMN_REG007C, 0x00);
1585 	hdptx_write(hdptx, CMN_REG007D, 0x00);
1586 	hdptx_write(hdptx, CMN_REG007E, 0x00);
1587 	hdptx_write(hdptx, CMN_REG007F, 0x00);
1588 	hdptx_write(hdptx, CMN_REG0080, 0x00);
1589 	hdptx_write(hdptx, CMN_REG0081, 0x09);
1590 	hdptx_write(hdptx, CMN_REG0082, 0x04);
1591 	hdptx_write(hdptx, CMN_REG0083, 0x24);
1592 	hdptx_write(hdptx, CMN_REG0084, 0x20);
1593 	hdptx_write(hdptx, CMN_REG0085, 0x03);
1594 	hdptx_write(hdptx, CMN_REG0086, 0x11);
1595 	hdptx_write(hdptx, CMN_REG0087, 0x0c);
1596 	hdptx_write(hdptx, CMN_REG0089, 0x00);
1597 	hdptx_write(hdptx, CMN_REG008A, 0x55);
1598 	hdptx_write(hdptx, CMN_REG008B, 0x25);
1599 	hdptx_write(hdptx, CMN_REG008C, 0x2c);
1600 	hdptx_write(hdptx, CMN_REG008D, 0x22);
1601 	hdptx_write(hdptx, CMN_REG008E, 0x14);
1602 	hdptx_write(hdptx, CMN_REG008F, 0x20);
1603 	hdptx_write(hdptx, CMN_REG0090, 0x00);
1604 	hdptx_write(hdptx, CMN_REG0091, 0x00);
1605 	hdptx_write(hdptx, CMN_REG0092, 0x00);
1606 	hdptx_write(hdptx, CMN_REG0093, 0x00);
1607 	hdptx_write(hdptx, CMN_REG0095, 0x03);
1608 	hdptx_write(hdptx, CMN_REG0097, 0x00);
1609 	hdptx_write(hdptx, CMN_REG0099, 0x00);
1610 	hdptx_write(hdptx, CMN_REG009A, 0x11);
1611 	hdptx_write(hdptx, CMN_REG009B, 0x10);
1612 
1613 	hdptx_write(hdptx, CMN_REG009E, 0x03);
1614 	hdptx_write(hdptx, CMN_REG00A0, 0x60);
1615 	hdptx_write(hdptx, CMN_REG009F, 0xff);
1616 
1617 	return hdptx_post_enable_pll(hdptx);
1618 }
1619 
1620 
hdptx_lcpll_ropll_frl_mode_config(struct rockchip_hdptx_phy * hdptx)1621 static int hdptx_lcpll_ropll_frl_mode_config(struct rockchip_hdptx_phy *hdptx)
1622 {
1623 	hdptx_write(hdptx, SB_REG0114, 0x00);
1624 	hdptx_write(hdptx, SB_REG0115, 0x00);
1625 	hdptx_write(hdptx, SB_REG0116, 0x00);
1626 	hdptx_write(hdptx, SB_REG0117, 0x00);
1627 	hdptx_write(hdptx, LNTOP_REG0200, 0x04);
1628 	hdptx_write(hdptx, LNTOP_REG0201, 0x00);
1629 	hdptx_write(hdptx, LNTOP_REG0202, 0x00);
1630 	hdptx_write(hdptx, LNTOP_REG0203, 0xf0);
1631 	hdptx_write(hdptx, LNTOP_REG0204, 0xff);
1632 	hdptx_write(hdptx, LNTOP_REG0205, 0xff);
1633 	hdptx_write(hdptx, LNTOP_REG0206, 0x05);
1634 	hdptx_write(hdptx, LANE_REG0303, 0x0c);
1635 	hdptx_write(hdptx, LANE_REG0307, 0x20);
1636 	hdptx_write(hdptx, LANE_REG030A, 0x17);
1637 	hdptx_write(hdptx, LANE_REG030B, 0x77);
1638 	hdptx_write(hdptx, LANE_REG030C, 0x77);
1639 	hdptx_write(hdptx, LANE_REG030D, 0x77);
1640 	hdptx_write(hdptx, LANE_REG030E, 0x38);
1641 	hdptx_write(hdptx, LANE_REG0310, 0x03);
1642 	hdptx_write(hdptx, LANE_REG0311, 0x0f);
1643 	hdptx_write(hdptx, LANE_REG0312, 0x3c);
1644 	hdptx_write(hdptx, LANE_REG0316, 0x02);
1645 	hdptx_write(hdptx, LANE_REG031B, 0x01);
1646 	hdptx_write(hdptx, LANE_REG031F, 0x15);
1647 	hdptx_write(hdptx, LANE_REG0320, 0xa0);
1648 	hdptx_write(hdptx, LANE_REG0403, 0x0c);
1649 	hdptx_write(hdptx, LANE_REG0407, 0x20);
1650 	hdptx_write(hdptx, LANE_REG040A, 0x17);
1651 	hdptx_write(hdptx, LANE_REG040B, 0x77);
1652 	hdptx_write(hdptx, LANE_REG040C, 0x77);
1653 	hdptx_write(hdptx, LANE_REG040D, 0x77);
1654 	hdptx_write(hdptx, LANE_REG040E, 0x38);
1655 	hdptx_write(hdptx, LANE_REG0410, 0x03);
1656 	hdptx_write(hdptx, LANE_REG0411, 0x0f);
1657 	hdptx_write(hdptx, LANE_REG0412, 0x3c);
1658 	hdptx_write(hdptx, LANE_REG0416, 0x02);
1659 	hdptx_write(hdptx, LANE_REG041B, 0x01);
1660 	hdptx_write(hdptx, LANE_REG041F, 0x15);
1661 	hdptx_write(hdptx, LANE_REG0420, 0xa0);
1662 	hdptx_write(hdptx, LANE_REG0503, 0x0c);
1663 	hdptx_write(hdptx, LANE_REG0507, 0x20);
1664 	hdptx_write(hdptx, LANE_REG050A, 0x17);
1665 	hdptx_write(hdptx, LANE_REG050B, 0x77);
1666 	hdptx_write(hdptx, LANE_REG050C, 0x77);
1667 	hdptx_write(hdptx, LANE_REG050D, 0x77);
1668 	hdptx_write(hdptx, LANE_REG0507, 0x20);
1669 	hdptx_write(hdptx, LANE_REG050A, 0x17);
1670 	hdptx_write(hdptx, LANE_REG050B, 0x77);
1671 	hdptx_write(hdptx, LANE_REG050C, 0x77);
1672 	hdptx_write(hdptx, LANE_REG050D, 0x77);
1673 	hdptx_write(hdptx, LANE_REG050E, 0x38);
1674 	hdptx_write(hdptx, LANE_REG0510, 0x03);
1675 	hdptx_write(hdptx, LANE_REG0511, 0x0f);
1676 	hdptx_write(hdptx, LANE_REG0512, 0x3c);
1677 	hdptx_write(hdptx, LANE_REG0516, 0x02);
1678 	hdptx_write(hdptx, LANE_REG051B, 0x01);
1679 	hdptx_write(hdptx, LANE_REG051F, 0x15);
1680 	hdptx_write(hdptx, LANE_REG0520, 0xa0);
1681 	hdptx_write(hdptx, LANE_REG0603, 0x0c);
1682 	hdptx_write(hdptx, LANE_REG0607, 0x20);
1683 	hdptx_write(hdptx, LANE_REG060A, 0x17);
1684 	hdptx_write(hdptx, LANE_REG060B, 0x77);
1685 	hdptx_write(hdptx, LANE_REG060C, 0x77);
1686 	hdptx_write(hdptx, LANE_REG060D, 0x77);
1687 	hdptx_write(hdptx, LANE_REG060E, 0x38);
1688 	hdptx_write(hdptx, LANE_REG0610, 0x03);
1689 	hdptx_write(hdptx, LANE_REG0611, 0x0f);
1690 	hdptx_write(hdptx, LANE_REG0612, 0x3c);
1691 	hdptx_write(hdptx, LANE_REG0616, 0x02);
1692 	hdptx_write(hdptx, LANE_REG061B, 0x01);
1693 	hdptx_write(hdptx, LANE_REG061F, 0x15);
1694 	hdptx_write(hdptx, LANE_REG0620, 0xa0);
1695 
1696 	hdptx_write(hdptx, LANE_REG031E, 0x02);
1697 	hdptx_write(hdptx, LANE_REG041E, 0x02);
1698 	hdptx_write(hdptx, LANE_REG051E, 0x02);
1699 	hdptx_write(hdptx, LANE_REG061E, 0x02);
1700 
1701 	hdptx_write(hdptx, LANE_REG0303, 0x2f);
1702 	hdptx_write(hdptx, LANE_REG0403, 0x2f);
1703 	hdptx_write(hdptx, LANE_REG0503, 0x2f);
1704 	hdptx_write(hdptx, LANE_REG0603, 0x2f);
1705 	hdptx_write(hdptx, LANE_REG0305, 0x03);
1706 	hdptx_write(hdptx, LANE_REG0405, 0x03);
1707 	hdptx_write(hdptx, LANE_REG0505, 0x03);
1708 	hdptx_write(hdptx, LANE_REG0605, 0x03);
1709 	hdptx_write(hdptx, LANE_REG0306, 0xfc);
1710 	hdptx_write(hdptx, LANE_REG0406, 0xfc);
1711 	hdptx_write(hdptx, LANE_REG0506, 0xfc);
1712 	hdptx_write(hdptx, LANE_REG0606, 0xfc);
1713 
1714 	hdptx_write(hdptx, LANE_REG0305, 0x4f);
1715 	hdptx_write(hdptx, LANE_REG0405, 0x4f);
1716 	hdptx_write(hdptx, LANE_REG0505, 0x4f);
1717 	hdptx_write(hdptx, LANE_REG0605, 0x4f);
1718 	hdptx_write(hdptx, LANE_REG0304, 0x14);
1719 	hdptx_write(hdptx, LANE_REG0404, 0x14);
1720 	hdptx_write(hdptx, LANE_REG0504, 0x14);
1721 	hdptx_write(hdptx, LANE_REG0604, 0x14);
1722 
1723 	return hdptx_post_enable_lane(hdptx);
1724 }
1725 
1726 
hdptx_lcpll_frl_mode_config(struct rockchip_hdptx_phy * hdptx,u32 rate)1727 static int hdptx_lcpll_frl_mode_config(struct rockchip_hdptx_phy *hdptx, u32 rate)
1728 {
1729 	hdptx_write(hdptx, LNTOP_REG0200, 0x04);
1730 	hdptx_write(hdptx, LNTOP_REG0201, 0x00);
1731 	hdptx_write(hdptx, LNTOP_REG0202, 0x00);
1732 	hdptx_write(hdptx, LNTOP_REG0203, 0xf0);
1733 	hdptx_write(hdptx, LNTOP_REG0204, 0xff);
1734 	hdptx_write(hdptx, LNTOP_REG0205, 0xff);
1735 	hdptx_write(hdptx, LNTOP_REG0206, 0x05);
1736 	hdptx_write(hdptx, LANE_REG0303, 0x0c);
1737 	hdptx_write(hdptx, LANE_REG0307, 0x20);
1738 	hdptx_write(hdptx, LANE_REG030A, 0x17);
1739 	hdptx_write(hdptx, LANE_REG030B, 0x77);
1740 	hdptx_write(hdptx, LANE_REG030C, 0x77);
1741 	hdptx_write(hdptx, LANE_REG030D, 0x77);
1742 	hdptx_write(hdptx, LANE_REG030E, 0x38);
1743 	hdptx_write(hdptx, LANE_REG0310, 0x03);
1744 	hdptx_write(hdptx, LANE_REG0311, 0x0f);
1745 	hdptx_write(hdptx, LANE_REG0312, 0x3c);
1746 	hdptx_write(hdptx, LANE_REG0316, 0x02);
1747 	hdptx_write(hdptx, LANE_REG031B, 0x01);
1748 	hdptx_write(hdptx, LANE_REG031F, 0x15);
1749 	hdptx_write(hdptx, LANE_REG0320, 0xa0);
1750 	hdptx_write(hdptx, LANE_REG0403, 0x0c);
1751 	hdptx_write(hdptx, LANE_REG0407, 0x20);
1752 	hdptx_write(hdptx, LANE_REG040A, 0x17);
1753 	hdptx_write(hdptx, LANE_REG040B, 0x77);
1754 	hdptx_write(hdptx, LANE_REG040C, 0x77);
1755 	hdptx_write(hdptx, LANE_REG040D, 0x77);
1756 	hdptx_write(hdptx, LANE_REG040E, 0x38);
1757 	hdptx_write(hdptx, LANE_REG0410, 0x03);
1758 	hdptx_write(hdptx, LANE_REG0411, 0x0f);
1759 	hdptx_write(hdptx, LANE_REG0412, 0x3c);
1760 	hdptx_write(hdptx, LANE_REG0416, 0x02);
1761 	hdptx_write(hdptx, LANE_REG041B, 0x01);
1762 	hdptx_write(hdptx, LANE_REG041F, 0x15);
1763 	hdptx_write(hdptx, LANE_REG0420, 0xa0);
1764 	hdptx_write(hdptx, LANE_REG0503, 0x0c);
1765 	hdptx_write(hdptx, LANE_REG0507, 0x20);
1766 	hdptx_write(hdptx, LANE_REG050A, 0x17);
1767 	hdptx_write(hdptx, LANE_REG050B, 0x77);
1768 	hdptx_write(hdptx, LANE_REG050C, 0x77);
1769 	hdptx_write(hdptx, LANE_REG050D, 0x77);
1770 	hdptx_write(hdptx, LANE_REG050E, 0x38);
1771 	hdptx_write(hdptx, LANE_REG0510, 0x03);
1772 	hdptx_write(hdptx, LANE_REG0511, 0x0f);
1773 	hdptx_write(hdptx, LANE_REG0512, 0x3c);
1774 	hdptx_write(hdptx, LANE_REG0516, 0x02);
1775 	hdptx_write(hdptx, LANE_REG051B, 0x01);
1776 	hdptx_write(hdptx, LANE_REG051F, 0x15);
1777 	hdptx_write(hdptx, LANE_REG0520, 0xa0);
1778 	hdptx_write(hdptx, LANE_REG0603, 0x0c);
1779 	hdptx_write(hdptx, LANE_REG0607, 0x20);
1780 	hdptx_write(hdptx, LANE_REG060A, 0x17);
1781 	hdptx_write(hdptx, LANE_REG060B, 0x77);
1782 	hdptx_write(hdptx, LANE_REG060C, 0x77);
1783 	hdptx_write(hdptx, LANE_REG060D, 0x77);
1784 	hdptx_write(hdptx, LANE_REG060E, 0x38);
1785 	hdptx_write(hdptx, LANE_REG0610, 0x03);
1786 	hdptx_write(hdptx, LANE_REG0611, 0x0f);
1787 	hdptx_write(hdptx, LANE_REG0612, 0x3c);
1788 	hdptx_write(hdptx, LANE_REG0616, 0x02);
1789 	hdptx_write(hdptx, LANE_REG061B, 0x01);
1790 	hdptx_write(hdptx, LANE_REG061F, 0x15);
1791 	hdptx_write(hdptx, LANE_REG0620, 0xa0);
1792 
1793 	hdptx_write(hdptx, LANE_REG031E, 0x02);
1794 	hdptx_write(hdptx, LANE_REG041E, 0x02);
1795 	hdptx_write(hdptx, LANE_REG051E, 0x02);
1796 	hdptx_write(hdptx, LANE_REG061E, 0x02);
1797 
1798 	hdptx_write(hdptx, LANE_REG0303, 0x2f);
1799 	hdptx_write(hdptx, LANE_REG0403, 0x2f);
1800 	hdptx_write(hdptx, LANE_REG0503, 0x2f);
1801 	hdptx_write(hdptx, LANE_REG0603, 0x2f);
1802 	hdptx_write(hdptx, LANE_REG0305, 0x03);
1803 	hdptx_write(hdptx, LANE_REG0405, 0x03);
1804 	hdptx_write(hdptx, LANE_REG0505, 0x03);
1805 	hdptx_write(hdptx, LANE_REG0605, 0x03);
1806 	hdptx_write(hdptx, LANE_REG0306, 0xfc);
1807 	hdptx_write(hdptx, LANE_REG0406, 0xfc);
1808 	hdptx_write(hdptx, LANE_REG0506, 0xfc);
1809 	hdptx_write(hdptx, LANE_REG0606, 0xfc);
1810 
1811 	hdptx_write(hdptx, LANE_REG0305, 0x4f);
1812 	hdptx_write(hdptx, LANE_REG0405, 0x4f);
1813 	hdptx_write(hdptx, LANE_REG0505, 0x4f);
1814 	hdptx_write(hdptx, LANE_REG0605, 0x4f);
1815 	hdptx_write(hdptx, LANE_REG0304, 0x14);
1816 	hdptx_write(hdptx, LANE_REG0404, 0x14);
1817 	hdptx_write(hdptx, LANE_REG0504, 0x14);
1818 	hdptx_write(hdptx, LANE_REG0604, 0x14);
1819 
1820 	return hdptx_post_enable_lane(hdptx);
1821 }
1822 
rockchip_hdptx_phy_power_on(struct rockchip_phy * phy)1823 static int rockchip_hdptx_phy_power_on(struct rockchip_phy *phy)
1824 {
1825 	struct rockchip_hdptx_phy *hdptx = dev_get_priv(phy->dev);
1826 	int bus_width = hdptx->bus_width;
1827 	int bit_rate = bus_width & DATA_RATE_MASK;
1828 
1829 	printf("bus_width:0x%x,bit_rate:%d\n", bus_width, bit_rate);
1830 
1831 	if (bus_width & HDMI_MODE_MASK)
1832 		if (bit_rate != (FRL_8G_4LANES / 100))
1833 			return hdptx_lcpll_frl_mode_config(hdptx, bus_width);
1834 		else
1835 			return hdptx_lcpll_ropll_frl_mode_config(hdptx);
1836 	else
1837 		return hdptx_ropll_tmds_mode_config(hdptx, bus_width);
1838 }
1839 
rockchip_hdptx_phy_power_off(struct rockchip_phy * phy)1840 static int rockchip_hdptx_phy_power_off(struct rockchip_phy *phy)
1841 {
1842 	return 0;
1843 }
1844 
rockchip_hdptx_phy_clk_round_rate(struct rockchip_phy * phy,unsigned long rate)1845 static long rockchip_hdptx_phy_clk_round_rate(struct rockchip_phy *phy,
1846 					      unsigned long rate)
1847 {
1848 	struct ropll_config *cfg = ropll_tmds_cfg;
1849 	u32 bit_rate = rate / 100;
1850 
1851 	for (; cfg->bit_rate != ~0; cfg++)
1852 		if (bit_rate == cfg->bit_rate)
1853 			break;
1854 
1855 	if (cfg->bit_rate == ~0 && !hdptx_phy_clk_pll_calc(bit_rate, NULL))
1856 		return -EINVAL;
1857 
1858 	return rate;
1859 }
1860 
rockchip_hdptx_phy_clk_set_rate(struct rockchip_phy * phy,unsigned long rate)1861 static unsigned long rockchip_hdptx_phy_clk_set_rate(struct rockchip_phy *phy,
1862 						     unsigned long rate)
1863 {
1864 	struct rockchip_hdptx_phy *hdptx = dev_get_priv(phy->dev);
1865 	int bus_width = hdptx->bus_width;
1866 	u8 color_depth = (bus_width & COLOR_DEPTH_MASK) ? 1 : 0;
1867 
1868 	if (color_depth)
1869 		rate = (rate / 100) * 5 / 4;
1870 	else
1871 		rate = rate / 100;
1872 	return hdptx_ropll_cmn_config(hdptx, rate);
1873 }
1874 
1875 static int
rockchip_hdptx_phy_set_bus_width(struct rockchip_phy * phy,u32 bus_width)1876 rockchip_hdptx_phy_set_bus_width(struct rockchip_phy *phy, u32 bus_width)
1877 {
1878 	struct rockchip_hdptx_phy *hdptx = dev_get_priv(phy->dev);
1879 
1880 	hdptx->bus_width = bus_width;
1881 
1882 	return 0;
1883 }
1884 
1885 const struct rockchip_phy_funcs hdptx_hdmi_phy_funcs = {
1886 	.power_on = rockchip_hdptx_phy_power_on,
1887 	.power_off = rockchip_hdptx_phy_power_off,
1888 	.set_pll = rockchip_hdptx_phy_clk_set_rate,
1889 	.set_bus_width = rockchip_hdptx_phy_set_bus_width,
1890 	.round_rate = rockchip_hdptx_phy_clk_round_rate,
1891 };
1892 
1893 static struct rockchip_phy hdptx_hdmi_phy_driver_data0 = {
1894 	 .funcs = &hdptx_hdmi_phy_funcs,
1895 };
1896 
1897 static struct rockchip_phy hdptx_hdmi_phy_driver_data1 = {
1898 	 .funcs = &hdptx_hdmi_phy_funcs,
1899 };
1900 
rockchip_hdptx_phy_hdmi_probe(struct udevice * dev)1901 static int rockchip_hdptx_phy_hdmi_probe(struct udevice *dev)
1902 {
1903 	struct rockchip_hdptx_phy *hdptx = dev_get_priv(dev);
1904 	struct rockchip_phy *phy;
1905 	struct udevice *syscon;
1906 	int ret;
1907 
1908 	hdptx->id = of_alias_get_id(ofnode_to_np(dev->node), "hdptxhdmi");
1909 	if (hdptx->id < 0)
1910 		hdptx->id = 0;
1911 
1912 	if (!hdptx->id) {
1913 		g_hdptx0 = hdptx;
1914 		dev->driver_data = (ulong)&hdptx_hdmi_phy_driver_data0;
1915 		phy = &hdptx_hdmi_phy_driver_data0;
1916 	} else {
1917 		g_hdptx1 = hdptx;
1918 		dev->driver_data = (ulong)&hdptx_hdmi_phy_driver_data1;
1919 		phy = &hdptx_hdmi_phy_driver_data1;
1920 	}
1921 
1922 	hdptx->base = dev_read_addr_ptr(dev);
1923 	if (!hdptx->base)
1924 		return -ENOENT;
1925 
1926 	ret = uclass_get_device_by_phandle(UCLASS_SYSCON, dev, "rockchip,grf",
1927 					   &syscon);
1928 	if (ret)
1929 		return ret;
1930 
1931 	hdptx->grf = syscon_get_regmap(syscon);
1932 	if (IS_ERR(hdptx->grf)) {
1933 		ret = PTR_ERR(hdptx->grf);
1934 		dev_err(dev, "unable to find regmap: %d\n", ret);
1935 		return ret;
1936 	}
1937 
1938 	hdptx->dev = dev;
1939 	phy->dev = dev;
1940 
1941 	ret = reset_get_by_name(dev, "apb", &hdptx->apb_reset);
1942 	if (ret < 0) {
1943 		dev_err(dev, "failed to get apb reset: %d\n", ret);
1944 		return ret;
1945 	}
1946 
1947 	ret = reset_get_by_name(dev, "init", &hdptx->init_reset);
1948 	if (ret < 0) {
1949 		dev_err(dev, "failed to get init reset: %d\n", ret);
1950 		return ret;
1951 	}
1952 
1953 	ret = reset_get_by_name(dev, "cmn", &hdptx->cmn_reset);
1954 	if (ret < 0) {
1955 		dev_err(dev, "failed to get cmn reset: %d\n", ret);
1956 		return ret;
1957 	}
1958 
1959 	ret = reset_get_by_name(dev, "lane", &hdptx->lane_reset);
1960 	if (ret < 0) {
1961 		dev_err(dev, "failed to get lane reset: %d\n", ret);
1962 		return ret;
1963 	}
1964 
1965 	return 0;
1966 }
1967 
rockchip_hdptx_phy_hdmi_bind(struct udevice * parent)1968 static int rockchip_hdptx_phy_hdmi_bind(struct udevice *parent)
1969 {
1970 	struct udevice *child;
1971 	ofnode subnode;
1972 	char name[30], *str;
1973 	int id, ret;
1974 
1975 	id = of_alias_get_id(ofnode_to_np(parent->node), "hdptxhdmi");
1976 	if (id < 0)
1977 		id = 0;
1978 
1979 	sprintf(name, "hdmiphypll_clk%d", id);
1980 	str = strdup(name);
1981 
1982 	subnode = ofnode_find_subnode(parent->node, "clk-port");
1983 	if (!ofnode_valid(subnode)) {
1984 		ret = device_bind_driver_to_node(parent, "clk_hdptx", str,
1985 						 dev_ofnode(parent), NULL);
1986 	} else {
1987 		ret = device_bind_driver_to_node(parent, "clk_hdptx", str,
1988 						 subnode, &child);
1989 	}
1990 
1991 	if (ret) {
1992 		free(str);
1993 		printf("%s: clk_hdptx cannot bind its driver\n", __func__);
1994 		return ret;
1995 	}
1996 
1997 	return 0;
1998 }
1999 
2000 static const struct udevice_id rockchip_hdptx_phy_hdmi_ids[] = {
2001 	{ .compatible = "rockchip,rk3588-hdptx-phy-hdmi",
2002 	  .data = (ulong)&hdptx_hdmi_phy_driver_data0,
2003 	},
2004 	{}
2005 };
2006 
2007 U_BOOT_DRIVER(rockchip_hdptx_phy_hdmi) = {
2008 	.name		= "rockchip_hdptx_phy_hdmi",
2009 	.id		= UCLASS_PHY,
2010 	.of_match	= rockchip_hdptx_phy_hdmi_ids,
2011 	.probe		= rockchip_hdptx_phy_hdmi_probe,
2012 	.bind		= rockchip_hdptx_phy_hdmi_bind,
2013 	.priv_auto_alloc_size = sizeof(struct rockchip_hdptx_phy),
2014 };
2015 
get_hdptx(struct udevice * dev)2016 static struct rockchip_hdptx_phy *get_hdptx(struct udevice *dev)
2017 {
2018 	if (!strcmp(dev->name, "hdmiphypll_clk0"))
2019 		return g_hdptx0;
2020 
2021 	return g_hdptx1;
2022 }
2023 
hdptx_clk_get_rate(struct clk * clk)2024 static ulong hdptx_clk_get_rate(struct clk *clk)
2025 {
2026 	struct clk_hdptx *priv = dev_get_priv(clk->dev);
2027 
2028 	return priv->rate;
2029 }
2030 
hdptx_clk_set_rate(struct clk * clk,ulong rate)2031 static ulong hdptx_clk_set_rate(struct clk *clk, ulong rate)
2032 {
2033 	struct clk_hdptx *priv = dev_get_priv(clk->dev);
2034 	struct rockchip_hdptx_phy *hdptx = get_hdptx(clk->dev);
2035 	int bus_width = hdptx->bus_width;
2036 	u8 color_depth = (bus_width & COLOR_DEPTH_MASK) ? 1 : 0;
2037 	ulong new_rate = -ENOENT;
2038 
2039 	if (color_depth && rate <= HDMI20_MAX_RATE)
2040 		rate = (rate / 100) * 5 / 4;
2041 	else
2042 		rate = rate / 100;
2043 
2044 	if (rate > (HDMI20_MAX_RATE / 100)) {
2045 		if  (rate == FRL_8G_4LANES / 100) {
2046 			if (!hdptx_lcpll_ropll_cmn_config(hdptx, rate)) {
2047 				new_rate = rate;
2048 				priv->rate = rate;
2049 			}
2050 		} else {
2051 			if (!hdptx_lcpll_cmn_config(hdptx, rate)) {
2052 				new_rate = rate;
2053 				priv->rate = rate;
2054 			}
2055 		}
2056 	} else {
2057 		if (!hdptx_ropll_cmn_config(hdptx, rate)) {
2058 			new_rate = rate;
2059 			priv->rate = rate;
2060 		}
2061 	}
2062 
2063 	return new_rate;
2064 }
2065 
2066 static const struct clk_ops hdptx_clk_ops = {
2067 	.get_rate	= hdptx_clk_get_rate,
2068 	.set_rate	= hdptx_clk_set_rate,
2069 };
2070 
hdptx_clk_probe(struct udevice * dev)2071 static int hdptx_clk_probe(struct udevice *dev)
2072 {
2073 	return 0;
2074 }
2075 
2076 /*
2077  * In order for other display interfaces to use hdmiphy as source
2078  * for dclk, hdmiphy must register a virtual clock driver
2079  */
2080 U_BOOT_DRIVER(clk_hdptx) = {
2081 	.name		= "clk_hdptx",
2082 	.id		= UCLASS_CLK,
2083 	.priv_auto_alloc_size = sizeof(struct clk_hdptx),
2084 	.ops		= &hdptx_clk_ops,
2085 	.probe		= hdptx_clk_probe,
2086 };
2087