blob: 7267a89b30cc94574c42ef1eed70e7788eeb846c [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Przemyslaw Marczak08edd002015-04-20 20:07:42 +02002/*
3 * Copyright (C) 2014-2015 Samsung Electronics
4 * Przemyslaw Marczak <p.marczak@samsung.com>
Przemyslaw Marczak08edd002015-04-20 20:07:42 +02005 */
Simon Glasseeb99de2017-05-18 20:09:34 -06006
Przemyslaw Marczak08edd002015-04-20 20:07:42 +02007#include <common.h>
Przemyslaw Marczak08edd002015-04-20 20:07:42 +02008#include <errno.h>
9#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Przemyslaw Marczak08edd002015-04-20 20:07:42 +020011#include <dm/uclass-internal.h>
Simon Glassdbd79542020-05-10 11:40:11 -060012#include <linux/delay.h>
Przemyslaw Marczak08edd002015-04-20 20:07:42 +020013#include <power/pmic.h>
14#include <power/regulator.h>
15
Przemyslaw Marczak08edd002015-04-20 20:07:42 +020016int regulator_mode(struct udevice *dev, struct dm_regulator_mode **modep)
17{
Simon Glass71fa5b42020-12-03 16:55:18 -070018 struct dm_regulator_uclass_plat *uc_pdata;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +020019
20 *modep = NULL;
21
Simon Glass71fa5b42020-12-03 16:55:18 -070022 uc_pdata = dev_get_uclass_plat(dev);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +020023 if (!uc_pdata)
24 return -ENXIO;
25
26 *modep = uc_pdata->mode;
27 return uc_pdata->mode_count;
28}
29
30int regulator_get_value(struct udevice *dev)
31{
32 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
33
34 if (!ops || !ops->get_value)
35 return -ENOSYS;
36
37 return ops->get_value(dev);
38}
39
Krzysztof Kozlowskie2fa3972019-03-06 19:37:54 +010040static void regulator_set_value_ramp_delay(struct udevice *dev, int old_uV,
41 int new_uV, unsigned int ramp_delay)
42{
43 int delay = DIV_ROUND_UP(abs(new_uV - old_uV), ramp_delay);
44
45 debug("regulator %s: delay %u us (%d uV -> %d uV)\n", dev->name, delay,
46 old_uV, new_uV);
47
48 udelay(delay);
49}
50
Przemyslaw Marczak08edd002015-04-20 20:07:42 +020051int regulator_set_value(struct udevice *dev, int uV)
52{
53 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
Simon Glass71fa5b42020-12-03 16:55:18 -070054 struct dm_regulator_uclass_plat *uc_pdata;
Krzysztof Kozlowskie2fa3972019-03-06 19:37:54 +010055 int ret, old_uV = uV, is_enabled = 0;
Keerthyc6e66692016-10-26 13:42:31 +053056
Simon Glass71fa5b42020-12-03 16:55:18 -070057 uc_pdata = dev_get_uclass_plat(dev);
Keerthyc6e66692016-10-26 13:42:31 +053058 if (uc_pdata->min_uV != -ENODATA && uV < uc_pdata->min_uV)
59 return -EINVAL;
60 if (uc_pdata->max_uV != -ENODATA && uV > uc_pdata->max_uV)
61 return -EINVAL;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +020062
63 if (!ops || !ops->set_value)
64 return -ENOSYS;
65
Krzysztof Kozlowskie2fa3972019-03-06 19:37:54 +010066 if (uc_pdata->ramp_delay) {
67 is_enabled = regulator_get_enable(dev);
68 old_uV = regulator_get_value(dev);
69 }
70
71 ret = ops->set_value(dev, uV);
72
73 if (!ret) {
74 if (uc_pdata->ramp_delay && old_uV > 0 && is_enabled)
75 regulator_set_value_ramp_delay(dev, old_uV, uV,
76 uc_pdata->ramp_delay);
77 }
78
79 return ret;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +020080}
81
Joseph Chenbb511322019-09-26 15:43:52 +080082int regulator_set_suspend_value(struct udevice *dev, int uV)
83{
84 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
Simon Glass71fa5b42020-12-03 16:55:18 -070085 struct dm_regulator_uclass_plat *uc_pdata;
Joseph Chenbb511322019-09-26 15:43:52 +080086
Simon Glass71fa5b42020-12-03 16:55:18 -070087 uc_pdata = dev_get_uclass_plat(dev);
Joseph Chenbb511322019-09-26 15:43:52 +080088 if (uc_pdata->min_uV != -ENODATA && uV < uc_pdata->min_uV)
89 return -EINVAL;
90 if (uc_pdata->max_uV != -ENODATA && uV > uc_pdata->max_uV)
91 return -EINVAL;
92
93 if (!ops->set_suspend_value)
94 return -ENOSYS;
95
96 return ops->set_suspend_value(dev, uV);
97}
98
99int regulator_get_suspend_value(struct udevice *dev)
100{
101 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
102
103 if (!ops->get_suspend_value)
104 return -ENOSYS;
105
106 return ops->get_suspend_value(dev);
107}
108
Keerthy162c02e2016-10-26 13:42:30 +0530109/*
110 * To be called with at most caution as there is no check
111 * before setting the actual voltage value.
112 */
113int regulator_set_value_force(struct udevice *dev, int uV)
114{
115 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
116
117 if (!ops || !ops->set_value)
118 return -ENOSYS;
119
120 return ops->set_value(dev, uV);
121}
122
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200123int regulator_get_current(struct udevice *dev)
124{
125 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
126
127 if (!ops || !ops->get_current)
128 return -ENOSYS;
129
130 return ops->get_current(dev);
131}
132
133int regulator_set_current(struct udevice *dev, int uA)
134{
135 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
Simon Glass71fa5b42020-12-03 16:55:18 -0700136 struct dm_regulator_uclass_plat *uc_pdata;
Keerthyce152be2016-10-26 13:42:32 +0530137
Simon Glass71fa5b42020-12-03 16:55:18 -0700138 uc_pdata = dev_get_uclass_plat(dev);
Keerthyce152be2016-10-26 13:42:32 +0530139 if (uc_pdata->min_uA != -ENODATA && uA < uc_pdata->min_uA)
140 return -EINVAL;
141 if (uc_pdata->max_uA != -ENODATA && uA > uc_pdata->max_uA)
142 return -EINVAL;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200143
144 if (!ops || !ops->set_current)
145 return -ENOSYS;
146
147 return ops->set_current(dev, uA);
148}
149
Keerthy23be7fb2017-06-13 09:53:45 +0530150int regulator_get_enable(struct udevice *dev)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200151{
152 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
153
154 if (!ops || !ops->get_enable)
155 return -ENOSYS;
156
157 return ops->get_enable(dev);
158}
159
160int regulator_set_enable(struct udevice *dev, bool enable)
161{
162 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
Simon Glass71fa5b42020-12-03 16:55:18 -0700163 struct dm_regulator_uclass_plat *uc_pdata;
Krzysztof Kozlowskie2fa3972019-03-06 19:37:54 +0100164 int ret, old_enable = 0;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200165
166 if (!ops || !ops->set_enable)
167 return -ENOSYS;
168
Simon Glass71fa5b42020-12-03 16:55:18 -0700169 uc_pdata = dev_get_uclass_plat(dev);
Patrick Delaunayd7585f22018-11-15 13:45:31 +0100170 if (!enable && uc_pdata->always_on)
Lokesh Vutlaf870da22019-01-11 15:15:50 +0530171 return -EACCES;
Patrick Delaunayd7585f22018-11-15 13:45:31 +0100172
Krzysztof Kozlowskie2fa3972019-03-06 19:37:54 +0100173 if (uc_pdata->ramp_delay)
174 old_enable = regulator_get_enable(dev);
175
176 ret = ops->set_enable(dev, enable);
177 if (!ret) {
178 if (uc_pdata->ramp_delay && !old_enable && enable) {
179 int uV = regulator_get_value(dev);
180
181 if (uV > 0) {
182 regulator_set_value_ramp_delay(dev, 0, uV,
183 uc_pdata->ramp_delay);
184 }
185 }
186 }
187
188 return ret;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200189}
190
Lokesh Vutla7106b782019-01-11 15:15:51 +0530191int regulator_set_enable_if_allowed(struct udevice *dev, bool enable)
192{
193 int ret;
194
195 ret = regulator_set_enable(dev, enable);
196 if (ret == -ENOSYS || ret == -EACCES)
197 return 0;
198
199 return ret;
200}
201
Joseph Chenbb511322019-09-26 15:43:52 +0800202int regulator_set_suspend_enable(struct udevice *dev, bool enable)
203{
204 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
205
206 if (!ops->set_suspend_enable)
207 return -ENOSYS;
208
209 return ops->set_suspend_enable(dev, enable);
210}
211
212int regulator_get_suspend_enable(struct udevice *dev)
213{
214 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
215
216 if (!ops->get_suspend_enable)
217 return -ENOSYS;
218
219 return ops->get_suspend_enable(dev);
220}
221
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200222int regulator_get_mode(struct udevice *dev)
223{
224 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
225
226 if (!ops || !ops->get_mode)
227 return -ENOSYS;
228
229 return ops->get_mode(dev);
230}
231
232int regulator_set_mode(struct udevice *dev, int mode)
233{
234 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
235
236 if (!ops || !ops->set_mode)
237 return -ENOSYS;
238
239 return ops->set_mode(dev, mode);
240}
241
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200242int regulator_get_by_platname(const char *plat_name, struct udevice **devp)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200243{
Simon Glass71fa5b42020-12-03 16:55:18 -0700244 struct dm_regulator_uclass_plat *uc_pdata;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200245 struct udevice *dev;
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200246 int ret;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200247
248 *devp = NULL;
249
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200250 for (ret = uclass_find_first_device(UCLASS_REGULATOR, &dev); dev;
251 ret = uclass_find_next_device(&dev)) {
Simon Glassfc3ebf12017-05-31 17:57:15 -0600252 if (ret) {
253 debug("regulator %s, ret=%d\n", dev->name, ret);
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200254 continue;
Simon Glassfc3ebf12017-05-31 17:57:15 -0600255 }
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200256
Simon Glass71fa5b42020-12-03 16:55:18 -0700257 uc_pdata = dev_get_uclass_plat(dev);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200258 if (!uc_pdata || strcmp(plat_name, uc_pdata->name))
259 continue;
260
261 return uclass_get_device_tail(dev, 0, devp);
262 }
263
Simon Glassfc3ebf12017-05-31 17:57:15 -0600264 debug("%s: can't find: %s, ret=%d\n", __func__, plat_name, ret);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200265
266 return -ENODEV;
267}
268
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200269int regulator_get_by_devname(const char *devname, struct udevice **devp)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200270{
271 return uclass_get_device_by_name(UCLASS_REGULATOR, devname, devp);
272}
273
Przemyslaw Marczakc9001032015-10-27 13:07:59 +0100274int device_get_supply_regulator(struct udevice *dev, const char *supply_name,
275 struct udevice **devp)
276{
277 return uclass_get_device_by_phandle(UCLASS_REGULATOR, dev,
278 supply_name, devp);
279}
280
Simon Glass46cb8242015-06-23 15:38:58 -0600281int regulator_autoset(struct udevice *dev)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200282{
Simon Glass71fa5b42020-12-03 16:55:18 -0700283 struct dm_regulator_uclass_plat *uc_pdata;
Simon Glass46cb8242015-06-23 15:38:58 -0600284 int ret = 0;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200285
Simon Glass71fa5b42020-12-03 16:55:18 -0700286 uc_pdata = dev_get_uclass_plat(dev);
Joseph Chenbb511322019-09-26 15:43:52 +0800287
288 ret = regulator_set_suspend_enable(dev, uc_pdata->suspend_on);
289 if (!ret && uc_pdata->suspend_on) {
290 ret = regulator_set_suspend_value(dev, uc_pdata->suspend_uV);
291 if (!ret)
292 return ret;
293 }
294
Simon Glass46cb8242015-06-23 15:38:58 -0600295 if (!uc_pdata->always_on && !uc_pdata->boot_on)
296 return -EMEDIUMTYPE;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200297
Sven Schwermerd033cbf2019-06-12 08:32:38 +0200298 if (uc_pdata->type == REGULATOR_TYPE_FIXED)
299 return regulator_set_enable(dev, true);
300
Simon Glass46cb8242015-06-23 15:38:58 -0600301 if (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UV)
302 ret = regulator_set_value(dev, uc_pdata->min_uV);
Joseph Chenbb511322019-09-26 15:43:52 +0800303 if (uc_pdata->init_uV > 0)
304 ret = regulator_set_value(dev, uc_pdata->init_uV);
Simon Glass46cb8242015-06-23 15:38:58 -0600305 if (!ret && (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UA))
306 ret = regulator_set_current(dev, uc_pdata->min_uA);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200307
308 if (!ret)
Simon Glass46cb8242015-06-23 15:38:58 -0600309 ret = regulator_set_enable(dev, true);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200310
311 return ret;
312}
313
Simon Glass46cb8242015-06-23 15:38:58 -0600314static void regulator_show(struct udevice *dev, int ret)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200315{
Simon Glass71fa5b42020-12-03 16:55:18 -0700316 struct dm_regulator_uclass_plat *uc_pdata;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200317
Simon Glass71fa5b42020-12-03 16:55:18 -0700318 uc_pdata = dev_get_uclass_plat(dev);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200319
Simon Glass46cb8242015-06-23 15:38:58 -0600320 printf("%s@%s: ", dev->name, uc_pdata->name);
321 if (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UV)
322 printf("set %d uV", uc_pdata->min_uV);
323 if (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UA)
324 printf("; set %d uA", uc_pdata->min_uA);
325 printf("; enabling");
326 if (ret)
Simon Glass46ad8cb2016-01-21 19:43:58 -0700327 printf(" (ret: %d)", ret);
Simon Glass46cb8242015-06-23 15:38:58 -0600328 printf("\n");
329}
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200330
Simon Glass46cb8242015-06-23 15:38:58 -0600331int regulator_autoset_by_name(const char *platname, struct udevice **devp)
332{
333 struct udevice *dev;
334 int ret;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200335
Simon Glass46cb8242015-06-23 15:38:58 -0600336 ret = regulator_get_by_platname(platname, &dev);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200337 if (devp)
338 *devp = dev;
Simon Glass46cb8242015-06-23 15:38:58 -0600339 if (ret) {
Simon Glassfc3ebf12017-05-31 17:57:15 -0600340 debug("Can get the regulator: %s (err=%d)\n", platname, ret);
Simon Glass46cb8242015-06-23 15:38:58 -0600341 return ret;
342 }
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200343
Simon Glass46cb8242015-06-23 15:38:58 -0600344 return regulator_autoset(dev);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200345}
346
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200347int regulator_list_autoset(const char *list_platname[],
348 struct udevice *list_devp[],
349 bool verbose)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200350{
351 struct udevice *dev;
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200352 int error = 0, i = 0, ret;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200353
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200354 while (list_platname[i]) {
Simon Glass46cb8242015-06-23 15:38:58 -0600355 ret = regulator_autoset_by_name(list_platname[i], &dev);
356 if (ret != -EMEDIUMTYPE && verbose)
357 regulator_show(dev, ret);
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200358 if (ret & !error)
359 error = ret;
360
361 if (list_devp)
362 list_devp[i] = dev;
363
364 i++;
365 }
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200366
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200367 return error;
368}
369
370static bool regulator_name_is_unique(struct udevice *check_dev,
371 const char *check_name)
372{
Simon Glass71fa5b42020-12-03 16:55:18 -0700373 struct dm_regulator_uclass_plat *uc_pdata;
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200374 struct udevice *dev;
375 int check_len = strlen(check_name);
376 int ret;
377 int len;
378
379 for (ret = uclass_find_first_device(UCLASS_REGULATOR, &dev); dev;
380 ret = uclass_find_next_device(&dev)) {
381 if (ret || dev == check_dev)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200382 continue;
383
Simon Glass71fa5b42020-12-03 16:55:18 -0700384 uc_pdata = dev_get_uclass_plat(dev);
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200385 len = strlen(uc_pdata->name);
386 if (len != check_len)
387 continue;
388
389 if (!strcmp(uc_pdata->name, check_name))
390 return false;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200391 }
392
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200393 return true;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200394}
395
396static int regulator_post_bind(struct udevice *dev)
397{
Simon Glass71fa5b42020-12-03 16:55:18 -0700398 struct dm_regulator_uclass_plat *uc_pdata;
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200399 const char *property = "regulator-name";
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200400
Simon Glass71fa5b42020-12-03 16:55:18 -0700401 uc_pdata = dev_get_uclass_plat(dev);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200402
403 /* Regulator's mandatory constraint */
Simon Glasseeb99de2017-05-18 20:09:34 -0600404 uc_pdata->name = dev_read_string(dev, property);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200405 if (!uc_pdata->name) {
Simon Glasseeb99de2017-05-18 20:09:34 -0600406 debug("%s: dev '%s' has no property '%s'\n",
407 __func__, dev->name, property);
408 uc_pdata->name = dev_read_name(dev);
Peng Fancd672d42015-08-07 16:43:42 +0800409 if (!uc_pdata->name)
410 return -EINVAL;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200411 }
412
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200413 if (regulator_name_is_unique(dev, uc_pdata->name))
414 return 0;
415
Simon Glasseeb99de2017-05-18 20:09:34 -0600416 debug("'%s' of dev: '%s', has nonunique value: '%s\n",
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200417 property, dev->name, uc_pdata->name);
418
419 return -EINVAL;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200420}
421
422static int regulator_pre_probe(struct udevice *dev)
423{
Simon Glass71fa5b42020-12-03 16:55:18 -0700424 struct dm_regulator_uclass_plat *uc_pdata;
Joseph Chenbb511322019-09-26 15:43:52 +0800425 ofnode node;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200426
Simon Glass71fa5b42020-12-03 16:55:18 -0700427 uc_pdata = dev_get_uclass_plat(dev);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200428 if (!uc_pdata)
429 return -ENXIO;
430
431 /* Regulator's optional constraints */
Simon Glasseeb99de2017-05-18 20:09:34 -0600432 uc_pdata->min_uV = dev_read_u32_default(dev, "regulator-min-microvolt",
433 -ENODATA);
434 uc_pdata->max_uV = dev_read_u32_default(dev, "regulator-max-microvolt",
435 -ENODATA);
Joseph Chenbb511322019-09-26 15:43:52 +0800436 uc_pdata->init_uV = dev_read_u32_default(dev, "regulator-init-microvolt",
437 -ENODATA);
Simon Glasseeb99de2017-05-18 20:09:34 -0600438 uc_pdata->min_uA = dev_read_u32_default(dev, "regulator-min-microamp",
439 -ENODATA);
440 uc_pdata->max_uA = dev_read_u32_default(dev, "regulator-max-microamp",
441 -ENODATA);
442 uc_pdata->always_on = dev_read_bool(dev, "regulator-always-on");
443 uc_pdata->boot_on = dev_read_bool(dev, "regulator-boot-on");
Krzysztof Kozlowskie2fa3972019-03-06 19:37:54 +0100444 uc_pdata->ramp_delay = dev_read_u32_default(dev, "regulator-ramp-delay",
445 0);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200446
Joseph Chenbb511322019-09-26 15:43:52 +0800447 node = dev_read_subnode(dev, "regulator-state-mem");
448 if (ofnode_valid(node)) {
449 uc_pdata->suspend_on = !ofnode_read_bool(node, "regulator-off-in-suspend");
450 if (ofnode_read_u32(node, "regulator-suspend-microvolt", &uc_pdata->suspend_uV))
451 uc_pdata->suspend_uV = uc_pdata->max_uV;
452 } else {
453 uc_pdata->suspend_on = true;
454 uc_pdata->suspend_uV = uc_pdata->max_uV;
455 }
456
Simon Glass991f9bc2015-06-23 15:38:57 -0600457 /* Those values are optional (-ENODATA if unset) */
458 if ((uc_pdata->min_uV != -ENODATA) &&
459 (uc_pdata->max_uV != -ENODATA) &&
460 (uc_pdata->min_uV == uc_pdata->max_uV))
461 uc_pdata->flags |= REGULATOR_FLAG_AUTOSET_UV;
462
463 /* Those values are optional (-ENODATA if unset) */
464 if ((uc_pdata->min_uA != -ENODATA) &&
465 (uc_pdata->max_uA != -ENODATA) &&
466 (uc_pdata->min_uA == uc_pdata->max_uA))
467 uc_pdata->flags |= REGULATOR_FLAG_AUTOSET_UA;
468
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200469 return 0;
470}
471
Simon Glassefeeb2c2015-06-23 15:38:59 -0600472int regulators_enable_boot_on(bool verbose)
473{
474 struct udevice *dev;
475 struct uclass *uc;
476 int ret;
477
478 ret = uclass_get(UCLASS_REGULATOR, &uc);
479 if (ret)
480 return ret;
481 for (uclass_first_device(UCLASS_REGULATOR, &dev);
Simon Glassc7298e72016-02-11 13:23:26 -0700482 dev;
Simon Glassefeeb2c2015-06-23 15:38:59 -0600483 uclass_next_device(&dev)) {
484 ret = regulator_autoset(dev);
Simon Glassf55c9512015-07-02 18:16:06 -0600485 if (ret == -EMEDIUMTYPE) {
486 ret = 0;
Simon Glassefeeb2c2015-06-23 15:38:59 -0600487 continue;
Simon Glassf55c9512015-07-02 18:16:06 -0600488 }
Simon Glassefeeb2c2015-06-23 15:38:59 -0600489 if (verbose)
490 regulator_show(dev, ret);
Simon Glassd6eddad2016-01-21 19:43:59 -0700491 if (ret == -ENOSYS)
492 ret = 0;
Simon Glassefeeb2c2015-06-23 15:38:59 -0600493 }
494
495 return ret;
496}
497
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200498UCLASS_DRIVER(regulator) = {
499 .id = UCLASS_REGULATOR,
500 .name = "regulator",
501 .post_bind = regulator_post_bind,
502 .pre_probe = regulator_pre_probe,
Simon Glass71fa5b42020-12-03 16:55:18 -0700503 .per_device_plat_auto =
504 sizeof(struct dm_regulator_uclass_plat),
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200505};