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