blob: 9e45fd177b944bd8ff35388c8326af383126b977 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Przemyslaw Marczakdca94502015-05-13 13:38:33 +02002/*
3 * Tests for the driver model regulator API
4 *
5 * Copyright (c) 2015 Samsung Electronics
6 * Przemyslaw Marczak <p.marczak@samsung.com>
Przemyslaw Marczakdca94502015-05-13 13:38:33 +02007 */
8
Przemyslaw Marczakdca94502015-05-13 13:38:33 +02009#include <errno.h>
10#include <dm.h>
11#include <fdtdec.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Przemyslaw Marczakdca94502015-05-13 13:38:33 +020013#include <malloc.h>
14#include <dm/device-internal.h>
15#include <dm/root.h>
Przemyslaw Marczakdca94502015-05-13 13:38:33 +020016#include <dm/util.h>
17#include <dm/test.h>
18#include <dm/uclass-internal.h>
19#include <power/pmic.h>
20#include <power/regulator.h>
21#include <power/sandbox_pmic.h>
Simon Glass75c4d412020-07-19 10:15:37 -060022#include <test/test.h>
Joe Hershberger3a77be52015-05-20 14:27:27 -050023#include <test/ut.h>
Przemyslaw Marczakdca94502015-05-13 13:38:33 +020024
Przemyslaw Marczakdca94502015-05-13 13:38:33 +020025enum {
26 BUCK1,
27 BUCK2,
Felix Bracke23c3882017-11-27 09:14:16 +010028 BUCK3,
Przemyslaw Marczakdca94502015-05-13 13:38:33 +020029 LDO1,
30 LDO2,
31 OUTPUT_COUNT,
32};
33
34enum {
35 DEVNAME = 0,
36 PLATNAME,
37 OUTPUT_NAME_COUNT,
38};
39
40static const char *regulator_names[OUTPUT_COUNT][OUTPUT_NAME_COUNT] = {
41 /* devname, platname */
42 { SANDBOX_BUCK1_DEVNAME, SANDBOX_BUCK1_PLATNAME },
43 { SANDBOX_BUCK2_DEVNAME, SANDBOX_BUCK2_PLATNAME },
Felix Bracke23c3882017-11-27 09:14:16 +010044 { SANDBOX_BUCK3_DEVNAME, SANDBOX_BUCK3_PLATNAME },
Przemyslaw Marczakdca94502015-05-13 13:38:33 +020045 { SANDBOX_LDO1_DEVNAME, SANDBOX_LDO1_PLATNAME},
46 { SANDBOX_LDO2_DEVNAME, SANDBOX_LDO2_PLATNAME},
47};
48
49/* Test regulator get method */
Joe Hershberger3a77be52015-05-20 14:27:27 -050050static int dm_test_power_regulator_get(struct unit_test_state *uts)
Przemyslaw Marczakdca94502015-05-13 13:38:33 +020051{
Simon Glass71fa5b42020-12-03 16:55:18 -070052 struct dm_regulator_uclass_plat *uc_pdata;
Przemyslaw Marczakdca94502015-05-13 13:38:33 +020053 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));
Simon Glass71fa5b42020-12-03 16:55:18 -070079 uc_pdata = dev_get_uclass_plat(dev_by_platname);
Przemyslaw Marczakdca94502015-05-13 13:38:33 +020080 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}
Simon Glass974dccd2020-07-28 19:41:12 -060092DM_TEST(dm_test_power_regulator_get, UT_TESTF_SCAN_FDT);
Przemyslaw Marczakdca94502015-05-13 13:38:33 +020093
94/* Test regulator set and get Voltage method */
Joe Hershberger3a77be52015-05-20 14:27:27 -050095static int dm_test_power_regulator_set_get_voltage(struct unit_test_state *uts)
Przemyslaw Marczakdca94502015-05-13 13:38:33 +020096{
Simon Glass71fa5b42020-12-03 16:55:18 -070097 struct dm_regulator_uclass_plat *uc_pdata;
Przemyslaw Marczakdca94502015-05-13 13:38:33 +020098 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
Simon Glass71fa5b42020-12-03 16:55:18 -0700106 uc_pdata = dev_get_uclass_plat(dev);
Przemyslaw Marczakdca94502015-05-13 13:38:33 +0200107 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}
Simon Glass974dccd2020-07-28 19:41:12 -0600119DM_TEST(dm_test_power_regulator_set_get_voltage, UT_TESTF_SCAN_FDT);
Przemyslaw Marczakdca94502015-05-13 13:38:33 +0200120
121/* Test regulator set and get Current method */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500122static int dm_test_power_regulator_set_get_current(struct unit_test_state *uts)
Przemyslaw Marczakdca94502015-05-13 13:38:33 +0200123{
Simon Glass71fa5b42020-12-03 16:55:18 -0700124 struct dm_regulator_uclass_plat *uc_pdata;
Przemyslaw Marczakdca94502015-05-13 13:38:33 +0200125 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
Simon Glass71fa5b42020-12-03 16:55:18 -0700133 uc_pdata = dev_get_uclass_plat(dev);
Przemyslaw Marczakdca94502015-05-13 13:38:33 +0200134 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
Simon Glass71fa5b42020-12-03 16:55:18 -0700148 uc_pdata = dev_get_uclass_plat(dev);
Przemyslaw Marczakdca94502015-05-13 13:38:33 +0200149 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}
Simon Glass974dccd2020-07-28 19:41:12 -0600158DM_TEST(dm_test_power_regulator_set_get_current, UT_TESTF_SCAN_FDT);
Przemyslaw Marczakdca94502015-05-13 13:38:33 +0200159
160/* Test regulator set and get Enable method */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500161static int dm_test_power_regulator_set_get_enable(struct unit_test_state *uts)
Przemyslaw Marczakdca94502015-05-13 13:38:33 +0200162{
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}
Simon Glass974dccd2020-07-28 19:41:12 -0600177DM_TEST(dm_test_power_regulator_set_get_enable, UT_TESTF_SCAN_FDT);
Przemyslaw Marczakdca94502015-05-13 13:38:33 +0200178
Lokesh Vutla7106b782019-01-11 15:15:51 +0530179/* Test regulator set and get enable if allowed method */
180static
181int dm_test_power_regulator_set_enable_if_allowed(struct unit_test_state *uts)
182{
183 const char *platname;
184 struct udevice *dev, *dev_autoset;
185 bool val_set = false;
186
187 /* Get BUCK1 - always on regulator */
188 platname = regulator_names[BUCK1][PLATNAME];
189 ut_assertok(regulator_autoset_by_name(platname, &dev_autoset));
190 ut_assertok(regulator_get_by_platname(platname, &dev));
191
192 /* Try disabling always-on regulator */
193 ut_assertok(regulator_set_enable_if_allowed(dev, val_set));
194 ut_asserteq(regulator_get_enable(dev), !val_set);
195
196 return 0;
197}
Simon Glass974dccd2020-07-28 19:41:12 -0600198DM_TEST(dm_test_power_regulator_set_enable_if_allowed, UT_TESTF_SCAN_FDT);
Lokesh Vutla7106b782019-01-11 15:15:51 +0530199
Przemyslaw Marczakdca94502015-05-13 13:38:33 +0200200/* Test regulator set and get mode method */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500201static int dm_test_power_regulator_set_get_mode(struct unit_test_state *uts)
Przemyslaw Marczakdca94502015-05-13 13:38:33 +0200202{
203 const char *platname;
204 struct udevice *dev;
205 int val_set = LDO_OM_SLEEP;
206
207 /* Set the mode id to LDO_OM_SLEEP of LDO1 - default is LDO_OM_OFF */
208 platname = regulator_names[LDO1][PLATNAME];
209 ut_assertok(regulator_get_by_platname(platname, &dev));
210 ut_assertok(regulator_set_mode(dev, val_set));
211
212 /* Get the mode id of LDO1 and compare it with the requested one */
213 ut_asserteq(regulator_get_mode(dev), val_set);
214
215 return 0;
216}
Simon Glass974dccd2020-07-28 19:41:12 -0600217DM_TEST(dm_test_power_regulator_set_get_mode, UT_TESTF_SCAN_FDT);
Przemyslaw Marczakdca94502015-05-13 13:38:33 +0200218
Joseph Chenbb511322019-09-26 15:43:52 +0800219/* Test regulator set and get suspend Voltage method */
220static int dm_test_power_regulator_set_get_suspend_voltage(struct unit_test_state *uts)
221{
Simon Glass71fa5b42020-12-03 16:55:18 -0700222 struct dm_regulator_uclass_plat *uc_pdata;
Joseph Chenbb511322019-09-26 15:43:52 +0800223 const struct dm_regulator_ops *ops;
224 struct udevice *dev;
225 const char *platname;
226 int val_set, val_get;
227
228 /* Set and get Voltage of BUCK1 - set to 'min' constraint */
229 platname = regulator_names[BUCK1][PLATNAME];
230 ut_assertok(regulator_get_by_platname(platname, &dev));
231
Simon Glass71fa5b42020-12-03 16:55:18 -0700232 uc_pdata = dev_get_uclass_plat(dev);
Joseph Chenbb511322019-09-26 15:43:52 +0800233 ut_assert(uc_pdata);
234
235 ops = dev_get_driver_ops(dev);
236
237 if (ops->set_suspend_value && ops->get_suspend_value) {
238 val_set = uc_pdata->suspend_uV;
239 ut_assertok(regulator_set_suspend_value(dev, val_set));
240 val_get = regulator_get_suspend_value(dev);
241 ut_assert(val_get >= 0);
242
243 ut_asserteq(val_set, val_get);
244 }
245 return 0;
246}
Simon Glass974dccd2020-07-28 19:41:12 -0600247DM_TEST(dm_test_power_regulator_set_get_suspend_voltage, UT_TESTF_SCAN_FDT);
Joseph Chenbb511322019-09-26 15:43:52 +0800248
249/* Test regulator set and get suspend Enable method */
250static int dm_test_power_regulator_set_get_suspend_enable(struct unit_test_state *uts)
251{
252 const struct dm_regulator_ops *ops;
253 const char *platname;
254 struct udevice *dev;
255 bool val_set = true;
256
257 /* Set the Enable of LDO1 - default is disabled */
258 platname = regulator_names[LDO1][PLATNAME];
259 ut_assertok(regulator_get_by_platname(platname, &dev));
260
261 ops = dev_get_driver_ops(dev);
262
263 if (ops->set_suspend_enable && ops->get_suspend_enable) {
264 ut_assertok(regulator_set_suspend_enable(dev, val_set));
265
266 /*
267 * Get the Enable state of LDO1 and
268 * compare it with the requested one
269 */
270 ut_asserteq(regulator_get_suspend_enable(dev), val_set);
271 }
272 return 0;
273}
Simon Glass974dccd2020-07-28 19:41:12 -0600274DM_TEST(dm_test_power_regulator_set_get_suspend_enable, UT_TESTF_SCAN_FDT);
Joseph Chenbb511322019-09-26 15:43:52 +0800275
Przemyslaw Marczakdca94502015-05-13 13:38:33 +0200276/* Test regulator autoset method */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500277static int dm_test_power_regulator_autoset(struct unit_test_state *uts)
Przemyslaw Marczakdca94502015-05-13 13:38:33 +0200278{
279 const char *platname;
280 struct udevice *dev, *dev_autoset;
281
282 /*
283 * Test the BUCK1 with fdt properties
284 * - min-microvolt = max-microvolt = 1200000
285 * - min-microamp = max-microamp = 200000
286 * - always-on = set
287 * - boot-on = not set
288 * Expected output state: uV=1200000; uA=200000; output enabled
289 */
290 platname = regulator_names[BUCK1][PLATNAME];
Simon Glass46cb8242015-06-23 15:38:58 -0600291 ut_assertok(regulator_autoset_by_name(platname, &dev_autoset));
Przemyslaw Marczakdca94502015-05-13 13:38:33 +0200292
293 /* Check, that the returned device is proper */
294 ut_assertok(regulator_get_by_platname(platname, &dev));
295 ut_asserteq_ptr(dev, dev_autoset);
296
297 /* Check the setup after autoset */
298 ut_asserteq(regulator_get_value(dev),
299 SANDBOX_BUCK1_AUTOSET_EXPECTED_UV);
300 ut_asserteq(regulator_get_current(dev),
301 SANDBOX_BUCK1_AUTOSET_EXPECTED_UA);
302 ut_asserteq(regulator_get_enable(dev),
303 SANDBOX_BUCK1_AUTOSET_EXPECTED_ENABLE);
304
305 return 0;
306}
Simon Glass974dccd2020-07-28 19:41:12 -0600307DM_TEST(dm_test_power_regulator_autoset, UT_TESTF_SCAN_FDT);
Przemyslaw Marczakdca94502015-05-13 13:38:33 +0200308
309/*
310 * Struct setting: to keep the expected output settings.
311 * @voltage: Voltage value [uV]
312 * @current: Current value [uA]
313 * @enable: output enable state: true/false
314 */
315struct setting {
316 int voltage;
317 int current;
318 bool enable;
319};
320
321/*
322 * platname_list: an array of regulator platform names.
323 * For testing regulator_list_autoset() for outputs:
324 * - LDO1
325 * - LDO2
326 */
327static const char *platname_list[] = {
328 SANDBOX_LDO1_PLATNAME,
329 SANDBOX_LDO2_PLATNAME,
330 NULL,
331};
332
333/*
334 * expected_setting_list: an array of regulator output setting, expected after
335 * call of the regulator_list_autoset() for the "platname_list" array.
336 * For testing results of regulator_list_autoset() for outputs:
337 * - LDO1
338 * - LDO2
339 * The settings are defined in: include/power/sandbox_pmic.h
340 */
341static const struct setting expected_setting_list[] = {
342 [0] = { /* LDO1 */
343 .voltage = SANDBOX_LDO1_AUTOSET_EXPECTED_UV,
344 .current = SANDBOX_LDO1_AUTOSET_EXPECTED_UA,
345 .enable = SANDBOX_LDO1_AUTOSET_EXPECTED_ENABLE,
346 },
347 [1] = { /* LDO2 */
348 .voltage = SANDBOX_LDO2_AUTOSET_EXPECTED_UV,
349 .current = SANDBOX_LDO2_AUTOSET_EXPECTED_UA,
350 .enable = SANDBOX_LDO2_AUTOSET_EXPECTED_ENABLE,
351 },
352};
353
354static int list_count = ARRAY_SIZE(expected_setting_list);
355
356/* Test regulator list autoset method */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500357static int dm_test_power_regulator_autoset_list(struct unit_test_state *uts)
Przemyslaw Marczakdca94502015-05-13 13:38:33 +0200358{
359 struct udevice *dev_list[2], *dev;
360 int i;
361
362 /*
363 * Test the settings of the regulator list:
364 * LDO1 with fdt properties:
365 * - min-microvolt = max-microvolt = 1800000
366 * - min-microamp = max-microamp = 100000
367 * - always-on = not set
368 * - boot-on = set
369 * Expected output state: uV=1800000; uA=100000; output enabled
370 *
371 * LDO2 with fdt properties:
372 * - min-microvolt = max-microvolt = 3300000
373 * - always-on = not set
374 * - boot-on = not set
375 * Expected output state: uV=300000(default); output disabled(default)
376 * The expected settings are defined in: include/power/sandbox_pmic.h.
377 */
378 ut_assertok(regulator_list_autoset(platname_list, dev_list, false));
379
380 for (i = 0; i < list_count; i++) {
381 /* Check, that the returned device is non-NULL */
382 ut_assert(dev_list[i]);
383
384 /* Check, that the returned device is proper */
385 ut_assertok(regulator_get_by_platname(platname_list[i], &dev));
386 ut_asserteq_ptr(dev_list[i], dev);
387
388 /* Check, that regulator output Voltage value is as expected */
389 ut_asserteq(regulator_get_value(dev_list[i]),
390 expected_setting_list[i].voltage);
391
392 /* Check, that regulator output Current value is as expected */
393 ut_asserteq(regulator_get_current(dev_list[i]),
394 expected_setting_list[i].current);
395
396 /* Check, that regulator output Enable state is as expected */
397 ut_asserteq(regulator_get_enable(dev_list[i]),
398 expected_setting_list[i].enable);
399 }
400
401 return 0;
402}
Simon Glass974dccd2020-07-28 19:41:12 -0600403DM_TEST(dm_test_power_regulator_autoset_list, UT_TESTF_SCAN_FDT);