blob: fac960682331f9045c96f2a4393817aa55508435 [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
Konstantin Porotchkin621dab52017-05-29 15:59:38 +0300314int regulator_unset(struct udevice *dev)
315{
316 struct dm_regulator_uclass_plat *uc_pdata;
317
318 uc_pdata = dev_get_uclass_plat(dev);
319 if (uc_pdata && uc_pdata->force_off)
320 return regulator_set_enable(dev, false);
321
322 return -EMEDIUMTYPE;
323}
324
Simon Glass46cb8242015-06-23 15:38:58 -0600325static void regulator_show(struct udevice *dev, int ret)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200326{
Simon Glass71fa5b42020-12-03 16:55:18 -0700327 struct dm_regulator_uclass_plat *uc_pdata;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200328
Simon Glass71fa5b42020-12-03 16:55:18 -0700329 uc_pdata = dev_get_uclass_plat(dev);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200330
Simon Glass46cb8242015-06-23 15:38:58 -0600331 printf("%s@%s: ", dev->name, uc_pdata->name);
332 if (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UV)
333 printf("set %d uV", uc_pdata->min_uV);
334 if (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UA)
335 printf("; set %d uA", uc_pdata->min_uA);
336 printf("; enabling");
337 if (ret)
Simon Glass46ad8cb2016-01-21 19:43:58 -0700338 printf(" (ret: %d)", ret);
Simon Glass46cb8242015-06-23 15:38:58 -0600339 printf("\n");
340}
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200341
Simon Glass46cb8242015-06-23 15:38:58 -0600342int regulator_autoset_by_name(const char *platname, struct udevice **devp)
343{
344 struct udevice *dev;
345 int ret;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200346
Simon Glass46cb8242015-06-23 15:38:58 -0600347 ret = regulator_get_by_platname(platname, &dev);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200348 if (devp)
349 *devp = dev;
Simon Glass46cb8242015-06-23 15:38:58 -0600350 if (ret) {
Simon Glassfc3ebf12017-05-31 17:57:15 -0600351 debug("Can get the regulator: %s (err=%d)\n", platname, ret);
Simon Glass46cb8242015-06-23 15:38:58 -0600352 return ret;
353 }
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200354
Simon Glass46cb8242015-06-23 15:38:58 -0600355 return regulator_autoset(dev);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200356}
357
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200358int regulator_list_autoset(const char *list_platname[],
359 struct udevice *list_devp[],
360 bool verbose)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200361{
362 struct udevice *dev;
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200363 int error = 0, i = 0, ret;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200364
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200365 while (list_platname[i]) {
Simon Glass46cb8242015-06-23 15:38:58 -0600366 ret = regulator_autoset_by_name(list_platname[i], &dev);
367 if (ret != -EMEDIUMTYPE && verbose)
368 regulator_show(dev, ret);
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200369 if (ret & !error)
370 error = ret;
371
372 if (list_devp)
373 list_devp[i] = dev;
374
375 i++;
376 }
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200377
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200378 return error;
379}
380
381static bool regulator_name_is_unique(struct udevice *check_dev,
382 const char *check_name)
383{
Simon Glass71fa5b42020-12-03 16:55:18 -0700384 struct dm_regulator_uclass_plat *uc_pdata;
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200385 struct udevice *dev;
386 int check_len = strlen(check_name);
387 int ret;
388 int len;
389
390 for (ret = uclass_find_first_device(UCLASS_REGULATOR, &dev); dev;
391 ret = uclass_find_next_device(&dev)) {
392 if (ret || dev == check_dev)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200393 continue;
394
Simon Glass71fa5b42020-12-03 16:55:18 -0700395 uc_pdata = dev_get_uclass_plat(dev);
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200396 len = strlen(uc_pdata->name);
397 if (len != check_len)
398 continue;
399
400 if (!strcmp(uc_pdata->name, check_name))
401 return false;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200402 }
403
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200404 return true;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200405}
406
407static int regulator_post_bind(struct udevice *dev)
408{
Simon Glass71fa5b42020-12-03 16:55:18 -0700409 struct dm_regulator_uclass_plat *uc_pdata;
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200410 const char *property = "regulator-name";
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200411
Simon Glass71fa5b42020-12-03 16:55:18 -0700412 uc_pdata = dev_get_uclass_plat(dev);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200413
414 /* Regulator's mandatory constraint */
Simon Glasseeb99de2017-05-18 20:09:34 -0600415 uc_pdata->name = dev_read_string(dev, property);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200416 if (!uc_pdata->name) {
Simon Glasseeb99de2017-05-18 20:09:34 -0600417 debug("%s: dev '%s' has no property '%s'\n",
418 __func__, dev->name, property);
419 uc_pdata->name = dev_read_name(dev);
Peng Fancd672d42015-08-07 16:43:42 +0800420 if (!uc_pdata->name)
421 return -EINVAL;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200422 }
423
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200424 if (regulator_name_is_unique(dev, uc_pdata->name))
425 return 0;
426
Simon Glasseeb99de2017-05-18 20:09:34 -0600427 debug("'%s' of dev: '%s', has nonunique value: '%s\n",
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200428 property, dev->name, uc_pdata->name);
429
430 return -EINVAL;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200431}
432
433static int regulator_pre_probe(struct udevice *dev)
434{
Simon Glass71fa5b42020-12-03 16:55:18 -0700435 struct dm_regulator_uclass_plat *uc_pdata;
Joseph Chenbb511322019-09-26 15:43:52 +0800436 ofnode node;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200437
Simon Glass71fa5b42020-12-03 16:55:18 -0700438 uc_pdata = dev_get_uclass_plat(dev);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200439 if (!uc_pdata)
440 return -ENXIO;
441
442 /* Regulator's optional constraints */
Simon Glasseeb99de2017-05-18 20:09:34 -0600443 uc_pdata->min_uV = dev_read_u32_default(dev, "regulator-min-microvolt",
444 -ENODATA);
445 uc_pdata->max_uV = dev_read_u32_default(dev, "regulator-max-microvolt",
446 -ENODATA);
Joseph Chenbb511322019-09-26 15:43:52 +0800447 uc_pdata->init_uV = dev_read_u32_default(dev, "regulator-init-microvolt",
448 -ENODATA);
Simon Glasseeb99de2017-05-18 20:09:34 -0600449 uc_pdata->min_uA = dev_read_u32_default(dev, "regulator-min-microamp",
450 -ENODATA);
451 uc_pdata->max_uA = dev_read_u32_default(dev, "regulator-max-microamp",
452 -ENODATA);
453 uc_pdata->always_on = dev_read_bool(dev, "regulator-always-on");
454 uc_pdata->boot_on = dev_read_bool(dev, "regulator-boot-on");
Krzysztof Kozlowskie2fa3972019-03-06 19:37:54 +0100455 uc_pdata->ramp_delay = dev_read_u32_default(dev, "regulator-ramp-delay",
456 0);
Konstantin Porotchkin621dab52017-05-29 15:59:38 +0300457 uc_pdata->force_off = dev_read_bool(dev, "regulator-force-boot-off");
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200458
Joseph Chenbb511322019-09-26 15:43:52 +0800459 node = dev_read_subnode(dev, "regulator-state-mem");
460 if (ofnode_valid(node)) {
461 uc_pdata->suspend_on = !ofnode_read_bool(node, "regulator-off-in-suspend");
462 if (ofnode_read_u32(node, "regulator-suspend-microvolt", &uc_pdata->suspend_uV))
463 uc_pdata->suspend_uV = uc_pdata->max_uV;
464 } else {
465 uc_pdata->suspend_on = true;
466 uc_pdata->suspend_uV = uc_pdata->max_uV;
467 }
468
Simon Glass991f9bc2015-06-23 15:38:57 -0600469 /* Those values are optional (-ENODATA if unset) */
470 if ((uc_pdata->min_uV != -ENODATA) &&
471 (uc_pdata->max_uV != -ENODATA) &&
472 (uc_pdata->min_uV == uc_pdata->max_uV))
473 uc_pdata->flags |= REGULATOR_FLAG_AUTOSET_UV;
474
475 /* Those values are optional (-ENODATA if unset) */
476 if ((uc_pdata->min_uA != -ENODATA) &&
477 (uc_pdata->max_uA != -ENODATA) &&
478 (uc_pdata->min_uA == uc_pdata->max_uA))
479 uc_pdata->flags |= REGULATOR_FLAG_AUTOSET_UA;
480
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200481 return 0;
482}
483
Simon Glassefeeb2c2015-06-23 15:38:59 -0600484int regulators_enable_boot_on(bool verbose)
485{
486 struct udevice *dev;
487 struct uclass *uc;
488 int ret;
489
490 ret = uclass_get(UCLASS_REGULATOR, &uc);
491 if (ret)
492 return ret;
493 for (uclass_first_device(UCLASS_REGULATOR, &dev);
Simon Glassc7298e72016-02-11 13:23:26 -0700494 dev;
Simon Glassefeeb2c2015-06-23 15:38:59 -0600495 uclass_next_device(&dev)) {
496 ret = regulator_autoset(dev);
Simon Glassf55c9512015-07-02 18:16:06 -0600497 if (ret == -EMEDIUMTYPE) {
498 ret = 0;
Simon Glassefeeb2c2015-06-23 15:38:59 -0600499 continue;
Simon Glassf55c9512015-07-02 18:16:06 -0600500 }
Simon Glassefeeb2c2015-06-23 15:38:59 -0600501 if (verbose)
502 regulator_show(dev, ret);
Simon Glassd6eddad2016-01-21 19:43:59 -0700503 if (ret == -ENOSYS)
504 ret = 0;
Simon Glassefeeb2c2015-06-23 15:38:59 -0600505 }
506
507 return ret;
508}
509
Konstantin Porotchkin621dab52017-05-29 15:59:38 +0300510int regulators_enable_boot_off(bool verbose)
511{
512 struct udevice *dev;
513 struct uclass *uc;
514 int ret;
515
516 ret = uclass_get(UCLASS_REGULATOR, &uc);
517 if (ret)
518 return ret;
519 for (uclass_first_device(UCLASS_REGULATOR, &dev);
520 dev;
521 uclass_next_device(&dev)) {
522 ret = regulator_unset(dev);
523 if (ret == -EMEDIUMTYPE) {
524 ret = 0;
525 continue;
526 }
527 if (verbose)
528 regulator_show(dev, ret);
529 if (ret == -ENOSYS)
530 ret = 0;
531 }
532
533 return ret;
534}
535
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200536UCLASS_DRIVER(regulator) = {
537 .id = UCLASS_REGULATOR,
538 .name = "regulator",
539 .post_bind = regulator_post_bind,
540 .pre_probe = regulator_pre_probe,
Simon Glass33b2efb2020-12-03 16:55:22 -0700541 .per_device_plat_auto = sizeof(struct dm_regulator_uclass_plat),
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200542};