regulator: pwm: Fix regulator ramp delay for continuous mode
Subsystems (2)
PATCH 1/1 regulator: pwm: Fix regulator ramp delay for continuous mode
The original commit adding support for continuous voltage mode didn't handle the regulator ramp delay properly. It treated the delay as a fixed delay in uS despite the property being defined as uV / uS. Let's adjust it. Luckily there appear to be no users of this ramp delay for PWM regulators (as per grepping through device trees in linuxnext).
Note also that the upper bound of usleep_range probably shouldn't be a full 1 ms longer than the lower bound since I've seen plenty of hardware with a ramp rate of ~5000 uS / uV and for small jumps the total delays are in the tens of uS. 1000 is way too much. We'll try to be dynamic and use 10%
---
Note that this patch is atop Boris's recent PWM regulator fixes. If desired it wouldn't be too hard to write it atop the old code, though quite honestly anyone using a PWM regulator should probably be using his new code.
drivers/regulator/pwm-regulator.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/regulator/pwm-regulator.c +7 -2
@@ -188,6 +188,7 @@ static int pwm_regulator_set_voltage(struct regulator_dev *rdev,struct pwm_state pstate;unsigned int diff_duty;unsigned int dutycycle; +int old_uV =pwm_regulator_get_voltage (rdev);int ret;pwm_init_state (drvdata->pwm, &pstate);
@@ -219,8 +220,12 @@ static int pwm_regulator_set_voltage(struct regulator_dev *rdev,return ret; } -/* Delay required by PWM regulator to settle to the new voltage */ -usleep_range (ramp_delay, ramp_delay +1000 ); +if (ramp_delay ==0 ) +return 0 ; + +/* Ramp delay is in uV/uS. Adjust to uS and delay */ + ramp_delay =DIV_ROUND_UP (abs (req_min_uV - old_uV), ramp_delay); +usleep_range (ramp_delay, ramp_delay +DIV_ROUND_UP (ramp_delay,10 ));return 0 ; }
Signature · 1 line
2.8.0.rc3.226.g39d4020
Quoted 11 lines · Douglas Anderson
The original commit adding support for continuous voltage mode didn't handle the regulator ramp delay properly. It treated the delay as a fixed delay in uS despite the property being defined as uV / uS. Let's adjust it. Luckily there appear to be no users of this ramp delay for PWM regulators (as per grepping through device trees in linuxnext).
Note also that the upper bound of
usleep_rangeprobably shouldn't be a full 1 ms longer than the lower bound since I've seen plenty of hardware with a ramp rate of ~5000 uS / uV and for small jumps the total delays are in the tens of uS. 1000 is way too much. We'll try to be dynamic and use 10%
I'm agree with the dynamic and use 10%.
Quoted 2 lines
Signed-off-by Douglas Anderson <[email protected]>
Tested for my rk3399 board. That's still happy work for my board. ..
[ 2891.541958] pwm_regulator_set_voltage: delay=38, min-v=800000, old-v=1024000 [ 2898.188785] pwm_regulator_set_voltage: delay=13, min-v=875000, old-v=800000 [ 2898.211873] pwm_regulator_set_voltage: delay=8, min-v=925000, old-v=877000 [ 2898.312026] pwm_regulator_set_voltage: delay=21, min-v=-800000, old-v=926000
..
Quoted 36 lines
---
Note that this patch is atop Boris's recent PWM regulator fixes. If desired it wouldn't be too hard to write it atop the old code, though quite honestly anyone using a PWM regulator should probably be using his new code.drivers/regulator/pwm-regulator.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)drivers/regulator/pwm-regulator.c +7
-2@@ -188,6 +188,7 @@ static int pwm_regulator_set_voltage(struct regulator_dev *rdev,struct pwm_state pstate;unsigned int diff_duty;unsigned int dutycycle; +int old_uV =pwm_regulator_get_voltage (rdev);int ret;pwm_init_state (drvdata->pwm, &pstate);@@ -219,8 +220,12 @@ static int pwm_regulator_set_voltage(struct regulator_dev *rdev,return ret; } -/* Delay required by PWM regulator to settle to the new voltage */ -usleep_range (ramp_delay, ramp_delay +1000 ); +if (ramp_delay ==0 ) +return 0 ; + +/* Ramp delay is in uV/uS. Adjust to uS and delay */ + ramp_delay =DIV_ROUND_UP (abs (req_min_uV - old_uV), ramp_delay); +usleep_range (ramp_delay, ramp_delay +DIV_ROUND_UP (ramp_delay,10 ));return 0 ; }
Quoted 5 lines · Douglas Anderson
The original commit adding support for continuous voltage mode didn't handle the regulator ramp delay properly. It treated the delay as a fixed delay in uS despite the property being defined as uV / uS. Let's adjust it. Luckily there appear to be no users of this ramp delay for PWM regulators (as per grepping through device trees in linuxnext).
My grepping agrees, though I'm sure I didn't do a very thorough job.
Quoted 29 lines
Note also that the upper bound of
usleep_rangeprobably shouldn't be a full 1 ms longer than the lower bound since I've seen plenty of hardware with a ramp rate of ~5000 uS / uV and for small jumps the total delays are in the tens of uS. 1000 is way too much. We'll try to be dynamic and use 10%Signed-off-by Douglas Anderson <[email protected]>---
Note that this patch is atop Boris's recent PWM regulator fixes. If desired it wouldn't be too hard to write it atop the old code, though quite honestly anyone using a PWM regulator should probably be using his new code.drivers/regulator/pwm-regulator.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)drivers/regulator/pwm-regulator.c +1
-0@@ -188,6 +188,7 @@ static int pwm_regulator_set_voltage(struct regulator_dev *rdev,struct pwm_state pstate;unsigned int diff_duty;unsigned int dutycycle; +int old_uV =pwm_regulator_get_voltage (rdev);int ret;pwm_init_state (drvdata->pwm, &pstate);
drivers/regulator/pwm-regulator.c static int pwm_regulator_set_voltage(struct regulator_dev *rdev, · line 223
Quoted 5 lines
drivers/regulator/pwm-regulator.c +0
-2@@ -219,8 +220,12 @@ static int pwm_regulator_set_voltage(struct regulator_dev *rdev,return ret; } -/* Delay required by PWM regulator to settle to the new voltage */ -usleep_range (ramp_delay, ramp_delay +1000 );
I was curious about the side effects of the unconditional usleep_range(ramp_delay, ...), even when ramp_delay is 0 (e.g., would we ever sleep here for up to 1ms?), but apparently the implementation optimizes the '0' case to be an unconditional, immediate wake-up. So refactoring this shouldn't have any accidental effects.
drivers/regulator/pwm-regulator.c static int pwm_regulator_set_voltage(struct regulator_dev *rdev, · line 228
Quoted 6 lines
drivers/regulator/pwm-regulator.c +6
-0+if (ramp_delay ==0 ) +return 0 ; + +/* Ramp delay is in uV/uS. Adjust to uS and delay */ + ramp_delay =DIV_ROUND_UP (abs (req_min_uV - old_uV), ramp_delay); +usleep_range (ramp_delay, ramp_delay +DIV_ROUND_UP (ramp_delay,10 ));
Math checks out to me.
drivers/regulator/pwm-regulator.c static int pwm_regulator_set_voltage(struct regulator_dev *rdev, · line 231
Quoted 3 lines
drivers/regulator/pwm-regulator.c +0
-0return 0 ; }
Mark Brown
maintainerQuoted 5 lines · Douglas Anderson
Note also that the upper bound of
usleep_rangeprobably shouldn't be a full 1 ms longer than the lower bound since I've seen plenty of hardware with a ramp rate of ~5000 uS / uV and for small jumps the total delays are in the tens of uS. 1000 is way too much. We'll try to be dynamic and use 10%
Surely the upper bound here is just an upper bound and we're essentially just saying that "anything over minimum is fine" here? Though now I look at the implementation it seems it's doing something entirely unehelpful and actually trying to delay for the longest possible time which doesn't seem like what we want or what the usleep_range() API would suggest :(
Mark Brown
maintainerQuoted 4 lines · Douglas Anderson
Note that this patch is atop Boris's recent PWM regulator fixes. If desired it wouldn't be too hard to write it atop the old code, though quite honestly anyone using a PWM regulator should probably be using his new code.
Oh, and the other thing I meant to say there: is that going anywhere? It seems like the underlying PWM interface is still unclear for some reason and there's been no comment on those bits of v3.
_______________________________________________
Linux-rockchip mailing list [email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip
Mark,
Quoted 8 lines · Mark Brown
Quoted 5 lines · Douglas Anderson
Note also that the upper bound of
usleep_rangeprobably shouldn't be a full 1 ms longer than the lower bound since I've seen plenty of hardware with a ramp rate of ~5000 uS / uV and for small jumps the total delays are in the tens of uS. 1000 is way too much. We'll try to be dynamic and use 10%
Quoted 6 lines
Surely the upper bound here is just an upper bound and we're essentially just saying that "anything over minimum is fine" here? Though now I look at the implementation it seems it's doing something entirely unehelpful and actually trying to delay for the longest possible time which doesn't seem like what we want or what the
usleep_range()API would suggest :(
Yeah, you're thinking like someone who wants your computer to perform fast. That's just simply the wrong mentality to have (how dare you?). You need to think about power savings, and a sleeping computer is a computer that doesn't draw much power. Thus, you want to sleep as long as you can. :-P
In all seriousness, I think the design for usleep_range() is to try to batch up wakeups to allow longer periods of sleeping and to optimize power. This you want to sleep as long as is allowable and then if you happen to wakeup for some other reason anyway then you process all the things whose minimum has passed. IIRC it was trying to get back to the good old days of only having jiffies where everyone was synchronized on the tick and you could sleep till the next tick after all the work was done. When you think of it this way then sleeping to the max makes sense. ...but it also means that you need to be careful and really not set the max too high.
Of course, in practice I've found that often usleep_range() 99% of the time sleeps for the max. That can lead to some very subtle bugs if your min sleep is not long enough (!).
-Doug
Hi,
Quoted 10 lines · Mark Brown
Quoted 4 lines · Douglas Anderson
Note that this patch is atop Boris's recent PWM regulator fixes. If desired it wouldn't be too hard to write it atop the old code, though quite honestly anyone using a PWM regulator should probably be using his new code.
Oh, and the other thing I meant to say there: is that going anywhere? It seems like the underlying PWM interface is still unclear for some reason and there's been no comment on those bits of v3.
As far as I know it is going somewhere, but it's not moving terribly quickly. At some point I think Boris got tired of posting huge patch sets and so split things in two. The first half did eventually land, but the second half is still outstanding and things seem quiet fro the last two weeks. From patchwork we have these outstanding:
9175319 [v3,01/14] pwm: Add an helper to prepare a new PWM state 9175345 [v3,02/14] pwm: Add two helpers to ease relative duty cycle ... 9175361 [v3,03/14] pwm: rockchip: Fix period and duty_cycle approximation 9175341 [v3,04/14] pwm: rockchip: Add support for hardware readout 9175335 [v3,05/14] pwm: rockchip: Avoid glitches on already running PWMs 9175337 [v3,06/14] pwm: rockchip: Add support for atomic update 9175265 [v3,07/14] pwm: sti: Add support for hardware readout 9175317 [v3,08/14] pwm: sti: Avoid glitches on already running PWMs 9175323 [v3,09/14] regulator: pwm: Adjust PWM config at probe time 9175305 [v3,10/14] regulator: pwm: Switch to the atomic PWM API 9175295 [v3,11/14] regulator: pwm: Properly initialize the ->state field 9175267 [v3,12/14] regulator: pwm: Retrieve correct voltage 9175279 [v3,13/14] regulator: pwm: Support extra continuous mode cases 9175273 [v3,14/14] regulator: pwm: Document pwm-dutycycle-unit and ...
-Doug
Mark Brown
maintainerQuoted 10 lines · Doug Anderson
In all seriousness, I think the design for
usleep_range()is to try to batch up wakeups to allow longer periods of sleeping and to optimize power. This you want to sleep as long as is allowable and then if you happen to wakeup for some other reason anyway then you process all the things whose minimum has passed. IIRC it was trying to get back to the good old days of only having jiffies where everyone was synchronized on the tick and you could sleep till the next tick after all the work was done. When you think of it this way then sleeping to the max makes sense. ...but it also means that you need to be careful and really not set the max too high.
That's what I *thought* it was doing, but when I looked at the implementation down to the hrtimer level it was explicitly saying it was actively trying to deliver the maximum delay but might do less. I'd have expected that the implementation would check to see if there was a wakeup scheduled in the range, piggybacking on the first one if so, and if there was nothing it'd go for the minimum (perhaps with a bit of rounding). Oh well.
Quoted 3 lines
Of course, in practice I've found that often
usleep_range()99% of the time sleeps for the max. That can lead to some very subtle bugs if your min sleep is not long enough (!).
Yup.
_______________________________________________
Linux-rockchip mailing list [email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip
Mark Brown
maintainerQuoted 4 lines · Douglas Anderson
Note that this patch is atop Boris's recent PWM regulator fixes. If desired it wouldn't be too hard to write it atop the old code, though quite honestly anyone using a PWM regulator should probably be using his new code.
Given that those don't seem likely to go in any time soon and aren't going to go in as a fix can you please respin against current code?
Mark,
Quoted 9 lines · Mark Brown
Quoted 4 lines · Douglas Anderson
Note that this patch is atop Boris's recent PWM regulator fixes. If desired it wouldn't be too hard to write it atop the old code, though quite honestly anyone using a PWM regulator should probably be using his new code.
Given that those don't seem likely to go in any time soon and aren't going to go in as a fix can you please respin against current code?
I went to write this patch and it was pretty easy to write. ...but then I realized that I had no real way to test it. I can compile test it, but that's it. Do you still want it?
Specifically without Boris's patches then I don't believe the devices I have access to are bootable when I tell Linux about the PWM regulator, especially when I configure the PWM regulator to use continuous mode (which is where the bug is). As I understand from
<https://patchwork.kernel.org/patch/9175279/> the current PWM regulator assumes that 0% duty cycle is the lowest voltage and 100% is the highest voltage. My system is backwards (0% is highest, 100% lowest). Also: last I tried even the non-continuous mode without Boris's patches the regulator would glitch enough at bootup that my system would crash (the PWM regulator affects the memory controller).
Anyway, let me know if you want me to post the untested, rebased patch... Personally I'd prefer to wait for Boris's patches and the land the tested version.
Boris: any idea for what the next steps on your series are?
-Doug
Mark Brown
maintainerQuoted 3 lines · Doug Anderson
Anyway, let me know if you want me to post the untested, rebased patch... Personally I'd prefer to wait for Boris's patches and the land the tested version.
We've got some boards in kernelci.org using PWM regulators I think, or perhaps someone else can test. It does seem like a real fix so...
_______________________________________________
Linux-rockchip mailing list [email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip
+Thierry
Hi Doug,
Quoted 28 lines · On Fri, 1 Jul 2016 13:17:48 -0700 Doug Anderson…
Mark,
Quoted 9 lines · Mark Brown
Quoted 4 lines · Douglas Anderson
Note that this patch is atop Boris's recent PWM regulator fixes. If desired it wouldn't be too hard to write it atop the old code, though quite honestly anyone using a PWM regulator should probably be using his new code.
Given that those don't seem likely to go in any time soon and aren't going to go in as a fix can you please respin against current code?
I went to write this patch and it was pretty easy to write. ...but then I realized that I had no real way to test it. I can compile test it, but that's it. Do you still want it?
Specifically without Boris's patches then I don't believe the devices I have access to are bootable when I tell Linux about the PWM regulator, especially when I configure the PWM regulator to use continuous mode (which is where the bug is). As I understand from
<https://patchwork.kernel.org/patch/9175279/> the current PWM regulator assumes that 0% duty cycle is the lowest voltage and 100% is the highest voltage. My system is backwards (0% is highest, 100% lowest). Also: last I tried even the non-continuous mode without Boris's patches the regulator would glitch enough at bootup that my system would crash (the PWM regulator affects the memory controller).
Quoted 5 lines
Anyway, let me know if you want me to post the untested, rebased patch... Personally I'd prefer to wait for Boris's patches and the land the tested version.
Boris: any idea for what the next steps on your series are?
I'm waiting for reviews or acks. AFAIR, I addressed all the comments Thierry made on my v2, so I guess we're good on the PWM side, but that would be good to have a review from Mark now that we agreed on the PWM APIs.
That shouldn't prevent you from rebasing this patch on Linus' tree and tag it for stable (I'll rebase my series on top of the regulator tree if needed).
Regards,
Boris
Mark Brown
maintainerQuoted 4 lines · Boris Brezillon
I'm waiting for reviews or acks. AFAIR, I addressed all the comments Thierry made on my v2, so I guess we're good on the PWM side, but that would be good to have a review from Mark now that we agreed on the PWM APIs.
I've already have one round of review invalidated by changes in the PWM bits so I've been deprioritising it until it's clear that the churn there has stopped. I understand things are supposed to be quieting down but I also have a bunch of other things to look at...
_______________________________________________
Linux-rockchip mailing list [email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip
Hi Mark,
Quoted 11 lines · On Mon, 4 Jul 2016 10:53:36 +0200 Mark Brown…
Quoted 4 lines · Boris Brezillon
I'm waiting for reviews or acks. AFAIR, I addressed all the comments Thierry made on my v2, so I guess we're good on the PWM side, but that would be good to have a review from Mark now that we agreed on the PWM APIs.
I've already have one round of review invalidated by changes in the PWM bits so I've been deprioritising it until it's clear that the churn there has stopped. I understand things are supposed to be quieting down but I also have a bunch of other things to look at...
I understand, but can we still expect to have it in 4.8 (I mean, assuming Thierry is okay taking the first 2 patching for 4.8 too)?
Thanks,
Boris
Mark Brown
maintainerQuoted 2 lines · Boris Brezillon
I understand, but can we still expect to have it in 4.8 (I mean, assuming Thierry is okay taking the first 2 patching for 4.8 too)?
Possibly - we are getting very close to the merge window now, I don't know what Thierry's policies are on applying things as the merge window gets near.
_______________________________________________
Linux-rockchip mailing list [email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip
Quoted 11 lines · On Mon, 27 Jun 2016 21:53:11 -0700 Douglas Anderson <[email protected]>…
The original commit adding support for continuous voltage mode didn't handle the regulator ramp delay properly. It treated the delay as a fixed delay in uS despite the property being defined as uV / uS. Let's adjust it. Luckily there appear to be no users of this ramp delay for PWM regulators (as per grepping through device trees in linuxnext).
Note also that the upper bound of
usleep_rangeprobably shouldn't be a full 1 ms longer than the lower bound since I've seen plenty of hardware with a ramp rate of ~5000 uS / uV and for small jumps the total delays are in the tens of uS. 1000 is way too much. We'll try to be dynamic and use 10%
I realize I may have introduced another bug when adding the ->enable()/disable() methods: we are only waiting for the ramp up delay when changing the voltage, but it should probably be applied when enabling the PWM, and conditionally applied when changing the voltage only if the PWM is enabled.
Quoted 38 lines
Signed-off-by Douglas Anderson <[email protected]>---
Note that this patch is atop Boris's recent PWM regulator fixes. If desired it wouldn't be too hard to write it atop the old code, though quite honestly anyone using a PWM regulator should probably be using his new code.drivers/regulator/pwm-regulator.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)drivers/regulator/pwm-regulator.c +7
-2@@ -188,6 +188,7 @@ static int pwm_regulator_set_voltage(struct regulator_dev *rdev,struct pwm_state pstate;unsigned int diff_duty;unsigned int dutycycle; +int old_uV =pwm_regulator_get_voltage (rdev);int ret;pwm_init_state (drvdata->pwm, &pstate);@@ -219,8 +220,12 @@ static int pwm_regulator_set_voltage(struct regulator_dev *rdev,return ret; } -/* Delay required by PWM regulator to settle to the new voltage */ -usleep_range (ramp_delay, ramp_delay +1000 ); +if (ramp_delay ==0 ) +return 0 ; + +/* Ramp delay is in uV/uS. Adjust to uS and delay */ + ramp_delay =DIV_ROUND_UP (abs (req_min_uV - old_uV), ramp_delay); +usleep_range (ramp_delay, ramp_delay +DIV_ROUND_UP (ramp_delay,10 ));return 0 ; }
Mark,
Quoted 8 lines · Mark Brown
Quoted 3 lines · Doug Anderson
Anyway, let me know if you want me to post the untested, rebased patch... Personally I'd prefer to wait for Boris's patches and the land the tested version.
We've got some boards in kernelci.org using PWM regulators I think, or perhaps someone else can test. It does seem like a real fix so...
OK, post coming up then. Sorry for the delay--I was on holiday the last two days.
Note: which boards are you thinking would test this? My grep showed zero boards using regulator-ramp-delay with PWM regulator. While this is an important fix for anyone using the ramp delay, I'm not convinced that anyone in mainline is using it.
-Doug
Boris,
Quoted 15 lines · Boris Brezillon
Quoted 11 lines · On Mon, 27 Jun 2016 21:53:11 -0700 Douglas Anderson <[email protected]>…
The original commit adding support for continuous voltage mode didn't handle the regulator ramp delay properly. It treated the delay as a fixed delay in uS despite the property being defined as uV / uS. Let's adjust it. Luckily there appear to be no users of this ramp delay for PWM regulators (as per grepping through device trees in linuxnext).
Note also that the upper bound of
usleep_rangeprobably shouldn't be a full 1 ms longer than the lower bound since I've seen plenty of hardware with a ramp rate of ~5000 uS / uV and for small jumps the total delays are in the tens of uS. 1000 is way too much. We'll try to be dynamic and use 10%
Quoted 5 lines
I realize I may have introduced another bug when adding the ->enable()/disable() methods: we are only waiting for the ramp up delay when changing the voltage, but it should probably be applied when enabling the PWM, and conditionally applied when changing the voltage only if the PWM is enabled.
I'll certainly let Mark comment here, but:
* For enabling the PWM, I think you want want "regulator-enable-ramp-delay".
* Right, we probably shouldn't be delaying if the regulator is off. I'll add that to my patch.
-Doug
Mark Brown
maintainerQuoted 5 lines · Boris Brezillon
I realize I may have introduced another bug when adding the ->enable()/disable() methods: we are only waiting for the ramp up delay when changing the voltage, but it should probably be applied when enabling the PWM, and conditionally applied when changing the voltage only if the PWM is enabled.
Yes, it should be applied on enable.
_______________________________________________
Linux-rockchip mailing list [email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip
Mark Brown
maintainerQuoted 3 lines · Doug Anderson
I'll certainly let Mark comment here, but:
* For enabling the PWM, I think you want want "regulator-enable-ramp-delay".
In the absence of that we should just be treating it as a voltage change from zero. Some regulators do have different characteristics on initial ramp up but if we don't know about them we ought to just use the regular one. Note that the core does have standard code to apply delays which may just be working here.
Mark Brown
maintainerQuoted 4 lines · Doug Anderson
Note: which boards are you thinking would test this? My grep showed zero boards using regulator-ramp-delay with PWM regulator. While this is an important fix for anyone using the ramp delay, I'm not convinced that anyone in mainline is using it.
I just meant there are boards out there using the regulator, not the specific feature.
Quoted 8 lines · Mark Brown
Quoted 2 lines · Boris Brezillon
I understand, but can we still expect to have it in 4.8 (I mean, assuming Thierry is okay taking the first 2 patching for 4.8 too)?
Possibly - we are getting very close to the merge window now, I don't know what Thierry's policies are on applying things as the merge window gets near.
I don't have hard deadlines, especially not for new code that has zero potential for regressing. I've now pulled that whole series into the PWM tree.
Thierry