Skip to content

Commit

Permalink
Merge remote-tracking branch 'regulator/topic/linear-range' into regu…
Browse files Browse the repository at this point in the history
…lator-helpers
  • Loading branch information
Mark Brown committed Aug 7, 2013
2 parents 587cea2 + 550bf89 commit eca2a65
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 187 deletions.
95 changes: 95 additions & 0 deletions drivers/regulator/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -2083,6 +2083,43 @@ int regulator_list_voltage_linear(struct regulator_dev *rdev,
}
EXPORT_SYMBOL_GPL(regulator_list_voltage_linear);

/**
* regulator_list_voltage_linear_range - List voltages for linear ranges
*
* @rdev: Regulator device
* @selector: Selector to convert into a voltage
*
* Regulators with a series of simple linear mappings between voltages
* and selectors can set linear_ranges in the regulator descriptor and
* then use this function as their list_voltage() operation,
*/
int regulator_list_voltage_linear_range(struct regulator_dev *rdev,
unsigned int selector)
{
const struct regulator_linear_range *range;
int i;

if (!rdev->desc->n_linear_ranges) {
BUG_ON(!rdev->desc->n_linear_ranges);
return -EINVAL;
}

for (i = 0; i < rdev->desc->n_linear_ranges; i++) {
range = &rdev->desc->linear_ranges[i];

if (!(selector >= range->min_sel &&
selector <= range->max_sel))
continue;

selector -= range->min_sel;

return range->min_uV + (range->uV_step * selector);
}

return -EINVAL;
}
EXPORT_SYMBOL_GPL(regulator_list_voltage_linear_range);

/**
* regulator_list_voltage_table - List voltages with table based mapping
*
Expand Down Expand Up @@ -2373,6 +2410,64 @@ int regulator_map_voltage_linear(struct regulator_dev *rdev,
}
EXPORT_SYMBOL_GPL(regulator_map_voltage_linear);

/**
* regulator_map_voltage_linear - map_voltage() for multiple linear ranges
*
* @rdev: Regulator to operate on
* @min_uV: Lower bound for voltage
* @max_uV: Upper bound for voltage
*
* Drivers providing linear_ranges in their descriptor can use this as
* their map_voltage() callback.
*/
int regulator_map_voltage_linear_range(struct regulator_dev *rdev,
int min_uV, int max_uV)
{
const struct regulator_linear_range *range;
int ret = -EINVAL;
int voltage, i;

if (!rdev->desc->n_linear_ranges) {
BUG_ON(!rdev->desc->n_linear_ranges);
return -EINVAL;
}

for (i = 0; i < rdev->desc->n_linear_ranges; i++) {
range = &rdev->desc->linear_ranges[i];

if (!(min_uV <= range->max_uV && max_uV >= range->min_uV))
continue;

if (min_uV <= range->min_uV)
min_uV = range->min_uV;

/* range->uV_step == 0 means fixed voltage range */
if (range->uV_step == 0) {
ret = 0;
} else {
ret = DIV_ROUND_UP(min_uV - range->min_uV,
range->uV_step);
if (ret < 0)
return ret;
}

ret += range->min_sel;

break;
}

if (i == rdev->desc->n_linear_ranges)
return -EINVAL;

/* 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_range);

static int _regulator_do_set_voltage(struct regulator_dev *rdev,
int min_uV, int max_uV)
{
Expand Down
45 changes: 10 additions & 35 deletions drivers/regulator/da903x.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,39 +252,12 @@ static int da9034_set_dvc_voltage_sel(struct regulator_dev *rdev,
return ret;
}

static int da9034_map_ldo12_voltage(struct regulator_dev *rdev,
int min_uV, int max_uV)
{
struct da903x_regulator_info *info = rdev_get_drvdata(rdev);
int sel;

if (check_range(info, min_uV, max_uV)) {
pr_err("invalid voltage range (%d, %d) uV\n", min_uV, max_uV);
return -EINVAL;
}

sel = DIV_ROUND_UP(min_uV - info->desc.min_uV, info->desc.uV_step);
sel = (sel >= 20) ? sel - 12 : ((sel > 7) ? 8 : sel);

return sel;
}

static int da9034_list_ldo12_voltage(struct regulator_dev *rdev,
unsigned selector)
{
struct da903x_regulator_info *info = rdev_get_drvdata(rdev);
int volt;

if (selector >= 8)
volt = 2700000 + rdev->desc->uV_step * (selector - 8);
else
volt = rdev->desc->min_uV + rdev->desc->uV_step * selector;

if (volt > info->max_uV)
return -EINVAL;

return volt;
}
static const struct regulator_linear_range da9034_ldo12_ranges[] = {
{ .min_uV = 1700000, .max_uV = 2050000, .min_sel = 0, .max_sel = 7,
.uV_step = 50000 },
{ .min_uV = 2700000, .max_uV = 3050000, .min_sel = 8, .max_sel = 15,
.uV_step = 50000 },
};

static struct regulator_ops da903x_regulator_ldo_ops = {
.set_voltage_sel = da903x_set_voltage_sel,
Expand Down Expand Up @@ -332,8 +305,8 @@ static struct regulator_ops da9034_regulator_dvc_ops = {
static struct regulator_ops da9034_regulator_ldo12_ops = {
.set_voltage_sel = da903x_set_voltage_sel,
.get_voltage_sel = da903x_get_voltage_sel,
.list_voltage = da9034_list_ldo12_voltage,
.map_voltage = da9034_map_ldo12_voltage,
.list_voltage = regulator_list_voltage_linear_range,
.map_voltage = regulator_map_voltage_linear_range,
.enable = da903x_enable,
.disable = da903x_disable,
.is_enabled = da903x_is_enabled,
Expand Down Expand Up @@ -476,6 +449,8 @@ static int da903x_regulator_probe(struct platform_device *pdev)
if (ri->desc.id == DA9034_ID_LDO12) {
ri->desc.ops = &da9034_regulator_ldo12_ops;
ri->desc.n_voltages = 16;
ri->desc.linear_ranges = da9034_ldo12_ranges;
ri->desc.n_linear_ranges = ARRAY_SIZE(da9034_ldo12_ranges);
}

if (ri->desc.id == DA9030_ID_LDO14)
Expand Down
104 changes: 24 additions & 80 deletions drivers/regulator/wm831x-ldo.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,41 +62,12 @@ static irqreturn_t wm831x_ldo_uv_irq(int irq, void *data)
* General purpose LDOs
*/

#define WM831X_GP_LDO_SELECTOR_LOW 0xe
#define WM831X_GP_LDO_MAX_SELECTOR 0x1f

static int wm831x_gp_ldo_list_voltage(struct regulator_dev *rdev,
unsigned int selector)
{
/* 0.9-1.6V in 50mV steps */
if (selector <= WM831X_GP_LDO_SELECTOR_LOW)
return 900000 + (selector * 50000);
/* 1.7-3.3V in 100mV steps */
if (selector <= WM831X_GP_LDO_MAX_SELECTOR)
return 1600000 + ((selector - WM831X_GP_LDO_SELECTOR_LOW)
* 100000);
return -EINVAL;
}

static int wm831x_gp_ldo_map_voltage(struct regulator_dev *rdev,
int min_uV, int max_uV)
{
int volt, vsel;

if (min_uV < 900000)
vsel = 0;
else if (min_uV < 1700000)
vsel = ((min_uV - 900000) / 50000);
else
vsel = ((min_uV - 1700000) / 100000)
+ WM831X_GP_LDO_SELECTOR_LOW + 1;

volt = wm831x_gp_ldo_list_voltage(rdev, vsel);
if (volt < min_uV || volt > max_uV)
return -EINVAL;

return vsel;
}
static const struct regulator_linear_range wm831x_gp_ldo_ranges[] = {
{ .min_uV = 900000, .max_uV = 1650000, .min_sel = 0, .max_sel = 14,
.uV_step = 50000 },
{ .min_uV = 1700000, .max_uV = 3300000, .min_sel = 15, .max_sel = 31,
.uV_step = 100000 },
};

static int wm831x_gp_ldo_set_suspend_voltage(struct regulator_dev *rdev,
int uV)
Expand All @@ -105,7 +76,7 @@ static int wm831x_gp_ldo_set_suspend_voltage(struct regulator_dev *rdev,
struct wm831x *wm831x = ldo->wm831x;
int sel, reg = ldo->base + WM831X_LDO_SLEEP_CONTROL;

sel = wm831x_gp_ldo_map_voltage(rdev, uV, uV);
sel = regulator_map_voltage_linear_range(rdev, uV, uV);
if (sel < 0)
return sel;

Expand Down Expand Up @@ -230,8 +201,8 @@ static unsigned int wm831x_gp_ldo_get_optimum_mode(struct regulator_dev *rdev,


static struct regulator_ops wm831x_gp_ldo_ops = {
.list_voltage = wm831x_gp_ldo_list_voltage,
.map_voltage = wm831x_gp_ldo_map_voltage,
.list_voltage = regulator_list_voltage_linear_range,
.map_voltage = regulator_map_voltage_linear_range,
.get_voltage_sel = regulator_get_voltage_sel_regmap,
.set_voltage_sel = regulator_set_voltage_sel_regmap,
.set_suspend_voltage = wm831x_gp_ldo_set_suspend_voltage,
Expand Down Expand Up @@ -290,7 +261,7 @@ static int wm831x_gp_ldo_probe(struct platform_device *pdev)

ldo->desc.id = id;
ldo->desc.type = REGULATOR_VOLTAGE;
ldo->desc.n_voltages = WM831X_GP_LDO_MAX_SELECTOR + 1;
ldo->desc.n_voltages = 32;
ldo->desc.ops = &wm831x_gp_ldo_ops;
ldo->desc.owner = THIS_MODULE;
ldo->desc.vsel_reg = ldo->base + WM831X_LDO_ON_CONTROL;
Expand All @@ -299,6 +270,8 @@ static int wm831x_gp_ldo_probe(struct platform_device *pdev)
ldo->desc.enable_mask = 1 << id;
ldo->desc.bypass_reg = ldo->base;
ldo->desc.bypass_mask = WM831X_LDO1_SWI;
ldo->desc.linear_ranges = wm831x_gp_ldo_ranges;
ldo->desc.n_linear_ranges = ARRAY_SIZE(wm831x_gp_ldo_ranges);

config.dev = pdev->dev.parent;
if (pdata)
Expand Down Expand Up @@ -358,43 +331,12 @@ static struct platform_driver wm831x_gp_ldo_driver = {
* Analogue LDOs
*/


#define WM831X_ALDO_SELECTOR_LOW 0xc
#define WM831X_ALDO_MAX_SELECTOR 0x1f

static int wm831x_aldo_list_voltage(struct regulator_dev *rdev,
unsigned int selector)
{
/* 1-1.6V in 50mV steps */
if (selector <= WM831X_ALDO_SELECTOR_LOW)
return 1000000 + (selector * 50000);
/* 1.7-3.5V in 100mV steps */
if (selector <= WM831X_ALDO_MAX_SELECTOR)
return 1600000 + ((selector - WM831X_ALDO_SELECTOR_LOW)
* 100000);
return -EINVAL;
}

static int wm831x_aldo_map_voltage(struct regulator_dev *rdev,
int min_uV, int max_uV)
{
int volt, vsel;

if (min_uV < 1000000)
vsel = 0;
else if (min_uV < 1700000)
vsel = ((min_uV - 1000000) / 50000);
else
vsel = ((min_uV - 1700000) / 100000)
+ WM831X_ALDO_SELECTOR_LOW + 1;

volt = wm831x_aldo_list_voltage(rdev, vsel);
if (volt < min_uV || volt > max_uV)
return -EINVAL;

return vsel;

}
static const struct regulator_linear_range wm831x_aldo_ranges[] = {
{ .min_uV = 1000000, .max_uV = 1650000, .min_sel = 0, .max_sel = 12,
.uV_step = 50000 },
{ .min_uV = 1700000, .max_uV = 3500000, .min_sel = 13, .max_sel = 31,
.uV_step = 100000 },
};

static int wm831x_aldo_set_suspend_voltage(struct regulator_dev *rdev,
int uV)
Expand All @@ -403,7 +345,7 @@ static int wm831x_aldo_set_suspend_voltage(struct regulator_dev *rdev,
struct wm831x *wm831x = ldo->wm831x;
int sel, reg = ldo->base + WM831X_LDO_SLEEP_CONTROL;

sel = wm831x_aldo_map_voltage(rdev, uV, uV);
sel = regulator_map_voltage_linear_range(rdev, uV, uV);
if (sel < 0)
return sel;

Expand Down Expand Up @@ -486,8 +428,8 @@ static int wm831x_aldo_get_status(struct regulator_dev *rdev)
}

static struct regulator_ops wm831x_aldo_ops = {
.list_voltage = wm831x_aldo_list_voltage,
.map_voltage = wm831x_aldo_map_voltage,
.list_voltage = regulator_list_voltage_linear_range,
.map_voltage = regulator_map_voltage_linear_range,
.get_voltage_sel = regulator_get_voltage_sel_regmap,
.set_voltage_sel = regulator_set_voltage_sel_regmap,
.set_suspend_voltage = wm831x_aldo_set_suspend_voltage,
Expand Down Expand Up @@ -545,7 +487,9 @@ static int wm831x_aldo_probe(struct platform_device *pdev)

ldo->desc.id = id;
ldo->desc.type = REGULATOR_VOLTAGE;
ldo->desc.n_voltages = WM831X_ALDO_MAX_SELECTOR + 1;
ldo->desc.n_voltages = 32;
ldo->desc.linear_ranges = wm831x_aldo_ranges;
ldo->desc.n_linear_ranges = ARRAY_SIZE(wm831x_aldo_ranges);
ldo->desc.ops = &wm831x_aldo_ops;
ldo->desc.owner = THIS_MODULE;
ldo->desc.vsel_reg = ldo->base + WM831X_LDO_ON_CONTROL;
Expand Down
Loading

0 comments on commit eca2a65

Please sign in to comment.