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