1 // SPDX-License-Identifier: GPL-2.0+
3 * Tests for the driver model regulator API
5 * Copyright (c) 2015 Samsung Electronics
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>
39 static const char *regulator_names[OUTPUT_COUNT][OUTPUT_NAME_COUNT] = {
40 /* devname, platname */
41 { SANDBOX_BUCK1_DEVNAME, SANDBOX_BUCK1_PLATNAME },
42 { SANDBOX_BUCK2_DEVNAME, SANDBOX_BUCK2_PLATNAME },
43 { SANDBOX_BUCK3_DEVNAME, SANDBOX_BUCK3_PLATNAME },
44 { SANDBOX_LDO1_DEVNAME, SANDBOX_LDO1_PLATNAME},
45 { SANDBOX_LDO2_DEVNAME, SANDBOX_LDO2_PLATNAME},
48 /* Test regulator get method */
49 static int dm_test_power_regulator_get(struct unit_test_state *uts)
51 struct dm_regulator_uclass_platdata *uc_pdata;
52 struct udevice *dev_by_devname;
53 struct udevice *dev_by_platname;
58 for (i = 0; i < OUTPUT_COUNT; i++) {
60 * Do the test for each regulator's devname and platname,
61 * which are related to a single device.
63 devname = regulator_names[i][DEVNAME];
64 platname = regulator_names[i][PLATNAME];
67 * Check, that regulator_get_by_devname() function, returns
68 * a device with the name equal to the requested one.
70 ut_assertok(regulator_get_by_devname(devname, &dev_by_devname));
71 ut_asserteq_str(devname, dev_by_devname->name);
74 * Check, that regulator_get_by_platname() function, returns
75 * a device with the name equal to the requested one.
77 ut_assertok(regulator_get_by_platname(platname, &dev_by_platname));
78 uc_pdata = dev_get_uclass_platdata(dev_by_platname);
80 ut_asserteq_str(platname, uc_pdata->name);
83 * Check, that the pointers returned by both get functions,
84 * points to the same regulator device.
86 ut_asserteq_ptr(dev_by_devname, dev_by_platname);
91 DM_TEST(dm_test_power_regulator_get, DM_TESTF_SCAN_FDT);
93 /* Test regulator set and get Voltage method */
94 static int dm_test_power_regulator_set_get_voltage(struct unit_test_state *uts)
96 struct dm_regulator_uclass_platdata *uc_pdata;
101 /* Set and get Voltage of BUCK1 - set to 'min' constraint */
102 platname = regulator_names[BUCK1][PLATNAME];
103 ut_assertok(regulator_get_by_platname(platname, &dev));
105 uc_pdata = dev_get_uclass_platdata(dev);
108 val_set = uc_pdata->min_uV;
109 ut_assertok(regulator_set_value(dev, val_set));
111 val_get = regulator_get_value(dev);
112 ut_assert(val_get >= 0);
114 ut_asserteq(val_set, val_get);
118 DM_TEST(dm_test_power_regulator_set_get_voltage, DM_TESTF_SCAN_FDT);
120 /* Test regulator set and get Current method */
121 static int dm_test_power_regulator_set_get_current(struct unit_test_state *uts)
123 struct dm_regulator_uclass_platdata *uc_pdata;
125 const char *platname;
126 int val_set, val_get;
128 /* Set and get the Current of LDO1 - set to 'min' constraint */
129 platname = regulator_names[LDO1][PLATNAME];
130 ut_assertok(regulator_get_by_platname(platname, &dev));
132 uc_pdata = dev_get_uclass_platdata(dev);
135 val_set = uc_pdata->min_uA;
136 ut_assertok(regulator_set_current(dev, val_set));
138 val_get = regulator_get_current(dev);
139 ut_assert(val_get >= 0);
141 ut_asserteq(val_set, val_get);
143 /* Check LDO2 current limit constraints - should be -ENODATA */
144 platname = regulator_names[LDO2][PLATNAME];
145 ut_assertok(regulator_get_by_platname(platname, &dev));
147 uc_pdata = dev_get_uclass_platdata(dev);
149 ut_asserteq(-ENODATA, uc_pdata->min_uA);
150 ut_asserteq(-ENODATA, uc_pdata->max_uA);
152 /* Try set the Current of LDO2 - should return -ENOSYS */
153 ut_asserteq(-ENOSYS, regulator_set_current(dev, 0));
157 DM_TEST(dm_test_power_regulator_set_get_current, DM_TESTF_SCAN_FDT);
159 /* Test regulator set and get Enable method */
160 static int dm_test_power_regulator_set_get_enable(struct unit_test_state *uts)
162 const char *platname;
166 /* Set the Enable of LDO1 - default is disabled */
167 platname = regulator_names[LDO1][PLATNAME];
168 ut_assertok(regulator_get_by_platname(platname, &dev));
169 ut_assertok(regulator_set_enable(dev, val_set));
171 /* Get the Enable state of LDO1 and compare it with the requested one */
172 ut_asserteq(regulator_get_enable(dev), val_set);
176 DM_TEST(dm_test_power_regulator_set_get_enable, DM_TESTF_SCAN_FDT);
178 /* Test regulator set and get enable if allowed method */
180 int dm_test_power_regulator_set_enable_if_allowed(struct unit_test_state *uts)
182 const char *platname;
183 struct udevice *dev, *dev_autoset;
184 bool val_set = false;
186 /* Get BUCK1 - always on regulator */
187 platname = regulator_names[BUCK1][PLATNAME];
188 ut_assertok(regulator_autoset_by_name(platname, &dev_autoset));
189 ut_assertok(regulator_get_by_platname(platname, &dev));
191 /* Try disabling always-on regulator */
192 ut_assertok(regulator_set_enable_if_allowed(dev, val_set));
193 ut_asserteq(regulator_get_enable(dev), !val_set);
197 DM_TEST(dm_test_power_regulator_set_enable_if_allowed, DM_TESTF_SCAN_FDT);
199 /* Test regulator set and get mode method */
200 static int dm_test_power_regulator_set_get_mode(struct unit_test_state *uts)
202 const char *platname;
204 int val_set = LDO_OM_SLEEP;
206 /* Set the mode id to LDO_OM_SLEEP of LDO1 - default is LDO_OM_OFF */
207 platname = regulator_names[LDO1][PLATNAME];
208 ut_assertok(regulator_get_by_platname(platname, &dev));
209 ut_assertok(regulator_set_mode(dev, val_set));
211 /* Get the mode id of LDO1 and compare it with the requested one */
212 ut_asserteq(regulator_get_mode(dev), val_set);
216 DM_TEST(dm_test_power_regulator_set_get_mode, DM_TESTF_SCAN_FDT);
218 /* Test regulator autoset method */
219 static int dm_test_power_regulator_autoset(struct unit_test_state *uts)
221 const char *platname;
222 struct udevice *dev, *dev_autoset;
225 * Test the BUCK1 with fdt properties
226 * - min-microvolt = max-microvolt = 1200000
227 * - min-microamp = max-microamp = 200000
229 * - boot-on = not set
230 * Expected output state: uV=1200000; uA=200000; output enabled
232 platname = regulator_names[BUCK1][PLATNAME];
233 ut_assertok(regulator_autoset_by_name(platname, &dev_autoset));
235 /* Check, that the returned device is proper */
236 ut_assertok(regulator_get_by_platname(platname, &dev));
237 ut_asserteq_ptr(dev, dev_autoset);
239 /* Check the setup after autoset */
240 ut_asserteq(regulator_get_value(dev),
241 SANDBOX_BUCK1_AUTOSET_EXPECTED_UV);
242 ut_asserteq(regulator_get_current(dev),
243 SANDBOX_BUCK1_AUTOSET_EXPECTED_UA);
244 ut_asserteq(regulator_get_enable(dev),
245 SANDBOX_BUCK1_AUTOSET_EXPECTED_ENABLE);
249 DM_TEST(dm_test_power_regulator_autoset, DM_TESTF_SCAN_FDT);
252 * Struct setting: to keep the expected output settings.
253 * @voltage: Voltage value [uV]
254 * @current: Current value [uA]
255 * @enable: output enable state: true/false
264 * platname_list: an array of regulator platform names.
265 * For testing regulator_list_autoset() for outputs:
269 static const char *platname_list[] = {
270 SANDBOX_LDO1_PLATNAME,
271 SANDBOX_LDO2_PLATNAME,
276 * expected_setting_list: an array of regulator output setting, expected after
277 * call of the regulator_list_autoset() for the "platname_list" array.
278 * For testing results of regulator_list_autoset() for outputs:
281 * The settings are defined in: include/power/sandbox_pmic.h
283 static const struct setting expected_setting_list[] = {
285 .voltage = SANDBOX_LDO1_AUTOSET_EXPECTED_UV,
286 .current = SANDBOX_LDO1_AUTOSET_EXPECTED_UA,
287 .enable = SANDBOX_LDO1_AUTOSET_EXPECTED_ENABLE,
290 .voltage = SANDBOX_LDO2_AUTOSET_EXPECTED_UV,
291 .current = SANDBOX_LDO2_AUTOSET_EXPECTED_UA,
292 .enable = SANDBOX_LDO2_AUTOSET_EXPECTED_ENABLE,
296 static int list_count = ARRAY_SIZE(expected_setting_list);
298 /* Test regulator list autoset method */
299 static int dm_test_power_regulator_autoset_list(struct unit_test_state *uts)
301 struct udevice *dev_list[2], *dev;
305 * Test the settings of the regulator list:
306 * LDO1 with fdt properties:
307 * - min-microvolt = max-microvolt = 1800000
308 * - min-microamp = max-microamp = 100000
309 * - always-on = not set
311 * Expected output state: uV=1800000; uA=100000; output enabled
313 * LDO2 with fdt properties:
314 * - min-microvolt = max-microvolt = 3300000
315 * - always-on = not set
316 * - boot-on = not set
317 * Expected output state: uV=300000(default); output disabled(default)
318 * The expected settings are defined in: include/power/sandbox_pmic.h.
320 ut_assertok(regulator_list_autoset(platname_list, dev_list, false));
322 for (i = 0; i < list_count; i++) {
323 /* Check, that the returned device is non-NULL */
324 ut_assert(dev_list[i]);
326 /* Check, that the returned device is proper */
327 ut_assertok(regulator_get_by_platname(platname_list[i], &dev));
328 ut_asserteq_ptr(dev_list[i], dev);
330 /* Check, that regulator output Voltage value is as expected */
331 ut_asserteq(regulator_get_value(dev_list[i]),
332 expected_setting_list[i].voltage);
334 /* Check, that regulator output Current value is as expected */
335 ut_asserteq(regulator_get_current(dev_list[i]),
336 expected_setting_list[i].current);
338 /* Check, that regulator output Enable state is as expected */
339 ut_asserteq(regulator_get_enable(dev_list[i]),
340 expected_setting_list[i].enable);
345 DM_TEST(dm_test_power_regulator_autoset_list, DM_TESTF_SCAN_FDT);