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