From cca859864b6f1af7ae4e9d699b249b59e2efa6b8 Mon Sep 17 00:00:00 2001 From: Andrey Smirnov Date: Wed, 16 Jan 2019 18:16:46 -0800 Subject: regulator: Add support for setting regulator's voltage Add code needed to implement regulator_set_voltage(). Currently only bare minmum needed for ANATOP driver (added in follow up commit) is supported. Signed-off-by: Andrey Smirnov Signed-off-by: Sascha Hauer --- drivers/regulator/core.c | 44 ++++++++++++++++++++ drivers/regulator/helpers.c | 99 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 143 insertions(+) (limited to 'drivers/regulator') diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c index bcfbda62e3..795dcdb8c1 100644 --- a/drivers/regulator/core.c +++ b/drivers/regulator/core.c @@ -43,6 +43,15 @@ struct regulator { struct device_d *dev; }; +static int regulator_map_voltage(struct regulator_dev *rdev, int min_uV, + int max_uV) +{ + if (rdev->desc->ops->list_voltage == regulator_list_voltage_linear) + return regulator_map_voltage_linear(rdev, min_uV, max_uV); + + return -ENOSYS; +} + static int regulator_enable_internal(struct regulator_internal *ri) { int ret; @@ -86,6 +95,33 @@ static int regulator_disable_internal(struct regulator_internal *ri) return 0; } +static int regulator_set_voltage_internal(struct regulator_internal *ri, + int min_uV, int max_uV) +{ + struct regulator_dev *rdev = ri->rdev; + const struct regulator_ops *ops = rdev->desc->ops; + unsigned int selector; + int best_val = 0; + int ret; + + if (ops->set_voltage_sel) { + ret = regulator_map_voltage(rdev, min_uV, max_uV); + if (ret >= 0) { + best_val = ops->list_voltage(rdev, ret); + if (min_uV <= best_val && max_uV >= best_val) { + selector = ret; + ret = ops->set_voltage_sel(rdev, selector); + } else { + ret = -EINVAL; + } + } + + return ret; + } + + return -ENOSYS; +} + static struct regulator_internal * __regulator_register(struct regulator_dev *rd, const char *name) { struct regulator_internal *ri; @@ -320,6 +356,14 @@ int regulator_disable(struct regulator *r) return regulator_disable_internal(r->ri); } +int regulator_set_voltage(struct regulator *r, int min_uV, int max_uV) +{ + if (!r) + return 0; + + return regulator_set_voltage_internal(r->ri, min_uV, max_uV); +} + static void regulator_print_one(struct regulator_internal *ri) { struct regulator *r; diff --git a/drivers/regulator/helpers.c b/drivers/regulator/helpers.c index 4495b4403f..f22d21b35d 100644 --- a/drivers/regulator/helpers.c +++ b/drivers/regulator/helpers.c @@ -85,3 +85,102 @@ int regulator_disable_regmap(struct regulator_dev *rdev) rdev->desc->enable_mask, val); } EXPORT_SYMBOL_GPL(regulator_disable_regmap); + +/** + * regulator_set_voltage_sel_regmap - standard set_voltage_sel for regmap users + * + * @rdev: regulator to operate on + * @sel: Selector to set + * + * Regulators that use regmap for their register I/O can set the + * vsel_reg and vsel_mask fields in their descriptor and then use this + * as their set_voltage_vsel operation, saving some code. + */ +int regulator_set_voltage_sel_regmap(struct regulator_dev *rdev, unsigned sel) +{ + int ret; + + sel <<= ffs(rdev->desc->vsel_mask) - 1; + + ret = regmap_update_bits(rdev->regmap, rdev->desc->vsel_reg, + rdev->desc->vsel_mask, sel); + if (ret) + return ret; + + if (rdev->desc->apply_bit) + ret = regmap_update_bits(rdev->regmap, rdev->desc->apply_reg, + rdev->desc->apply_bit, + rdev->desc->apply_bit); + return ret; +} +EXPORT_SYMBOL_GPL(regulator_set_voltage_sel_regmap); + +/** + * regulator_map_voltage_linear - map_voltage() for simple linear mappings + * + * @rdev: Regulator to operate on + * @min_uV: Lower bound for voltage + * @max_uV: Upper bound for voltage + * + * Drivers providing min_uV and uV_step in their regulator_desc can + * use this as their map_voltage() operation. + */ +int regulator_map_voltage_linear(struct regulator_dev *rdev, + int min_uV, int max_uV) +{ + int ret, voltage; + + /* Allow uV_step to be 0 for fixed voltage */ + if (rdev->desc->n_voltages == 1 && rdev->desc->uV_step == 0) { + if (min_uV <= rdev->desc->min_uV && rdev->desc->min_uV <= max_uV) + return 0; + else + return -EINVAL; + } + + if (!rdev->desc->uV_step) { + BUG_ON(!rdev->desc->uV_step); + return -EINVAL; + } + + if (min_uV < rdev->desc->min_uV) + min_uV = rdev->desc->min_uV; + + ret = DIV_ROUND_UP(min_uV - rdev->desc->min_uV, rdev->desc->uV_step); + if (ret < 0) + return ret; + + ret += rdev->desc->linear_min_sel; + + /* Map back into a voltage to verify we're still in bounds */ + voltage = rdev->desc->ops->list_voltage(rdev, ret); + if (voltage < min_uV || voltage > max_uV) + return -EINVAL; + + return ret; +} +EXPORT_SYMBOL_GPL(regulator_map_voltage_linear); + +/** + * regulator_list_voltage_linear - List voltages with simple calculation + * + * @rdev: Regulator device + * @selector: Selector to convert into a voltage + * + * Regulators with a simple linear mapping between voltages and + * selectors can set min_uV and uV_step in the regulator descriptor + * and then use this function as their list_voltage() operation, + */ +int regulator_list_voltage_linear(struct regulator_dev *rdev, + unsigned int selector) +{ + if (selector >= rdev->desc->n_voltages) + return -EINVAL; + if (selector < rdev->desc->linear_min_sel) + return 0; + + selector -= rdev->desc->linear_min_sel; + + return rdev->desc->min_uV + (rdev->desc->uV_step * selector); +} +EXPORT_SYMBOL_GPL(regulator_list_voltage_linear); -- cgit v1.2.3