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