blob: a42f80bb2b39cb4a5c25f5eca0c37b02e83e86f5 [file] [log] [blame]
Przemyslaw Marczak08edd002015-04-20 20:07:42 +02001/*
2 * Copyright (C) 2014-2015 Samsung Electronics
3 * Przemyslaw Marczak <p.marczak@samsung.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
Simon Glasseeb99de2017-05-18 20:09:34 -06007
Przemyslaw Marczak08edd002015-04-20 20:07:42 +02008#include <common.h>
Przemyslaw Marczak08edd002015-04-20 20:07:42 +02009#include <errno.h>
10#include <dm.h>
11#include <dm/uclass-internal.h>
12#include <power/pmic.h>
13#include <power/regulator.h>
14
15DECLARE_GLOBAL_DATA_PTR;
16
17int regulator_mode(struct udevice *dev, struct dm_regulator_mode **modep)
18{
19 struct dm_regulator_uclass_platdata *uc_pdata;
20
21 *modep = NULL;
22
23 uc_pdata = dev_get_uclass_platdata(dev);
24 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
41int regulator_set_value(struct udevice *dev, int uV)
42{
43 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
Keerthyc6e66692016-10-26 13:42:31 +053044 struct dm_regulator_uclass_platdata *uc_pdata;
45
46 uc_pdata = dev_get_uclass_platdata(dev);
47 if (uc_pdata->min_uV != -ENODATA && uV < uc_pdata->min_uV)
48 return -EINVAL;
49 if (uc_pdata->max_uV != -ENODATA && uV > uc_pdata->max_uV)
50 return -EINVAL;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +020051
52 if (!ops || !ops->set_value)
53 return -ENOSYS;
54
55 return ops->set_value(dev, uV);
56}
57
Keerthy162c02e2016-10-26 13:42:30 +053058/*
59 * To be called with at most caution as there is no check
60 * before setting the actual voltage value.
61 */
62int regulator_set_value_force(struct udevice *dev, int uV)
63{
64 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
65
66 if (!ops || !ops->set_value)
67 return -ENOSYS;
68
69 return ops->set_value(dev, uV);
70}
71
Przemyslaw Marczak08edd002015-04-20 20:07:42 +020072int regulator_get_current(struct udevice *dev)
73{
74 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
75
76 if (!ops || !ops->get_current)
77 return -ENOSYS;
78
79 return ops->get_current(dev);
80}
81
82int regulator_set_current(struct udevice *dev, int uA)
83{
84 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
Keerthyce152be2016-10-26 13:42:32 +053085 struct dm_regulator_uclass_platdata *uc_pdata;
86
87 uc_pdata = dev_get_uclass_platdata(dev);
88 if (uc_pdata->min_uA != -ENODATA && uA < uc_pdata->min_uA)
89 return -EINVAL;
90 if (uc_pdata->max_uA != -ENODATA && uA > uc_pdata->max_uA)
91 return -EINVAL;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +020092
93 if (!ops || !ops->set_current)
94 return -ENOSYS;
95
96 return ops->set_current(dev, uA);
97}
98
99bool regulator_get_enable(struct udevice *dev)
100{
101 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
102
103 if (!ops || !ops->get_enable)
104 return -ENOSYS;
105
106 return ops->get_enable(dev);
107}
108
109int regulator_set_enable(struct udevice *dev, bool enable)
110{
111 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
112
113 if (!ops || !ops->set_enable)
114 return -ENOSYS;
115
116 return ops->set_enable(dev, enable);
117}
118
119int regulator_get_mode(struct udevice *dev)
120{
121 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
122
123 if (!ops || !ops->get_mode)
124 return -ENOSYS;
125
126 return ops->get_mode(dev);
127}
128
129int regulator_set_mode(struct udevice *dev, int mode)
130{
131 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
132
133 if (!ops || !ops->set_mode)
134 return -ENOSYS;
135
136 return ops->set_mode(dev, mode);
137}
138
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200139int regulator_get_by_platname(const char *plat_name, struct udevice **devp)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200140{
141 struct dm_regulator_uclass_platdata *uc_pdata;
142 struct udevice *dev;
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200143 int ret;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200144
145 *devp = NULL;
146
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200147 for (ret = uclass_find_first_device(UCLASS_REGULATOR, &dev); dev;
148 ret = uclass_find_next_device(&dev)) {
149 if (ret)
150 continue;
151
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200152 uc_pdata = dev_get_uclass_platdata(dev);
153 if (!uc_pdata || strcmp(plat_name, uc_pdata->name))
154 continue;
155
156 return uclass_get_device_tail(dev, 0, devp);
157 }
158
159 debug("%s: can't find: %s\n", __func__, plat_name);
160
161 return -ENODEV;
162}
163
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200164int regulator_get_by_devname(const char *devname, struct udevice **devp)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200165{
166 return uclass_get_device_by_name(UCLASS_REGULATOR, devname, devp);
167}
168
Przemyslaw Marczakc9001032015-10-27 13:07:59 +0100169int device_get_supply_regulator(struct udevice *dev, const char *supply_name,
170 struct udevice **devp)
171{
172 return uclass_get_device_by_phandle(UCLASS_REGULATOR, dev,
173 supply_name, devp);
174}
175
Simon Glass46cb8242015-06-23 15:38:58 -0600176int regulator_autoset(struct udevice *dev)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200177{
Simon Glass46cb8242015-06-23 15:38:58 -0600178 struct dm_regulator_uclass_platdata *uc_pdata;
179 int ret = 0;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200180
Simon Glass46cb8242015-06-23 15:38:58 -0600181 uc_pdata = dev_get_uclass_platdata(dev);
182 if (!uc_pdata->always_on && !uc_pdata->boot_on)
183 return -EMEDIUMTYPE;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200184
Simon Glass46cb8242015-06-23 15:38:58 -0600185 if (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UV)
186 ret = regulator_set_value(dev, uc_pdata->min_uV);
187 if (!ret && (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UA))
188 ret = regulator_set_current(dev, uc_pdata->min_uA);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200189
190 if (!ret)
Simon Glass46cb8242015-06-23 15:38:58 -0600191 ret = regulator_set_enable(dev, true);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200192
193 return ret;
194}
195
Simon Glass46cb8242015-06-23 15:38:58 -0600196static void regulator_show(struct udevice *dev, int ret)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200197{
198 struct dm_regulator_uclass_platdata *uc_pdata;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200199
200 uc_pdata = dev_get_uclass_platdata(dev);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200201
Simon Glass46cb8242015-06-23 15:38:58 -0600202 printf("%s@%s: ", dev->name, uc_pdata->name);
203 if (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UV)
204 printf("set %d uV", uc_pdata->min_uV);
205 if (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UA)
206 printf("; set %d uA", uc_pdata->min_uA);
207 printf("; enabling");
208 if (ret)
Simon Glass46ad8cb2016-01-21 19:43:58 -0700209 printf(" (ret: %d)", ret);
Simon Glass46cb8242015-06-23 15:38:58 -0600210 printf("\n");
211}
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200212
Simon Glass46cb8242015-06-23 15:38:58 -0600213int regulator_autoset_by_name(const char *platname, struct udevice **devp)
214{
215 struct udevice *dev;
216 int ret;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200217
Simon Glass46cb8242015-06-23 15:38:58 -0600218 ret = regulator_get_by_platname(platname, &dev);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200219 if (devp)
220 *devp = dev;
Simon Glass46cb8242015-06-23 15:38:58 -0600221 if (ret) {
222 debug("Can get the regulator: %s!", platname);
223 return ret;
224 }
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200225
Simon Glass46cb8242015-06-23 15:38:58 -0600226 return regulator_autoset(dev);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200227}
228
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200229int regulator_list_autoset(const char *list_platname[],
230 struct udevice *list_devp[],
231 bool verbose)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200232{
233 struct udevice *dev;
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200234 int error = 0, i = 0, ret;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200235
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200236 while (list_platname[i]) {
Simon Glass46cb8242015-06-23 15:38:58 -0600237 ret = regulator_autoset_by_name(list_platname[i], &dev);
238 if (ret != -EMEDIUMTYPE && verbose)
239 regulator_show(dev, ret);
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200240 if (ret & !error)
241 error = ret;
242
243 if (list_devp)
244 list_devp[i] = dev;
245
246 i++;
247 }
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200248
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200249 return error;
250}
251
252static bool regulator_name_is_unique(struct udevice *check_dev,
253 const char *check_name)
254{
255 struct dm_regulator_uclass_platdata *uc_pdata;
256 struct udevice *dev;
257 int check_len = strlen(check_name);
258 int ret;
259 int len;
260
261 for (ret = uclass_find_first_device(UCLASS_REGULATOR, &dev); dev;
262 ret = uclass_find_next_device(&dev)) {
263 if (ret || dev == check_dev)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200264 continue;
265
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200266 uc_pdata = dev_get_uclass_platdata(dev);
267 len = strlen(uc_pdata->name);
268 if (len != check_len)
269 continue;
270
271 if (!strcmp(uc_pdata->name, check_name))
272 return false;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200273 }
274
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200275 return true;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200276}
277
278static int regulator_post_bind(struct udevice *dev)
279{
280 struct dm_regulator_uclass_platdata *uc_pdata;
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200281 const char *property = "regulator-name";
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200282
283 uc_pdata = dev_get_uclass_platdata(dev);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200284
285 /* Regulator's mandatory constraint */
Simon Glasseeb99de2017-05-18 20:09:34 -0600286 uc_pdata->name = dev_read_string(dev, property);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200287 if (!uc_pdata->name) {
Simon Glasseeb99de2017-05-18 20:09:34 -0600288 debug("%s: dev '%s' has no property '%s'\n",
289 __func__, dev->name, property);
290 uc_pdata->name = dev_read_name(dev);
Peng Fancd672d42015-08-07 16:43:42 +0800291 if (!uc_pdata->name)
292 return -EINVAL;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200293 }
294
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200295 if (regulator_name_is_unique(dev, uc_pdata->name))
296 return 0;
297
Simon Glasseeb99de2017-05-18 20:09:34 -0600298 debug("'%s' of dev: '%s', has nonunique value: '%s\n",
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200299 property, dev->name, uc_pdata->name);
300
301 return -EINVAL;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200302}
303
304static int regulator_pre_probe(struct udevice *dev)
305{
306 struct dm_regulator_uclass_platdata *uc_pdata;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200307
308 uc_pdata = dev_get_uclass_platdata(dev);
309 if (!uc_pdata)
310 return -ENXIO;
311
312 /* Regulator's optional constraints */
Simon Glasseeb99de2017-05-18 20:09:34 -0600313 uc_pdata->min_uV = dev_read_u32_default(dev, "regulator-min-microvolt",
314 -ENODATA);
315 uc_pdata->max_uV = dev_read_u32_default(dev, "regulator-max-microvolt",
316 -ENODATA);
317 uc_pdata->min_uA = dev_read_u32_default(dev, "regulator-min-microamp",
318 -ENODATA);
319 uc_pdata->max_uA = dev_read_u32_default(dev, "regulator-max-microamp",
320 -ENODATA);
321 uc_pdata->always_on = dev_read_bool(dev, "regulator-always-on");
322 uc_pdata->boot_on = dev_read_bool(dev, "regulator-boot-on");
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200323
Simon Glass991f9bc2015-06-23 15:38:57 -0600324 /* Those values are optional (-ENODATA if unset) */
325 if ((uc_pdata->min_uV != -ENODATA) &&
326 (uc_pdata->max_uV != -ENODATA) &&
327 (uc_pdata->min_uV == uc_pdata->max_uV))
328 uc_pdata->flags |= REGULATOR_FLAG_AUTOSET_UV;
329
330 /* Those values are optional (-ENODATA if unset) */
331 if ((uc_pdata->min_uA != -ENODATA) &&
332 (uc_pdata->max_uA != -ENODATA) &&
333 (uc_pdata->min_uA == uc_pdata->max_uA))
334 uc_pdata->flags |= REGULATOR_FLAG_AUTOSET_UA;
335
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200336 return 0;
337}
338
Simon Glassefeeb2c2015-06-23 15:38:59 -0600339int regulators_enable_boot_on(bool verbose)
340{
341 struct udevice *dev;
342 struct uclass *uc;
343 int ret;
344
345 ret = uclass_get(UCLASS_REGULATOR, &uc);
346 if (ret)
347 return ret;
348 for (uclass_first_device(UCLASS_REGULATOR, &dev);
Simon Glassc7298e72016-02-11 13:23:26 -0700349 dev;
Simon Glassefeeb2c2015-06-23 15:38:59 -0600350 uclass_next_device(&dev)) {
351 ret = regulator_autoset(dev);
Simon Glassf55c9512015-07-02 18:16:06 -0600352 if (ret == -EMEDIUMTYPE) {
353 ret = 0;
Simon Glassefeeb2c2015-06-23 15:38:59 -0600354 continue;
Simon Glassf55c9512015-07-02 18:16:06 -0600355 }
Simon Glassefeeb2c2015-06-23 15:38:59 -0600356 if (verbose)
357 regulator_show(dev, ret);
Simon Glassd6eddad2016-01-21 19:43:59 -0700358 if (ret == -ENOSYS)
359 ret = 0;
Simon Glassefeeb2c2015-06-23 15:38:59 -0600360 }
361
362 return ret;
363}
364
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200365UCLASS_DRIVER(regulator) = {
366 .id = UCLASS_REGULATOR,
367 .name = "regulator",
368 .post_bind = regulator_post_bind,
369 .pre_probe = regulator_pre_probe,
370 .per_device_platdata_auto_alloc_size =
371 sizeof(struct dm_regulator_uclass_platdata),
372};