1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2015 Samsung Electronics
4  * Przemyslaw Marczak <p.marczak@samsung.com>
5  */
6 
7 #include <common.h>
8 #include <adc.h>
9 #include <dm.h>
10 #include <errno.h>
11 #include <fdtdec.h>
12 #include <power/pmic.h>
13 #include <power/regulator.h>
14 #include <power/s2mps11.h>
15 #include <samsung/exynos5-dt-types.h>
16 #include <samsung/misc.h>
17 
18 DECLARE_GLOBAL_DATA_PTR;
19 
20 static const struct udevice_id board_ids[] = {
21 	{ .compatible = "samsung,odroidxu3", .data = EXYNOS5_BOARD_ODROID_XU3 },
22 	{ .compatible = "samsung,exynos5", .data = EXYNOS5_BOARD_GENERIC },
23 	{ },
24 };
25 
26 /**
27  * Odroix XU3/XU4/HC1/HC2 board revisions (from HC1+_HC2_MAIN_REV0.1_20171017.pdf):
28  * Rev   ADCmax  Board
29  * 0.1     0     XU3 0.1
30  * 0.2   372     XU3 0.2 | XU3L - no DISPLAYPORT (probe I2C0:0x40 / INA231)
31  * 0.3  1280     XU4 0.1
32  * 0.4   739     XU4 0.2
33  * 0.5  1016     XU4+Air0.1 (Passive cooling)
34  * 0.6  1309     XU4-HC1 0.1
35  * 0.7  1470     XU4-HC1+ 0.1 (HC2)
36  * Use +1% for ADC value tolerance in the array below, the code loops until
37  * the measured ADC value is lower than then ADCmax from the array.
38  */
39 struct odroid_rev_info odroid_info[] = {
40 	{ EXYNOS5_BOARD_ODROID_XU3_REV01, 1, 10, "xu3" },
41 	{ EXYNOS5_BOARD_ODROID_XU3_REV02, 2, 375, "xu3" },
42 	{ EXYNOS5_BOARD_ODROID_XU4_REV01, 1, 1293, "xu4" },
43 	{ EXYNOS5_BOARD_ODROID_HC1_REV01, 1, 1322, "hc1" },
44 	{ EXYNOS5_BOARD_ODROID_HC2_REV01, 1, 1484, "hc1" },
45 	{ EXYNOS5_BOARD_ODROID_UNKNOWN, 0, 4095, "unknown" },
46 };
47 
odroid_get_rev(void)48 static unsigned int odroid_get_rev(void)
49 {
50 	int i;
51 
52 	for (i = 0; i < ARRAY_SIZE(odroid_info); i++) {
53 		if (odroid_info[i].board_type == gd->board_type)
54 			return odroid_info[i].board_rev;
55 	}
56 
57 	return 0;
58 }
59 
60 /*
61  * Read ADC at least twice and check the resuls.  If regulator providing voltage
62  * on to measured point was just turned on, first reads might require time
63  * to stabilize.
64  */
odroid_get_adc_val(unsigned int * adcval)65 static int odroid_get_adc_val(unsigned int *adcval)
66 {
67 	unsigned int adcval_prev = 0;
68 	int ret, retries = 20;
69 
70 	ret = adc_channel_single_shot("adc", CONFIG_ODROID_REV_AIN,
71 				      &adcval_prev);
72 	if (ret)
73 		return ret;
74 
75 	while (retries--) {
76 		mdelay(5);
77 
78 		ret = adc_channel_single_shot("adc", CONFIG_ODROID_REV_AIN,
79 					      adcval);
80 		if (ret)
81 			return ret;
82 
83 		/*
84 		 * If difference between ADC reads is less than 3%,
85 		 * accept the result
86 		 */
87 		if ((100 * abs(*adcval - adcval_prev) / adcval_prev) < 3)
88 			return ret;
89 
90 		adcval_prev = *adcval;
91 	}
92 
93 	return ret;
94 }
95 
odroid_get_board_type(void)96 static int odroid_get_board_type(void)
97 {
98 	unsigned int adcval;
99 	int ret, i;
100 
101 	ret = odroid_get_adc_val(&adcval);
102 	if (ret)
103 		goto rev_default;
104 
105 	for (i = 0; i < ARRAY_SIZE(odroid_info); i++) {
106 		/* ADC tolerance: +1% */
107 		if (adcval < odroid_info[i].adc_val)
108 			return odroid_info[i].board_type;
109 	}
110 
111 rev_default:
112 	return EXYNOS5_BOARD_ODROID_XU3;
113 }
114 
115 /**
116  * odroid_get_type_str - returns pointer to one of the board type string.
117  * Board types: "xu3", "xu3-lite", "xu4". However the "xu3lite" can be
118  * detected only when the i2c controller is ready to use. Fortunately,
119  * XU3 and XU3L are compatible, and the information about board lite
120  * revision is needed before booting the linux, to set proper environment
121  * variable: $fdtfile.
122  */
odroid_get_type_str(void)123 static const char *odroid_get_type_str(void)
124 {
125 	const char *type_xu3l = "xu3-lite";
126 	struct udevice *dev, *chip;
127 	int i, ret;
128 
129 	if (gd->board_type != EXYNOS5_BOARD_ODROID_XU3_REV02)
130 		goto exit;
131 
132 	ret = pmic_get("s2mps11", &dev);
133 	if (ret)
134 		goto exit;
135 
136 	/* Enable LDO26: 3.0V */
137 	ret = pmic_reg_write(dev, S2MPS11_REG_L26CTRL,
138 			     S2MPS11_LDO26_ENABLE);
139 	if (ret)
140 		goto exit;
141 
142 	/* Check XU3Lite by probe INA231 I2C0:0x40 */
143 	ret = uclass_get_device(UCLASS_I2C, 0, &dev);
144 	if (ret)
145 		goto exit;
146 
147 	ret = dm_i2c_probe(dev, 0x40, 0x0, &chip);
148 	if (ret)
149 		return type_xu3l;
150 
151 exit:
152 	for (i = 0; i < ARRAY_SIZE(odroid_info); i++) {
153 		if (odroid_info[i].board_type == gd->board_type)
154 			return odroid_info[i].name;
155 	}
156 
157 	return NULL;
158 }
159 
board_is_odroidxu3(void)160 bool board_is_odroidxu3(void)
161 {
162 	if (gd->board_type >= EXYNOS5_BOARD_ODROID_XU3 &&
163 	    gd->board_type <= EXYNOS5_BOARD_ODROID_XU3_REV02)
164 		return true;
165 
166 	return false;
167 }
168 
board_is_odroidxu4(void)169 bool board_is_odroidxu4(void)
170 {
171 	if (gd->board_type == EXYNOS5_BOARD_ODROID_XU4_REV01)
172 		return true;
173 
174 	return false;
175 }
176 
board_is_odroidhc1(void)177 bool board_is_odroidhc1(void)
178 {
179 	if (gd->board_type == EXYNOS5_BOARD_ODROID_HC1_REV01)
180 		return true;
181 
182 	return false;
183 }
184 
board_is_odroidhc2(void)185 bool board_is_odroidhc2(void)
186 {
187 	if (gd->board_type == EXYNOS5_BOARD_ODROID_HC2_REV01)
188 		return true;
189 
190 	return false;
191 }
192 
board_is_generic(void)193 bool board_is_generic(void)
194 {
195 	if (gd->board_type == EXYNOS5_BOARD_GENERIC)
196 		return true;
197 
198 	return false;
199 }
200 
201 /**
202  * get_board_rev() - return detected board revision.
203  *
204  * @return:  return board revision number for XU3 or 0 for generic
205  */
get_board_rev(void)206 u32 get_board_rev(void)
207 {
208 	if (board_is_generic())
209 		return 0;
210 
211 	return odroid_get_rev();
212 }
213 
214 /**
215  * get_board_type() - returns board type string.
216  *
217  * @return:  return board type string for XU3 or empty string for generic
218  */
get_board_type(void)219 const char *get_board_type(void)
220 {
221 	const char *generic = "";
222 
223 	if (board_is_generic())
224 		return generic;
225 
226 	return odroid_get_type_str();
227 }
228 
229 /**
230  * set_board_type() - set board type in gd->board_type.
231  * As default type set EXYNOS5_BOARD_GENERIC. If Odroid is detected,
232  * set its proper type based on device tree.
233  *
234  * This might be called early when some more specific ways to detect revision
235  * are not yet available.
236  */
set_board_type(void)237 void set_board_type(void)
238 {
239 	const struct udevice_id *of_match = board_ids;
240 	int ret;
241 
242 	gd->board_type = EXYNOS5_BOARD_GENERIC;
243 
244 	while (of_match->compatible) {
245 		ret = fdt_node_check_compatible(gd->fdt_blob, 0,
246 						of_match->compatible);
247 		if (ret)
248 			of_match++;
249 
250 		gd->board_type = of_match->data;
251 		break;
252 	}
253 }
254 
255 /**
256  * set_board_revision() - set detailed board type in gd->board_type.
257  * Should be called when resources (e.g. regulators) are available
258  * so ADC can be used to detect the specific revision of a board.
259  */
set_board_revision(void)260 void set_board_revision(void)
261 {
262 	if (board_is_odroidxu3())
263 		gd->board_type = odroid_get_board_type();
264 }
265