1
2
3
4
5
6
7
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>
16#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>
22#include <test/ut.h>
23
24enum {
25 BUCK1,
26 BUCK2,
27 BUCK3,
28 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
41 { SANDBOX_BUCK1_DEVNAME, SANDBOX_BUCK1_PLATNAME },
42 { SANDBOX_BUCK2_DEVNAME, SANDBOX_BUCK2_PLATNAME },
43 { SANDBOX_BUCK3_DEVNAME, SANDBOX_BUCK3_PLATNAME },
44 { SANDBOX_LDO1_DEVNAME, SANDBOX_LDO1_PLATNAME},
45 { SANDBOX_LDO2_DEVNAME, SANDBOX_LDO2_PLATNAME},
46};
47
48
49static int dm_test_power_regulator_get(struct unit_test_state *uts)
50{
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
61
62
63 devname = regulator_names[i][DEVNAME];
64 platname = regulator_names[i][PLATNAME];
65
66
67
68
69
70 ut_assertok(regulator_get_by_devname(devname, &dev_by_devname));
71 ut_asserteq_str(devname, dev_by_devname->name);
72
73
74
75
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
84
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
94static int dm_test_power_regulator_set_get_voltage(struct unit_test_state *uts)
95{
96 struct dm_regulator_uclass_platdata *uc_pdata;
97 struct udevice *dev;
98 const char *platname;
99 int val_set, val_get;
100
101
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
121static int dm_test_power_regulator_set_get_current(struct unit_test_state *uts)
122{
123 struct dm_regulator_uclass_platdata *uc_pdata;
124 struct udevice *dev;
125 const char *platname;
126 int val_set, val_get;
127
128
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
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
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
160static int dm_test_power_regulator_set_get_enable(struct unit_test_state *uts)
161{
162 const char *platname;
163 struct udevice *dev;
164 bool val_set = true;
165
166
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
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
178
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
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
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
199
200static int dm_test_power_regulator_set_get_mode(struct unit_test_state *uts)
201{
202 const char *platname;
203 struct udevice *dev;
204 int val_set = LDO_OM_SLEEP;
205
206
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
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
218
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
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
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
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
267
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
275
276static int dm_test_power_regulator_autoset(struct unit_test_state *uts)
277{
278 const char *platname;
279 struct udevice *dev, *dev_autoset;
280
281
282
283
284
285
286
287
288
289 platname = regulator_names[BUCK1][PLATNAME];
290 ut_assertok(regulator_autoset_by_name(platname, &dev_autoset));
291
292
293 ut_assertok(regulator_get_by_platname(platname, &dev));
294 ut_asserteq_ptr(dev, dev_autoset);
295
296
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
310
311
312
313
314struct setting {
315 int voltage;
316 int current;
317 bool enable;
318};
319
320
321
322
323
324
325
326static const char *platname_list[] = {
327 SANDBOX_LDO1_PLATNAME,
328 SANDBOX_LDO2_PLATNAME,
329 NULL,
330};
331
332
333
334
335
336
337
338
339
340static const struct setting expected_setting_list[] = {
341 [0] = {
342 .voltage = SANDBOX_LDO1_AUTOSET_EXPECTED_UV,
343 .current = SANDBOX_LDO1_AUTOSET_EXPECTED_UA,
344 .enable = SANDBOX_LDO1_AUTOSET_EXPECTED_ENABLE,
345 },
346 [1] = {
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
356static int dm_test_power_regulator_autoset_list(struct unit_test_state *uts)
357{
358 struct udevice *dev_list[2], *dev;
359 int i;
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377 ut_assertok(regulator_list_autoset(platname_list, dev_list, false));
378
379 for (i = 0; i < list_count; i++) {
380
381 ut_assert(dev_list[i]);
382
383
384 ut_assertok(regulator_get_by_platname(platname_list[i], &dev));
385 ut_asserteq_ptr(dev_list[i], dev);
386
387
388 ut_asserteq(regulator_get_value(dev_list[i]),
389 expected_setting_list[i].voltage);
390
391
392 ut_asserteq(regulator_get_current(dev_list[i]),
393 expected_setting_list[i].current);
394
395
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);
403