blob: 03d39587b31508c723ef494ed7ea326ced0cc03d [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;
Jonas Karlmanc3c84ad2024-07-24 22:47:10 +000063 if (uV == -ENODATA)
64 return -EINVAL;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +020065
66 if (!ops || !ops->set_value)
67 return -ENOSYS;
68
Krzysztof Kozlowskie2fa3972019-03-06 19:37:54 +010069 if (uc_pdata->ramp_delay) {
70 is_enabled = regulator_get_enable(dev);
71 old_uV = regulator_get_value(dev);
72 }
73
74 ret = ops->set_value(dev, uV);
75
76 if (!ret) {
77 if (uc_pdata->ramp_delay && old_uV > 0 && is_enabled)
78 regulator_set_value_ramp_delay(dev, old_uV, uV,
79 uc_pdata->ramp_delay);
80 }
81
82 return ret;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +020083}
84
Joseph Chenbb511322019-09-26 15:43:52 +080085int regulator_set_suspend_value(struct udevice *dev, int uV)
86{
87 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
Simon Glass71fa5b42020-12-03 16:55:18 -070088 struct dm_regulator_uclass_plat *uc_pdata;
Joseph Chenbb511322019-09-26 15:43:52 +080089
Simon Glass71fa5b42020-12-03 16:55:18 -070090 uc_pdata = dev_get_uclass_plat(dev);
Joseph Chenbb511322019-09-26 15:43:52 +080091 if (uc_pdata->min_uV != -ENODATA && uV < uc_pdata->min_uV)
92 return -EINVAL;
93 if (uc_pdata->max_uV != -ENODATA && uV > uc_pdata->max_uV)
94 return -EINVAL;
Jonas Karlmanc3c84ad2024-07-24 22:47:10 +000095 if (uV == -ENODATA)
96 return -EINVAL;
Joseph Chenbb511322019-09-26 15:43:52 +080097
98 if (!ops->set_suspend_value)
99 return -ENOSYS;
100
101 return ops->set_suspend_value(dev, uV);
102}
103
104int regulator_get_suspend_value(struct udevice *dev)
105{
106 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
107
108 if (!ops->get_suspend_value)
109 return -ENOSYS;
110
111 return ops->get_suspend_value(dev);
112}
113
Keerthy162c02e2016-10-26 13:42:30 +0530114/*
115 * To be called with at most caution as there is no check
116 * before setting the actual voltage value.
117 */
118int regulator_set_value_force(struct udevice *dev, int uV)
119{
120 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
121
122 if (!ops || !ops->set_value)
123 return -ENOSYS;
124
125 return ops->set_value(dev, uV);
126}
127
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200128int regulator_get_current(struct udevice *dev)
129{
130 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
131
132 if (!ops || !ops->get_current)
133 return -ENOSYS;
134
135 return ops->get_current(dev);
136}
137
138int regulator_set_current(struct udevice *dev, int uA)
139{
140 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
Simon Glass71fa5b42020-12-03 16:55:18 -0700141 struct dm_regulator_uclass_plat *uc_pdata;
Keerthyce152be2016-10-26 13:42:32 +0530142
Simon Glass71fa5b42020-12-03 16:55:18 -0700143 uc_pdata = dev_get_uclass_plat(dev);
Keerthyce152be2016-10-26 13:42:32 +0530144 if (uc_pdata->min_uA != -ENODATA && uA < uc_pdata->min_uA)
145 return -EINVAL;
146 if (uc_pdata->max_uA != -ENODATA && uA > uc_pdata->max_uA)
147 return -EINVAL;
Jonas Karlmanc3c84ad2024-07-24 22:47:10 +0000148 if (uA == -ENODATA)
149 return -EINVAL;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200150
151 if (!ops || !ops->set_current)
152 return -ENOSYS;
153
154 return ops->set_current(dev, uA);
155}
156
Keerthy23be7fb2017-06-13 09:53:45 +0530157int regulator_get_enable(struct udevice *dev)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200158{
159 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
160
161 if (!ops || !ops->get_enable)
162 return -ENOSYS;
163
164 return ops->get_enable(dev);
165}
166
167int regulator_set_enable(struct udevice *dev, bool enable)
168{
169 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
Simon Glass71fa5b42020-12-03 16:55:18 -0700170 struct dm_regulator_uclass_plat *uc_pdata;
Krzysztof Kozlowskie2fa3972019-03-06 19:37:54 +0100171 int ret, old_enable = 0;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200172
173 if (!ops || !ops->set_enable)
174 return -ENOSYS;
175
Simon Glass71fa5b42020-12-03 16:55:18 -0700176 uc_pdata = dev_get_uclass_plat(dev);
Patrick Delaunayd7585f22018-11-15 13:45:31 +0100177 if (!enable && uc_pdata->always_on)
Lokesh Vutlaf870da22019-01-11 15:15:50 +0530178 return -EACCES;
Patrick Delaunayd7585f22018-11-15 13:45:31 +0100179
Krzysztof Kozlowskie2fa3972019-03-06 19:37:54 +0100180 if (uc_pdata->ramp_delay)
181 old_enable = regulator_get_enable(dev);
182
183 ret = ops->set_enable(dev, enable);
184 if (!ret) {
185 if (uc_pdata->ramp_delay && !old_enable && enable) {
186 int uV = regulator_get_value(dev);
187
188 if (uV > 0) {
189 regulator_set_value_ramp_delay(dev, 0, uV,
190 uc_pdata->ramp_delay);
191 }
192 }
193 }
194
195 return ret;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200196}
197
Lokesh Vutla7106b782019-01-11 15:15:51 +0530198int regulator_set_enable_if_allowed(struct udevice *dev, bool enable)
199{
200 int ret;
201
202 ret = regulator_set_enable(dev, enable);
203 if (ret == -ENOSYS || ret == -EACCES)
204 return 0;
Eugen Hristev5a1c0f32023-04-19 16:45:26 +0300205 /* if we want to disable but it's in use by someone else */
206 if (!enable && ret == -EBUSY)
207 return 0;
208 /* if it's already enabled/disabled */
209 if (ret == -EALREADY)
210 return 0;
Lokesh Vutla7106b782019-01-11 15:15:51 +0530211
212 return ret;
213}
214
Joseph Chenbb511322019-09-26 15:43:52 +0800215int regulator_set_suspend_enable(struct udevice *dev, bool enable)
216{
217 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
218
219 if (!ops->set_suspend_enable)
220 return -ENOSYS;
221
222 return ops->set_suspend_enable(dev, enable);
223}
224
225int regulator_get_suspend_enable(struct udevice *dev)
226{
227 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
228
229 if (!ops->get_suspend_enable)
230 return -ENOSYS;
231
232 return ops->get_suspend_enable(dev);
233}
234
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200235int regulator_get_mode(struct udevice *dev)
236{
237 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
238
239 if (!ops || !ops->get_mode)
240 return -ENOSYS;
241
242 return ops->get_mode(dev);
243}
244
245int regulator_set_mode(struct udevice *dev, int mode)
246{
247 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
248
249 if (!ops || !ops->set_mode)
250 return -ENOSYS;
251
252 return ops->set_mode(dev, mode);
253}
254
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200255int regulator_get_by_platname(const char *plat_name, struct udevice **devp)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200256{
Simon Glass71fa5b42020-12-03 16:55:18 -0700257 struct dm_regulator_uclass_plat *uc_pdata;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200258 struct udevice *dev;
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200259 int ret;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200260
261 *devp = NULL;
262
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200263 for (ret = uclass_find_first_device(UCLASS_REGULATOR, &dev); dev;
264 ret = uclass_find_next_device(&dev)) {
Simon Glassfc3ebf12017-05-31 17:57:15 -0600265 if (ret) {
266 debug("regulator %s, ret=%d\n", dev->name, ret);
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200267 continue;
Simon Glassfc3ebf12017-05-31 17:57:15 -0600268 }
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200269
Simon Glass71fa5b42020-12-03 16:55:18 -0700270 uc_pdata = dev_get_uclass_plat(dev);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200271 if (!uc_pdata || strcmp(plat_name, uc_pdata->name))
272 continue;
273
274 return uclass_get_device_tail(dev, 0, devp);
275 }
276
Simon Glassfc3ebf12017-05-31 17:57:15 -0600277 debug("%s: can't find: %s, ret=%d\n", __func__, plat_name, ret);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200278
279 return -ENODEV;
280}
281
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200282int regulator_get_by_devname(const char *devname, struct udevice **devp)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200283{
284 return uclass_get_device_by_name(UCLASS_REGULATOR, devname, devp);
285}
286
Przemyslaw Marczakc9001032015-10-27 13:07:59 +0100287int device_get_supply_regulator(struct udevice *dev, const char *supply_name,
288 struct udevice **devp)
289{
290 return uclass_get_device_by_phandle(UCLASS_REGULATOR, dev,
291 supply_name, devp);
292}
293
Simon Glass46cb8242015-06-23 15:38:58 -0600294int regulator_autoset(struct udevice *dev)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200295{
Simon Glass71fa5b42020-12-03 16:55:18 -0700296 struct dm_regulator_uclass_plat *uc_pdata;
Simon Glass46cb8242015-06-23 15:38:58 -0600297 int ret = 0;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200298
Simon Glass71fa5b42020-12-03 16:55:18 -0700299 uc_pdata = dev_get_uclass_plat(dev);
Joseph Chenbb511322019-09-26 15:43:52 +0800300
Jonas Karlmanc2ef8812023-08-21 22:30:24 +0000301 if (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_DONE)
302 return -EALREADY;
303
Joseph Chenbb511322019-09-26 15:43:52 +0800304 ret = regulator_set_suspend_enable(dev, uc_pdata->suspend_on);
John Keeping6ec6a072022-09-08 12:38:35 +0100305 if (ret == -ENOSYS)
306 ret = 0;
307
Jonas Karlmanc3c84ad2024-07-24 22:47:10 +0000308 if (!ret && uc_pdata->suspend_on && uc_pdata->suspend_uV != -ENODATA) {
Joseph Chenbb511322019-09-26 15:43:52 +0800309 ret = regulator_set_suspend_value(dev, uc_pdata->suspend_uV);
John Keeping6ec6a072022-09-08 12:38:35 +0100310 if (ret == -ENOSYS)
311 ret = 0;
312
313 if (ret)
Joseph Chenbb511322019-09-26 15:43:52 +0800314 return ret;
315 }
316
Jonas Karlmanc2ef8812023-08-21 22:30:24 +0000317 if (!uc_pdata->always_on && !uc_pdata->boot_on) {
318 ret = -EMEDIUMTYPE;
319 goto out;
320 }
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200321
Jonas Karlmanc2ef8812023-08-21 22:30:24 +0000322 if (uc_pdata->type == REGULATOR_TYPE_FIXED) {
323 ret = regulator_set_enable(dev, true);
324 goto out;
325 }
Sven Schwermerd033cbf2019-06-12 08:32:38 +0200326
Simon Glass46cb8242015-06-23 15:38:58 -0600327 if (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UV)
328 ret = regulator_set_value(dev, uc_pdata->min_uV);
Joseph Chenbb511322019-09-26 15:43:52 +0800329 if (uc_pdata->init_uV > 0)
330 ret = regulator_set_value(dev, uc_pdata->init_uV);
Simon Glass46cb8242015-06-23 15:38:58 -0600331 if (!ret && (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UA))
332 ret = regulator_set_current(dev, uc_pdata->min_uA);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200333
334 if (!ret)
Simon Glass46cb8242015-06-23 15:38:58 -0600335 ret = regulator_set_enable(dev, true);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200336
Jonas Karlmanc2ef8812023-08-21 22:30:24 +0000337out:
338 uc_pdata->flags |= REGULATOR_FLAG_AUTOSET_DONE;
339
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200340 return ret;
341}
342
Konstantin Porotchkin621dab52017-05-29 15:59:38 +0300343int regulator_unset(struct udevice *dev)
344{
345 struct dm_regulator_uclass_plat *uc_pdata;
346
347 uc_pdata = dev_get_uclass_plat(dev);
348 if (uc_pdata && uc_pdata->force_off)
349 return regulator_set_enable(dev, false);
350
351 return -EMEDIUMTYPE;
352}
353
Simon Glass46cb8242015-06-23 15:38:58 -0600354static void regulator_show(struct udevice *dev, int ret)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200355{
Simon Glass71fa5b42020-12-03 16:55:18 -0700356 struct dm_regulator_uclass_plat *uc_pdata;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200357
Simon Glass71fa5b42020-12-03 16:55:18 -0700358 uc_pdata = dev_get_uclass_plat(dev);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200359
Simon Glass46cb8242015-06-23 15:38:58 -0600360 printf("%s@%s: ", dev->name, uc_pdata->name);
361 if (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UV)
362 printf("set %d uV", uc_pdata->min_uV);
363 if (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UA)
364 printf("; set %d uA", uc_pdata->min_uA);
365 printf("; enabling");
366 if (ret)
Simon Glass46ad8cb2016-01-21 19:43:58 -0700367 printf(" (ret: %d)", ret);
Simon Glass46cb8242015-06-23 15:38:58 -0600368 printf("\n");
369}
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200370
Simon Glass46cb8242015-06-23 15:38:58 -0600371int regulator_autoset_by_name(const char *platname, struct udevice **devp)
372{
373 struct udevice *dev;
374 int ret;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200375
Simon Glass46cb8242015-06-23 15:38:58 -0600376 ret = regulator_get_by_platname(platname, &dev);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200377 if (devp)
378 *devp = dev;
Simon Glass46cb8242015-06-23 15:38:58 -0600379 if (ret) {
Simon Glassfc3ebf12017-05-31 17:57:15 -0600380 debug("Can get the regulator: %s (err=%d)\n", platname, ret);
Simon Glass46cb8242015-06-23 15:38:58 -0600381 return ret;
382 }
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200383
Simon Glass46cb8242015-06-23 15:38:58 -0600384 return regulator_autoset(dev);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200385}
386
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200387int regulator_list_autoset(const char *list_platname[],
388 struct udevice *list_devp[],
389 bool verbose)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200390{
391 struct udevice *dev;
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200392 int error = 0, i = 0, ret;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200393
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200394 while (list_platname[i]) {
Simon Glass46cb8242015-06-23 15:38:58 -0600395 ret = regulator_autoset_by_name(list_platname[i], &dev);
396 if (ret != -EMEDIUMTYPE && verbose)
397 regulator_show(dev, ret);
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200398 if (ret & !error)
399 error = ret;
400
401 if (list_devp)
402 list_devp[i] = dev;
403
404 i++;
405 }
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200406
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200407 return error;
408}
409
410static bool regulator_name_is_unique(struct udevice *check_dev,
411 const char *check_name)
412{
Simon Glass71fa5b42020-12-03 16:55:18 -0700413 struct dm_regulator_uclass_plat *uc_pdata;
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200414 struct udevice *dev;
415 int check_len = strlen(check_name);
416 int ret;
417 int len;
418
419 for (ret = uclass_find_first_device(UCLASS_REGULATOR, &dev); dev;
420 ret = uclass_find_next_device(&dev)) {
421 if (ret || dev == check_dev)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200422 continue;
423
Simon Glass71fa5b42020-12-03 16:55:18 -0700424 uc_pdata = dev_get_uclass_plat(dev);
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200425 len = strlen(uc_pdata->name);
426 if (len != check_len)
427 continue;
428
429 if (!strcmp(uc_pdata->name, check_name))
430 return false;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200431 }
432
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200433 return true;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200434}
435
436static int regulator_post_bind(struct udevice *dev)
437{
Simon Glass71fa5b42020-12-03 16:55:18 -0700438 struct dm_regulator_uclass_plat *uc_pdata;
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200439 const char *property = "regulator-name";
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200440
Simon Glass71fa5b42020-12-03 16:55:18 -0700441 uc_pdata = dev_get_uclass_plat(dev);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200442
443 /* Regulator's mandatory constraint */
Simon Glasseeb99de2017-05-18 20:09:34 -0600444 uc_pdata->name = dev_read_string(dev, property);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200445 if (!uc_pdata->name) {
Simon Glasseeb99de2017-05-18 20:09:34 -0600446 debug("%s: dev '%s' has no property '%s'\n",
447 __func__, dev->name, property);
448 uc_pdata->name = dev_read_name(dev);
Peng Fancd672d42015-08-07 16:43:42 +0800449 if (!uc_pdata->name)
450 return -EINVAL;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200451 }
452
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200453 if (regulator_name_is_unique(dev, uc_pdata->name))
454 return 0;
455
Simon Glasseeb99de2017-05-18 20:09:34 -0600456 debug("'%s' of dev: '%s', has nonunique value: '%s\n",
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200457 property, dev->name, uc_pdata->name);
458
459 return -EINVAL;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200460}
461
462static int regulator_pre_probe(struct udevice *dev)
463{
Simon Glass71fa5b42020-12-03 16:55:18 -0700464 struct dm_regulator_uclass_plat *uc_pdata;
Joseph Chenbb511322019-09-26 15:43:52 +0800465 ofnode node;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200466
Simon Glass71fa5b42020-12-03 16:55:18 -0700467 uc_pdata = dev_get_uclass_plat(dev);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200468 if (!uc_pdata)
469 return -ENXIO;
470
471 /* Regulator's optional constraints */
Simon Glasseeb99de2017-05-18 20:09:34 -0600472 uc_pdata->min_uV = dev_read_u32_default(dev, "regulator-min-microvolt",
473 -ENODATA);
474 uc_pdata->max_uV = dev_read_u32_default(dev, "regulator-max-microvolt",
475 -ENODATA);
Joseph Chenbb511322019-09-26 15:43:52 +0800476 uc_pdata->init_uV = dev_read_u32_default(dev, "regulator-init-microvolt",
477 -ENODATA);
Simon Glasseeb99de2017-05-18 20:09:34 -0600478 uc_pdata->min_uA = dev_read_u32_default(dev, "regulator-min-microamp",
479 -ENODATA);
480 uc_pdata->max_uA = dev_read_u32_default(dev, "regulator-max-microamp",
481 -ENODATA);
482 uc_pdata->always_on = dev_read_bool(dev, "regulator-always-on");
483 uc_pdata->boot_on = dev_read_bool(dev, "regulator-boot-on");
Krzysztof Kozlowskie2fa3972019-03-06 19:37:54 +0100484 uc_pdata->ramp_delay = dev_read_u32_default(dev, "regulator-ramp-delay",
485 0);
Konstantin Porotchkin621dab52017-05-29 15:59:38 +0300486 uc_pdata->force_off = dev_read_bool(dev, "regulator-force-boot-off");
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200487
Joseph Chenbb511322019-09-26 15:43:52 +0800488 node = dev_read_subnode(dev, "regulator-state-mem");
489 if (ofnode_valid(node)) {
490 uc_pdata->suspend_on = !ofnode_read_bool(node, "regulator-off-in-suspend");
491 if (ofnode_read_u32(node, "regulator-suspend-microvolt", &uc_pdata->suspend_uV))
492 uc_pdata->suspend_uV = uc_pdata->max_uV;
493 } else {
494 uc_pdata->suspend_on = true;
495 uc_pdata->suspend_uV = uc_pdata->max_uV;
496 }
497
Simon Glass991f9bc2015-06-23 15:38:57 -0600498 /* Those values are optional (-ENODATA if unset) */
499 if ((uc_pdata->min_uV != -ENODATA) &&
500 (uc_pdata->max_uV != -ENODATA) &&
501 (uc_pdata->min_uV == uc_pdata->max_uV))
502 uc_pdata->flags |= REGULATOR_FLAG_AUTOSET_UV;
503
504 /* Those values are optional (-ENODATA if unset) */
505 if ((uc_pdata->min_uA != -ENODATA) &&
506 (uc_pdata->max_uA != -ENODATA) &&
507 (uc_pdata->min_uA == uc_pdata->max_uA))
508 uc_pdata->flags |= REGULATOR_FLAG_AUTOSET_UA;
509
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200510 return 0;
511}
512
Simon Glassefeeb2c2015-06-23 15:38:59 -0600513int regulators_enable_boot_on(bool verbose)
514{
515 struct udevice *dev;
516 struct uclass *uc;
517 int ret;
518
519 ret = uclass_get(UCLASS_REGULATOR, &uc);
520 if (ret)
521 return ret;
522 for (uclass_first_device(UCLASS_REGULATOR, &dev);
Simon Glassc7298e72016-02-11 13:23:26 -0700523 dev;
Simon Glassefeeb2c2015-06-23 15:38:59 -0600524 uclass_next_device(&dev)) {
525 ret = regulator_autoset(dev);
Simon Glassec88aa72024-06-27 09:29:46 +0100526 if (ret == -EMEDIUMTYPE || ret == -EALREADY) {
Simon Glassf55c9512015-07-02 18:16:06 -0600527 ret = 0;
Simon Glassefeeb2c2015-06-23 15:38:59 -0600528 continue;
Simon Glassf55c9512015-07-02 18:16:06 -0600529 }
Simon Glassefeeb2c2015-06-23 15:38:59 -0600530 if (verbose)
531 regulator_show(dev, ret);
Simon Glassd6eddad2016-01-21 19:43:59 -0700532 if (ret == -ENOSYS)
533 ret = 0;
Simon Glassefeeb2c2015-06-23 15:38:59 -0600534 }
535
536 return ret;
537}
538
Konstantin Porotchkin621dab52017-05-29 15:59:38 +0300539int regulators_enable_boot_off(bool verbose)
540{
541 struct udevice *dev;
542 struct uclass *uc;
543 int ret;
544
545 ret = uclass_get(UCLASS_REGULATOR, &uc);
546 if (ret)
547 return ret;
548 for (uclass_first_device(UCLASS_REGULATOR, &dev);
549 dev;
550 uclass_next_device(&dev)) {
551 ret = regulator_unset(dev);
552 if (ret == -EMEDIUMTYPE) {
553 ret = 0;
554 continue;
555 }
556 if (verbose)
557 regulator_show(dev, ret);
558 if (ret == -ENOSYS)
559 ret = 0;
560 }
561
562 return ret;
563}
564
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200565UCLASS_DRIVER(regulator) = {
566 .id = UCLASS_REGULATOR,
567 .name = "regulator",
568 .post_bind = regulator_post_bind,
569 .pre_probe = regulator_pre_probe,
Simon Glass33b2efb2020-12-03 16:55:22 -0700570 .per_device_plat_auto = sizeof(struct dm_regulator_uclass_plat),
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200571};