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