Skip to content

Commit bca7bbf

Browse files
committed
regulator: core: Allow drivers to set simple linear voltage maps as data
A lot of regulator hardware maps selectors on to voltages with a simple linear mapping function selector = base + (selector * step size) Provide off the shelf list_voltage() and map_voltage() operations which use new min_uV and uV_step members in the regulator_desc to implement this function. Signed-off-by: Mark Brown <broonie@opensource.wolfsonmicro.com> Acked-by: Liam Girdwood <lrg@ti.com>
1 parent e843fc4 commit bca7bbf

2 files changed

Lines changed: 66 additions & 2 deletions

File tree

‎drivers/regulator/core.c‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1859,6 +1859,26 @@ int regulator_count_voltages(struct regulator *regulator)
18591859
}
18601860
EXPORT_SYMBOL_GPL(regulator_count_voltages);
18611861

1862+
/**
1863+
* regulator_list_voltage_linear - List voltages with simple calculation
1864+
*
1865+
* @rdev: Regulator device
1866+
* @selector: Selector to convert into a voltage
1867+
*
1868+
* Regulators with a simple linear mapping between voltages and
1869+
* selectors can set min_uV and uV_step in the regulator descriptor
1870+
* and then use this function as their list_voltage() operation,
1871+
*/
1872+
int regulator_list_voltage_linear(struct regulator_dev *rdev,
1873+
unsigned int selector)
1874+
{
1875+
if (selector >= rdev->desc->n_voltages)
1876+
return -EINVAL;
1877+
1878+
return rdev->desc->min_uV + (rdev->desc->uV_step * selector);
1879+
}
1880+
EXPORT_SYMBOL_GPL(regulator_list_voltage_linear);
1881+
18621882
/**
18631883
* regulator_list_voltage - enumerate supported voltages
18641884
* @regulator: regulator source
@@ -2007,6 +2027,39 @@ int regulator_map_voltage_iterate(struct regulator_dev *rdev,
20072027
}
20082028
EXPORT_SYMBOL_GPL(regulator_map_voltage_iterate);
20092029

2030+
/**
2031+
* regulator_map_voltage_linear - map_voltage() for simple linear mappings
2032+
*
2033+
* @rdev: Regulator to operate on
2034+
* @min_uV: Lower bound for voltage
2035+
* @max_uV: Upper bound for voltage
2036+
*
2037+
* Drivers providing min_uV and uV_step in their regulator_desc can
2038+
* use this as their map_voltage() operation.
2039+
*/
2040+
int regulator_map_voltage_linear(struct regulator_dev *rdev,
2041+
int min_uV, int max_uV)
2042+
{
2043+
int ret, voltage;
2044+
2045+
if (!rdev->desc->uV_step) {
2046+
BUG_ON(!rdev->desc->uV_step);
2047+
return -EINVAL;
2048+
}
2049+
2050+
ret = (min_uV - rdev->desc->min_uV) / rdev->desc->uV_step;
2051+
if (ret < 0)
2052+
return ret;
2053+
2054+
/* Map back into a voltage to verify we're still in bounds */
2055+
voltage = rdev->desc->ops->list_voltage(rdev, ret);
2056+
if (voltage < min_uV || voltage > max_uV)
2057+
return -EINVAL;
2058+
2059+
return ret;
2060+
}
2061+
EXPORT_SYMBOL_GPL(regulator_map_voltage_linear);
2062+
20102063
static int _regulator_do_set_voltage(struct regulator_dev *rdev,
20112064
int min_uV, int max_uV)
20122065
{

‎include/linux/regulator/driver.h‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,16 @@ enum regulator_type {
161161
* @name: Identifying name for the regulator.
162162
* @supply_name: Identifying the regulator supply
163163
* @id: Numerical identifier for the regulator.
164-
* @n_voltages: Number of selectors available for ops.list_voltage().
165164
* @ops: Regulator operations table.
166165
* @irq: Interrupt number for the regulator.
167166
* @type: Indicates if the regulator is a voltage or current regulator.
168167
* @owner: Module providing the regulator, used for refcounting.
169-
168+
*
169+
* @n_voltages: Number of selectors available for ops.list_voltage().
170+
*
171+
* @min_uV: Voltage given by the lowest selector (if linear mapping)
172+
* @uV_step: Voltage increase with each selector (if linear mapping)
173+
*
170174
* @vsel_reg: Register for selector when using regulator_regmap_X_voltage_
171175
* @vsel_mask: Mask for register bitfield used for selector
172176
* @enable_reg: Register for control when using regmap enable/disable ops
@@ -182,6 +186,9 @@ struct regulator_desc {
182186
enum regulator_type type;
183187
struct module *owner;
184188

189+
unsigned int min_uV;
190+
unsigned int uV_step;
191+
185192
unsigned int vsel_reg;
186193
unsigned int vsel_mask;
187194
unsigned int enable_reg;
@@ -262,6 +269,10 @@ int rdev_get_id(struct regulator_dev *rdev);
262269

263270
int regulator_mode_to_status(unsigned int);
264271

272+
int regulator_list_voltage_linear(struct regulator_dev *rdev,
273+
unsigned int selector);
274+
int regulator_map_voltage_linear(struct regulator_dev *rdev,
275+
int min_uV, int max_uV);
265276
int regulator_map_voltage_iterate(struct regulator_dev *rdev,
266277
int min_uV, int max_uV);
267278
int regulator_get_voltage_sel_regmap(struct regulator_dev *rdev);

0 commit comments

Comments
 (0)