pwm: pxa: Fixes for enable/disable transitions
cover pwm: pxa: Fixes for enable/disable transitions
I ran into a couple of problems while getting this driver working on the PXA168. It wouldn't always activate properly when I turned it on, and it wouldn't always deactivate properly when I turned it off. These patches fix issues with the clock enable/disable transitions.
With these patches applied, the driver works flawlessly with my use cases on the PXA168. I don't have any other PXAxxx devices to test with.
Doug Brown (2):
pwm: pxa: Enable clock before applying config
pwm: pxa: Set duty cycle to 0 before disabling
drivers/pwm/pwm-pxa.c | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-)
Signature · 1 line
2.34.1
PATCH 1/2 pwm: pxa: Enable clock before applying config
The clock has to be on before we can apply the config anyway, so there is no need to turn the clock on, off, and back on again.
This fixes an issue discovered on the PXA168 where sometimes the PWM output doesn't activate properly if the clock is turned off and back on too quickly. Removing the on/off/on sequence eliminates the problem.
--- drivers/pwm/pwm-pxa.c | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-)
drivers/pwm/pwm-pxa.c +10 -13
@@ -64,7 +64,6 @@ static int pxa_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,unsigned long long c;unsigned long period_cycles, prescale, pv, dc;unsigned long offset; -int rc; offset = pwm->hwpwm ?0x10 :0 ;
@@ -86,18 +85,11 @@ static int pxa_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,else dc =mul_u64_u64_div_u64 (pv +1 , duty_ns, period_ns); -/* NOTE: the clock to PWM has to be enabled first -* before writing to the registers -*/ - rc =clk_prepare_enable (pc->clk); -if (rc <0 ) -return rc; - +/* Clock is required here. We can assume it's already on. */ writel (prescale, pc->mmio_base + offset +PWMCR );writel (dc, pc->mmio_base + offset +PWMDCR );writel (pv, pc->mmio_base + offset +PWMPCR ); -clk_disable_unprepare (pc->clk);return 0 ; }
@@ -130,12 +122,17 @@ static int pxa_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,return 0 ; } +if (!pwm->state.enabled) { + err =pxa_pwm_enable (chip, pwm); +if (err) +return err; + } + err =pxa_pwm_config (chip, pwm, state->duty_cycle, state->period); -if (err) +if (err) { +pxa_pwm_disable (chip, pwm);return err; - -if (!pwm->state.enabled) -return pxa_pwm_enable (chip, pwm); + }return 0 ; }
Signature · 1 line
2.34.1
Re: [PATCH 1/2] pwm: pxa: Enable clock before applying config
Hello Doug,
Quoted 45 lines · Doug Brown
The clock has to be on before we can apply the config anyway, so there is no need to turn the clock on, off, and back on again.
This fixes an issue discovered on the PXA168 where sometimes the PWM output doesn't activate properly if the clock is turned off and back on too quickly. Removing the on/off/on sequence eliminates the problem.
Signed-off-by Doug Brown <[email protected]>--- drivers/pwm/pwm-pxa.c | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-)drivers/pwm/pwm-pxa.c +1
-9@@ -64,7 +64,6 @@ static int pxa_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,unsigned long long c;unsigned long period_cycles, prescale, pv, dc;unsigned long offset; -int rc; offset = pwm->hwpwm ?0x10 :0 ;@@ -86,18 +85,11 @@ static int pxa_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,else dc =mul_u64_u64_div_u64 (pv +1 , duty_ns, period_ns); -/* NOTE: the clock to PWM has to be enabled first -* before writing to the registers -*/ - rc =clk_prepare_enable (pc->clk); -if (rc <0 ) -return rc; - +/* Clock is required here. We can assume it's already on. */ writel (prescale, pc->mmio_base + offset +PWMCR );writel (dc, pc->mmio_base + offset +PWMDCR );writel (pv, pc->mmio_base + offset +PWMPCR ); -clk_disable_unprepare (pc->clk);return 0 ; }
drivers/pwm/pwm-pxa.c static int pxa_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, · line 129
Quoted 8 lines
drivers/pwm/pwm-pxa.c +5
-0@@ -130,12 +122,17 @@ static int pxa_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,return 0 ; } +if (!pwm->state.enabled) { + err =pxa_pwm_enable (chip, pwm); +if (err) +return err; + }
First, would you mind adding a first patch that just replaces pxa_pwm_enable() by clk_prepare_enable(pc->clk) and pxa_pwm_disable() by clk_disable_unprepare(pc->clk)? Given that the dedicated .enable() and .disable() callbacks are not needed any more, this would simplify the driver a bit.
Then I think to understand that you need the clk enabled for operating the output and for register access, right? I'm not sure, but after the above change it might be simpler/more efficient to enable the clk here unconditionally (and adapt the end of the function accordingly).
drivers/pwm/pwm-pxa.c static int pxa_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, · line 135
Quoted 9 lines
drivers/pwm/pwm-pxa.c +3
-4err =pxa_pwm_config (chip, pwm, state->duty_cycle, state->period); -if (err) +if (err) { +pxa_pwm_disable (chip, pwm);return err; - -if (!pwm->state.enabled) -return pxa_pwm_enable (chip, pwm); + }
If there is a transistion say from:
.period = A, .duty_cycle = B, .enabled = false
to
.period = C, .duty_cycle = D, .enabled = true
can it happen that you (shortly) see A and B on the wire? (I think this is orthogonal to this patch and happens with and without it.) If so it might be prudent do configure duty_cycle = 0 for the .enabled = false case.
Best regards
Uwe
Signature · 2 lines
Pengutronix e.K. | Uwe Kleine-König | Industrial Linux Solutions | https://www.pengutronix.de/ |
Re: [PATCH 1/2] pwm: pxa: Enable clock before applying config
Hi Uwe,
Quoted 5 lines · Uwe Kleine-König
First, would you mind adding a first patch that just replaces
pxa_pwm_enable()byclk_prepare_enable(pc->clk)andpxa_pwm_disable()byclk_disable_unprepare(pc->clk)? Given that the dedicated .enable() and .disable() callbacks are not needed any more, this would simplify the driver a bit.
Will do -- makes sense.
Quoted 4 lines
Then I think to understand that you need the clk enabled for operating the output and for register access, right? I'm not sure, but after the above change it might be simpler/more efficient to enable the clk here unconditionally (and adapt the end of the function accordingly).
Right. It has to be enabled in order to access the registers, and it also simultaneously enables the actual output. What you are suggesting makes perfect sense to me. I will play with that approach for V2.
Quoted 4 lines
If there is a transistion say from:
.period = A, .duty_cycle = B, .enabled = false
Quoted 8 lines
to
.period = C, .duty_cycle = D, .enabled = true
can it happen that you (shortly) see A and B on the wire? (I think this is orthogonal to this patch and happens with and without it.) If so it might be prudent do configure
duty_cycle= 0 for the .enabled = false case.
You are absolutely correct. I had noticed that case was possible, but it didn't click in my head that setting duty_cycle to 0 when disabling would work around it. I need to set duty_cycle to 0 when disabling the PWM anyway, because it seems to have a tendency to get stuck high when stopping the clock.
I also realized I forgot to submit one other patch to allow this driver to be selected with ARCH_MMP (which the PXA168 belongs to). I will include that with V2 as well.
Thanks for reviewing this so quickly and providing great feedback!
Doug
PATCH 2/2 pwm: pxa: Set duty cycle to 0 before disabling
In reply to Doug BrownTurning off the clock doesn't guarantee that the pin will be left in a low state. Set the duty cycle to 0 first to force it to turn off.
Without this fix, the PWM peripheral on a PXA168 was observed to randomly leave the PWM pin high after attempting to turn it off, especially when the duty cycle was set to 100%.
--- drivers/pwm/pwm-pxa.c | 6 ++++++ 1 file changed, 6 insertions(+)
drivers/pwm/pwm-pxa.c +6 -0
@@ -103,6 +103,12 @@ static int pxa_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)static void pxa_pwm_disable (struct pwm_chip *chip,struct pwm_device *pwm) {struct pxa_pwm_chip *pc =to_pxa_pwm_chip (chip); +unsigned long offset = pwm->hwpwm ?0x10 :0 ; + +/* Ensure the duty cycle is set to 0 before turning off the clock. +* Otherwise, the PWM pin might stay high. +*/ +writel (0 , pc->mmio_base + offset +PWMDCR );clk_disable_unprepare (pc->clk); }
Signature · 1 line
2.34.1