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;
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_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_platdata *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_platdata(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, DM_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_platdata *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_platdata(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, DM_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_platdata *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_platdata(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_platdata(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, DM_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, DM_TESTF_SCAN_FDT);
179 /* Test regulator set and get mode method */
180 static int dm_test_power_regulator_set_get_mode(struct unit_test_state *uts)
182 const char *platname;
184 int val_set = LDO_OM_SLEEP;
186 /* Set the mode id to LDO_OM_SLEEP of LDO1 - default is LDO_OM_OFF */
187 platname = regulator_names[LDO1][PLATNAME];
188 ut_assertok(regulator_get_by_platname(platname, &dev));
189 ut_assertok(regulator_set_mode(dev, val_set));
191 /* Get the mode id of LDO1 and compare it with the requested one */
192 ut_asserteq(regulator_get_mode(dev), val_set);
196 DM_TEST(dm_test_power_regulator_set_get_mode, DM_TESTF_SCAN_FDT);
198 /* Test regulator autoset method */
199 static int dm_test_power_regulator_autoset(struct unit_test_state *uts)
201 const char *platname;
202 struct udevice *dev, *dev_autoset;
205 * Test the BUCK1 with fdt properties
206 * - min-microvolt = max-microvolt = 1200000
207 * - min-microamp = max-microamp = 200000
209 * - boot-on = not set
210 * Expected output state: uV=1200000; uA=200000; output enabled
212 platname = regulator_names[BUCK1][PLATNAME];
213 ut_assertok(regulator_autoset_by_name(platname, &dev_autoset));
215 /* Check, that the returned device is proper */
216 ut_assertok(regulator_get_by_platname(platname, &dev));
217 ut_asserteq_ptr(dev, dev_autoset);
219 /* Check the setup after autoset */
220 ut_asserteq(regulator_get_value(dev),
221 SANDBOX_BUCK1_AUTOSET_EXPECTED_UV);
222 ut_asserteq(regulator_get_current(dev),
223 SANDBOX_BUCK1_AUTOSET_EXPECTED_UA);
224 ut_asserteq(regulator_get_enable(dev),
225 SANDBOX_BUCK1_AUTOSET_EXPECTED_ENABLE);
229 DM_TEST(dm_test_power_regulator_autoset, DM_TESTF_SCAN_FDT);
232 * Struct setting: to keep the expected output settings.
233 * @voltage: Voltage value [uV]
234 * @current: Current value [uA]
235 * @enable: output enable state: true/false
244 * platname_list: an array of regulator platform names.
245 * For testing regulator_list_autoset() for outputs:
249 static const char *platname_list[] = {
250 SANDBOX_LDO1_PLATNAME,
251 SANDBOX_LDO2_PLATNAME,
256 * expected_setting_list: an array of regulator output setting, expected after
257 * call of the regulator_list_autoset() for the "platname_list" array.
258 * For testing results of regulator_list_autoset() for outputs:
261 * The settings are defined in: include/power/sandbox_pmic.h
263 static const struct setting expected_setting_list[] = {
265 .voltage = SANDBOX_LDO1_AUTOSET_EXPECTED_UV,
266 .current = SANDBOX_LDO1_AUTOSET_EXPECTED_UA,
267 .enable = SANDBOX_LDO1_AUTOSET_EXPECTED_ENABLE,
270 .voltage = SANDBOX_LDO2_AUTOSET_EXPECTED_UV,
271 .current = SANDBOX_LDO2_AUTOSET_EXPECTED_UA,
272 .enable = SANDBOX_LDO2_AUTOSET_EXPECTED_ENABLE,
276 static int list_count = ARRAY_SIZE(expected_setting_list);
278 /* Test regulator list autoset method */
279 static int dm_test_power_regulator_autoset_list(struct unit_test_state *uts)
281 struct udevice *dev_list[2], *dev;
285 * Test the settings of the regulator list:
286 * LDO1 with fdt properties:
287 * - min-microvolt = max-microvolt = 1800000
288 * - min-microamp = max-microamp = 100000
289 * - always-on = not set
291 * Expected output state: uV=1800000; uA=100000; output enabled
293 * LDO2 with fdt properties:
294 * - min-microvolt = max-microvolt = 3300000
295 * - always-on = not set
296 * - boot-on = not set
297 * Expected output state: uV=300000(default); output disabled(default)
298 * The expected settings are defined in: include/power/sandbox_pmic.h.
300 ut_assertok(regulator_list_autoset(platname_list, dev_list, false));
302 for (i = 0; i < list_count; i++) {
303 /* Check, that the returned device is non-NULL */
304 ut_assert(dev_list[i]);
306 /* Check, that the returned device is proper */
307 ut_assertok(regulator_get_by_platname(platname_list[i], &dev));
308 ut_asserteq_ptr(dev_list[i], dev);
310 /* Check, that regulator output Voltage value is as expected */
311 ut_asserteq(regulator_get_value(dev_list[i]),
312 expected_setting_list[i].voltage);
314 /* Check, that regulator output Current value is as expected */
315 ut_asserteq(regulator_get_current(dev_list[i]),
316 expected_setting_list[i].current);
318 /* Check, that regulator output Enable state is as expected */
319 ut_asserteq(regulator_get_enable(dev_list[i]),
320 expected_setting_list[i].enable);
325 DM_TEST(dm_test_power_regulator_autoset_list, DM_TESTF_SCAN_FDT);