blob: 86f4862d9dd07cecda929609617d609f10c0bfb2 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Przemyslaw Marczake8f339e2015-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 Marczake8f339e2015-05-13 13:38:33 +02007 */
8
9#include <common.h>
10#include <errno.h>
11#include <dm.h>
12#include <fdtdec.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060013#include <log.h>
Przemyslaw Marczake8f339e2015-05-13 13:38:33 +020014#include <malloc.h>
15#include <dm/device-internal.h>
16#include <dm/root.h>
Przemyslaw Marczake8f339e2015-05-13 13:38:33 +020017#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>
Simon Glass0e1fad42020-07-19 10:15:37 -060023#include <test/test.h>
Joe Hershbergere721b882015-05-20 14:27:27 -050024#include <test/ut.h>
Przemyslaw Marczake8f339e2015-05-13 13:38:33 +020025
Przemyslaw Marczake8f339e2015-05-13 13:38:33 +020026enum {
27 BUCK1,
28 BUCK2,
Felix Brackbf802f52017-11-27 09:14:16 +010029 BUCK3,
Przemyslaw Marczake8f339e2015-05-13 13:38:33 +020030 LDO1,
31 LDO2,
32 OUTPUT_COUNT,
33};
34
35enum {
36 DEVNAME = 0,
37 PLATNAME,
38 OUTPUT_NAME_COUNT,
39};
40
41static 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 },
Felix Brackbf802f52017-11-27 09:14:16 +010045 { SANDBOX_BUCK3_DEVNAME, SANDBOX_BUCK3_PLATNAME },
Przemyslaw Marczake8f339e2015-05-13 13:38:33 +020046 { SANDBOX_LDO1_DEVNAME, SANDBOX_LDO1_PLATNAME},
47 { SANDBOX_LDO2_DEVNAME, SANDBOX_LDO2_PLATNAME},
48};
49
50/* Test regulator get method */
Joe Hershbergere721b882015-05-20 14:27:27 -050051static int dm_test_power_regulator_get(struct unit_test_state *uts)
Przemyslaw Marczake8f339e2015-05-13 13:38:33 +020052{
Simon Glasscaa4daa2020-12-03 16:55:18 -070053 struct dm_regulator_uclass_plat *uc_pdata;
Przemyslaw Marczake8f339e2015-05-13 13:38:33 +020054 struct udevice *dev_by_devname;
55 struct udevice *dev_by_platname;
56 const char *devname;
57 const char *platname;
58 int i;
59
60 for (i = 0; i < OUTPUT_COUNT; i++) {
61 /*
62 * Do the test for each regulator's devname and platname,
63 * which are related to a single device.
64 */
65 devname = regulator_names[i][DEVNAME];
66 platname = regulator_names[i][PLATNAME];
67
68 /*
69 * Check, that regulator_get_by_devname() function, returns
70 * a device with the name equal to the requested one.
71 */
72 ut_assertok(regulator_get_by_devname(devname, &dev_by_devname));
73 ut_asserteq_str(devname, dev_by_devname->name);
74
75 /*
76 * Check, that regulator_get_by_platname() function, returns
77 * a device with the name equal to the requested one.
78 */
79 ut_assertok(regulator_get_by_platname(platname, &dev_by_platname));
Simon Glasscaa4daa2020-12-03 16:55:18 -070080 uc_pdata = dev_get_uclass_plat(dev_by_platname);
Przemyslaw Marczake8f339e2015-05-13 13:38:33 +020081 ut_assert(uc_pdata);
82 ut_asserteq_str(platname, uc_pdata->name);
83
84 /*
85 * Check, that the pointers returned by both get functions,
86 * points to the same regulator device.
87 */
88 ut_asserteq_ptr(dev_by_devname, dev_by_platname);
89 }
90
91 return 0;
92}
Simon Glasse180c2b2020-07-28 19:41:12 -060093DM_TEST(dm_test_power_regulator_get, UT_TESTF_SCAN_FDT);
Przemyslaw Marczake8f339e2015-05-13 13:38:33 +020094
95/* Test regulator set and get Voltage method */
Joe Hershbergere721b882015-05-20 14:27:27 -050096static int dm_test_power_regulator_set_get_voltage(struct unit_test_state *uts)
Przemyslaw Marczake8f339e2015-05-13 13:38:33 +020097{
Simon Glasscaa4daa2020-12-03 16:55:18 -070098 struct dm_regulator_uclass_plat *uc_pdata;
Przemyslaw Marczake8f339e2015-05-13 13:38:33 +020099 struct udevice *dev;
100 const char *platname;
101 int val_set, val_get;
102
103 /* Set and get Voltage of BUCK1 - set to 'min' constraint */
104 platname = regulator_names[BUCK1][PLATNAME];
105 ut_assertok(regulator_get_by_platname(platname, &dev));
106
Simon Glasscaa4daa2020-12-03 16:55:18 -0700107 uc_pdata = dev_get_uclass_plat(dev);
Przemyslaw Marczake8f339e2015-05-13 13:38:33 +0200108 ut_assert(uc_pdata);
109
110 val_set = uc_pdata->min_uV;
111 ut_assertok(regulator_set_value(dev, val_set));
112
113 val_get = regulator_get_value(dev);
114 ut_assert(val_get >= 0);
115
116 ut_asserteq(val_set, val_get);
117
118 return 0;
119}
Simon Glasse180c2b2020-07-28 19:41:12 -0600120DM_TEST(dm_test_power_regulator_set_get_voltage, UT_TESTF_SCAN_FDT);
Przemyslaw Marczake8f339e2015-05-13 13:38:33 +0200121
122/* Test regulator set and get Current method */
Joe Hershbergere721b882015-05-20 14:27:27 -0500123static int dm_test_power_regulator_set_get_current(struct unit_test_state *uts)
Przemyslaw Marczake8f339e2015-05-13 13:38:33 +0200124{
Simon Glasscaa4daa2020-12-03 16:55:18 -0700125 struct dm_regulator_uclass_plat *uc_pdata;
Przemyslaw Marczake8f339e2015-05-13 13:38:33 +0200126 struct udevice *dev;
127 const char *platname;
128 int val_set, val_get;
129
130 /* Set and get the Current of LDO1 - set to 'min' constraint */
131 platname = regulator_names[LDO1][PLATNAME];
132 ut_assertok(regulator_get_by_platname(platname, &dev));
133
Simon Glasscaa4daa2020-12-03 16:55:18 -0700134 uc_pdata = dev_get_uclass_plat(dev);
Przemyslaw Marczake8f339e2015-05-13 13:38:33 +0200135 ut_assert(uc_pdata);
136
137 val_set = uc_pdata->min_uA;
138 ut_assertok(regulator_set_current(dev, val_set));
139
140 val_get = regulator_get_current(dev);
141 ut_assert(val_get >= 0);
142
143 ut_asserteq(val_set, val_get);
144
145 /* Check LDO2 current limit constraints - should be -ENODATA */
146 platname = regulator_names[LDO2][PLATNAME];
147 ut_assertok(regulator_get_by_platname(platname, &dev));
148
Simon Glasscaa4daa2020-12-03 16:55:18 -0700149 uc_pdata = dev_get_uclass_plat(dev);
Przemyslaw Marczake8f339e2015-05-13 13:38:33 +0200150 ut_assert(uc_pdata);
151 ut_asserteq(-ENODATA, uc_pdata->min_uA);
152 ut_asserteq(-ENODATA, uc_pdata->max_uA);
153
154 /* Try set the Current of LDO2 - should return -ENOSYS */
155 ut_asserteq(-ENOSYS, regulator_set_current(dev, 0));
156
157 return 0;
158}
Simon Glasse180c2b2020-07-28 19:41:12 -0600159DM_TEST(dm_test_power_regulator_set_get_current, UT_TESTF_SCAN_FDT);
Przemyslaw Marczake8f339e2015-05-13 13:38:33 +0200160
161/* Test regulator set and get Enable method */
Joe Hershbergere721b882015-05-20 14:27:27 -0500162static int dm_test_power_regulator_set_get_enable(struct unit_test_state *uts)
Przemyslaw Marczake8f339e2015-05-13 13:38:33 +0200163{
164 const char *platname;
165 struct udevice *dev;
166 bool val_set = true;
167
168 /* Set the Enable of LDO1 - default is disabled */
169 platname = regulator_names[LDO1][PLATNAME];
170 ut_assertok(regulator_get_by_platname(platname, &dev));
171 ut_assertok(regulator_set_enable(dev, val_set));
172
173 /* Get the Enable state of LDO1 and compare it with the requested one */
174 ut_asserteq(regulator_get_enable(dev), val_set);
175
176 return 0;
177}
Simon Glasse180c2b2020-07-28 19:41:12 -0600178DM_TEST(dm_test_power_regulator_set_get_enable, UT_TESTF_SCAN_FDT);
Przemyslaw Marczake8f339e2015-05-13 13:38:33 +0200179
Lokesh Vutlacc4a2242019-01-11 15:15:51 +0530180/* Test regulator set and get enable if allowed method */
181static
182int dm_test_power_regulator_set_enable_if_allowed(struct unit_test_state *uts)
183{
184 const char *platname;
185 struct udevice *dev, *dev_autoset;
186 bool val_set = false;
187
188 /* Get BUCK1 - always on regulator */
189 platname = regulator_names[BUCK1][PLATNAME];
190 ut_assertok(regulator_autoset_by_name(platname, &dev_autoset));
191 ut_assertok(regulator_get_by_platname(platname, &dev));
192
193 /* Try disabling always-on regulator */
194 ut_assertok(regulator_set_enable_if_allowed(dev, val_set));
195 ut_asserteq(regulator_get_enable(dev), !val_set);
196
197 return 0;
198}
Simon Glasse180c2b2020-07-28 19:41:12 -0600199DM_TEST(dm_test_power_regulator_set_enable_if_allowed, UT_TESTF_SCAN_FDT);
Lokesh Vutlacc4a2242019-01-11 15:15:51 +0530200
Przemyslaw Marczake8f339e2015-05-13 13:38:33 +0200201/* Test regulator set and get mode method */
Joe Hershbergere721b882015-05-20 14:27:27 -0500202static int dm_test_power_regulator_set_get_mode(struct unit_test_state *uts)
Przemyslaw Marczake8f339e2015-05-13 13:38:33 +0200203{
204 const char *platname;
205 struct udevice *dev;
206 int val_set = LDO_OM_SLEEP;
207
208 /* Set the mode id to LDO_OM_SLEEP of LDO1 - default is LDO_OM_OFF */
209 platname = regulator_names[LDO1][PLATNAME];
210 ut_assertok(regulator_get_by_platname(platname, &dev));
211 ut_assertok(regulator_set_mode(dev, val_set));
212
213 /* Get the mode id of LDO1 and compare it with the requested one */
214 ut_asserteq(regulator_get_mode(dev), val_set);
215
216 return 0;
217}
Simon Glasse180c2b2020-07-28 19:41:12 -0600218DM_TEST(dm_test_power_regulator_set_get_mode, UT_TESTF_SCAN_FDT);
Przemyslaw Marczake8f339e2015-05-13 13:38:33 +0200219
Joseph Chen11406b82019-09-26 15:43:52 +0800220/* Test regulator set and get suspend Voltage method */
221static int dm_test_power_regulator_set_get_suspend_voltage(struct unit_test_state *uts)
222{
Simon Glasscaa4daa2020-12-03 16:55:18 -0700223 struct dm_regulator_uclass_plat *uc_pdata;
Joseph Chen11406b82019-09-26 15:43:52 +0800224 const struct dm_regulator_ops *ops;
225 struct udevice *dev;
226 const char *platname;
227 int val_set, val_get;
228
229 /* Set and get Voltage of BUCK1 - set to 'min' constraint */
230 platname = regulator_names[BUCK1][PLATNAME];
231 ut_assertok(regulator_get_by_platname(platname, &dev));
232
Simon Glasscaa4daa2020-12-03 16:55:18 -0700233 uc_pdata = dev_get_uclass_plat(dev);
Joseph Chen11406b82019-09-26 15:43:52 +0800234 ut_assert(uc_pdata);
235
236 ops = dev_get_driver_ops(dev);
237
238 if (ops->set_suspend_value && ops->get_suspend_value) {
239 val_set = uc_pdata->suspend_uV;
240 ut_assertok(regulator_set_suspend_value(dev, val_set));
241 val_get = regulator_get_suspend_value(dev);
242 ut_assert(val_get >= 0);
243
244 ut_asserteq(val_set, val_get);
245 }
246 return 0;
247}
Simon Glasse180c2b2020-07-28 19:41:12 -0600248DM_TEST(dm_test_power_regulator_set_get_suspend_voltage, UT_TESTF_SCAN_FDT);
Joseph Chen11406b82019-09-26 15:43:52 +0800249
250/* Test regulator set and get suspend Enable method */
251static int dm_test_power_regulator_set_get_suspend_enable(struct unit_test_state *uts)
252{
253 const struct dm_regulator_ops *ops;
254 const char *platname;
255 struct udevice *dev;
256 bool val_set = true;
257
258 /* Set the Enable of LDO1 - default is disabled */
259 platname = regulator_names[LDO1][PLATNAME];
260 ut_assertok(regulator_get_by_platname(platname, &dev));
261
262 ops = dev_get_driver_ops(dev);
263
264 if (ops->set_suspend_enable && ops->get_suspend_enable) {
265 ut_assertok(regulator_set_suspend_enable(dev, val_set));
266
267 /*
268 * Get the Enable state of LDO1 and
269 * compare it with the requested one
270 */
271 ut_asserteq(regulator_get_suspend_enable(dev), val_set);
272 }
273 return 0;
274}
Simon Glasse180c2b2020-07-28 19:41:12 -0600275DM_TEST(dm_test_power_regulator_set_get_suspend_enable, UT_TESTF_SCAN_FDT);
Joseph Chen11406b82019-09-26 15:43:52 +0800276
Przemyslaw Marczake8f339e2015-05-13 13:38:33 +0200277/* Test regulator autoset method */
Joe Hershbergere721b882015-05-20 14:27:27 -0500278static int dm_test_power_regulator_autoset(struct unit_test_state *uts)
Przemyslaw Marczake8f339e2015-05-13 13:38:33 +0200279{
280 const char *platname;
281 struct udevice *dev, *dev_autoset;
282
283 /*
284 * Test the BUCK1 with fdt properties
285 * - min-microvolt = max-microvolt = 1200000
286 * - min-microamp = max-microamp = 200000
287 * - always-on = set
288 * - boot-on = not set
289 * Expected output state: uV=1200000; uA=200000; output enabled
290 */
291 platname = regulator_names[BUCK1][PLATNAME];
Simon Glass3b55d302015-06-23 15:38:58 -0600292 ut_assertok(regulator_autoset_by_name(platname, &dev_autoset));
Przemyslaw Marczake8f339e2015-05-13 13:38:33 +0200293
294 /* Check, that the returned device is proper */
295 ut_assertok(regulator_get_by_platname(platname, &dev));
296 ut_asserteq_ptr(dev, dev_autoset);
297
298 /* Check the setup after autoset */
299 ut_asserteq(regulator_get_value(dev),
300 SANDBOX_BUCK1_AUTOSET_EXPECTED_UV);
301 ut_asserteq(regulator_get_current(dev),
302 SANDBOX_BUCK1_AUTOSET_EXPECTED_UA);
303 ut_asserteq(regulator_get_enable(dev),
304 SANDBOX_BUCK1_AUTOSET_EXPECTED_ENABLE);
305
306 return 0;
307}
Simon Glasse180c2b2020-07-28 19:41:12 -0600308DM_TEST(dm_test_power_regulator_autoset, UT_TESTF_SCAN_FDT);
Przemyslaw Marczake8f339e2015-05-13 13:38:33 +0200309
310/*
311 * Struct setting: to keep the expected output settings.
312 * @voltage: Voltage value [uV]
313 * @current: Current value [uA]
314 * @enable: output enable state: true/false
315 */
316struct setting {
317 int voltage;
318 int current;
319 bool enable;
320};
321
322/*
323 * platname_list: an array of regulator platform names.
324 * For testing regulator_list_autoset() for outputs:
325 * - LDO1
326 * - LDO2
327 */
328static const char *platname_list[] = {
329 SANDBOX_LDO1_PLATNAME,
330 SANDBOX_LDO2_PLATNAME,
331 NULL,
332};
333
334/*
335 * expected_setting_list: an array of regulator output setting, expected after
336 * call of the regulator_list_autoset() for the "platname_list" array.
337 * For testing results of regulator_list_autoset() for outputs:
338 * - LDO1
339 * - LDO2
340 * The settings are defined in: include/power/sandbox_pmic.h
341 */
342static const struct setting expected_setting_list[] = {
343 [0] = { /* LDO1 */
344 .voltage = SANDBOX_LDO1_AUTOSET_EXPECTED_UV,
345 .current = SANDBOX_LDO1_AUTOSET_EXPECTED_UA,
346 .enable = SANDBOX_LDO1_AUTOSET_EXPECTED_ENABLE,
347 },
348 [1] = { /* LDO2 */
349 .voltage = SANDBOX_LDO2_AUTOSET_EXPECTED_UV,
350 .current = SANDBOX_LDO2_AUTOSET_EXPECTED_UA,
351 .enable = SANDBOX_LDO2_AUTOSET_EXPECTED_ENABLE,
352 },
353};
354
355static int list_count = ARRAY_SIZE(expected_setting_list);
356
357/* Test regulator list autoset method */
Joe Hershbergere721b882015-05-20 14:27:27 -0500358static int dm_test_power_regulator_autoset_list(struct unit_test_state *uts)
Przemyslaw Marczake8f339e2015-05-13 13:38:33 +0200359{
360 struct udevice *dev_list[2], *dev;
361 int i;
362
363 /*
364 * Test the settings of the regulator list:
365 * LDO1 with fdt properties:
366 * - min-microvolt = max-microvolt = 1800000
367 * - min-microamp = max-microamp = 100000
368 * - always-on = not set
369 * - boot-on = set
370 * Expected output state: uV=1800000; uA=100000; output enabled
371 *
372 * LDO2 with fdt properties:
373 * - min-microvolt = max-microvolt = 3300000
374 * - always-on = not set
375 * - boot-on = not set
376 * Expected output state: uV=300000(default); output disabled(default)
377 * The expected settings are defined in: include/power/sandbox_pmic.h.
378 */
379 ut_assertok(regulator_list_autoset(platname_list, dev_list, false));
380
381 for (i = 0; i < list_count; i++) {
382 /* Check, that the returned device is non-NULL */
383 ut_assert(dev_list[i]);
384
385 /* Check, that the returned device is proper */
386 ut_assertok(regulator_get_by_platname(platname_list[i], &dev));
387 ut_asserteq_ptr(dev_list[i], dev);
388
389 /* Check, that regulator output Voltage value is as expected */
390 ut_asserteq(regulator_get_value(dev_list[i]),
391 expected_setting_list[i].voltage);
392
393 /* Check, that regulator output Current value is as expected */
394 ut_asserteq(regulator_get_current(dev_list[i]),
395 expected_setting_list[i].current);
396
397 /* Check, that regulator output Enable state is as expected */
398 ut_asserteq(regulator_get_enable(dev_list[i]),
399 expected_setting_list[i].enable);
400 }
401
402 return 0;
403}
Simon Glasse180c2b2020-07-28 19:41:12 -0600404DM_TEST(dm_test_power_regulator_autoset_list, UT_TESTF_SCAN_FDT);