1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
/*
* Copyright (C) 2014-2015 Samsung Electronics
* Przemyslaw Marczak <p.marczak@samsung.com>
*
* SPDX-License-Identifier: GPL-2.0+
*/
#include <common.h>
#include <fdtdec.h>
#include <errno.h>
#include <dm.h>
#include <dm/uclass-internal.h>
#include <power/pmic.h>
#include <power/regulator.h>
DECLARE_GLOBAL_DATA_PTR;
int regulator_mode(struct udevice *dev, struct dm_regulator_mode **modep)
{
struct dm_regulator_uclass_platdata *uc_pdata;
*modep = NULL;
uc_pdata = dev_get_uclass_platdata(dev);
if (!uc_pdata)
return -ENXIO;
*modep = uc_pdata->mode;
return uc_pdata->mode_count;
}
int regulator_get_value(struct udevice *dev)
{
const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
if (!ops || !ops->get_value)
return -ENOSYS;
return ops->get_value(dev);
}
int regulator_set_value(struct udevice *dev, int uV)
{
const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
if (!ops || !ops->set_value)
return -ENOSYS;
return ops->set_value(dev, uV);
}
int regulator_get_current(struct udevice *dev)
{
const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
if (!ops || !ops->get_current)
return -ENOSYS;
return ops->get_current(dev);
}
int regulator_set_current(struct udevice *dev, int uA)
{
const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
if (!ops || !ops->set_current)
return -ENOSYS;
return ops->set_current(dev, uA);
}
bool regulator_get_enable(struct udevice *dev)
{
const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
if (!ops || !ops->get_enable)
return -ENOSYS;
return ops->get_enable(dev);
}
int regulator_set_enable(struct udevice *dev, bool enable)
{
const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
if (!ops || !ops->set_enable)
return -ENOSYS;
return ops->set_enable(dev, enable);
}
int regulator_get_mode(struct udevice *dev)
{
const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
if (!ops || !ops->get_mode)
return -ENOSYS;
return ops->get_mode(dev);
}
int regulator_set_mode(struct udevice *dev, int mode)
{
const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
if (!ops || !ops->set_mode)
return -ENOSYS;
return ops->set_mode(dev, mode);
}
int regulator_get_by_platname(const char *plat_name, struct udevice **devp)
{
struct dm_regulator_uclass_platdata *uc_pdata;
struct udevice *dev;
int ret;
*devp = NULL;
for (ret = uclass_find_first_device(UCLASS_REGULATOR, &dev); dev;
ret = uclass_find_next_device(&dev)) {
if (ret)
continue;
uc_pdata = dev_get_uclass_platdata(dev);
if (!uc_pdata || strcmp(plat_name, uc_pdata->name))
continue;
return uclass_get_device_tail(dev, 0, devp);
}
debug("%s: can't find: %s\n", __func__, plat_name);
return -ENODEV;
}
int regulator_get_by_devname(const char *devname, struct udevice **devp)
{
return uclass_get_device_by_name(UCLASS_REGULATOR, devname, devp);
}
int regulator_autoset(struct udevice *dev)
{
struct dm_regulator_uclass_platdata *uc_pdata;
int ret = 0;
uc_pdata = dev_get_uclass_platdata(dev);
if (!uc_pdata->always_on && !uc_pdata->boot_on)
return -EMEDIUMTYPE;
if (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UV)
ret = regulator_set_value(dev, uc_pdata->min_uV);
if (!ret && (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UA))
ret = regulator_set_current(dev, uc_pdata->min_uA);
if (!ret)
ret = regulator_set_enable(dev, true);
return ret;
}
static void regulator_show(struct udevice *dev, int ret)
{
struct dm_regulator_uclass_platdata *uc_pdata;
uc_pdata = dev_get_uclass_platdata(dev);
printf("%s@%s: ", dev->name, uc_pdata->name);
if (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UV)
printf("set %d uV", uc_pdata->min_uV);
if (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UA)
printf("; set %d uA", uc_pdata->min_uA);
printf("; enabling");
if (ret)
printf(" (ret: %d)\n", ret);
printf("\n");
}
int regulator_autoset_by_name(const char *platname, struct udevice **devp)
{
struct udevice *dev;
int ret;
ret = regulator_get_by_platname(platname, &dev);
if (devp)
*devp = dev;
if (ret) {
debug("Can get the regulator: %s!", platname);
return ret;
}
return regulator_autoset(dev);
}
int regulator_list_autoset(const char *list_platname[],
struct udevice *list_devp[],
bool verbose)
{
struct udevice *dev;
int error = 0, i = 0, ret;
while (list_platname[i]) {
ret = regulator_autoset_by_name(list_platname[i], &dev);
if (ret != -EMEDIUMTYPE && verbose)
regulator_show(dev, ret);
if (ret & !error)
error = ret;
if (list_devp)
list_devp[i] = dev;
i++;
}
return error;
}
static bool regulator_name_is_unique(struct udevice *check_dev,
const char *check_name)
{
struct dm_regulator_uclass_platdata *uc_pdata;
struct udevice *dev;
int check_len = strlen(check_name);
int ret;
int len;
for (ret = uclass_find_first_device(UCLASS_REGULATOR, &dev); dev;
ret = uclass_find_next_device(&dev)) {
if (ret || dev == check_dev)
continue;
uc_pdata = dev_get_uclass_platdata(dev);
len = strlen(uc_pdata->name);
if (len != check_len)
continue;
if (!strcmp(uc_pdata->name, check_name))
return false;
}
return true;
}
static int regulator_post_bind(struct udevice *dev)
{
struct dm_regulator_uclass_platdata *uc_pdata;
int offset = dev->of_offset;
const void *blob = gd->fdt_blob;
const char *property = "regulator-name";
uc_pdata = dev_get_uclass_platdata(dev);
if (!uc_pdata)
return -ENXIO;
/* Regulator's mandatory constraint */
uc_pdata->name = fdt_getprop(blob, offset, property, NULL);
if (!uc_pdata->name) {
debug("%s: dev: %s has no property 'regulator-name'\n",
__func__, dev->name);
return -EINVAL;
}
if (regulator_name_is_unique(dev, uc_pdata->name))
return 0;
debug("\"%s\" of dev: \"%s\", has nonunique value: \"%s\"",
property, dev->name, uc_pdata->name);
return -EINVAL;
}
static int regulator_pre_probe(struct udevice *dev)
{
struct dm_regulator_uclass_platdata *uc_pdata;
int offset = dev->of_offset;
uc_pdata = dev_get_uclass_platdata(dev);
if (!uc_pdata)
return -ENXIO;
/* Regulator's optional constraints */
uc_pdata->min_uV = fdtdec_get_int(gd->fdt_blob, offset,
"regulator-min-microvolt", -ENODATA);
uc_pdata->max_uV = fdtdec_get_int(gd->fdt_blob, offset,
"regulator-max-microvolt", -ENODATA);
uc_pdata->min_uA = fdtdec_get_int(gd->fdt_blob, offset,
"regulator-min-microamp", -ENODATA);
uc_pdata->max_uA = fdtdec_get_int(gd->fdt_blob, offset,
"regulator-max-microamp", -ENODATA);
uc_pdata->always_on = fdtdec_get_bool(gd->fdt_blob, offset,
"regulator-always-on");
uc_pdata->boot_on = fdtdec_get_bool(gd->fdt_blob, offset,
"regulator-boot-on");
/* Those values are optional (-ENODATA if unset) */
if ((uc_pdata->min_uV != -ENODATA) &&
(uc_pdata->max_uV != -ENODATA) &&
(uc_pdata->min_uV == uc_pdata->max_uV))
uc_pdata->flags |= REGULATOR_FLAG_AUTOSET_UV;
/* Those values are optional (-ENODATA if unset) */
if ((uc_pdata->min_uA != -ENODATA) &&
(uc_pdata->max_uA != -ENODATA) &&
(uc_pdata->min_uA == uc_pdata->max_uA))
uc_pdata->flags |= REGULATOR_FLAG_AUTOSET_UA;
return 0;
}
int regulators_enable_boot_on(bool verbose)
{
struct udevice *dev;
struct uclass *uc;
int ret;
ret = uclass_get(UCLASS_REGULATOR, &uc);
if (ret)
return ret;
for (uclass_first_device(UCLASS_REGULATOR, &dev);
dev && !ret;
uclass_next_device(&dev)) {
ret = regulator_autoset(dev);
if (ret == -EMEDIUMTYPE) {
ret = 0;
continue;
}
if (verbose)
regulator_show(dev, ret);
}
return ret;
}
UCLASS_DRIVER(regulator) = {
.id = UCLASS_REGULATOR,
.name = "regulator",
.post_bind = regulator_post_bind,
.pre_probe = regulator_pre_probe,
.per_device_platdata_auto_alloc_size =
sizeof(struct dm_regulator_uclass_platdata),
};
|