2 * Tests for the driver model regulator API
4 * Copyright (c) 2015 Samsung Electronics
5 * Przemyslaw Marczak <p.marczak@samsung.com>
7 * SPDX-License-Identifier: GPL-2.0+
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>
25 DECLARE_GLOBAL_DATA_PTR;
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},
51 /* Test regulator get method */
52 static int dm_test_power_regulator_get(struct unit_test_state *uts)
54 struct dm_regulator_uclass_platdata *uc_pdata;
55 struct udevice *dev_by_devname;
56 struct udevice *dev_by_platname;
61 for (i = 0; i < OUTPUT_COUNT; i++) {
63 * Do the test for each regulator's devname and platname,
64 * which are related to a single device.
66 devname = regulator_names[i][DEVNAME];
67 platname = regulator_names[i][PLATNAME];
70 * Check, that regulator_get_by_devname() function, returns
71 * a device with the name equal to the requested one.
73 ut_assertok(regulator_get_by_devname(devname, &dev_by_devname));
74 ut_asserteq_str(devname, dev_by_devname->name);
77 * Check, that regulator_get_by_platname() function, returns
78 * a device with the name equal to the requested one.
80 ut_assertok(regulator_get_by_platname(platname, &dev_by_platname));
81 uc_pdata = dev_get_uclass_platdata(dev_by_platname);
83 ut_asserteq_str(platname, uc_pdata->name);
86 * Check, that the pointers returned by both get functions,
87 * points to the same regulator device.
89 ut_asserteq_ptr(dev_by_devname, dev_by_platname);
94 DM_TEST(dm_test_power_regulator_get, DM_TESTF_SCAN_FDT);
96 /* Test regulator set and get Voltage method */
97 static int dm_test_power_regulator_set_get_voltage(struct unit_test_state *uts)
99 struct dm_regulator_uclass_platdata *uc_pdata;
101 const char *platname;
102 int val_set, val_get;
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));
108 uc_pdata = dev_get_uclass_platdata(dev);
111 val_set = uc_pdata->min_uV;
112 ut_assertok(regulator_set_value(dev, val_set));
114 val_get = regulator_get_value(dev);
115 ut_assert(val_get >= 0);
117 ut_asserteq(val_set, val_get);
121 DM_TEST(dm_test_power_regulator_set_get_voltage, DM_TESTF_SCAN_FDT);
123 /* Test regulator set and get Current method */
124 static int dm_test_power_regulator_set_get_current(struct unit_test_state *uts)
126 struct dm_regulator_uclass_platdata *uc_pdata;
128 const char *platname;
129 int val_set, val_get;
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));
135 uc_pdata = dev_get_uclass_platdata(dev);
138 val_set = uc_pdata->min_uA;
139 ut_assertok(regulator_set_current(dev, val_set));
141 val_get = regulator_get_current(dev);
142 ut_assert(val_get >= 0);
144 ut_asserteq(val_set, val_get);
146 /* Check LDO2 current limit constraints - should be -ENODATA */
147 platname = regulator_names[LDO2][PLATNAME];
148 ut_assertok(regulator_get_by_platname(platname, &dev));
150 uc_pdata = dev_get_uclass_platdata(dev);
152 ut_asserteq(-ENODATA, uc_pdata->min_uA);
153 ut_asserteq(-ENODATA, uc_pdata->max_uA);
155 /* Try set the Current of LDO2 - should return -ENOSYS */
156 ut_asserteq(-ENOSYS, regulator_set_current(dev, 0));
160 DM_TEST(dm_test_power_regulator_set_get_current, DM_TESTF_SCAN_FDT);
162 /* Test regulator set and get Enable method */
163 static int dm_test_power_regulator_set_get_enable(struct unit_test_state *uts)
165 const char *platname;
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));
174 /* Get the Enable state of LDO1 and compare it with the requested one */
175 ut_asserteq(regulator_get_enable(dev), val_set);
179 DM_TEST(dm_test_power_regulator_set_get_enable, DM_TESTF_SCAN_FDT);
181 /* Test regulator set and get mode method */
182 static int dm_test_power_regulator_set_get_mode(struct unit_test_state *uts)
184 const char *platname;
186 int val_set = LDO_OM_SLEEP;
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));
193 /* Get the mode id of LDO1 and compare it with the requested one */
194 ut_asserteq(regulator_get_mode(dev), val_set);
198 DM_TEST(dm_test_power_regulator_set_get_mode, DM_TESTF_SCAN_FDT);
200 /* Test regulator autoset method */
201 static int dm_test_power_regulator_autoset(struct unit_test_state *uts)
203 const char *platname;
204 struct udevice *dev, *dev_autoset;
207 * Test the BUCK1 with fdt properties
208 * - min-microvolt = max-microvolt = 1200000
209 * - min-microamp = max-microamp = 200000
211 * - boot-on = not set
212 * Expected output state: uV=1200000; uA=200000; output enabled
214 platname = regulator_names[BUCK1][PLATNAME];
215 ut_assertok(regulator_autoset_by_name(platname, &dev_autoset));
217 /* Check, that the returned device is proper */
218 ut_assertok(regulator_get_by_platname(platname, &dev));
219 ut_asserteq_ptr(dev, dev_autoset);
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);
231 DM_TEST(dm_test_power_regulator_autoset, DM_TESTF_SCAN_FDT);
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
246 * platname_list: an array of regulator platform names.
247 * For testing regulator_list_autoset() for outputs:
251 static const char *platname_list[] = {
252 SANDBOX_LDO1_PLATNAME,
253 SANDBOX_LDO2_PLATNAME,
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:
263 * The settings are defined in: include/power/sandbox_pmic.h
265 static const struct setting expected_setting_list[] = {
267 .voltage = SANDBOX_LDO1_AUTOSET_EXPECTED_UV,
268 .current = SANDBOX_LDO1_AUTOSET_EXPECTED_UA,
269 .enable = SANDBOX_LDO1_AUTOSET_EXPECTED_ENABLE,
272 .voltage = SANDBOX_LDO2_AUTOSET_EXPECTED_UV,
273 .current = SANDBOX_LDO2_AUTOSET_EXPECTED_UA,
274 .enable = SANDBOX_LDO2_AUTOSET_EXPECTED_ENABLE,
278 static int list_count = ARRAY_SIZE(expected_setting_list);
280 /* Test regulator list autoset method */
281 static int dm_test_power_regulator_autoset_list(struct unit_test_state *uts)
283 struct udevice *dev_list[2], *dev;
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
293 * Expected output state: uV=1800000; uA=100000; output enabled
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.
302 ut_assertok(regulator_list_autoset(platname_list, dev_list, false));
304 for (i = 0; i < list_count; i++) {
305 /* Check, that the returned device is non-NULL */
306 ut_assert(dev_list[i]);
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);
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);
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);
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);
327 DM_TEST(dm_test_power_regulator_autoset_list, DM_TESTF_SCAN_FDT);