[Intel-gfx] [RFC v5 4/9] drivers/pwm: Add helper to configure pwm using clock divisor and duty percent

Shobhit Kumar shobhit.kumar at intel.com
Thu Mar 12 09:31:28 PDT 2015


Some chips instead of using period_ns and duty_ns can be configured
using the clock divisor and duty percent. Adds an alternative
configuration method for such chips

CC: Samuel Ortiz <sameo at linux.intel.com>
Cc: Linus Walleij <linus.walleij at linaro.org>
Cc: Alexandre Courbot <gnurou at gmail.com>
Cc: Thierry Reding <thierry.reding at gmail.com>
Signed-off-by: Shobhit Kumar <shobhit.kumar at intel.com>
---
 drivers/pwm/core.c  | 24 ++++++++++++++++++++++++
 include/linux/pwm.h | 33 +++++++++++++++++++++++++++++++++
 2 files changed, 57 insertions(+)

diff --git a/drivers/pwm/core.c b/drivers/pwm/core.c
index 810aef3..d979bc0 100644
--- a/drivers/pwm/core.c
+++ b/drivers/pwm/core.c
@@ -422,6 +422,30 @@ int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
 EXPORT_SYMBOL_GPL(pwm_config);
 
 /**
+ * pwm_config_alternate() - change a PWM device configuration
+ * @pwm: PWM device
+ * @clk_div: Clock divider to configure
+ * @duty_percentage: duty cycle in percentage
+ */
+int pwm_config_alternate(struct pwm_device *pwm, int clk_div, int duty_percent)
+{
+	int err;
+
+	if (!pwm || clk_div < 0 || duty_percent < 0 || duty_percent > 100)
+		return -EINVAL;
+
+	err = pwm->chip->ops->config_alternate(pwm->chip, pwm, clk_div, duty_percent);
+	if (err)
+		return err;
+
+	pwm->clk_div = clk_div;
+	pwm->duty_percent = duty_percent;
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(pwm_config_alternate);
+
+/**
  * pwm_set_polarity() - configure the polarity of a PWM signal
  * @pwm: PWM device
  * @polarity: new polarity of the PWM signal
diff --git a/include/linux/pwm.h b/include/linux/pwm.h
index e90628c..739cb2b 100644
--- a/include/linux/pwm.h
+++ b/include/linux/pwm.h
@@ -24,6 +24,12 @@ void pwm_free(struct pwm_device *pwm);
 int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns);
 
 /*
+ * pwm_config_alternate - change a PWM device configuration
+ *			  based on clk_dic and duty_percent
+ */
+int pwm_config_alternate(struct pwm_device *pwm, int clk_div, int duty_percent);
+
+/*
  * pwm_enable - start a PWM output toggling
  */
 int pwm_enable(struct pwm_device *pwm);
@@ -89,6 +95,8 @@ struct pwm_device {
 
 	unsigned int		period; 	/* in nanoseconds */
 	unsigned int		duty_cycle;	/* in nanoseconds */
+	unsigned int		clk_div;
+	unsigned int		duty_percent;
 	enum pwm_polarity	polarity;
 };
 
@@ -114,6 +122,28 @@ static inline unsigned int pwm_get_duty_cycle(struct pwm_device *pwm)
 	return pwm ? pwm->duty_cycle : 0;
 }
 
+static inline void pwm_set_clk_div(struct pwm_device *pwm, unsigned int clk_div)
+{
+	if (pwm)
+		pwm->clk_div = clk_div;
+}
+
+static inline unsigned int pwm_get_clk_div(struct pwm_device *pwm)
+{
+	return pwm ? pwm->clk_div : 0;
+}
+
+static inline void pwm_set_duty_percent(struct pwm_device *pwm, unsigned int duty_percent)
+{
+	if (pwm)
+		pwm->duty_percent = duty_percent;
+}
+
+static inline unsigned int pwm_get_duty_percent(struct pwm_device *pwm)
+{
+	return pwm ? pwm->duty_percent : 0;
+}
+
 /*
  * pwm_set_polarity - configure the polarity of a PWM signal
  */
@@ -138,6 +168,9 @@ struct pwm_ops {
 	int			(*config)(struct pwm_chip *chip,
 					  struct pwm_device *pwm,
 					  int duty_ns, int period_ns);
+	int			(*config_alternate)(struct pwm_chip *chip,
+					  struct pwm_device *pwm,
+					  int clk_div, int duty_percent);
 	int			(*set_polarity)(struct pwm_chip *chip,
 					  struct pwm_device *pwm,
 					  enum pwm_polarity polarity);
-- 
2.1.0



More information about the Intel-gfx mailing list