Skip to content

Commit e843fc4

Browse files
committed
regulator: core: Allow regulators to provide a voltage to selector mapping
In order to allow more drivers to factor things out into data allow drivers to provide a mapping function to convert voltages into selectors. This allows any driver to use set_voltage_sel(). The existing mapping based on iterating over list_voltage() is provided as an operation which can be assigned to the new map_voltage() function though for ease of transition it is treated as the default. Signed-off-by: Mark Brown <broonie@opensource.wolfsonmicro.com> Acked-by: Liam Girdwood <lrg@ti.com>
1 parent 5949a7e commit e843fc4

2 files changed

Lines changed: 59 additions & 27 deletions

File tree

‎drivers/regulator/core.c‎

Lines changed: 55 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1967,14 +1967,54 @@ int regulator_set_voltage_sel_regmap(struct regulator_dev *rdev, unsigned sel)
19671967
}
19681968
EXPORT_SYMBOL_GPL(regulator_set_voltage_sel_regmap);
19691969

1970+
/**
1971+
* regulator_map_voltage_iterate - map_voltage() based on list_voltage()
1972+
*
1973+
* @rdev: Regulator to operate on
1974+
* @min_uV: Lower bound for voltage
1975+
* @max_uV: Upper bound for voltage
1976+
*
1977+
* Drivers implementing set_voltage_sel() and list_voltage() can use
1978+
* this as their map_voltage() operation. It will find a suitable
1979+
* voltage by calling list_voltage() until it gets something in bounds
1980+
* for the requested voltages.
1981+
*/
1982+
int regulator_map_voltage_iterate(struct regulator_dev *rdev,
1983+
int min_uV, int max_uV)
1984+
{
1985+
int best_val = INT_MAX;
1986+
int selector = 0;
1987+
int i, ret;
1988+
1989+
/* Find the smallest voltage that falls within the specified
1990+
* range.
1991+
*/
1992+
for (i = 0; i < rdev->desc->n_voltages; i++) {
1993+
ret = rdev->desc->ops->list_voltage(rdev, i);
1994+
if (ret < 0)
1995+
continue;
1996+
1997+
if (ret < best_val && ret >= min_uV && ret <= max_uV) {
1998+
best_val = ret;
1999+
selector = i;
2000+
}
2001+
}
2002+
2003+
if (best_val != INT_MAX)
2004+
return selector;
2005+
else
2006+
return -EINVAL;
2007+
}
2008+
EXPORT_SYMBOL_GPL(regulator_map_voltage_iterate);
2009+
19702010
static int _regulator_do_set_voltage(struct regulator_dev *rdev,
19712011
int min_uV, int max_uV)
19722012
{
19732013
int ret;
19742014
int delay = 0;
2015+
int best_val;
19752016
unsigned int selector;
19762017
int old_selector = -1;
1977-
int best_val = INT_MAX;
19782018

19792019
trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
19802020

@@ -1995,39 +2035,27 @@ static int _regulator_do_set_voltage(struct regulator_dev *rdev,
19952035
if (rdev->desc->ops->set_voltage) {
19962036
ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
19972037
&selector);
1998-
1999-
if (rdev->desc->ops->list_voltage)
2000-
best_val = rdev->desc->ops->list_voltage(rdev,
2001-
selector);
2002-
else
2003-
best_val = -1;
20042038
} else if (rdev->desc->ops->set_voltage_sel) {
2005-
int i;
2006-
2007-
selector = 0;
2008-
2009-
/* Find the smallest voltage that falls within the specified
2010-
* range.
2011-
*/
2012-
for (i = 0; i < rdev->desc->n_voltages; i++) {
2013-
ret = rdev->desc->ops->list_voltage(rdev, i);
2014-
if (ret < 0)
2015-
continue;
2039+
if (rdev->desc->ops->map_voltage)
2040+
ret = rdev->desc->ops->map_voltage(rdev, min_uV,
2041+
max_uV);
2042+
else
2043+
ret = regulator_map_voltage_iterate(rdev, min_uV,
2044+
max_uV);
20162045

2017-
if (ret < best_val && ret >= min_uV && ret <= max_uV) {
2018-
best_val = ret;
2019-
selector = i;
2020-
}
2046+
if (ret >= 0) {
2047+
selector = ret;
2048+
ret = rdev->desc->ops->set_voltage_sel(rdev, ret);
20212049
}
2022-
2023-
if (best_val != INT_MAX)
2024-
ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
2025-
else
2026-
ret = -EINVAL;
20272050
} else {
20282051
ret = -EINVAL;
20292052
}
20302053

2054+
if (rdev->desc->ops->list_voltage)
2055+
best_val = rdev->desc->ops->list_voltage(rdev, selector);
2056+
else
2057+
best_val = -1;
2058+
20312059
/* Call set_voltage_time_sel if successfully obtained old_selector */
20322060
if (ret == 0 && old_selector >= 0 &&
20332061
rdev->desc->ops->set_voltage_time_sel) {

‎include/linux/regulator/driver.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ enum regulator_status {
4646
* The driver should select the voltage closest to min_uV.
4747
* @set_voltage_sel: Set the voltage for the regulator using the specified
4848
* selector.
49+
* @map_voltage: Convert a voltage into a selector
4950
* @get_voltage: Return the currently configured voltage for the regulator.
5051
* @get_voltage_sel: Return the currently configured voltage selector for the
5152
* regulator.
@@ -91,6 +92,7 @@ struct regulator_ops {
9192
/* get/set regulator voltage */
9293
int (*set_voltage) (struct regulator_dev *, int min_uV, int max_uV,
9394
unsigned *selector);
95+
int (*map_voltage)(struct regulator_dev *, int min_uV, int max_uV);
9496
int (*set_voltage_sel) (struct regulator_dev *, unsigned selector);
9597
int (*get_voltage) (struct regulator_dev *);
9698
int (*get_voltage_sel) (struct regulator_dev *);
@@ -260,6 +262,8 @@ int rdev_get_id(struct regulator_dev *rdev);
260262

261263
int regulator_mode_to_status(unsigned int);
262264

265+
int regulator_map_voltage_iterate(struct regulator_dev *rdev,
266+
int min_uV, int max_uV);
263267
int regulator_get_voltage_sel_regmap(struct regulator_dev *rdev);
264268
int regulator_set_voltage_sel_regmap(struct regulator_dev *rdev, unsigned sel);
265269
int regulator_is_enabled_regmap(struct regulator_dev *rdev);

0 commit comments

Comments
 (0)