mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: barebox@lists.infradead.org
Subject: [PATCH 4/5] led: Add pwm-led driver
Date: Fri, 28 Feb 2014 15:24:54 +0100	[thread overview]
Message-ID: <1393597495-13896-4-git-send-email-s.hauer@pengutronix.de> (raw)
In-Reply-To: <1393597495-13896-1-git-send-email-s.hauer@pengutronix.de>

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

  parent reply	other threads:[~2014-02-28 14:25 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2014-02-28 14:24 ` [PATCH 5/5] led: Add default-on trigger Sascha Hauer

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=1393597495-13896-4-git-send-email-s.hauer@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --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