mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/5] PWM: Add functions for getting/setting period/duty cycle
@ 2014-02-28 14:24 Sascha Hauer
  2014-02-28 14:24 ` [PATCH 2/5] PWM: Implement devicetree support Sascha Hauer
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Sascha Hauer @ 2014-02-28 14:24 UTC (permalink / raw)
  To: barebox

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 drivers/pwm/core.c | 20 ++++++++++++++++++++
 include/pwm.h      |  5 +++++
 2 files changed, 25 insertions(+)

diff --git a/drivers/pwm/core.c b/drivers/pwm/core.c
index f2b062e..7f30724 100644
--- a/drivers/pwm/core.c
+++ b/drivers/pwm/core.c
@@ -198,6 +198,26 @@ int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
 }
 EXPORT_SYMBOL_GPL(pwm_config);
 
+void pwm_set_period(struct pwm_device *pwm, unsigned int period_ns)
+{
+	pwm->period_ns = period_ns;
+}
+
+unsigned int pwm_get_period(struct pwm_device *pwm)
+{
+	return pwm->period_ns;
+}
+
+void pwm_set_duty_cycle(struct pwm_device *pwm, unsigned int duty_ns)
+{
+	pwm->duty_ns = duty_ns;
+}
+
+unsigned int pwm_get_duty_cycle(struct pwm_device *pwm)
+{
+	return pwm->duty_ns;
+}
+
 /*
  * pwm_enable - start a PWM output toggling
  */
diff --git a/include/pwm.h b/include/pwm.h
index bdc2fdd..5ca9fa0 100644
--- a/include/pwm.h
+++ b/include/pwm.h
@@ -29,6 +29,11 @@ int pwm_enable(struct pwm_device *pwm);
  */
 void pwm_disable(struct pwm_device *pwm);
 
+void pwm_set_period(struct pwm_device *pwm, unsigned int period);
+unsigned int pwm_get_period(struct pwm_device *pwm);
+void pwm_set_duty_cycle(struct pwm_device *pwm, unsigned int duty);
+unsigned int pwm_get_duty_cycle(struct pwm_device *pwm);
+
 struct pwm_chip;
 
 /**
-- 
1.8.5.3


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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 2/5] PWM: Implement devicetree support
  2014-02-28 14:24 [PATCH 1/5] PWM: Add functions for getting/setting period/duty cycle Sascha Hauer
@ 2014-02-28 14:24 ` Sascha Hauer
  2014-02-28 14:24 ` [PATCH 3/5] led: move led_of_parse_trigger to core Sascha Hauer
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Sascha Hauer @ 2014-02-28 14:24 UTC (permalink / raw)
  To: barebox

This implements of_pwm_request() for PWM client drivers.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 drivers/pwm/core.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++++------
 include/pwm.h      |  2 ++
 2 files changed, 66 insertions(+), 7 deletions(-)

diff --git a/drivers/pwm/core.c b/drivers/pwm/core.c
index 7f30724..cc33dec 100644
--- a/drivers/pwm/core.c
+++ b/drivers/pwm/core.c
@@ -143,6 +143,24 @@ int pwmchip_remove(struct pwm_chip *chip)
 }
 EXPORT_SYMBOL_GPL(pwmchip_remove);
 
+static int __pwm_request(struct pwm_device *pwm)
+{
+	int ret;
+
+	if (test_bit(FLAG_REQUESTED, &pwm->flags))
+		return -EBUSY;
+
+	if (pwm->chip->ops->request) {
+		ret = pwm->chip->ops->request(pwm->chip);
+		if (ret)
+			return ret;
+	}
+
+	set_bit(FLAG_REQUESTED, &pwm->flags);
+
+	return 0;
+}
+
 /*
  * pwm_request - request a PWM device
  */
@@ -155,20 +173,59 @@ struct pwm_device *pwm_request(const char *devname)
 	if (!pwm)
 		return NULL;
 
-	if (test_bit(FLAG_REQUESTED, &pwm->flags))
+	ret = __pwm_request(pwm);
+	if (ret)
 		return NULL;
 
-	if (pwm->chip->ops->request) {
-		ret = pwm->chip->ops->request(pwm->chip);
-		if (ret)
-			return NULL;
+	return pwm;
+}
+EXPORT_SYMBOL_GPL(pwm_request);
+
+static struct pwm_device *of_node_to_pwm_device(struct device_node *np)
+{
+	struct pwm_device *pwm;
+
+	list_for_each_entry(pwm, &pwm_list, node) {
+		if (pwm->hwdev && pwm->hwdev->device_node == np)
+			return pwm;
 	}
 
-	set_bit(FLAG_REQUESTED, &pwm->flags);
+        return ERR_PTR(-ENODEV);
+}
+
+struct pwm_device *of_pwm_request(struct device_node *np, const char *con_id)
+{
+	struct of_phandle_args args;
+	int index = 0;
+	struct pwm_device *pwm;
+	int ret;
+
+	if (con_id)
+		return ERR_PTR(-EINVAL);
+
+	ret = of_parse_phandle_with_args(np, "pwms", "#pwm-cells", index,
+			&args);
+	if (ret) {
+		pr_debug("%s(): can't parse \"pwms\" property\n", __func__);
+		return ERR_PTR(ret);
+	}
+
+	pwm = of_node_to_pwm_device(args.np);
+	if (IS_ERR(pwm)) {
+		pr_debug("%s(): PWM chip not found\n", __func__);
+		return pwm;
+	}
+
+	if (args.args_count > 1)
+		pwm_set_period(pwm, args.args[1]);
+
+	ret = __pwm_request(pwm);
+	if (ret)
+		return ERR_PTR(-ret);
 
 	return pwm;
 }
-EXPORT_SYMBOL_GPL(pwm_request);
+EXPORT_SYMBOL_GPL(of_pwm_request);
 
 /*
  * pwm_free - free a PWM device
diff --git a/include/pwm.h b/include/pwm.h
index 5ca9fa0..59d86d4 100644
--- a/include/pwm.h
+++ b/include/pwm.h
@@ -9,6 +9,8 @@ struct device_d;
  */
 struct pwm_device *pwm_request(const char *pwmname);
 
+struct pwm_device *of_pwm_request(struct device_node *np, const char *con_id);
+
 /*
  * pwm_free - free a PWM device
  */
-- 
1.8.5.3


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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 3/5] led: move led_of_parse_trigger to core
  2014-02-28 14:24 [PATCH 1/5] PWM: Add functions for getting/setting period/duty cycle Sascha Hauer
  2014-02-28 14:24 ` [PATCH 2/5] PWM: Implement devicetree support Sascha Hauer
@ 2014-02-28 14:24 ` Sascha Hauer
  2014-02-28 14:24 ` [PATCH 4/5] led: Add pwm-led driver Sascha Hauer
  2014-02-28 14:24 ` [PATCH 5/5] led: Add default-on trigger Sascha Hauer
  3 siblings, 0 replies; 5+ messages in thread
From: Sascha Hauer @ 2014-02-28 14:24 UTC (permalink / raw)
  To: barebox

So that other LED drivers can use it.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 drivers/led/core.c     | 35 +++++++++++++++++++++++++++++++++++
 drivers/led/led-gpio.c | 35 -----------------------------------
 include/led.h          |  2 ++
 3 files changed, 37 insertions(+), 35 deletions(-)

diff --git a/drivers/led/core.c b/drivers/led/core.c
index 8537aa1..30b016b 100644
--- a/drivers/led/core.c
+++ b/drivers/led/core.c
@@ -153,3 +153,38 @@ void led_unregister(struct led *led)
 {
 	list_del(&led->list);
 }
+
+struct led_trg {
+	const char *str;
+	enum led_trigger trg;
+};
+
+static struct led_trg triggers[] = {
+	{ .str = "heartbeat", LED_TRIGGER_HEARTBEAT, },
+	{ .str = "panic", LED_TRIGGER_PANIC, },
+	{ .str = "net", LED_TRIGGER_NET_TXRX, },
+	{ .str = "default-on", LED_TRIGGER_DEFAULT_ON, },
+};
+
+void led_of_parse_trigger(struct led *led, struct device_node *np)
+{
+	const char *trigger;
+	int i;
+
+	trigger = of_get_property(np, "linux,default-trigger", NULL);
+	if (!trigger)
+		trigger = of_get_property(np, "barebox,default-trigger", NULL);
+
+	if (!trigger)
+		return;
+
+	for (i = 0; i < ARRAY_SIZE(triggers); i++) {
+		struct led_trg *trg = &triggers[i];
+		if (!strcmp(trg->str, trigger)) {
+			/* disable LED before installing trigger */
+			led_set(led, 0);
+			led_set_trigger(trg->trg, led);
+			return;
+		}
+	}
+}
diff --git a/drivers/led/led-gpio.c b/drivers/led/led-gpio.c
index 7a5ef47..7bb3b49 100644
--- a/drivers/led/led-gpio.c
+++ b/drivers/led/led-gpio.c
@@ -198,41 +198,6 @@ void led_gpio_rgb_unregister(struct gpio_led *led)
 #endif /* CONFIG_LED_GPIO_RGB */
 
 #ifdef CONFIG_LED_GPIO_OF
-
-struct led_trg {
-	const char *str;
-	enum led_trigger trg;
-};
-
-static struct led_trg triggers[] = {
-	{ .str = "heartbeat", LED_TRIGGER_HEARTBEAT, },
-	{ .str = "panic", LED_TRIGGER_PANIC, },
-	{ .str = "net", LED_TRIGGER_NET_TXRX, },
-};
-
-static void led_of_parse_trigger(struct led *led, struct device_node *np)
-{
-	const char *trigger;
-	int i;
-
-	trigger = of_get_property(np, "linux,default-trigger", NULL);
-	if (!trigger)
-		trigger = of_get_property(np, "barebox,default-trigger", NULL);
-
-	if (!trigger)
-		return;
-
-	for (i = 0; i < ARRAY_SIZE(triggers); i++) {
-		struct led_trg *trg = &triggers[i];
-		if (!strcmp(trg->str, trigger)) {
-			/* disable LED before installing trigger */
-			led_set(led, 0);
-			led_set_trigger(trg->trg, led);
-			return;
-		}
-	}
-}
-
 static int led_gpio_of_probe(struct device_d *dev)
 {
 	struct device_node *child;
diff --git a/include/led.h b/include/led.h
index dd551fe..0217f4b 100644
--- a/include/led.h
+++ b/include/led.h
@@ -58,6 +58,8 @@ static inline void led_trigger(enum led_trigger trigger, enum trigger_type type)
 
 int led_get_trigger(enum led_trigger trigger);
 
+void led_of_parse_trigger(struct led *led, struct device_node *np);
+
 /* gpio LED support */
 struct gpio_led {
 	int gpio;
-- 
1.8.5.3


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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 4/5] led: Add pwm-led driver
  2014-02-28 14:24 [PATCH 1/5] PWM: Add functions for getting/setting period/duty cycle Sascha Hauer
  2014-02-28 14:24 ` [PATCH 2/5] PWM: Implement devicetree support Sascha Hauer
  2014-02-28 14:24 ` [PATCH 3/5] led: move led_of_parse_trigger to core Sascha Hauer
@ 2014-02-28 14:24 ` Sascha Hauer
  2014-02-28 14:24 ` [PATCH 5/5] led: Add default-on trigger Sascha Hauer
  3 siblings, 0 replies; 5+ messages in thread
From: Sascha Hauer @ 2014-02-28 14:24 UTC (permalink / raw)
  To: barebox

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 drivers/led/Kconfig   |  4 +++
 drivers/led/Makefile  |  1 +
 drivers/led/led-pwm.c | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 98 insertions(+)
 create mode 100644 drivers/led/led-pwm.c

diff --git a/drivers/led/Kconfig b/drivers/led/Kconfig
index 3ead82e..155a78a 100644
--- a/drivers/led/Kconfig
+++ b/drivers/led/Kconfig
@@ -7,6 +7,10 @@ config LED_GPIO
 	bool "gpio LED support"
 	depends on GENERIC_GPIO
 
+config LED_PWM
+	bool "PWM LED support"
+	depends on PWM
+
 config LED_GPIO_OF
 	bool "support parsing gpio LEDs from device tree"
 	depends on LED_GPIO && OFTREE
diff --git a/drivers/led/Makefile b/drivers/led/Makefile
index 6aafa6d..619bbcf 100644
--- a/drivers/led/Makefile
+++ b/drivers/led/Makefile
@@ -1,3 +1,4 @@
 obj-$(CONFIG_LED) += core.o
 obj-$(CONFIG_LED_GPIO) += led-gpio.o
+obj-$(CONFIG_LED_PWM) += led-pwm.o
 obj-$(CONFIG_LED_TRIGGERS) += led-triggers.o
diff --git a/drivers/led/led-pwm.c b/drivers/led/led-pwm.c
new file mode 100644
index 0000000..16d22b5
--- /dev/null
+++ b/drivers/led/led-pwm.c
@@ -0,0 +1,93 @@
+/*
+ * pwm LED support for barebox
+ *
+ * (C) Copyright 2010 Sascha Hauer, Pengutronix
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ */
+#include <common.h>
+#include <malloc.h>
+#include <init.h>
+#include <led.h>
+#include <pwm.h>
+#include <of.h>
+#include <asm-generic/div64.h>
+
+struct pwmled {
+	bool active_low;
+	struct led led;
+	struct pwm_device *pwm;
+	uint32_t period;
+};
+
+static void led_pwm_set(struct led *led, unsigned int brightness)
+{
+	struct pwmled *pwmled = container_of(led, struct pwmled, led);
+	unsigned long long duty =  pwmled->period;
+	unsigned int max = pwmled->led.max_value;
+
+        duty *= brightness;
+        do_div(duty, max);
+
+	pwm_config(pwmled->pwm, duty, pwmled->period);
+}
+
+static int led_pwm_of_probe(struct device_d *dev)
+{
+	struct device_node *child;
+	int ret;
+
+	for_each_child_of_node(dev->device_node, child) {
+		struct pwmled *pwmled;
+		struct pwm_device *pwm;
+
+		pwm = of_pwm_request(child, NULL);
+		if (pwm < 0)
+			continue;
+
+		pwmled = xzalloc(sizeof(*pwmled));
+		pwmled->led.name = xstrdup(child->name);
+		pwmled->pwm = pwm;
+
+		of_property_read_u32(child, "max-brightness", &pwmled->led.max_value);
+
+		pwmled->period = pwm_get_period(pwmled->pwm);
+
+		pwmled->led.set = led_pwm_set;
+
+		pwm_config(pwmled->pwm, 0, pwmled->period);
+		pwm_enable(pwmled->pwm);
+
+		ret = led_register(&pwmled->led);
+		if (ret)
+			return ret;
+
+		led_of_parse_trigger(&pwmled->led, child);
+	}
+
+	return 0;
+}
+
+static struct of_device_id led_pwm_of_ids[] = {
+	{ .compatible = "pwm-leds", },
+	{ }
+};
+
+static struct driver_d led_pwm_of_driver = {
+	.name  = "pwm-leds",
+	.probe = led_pwm_of_probe,
+	.of_compatible = DRV_OF_COMPAT(led_pwm_of_ids),
+};
+device_platform_driver(led_pwm_of_driver);
-- 
1.8.5.3


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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 5/5] led: Add default-on trigger
  2014-02-28 14:24 [PATCH 1/5] PWM: Add functions for getting/setting period/duty cycle Sascha Hauer
                   ` (2 preceding siblings ...)
  2014-02-28 14:24 ` [PATCH 4/5] led: Add pwm-led driver Sascha Hauer
@ 2014-02-28 14:24 ` Sascha Hauer
  3 siblings, 0 replies; 5+ messages in thread
From: Sascha Hauer @ 2014-02-28 14:24 UTC (permalink / raw)
  To: barebox

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 drivers/led/led-triggers.c | 3 +++
 include/led.h              | 1 +
 2 files changed, 4 insertions(+)

diff --git a/drivers/led/led-triggers.c b/drivers/led/led-triggers.c
index 7555eee..5eaf732 100644
--- a/drivers/led/led-triggers.c
+++ b/drivers/led/led-triggers.c
@@ -124,6 +124,9 @@ int led_set_trigger(enum led_trigger trigger, struct led *led)
 
 	triggers[trigger].led = led;
 
+	if (trigger == LED_TRIGGER_DEFAULT_ON)
+		led_set(triggers[trigger].led, triggers[trigger].led->max_value);
+
 	return 0;
 }
 
diff --git a/include/led.h b/include/led.h
index 0217f4b..f17621e 100644
--- a/include/led.h
+++ b/include/led.h
@@ -33,6 +33,7 @@ enum led_trigger {
 	LED_TRIGGER_NET_RX,
 	LED_TRIGGER_NET_TX,
 	LED_TRIGGER_NET_TXRX,
+	LED_TRIGGER_DEFAULT_ON,
 	LED_TRIGGER_MAX,
 };
 
-- 
1.8.5.3


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

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2014-02-28 14:25 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-02-28 14:24 [PATCH 1/5] PWM: Add functions for getting/setting period/duty cycle Sascha Hauer
2014-02-28 14:24 ` [PATCH 2/5] PWM: Implement devicetree support Sascha Hauer
2014-02-28 14:24 ` [PATCH 3/5] led: move led_of_parse_trigger to core Sascha Hauer
2014-02-28 14:24 ` [PATCH 4/5] led: Add pwm-led driver Sascha Hauer
2014-02-28 14:24 ` [PATCH 5/5] led: Add default-on trigger Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox