Skip to contentSkip to conversation

pwm: pxa: Fixes for enable/disable transitions

PATCH In review Doug Brown 2 participants 10 messages Nov 13, 2022 – Nov 28, 2022 PWM

lore
Share & tools
lore

Doug Brown

author
#0 lore raw

v3 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.

Changes in v3:

- Switch to abrupt shutdown mode instead of the delay hack from v2.
- Fix commit message explaining why duty cycle is set to 0.
- Add documentation link and limitations section at top of file.

Changes in v2:

- Remove pxa_pwm_enable and pxa_pwm_disable.
- Reorganize pxa_pwm_apply based on Uwe's suggestions.
- Enable this driver with ARCH_MMP for the PXA168.
- Add delay after clock is disabled (and inhibit brief on/off blips).

Doug Brown (6):
pwm: pxa: Remove pxa_pwm_enable/disable
pwm: pxa: Set duty cycle to 0 when disabling PWM
pwm: pxa: Remove clk enable/disable from pxa_pwm_config
pwm: pxa: Use abrupt shutdown mode
pwm: pxa: Add reference manual link and limitations
pwm: pxa: Enable for MMP platform

 drivers/pwm/Kconfig   |  2 +-
 drivers/pwm/pwm-pxa.c | 58 +++++++++++++++++++------------------------
 2 files changed, 26 insertions(+), 34 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v3 1/6 pwm: pxa: Remove pxa_pwm_enable/disable

These functions are only acting as wrappers for clk_prepare_enable and clk_disable_unprepare now, so remove them to simplify the driver.

---
 drivers/pwm/pwm-pxa.c | 19 +++----------------
 1 file changed, 3 insertions(+), 16 deletions(-)
drivers/pwm/pwm-pxa.c +3 -16
index 0bcaa58c6a91..0ac052652c62 100644
@@ -101,23 +101,10 @@ static int pxa_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
 	return 0;
 }
 
-static int pxa_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
-{
-	struct pxa_pwm_chip *pc = to_pxa_pwm_chip(chip);
-
-	return clk_prepare_enable(pc->clk);
-}
-
-static void pxa_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
-{
-	struct pxa_pwm_chip *pc = to_pxa_pwm_chip(chip);
-
-	clk_disable_unprepare(pc->clk);
-}
-
 static int pxa_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
 			 const struct pwm_state *state)
 {
+	struct pxa_pwm_chip *pc = to_pxa_pwm_chip(chip);
 	int err;
 
 	if (state->polarity != PWM_POLARITY_NORMAL)
@@ -125,7 +112,7 @@ static int pxa_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
 
 	if (!state->enabled) {
 		if (pwm->state.enabled)
-			pxa_pwm_disable(chip, pwm);
+			clk_disable_unprepare(pc->clk);
 
 		return 0;
 	}
@@ -135,7 +122,7 @@ static int pxa_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
 		return err;
 
 	if (!pwm->state.enabled)
-		return pxa_pwm_enable(chip, pwm);
+		return clk_prepare_enable(pc->clk);
 
 	return 0;
 }
Signature · 1 line
2.34.1
author
#2 lore raw

PATCH v3 2/6 pwm: pxa: Set duty cycle to 0 when disabling PWM

In reply to Doug Brown

When disabling PWM, the duty cycle needs to be set to 0. This prevents the previous duty cycle from showing up momentarily when the clock is re-enabled next time.

Because the clock has to be running in order to configure the duty cycle, unconditionally enable it early in pxa_pwm_apply and account for the correct enable count at the end.

---
 drivers/pwm/pwm-pxa.c | 25 ++++++++++++++++---------
 1 file changed, 16 insertions(+), 9 deletions(-)
drivers/pwm/pwm-pxa.c +16 -9
index 0ac052652c62..9ee9b41d62b8 100644
@@ -105,24 +105,31 @@ static int pxa_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
 			 const struct pwm_state *state)
 {
 	struct pxa_pwm_chip *pc = to_pxa_pwm_chip(chip);
+	u64 duty_cycle;
 	int err;
 
 	if (state->polarity != PWM_POLARITY_NORMAL)
 		return -EINVAL;
 
-	if (!state->enabled) {
-		if (pwm->state.enabled)
-			clk_disable_unprepare(pc->clk);
+	err = clk_prepare_enable(pc->clk);
+	if (err)
+		return err;
 
-		return 0;
-	}
+	duty_cycle = state->enabled ? state->duty_cycle : 0;
 
-	err = pxa_pwm_config(chip, pwm, state->duty_cycle, state->period);
-	if (err)
+	err = pxa_pwm_config(chip, pwm, duty_cycle, state->period);
+	if (err) {
+		clk_disable_unprepare(pc->clk);
 		return err;
+	}
+
+	if (state->enabled && !pwm->state.enabled)
+		return 0;
+
+	clk_disable_unprepare(pc->clk);
 
-	if (!pwm->state.enabled)
-		return clk_prepare_enable(pc->clk);
+	if (!state->enabled && pwm->state.enabled)
+		clk_disable_unprepare(pc->clk);
 
 	return 0;
 }
Signature · 1 line
2.34.1
Reviewed-by on v3 2/6
#3 lore raw

Re: [PATCH v3 2/6] pwm: pxa: Set duty cycle to 0 when disabling PWM

Hello,

Quoted 49 lines · Doug Brown

When disabling PWM, the duty cycle needs to be set to 0. This prevents the previous duty cycle from showing up momentarily when the clock is re-enabled next time.

Because the clock has to be running in order to configure the duty cycle, unconditionally enable it early in pxa_pwm_apply and account for the correct enable count at the end.

---
 drivers/pwm/pwm-pxa.c | 25 ++++++++++++++++---------
 1 file changed, 16 insertions(+), 9 deletions(-)
drivers/pwm/pwm-pxa.c +12 -7
index 0ac052652c62..9ee9b41d62b8 100644
@@ -105,24 +105,31 @@ static int pxa_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
 			 const struct pwm_state *state)
 {
 	struct pxa_pwm_chip *pc = to_pxa_pwm_chip(chip);
+	u64 duty_cycle;
 	int err;
 
 	if (state->polarity != PWM_POLARITY_NORMAL)
 		return -EINVAL;
 
-	if (!state->enabled) {
-		if (pwm->state.enabled)
-			clk_disable_unprepare(pc->clk);
+	err = clk_prepare_enable(pc->clk);
+	if (err)
+		return err;
 
-		return 0;
-	}
+	duty_cycle = state->enabled ? state->duty_cycle : 0;
 
-	err = pxa_pwm_config(chip, pwm, state->duty_cycle, state->period);
-	if (err)
+	err = pxa_pwm_config(chip, pwm, duty_cycle, state->period);
+	if (err) {
+		clk_disable_unprepare(pc->clk);
 		return err;
+	}
+
+	if (state->enabled && !pwm->state.enabled)
+		return 0;

drivers/pwm/pwm-pxa.c static int pxa_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, · line 135

Quoted 10 lines
drivers/pwm/pwm-pxa.c +4 -2
+
+	clk_disable_unprepare(pc->clk);
 
-	if (!pwm->state.enabled)
-		return clk_prepare_enable(pc->clk);
+	if (!state->enabled && pwm->state.enabled)
+		clk_disable_unprepare(pc->clk);
 
 	return 0;
 }

This has the side effect that when going from

.duty_cycle = A, .period = B, .enabled = false

to

.duty_cycle = C, .period = D, .enabled = false

the register values for C and D are calculated and written into the hardware without any win, but the net gain is still positive I think. So:

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |
author
#4 lore raw

PATCH v3 3/6 pwm: pxa: Remove clk enable/disable from pxa_pwm_config

In reply to Doug Brown

Now that pxa_pwm_apply always enables the clock first, there is no need for pxa_pwm_config to do any clock enabling/disabling.

---
 drivers/pwm/pwm-pxa.c | 9 ---------
 1 file changed, 9 deletions(-)
drivers/pwm/pwm-pxa.c +0 -9
index 9ee9b41d62b8..cf4d22c91929 100644
@@ -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,10 @@ 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;
-
 	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;
 }
 
Signature · 1 line
2.34.1
author
#5 lore raw

PATCH v3 4/6 pwm: pxa: Use abrupt shutdown mode

In reply to Doug Brown

Switch to abrupt shutdown mode in order to stop the clock as soon as possible when PWM is disabled. This minimizes the possibility of the clock being re-enabled while it is still in the process of turning off, which will result in the clock ending up erroneously disabled.

---
 drivers/pwm/pwm-pxa.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pwm/pwm-pxa.c +1 -1
index cf4d22c91929..00063ead7c0a 100644
@@ -85,7 +85,7 @@ 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);
 
-	writel(prescale, pc->mmio_base + offset + PWMCR);
+	writel(prescale | PWMCR_SD, pc->mmio_base + offset + PWMCR);
 	writel(dc, pc->mmio_base + offset + PWMDCR);
 	writel(pv, pc->mmio_base + offset + PWMPCR);
 
Signature · 1 line
2.34.1
Reviewed-by on v3 4/6
#6 lore raw

Re: [PATCH v3 4/6] pwm: pxa: Use abrupt shutdown mode

Quoted 6 lines · Doug Brown

Switch to abrupt shutdown mode in order to stop the clock as soon as possible when PWM is disabled. This minimizes the possibility of the clock being re-enabled while it is still in the process of turning off, which will result in the clock ending up erroneously disabled.

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |
author
#7 lore raw

PATCH v3 5/6 pwm: pxa: Add reference manual link and limitations

In reply to Doug Brown

Add a pointer to the location of reference manuals for some of the supported chips, and add a limitations section explaining the hardware's PWM disable behavior.

---
 drivers/pwm/pwm-pxa.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/pwm/pwm-pxa.c +7 -0
index 00063ead7c0a..46ed668bd141 100644
@@ -6,6 +6,13 @@
  *
  * 2008-02-13	initial version
  *		eric miao <[email protected]>
+ *
+ * Links to reference manuals for some of the supported PWM chips can be found
+ * in Documentation/arm/marvell.rst.
+ *
+ * Limitations:
+ * - When PWM is stopped, the current PWM period stops abruptly at the next
+ *   input clock (PWMCR_SD is set) and the output is driven to inactive.
  */
 
 #include <linux/module.h>
Signature · 1 line
2.34.1
Reviewed-by on v3 5/6
#8 lore raw

Re: [PATCH v3 5/6] pwm: pxa: Add reference manual link and limitations

Quoted 6 lines · Doug Brown

Add a pointer to the location of reference manuals for some of the supported chips, and add a limitations section explaining the hardware's PWM disable behavior.

Thanks
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |
author
#9 lore raw

PATCH v3 6/6 pwm: pxa: Enable for MMP platform

In reply to Doug Brown

The PXA168, which is part of the MMP platform, also uses this driver.

---
 drivers/pwm/Kconfig | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pwm/Kconfig +1 -1
index 60d13a949bc5..d0d4caebf12f 100644
@@ -434,7 +434,7 @@ config PWM_PCA9685
 
 config PWM_PXA
 	tristate "PXA PWM support"
-	depends on ARCH_PXA || COMPILE_TEST
+	depends on ARCH_PXA || ARCH_MMP || COMPILE_TEST
 	depends on HAS_IOMEM
 	help
 	  Generic PWM framework driver for PXA.
Signature · 1 line
2.34.1