diff mbox

[v2] PWM: atmel-pwm: use request/free instead of enable/disable

Message ID 1380554553-25676-1-git-send-email-alexandre.belloni@free-electrons.com (mailing list archive)
State New, archived
Headers show

Commit Message

Alexandre Belloni Sept. 30, 2013, 3:22 p.m. UTC
I found that disabling a pwm while it is at a low level will actually put it
back at a high level. The main symptom is that leds-pwm is calling pwm_disable()
after setting the duty cycle to 0. Hence, instead of getting a switched off LED,
you get an LED lit up at full brightness.

Solve that by using the request and free callbacks to enable and disable the
pwm channels and the clock.

Signed-off-by: Alexandre Belloni <alexandre.belloni@free-electrons.com>
---
Applies on [PATCH v5] PWM: atmel-pwm: add PWM controller driver

Changes in v2:
  - add dummies pwm_enable and pwm_disable to be able to register the pwm chip.

 drivers/pwm/pwm-atmel.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)

Comments

Thierry Reding Oct. 8, 2013, 12:05 p.m. UTC | #1
On Mon, Sep 30, 2013 at 05:22:33PM +0200, Alexandre Belloni wrote:
> I found that disabling a pwm while it is at a low level will actually put it
> back at a high level. The main symptom is that leds-pwm is calling pwm_disable()
> after setting the duty cycle to 0. Hence, instead of getting a switched off LED,
> you get an LED lit up at full brightness.

I wonder why that's the case. What's causing the PWM to go back to full
duty cycle when disabled? There was a similar issue with some other PWM
driver a while back and the cause was tracked down to be that you had to
wait for a full period to make sure the signal was constantly low before
switching off the clock. Perhaps something similar is the issue here?

> Solve that by using the request and free callbacks to enable and disable the
> pwm channels and the clock.

"pwm" -> "PWM",  please.

> diff --git a/drivers/pwm/pwm-atmel.c b/drivers/pwm/pwm-atmel.c
[...]
> +static int atmel_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
> +{
> +	/*
> +	 * This is a dummy function, required to be able to register the pwm
> +	 * chip, see pwmadd_chip() in pwm/core.c
> +	 */
> +	return 0;
> +}
> +
> +static void atmel_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
> +{
> +	/*
> +	 * This is a dummy function, required to be able to register the pwm
> +	 * chip, see pwmadd_chip() in pwm/core.c
> +	 */
> +}

This just doesn't feel right. Can somebody please investigate what the
real reason is for the behaviour described in the commit message and see
if we can't solve this in some other way? .request() and .free() are
typically called very early or very late, respectively, so this patch
will keep the PWM clock on forever (pretty much).

Thierry
Alexandre Belloni Oct. 9, 2013, 11:14 a.m. UTC | #2
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

On 08/10/2013 14:05, Thierry Reding wrote:
> On Mon, Sep 30, 2013 at 05:22:33PM +0200, Alexandre Belloni wrote:
>> I found that disabling a pwm while it is at a low level will actually
put it
>> back at a high level. The main symptom is that leds-pwm is calling
pwm_disable()
>> after setting the duty cycle to 0. Hence, instead of getting a
switched off LED,
>> you get an LED lit up at full brightness.
>
> I wonder why that's the case. What's causing the PWM to go back to full
> duty cycle when disabled? There was a similar issue with some other PWM
> driver a while back and the cause was tracked down to be that you had to
> wait for a full period to make sure the signal was constantly low before
> switching off the clock. Perhaps something similar is the issue here?

I tried letting enabling/disabling the PWM channel in
pwm_enable/pwm_disable and enabling/disabling the clock in
pwm_request/pwm_free. The issue is still there.

- From the datasheet (also it seems something is fishy there, wrt CPOL):

"Waveforms are fixed at 0 when:
- - CDTY = CPRD and CPOL = 0
- - CDTY = 0 and CPOL = 1
Waveforms are fixed at 1 (once the channel is enabled) when:
- - CDTY = 0 and CPOL = 0
- - CDTY = CPRD and CPOL = 1
The waveform polarity must be set before enabling the channel. This
immediately affects the channel output level."

So, while I agree with you that it makes us keep the pwm clock enabled
forever, it makes me believe that we have to let the PWM enabled to get
that working correctly.


I also tried changing the polarity and setting pull down on the pin but
the level seems to be driven high. One thing that is working though is
setting the override value before disabling the PWM. Then the level that
is set is still kept when the pwm is disabled. Unfortunately, that
feature is only available since the sama5.

>
>> Solve that by using the request and free callbacks to enable and
disable the
>> pwm channels and the clock.
>
> "pwm" -> "PWM",  please.
>
>> diff --git a/drivers/pwm/pwm-atmel.c b/drivers/pwm/pwm-atmel.c
> [...]
>> +static int atmel_pwm_enable(struct pwm_chip *chip, struct pwm_device
*pwm)
>> +{
>> +    /*
>> +     * This is a dummy function, required to be able to register the pwm
>> +     * chip, see pwmadd_chip() in pwm/core.c
>> +     */
>> +    return 0;
>> +}
>> +
>> +static void atmel_pwm_disable(struct pwm_chip *chip, struct
pwm_device *pwm)
>> +{
>> +    /*
>> +     * This is a dummy function, required to be able to register the pwm
>> +     * chip, see pwmadd_chip() in pwm/core.c
>> +     */
>> +}
>
> This just doesn't feel right. Can somebody please investigate what the
> real reason is for the behaviour described in the commit message and see
> if we can't solve this in some other way? .request() and .free() are
> typically called very early or very late, respectively, so this patch
> will keep the PWM clock on forever (pretty much).
>
> Thierry


- -- 
Alexandre Belloni, Free Electrons
Embedded Linux, Kernel and Android engineering
http://free-electrons.com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)

iQIcBAEBCAAGBQJSVTqjAAoJEKbNnwlvZCyzzE4P/igeF8UBUPdEQAYpgG3avTTh
93oEGu8sHoNSbUJY6O51x5f+96QiwrZF62rjy4PO8BtHR6fGgUt94RtyMacut/aY
NfRwdwfe24wYwHNpwOCv9uD1ZtiLyD9wAMeIEsoYy57A9XPPuuTOmJamN7jzsh/K
OvjdmjWcJQPymSTABdus4Zt7ccIgPqMsai8HQYiNqnKbPDUXx20/+QH5Kw2hPxi0
YPK36xEeEW0pB+v9fVU54abAV3Bh4tEOIJ/7wqWeDxN9WBK8LEG20pb6ZNFI3GJO
KzEO7Y4fZzPc85RCPfigyyDnOvzHRP5vc1vfSIOxwPqhZLZL6ceBlCHWhAr+CYSU
mWtNruBP9dhJfRDKtchDv3D63YBDip4DTDh/0jiqT6Qw6LTjSU+bWi1RVlrpa6HY
Wv6siL0gy4USKk8DJEl/pWBi3SlSbPynT+sZLKrWpz2E3oLjgDNALPIZtMM38xYj
X7LzZX1v8t6psksnH13Q4yAgyuiIUcJWXTn7wMPZ70DhJ98JpcsV3o9kZQnGTN+A
sp9/pZTd/7sK5LzZx+aMnMItNLgWy85LRLfHzH2c3vAKETVVPx+Vqgojw0sWMC8E
U3oA71/nYOFUssCs9kzCNvU/n7p16bGxyPzbHH2D+2CowiFdYVwJhX1iT4TFTkQJ
ymqGpRABiFB6bmhf4WRg
=zebD
-----END PGP SIGNATURE-----
diff mbox

Patch

diff --git a/drivers/pwm/pwm-atmel.c b/drivers/pwm/pwm-atmel.c
index b4df36c..732312c 100644
--- a/drivers/pwm/pwm-atmel.c
+++ b/drivers/pwm/pwm-atmel.c
@@ -206,7 +206,7 @@  static int atmel_pwm_set_polarity(struct pwm_chip *chip, struct pwm_device *pwm,
 	return 0;
 }
 
-static int atmel_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
+static int atmel_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
 {
 	struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
 	int ret;
@@ -222,7 +222,7 @@  static int atmel_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
 	return 0;
 }
 
-static void atmel_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
+static void atmel_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
 {
 	struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
 
@@ -231,9 +231,28 @@  static void atmel_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
 	clk_disable(atmel_pwm->clk);
 }
 
+static int atmel_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
+{
+	/*
+	 * This is a dummy function, required to be able to register the pwm
+	 * chip, see pwmadd_chip() in pwm/core.c
+	 */
+	return 0;
+}
+
+static void atmel_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
+{
+	/*
+	 * This is a dummy function, required to be able to register the pwm
+	 * chip, see pwmadd_chip() in pwm/core.c
+	 */
+}
+
 static const struct pwm_ops atmel_pwm_ops = {
 	.config = atmel_pwm_config,
 	.set_polarity = atmel_pwm_set_polarity,
+	.request = atmel_pwm_request,
+	.free = atmel_pwm_free,
 	.enable = atmel_pwm_enable,
 	.disable = atmel_pwm_disable,
 	.owner = THIS_MODULE,