2 * Copyright (C) 2014-2015 Samsung Electronics
3 * Przemyslaw Marczak <p.marczak@samsung.com>
5 * SPDX-License-Identifier: GPL-2.0+
11 #include <dm/uclass-internal.h>
12 #include <power/pmic.h>
13 #include <power/regulator.h>
15 DECLARE_GLOBAL_DATA_PTR;
17 int regulator_mode(struct udevice *dev, struct dm_regulator_mode **modep)
19 struct dm_regulator_uclass_platdata *uc_pdata;
23 uc_pdata = dev_get_uclass_platdata(dev);
27 *modep = uc_pdata->mode;
28 return uc_pdata->mode_count;
31 int regulator_get_value(struct udevice *dev)
33 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
35 if (!ops || !ops->get_value)
38 return ops->get_value(dev);
41 int regulator_set_value(struct udevice *dev, int uV)
43 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
45 if (!ops || !ops->set_value)
48 return ops->set_value(dev, uV);
51 int regulator_get_current(struct udevice *dev)
53 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
55 if (!ops || !ops->get_current)
58 return ops->get_current(dev);
61 int regulator_set_current(struct udevice *dev, int uA)
63 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
65 if (!ops || !ops->set_current)
68 return ops->set_current(dev, uA);
71 bool regulator_get_enable(struct udevice *dev)
73 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
75 if (!ops || !ops->get_enable)
78 return ops->get_enable(dev);
81 int regulator_set_enable(struct udevice *dev, bool enable)
83 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
85 if (!ops || !ops->set_enable)
88 return ops->set_enable(dev, enable);
91 int regulator_get_mode(struct udevice *dev)
93 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
95 if (!ops || !ops->get_mode)
98 return ops->get_mode(dev);
101 int regulator_set_mode(struct udevice *dev, int mode)
103 const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
105 if (!ops || !ops->set_mode)
108 return ops->set_mode(dev, mode);
111 int regulator_by_platname(const char *plat_name, struct udevice **devp)
113 struct dm_regulator_uclass_platdata *uc_pdata;
118 for (uclass_find_first_device(UCLASS_REGULATOR, &dev);
120 uclass_find_next_device(&dev)) {
121 uc_pdata = dev_get_uclass_platdata(dev);
122 if (!uc_pdata || strcmp(plat_name, uc_pdata->name))
125 return uclass_get_device_tail(dev, 0, devp);
128 debug("%s: can't find: %s\n", __func__, plat_name);
133 int regulator_by_devname(const char *devname, struct udevice **devp)
135 return uclass_get_device_by_name(UCLASS_REGULATOR, devname, devp);
138 static int setting_failed(int ret, bool verbose, const char *fmt, ...)
143 if (verbose == false)
147 vscnprintf(buf, sizeof(buf), fmt, args);
155 printf(" (ret: %d)", ret);
160 int regulator_by_platname_autoset_and_enable(const char *platname,
161 struct udevice **devp,
164 struct dm_regulator_uclass_platdata *uc_pdata;
172 ret = regulator_by_platname(platname, &dev);
174 error("Can get the regulator: %s!", platname);
178 uc_pdata = dev_get_uclass_platdata(dev);
180 error("Can get the regulator %s uclass platdata!", platname);
185 printf("%s@%s: ", dev->name, uc_pdata->name);
187 /* Those values are optional (-ENODATA if unset) */
188 if ((uc_pdata->min_uV != -ENODATA) &&
189 (uc_pdata->max_uV != -ENODATA) &&
190 (uc_pdata->min_uV == uc_pdata->max_uV)) {
191 ret = regulator_set_value(dev, uc_pdata->min_uV);
192 if (setting_failed(ret, v, "set %d uV", uc_pdata->min_uV))
196 /* Those values are optional (-ENODATA if unset) */
197 if ((uc_pdata->min_uA != -ENODATA) &&
198 (uc_pdata->max_uA != -ENODATA) &&
199 (uc_pdata->min_uA == uc_pdata->max_uA)) {
200 ret = regulator_set_current(dev, uc_pdata->min_uA);
201 if (setting_failed(ret, v, "; set %d uA", uc_pdata->min_uA))
205 if (!uc_pdata->always_on && !uc_pdata->boot_on)
208 ret = regulator_set_enable(dev, true);
209 if (setting_failed(ret, v, "; enabling", uc_pdata->min_uA))
221 int regulator_by_platname_list_autoset_and_enable(const char *list_platname[],
223 struct udevice *list_devp[],
227 int i, ret, success = 0;
229 for (i = 0; i < list_entries; i++) {
230 ret = regulator_autoset(list_platname[i], &dev, verbose);
243 return (success != list_entries);
246 static int regulator_post_bind(struct udevice *dev)
248 struct dm_regulator_uclass_platdata *uc_pdata;
249 int offset = dev->of_offset;
250 const void *blob = gd->fdt_blob;
252 uc_pdata = dev_get_uclass_platdata(dev);
256 /* Regulator's mandatory constraint */
257 uc_pdata->name = fdt_getprop(blob, offset, "regulator-name", NULL);
258 if (!uc_pdata->name) {
259 debug("%s: dev: %s has no property 'regulator-name'\n",
260 __func__, dev->name);
267 static int regulator_pre_probe(struct udevice *dev)
269 struct dm_regulator_uclass_platdata *uc_pdata;
270 int offset = dev->of_offset;
272 uc_pdata = dev_get_uclass_platdata(dev);
276 /* Regulator's optional constraints */
277 uc_pdata->min_uV = fdtdec_get_int(gd->fdt_blob, offset,
278 "regulator-min-microvolt", -ENODATA);
279 uc_pdata->max_uV = fdtdec_get_int(gd->fdt_blob, offset,
280 "regulator-max-microvolt", -ENODATA);
281 uc_pdata->min_uA = fdtdec_get_int(gd->fdt_blob, offset,
282 "regulator-min-microamp", -ENODATA);
283 uc_pdata->max_uA = fdtdec_get_int(gd->fdt_blob, offset,
284 "regulator-max-microamp", -ENODATA);
285 uc_pdata->always_on = fdtdec_get_bool(gd->fdt_blob, offset,
286 "regulator-always-on");
287 uc_pdata->boot_on = fdtdec_get_bool(gd->fdt_blob, offset,
288 "regulator-boot-on");
293 UCLASS_DRIVER(regulator) = {
294 .id = UCLASS_REGULATOR,
296 .post_bind = regulator_post_bind,
297 .pre_probe = regulator_pre_probe,
298 .per_device_platdata_auto_alloc_size =
299 sizeof(struct dm_regulator_uclass_platdata),