blob: 76be95bcd1591a2c8e06e4a7c3d3f8c66b798fe2 [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
Sven Schwermerd033cbf2019-06-12 08:32:38 +0200241 if (uc_pdata->type == REGULATOR_TYPE_FIXED)
242 return regulator_set_enable(dev, true);
243
Simon Glass46cb8242015-06-23 15:38:58 -0600244 if (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UV)
245 ret = regulator_set_value(dev, uc_pdata->min_uV);
246 if (!ret && (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UA))
247 ret = regulator_set_current(dev, uc_pdata->min_uA);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200248
249 if (!ret)
Simon Glass46cb8242015-06-23 15:38:58 -0600250 ret = regulator_set_enable(dev, true);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200251
252 return ret;
253}
254
Simon Glass46cb8242015-06-23 15:38:58 -0600255static void regulator_show(struct udevice *dev, int ret)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200256{
257 struct dm_regulator_uclass_platdata *uc_pdata;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200258
259 uc_pdata = dev_get_uclass_platdata(dev);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200260
Simon Glass46cb8242015-06-23 15:38:58 -0600261 printf("%s@%s: ", dev->name, uc_pdata->name);
262 if (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UV)
263 printf("set %d uV", uc_pdata->min_uV);
264 if (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UA)
265 printf("; set %d uA", uc_pdata->min_uA);
266 printf("; enabling");
267 if (ret)
Simon Glass46ad8cb2016-01-21 19:43:58 -0700268 printf(" (ret: %d)", ret);
Simon Glass46cb8242015-06-23 15:38:58 -0600269 printf("\n");
270}
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200271
Simon Glass46cb8242015-06-23 15:38:58 -0600272int regulator_autoset_by_name(const char *platname, struct udevice **devp)
273{
274 struct udevice *dev;
275 int ret;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200276
Simon Glass46cb8242015-06-23 15:38:58 -0600277 ret = regulator_get_by_platname(platname, &dev);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200278 if (devp)
279 *devp = dev;
Simon Glass46cb8242015-06-23 15:38:58 -0600280 if (ret) {
Simon Glassfc3ebf12017-05-31 17:57:15 -0600281 debug("Can get the regulator: %s (err=%d)\n", platname, ret);
Simon Glass46cb8242015-06-23 15:38:58 -0600282 return ret;
283 }
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200284
Simon Glass46cb8242015-06-23 15:38:58 -0600285 return regulator_autoset(dev);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200286}
287
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200288int regulator_list_autoset(const char *list_platname[],
289 struct udevice *list_devp[],
290 bool verbose)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200291{
292 struct udevice *dev;
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200293 int error = 0, i = 0, ret;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200294
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200295 while (list_platname[i]) {
Simon Glass46cb8242015-06-23 15:38:58 -0600296 ret = regulator_autoset_by_name(list_platname[i], &dev);
297 if (ret != -EMEDIUMTYPE && verbose)
298 regulator_show(dev, ret);
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200299 if (ret & !error)
300 error = ret;
301
302 if (list_devp)
303 list_devp[i] = dev;
304
305 i++;
306 }
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200307
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200308 return error;
309}
310
311static bool regulator_name_is_unique(struct udevice *check_dev,
312 const char *check_name)
313{
314 struct dm_regulator_uclass_platdata *uc_pdata;
315 struct udevice *dev;
316 int check_len = strlen(check_name);
317 int ret;
318 int len;
319
320 for (ret = uclass_find_first_device(UCLASS_REGULATOR, &dev); dev;
321 ret = uclass_find_next_device(&dev)) {
322 if (ret || dev == check_dev)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200323 continue;
324
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200325 uc_pdata = dev_get_uclass_platdata(dev);
326 len = strlen(uc_pdata->name);
327 if (len != check_len)
328 continue;
329
330 if (!strcmp(uc_pdata->name, check_name))
331 return false;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200332 }
333
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200334 return true;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200335}
336
337static int regulator_post_bind(struct udevice *dev)
338{
339 struct dm_regulator_uclass_platdata *uc_pdata;
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200340 const char *property = "regulator-name";
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200341
342 uc_pdata = dev_get_uclass_platdata(dev);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200343
344 /* Regulator's mandatory constraint */
Simon Glasseeb99de2017-05-18 20:09:34 -0600345 uc_pdata->name = dev_read_string(dev, property);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200346 if (!uc_pdata->name) {
Simon Glasseeb99de2017-05-18 20:09:34 -0600347 debug("%s: dev '%s' has no property '%s'\n",
348 __func__, dev->name, property);
349 uc_pdata->name = dev_read_name(dev);
Peng Fancd672d42015-08-07 16:43:42 +0800350 if (!uc_pdata->name)
351 return -EINVAL;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200352 }
353
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200354 if (regulator_name_is_unique(dev, uc_pdata->name))
355 return 0;
356
Simon Glasseeb99de2017-05-18 20:09:34 -0600357 debug("'%s' of dev: '%s', has nonunique value: '%s\n",
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200358 property, dev->name, uc_pdata->name);
359
360 return -EINVAL;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200361}
362
363static int regulator_pre_probe(struct udevice *dev)
364{
365 struct dm_regulator_uclass_platdata *uc_pdata;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200366
367 uc_pdata = dev_get_uclass_platdata(dev);
368 if (!uc_pdata)
369 return -ENXIO;
370
371 /* Regulator's optional constraints */
Simon Glasseeb99de2017-05-18 20:09:34 -0600372 uc_pdata->min_uV = dev_read_u32_default(dev, "regulator-min-microvolt",
373 -ENODATA);
374 uc_pdata->max_uV = dev_read_u32_default(dev, "regulator-max-microvolt",
375 -ENODATA);
376 uc_pdata->min_uA = dev_read_u32_default(dev, "regulator-min-microamp",
377 -ENODATA);
378 uc_pdata->max_uA = dev_read_u32_default(dev, "regulator-max-microamp",
379 -ENODATA);
380 uc_pdata->always_on = dev_read_bool(dev, "regulator-always-on");
381 uc_pdata->boot_on = dev_read_bool(dev, "regulator-boot-on");
Krzysztof Kozlowskie2fa3972019-03-06 19:37:54 +0100382 uc_pdata->ramp_delay = dev_read_u32_default(dev, "regulator-ramp-delay",
383 0);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200384
Simon Glass991f9bc2015-06-23 15:38:57 -0600385 /* Those values are optional (-ENODATA if unset) */
386 if ((uc_pdata->min_uV != -ENODATA) &&
387 (uc_pdata->max_uV != -ENODATA) &&
388 (uc_pdata->min_uV == uc_pdata->max_uV))
389 uc_pdata->flags |= REGULATOR_FLAG_AUTOSET_UV;
390
391 /* Those values are optional (-ENODATA if unset) */
392 if ((uc_pdata->min_uA != -ENODATA) &&
393 (uc_pdata->max_uA != -ENODATA) &&
394 (uc_pdata->min_uA == uc_pdata->max_uA))
395 uc_pdata->flags |= REGULATOR_FLAG_AUTOSET_UA;
396
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200397 return 0;
398}
399
Simon Glassefeeb2c2015-06-23 15:38:59 -0600400int regulators_enable_boot_on(bool verbose)
401{
402 struct udevice *dev;
403 struct uclass *uc;
404 int ret;
405
406 ret = uclass_get(UCLASS_REGULATOR, &uc);
407 if (ret)
408 return ret;
409 for (uclass_first_device(UCLASS_REGULATOR, &dev);
Simon Glassc7298e72016-02-11 13:23:26 -0700410 dev;
Simon Glassefeeb2c2015-06-23 15:38:59 -0600411 uclass_next_device(&dev)) {
412 ret = regulator_autoset(dev);
Simon Glassf55c9512015-07-02 18:16:06 -0600413 if (ret == -EMEDIUMTYPE) {
414 ret = 0;
Simon Glassefeeb2c2015-06-23 15:38:59 -0600415 continue;
Simon Glassf55c9512015-07-02 18:16:06 -0600416 }
Simon Glassefeeb2c2015-06-23 15:38:59 -0600417 if (verbose)
418 regulator_show(dev, ret);
Simon Glassd6eddad2016-01-21 19:43:59 -0700419 if (ret == -ENOSYS)
420 ret = 0;
Simon Glassefeeb2c2015-06-23 15:38:59 -0600421 }
422
423 return ret;
424}
425
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200426UCLASS_DRIVER(regulator) = {
427 .id = UCLASS_REGULATOR,
428 .name = "regulator",
429 .post_bind = regulator_post_bind,
430 .pre_probe = regulator_pre_probe,
431 .per_device_platdata_auto_alloc_size =
432 sizeof(struct dm_regulator_uclass_platdata),
433};