blob: 9118b8eb39e34ff4266d2d1f8fc9a9f562565885 [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
Przemyslaw Marczak08edd002015-04-20 20:07:42 +02007#include <common.h>
Przemyslaw Marczak08edd002015-04-20 20:07:42 +02008#include <errno.h>
9#include <dm.h>
10#include <dm/uclass-internal.h>
11#include <power/pmic.h>
12#include <power/regulator.h>
13
Przemyslaw Marczak08edd002015-04-20 20:07:42 +020014int regulator_mode(struct udevice *dev, struct dm_regulator_mode **modep)
15{
16 struct dm_regulator_uclass_platdata *uc_pdata;
17
18 *modep = NULL;
19
20 uc_pdata = dev_get_uclass_platdata(dev);
21 if (!uc_pdata)
22 return -ENXIO;
23
24 *modep = uc_pdata->mode;
25 return uc_pdata->mode_count;
26}
27
28int regulator_get_value(struct udevice *dev)
29{
30 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
31
32 if (!ops || !ops->get_value)
33 return -ENOSYS;
34
35 return ops->get_value(dev);
36}
37
Krzysztof Kozlowskie2fa3972019-03-06 19:37:54 +010038static void regulator_set_value_ramp_delay(struct udevice *dev, int old_uV,
39 int new_uV, unsigned int ramp_delay)
40{
41 int delay = DIV_ROUND_UP(abs(new_uV - old_uV), ramp_delay);
42
43 debug("regulator %s: delay %u us (%d uV -> %d uV)\n", dev->name, delay,
44 old_uV, new_uV);
45
46 udelay(delay);
47}
48
Przemyslaw Marczak08edd002015-04-20 20:07:42 +020049int regulator_set_value(struct udevice *dev, int uV)
50{
51 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
Keerthyc6e66692016-10-26 13:42:31 +053052 struct dm_regulator_uclass_platdata *uc_pdata;
Krzysztof Kozlowskie2fa3972019-03-06 19:37:54 +010053 int ret, old_uV = uV, is_enabled = 0;
Keerthyc6e66692016-10-26 13:42:31 +053054
55 uc_pdata = dev_get_uclass_platdata(dev);
56 if (uc_pdata->min_uV != -ENODATA && uV < uc_pdata->min_uV)
57 return -EINVAL;
58 if (uc_pdata->max_uV != -ENODATA && uV > uc_pdata->max_uV)
59 return -EINVAL;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +020060
61 if (!ops || !ops->set_value)
62 return -ENOSYS;
63
Krzysztof Kozlowskie2fa3972019-03-06 19:37:54 +010064 if (uc_pdata->ramp_delay) {
65 is_enabled = regulator_get_enable(dev);
66 old_uV = regulator_get_value(dev);
67 }
68
69 ret = ops->set_value(dev, uV);
70
71 if (!ret) {
72 if (uc_pdata->ramp_delay && old_uV > 0 && is_enabled)
73 regulator_set_value_ramp_delay(dev, old_uV, uV,
74 uc_pdata->ramp_delay);
75 }
76
77 return ret;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +020078}
79
Keerthy162c02e2016-10-26 13:42:30 +053080/*
81 * To be called with at most caution as there is no check
82 * before setting the actual voltage value.
83 */
84int regulator_set_value_force(struct udevice *dev, int uV)
85{
86 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
87
88 if (!ops || !ops->set_value)
89 return -ENOSYS;
90
91 return ops->set_value(dev, uV);
92}
93
Przemyslaw Marczak08edd002015-04-20 20:07:42 +020094int regulator_get_current(struct udevice *dev)
95{
96 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
97
98 if (!ops || !ops->get_current)
99 return -ENOSYS;
100
101 return ops->get_current(dev);
102}
103
104int regulator_set_current(struct udevice *dev, int uA)
105{
106 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
Keerthyce152be2016-10-26 13:42:32 +0530107 struct dm_regulator_uclass_platdata *uc_pdata;
108
109 uc_pdata = dev_get_uclass_platdata(dev);
110 if (uc_pdata->min_uA != -ENODATA && uA < uc_pdata->min_uA)
111 return -EINVAL;
112 if (uc_pdata->max_uA != -ENODATA && uA > uc_pdata->max_uA)
113 return -EINVAL;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200114
115 if (!ops || !ops->set_current)
116 return -ENOSYS;
117
118 return ops->set_current(dev, uA);
119}
120
Keerthy23be7fb2017-06-13 09:53:45 +0530121int regulator_get_enable(struct udevice *dev)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200122{
123 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
124
125 if (!ops || !ops->get_enable)
126 return -ENOSYS;
127
128 return ops->get_enable(dev);
129}
130
131int regulator_set_enable(struct udevice *dev, bool enable)
132{
133 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
Patrick Delaunayd7585f22018-11-15 13:45:31 +0100134 struct dm_regulator_uclass_platdata *uc_pdata;
Krzysztof Kozlowskie2fa3972019-03-06 19:37:54 +0100135 int ret, old_enable = 0;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200136
137 if (!ops || !ops->set_enable)
138 return -ENOSYS;
139
Patrick Delaunayd7585f22018-11-15 13:45:31 +0100140 uc_pdata = dev_get_uclass_platdata(dev);
141 if (!enable && uc_pdata->always_on)
Lokesh Vutlaf870da22019-01-11 15:15:50 +0530142 return -EACCES;
Patrick Delaunayd7585f22018-11-15 13:45:31 +0100143
Krzysztof Kozlowskie2fa3972019-03-06 19:37:54 +0100144 if (uc_pdata->ramp_delay)
145 old_enable = regulator_get_enable(dev);
146
147 ret = ops->set_enable(dev, enable);
148 if (!ret) {
149 if (uc_pdata->ramp_delay && !old_enable && enable) {
150 int uV = regulator_get_value(dev);
151
152 if (uV > 0) {
153 regulator_set_value_ramp_delay(dev, 0, uV,
154 uc_pdata->ramp_delay);
155 }
156 }
157 }
158
159 return ret;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200160}
161
Lokesh Vutla7106b782019-01-11 15:15:51 +0530162int regulator_set_enable_if_allowed(struct udevice *dev, bool enable)
163{
164 int ret;
165
166 ret = regulator_set_enable(dev, enable);
167 if (ret == -ENOSYS || ret == -EACCES)
168 return 0;
169
170 return ret;
171}
172
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200173int regulator_get_mode(struct udevice *dev)
174{
175 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
176
177 if (!ops || !ops->get_mode)
178 return -ENOSYS;
179
180 return ops->get_mode(dev);
181}
182
183int regulator_set_mode(struct udevice *dev, int mode)
184{
185 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
186
187 if (!ops || !ops->set_mode)
188 return -ENOSYS;
189
190 return ops->set_mode(dev, mode);
191}
192
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200193int regulator_get_by_platname(const char *plat_name, struct udevice **devp)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200194{
195 struct dm_regulator_uclass_platdata *uc_pdata;
196 struct udevice *dev;
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200197 int ret;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200198
199 *devp = NULL;
200
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200201 for (ret = uclass_find_first_device(UCLASS_REGULATOR, &dev); dev;
202 ret = uclass_find_next_device(&dev)) {
Simon Glassfc3ebf12017-05-31 17:57:15 -0600203 if (ret) {
204 debug("regulator %s, ret=%d\n", dev->name, ret);
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200205 continue;
Simon Glassfc3ebf12017-05-31 17:57:15 -0600206 }
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200207
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200208 uc_pdata = dev_get_uclass_platdata(dev);
209 if (!uc_pdata || strcmp(plat_name, uc_pdata->name))
210 continue;
211
212 return uclass_get_device_tail(dev, 0, devp);
213 }
214
Simon Glassfc3ebf12017-05-31 17:57:15 -0600215 debug("%s: can't find: %s, ret=%d\n", __func__, plat_name, ret);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200216
217 return -ENODEV;
218}
219
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200220int regulator_get_by_devname(const char *devname, struct udevice **devp)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200221{
222 return uclass_get_device_by_name(UCLASS_REGULATOR, devname, devp);
223}
224
Przemyslaw Marczakc9001032015-10-27 13:07:59 +0100225int device_get_supply_regulator(struct udevice *dev, const char *supply_name,
226 struct udevice **devp)
227{
228 return uclass_get_device_by_phandle(UCLASS_REGULATOR, dev,
229 supply_name, devp);
230}
231
Simon Glass46cb8242015-06-23 15:38:58 -0600232int regulator_autoset(struct udevice *dev)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200233{
Simon Glass46cb8242015-06-23 15:38:58 -0600234 struct dm_regulator_uclass_platdata *uc_pdata;
235 int ret = 0;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200236
Simon Glass46cb8242015-06-23 15:38:58 -0600237 uc_pdata = dev_get_uclass_platdata(dev);
238 if (!uc_pdata->always_on && !uc_pdata->boot_on)
239 return -EMEDIUMTYPE;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200240
Simon Glass46cb8242015-06-23 15:38:58 -0600241 if (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UV)
242 ret = regulator_set_value(dev, uc_pdata->min_uV);
243 if (!ret && (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UA))
244 ret = regulator_set_current(dev, uc_pdata->min_uA);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200245
246 if (!ret)
Simon Glass46cb8242015-06-23 15:38:58 -0600247 ret = regulator_set_enable(dev, true);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200248
249 return ret;
250}
251
Simon Glass46cb8242015-06-23 15:38:58 -0600252static void regulator_show(struct udevice *dev, int ret)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200253{
254 struct dm_regulator_uclass_platdata *uc_pdata;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200255
256 uc_pdata = dev_get_uclass_platdata(dev);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200257
Simon Glass46cb8242015-06-23 15:38:58 -0600258 printf("%s@%s: ", dev->name, uc_pdata->name);
259 if (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UV)
260 printf("set %d uV", uc_pdata->min_uV);
261 if (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UA)
262 printf("; set %d uA", uc_pdata->min_uA);
263 printf("; enabling");
264 if (ret)
Simon Glass46ad8cb2016-01-21 19:43:58 -0700265 printf(" (ret: %d)", ret);
Simon Glass46cb8242015-06-23 15:38:58 -0600266 printf("\n");
267}
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200268
Simon Glass46cb8242015-06-23 15:38:58 -0600269int regulator_autoset_by_name(const char *platname, struct udevice **devp)
270{
271 struct udevice *dev;
272 int ret;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200273
Simon Glass46cb8242015-06-23 15:38:58 -0600274 ret = regulator_get_by_platname(platname, &dev);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200275 if (devp)
276 *devp = dev;
Simon Glass46cb8242015-06-23 15:38:58 -0600277 if (ret) {
Simon Glassfc3ebf12017-05-31 17:57:15 -0600278 debug("Can get the regulator: %s (err=%d)\n", platname, ret);
Simon Glass46cb8242015-06-23 15:38:58 -0600279 return ret;
280 }
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200281
Simon Glass46cb8242015-06-23 15:38:58 -0600282 return regulator_autoset(dev);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200283}
284
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200285int regulator_list_autoset(const char *list_platname[],
286 struct udevice *list_devp[],
287 bool verbose)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200288{
289 struct udevice *dev;
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200290 int error = 0, i = 0, ret;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200291
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200292 while (list_platname[i]) {
Simon Glass46cb8242015-06-23 15:38:58 -0600293 ret = regulator_autoset_by_name(list_platname[i], &dev);
294 if (ret != -EMEDIUMTYPE && verbose)
295 regulator_show(dev, ret);
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200296 if (ret & !error)
297 error = ret;
298
299 if (list_devp)
300 list_devp[i] = dev;
301
302 i++;
303 }
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200304
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200305 return error;
306}
307
308static bool regulator_name_is_unique(struct udevice *check_dev,
309 const char *check_name)
310{
311 struct dm_regulator_uclass_platdata *uc_pdata;
312 struct udevice *dev;
313 int check_len = strlen(check_name);
314 int ret;
315 int len;
316
317 for (ret = uclass_find_first_device(UCLASS_REGULATOR, &dev); dev;
318 ret = uclass_find_next_device(&dev)) {
319 if (ret || dev == check_dev)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200320 continue;
321
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200322 uc_pdata = dev_get_uclass_platdata(dev);
323 len = strlen(uc_pdata->name);
324 if (len != check_len)
325 continue;
326
327 if (!strcmp(uc_pdata->name, check_name))
328 return false;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200329 }
330
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200331 return true;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200332}
333
334static int regulator_post_bind(struct udevice *dev)
335{
336 struct dm_regulator_uclass_platdata *uc_pdata;
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200337 const char *property = "regulator-name";
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200338
339 uc_pdata = dev_get_uclass_platdata(dev);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200340
341 /* Regulator's mandatory constraint */
Simon Glasseeb99de2017-05-18 20:09:34 -0600342 uc_pdata->name = dev_read_string(dev, property);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200343 if (!uc_pdata->name) {
Simon Glasseeb99de2017-05-18 20:09:34 -0600344 debug("%s: dev '%s' has no property '%s'\n",
345 __func__, dev->name, property);
346 uc_pdata->name = dev_read_name(dev);
Peng Fancd672d42015-08-07 16:43:42 +0800347 if (!uc_pdata->name)
348 return -EINVAL;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200349 }
350
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200351 if (regulator_name_is_unique(dev, uc_pdata->name))
352 return 0;
353
Simon Glasseeb99de2017-05-18 20:09:34 -0600354 debug("'%s' of dev: '%s', has nonunique value: '%s\n",
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200355 property, dev->name, uc_pdata->name);
356
357 return -EINVAL;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200358}
359
360static int regulator_pre_probe(struct udevice *dev)
361{
362 struct dm_regulator_uclass_platdata *uc_pdata;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200363
364 uc_pdata = dev_get_uclass_platdata(dev);
365 if (!uc_pdata)
366 return -ENXIO;
367
368 /* Regulator's optional constraints */
Simon Glasseeb99de2017-05-18 20:09:34 -0600369 uc_pdata->min_uV = dev_read_u32_default(dev, "regulator-min-microvolt",
370 -ENODATA);
371 uc_pdata->max_uV = dev_read_u32_default(dev, "regulator-max-microvolt",
372 -ENODATA);
373 uc_pdata->min_uA = dev_read_u32_default(dev, "regulator-min-microamp",
374 -ENODATA);
375 uc_pdata->max_uA = dev_read_u32_default(dev, "regulator-max-microamp",
376 -ENODATA);
377 uc_pdata->always_on = dev_read_bool(dev, "regulator-always-on");
378 uc_pdata->boot_on = dev_read_bool(dev, "regulator-boot-on");
Krzysztof Kozlowskie2fa3972019-03-06 19:37:54 +0100379 uc_pdata->ramp_delay = dev_read_u32_default(dev, "regulator-ramp-delay",
380 0);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200381
Simon Glass991f9bc2015-06-23 15:38:57 -0600382 /* Those values are optional (-ENODATA if unset) */
383 if ((uc_pdata->min_uV != -ENODATA) &&
384 (uc_pdata->max_uV != -ENODATA) &&
385 (uc_pdata->min_uV == uc_pdata->max_uV))
386 uc_pdata->flags |= REGULATOR_FLAG_AUTOSET_UV;
387
388 /* Those values are optional (-ENODATA if unset) */
389 if ((uc_pdata->min_uA != -ENODATA) &&
390 (uc_pdata->max_uA != -ENODATA) &&
391 (uc_pdata->min_uA == uc_pdata->max_uA))
392 uc_pdata->flags |= REGULATOR_FLAG_AUTOSET_UA;
393
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200394 return 0;
395}
396
Simon Glassefeeb2c2015-06-23 15:38:59 -0600397int regulators_enable_boot_on(bool verbose)
398{
399 struct udevice *dev;
400 struct uclass *uc;
401 int ret;
402
403 ret = uclass_get(UCLASS_REGULATOR, &uc);
404 if (ret)
405 return ret;
406 for (uclass_first_device(UCLASS_REGULATOR, &dev);
Simon Glassc7298e72016-02-11 13:23:26 -0700407 dev;
Simon Glassefeeb2c2015-06-23 15:38:59 -0600408 uclass_next_device(&dev)) {
409 ret = regulator_autoset(dev);
Simon Glassf55c9512015-07-02 18:16:06 -0600410 if (ret == -EMEDIUMTYPE) {
411 ret = 0;
Simon Glassefeeb2c2015-06-23 15:38:59 -0600412 continue;
Simon Glassf55c9512015-07-02 18:16:06 -0600413 }
Simon Glassefeeb2c2015-06-23 15:38:59 -0600414 if (verbose)
415 regulator_show(dev, ret);
Simon Glassd6eddad2016-01-21 19:43:59 -0700416 if (ret == -ENOSYS)
417 ret = 0;
Simon Glassefeeb2c2015-06-23 15:38:59 -0600418 }
419
420 return ret;
421}
422
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200423UCLASS_DRIVER(regulator) = {
424 .id = UCLASS_REGULATOR,
425 .name = "regulator",
426 .post_bind = regulator_post_bind,
427 .pre_probe = regulator_pre_probe,
428 .per_device_platdata_auto_alloc_size =
429 sizeof(struct dm_regulator_uclass_platdata),
430};