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