blob: b967902493dd3a0005ae1eaf2dfc394f851edb9c [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>
13#include <malloc.h>
14#include <dm/device-internal.h>
15#include <dm/root.h>
Przemyslaw Marczake8f339e2015-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>
Joe Hershbergere721b882015-05-20 14:27:27 -050022#include <test/ut.h>
Przemyslaw Marczake8f339e2015-05-13 13:38:33 +020023
Przemyslaw Marczake8f339e2015-05-13 13:38:33 +020024enum {
25 BUCK1,
26 BUCK2,
Felix Brackbf802f52017-11-27 09:14:16 +010027 BUCK3,
Przemyslaw Marczake8f339e2015-05-13 13:38:33 +020028 LDO1,
29 LDO2,
30 OUTPUT_COUNT,
31};
32
33enum {
34 DEVNAME = 0,
35 PLATNAME,
36 OUTPUT_NAME_COUNT,
37};
38
39static const char *regulator_names[OUTPUT_COUNT][OUTPUT_NAME_COUNT] = {
40 /* devname, platname */
41 { SANDBOX_BUCK1_DEVNAME, SANDBOX_BUCK1_PLATNAME },
42 { SANDBOX_BUCK2_DEVNAME, SANDBOX_BUCK2_PLATNAME },
Felix Brackbf802f52017-11-27 09:14:16 +010043 { SANDBOX_BUCK3_DEVNAME, SANDBOX_BUCK3_PLATNAME },
Przemyslaw Marczake8f339e2015-05-13 13:38:33 +020044 { SANDBOX_LDO1_DEVNAME, SANDBOX_LDO1_PLATNAME},
45 { SANDBOX_LDO2_DEVNAME, SANDBOX_LDO2_PLATNAME},
46};
47
48/* Test regulator get method */
Joe Hershbergere721b882015-05-20 14:27:27 -050049static int dm_test_power_regulator_get(struct unit_test_state *uts)
Przemyslaw Marczake8f339e2015-05-13 13:38:33 +020050{
51 struct dm_regulator_uclass_platdata *uc_pdata;
52 struct udevice *dev_by_devname;
53 struct udevice *dev_by_platname;
54 const char *devname;
55 const char *platname;
56 int i;
57
58 for (i = 0; i < OUTPUT_COUNT; i++) {
59 /*
60 * Do the test for each regulator's devname and platname,
61 * which are related to a single device.
62 */
63 devname = regulator_names[i][DEVNAME];
64 platname = regulator_names[i][PLATNAME];
65
66 /*
67 * Check, that regulator_get_by_devname() function, returns
68 * a device with the name equal to the requested one.
69 */
70 ut_assertok(regulator_get_by_devname(devname, &dev_by_devname));
71 ut_asserteq_str(devname, dev_by_devname->name);
72
73 /*
74 * Check, that regulator_get_by_platname() function, returns
75 * a device with the name equal to the requested one.
76 */
77 ut_assertok(regulator_get_by_platname(platname, &dev_by_platname));
78 uc_pdata = dev_get_uclass_platdata(dev_by_platname);
79 ut_assert(uc_pdata);
80 ut_asserteq_str(platname, uc_pdata->name);
81
82 /*
83 * Check, that the pointers returned by both get functions,
84 * points to the same regulator device.
85 */
86 ut_asserteq_ptr(dev_by_devname, dev_by_platname);
87 }
88
89 return 0;
90}
91DM_TEST(dm_test_power_regulator_get, DM_TESTF_SCAN_FDT);
92
93/* Test regulator set and get Voltage method */
Joe Hershbergere721b882015-05-20 14:27:27 -050094static int dm_test_power_regulator_set_get_voltage(struct unit_test_state *uts)
Przemyslaw Marczake8f339e2015-05-13 13:38:33 +020095{
96 struct dm_regulator_uclass_platdata *uc_pdata;
97 struct udevice *dev;
98 const char *platname;
99 int val_set, val_get;
100
101 /* Set and get Voltage of BUCK1 - set to 'min' constraint */
102 platname = regulator_names[BUCK1][PLATNAME];
103 ut_assertok(regulator_get_by_platname(platname, &dev));
104
105 uc_pdata = dev_get_uclass_platdata(dev);
106 ut_assert(uc_pdata);
107
108 val_set = uc_pdata->min_uV;
109 ut_assertok(regulator_set_value(dev, val_set));
110
111 val_get = regulator_get_value(dev);
112 ut_assert(val_get >= 0);
113
114 ut_asserteq(val_set, val_get);
115
116 return 0;
117}
118DM_TEST(dm_test_power_regulator_set_get_voltage, DM_TESTF_SCAN_FDT);
119
120/* Test regulator set and get Current method */
Joe Hershbergere721b882015-05-20 14:27:27 -0500121static int dm_test_power_regulator_set_get_current(struct unit_test_state *uts)
Przemyslaw Marczake8f339e2015-05-13 13:38:33 +0200122{
123 struct dm_regulator_uclass_platdata *uc_pdata;
124 struct udevice *dev;
125 const char *platname;
126 int val_set, val_get;
127
128 /* Set and get the Current of LDO1 - set to 'min' constraint */
129 platname = regulator_names[LDO1][PLATNAME];
130 ut_assertok(regulator_get_by_platname(platname, &dev));
131
132 uc_pdata = dev_get_uclass_platdata(dev);
133 ut_assert(uc_pdata);
134
135 val_set = uc_pdata->min_uA;
136 ut_assertok(regulator_set_current(dev, val_set));
137
138 val_get = regulator_get_current(dev);
139 ut_assert(val_get >= 0);
140
141 ut_asserteq(val_set, val_get);
142
143 /* Check LDO2 current limit constraints - should be -ENODATA */
144 platname = regulator_names[LDO2][PLATNAME];
145 ut_assertok(regulator_get_by_platname(platname, &dev));
146
147 uc_pdata = dev_get_uclass_platdata(dev);
148 ut_assert(uc_pdata);
149 ut_asserteq(-ENODATA, uc_pdata->min_uA);
150 ut_asserteq(-ENODATA, uc_pdata->max_uA);
151
152 /* Try set the Current of LDO2 - should return -ENOSYS */
153 ut_asserteq(-ENOSYS, regulator_set_current(dev, 0));
154
155 return 0;
156}
157DM_TEST(dm_test_power_regulator_set_get_current, DM_TESTF_SCAN_FDT);
158
159/* Test regulator set and get Enable method */
Joe Hershbergere721b882015-05-20 14:27:27 -0500160static int dm_test_power_regulator_set_get_enable(struct unit_test_state *uts)
Przemyslaw Marczake8f339e2015-05-13 13:38:33 +0200161{
162 const char *platname;
163 struct udevice *dev;
164 bool val_set = true;
165
166 /* Set the Enable of LDO1 - default is disabled */
167 platname = regulator_names[LDO1][PLATNAME];
168 ut_assertok(regulator_get_by_platname(platname, &dev));
169 ut_assertok(regulator_set_enable(dev, val_set));
170
171 /* Get the Enable state of LDO1 and compare it with the requested one */
172 ut_asserteq(regulator_get_enable(dev), val_set);
173
174 return 0;
175}
176DM_TEST(dm_test_power_regulator_set_get_enable, DM_TESTF_SCAN_FDT);
177
Lokesh Vutlacc4a2242019-01-11 15:15:51 +0530178/* Test regulator set and get enable if allowed method */
179static
180int dm_test_power_regulator_set_enable_if_allowed(struct unit_test_state *uts)
181{
182 const char *platname;
183 struct udevice *dev, *dev_autoset;
184 bool val_set = false;
185
186 /* Get BUCK1 - always on regulator */
187 platname = regulator_names[BUCK1][PLATNAME];
188 ut_assertok(regulator_autoset_by_name(platname, &dev_autoset));
189 ut_assertok(regulator_get_by_platname(platname, &dev));
190
191 /* Try disabling always-on regulator */
192 ut_assertok(regulator_set_enable_if_allowed(dev, val_set));
193 ut_asserteq(regulator_get_enable(dev), !val_set);
194
195 return 0;
196}
197DM_TEST(dm_test_power_regulator_set_enable_if_allowed, DM_TESTF_SCAN_FDT);
198
Przemyslaw Marczake8f339e2015-05-13 13:38:33 +0200199/* Test regulator set and get mode method */
Joe Hershbergere721b882015-05-20 14:27:27 -0500200static int dm_test_power_regulator_set_get_mode(struct unit_test_state *uts)
Przemyslaw Marczake8f339e2015-05-13 13:38:33 +0200201{
202 const char *platname;
203 struct udevice *dev;
204 int val_set = LDO_OM_SLEEP;
205
206 /* Set the mode id to LDO_OM_SLEEP of LDO1 - default is LDO_OM_OFF */
207 platname = regulator_names[LDO1][PLATNAME];
208 ut_assertok(regulator_get_by_platname(platname, &dev));
209 ut_assertok(regulator_set_mode(dev, val_set));
210
211 /* Get the mode id of LDO1 and compare it with the requested one */
212 ut_asserteq(regulator_get_mode(dev), val_set);
213
214 return 0;
215}
216DM_TEST(dm_test_power_regulator_set_get_mode, DM_TESTF_SCAN_FDT);
217
Joseph Chen11406b82019-09-26 15:43:52 +0800218/* Test regulator set and get suspend Voltage method */
219static int dm_test_power_regulator_set_get_suspend_voltage(struct unit_test_state *uts)
220{
221 struct dm_regulator_uclass_platdata *uc_pdata;
222 const struct dm_regulator_ops *ops;
223 struct udevice *dev;
224 const char *platname;
225 int val_set, val_get;
226
227 /* Set and get Voltage of BUCK1 - set to 'min' constraint */
228 platname = regulator_names[BUCK1][PLATNAME];
229 ut_assertok(regulator_get_by_platname(platname, &dev));
230
231 uc_pdata = dev_get_uclass_platdata(dev);
232 ut_assert(uc_pdata);
233
234 ops = dev_get_driver_ops(dev);
235
236 if (ops->set_suspend_value && ops->get_suspend_value) {
237 val_set = uc_pdata->suspend_uV;
238 ut_assertok(regulator_set_suspend_value(dev, val_set));
239 val_get = regulator_get_suspend_value(dev);
240 ut_assert(val_get >= 0);
241
242 ut_asserteq(val_set, val_get);
243 }
244 return 0;
245}
246DM_TEST(dm_test_power_regulator_set_get_suspend_voltage, DM_TESTF_SCAN_FDT);
247
248/* Test regulator set and get suspend Enable method */
249static int dm_test_power_regulator_set_get_suspend_enable(struct unit_test_state *uts)
250{
251 const struct dm_regulator_ops *ops;
252 const char *platname;
253 struct udevice *dev;
254 bool val_set = true;
255
256 /* Set the Enable of LDO1 - default is disabled */
257 platname = regulator_names[LDO1][PLATNAME];
258 ut_assertok(regulator_get_by_platname(platname, &dev));
259
260 ops = dev_get_driver_ops(dev);
261
262 if (ops->set_suspend_enable && ops->get_suspend_enable) {
263 ut_assertok(regulator_set_suspend_enable(dev, val_set));
264
265 /*
266 * Get the Enable state of LDO1 and
267 * compare it with the requested one
268 */
269 ut_asserteq(regulator_get_suspend_enable(dev), val_set);
270 }
271 return 0;
272}
273DM_TEST(dm_test_power_regulator_set_get_suspend_enable, DM_TESTF_SCAN_FDT);
274
Przemyslaw Marczake8f339e2015-05-13 13:38:33 +0200275/* Test regulator autoset method */
Joe Hershbergere721b882015-05-20 14:27:27 -0500276static int dm_test_power_regulator_autoset(struct unit_test_state *uts)
Przemyslaw Marczake8f339e2015-05-13 13:38:33 +0200277{
278 const char *platname;
279 struct udevice *dev, *dev_autoset;
280
281 /*
282 * Test the BUCK1 with fdt properties
283 * - min-microvolt = max-microvolt = 1200000
284 * - min-microamp = max-microamp = 200000
285 * - always-on = set
286 * - boot-on = not set
287 * Expected output state: uV=1200000; uA=200000; output enabled
288 */
289 platname = regulator_names[BUCK1][PLATNAME];
Simon Glass3b55d302015-06-23 15:38:58 -0600290 ut_assertok(regulator_autoset_by_name(platname, &dev_autoset));
Przemyslaw Marczake8f339e2015-05-13 13:38:33 +0200291
292 /* Check, that the returned device is proper */
293 ut_assertok(regulator_get_by_platname(platname, &dev));
294 ut_asserteq_ptr(dev, dev_autoset);
295
296 /* Check the setup after autoset */
297 ut_asserteq(regulator_get_value(dev),
298 SANDBOX_BUCK1_AUTOSET_EXPECTED_UV);
299 ut_asserteq(regulator_get_current(dev),
300 SANDBOX_BUCK1_AUTOSET_EXPECTED_UA);
301 ut_asserteq(regulator_get_enable(dev),
302 SANDBOX_BUCK1_AUTOSET_EXPECTED_ENABLE);
303
304 return 0;
305}
306DM_TEST(dm_test_power_regulator_autoset, DM_TESTF_SCAN_FDT);
307
308/*
309 * Struct setting: to keep the expected output settings.
310 * @voltage: Voltage value [uV]
311 * @current: Current value [uA]
312 * @enable: output enable state: true/false
313 */
314struct setting {
315 int voltage;
316 int current;
317 bool enable;
318};
319
320/*
321 * platname_list: an array of regulator platform names.
322 * For testing regulator_list_autoset() for outputs:
323 * - LDO1
324 * - LDO2
325 */
326static const char *platname_list[] = {
327 SANDBOX_LDO1_PLATNAME,
328 SANDBOX_LDO2_PLATNAME,
329 NULL,
330};
331
332/*
333 * expected_setting_list: an array of regulator output setting, expected after
334 * call of the regulator_list_autoset() for the "platname_list" array.
335 * For testing results of regulator_list_autoset() for outputs:
336 * - LDO1
337 * - LDO2
338 * The settings are defined in: include/power/sandbox_pmic.h
339 */
340static const struct setting expected_setting_list[] = {
341 [0] = { /* LDO1 */
342 .voltage = SANDBOX_LDO1_AUTOSET_EXPECTED_UV,
343 .current = SANDBOX_LDO1_AUTOSET_EXPECTED_UA,
344 .enable = SANDBOX_LDO1_AUTOSET_EXPECTED_ENABLE,
345 },
346 [1] = { /* LDO2 */
347 .voltage = SANDBOX_LDO2_AUTOSET_EXPECTED_UV,
348 .current = SANDBOX_LDO2_AUTOSET_EXPECTED_UA,
349 .enable = SANDBOX_LDO2_AUTOSET_EXPECTED_ENABLE,
350 },
351};
352
353static int list_count = ARRAY_SIZE(expected_setting_list);
354
355/* Test regulator list autoset method */
Joe Hershbergere721b882015-05-20 14:27:27 -0500356static int dm_test_power_regulator_autoset_list(struct unit_test_state *uts)
Przemyslaw Marczake8f339e2015-05-13 13:38:33 +0200357{
358 struct udevice *dev_list[2], *dev;
359 int i;
360
361 /*
362 * Test the settings of the regulator list:
363 * LDO1 with fdt properties:
364 * - min-microvolt = max-microvolt = 1800000
365 * - min-microamp = max-microamp = 100000
366 * - always-on = not set
367 * - boot-on = set
368 * Expected output state: uV=1800000; uA=100000; output enabled
369 *
370 * LDO2 with fdt properties:
371 * - min-microvolt = max-microvolt = 3300000
372 * - always-on = not set
373 * - boot-on = not set
374 * Expected output state: uV=300000(default); output disabled(default)
375 * The expected settings are defined in: include/power/sandbox_pmic.h.
376 */
377 ut_assertok(regulator_list_autoset(platname_list, dev_list, false));
378
379 for (i = 0; i < list_count; i++) {
380 /* Check, that the returned device is non-NULL */
381 ut_assert(dev_list[i]);
382
383 /* Check, that the returned device is proper */
384 ut_assertok(regulator_get_by_platname(platname_list[i], &dev));
385 ut_asserteq_ptr(dev_list[i], dev);
386
387 /* Check, that regulator output Voltage value is as expected */
388 ut_asserteq(regulator_get_value(dev_list[i]),
389 expected_setting_list[i].voltage);
390
391 /* Check, that regulator output Current value is as expected */
392 ut_asserteq(regulator_get_current(dev_list[i]),
393 expected_setting_list[i].current);
394
395 /* Check, that regulator output Enable state is as expected */
396 ut_asserteq(regulator_get_enable(dev_list[i]),
397 expected_setting_list[i].enable);
398 }
399
400 return 0;
401}
402DM_TEST(dm_test_power_regulator_autoset_list, DM_TESTF_SCAN_FDT);