blob: decd0802c8489af4196c965baf528a9fc29651a7 [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
Marek Vasutae4326a2024-09-27 01:14:13 +0200317 if (uc_pdata->force_off) {
318 ret = regulator_set_enable(dev, false);
319 goto out;
320 }
321
Jonas Karlmanc2ef8812023-08-21 22:30:24 +0000322 if (!uc_pdata->always_on && !uc_pdata->boot_on) {
323 ret = -EMEDIUMTYPE;
324 goto out;
325 }
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200326
Jonas Karlmanc2ef8812023-08-21 22:30:24 +0000327 if (uc_pdata->type == REGULATOR_TYPE_FIXED) {
328 ret = regulator_set_enable(dev, true);
329 goto out;
330 }
Sven Schwermerd033cbf2019-06-12 08:32:38 +0200331
Simon Glass46cb8242015-06-23 15:38:58 -0600332 if (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UV)
333 ret = regulator_set_value(dev, uc_pdata->min_uV);
Joseph Chenbb511322019-09-26 15:43:52 +0800334 if (uc_pdata->init_uV > 0)
335 ret = regulator_set_value(dev, uc_pdata->init_uV);
Simon Glass46cb8242015-06-23 15:38:58 -0600336 if (!ret && (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UA))
337 ret = regulator_set_current(dev, uc_pdata->min_uA);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200338
339 if (!ret)
Simon Glass46cb8242015-06-23 15:38:58 -0600340 ret = regulator_set_enable(dev, true);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200341
Jonas Karlmanc2ef8812023-08-21 22:30:24 +0000342out:
343 uc_pdata->flags |= REGULATOR_FLAG_AUTOSET_DONE;
344
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200345 return ret;
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);
Marek Vasut152fe722024-09-27 01:14:12 +0200436 uc_pdata->always_on = dev_read_bool(dev, "regulator-always-on");
437 uc_pdata->boot_on = dev_read_bool(dev, "regulator-boot-on");
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200438
439 /* Regulator's mandatory constraint */
Simon Glasseeb99de2017-05-18 20:09:34 -0600440 uc_pdata->name = dev_read_string(dev, property);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200441 if (!uc_pdata->name) {
Simon Glasseeb99de2017-05-18 20:09:34 -0600442 debug("%s: dev '%s' has no property '%s'\n",
443 __func__, dev->name, property);
444 uc_pdata->name = dev_read_name(dev);
Peng Fancd672d42015-08-07 16:43:42 +0800445 if (!uc_pdata->name)
446 return -EINVAL;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200447 }
448
Marek Vasut152fe722024-09-27 01:14:12 +0200449 if (!regulator_name_is_unique(dev, uc_pdata->name)) {
450 debug("'%s' of dev: '%s', has nonunique value: '%s\n",
451 property, dev->name, uc_pdata->name);
452 return -EINVAL;
453 }
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200454
Marek Vasut152fe722024-09-27 01:14:12 +0200455 /*
456 * In case the regulator has regulator-always-on or
457 * regulator-boot-on DT property, trigger probe() to
458 * configure its default state during startup.
459 */
460 if (uc_pdata->always_on || uc_pdata->boot_on)
461 dev_or_flags(dev, DM_FLAG_PROBE_AFTER_BIND);
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200462
Marek Vasut152fe722024-09-27 01:14:12 +0200463 return 0;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200464}
465
466static int regulator_pre_probe(struct udevice *dev)
467{
Simon Glass71fa5b42020-12-03 16:55:18 -0700468 struct dm_regulator_uclass_plat *uc_pdata;
Joseph Chenbb511322019-09-26 15:43:52 +0800469 ofnode node;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200470
Simon Glass71fa5b42020-12-03 16:55:18 -0700471 uc_pdata = dev_get_uclass_plat(dev);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200472 if (!uc_pdata)
473 return -ENXIO;
474
475 /* Regulator's optional constraints */
Simon Glasseeb99de2017-05-18 20:09:34 -0600476 uc_pdata->min_uV = dev_read_u32_default(dev, "regulator-min-microvolt",
477 -ENODATA);
478 uc_pdata->max_uV = dev_read_u32_default(dev, "regulator-max-microvolt",
479 -ENODATA);
Joseph Chenbb511322019-09-26 15:43:52 +0800480 uc_pdata->init_uV = dev_read_u32_default(dev, "regulator-init-microvolt",
481 -ENODATA);
Simon Glasseeb99de2017-05-18 20:09:34 -0600482 uc_pdata->min_uA = dev_read_u32_default(dev, "regulator-min-microamp",
483 -ENODATA);
484 uc_pdata->max_uA = dev_read_u32_default(dev, "regulator-max-microamp",
485 -ENODATA);
Krzysztof Kozlowskie2fa3972019-03-06 19:37:54 +0100486 uc_pdata->ramp_delay = dev_read_u32_default(dev, "regulator-ramp-delay",
487 0);
Konstantin Porotchkin621dab52017-05-29 15:59:38 +0300488 uc_pdata->force_off = dev_read_bool(dev, "regulator-force-boot-off");
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200489
Joseph Chenbb511322019-09-26 15:43:52 +0800490 node = dev_read_subnode(dev, "regulator-state-mem");
491 if (ofnode_valid(node)) {
492 uc_pdata->suspend_on = !ofnode_read_bool(node, "regulator-off-in-suspend");
493 if (ofnode_read_u32(node, "regulator-suspend-microvolt", &uc_pdata->suspend_uV))
494 uc_pdata->suspend_uV = uc_pdata->max_uV;
495 } else {
496 uc_pdata->suspend_on = true;
497 uc_pdata->suspend_uV = uc_pdata->max_uV;
498 }
499
Simon Glass991f9bc2015-06-23 15:38:57 -0600500 /* Those values are optional (-ENODATA if unset) */
501 if ((uc_pdata->min_uV != -ENODATA) &&
502 (uc_pdata->max_uV != -ENODATA) &&
503 (uc_pdata->min_uV == uc_pdata->max_uV))
504 uc_pdata->flags |= REGULATOR_FLAG_AUTOSET_UV;
505
506 /* Those values are optional (-ENODATA if unset) */
507 if ((uc_pdata->min_uA != -ENODATA) &&
508 (uc_pdata->max_uA != -ENODATA) &&
509 (uc_pdata->min_uA == uc_pdata->max_uA))
510 uc_pdata->flags |= REGULATOR_FLAG_AUTOSET_UA;
511
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200512 return 0;
513}
514
Marek Vasutae4326a2024-09-27 01:14:13 +0200515static int regulator_post_probe(struct udevice *dev)
Simon Glassefeeb2c2015-06-23 15:38:59 -0600516{
Simon Glassefeeb2c2015-06-23 15:38:59 -0600517 int ret;
518
Marek Vasutae4326a2024-09-27 01:14:13 +0200519 ret = regulator_autoset(dev);
520 if (ret && ret != -EMEDIUMTYPE && ret != -EALREADY && ret != ENOSYS)
Simon Glassefeeb2c2015-06-23 15:38:59 -0600521 return ret;
Simon Glassefeeb2c2015-06-23 15:38:59 -0600522
Marek Vasutae4326a2024-09-27 01:14:13 +0200523 if (_DEBUG)
524 regulator_show(dev, ret);
525
526 return 0;
Simon Glassefeeb2c2015-06-23 15:38:59 -0600527}
528
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200529UCLASS_DRIVER(regulator) = {
530 .id = UCLASS_REGULATOR,
531 .name = "regulator",
532 .post_bind = regulator_post_bind,
533 .pre_probe = regulator_pre_probe,
Marek Vasutae4326a2024-09-27 01:14:13 +0200534 .post_probe = regulator_post_probe,
Simon Glass33b2efb2020-12-03 16:55:22 -0700535 .per_device_plat_auto = sizeof(struct dm_regulator_uclass_plat),
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200536};