blob: 3a6ba69f6d5ff3495431353101d046ff8d67a526 [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 <common.h>
Przemyslaw Marczak08edd002015-04-20 20:07:42 +020010#include <errno.h>
11#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Przemyslaw Marczak08edd002015-04-20 20:07:42 +020013#include <dm/uclass-internal.h>
Simon Glassdbd79542020-05-10 11:40:11 -060014#include <linux/delay.h>
Przemyslaw Marczak08edd002015-04-20 20:07:42 +020015#include <power/pmic.h>
16#include <power/regulator.h>
17
Przemyslaw Marczak08edd002015-04-20 20:07:42 +020018int regulator_mode(struct udevice *dev, struct dm_regulator_mode **modep)
19{
Simon Glass71fa5b42020-12-03 16:55:18 -070020 struct dm_regulator_uclass_plat *uc_pdata;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +020021
22 *modep = NULL;
23
Simon Glass71fa5b42020-12-03 16:55:18 -070024 uc_pdata = dev_get_uclass_plat(dev);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +020025 if (!uc_pdata)
26 return -ENXIO;
27
28 *modep = uc_pdata->mode;
29 return uc_pdata->mode_count;
30}
31
32int regulator_get_value(struct udevice *dev)
33{
34 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
35
36 if (!ops || !ops->get_value)
37 return -ENOSYS;
38
39 return ops->get_value(dev);
40}
41
Krzysztof Kozlowskie2fa3972019-03-06 19:37:54 +010042static void regulator_set_value_ramp_delay(struct udevice *dev, int old_uV,
43 int new_uV, unsigned int ramp_delay)
44{
45 int delay = DIV_ROUND_UP(abs(new_uV - old_uV), ramp_delay);
46
47 debug("regulator %s: delay %u us (%d uV -> %d uV)\n", dev->name, delay,
48 old_uV, new_uV);
49
50 udelay(delay);
51}
52
Przemyslaw Marczak08edd002015-04-20 20:07:42 +020053int regulator_set_value(struct udevice *dev, int uV)
54{
55 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
Simon Glass71fa5b42020-12-03 16:55:18 -070056 struct dm_regulator_uclass_plat *uc_pdata;
Krzysztof Kozlowskie2fa3972019-03-06 19:37:54 +010057 int ret, old_uV = uV, is_enabled = 0;
Keerthyc6e66692016-10-26 13:42:31 +053058
Simon Glass71fa5b42020-12-03 16:55:18 -070059 uc_pdata = dev_get_uclass_plat(dev);
Keerthyc6e66692016-10-26 13:42:31 +053060 if (uc_pdata->min_uV != -ENODATA && uV < uc_pdata->min_uV)
61 return -EINVAL;
62 if (uc_pdata->max_uV != -ENODATA && uV > uc_pdata->max_uV)
63 return -EINVAL;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +020064
65 if (!ops || !ops->set_value)
66 return -ENOSYS;
67
Krzysztof Kozlowskie2fa3972019-03-06 19:37:54 +010068 if (uc_pdata->ramp_delay) {
69 is_enabled = regulator_get_enable(dev);
70 old_uV = regulator_get_value(dev);
71 }
72
73 ret = ops->set_value(dev, uV);
74
75 if (!ret) {
76 if (uc_pdata->ramp_delay && old_uV > 0 && is_enabled)
77 regulator_set_value_ramp_delay(dev, old_uV, uV,
78 uc_pdata->ramp_delay);
79 }
80
81 return ret;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +020082}
83
Joseph Chenbb511322019-09-26 15:43:52 +080084int regulator_set_suspend_value(struct udevice *dev, int uV)
85{
86 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
Simon Glass71fa5b42020-12-03 16:55:18 -070087 struct dm_regulator_uclass_plat *uc_pdata;
Joseph Chenbb511322019-09-26 15:43:52 +080088
Simon Glass71fa5b42020-12-03 16:55:18 -070089 uc_pdata = dev_get_uclass_plat(dev);
Joseph Chenbb511322019-09-26 15:43:52 +080090 if (uc_pdata->min_uV != -ENODATA && uV < uc_pdata->min_uV)
91 return -EINVAL;
92 if (uc_pdata->max_uV != -ENODATA && uV > uc_pdata->max_uV)
93 return -EINVAL;
94
95 if (!ops->set_suspend_value)
96 return -ENOSYS;
97
98 return ops->set_suspend_value(dev, uV);
99}
100
101int regulator_get_suspend_value(struct udevice *dev)
102{
103 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
104
105 if (!ops->get_suspend_value)
106 return -ENOSYS;
107
108 return ops->get_suspend_value(dev);
109}
110
Keerthy162c02e2016-10-26 13:42:30 +0530111/*
112 * To be called with at most caution as there is no check
113 * before setting the actual voltage value.
114 */
115int regulator_set_value_force(struct udevice *dev, int uV)
116{
117 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
118
119 if (!ops || !ops->set_value)
120 return -ENOSYS;
121
122 return ops->set_value(dev, uV);
123}
124
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200125int regulator_get_current(struct udevice *dev)
126{
127 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
128
129 if (!ops || !ops->get_current)
130 return -ENOSYS;
131
132 return ops->get_current(dev);
133}
134
135int regulator_set_current(struct udevice *dev, int uA)
136{
137 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
Simon Glass71fa5b42020-12-03 16:55:18 -0700138 struct dm_regulator_uclass_plat *uc_pdata;
Keerthyce152be2016-10-26 13:42:32 +0530139
Simon Glass71fa5b42020-12-03 16:55:18 -0700140 uc_pdata = dev_get_uclass_plat(dev);
Keerthyce152be2016-10-26 13:42:32 +0530141 if (uc_pdata->min_uA != -ENODATA && uA < uc_pdata->min_uA)
142 return -EINVAL;
143 if (uc_pdata->max_uA != -ENODATA && uA > uc_pdata->max_uA)
144 return -EINVAL;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200145
146 if (!ops || !ops->set_current)
147 return -ENOSYS;
148
149 return ops->set_current(dev, uA);
150}
151
Keerthy23be7fb2017-06-13 09:53:45 +0530152int regulator_get_enable(struct udevice *dev)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200153{
154 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
155
156 if (!ops || !ops->get_enable)
157 return -ENOSYS;
158
159 return ops->get_enable(dev);
160}
161
162int regulator_set_enable(struct udevice *dev, bool enable)
163{
164 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
Simon Glass71fa5b42020-12-03 16:55:18 -0700165 struct dm_regulator_uclass_plat *uc_pdata;
Krzysztof Kozlowskie2fa3972019-03-06 19:37:54 +0100166 int ret, old_enable = 0;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200167
168 if (!ops || !ops->set_enable)
169 return -ENOSYS;
170
Simon Glass71fa5b42020-12-03 16:55:18 -0700171 uc_pdata = dev_get_uclass_plat(dev);
Patrick Delaunayd7585f22018-11-15 13:45:31 +0100172 if (!enable && uc_pdata->always_on)
Lokesh Vutlaf870da22019-01-11 15:15:50 +0530173 return -EACCES;
Patrick Delaunayd7585f22018-11-15 13:45:31 +0100174
Krzysztof Kozlowskie2fa3972019-03-06 19:37:54 +0100175 if (uc_pdata->ramp_delay)
176 old_enable = regulator_get_enable(dev);
177
178 ret = ops->set_enable(dev, enable);
179 if (!ret) {
180 if (uc_pdata->ramp_delay && !old_enable && enable) {
181 int uV = regulator_get_value(dev);
182
183 if (uV > 0) {
184 regulator_set_value_ramp_delay(dev, 0, uV,
185 uc_pdata->ramp_delay);
186 }
187 }
188 }
189
190 return ret;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200191}
192
Lokesh Vutla7106b782019-01-11 15:15:51 +0530193int regulator_set_enable_if_allowed(struct udevice *dev, bool enable)
194{
195 int ret;
196
197 ret = regulator_set_enable(dev, enable);
198 if (ret == -ENOSYS || ret == -EACCES)
199 return 0;
Eugen Hristev5a1c0f32023-04-19 16:45:26 +0300200 /* if we want to disable but it's in use by someone else */
201 if (!enable && ret == -EBUSY)
202 return 0;
203 /* if it's already enabled/disabled */
204 if (ret == -EALREADY)
205 return 0;
Lokesh Vutla7106b782019-01-11 15:15:51 +0530206
207 return ret;
208}
209
Joseph Chenbb511322019-09-26 15:43:52 +0800210int regulator_set_suspend_enable(struct udevice *dev, bool enable)
211{
212 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
213
214 if (!ops->set_suspend_enable)
215 return -ENOSYS;
216
217 return ops->set_suspend_enable(dev, enable);
218}
219
220int regulator_get_suspend_enable(struct udevice *dev)
221{
222 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
223
224 if (!ops->get_suspend_enable)
225 return -ENOSYS;
226
227 return ops->get_suspend_enable(dev);
228}
229
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200230int regulator_get_mode(struct udevice *dev)
231{
232 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
233
234 if (!ops || !ops->get_mode)
235 return -ENOSYS;
236
237 return ops->get_mode(dev);
238}
239
240int regulator_set_mode(struct udevice *dev, int mode)
241{
242 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
243
244 if (!ops || !ops->set_mode)
245 return -ENOSYS;
246
247 return ops->set_mode(dev, mode);
248}
249
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200250int regulator_get_by_platname(const char *plat_name, struct udevice **devp)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200251{
Simon Glass71fa5b42020-12-03 16:55:18 -0700252 struct dm_regulator_uclass_plat *uc_pdata;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200253 struct udevice *dev;
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200254 int ret;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200255
256 *devp = NULL;
257
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200258 for (ret = uclass_find_first_device(UCLASS_REGULATOR, &dev); dev;
259 ret = uclass_find_next_device(&dev)) {
Simon Glassfc3ebf12017-05-31 17:57:15 -0600260 if (ret) {
261 debug("regulator %s, ret=%d\n", dev->name, ret);
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200262 continue;
Simon Glassfc3ebf12017-05-31 17:57:15 -0600263 }
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200264
Simon Glass71fa5b42020-12-03 16:55:18 -0700265 uc_pdata = dev_get_uclass_plat(dev);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200266 if (!uc_pdata || strcmp(plat_name, uc_pdata->name))
267 continue;
268
269 return uclass_get_device_tail(dev, 0, devp);
270 }
271
Simon Glassfc3ebf12017-05-31 17:57:15 -0600272 debug("%s: can't find: %s, ret=%d\n", __func__, plat_name, ret);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200273
274 return -ENODEV;
275}
276
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200277int regulator_get_by_devname(const char *devname, struct udevice **devp)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200278{
279 return uclass_get_device_by_name(UCLASS_REGULATOR, devname, devp);
280}
281
Przemyslaw Marczakc9001032015-10-27 13:07:59 +0100282int device_get_supply_regulator(struct udevice *dev, const char *supply_name,
283 struct udevice **devp)
284{
285 return uclass_get_device_by_phandle(UCLASS_REGULATOR, dev,
286 supply_name, devp);
287}
288
Simon Glass46cb8242015-06-23 15:38:58 -0600289int regulator_autoset(struct udevice *dev)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200290{
Simon Glass71fa5b42020-12-03 16:55:18 -0700291 struct dm_regulator_uclass_plat *uc_pdata;
Simon Glass46cb8242015-06-23 15:38:58 -0600292 int ret = 0;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200293
Simon Glass71fa5b42020-12-03 16:55:18 -0700294 uc_pdata = dev_get_uclass_plat(dev);
Joseph Chenbb511322019-09-26 15:43:52 +0800295
296 ret = regulator_set_suspend_enable(dev, uc_pdata->suspend_on);
John Keeping6ec6a072022-09-08 12:38:35 +0100297 if (ret == -ENOSYS)
298 ret = 0;
299
Joseph Chenbb511322019-09-26 15:43:52 +0800300 if (!ret && uc_pdata->suspend_on) {
301 ret = regulator_set_suspend_value(dev, uc_pdata->suspend_uV);
John Keeping6ec6a072022-09-08 12:38:35 +0100302 if (ret == -ENOSYS)
303 ret = 0;
304
305 if (ret)
Joseph Chenbb511322019-09-26 15:43:52 +0800306 return ret;
307 }
308
Simon Glass46cb8242015-06-23 15:38:58 -0600309 if (!uc_pdata->always_on && !uc_pdata->boot_on)
310 return -EMEDIUMTYPE;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200311
Sven Schwermerd033cbf2019-06-12 08:32:38 +0200312 if (uc_pdata->type == REGULATOR_TYPE_FIXED)
313 return regulator_set_enable(dev, true);
314
Simon Glass46cb8242015-06-23 15:38:58 -0600315 if (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UV)
316 ret = regulator_set_value(dev, uc_pdata->min_uV);
Joseph Chenbb511322019-09-26 15:43:52 +0800317 if (uc_pdata->init_uV > 0)
318 ret = regulator_set_value(dev, uc_pdata->init_uV);
Simon Glass46cb8242015-06-23 15:38:58 -0600319 if (!ret && (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UA))
320 ret = regulator_set_current(dev, uc_pdata->min_uA);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200321
322 if (!ret)
Simon Glass46cb8242015-06-23 15:38:58 -0600323 ret = regulator_set_enable(dev, true);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200324
325 return ret;
326}
327
Konstantin Porotchkin621dab52017-05-29 15:59:38 +0300328int regulator_unset(struct udevice *dev)
329{
330 struct dm_regulator_uclass_plat *uc_pdata;
331
332 uc_pdata = dev_get_uclass_plat(dev);
333 if (uc_pdata && uc_pdata->force_off)
334 return regulator_set_enable(dev, false);
335
336 return -EMEDIUMTYPE;
337}
338
Simon Glass46cb8242015-06-23 15:38:58 -0600339static void regulator_show(struct udevice *dev, int ret)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200340{
Simon Glass71fa5b42020-12-03 16:55:18 -0700341 struct dm_regulator_uclass_plat *uc_pdata;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200342
Simon Glass71fa5b42020-12-03 16:55:18 -0700343 uc_pdata = dev_get_uclass_plat(dev);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200344
Simon Glass46cb8242015-06-23 15:38:58 -0600345 printf("%s@%s: ", dev->name, uc_pdata->name);
346 if (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UV)
347 printf("set %d uV", uc_pdata->min_uV);
348 if (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UA)
349 printf("; set %d uA", uc_pdata->min_uA);
350 printf("; enabling");
351 if (ret)
Simon Glass46ad8cb2016-01-21 19:43:58 -0700352 printf(" (ret: %d)", ret);
Simon Glass46cb8242015-06-23 15:38:58 -0600353 printf("\n");
354}
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200355
Simon Glass46cb8242015-06-23 15:38:58 -0600356int regulator_autoset_by_name(const char *platname, struct udevice **devp)
357{
358 struct udevice *dev;
359 int ret;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200360
Simon Glass46cb8242015-06-23 15:38:58 -0600361 ret = regulator_get_by_platname(platname, &dev);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200362 if (devp)
363 *devp = dev;
Simon Glass46cb8242015-06-23 15:38:58 -0600364 if (ret) {
Simon Glassfc3ebf12017-05-31 17:57:15 -0600365 debug("Can get the regulator: %s (err=%d)\n", platname, ret);
Simon Glass46cb8242015-06-23 15:38:58 -0600366 return ret;
367 }
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200368
Simon Glass46cb8242015-06-23 15:38:58 -0600369 return regulator_autoset(dev);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200370}
371
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200372int regulator_list_autoset(const char *list_platname[],
373 struct udevice *list_devp[],
374 bool verbose)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200375{
376 struct udevice *dev;
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200377 int error = 0, i = 0, ret;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200378
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200379 while (list_platname[i]) {
Simon Glass46cb8242015-06-23 15:38:58 -0600380 ret = regulator_autoset_by_name(list_platname[i], &dev);
381 if (ret != -EMEDIUMTYPE && verbose)
382 regulator_show(dev, ret);
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200383 if (ret & !error)
384 error = ret;
385
386 if (list_devp)
387 list_devp[i] = dev;
388
389 i++;
390 }
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200391
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200392 return error;
393}
394
395static bool regulator_name_is_unique(struct udevice *check_dev,
396 const char *check_name)
397{
Simon Glass71fa5b42020-12-03 16:55:18 -0700398 struct dm_regulator_uclass_plat *uc_pdata;
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200399 struct udevice *dev;
400 int check_len = strlen(check_name);
401 int ret;
402 int len;
403
404 for (ret = uclass_find_first_device(UCLASS_REGULATOR, &dev); dev;
405 ret = uclass_find_next_device(&dev)) {
406 if (ret || dev == check_dev)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200407 continue;
408
Simon Glass71fa5b42020-12-03 16:55:18 -0700409 uc_pdata = dev_get_uclass_plat(dev);
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200410 len = strlen(uc_pdata->name);
411 if (len != check_len)
412 continue;
413
414 if (!strcmp(uc_pdata->name, check_name))
415 return false;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200416 }
417
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200418 return true;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200419}
420
421static int regulator_post_bind(struct udevice *dev)
422{
Simon Glass71fa5b42020-12-03 16:55:18 -0700423 struct dm_regulator_uclass_plat *uc_pdata;
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200424 const char *property = "regulator-name";
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200425
Simon Glass71fa5b42020-12-03 16:55:18 -0700426 uc_pdata = dev_get_uclass_plat(dev);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200427
428 /* Regulator's mandatory constraint */
Simon Glasseeb99de2017-05-18 20:09:34 -0600429 uc_pdata->name = dev_read_string(dev, property);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200430 if (!uc_pdata->name) {
Simon Glasseeb99de2017-05-18 20:09:34 -0600431 debug("%s: dev '%s' has no property '%s'\n",
432 __func__, dev->name, property);
433 uc_pdata->name = dev_read_name(dev);
Peng Fancd672d42015-08-07 16:43:42 +0800434 if (!uc_pdata->name)
435 return -EINVAL;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200436 }
437
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200438 if (regulator_name_is_unique(dev, uc_pdata->name))
439 return 0;
440
Simon Glasseeb99de2017-05-18 20:09:34 -0600441 debug("'%s' of dev: '%s', has nonunique value: '%s\n",
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200442 property, dev->name, uc_pdata->name);
443
444 return -EINVAL;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200445}
446
447static int regulator_pre_probe(struct udevice *dev)
448{
Simon Glass71fa5b42020-12-03 16:55:18 -0700449 struct dm_regulator_uclass_plat *uc_pdata;
Joseph Chenbb511322019-09-26 15:43:52 +0800450 ofnode node;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200451
Simon Glass71fa5b42020-12-03 16:55:18 -0700452 uc_pdata = dev_get_uclass_plat(dev);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200453 if (!uc_pdata)
454 return -ENXIO;
455
456 /* Regulator's optional constraints */
Simon Glasseeb99de2017-05-18 20:09:34 -0600457 uc_pdata->min_uV = dev_read_u32_default(dev, "regulator-min-microvolt",
458 -ENODATA);
459 uc_pdata->max_uV = dev_read_u32_default(dev, "regulator-max-microvolt",
460 -ENODATA);
Joseph Chenbb511322019-09-26 15:43:52 +0800461 uc_pdata->init_uV = dev_read_u32_default(dev, "regulator-init-microvolt",
462 -ENODATA);
Simon Glasseeb99de2017-05-18 20:09:34 -0600463 uc_pdata->min_uA = dev_read_u32_default(dev, "regulator-min-microamp",
464 -ENODATA);
465 uc_pdata->max_uA = dev_read_u32_default(dev, "regulator-max-microamp",
466 -ENODATA);
467 uc_pdata->always_on = dev_read_bool(dev, "regulator-always-on");
468 uc_pdata->boot_on = dev_read_bool(dev, "regulator-boot-on");
Krzysztof Kozlowskie2fa3972019-03-06 19:37:54 +0100469 uc_pdata->ramp_delay = dev_read_u32_default(dev, "regulator-ramp-delay",
470 0);
Konstantin Porotchkin621dab52017-05-29 15:59:38 +0300471 uc_pdata->force_off = dev_read_bool(dev, "regulator-force-boot-off");
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200472
Joseph Chenbb511322019-09-26 15:43:52 +0800473 node = dev_read_subnode(dev, "regulator-state-mem");
474 if (ofnode_valid(node)) {
475 uc_pdata->suspend_on = !ofnode_read_bool(node, "regulator-off-in-suspend");
476 if (ofnode_read_u32(node, "regulator-suspend-microvolt", &uc_pdata->suspend_uV))
477 uc_pdata->suspend_uV = uc_pdata->max_uV;
478 } else {
479 uc_pdata->suspend_on = true;
480 uc_pdata->suspend_uV = uc_pdata->max_uV;
481 }
482
Simon Glass991f9bc2015-06-23 15:38:57 -0600483 /* Those values are optional (-ENODATA if unset) */
484 if ((uc_pdata->min_uV != -ENODATA) &&
485 (uc_pdata->max_uV != -ENODATA) &&
486 (uc_pdata->min_uV == uc_pdata->max_uV))
487 uc_pdata->flags |= REGULATOR_FLAG_AUTOSET_UV;
488
489 /* Those values are optional (-ENODATA if unset) */
490 if ((uc_pdata->min_uA != -ENODATA) &&
491 (uc_pdata->max_uA != -ENODATA) &&
492 (uc_pdata->min_uA == uc_pdata->max_uA))
493 uc_pdata->flags |= REGULATOR_FLAG_AUTOSET_UA;
494
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200495 return 0;
496}
497
Simon Glassefeeb2c2015-06-23 15:38:59 -0600498int regulators_enable_boot_on(bool verbose)
499{
500 struct udevice *dev;
501 struct uclass *uc;
502 int ret;
503
504 ret = uclass_get(UCLASS_REGULATOR, &uc);
505 if (ret)
506 return ret;
507 for (uclass_first_device(UCLASS_REGULATOR, &dev);
Simon Glassc7298e72016-02-11 13:23:26 -0700508 dev;
Simon Glassefeeb2c2015-06-23 15:38:59 -0600509 uclass_next_device(&dev)) {
510 ret = regulator_autoset(dev);
Simon Glassf55c9512015-07-02 18:16:06 -0600511 if (ret == -EMEDIUMTYPE) {
512 ret = 0;
Simon Glassefeeb2c2015-06-23 15:38:59 -0600513 continue;
Simon Glassf55c9512015-07-02 18:16:06 -0600514 }
Simon Glassefeeb2c2015-06-23 15:38:59 -0600515 if (verbose)
516 regulator_show(dev, ret);
Simon Glassd6eddad2016-01-21 19:43:59 -0700517 if (ret == -ENOSYS)
518 ret = 0;
Simon Glassefeeb2c2015-06-23 15:38:59 -0600519 }
520
521 return ret;
522}
523
Konstantin Porotchkin621dab52017-05-29 15:59:38 +0300524int regulators_enable_boot_off(bool verbose)
525{
526 struct udevice *dev;
527 struct uclass *uc;
528 int ret;
529
530 ret = uclass_get(UCLASS_REGULATOR, &uc);
531 if (ret)
532 return ret;
533 for (uclass_first_device(UCLASS_REGULATOR, &dev);
534 dev;
535 uclass_next_device(&dev)) {
536 ret = regulator_unset(dev);
537 if (ret == -EMEDIUMTYPE) {
538 ret = 0;
539 continue;
540 }
541 if (verbose)
542 regulator_show(dev, ret);
543 if (ret == -ENOSYS)
544 ret = 0;
545 }
546
547 return ret;
548}
549
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200550UCLASS_DRIVER(regulator) = {
551 .id = UCLASS_REGULATOR,
552 .name = "regulator",
553 .post_bind = regulator_post_bind,
554 .pre_probe = regulator_pre_probe,
Simon Glass33b2efb2020-12-03 16:55:22 -0700555 .per_device_plat_auto = sizeof(struct dm_regulator_uclass_plat),
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200556};