blob: 1a970004540a1e17b667d86d4cb7623eb8736e78 [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
Jonas Karlmana7c6f6b2024-07-24 22:47:11 +000058 if (!ops || !ops->set_value)
59 return -ENOSYS;
60
Simon Glass71fa5b42020-12-03 16:55:18 -070061 uc_pdata = dev_get_uclass_plat(dev);
Keerthyc6e66692016-10-26 13:42:31 +053062 if (uc_pdata->min_uV != -ENODATA && uV < uc_pdata->min_uV)
63 return -EINVAL;
64 if (uc_pdata->max_uV != -ENODATA && uV > uc_pdata->max_uV)
65 return -EINVAL;
Jonas Karlmanc3c84ad2024-07-24 22:47:10 +000066 if (uV == -ENODATA)
67 return -EINVAL;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +020068
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
Jonas Karlmana7c6f6b2024-07-24 22:47:11 +000090 if (!ops || !ops->set_suspend_value)
91 return -ENOSYS;
92
Simon Glass71fa5b42020-12-03 16:55:18 -070093 uc_pdata = dev_get_uclass_plat(dev);
Joseph Chenbb511322019-09-26 15:43:52 +080094 if (uc_pdata->min_uV != -ENODATA && uV < uc_pdata->min_uV)
95 return -EINVAL;
96 if (uc_pdata->max_uV != -ENODATA && uV > uc_pdata->max_uV)
97 return -EINVAL;
Jonas Karlmanc3c84ad2024-07-24 22:47:10 +000098 if (uV == -ENODATA)
99 return -EINVAL;
Joseph Chenbb511322019-09-26 15:43:52 +0800100
Joseph Chenbb511322019-09-26 15:43:52 +0800101 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
Jonas Karlmana7c6f6b2024-07-24 22:47:11 +0000108 if (!ops || !ops->get_suspend_value)
Joseph Chenbb511322019-09-26 15:43:52 +0800109 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
Jonas Karlmana7c6f6b2024-07-24 22:47:11 +0000143 if (!ops || !ops->set_current)
144 return -ENOSYS;
145
Simon Glass71fa5b42020-12-03 16:55:18 -0700146 uc_pdata = dev_get_uclass_plat(dev);
Keerthyce152be2016-10-26 13:42:32 +0530147 if (uc_pdata->min_uA != -ENODATA && uA < uc_pdata->min_uA)
148 return -EINVAL;
149 if (uc_pdata->max_uA != -ENODATA && uA > uc_pdata->max_uA)
150 return -EINVAL;
Jonas Karlmanc3c84ad2024-07-24 22:47:10 +0000151 if (uA == -ENODATA)
152 return -EINVAL;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200153
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200154 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
Jonas Karlmana7c6f6b2024-07-24 22:47:11 +0000219 if (!ops || !ops->set_suspend_enable)
Joseph Chenbb511322019-09-26 15:43:52 +0800220 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
Jonas Karlmana7c6f6b2024-07-24 22:47:11 +0000229 if (!ops || !ops->get_suspend_enable)
Joseph Chenbb511322019-09-26 15:43:52 +0800230 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);
Marek Vasut152fe722024-09-27 01:14:12 +0200442 uc_pdata->always_on = dev_read_bool(dev, "regulator-always-on");
443 uc_pdata->boot_on = dev_read_bool(dev, "regulator-boot-on");
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200444
445 /* Regulator's mandatory constraint */
Simon Glasseeb99de2017-05-18 20:09:34 -0600446 uc_pdata->name = dev_read_string(dev, property);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200447 if (!uc_pdata->name) {
Simon Glasseeb99de2017-05-18 20:09:34 -0600448 debug("%s: dev '%s' has no property '%s'\n",
449 __func__, dev->name, property);
450 uc_pdata->name = dev_read_name(dev);
Peng Fancd672d42015-08-07 16:43:42 +0800451 if (!uc_pdata->name)
452 return -EINVAL;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200453 }
454
Marek Vasut152fe722024-09-27 01:14:12 +0200455 if (!regulator_name_is_unique(dev, uc_pdata->name)) {
456 debug("'%s' of dev: '%s', has nonunique value: '%s\n",
457 property, dev->name, uc_pdata->name);
458 return -EINVAL;
459 }
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200460
Marek Vasut152fe722024-09-27 01:14:12 +0200461 /*
462 * In case the regulator has regulator-always-on or
463 * regulator-boot-on DT property, trigger probe() to
464 * configure its default state during startup.
465 */
466 if (uc_pdata->always_on || uc_pdata->boot_on)
467 dev_or_flags(dev, DM_FLAG_PROBE_AFTER_BIND);
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200468
Marek Vasut152fe722024-09-27 01:14:12 +0200469 return 0;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200470}
471
472static int regulator_pre_probe(struct udevice *dev)
473{
Simon Glass71fa5b42020-12-03 16:55:18 -0700474 struct dm_regulator_uclass_plat *uc_pdata;
Joseph Chenbb511322019-09-26 15:43:52 +0800475 ofnode node;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200476
Simon Glass71fa5b42020-12-03 16:55:18 -0700477 uc_pdata = dev_get_uclass_plat(dev);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200478 if (!uc_pdata)
479 return -ENXIO;
480
481 /* Regulator's optional constraints */
Simon Glasseeb99de2017-05-18 20:09:34 -0600482 uc_pdata->min_uV = dev_read_u32_default(dev, "regulator-min-microvolt",
483 -ENODATA);
484 uc_pdata->max_uV = dev_read_u32_default(dev, "regulator-max-microvolt",
485 -ENODATA);
Joseph Chenbb511322019-09-26 15:43:52 +0800486 uc_pdata->init_uV = dev_read_u32_default(dev, "regulator-init-microvolt",
487 -ENODATA);
Simon Glasseeb99de2017-05-18 20:09:34 -0600488 uc_pdata->min_uA = dev_read_u32_default(dev, "regulator-min-microamp",
489 -ENODATA);
490 uc_pdata->max_uA = dev_read_u32_default(dev, "regulator-max-microamp",
491 -ENODATA);
Krzysztof Kozlowskie2fa3972019-03-06 19:37:54 +0100492 uc_pdata->ramp_delay = dev_read_u32_default(dev, "regulator-ramp-delay",
493 0);
Konstantin Porotchkin621dab52017-05-29 15:59:38 +0300494 uc_pdata->force_off = dev_read_bool(dev, "regulator-force-boot-off");
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200495
Joseph Chenbb511322019-09-26 15:43:52 +0800496 node = dev_read_subnode(dev, "regulator-state-mem");
497 if (ofnode_valid(node)) {
498 uc_pdata->suspend_on = !ofnode_read_bool(node, "regulator-off-in-suspend");
499 if (ofnode_read_u32(node, "regulator-suspend-microvolt", &uc_pdata->suspend_uV))
500 uc_pdata->suspend_uV = uc_pdata->max_uV;
501 } else {
502 uc_pdata->suspend_on = true;
503 uc_pdata->suspend_uV = uc_pdata->max_uV;
504 }
505
Simon Glass991f9bc2015-06-23 15:38:57 -0600506 /* Those values are optional (-ENODATA if unset) */
507 if ((uc_pdata->min_uV != -ENODATA) &&
508 (uc_pdata->max_uV != -ENODATA) &&
509 (uc_pdata->min_uV == uc_pdata->max_uV))
510 uc_pdata->flags |= REGULATOR_FLAG_AUTOSET_UV;
511
512 /* Those values are optional (-ENODATA if unset) */
513 if ((uc_pdata->min_uA != -ENODATA) &&
514 (uc_pdata->max_uA != -ENODATA) &&
515 (uc_pdata->min_uA == uc_pdata->max_uA))
516 uc_pdata->flags |= REGULATOR_FLAG_AUTOSET_UA;
517
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200518 return 0;
519}
520
Simon Glassefeeb2c2015-06-23 15:38:59 -0600521int regulators_enable_boot_on(bool verbose)
522{
523 struct udevice *dev;
524 struct uclass *uc;
525 int ret;
526
527 ret = uclass_get(UCLASS_REGULATOR, &uc);
528 if (ret)
529 return ret;
530 for (uclass_first_device(UCLASS_REGULATOR, &dev);
Simon Glassc7298e72016-02-11 13:23:26 -0700531 dev;
Simon Glassefeeb2c2015-06-23 15:38:59 -0600532 uclass_next_device(&dev)) {
533 ret = regulator_autoset(dev);
Simon Glassec88aa72024-06-27 09:29:46 +0100534 if (ret == -EMEDIUMTYPE || ret == -EALREADY) {
Simon Glassf55c9512015-07-02 18:16:06 -0600535 ret = 0;
Simon Glassefeeb2c2015-06-23 15:38:59 -0600536 continue;
Simon Glassf55c9512015-07-02 18:16:06 -0600537 }
Simon Glassefeeb2c2015-06-23 15:38:59 -0600538 if (verbose)
539 regulator_show(dev, ret);
Simon Glassd6eddad2016-01-21 19:43:59 -0700540 if (ret == -ENOSYS)
541 ret = 0;
Simon Glassefeeb2c2015-06-23 15:38:59 -0600542 }
543
544 return ret;
545}
546
Konstantin Porotchkin621dab52017-05-29 15:59:38 +0300547int regulators_enable_boot_off(bool verbose)
548{
549 struct udevice *dev;
550 struct uclass *uc;
551 int ret;
552
553 ret = uclass_get(UCLASS_REGULATOR, &uc);
554 if (ret)
555 return ret;
556 for (uclass_first_device(UCLASS_REGULATOR, &dev);
557 dev;
558 uclass_next_device(&dev)) {
559 ret = regulator_unset(dev);
560 if (ret == -EMEDIUMTYPE) {
561 ret = 0;
562 continue;
563 }
564 if (verbose)
565 regulator_show(dev, ret);
566 if (ret == -ENOSYS)
567 ret = 0;
568 }
569
570 return ret;
571}
572
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200573UCLASS_DRIVER(regulator) = {
574 .id = UCLASS_REGULATOR,
575 .name = "regulator",
576 .post_bind = regulator_post_bind,
577 .pre_probe = regulator_pre_probe,
Simon Glass33b2efb2020-12-03 16:55:22 -0700578 .per_device_plat_auto = sizeof(struct dm_regulator_uclass_plat),
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200579};