summaryrefslogtreecommitdiffstats
path: root/drivers/led
diff options
context:
space:
mode:
authorSascha Hauer <s.hauer@pengutronix.de>2017-03-13 10:33:36 +0100
committerSascha Hauer <s.hauer@pengutronix.de>2017-03-30 08:23:20 +0200
commitaea9aba24910b8aa89a889e8d826b8dc69d3d48b (patch)
tree51469149a38ca550f1e041a70603078ca630332a /drivers/led
parent441e9f5a72b245b671118f3e771eb129834a7a34 (diff)
downloadbarebox-aea9aba24910b8aa89a889e8d826b8dc69d3d48b.tar.gz
barebox-aea9aba24910b8aa89a889e8d826b8dc69d3d48b.tar.xz
led: add blinking/flashing and led_blink_pattern interface
So far blinking/flashing LEDs is only supported on led-trigger level. Even without triggers it useful to be able to blink/flash LEDs, so add this functionality to the LED core. A led_blink_pattern consists of a number of on and off-periods which are described in an array. Using such an array you can encode nearly every blink pattern you need. Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de> Signed-off-by: Markus Pargmann <mpa@pengutronix.de> Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'drivers/led')
-rw-r--r--drivers/led/Kconfig1
-rw-r--r--drivers/led/core.c101
2 files changed, 101 insertions, 1 deletions
diff --git a/drivers/led/Kconfig b/drivers/led/Kconfig
index 155a78a7df..50f0d8f974 100644
--- a/drivers/led/Kconfig
+++ b/drivers/led/Kconfig
@@ -1,5 +1,6 @@
menuconfig LED
bool "LED support"
+ select POLLER
if LED
diff --git a/drivers/led/core.c b/drivers/led/core.c
index 30b016bb34..6f66de0fbb 100644
--- a/drivers/led/core.c
+++ b/drivers/led/core.c
@@ -23,6 +23,7 @@
#include <linux/list.h>
#include <errno.h>
#include <led.h>
+#include <init.h>
#include <poller.h>
#include <clock.h>
#include <linux/ctype.h>
@@ -101,7 +102,7 @@ struct led *led_by_name_or_number(const char *str)
* @param led the led
* @param value the value of the LED (0 is disabled)
*/
-int led_set(struct led *led, unsigned int value)
+static int __led_set(struct led *led, unsigned int value)
{
if (value > led->max_value)
value = led->max_value;
@@ -114,6 +115,104 @@ int led_set(struct led *led, unsigned int value)
return 0;
}
+int led_set(struct led *led, unsigned int value)
+{
+ led->blink = 0;
+ led->flash = 0;
+ return __led_set(led, value);
+}
+
+static void led_blink_func(struct poller_struct *poller)
+{
+ struct led *led;
+
+ list_for_each_entry(led, &leds, list) {
+ bool on;
+
+ if (!led->blink && !led->flash)
+ continue;
+
+ if (led->blink_next_event > get_time_ns()) {
+ continue;
+ }
+
+ on = !(led->blink_next_state % 2);
+
+ led->blink_next_event = get_time_ns() +
+ (led->blink_states[led->blink_next_state] * MSECOND);
+ led->blink_next_state = (led->blink_next_state + 1) %
+ led->blink_nr_states;
+
+ if (led->flash && !on)
+ led->flash = 0;
+
+ __led_set(led, on);
+ }
+}
+
+/**
+ * led_blink_pattern - Blink a led with flexible timings.
+ * @led LED used
+ * @pattern Array of millisecond intervals describing the on and off periods of
+ * the pattern. At the end of the array/pattern it is repeated. The array
+ * starts with an on-period. In general every array item with even index
+ * describes an on-period, every item with odd index an off-period.
+ * @pattern_len Length of the pattern array.
+ *
+ * Returns 0 on success.
+ *
+ * Example:
+ * pattern = {500, 1000};
+ * This will enable the LED for 500ms and disable it for 1000ms after
+ * that. This is repeated forever.
+ */
+int led_blink_pattern(struct led *led, const unsigned int *pattern,
+ unsigned int pattern_len)
+{
+ free(led->blink_states);
+ led->blink_states = xmemdup(pattern,
+ pattern_len * sizeof(*led->blink_states));
+ led->blink_nr_states = pattern_len;
+ led->blink_next_state = 0;
+ led->blink_next_event = get_time_ns();
+ led->blink = 1;
+ led->flash = 0;
+
+ return 0;
+}
+
+int led_blink(struct led *led, unsigned int on_ms, unsigned int off_ms)
+{
+ unsigned int pattern[] = {on_ms, off_ms};
+
+ return led_blink_pattern(led, pattern, 2);
+}
+
+int led_flash(struct led *led, unsigned int duration_ms)
+{
+ unsigned int pattern[] = {duration_ms, 0};
+ int ret;
+
+ ret = led_blink_pattern(led, pattern, 2);
+ if (ret)
+ return ret;
+
+ led->flash = 1;
+ led->blink = 0;
+
+ return 0;
+}
+
+static struct poller_struct led_poller = {
+ .func = led_blink_func,
+};
+
+static int led_blink_init(void)
+{
+ return poller_register(&led_poller);
+}
+late_initcall(led_blink_init);
+
/**
* led_set_num - set the value of a LED
* @param num the number of the LED