1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2014-2015 Samsung Electronics
11 #include <dm/uclass-internal.h>
12 #include <linux/delay.h>
13 #include <power/pmic.h>
14 #include <power/regulator.h>
16 int regulator_mode(struct udevice *dev, struct dm_regulator_mode **modep)
18 struct dm_regulator_uclass_platdata *uc_pdata;
22 uc_pdata = dev_get_uclass_platdata(dev);
26 *modep = uc_pdata->mode;
27 return uc_pdata->mode_count;
30 int regulator_get_value(struct udevice *dev)
32 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
34 if (!ops || !ops->get_value)
37 return ops->get_value(dev);
40 static void regulator_set_value_ramp_delay(struct udevice *dev, int old_uV,
41 int new_uV, unsigned int ramp_delay)
43 int delay = DIV_ROUND_UP(abs(new_uV - old_uV), ramp_delay);
45 debug("regulator %s: delay %u us (%d uV -> %d uV)\n", dev->name, delay,
51 int regulator_set_value(struct udevice *dev, int uV)
53 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
54 struct dm_regulator_uclass_platdata *uc_pdata;
55 int ret, old_uV = uV, is_enabled = 0;
57 uc_pdata = dev_get_uclass_platdata(dev);
58 if (uc_pdata->min_uV != -ENODATA && uV < uc_pdata->min_uV)
60 if (uc_pdata->max_uV != -ENODATA && uV > uc_pdata->max_uV)
63 if (!ops || !ops->set_value)
66 if (uc_pdata->ramp_delay) {
67 is_enabled = regulator_get_enable(dev);
68 old_uV = regulator_get_value(dev);
71 ret = ops->set_value(dev, uV);
74 if (uc_pdata->ramp_delay && old_uV > 0 && is_enabled)
75 regulator_set_value_ramp_delay(dev, old_uV, uV,
76 uc_pdata->ramp_delay);
82 int regulator_set_suspend_value(struct udevice *dev, int uV)
84 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
85 struct dm_regulator_uclass_platdata *uc_pdata;
87 uc_pdata = dev_get_uclass_platdata(dev);
88 if (uc_pdata->min_uV != -ENODATA && uV < uc_pdata->min_uV)
90 if (uc_pdata->max_uV != -ENODATA && uV > uc_pdata->max_uV)
93 if (!ops->set_suspend_value)
96 return ops->set_suspend_value(dev, uV);
99 int regulator_get_suspend_value(struct udevice *dev)
101 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
103 if (!ops->get_suspend_value)
106 return ops->get_suspend_value(dev);
110 * To be called with at most caution as there is no check
111 * before setting the actual voltage value.
113 int regulator_set_value_force(struct udevice *dev, int uV)
115 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
117 if (!ops || !ops->set_value)
120 return ops->set_value(dev, uV);
123 int regulator_get_current(struct udevice *dev)
125 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
127 if (!ops || !ops->get_current)
130 return ops->get_current(dev);
133 int regulator_set_current(struct udevice *dev, int uA)
135 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
136 struct dm_regulator_uclass_platdata *uc_pdata;
138 uc_pdata = dev_get_uclass_platdata(dev);
139 if (uc_pdata->min_uA != -ENODATA && uA < uc_pdata->min_uA)
141 if (uc_pdata->max_uA != -ENODATA && uA > uc_pdata->max_uA)
144 if (!ops || !ops->set_current)
147 return ops->set_current(dev, uA);
150 int regulator_get_enable(struct udevice *dev)
152 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
154 if (!ops || !ops->get_enable)
157 return ops->get_enable(dev);
160 int regulator_set_enable(struct udevice *dev, bool enable)
162 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
163 struct dm_regulator_uclass_platdata *uc_pdata;
164 int ret, old_enable = 0;
166 if (!ops || !ops->set_enable)
169 uc_pdata = dev_get_uclass_platdata(dev);
170 if (!enable && uc_pdata->always_on)
173 if (uc_pdata->ramp_delay)
174 old_enable = regulator_get_enable(dev);
176 ret = ops->set_enable(dev, enable);
178 if (uc_pdata->ramp_delay && !old_enable && enable) {
179 int uV = regulator_get_value(dev);
182 regulator_set_value_ramp_delay(dev, 0, uV,
183 uc_pdata->ramp_delay);
191 int regulator_set_enable_if_allowed(struct udevice *dev, bool enable)
195 ret = regulator_set_enable(dev, enable);
196 if (ret == -ENOSYS || ret == -EACCES)
202 int regulator_set_suspend_enable(struct udevice *dev, bool enable)
204 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
206 if (!ops->set_suspend_enable)
209 return ops->set_suspend_enable(dev, enable);
212 int regulator_get_suspend_enable(struct udevice *dev)
214 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
216 if (!ops->get_suspend_enable)
219 return ops->get_suspend_enable(dev);
222 int regulator_get_mode(struct udevice *dev)
224 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
226 if (!ops || !ops->get_mode)
229 return ops->get_mode(dev);
232 int regulator_set_mode(struct udevice *dev, int mode)
234 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
236 if (!ops || !ops->set_mode)
239 return ops->set_mode(dev, mode);
242 int regulator_get_by_platname(const char *plat_name, struct udevice **devp)
244 struct dm_regulator_uclass_platdata *uc_pdata;
250 for (ret = uclass_find_first_device(UCLASS_REGULATOR, &dev); dev;
251 ret = uclass_find_next_device(&dev)) {
253 debug("regulator %s, ret=%d\n", dev->name, ret);
257 uc_pdata = dev_get_uclass_platdata(dev);
258 if (!uc_pdata || strcmp(plat_name, uc_pdata->name))
261 return uclass_get_device_tail(dev, 0, devp);
264 debug("%s: can't find: %s, ret=%d\n", __func__, plat_name, ret);
269 int regulator_get_by_devname(const char *devname, struct udevice **devp)
271 return uclass_get_device_by_name(UCLASS_REGULATOR, devname, devp);
274 int device_get_supply_regulator(struct udevice *dev, const char *supply_name,
275 struct udevice **devp)
277 return uclass_get_device_by_phandle(UCLASS_REGULATOR, dev,
281 int regulator_autoset(struct udevice *dev)
283 struct dm_regulator_uclass_platdata *uc_pdata;
286 uc_pdata = dev_get_uclass_platdata(dev);
288 ret = regulator_set_suspend_enable(dev, uc_pdata->suspend_on);
289 if (!ret && uc_pdata->suspend_on) {
290 ret = regulator_set_suspend_value(dev, uc_pdata->suspend_uV);
295 if (!uc_pdata->always_on && !uc_pdata->boot_on)
298 if (uc_pdata->type == REGULATOR_TYPE_FIXED)
299 return regulator_set_enable(dev, true);
301 if (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UV)
302 ret = regulator_set_value(dev, uc_pdata->min_uV);
303 if (uc_pdata->init_uV > 0)
304 ret = regulator_set_value(dev, uc_pdata->init_uV);
305 if (!ret && (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UA))
306 ret = regulator_set_current(dev, uc_pdata->min_uA);
309 ret = regulator_set_enable(dev, true);
314 static void regulator_show(struct udevice *dev, int ret)
316 struct dm_regulator_uclass_platdata *uc_pdata;
318 uc_pdata = dev_get_uclass_platdata(dev);
320 printf("%s@%s: ", dev->name, uc_pdata->name);
321 if (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UV)
322 printf("set %d uV", uc_pdata->min_uV);
323 if (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UA)
324 printf("; set %d uA", uc_pdata->min_uA);
325 printf("; enabling");
327 printf(" (ret: %d)", ret);
331 int regulator_autoset_by_name(const char *platname, struct udevice **devp)
336 ret = regulator_get_by_platname(platname, &dev);
340 debug("Can get the regulator: %s (err=%d)\n", platname, ret);
344 return regulator_autoset(dev);
347 int regulator_list_autoset(const char *list_platname[],
348 struct udevice *list_devp[],
352 int error = 0, i = 0, ret;
354 while (list_platname[i]) {
355 ret = regulator_autoset_by_name(list_platname[i], &dev);
356 if (ret != -EMEDIUMTYPE && verbose)
357 regulator_show(dev, ret);
370 static bool regulator_name_is_unique(struct udevice *check_dev,
371 const char *check_name)
373 struct dm_regulator_uclass_platdata *uc_pdata;
375 int check_len = strlen(check_name);
379 for (ret = uclass_find_first_device(UCLASS_REGULATOR, &dev); dev;
380 ret = uclass_find_next_device(&dev)) {
381 if (ret || dev == check_dev)
384 uc_pdata = dev_get_uclass_platdata(dev);
385 len = strlen(uc_pdata->name);
386 if (len != check_len)
389 if (!strcmp(uc_pdata->name, check_name))
396 static int regulator_post_bind(struct udevice *dev)
398 struct dm_regulator_uclass_platdata *uc_pdata;
399 const char *property = "regulator-name";
401 uc_pdata = dev_get_uclass_platdata(dev);
403 /* Regulator's mandatory constraint */
404 uc_pdata->name = dev_read_string(dev, property);
405 if (!uc_pdata->name) {
406 debug("%s: dev '%s' has no property '%s'\n",
407 __func__, dev->name, property);
408 uc_pdata->name = dev_read_name(dev);
413 if (regulator_name_is_unique(dev, uc_pdata->name))
416 debug("'%s' of dev: '%s', has nonunique value: '%s\n",
417 property, dev->name, uc_pdata->name);
422 static int regulator_pre_probe(struct udevice *dev)
424 struct dm_regulator_uclass_platdata *uc_pdata;
427 uc_pdata = dev_get_uclass_platdata(dev);
431 /* Regulator's optional constraints */
432 uc_pdata->min_uV = dev_read_u32_default(dev, "regulator-min-microvolt",
434 uc_pdata->max_uV = dev_read_u32_default(dev, "regulator-max-microvolt",
436 uc_pdata->init_uV = dev_read_u32_default(dev, "regulator-init-microvolt",
438 uc_pdata->min_uA = dev_read_u32_default(dev, "regulator-min-microamp",
440 uc_pdata->max_uA = dev_read_u32_default(dev, "regulator-max-microamp",
442 uc_pdata->always_on = dev_read_bool(dev, "regulator-always-on");
443 uc_pdata->boot_on = dev_read_bool(dev, "regulator-boot-on");
444 uc_pdata->ramp_delay = dev_read_u32_default(dev, "regulator-ramp-delay",
447 node = dev_read_subnode(dev, "regulator-state-mem");
448 if (ofnode_valid(node)) {
449 uc_pdata->suspend_on = !ofnode_read_bool(node, "regulator-off-in-suspend");
450 if (ofnode_read_u32(node, "regulator-suspend-microvolt", &uc_pdata->suspend_uV))
451 uc_pdata->suspend_uV = uc_pdata->max_uV;
453 uc_pdata->suspend_on = true;
454 uc_pdata->suspend_uV = uc_pdata->max_uV;
457 /* Those values are optional (-ENODATA if unset) */
458 if ((uc_pdata->min_uV != -ENODATA) &&
459 (uc_pdata->max_uV != -ENODATA) &&
460 (uc_pdata->min_uV == uc_pdata->max_uV))
461 uc_pdata->flags |= REGULATOR_FLAG_AUTOSET_UV;
463 /* Those values are optional (-ENODATA if unset) */
464 if ((uc_pdata->min_uA != -ENODATA) &&
465 (uc_pdata->max_uA != -ENODATA) &&
466 (uc_pdata->min_uA == uc_pdata->max_uA))
467 uc_pdata->flags |= REGULATOR_FLAG_AUTOSET_UA;
472 int regulators_enable_boot_on(bool verbose)
478 ret = uclass_get(UCLASS_REGULATOR, &uc);
481 for (uclass_first_device(UCLASS_REGULATOR, &dev);
483 uclass_next_device(&dev)) {
484 ret = regulator_autoset(dev);
485 if (ret == -EMEDIUMTYPE) {
490 regulator_show(dev, ret);
498 UCLASS_DRIVER(regulator) = {
499 .id = UCLASS_REGULATOR,
501 .post_bind = regulator_post_bind,
502 .pre_probe = regulator_pre_probe,
503 .per_device_platdata_auto =
504 sizeof(struct dm_regulator_uclass_platdata),