pwm: Set enable state properly on failed call to enable
authorJonathan Richardson <jonathar@broadcom.com>
Sat, 17 Oct 2015 00:40:58 +0000 (17:40 -0700)
committerThierry Reding <thierry.reding@gmail.com>
Tue, 10 Nov 2015 12:06:16 +0000 (13:06 +0100)
The pwm_enable() function didn't clear the enabled bit if a call to the
driver's ->enable() callback returned an error. The result was that the
state of the PWM core was wrong. Clearing the bit when enable returns
an error ensures the state is properly set.

Tested-by: Jonathan Richardson <jonathar@broadcom.com>
Reviewed-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
Signed-off-by: Jonathan Richardson <jonathar@broadcom.com>
[thierry.reding@gmail.com: add missing kerneldoc for the lock]
Signed-off-by: Thierry Reding <thierry.reding@gmail.com>
drivers/pwm/core.c
include/linux/pwm.h

index 3f9df3ea33508da41334178038c121f6a3939f79..b8f6c309c160ace262e50ea5f2513a3673ce69fc 100644 (file)
@@ -269,6 +269,7 @@ int pwmchip_add_with_polarity(struct pwm_chip *chip,
                pwm->pwm = chip->base + i;
                pwm->hwpwm = i;
                pwm->polarity = polarity;
+               mutex_init(&pwm->lock);
 
                radix_tree_insert(&pwm_tree, pwm->pwm, pwm);
        }
@@ -473,16 +474,22 @@ int pwm_set_polarity(struct pwm_device *pwm, enum pwm_polarity polarity)
        if (!pwm->chip->ops->set_polarity)
                return -ENOSYS;
 
-       if (pwm_is_enabled(pwm))
-               return -EBUSY;
+       mutex_lock(&pwm->lock);
+
+       if (pwm_is_enabled(pwm)) {
+               err = -EBUSY;
+               goto unlock;
+       }
 
        err = pwm->chip->ops->set_polarity(pwm->chip, pwm, polarity);
        if (err)
-               return err;
+               goto unlock;
 
        pwm->polarity = polarity;
 
-       return 0;
+unlock:
+       mutex_unlock(&pwm->lock);
+       return err;
 }
 EXPORT_SYMBOL_GPL(pwm_set_polarity);
 
@@ -494,10 +501,22 @@ EXPORT_SYMBOL_GPL(pwm_set_polarity);
  */
 int pwm_enable(struct pwm_device *pwm)
 {
-       if (pwm && !test_and_set_bit(PWMF_ENABLED, &pwm->flags))
-               return pwm->chip->ops->enable(pwm->chip, pwm);
+       int err = 0;
+
+       if (!pwm)
+               return -EINVAL;
+
+       mutex_lock(&pwm->lock);
+
+       if (!test_and_set_bit(PWMF_ENABLED, &pwm->flags)) {
+               err = pwm->chip->ops->enable(pwm->chip, pwm);
+               if (err)
+                       clear_bit(PWMF_ENABLED, &pwm->flags);
+       }
+
+       mutex_unlock(&pwm->lock);
 
-       return pwm ? 0 : -EINVAL;
+       return err;
 }
 EXPORT_SYMBOL_GPL(pwm_enable);
 
index d681f6875aef1a7a8b22c9453b8f9019f9fd45eb..cfc3ed46cad20a26ffa131b51e25927ac6045f4c 100644 (file)
@@ -2,6 +2,7 @@
 #define __LINUX_PWM_H
 
 #include <linux/err.h>
+#include <linux/mutex.h>
 #include <linux/of.h>
 
 struct pwm_device;
@@ -87,6 +88,7 @@ enum {
  * @pwm: global index of the PWM device
  * @chip: PWM chip providing this PWM device
  * @chip_data: chip-private data associated with the PWM device
+ * @lock: used to serialize accesses to the PWM device where necessary
  * @period: period of the PWM signal (in nanoseconds)
  * @duty_cycle: duty cycle of the PWM signal (in nanoseconds)
  * @polarity: polarity of the PWM signal
@@ -98,6 +100,7 @@ struct pwm_device {
        unsigned int pwm;
        struct pwm_chip *chip;
        void *chip_data;
+       struct mutex lock;
 
        unsigned int period;
        unsigned int duty_cycle;