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>
15 #include <dm/device-internal.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>
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},
50 /* Test regulator get method */
51 static int dm_test_power_regulator_get(struct unit_test_state *uts)
53 struct dm_regulator_uclass_platdata *uc_pdata;
54 struct udevice *dev_by_devname;
55 struct udevice *dev_by_platname;
60 for (i = 0; i < OUTPUT_COUNT; i++) {
62 * Do the test for each regulator's devname and platname,
63 * which are related to a single device.
65 devname = regulator_names[i][DEVNAME];
66 platname = regulator_names[i][PLATNAME];
69 * Check, that regulator_get_by_devname() function, returns
70 * a device with the name equal to the requested one.
72 ut_assertok(regulator_get_by_devname(devname, &dev_by_devname));
73 ut_asserteq_str(devname, dev_by_devname->name);
76 * Check, that regulator_get_by_platname() function, returns
77 * a device with the name equal to the requested one.
79 ut_assertok(regulator_get_by_platname(platname, &dev_by_platname));
80 uc_pdata = dev_get_uclass_platdata(dev_by_platname);
82 ut_asserteq_str(platname, uc_pdata->name);
85 * Check, that the pointers returned by both get functions,
86 * points to the same regulator device.
88 ut_asserteq_ptr(dev_by_devname, dev_by_platname);
93 DM_TEST(dm_test_power_regulator_get, UT_TESTF_SCAN_FDT);
95 /* Test regulator set and get Voltage method */
96 static int dm_test_power_regulator_set_get_voltage(struct unit_test_state *uts)
98 struct dm_regulator_uclass_platdata *uc_pdata;
100 const char *platname;
101 int val_set, val_get;
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));
107 uc_pdata = dev_get_uclass_platdata(dev);
110 val_set = uc_pdata->min_uV;
111 ut_assertok(regulator_set_value(dev, val_set));
113 val_get = regulator_get_value(dev);
114 ut_assert(val_get >= 0);
116 ut_asserteq(val_set, val_get);
120 DM_TEST(dm_test_power_regulator_set_get_voltage, UT_TESTF_SCAN_FDT);
122 /* Test regulator set and get Current method */
123 static int dm_test_power_regulator_set_get_current(struct unit_test_state *uts)
125 struct dm_regulator_uclass_platdata *uc_pdata;
127 const char *platname;
128 int val_set, val_get;
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));
134 uc_pdata = dev_get_uclass_platdata(dev);
137 val_set = uc_pdata->min_uA;
138 ut_assertok(regulator_set_current(dev, val_set));
140 val_get = regulator_get_current(dev);
141 ut_assert(val_get >= 0);
143 ut_asserteq(val_set, val_get);
145 /* Check LDO2 current limit constraints - should be -ENODATA */
146 platname = regulator_names[LDO2][PLATNAME];
147 ut_assertok(regulator_get_by_platname(platname, &dev));
149 uc_pdata = dev_get_uclass_platdata(dev);
151 ut_asserteq(-ENODATA, uc_pdata->min_uA);
152 ut_asserteq(-ENODATA, uc_pdata->max_uA);
154 /* Try set the Current of LDO2 - should return -ENOSYS */
155 ut_asserteq(-ENOSYS, regulator_set_current(dev, 0));
159 DM_TEST(dm_test_power_regulator_set_get_current, UT_TESTF_SCAN_FDT);
161 /* Test regulator set and get Enable method */
162 static int dm_test_power_regulator_set_get_enable(struct unit_test_state *uts)
164 const char *platname;
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));
173 /* Get the Enable state of LDO1 and compare it with the requested one */
174 ut_asserteq(regulator_get_enable(dev), val_set);
178 DM_TEST(dm_test_power_regulator_set_get_enable, UT_TESTF_SCAN_FDT);
180 /* Test regulator set and get enable if allowed method */
182 int dm_test_power_regulator_set_enable_if_allowed(struct unit_test_state *uts)
184 const char *platname;
185 struct udevice *dev, *dev_autoset;
186 bool val_set = false;
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));
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);
199 DM_TEST(dm_test_power_regulator_set_enable_if_allowed, UT_TESTF_SCAN_FDT);
201 /* Test regulator set and get mode method */
202 static int dm_test_power_regulator_set_get_mode(struct unit_test_state *uts)
204 const char *platname;
206 int val_set = LDO_OM_SLEEP;
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));
213 /* Get the mode id of LDO1 and compare it with the requested one */
214 ut_asserteq(regulator_get_mode(dev), val_set);
218 DM_TEST(dm_test_power_regulator_set_get_mode, UT_TESTF_SCAN_FDT);
220 /* Test regulator set and get suspend Voltage method */
221 static int dm_test_power_regulator_set_get_suspend_voltage(struct unit_test_state *uts)
223 struct dm_regulator_uclass_platdata *uc_pdata;
224 const struct dm_regulator_ops *ops;
226 const char *platname;
227 int val_set, val_get;
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));
233 uc_pdata = dev_get_uclass_platdata(dev);
236 ops = dev_get_driver_ops(dev);
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);
244 ut_asserteq(val_set, val_get);
248 DM_TEST(dm_test_power_regulator_set_get_suspend_voltage, UT_TESTF_SCAN_FDT);
250 /* Test regulator set and get suspend Enable method */
251 static int dm_test_power_regulator_set_get_suspend_enable(struct unit_test_state *uts)
253 const struct dm_regulator_ops *ops;
254 const char *platname;
258 /* Set the Enable of LDO1 - default is disabled */
259 platname = regulator_names[LDO1][PLATNAME];
260 ut_assertok(regulator_get_by_platname(platname, &dev));
262 ops = dev_get_driver_ops(dev);
264 if (ops->set_suspend_enable && ops->get_suspend_enable) {
265 ut_assertok(regulator_set_suspend_enable(dev, val_set));
268 * Get the Enable state of LDO1 and
269 * compare it with the requested one
271 ut_asserteq(regulator_get_suspend_enable(dev), val_set);
275 DM_TEST(dm_test_power_regulator_set_get_suspend_enable, UT_TESTF_SCAN_FDT);
277 /* Test regulator autoset method */
278 static int dm_test_power_regulator_autoset(struct unit_test_state *uts)
280 const char *platname;
281 struct udevice *dev, *dev_autoset;
284 * Test the BUCK1 with fdt properties
285 * - min-microvolt = max-microvolt = 1200000
286 * - min-microamp = max-microamp = 200000
288 * - boot-on = not set
289 * Expected output state: uV=1200000; uA=200000; output enabled
291 platname = regulator_names[BUCK1][PLATNAME];
292 ut_assertok(regulator_autoset_by_name(platname, &dev_autoset));
294 /* Check, that the returned device is proper */
295 ut_assertok(regulator_get_by_platname(platname, &dev));
296 ut_asserteq_ptr(dev, dev_autoset);
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);
308 DM_TEST(dm_test_power_regulator_autoset, UT_TESTF_SCAN_FDT);
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
323 * platname_list: an array of regulator platform names.
324 * For testing regulator_list_autoset() for outputs:
328 static const char *platname_list[] = {
329 SANDBOX_LDO1_PLATNAME,
330 SANDBOX_LDO2_PLATNAME,
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:
340 * The settings are defined in: include/power/sandbox_pmic.h
342 static const struct setting expected_setting_list[] = {
344 .voltage = SANDBOX_LDO1_AUTOSET_EXPECTED_UV,
345 .current = SANDBOX_LDO1_AUTOSET_EXPECTED_UA,
346 .enable = SANDBOX_LDO1_AUTOSET_EXPECTED_ENABLE,
349 .voltage = SANDBOX_LDO2_AUTOSET_EXPECTED_UV,
350 .current = SANDBOX_LDO2_AUTOSET_EXPECTED_UA,
351 .enable = SANDBOX_LDO2_AUTOSET_EXPECTED_ENABLE,
355 static int list_count = ARRAY_SIZE(expected_setting_list);
357 /* Test regulator list autoset method */
358 static int dm_test_power_regulator_autoset_list(struct unit_test_state *uts)
360 struct udevice *dev_list[2], *dev;
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
370 * Expected output state: uV=1800000; uA=100000; output enabled
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.
379 ut_assertok(regulator_list_autoset(platname_list, dev_list, false));
381 for (i = 0; i < list_count; i++) {
382 /* Check, that the returned device is non-NULL */
383 ut_assert(dev_list[i]);
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);
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);
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);
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);
404 DM_TEST(dm_test_power_regulator_autoset_list, UT_TESTF_SCAN_FDT);