Re: [PATCH v4 1/3] pwm: loongson: Fix low pulse buffer register handling

From: Uwe Kleine-König

Date: Mon Nov 30 2026 - 10:55:45 EST


Hello,

On Sun, Sep 20, 2026 at 08:02:06PM +0800, Keguang Zhang via B4 Relay wrote:
> From: Keguang Zhang <keguang.zhang@xxxxxxxxx>
>
> The Loongson PWM register at offset 0x4 is documented as the Low
> Pulse Buffer Register, which stores the low pulse width rather than
> the duty cycle.
>
> However, this register was incorrectly defined and treated as a
> duty-cycle register. As a result, the duty cycle and low pulse cycle
> are swapped in the generated PWM waveform.
>
> Program the low pulse (period - duty) into the register and
> adjust pwm_loongson_get_state() accordingly when reconstructing the
> duty cycle.
>
> Fixes: 2b62c89448dd ("pwm: Add Loongson PWM controller support")
> Cc: stable@xxxxxxxxxxxxxxx
> Signed-off-by: Keguang Zhang <keguang.zhang@xxxxxxxxx>
> ---
> drivers/pwm/pwm-loongson.c | 29 +++++++++++++++++------------
> 1 file changed, 17 insertions(+), 12 deletions(-)
>
> diff --git a/drivers/pwm/pwm-loongson.c b/drivers/pwm/pwm-loongson.c
> index 98c809bde2b3..7af5d4ce1060 100644
> --- a/drivers/pwm/pwm-loongson.c
> +++ b/drivers/pwm/pwm-loongson.c
> @@ -33,7 +33,7 @@
> #include <linux/units.h>
>
> /* Loongson PWM registers */
> -#define LOONGSON_PWM_REG_DUTY 0x4 /* Low Pulse Buffer Register */
> +#define LOONGSON_PWM_REG_LOW 0x4 /* Low Pulse Buffer Register */
> #define LOONGSON_PWM_REG_PERIOD 0x8 /* Pulse Period Buffer Register */
> #define LOONGSON_PWM_REG_CTRL 0xc /* Control Register */
>
> @@ -120,20 +120,20 @@ static int pwm_loongson_enable(struct pwm_chip *chip, struct pwm_device *pwm)
> static int pwm_loongson_config(struct pwm_chip *chip, struct pwm_device *pwm,
> u64 duty_ns, u64 period_ns)
> {
> - u64 duty, period;
> + u64 low, duty, period;
> struct pwm_loongson_ddata *ddata = to_pwm_loongson_ddata(chip);
>
> - /* duty = duty_ns * ddata->clk_rate / NSEC_PER_SEC */
> - duty = mul_u64_u64_div_u64(duty_ns, ddata->clk_rate, NSEC_PER_SEC);
> - if (duty > U32_MAX)
> - duty = U32_MAX;
> -
> /* period = period_ns * ddata->clk_rate / NSEC_PER_SEC */
> period = mul_u64_u64_div_u64(period_ns, ddata->clk_rate, NSEC_PER_SEC);
> if (period > U32_MAX)
> period = U32_MAX;
>
> - pwm_loongson_writel(ddata, duty, LOONGSON_PWM_REG_DUTY);
> + /* duty = duty_ns * ddata->clk_rate / NSEC_PER_SEC */
> + duty = mul_u64_u64_div_u64_roundup(duty_ns, ddata->clk_rate, NSEC_PER_SEC);
> + duty = min(duty, period);
> + low = period - duty;

That semantic of the register at offset 4 made me wonder if the hardware
emits an inverted signal by default. Looking at the reference manual
linked at the top of the header describes the INVERT bit of the control
register as:

1: .. period starts at high level
0: .. period starts at low level

So for a pwm_state with polarity == PWM_POLARITY_NORMAL the bit must
be set. That's wrong in pwm_loongson_set_polarity() and probably
deserves to be fixed in the same commit. Also the reference manual isn't
very explicit, but if the low_buffer register (named
LOONGSON_PWM_REG_LOW here now) describes the low pulse length for both
settings of the INVERT bit, the fix must look different.

Best regards
Uwe

Attachment: signature.asc
Description: PGP signature