mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Robert Jarzmik <robert.jarzmik@free.fr>
To: barebox@lists.infradead.org
Subject: [PATCH V3 1/3] drivers/pwm: add duty_ns and period_ns to core pwm chip
Date: Thu, 16 Feb 2012 19:23:46 +0100	[thread overview]
Message-ID: <1329416628-19781-1-git-send-email-robert.jarzmik@free.fr> (raw)
In-Reply-To: <20120216074031.GG3852@pengutronix.de>

Add variables to control the duty_ns and period_ns of PWM
chips. When these variables are set, a call to either
pwm_enable() or pwm_config() is performed to enforce the
setup values.

Signed-off-by: Robert Jarzmik <robert.jarzmik@free.fr>
---
 drivers/pwm/core.c |   76 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 include/pwm.h      |    7 ++++-
 2 files changed, 81 insertions(+), 2 deletions(-)

diff --git a/drivers/pwm/core.c b/drivers/pwm/core.c
index af30edf..5895584 100644
--- a/drivers/pwm/core.c
+++ b/drivers/pwm/core.c
@@ -29,10 +29,23 @@ struct pwm_device {
 #define FLAG_REQUESTED	0
 #define FLAG_ENABLED	1
 	struct list_head	node;
+	struct device_d		*dev;
 };
 
 static LIST_HEAD(pwm_list);
 
+static struct pwm_device *dev_to_pwm(struct device_d *dev)
+{
+	struct pwm_device *pwm;
+
+	list_for_each_entry(pwm, &pwm_list, node) {
+		if (pwm->dev == dev)
+			return pwm;
+	}
+
+	return NULL;
+}
+
 static struct pwm_device *_find_pwm(const char *devname)
 {
 	struct pwm_device *pwm;
@@ -45,6 +58,63 @@ static struct pwm_device *_find_pwm(const char *devname)
 	return NULL;
 }
 
+static int set_period_ns(struct device_d *dev, struct param_d *p,
+			 const char *val)
+{
+	struct pwm_device *pwm = dev_to_pwm(dev);
+	int period_ns;
+
+	if (!val)
+		return dev_param_set_generic(dev, p, NULL);
+
+	period_ns = simple_strtoul(val, NULL, 0);
+	pwm_config(pwm, pwm->chip->duty_ns, period_ns);
+	return dev_param_set_generic(dev, p, val);
+}
+
+static int set_duty_ns(struct device_d *dev, struct param_d *p, const char *val)
+{
+	struct pwm_device *pwm = dev_to_pwm(dev);
+	int duty_ns;
+
+	if (!val)
+		return dev_param_set_generic(dev, p, NULL);
+
+	duty_ns = simple_strtoul(val, NULL, 0);
+	pwm_config(pwm, duty_ns, pwm->chip->period_ns);
+	return dev_param_set_generic(dev, p, val);
+}
+
+static int set_enable(struct device_d *dev, struct param_d *p, const char *val)
+{
+	struct pwm_device *pwm = dev_to_pwm(dev);
+	int enable;
+
+	if (!val)
+		return dev_param_set_generic(dev, p, NULL);
+
+	enable = !!simple_strtoul(val, NULL, 0);
+	if (enable)
+		pwm_enable(pwm);
+	else
+		pwm_disable(pwm);
+	return dev_param_set_generic(dev, p, enable ? "1" : "0");
+}
+
+static int pwm_register_vars(struct device_d *dev)
+{
+	int ret;
+
+	ret = dev_add_param(dev, "duty_ns", set_duty_ns, NULL, 0);
+	if (!ret)
+		ret = dev_add_param(dev, "period_ns", set_period_ns, NULL, 0);
+	if (!ret)
+		ret = dev_add_param(dev, "enable", set_enable, NULL, 0);
+	if (!ret)
+		ret = dev_set_param(dev, "enable", 0);
+	return ret;
+}
+
 /**
  * pwmchip_add() - register a new pwm
  * @chip: the pwm
@@ -52,7 +122,7 @@ static struct pwm_device *_find_pwm(const char *devname)
  * register a new pwm. pwm->devname must be initialized, usually
  * from dev_name(dev) from the hardware driver.
  */
-int pwmchip_add(struct pwm_chip *chip)
+int pwmchip_add(struct pwm_chip *chip, struct device_d *dev)
 {
 	struct pwm_device *pwm;
 	int ret = 0;
@@ -62,8 +132,10 @@ int pwmchip_add(struct pwm_chip *chip)
 
 	pwm = xzalloc(sizeof(*pwm));
 	pwm->chip = chip;
+	pwm->dev = dev;
 
 	list_add_tail(&pwm->node, &pwm_list);
+	pwm_register_vars(dev);
 
 	return ret;
 }
@@ -136,6 +208,8 @@ EXPORT_SYMBOL_GPL(pwm_free);
  */
 int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
 {
+	pwm->chip->duty_ns = duty_ns;
+	pwm->chip->period_ns = period_ns;
 	return pwm->chip->ops->config(pwm->chip, duty_ns, period_ns);
 }
 EXPORT_SYMBOL_GPL(pwm_config);
diff --git a/include/pwm.h b/include/pwm.h
index 80f88b1..bdc2fdd 100644
--- a/include/pwm.h
+++ b/include/pwm.h
@@ -2,6 +2,7 @@
 #define __PWM_H
 
 struct pwm_device;
+struct device_d;
 
 /*
  * pwm_request - request a PWM device
@@ -51,13 +52,17 @@ struct pwm_ops {
  * struct pwm_chip - abstract a PWM
  * @devname: unique identifier for this pwm
  * @ops: The callbacks for this PWM
+ * @duty_ns: The duty cycle of the PWM, in nano-seconds
+ * @period_ns: The period of the PWM, in nano-seconds
  */
 struct pwm_chip {
 	const char		*devname;
 	struct pwm_ops		*ops;
+	int			duty_ns;
+	int			period_ns;
 };
 
-int pwmchip_add(struct pwm_chip *chip);
+int pwmchip_add(struct pwm_chip *chip, struct device_d *dev);
 int pwmchip_remove(struct pwm_chip *chip);
 
 #endif /* __PWM_H */
-- 
1.7.5.4


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  reply	other threads:[~2012-02-16 18:24 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-01-31  9:36 [PATCH] Add pwm core support Sascha Hauer
2012-02-01  8:20 ` Robert Jarzmik
2012-02-01 22:42 ` [PATCH] drivers/pwm: add PXA pulse width modulator controller Robert Jarzmik
2012-02-03  9:52   ` Sascha Hauer
2012-02-03 15:39     ` Robert Jarzmik
2012-02-08 15:26       ` Robert Jarzmik
2012-02-09  8:10         ` Sascha Hauer
2012-02-09 11:50           ` Robert Jarzmik
2012-02-09 13:44             ` Sascha Hauer
2012-02-09 14:30               ` Robert Jarzmik
2012-02-14 12:58             ` [PATCH V2 1/2] " Robert Jarzmik
2012-02-14 12:58               ` [PATCH V2 2/2] drivers/video: remove pxafb enable on load Robert Jarzmik
2012-02-15  8:32               ` [PATCH V2 1/2] drivers/pwm: add PXA pulse width modulator controller Sascha Hauer
2012-02-15 11:22                 ` Robert Jarzmik
2012-02-15 15:21                 ` Robert Jarzmik
2012-02-16  7:40                   ` Sascha Hauer
2012-02-16 18:23                     ` Robert Jarzmik [this message]
2012-02-16 18:23                       ` [PATCH V3 2/3] " Robert Jarzmik
2012-02-16 18:23                       ` [PATCH V3 3/3] drivers/video: remove pxafb enable on load Robert Jarzmik

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1329416628-19781-1-git-send-email-robert.jarzmik@free.fr \
    --to=robert.jarzmik@free.fr \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox