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 
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 static int odroid_get_board_type(void)
61 {
62 	unsigned int adcval;
63 	int ret, i;
64 
65 	ret = adc_channel_single_shot("adc", CONFIG_ODROID_REV_AIN, &adcval);
66 	if (ret)
67 		goto rev_default;
68 
69 	for (i = 0; i < ARRAY_SIZE(odroid_info); i++) {
70 		/* ADC tolerance: +1% */
71 		if (adcval < odroid_info[i].adc_val)
72 			return odroid_info[i].board_type;
73 	}
74 
75 rev_default:
76 	return EXYNOS5_BOARD_ODROID_XU3;
77 }
78 
79 /**
80  * odroid_get_type_str - returns pointer to one of the board type string.
81  * Board types: "xu3", "xu3-lite", "xu4". However the "xu3lite" can be
82  * detected only when the i2c controller is ready to use. Fortunately,
83  * XU3 and XU3L are compatible, and the information about board lite
84  * revision is needed before booting the linux, to set proper environment
85  * variable: $fdtfile.
86  */
87 static const char *odroid_get_type_str(void)
88 {
89 	const char *type_xu3l = "xu3-lite";
90 	struct udevice *dev, *chip;
91 	int i, ret;
92 
93 	if (gd->board_type != EXYNOS5_BOARD_ODROID_XU3_REV02)
94 		goto exit;
95 
96 	ret = pmic_get("s2mps11", &dev);
97 	if (ret)
98 		goto exit;
99 
100 	/* Enable LDO26: 3.0V */
101 	ret = pmic_reg_write(dev, S2MPS11_REG_L26CTRL,
102 			     S2MPS11_LDO26_ENABLE);
103 	if (ret)
104 		goto exit;
105 
106 	/* Check XU3Lite by probe INA231 I2C0:0x40 */
107 	ret = uclass_get_device(UCLASS_I2C, 0, &dev);
108 	if (ret)
109 		goto exit;
110 
111 	ret = dm_i2c_probe(dev, 0x40, 0x0, &chip);
112 	if (ret)
113 		return type_xu3l;
114 
115 exit:
116 	for (i = 0; i < ARRAY_SIZE(odroid_info); i++) {
117 		if (odroid_info[i].board_type == gd->board_type)
118 			return odroid_info[i].name;
119 	}
120 
121 	return NULL;
122 }
123 
124 bool board_is_odroidxu3(void)
125 {
126 	if (gd->board_type >= EXYNOS5_BOARD_ODROID_XU3 &&
127 	    gd->board_type <= EXYNOS5_BOARD_ODROID_XU3_REV02)
128 		return true;
129 
130 	return false;
131 }
132 
133 bool board_is_odroidxu4(void)
134 {
135 	if (gd->board_type == EXYNOS5_BOARD_ODROID_XU4_REV01)
136 		return true;
137 
138 	return false;
139 }
140 
141 bool board_is_odroidhc1(void)
142 {
143 	if (gd->board_type == EXYNOS5_BOARD_ODROID_HC1_REV01)
144 		return true;
145 
146 	return false;
147 }
148 
149 bool board_is_odroidhc2(void)
150 {
151 	if (gd->board_type == EXYNOS5_BOARD_ODROID_HC2_REV01)
152 		return true;
153 
154 	return false;
155 }
156 
157 bool board_is_generic(void)
158 {
159 	if (gd->board_type == EXYNOS5_BOARD_GENERIC)
160 		return true;
161 
162 	return false;
163 }
164 
165 /**
166  * get_board_rev() - return detected board revision.
167  *
168  * @return:  return board revision number for XU3 or 0 for generic
169  */
170 u32 get_board_rev(void)
171 {
172 	if (board_is_generic())
173 		return 0;
174 
175 	return odroid_get_rev();
176 }
177 
178 /**
179  * get_board_type() - returns board type string.
180  *
181  * @return:  return board type string for XU3 or empty string for generic
182  */
183 const char *get_board_type(void)
184 {
185 	const char *generic = "";
186 
187 	if (board_is_generic())
188 		return generic;
189 
190 	return odroid_get_type_str();
191 }
192 
193 /**
194  * set_board_type() - set board type in gd->board_type.
195  * As default type set EXYNOS5_BOARD_GENERIC, if detect Odroid,
196  * then set its proper type.
197  */
198 void set_board_type(void)
199 {
200 	const struct udevice_id *of_match = board_ids;
201 	int ret;
202 
203 	gd->board_type = EXYNOS5_BOARD_GENERIC;
204 
205 	while (of_match->compatible) {
206 		ret = fdt_node_check_compatible(gd->fdt_blob, 0,
207 						of_match->compatible);
208 		if (ret)
209 			of_match++;
210 
211 		gd->board_type = of_match->data;
212 		break;
213 	}
214 
215 	/* If Odroid, then check its revision */
216 	if (board_is_odroidxu3())
217 		gd->board_type = odroid_get_board_type();
218 }
219