* [PATCH 4/12] LED: Add LED Timer Trigger
@ 2006-02-05 15:52 Richard Purdie
0 siblings, 0 replies; 2+ messages in thread
From: Richard Purdie @ 2006-02-05 15:52 UTC (permalink / raw)
To: LKML; +Cc: Andrew Morton
Add an example of a complex LED trigger in the form of a generic timer
which triggers the LED its attached to at a user specified frequency
and duty cycle.
Signed-off-by: Richard Purdie <rpurdie@rpsys.net>
Index: linux-2.6.15/drivers/leds/Kconfig
===================================================================
--- linux-2.6.15.orig/drivers/leds/Kconfig 2006-01-29 16:13:48.000000000 +0000
+++ linux-2.6.15/drivers/leds/Kconfig 2006-01-29 20:32:16.000000000 +0000
@@ -22,5 +22,12 @@
These triggers allow kernel events to drive the LEDs and can
be configured via sysfs. If unsure, say Y.
+config LEDS_TRIGGER_TIMER
+ tristate "LED Timer Trigger"
+ depends LEDS_TRIGGERS
+ help
+ This allows LEDs to be controlled by a programmable timer
+ via sysfs. If unsure, say Y.
+
endmenu
Index: linux-2.6.15/drivers/leds/Makefile
===================================================================
--- linux-2.6.15.orig/drivers/leds/Makefile 2006-01-29 16:13:48.000000000 +0000
+++ linux-2.6.15/drivers/leds/Makefile 2006-01-29 20:32:16.000000000 +0000
@@ -3,3 +3,6 @@
obj-$(CONFIG_NEW_LEDS) += led-core.o
obj-$(CONFIG_LEDS_CLASS) += led-class.o
obj-$(CONFIG_LEDS_TRIGGERS) += led-triggers.o
+
+# LED Triggers
+obj-$(CONFIG_LEDS_TRIGGER_TIMER) += ledtrig-timer.o
Index: linux-2.6.15/drivers/leds/ledtrig-timer.c
===================================================================
--- /dev/null 1970-01-01 00:00:00.000000000 +0000
+++ linux-2.6.15/drivers/leds/ledtrig-timer.c 2006-01-29 17:40:11.000000000 +0000
@@ -0,0 +1,205 @@
+/*
+ * LED Kernel Timer Trigger
+ *
+ * Copyright 2005-2006 Openedhand Ltd.
+ *
+ * Author: Richard Purdie <rpurdie@openedhand.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ */
+
+#include <linux/config.h>
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/init.h>
+#include <linux/list.h>
+#include <linux/spinlock.h>
+#include <linux/device.h>
+#include <linux/sysdev.h>
+#include <linux/timer.h>
+#include <linux/leds.h>
+#include "leds.h"
+
+struct timer_trig_data {
+ unsigned long duty; /* duty cycle, as a percentage */
+ unsigned long frequency; /* frequency of blinking, in Hz */
+ unsigned long delay_on; /* milliseconds on */
+ unsigned long delay_off; /* milliseconds off */
+ struct timer_list timer;
+};
+
+static void led_timer_function(unsigned long data)
+{
+ struct led_classdev *led_cdev = (struct led_classdev *) data;
+ struct timer_trig_data *timer_data = led_cdev->trigger_data;
+ unsigned long brightness = LED_OFF;
+ unsigned long delay = timer_data->delay_off;
+
+ write_lock(&led_cdev->lock);
+
+ if (!timer_data->frequency) {
+ led_set_brightness(led_cdev, LED_OFF);
+ write_unlock(&led_cdev->lock);
+ return;
+ }
+
+ if (!led_cdev->brightness) {
+ brightness = LED_FULL;
+ delay = timer_data->delay_on;
+ }
+
+ led_set_brightness(led_cdev, brightness);
+
+ mod_timer(&timer_data->timer, jiffies + msecs_to_jiffies(delay));
+ write_unlock(&led_cdev->lock);
+}
+
+/* led_cdev write lock needs to be held */
+static int led_timer_setdata(struct led_classdev *led_cdev, unsigned long duty, unsigned long frequency)
+{
+ struct timer_trig_data *timer_data = led_cdev->trigger_data;
+
+ if (frequency > 500)
+ return -EINVAL;
+
+ if (duty > 100)
+ return -EINVAL;
+
+ timer_data->duty = duty;
+ timer_data->frequency = frequency;
+ if (frequency != 0) {
+ timer_data->delay_on = duty * 1000 / 50 / frequency / 2;
+ timer_data->delay_off = (100 - duty) * 1000 / 50 / frequency / 2;
+ }
+
+ mod_timer(&timer_data->timer, jiffies + 1);
+
+ return 0;
+}
+
+static ssize_t led_duty_show(struct class_device *dev, char *buf)
+{
+ struct led_classdev *led_cdev = dev->class_data;
+ struct timer_trig_data *timer_data;
+
+ read_lock(&led_cdev->lock);
+ timer_data = led_cdev->trigger_data;
+ sprintf(buf, "%lu\n", timer_data->duty);
+ read_unlock(&led_cdev->lock);
+
+ return strlen(buf) + 1;
+}
+
+static ssize_t led_duty_store(struct class_device *dev, const char *buf, size_t size)
+{
+ struct led_classdev *led_cdev = dev->class_data;
+ struct timer_trig_data *timer_data;
+ int ret = -EINVAL;
+ char *after;
+
+ unsigned long state = simple_strtoul(buf, &after, 10);
+ if (after - buf > 0) {
+ write_lock(&led_cdev->lock);
+ timer_data = led_cdev->trigger_data;
+ ret = led_timer_setdata(led_cdev, state, timer_data->frequency);
+ if (!ret)
+ ret = after - buf;
+ write_unlock(&led_cdev->lock);
+ }
+
+ return ret;
+}
+
+
+static ssize_t led_frequency_show(struct class_device *dev, char *buf)
+{
+ struct led_classdev *led_cdev = dev->class_data;
+ struct timer_trig_data *timer_data;
+
+ read_lock(&led_cdev->lock);
+ timer_data = led_cdev->trigger_data;
+ sprintf(buf, "%lu\n", timer_data->frequency);
+ read_unlock(&led_cdev->lock);
+
+ return strlen(buf) + 1;
+}
+
+static ssize_t led_frequency_store(struct class_device *dev, const char *buf, size_t size)
+{
+ struct led_classdev *led_cdev = dev->class_data;
+ struct timer_trig_data *timer_data;
+ int ret = -EINVAL;
+ char *after;
+
+ unsigned long state = simple_strtoul(buf, &after, 10);
+ if (after - buf > 0) {
+ write_lock(&led_cdev->lock);
+ timer_data = led_cdev->trigger_data;
+ ret = led_timer_setdata(led_cdev, timer_data->duty, state);
+ if (!ret)
+ ret = after - buf;
+ write_unlock(&led_cdev->lock);
+ }
+
+ return ret;
+}
+
+static CLASS_DEVICE_ATTR(duty, 0644, led_duty_show, led_duty_store);
+static CLASS_DEVICE_ATTR(frequency, 0644, led_frequency_show, led_frequency_store);
+
+static void timer_trig_activate(struct led_classdev *led_cdev)
+{
+ struct timer_trig_data *timer_data;
+
+ timer_data = kzalloc(sizeof(struct timer_trig_data), GFP_KERNEL);
+ if (!timer_data)
+ return;
+
+ led_cdev->trigger_data = timer_data;
+
+ init_timer(&timer_data->timer);
+ timer_data->timer.function = led_timer_function;
+ timer_data->timer.data = (unsigned long) led_cdev;
+
+ timer_data->duty = 50;
+
+ class_device_create_file(led_cdev->class_dev, &class_device_attr_duty);
+ class_device_create_file(led_cdev->class_dev, &class_device_attr_frequency);
+}
+
+static void timer_trig_deactivate(struct led_classdev *led_cdev)
+{
+ struct timer_trig_data *timer_data = led_cdev->trigger_data;
+ if (timer_data) {
+ class_device_remove_file(led_cdev->class_dev, &class_device_attr_duty);
+ class_device_remove_file(led_cdev->class_dev, &class_device_attr_frequency);
+ del_timer_sync(&timer_data->timer);
+ kfree(timer_data);
+ }
+}
+
+static struct led_trigger timer_led_trigger = {
+ .name = "timer",
+ .activate = timer_trig_activate,
+ .deactivate = timer_trig_deactivate,
+};
+
+static int __init timer_trig_init(void)
+{
+ return led_trigger_register(&timer_led_trigger);
+}
+
+static void __exit timer_trig_exit (void)
+{
+ led_trigger_unregister(&timer_led_trigger);
+}
+
+module_init(timer_trig_init);
+module_exit(timer_trig_exit);
+
+MODULE_AUTHOR("Richard Purdie <rpurdie@openedhand.com>");
+MODULE_DESCRIPTION("Timer LED trigger");
+MODULE_LICENSE("GPL");
^ permalink raw reply [flat|nested] 2+ messages in thread* RE: [PATCH 0/12] LED Class, Triggers and Drivers
@ 2006-02-05 20:10 John Bowler
2006-02-08 2:45 ` [PATCH 4/12] LED: Add LED Timer Trigger Richard Purdie
0 siblings, 1 reply; 2+ messages in thread
From: John Bowler @ 2006-02-05 20:10 UTC (permalink / raw)
To: 'LKML'
From: Richard Purdie [mailto:rpurdie@rpsys.net]
>This is an updated version of the LED class/subsystem. The main change
>is the renamed API - I've settled on led_device -> led_classdev. Other
>minor issues like the error cases in the timer trigger were also fixed.
In the previous version 'frequency' for the timer trigger was actually
half the period of the oscillation - the time in ms between each (on/off)
state transition.
In this version 'frequency' is the frequency of the oscillation in Hz.
That creates a big problem for me because the value is parsed as an
integer and I can no longer achieve slow flash rates (<1Hz). Since I
have to be able to do this I made a fairly crude patch to store the
frequency in mHz, not Hz, and to handle a decimal point in the value.
Possible fixes:
1) Use 'period' not 'frequency' and accept a value in ms (as before,
but with the off-by-2 error corrected.)
2) Use 'mark' and 'space' or 'time_on', 'time_off' or something similar
and remove 'duty' (I *do* need flashing with duty cycle != 0.5)
3) Accept fractional frequency (as in my patch).
4) Provide both period and frequency as integers and accept that
long period flashs will come out as frequency '1' (but the period
would still need to be in ms, because periods about 1500ms are of
significant utility.)
John Bowler <jbowler@acm.org>
^ permalink raw reply [flat|nested] 2+ messages in thread
* RE: [PATCH 4/12] LED: Add LED Timer Trigger
2006-02-05 20:10 [PATCH 0/12] LED Class, Triggers and Drivers John Bowler
@ 2006-02-08 2:45 ` Richard Purdie
0 siblings, 0 replies; 2+ messages in thread
From: Richard Purdie @ 2006-02-08 2:45 UTC (permalink / raw)
To: Andrew Morton, LKML; +Cc: jbowler
As mentioned on LKML by John Bowler, using frequency and duty as
attributes is a bad idea as it restricts the potential usage of this
trigger. Therefore, instead export the two delay parameters directly.
Also remove unneeded locking and use class_get_devdata().
Signed-off-by: Richard Purdie <rpurdie@rpsys.net>
Index: linux-2.6.15/drivers/leds/ledtrig-timer.c
===================================================================
--- linux-2.6.15.orig/drivers/leds/ledtrig-timer.c 2006-02-08 01:42:26.000000000 +0000
+++ linux-2.6.15/drivers/leds/ledtrig-timer.c 2006-02-08 02:03:20.000000000 +0000
@@ -24,8 +24,6 @@
#include "leds.h"
struct timer_trig_data {
- unsigned long duty; /* duty cycle, as a percentage */
- unsigned long frequency; /* frequency of blinking, in Hz */
unsigned long delay_on; /* milliseconds on */
unsigned long delay_off; /* milliseconds off */
struct timer_list timer;
@@ -38,9 +36,8 @@
unsigned long brightness = LED_OFF;
unsigned long delay = timer_data->delay_off;
- write_lock(&led_cdev->lock);
-
- if (!timer_data->frequency) {
+ if (!timer_data->delay_on || !timer_data->delay_off) {
+ write_lock(&led_cdev->lock);
led_set_brightness(led_cdev, LED_OFF);
write_unlock(&led_cdev->lock);
return;
@@ -51,107 +48,73 @@
delay = timer_data->delay_on;
}
+ write_lock(&led_cdev->lock);
led_set_brightness(led_cdev, brightness);
+ write_unlock(&led_cdev->lock);
mod_timer(&timer_data->timer, jiffies + msecs_to_jiffies(delay));
- write_unlock(&led_cdev->lock);
}
-/* led_cdev write lock needs to be held */
-static int led_timer_setdata(struct led_classdev *led_cdev, unsigned long duty,
- unsigned long frequency)
+static ssize_t led_delay_on_show(struct class_device *dev, char *buf)
{
+ struct led_classdev *led_cdev = class_get_devdata(dev);
struct timer_trig_data *timer_data = led_cdev->trigger_data;
- if (frequency > 500)
- return -EINVAL;
-
- if (duty > 100)
- return -EINVAL;
-
- timer_data->duty = duty;
- timer_data->frequency = frequency;
- if (frequency != 0) {
- timer_data->delay_on = duty * 1000 / 50 / frequency / 2;
- timer_data->delay_off = (100 - duty)*1000 / 50 / frequency / 2;
- }
-
- mod_timer(&timer_data->timer, jiffies + 1);
-
- return 0;
-}
-
-static ssize_t led_duty_show(struct class_device *dev, char *buf)
-{
- struct led_classdev *led_cdev = dev->class_data;
- struct timer_trig_data *timer_data;
-
- read_lock(&led_cdev->lock);
- timer_data = led_cdev->trigger_data;
- sprintf(buf, "%lu\n", timer_data->duty);
- read_unlock(&led_cdev->lock);
+ sprintf(buf, "%lu\n", timer_data->delay_on);
return strlen(buf) + 1;
}
-static ssize_t led_duty_store(struct class_device *dev, const char *buf,
+static ssize_t led_delay_on_store(struct class_device *dev, const char *buf,
size_t size)
{
- struct led_classdev *led_cdev = dev->class_data;
- struct timer_trig_data *timer_data;
+ struct led_classdev *led_cdev = class_get_devdata(dev);
+ struct timer_trig_data *timer_data = led_cdev->trigger_data;
int ret = -EINVAL;
char *after;
-
unsigned long state = simple_strtoul(buf, &after, 10);
+
if (after - buf > 0) {
- write_lock(&led_cdev->lock);
- timer_data = led_cdev->trigger_data;
- ret = led_timer_setdata(led_cdev, state, timer_data->frequency);
- if (!ret)
- ret = after - buf;
- write_unlock(&led_cdev->lock);
+ timer_data->delay_on = state;
+ mod_timer(&timer_data->timer, jiffies + 1);
+ ret = after - buf;
}
return ret;
}
-static ssize_t led_frequency_show(struct class_device *dev, char *buf)
+static ssize_t led_delay_off_show(struct class_device *dev, char *buf)
{
- struct led_classdev *led_cdev = dev->class_data;
- struct timer_trig_data *timer_data;
+ struct led_classdev *led_cdev = class_get_devdata(dev);
+ struct timer_trig_data *timer_data = led_cdev->trigger_data;
- read_lock(&led_cdev->lock);
- timer_data = led_cdev->trigger_data;
- sprintf(buf, "%lu\n", timer_data->frequency);
- read_unlock(&led_cdev->lock);
+ sprintf(buf, "%lu\n", timer_data->delay_off);
return strlen(buf) + 1;
}
-static ssize_t led_frequency_store(struct class_device *dev, const char *buf,
+static ssize_t led_delay_off_store(struct class_device *dev, const char *buf,
size_t size)
{
- struct led_classdev *led_cdev = dev->class_data;
- struct timer_trig_data *timer_data;
+ struct led_classdev *led_cdev = class_get_devdata(dev);
+ struct timer_trig_data *timer_data = led_cdev->trigger_data;
int ret = -EINVAL;
char *after;
unsigned long state = simple_strtoul(buf, &after, 10);
if (after - buf > 0) {
- write_lock(&led_cdev->lock);
- timer_data = led_cdev->trigger_data;
- ret = led_timer_setdata(led_cdev, timer_data->duty, state);
- if (!ret)
- ret = after - buf;
- write_unlock(&led_cdev->lock);
+ timer_data->delay_off = state;
+ mod_timer(&timer_data->timer, jiffies + 1);
+ ret = after - buf;
}
return ret;
}
-static CLASS_DEVICE_ATTR(duty, 0644, led_duty_show, led_duty_store);
-static CLASS_DEVICE_ATTR(frequency, 0644, led_frequency_show,
- led_frequency_store);
+static CLASS_DEVICE_ATTR(delay_on, 0644, led_delay_on_show,
+ led_delay_on_store);
+static CLASS_DEVICE_ATTR(delay_off, 0644, led_delay_off_show,
+ led_delay_off_store);
static void timer_trig_activate(struct led_classdev *led_cdev)
{
@@ -167,11 +130,10 @@
timer_data->timer.function = led_timer_function;
timer_data->timer.data = (unsigned long) led_cdev;
- timer_data->duty = 50;
-
- class_device_create_file(led_cdev->class_dev, &class_device_attr_duty);
+ class_device_create_file(led_cdev->class_dev,
+ &class_device_attr_delay_on);
class_device_create_file(led_cdev->class_dev,
- &class_device_attr_frequency);
+ &class_device_attr_delay_off);
}
static void timer_trig_deactivate(struct led_classdev *led_cdev)
@@ -180,9 +142,9 @@
if (timer_data) {
class_device_remove_file(led_cdev->class_dev,
- &class_device_attr_duty);
+ &class_device_attr_delay_on);
class_device_remove_file(led_cdev->class_dev,
- &class_device_attr_frequency);
+ &class_device_attr_delay_off);
del_timer_sync(&timer_data->timer);
kfree(timer_data);
}
@@ -199,7 +161,7 @@
return led_trigger_register(&timer_led_trigger);
}
-static void __exit timer_trig_exit (void)
+static void __exit timer_trig_exit(void)
{
led_trigger_unregister(&timer_led_trigger);
}
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2006-02-08 2:46 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-02-05 15:52 [PATCH 4/12] LED: Add LED Timer Trigger Richard Purdie
2006-02-05 20:10 [PATCH 0/12] LED Class, Triggers and Drivers John Bowler
2006-02-08 2:45 ` [PATCH 4/12] LED: Add LED Timer Trigger Richard Purdie
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Powered by JetHome