1 // SPDX-License-Identifier: GPL-2.0+
3 * Tests for the driver model regulator API
5 * Copyright (c) 2015 Samsung Electronics
6 * Przemyslaw Marczak <p.marczak@samsung.com>
14 #include <dm/device-internal.h>
18 #include <dm/uclass-internal.h>
19 #include <power/pmic.h>
20 #include <power/regulator.h>
21 #include <power/sandbox_pmic.h>
22 #include <test/test.h>
40 static const char *regulator_names[OUTPUT_COUNT][OUTPUT_NAME_COUNT] = {
41 /* devname, platname */
42 { SANDBOX_BUCK1_DEVNAME, SANDBOX_BUCK1_PLATNAME },
43 { SANDBOX_BUCK2_DEVNAME, SANDBOX_BUCK2_PLATNAME },
44 { SANDBOX_BUCK3_DEVNAME, SANDBOX_BUCK3_PLATNAME },
45 { SANDBOX_LDO1_DEVNAME, SANDBOX_LDO1_PLATNAME},
46 { SANDBOX_LDO2_DEVNAME, SANDBOX_LDO2_PLATNAME},
49 /* Test regulator get method */
50 static int dm_test_power_regulator_get(struct unit_test_state *uts)
52 struct dm_regulator_uclass_plat *uc_pdata;
53 struct udevice *dev_by_devname;
54 struct udevice *dev_by_platname;
59 for (i = 0; i < OUTPUT_COUNT; i++) {
61 * Do the test for each regulator's devname and platname,
62 * which are related to a single device.
64 devname = regulator_names[i][DEVNAME];
65 platname = regulator_names[i][PLATNAME];
68 * Check, that regulator_get_by_devname() function, returns
69 * a device with the name equal to the requested one.
71 ut_assertok(regulator_get_by_devname(devname, &dev_by_devname));
72 ut_asserteq_str(devname, dev_by_devname->name);
75 * Check, that regulator_get_by_platname() function, returns
76 * a device with the name equal to the requested one.
78 ut_assertok(regulator_get_by_platname(platname, &dev_by_platname));
79 uc_pdata = dev_get_uclass_plat(dev_by_platname);
81 ut_asserteq_str(platname, uc_pdata->name);
84 * Check, that the pointers returned by both get functions,
85 * points to the same regulator device.
87 ut_asserteq_ptr(dev_by_devname, dev_by_platname);
92 DM_TEST(dm_test_power_regulator_get, UT_TESTF_SCAN_FDT);
94 /* Test regulator set and get Voltage method */
95 static int dm_test_power_regulator_set_get_voltage(struct unit_test_state *uts)
97 struct dm_regulator_uclass_plat *uc_pdata;
100 int val_set, val_get;
102 /* Set and get Voltage of BUCK1 - set to 'min' constraint */
103 platname = regulator_names[BUCK1][PLATNAME];
104 ut_assertok(regulator_get_by_platname(platname, &dev));
106 uc_pdata = dev_get_uclass_plat(dev);
109 val_set = uc_pdata->min_uV;
110 ut_assertok(regulator_set_value(dev, val_set));
112 val_get = regulator_get_value(dev);
113 ut_assert(val_get >= 0);
115 ut_asserteq(val_set, val_get);
119 DM_TEST(dm_test_power_regulator_set_get_voltage, UT_TESTF_SCAN_FDT);
121 /* Test regulator set and get Current method */
122 static int dm_test_power_regulator_set_get_current(struct unit_test_state *uts)
124 struct dm_regulator_uclass_plat *uc_pdata;
126 const char *platname;
127 int val_set, val_get;
129 /* Set and get the Current of LDO1 - set to 'min' constraint */
130 platname = regulator_names[LDO1][PLATNAME];
131 ut_assertok(regulator_get_by_platname(platname, &dev));
133 uc_pdata = dev_get_uclass_plat(dev);
136 val_set = uc_pdata->min_uA;
137 ut_assertok(regulator_set_current(dev, val_set));
139 val_get = regulator_get_current(dev);
140 ut_assert(val_get >= 0);
142 ut_asserteq(val_set, val_get);
144 /* Check LDO2 current limit constraints - should be -ENODATA */
145 platname = regulator_names[LDO2][PLATNAME];
146 ut_assertok(regulator_get_by_platname(platname, &dev));
148 uc_pdata = dev_get_uclass_plat(dev);
150 ut_asserteq(-ENODATA, uc_pdata->min_uA);
151 ut_asserteq(-ENODATA, uc_pdata->max_uA);
153 /* Try set the Current of LDO2 - should return -ENOSYS */
154 ut_asserteq(-ENOSYS, regulator_set_current(dev, 0));
158 DM_TEST(dm_test_power_regulator_set_get_current, UT_TESTF_SCAN_FDT);
160 /* Test regulator set and get Enable method */
161 static int dm_test_power_regulator_set_get_enable(struct unit_test_state *uts)
163 const char *platname;
167 /* Set the Enable of LDO1 - default is disabled */
168 platname = regulator_names[LDO1][PLATNAME];
169 ut_assertok(regulator_get_by_platname(platname, &dev));
170 ut_assertok(regulator_set_enable(dev, val_set));
172 /* Get the Enable state of LDO1 and compare it with the requested one */
173 ut_asserteq(regulator_get_enable(dev), val_set);
177 DM_TEST(dm_test_power_regulator_set_get_enable, UT_TESTF_SCAN_FDT);
179 /* Test regulator set and get enable if allowed method */
181 int dm_test_power_regulator_set_enable_if_allowed(struct unit_test_state *uts)
183 const char *platname;
184 struct udevice *dev, *dev_autoset;
185 bool val_set = false;
187 /* Get BUCK1 - always on regulator */
188 platname = regulator_names[BUCK1][PLATNAME];
189 ut_assertok(regulator_autoset_by_name(platname, &dev_autoset));
190 ut_assertok(regulator_get_by_platname(platname, &dev));
192 /* Try disabling always-on regulator */
193 ut_assertok(regulator_set_enable_if_allowed(dev, val_set));
194 ut_asserteq(regulator_get_enable(dev), !val_set);
198 DM_TEST(dm_test_power_regulator_set_enable_if_allowed, UT_TESTF_SCAN_FDT);
200 /* Test regulator set and get mode method */
201 static int dm_test_power_regulator_set_get_mode(struct unit_test_state *uts)
203 const char *platname;
205 int val_set = LDO_OM_SLEEP;
207 /* Set the mode id to LDO_OM_SLEEP of LDO1 - default is LDO_OM_OFF */
208 platname = regulator_names[LDO1][PLATNAME];
209 ut_assertok(regulator_get_by_platname(platname, &dev));
210 ut_assertok(regulator_set_mode(dev, val_set));
212 /* Get the mode id of LDO1 and compare it with the requested one */
213 ut_asserteq(regulator_get_mode(dev), val_set);
217 DM_TEST(dm_test_power_regulator_set_get_mode, UT_TESTF_SCAN_FDT);
219 /* Test regulator set and get suspend Voltage method */
220 static int dm_test_power_regulator_set_get_suspend_voltage(struct unit_test_state *uts)
222 struct dm_regulator_uclass_plat *uc_pdata;
223 const struct dm_regulator_ops *ops;
225 const char *platname;
226 int val_set, val_get;
228 /* Set and get Voltage of BUCK1 - set to 'min' constraint */
229 platname = regulator_names[BUCK1][PLATNAME];
230 ut_assertok(regulator_get_by_platname(platname, &dev));
232 uc_pdata = dev_get_uclass_plat(dev);
235 ops = dev_get_driver_ops(dev);
237 if (ops->set_suspend_value && ops->get_suspend_value) {
238 val_set = uc_pdata->suspend_uV;
239 ut_assertok(regulator_set_suspend_value(dev, val_set));
240 val_get = regulator_get_suspend_value(dev);
241 ut_assert(val_get >= 0);
243 ut_asserteq(val_set, val_get);
247 DM_TEST(dm_test_power_regulator_set_get_suspend_voltage, UT_TESTF_SCAN_FDT);
249 /* Test regulator set and get suspend Enable method */
250 static int dm_test_power_regulator_set_get_suspend_enable(struct unit_test_state *uts)
252 const struct dm_regulator_ops *ops;
253 const char *platname;
257 /* Set the Enable of LDO1 - default is disabled */
258 platname = regulator_names[LDO1][PLATNAME];
259 ut_assertok(regulator_get_by_platname(platname, &dev));
261 ops = dev_get_driver_ops(dev);
263 if (ops->set_suspend_enable && ops->get_suspend_enable) {
264 ut_assertok(regulator_set_suspend_enable(dev, val_set));
267 * Get the Enable state of LDO1 and
268 * compare it with the requested one
270 ut_asserteq(regulator_get_suspend_enable(dev), val_set);
274 DM_TEST(dm_test_power_regulator_set_get_suspend_enable, UT_TESTF_SCAN_FDT);
276 /* Test regulator autoset method */
277 static int dm_test_power_regulator_autoset(struct unit_test_state *uts)
279 const char *platname;
280 struct udevice *dev, *dev_autoset;
283 * Test the BUCK1 with fdt properties
284 * - min-microvolt = max-microvolt = 1200000
285 * - min-microamp = max-microamp = 200000
287 * - boot-on = not set
288 * Expected output state: uV=1200000; uA=200000; output enabled
290 platname = regulator_names[BUCK1][PLATNAME];
291 ut_assertok(regulator_autoset_by_name(platname, &dev_autoset));
293 /* Check, that the returned device is proper */
294 ut_assertok(regulator_get_by_platname(platname, &dev));
295 ut_asserteq_ptr(dev, dev_autoset);
297 /* Check the setup after autoset */
298 ut_asserteq(regulator_get_value(dev),
299 SANDBOX_BUCK1_AUTOSET_EXPECTED_UV);
300 ut_asserteq(regulator_get_current(dev),
301 SANDBOX_BUCK1_AUTOSET_EXPECTED_UA);
302 ut_asserteq(regulator_get_enable(dev),
303 SANDBOX_BUCK1_AUTOSET_EXPECTED_ENABLE);
307 DM_TEST(dm_test_power_regulator_autoset, UT_TESTF_SCAN_FDT);
310 * Struct setting: to keep the expected output settings.
311 * @voltage: Voltage value [uV]
312 * @current: Current value [uA]
313 * @enable: output enable state: true/false
322 * platname_list: an array of regulator platform names.
323 * For testing regulator_list_autoset() for outputs:
327 static const char *platname_list[] = {
328 SANDBOX_LDO1_PLATNAME,
329 SANDBOX_LDO2_PLATNAME,
334 * expected_setting_list: an array of regulator output setting, expected after
335 * call of the regulator_list_autoset() for the "platname_list" array.
336 * For testing results of regulator_list_autoset() for outputs:
339 * The settings are defined in: include/power/sandbox_pmic.h
341 static const struct setting expected_setting_list[] = {
343 .voltage = SANDBOX_LDO1_AUTOSET_EXPECTED_UV,
344 .current = SANDBOX_LDO1_AUTOSET_EXPECTED_UA,
345 .enable = SANDBOX_LDO1_AUTOSET_EXPECTED_ENABLE,
348 .voltage = SANDBOX_LDO2_AUTOSET_EXPECTED_UV,
349 .current = SANDBOX_LDO2_AUTOSET_EXPECTED_UA,
350 .enable = SANDBOX_LDO2_AUTOSET_EXPECTED_ENABLE,
354 static int list_count = ARRAY_SIZE(expected_setting_list);
356 /* Test regulator list autoset method */
357 static int dm_test_power_regulator_autoset_list(struct unit_test_state *uts)
359 struct udevice *dev_list[2], *dev;
363 * Test the settings of the regulator list:
364 * LDO1 with fdt properties:
365 * - min-microvolt = max-microvolt = 1800000
366 * - min-microamp = max-microamp = 100000
367 * - always-on = not set
369 * Expected output state: uV=1800000; uA=100000; output enabled
371 * LDO2 with fdt properties:
372 * - min-microvolt = max-microvolt = 3300000
373 * - always-on = not set
374 * - boot-on = not set
375 * Expected output state: uV=300000(default); output disabled(default)
376 * The expected settings are defined in: include/power/sandbox_pmic.h.
378 ut_assertok(regulator_list_autoset(platname_list, dev_list, false));
380 for (i = 0; i < list_count; i++) {
381 /* Check, that the returned device is non-NULL */
382 ut_assert(dev_list[i]);
384 /* Check, that the returned device is proper */
385 ut_assertok(regulator_get_by_platname(platname_list[i], &dev));
386 ut_asserteq_ptr(dev_list[i], dev);
388 /* Check, that regulator output Voltage value is as expected */
389 ut_asserteq(regulator_get_value(dev_list[i]),
390 expected_setting_list[i].voltage);
392 /* Check, that regulator output Current value is as expected */
393 ut_asserteq(regulator_get_current(dev_list[i]),
394 expected_setting_list[i].current);
396 /* Check, that regulator output Enable state is as expected */
397 ut_asserteq(regulator_get_enable(dev_list[i]),
398 expected_setting_list[i].enable);
403 DM_TEST(dm_test_power_regulator_autoset_list, UT_TESTF_SCAN_FDT);