Skip to contentSkip to conversation

pwm: Add support for configuring the PWM polarity

PATCH Applied Philip, Avinash 2 participants 3 messages Jul 24, 2012 – Aug 9, 2012 PWM

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 pwm: Add support for configuring the PWM polarity

Some hardware supports inverting the polarity of the PWM signal. This commit adds support to the PWM framework to allow users of the PWM API to configure the polarity. Note that in order to reduce complexity, changing the polarity of a PWM signal is only allowed while the PWM is disabled.

A practical example where this can prove useful is to simulate inversion of the duty cycle. While inversion of polarity and duty cycle are not exactly the same, the differences for most use-cases are negligible.

---
changes in V2:
- Updates commit message
- Provides proper comments
- Removes usage of polarity variable.
- Removes encoding of polarity in flags

Configuring polarity to be done with PWM device disabled, if not failure reported.

If PWM device is enabled while configuring polarity, disabling and re_enabling make it complex. Whoever uses this API has to taken care of the basic rules.

Discussions related to this can found at
http://www.spinics.net/lists/kernel/msg1372110.html

:100644 100644 ecb7690... f0a1db3... M drivers/pwm/core.c
:100644 100644 21d076c... 354764c... M include/linux/pwm.h

 drivers/pwm/core.c  |   22 ++++++++++++++++++++++
 include/linux/pwm.h |   23 +++++++++++++++++++++++
 2 files changed, 45 insertions(+), 0 deletions(-)
drivers/pwm/core.c +22 -0
index ecb7690..f0a1db3 100644
@@ -379,6 +379,28 @@ int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
 EXPORT_SYMBOL_GPL(pwm_config);
 
 /**
+ * pwm_set_polarity() - configure the polarity of a PWM signal
+ * @pwm: PWM device
+ * @polarity: new polarity of the PWM signal
+ *
+ * Note that the polarity cannot be configured while the PWM device is enabled
+ */
+int pwm_set_polarity(struct pwm_device *pwm, enum pwm_polarity polarity)
+{
+	if (!pwm || !pwm->chip->ops)
+		return -EINVAL;
+
+	if (!pwm->chip->ops->set_polarity)
+		return -ENOSYS;
+
+	if (test_bit(PWMF_ENABLED, &pwm->flags))
+		return -EBUSY;
+
+	return pwm->chip->ops->set_polarity(pwm->chip, pwm, polarity);
+}
+EXPORT_SYMBOL_GPL(pwm_set_polarity);
+
+/**
  * pwm_enable() - start a PWM output toggling
  * @pwm: PWM device
  */
include/linux/pwm.h +23 -0
index 21d076c..354764c 100644
@@ -34,6 +34,20 @@ void pwm_disable(struct pwm_device *pwm);
 #ifdef CONFIG_PWM
 struct pwm_chip;
 
+/**
+ * enum pwm_polarity - polarity of a PWM signal
+ * @PWM_POLARITY_NORMAL: a high signal for the duration of the duty-
+ * cycle, followed by a low signal for the remainder of the pulse
+ * period
+ * @PWM_POLARITY_INVERSED: a low signal for the duration of the duty-
+ * cycle, followed by a high signal for the remainder of the pulse
+ * period
+ */
+enum pwm_polarity {
+	PWM_POLARITY_NORMAL,
+	PWM_POLARITY_INVERSED,
+};
+
 enum {
 	PWMF_REQUESTED = 1 << 0,
 	PWMF_ENABLED = 1 << 1,
@@ -61,11 +75,17 @@ static inline unsigned int pwm_get_period(struct pwm_device *pwm)
 	return pwm ? pwm->period : 0;
 }
 
+/*
+ * pwm_set_polarity - configure the polarity of a PWM signal
+ */
+int pwm_set_polarity(struct pwm_device *pwm, enum pwm_polarity polarity);
+
 /**
  * struct pwm_ops - PWM controller operations
  * @request: optional hook for requesting a PWM
  * @free: optional hook for freeing a PWM
  * @config: configure duty cycles and period length for this PWM
+ * @set_polarity: configure the polarity of this PWM
  * @enable: enable PWM output toggling
  * @disable: disable PWM output toggling
  * @dbg_show: optional routine to show contents in debugfs
@@ -79,6 +99,9 @@ struct pwm_ops {
 	int			(*config)(struct pwm_chip *chip,
 					  struct pwm_device *pwm,
 					  int duty_ns, int period_ns);
+	int			(*set_polarity)(struct pwm_chip *chip,
+					  struct pwm_device *pwm,
+					  enum pwm_polarity polarity);
 	int			(*enable)(struct pwm_chip *chip,
 					  struct pwm_device *pwm);
 	void			(*disable)(struct pwm_chip *chip,
Signature · 1 line
1.7.1
#1 lore raw
Quoted 11 lines · Avinash

Some hardware supports inverting the polarity of the PWM signal. This commit adds support to the PWM framework to allow users of the PWM API to configure the polarity. Note that in order to reduce complexity, changing the polarity of a PWM signal is only allowed while the PWM is disabled.

A practical example where this can prove useful is to simulate inversion of the duty cycle. While inversion of polarity and duty cycle are not exactly the same, the differences for most use-cases are negligible.

This looks good to me. I'll queue this for 3.7 once the 3.6 merge window closes.

Thanks,
Thierry

#2 lore raw
In reply to Philip, Avinash
Quoted 25 lines · Avinash

Some hardware supports inverting the polarity of the PWM signal. This commit adds support to the PWM framework to allow users of the PWM API to configure the polarity. Note that in order to reduce complexity, changing the polarity of a PWM signal is only allowed while the PWM is disabled.

A practical example where this can prove useful is to simulate inversion of the duty cycle. While inversion of polarity and duty cycle are not exactly the same, the differences for most use-cases are negligible.

---
changes in V2:
- Updates commit message
- Provides proper comments
- Removes usage of polarity variable.
- Removes encoding of polarity in flags

Configuring polarity to be done with PWM device disabled, if not failure reported.

If PWM device is enabled while configuring polarity, disabling and re_enabling make it complex. Whoever uses this API has to taken care of the basic rules.

Quoted 8 lines

Discussions related to this can found at
http://www.spinics.net/lists/kernel/msg1372110.html

:100644 100644 ecb7690... f0a1db3... M drivers/pwm/core.c
:100644 100644 21d076c... 354764c... M include/linux/pwm.h

 drivers/pwm/core.c  |   22 ++++++++++++++++++++++
 include/linux/pwm.h |   23 +++++++++++++++++++++++
 2 files changed, 45 insertions(+), 0 deletions(-)

Applied, thanks.

Thierry