1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (C) 2014-2015 Samsung Electronics
4 * Przemyslaw Marczak <p.marczak@samsung.com>
5 */
6
7 #include <common.h>
8 #include <errno.h>
9 #include <dm.h>
10 #include <dm/uclass-internal.h>
11 #include <power/pmic.h>
12 #include <power/regulator.h>
13
regulator_mode(struct udevice * dev,struct dm_regulator_mode ** modep)14 int 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
regulator_get_value(struct udevice * dev)28 int 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
regulator_set_value_ramp_delay(struct udevice * dev,int old_uV,int new_uV,unsigned int ramp_delay)38 static 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
regulator_set_value(struct udevice * dev,int uV)49 int regulator_set_value(struct udevice *dev, int uV)
50 {
51 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
52 struct dm_regulator_uclass_platdata *uc_pdata;
53 int ret, old_uV = uV, is_enabled = 0;
54
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;
60
61 if (!ops || !ops->set_value)
62 return -ENOSYS;
63
64 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;
78 }
79
80 /*
81 * To be called with at most caution as there is no check
82 * before setting the actual voltage value.
83 */
regulator_set_value_force(struct udevice * dev,int uV)84 int 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
regulator_get_current(struct udevice * dev)94 int 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
regulator_set_current(struct udevice * dev,int uA)104 int regulator_set_current(struct udevice *dev, int uA)
105 {
106 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
107 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;
114
115 if (!ops || !ops->set_current)
116 return -ENOSYS;
117
118 return ops->set_current(dev, uA);
119 }
120
regulator_get_enable(struct udevice * dev)121 int regulator_get_enable(struct udevice *dev)
122 {
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
regulator_set_enable(struct udevice * dev,bool enable)131 int regulator_set_enable(struct udevice *dev, bool enable)
132 {
133 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
134 struct dm_regulator_uclass_platdata *uc_pdata;
135 int ret, old_enable = 0;
136
137 if (!ops || !ops->set_enable)
138 return -ENOSYS;
139
140 uc_pdata = dev_get_uclass_platdata(dev);
141 if (!enable && uc_pdata->always_on)
142 return -EACCES;
143
144 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;
160 }
161
regulator_set_enable_if_allowed(struct udevice * dev,bool enable)162 int 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
regulator_get_mode(struct udevice * dev)173 int 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
regulator_set_mode(struct udevice * dev,int mode)183 int 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
regulator_get_by_platname(const char * plat_name,struct udevice ** devp)193 int regulator_get_by_platname(const char *plat_name, struct udevice **devp)
194 {
195 struct dm_regulator_uclass_platdata *uc_pdata;
196 struct udevice *dev;
197 int ret;
198
199 *devp = NULL;
200
201 for (ret = uclass_find_first_device(UCLASS_REGULATOR, &dev); dev;
202 ret = uclass_find_next_device(&dev)) {
203 if (ret) {
204 debug("regulator %s, ret=%d\n", dev->name, ret);
205 continue;
206 }
207
208 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
215 debug("%s: can't find: %s, ret=%d\n", __func__, plat_name, ret);
216
217 return -ENODEV;
218 }
219
regulator_get_by_devname(const char * devname,struct udevice ** devp)220 int regulator_get_by_devname(const char *devname, struct udevice **devp)
221 {
222 return uclass_get_device_by_name(UCLASS_REGULATOR, devname, devp);
223 }
224
device_get_supply_regulator(struct udevice * dev,const char * supply_name,struct udevice ** devp)225 int 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
regulator_autoset(struct udevice * dev)232 int regulator_autoset(struct udevice *dev)
233 {
234 struct dm_regulator_uclass_platdata *uc_pdata;
235 int ret = 0;
236
237 uc_pdata = dev_get_uclass_platdata(dev);
238 if (!uc_pdata->always_on && !uc_pdata->boot_on)
239 return -EMEDIUMTYPE;
240
241 if (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UV)
242 ret = regulator_set_value(dev, uc_pdata->min_uV);
243 if (!ret && (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UA))
244 ret = regulator_set_current(dev, uc_pdata->min_uA);
245
246 if (!ret)
247 ret = regulator_set_enable(dev, true);
248
249 return ret;
250 }
251
regulator_show(struct udevice * dev,int ret)252 static void regulator_show(struct udevice *dev, int ret)
253 {
254 struct dm_regulator_uclass_platdata *uc_pdata;
255
256 uc_pdata = dev_get_uclass_platdata(dev);
257
258 printf("%s@%s: ", dev->name, uc_pdata->name);
259 if (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UV)
260 printf("set %d uV", uc_pdata->min_uV);
261 if (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UA)
262 printf("; set %d uA", uc_pdata->min_uA);
263 printf("; enabling");
264 if (ret)
265 printf(" (ret: %d)", ret);
266 printf("\n");
267 }
268
regulator_autoset_by_name(const char * platname,struct udevice ** devp)269 int regulator_autoset_by_name(const char *platname, struct udevice **devp)
270 {
271 struct udevice *dev;
272 int ret;
273
274 ret = regulator_get_by_platname(platname, &dev);
275 if (devp)
276 *devp = dev;
277 if (ret) {
278 debug("Can get the regulator: %s (err=%d)\n", platname, ret);
279 return ret;
280 }
281
282 return regulator_autoset(dev);
283 }
284
regulator_list_autoset(const char * list_platname[],struct udevice * list_devp[],bool verbose)285 int regulator_list_autoset(const char *list_platname[],
286 struct udevice *list_devp[],
287 bool verbose)
288 {
289 struct udevice *dev;
290 int error = 0, i = 0, ret;
291
292 while (list_platname[i]) {
293 ret = regulator_autoset_by_name(list_platname[i], &dev);
294 if (ret != -EMEDIUMTYPE && verbose)
295 regulator_show(dev, ret);
296 if (ret & !error)
297 error = ret;
298
299 if (list_devp)
300 list_devp[i] = dev;
301
302 i++;
303 }
304
305 return error;
306 }
307
regulator_name_is_unique(struct udevice * check_dev,const char * check_name)308 static bool regulator_name_is_unique(struct udevice *check_dev,
309 const char *check_name)
310 {
311 struct dm_regulator_uclass_platdata *uc_pdata;
312 struct udevice *dev;
313 int check_len = strlen(check_name);
314 int ret;
315 int len;
316
317 for (ret = uclass_find_first_device(UCLASS_REGULATOR, &dev); dev;
318 ret = uclass_find_next_device(&dev)) {
319 if (ret || dev == check_dev)
320 continue;
321
322 uc_pdata = dev_get_uclass_platdata(dev);
323 len = strlen(uc_pdata->name);
324 if (len != check_len)
325 continue;
326
327 if (!strcmp(uc_pdata->name, check_name))
328 return false;
329 }
330
331 return true;
332 }
333
regulator_post_bind(struct udevice * dev)334 static int regulator_post_bind(struct udevice *dev)
335 {
336 struct dm_regulator_uclass_platdata *uc_pdata;
337 const char *property = "regulator-name";
338
339 uc_pdata = dev_get_uclass_platdata(dev);
340
341 /* Regulator's mandatory constraint */
342 uc_pdata->name = dev_read_string(dev, property);
343 if (!uc_pdata->name) {
344 debug("%s: dev '%s' has no property '%s'\n",
345 __func__, dev->name, property);
346 uc_pdata->name = dev_read_name(dev);
347 if (!uc_pdata->name)
348 return -EINVAL;
349 }
350
351 if (regulator_name_is_unique(dev, uc_pdata->name))
352 return 0;
353
354 debug("'%s' of dev: '%s', has nonunique value: '%s\n",
355 property, dev->name, uc_pdata->name);
356
357 return -EINVAL;
358 }
359
regulator_pre_probe(struct udevice * dev)360 static int regulator_pre_probe(struct udevice *dev)
361 {
362 struct dm_regulator_uclass_platdata *uc_pdata;
363
364 uc_pdata = dev_get_uclass_platdata(dev);
365 if (!uc_pdata)
366 return -ENXIO;
367
368 /* Regulator's optional constraints */
369 uc_pdata->min_uV = dev_read_u32_default(dev, "regulator-min-microvolt",
370 -ENODATA);
371 uc_pdata->max_uV = dev_read_u32_default(dev, "regulator-max-microvolt",
372 -ENODATA);
373 uc_pdata->min_uA = dev_read_u32_default(dev, "regulator-min-microamp",
374 -ENODATA);
375 uc_pdata->max_uA = dev_read_u32_default(dev, "regulator-max-microamp",
376 -ENODATA);
377 uc_pdata->always_on = dev_read_bool(dev, "regulator-always-on");
378 uc_pdata->boot_on = dev_read_bool(dev, "regulator-boot-on");
379 uc_pdata->ramp_delay = dev_read_u32_default(dev, "regulator-ramp-delay",
380 0);
381
382 /* Those values are optional (-ENODATA if unset) */
383 if ((uc_pdata->min_uV != -ENODATA) &&
384 (uc_pdata->max_uV != -ENODATA) &&
385 (uc_pdata->min_uV == uc_pdata->max_uV))
386 uc_pdata->flags |= REGULATOR_FLAG_AUTOSET_UV;
387
388 /* Those values are optional (-ENODATA if unset) */
389 if ((uc_pdata->min_uA != -ENODATA) &&
390 (uc_pdata->max_uA != -ENODATA) &&
391 (uc_pdata->min_uA == uc_pdata->max_uA))
392 uc_pdata->flags |= REGULATOR_FLAG_AUTOSET_UA;
393
394 return 0;
395 }
396
regulators_enable_boot_on(bool verbose)397 int regulators_enable_boot_on(bool verbose)
398 {
399 struct udevice *dev;
400 struct uclass *uc;
401 int ret;
402
403 ret = uclass_get(UCLASS_REGULATOR, &uc);
404 if (ret)
405 return ret;
406 for (uclass_first_device(UCLASS_REGULATOR, &dev);
407 dev;
408 uclass_next_device(&dev)) {
409 ret = regulator_autoset(dev);
410 if (ret == -EMEDIUMTYPE) {
411 ret = 0;
412 continue;
413 }
414 if (verbose)
415 regulator_show(dev, ret);
416 if (ret == -ENOSYS)
417 ret = 0;
418 }
419
420 return ret;
421 }
422
423 UCLASS_DRIVER(regulator) = {
424 .id = UCLASS_REGULATOR,
425 .name = "regulator",
426 .post_bind = regulator_post_bind,
427 .pre_probe = regulator_pre_probe,
428 .per_device_platdata_auto_alloc_size =
429 sizeof(struct dm_regulator_uclass_platdata),
430 };
431