Skip to content

Commit e8eef82

Browse files
broonieLiam Girdwood
authored andcommitted
regulator: Provide a selector based set_voltage_sel() operation
Many regulator drivers implement voltage setting by looping through a table of possible values, normally because the set of available voltages can't be mapped onto selectors with simple calcuation. Factor out these loops by providing a variant of set_voltage() which takes a selector rather than a voltage range as an argument and implementing a loop through the available selectors in the core. This is not going to be suitable for use with all devices as when the regulator voltage can be mapped onto selector values with a simple calculation the linear scan through the available values will be more expensive than just doing the calculation, especially for regulators that provide fine grained voltage control. Signed-off-by: Mark Brown <broonie@opensource.wolfsonmicro.com> Signed-off-by: Liam Girdwood <lrg@slimlogic.co.uk>
1 parent 7579025 commit e8eef82

2 files changed

Lines changed: 38 additions & 2 deletions

File tree

‎drivers/regulator/core.c‎

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1637,6 +1637,32 @@ static int _regulator_do_set_voltage(struct regulator_dev *rdev,
16371637
selector);
16381638
else
16391639
selector = -1;
1640+
} else if (rdev->desc->ops->set_voltage_sel) {
1641+
int best_val = INT_MAX;
1642+
int i;
1643+
1644+
selector = 0;
1645+
1646+
/* Find the smallest voltage that falls within the specified
1647+
* range.
1648+
*/
1649+
for (i = 0; i < rdev->desc->n_voltages; i++) {
1650+
ret = rdev->desc->ops->list_voltage(rdev, i);
1651+
if (ret < 0)
1652+
continue;
1653+
1654+
if (ret < best_val && ret >= min_uV && ret <= max_uV) {
1655+
best_val = ret;
1656+
selector = i;
1657+
}
1658+
}
1659+
1660+
if (best_val != INT_MAX) {
1661+
ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
1662+
selector = best_val;
1663+
} else {
1664+
ret = -EINVAL;
1665+
}
16401666
} else {
16411667
ret = -EINVAL;
16421668
}
@@ -1672,7 +1698,8 @@ int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
16721698
mutex_lock(&rdev->mutex);
16731699

16741700
/* sanity check */
1675-
if (!rdev->desc->ops->set_voltage) {
1701+
if (!rdev->desc->ops->set_voltage &&
1702+
!rdev->desc->ops->set_voltage_sel) {
16761703
ret = -EINVAL;
16771704
goto out;
16781705
}
@@ -2256,7 +2283,7 @@ static int add_regulator_attributes(struct regulator_dev *rdev)
22562283
return status;
22572284

22582285
/* constraints need specific supporting methods */
2259-
if (ops->set_voltage) {
2286+
if (ops->set_voltage || ops->set_voltage_sel) {
22602287
status = device_create_file(dev, &dev_attr_min_microvolts);
22612288
if (status < 0)
22622289
return status;
@@ -2354,12 +2381,18 @@ struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
23542381
/* Only one of each should be implemented */
23552382
WARN_ON(regulator_desc->ops->get_voltage &&
23562383
regulator_desc->ops->get_voltage_sel);
2384+
WARN_ON(regulator_desc->ops->set_voltage &&
2385+
regulator_desc->ops->set_voltage_sel);
23572386

23582387
/* If we're using selectors we must implement list_voltage. */
23592388
if (regulator_desc->ops->get_voltage_sel &&
23602389
!regulator_desc->ops->list_voltage) {
23612390
return ERR_PTR(-EINVAL);
23622391
}
2392+
if (regulator_desc->ops->set_voltage_sel &&
2393+
!regulator_desc->ops->list_voltage) {
2394+
return ERR_PTR(-EINVAL);
2395+
}
23632396

23642397
rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
23652398
if (rdev == NULL)

‎include/linux/regulator/driver.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ enum regulator_status {
4242
*
4343
* @set_voltage: Set the voltage for the regulator within the range specified.
4444
* The driver should select the voltage closest to min_uV.
45+
* @set_voltage_sel: Set the voltage for the regulator using the specified
46+
* selector.
4547
* @get_voltage: Return the currently configured voltage for the regulator.
4648
* @get_voltage_sel: Return the currently configured voltage selector for the
4749
* regulator.
@@ -83,6 +85,7 @@ struct regulator_ops {
8385
/* get/set regulator voltage */
8486
int (*set_voltage) (struct regulator_dev *, int min_uV, int max_uV,
8587
unsigned *selector);
88+
int (*set_voltage_sel) (struct regulator_dev *, unsigned selector);
8689
int (*get_voltage) (struct regulator_dev *);
8790
int (*get_voltage_sel) (struct regulator_dev *);
8891

0 commit comments

Comments
 (0)