xref: /linux/drivers/phy/rockchip/phy-rockchip-samsung-dcphy.c (revision 16e6a1a3cb3fa5fa11cd76a9d71235d927923329)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2025 Rockchip Electronics Co.Ltd
4  * Author:
5  *      Guochun Huang <hero.huang@rock-chips.com>
6  */
7 
8 #include <dt-bindings/phy/phy.h>
9 #include <linux/bitfield.h>
10 #include <linux/clk.h>
11 #include <linux/hw_bitfield.h>
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/mfd/syscon.h>
15 #include <linux/module.h>
16 #include <linux/of.h>
17 #include <linux/phy/phy.h>
18 #include <linux/platform_device.h>
19 #include <linux/pm_runtime.h>
20 #include <linux/regmap.h>
21 #include <linux/reset.h>
22 
23 #define BIAS_CON0		0x0000
24 #define I_RES_CNTL_MASK		GENMASK(6, 4)
25 #define I_RES_CNTL(x)		FIELD_PREP(I_RES_CNTL_MASK, x)
26 #define I_RES_059_2UA		I_RES_CNTL(0)
27 #define I_RES_100_2UA		I_RES_CNTL(1)
28 #define I_RES_094_2UA		I_RES_CNTL(2)
29 #define I_RES_113_8UA		I_RES_CNTL(3)
30 #define I_RES_089_7UA		I_RES_CNTL(4)
31 #define I_RES_111_8UA		I_RES_CNTL(5)
32 #define I_RES_108_2UA		I_RES_CNTL(6)
33 #define I_RES_120_8UA		I_RES_CNTL(7)
34 #define I_DEV_SEL_MASK		GENMASK(1, 0)
35 #define I_DEV_SEL(x)		FIELD_PREP(I_DEV_SEL_MASK, x)
36 #define I_DEV_DIV_6		I_DEV_SEL(0)
37 #define I_DEV_DIV_12		I_DEV_SEL(1)
38 #define I_DEV_DIV_20		I_DEV_SEL(2)
39 #define I_DEV_DIV_40		I_DEV_SEL(3)
40 
41 #define BIAS_CON1		0x0004
42 #define I_VBG_SEL_MASK		GENMASK(9, 8)
43 #define I_VBG_SEL(x)		FIELD_PREP(I_VBG_SEL_MASK, x)
44 #define I_VBG_SEL_780MV		I_VBG_SEL(0)
45 #define I_VBG_SEL_820MV		I_VBG_SEL(1)
46 #define I_VBG_SEL_860MV		I_VBG_SEL(2)
47 #define I_VBG_SEL_900MV		I_VBG_SEL(3)
48 #define I_BGR_VREF_SEL_MASK	GENMASK(5, 4)
49 #define I_BGR_VREF_SEL(x)	FIELD_PREP(I_BGR_VREF_SEL_MASK, x)
50 #define I_BGR_VREF_810MV	I_BGR_VREF_SEL(0)
51 #define I_BGR_VREF_820MV	I_BGR_VREF_SEL(1)
52 #define I_BGR_VREF_830MV	I_BGR_VREF_SEL(2)
53 #define I_BGR_VREF_840MV	I_BGR_VREF_SEL(3)
54 #define I_LADDER_SEL_MASK	GENMASK(2, 0)
55 #define I_LADDER_SEL(x)		FIELD_PREP(I_LADDER_SEL_MASK, x)
56 #define I_LADDER_1_00V		I_LADDER_SEL(0)
57 #define I_LADDER_0_96V		I_LADDER_SEL(1)
58 #define I_LADDER_0_92V		I_LADDER_SEL(2)
59 #define I_LADDER_0_88V		I_LADDER_SEL(3)
60 #define I_LADDER_0_84V		I_LADDER_SEL(4)
61 #define I_LADDER_0_80V		I_LADDER_SEL(5)
62 #define I_LADDER_0_76V		I_LADDER_SEL(6)
63 #define I_LADDER_0_72V		I_LADDER_SEL(7)
64 
65 /*
66  * Voltage corrections around reference voltages
67  * The selection between the 400-based or 200-based values for REG_400M
68  * is done by the hw depending on I_MUX below being 400MV or 200MV.
69  */
70 #define BIAS_CON2		0x0008
71 #define REG_325M_MASK		GENMASK(14, 12)
72 #define REG_325M(x)		FIELD_PREP(REG_325M_MASK, x)
73 #define REG_325M_295MV		REG_325M(0)
74 #define REG_325M_305MV		REG_325M(1)
75 #define REG_325M_315MV		REG_325M(2)
76 #define REG_325M_325MV		REG_325M(3)
77 #define REG_325M_335MV		REG_325M(4)
78 #define REG_325M_345MV		REG_325M(5)
79 #define REG_325M_355MV		REG_325M(6)
80 #define REG_325M_365MV		REG_325M(7)
81 #define REG_LP_400M_MASK	GENMASK(10, 8)
82 #define REG_LP_400M(x)		FIELD_PREP(REG_LP_400M_MASK, x)
83 #define REG_LP_400M_380MV	REG_LP_400M(0)
84 #define REG_LP_400M_390MV	REG_LP_400M(1)
85 #define REG_LP_400M_400MV	REG_LP_400M(2)
86 #define REG_LP_400M_410MV	REG_LP_400M(3)
87 #define REG_LP_400M_420MV	REG_LP_400M(4)
88 #define REG_LP_400M_430MV	REG_LP_400M(5)
89 #define REG_LP_400M_440MV	REG_LP_400M(6)
90 #define REG_LP_400M_450MV	REG_LP_400M(7)
91 #define REG_400M_MASK		GENMASK(6, 4)
92 #define REG_400M(x)		FIELD_PREP(REG_400M_MASK, x)
93 #define REG_400M_380MV		REG_400M(0)
94 #define REG_400M_390MV		REG_400M(1)
95 #define REG_400M_400MV		REG_400M(2)
96 #define REG_400M_410MV		REG_400M(3)
97 #define REG_400M_420MV		REG_400M(4)
98 #define REG_400M_430MV		REG_400M(5)
99 #define REG_400M_440MV		REG_400M(6)
100 #define REG_400M_450MV		REG_400M(7)
101 #define REG_400M_230MV		REG_400M(0)
102 #define REG_400M_220MV		REG_400M(1)
103 #define REG_400M_210MV		REG_400M(2)
104 #define REG_400M_200MV		REG_400M(3)
105 #define REG_400M_190MV		REG_400M(4)
106 #define REG_400M_180MV		REG_400M(5)
107 #define REG_400M_170MV		REG_400M(6)
108 #define REG_400M_160MV		REG_400M(7)
109 #define REG_645M_MASK		GENMASK(2, 0)
110 #define REG_645M(x)		FIELD_PREP(REG_645M_MASK, x)
111 #define REG_645M_605MV		REG_645M(0)
112 #define REG_645M_625MV		REG_645M(1)
113 #define REG_645M_635MV		REG_645M(2)
114 #define REG_645M_645MV		REG_645M(3)
115 #define REG_645M_655MV		REG_645M(4)
116 #define REG_645M_665MV		REG_645M(5)
117 #define REG_645M_685MV		REG_645M(6)
118 #define REG_645M_725MV		REG_645M(7)
119 
120 #define BIAS_CON4		0x0010
121 #define I_MUX_SEL_MASK		GENMASK(6, 5)
122 #define I_MUX_SEL(x)		FIELD_PREP(I_MUX_SEL_MASK, x)
123 #define I_MUX_400MV		I_MUX_SEL(0)
124 #define I_MUX_200MV		I_MUX_SEL(1)
125 #define I_MUX_530MV		I_MUX_SEL(2)
126 
127 #define PLL_CON0		0x0100
128 #define PLL_EN			BIT(12)
129 #define S_MASK			GENMASK(10, 8)
130 #define S(x)			FIELD_PREP(S_MASK, x)
131 #define P_MASK			GENMASK(5, 0)
132 #define P(x)			FIELD_PREP(P_MASK, x)
133 #define PLL_CON1		0x0104
134 #define PLL_CON2		0x0108
135 #define M_MASK			GENMASK(9, 0)
136 #define M(x)			FIELD_PREP(M_MASK, x)
137 #define PLL_CON3		0x010c
138 #define MRR_MASK		GENMASK(13, 8)
139 #define MRR(x)			FIELD_PREP(MRR_MASK, x)
140 #define MFR_MASK		GENMASK(7, 0)
141 #define MFR(x)			FIELD_PREP(MFR_MASK, x)
142 #define PLL_CON4		0x0110
143 #define SSCG_EN			BIT(11)
144 #define PLL_CON5		0x0114
145 #define RESET_N_SEL		BIT(10)
146 #define PLL_ENABLE_SEL		BIT(8)
147 #define PLL_CON6		0x0118
148 #define PLL_CON7		0x011c
149 #define PLL_LOCK_CNT(x)		FIELD_PREP(GENMASK(15, 0), x)
150 #define PLL_CON8		0x0120
151 #define PLL_STB_CNT(x)		FIELD_PREP(GENMASK(15, 0), x)
152 #define PLL_STAT0		0x0140
153 #define PLL_LOCK		BIT(0)
154 
155 #define DPHY_MC_GNR_CON0	0x0300
156 #define PHY_READY		BIT(1)
157 #define PHY_ENABLE		BIT(0)
158 #define DPHY_MC_GNR_CON1	0x0304
159 #define T_PHY_READY(x)		FIELD_PREP(GENMASK(15, 0), x)
160 #define DPHY_MC_ANA_CON0	0x0308
161 #define EDGE_CON(x)		FIELD_PREP(GENMASK(14, 12), x)
162 #define EDGE_CON_DIR(x)		FIELD_PREP(BIT(9), x)
163 #define EDGE_CON_EN		BIT(8)
164 #define RES_UP(x)		FIELD_PREP(GENMASK(7, 4), x)
165 #define RES_DN(x)		FIELD_PREP(GENMASK(3, 0), x)
166 #define DPHY_MC_ANA_CON1	0x030c
167 #define DPHY_MC_ANA_CON2	0x0310
168 #define HS_VREG_AMP_ICON(x)	FIELD_PREP(GENMASK(1, 0), x)
169 #define DPHY_MC_TIME_CON0	0x0330
170 #define HSTX_CLK_SEL		BIT(12)
171 #define T_LPX(x)		FIELD_PREP(GENMASK(11, 4), x)
172 #define DPHY_MC_TIME_CON1	0x0334
173 #define T_CLK_ZERO(x)		FIELD_PREP(GENMASK(15, 8), x)
174 #define T_CLK_PREPARE(x)	FIELD_PREP(GENMASK(7, 0), x)
175 #define DPHY_MC_TIME_CON2	0x0338
176 #define T_HS_EXIT(x)		FIELD_PREP(GENMASK(15, 8), x)
177 #define T_CLK_TRAIL(x)		FIELD_PREP(GENMASK(7, 0), x)
178 #define DPHY_MC_TIME_CON3	0x033c
179 #define T_CLK_POST(x)		FIELD_PREP(GENMASK(7, 0), x)
180 #define DPHY_MC_TIME_CON4	0x0340
181 #define T_ULPS_EXIT(x)		FIELD_PREP(GENMASK(9, 0), x)
182 #define DPHY_MC_DESKEW_CON0	0x0350
183 #define SKEW_CAL_RUN_TIME(x)	FIELD_PREP(GENMASK(15, 12), x)
184 
185 #define SKEW_CAL_INIT_RUN_TIME(x)	FIELD_PREP(GENMASK(11, 8), x)
186 #define SKEW_CAL_INIT_WAIT_TIME(x)	FIELD_PREP(GENMASK(7, 4), x)
187 #define SKEW_CAL_EN			BIT(0)
188 
189 #define COMBO_MD0_GNR_CON0	0x0400
190 #define COMBO_MD0_GNR_CON1	0x0404
191 #define COMBO_MD0_ANA_CON0	0x0408
192 #define COMBO_MD0_ANA_CON1	0x040c
193 #define COMBO_MD0_ANA_CON2	0x0410
194 
195 #define COMBO_MD0_TIME_CON0	0x0430
196 #define COMBO_MD0_TIME_CON1	0x0434
197 #define COMBO_MD0_TIME_CON2	0x0438
198 #define COMBO_MD0_TIME_CON3	0x043c
199 #define COMBO_MD0_TIME_CON4	0x0440
200 #define COMBO_MD0_DATA_CON0	0x0444
201 
202 #define COMBO_MD1_GNR_CON0	0x0500
203 #define COMBO_MD1_GNR_CON1	0x0504
204 #define COMBO_MD1_ANA_CON0	0x0508
205 #define COMBO_MD1_ANA_CON1	0x050c
206 #define COMBO_MD1_ANA_CON2	0x0510
207 #define COMBO_MD1_TIME_CON0	0x0530
208 #define COMBO_MD1_TIME_CON1	0x0534
209 #define COMBO_MD1_TIME_CON2	0x0538
210 #define COMBO_MD1_TIME_CON3	0x053c
211 #define COMBO_MD1_TIME_CON4	0x0540
212 #define COMBO_MD1_DATA_CON0	0x0544
213 
214 #define COMBO_MD2_GNR_CON0	0x0600
215 #define COMBO_MD2_GNR_CON1	0x0604
216 #define COMBO_MD2_ANA_CON0	0X0608
217 #define COMBO_MD2_ANA_CON1	0X060c
218 #define COMBO_MD2_ANA_CON2	0X0610
219 #define COMBO_MD2_TIME_CON0	0x0630
220 #define COMBO_MD2_TIME_CON1	0x0634
221 #define COMBO_MD2_TIME_CON2	0x0638
222 #define COMBO_MD2_TIME_CON3	0x063c
223 #define COMBO_MD2_TIME_CON4	0x0640
224 #define COMBO_MD2_DATA_CON0	0x0644
225 
226 #define DPHY_MD3_GNR_CON0	0x0700
227 #define DPHY_MD3_GNR_CON1	0x0704
228 #define DPHY_MD3_ANA_CON0	0X0708
229 #define DPHY_MD3_ANA_CON1	0X070c
230 #define DPHY_MD3_ANA_CON2	0X0710
231 #define DPHY_MD3_TIME_CON0	0x0730
232 #define DPHY_MD3_TIME_CON1	0x0734
233 #define DPHY_MD3_TIME_CON2	0x0738
234 #define DPHY_MD3_TIME_CON3	0x073c
235 #define DPHY_MD3_TIME_CON4	0x0740
236 #define DPHY_MD3_DATA_CON0	0x0744
237 
238 #define T_LP_EXIT_SKEW(x)	FIELD_PREP(GENMASK(3, 2), x)
239 #define T_LP_ENTRY_SKEW(x)	FIELD_PREP(GENMASK(1, 0), x)
240 #define T_HS_ZERO(x)		FIELD_PREP(GENMASK(15, 8), x)
241 #define T_HS_PREPARE(x)		FIELD_PREP(GENMASK(7, 0), x)
242 #define T_HS_EXIT(x)		FIELD_PREP(GENMASK(15, 8), x)
243 #define T_HS_TRAIL(x)		FIELD_PREP(GENMASK(7, 0), x)
244 #define T_TA_GET(x)		FIELD_PREP(GENMASK(7, 4), x)
245 #define T_TA_GO(x)		FIELD_PREP(GENMASK(3, 0), x)
246 
247 /* MIPI_CDPHY_GRF registers */
248 #define MIPI_DCPHY_GRF_CON0		0x0000
249 #define S_CPHY_MODE			FIELD_PREP_WM16(BIT(3), 1)
250 #define M_CPHY_MODE			FIELD_PREP_WM16(BIT(0), 1)
251 
252 enum hs_drv_res_ohm {
253 	STRENGTH_30_OHM = 0x8,
254 	STRENGTH_31_2_OHM,
255 	STRENGTH_32_5_OHM,
256 	STRENGTH_34_OHM,
257 	STRENGTH_35_5_OHM,
258 	STRENGTH_37_OHM,
259 	STRENGTH_39_OHM,
260 	STRENGTH_41_OHM,
261 	STRENGTH_43_OHM = 0x0,
262 	STRENGTH_46_OHM,
263 	STRENGTH_49_OHM,
264 	STRENGTH_52_OHM,
265 	STRENGTH_56_OHM,
266 	STRENGTH_60_OHM,
267 	STRENGTH_66_OHM,
268 	STRENGTH_73_OHM,
269 };
270 
271 struct hs_drv_res_cfg {
272 	enum hs_drv_res_ohm clk_hs_drv_up_ohm;
273 	enum hs_drv_res_ohm clk_hs_drv_down_ohm;
274 	enum hs_drv_res_ohm data_hs_drv_up_ohm;
275 	enum hs_drv_res_ohm data_hs_drv_down_ohm;
276 };
277 
278 struct samsung_mipi_dcphy_plat_data {
279 	const struct hs_drv_res_cfg *dphy_hs_drv_res_cfg;
280 	u32 dphy_tx_max_lane_kbps;
281 };
282 
283 struct samsung_mipi_dcphy {
284 	struct device *dev;
285 	struct clk *ref_clk;
286 	struct clk *pclk;
287 	struct regmap *regmap;
288 	struct regmap *grf_regmap;
289 	struct reset_control *m_phy_rst;
290 	struct reset_control *s_phy_rst;
291 	struct reset_control *apb_rst;
292 	struct reset_control *grf_apb_rst;
293 	unsigned int lanes;
294 	struct phy *phy;
295 	u8 type;
296 
297 	const struct samsung_mipi_dcphy_plat_data *pdata;
298 	struct {
299 		unsigned long long rate;
300 		u8 prediv;
301 		u16 fbdiv;
302 		long dsm;
303 		u8 scaler;
304 
305 		bool ssc_en;
306 		u8 mfr;
307 		u8 mrr;
308 	} pll;
309 };
310 
311 struct samsung_mipi_dphy_timing {
312 	unsigned int max_lane_mbps;
313 	u8 clk_prepare;
314 	u8 clk_zero;
315 	u8 clk_post;
316 	u8 clk_trail_eot;
317 	u8 hs_prepare;
318 	u8 hs_zero;
319 	u8 hs_trail_eot;
320 	u8 lpx;
321 	u8 hs_exit;
322 	u8 hs_settle;
323 };
324 
325 /*
326  * Timing values taken from rk3588 vendor kernel.
327  * Not documented in hw documentation.
328  */
329 static const
330 struct samsung_mipi_dphy_timing samsung_mipi_dphy_timing_table[] = {
331 	{6500, 32, 117, 31, 28, 30, 56, 27, 24, 44, 37},
332 	{6490, 32, 116, 31, 28, 30, 56, 27, 24, 44, 37},
333 	{6480, 32, 116, 31, 28, 30, 56, 27, 24, 44, 37},
334 	{6470, 32, 116, 31, 28, 30, 56, 27, 24, 44, 37},
335 	{6460, 32, 116, 31, 28, 30, 56, 27, 24, 44, 37},
336 	{6450, 32, 115, 31, 28, 30, 56, 27, 24, 44, 37},
337 	{6440, 32, 115, 31, 28, 30, 56, 27, 24, 44, 37},
338 	{6430, 31, 116, 31, 28, 30, 55, 27, 24, 44, 37},
339 	{6420, 31, 116, 31, 28, 30, 55, 27, 24, 44, 37},
340 	{6410, 31, 116, 31, 27, 30, 55, 27, 24, 44, 37},
341 	{6400, 31, 115, 30, 27, 30, 55, 27, 23, 43, 36},
342 	{6390, 31, 115, 30, 27, 30, 55, 27, 23, 43, 36},
343 	{6380, 31, 115, 30, 27, 30, 55, 27, 23, 43, 36},
344 	{6370, 31, 115, 30, 27, 30, 55, 26, 23, 43, 36},
345 	{6360, 31, 114, 30, 27, 30, 54, 26, 23, 43, 36},
346 	{6350, 31, 114, 30, 27, 30, 54, 26, 23, 43, 36},
347 	{6340, 31, 114, 30, 27, 30, 54, 26, 23, 43, 36},
348 	{6330, 31, 114, 30, 27, 30, 54, 26, 23, 43, 36},
349 	{6320, 31, 113, 30, 27, 30, 54, 26, 23, 43, 36},
350 	{6310, 31, 113, 30, 27, 30, 54, 26, 23, 43, 36},
351 	{6300, 31, 113, 30, 27, 30, 54, 26, 23, 43, 36},
352 	{6290, 31, 113, 30, 27, 29, 54, 26, 23, 43, 36},
353 	{6280, 31, 112, 30, 27, 29, 54, 26, 23, 43, 36},
354 	{6270, 31, 112, 30, 27, 29, 54, 26, 23, 43, 36},
355 	{6260, 31, 112, 30, 27, 29, 54, 26, 23, 43, 36},
356 	{6250, 31, 112, 30, 27, 29, 54, 26, 23, 42, 36},
357 	{6240, 30, 113, 30, 27, 29, 54, 26, 23, 42, 36},
358 	{6230, 30, 112, 30, 27, 29, 54, 26, 23, 42, 35},
359 	{6220, 30, 112, 30, 27, 29, 53, 26, 23, 42, 35},
360 	{6210, 30, 112, 30, 27, 29, 53, 26, 23, 42, 35},
361 	{6200, 30, 112, 29, 27, 29, 53, 26, 23, 42, 35},
362 	{6190, 30, 111, 29, 27, 29, 53, 26, 23, 42, 35},
363 	{6180, 30, 111, 29, 27, 29, 53, 26, 23, 42, 35},
364 	{6170, 30, 111, 29, 26, 29, 53, 26, 23, 42, 35},
365 	{6160, 30, 111, 29, 26, 29, 53, 26, 23, 42, 35},
366 	{6150, 30, 110, 29, 26, 29, 53, 26, 23, 42, 35},
367 	{6140, 30, 110, 29, 26, 29, 52, 26, 23, 42, 35},
368 	{6130, 30, 110, 29, 26, 29, 52, 25, 22, 42, 35},
369 	{6120, 30, 110, 29, 26, 29, 52, 25, 22, 42, 35},
370 	{6110, 30, 110, 29, 26, 29, 52, 25, 22, 42, 35},
371 	{6100, 30, 109, 29, 26, 29, 52, 25, 22, 41, 35},
372 	{6090, 30, 109, 29, 26, 29, 52, 25, 22, 41, 35},
373 	{6080, 30, 109, 29, 26, 28, 53, 25, 22, 41, 35},
374 	{6070, 30, 109, 29, 26, 28, 52, 25, 22, 41, 34},
375 	{6060, 30, 108, 29, 26, 28, 52, 25, 22, 41, 34},
376 	{6050, 30, 108, 29, 26, 28, 52, 25, 22, 41, 34},
377 	{6040, 29, 109, 29, 26, 28, 52, 25, 22, 41, 34},
378 	{6030, 29, 109, 29, 26, 28, 52, 25, 22, 41, 34},
379 	{6020, 29, 108, 29, 26, 28, 52, 25, 22, 41, 34},
380 	{6010, 29, 108, 29, 26, 28, 52, 25, 22, 41, 34},
381 	{6000, 29, 108, 28, 26, 28, 51, 25, 22, 41, 34},
382 	{5990, 29, 108, 28, 26, 28, 51, 25, 22, 41, 34},
383 	{5980, 29, 107, 28, 26, 28, 51, 25, 22, 41, 34},
384 	{5970, 29, 107, 28, 26, 28, 51, 25, 22, 41, 34},
385 	{5960, 29, 107, 28, 26, 28, 51, 25, 22, 40, 34},
386 	{5950, 29, 107, 28, 26, 28, 51, 25, 22, 40, 34},
387 	{5940, 29, 107, 28, 25, 28, 51, 25, 22, 40, 34},
388 	{5930, 29, 106, 28, 25, 28, 50, 25, 22, 40, 34},
389 	{5920, 29, 106, 28, 25, 28, 50, 25, 22, 40, 34},
390 	{5910, 29, 106, 28, 25, 28, 50, 25, 22, 40, 34},
391 	{5900, 29, 106, 28, 25, 28, 50, 24, 22, 40, 33},
392 	{5890, 29, 105, 28, 25, 28, 50, 24, 22, 40, 33},
393 	{5880, 29, 105, 28, 25, 28, 50, 24, 22, 40, 33},
394 	{5870, 29, 105, 28, 25, 27, 51, 24, 22, 40, 33},
395 	{5860, 29, 105, 28, 25, 27, 51, 24, 21, 40, 33},
396 	{5850, 29, 104, 28, 25, 27, 50, 24, 21, 40, 33},
397 	{5840, 28, 105, 28, 25, 27, 50, 24, 21, 40, 33},
398 	{5830, 28, 105, 28, 25, 27, 50, 24, 21, 40, 33},
399 	{5820, 28, 105, 28, 25, 27, 50, 24, 21, 40, 33},
400 	{5810, 28, 104, 28, 25, 27, 50, 24, 21, 39, 33},
401 	{5800, 28, 104, 27, 25, 27, 50, 24, 21, 39, 33},
402 	{5790, 28, 104, 27, 25, 27, 50, 24, 21, 39, 33},
403 	{5780, 28, 104, 27, 25, 27, 49, 24, 21, 39, 33},
404 	{5770, 28, 104, 27, 25, 27, 49, 24, 21, 39, 33},
405 	{5760, 28, 103, 27, 25, 27, 49, 24, 21, 39, 33},
406 	{5750, 28, 103, 27, 25, 27, 49, 24, 21, 39, 33},
407 	{5740, 28, 103, 27, 25, 27, 49, 24, 21, 39, 33},
408 	{5730, 28, 103, 27, 25, 27, 49, 24, 21, 39, 32},
409 	{5720, 28, 102, 27, 25, 27, 49, 24, 21, 39, 32},
410 	{5710, 28, 102, 27, 25, 27, 48, 24, 21, 39, 32},
411 	{5700, 28, 102, 27, 24, 27, 48, 24, 21, 39, 32},
412 	{5690, 28, 102, 27, 24, 27, 48, 24, 21, 39, 32},
413 	{5680, 28, 101, 27, 24, 27, 48, 24, 21, 39, 32},
414 	{5670, 28, 101, 27, 24, 27, 48, 23, 21, 38, 32},
415 	{5660, 28, 101, 27, 24, 26, 49, 23, 21, 38, 32},
416 	{5650, 28, 101, 27, 24, 26, 49, 23, 21, 38, 32},
417 	{5640, 27, 101, 27, 24, 26, 48, 23, 21, 38, 32},
418 	{5630, 27, 101, 27, 24, 26, 48, 23, 21, 38, 32},
419 	{5620, 27, 101, 27, 24, 26, 48, 23, 21, 38, 32},
420 	{5610, 27, 101, 27, 24, 26, 48, 23, 21, 38, 32},
421 	{5600, 27, 101, 26, 24, 26, 48, 23, 20, 38, 32},
422 	{5590, 27, 100, 26, 24, 26, 48, 23, 20, 38, 32},
423 	{5580, 27, 100, 26, 24, 26, 48, 23, 20, 38, 32},
424 	{5570, 27, 100, 26, 24, 26, 48, 23, 20, 38, 31},
425 	{5560, 27, 100, 26, 24, 26, 47, 23, 20, 38, 31},
426 	{5550, 27,  99, 26, 24, 26, 47, 23, 20, 38, 31},
427 	{5540, 27,  99, 26, 24, 26, 47, 23, 20, 38, 31},
428 	{5530, 27,  99, 26, 24, 26, 47, 23, 20, 38, 31},
429 	{5520, 27,  99, 26, 24, 26, 47, 23, 20, 37, 31},
430 	{5510, 27,  98, 26, 24, 26, 47, 23, 20, 37, 31},
431 	{5500, 27,  98, 26, 24, 26, 47, 23, 20, 37, 31},
432 	{5490, 27,  98, 26, 24, 26, 46, 23, 20, 37, 31},
433 	{5480, 27,  98, 26, 24, 26, 46, 23, 20, 37, 31},
434 	{5470, 27,  97, 26, 23, 26, 46, 23, 20, 37, 31},
435 	{5460, 27,  97, 26, 23, 26, 46, 23, 20, 37, 31},
436 	{5450, 27,  97, 26, 23, 25, 47, 23, 20, 37, 31},
437 	{5440, 26,  98, 26, 23, 25, 47, 23, 20, 37, 31},
438 	{5430, 26,  98, 26, 23, 25, 47, 22, 20, 37, 31},
439 	{5420, 26,  97, 26, 23, 25, 46, 22, 20, 37, 31},
440 	{5410, 26,  97, 26, 23, 25, 46, 22, 20, 37, 31},
441 	{5400, 26,  97, 25, 23, 25, 46, 22, 20, 37, 30},
442 	{5390, 26,  97, 25, 23, 25, 46, 22, 20, 37, 30},
443 	{5380, 26,  96, 25, 23, 25, 46, 22, 20, 36, 30},
444 	{5370, 26,  96, 25, 23, 25, 46, 22, 20, 36, 30},
445 	{5360, 26,  96, 25, 23, 25, 46, 22, 20, 36, 30},
446 	{5350, 26,  96, 25, 23, 25, 46, 22, 20, 36, 30},
447 	{5340, 26,  95, 25, 23, 25, 45, 22, 20, 36, 30},
448 	{5330, 26,  95, 25, 23, 25, 45, 22, 19, 36, 30},
449 	{5320, 26,  95, 25, 23, 25, 45, 22, 19, 36, 30},
450 	{5310, 26,  95, 25, 23, 25, 45, 22, 19, 36, 30},
451 	{5300, 26,  95, 25, 23, 25, 45, 22, 19, 36, 30},
452 	{5290, 26,  94, 25, 23, 25, 45, 22, 19, 36, 30},
453 	{5280, 26,  94, 25, 23, 25, 45, 22, 19, 36, 30},
454 	{5270, 26,  94, 25, 23, 25, 44, 22, 19, 36, 30},
455 	{5260, 26,  94, 25, 23, 25, 44, 22, 19, 36, 30},
456 	{5250, 25,  94, 25, 23, 24, 45, 22, 19, 36, 30},
457 	{5240, 25,  94, 25, 23, 24, 45, 22, 19, 36, 29},
458 	{5230, 25,  94, 25, 22, 24, 45, 22, 19, 35, 29},
459 	{5220, 25,  94, 25, 22, 24, 45, 22, 19, 35, 29},
460 	{5210, 25,  93, 25, 22, 24, 45, 22, 19, 35, 29},
461 	{5200, 25,  93, 24, 22, 24, 44, 21, 19, 35, 29},
462 	{5190, 25,  93, 24, 22, 24, 44, 21, 19, 35, 29},
463 	{5180, 25,  93, 24, 22, 24, 44, 21, 19, 35, 29},
464 	{5170, 25,  92, 24, 22, 24, 44, 21, 19, 35, 29},
465 	{5160, 25,  92, 24, 22, 24, 44, 21, 19, 35, 29},
466 	{5150, 25,  92, 24, 22, 24, 44, 21, 19, 35, 29},
467 	{5140, 25,  92, 24, 22, 24, 44, 21, 19, 35, 29},
468 	{5130, 25,  92, 24, 22, 24, 43, 21, 19, 35, 29},
469 	{5120, 25,  91, 24, 22, 24, 43, 21, 19, 35, 29},
470 	{5110, 25,  91, 24, 22, 24, 43, 21, 19, 35, 29},
471 	{5100, 25,  91, 24, 22, 24, 43, 21, 19, 35, 29},
472 	{5090, 25,  91, 24, 22, 24, 43, 21, 19, 34, 29},
473 	{5080, 25,  90, 24, 22, 24, 43, 21, 19, 34, 29},
474 	{5070, 25,  90, 24, 22, 24, 43, 21, 19, 34, 28},
475 	{5060, 25,  90, 24, 22, 24, 43, 21, 18, 34, 28},
476 	{5050, 24,  91, 24, 22, 24, 42, 21, 18, 34, 28},
477 	{5040, 24,  90, 24, 22, 23, 43, 21, 18, 34, 28},
478 	{5030, 24,  90, 24, 22, 23, 43, 21, 18, 34, 28},
479 	{5020, 24,  90, 24, 22, 23, 43, 21, 18, 34, 28},
480 	{5010, 24,  90, 24, 22, 23, 43, 21, 18, 34, 28},
481 	{5000, 24,  89, 23, 21, 23, 43, 21, 18, 34, 28},
482 	{4990, 24,  89, 23, 21, 23, 43, 21, 18, 34, 28},
483 	{4980, 24,  89, 23, 21, 23, 42, 21, 18, 34, 28},
484 	{4970, 24,  89, 23, 21, 23, 42, 21, 18, 34, 28},
485 	{4960, 24,  89, 23, 21, 23, 42, 20, 18, 34, 28},
486 	{4950, 24,  88, 23, 21, 23, 42, 20, 18, 34, 28},
487 	{4940, 24,  88, 23, 21, 23, 42, 20, 18, 33, 28},
488 	{4930, 24,  88, 23, 21, 23, 42, 20, 18, 33, 28},
489 	{4920, 24,  88, 23, 21, 23, 42, 20, 18, 33, 28},
490 	{4910, 24,  87, 23, 21, 23, 41, 20, 18, 33, 28},
491 	{4900, 24,  87, 23, 21, 23, 41, 20, 18, 33, 27},
492 	{4890, 24,  87, 23, 21, 23, 41, 20, 18, 33, 27},
493 	{4880, 24,  87, 23, 21, 23, 41, 20, 18, 33, 27},
494 	{4870, 24,  86, 23, 21, 23, 41, 20, 18, 33, 27},
495 	{4860, 24,  86, 23, 21, 23, 41, 20, 18, 33, 27},
496 	{4850, 23,  87, 23, 21, 23, 41, 20, 18, 33, 27},
497 	{4840, 23,  87, 23, 21, 23, 40, 20, 18, 33, 27},
498 	{4830, 23,  86, 23, 21, 22, 41, 20, 18, 33, 27},
499 	{4820, 23,  86, 23, 21, 22, 41, 20, 18, 33, 27},
500 	{4810, 23,  86, 23, 21, 22, 41, 20, 18, 33, 27},
501 	{4800, 23,  86, 22, 21, 22, 41, 20, 17, 32, 27},
502 	{4790, 23,  86, 22, 21, 22, 41, 20, 17, 32, 27},
503 	{4780, 23,  85, 22, 21, 22, 41, 20, 17, 32, 27},
504 	{4770, 23,  85, 22, 21, 22, 41, 20, 17, 32, 27},
505 	{4760, 23,  85, 22, 20, 22, 40, 20, 17, 32, 27},
506 	{4750, 23,  85, 22, 20, 22, 40, 20, 17, 32, 27},
507 	{4740, 23,  84, 22, 20, 22, 40, 20, 17, 32, 26},
508 	{4730, 23,  84, 22, 20, 22, 40, 19, 17, 32, 26},
509 	{4720, 23,  84, 22, 20, 22, 40, 19, 17, 32, 26},
510 	{4710, 23,  84, 22, 20, 22, 40, 19, 17, 32, 26},
511 	{4700, 23,  83, 22, 20, 22, 40, 19, 17, 32, 26},
512 	{4690, 23,  83, 22, 20, 22, 39, 19, 17, 32, 26},
513 	{4680, 23,  83, 22, 20, 22, 39, 19, 17, 32, 26},
514 	{4670, 23,  83, 22, 20, 22, 39, 19, 17, 32, 26},
515 	{4660, 23,  82, 22, 20, 22, 39, 19, 17, 32, 26},
516 	{4650, 22,  83, 22, 20, 22, 39, 19, 17, 31, 26},
517 	{4640, 22,  83, 22, 20, 22, 39, 19, 17, 31, 26},
518 	{4630, 22,  83, 22, 20, 22, 39, 19, 17, 31, 26},
519 	{4620, 22,  83, 22, 20, 21, 39, 19, 17, 31, 26},
520 	{4610, 22,  82, 22, 20, 21, 39, 19, 17, 31, 26},
521 	{4600, 22,  82, 21, 20, 21, 39, 19, 17, 31, 26},
522 	{4590, 22,  82, 21, 20, 21, 39, 19, 17, 31, 26},
523 	{4580, 22,  82, 21, 20, 21, 39, 19, 17, 31, 26},
524 	{4570, 22,  81, 21, 20, 21, 39, 19, 17, 31, 25},
525 	{4560, 22,  81, 21, 20, 21, 39, 19, 17, 31, 25},
526 	{4550, 22,  81, 21, 20, 21, 38, 19, 17, 31, 25},
527 	{4540, 22,  81, 21, 20, 21, 38, 19, 17, 31, 25},
528 	{4530, 22,  80, 21, 19, 21, 38, 19, 16, 31, 25},
529 	{4520, 22,  80, 21, 19, 21, 38, 19, 16, 31, 25},
530 	{4510, 22,  80, 21, 19, 21, 38, 19, 16, 31, 25},
531 	{4500, 22,  80, 21, 19, 21, 38, 19, 16, 30, 25},
532 	{4490, 22,  80, 21, 19, 21, 38, 18, 16, 30, 25},
533 	{4480, 22,  79, 21, 19, 21, 38, 18, 16, 30, 25},
534 	{4470, 22,  79, 21, 19, 21, 37, 18, 16, 30, 25},
535 	{4460, 22,  79, 21, 19, 21, 37, 18, 16, 30, 25},
536 	{4450, 21,  80, 21, 19, 21, 37, 18, 16, 30, 25},
537 	{4440, 21,  79, 21, 19, 21, 37, 18, 16, 30, 25},
538 	{4430, 21,  79, 21, 19, 21, 37, 18, 16, 30, 25},
539 	{4420, 21,  79, 21, 19, 21, 37, 18, 16, 30, 25},
540 	{4410, 21,  79, 21, 19, 20, 38, 18, 16, 30, 25},
541 	{4400, 21,  78, 20, 19, 20, 37, 18, 16, 30, 24},
542 	{4390, 21,  78, 20, 19, 20, 37, 18, 16, 30, 24},
543 	{4380, 21,  78, 20, 19, 20, 37, 18, 16, 30, 24},
544 	{4370, 21,  78, 20, 19, 20, 37, 18, 16, 30, 24},
545 	{4360, 21,  77, 20, 19, 20, 37, 18, 16, 29, 24},
546 	{4350, 21,  77, 20, 19, 20, 37, 18, 16, 29, 24},
547 	{4340, 21,  77, 20, 19, 20, 37, 18, 16, 29, 24},
548 	{4330, 21,  77, 20, 19, 20, 36, 18, 16, 29, 24},
549 	{4320, 21,  77, 20, 19, 20, 36, 18, 16, 29, 24},
550 	{4310, 21,  76, 20, 19, 20, 36, 18, 16, 29, 24},
551 	{4300, 21,  76, 20, 18, 20, 36, 18, 16, 29, 24},
552 	{4290, 21,  76, 20, 18, 20, 36, 18, 16, 29, 24},
553 	{4280, 21,  76, 20, 18, 20, 36, 18, 16, 29, 24},
554 	{4270, 21,  75, 20, 18, 20, 36, 18, 16, 29, 24},
555 	{4260, 21,  75, 20, 18, 20, 35, 17, 15, 29, 24},
556 	{4250, 20,  76, 20, 18, 20, 35, 17, 15, 29, 24},
557 	{4240, 20,  76, 20, 18, 20, 35, 17, 15, 29, 23},
558 	{4230, 20,  75, 20, 18, 20, 35, 17, 15, 29, 23},
559 	{4220, 20,  75, 20, 18, 20, 35, 17, 15, 29, 23},
560 	{4210, 20,  75, 20, 18, 20, 35, 17, 15, 28, 23},
561 	{4200, 20,  75, 19, 18, 19, 36, 17, 15, 28, 23},
562 	{4190, 20,  74, 19, 18, 19, 36, 17, 15, 28, 23},
563 	{4180, 20,  74, 19, 18, 19, 35, 17, 15, 28, 23},
564 	{4170, 20,  74, 19, 18, 19, 35, 17, 15, 28, 23},
565 	{4160, 20,  74, 19, 18, 19, 35, 17, 15, 28, 23},
566 	{4150, 20,  74, 19, 18, 19, 35, 17, 15, 28, 23},
567 	{4140, 20,  73, 19, 18, 19, 35, 17, 15, 28, 23},
568 	{4130, 20,  73, 19, 18, 19, 35, 17, 15, 28, 23},
569 	{4120, 20,  73, 19, 18, 19, 35, 17, 15, 28, 23},
570 	{4110, 20,  73, 19, 18, 19, 34, 17, 15, 28, 23},
571 	{4100, 20,  72, 19, 18, 19, 34, 17, 15, 28, 23},
572 	{4090, 20,  72, 19, 18, 19, 34, 17, 15, 28, 23},
573 	{4080, 20,  72, 19, 18, 19, 34, 17, 15, 28, 23},
574 	{4070, 20,  72, 19, 18, 19, 34, 17, 15, 27, 22},
575 	{4060, 19,  72, 19, 17, 19, 34, 17, 15, 27, 22},
576 	{4050, 19,  72, 19, 17, 19, 34, 17, 15, 27, 22},
577 	{4040, 19,  72, 19, 17, 19, 33, 17, 15, 27, 22},
578 	{4030, 19,  72, 19, 17, 19, 33, 17, 15, 27, 22},
579 	{4020, 19,  71, 19, 17, 19, 33, 16, 15, 27, 22},
580 	{4010, 19,  71, 19, 17, 19, 33, 16, 15, 27, 22},
581 	{4000, 19,  71, 18, 17, 19, 33, 16, 14, 27, 22},
582 	{3990, 19,  71, 18, 17, 18, 34, 16, 14, 27, 22},
583 	{3980, 19,  71, 18, 17, 18, 34, 16, 14, 27, 22},
584 	{3970, 19,  70, 18, 17, 18, 33, 16, 14, 27, 22},
585 	{3960, 19,  70, 18, 17, 18, 33, 16, 14, 27, 22},
586 	{3950, 19,  70, 18, 17, 18, 33, 16, 14, 27, 22},
587 	{3940, 19,  70, 18, 17, 18, 33, 16, 14, 27, 22},
588 	{3930, 19,  69, 18, 17, 18, 33, 16, 14, 27, 22},
589 	{3920, 19,  69, 18, 17, 18, 33, 16, 14, 26, 22},
590 	{3910, 19,  69, 18, 17, 18, 33, 16, 14, 26, 22},
591 	{3900, 19,  69, 18, 17, 18, 33, 16, 14, 26, 21},
592 	{3890, 19,  68, 18, 17, 18, 32, 16, 14, 26, 21},
593 	{3880, 19,  68, 18, 17, 18, 32, 16, 14, 26, 21},
594 	{3870, 19,  68, 18, 17, 18, 32, 16, 14, 26, 21},
595 	{3860, 18,  69, 18, 17, 18, 32, 16, 14, 26, 21},
596 	{3850, 18,  68, 18, 17, 18, 32, 16, 14, 26, 21},
597 	{3840, 18,  68, 18, 17, 18, 32, 16, 14, 26, 21},
598 	{3830, 18,  68, 18, 16, 18, 32, 16, 14, 26, 21},
599 	{3820, 18,  68, 18, 16, 18, 31, 16, 14, 26, 21},
600 	{3810, 18,  68, 18, 16, 18, 31, 16, 14, 26, 21},
601 	{3800, 18,  67, 17, 16, 18, 31, 16, 14, 26, 21},
602 	{3790, 18,  67, 17, 16, 17, 32, 15, 14, 26, 21},
603 	{3780, 18,  67, 17, 16, 17, 32, 15, 14, 25, 21},
604 	{3770, 18,  67, 17, 16, 17, 32, 15, 14, 25, 21},
605 	{3760, 18,  66, 17, 16, 17, 32, 15, 14, 25, 21},
606 	{3750, 18,  66, 17, 16, 17, 31, 15, 14, 25, 21},
607 	{3740, 18,  66, 17, 16, 17, 31, 15, 14, 25, 20},
608 	{3730, 18,  66, 17, 16, 17, 31, 15, 13, 25, 20},
609 	{3720, 18,  65, 17, 16, 17, 31, 15, 13, 25, 20},
610 	{3710, 18,  65, 17, 16, 17, 31, 15, 13, 25, 20},
611 	{3700, 18,  65, 17, 16, 17, 31, 15, 13, 25, 20},
612 	{3690, 18,  65, 17, 16, 17, 31, 15, 13, 25, 20},
613 	{3680, 18,  64, 17, 16, 17, 31, 15, 13, 25, 20},
614 	{3670, 18,  64, 17, 16, 17, 30, 15, 13, 25, 20},
615 	{3660, 17,  65, 17, 16, 17, 30, 15, 13, 25, 20},
616 	{3650, 17,  65, 17, 16, 17, 30, 15, 13, 25, 20},
617 	{3640, 17,  65, 17, 16, 17, 30, 15, 13, 25, 20},
618 	{3630, 17,  64, 17, 16, 17, 30, 15, 13, 24, 20},
619 	{3620, 17,  64, 17, 16, 17, 30, 15, 13, 24, 20},
620 	{3610, 17,  64, 17, 16, 17, 30, 15, 13, 24, 20},
621 	{3600, 17,  64, 16, 16, 17, 29, 15, 13, 24, 20},
622 	{3590, 17,  63, 16, 15, 17, 29, 15, 13, 24, 20},
623 	{3580, 17,  63, 16, 15, 16, 30, 15, 13, 24, 20},
624 	{3570, 17,  63, 16, 15, 16, 30, 15, 13, 24, 19},
625 	{3560, 17,  63, 16, 15, 16, 30, 14, 13, 24, 19},
626 	{3550, 17,  62, 16, 15, 16, 30, 14, 13, 24, 19},
627 	{3540, 17,  62, 16, 15, 16, 30, 14, 13, 24, 19},
628 	{3530, 17,  62, 16, 15, 16, 29, 14, 13, 24, 19},
629 	{3520, 17,  62, 16, 15, 16, 29, 14, 13, 24, 19},
630 	{3510, 17,  62, 16, 15, 16, 29, 14, 13, 24, 19},
631 	{3500, 17,  61, 16, 15, 16, 29, 14, 13, 24, 19},
632 	{3490, 17,  61, 16, 15, 16, 29, 14, 13, 23, 19},
633 	{3480, 17,  61, 16, 15, 16, 29, 14, 13, 23, 19},
634 	{3470, 17,  61, 16, 15, 16, 29, 14, 13, 23, 19},
635 	{3460, 16,  61, 16, 15, 16, 28, 14, 12, 23, 19},
636 	{3450, 16,  61, 16, 15, 16, 28, 14, 12, 23, 19},
637 	{3440, 16,  61, 16, 15, 16, 28, 14, 12, 23, 19},
638 	{3430, 16,  61, 16, 15, 16, 28, 14, 12, 23, 19},
639 	{3420, 16,  60, 16, 15, 16, 28, 14, 12, 23, 19},
640 	{3410, 16,  60, 16, 15, 16, 28, 14, 12, 23, 18},
641 	{3400, 16,  60, 15, 15, 16, 28, 14, 12, 23, 18},
642 	{3390, 16,  60, 15, 15, 16, 28, 14, 12, 23, 18},
643 	{3380, 16,  59, 15, 15, 16, 27, 14, 12, 23, 18},
644 	{3370, 16,  59, 15, 15, 15, 28, 14, 12, 23, 18},
645 	{3360, 16,  59, 15, 14, 15, 28, 14, 12, 23, 18},
646 	{3350, 16,  59, 15, 14, 15, 28, 14, 12, 23, 18},
647 	{3340, 16,  59, 15, 14, 15, 28, 14, 12, 22, 18},
648 	{3330, 16,  58, 15, 14, 15, 28, 14, 12, 22, 18},
649 	{3320, 16,  58, 15, 14, 15, 28, 13, 12, 22, 18},
650 	{3310, 16,  58, 15, 14, 15, 27, 13, 12, 22, 18},
651 	{3300, 16,  58, 15, 14, 15, 27, 13, 12, 22, 18},
652 	{3290, 16,  57, 15, 14, 15, 27, 13, 12, 22, 18},
653 	{3280, 16,  57, 15, 14, 15, 27, 13, 12, 22, 18},
654 	{3270, 16,  57, 15, 14, 15, 27, 13, 12, 22, 18},
655 	{3260, 15,  58, 15, 14, 15, 27, 13, 12, 22, 18},
656 	{3250, 15,  57, 15, 14, 15, 27, 13, 12, 22, 18},
657 	{3240, 15,  57, 15, 14, 15, 26, 13, 12, 22, 17},
658 	{3230, 15,  57, 15, 14, 15, 26, 13, 12, 22, 17},
659 	{3220, 15,  57, 15, 14, 15, 26, 13, 12, 22, 17},
660 	{3210, 15,  56, 15, 14, 15, 26, 13, 12, 22, 17},
661 	{3200, 15,  56, 14, 14, 15, 26, 13, 11, 21, 17},
662 	{3190, 15,  56, 14, 14, 15, 26, 13, 11, 21, 17},
663 	{3180, 15,  56, 14, 14, 15, 26, 13, 11, 21, 17},
664 	{3170, 15,  56, 14, 14, 15, 25, 13, 11, 21, 17},
665 	{3160, 15,  55, 14, 14, 14, 26, 13, 11, 21, 17},
666 	{3150, 15,  55, 14, 14, 14, 26, 13, 11, 21, 17},
667 	{3140, 15,  55, 14, 14, 14, 26, 13, 11, 21, 17},
668 	{3130, 15,  55, 14, 14, 14, 26, 13, 11, 21, 17},
669 	{3120, 15,  54, 14, 13, 14, 26, 13, 11, 21, 17},
670 	{3110, 15,  54, 14, 13, 14, 26, 13, 11, 21, 17},
671 	{3100, 15,  54, 14, 13, 14, 26, 13, 11, 21, 17},
672 	{3090, 15,  54, 14, 13, 14, 25, 12, 11, 21, 17},
673 	{3080, 15,  53, 14, 13, 14, 25, 12, 11, 21, 17},
674 	{3070, 14,  54, 14, 13, 14, 25, 12, 11, 21, 16},
675 	{3060, 14,  54, 14, 13, 14, 25, 12, 11, 21, 16},
676 	{3050, 14,  54, 14, 13, 14, 25, 12, 11, 20, 16},
677 	{3040, 14,  53, 14, 13, 14, 25, 12, 11, 20, 16},
678 	{3030, 14,  53, 14, 13, 14, 25, 12, 11, 20, 16},
679 	{3020, 14,  53, 14, 13, 14, 24, 12, 11, 20, 16},
680 	{3010, 14,  53, 14, 13, 14, 24, 12, 11, 20, 16},
681 	{3000, 14,  53, 13, 13, 14, 24, 12, 11, 20, 16},
682 	{2990, 14,  52, 13, 13, 14, 24, 12, 11, 20, 16},
683 	{2980, 14,  52, 13, 13, 14, 24, 12, 11, 20, 16},
684 	{2970, 14,  52, 13, 13, 14, 24, 12, 11, 20, 16},
685 	{2960, 14,  52, 13, 13, 14, 24, 12, 11, 20, 16},
686 	{2950, 14,  51, 13, 13, 13, 24, 12, 11, 20, 16},
687 	{2940, 14,  51, 13, 13, 13, 24, 12, 11, 20, 16},
688 	{2930, 14,  51, 13, 13, 13, 24, 12, 10, 20, 16},
689 	{2920, 14,  51, 13, 13, 13, 24, 12, 10, 20, 16},
690 	{2910, 14,  50, 13, 13, 13, 24, 12, 10, 20, 15},
691 	{2900, 14,  50, 13, 13, 13, 24, 12, 10, 19, 15},
692 	{2890, 14,  50, 13, 12, 13, 24, 12, 10, 19, 15},
693 	{2880, 14,  50, 13, 12, 13, 23, 12, 10, 19, 15},
694 	{2870, 13,  50, 13, 12, 13, 23, 12, 10, 19, 15},
695 	{2860, 13,  50, 13, 12, 13, 23, 12, 10, 19, 15},
696 	{2850, 13,  50, 13, 12, 13, 23, 11, 10, 19, 15},
697 	{2840, 13,  50, 13, 12, 13, 23, 11, 10, 19, 15},
698 	{2830, 13,  50, 13, 12, 13, 23, 11, 10, 19, 15},
699 	{2820, 13,  49, 13, 12, 13, 23, 11, 10, 19, 15},
700 	{2810, 13,  49, 13, 12, 13, 23, 11, 10, 19, 15},
701 	{2800, 13,  49, 12, 12, 13, 22, 11, 10, 19, 15},
702 	{2790, 13,  49, 12, 12, 13, 22, 11, 10, 19, 15},
703 	{2780, 13,  48, 12, 12, 13, 22, 11, 10, 19, 15},
704 	{2770, 13,  48, 12, 12, 13, 22, 11, 10, 19, 15},
705 	{2760, 13,  48, 12, 12, 13, 22, 11, 10, 18, 15},
706 	{2750, 13,  48, 12, 12, 13, 22, 11, 10, 18, 15},
707 	{2740, 13,  47, 12, 12, 12, 23, 11, 10, 18, 14},
708 	{2730, 13,  47, 12, 12, 12, 22, 11, 10, 18, 14},
709 	{2720, 13,  47, 12, 12, 12, 22, 11, 10, 18, 14},
710 	{2710, 13,  47, 12, 12, 12, 22, 11, 10, 18, 14},
711 	{2700, 13,  47, 12, 12, 12, 22, 11, 10, 18, 14},
712 	{2690, 13,  46, 12, 12, 12, 22, 11, 10, 18, 14},
713 	{2680, 13,  46, 12, 12, 12, 22, 11, 10, 18, 14},
714 	{2670, 12,  47, 12, 12, 12, 22, 11, 10, 18, 14},
715 	{2660, 12,  47, 12, 12, 12, 21, 11,  9, 18, 14},
716 	{2650, 12,  46, 12, 11, 12, 21, 11,  9, 18, 14},
717 	{2640, 12,  46, 12, 11, 12, 21, 11,  9, 18, 14},
718 	{2630, 12,  46, 12, 11, 12, 21, 11,  9, 18, 14},
719 	{2620, 12,  46, 12, 11, 12, 21, 10,  9, 18, 14},
720 	{2610, 12,  45, 12, 11, 12, 21, 10,  9, 17, 14},
721 	{2600, 12,  45, 11, 11, 12, 21, 10,  9, 17, 14},
722 	{2590, 12,  45, 11, 11, 12, 20, 10,  9, 17, 14},
723 	{2580, 12,  45, 11, 11, 12, 20, 10,  9, 17, 14},
724 	{2570, 12,  44, 11, 11, 12, 20, 10,  9, 17, 13},
725 	{2560, 12,  44, 11, 11, 12, 20, 10,  9, 17, 13},
726 	{2550, 12,  44, 11, 11, 12, 20, 10,  9, 17, 13},
727 	{2540, 12,  44, 11, 11, 11, 21, 10,  9, 17, 13},
728 	{2530, 12,  44, 11, 11, 11, 21, 10,  9, 17, 13},
729 	{2520, 12,  43, 11, 11, 11, 21, 10,  9, 17, 13},
730 	{2510, 12,  43, 11, 11, 11, 20, 10,  9, 17, 13},
731 	{2500, 12,  43, 11, 11, 11, 20, 10,  9, 17, 13},
732 	{2490, 12,  43, 11, 11, 11, 20, 10,  9, 17, 13},
733 	{2480, 12,  42, 11, 11, 11, 20, 10,  9, 17, 13},
734 	{2470, 11,  43, 11, 11, 11, 20, 10,  9, 16, 13},
735 	{2460, 11,  43, 11, 11, 11, 20, 10,  9, 16, 13},
736 	{2450, 11,  43, 11, 11, 11, 20, 10,  9, 16, 13},
737 	{2440, 11,  42, 11, 11, 11, 19, 10,  9, 16, 13},
738 	{2430, 11,  42, 11, 11, 11, 19, 10,  9, 16, 13},
739 	{2420, 11,  42, 11, 10, 11, 19, 10,  9, 16, 13},
740 	{2410, 11,  42, 11, 10, 11, 19, 10,  9, 16, 12},
741 	{2400, 11,  41, 10, 10, 11, 19, 10,  8, 16, 12},
742 	{2390, 11,  41, 10, 10, 11, 19, 10,  8, 16, 12},
743 	{2380, 11,  41, 10, 10, 11, 19,  9,  8, 16, 12},
744 	{2370, 11,  41, 10, 10, 11, 18,  9,  8, 16, 12},
745 	{2360, 11,  41, 10, 10, 11, 18,  9,  8, 16, 12},
746 	{2350, 11,  40, 10, 10, 11, 18,  9,  8, 16, 12},
747 	{2340, 11,  40, 10, 10, 11, 18,  9,  8, 16, 12},
748 	{2330, 11,  40, 10, 10, 10, 19,  9,  8, 16, 12},
749 	{2320, 11,  40, 10, 10, 10, 19,  9,  8, 15, 12},
750 	{2310, 11,  39, 10, 10, 10, 19,  9,  8, 15, 12},
751 	{2300, 11,  39, 10, 10, 10, 18,  9,  8, 15, 12},
752 	{2290, 11,  39, 10, 10, 10, 18,  9,  8, 15, 12},
753 	{2280, 11,  39, 10, 10, 10, 18,  9,  8, 15, 12},
754 	{2270, 10,  39, 10, 10, 10, 18,  9,  8, 15, 12},
755 	{2260, 10,  39, 10, 10, 10, 18,  9,  8, 15, 12},
756 	{2250, 10,  39, 10, 10, 10, 18,  9,  8, 15, 12},
757 	{2240, 10,  39, 10, 10, 10, 18,  9,  8, 15, 11},
758 	{2230, 10,  38, 10, 10, 10, 18,  9,  8, 15, 11},
759 	{2220, 10,  38, 10, 10, 10, 17,  9,  8, 15, 11},
760 	{2210, 10,  38, 10, 10, 10, 17,  9,  8, 15, 11},
761 	{2200, 10,  38,  9, 10, 10, 17,  9,  8, 15, 11},
762 	{2190, 10,  38,  9,  9, 10, 17,  9,  8, 15, 11},
763 	{2180, 10,  37,  9,  9, 10, 17,  9,  8, 14, 11},
764 	{2170, 10,  37,  9,  9, 10, 17,  9,  8, 14, 11},
765 	{2160, 10,  37,  9,  9, 10, 17,  9,  8, 14, 11},
766 	{2150, 10,  37,  9,  9, 10, 16,  8,  8, 14, 11},
767 	{2140, 10,  36,  9,  9, 10, 16,  8,  8, 14, 11},
768 	{2130, 10,  36,  9,  9, 10, 16,  8,  7, 14, 11},
769 	{2120, 10,  36,  9,  9,  9, 17,  8,  7, 14, 11},
770 	{2110, 10,  36,  9,  9,  9, 17,  8,  7, 14, 11},
771 	{2100, 10,  35,  9,  9,  9, 17,  8,  7, 14, 11},
772 	{2090, 10,  35,  9,  9,  9, 17,  8,  7, 14, 11},
773 	{2080,  9,  36,  9,  9,  9, 16,  8,  7, 14, 11},
774 	{2070,  9,  36,  9,  9,  9, 16,  8,  7, 14, 10},
775 	{2060,  9,  35,  9,  9,  9, 16,  8,  7, 14, 10},
776 	{2050,  9,  35,  9,  9,  9, 16,  8,  7, 14, 10},
777 	{2040,  9,  35,  9,  9,  9, 16,  8,  7, 14, 10},
778 	{2030,  9,  35,  9,  9,  9, 16,  8,  7, 13, 10},
779 	{2020,  9,  35,  9,  9,  9, 16,  8,  7, 13, 10},
780 	{2010,  9,  34,  9,  9,  9, 15,  8,  7, 13, 10},
781 	{2000,  9,  34,  8,  9,  9, 15,  8,  7, 13, 10},
782 	{1990,  9,  34,  8,  9,  9, 15,  8,  7, 13, 10},
783 	{1980,  9,  34,  8,  9,  9, 15,  8,  7, 13, 10},
784 	{1970,  9,  33,  8,  9,  9, 15,  8,  7, 13, 10},
785 	{1960,  9,  33,  8,  9,  9, 15,  8,  7, 13, 10},
786 	{1950,  9,  33,  8,  8,  9, 15,  8,  7, 13, 10},
787 	{1940,  9,  33,  8,  8,  9, 15,  8,  7, 13, 10},
788 	{1930,  9,  32,  8,  8,  9, 14,  8,  7, 13, 10},
789 	{1920,  9,  32,  8,  8,  9, 14,  8,  7, 13, 10},
790 	{1910,  9,  32,  8,  8,  8, 15,  7,  7, 13,  9},
791 	{1900,  9,  32,  8,  8,  8, 15,  7,  7, 13,  9},
792 	{1890,  9,  31,  8,  8,  8, 15,  7,  7, 12,  9},
793 	{1880,  8,  32,  8,  8,  8, 15,  7,  7, 12,  9},
794 	{1870,  8,  32,  8,  8,  8, 15,  7,  7, 12,  9},
795 	{1860,  8,  32,  8,  8,  8, 14,  7,  6, 12,  9},
796 	{1850,  8,  32,  8,  8,  8, 14,  7,  6, 12,  9},
797 	{1840,  8,  31,  8,  8,  8, 14,  7,  6, 12,  9},
798 	{1830,  8,  31,  8,  8,  8, 14,  7,  6, 12,  9},
799 	{1820,  8,  31,  8,  8,  8, 14,  7,  6, 12,  9},
800 	{1810,  8,  31,  8,  8,  8, 14,  7,  6, 12,  9},
801 	{1800,  8,  30,  7,  8,  8, 14,  7,  6, 12,  9},
802 	{1790,  8,  30,  7,  8,  8, 13,  7,  6, 12,  9},
803 	{1780,  8,  30,  7,  8,  8, 13,  7,  6, 12,  9},
804 	{1770,  8,  30,  7,  8,  8, 13,  7,  6, 12,  9},
805 	{1760,  8,  29,  7,  8,  8, 13,  7,  6, 12,  9},
806 	{1750,  8,  29,  7,  8,  8, 13,  7,  6, 12,  9},
807 	{1740,  8,  29,  7,  8,  8, 13,  7,  6, 11,  8},
808 	{1730,  8,  29,  7,  8,  8, 13,  7,  6, 11,  8},
809 	{1720,  8,  29,  7,  7,  8, 13,  7,  6, 11,  8},
810 	{1710,  8,  28,  7,  7,  8, 12,  7,  6, 11,  8},
811 	{1700,  8,  28,  7,  7,  7, 13,  7,  6, 11,  8},
812 	{1690,  8,  28,  7,  7,  7, 13,  7,  6, 11,  8},
813 	{1680,  7,  29,  7,  7,  7, 13,  6,  6, 11,  8},
814 	{1670,  7,  28,  7,  7,  7, 13,  6,  6, 11,  8},
815 	{1660,  7,  28,  7,  7,  7, 13,  6,  6, 11,  8},
816 	{1650,  7,  28,  7,  7,  7, 13,  6,  6, 11,  8},
817 	{1640,  7,  28,  7,  7,  7, 12,  6,  6, 11,  8},
818 	{1630,  7,  27,  7,  7,  7, 12,  6,  6, 11,  8},
819 	{1620,  7,  27,  7,  7,  7, 12,  6,  6, 11,  8},
820 	{1610,  7,  27,  7,  7,  7, 12,  6,  6, 11,  8},
821 	{1600,  7,  27,  6,  7,  7, 12,  6,  5, 10,  8},
822 	{1590,  7,  26,  6,  7,  7, 12,  6,  5, 10,  8},
823 	{1580,  7,  26,  6,  7,  7, 12,  6,  5, 10,  7},
824 	{1570,  7,  26,  6,  7,  7, 11,  6,  5, 10,  7},
825 	{1560,  7,  26,  6,  7,  7, 11,  6,  5, 10,  7},
826 	{1550,  7,  26,  6,  7,  7, 11,  6,  5, 10,  7},
827 	{1540,  7,  25,  6,  7,  7, 11,  6,  5, 10,  7},
828 	{1530,  7,  25,  6,  7,  7, 11,  6,  5, 10,  7},
829 	{1520,  7,  25,  6,  7,  7, 11,  6,  5, 10,  7},
830 	{1510,  7,  25,  6,  7,  7, 11,  6,  5, 10,  7},
831 	{1500,  7,  24,  6,  7,  7, 10,  6,  5, 10,  7},
832 	{1490, 59,  25,  6, 77, 59, 10, 70, 44,  9, 73},
833 	{1480, 59,  24,  6, 76, 58, 10, 70, 44,  9, 73},
834 	{1470, 58,  24,  6, 76, 58, 10, 69, 44,  9, 72},
835 	{1460, 58,  24,  6, 76, 58, 10, 69, 43,  9, 72},
836 	{1450, 58,  24,  6, 75, 57, 10, 68, 43,  9, 71},
837 	{1440, 57,  24,  6, 75, 57, 10, 68, 43,  9, 71},
838 	{1430, 57,  23,  6, 75, 57, 10, 68, 43,  8, 70},
839 	{1420, 56,  23,  6, 74, 57,  9, 67, 43,  8, 70},
840 	{1410, 56,  23,  6, 74, 57,  9, 67, 43,  8, 69},
841 	{1400, 56,  23,  5, 74, 55,  9, 67, 41,  8, 69},
842 	{1390, 55,  23,  5, 73, 55,  9, 66, 41,  8, 68},
843 	{1380, 55,  23,  5, 73, 54,  9, 66, 41,  8, 68},
844 	{1370, 54,  22,  5, 72, 54,  9, 66, 41,  8, 67},
845 	{1360, 54,  22,  5, 72, 54,  9, 65, 40,  8, 67},
846 	{1350, 54,  22,  5, 72, 53,  9, 65, 40,  8, 66},
847 	{1340, 53,  22,  5, 71, 53,  9, 65, 40,  8, 66},
848 	{1330, 53,  22,  5, 71, 53,  9, 64, 39,  8, 65},
849 	{1320, 52,  22,  5, 71, 53,  8, 64, 40,  8, 65},
850 	{1310, 52,  21,  5, 70, 53,  8, 64, 40,  8, 64},
851 	{1300, 51,  21,  5, 70, 51,  8, 63, 38,  8, 64},
852 	{1290, 51,  21,  5, 70, 51,  8, 63, 38,  7, 64},
853 	{1280, 51,  21,  5, 69, 51,  8, 63, 38,  7, 63},
854 	{1270, 50,  21,  5, 69, 50,  8, 62, 38,  7, 63},
855 	{1260, 50,  20,  5, 69, 50,  8, 62, 37,  7, 62},
856 	{1250, 49,  20,  5, 68, 49,  8, 62, 37,  7, 62},
857 	{1240, 49,  20,  5, 68, 49,  8, 61, 37,  7, 61},
858 	{1230, 49,  20,  5, 68, 49,  8, 61, 36,  7, 61},
859 	{1220, 48,  20,  5, 67, 48,  8, 61, 36,  7, 60},
860 	{1210, 48,  19,  5, 67, 48,  7, 60, 36,  7, 60},
861 	{1200, 49,  19,  4, 67, 49,  7, 60, 36,  7, 59},
862 	{1190, 48,  19,  4, 66, 48,  7, 60, 36,  7, 59},
863 	{1180, 48,  19,  4, 66, 48,  7, 59, 36,  7, 58},
864 	{1170, 46,  19,  4, 66, 46,  7, 59, 35,  7, 58},
865 	{1160, 46,  18,  4, 65, 46,  7, 59, 34,  7, 57},
866 	{1150, 45,  18,  4, 65, 46,  7, 58, 34,  7, 57},
867 	{1140, 45,  18,  4, 65, 45,  7, 58, 34,  6, 56},
868 	{1130, 45,  18,  4, 64, 45,  7, 58, 33,  6, 56},
869 	{1120, 44,  18,  4, 64, 44,  7, 57, 33,  6, 55},
870 	{1110, 44,  18,  4, 64, 44,  7, 57, 33,  6, 55},
871 	{1100, 43,  17,  4, 63, 44,  6, 57, 32,  6, 54},
872 	{1090, 43,  17,  4, 63, 44,  6, 56, 33,  6, 54},
873 	{1080, 43,  17,  4, 63, 44,  6, 56, 33,  6, 53},
874 	{1070, 42,  17,  4, 62, 44,  6, 56, 33,  6, 53},
875 	{1060, 42,  17,  4, 62, 42,  6, 55, 31,  6, 52},
876 	{1050, 41,  17,  4, 62, 42,  6, 55, 31,  6, 52},
877 	{1040, 41,  16,  4, 61, 41,  6, 54, 31,  6, 52},
878 	{1030, 41,  16,  4, 61, 41,  6, 54, 30,  6, 51},
879 	{1020, 40,  16,  4, 61, 41,  6, 54, 30,  6, 51},
880 	{1010, 40,  16,  4, 60, 40,  6, 53, 30,  6, 50},
881 	{1000, 39,  16,  3, 60, 40,  6, 53, 29,  5, 50},
882 	{ 990, 39,  15,  3, 60, 39,  6, 53, 29,  5, 49},
883 	{ 980, 39,  15,  3, 59, 39,  5, 52, 29,  5, 49},
884 	{ 970, 38,  15,  3, 59, 39,  5, 52, 29,  5, 48},
885 	{ 960, 38,  15,  3, 59, 39,  5, 52, 29,  5, 48},
886 	{ 950, 37,  15,  3, 58, 39,  5, 51, 29,  5, 47},
887 	{ 940, 37,  14,  3, 58, 39,  5, 51, 29,  5, 47},
888 	{ 930, 37,  14,  3, 57, 37,  5, 51, 27,  5, 46},
889 	{ 920, 36,  14,  3, 57, 37,  5, 50, 27,  5, 46},
890 	{ 910, 36,  14,  3, 57, 36,  5, 50, 27,  5, 45},
891 	{ 900, 35,  14,  3, 56, 36,  5, 50, 26,  5, 45},
892 	{ 890, 35,  14,  3, 56, 36,  5, 49, 26,  5, 44},
893 	{ 880, 35,  13,  3, 56, 35,  5, 49, 26,  5, 44},
894 	{ 870, 34,  13,  3, 55, 35,  4, 49, 26,  5, 43},
895 	{ 860, 34,  13,  3, 55, 35,  4, 48, 25,  5, 43},
896 	{ 850, 33,  13,  3, 55, 35,  4, 48, 26,  4, 42},
897 	{ 840, 33,  13,  3, 54, 35,  4, 48, 26,  4, 42},
898 	{ 830, 33,  12,  3, 54, 33,  4, 47, 24,  4, 41},
899 	{ 820, 32,  12,  3, 54, 33,  4, 47, 24,  4, 41},
900 	{ 810, 32,  12,  3, 53, 33,  4, 47, 24,  4, 40},
901 	{ 800, 31,  12,  2, 53, 32,  4, 46, 23,  4, 40},
902 	{ 790, 31,  12,  2, 53, 32,  4, 46, 23,  4, 39},
903 	{ 780, 30,  12,  2, 52, 31,  4, 46, 23,  4, 39},
904 	{ 770, 30,  11,  2, 52, 31,  4, 45, 23,  4, 39},
905 	{ 760, 30,  11,  2, 52, 31,  3, 45, 22,  4, 38},
906 	{ 750, 29,  11,  2, 51, 30,  3, 45, 22,  4, 38},
907 	{ 740, 29,  11,  2, 51, 30,  3, 44, 22,  4, 37},
908 	{ 730, 28,  11,  2, 51, 31,  3, 44, 22,  4, 37},
909 	{ 720, 28,  10,  2, 50, 30,  3, 44, 22,  4, 36},
910 	{ 710, 28,  10,  2, 50, 30,  3, 43, 22,  4, 36},
911 	{ 700, 27,  10,  2, 50, 28,  3, 43, 20,  3, 35},
912 	{ 690, 27,  10,  2, 49, 28,  3, 43, 20,  3, 35},
913 	{ 680, 26,  10,  2, 49, 28,  3, 42, 20,  3, 34},
914 	{ 670, 26,  10,  2, 49, 27,  3, 42, 20,  3, 34},
915 	{ 660, 26,   9,  2, 48, 27,  3, 42, 19,  3, 33},
916 	{ 650, 25,   9,  2, 48, 26,  3, 41, 19,  3, 33},
917 	{ 640, 25,   9,  2, 48, 26,  2, 41, 19,  3, 32},
918 	{ 630, 24,   9,  2, 47, 26,  2, 40, 18,  3, 32},
919 	{ 620, 24,   9,  2, 47, 26,  2, 40, 19,  3, 31},
920 	{ 610, 24,   8,  2, 47, 26,  2, 40, 19,  3, 31},
921 	{ 600, 23,   8,  1, 46, 26,  2, 39, 18,  3, 30},
922 	{ 590, 23,   8,  1, 46, 24,  2, 39, 17,  3, 30},
923 	{ 580, 22,   8,  1, 46, 24,  2, 39, 17,  3, 29},
924 	{ 570, 22,   8,  1, 45, 23,  2, 38, 17,  3, 29},
925 	{ 560, 22,   7,  1, 45, 23,  2, 38, 16,  2, 28},
926 	{ 550, 21,   7,  1, 45, 23,  2, 38, 16,  2, 28},
927 	{ 540, 21,   7,  1, 44, 22,  2, 37, 16,  2, 27},
928 	{ 530, 20,   7,  1, 44, 22,  1, 37, 15,  2, 27},
929 	{ 520, 20,   7,  1, 43, 21,  1, 37, 15,  2, 27},
930 	{ 510, 20,   6,  1, 43, 21,  1, 36, 15,  2, 26},
931 	{ 500, 19,   6,  1, 43, 22,  1, 36, 15,  2, 26},
932 	{ 490, 19,   6,  1, 42, 21,  1, 36, 15,  2, 25},
933 	{ 480, 18,   6,  1, 42, 21,  1, 35, 15,  2, 25},
934 	{ 470, 18,   6,  1, 42, 21,  1, 35, 15,  2, 24},
935 	{ 460, 18,   6,  1, 41, 19,  1, 35, 13,  2, 24},
936 	{ 450, 17,   5,  1, 41, 19,  1, 34, 13,  2, 23},
937 	{ 440, 17,   5,  1, 41, 18,  1, 34, 13,  2, 23},
938 	{ 430, 16,   5,  1, 40, 18,  0, 34, 12,  2, 22},
939 	{ 420, 16,   5,  1, 40, 18,  0, 33, 12,  2, 22},
940 	{ 410, 16,   5,  1, 40, 17,  0, 33, 12,  1, 21},
941 	{ 400, 15,   5,  0, 39, 17,  0, 33, 11,  1, 21},
942 	{ 390, 15,   4,  0, 39, 17,  0, 32, 12,  1, 20},
943 	{ 380, 14,   4,  0, 39, 17,  0, 32, 12,  1, 20},
944 	{ 370, 14,   4,  0, 38, 17,  0, 32, 12,  1, 19},
945 	{ 360, 14,   4,  0, 38, 15,  0, 31, 10,  1, 19},
946 	{ 350, 13,   4,  0, 38, 15,  0, 31, 10,  1, 18},
947 	{ 340, 13,   3,  0, 37, 15,  0, 31, 10,  1, 18},
948 	{ 330, 12,   3,  0, 37, 14,  0, 30,  9,  1, 17},
949 	{ 320, 12,   3,  0, 37, 14,  0, 30,  9,  1, 17},
950 	{ 310, 12,   3,  0, 36, 13,  0, 30,  9,  1, 16},
951 	{ 300, 11,   3,  0, 36, 13,  0, 29,  8,  1, 16},
952 	{ 290, 11,   2,  0, 36, 13,  0, 29,  8,  1, 15},
953 	{ 280, 10,   2,  0, 35, 12,  0, 29,  8,  1, 15},
954 	{ 270, 10,   2,  0, 35, 12,  0, 28,  8,  0, 14},
955 	{ 260,  9,   2,  0, 35, 12,  0, 28,  8,  0, 14},
956 	{ 250,  9,   2,  0, 34, 12,  0, 28,  8,  0, 14},
957 	{ 240,  9,   2,  0, 34, 12,  0, 27,  8,  0, 13},
958 	{ 230,  8,   1,  0, 34, 10,  0, 27,  6,  0, 13},
959 	{ 220,  8,   1,  0, 33, 10,  0, 27,  6,  0, 12},
960 	{ 210,  7,   1,  0, 33, 10,  0, 26,  6,  0, 12},
961 	{ 200,  7,   1,  0, 33,  9,  0, 26,  5,  0, 11},
962 	{ 190,  7,   1,  0, 32,  9,  0, 25,  5,  0, 11},
963 	{ 180,  6,   1,  0, 32,  8,  0, 25,  5,  0, 10},
964 	{ 170,  6,   0,  0, 32,  8,  0, 25,  5,  0, 10},
965 	{ 160,  5,   0,  0, 31,  8,  0, 24,  4,  0,  9},
966 	{ 150,  5,   0,  0, 31,  8,  0, 24,  5,  0,  9},
967 	{ 140,  5,   0,  0, 31,  8,  0, 24,  5,  0,  8},
968 	{ 130,  4,   0,  0, 30,  6,  0, 23,  3,  0,  8},
969 	{ 120,  4,   0,  0, 30,  6,  0, 23,  3,  0,  7},
970 	{ 110,  3,   0,  0, 30,  6,  0, 23,  3,  0,  7},
971 	{ 100,  3,   0,  0, 29,  5,  0, 22,  2,  0,  6},
972 	{  90,  3,   0,  0, 29,  5,  0, 22,  2,  0,  6},
973 	{  80,  2,   0,  0, 28,  5,  0, 22,  2,  0,  5},
974 };
975 
samsung_mipi_dcphy_bias_block_enable(struct samsung_mipi_dcphy * samsung)976 static void samsung_mipi_dcphy_bias_block_enable(struct samsung_mipi_dcphy *samsung)
977 {
978 	regmap_write(samsung->regmap, BIAS_CON0, I_DEV_DIV_6 | I_RES_100_2UA);
979 	regmap_write(samsung->regmap, BIAS_CON1, I_VBG_SEL_820MV | I_BGR_VREF_820MV |
980 						 I_LADDER_1_00V);
981 	regmap_write(samsung->regmap, BIAS_CON2, REG_325M_325MV | REG_LP_400M_400MV |
982 						 REG_400M_400MV | REG_645M_645MV);
983 
984 	/* default output voltage select:
985 	 * dphy: 400mv
986 	 * cphy: 530mv
987 	 */
988 	regmap_update_bits(samsung->regmap, BIAS_CON4,
989 			   I_MUX_SEL_MASK, I_MUX_400MV);
990 }
991 
samsung_mipi_dphy_lane_enable(struct samsung_mipi_dcphy * samsung)992 static void samsung_mipi_dphy_lane_enable(struct samsung_mipi_dcphy *samsung)
993 {
994 	regmap_write(samsung->regmap, DPHY_MC_GNR_CON1, T_PHY_READY(0x2000));
995 	regmap_update_bits(samsung->regmap, DPHY_MC_GNR_CON0,
996 			   PHY_ENABLE, PHY_ENABLE);
997 
998 	switch (samsung->lanes) {
999 	case 4:
1000 		regmap_write(samsung->regmap, DPHY_MD3_GNR_CON1,
1001 			     T_PHY_READY(0x2000));
1002 		regmap_update_bits(samsung->regmap, DPHY_MD3_GNR_CON0,
1003 				   PHY_ENABLE, PHY_ENABLE);
1004 		fallthrough;
1005 	case 3:
1006 		regmap_write(samsung->regmap, COMBO_MD2_GNR_CON1,
1007 			     T_PHY_READY(0x2000));
1008 		regmap_update_bits(samsung->regmap, COMBO_MD2_GNR_CON0,
1009 				   PHY_ENABLE, PHY_ENABLE);
1010 		fallthrough;
1011 	case 2:
1012 		regmap_write(samsung->regmap, COMBO_MD1_GNR_CON1,
1013 			     T_PHY_READY(0x2000));
1014 		regmap_update_bits(samsung->regmap, COMBO_MD1_GNR_CON0,
1015 				   PHY_ENABLE, PHY_ENABLE);
1016 		fallthrough;
1017 	case 1:
1018 	default:
1019 		regmap_write(samsung->regmap, COMBO_MD0_GNR_CON1,
1020 			     T_PHY_READY(0x2000));
1021 		regmap_update_bits(samsung->regmap, COMBO_MD0_GNR_CON0,
1022 				   PHY_ENABLE, PHY_ENABLE);
1023 		break;
1024 	}
1025 }
1026 
samsung_mipi_dphy_lane_disable(struct samsung_mipi_dcphy * samsung)1027 static void samsung_mipi_dphy_lane_disable(struct samsung_mipi_dcphy *samsung)
1028 {
1029 	switch (samsung->lanes) {
1030 	case 4:
1031 		regmap_update_bits(samsung->regmap, DPHY_MD3_GNR_CON0,
1032 				   PHY_ENABLE, 0);
1033 		fallthrough;
1034 	case 3:
1035 		regmap_update_bits(samsung->regmap, COMBO_MD2_GNR_CON0,
1036 				   PHY_ENABLE, 0);
1037 		fallthrough;
1038 	case 2:
1039 		regmap_update_bits(samsung->regmap, COMBO_MD1_GNR_CON0,
1040 				   PHY_ENABLE, 0);
1041 		fallthrough;
1042 	case 1:
1043 	default:
1044 		regmap_update_bits(samsung->regmap, COMBO_MD0_GNR_CON0,
1045 				   PHY_ENABLE, 0);
1046 		break;
1047 	}
1048 
1049 	regmap_update_bits(samsung->regmap, DPHY_MC_GNR_CON0, PHY_ENABLE, 0);
1050 }
1051 
samsung_mipi_dcphy_pll_configure(struct samsung_mipi_dcphy * samsung)1052 static void samsung_mipi_dcphy_pll_configure(struct samsung_mipi_dcphy *samsung)
1053 {
1054 	regmap_update_bits(samsung->regmap, PLL_CON0, S_MASK | P_MASK,
1055 			   S(samsung->pll.scaler) | P(samsung->pll.prediv));
1056 
1057 	if (samsung->pll.dsm < 0) {
1058 		u16 dsm_tmp;
1059 
1060 		/* Using opposite number subtraction to find complement */
1061 		dsm_tmp = abs(samsung->pll.dsm);
1062 		dsm_tmp = dsm_tmp - 1;
1063 		dsm_tmp ^= 0xffff;
1064 		regmap_write(samsung->regmap, PLL_CON1, dsm_tmp);
1065 	} else {
1066 		regmap_write(samsung->regmap, PLL_CON1, samsung->pll.dsm);
1067 	}
1068 
1069 	regmap_update_bits(samsung->regmap, PLL_CON2,
1070 			   M_MASK, M(samsung->pll.fbdiv));
1071 
1072 	if (samsung->pll.ssc_en) {
1073 		regmap_write(samsung->regmap, PLL_CON3,
1074 			     MRR(samsung->pll.mrr) | MFR(samsung->pll.mfr));
1075 		regmap_update_bits(samsung->regmap, PLL_CON4, SSCG_EN, SSCG_EN);
1076 	}
1077 
1078 	regmap_write(samsung->regmap, PLL_CON5, RESET_N_SEL | PLL_ENABLE_SEL);
1079 	regmap_write(samsung->regmap, PLL_CON7, PLL_LOCK_CNT(0xf000));
1080 	regmap_write(samsung->regmap, PLL_CON8, PLL_STB_CNT(0xf000));
1081 }
1082 
samsung_mipi_dcphy_pll_enable(struct samsung_mipi_dcphy * samsung)1083 static int samsung_mipi_dcphy_pll_enable(struct samsung_mipi_dcphy *samsung)
1084 {
1085 	u32 sts;
1086 	int ret;
1087 
1088 	regmap_update_bits(samsung->regmap, PLL_CON0, PLL_EN, PLL_EN);
1089 
1090 	ret = regmap_read_poll_timeout(samsung->regmap, PLL_STAT0,
1091 				       sts, (sts & PLL_LOCK), 1000, 20000);
1092 	if (ret < 0)
1093 		dev_err(samsung->dev, "DC-PHY pll failed to lock\n");
1094 
1095 	return ret;
1096 }
1097 
samsung_mipi_dcphy_pll_disable(struct samsung_mipi_dcphy * samsung)1098 static void samsung_mipi_dcphy_pll_disable(struct samsung_mipi_dcphy *samsung)
1099 {
1100 	regmap_update_bits(samsung->regmap, PLL_CON0, PLL_EN, 0);
1101 }
1102 
1103 static const struct samsung_mipi_dphy_timing *
samsung_mipi_dphy_get_timing(struct samsung_mipi_dcphy * samsung)1104 samsung_mipi_dphy_get_timing(struct samsung_mipi_dcphy *samsung)
1105 {
1106 	const struct samsung_mipi_dphy_timing *timings;
1107 	unsigned int num_timings;
1108 	unsigned int lane_mbps = div64_ul(samsung->pll.rate, USEC_PER_SEC);
1109 	unsigned int i;
1110 
1111 	timings = samsung_mipi_dphy_timing_table;
1112 	num_timings = ARRAY_SIZE(samsung_mipi_dphy_timing_table);
1113 
1114 	for (i = num_timings; i > 1; i--)
1115 		if (lane_mbps <= timings[i - 1].max_lane_mbps)
1116 			break;
1117 
1118 	return &timings[i - 1];
1119 }
1120 
1121 static unsigned long
samsung_mipi_dcphy_pll_round_rate(struct samsung_mipi_dcphy * samsung,unsigned long prate,unsigned long rate,u8 * prediv,u16 * fbdiv,int * dsm,u8 * scaler)1122 samsung_mipi_dcphy_pll_round_rate(struct samsung_mipi_dcphy *samsung,
1123 				  unsigned long prate, unsigned long rate,
1124 				  u8 *prediv, u16 *fbdiv, int *dsm, u8 *scaler)
1125 {
1126 	u32 max_fout = samsung->pdata->dphy_tx_max_lane_kbps;
1127 	u64 best_freq = 0;
1128 	u64 fin, fvco, fout;
1129 	u8 min_prediv, max_prediv;
1130 	u8 _prediv, best_prediv = 1;
1131 	u16 _fbdiv, best_fbdiv = 1;
1132 	u8 _scaler, best_scaler = 0;
1133 	u32 min_delta = UINT_MAX;
1134 	long _dsm, best_dsm = 0;
1135 
1136 	if (!prate) {
1137 		dev_err(samsung->dev, "parent rate of PLL can not be zero\n");
1138 		return 0;
1139 	}
1140 
1141 	/*
1142 	 * The PLL output frequency can be calculated using a simple formula:
1143 	 * Fvco = ((m+k/65536) x 2 x Fin) / p
1144 	 * Fout = ((m+k/65536) x 2 x Fin) / (p x 2^s)
1145 	 */
1146 	fin = div64_ul(prate, MSEC_PER_SEC);
1147 
1148 	while (!best_freq) {
1149 		fout = div64_ul(rate, MSEC_PER_SEC);
1150 		if (fout > max_fout)
1151 			fout = max_fout;
1152 
1153 		/* 0 ≤ S[2:0] ≤ 6 */
1154 		for (_scaler = 0; _scaler < 7; _scaler++) {
1155 			fvco = fout << _scaler;
1156 
1157 			/*
1158 			 * 2600MHz ≤ FVCO ≤ 6600MHz
1159 			 */
1160 			if (fvco < 2600 * MSEC_PER_SEC || fvco > 6600 * MSEC_PER_SEC)
1161 				continue;
1162 
1163 			/* 6MHz ≤ Fref(Fin / p) ≤ 30MHz */
1164 			min_prediv = DIV_ROUND_UP_ULL(fin, 30 * MSEC_PER_SEC);
1165 			max_prediv = DIV_ROUND_CLOSEST_ULL(fin, 6 * MSEC_PER_SEC);
1166 
1167 			for (_prediv = min_prediv; _prediv <= max_prediv; _prediv++) {
1168 				u64 delta, tmp;
1169 
1170 				_fbdiv = DIV_ROUND_CLOSEST_ULL(fvco * _prediv, 2 * fin);
1171 
1172 				 /* 64 ≤ M[9:0] ≤ 1023 */
1173 				if (_fbdiv < 64 || _fbdiv > 1023)
1174 					continue;
1175 
1176 				/* -32767 ≤ K[15:0] ≤ 32767 */
1177 				_dsm = ((_prediv * fvco) - (2 * _fbdiv * fin));
1178 				_dsm = DIV_ROUND_UP_ULL(_dsm << 15, fin);
1179 				if (abs(_dsm) > 32767)
1180 					continue;
1181 
1182 				tmp = DIV_ROUND_CLOSEST_ULL((_fbdiv * fin * 2 * 1000), _prediv);
1183 				tmp += DIV_ROUND_CLOSEST_ULL((_dsm * fin * 1000), _prediv << 15);
1184 
1185 				delta = abs(fvco * MSEC_PER_SEC - tmp);
1186 				if (delta < min_delta) {
1187 					best_prediv = _prediv;
1188 					best_fbdiv = _fbdiv;
1189 					best_dsm = _dsm;
1190 					best_scaler = _scaler;
1191 					min_delta = delta;
1192 					best_freq = DIV_ROUND_CLOSEST_ULL(tmp, 1000) * MSEC_PER_SEC;
1193 				}
1194 			}
1195 		}
1196 
1197 		rate += 100 * MSEC_PER_SEC;
1198 	}
1199 
1200 	*prediv = best_prediv;
1201 	*fbdiv = best_fbdiv;
1202 	*dsm = (int)best_dsm & 0xffff;
1203 	*scaler = best_scaler;
1204 	dev_dbg(samsung->dev, "p: %d, m: %d, dsm:%ld, scaler: %d\n",
1205 		best_prediv, best_fbdiv, best_dsm, best_scaler);
1206 
1207 	return best_freq >> best_scaler;
1208 }
1209 
1210 static void
samsung_mipi_dphy_clk_lane_timing_init(struct samsung_mipi_dcphy * samsung)1211 samsung_mipi_dphy_clk_lane_timing_init(struct samsung_mipi_dcphy *samsung)
1212 {
1213 	const struct samsung_mipi_dphy_timing *timing;
1214 	unsigned int lane_hs_rate = div64_ul(samsung->pll.rate, USEC_PER_SEC);
1215 	u32 val, res_up, res_down;
1216 
1217 	timing = samsung_mipi_dphy_get_timing(samsung);
1218 	regmap_write(samsung->regmap, DPHY_MC_GNR_CON0, 0xf000);
1219 
1220 	/*
1221 	 * The Drive-Strength / Voltage-Amplitude is adjusted by setting
1222 	 * the Driver-Up Resistor and Driver-Down Resistor.
1223 	 */
1224 	res_up = samsung->pdata->dphy_hs_drv_res_cfg->clk_hs_drv_up_ohm;
1225 	res_down = samsung->pdata->dphy_hs_drv_res_cfg->clk_hs_drv_down_ohm;
1226 	val = EDGE_CON(7) | EDGE_CON_DIR(0) | EDGE_CON_EN |
1227 	      RES_UP(res_up) | RES_DN(res_down);
1228 	regmap_write(samsung->regmap, DPHY_MC_ANA_CON0, val);
1229 
1230 	if (lane_hs_rate >= 4500)
1231 		regmap_write(samsung->regmap, DPHY_MC_ANA_CON1, 0x0001);
1232 
1233 	val = 0;
1234 	/*
1235 	 * Divide-by-2 Clock from Serial Clock. Use this when data rate is under
1236 	 * 1500Mbps, otherwise divide-by-16 Clock from Serial Clock
1237 	 */
1238 	if (lane_hs_rate < 1500)
1239 		val = HSTX_CLK_SEL;
1240 
1241 	val |= T_LPX(timing->lpx);
1242 	/*  T_LP_EXIT_SKEW/T_LP_ENTRY_SKEW unconfig */
1243 	regmap_write(samsung->regmap, DPHY_MC_TIME_CON0, val);
1244 
1245 	val = T_CLK_ZERO(timing->clk_zero) | T_CLK_PREPARE(timing->clk_prepare);
1246 	regmap_write(samsung->regmap, DPHY_MC_TIME_CON1, val);
1247 
1248 	val = T_HS_EXIT(timing->hs_exit) | T_CLK_TRAIL(timing->clk_trail_eot);
1249 	regmap_write(samsung->regmap, DPHY_MC_TIME_CON2, val);
1250 
1251 	val = T_CLK_POST(timing->clk_post);
1252 	regmap_write(samsung->regmap, DPHY_MC_TIME_CON3, val);
1253 
1254 	/* Escape Clock is 20.00MHz */
1255 	regmap_write(samsung->regmap, DPHY_MC_TIME_CON4, 0x1f4);
1256 
1257 	/*
1258 	 * skew calibration should be off, if the operation data rate is
1259 	 * under 1.5Gbps or equal to 1.5Gbps.
1260 	 */
1261 	if (lane_hs_rate > 1500)
1262 		regmap_write(samsung->regmap, DPHY_MC_DESKEW_CON0, 0x9cb1);
1263 }
1264 
1265 static void
samsung_mipi_dphy_data_lane_timing_init(struct samsung_mipi_dcphy * samsung)1266 samsung_mipi_dphy_data_lane_timing_init(struct samsung_mipi_dcphy *samsung)
1267 {
1268 	const struct samsung_mipi_dphy_timing *timing;
1269 	unsigned int lane_hs_rate = div64_ul(samsung->pll.rate, USEC_PER_SEC);
1270 	u32 val, res_up, res_down;
1271 
1272 	timing = samsung_mipi_dphy_get_timing(samsung);
1273 
1274 	/*
1275 	 * The Drive-Strength / Voltage-Amplitude is adjusted by adjusting the
1276 	 *  Driver-Up Resistor and Driver-Down Resistor.
1277 	 */
1278 	res_up = samsung->pdata->dphy_hs_drv_res_cfg->data_hs_drv_up_ohm;
1279 	res_down = samsung->pdata->dphy_hs_drv_res_cfg->data_hs_drv_down_ohm;
1280 	val = EDGE_CON(7) | EDGE_CON_DIR(0) | EDGE_CON_EN |
1281 	      RES_UP(res_up) | RES_DN(res_down);
1282 	regmap_write(samsung->regmap, COMBO_MD0_ANA_CON0, val);
1283 	regmap_write(samsung->regmap, COMBO_MD1_ANA_CON0, val);
1284 	regmap_write(samsung->regmap, COMBO_MD2_ANA_CON0, val);
1285 	regmap_write(samsung->regmap, DPHY_MD3_ANA_CON0, val);
1286 
1287 	if (lane_hs_rate >= 4500) {
1288 		regmap_write(samsung->regmap, COMBO_MD0_ANA_CON1, 0x0001);
1289 		regmap_write(samsung->regmap, COMBO_MD1_ANA_CON1, 0x0001);
1290 		regmap_write(samsung->regmap, COMBO_MD2_ANA_CON1, 0x0001);
1291 		regmap_write(samsung->regmap, DPHY_MD3_ANA_CON1, 0x0001);
1292 	}
1293 
1294 	val = 0;
1295 	/*
1296 	 * Divide-by-2 Clock from Serial Clock. Use this when data rate is under
1297 	 * 1500Mbps, otherwise divide-by-16 Clock from Serial Clock
1298 	 */
1299 	if (lane_hs_rate < 1500)
1300 		val = HSTX_CLK_SEL;
1301 
1302 	val |= T_LPX(timing->lpx);
1303 	/*  T_LP_EXIT_SKEW/T_LP_ENTRY_SKEW unconfig */
1304 	regmap_write(samsung->regmap, COMBO_MD0_TIME_CON0, val);
1305 	regmap_write(samsung->regmap, COMBO_MD1_TIME_CON0, val);
1306 	regmap_write(samsung->regmap, COMBO_MD2_TIME_CON0, val);
1307 	regmap_write(samsung->regmap, DPHY_MD3_TIME_CON0, val);
1308 
1309 	val = T_HS_ZERO(timing->hs_zero) | T_HS_PREPARE(timing->hs_prepare);
1310 	regmap_write(samsung->regmap, COMBO_MD0_TIME_CON1, val);
1311 	regmap_write(samsung->regmap, COMBO_MD1_TIME_CON1, val);
1312 	regmap_write(samsung->regmap, COMBO_MD2_TIME_CON1, val);
1313 	regmap_write(samsung->regmap, DPHY_MD3_TIME_CON1, val);
1314 
1315 	val = T_HS_EXIT(timing->hs_exit) | T_HS_TRAIL(timing->hs_trail_eot);
1316 	regmap_write(samsung->regmap, COMBO_MD0_TIME_CON2, val);
1317 	regmap_write(samsung->regmap, COMBO_MD1_TIME_CON2, val);
1318 	regmap_write(samsung->regmap, COMBO_MD2_TIME_CON2, val);
1319 	regmap_write(samsung->regmap, DPHY_MD3_TIME_CON2, val);
1320 
1321 	/* TTA-GET/TTA-GO Timing Counter register use default value */
1322 	val = T_TA_GET(0x3) | T_TA_GO(0x0);
1323 	regmap_write(samsung->regmap, COMBO_MD0_TIME_CON3, val);
1324 	regmap_write(samsung->regmap, COMBO_MD1_TIME_CON3, val);
1325 	regmap_write(samsung->regmap, COMBO_MD2_TIME_CON3, val);
1326 	regmap_write(samsung->regmap, DPHY_MD3_TIME_CON3, val);
1327 
1328 	/* Escape Clock is 20.00MHz */
1329 	regmap_write(samsung->regmap, COMBO_MD0_TIME_CON4, 0x1f4);
1330 	regmap_write(samsung->regmap, COMBO_MD1_TIME_CON4, 0x1f4);
1331 	regmap_write(samsung->regmap, COMBO_MD2_TIME_CON4, 0x1f4);
1332 	regmap_write(samsung->regmap, DPHY_MD3_TIME_CON4, 0x1f4);
1333 }
1334 
samsung_mipi_dphy_power_on(struct samsung_mipi_dcphy * samsung)1335 static int samsung_mipi_dphy_power_on(struct samsung_mipi_dcphy *samsung)
1336 {
1337 	int ret;
1338 
1339 	reset_control_assert(samsung->m_phy_rst);
1340 
1341 	samsung_mipi_dcphy_bias_block_enable(samsung);
1342 	samsung_mipi_dcphy_pll_configure(samsung);
1343 	samsung_mipi_dphy_clk_lane_timing_init(samsung);
1344 	samsung_mipi_dphy_data_lane_timing_init(samsung);
1345 	ret = samsung_mipi_dcphy_pll_enable(samsung);
1346 	if (ret < 0)
1347 		return ret;
1348 
1349 	samsung_mipi_dphy_lane_enable(samsung);
1350 
1351 	reset_control_deassert(samsung->m_phy_rst);
1352 
1353 	/* The TSKEWCAL maximum is 100 µsec
1354 	 * at initial calibration.
1355 	 */
1356 	usleep_range(100, 110);
1357 
1358 	return 0;
1359 }
1360 
samsung_mipi_dcphy_power_on(struct phy * phy)1361 static int samsung_mipi_dcphy_power_on(struct phy *phy)
1362 {
1363 	struct samsung_mipi_dcphy *samsung = phy_get_drvdata(phy);
1364 
1365 	reset_control_assert(samsung->apb_rst);
1366 	udelay(1);
1367 	reset_control_deassert(samsung->apb_rst);
1368 
1369 	switch (samsung->type) {
1370 	case PHY_TYPE_DPHY:
1371 		return samsung_mipi_dphy_power_on(samsung);
1372 	default:
1373 		/* CPHY part to be implemented later */
1374 		return -EOPNOTSUPP;
1375 	}
1376 
1377 	return 0;
1378 }
1379 
samsung_mipi_dcphy_power_off(struct phy * phy)1380 static int samsung_mipi_dcphy_power_off(struct phy *phy)
1381 {
1382 	struct samsung_mipi_dcphy *samsung = phy_get_drvdata(phy);
1383 
1384 	switch (samsung->type) {
1385 	case PHY_TYPE_DPHY:
1386 		samsung_mipi_dphy_lane_disable(samsung);
1387 		break;
1388 	default:
1389 		/* CPHY part to be implemented later */
1390 		return -EOPNOTSUPP;
1391 	}
1392 
1393 	samsung_mipi_dcphy_pll_disable(samsung);
1394 
1395 	return 0;
1396 }
1397 
1398 static int
samsung_mipi_dcphy_pll_ssc_modulation_calc(struct samsung_mipi_dcphy * samsung,u8 * mfr,u8 * mrr)1399 samsung_mipi_dcphy_pll_ssc_modulation_calc(struct samsung_mipi_dcphy *samsung,
1400 					   u8 *mfr, u8 *mrr)
1401 {
1402 	unsigned long fin = div64_ul(clk_get_rate(samsung->ref_clk), MSEC_PER_SEC);
1403 	u16 prediv = samsung->pll.prediv;
1404 	u16 fbdiv = samsung->pll.fbdiv;
1405 	u16 min_mfr, max_mfr;
1406 	u16 _mfr, best_mfr = 0;
1407 	u16 mr, _mrr, best_mrr = 0;
1408 
1409 	/* 20KHz ≤ MF ≤ 150KHz */
1410 	max_mfr = DIV_ROUND_UP(fin, (20 * prediv) << 5);
1411 	min_mfr = div64_ul(fin, ((150 * prediv) << 5));
1412 	/*0 ≤ mfr ≤ 255 */
1413 	if (max_mfr > 256)
1414 		max_mfr = 256;
1415 
1416 	for (_mfr = min_mfr; _mfr < max_mfr; _mfr++) {
1417 		/* 1 ≤ mrr ≤ 31 */
1418 		for (_mrr = 1; _mrr < 32; _mrr++) {
1419 			mr = DIV_ROUND_UP(_mfr * _mrr * 100, fbdiv << 6);
1420 			/* 0 ≤ MR ≤ 5% */
1421 			if (mr > 5)
1422 				continue;
1423 
1424 			if (_mfr * _mrr < 513) {
1425 				best_mfr = _mfr;
1426 				best_mrr = _mrr;
1427 				break;
1428 			}
1429 		}
1430 	}
1431 
1432 	if (best_mrr) {
1433 		*mfr = best_mfr & 0xff;
1434 		*mrr = best_mrr & 0x3f;
1435 	} else {
1436 		dev_err(samsung->dev, "failed to calc ssc parameter mfr and mrr\n");
1437 		return -EINVAL;
1438 	}
1439 
1440 	return 0;
1441 }
1442 
1443 static void
samsung_mipi_dcphy_pll_calc_rate(struct samsung_mipi_dcphy * samsung,unsigned long long rate)1444 samsung_mipi_dcphy_pll_calc_rate(struct samsung_mipi_dcphy *samsung,
1445 				 unsigned long long rate)
1446 {
1447 	unsigned long prate = clk_get_rate(samsung->ref_clk);
1448 	unsigned long fout;
1449 	u8 scaler = 0, mfr = 0, mrr = 0;
1450 	u16 fbdiv = 0;
1451 	u8 prediv = 1;
1452 	int dsm = 0;
1453 	int ret;
1454 
1455 	fout = samsung_mipi_dcphy_pll_round_rate(samsung, prate, rate,
1456 						 &prediv, &fbdiv, &dsm,
1457 						 &scaler);
1458 
1459 	dev_dbg(samsung->dev, "%s: fin=%lu, req_rate=%llu\n",
1460 		__func__, prate, rate);
1461 	dev_dbg(samsung->dev, "%s: fout=%lu, prediv=%u, fbdiv=%u\n",
1462 		__func__, fout, prediv, fbdiv);
1463 
1464 	samsung->pll.prediv = prediv;
1465 	samsung->pll.fbdiv = fbdiv;
1466 	samsung->pll.dsm = dsm;
1467 	samsung->pll.scaler = scaler;
1468 	samsung->pll.rate = fout;
1469 
1470 	/*
1471 	 * All DPHY 2.0 compliant Transmitters shall support SSC operating above
1472 	 * 2.5 Gbps
1473 	 */
1474 	if (fout > 2500000000LL) {
1475 		ret = samsung_mipi_dcphy_pll_ssc_modulation_calc(samsung,
1476 								 &mfr, &mrr);
1477 		if (!ret) {
1478 			samsung->pll.ssc_en = true;
1479 			samsung->pll.mfr = mfr;
1480 			samsung->pll.mrr = mrr;
1481 		}
1482 	}
1483 }
1484 
samsung_mipi_dcphy_configure(struct phy * phy,union phy_configure_opts * opts)1485 static int samsung_mipi_dcphy_configure(struct phy *phy,
1486 					union phy_configure_opts *opts)
1487 {
1488 	struct samsung_mipi_dcphy *samsung = phy_get_drvdata(phy);
1489 	unsigned long long target_rate = opts->mipi_dphy.hs_clk_rate;
1490 
1491 	samsung->lanes = opts->mipi_dphy.lanes > 4 ? 4 : opts->mipi_dphy.lanes;
1492 
1493 	samsung_mipi_dcphy_pll_calc_rate(samsung, target_rate);
1494 	opts->mipi_dphy.hs_clk_rate = samsung->pll.rate;
1495 
1496 	return 0;
1497 }
1498 
samsung_mipi_dcphy_init(struct phy * phy)1499 static int samsung_mipi_dcphy_init(struct phy *phy)
1500 {
1501 	struct samsung_mipi_dcphy *samsung = phy_get_drvdata(phy);
1502 
1503 	return pm_runtime_resume_and_get(samsung->dev);
1504 }
1505 
samsung_mipi_dcphy_exit(struct phy * phy)1506 static int samsung_mipi_dcphy_exit(struct phy *phy)
1507 {
1508 	struct samsung_mipi_dcphy *samsung = phy_get_drvdata(phy);
1509 
1510 	pm_runtime_put(samsung->dev);
1511 
1512 	return 0;
1513 }
1514 
1515 static const struct phy_ops samsung_mipi_dcphy_ops = {
1516 	.configure = samsung_mipi_dcphy_configure,
1517 	.power_on  = samsung_mipi_dcphy_power_on,
1518 	.power_off = samsung_mipi_dcphy_power_off,
1519 	.init = samsung_mipi_dcphy_init,
1520 	.exit = samsung_mipi_dcphy_exit,
1521 	.owner	   = THIS_MODULE,
1522 };
1523 
1524 static const struct regmap_config samsung_mipi_dcphy_regmap_config = {
1525 	.name = "dcphy",
1526 	.reg_bits = 32,
1527 	.val_bits = 32,
1528 	.reg_stride = 4,
1529 	.max_register = 0xfffc,
1530 };
1531 
samsung_mipi_dcphy_xlate(struct device * dev,const struct of_phandle_args * args)1532 static struct phy *samsung_mipi_dcphy_xlate(struct device *dev,
1533 					    const struct of_phandle_args *args)
1534 {
1535 	struct samsung_mipi_dcphy *samsung = dev_get_drvdata(dev);
1536 
1537 	if (args->args_count != 1) {
1538 		dev_err(dev, "invalid number of arguments\n");
1539 		return ERR_PTR(-EINVAL);
1540 	}
1541 
1542 	if (samsung->type != PHY_NONE && samsung->type != args->args[0])
1543 		dev_warn(dev, "phy type select %d overwriting type %d\n",
1544 			 args->args[0], samsung->type);
1545 
1546 	samsung->type = args->args[0];
1547 
1548 	return samsung->phy;
1549 }
1550 
samsung_mipi_dcphy_probe(struct platform_device * pdev)1551 static int samsung_mipi_dcphy_probe(struct platform_device *pdev)
1552 {
1553 	struct device *dev = &pdev->dev;
1554 	struct device_node *np = dev->of_node;
1555 	struct samsung_mipi_dcphy *samsung;
1556 	struct phy_provider *phy_provider;
1557 	struct resource *res;
1558 	void __iomem *regs;
1559 	int ret;
1560 
1561 	samsung = devm_kzalloc(dev, sizeof(*samsung), GFP_KERNEL);
1562 	if (!samsung)
1563 		return -ENOMEM;
1564 
1565 	samsung->dev = dev;
1566 	samsung->pdata = device_get_match_data(dev);
1567 	platform_set_drvdata(pdev, samsung);
1568 
1569 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1570 	regs = devm_ioremap_resource(dev, res);
1571 	if (IS_ERR(regs))
1572 		return PTR_ERR(regs);
1573 
1574 	samsung->regmap = devm_regmap_init_mmio(dev, regs,
1575 						&samsung_mipi_dcphy_regmap_config);
1576 	if (IS_ERR(samsung->regmap))
1577 		return dev_err_probe(dev, PTR_ERR(samsung->regmap), "Failed to init regmap\n");
1578 
1579 	samsung->grf_regmap = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
1580 	if (IS_ERR(samsung->grf_regmap))
1581 		return dev_err_probe(dev, PTR_ERR(samsung->grf_regmap),
1582 				     "Unable to get rockchip,grf\n");
1583 
1584 	samsung->ref_clk = devm_clk_get(dev, "ref");
1585 	if (IS_ERR(samsung->ref_clk))
1586 		return dev_err_probe(dev, PTR_ERR(samsung->ref_clk),
1587 				     "Failed to get reference clock\n");
1588 
1589 	samsung->pclk = devm_clk_get(dev, "pclk");
1590 	if (IS_ERR(samsung->pclk))
1591 		return dev_err_probe(dev, PTR_ERR(samsung->pclk), "Failed to get pclk\n");
1592 
1593 	samsung->m_phy_rst = devm_reset_control_get(dev, "m_phy");
1594 	if (IS_ERR(samsung->m_phy_rst))
1595 		return dev_err_probe(dev, PTR_ERR(samsung->m_phy_rst),
1596 				     "Failed to get system m_phy_rst control\n");
1597 
1598 	samsung->s_phy_rst = devm_reset_control_get(dev, "s_phy");
1599 	if (IS_ERR(samsung->s_phy_rst))
1600 		return dev_err_probe(dev, PTR_ERR(samsung->s_phy_rst),
1601 				     "Failed to get system s_phy_rst control\n");
1602 
1603 	samsung->apb_rst = devm_reset_control_get(dev, "apb");
1604 	if (IS_ERR(samsung->apb_rst))
1605 		return dev_err_probe(dev, PTR_ERR(samsung->apb_rst),
1606 				     "Failed to get system apb_rst control\n");
1607 
1608 	samsung->grf_apb_rst = devm_reset_control_get(dev, "grf");
1609 	if (IS_ERR(samsung->grf_apb_rst))
1610 		return dev_err_probe(dev, PTR_ERR(samsung->grf_apb_rst),
1611 				     "Failed to get system grf_apb_rst control\n");
1612 
1613 	samsung->phy = devm_phy_create(dev, NULL, &samsung_mipi_dcphy_ops);
1614 	if (IS_ERR(samsung->phy))
1615 		return dev_err_probe(dev, PTR_ERR(samsung->phy), "Failed to create MIPI DC-PHY\n");
1616 
1617 	phy_set_drvdata(samsung->phy, samsung);
1618 
1619 	ret = devm_pm_runtime_enable(dev);
1620 	if (ret)
1621 		return dev_err_probe(dev, ret, "Failed to enable runtime PM\n");
1622 
1623 	phy_provider = devm_of_phy_provider_register(dev, samsung_mipi_dcphy_xlate);
1624 	if (IS_ERR(phy_provider))
1625 		return dev_err_probe(dev, PTR_ERR(phy_provider),
1626 				     "Failed to register phy provider\n");
1627 
1628 	return 0;
1629 }
1630 
samsung_mipi_dcphy_runtime_suspend(struct device * dev)1631 static __maybe_unused int samsung_mipi_dcphy_runtime_suspend(struct device *dev)
1632 {
1633 	struct samsung_mipi_dcphy *samsung = dev_get_drvdata(dev);
1634 
1635 	clk_disable_unprepare(samsung->ref_clk);
1636 	clk_disable_unprepare(samsung->pclk);
1637 
1638 	return 0;
1639 }
1640 
samsung_mipi_dcphy_runtime_resume(struct device * dev)1641 static __maybe_unused int samsung_mipi_dcphy_runtime_resume(struct device *dev)
1642 {
1643 	struct samsung_mipi_dcphy *samsung = dev_get_drvdata(dev);
1644 	int ret;
1645 
1646 	ret = clk_prepare_enable(samsung->pclk);
1647 	if (ret) {
1648 		dev_err(samsung->dev, "Failed to enable pclk, %d\n", ret);
1649 		return ret;
1650 	}
1651 
1652 	ret = clk_prepare_enable(samsung->ref_clk);
1653 	if (ret) {
1654 		dev_err(samsung->dev, "Failed to enable reference clock, %d\n", ret);
1655 		clk_disable_unprepare(samsung->pclk);
1656 		return ret;
1657 	}
1658 
1659 	return 0;
1660 }
1661 
1662 static const struct dev_pm_ops samsung_mipi_dcphy_pm_ops = {
1663 	SET_RUNTIME_PM_OPS(samsung_mipi_dcphy_runtime_suspend,
1664 			   samsung_mipi_dcphy_runtime_resume, NULL)
1665 };
1666 
1667 static const struct hs_drv_res_cfg rk3576_dphy_hs_drv_res_cfg = {
1668 	.clk_hs_drv_up_ohm = STRENGTH_52_OHM,
1669 	.clk_hs_drv_down_ohm = STRENGTH_52_OHM,
1670 	.data_hs_drv_up_ohm = STRENGTH_39_OHM,
1671 	.data_hs_drv_down_ohm = STRENGTH_39_OHM,
1672 };
1673 
1674 static const struct hs_drv_res_cfg rk3588_dphy_hs_drv_res_cfg = {
1675 	.clk_hs_drv_up_ohm = STRENGTH_34_OHM,
1676 	.clk_hs_drv_down_ohm = STRENGTH_34_OHM,
1677 	.data_hs_drv_up_ohm = STRENGTH_43_OHM,
1678 	.data_hs_drv_down_ohm = STRENGTH_43_OHM,
1679 };
1680 
1681 static const struct samsung_mipi_dcphy_plat_data rk3576_samsung_mipi_dcphy_plat_data = {
1682 	.dphy_hs_drv_res_cfg = &rk3576_dphy_hs_drv_res_cfg,
1683 	.dphy_tx_max_lane_kbps = 2500000L,
1684 };
1685 
1686 static const struct samsung_mipi_dcphy_plat_data rk3588_samsung_mipi_dcphy_plat_data = {
1687 	.dphy_hs_drv_res_cfg = &rk3588_dphy_hs_drv_res_cfg,
1688 	.dphy_tx_max_lane_kbps = 4500000L,
1689 };
1690 
1691 static const struct of_device_id samsung_mipi_dcphy_of_match[] = {
1692 	{
1693 		.compatible = "rockchip,rk3576-mipi-dcphy",
1694 		.data = &rk3576_samsung_mipi_dcphy_plat_data,
1695 	}, {
1696 		.compatible = "rockchip,rk3588-mipi-dcphy",
1697 		.data = &rk3588_samsung_mipi_dcphy_plat_data,
1698 	},
1699 	{ /* sentinel */ }
1700 };
1701 MODULE_DEVICE_TABLE(of, samsung_mipi_dcphy_of_match);
1702 
1703 static struct platform_driver samsung_mipi_dcphy_driver = {
1704 	.driver = {
1705 		.name = "samsung-mipi-dcphy",
1706 		.of_match_table	= samsung_mipi_dcphy_of_match,
1707 		.pm = &samsung_mipi_dcphy_pm_ops,
1708 	},
1709 	.probe	= samsung_mipi_dcphy_probe,
1710 };
1711 module_platform_driver(samsung_mipi_dcphy_driver);
1712 
1713 MODULE_AUTHOR("Guochun Huang <hero.huang@rock-chips.com>");
1714 MODULE_DESCRIPTION("Samsung MIPI DCPHY Driver");
1715 MODULE_LICENSE("GPL");
1716