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