blob: 66fd531da043c867d2c836566cecbb19ef968272 [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
Patrick Delaunay81313352021-04-27 11:02:19 +02007#define LOG_CATEGORY UCLASS_REGULATOR
8
Przemyslaw Marczak08edd002015-04-20 20:07:42 +02009#include <errno.h>
10#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Przemyslaw Marczak08edd002015-04-20 20:07:42 +020012#include <dm/uclass-internal.h>
Simon Glassdbd79542020-05-10 11:40:11 -060013#include <linux/delay.h>
Przemyslaw Marczak08edd002015-04-20 20:07:42 +020014#include <power/pmic.h>
15#include <power/regulator.h>
16
Przemyslaw Marczak08edd002015-04-20 20:07:42 +020017int regulator_mode(struct udevice *dev, struct dm_regulator_mode **modep)
18{
Simon Glass71fa5b42020-12-03 16:55:18 -070019 struct dm_regulator_uclass_plat *uc_pdata;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +020020
21 *modep = NULL;
22
Simon Glass71fa5b42020-12-03 16:55:18 -070023 uc_pdata = dev_get_uclass_plat(dev);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +020024 if (!uc_pdata)
25 return -ENXIO;
26
27 *modep = uc_pdata->mode;
28 return uc_pdata->mode_count;
29}
30
31int regulator_get_value(struct udevice *dev)
32{
33 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
34
35 if (!ops || !ops->get_value)
36 return -ENOSYS;
37
38 return ops->get_value(dev);
39}
40
Krzysztof Kozlowskie2fa3972019-03-06 19:37:54 +010041static void regulator_set_value_ramp_delay(struct udevice *dev, int old_uV,
42 int new_uV, unsigned int ramp_delay)
43{
44 int delay = DIV_ROUND_UP(abs(new_uV - old_uV), ramp_delay);
45
46 debug("regulator %s: delay %u us (%d uV -> %d uV)\n", dev->name, delay,
47 old_uV, new_uV);
48
49 udelay(delay);
50}
51
Przemyslaw Marczak08edd002015-04-20 20:07:42 +020052int regulator_set_value(struct udevice *dev, int uV)
53{
54 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
Simon Glass71fa5b42020-12-03 16:55:18 -070055 struct dm_regulator_uclass_plat *uc_pdata;
Krzysztof Kozlowskie2fa3972019-03-06 19:37:54 +010056 int ret, old_uV = uV, is_enabled = 0;
Keerthyc6e66692016-10-26 13:42:31 +053057
Simon Glass71fa5b42020-12-03 16:55:18 -070058 uc_pdata = dev_get_uclass_plat(dev);
Keerthyc6e66692016-10-26 13:42:31 +053059 if (uc_pdata->min_uV != -ENODATA && uV < uc_pdata->min_uV)
60 return -EINVAL;
61 if (uc_pdata->max_uV != -ENODATA && uV > uc_pdata->max_uV)
62 return -EINVAL;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +020063
64 if (!ops || !ops->set_value)
65 return -ENOSYS;
66
Krzysztof Kozlowskie2fa3972019-03-06 19:37:54 +010067 if (uc_pdata->ramp_delay) {
68 is_enabled = regulator_get_enable(dev);
69 old_uV = regulator_get_value(dev);
70 }
71
72 ret = ops->set_value(dev, uV);
73
74 if (!ret) {
75 if (uc_pdata->ramp_delay && old_uV > 0 && is_enabled)
76 regulator_set_value_ramp_delay(dev, old_uV, uV,
77 uc_pdata->ramp_delay);
78 }
79
80 return ret;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +020081}
82
Joseph Chenbb511322019-09-26 15:43:52 +080083int regulator_set_suspend_value(struct udevice *dev, int uV)
84{
85 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
Simon Glass71fa5b42020-12-03 16:55:18 -070086 struct dm_regulator_uclass_plat *uc_pdata;
Joseph Chenbb511322019-09-26 15:43:52 +080087
Simon Glass71fa5b42020-12-03 16:55:18 -070088 uc_pdata = dev_get_uclass_plat(dev);
Joseph Chenbb511322019-09-26 15:43:52 +080089 if (uc_pdata->min_uV != -ENODATA && uV < uc_pdata->min_uV)
90 return -EINVAL;
91 if (uc_pdata->max_uV != -ENODATA && uV > uc_pdata->max_uV)
92 return -EINVAL;
93
94 if (!ops->set_suspend_value)
95 return -ENOSYS;
96
97 return ops->set_suspend_value(dev, uV);
98}
99
100int regulator_get_suspend_value(struct udevice *dev)
101{
102 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
103
104 if (!ops->get_suspend_value)
105 return -ENOSYS;
106
107 return ops->get_suspend_value(dev);
108}
109
Keerthy162c02e2016-10-26 13:42:30 +0530110/*
111 * To be called with at most caution as there is no check
112 * before setting the actual voltage value.
113 */
114int regulator_set_value_force(struct udevice *dev, int uV)
115{
116 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
117
118 if (!ops || !ops->set_value)
119 return -ENOSYS;
120
121 return ops->set_value(dev, uV);
122}
123
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200124int regulator_get_current(struct udevice *dev)
125{
126 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
127
128 if (!ops || !ops->get_current)
129 return -ENOSYS;
130
131 return ops->get_current(dev);
132}
133
134int regulator_set_current(struct udevice *dev, int uA)
135{
136 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
Simon Glass71fa5b42020-12-03 16:55:18 -0700137 struct dm_regulator_uclass_plat *uc_pdata;
Keerthyce152be2016-10-26 13:42:32 +0530138
Simon Glass71fa5b42020-12-03 16:55:18 -0700139 uc_pdata = dev_get_uclass_plat(dev);
Keerthyce152be2016-10-26 13:42:32 +0530140 if (uc_pdata->min_uA != -ENODATA && uA < uc_pdata->min_uA)
141 return -EINVAL;
142 if (uc_pdata->max_uA != -ENODATA && uA > uc_pdata->max_uA)
143 return -EINVAL;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200144
145 if (!ops || !ops->set_current)
146 return -ENOSYS;
147
148 return ops->set_current(dev, uA);
149}
150
Keerthy23be7fb2017-06-13 09:53:45 +0530151int regulator_get_enable(struct udevice *dev)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200152{
153 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
154
155 if (!ops || !ops->get_enable)
156 return -ENOSYS;
157
158 return ops->get_enable(dev);
159}
160
161int regulator_set_enable(struct udevice *dev, bool enable)
162{
163 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
Simon Glass71fa5b42020-12-03 16:55:18 -0700164 struct dm_regulator_uclass_plat *uc_pdata;
Krzysztof Kozlowskie2fa3972019-03-06 19:37:54 +0100165 int ret, old_enable = 0;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200166
167 if (!ops || !ops->set_enable)
168 return -ENOSYS;
169
Simon Glass71fa5b42020-12-03 16:55:18 -0700170 uc_pdata = dev_get_uclass_plat(dev);
Patrick Delaunayd7585f22018-11-15 13:45:31 +0100171 if (!enable && uc_pdata->always_on)
Lokesh Vutlaf870da22019-01-11 15:15:50 +0530172 return -EACCES;
Patrick Delaunayd7585f22018-11-15 13:45:31 +0100173
Krzysztof Kozlowskie2fa3972019-03-06 19:37:54 +0100174 if (uc_pdata->ramp_delay)
175 old_enable = regulator_get_enable(dev);
176
177 ret = ops->set_enable(dev, enable);
178 if (!ret) {
179 if (uc_pdata->ramp_delay && !old_enable && enable) {
180 int uV = regulator_get_value(dev);
181
182 if (uV > 0) {
183 regulator_set_value_ramp_delay(dev, 0, uV,
184 uc_pdata->ramp_delay);
185 }
186 }
187 }
188
189 return ret;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200190}
191
Lokesh Vutla7106b782019-01-11 15:15:51 +0530192int regulator_set_enable_if_allowed(struct udevice *dev, bool enable)
193{
194 int ret;
195
196 ret = regulator_set_enable(dev, enable);
197 if (ret == -ENOSYS || ret == -EACCES)
198 return 0;
Eugen Hristev5a1c0f32023-04-19 16:45:26 +0300199 /* if we want to disable but it's in use by someone else */
200 if (!enable && ret == -EBUSY)
201 return 0;
202 /* if it's already enabled/disabled */
203 if (ret == -EALREADY)
204 return 0;
Lokesh Vutla7106b782019-01-11 15:15:51 +0530205
206 return ret;
207}
208
Joseph Chenbb511322019-09-26 15:43:52 +0800209int regulator_set_suspend_enable(struct udevice *dev, bool enable)
210{
211 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
212
213 if (!ops->set_suspend_enable)
214 return -ENOSYS;
215
216 return ops->set_suspend_enable(dev, enable);
217}
218
219int regulator_get_suspend_enable(struct udevice *dev)
220{
221 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
222
223 if (!ops->get_suspend_enable)
224 return -ENOSYS;
225
226 return ops->get_suspend_enable(dev);
227}
228
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200229int regulator_get_mode(struct udevice *dev)
230{
231 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
232
233 if (!ops || !ops->get_mode)
234 return -ENOSYS;
235
236 return ops->get_mode(dev);
237}
238
239int regulator_set_mode(struct udevice *dev, int mode)
240{
241 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
242
243 if (!ops || !ops->set_mode)
244 return -ENOSYS;
245
246 return ops->set_mode(dev, mode);
247}
248
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200249int regulator_get_by_platname(const char *plat_name, struct udevice **devp)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200250{
Simon Glass71fa5b42020-12-03 16:55:18 -0700251 struct dm_regulator_uclass_plat *uc_pdata;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200252 struct udevice *dev;
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200253 int ret;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200254
255 *devp = NULL;
256
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200257 for (ret = uclass_find_first_device(UCLASS_REGULATOR, &dev); dev;
258 ret = uclass_find_next_device(&dev)) {
Simon Glassfc3ebf12017-05-31 17:57:15 -0600259 if (ret) {
260 debug("regulator %s, ret=%d\n", dev->name, ret);
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200261 continue;
Simon Glassfc3ebf12017-05-31 17:57:15 -0600262 }
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200263
Simon Glass71fa5b42020-12-03 16:55:18 -0700264 uc_pdata = dev_get_uclass_plat(dev);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200265 if (!uc_pdata || strcmp(plat_name, uc_pdata->name))
266 continue;
267
268 return uclass_get_device_tail(dev, 0, devp);
269 }
270
Simon Glassfc3ebf12017-05-31 17:57:15 -0600271 debug("%s: can't find: %s, ret=%d\n", __func__, plat_name, ret);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200272
273 return -ENODEV;
274}
275
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200276int regulator_get_by_devname(const char *devname, struct udevice **devp)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200277{
278 return uclass_get_device_by_name(UCLASS_REGULATOR, devname, devp);
279}
280
Przemyslaw Marczakc9001032015-10-27 13:07:59 +0100281int device_get_supply_regulator(struct udevice *dev, const char *supply_name,
282 struct udevice **devp)
283{
284 return uclass_get_device_by_phandle(UCLASS_REGULATOR, dev,
285 supply_name, devp);
286}
287
Simon Glass46cb8242015-06-23 15:38:58 -0600288int regulator_autoset(struct udevice *dev)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200289{
Simon Glass71fa5b42020-12-03 16:55:18 -0700290 struct dm_regulator_uclass_plat *uc_pdata;
Simon Glass46cb8242015-06-23 15:38:58 -0600291 int ret = 0;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200292
Simon Glass71fa5b42020-12-03 16:55:18 -0700293 uc_pdata = dev_get_uclass_plat(dev);
Joseph Chenbb511322019-09-26 15:43:52 +0800294
Jonas Karlmanc2ef8812023-08-21 22:30:24 +0000295 if (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_DONE)
296 return -EALREADY;
297
Joseph Chenbb511322019-09-26 15:43:52 +0800298 ret = regulator_set_suspend_enable(dev, uc_pdata->suspend_on);
John Keeping6ec6a072022-09-08 12:38:35 +0100299 if (ret == -ENOSYS)
300 ret = 0;
301
Joseph Chenbb511322019-09-26 15:43:52 +0800302 if (!ret && uc_pdata->suspend_on) {
303 ret = regulator_set_suspend_value(dev, uc_pdata->suspend_uV);
John Keeping6ec6a072022-09-08 12:38:35 +0100304 if (ret == -ENOSYS)
305 ret = 0;
306
307 if (ret)
Joseph Chenbb511322019-09-26 15:43:52 +0800308 return ret;
309 }
310
Jonas Karlmanc2ef8812023-08-21 22:30:24 +0000311 if (!uc_pdata->always_on && !uc_pdata->boot_on) {
312 ret = -EMEDIUMTYPE;
313 goto out;
314 }
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200315
Jonas Karlmanc2ef8812023-08-21 22:30:24 +0000316 if (uc_pdata->type == REGULATOR_TYPE_FIXED) {
317 ret = regulator_set_enable(dev, true);
318 goto out;
319 }
Sven Schwermerd033cbf2019-06-12 08:32:38 +0200320
Simon Glass46cb8242015-06-23 15:38:58 -0600321 if (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UV)
322 ret = regulator_set_value(dev, uc_pdata->min_uV);
Joseph Chenbb511322019-09-26 15:43:52 +0800323 if (uc_pdata->init_uV > 0)
324 ret = regulator_set_value(dev, uc_pdata->init_uV);
Simon Glass46cb8242015-06-23 15:38:58 -0600325 if (!ret && (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UA))
326 ret = regulator_set_current(dev, uc_pdata->min_uA);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200327
328 if (!ret)
Simon Glass46cb8242015-06-23 15:38:58 -0600329 ret = regulator_set_enable(dev, true);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200330
Jonas Karlmanc2ef8812023-08-21 22:30:24 +0000331out:
332 uc_pdata->flags |= REGULATOR_FLAG_AUTOSET_DONE;
333
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200334 return ret;
335}
336
Konstantin Porotchkin621dab52017-05-29 15:59:38 +0300337int regulator_unset(struct udevice *dev)
338{
339 struct dm_regulator_uclass_plat *uc_pdata;
340
341 uc_pdata = dev_get_uclass_plat(dev);
342 if (uc_pdata && uc_pdata->force_off)
343 return regulator_set_enable(dev, false);
344
345 return -EMEDIUMTYPE;
346}
347
Simon Glass46cb8242015-06-23 15:38:58 -0600348static void regulator_show(struct udevice *dev, int ret)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200349{
Simon Glass71fa5b42020-12-03 16:55:18 -0700350 struct dm_regulator_uclass_plat *uc_pdata;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200351
Simon Glass71fa5b42020-12-03 16:55:18 -0700352 uc_pdata = dev_get_uclass_plat(dev);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200353
Simon Glass46cb8242015-06-23 15:38:58 -0600354 printf("%s@%s: ", dev->name, uc_pdata->name);
355 if (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UV)
356 printf("set %d uV", uc_pdata->min_uV);
357 if (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UA)
358 printf("; set %d uA", uc_pdata->min_uA);
359 printf("; enabling");
360 if (ret)
Simon Glass46ad8cb2016-01-21 19:43:58 -0700361 printf(" (ret: %d)", ret);
Simon Glass46cb8242015-06-23 15:38:58 -0600362 printf("\n");
363}
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200364
Simon Glass46cb8242015-06-23 15:38:58 -0600365int regulator_autoset_by_name(const char *platname, struct udevice **devp)
366{
367 struct udevice *dev;
368 int ret;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200369
Simon Glass46cb8242015-06-23 15:38:58 -0600370 ret = regulator_get_by_platname(platname, &dev);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200371 if (devp)
372 *devp = dev;
Simon Glass46cb8242015-06-23 15:38:58 -0600373 if (ret) {
Simon Glassfc3ebf12017-05-31 17:57:15 -0600374 debug("Can get the regulator: %s (err=%d)\n", platname, ret);
Simon Glass46cb8242015-06-23 15:38:58 -0600375 return ret;
376 }
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200377
Simon Glass46cb8242015-06-23 15:38:58 -0600378 return regulator_autoset(dev);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200379}
380
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200381int regulator_list_autoset(const char *list_platname[],
382 struct udevice *list_devp[],
383 bool verbose)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200384{
385 struct udevice *dev;
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200386 int error = 0, i = 0, ret;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200387
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200388 while (list_platname[i]) {
Simon Glass46cb8242015-06-23 15:38:58 -0600389 ret = regulator_autoset_by_name(list_platname[i], &dev);
390 if (ret != -EMEDIUMTYPE && verbose)
391 regulator_show(dev, ret);
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200392 if (ret & !error)
393 error = ret;
394
395 if (list_devp)
396 list_devp[i] = dev;
397
398 i++;
399 }
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200400
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200401 return error;
402}
403
404static bool regulator_name_is_unique(struct udevice *check_dev,
405 const char *check_name)
406{
Simon Glass71fa5b42020-12-03 16:55:18 -0700407 struct dm_regulator_uclass_plat *uc_pdata;
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200408 struct udevice *dev;
409 int check_len = strlen(check_name);
410 int ret;
411 int len;
412
413 for (ret = uclass_find_first_device(UCLASS_REGULATOR, &dev); dev;
414 ret = uclass_find_next_device(&dev)) {
415 if (ret || dev == check_dev)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200416 continue;
417
Simon Glass71fa5b42020-12-03 16:55:18 -0700418 uc_pdata = dev_get_uclass_plat(dev);
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200419 len = strlen(uc_pdata->name);
420 if (len != check_len)
421 continue;
422
423 if (!strcmp(uc_pdata->name, check_name))
424 return false;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200425 }
426
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200427 return true;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200428}
429
430static int regulator_post_bind(struct udevice *dev)
431{
Simon Glass71fa5b42020-12-03 16:55:18 -0700432 struct dm_regulator_uclass_plat *uc_pdata;
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200433 const char *property = "regulator-name";
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200434
Simon Glass71fa5b42020-12-03 16:55:18 -0700435 uc_pdata = dev_get_uclass_plat(dev);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200436
437 /* Regulator's mandatory constraint */
Simon Glasseeb99de2017-05-18 20:09:34 -0600438 uc_pdata->name = dev_read_string(dev, property);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200439 if (!uc_pdata->name) {
Simon Glasseeb99de2017-05-18 20:09:34 -0600440 debug("%s: dev '%s' has no property '%s'\n",
441 __func__, dev->name, property);
442 uc_pdata->name = dev_read_name(dev);
Peng Fancd672d42015-08-07 16:43:42 +0800443 if (!uc_pdata->name)
444 return -EINVAL;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200445 }
446
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200447 if (regulator_name_is_unique(dev, uc_pdata->name))
448 return 0;
449
Simon Glasseeb99de2017-05-18 20:09:34 -0600450 debug("'%s' of dev: '%s', has nonunique value: '%s\n",
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200451 property, dev->name, uc_pdata->name);
452
453 return -EINVAL;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200454}
455
456static int regulator_pre_probe(struct udevice *dev)
457{
Simon Glass71fa5b42020-12-03 16:55:18 -0700458 struct dm_regulator_uclass_plat *uc_pdata;
Joseph Chenbb511322019-09-26 15:43:52 +0800459 ofnode node;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200460
Simon Glass71fa5b42020-12-03 16:55:18 -0700461 uc_pdata = dev_get_uclass_plat(dev);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200462 if (!uc_pdata)
463 return -ENXIO;
464
465 /* Regulator's optional constraints */
Simon Glasseeb99de2017-05-18 20:09:34 -0600466 uc_pdata->min_uV = dev_read_u32_default(dev, "regulator-min-microvolt",
467 -ENODATA);
468 uc_pdata->max_uV = dev_read_u32_default(dev, "regulator-max-microvolt",
469 -ENODATA);
Joseph Chenbb511322019-09-26 15:43:52 +0800470 uc_pdata->init_uV = dev_read_u32_default(dev, "regulator-init-microvolt",
471 -ENODATA);
Simon Glasseeb99de2017-05-18 20:09:34 -0600472 uc_pdata->min_uA = dev_read_u32_default(dev, "regulator-min-microamp",
473 -ENODATA);
474 uc_pdata->max_uA = dev_read_u32_default(dev, "regulator-max-microamp",
475 -ENODATA);
476 uc_pdata->always_on = dev_read_bool(dev, "regulator-always-on");
477 uc_pdata->boot_on = dev_read_bool(dev, "regulator-boot-on");
Krzysztof Kozlowskie2fa3972019-03-06 19:37:54 +0100478 uc_pdata->ramp_delay = dev_read_u32_default(dev, "regulator-ramp-delay",
479 0);
Konstantin Porotchkin621dab52017-05-29 15:59:38 +0300480 uc_pdata->force_off = dev_read_bool(dev, "regulator-force-boot-off");
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200481
Joseph Chenbb511322019-09-26 15:43:52 +0800482 node = dev_read_subnode(dev, "regulator-state-mem");
483 if (ofnode_valid(node)) {
484 uc_pdata->suspend_on = !ofnode_read_bool(node, "regulator-off-in-suspend");
485 if (ofnode_read_u32(node, "regulator-suspend-microvolt", &uc_pdata->suspend_uV))
486 uc_pdata->suspend_uV = uc_pdata->max_uV;
487 } else {
488 uc_pdata->suspend_on = true;
489 uc_pdata->suspend_uV = uc_pdata->max_uV;
490 }
491
Simon Glass991f9bc2015-06-23 15:38:57 -0600492 /* Those values are optional (-ENODATA if unset) */
493 if ((uc_pdata->min_uV != -ENODATA) &&
494 (uc_pdata->max_uV != -ENODATA) &&
495 (uc_pdata->min_uV == uc_pdata->max_uV))
496 uc_pdata->flags |= REGULATOR_FLAG_AUTOSET_UV;
497
498 /* Those values are optional (-ENODATA if unset) */
499 if ((uc_pdata->min_uA != -ENODATA) &&
500 (uc_pdata->max_uA != -ENODATA) &&
501 (uc_pdata->min_uA == uc_pdata->max_uA))
502 uc_pdata->flags |= REGULATOR_FLAG_AUTOSET_UA;
503
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200504 return 0;
505}
506
Simon Glassefeeb2c2015-06-23 15:38:59 -0600507int regulators_enable_boot_on(bool verbose)
508{
509 struct udevice *dev;
510 struct uclass *uc;
511 int ret;
512
513 ret = uclass_get(UCLASS_REGULATOR, &uc);
514 if (ret)
515 return ret;
516 for (uclass_first_device(UCLASS_REGULATOR, &dev);
Simon Glassc7298e72016-02-11 13:23:26 -0700517 dev;
Simon Glassefeeb2c2015-06-23 15:38:59 -0600518 uclass_next_device(&dev)) {
519 ret = regulator_autoset(dev);
Simon Glassf55c9512015-07-02 18:16:06 -0600520 if (ret == -EMEDIUMTYPE) {
521 ret = 0;
Simon Glassefeeb2c2015-06-23 15:38:59 -0600522 continue;
Simon Glassf55c9512015-07-02 18:16:06 -0600523 }
Simon Glassefeeb2c2015-06-23 15:38:59 -0600524 if (verbose)
525 regulator_show(dev, ret);
Simon Glassd6eddad2016-01-21 19:43:59 -0700526 if (ret == -ENOSYS)
527 ret = 0;
Simon Glassefeeb2c2015-06-23 15:38:59 -0600528 }
529
530 return ret;
531}
532
Konstantin Porotchkin621dab52017-05-29 15:59:38 +0300533int regulators_enable_boot_off(bool verbose)
534{
535 struct udevice *dev;
536 struct uclass *uc;
537 int ret;
538
539 ret = uclass_get(UCLASS_REGULATOR, &uc);
540 if (ret)
541 return ret;
542 for (uclass_first_device(UCLASS_REGULATOR, &dev);
543 dev;
544 uclass_next_device(&dev)) {
545 ret = regulator_unset(dev);
546 if (ret == -EMEDIUMTYPE) {
547 ret = 0;
548 continue;
549 }
550 if (verbose)
551 regulator_show(dev, ret);
552 if (ret == -ENOSYS)
553 ret = 0;
554 }
555
556 return ret;
557}
558
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200559UCLASS_DRIVER(regulator) = {
560 .id = UCLASS_REGULATOR,
561 .name = "regulator",
562 .post_bind = regulator_post_bind,
563 .pre_probe = regulator_pre_probe,
Simon Glass33b2efb2020-12-03 16:55:22 -0700564 .per_device_plat_auto = sizeof(struct dm_regulator_uclass_plat),
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200565};