blob: 31ffd44454cf30e5d0b51b3c57799b4c21a8e802 [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 */
7#include <common.h>
8#include <fdtdec.h>
9#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);
44
45 if (!ops || !ops->set_value)
46 return -ENOSYS;
47
48 return ops->set_value(dev, uV);
49}
50
51int regulator_get_current(struct udevice *dev)
52{
53 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
54
55 if (!ops || !ops->get_current)
56 return -ENOSYS;
57
58 return ops->get_current(dev);
59}
60
61int regulator_set_current(struct udevice *dev, int uA)
62{
63 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
64
65 if (!ops || !ops->set_current)
66 return -ENOSYS;
67
68 return ops->set_current(dev, uA);
69}
70
71bool regulator_get_enable(struct udevice *dev)
72{
73 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
74
75 if (!ops || !ops->get_enable)
76 return -ENOSYS;
77
78 return ops->get_enable(dev);
79}
80
81int regulator_set_enable(struct udevice *dev, bool enable)
82{
83 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
84
85 if (!ops || !ops->set_enable)
86 return -ENOSYS;
87
88 return ops->set_enable(dev, enable);
89}
90
91int regulator_get_mode(struct udevice *dev)
92{
93 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
94
95 if (!ops || !ops->get_mode)
96 return -ENOSYS;
97
98 return ops->get_mode(dev);
99}
100
101int regulator_set_mode(struct udevice *dev, int mode)
102{
103 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
104
105 if (!ops || !ops->set_mode)
106 return -ENOSYS;
107
108 return ops->set_mode(dev, mode);
109}
110
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200111int regulator_get_by_platname(const char *plat_name, struct udevice **devp)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200112{
113 struct dm_regulator_uclass_platdata *uc_pdata;
114 struct udevice *dev;
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200115 int ret;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200116
117 *devp = NULL;
118
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200119 for (ret = uclass_find_first_device(UCLASS_REGULATOR, &dev); dev;
120 ret = uclass_find_next_device(&dev)) {
121 if (ret)
122 continue;
123
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200124 uc_pdata = dev_get_uclass_platdata(dev);
125 if (!uc_pdata || strcmp(plat_name, uc_pdata->name))
126 continue;
127
128 return uclass_get_device_tail(dev, 0, devp);
129 }
130
131 debug("%s: can't find: %s\n", __func__, plat_name);
132
133 return -ENODEV;
134}
135
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200136int regulator_get_by_devname(const char *devname, struct udevice **devp)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200137{
138 return uclass_get_device_by_name(UCLASS_REGULATOR, devname, devp);
139}
140
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200141static int failed(int ret, bool verbose, const char *fmt, ...)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200142{
143 va_list args;
144 char buf[64];
145
146 if (verbose == false)
147 return ret;
148
149 va_start(args, fmt);
150 vscnprintf(buf, sizeof(buf), fmt, args);
151 va_end(args);
152
153 printf(buf);
154
155 if (!ret)
156 return 0;
157
158 printf(" (ret: %d)", ret);
159
160 return ret;
161}
162
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200163int regulator_autoset(const char *platname,
164 struct udevice **devp,
165 bool verbose)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200166{
167 struct dm_regulator_uclass_platdata *uc_pdata;
168 struct udevice *dev;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200169 int ret;
170
171 if (devp)
172 *devp = NULL;
173
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200174 ret = regulator_get_by_platname(platname, &dev);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200175 if (ret) {
176 error("Can get the regulator: %s!", platname);
177 return ret;
178 }
179
180 uc_pdata = dev_get_uclass_platdata(dev);
181 if (!uc_pdata) {
182 error("Can get the regulator %s uclass platdata!", platname);
183 return -ENXIO;
184 }
185
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200186 if (!uc_pdata->always_on && !uc_pdata->boot_on)
187 goto retdev;
188
189 if (verbose)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200190 printf("%s@%s: ", dev->name, uc_pdata->name);
191
192 /* Those values are optional (-ENODATA if unset) */
193 if ((uc_pdata->min_uV != -ENODATA) &&
194 (uc_pdata->max_uV != -ENODATA) &&
195 (uc_pdata->min_uV == uc_pdata->max_uV)) {
196 ret = regulator_set_value(dev, uc_pdata->min_uV);
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200197 if (failed(ret, verbose, "set %d uV", uc_pdata->min_uV))
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200198 goto exit;
199 }
200
201 /* Those values are optional (-ENODATA if unset) */
202 if ((uc_pdata->min_uA != -ENODATA) &&
203 (uc_pdata->max_uA != -ENODATA) &&
204 (uc_pdata->min_uA == uc_pdata->max_uA)) {
205 ret = regulator_set_current(dev, uc_pdata->min_uA);
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200206 if (failed(ret, verbose, "; set %d uA", uc_pdata->min_uA))
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200207 goto exit;
208 }
209
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200210 ret = regulator_set_enable(dev, true);
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200211 if (failed(ret, verbose, "; enabling", uc_pdata->min_uA))
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200212 goto exit;
213
214retdev:
215 if (devp)
216 *devp = dev;
217exit:
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200218 if (verbose)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200219 printf("\n");
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200220
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200221 return ret;
222}
223
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200224int regulator_list_autoset(const char *list_platname[],
225 struct udevice *list_devp[],
226 bool verbose)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200227{
228 struct udevice *dev;
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200229 int error = 0, i = 0, ret;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200230
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200231 while (list_platname[i]) {
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200232 ret = regulator_autoset(list_platname[i], &dev, verbose);
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200233 if (ret & !error)
234 error = ret;
235
236 if (list_devp)
237 list_devp[i] = dev;
238
239 i++;
240 }
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200241
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200242 return error;
243}
244
245static bool regulator_name_is_unique(struct udevice *check_dev,
246 const char *check_name)
247{
248 struct dm_regulator_uclass_platdata *uc_pdata;
249 struct udevice *dev;
250 int check_len = strlen(check_name);
251 int ret;
252 int len;
253
254 for (ret = uclass_find_first_device(UCLASS_REGULATOR, &dev); dev;
255 ret = uclass_find_next_device(&dev)) {
256 if (ret || dev == check_dev)
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200257 continue;
258
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200259 uc_pdata = dev_get_uclass_platdata(dev);
260 len = strlen(uc_pdata->name);
261 if (len != check_len)
262 continue;
263
264 if (!strcmp(uc_pdata->name, check_name))
265 return false;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200266 }
267
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200268 return true;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200269}
270
271static int regulator_post_bind(struct udevice *dev)
272{
273 struct dm_regulator_uclass_platdata *uc_pdata;
274 int offset = dev->of_offset;
275 const void *blob = gd->fdt_blob;
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200276 const char *property = "regulator-name";
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200277
278 uc_pdata = dev_get_uclass_platdata(dev);
279 if (!uc_pdata)
280 return -ENXIO;
281
282 /* Regulator's mandatory constraint */
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200283 uc_pdata->name = fdt_getprop(blob, offset, property, NULL);
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200284 if (!uc_pdata->name) {
285 debug("%s: dev: %s has no property 'regulator-name'\n",
286 __func__, dev->name);
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200287 return -EINVAL;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200288 }
289
Przemyslaw Marczak75692a32015-05-13 13:38:27 +0200290 if (regulator_name_is_unique(dev, uc_pdata->name))
291 return 0;
292
293 error("\"%s\" of dev: \"%s\", has nonunique value: \"%s\"",
294 property, dev->name, uc_pdata->name);
295
296 return -EINVAL;
Przemyslaw Marczak08edd002015-04-20 20:07:42 +0200297}
298
299static int regulator_pre_probe(struct udevice *dev)
300{
301 struct dm_regulator_uclass_platdata *uc_pdata;
302 int offset = dev->of_offset;
303
304 uc_pdata = dev_get_uclass_platdata(dev);
305 if (!uc_pdata)
306 return -ENXIO;
307
308 /* Regulator's optional constraints */
309 uc_pdata->min_uV = fdtdec_get_int(gd->fdt_blob, offset,
310 "regulator-min-microvolt", -ENODATA);
311 uc_pdata->max_uV = fdtdec_get_int(gd->fdt_blob, offset,
312 "regulator-max-microvolt", -ENODATA);
313 uc_pdata->min_uA = fdtdec_get_int(gd->fdt_blob, offset,
314 "regulator-min-microamp", -ENODATA);
315 uc_pdata->max_uA = fdtdec_get_int(gd->fdt_blob, offset,
316 "regulator-max-microamp", -ENODATA);
317 uc_pdata->always_on = fdtdec_get_bool(gd->fdt_blob, offset,
318 "regulator-always-on");
319 uc_pdata->boot_on = fdtdec_get_bool(gd->fdt_blob, offset,
320 "regulator-boot-on");
321
322 return 0;
323}
324
325UCLASS_DRIVER(regulator) = {
326 .id = UCLASS_REGULATOR,
327 .name = "regulator",
328 .post_bind = regulator_post_bind,
329 .pre_probe = regulator_pre_probe,
330 .per_device_platdata_auto_alloc_size =
331 sizeof(struct dm_regulator_uclass_platdata),
332};