linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/2] pwm: pca9685: fix duty cycle loss on disable/enable
@ 2020-11-12 16:37 Clemens Gruber
  2020-11-12 16:37 ` [PATCH 2/2] pwm: pca9685: support staggered output ON times Clemens Gruber
  2020-11-15 17:46 ` [PATCH 1/2] pwm: pca9685: fix duty cycle loss on disable/enable Clemens Gruber
  0 siblings, 2 replies; 3+ messages in thread
From: Clemens Gruber @ 2020-11-12 16:37 UTC (permalink / raw)
  To: linux-pwm
  Cc: Thierry Reding, u.kleine-koenig, Lee Jones, linux-kernel, Clemens Gruber

Currently, disabling a pwm channel clears the OFF registers, which
resets the configured duty cycle. Therefore, after a disable/enable
sequence, the pwm state is not in the same state as it was before.
As a workaround, the user had to do a config call after disable/enable.

Fix it by only toggling the "full-off" bit in disable/enable and no
longer use the "full-on" / "full-off" bits in config.

From now on, the "full-on" bit is only used in GPIO mode and the
"full-off" bit is only used in disable/enable, which also reduces
complexity in the driver.

Signed-off-by: Clemens Gruber <clemens.gruber@pqgruber.com>
---
 drivers/pwm/pwm-pca9685.c | 83 +++++----------------------------------
 1 file changed, 9 insertions(+), 74 deletions(-)

diff --git a/drivers/pwm/pwm-pca9685.c b/drivers/pwm/pwm-pca9685.c
index 4a55dc18656c..df214758256e 100644
--- a/drivers/pwm/pwm-pca9685.c
+++ b/drivers/pwm/pwm-pca9685.c
@@ -283,45 +283,7 @@ static int pca9685_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
 		}
 	}
 
-	if (duty_ns < 1) {
-		if (pwm->hwpwm >= PCA9685_MAXCHAN)
-			reg = PCA9685_ALL_LED_OFF_H;
-		else
-			reg = LED_N_OFF_H(pwm->hwpwm);
-
-		regmap_write(pca->regmap, reg, LED_FULL);
-
-		return 0;
-	}
-
-	if (duty_ns == period_ns) {
-		/* Clear both OFF registers */
-		if (pwm->hwpwm >= PCA9685_MAXCHAN)
-			reg = PCA9685_ALL_LED_OFF_L;
-		else
-			reg = LED_N_OFF_L(pwm->hwpwm);
-
-		regmap_write(pca->regmap, reg, 0x0);
-
-		if (pwm->hwpwm >= PCA9685_MAXCHAN)
-			reg = PCA9685_ALL_LED_OFF_H;
-		else
-			reg = LED_N_OFF_H(pwm->hwpwm);
-
-		regmap_write(pca->regmap, reg, 0x0);
-
-		/* Set the full ON bit */
-		if (pwm->hwpwm >= PCA9685_MAXCHAN)
-			reg = PCA9685_ALL_LED_ON_H;
-		else
-			reg = LED_N_ON_H(pwm->hwpwm);
-
-		regmap_write(pca->regmap, reg, LED_FULL);
-
-		return 0;
-	}
-
-	duty = PCA9685_COUNTER_RANGE * (unsigned long long)duty_ns;
+	duty = (PCA9685_COUNTER_RANGE - 1) * (unsigned long long)duty_ns;
 	duty = DIV_ROUND_UP_ULL(duty, period_ns);
 
 	if (pwm->hwpwm >= PCA9685_MAXCHAN)
@@ -338,14 +300,6 @@ static int pca9685_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
 
 	regmap_write(pca->regmap, reg, ((int)duty >> 8) & 0xf);
 
-	/* Clear the full ON bit, otherwise the set OFF time has no effect */
-	if (pwm->hwpwm >= PCA9685_MAXCHAN)
-		reg = PCA9685_ALL_LED_ON_H;
-	else
-		reg = LED_N_ON_H(pwm->hwpwm);
-
-	regmap_write(pca->regmap, reg, 0);
-
 	return 0;
 }
 
@@ -354,24 +308,6 @@ static int pca9685_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
 	struct pca9685 *pca = to_pca(chip);
 	unsigned int reg;
 
-	/*
-	 * The PWM subsystem does not support a pre-delay.
-	 * So, set the ON-timeout to 0
-	 */
-	if (pwm->hwpwm >= PCA9685_MAXCHAN)
-		reg = PCA9685_ALL_LED_ON_L;
-	else
-		reg = LED_N_ON_L(pwm->hwpwm);
-
-	regmap_write(pca->regmap, reg, 0);
-
-	if (pwm->hwpwm >= PCA9685_MAXCHAN)
-		reg = PCA9685_ALL_LED_ON_H;
-	else
-		reg = LED_N_ON_H(pwm->hwpwm);
-
-	regmap_write(pca->regmap, reg, 0);
-
 	/*
 	 * Clear the full-off bit.
 	 * It has precedence over the others and must be off.
@@ -391,20 +327,16 @@ static void pca9685_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
 	struct pca9685 *pca = to_pca(chip);
 	unsigned int reg;
 
+	/*
+	 * Set the full-off bit.
+	 * It has precedence over the others.
+	 */
 	if (pwm->hwpwm >= PCA9685_MAXCHAN)
 		reg = PCA9685_ALL_LED_OFF_H;
 	else
 		reg = LED_N_OFF_H(pwm->hwpwm);
 
-	regmap_write(pca->regmap, reg, LED_FULL);
-
-	/* Clear the LED_OFF counter. */
-	if (pwm->hwpwm >= PCA9685_MAXCHAN)
-		reg = PCA9685_ALL_LED_OFF_L;
-	else
-		reg = LED_N_OFF_L(pwm->hwpwm);
-
-	regmap_write(pca->regmap, reg, 0x0);
+	regmap_update_bits(pca->regmap, reg, LED_FULL, LED_FULL);
 }
 
 static int pca9685_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
@@ -487,6 +419,9 @@ static int pca9685_pwm_probe(struct i2c_client *client,
 	/* Clear all "full off" bits */
 	regmap_write(pca->regmap, PCA9685_ALL_LED_OFF_L, 0);
 	regmap_write(pca->regmap, PCA9685_ALL_LED_OFF_H, 0);
+	/* Clear all "on" regs */
+	regmap_write(pca->regmap, PCA9685_ALL_LED_ON_L, 0);
+	regmap_write(pca->regmap, PCA9685_ALL_LED_ON_H, 0);
 
 	pca->chip.ops = &pca9685_pwm_ops;
 	/* Add an extra channel for ALL_LED */
-- 
2.29.2


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

* [PATCH 2/2] pwm: pca9685: support staggered output ON times
  2020-11-12 16:37 [PATCH 1/2] pwm: pca9685: fix duty cycle loss on disable/enable Clemens Gruber
@ 2020-11-12 16:37 ` Clemens Gruber
  2020-11-15 17:46 ` [PATCH 1/2] pwm: pca9685: fix duty cycle loss on disable/enable Clemens Gruber
  1 sibling, 0 replies; 3+ messages in thread
From: Clemens Gruber @ 2020-11-12 16:37 UTC (permalink / raw)
  To: linux-pwm
  Cc: Thierry Reding, u.kleine-koenig, Lee Jones, linux-kernel, Clemens Gruber

The PCA9685 supports staggered LED output ON times to minimize current
surges and reduce EMI.
When this new option is enabled, the ON times of each channel are
delayed by channel number x counter range / 16, which avoids asserting
all enabled outputs at the same counter value while still maintaining
the configured duty cycle of each output.

Signed-off-by: Clemens Gruber <clemens.gruber@pqgruber.com>
---
 drivers/pwm/pwm-pca9685.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)

diff --git a/drivers/pwm/pwm-pca9685.c b/drivers/pwm/pwm-pca9685.c
index df214758256e..b993231ebb5e 100644
--- a/drivers/pwm/pwm-pca9685.c
+++ b/drivers/pwm/pwm-pca9685.c
@@ -75,6 +75,7 @@ struct pca9685 {
 	struct pwm_chip chip;
 	struct regmap *regmap;
 	int period_ns;
+	bool staggered_outputs;
 #if IS_ENABLED(CONFIG_GPIOLIB)
 	struct mutex lock;
 	struct gpio_chip gpio;
@@ -251,6 +252,7 @@ static int pca9685_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
 {
 	struct pca9685 *pca = to_pca(chip);
 	unsigned long long duty;
+	unsigned int on, off;
 	unsigned int reg;
 	int prescale;
 
@@ -286,6 +288,32 @@ static int pca9685_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
 	duty = (PCA9685_COUNTER_RANGE - 1) * (unsigned long long)duty_ns;
 	duty = DIV_ROUND_UP_ULL(duty, period_ns);
 
+	if (pca->staggered_outputs && pwm->hwpwm < PCA9685_MAXCHAN) {
+		if (duty_ns > 0) {
+			/*
+			 * To reduce EMI, the ON times of each channel are
+			 * spread out evenly within the counter range, while
+			 * still maintaining the configured duty cycle
+			 */
+			on = pwm->hwpwm * PCA9685_COUNTER_RANGE /
+				PCA9685_MAXCHAN;
+			off = (on + duty) % PCA9685_COUNTER_RANGE;
+			regmap_write(pca->regmap, LED_N_ON_L(pwm->hwpwm),
+				     on & 0xff);
+			regmap_write(pca->regmap, LED_N_ON_H(pwm->hwpwm),
+				     (on >> 8) & 0xf);
+			regmap_write(pca->regmap, LED_N_OFF_L(pwm->hwpwm),
+				     off & 0xff);
+			regmap_write(pca->regmap, LED_N_OFF_H(pwm->hwpwm),
+				     (off >> 8) & 0xf);
+			return 0;
+		}
+
+		/* Clear ON times */
+		regmap_write(pca->regmap, LED_N_ON_L(pwm->hwpwm), 0);
+		regmap_write(pca->regmap, LED_N_ON_H(pwm->hwpwm), 0);
+	}
+
 	if (pwm->hwpwm >= PCA9685_MAXCHAN)
 		reg = PCA9685_ALL_LED_OFF_L;
 	else
@@ -416,6 +444,9 @@ static int pca9685_pwm_probe(struct i2c_client *client,
 	reg &= ~(MODE1_ALLCALL | MODE1_SUB1 | MODE1_SUB2 | MODE1_SUB3);
 	regmap_write(pca->regmap, PCA9685_MODE1, reg);
 
+	pca->staggered_outputs = device_property_read_bool(
+		&client->dev, "staggered-outputs");
+
 	/* Clear all "full off" bits */
 	regmap_write(pca->regmap, PCA9685_ALL_LED_OFF_L, 0);
 	regmap_write(pca->regmap, PCA9685_ALL_LED_OFF_H, 0);
-- 
2.29.2


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

* Re: [PATCH 1/2] pwm: pca9685: fix duty cycle loss on disable/enable
  2020-11-12 16:37 [PATCH 1/2] pwm: pca9685: fix duty cycle loss on disable/enable Clemens Gruber
  2020-11-12 16:37 ` [PATCH 2/2] pwm: pca9685: support staggered output ON times Clemens Gruber
@ 2020-11-15 17:46 ` Clemens Gruber
  1 sibling, 0 replies; 3+ messages in thread
From: Clemens Gruber @ 2020-11-15 17:46 UTC (permalink / raw)
  To: linux-pwm
  Cc: Thierry Reding, u.kleine-koenig, Lee Jones, linux-kernel, Rob Herring

On Thu, Nov 12, 2020 at 05:37:50PM +0100, Clemens Gruber wrote:
> Currently, disabling a pwm channel clears the OFF registers, which
> resets the configured duty cycle. Therefore, after a disable/enable
> sequence, the pwm state is not in the same state as it was before.
> As a workaround, the user had to do a config call after disable/enable.
> 
> Fix it by only toggling the "full-off" bit in disable/enable and no
> longer use the "full-on" / "full-off" bits in config.
> 
> From now on, the "full-on" bit is only used in GPIO mode and the
> "full-off" bit is only used in disable/enable, which also reduces
> complexity in the driver.
> 
> Signed-off-by: Clemens Gruber <clemens.gruber@pqgruber.com>
> ---
>  drivers/pwm/pwm-pca9685.c | 83 +++++----------------------------------
>  1 file changed, 9 insertions(+), 74 deletions(-)
> 
> diff --git a/drivers/pwm/pwm-pca9685.c b/drivers/pwm/pwm-pca9685.c
> index 4a55dc18656c..df214758256e 100644
> --- a/drivers/pwm/pwm-pca9685.c
> +++ b/drivers/pwm/pwm-pca9685.c
> @@ -283,45 +283,7 @@ static int pca9685_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
>  		}
>  	}
>  
> -	if (duty_ns < 1) {
> -		if (pwm->hwpwm >= PCA9685_MAXCHAN)
> -			reg = PCA9685_ALL_LED_OFF_H;
> -		else
> -			reg = LED_N_OFF_H(pwm->hwpwm);
> -
> -		regmap_write(pca->regmap, reg, LED_FULL);
> -
> -		return 0;
> -	}
> -
> -	if (duty_ns == period_ns) {
> -		/* Clear both OFF registers */
> -		if (pwm->hwpwm >= PCA9685_MAXCHAN)
> -			reg = PCA9685_ALL_LED_OFF_L;
> -		else
> -			reg = LED_N_OFF_L(pwm->hwpwm);
> -
> -		regmap_write(pca->regmap, reg, 0x0);
> -
> -		if (pwm->hwpwm >= PCA9685_MAXCHAN)
> -			reg = PCA9685_ALL_LED_OFF_H;
> -		else
> -			reg = LED_N_OFF_H(pwm->hwpwm);
> -
> -		regmap_write(pca->regmap, reg, 0x0);
> -
> -		/* Set the full ON bit */
> -		if (pwm->hwpwm >= PCA9685_MAXCHAN)
> -			reg = PCA9685_ALL_LED_ON_H;
> -		else
> -			reg = LED_N_ON_H(pwm->hwpwm);
> -
> -		regmap_write(pca->regmap, reg, LED_FULL);
> -
> -		return 0;
> -	}
> -
> -	duty = PCA9685_COUNTER_RANGE * (unsigned long long)duty_ns;
> +	duty = (PCA9685_COUNTER_RANGE - 1) * (unsigned long long)duty_ns;
>  	duty = DIV_ROUND_UP_ULL(duty, period_ns);
>  
>  	if (pwm->hwpwm >= PCA9685_MAXCHAN)
> @@ -338,14 +300,6 @@ static int pca9685_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
>  
>  	regmap_write(pca->regmap, reg, ((int)duty >> 8) & 0xf);
>  
> -	/* Clear the full ON bit, otherwise the set OFF time has no effect */
> -	if (pwm->hwpwm >= PCA9685_MAXCHAN)
> -		reg = PCA9685_ALL_LED_ON_H;
> -	else
> -		reg = LED_N_ON_H(pwm->hwpwm);
> -
> -	regmap_write(pca->regmap, reg, 0);
> -
>  	return 0;
>  }
>  
> @@ -354,24 +308,6 @@ static int pca9685_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
>  	struct pca9685 *pca = to_pca(chip);
>  	unsigned int reg;
>  
> -	/*
> -	 * The PWM subsystem does not support a pre-delay.
> -	 * So, set the ON-timeout to 0
> -	 */
> -	if (pwm->hwpwm >= PCA9685_MAXCHAN)
> -		reg = PCA9685_ALL_LED_ON_L;
> -	else
> -		reg = LED_N_ON_L(pwm->hwpwm);
> -
> -	regmap_write(pca->regmap, reg, 0);
> -
> -	if (pwm->hwpwm >= PCA9685_MAXCHAN)
> -		reg = PCA9685_ALL_LED_ON_H;
> -	else
> -		reg = LED_N_ON_H(pwm->hwpwm);
> -
> -	regmap_write(pca->regmap, reg, 0);
> -
>  	/*
>  	 * Clear the full-off bit.
>  	 * It has precedence over the others and must be off.
> @@ -391,20 +327,16 @@ static void pca9685_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
>  	struct pca9685 *pca = to_pca(chip);
>  	unsigned int reg;
>  
> +	/*
> +	 * Set the full-off bit.
> +	 * It has precedence over the others.
> +	 */
>  	if (pwm->hwpwm >= PCA9685_MAXCHAN)
>  		reg = PCA9685_ALL_LED_OFF_H;
>  	else
>  		reg = LED_N_OFF_H(pwm->hwpwm);
>  
> -	regmap_write(pca->regmap, reg, LED_FULL);
> -
> -	/* Clear the LED_OFF counter. */
> -	if (pwm->hwpwm >= PCA9685_MAXCHAN)
> -		reg = PCA9685_ALL_LED_OFF_L;
> -	else
> -		reg = LED_N_OFF_L(pwm->hwpwm);
> -
> -	regmap_write(pca->regmap, reg, 0x0);
> +	regmap_update_bits(pca->regmap, reg, LED_FULL, LED_FULL);
>  }
>  
>  static int pca9685_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
> @@ -487,6 +419,9 @@ static int pca9685_pwm_probe(struct i2c_client *client,
>  	/* Clear all "full off" bits */
>  	regmap_write(pca->regmap, PCA9685_ALL_LED_OFF_L, 0);
>  	regmap_write(pca->regmap, PCA9685_ALL_LED_OFF_H, 0);
> +	/* Clear all "on" regs */
> +	regmap_write(pca->regmap, PCA9685_ALL_LED_ON_L, 0);
> +	regmap_write(pca->regmap, PCA9685_ALL_LED_ON_H, 0);
>  
>  	pca->chip.ops = &pca9685_pwm_ops;
>  	/* Add an extra channel for ALL_LED */
> -- 
> 2.29.2
> 

Please ignore the whole series (we found a flaw!).

Will send a v2 in the upcoming week.

Best regards,
Clemens

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

end of thread, other threads:[~2020-11-15 17:46 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-12 16:37 [PATCH 1/2] pwm: pca9685: fix duty cycle loss on disable/enable Clemens Gruber
2020-11-12 16:37 ` [PATCH 2/2] pwm: pca9685: support staggered output ON times Clemens Gruber
2020-11-15 17:46 ` [PATCH 1/2] pwm: pca9685: fix duty cycle loss on disable/enable Clemens Gruber

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).