xref: /openbmc/u-boot/test/dm/regulator.c (revision 85231c08)
1 /*
2  * Tests for the driver model regulator API
3  *
4  * Copyright (c) 2015 Samsung Electronics
5  * Przemyslaw Marczak <p.marczak@samsung.com>
6  *
7  * SPDX-License-Identifier:	GPL-2.0+
8  */
9 
10 #include <common.h>
11 #include <errno.h>
12 #include <dm.h>
13 #include <fdtdec.h>
14 #include <malloc.h>
15 #include <dm/device-internal.h>
16 #include <dm/root.h>
17 #include <dm/util.h>
18 #include <dm/test.h>
19 #include <dm/uclass-internal.h>
20 #include <power/pmic.h>
21 #include <power/regulator.h>
22 #include <power/sandbox_pmic.h>
23 #include <test/ut.h>
24 
25 DECLARE_GLOBAL_DATA_PTR;
26 
27 enum {
28 	BUCK1,
29 	BUCK2,
30 	BUCK3,
31 	LDO1,
32 	LDO2,
33 	OUTPUT_COUNT,
34 };
35 
36 enum {
37 	DEVNAME = 0,
38 	PLATNAME,
39 	OUTPUT_NAME_COUNT,
40 };
41 
42 static const char *regulator_names[OUTPUT_COUNT][OUTPUT_NAME_COUNT] = {
43 	/* devname, platname */
44 	{ SANDBOX_BUCK1_DEVNAME, SANDBOX_BUCK1_PLATNAME },
45 	{ SANDBOX_BUCK2_DEVNAME, SANDBOX_BUCK2_PLATNAME },
46 	{ SANDBOX_BUCK3_DEVNAME, SANDBOX_BUCK3_PLATNAME },
47 	{ SANDBOX_LDO1_DEVNAME, SANDBOX_LDO1_PLATNAME},
48 	{ SANDBOX_LDO2_DEVNAME, SANDBOX_LDO2_PLATNAME},
49 };
50 
51 /* Test regulator get method */
52 static int dm_test_power_regulator_get(struct unit_test_state *uts)
53 {
54 	struct dm_regulator_uclass_platdata *uc_pdata;
55 	struct udevice *dev_by_devname;
56 	struct udevice *dev_by_platname;
57 	const char *devname;
58 	const char *platname;
59 	int i;
60 
61 	for (i = 0; i < OUTPUT_COUNT; i++) {
62 		/*
63 		 * Do the test for each regulator's devname and platname,
64 		 * which are related to a single device.
65 		 */
66 		devname = regulator_names[i][DEVNAME];
67 		platname = regulator_names[i][PLATNAME];
68 
69 		/*
70 		 * Check, that regulator_get_by_devname() function, returns
71 		 * a device with the name equal to the requested one.
72 		 */
73 		ut_assertok(regulator_get_by_devname(devname, &dev_by_devname));
74 		ut_asserteq_str(devname, dev_by_devname->name);
75 
76 		/*
77 		 * Check, that regulator_get_by_platname() function, returns
78 		 * a device with the name equal to the requested one.
79 		 */
80 		ut_assertok(regulator_get_by_platname(platname, &dev_by_platname));
81 		uc_pdata = dev_get_uclass_platdata(dev_by_platname);
82 		ut_assert(uc_pdata);
83 		ut_asserteq_str(platname, uc_pdata->name);
84 
85 		/*
86 		 * Check, that the pointers returned by both get functions,
87 		 * points to the same regulator device.
88 		 */
89 		ut_asserteq_ptr(dev_by_devname, dev_by_platname);
90 	}
91 
92 	return 0;
93 }
94 DM_TEST(dm_test_power_regulator_get, DM_TESTF_SCAN_FDT);
95 
96 /* Test regulator set and get Voltage method */
97 static int dm_test_power_regulator_set_get_voltage(struct unit_test_state *uts)
98 {
99 	struct dm_regulator_uclass_platdata *uc_pdata;
100 	struct udevice *dev;
101 	const char *platname;
102 	int val_set, val_get;
103 
104 	/* Set and get Voltage of BUCK1 - set to 'min' constraint */
105 	platname = regulator_names[BUCK1][PLATNAME];
106 	ut_assertok(regulator_get_by_platname(platname, &dev));
107 
108 	uc_pdata = dev_get_uclass_platdata(dev);
109 	ut_assert(uc_pdata);
110 
111 	val_set = uc_pdata->min_uV;
112 	ut_assertok(regulator_set_value(dev, val_set));
113 
114 	val_get = regulator_get_value(dev);
115 	ut_assert(val_get >= 0);
116 
117 	ut_asserteq(val_set, val_get);
118 
119 	return 0;
120 }
121 DM_TEST(dm_test_power_regulator_set_get_voltage, DM_TESTF_SCAN_FDT);
122 
123 /* Test regulator set and get Current method */
124 static int dm_test_power_regulator_set_get_current(struct unit_test_state *uts)
125 {
126 	struct dm_regulator_uclass_platdata *uc_pdata;
127 	struct udevice *dev;
128 	const char *platname;
129 	int val_set, val_get;
130 
131 	/* Set and get the Current of LDO1 - set to 'min' constraint */
132 	platname = regulator_names[LDO1][PLATNAME];
133 	ut_assertok(regulator_get_by_platname(platname, &dev));
134 
135 	uc_pdata = dev_get_uclass_platdata(dev);
136 	ut_assert(uc_pdata);
137 
138 	val_set = uc_pdata->min_uA;
139 	ut_assertok(regulator_set_current(dev, val_set));
140 
141 	val_get = regulator_get_current(dev);
142 	ut_assert(val_get >= 0);
143 
144 	ut_asserteq(val_set, val_get);
145 
146 	/* Check LDO2 current limit constraints - should be -ENODATA */
147 	platname = regulator_names[LDO2][PLATNAME];
148 	ut_assertok(regulator_get_by_platname(platname, &dev));
149 
150 	uc_pdata = dev_get_uclass_platdata(dev);
151 	ut_assert(uc_pdata);
152 	ut_asserteq(-ENODATA, uc_pdata->min_uA);
153 	ut_asserteq(-ENODATA, uc_pdata->max_uA);
154 
155 	/* Try set the Current of LDO2 - should return -ENOSYS */
156 	ut_asserteq(-ENOSYS, regulator_set_current(dev, 0));
157 
158 	return 0;
159 }
160 DM_TEST(dm_test_power_regulator_set_get_current, DM_TESTF_SCAN_FDT);
161 
162 /* Test regulator set and get Enable method */
163 static int dm_test_power_regulator_set_get_enable(struct unit_test_state *uts)
164 {
165 	const char *platname;
166 	struct udevice *dev;
167 	bool val_set = true;
168 
169 	/* Set the Enable of LDO1 - default is disabled */
170 	platname = regulator_names[LDO1][PLATNAME];
171 	ut_assertok(regulator_get_by_platname(platname, &dev));
172 	ut_assertok(regulator_set_enable(dev, val_set));
173 
174 	/* Get the Enable state of LDO1 and compare it with the requested one */
175 	ut_asserteq(regulator_get_enable(dev), val_set);
176 
177 	return 0;
178 }
179 DM_TEST(dm_test_power_regulator_set_get_enable, DM_TESTF_SCAN_FDT);
180 
181 /* Test regulator set and get mode method */
182 static int dm_test_power_regulator_set_get_mode(struct unit_test_state *uts)
183 {
184 	const char *platname;
185 	struct udevice *dev;
186 	int val_set = LDO_OM_SLEEP;
187 
188 	/* Set the mode id to LDO_OM_SLEEP of LDO1 - default is LDO_OM_OFF */
189 	platname = regulator_names[LDO1][PLATNAME];
190 	ut_assertok(regulator_get_by_platname(platname, &dev));
191 	ut_assertok(regulator_set_mode(dev, val_set));
192 
193 	/* Get the mode id of LDO1 and compare it with the requested one */
194 	ut_asserteq(regulator_get_mode(dev), val_set);
195 
196 	return 0;
197 }
198 DM_TEST(dm_test_power_regulator_set_get_mode, DM_TESTF_SCAN_FDT);
199 
200 /* Test regulator autoset method */
201 static int dm_test_power_regulator_autoset(struct unit_test_state *uts)
202 {
203 	const char *platname;
204 	struct udevice *dev, *dev_autoset;
205 
206 	/*
207 	 * Test the BUCK1 with fdt properties
208 	 * - min-microvolt = max-microvolt = 1200000
209 	 * - min-microamp = max-microamp = 200000
210 	 * - always-on = set
211 	 * - boot-on = not set
212 	 * Expected output state: uV=1200000; uA=200000; output enabled
213 	 */
214 	platname = regulator_names[BUCK1][PLATNAME];
215 	ut_assertok(regulator_autoset_by_name(platname, &dev_autoset));
216 
217 	/* Check, that the returned device is proper */
218 	ut_assertok(regulator_get_by_platname(platname, &dev));
219 	ut_asserteq_ptr(dev, dev_autoset);
220 
221 	/* Check the setup after autoset */
222 	ut_asserteq(regulator_get_value(dev),
223 		    SANDBOX_BUCK1_AUTOSET_EXPECTED_UV);
224 	ut_asserteq(regulator_get_current(dev),
225 		    SANDBOX_BUCK1_AUTOSET_EXPECTED_UA);
226 	ut_asserteq(regulator_get_enable(dev),
227 		    SANDBOX_BUCK1_AUTOSET_EXPECTED_ENABLE);
228 
229 	return 0;
230 }
231 DM_TEST(dm_test_power_regulator_autoset, DM_TESTF_SCAN_FDT);
232 
233 /*
234  * Struct setting: to keep the expected output settings.
235  * @voltage: Voltage value [uV]
236  * @current: Current value [uA]
237  * @enable: output enable state: true/false
238  */
239 struct setting {
240 	int voltage;
241 	int current;
242 	bool enable;
243 };
244 
245 /*
246  * platname_list: an array of regulator platform names.
247  * For testing regulator_list_autoset() for outputs:
248  * - LDO1
249  * - LDO2
250  */
251 static const char *platname_list[] = {
252 	SANDBOX_LDO1_PLATNAME,
253 	SANDBOX_LDO2_PLATNAME,
254 	NULL,
255 };
256 
257 /*
258  * expected_setting_list: an array of regulator output setting, expected after
259  * call of the regulator_list_autoset() for the "platname_list" array.
260  * For testing results of regulator_list_autoset() for outputs:
261  * - LDO1
262  * - LDO2
263  * The settings are defined in: include/power/sandbox_pmic.h
264  */
265 static const struct setting expected_setting_list[] = {
266 	[0] = { /* LDO1 */
267 	.voltage = SANDBOX_LDO1_AUTOSET_EXPECTED_UV,
268 	.current = SANDBOX_LDO1_AUTOSET_EXPECTED_UA,
269 	.enable  = SANDBOX_LDO1_AUTOSET_EXPECTED_ENABLE,
270 	},
271 	[1] = { /* LDO2 */
272 	.voltage = SANDBOX_LDO2_AUTOSET_EXPECTED_UV,
273 	.current = SANDBOX_LDO2_AUTOSET_EXPECTED_UA,
274 	.enable  = SANDBOX_LDO2_AUTOSET_EXPECTED_ENABLE,
275 	},
276 };
277 
278 static int list_count = ARRAY_SIZE(expected_setting_list);
279 
280 /* Test regulator list autoset method */
281 static int dm_test_power_regulator_autoset_list(struct unit_test_state *uts)
282 {
283 	struct udevice *dev_list[2], *dev;
284 	int i;
285 
286 	/*
287 	 * Test the settings of the regulator list:
288 	 * LDO1 with fdt properties:
289 	 * - min-microvolt = max-microvolt = 1800000
290 	 * - min-microamp = max-microamp = 100000
291 	 * - always-on = not set
292 	 * - boot-on = set
293 	 * Expected output state: uV=1800000; uA=100000; output enabled
294 	 *
295 	 * LDO2 with fdt properties:
296 	 * - min-microvolt = max-microvolt = 3300000
297 	 * - always-on = not set
298 	 * - boot-on = not set
299 	 * Expected output state: uV=300000(default); output disabled(default)
300 	 * The expected settings are defined in: include/power/sandbox_pmic.h.
301 	 */
302 	ut_assertok(regulator_list_autoset(platname_list, dev_list, false));
303 
304 	for (i = 0; i < list_count; i++) {
305 		/* Check, that the returned device is non-NULL */
306 		ut_assert(dev_list[i]);
307 
308 		/* Check, that the returned device is proper */
309 		ut_assertok(regulator_get_by_platname(platname_list[i], &dev));
310 		ut_asserteq_ptr(dev_list[i], dev);
311 
312 		/* Check, that regulator output Voltage value is as expected */
313 		ut_asserteq(regulator_get_value(dev_list[i]),
314 			    expected_setting_list[i].voltage);
315 
316 		/* Check, that regulator output Current value is as expected */
317 		ut_asserteq(regulator_get_current(dev_list[i]),
318 			    expected_setting_list[i].current);
319 
320 		/* Check, that regulator output Enable state is as expected */
321 		ut_asserteq(regulator_get_enable(dev_list[i]),
322 			    expected_setting_list[i].enable);
323 	}
324 
325 	return 0;
326 }
327 DM_TEST(dm_test_power_regulator_autoset_list, DM_TESTF_SCAN_FDT);
328