Skip to content

Commit 4ab5b3d

Browse files
committed
regulator: core: Provide regmap based voltage_sel operations
Since the voltage selector operations are intended to directly map a bitfield in the device register map into regulator API operations the code for implementing them is usually very standard we can save some code by providing standard implementations for devices using the regmap API. Drivers using regmap can pass their regmap in in the regmap_config struct, set vsel_reg and vsel_mask in their regulator_desc and then use regulator_{get,set}_voltage_sel_regmap in their ops. This saves a small amount of code from each driver. Signed-off-by: Mark Brown <broonie@opensource.wolfsonmicro.com> Acked-by: Liam Girdwood <lrg@ti.com>
1 parent 65b19ce commit 4ab5b3d

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

‎drivers/regulator/core.c‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1838,6 +1838,50 @@ int regulator_is_supported_voltage(struct regulator *regulator,
18381838
}
18391839
EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
18401840

1841+
/**
1842+
* regulator_get_voltage_sel_regmap - standard get_voltage_sel for regmap users
1843+
*
1844+
* @rdev: regulator to operate on
1845+
*
1846+
* Regulators that use regmap for their register I/O can set the
1847+
* vsel_reg and vsel_mask fields in their descriptor and then use this
1848+
* as their get_voltage_vsel operation, saving some code.
1849+
*/
1850+
int regulator_get_voltage_sel_regmap(struct regulator_dev *rdev)
1851+
{
1852+
unsigned int val;
1853+
int ret;
1854+
1855+
ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
1856+
if (ret != 0)
1857+
return ret;
1858+
1859+
val &= rdev->desc->vsel_mask;
1860+
val >>= ffs(rdev->desc->vsel_mask) - 1;
1861+
1862+
return val;
1863+
}
1864+
EXPORT_SYMBOL_GPL(regulator_get_voltage_sel_regmap);
1865+
1866+
/**
1867+
* regulator_set_voltage_sel_regmap - standard set_voltage_sel for regmap users
1868+
*
1869+
* @rdev: regulator to operate on
1870+
* @sel: Selector to set
1871+
*
1872+
* Regulators that use regmap for their register I/O can set the
1873+
* vsel_reg and vsel_mask fields in their descriptor and then use this
1874+
* as their set_voltage_vsel operation, saving some code.
1875+
*/
1876+
int regulator_set_voltage_sel_regmap(struct regulator_dev *rdev, unsigned sel)
1877+
{
1878+
sel <<= ffs(rdev->desc->vsel_mask) - 1;
1879+
1880+
return regmap_update_bits(rdev->regmap, rdev->desc->vsel_reg,
1881+
rdev->desc->vsel_mask, sel);
1882+
}
1883+
EXPORT_SYMBOL_GPL(regulator_set_voltage_sel_regmap);
1884+
18411885
static int _regulator_do_set_voltage(struct regulator_dev *rdev,
18421886
int min_uV, int max_uV)
18431887
{

‎include/linux/regulator/driver.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@ enum regulator_type {
164164
* @irq: Interrupt number for the regulator.
165165
* @type: Indicates if the regulator is a voltage or current regulator.
166166
* @owner: Module providing the regulator, used for refcounting.
167+
168+
* @vsel_reg: Register for selector when using regulator_regmap_X_voltage_
169+
* @vsel_mask: Mask for register bitfield used for selector
167170
*/
168171
struct regulator_desc {
169172
const char *name;
@@ -174,6 +177,9 @@ struct regulator_desc {
174177
int irq;
175178
enum regulator_type type;
176179
struct module *owner;
180+
181+
unsigned int vsel_reg;
182+
unsigned int vsel_mask;
177183
};
178184

179185
/**
@@ -250,6 +256,9 @@ int rdev_get_id(struct regulator_dev *rdev);
250256

251257
int regulator_mode_to_status(unsigned int);
252258

259+
int regulator_get_voltage_sel_regmap(struct regulator_dev *rdev);
260+
int regulator_set_voltage_sel_regmap(struct regulator_dev *rdev, unsigned sel);
261+
253262
void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data);
254263

255264
#endif

0 commit comments

Comments
 (0)