Re: [PATCH v4 2/3] pwm: loongson: Reset the counters before enabling PWM

From: Uwe Kleine-König

Date: Mon Nov 30 2026 - 11:10:53 EST


On Sun, Sep 20, 2026 at 08:02:07PM +0800, Keguang Zhang via B4 Relay wrote:
> From: Keguang Zhang <keguang.zhang@xxxxxxxxx>
>
> The PWM controller may retain its initial hardware state when the driver
> binds. Reset the counters before enabling to ensure that the PWM starts
> from a known state.
>
> Fixes: 2b62c89448dd ("pwm: Add Loongson PWM controller support")
> Fixes: 0de9b836a3bc ("pwm: loongson: Reload PWM configuration through counter reset")
> Cc: stable@xxxxxxxxxxxxxxx
> Suggested-by: Uwe Kleine-König <ukleinek@xxxxxxxxxx>
> Signed-off-by: Keguang Zhang <keguang.zhang@xxxxxxxxx>
> ---
> drivers/pwm/pwm-loongson.c | 8 ++++++++
> 1 file changed, 8 insertions(+)
>
> diff --git a/drivers/pwm/pwm-loongson.c b/drivers/pwm/pwm-loongson.c
> index 7af5d4ce1060..977d867bdb8a 100644
> --- a/drivers/pwm/pwm-loongson.c
> +++ b/drivers/pwm/pwm-loongson.c
> @@ -110,6 +110,14 @@ static int pwm_loongson_enable(struct pwm_chip *chip, struct pwm_device *pwm)
> struct pwm_loongson_ddata *ddata = to_pwm_loongson_ddata(chip);
>
> val = pwm_loongson_readl(ddata, LOONGSON_PWM_REG_CTRL);
> +
> + /*
> + * Ensure that the values of LOW and PERIOD are sampled into
> + * the shadow register when the hardware starts running.
> + */
> + if (!(val & LOONGSON_PWM_CTRL_REG_RST))
> + pwm_loongson_writel(ddata, val | LOONGSON_PWM_CTRL_REG_RST, LOONGSON_PWM_REG_CTRL);
> +

When the PWM transitions from one enabled state to another the driver
doesn't go through pwm_loongson_disable() + pwm_loongson_enable(). The
reference manual specifies to do that though (as I already noted in my
review of patch #3). If this is kept (which might be ok if verified
properly, and pointed out in a comment that this is against the
recommendation of the docs), I wonder about

If the register is reconfigured while PWM is running, it does
not complete the currently running period.

which is written at the top of the header. If the internel counter is
not reset but stays at the current value it might happen to be bigger
than the new period. What happens then?

Best regards
Uwe

Attachment: signature.asc
Description: PGP signature