Linux kernel -stable discussions
 help / color / mirror / Atom feed
* [PATCH v4 0/3] pwm: loongson: Fix PWM configuration handling
@ 2026-09-20 12:02 Keguang Zhang via B4 Relay
  2026-09-20 12:02 ` [PATCH v4 1/3] pwm: loongson: Fix low pulse buffer register handling Keguang Zhang via B4 Relay
  2026-09-20 12:02 ` [PATCH v4 2/3] pwm: loongson: Reset the counters before enabling PWM Keguang Zhang via B4 Relay
  0 siblings, 2 replies; 5+ messages in thread
From: Keguang Zhang via B4 Relay @ 2026-09-20 12:02 UTC (permalink / raw)
  To: Binbin Zhou, Uwe Kleine-König
  Cc: linux-pwm, linux-kernel, Keguang Zhang, stable

This series fixes two issues in the Loongson PWM driver:

- Fix the handling of the Low Pulse Buffer Register, which was
  incorrectly treated as a duty-cycle register.

- Reset the PWM counters before enabling to ensure that
  the PWM starts from a known state.

It also includes a follow-up rework:

- Use field helpers for period validation.

The whole patchset was verified on LS1B, LS1C, and LS2K0300 boards.

Signed-off-by: Keguang Zhang <keguang.zhang@gmail.com>
---
Changes in v4:
- Split the period validation rework into a separate patch from the
  functional fixes.
- Limit the duty cycle to the period before calculating the low pulse.
- Add a fix to reset the PWM counters before enabling, as suggested by
  Uwe Kleine-König.
- Drop v3 patch 2/3 (0de9b836a3bc), which has already been applied to
  pwm/for-next.
- Link to v3: https://lore.kernel.org/r/20260715-pwm-loongson-fix-v3-0-0aab2847eaa7@gmail.com

Changes in v3:
- Calculate the low pulse as period - duty after rounding the duty cycle.
- Handle LOW > PERIOD in pwm_loongson_get_state().
- Add a Fixes tag to the second patch.
- Link to v2: https://lore.kernel.org/r/20260626-pwm-loongson-fix-v2-0-5492db953879@gmail.com

Changes in v2:
- Drop the -ERANGE return change and restore the original behavior.
- Use mul_u64_u64_div_u64_roundup() to calculate the low pulse width.
- Update the commit message accordingly.
- Link to v1: https://lore.kernel.org/r/20260616-pwm-loongson-fix-v1-0-491dbf260a7f@gmail.com

---
Keguang Zhang (3):
      pwm: loongson: Fix low pulse buffer register handling
      pwm: loongson: Reset the counters before enabling PWM
      pwm: loongson: Use field helpers for period validation

 drivers/pwm/pwm-loongson.c | 44 ++++++++++++++++++++++++++++++--------------
 1 file changed, 30 insertions(+), 14 deletions(-)
---
base-commit: 17dbb6938d3ab444cd6be1c95506450595a8800d
change-id: 20260612-pwm-loongson-fix-183763451e93

Best regards,
-- 
Keguang Zhang <keguang.zhang@gmail.com>



^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v4 1/3] pwm: loongson: Fix low pulse buffer register handling
  2026-09-20 12:02 [PATCH v4 0/3] pwm: loongson: Fix PWM configuration handling Keguang Zhang via B4 Relay
@ 2026-09-20 12:02 ` Keguang Zhang via B4 Relay
  2026-11-30 15:55   ` Uwe Kleine-König
  2026-09-20 12:02 ` [PATCH v4 2/3] pwm: loongson: Reset the counters before enabling PWM Keguang Zhang via B4 Relay
  1 sibling, 1 reply; 5+ messages in thread
From: Keguang Zhang via B4 Relay @ 2026-09-20 12:02 UTC (permalink / raw)
  To: Binbin Zhou, Uwe Kleine-König
  Cc: linux-pwm, linux-kernel, Keguang Zhang, stable

From: Keguang Zhang <keguang.zhang@gmail.com>

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@vger.kernel.org
Signed-off-by: Keguang Zhang <keguang.zhang@gmail.com>
---
 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;
+
+	pwm_loongson_writel(ddata, low, LOONGSON_PWM_REG_LOW);
 	pwm_loongson_writel(ddata, period, LOONGSON_PWM_REG_PERIOD);
 
 	return 0;
@@ -168,15 +168,20 @@ static int pwm_loongson_apply(struct pwm_chip *chip, struct pwm_device *pwm,
 static int pwm_loongson_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
 				  struct pwm_state *state)
 {
-	u32 duty, period, ctrl;
+	u32 low, period, ctrl;
 	struct pwm_loongson_ddata *ddata = to_pwm_loongson_ddata(chip);
 
-	duty = pwm_loongson_readl(ddata, LOONGSON_PWM_REG_DUTY);
+	low = pwm_loongson_readl(ddata, LOONGSON_PWM_REG_LOW);
 	period = pwm_loongson_readl(ddata, LOONGSON_PWM_REG_PERIOD);
 	ctrl = pwm_loongson_readl(ddata, LOONGSON_PWM_REG_CTRL);
 
-	/* duty & period have a max of 2^32, so we can't overflow */
-	state->duty_cycle = DIV64_U64_ROUND_UP((u64)duty * NSEC_PER_SEC, ddata->clk_rate);
+	/* low & period have a max of 2^32 - 1, so we can't overflow */
+	if (low >= period)
+		state->duty_cycle = 0;
+	else
+		state->duty_cycle =
+		    DIV64_U64_ROUND_UP((u64)(period - low) * NSEC_PER_SEC, ddata->clk_rate);
+
 	state->period = DIV64_U64_ROUND_UP((u64)period * NSEC_PER_SEC, ddata->clk_rate);
 	state->polarity = (ctrl & LOONGSON_PWM_CTRL_REG_INVERT) ? PWM_POLARITY_INVERSED :
 			  PWM_POLARITY_NORMAL;

-- 
2.43.0



^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v4 2/3] pwm: loongson: Reset the counters before enabling PWM
  2026-09-20 12:02 [PATCH v4 0/3] pwm: loongson: Fix PWM configuration handling Keguang Zhang via B4 Relay
  2026-09-20 12:02 ` [PATCH v4 1/3] pwm: loongson: Fix low pulse buffer register handling Keguang Zhang via B4 Relay
@ 2026-09-20 12:02 ` Keguang Zhang via B4 Relay
  2026-11-30 16:10   ` Uwe Kleine-König
  1 sibling, 1 reply; 5+ messages in thread
From: Keguang Zhang via B4 Relay @ 2026-09-20 12:02 UTC (permalink / raw)
  To: Binbin Zhou, Uwe Kleine-König
  Cc: linux-pwm, linux-kernel, Keguang Zhang, stable

From: Keguang Zhang <keguang.zhang@gmail.com>

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@vger.kernel.org
Suggested-by: Uwe Kleine-König <ukleinek@kernel.org>
Signed-off-by: Keguang Zhang <keguang.zhang@gmail.com>
---
 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);
+
 	val &= ~LOONGSON_PWM_CTRL_REG_RST;
 	val |= LOONGSON_PWM_CTRL_REG_EN;
 	pwm_loongson_writel(ddata, val, LOONGSON_PWM_REG_CTRL);

-- 
2.43.0



^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v4 1/3] pwm: loongson: Fix low pulse buffer register handling
  2026-09-20 12:02 ` [PATCH v4 1/3] pwm: loongson: Fix low pulse buffer register handling Keguang Zhang via B4 Relay
@ 2026-11-30 15:55   ` Uwe Kleine-König
  0 siblings, 0 replies; 5+ messages in thread
From: Uwe Kleine-König @ 2026-11-30 15:55 UTC (permalink / raw)
  To: keguang.zhang; +Cc: Binbin Zhou, linux-pwm, linux-kernel, stable

[-- Attachment #1: Type: text/plain, Size: 3210 bytes --]

Hello,

On Sun, Sep 20, 2026 at 08:02:06PM +0800, Keguang Zhang via B4 Relay wrote:
> From: Keguang Zhang <keguang.zhang@gmail.com>
> 
> 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@vger.kernel.org
> Signed-off-by: Keguang Zhang <keguang.zhang@gmail.com>
> ---
>  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 #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v4 2/3] pwm: loongson: Reset the counters before enabling PWM
  2026-09-20 12:02 ` [PATCH v4 2/3] pwm: loongson: Reset the counters before enabling PWM Keguang Zhang via B4 Relay
@ 2026-11-30 16:10   ` Uwe Kleine-König
  0 siblings, 0 replies; 5+ messages in thread
From: Uwe Kleine-König @ 2026-11-30 16:10 UTC (permalink / raw)
  To: keguang.zhang; +Cc: Binbin Zhou, linux-pwm, linux-kernel, stable

[-- Attachment #1: Type: text/plain, Size: 2127 bytes --]

On Sun, Sep 20, 2026 at 08:02:07PM +0800, Keguang Zhang via B4 Relay wrote:
> From: Keguang Zhang <keguang.zhang@gmail.com>
> 
> 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@vger.kernel.org
> Suggested-by: Uwe Kleine-König <ukleinek@kernel.org>
> Signed-off-by: Keguang Zhang <keguang.zhang@gmail.com>
> ---
>  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 #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-11-30 16:10 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-09-20 12:02 [PATCH v4 0/3] pwm: loongson: Fix PWM configuration handling Keguang Zhang via B4 Relay
2026-09-20 12:02 ` [PATCH v4 1/3] pwm: loongson: Fix low pulse buffer register handling Keguang Zhang via B4 Relay
2026-11-30 15:55   ` Uwe Kleine-König
2026-09-20 12:02 ` [PATCH v4 2/3] pwm: loongson: Reset the counters before enabling PWM Keguang Zhang via B4 Relay
2026-11-30 16:10   ` Uwe Kleine-König

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox