Skip to content

Commit 73e705b

Browse files
Matthias Kaehlckebroonie
authored andcommitted
regulator: core: Add set_voltage_time op
The new op is analogous to set_voltage_time_sel. It can be used by regulators which don't have a table of discrete voltages. The function returns the time for the regulator output voltage to stabilize after being set to a new value, in microseconds. If the op is not set a default implementation is used to calculate the delay. This change also removes the ramp_delay calculation in the PWM regulator, since the driver now uses the core code for the calculation of the delay. Signed-off-by: Matthias Kaehlcke <mka@chromium.org> Signed-off-by: Mark Brown <broonie@kernel.org>
1 parent d89564e commit 73e705b

File tree

3 files changed

+67
-39
lines changed

3 files changed

+67
-39
lines changed

drivers/regulator/core.c

Lines changed: 59 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2743,6 +2743,24 @@ static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
27432743
return ret;
27442744
}
27452745

2746+
static int _regulator_set_voltage_time(struct regulator_dev *rdev,
2747+
int old_uV, int new_uV)
2748+
{
2749+
unsigned int ramp_delay = 0;
2750+
2751+
if (rdev->constraints->ramp_delay)
2752+
ramp_delay = rdev->constraints->ramp_delay;
2753+
else if (rdev->desc->ramp_delay)
2754+
ramp_delay = rdev->desc->ramp_delay;
2755+
2756+
if (ramp_delay == 0) {
2757+
rdev_warn(rdev, "ramp_delay not set\n");
2758+
return 0;
2759+
}
2760+
2761+
return DIV_ROUND_UP(abs(new_uV - old_uV), ramp_delay);
2762+
}
2763+
27462764
static int _regulator_do_set_voltage(struct regulator_dev *rdev,
27472765
int min_uV, int max_uV)
27482766
{
@@ -2752,6 +2770,7 @@ static int _regulator_do_set_voltage(struct regulator_dev *rdev,
27522770
unsigned int selector;
27532771
int old_selector = -1;
27542772
const struct regulator_ops *ops = rdev->desc->ops;
2773+
int old_uV = _regulator_get_voltage(rdev);
27552774

27562775
trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
27572776

@@ -2803,23 +2822,37 @@ static int _regulator_do_set_voltage(struct regulator_dev *rdev,
28032822
if (ret)
28042823
goto out;
28052824

2806-
/* Call set_voltage_time_sel if successfully obtained old_selector */
2807-
if (!old_selector >= 0 && old_selector != selector) {
2808-
delay = ops->set_voltage_time_sel(rdev,
2809-
old_selector, selector);
2810-
if (delay < 0) {
2811-
rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n",
2812-
delay);
2813-
delay = 0;
2825+
if (ops->set_voltage_time_sel) {
2826+
/*
2827+
* Call set_voltage_time_sel if successfully obtained
2828+
* old_selector
2829+
*/
2830+
if (old_selector >= 0 && old_selector != selector)
2831+
delay = ops->set_voltage_time_sel(rdev, old_selector,
2832+
selector);
2833+
} else {
2834+
if (old_uV != best_val) {
2835+
if (ops->set_voltage_time)
2836+
delay = ops->set_voltage_time(rdev, old_uV,
2837+
best_val);
2838+
else
2839+
delay = _regulator_set_voltage_time(rdev,
2840+
old_uV,
2841+
best_val);
28142842
}
2843+
}
28152844

2816-
/* Insert any necessary delays */
2817-
if (delay >= 1000) {
2818-
mdelay(delay / 1000);
2819-
udelay(delay % 1000);
2820-
} else if (delay) {
2821-
udelay(delay);
2822-
}
2845+
if (delay < 0) {
2846+
rdev_warn(rdev, "failed to get delay: %d\n", delay);
2847+
delay = 0;
2848+
}
2849+
2850+
/* Insert any necessary delays */
2851+
if (delay >= 1000) {
2852+
mdelay(delay / 1000);
2853+
udelay(delay % 1000);
2854+
} else if (delay) {
2855+
udelay(delay);
28232856
}
28242857

28252858
if (best_val >= 0) {
@@ -3000,9 +3033,13 @@ int regulator_set_voltage_time(struct regulator *regulator,
30003033
int voltage;
30013034
int i;
30023035

3036+
if (ops->set_voltage_time)
3037+
return ops->set_voltage_time(rdev, old_uV, new_uV);
3038+
else if (!ops->set_voltage_time_sel)
3039+
return _regulator_set_voltage_time(rdev, old_uV, new_uV);
3040+
30033041
/* Currently requires operations to do this */
3004-
if (!ops->list_voltage || !ops->set_voltage_time_sel
3005-
|| !rdev->desc->n_voltages)
3042+
if (!ops->list_voltage || !rdev->desc->n_voltages)
30063043
return -EINVAL;
30073044

30083045
for (i = 0; i < rdev->desc->n_voltages; i++) {
@@ -3041,25 +3078,20 @@ int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
30413078
unsigned int old_selector,
30423079
unsigned int new_selector)
30433080
{
3044-
unsigned int ramp_delay = 0;
30453081
int old_volt, new_volt;
30463082

3047-
if (rdev->constraints->ramp_delay)
3048-
ramp_delay = rdev->constraints->ramp_delay;
3049-
else if (rdev->desc->ramp_delay)
3050-
ramp_delay = rdev->desc->ramp_delay;
3051-
3052-
if (ramp_delay == 0)
3053-
return 0;
3054-
30553083
/* sanity check */
30563084
if (!rdev->desc->ops->list_voltage)
30573085
return -EINVAL;
30583086

30593087
old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
30603088
new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
30613089

3062-
return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
3090+
if (rdev->desc->ops->set_voltage_time)
3091+
return rdev->desc->ops->set_voltage_time(rdev, old_volt,
3092+
new_volt);
3093+
else
3094+
return _regulator_set_voltage_time(rdev, old_volt, new_volt);
30633095
}
30643096
EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
30653097

drivers/regulator/pwm-regulator.c

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
* published by the Free Software Foundation.
1111
*/
1212

13-
#include <linux/delay.h>
1413
#include <linux/module.h>
1514
#include <linux/init.h>
1615
#include <linux/err.h>
@@ -194,12 +193,10 @@ static int pwm_regulator_set_voltage(struct regulator_dev *rdev,
194193
unsigned int min_uV_duty = drvdata->continuous.min_uV_dutycycle;
195194
unsigned int max_uV_duty = drvdata->continuous.max_uV_dutycycle;
196195
unsigned int duty_unit = drvdata->continuous.dutycycle_unit;
197-
unsigned int ramp_delay = rdev->constraints->ramp_delay;
198196
int min_uV = rdev->constraints->min_uV;
199197
int max_uV = rdev->constraints->max_uV;
200198
int diff_uV = max_uV - min_uV;
201199
struct pwm_state pstate;
202-
int old_uV = pwm_regulator_get_voltage(rdev);
203200
unsigned int diff_duty;
204201
unsigned int dutycycle;
205202
int ret;
@@ -233,13 +230,6 @@ static int pwm_regulator_set_voltage(struct regulator_dev *rdev,
233230
return ret;
234231
}
235232

236-
if ((ramp_delay == 0) || !pwm_regulator_is_enabled(rdev))
237-
return 0;
238-
239-
/* Ramp delay is in uV/uS. Adjust to uS and delay */
240-
ramp_delay = DIV_ROUND_UP(abs(req_min_uV - old_uV), ramp_delay);
241-
usleep_range(ramp_delay, ramp_delay + DIV_ROUND_UP(ramp_delay, 10));
242-
243233
return 0;
244234
}
245235

include/linux/regulator/driver.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,14 @@ struct regulator_linear_range {
113113
* stabilise after being enabled, in microseconds.
114114
* @set_ramp_delay: Set the ramp delay for the regulator. The driver should
115115
* select ramp delay equal to or less than(closest) ramp_delay.
116+
* @set_voltage_time: Time taken for the regulator voltage output voltage
117+
* to stabilise after being set to a new value, in microseconds.
118+
* The function receives the from and to voltage as input, it
119+
* should return the worst case.
116120
* @set_voltage_time_sel: Time taken for the regulator voltage output voltage
117121
* to stabilise after being set to a new value, in microseconds.
118-
* The function provides the from and to voltage selector, the
119-
* function should return the worst case.
122+
* The function receives the from and to voltage selector as
123+
* input, it should return the worst case.
120124
* @set_soft_start: Enable soft start for the regulator.
121125
*
122126
* @set_suspend_voltage: Set the voltage for the regulator when the system
@@ -168,6 +172,8 @@ struct regulator_ops {
168172
/* Time taken to enable or set voltage on the regulator */
169173
int (*enable_time) (struct regulator_dev *);
170174
int (*set_ramp_delay) (struct regulator_dev *, int ramp_delay);
175+
int (*set_voltage_time) (struct regulator_dev *, int old_uV,
176+
int new_uV);
171177
int (*set_voltage_time_sel) (struct regulator_dev *,
172178
unsigned int old_selector,
173179
unsigned int new_selector);

0 commit comments

Comments
 (0)