2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2015-04-20 18:07:42 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014-2015 Samsung Electronics
|
|
|
|
* Przemyslaw Marczak <p.marczak@samsung.com>
|
|
|
|
*/
|
2017-05-19 02:09:34 +00:00
|
|
|
|
2015-04-20 18:07:42 +00:00
|
|
|
#include <common.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <dm.h>
|
|
|
|
#include <dm/uclass-internal.h>
|
|
|
|
#include <power/pmic.h>
|
|
|
|
#include <power/regulator.h>
|
|
|
|
|
|
|
|
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);
|
2016-10-26 08:12:31 +00:00
|
|
|
struct dm_regulator_uclass_platdata *uc_pdata;
|
|
|
|
|
|
|
|
uc_pdata = dev_get_uclass_platdata(dev);
|
|
|
|
if (uc_pdata->min_uV != -ENODATA && uV < uc_pdata->min_uV)
|
|
|
|
return -EINVAL;
|
|
|
|
if (uc_pdata->max_uV != -ENODATA && uV > uc_pdata->max_uV)
|
|
|
|
return -EINVAL;
|
2015-04-20 18:07:42 +00:00
|
|
|
|
|
|
|
if (!ops || !ops->set_value)
|
|
|
|
return -ENOSYS;
|
|
|
|
|
|
|
|
return ops->set_value(dev, uV);
|
|
|
|
}
|
2016-10-26 08:12:30 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* To be called with at most caution as there is no check
|
|
|
|
* before setting the actual voltage value.
|
|
|
|
*/
|
|
|
|
int regulator_set_value_force(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);
|
|
|
|
}
|
2015-04-20 18:07:42 +00:00
|
|
|
|
|
|
|
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);
|
2016-10-26 08:12:32 +00:00
|
|
|
struct dm_regulator_uclass_platdata *uc_pdata;
|
|
|
|
|
|
|
|
uc_pdata = dev_get_uclass_platdata(dev);
|
|
|
|
if (uc_pdata->min_uA != -ENODATA && uA < uc_pdata->min_uA)
|
|
|
|
return -EINVAL;
|
|
|
|
if (uc_pdata->max_uA != -ENODATA && uA > uc_pdata->max_uA)
|
|
|
|
return -EINVAL;
|
2015-04-20 18:07:42 +00:00
|
|
|
|
|
|
|
if (!ops || !ops->set_current)
|
|
|
|
return -ENOSYS;
|
|
|
|
|
|
|
|
return ops->set_current(dev, uA);
|
|
|
|
}
|
|
|
|
|
2017-06-13 04:23:45 +00:00
|
|
|
int regulator_get_enable(struct udevice *dev)
|
2015-04-20 18:07:42 +00:00
|
|
|
{
|
|
|
|
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);
|
2018-11-15 12:45:31 +00:00
|
|
|
struct dm_regulator_uclass_platdata *uc_pdata;
|
2015-04-20 18:07:42 +00:00
|
|
|
|
|
|
|
if (!ops || !ops->set_enable)
|
|
|
|
return -ENOSYS;
|
|
|
|
|
2018-11-15 12:45:31 +00:00
|
|
|
uc_pdata = dev_get_uclass_platdata(dev);
|
|
|
|
if (!enable && uc_pdata->always_on)
|
|
|
|
return -EACCES;
|
|
|
|
|
2015-04-20 18:07:42 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-05-13 11:38:27 +00:00
|
|
|
int regulator_get_by_platname(const char *plat_name, struct udevice **devp)
|
2015-04-20 18:07:42 +00:00
|
|
|
{
|
|
|
|
struct dm_regulator_uclass_platdata *uc_pdata;
|
|
|
|
struct udevice *dev;
|
2015-05-13 11:38:27 +00:00
|
|
|
int ret;
|
2015-04-20 18:07:42 +00:00
|
|
|
|
|
|
|
*devp = NULL;
|
|
|
|
|
2015-05-13 11:38:27 +00:00
|
|
|
for (ret = uclass_find_first_device(UCLASS_REGULATOR, &dev); dev;
|
|
|
|
ret = uclass_find_next_device(&dev)) {
|
2017-05-31 23:57:15 +00:00
|
|
|
if (ret) {
|
|
|
|
debug("regulator %s, ret=%d\n", dev->name, ret);
|
2015-05-13 11:38:27 +00:00
|
|
|
continue;
|
2017-05-31 23:57:15 +00:00
|
|
|
}
|
2015-05-13 11:38:27 +00:00
|
|
|
|
2015-04-20 18:07:42 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-05-31 23:57:15 +00:00
|
|
|
debug("%s: can't find: %s, ret=%d\n", __func__, plat_name, ret);
|
2015-04-20 18:07:42 +00:00
|
|
|
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2015-05-13 11:38:27 +00:00
|
|
|
int regulator_get_by_devname(const char *devname, struct udevice **devp)
|
2015-04-20 18:07:42 +00:00
|
|
|
{
|
|
|
|
return uclass_get_device_by_name(UCLASS_REGULATOR, devname, devp);
|
|
|
|
}
|
|
|
|
|
2015-10-27 12:07:59 +00:00
|
|
|
int device_get_supply_regulator(struct udevice *dev, const char *supply_name,
|
|
|
|
struct udevice **devp)
|
|
|
|
{
|
|
|
|
return uclass_get_device_by_phandle(UCLASS_REGULATOR, dev,
|
|
|
|
supply_name, devp);
|
|
|
|
}
|
|
|
|
|
2015-06-23 21:38:58 +00:00
|
|
|
int regulator_autoset(struct udevice *dev)
|
2015-04-20 18:07:42 +00:00
|
|
|
{
|
2015-06-23 21:38:58 +00:00
|
|
|
struct dm_regulator_uclass_platdata *uc_pdata;
|
|
|
|
int ret = 0;
|
2015-04-20 18:07:42 +00:00
|
|
|
|
2015-06-23 21:38:58 +00:00
|
|
|
uc_pdata = dev_get_uclass_platdata(dev);
|
|
|
|
if (!uc_pdata->always_on && !uc_pdata->boot_on)
|
|
|
|
return -EMEDIUMTYPE;
|
2015-04-20 18:07:42 +00:00
|
|
|
|
2015-06-23 21:38:58 +00:00
|
|
|
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);
|
2015-04-20 18:07:42 +00:00
|
|
|
|
|
|
|
if (!ret)
|
2015-06-23 21:38:58 +00:00
|
|
|
ret = regulator_set_enable(dev, true);
|
2015-04-20 18:07:42 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-06-23 21:38:58 +00:00
|
|
|
static void regulator_show(struct udevice *dev, int ret)
|
2015-04-20 18:07:42 +00:00
|
|
|
{
|
|
|
|
struct dm_regulator_uclass_platdata *uc_pdata;
|
|
|
|
|
|
|
|
uc_pdata = dev_get_uclass_platdata(dev);
|
2015-05-13 11:38:27 +00:00
|
|
|
|
2015-06-23 21:38:58 +00:00
|
|
|
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)
|
2016-01-22 02:43:58 +00:00
|
|
|
printf(" (ret: %d)", ret);
|
2015-06-23 21:38:58 +00:00
|
|
|
printf("\n");
|
|
|
|
}
|
2015-04-20 18:07:42 +00:00
|
|
|
|
2015-06-23 21:38:58 +00:00
|
|
|
int regulator_autoset_by_name(const char *platname, struct udevice **devp)
|
|
|
|
{
|
|
|
|
struct udevice *dev;
|
|
|
|
int ret;
|
2015-04-20 18:07:42 +00:00
|
|
|
|
2015-06-23 21:38:58 +00:00
|
|
|
ret = regulator_get_by_platname(platname, &dev);
|
2015-04-20 18:07:42 +00:00
|
|
|
if (devp)
|
|
|
|
*devp = dev;
|
2015-06-23 21:38:58 +00:00
|
|
|
if (ret) {
|
2017-05-31 23:57:15 +00:00
|
|
|
debug("Can get the regulator: %s (err=%d)\n", platname, ret);
|
2015-06-23 21:38:58 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2015-05-13 11:38:27 +00:00
|
|
|
|
2015-06-23 21:38:58 +00:00
|
|
|
return regulator_autoset(dev);
|
2015-04-20 18:07:42 +00:00
|
|
|
}
|
|
|
|
|
2015-05-13 11:38:27 +00:00
|
|
|
int regulator_list_autoset(const char *list_platname[],
|
|
|
|
struct udevice *list_devp[],
|
|
|
|
bool verbose)
|
2015-04-20 18:07:42 +00:00
|
|
|
{
|
|
|
|
struct udevice *dev;
|
2015-05-13 11:38:27 +00:00
|
|
|
int error = 0, i = 0, ret;
|
2015-04-20 18:07:42 +00:00
|
|
|
|
2015-05-13 11:38:27 +00:00
|
|
|
while (list_platname[i]) {
|
2015-06-23 21:38:58 +00:00
|
|
|
ret = regulator_autoset_by_name(list_platname[i], &dev);
|
|
|
|
if (ret != -EMEDIUMTYPE && verbose)
|
|
|
|
regulator_show(dev, ret);
|
2015-05-13 11:38:27 +00:00
|
|
|
if (ret & !error)
|
|
|
|
error = ret;
|
|
|
|
|
|
|
|
if (list_devp)
|
|
|
|
list_devp[i] = dev;
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
2015-04-20 18:07:42 +00:00
|
|
|
|
2015-05-13 11:38:27 +00:00
|
|
|
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)
|
2015-04-20 18:07:42 +00:00
|
|
|
continue;
|
|
|
|
|
2015-05-13 11:38:27 +00:00
|
|
|
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;
|
2015-04-20 18:07:42 +00:00
|
|
|
}
|
|
|
|
|
2015-05-13 11:38:27 +00:00
|
|
|
return true;
|
2015-04-20 18:07:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int regulator_post_bind(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct dm_regulator_uclass_platdata *uc_pdata;
|
2015-05-13 11:38:27 +00:00
|
|
|
const char *property = "regulator-name";
|
2015-04-20 18:07:42 +00:00
|
|
|
|
|
|
|
uc_pdata = dev_get_uclass_platdata(dev);
|
|
|
|
|
|
|
|
/* Regulator's mandatory constraint */
|
2017-05-19 02:09:34 +00:00
|
|
|
uc_pdata->name = dev_read_string(dev, property);
|
2015-04-20 18:07:42 +00:00
|
|
|
if (!uc_pdata->name) {
|
2017-05-19 02:09:34 +00:00
|
|
|
debug("%s: dev '%s' has no property '%s'\n",
|
|
|
|
__func__, dev->name, property);
|
|
|
|
uc_pdata->name = dev_read_name(dev);
|
2015-08-07 08:43:42 +00:00
|
|
|
if (!uc_pdata->name)
|
|
|
|
return -EINVAL;
|
2015-04-20 18:07:42 +00:00
|
|
|
}
|
|
|
|
|
2015-05-13 11:38:27 +00:00
|
|
|
if (regulator_name_is_unique(dev, uc_pdata->name))
|
|
|
|
return 0;
|
|
|
|
|
2017-05-19 02:09:34 +00:00
|
|
|
debug("'%s' of dev: '%s', has nonunique value: '%s\n",
|
2015-05-13 11:38:27 +00:00
|
|
|
property, dev->name, uc_pdata->name);
|
|
|
|
|
|
|
|
return -EINVAL;
|
2015-04-20 18:07:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int regulator_pre_probe(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct dm_regulator_uclass_platdata *uc_pdata;
|
|
|
|
|
|
|
|
uc_pdata = dev_get_uclass_platdata(dev);
|
|
|
|
if (!uc_pdata)
|
|
|
|
return -ENXIO;
|
|
|
|
|
|
|
|
/* Regulator's optional constraints */
|
2017-05-19 02:09:34 +00:00
|
|
|
uc_pdata->min_uV = dev_read_u32_default(dev, "regulator-min-microvolt",
|
|
|
|
-ENODATA);
|
|
|
|
uc_pdata->max_uV = dev_read_u32_default(dev, "regulator-max-microvolt",
|
|
|
|
-ENODATA);
|
|
|
|
uc_pdata->min_uA = dev_read_u32_default(dev, "regulator-min-microamp",
|
|
|
|
-ENODATA);
|
|
|
|
uc_pdata->max_uA = dev_read_u32_default(dev, "regulator-max-microamp",
|
|
|
|
-ENODATA);
|
|
|
|
uc_pdata->always_on = dev_read_bool(dev, "regulator-always-on");
|
|
|
|
uc_pdata->boot_on = dev_read_bool(dev, "regulator-boot-on");
|
2015-04-20 18:07:42 +00:00
|
|
|
|
2015-06-23 21:38:57 +00:00
|
|
|
/* 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;
|
|
|
|
|
2015-04-20 18:07:42 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-23 21:38:59 +00:00
|
|
|
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);
|
2016-02-11 20:23:26 +00:00
|
|
|
dev;
|
2015-06-23 21:38:59 +00:00
|
|
|
uclass_next_device(&dev)) {
|
|
|
|
ret = regulator_autoset(dev);
|
2015-07-03 00:16:06 +00:00
|
|
|
if (ret == -EMEDIUMTYPE) {
|
|
|
|
ret = 0;
|
2015-06-23 21:38:59 +00:00
|
|
|
continue;
|
2015-07-03 00:16:06 +00:00
|
|
|
}
|
2015-06-23 21:38:59 +00:00
|
|
|
if (verbose)
|
|
|
|
regulator_show(dev, ret);
|
2016-01-22 02:43:59 +00:00
|
|
|
if (ret == -ENOSYS)
|
|
|
|
ret = 0;
|
2015-06-23 21:38:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-04-20 18:07:42 +00:00
|
|
|
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),
|
|
|
|
};
|