pwm: pwm-samsung: Trigger manual update when disabling PWM
Subsystems (2)
PATCH v2 1/1 pwm: pwm-samsung: Trigger manual update when disabling PWM
When duty-cycle is at full level (100%), the TCNTn and TCMPn registers needs to be flushed in order to disable the signal. The PWM manual does not say anything about this, but states that only clearing the TCON auto-reload bit should be needed, and this seems to be true when the PWM duty-cycle is not at full level. This can be observed on an Axis ARTPEC-8, by running:
echo <period> > pwm/period
echo <period> > pwm/duty_cycle
echo 1 > pwm/enable
echo 0 > pwm/enable
Since the TCNTn and TCMPn registers are activated when enabling the PWM (setting TCON auto-reload bit), and are not touched when disabling the PWM, the double buffered auto-reload function seems to be still active. Lowering duty-cycle, and restoring it again in between the enabling and disabling, makes the disable work since it triggers a reload of the TCNTn and TCMPn registers.
Fix this by securing a reload of the TCNTn and TCMPn registers when disabling the PWM and having a full duty-cycle.
---
v2:
- Move fix above setting of disabled_mask
drivers/pwm/pwm-samsung.c | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-)
drivers/pwm/pwm-samsung.c +21 -4
@@ -105,6 +105,9 @@ struct samsung_pwm_chip {static DEFINE_SPINLOCK (samsung_pwm_lock);#endif +static void __pwm_samsung_manual_update (struct samsung_pwm_chip *chip, +struct pwm_device *pwm); +static inline struct samsung_pwm_chip *to_samsung_pwm_chip (struct pwm_chip *chip) {
@@ -276,19 +279,23 @@ static void pwm_samsung_disable(struct pwm_chip *chip, struct pwm_device *pwm) tcon &= ~TCON_AUTORELOAD (tcon_chan);writel (tcon, our_chip->base +REG_TCON ); +/* +* In case the PWM is at 100% duty cycle, force a manual +* update to prevent the signal from staying high. +*/ +if (readl (our_chip->base +REG_TCMPB (pwm->hwpwm)) == (u32)-1U ) +__pwm_samsung_manual_update (our_chip, pwm); + our_chip->disabled_mask |=BIT (pwm->hwpwm);spin_unlock_irqrestore (&samsung_pwm_lock, flags); } -static void pwm_samsung_manual_update (struct samsung_pwm_chip *chip, +static void __pwm_samsung_manual_update (struct samsung_pwm_chip *chip,struct pwm_device *pwm) {unsigned int tcon_chan =to_tcon_channel (pwm->hwpwm);u32 tcon; -unsigned long flags; - -spin_lock_irqsave (&samsung_pwm_lock, flags); tcon =readl (chip->base +REG_TCON ); tcon |=TCON_MANUALUPDATE (tcon_chan);
@@ -296,6 +303,16 @@ static void pwm_samsung_manual_update(struct samsung_pwm_chip *chip, tcon &= ~TCON_MANUALUPDATE (tcon_chan);writel (tcon, chip->base +REG_TCON ); +} + +static void pwm_samsung_manual_update (struct samsung_pwm_chip *chip, +struct pwm_device *pwm) +{ +unsigned long flags; + +spin_lock_irqsave (&samsung_pwm_lock, flags); + +__pwm_samsung_manual_update (chip, pwm);spin_unlock_irqrestore (&samsung_pwm_lock, flags); }
Signature · 1 line
2.20.1
Hello,
Quoted 36 lines · Mårten Lindahl
When duty-cycle is at full level (100%), the TCNTn and TCMPn registers needs to be flushed in order to disable the signal. The PWM manual does not say anything about this, but states that only clearing the TCON auto-reload bit should be needed, and this seems to be true when the PWM duty-cycle is not at full level. This can be observed on an Axis ARTPEC-8, by running:
echo <period> > pwm/period
echo <period> > pwm/duty_cycle
echo 1 > pwm/enable
echo 0 > pwm/enableSince the TCNTn and TCMPn registers are activated when enabling the PWM (setting TCON auto-reload bit), and are not touched when disabling the PWM, the double buffered auto-reload function seems to be still active. Lowering duty-cycle, and restoring it again in between the enabling and disabling, makes the disable work since it triggers a reload of the TCNTn and TCMPn registers.
Fix this by securing a reload of the TCNTn and TCMPn registers when disabling the PWM and having a full duty-cycle.
Signed-off-by Mårten Lindahl <[email protected]>---
v2:
- Move fix above setting ofdisabled_maskdrivers/pwm/pwm-samsung.c | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-)drivers/pwm/pwm-samsung.c +0
-0
drivers/pwm/pwm-samsung.c struct samsung_pwm_chip { · line 110
Quoted 6 lines
drivers/pwm/pwm-samsung.c +3
-0@@ -105,6 +105,9 @@ struct samsung_pwm_chip {static DEFINE_SPINLOCK (samsung_pwm_lock);#endif +static void __pwm_samsung_manual_update (struct samsung_pwm_chip *chip, +struct pwm_device *pwm); +
If you move the definition of __pwm_samsung_manual_update before pwm_samsung_disable() you can drop this declaration:
drivers/pwm/pwm-samsung.c +26 -29
@@ -105,9 +105,6 @@ struct samsung_pwm_chip {static DEFINE_SPINLOCK (samsung_pwm_lock);#endif -static void __pwm_samsung_manual_update (struct samsung_pwm_chip *chip, -struct pwm_device *pwm); -static inline struct samsung_pwm_chip *to_samsung_pwm_chip (struct pwm_chip *chip) {
@@ -120,6 +117,32 @@ static inline unsigned int to_tcon_channel(unsigned int channel)return (channel ==0 ) ?0 : (channel +1 ); } +static void __pwm_samsung_manual_update (struct samsung_pwm_chip *chip, +struct pwm_device *pwm) +{ +unsigned int tcon_chan =to_tcon_channel (pwm->hwpwm); +u32 tcon; + + tcon =readl (chip->base +REG_TCON ); + tcon |=TCON_MANUALUPDATE (tcon_chan); +writel (tcon, chip->base +REG_TCON ); + + tcon &= ~TCON_MANUALUPDATE (tcon_chan); +writel (tcon, chip->base +REG_TCON ); +} + +static void pwm_samsung_manual_update (struct samsung_pwm_chip *chip, +struct pwm_device *pwm) +{ +unsigned long flags; + +spin_lock_irqsave (&samsung_pwm_lock, flags); + +__pwm_samsung_manual_update (chip, pwm); + +spin_unlock_irqrestore (&samsung_pwm_lock, flags); +} +static void pwm_samsung_set_divisor (struct samsung_pwm_chip *pwm,unsigned int channel,u8 divisor) {
@@ -291,32 +314,6 @@ static void pwm_samsung_disable(struct pwm_chip *chip, struct pwm_device *pwm)spin_unlock_irqrestore (&samsung_pwm_lock, flags); } -static void __pwm_samsung_manual_update (struct samsung_pwm_chip *chip, -struct pwm_device *pwm) -{ -unsigned int tcon_chan =to_tcon_channel (pwm->hwpwm); -u32 tcon; - - tcon =readl (chip->base +REG_TCON ); - tcon |=TCON_MANUALUPDATE (tcon_chan); -writel (tcon, chip->base +REG_TCON ); - - tcon &= ~TCON_MANUALUPDATE (tcon_chan); -writel (tcon, chip->base +REG_TCON ); -} - -static void pwm_samsung_manual_update (struct samsung_pwm_chip *chip, -struct pwm_device *pwm) -{ -unsigned long flags; - -spin_lock_irqsave (&samsung_pwm_lock, flags); - -__pwm_samsung_manual_update (chip, pwm); - -spin_unlock_irqrestore (&samsung_pwm_lock, flags); -} -static int __pwm_samsung_config (struct pwm_chip *chip,struct pwm_device *pwm,int duty_ns,int period_ns,bool force_period) {
)
Maybe split the patch to have it nice and reviewable?
Orthogonal to your patch: I wonder what &samsung_pwm_lock actually protects and why it disables irqs. In general the pwm functions might sleep, at least some implementations do.
Best regards
Uwe
Signature · 2 lines
Pengutronix e.K. | Uwe Kleine-König | Industrial Linux Solutions | https://www.pengutronix.de/ |
On Thu, Sep 09, 2021 at 10:05:17AM +0200, Uwe Kleine-König wrote:
Hi Uwe!
Thanks for your feedback!
Quoted 42 lines
Hello,
Quoted 39 lines · Mårten Lindahl
When duty-cycle is at full level (100%), the TCNTn and TCMPn registers needs to be flushed in order to disable the signal. The PWM manual does not say anything about this, but states that only clearing the TCON auto-reload bit should be needed, and this seems to be true when the PWM duty-cycle is not at full level. This can be observed on an Axis ARTPEC-8, by running:
echo <period> > pwm/period
echo <period> > pwm/duty_cycle
echo 1 > pwm/enable
echo 0 > pwm/enableSince the TCNTn and TCMPn registers are activated when enabling the PWM (setting TCON auto-reload bit), and are not touched when disabling the PWM, the double buffered auto-reload function seems to be still active. Lowering duty-cycle, and restoring it again in between the enabling and disabling, makes the disable work since it triggers a reload of the TCNTn and TCMPn registers.
Fix this by securing a reload of the TCNTn and TCMPn registers when disabling the PWM and having a full duty-cycle.
Signed-off-by Mårten Lindahl <[email protected]>---
v2:
- Move fix above setting ofdisabled_maskdrivers/pwm/pwm-samsung.c | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-)drivers/pwm/pwm-samsung.c +0
-0@@ -105,6 +105,9 @@ struct samsung_pwm_chip {static DEFINE_SPINLOCK (samsung_pwm_lock);#endif
Quoted 7 lines
drivers/pwm/pwm-samsung.c static inline unsigned int to_tcon_channel(unsigned int channel) · line 125
Quoted 3 lines
drivers/pwm/pwm-samsung.c +3
-0+static void __pwm_samsung_manual_update (struct samsung_pwm_chip *chip, +struct pwm_device *pwm); +If you move the definition of
__pwm_samsung_manual_updatebeforepwm_samsung_disable()you can drop this declaration:
Yes, I will do that. Thanks.
Quoted 81 lines
drivers/pwm/pwm-samsung.c +26
-29@@ -105,9 +105,6 @@ struct samsung_pwm_chip {static DEFINE_SPINLOCK (samsung_pwm_lock);#endif -static void __pwm_samsung_manual_update (struct samsung_pwm_chip *chip, -struct pwm_device *pwm); -static inline struct samsung_pwm_chip *to_samsung_pwm_chip (struct pwm_chip *chip) {@@ -120,6 +117,32 @@ static inline unsigned int to_tcon_channel(unsigned int channel)return (channel ==0 ) ?0 : (channel +1 ); } +static void __pwm_samsung_manual_update (struct samsung_pwm_chip *chip, +struct pwm_device *pwm) +{ +unsigned int tcon_chan =to_tcon_channel (pwm->hwpwm); +u32 tcon; + + tcon =readl (chip->base +REG_TCON ); + tcon |=TCON_MANUALUPDATE (tcon_chan); +writel (tcon, chip->base +REG_TCON ); + + tcon &= ~TCON_MANUALUPDATE (tcon_chan); +writel (tcon, chip->base +REG_TCON ); +} + +static void pwm_samsung_manual_update (struct samsung_pwm_chip *chip, +struct pwm_device *pwm) +{ +unsigned long flags; + +spin_lock_irqsave (&samsung_pwm_lock, flags); + +__pwm_samsung_manual_update (chip, pwm); + +spin_unlock_irqrestore (&samsung_pwm_lock, flags); +} +static void pwm_samsung_set_divisor (struct samsung_pwm_chip *pwm,unsigned int channel,u8 divisor) {@@ -291,32 +314,6 @@ static void pwm_samsung_disable(struct pwm_chip *chip, struct pwm_device *pwm)spin_unlock_irqrestore (&samsung_pwm_lock, flags); } -static void __pwm_samsung_manual_update (struct samsung_pwm_chip *chip, -struct pwm_device *pwm) -{ -unsigned int tcon_chan =to_tcon_channel (pwm->hwpwm); -u32 tcon; - - tcon =readl (chip->base +REG_TCON ); - tcon |=TCON_MANUALUPDATE (tcon_chan); -writel (tcon, chip->base +REG_TCON ); - - tcon &= ~TCON_MANUALUPDATE (tcon_chan); -writel (tcon, chip->base +REG_TCON ); -} - -static void pwm_samsung_manual_update (struct samsung_pwm_chip *chip, -struct pwm_device *pwm) -{ -unsigned long flags; - -spin_lock_irqsave (&samsung_pwm_lock, flags); - -__pwm_samsung_manual_update (chip, pwm); - -spin_unlock_irqrestore (&samsung_pwm_lock, flags); -} -static int __pwm_samsung_config (struct pwm_chip *chip,struct pwm_device *pwm,int duty_ns,int period_ns,bool force_period) {
Quoted 3 lines
)
Maybe split the patch to have it nice and reviewable?
If I only move up the definition of __pwm_samsung_manual_update, and leave pwm_samsung_manual_update at its place, the patch becomes quite straightforward and overviewable. Or do you prefer to group the definitions of those two functions together?
Kind regards
Mårten
Quoted 11 lines
Orthogonal to your patch: I wonder what
&samsung_pwm_lockactually protects and why it disables irqs. In general the pwm functions might sleep, at least some implementations do.Best regards
UweSignature · 2 lines
Pengutronix e.K. | Uwe Kleine-König | Industrial Linux Solutions | https://www.pengutronix.de/ |
Hello Marten,
Quoted 4 lines · Marten Lindahl
If I only move up the definition of
__pwm_samsung_manual_update, and leavepwm_samsung_manual_updateat its place, the patch becomes quite straightforward and overviewable. Or do you prefer to group the definitions of those two functions together?
No strong preference. I found it natural to keep __pwm_samsung_manual_update and pwm_samsung_manual_update together, but I wouldn't object your suggestion either.
Best regards
Uwe
Signature · 2 lines
Pengutronix e.K. | Uwe Kleine-König | Industrial Linux Solutions | https://www.pengutronix.de/ |