]> git.ipfire.org Git - people/ms/u-boot.git/blob - test/dm/regulator.c
Add a power domain framework/uclass
[people/ms/u-boot.git] / test / dm / regulator.c
1 /*
2 * Tests for the driver model regulator API
3 *
4 * Copyright (c) 2015 Samsung Electronics
5 * Przemyslaw Marczak <p.marczak@samsung.com>
6 *
7 * SPDX-License-Identifier: GPL-2.0+
8 */
9
10 #include <common.h>
11 #include <errno.h>
12 #include <dm.h>
13 #include <fdtdec.h>
14 #include <malloc.h>
15 #include <dm/device-internal.h>
16 #include <dm/root.h>
17 #include <dm/util.h>
18 #include <dm/test.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/ut.h>
24
25 DECLARE_GLOBAL_DATA_PTR;
26
27 enum {
28 BUCK1,
29 BUCK2,
30 LDO1,
31 LDO2,
32 OUTPUT_COUNT,
33 };
34
35 enum {
36 DEVNAME = 0,
37 PLATNAME,
38 OUTPUT_NAME_COUNT,
39 };
40
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},
47 };
48
49 /* Test regulator get method */
50 static int dm_test_power_regulator_get(struct unit_test_state *uts)
51 {
52 struct dm_regulator_uclass_platdata *uc_pdata;
53 struct udevice *dev_by_devname;
54 struct udevice *dev_by_platname;
55 const char *devname;
56 const char *platname;
57 int i;
58
59 for (i = 0; i < OUTPUT_COUNT; i++) {
60 /*
61 * Do the test for each regulator's devname and platname,
62 * which are related to a single device.
63 */
64 devname = regulator_names[i][DEVNAME];
65 platname = regulator_names[i][PLATNAME];
66
67 /*
68 * Check, that regulator_get_by_devname() function, returns
69 * a device with the name equal to the requested one.
70 */
71 ut_assertok(regulator_get_by_devname(devname, &dev_by_devname));
72 ut_asserteq_str(devname, dev_by_devname->name);
73
74 /*
75 * Check, that regulator_get_by_platname() function, returns
76 * a device with the name equal to the requested one.
77 */
78 ut_assertok(regulator_get_by_platname(platname, &dev_by_platname));
79 uc_pdata = dev_get_uclass_platdata(dev_by_platname);
80 ut_assert(uc_pdata);
81 ut_asserteq_str(platname, uc_pdata->name);
82
83 /*
84 * Check, that the pointers returned by both get functions,
85 * points to the same regulator device.
86 */
87 ut_asserteq_ptr(dev_by_devname, dev_by_platname);
88 }
89
90 return 0;
91 }
92 DM_TEST(dm_test_power_regulator_get, DM_TESTF_SCAN_FDT);
93
94 /* Test regulator set and get Voltage method */
95 static int dm_test_power_regulator_set_get_voltage(struct unit_test_state *uts)
96 {
97 struct dm_regulator_uclass_platdata *uc_pdata;
98 struct udevice *dev;
99 const char *platname;
100 int val_set, val_get;
101
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));
105
106 uc_pdata = dev_get_uclass_platdata(dev);
107 ut_assert(uc_pdata);
108
109 val_set = uc_pdata->min_uV;
110 ut_assertok(regulator_set_value(dev, val_set));
111
112 val_get = regulator_get_value(dev);
113 ut_assert(val_get >= 0);
114
115 ut_asserteq(val_set, val_get);
116
117 return 0;
118 }
119 DM_TEST(dm_test_power_regulator_set_get_voltage, DM_TESTF_SCAN_FDT);
120
121 /* Test regulator set and get Current method */
122 static int dm_test_power_regulator_set_get_current(struct unit_test_state *uts)
123 {
124 struct dm_regulator_uclass_platdata *uc_pdata;
125 struct udevice *dev;
126 const char *platname;
127 int val_set, val_get;
128
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));
132
133 uc_pdata = dev_get_uclass_platdata(dev);
134 ut_assert(uc_pdata);
135
136 val_set = uc_pdata->min_uA;
137 ut_assertok(regulator_set_current(dev, val_set));
138
139 val_get = regulator_get_current(dev);
140 ut_assert(val_get >= 0);
141
142 ut_asserteq(val_set, val_get);
143
144 /* Check LDO2 current limit constraints - should be -ENODATA */
145 platname = regulator_names[LDO2][PLATNAME];
146 ut_assertok(regulator_get_by_platname(platname, &dev));
147
148 uc_pdata = dev_get_uclass_platdata(dev);
149 ut_assert(uc_pdata);
150 ut_asserteq(-ENODATA, uc_pdata->min_uA);
151 ut_asserteq(-ENODATA, uc_pdata->max_uA);
152
153 /* Try set the Current of LDO2 - should return -ENOSYS */
154 ut_asserteq(-ENOSYS, regulator_set_current(dev, 0));
155
156 return 0;
157 }
158 DM_TEST(dm_test_power_regulator_set_get_current, DM_TESTF_SCAN_FDT);
159
160 /* Test regulator set and get Enable method */
161 static int dm_test_power_regulator_set_get_enable(struct unit_test_state *uts)
162 {
163 const char *platname;
164 struct udevice *dev;
165 bool val_set = true;
166
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));
171
172 /* Get the Enable state of LDO1 and compare it with the requested one */
173 ut_asserteq(regulator_get_enable(dev), val_set);
174
175 return 0;
176 }
177 DM_TEST(dm_test_power_regulator_set_get_enable, DM_TESTF_SCAN_FDT);
178
179 /* Test regulator set and get mode method */
180 static int dm_test_power_regulator_set_get_mode(struct unit_test_state *uts)
181 {
182 const char *platname;
183 struct udevice *dev;
184 int val_set = LDO_OM_SLEEP;
185
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));
190
191 /* Get the mode id of LDO1 and compare it with the requested one */
192 ut_asserteq(regulator_get_mode(dev), val_set);
193
194 return 0;
195 }
196 DM_TEST(dm_test_power_regulator_set_get_mode, DM_TESTF_SCAN_FDT);
197
198 /* Test regulator autoset method */
199 static int dm_test_power_regulator_autoset(struct unit_test_state *uts)
200 {
201 const char *platname;
202 struct udevice *dev, *dev_autoset;
203
204 /*
205 * Test the BUCK1 with fdt properties
206 * - min-microvolt = max-microvolt = 1200000
207 * - min-microamp = max-microamp = 200000
208 * - always-on = set
209 * - boot-on = not set
210 * Expected output state: uV=1200000; uA=200000; output enabled
211 */
212 platname = regulator_names[BUCK1][PLATNAME];
213 ut_assertok(regulator_autoset_by_name(platname, &dev_autoset));
214
215 /* Check, that the returned device is proper */
216 ut_assertok(regulator_get_by_platname(platname, &dev));
217 ut_asserteq_ptr(dev, dev_autoset);
218
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);
226
227 return 0;
228 }
229 DM_TEST(dm_test_power_regulator_autoset, DM_TESTF_SCAN_FDT);
230
231 /*
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
236 */
237 struct setting {
238 int voltage;
239 int current;
240 bool enable;
241 };
242
243 /*
244 * platname_list: an array of regulator platform names.
245 * For testing regulator_list_autoset() for outputs:
246 * - LDO1
247 * - LDO2
248 */
249 static const char *platname_list[] = {
250 SANDBOX_LDO1_PLATNAME,
251 SANDBOX_LDO2_PLATNAME,
252 NULL,
253 };
254
255 /*
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:
259 * - LDO1
260 * - LDO2
261 * The settings are defined in: include/power/sandbox_pmic.h
262 */
263 static const struct setting expected_setting_list[] = {
264 [0] = { /* LDO1 */
265 .voltage = SANDBOX_LDO1_AUTOSET_EXPECTED_UV,
266 .current = SANDBOX_LDO1_AUTOSET_EXPECTED_UA,
267 .enable = SANDBOX_LDO1_AUTOSET_EXPECTED_ENABLE,
268 },
269 [1] = { /* LDO2 */
270 .voltage = SANDBOX_LDO2_AUTOSET_EXPECTED_UV,
271 .current = SANDBOX_LDO2_AUTOSET_EXPECTED_UA,
272 .enable = SANDBOX_LDO2_AUTOSET_EXPECTED_ENABLE,
273 },
274 };
275
276 static int list_count = ARRAY_SIZE(expected_setting_list);
277
278 /* Test regulator list autoset method */
279 static int dm_test_power_regulator_autoset_list(struct unit_test_state *uts)
280 {
281 struct udevice *dev_list[2], *dev;
282 int i;
283
284 /*
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
290 * - boot-on = set
291 * Expected output state: uV=1800000; uA=100000; output enabled
292 *
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.
299 */
300 ut_assertok(regulator_list_autoset(platname_list, dev_list, false));
301
302 for (i = 0; i < list_count; i++) {
303 /* Check, that the returned device is non-NULL */
304 ut_assert(dev_list[i]);
305
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);
309
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);
313
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);
317
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);
321 }
322
323 return 0;
324 }
325 DM_TEST(dm_test_power_regulator_autoset_list, DM_TESTF_SCAN_FDT);