blob: 4da8e43259fc29bb43fa9073b374ed04cd6ba248 [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
38int regulator_set_value(struct udevice *dev, int uV)
39{
40 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
Keerthyc6e66692016-10-26 13:42:31 +053041 struct dm_regulator_uclass_platdata *uc_pdata;
42
43 uc_pdata = dev_get_uclass_platdata(dev);
44 if (uc_pdata->min_uV != -ENODATA && uV < uc_pdata->min_uV)
45 return -EINVAL;
46 if (uc_pdata->max_uV != -ENODATA && uV > uc_pdata->max_uV)
47 return -EINVAL;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +020048
49 if (!ops || !ops->set_value)
50 return -ENOSYS;
51
52 return ops->set_value(dev, uV);
53}
54
Keerthy162c02e2016-10-26 13:42:30 +053055/*
56 * To be called with at most caution as there is no check
57 * before setting the actual voltage value.
58 */
59int regulator_set_value_force(struct udevice *dev, int uV)
60{
61 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
62
63 if (!ops || !ops->set_value)
64 return -ENOSYS;
65
66 return ops->set_value(dev, uV);
67}
68
Przemyslaw Marczak08edd002015-04-20 20:07:42 +020069int regulator_get_current(struct udevice *dev)
70{
71 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
72
73 if (!ops || !ops->get_current)
74 return -ENOSYS;
75
76 return ops->get_current(dev);
77}
78
79int regulator_set_current(struct udevice *dev, int uA)
80{
81 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
Keerthyce152be2016-10-26 13:42:32 +053082 struct dm_regulator_uclass_platdata *uc_pdata;
83
84 uc_pdata = dev_get_uclass_platdata(dev);
85 if (uc_pdata->min_uA != -ENODATA && uA < uc_pdata->min_uA)
86 return -EINVAL;
87 if (uc_pdata->max_uA != -ENODATA && uA > uc_pdata->max_uA)
88 return -EINVAL;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +020089
90 if (!ops || !ops->set_current)
91 return -ENOSYS;
92
93 return ops->set_current(dev, uA);
94}
95
Keerthy23be7fb2017-06-13 09:53:45 +053096int regulator_get_enable(struct udevice *dev)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +020097{
98 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
99
100 if (!ops || !ops->get_enable)
101 return -ENOSYS;
102
103 return ops->get_enable(dev);
104}
105
106int regulator_set_enable(struct udevice *dev, bool enable)
107{
108 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
109
110 if (!ops || !ops->set_enable)
111 return -ENOSYS;
112
113 return ops->set_enable(dev, enable);
114}
115
116int regulator_get_mode(struct udevice *dev)
117{
118 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
119
120 if (!ops || !ops->get_mode)
121 return -ENOSYS;
122
123 return ops->get_mode(dev);
124}
125
126int regulator_set_mode(struct udevice *dev, int mode)
127{
128 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
129
130 if (!ops || !ops->set_mode)
131 return -ENOSYS;
132
133 return ops->set_mode(dev, mode);
134}
135
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200136int regulator_get_by_platname(const char *plat_name, struct udevice **devp)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200137{
138 struct dm_regulator_uclass_platdata *uc_pdata;
139 struct udevice *dev;
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200140 int ret;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200141
142 *devp = NULL;
143
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200144 for (ret = uclass_find_first_device(UCLASS_REGULATOR, &dev); dev;
145 ret = uclass_find_next_device(&dev)) {
Simon Glassfc3ebf12017-05-31 17:57:15 -0600146 if (ret) {
147 debug("regulator %s, ret=%d\n", dev->name, ret);
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200148 continue;
Simon Glassfc3ebf12017-05-31 17:57:15 -0600149 }
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200150
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200151 uc_pdata = dev_get_uclass_platdata(dev);
152 if (!uc_pdata || strcmp(plat_name, uc_pdata->name))
153 continue;
154
155 return uclass_get_device_tail(dev, 0, devp);
156 }
157
Simon Glassfc3ebf12017-05-31 17:57:15 -0600158 debug("%s: can't find: %s, ret=%d\n", __func__, plat_name, ret);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200159
160 return -ENODEV;
161}
162
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200163int regulator_get_by_devname(const char *devname, struct udevice **devp)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200164{
165 return uclass_get_device_by_name(UCLASS_REGULATOR, devname, devp);
166}
167
Przemyslaw Marczakc9001032015-10-27 13:07:59 +0100168int device_get_supply_regulator(struct udevice *dev, const char *supply_name,
169 struct udevice **devp)
170{
171 return uclass_get_device_by_phandle(UCLASS_REGULATOR, dev,
172 supply_name, devp);
173}
174
Simon Glass46cb8242015-06-23 15:38:58 -0600175int regulator_autoset(struct udevice *dev)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200176{
Simon Glass46cb8242015-06-23 15:38:58 -0600177 struct dm_regulator_uclass_platdata *uc_pdata;
178 int ret = 0;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200179
Simon Glass46cb8242015-06-23 15:38:58 -0600180 uc_pdata = dev_get_uclass_platdata(dev);
181 if (!uc_pdata->always_on && !uc_pdata->boot_on)
182 return -EMEDIUMTYPE;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200183
Simon Glass46cb8242015-06-23 15:38:58 -0600184 if (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UV)
185 ret = regulator_set_value(dev, uc_pdata->min_uV);
186 if (!ret && (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UA))
187 ret = regulator_set_current(dev, uc_pdata->min_uA);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200188
189 if (!ret)
Simon Glass46cb8242015-06-23 15:38:58 -0600190 ret = regulator_set_enable(dev, true);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200191
192 return ret;
193}
194
Simon Glass46cb8242015-06-23 15:38:58 -0600195static void regulator_show(struct udevice *dev, int ret)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200196{
197 struct dm_regulator_uclass_platdata *uc_pdata;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200198
199 uc_pdata = dev_get_uclass_platdata(dev);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200200
Simon Glass46cb8242015-06-23 15:38:58 -0600201 printf("%s@%s: ", dev->name, uc_pdata->name);
202 if (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UV)
203 printf("set %d uV", uc_pdata->min_uV);
204 if (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UA)
205 printf("; set %d uA", uc_pdata->min_uA);
206 printf("; enabling");
207 if (ret)
Simon Glass46ad8cb2016-01-21 19:43:58 -0700208 printf(" (ret: %d)", ret);
Simon Glass46cb8242015-06-23 15:38:58 -0600209 printf("\n");
210}
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200211
Simon Glass46cb8242015-06-23 15:38:58 -0600212int regulator_autoset_by_name(const char *platname, struct udevice **devp)
213{
214 struct udevice *dev;
215 int ret;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200216
Simon Glass46cb8242015-06-23 15:38:58 -0600217 ret = regulator_get_by_platname(platname, &dev);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200218 if (devp)
219 *devp = dev;
Simon Glass46cb8242015-06-23 15:38:58 -0600220 if (ret) {
Simon Glassfc3ebf12017-05-31 17:57:15 -0600221 debug("Can get the regulator: %s (err=%d)\n", platname, ret);
Simon Glass46cb8242015-06-23 15:38:58 -0600222 return ret;
223 }
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200224
Simon Glass46cb8242015-06-23 15:38:58 -0600225 return regulator_autoset(dev);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200226}
227
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200228int regulator_list_autoset(const char *list_platname[],
229 struct udevice *list_devp[],
230 bool verbose)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200231{
232 struct udevice *dev;
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200233 int error = 0, i = 0, ret;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200234
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200235 while (list_platname[i]) {
Simon Glass46cb8242015-06-23 15:38:58 -0600236 ret = regulator_autoset_by_name(list_platname[i], &dev);
237 if (ret != -EMEDIUMTYPE && verbose)
238 regulator_show(dev, ret);
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200239 if (ret & !error)
240 error = ret;
241
242 if (list_devp)
243 list_devp[i] = dev;
244
245 i++;
246 }
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200247
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200248 return error;
249}
250
251static bool regulator_name_is_unique(struct udevice *check_dev,
252 const char *check_name)
253{
254 struct dm_regulator_uclass_platdata *uc_pdata;
255 struct udevice *dev;
256 int check_len = strlen(check_name);
257 int ret;
258 int len;
259
260 for (ret = uclass_find_first_device(UCLASS_REGULATOR, &dev); dev;
261 ret = uclass_find_next_device(&dev)) {
262 if (ret || dev == check_dev)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200263 continue;
264
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200265 uc_pdata = dev_get_uclass_platdata(dev);
266 len = strlen(uc_pdata->name);
267 if (len != check_len)
268 continue;
269
270 if (!strcmp(uc_pdata->name, check_name))
271 return false;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200272 }
273
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200274 return true;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200275}
276
277static int regulator_post_bind(struct udevice *dev)
278{
279 struct dm_regulator_uclass_platdata *uc_pdata;
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200280 const char *property = "regulator-name";
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200281
282 uc_pdata = dev_get_uclass_platdata(dev);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200283
284 /* Regulator's mandatory constraint */
Simon Glasseeb99de2017-05-18 20:09:34 -0600285 uc_pdata->name = dev_read_string(dev, property);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200286 if (!uc_pdata->name) {
Simon Glasseeb99de2017-05-18 20:09:34 -0600287 debug("%s: dev '%s' has no property '%s'\n",
288 __func__, dev->name, property);
289 uc_pdata->name = dev_read_name(dev);
Peng Fancd672d42015-08-07 16:43:42 +0800290 if (!uc_pdata->name)
291 return -EINVAL;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200292 }
293
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200294 if (regulator_name_is_unique(dev, uc_pdata->name))
295 return 0;
296
Simon Glasseeb99de2017-05-18 20:09:34 -0600297 debug("'%s' of dev: '%s', has nonunique value: '%s\n",
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200298 property, dev->name, uc_pdata->name);
299
300 return -EINVAL;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200301}
302
303static int regulator_pre_probe(struct udevice *dev)
304{
305 struct dm_regulator_uclass_platdata *uc_pdata;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200306
307 uc_pdata = dev_get_uclass_platdata(dev);
308 if (!uc_pdata)
309 return -ENXIO;
310
311 /* Regulator's optional constraints */
Simon Glasseeb99de2017-05-18 20:09:34 -0600312 uc_pdata->min_uV = dev_read_u32_default(dev, "regulator-min-microvolt",
313 -ENODATA);
314 uc_pdata->max_uV = dev_read_u32_default(dev, "regulator-max-microvolt",
315 -ENODATA);
316 uc_pdata->min_uA = dev_read_u32_default(dev, "regulator-min-microamp",
317 -ENODATA);
318 uc_pdata->max_uA = dev_read_u32_default(dev, "regulator-max-microamp",
319 -ENODATA);
320 uc_pdata->always_on = dev_read_bool(dev, "regulator-always-on");
321 uc_pdata->boot_on = dev_read_bool(dev, "regulator-boot-on");
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200322
Simon Glass991f9bc2015-06-23 15:38:57 -0600323 /* Those values are optional (-ENODATA if unset) */
324 if ((uc_pdata->min_uV != -ENODATA) &&
325 (uc_pdata->max_uV != -ENODATA) &&
326 (uc_pdata->min_uV == uc_pdata->max_uV))
327 uc_pdata->flags |= REGULATOR_FLAG_AUTOSET_UV;
328
329 /* Those values are optional (-ENODATA if unset) */
330 if ((uc_pdata->min_uA != -ENODATA) &&
331 (uc_pdata->max_uA != -ENODATA) &&
332 (uc_pdata->min_uA == uc_pdata->max_uA))
333 uc_pdata->flags |= REGULATOR_FLAG_AUTOSET_UA;
334
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200335 return 0;
336}
337
Simon Glassefeeb2c2015-06-23 15:38:59 -0600338int regulators_enable_boot_on(bool verbose)
339{
340 struct udevice *dev;
341 struct uclass *uc;
342 int ret;
343
344 ret = uclass_get(UCLASS_REGULATOR, &uc);
345 if (ret)
346 return ret;
347 for (uclass_first_device(UCLASS_REGULATOR, &dev);
Simon Glassc7298e72016-02-11 13:23:26 -0700348 dev;
Simon Glassefeeb2c2015-06-23 15:38:59 -0600349 uclass_next_device(&dev)) {
350 ret = regulator_autoset(dev);
Simon Glassf55c9512015-07-02 18:16:06 -0600351 if (ret == -EMEDIUMTYPE) {
352 ret = 0;
Simon Glassefeeb2c2015-06-23 15:38:59 -0600353 continue;
Simon Glassf55c9512015-07-02 18:16:06 -0600354 }
Simon Glassefeeb2c2015-06-23 15:38:59 -0600355 if (verbose)
356 regulator_show(dev, ret);
Simon Glassd6eddad2016-01-21 19:43:59 -0700357 if (ret == -ENOSYS)
358 ret = 0;
Simon Glassefeeb2c2015-06-23 15:38:59 -0600359 }
360
361 return ret;
362}
363
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200364UCLASS_DRIVER(regulator) = {
365 .id = UCLASS_REGULATOR,
366 .name = "regulator",
367 .post_bind = regulator_post_bind,
368 .pre_probe = regulator_pre_probe,
369 .per_device_platdata_auto_alloc_size =
370 sizeof(struct dm_regulator_uclass_platdata),
371};