2006-03-31 05:31:07 -05:00
|
|
|
/*
|
|
|
|
* 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/module.h>
|
2006-10-18 01:47:25 -04:00
|
|
|
#include <linux/jiffies.h>
|
2006-03-31 05:31:07 -05:00
|
|
|
#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>
|
2006-05-15 12:44:17 -04:00
|
|
|
#include <linux/ctype.h>
|
2006-03-31 05:31:07 -05:00
|
|
|
#include <linux/leds.h>
|
|
|
|
#include "leds.h"
|
|
|
|
|
|
|
|
struct timer_trig_data {
|
2008-03-09 16:54:37 -04:00
|
|
|
int brightness_on; /* LED brightness during "on" period.
|
|
|
|
* (LED_OFF < brightness_on <= LED_FULL)
|
|
|
|
*/
|
2006-03-31 05:31:07 -05:00
|
|
|
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;
|
2008-03-09 16:54:37 -04:00
|
|
|
unsigned long brightness;
|
|
|
|
unsigned long delay;
|
2006-03-31 05:31:07 -05:00
|
|
|
|
|
|
|
if (!timer_data->delay_on || !timer_data->delay_off) {
|
|
|
|
led_set_brightness(led_cdev, LED_OFF);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-03-09 16:54:37 -04:00
|
|
|
brightness = led_get_brightness(led_cdev);
|
|
|
|
if (!brightness) {
|
|
|
|
/* Time to switch the LED on. */
|
|
|
|
brightness = timer_data->brightness_on;
|
2006-03-31 05:31:07 -05:00
|
|
|
delay = timer_data->delay_on;
|
2008-03-09 16:54:37 -04:00
|
|
|
} else {
|
|
|
|
/* Store the current brightness value to be able
|
|
|
|
* to restore it when the delay_off period is over.
|
|
|
|
*/
|
|
|
|
timer_data->brightness_on = brightness;
|
|
|
|
brightness = LED_OFF;
|
|
|
|
delay = timer_data->delay_off;
|
2006-03-31 05:31:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
led_set_brightness(led_cdev, brightness);
|
|
|
|
|
|
|
|
mod_timer(&timer_data->timer, jiffies + msecs_to_jiffies(delay));
|
|
|
|
}
|
|
|
|
|
2008-03-09 16:59:57 -04:00
|
|
|
static ssize_t led_delay_on_show(struct device *dev,
|
2007-07-08 18:19:31 -04:00
|
|
|
struct device_attribute *attr, char *buf)
|
2006-03-31 05:31:07 -05:00
|
|
|
{
|
2007-07-08 18:19:31 -04:00
|
|
|
struct led_classdev *led_cdev = dev_get_drvdata(dev);
|
2006-03-31 05:31:07 -05:00
|
|
|
struct timer_trig_data *timer_data = led_cdev->trigger_data;
|
|
|
|
|
|
|
|
sprintf(buf, "%lu\n", timer_data->delay_on);
|
|
|
|
|
|
|
|
return strlen(buf) + 1;
|
|
|
|
}
|
|
|
|
|
2008-03-09 16:59:57 -04:00
|
|
|
static ssize_t led_delay_on_store(struct device *dev,
|
2007-07-08 18:19:31 -04:00
|
|
|
struct device_attribute *attr, const char *buf, size_t size)
|
2006-03-31 05:31:07 -05:00
|
|
|
{
|
2007-07-08 18:19:31 -04:00
|
|
|
struct led_classdev *led_cdev = dev_get_drvdata(dev);
|
2006-03-31 05:31:07 -05:00
|
|
|
struct timer_trig_data *timer_data = led_cdev->trigger_data;
|
|
|
|
int ret = -EINVAL;
|
|
|
|
char *after;
|
|
|
|
unsigned long state = simple_strtoul(buf, &after, 10);
|
2006-05-15 12:44:17 -04:00
|
|
|
size_t count = after - buf;
|
2006-03-31 05:31:07 -05:00
|
|
|
|
2006-05-15 12:44:17 -04:00
|
|
|
if (*after && isspace(*after))
|
|
|
|
count++;
|
|
|
|
|
|
|
|
if (count == size) {
|
2007-10-31 10:07:12 -04:00
|
|
|
if (timer_data->delay_on != state) {
|
|
|
|
/* the new value differs from the previous */
|
|
|
|
timer_data->delay_on = state;
|
|
|
|
|
|
|
|
/* deactivate previous settings */
|
|
|
|
del_timer_sync(&timer_data->timer);
|
|
|
|
|
|
|
|
/* try to activate hardware acceleration, if any */
|
|
|
|
if (!led_cdev->blink_set ||
|
|
|
|
led_cdev->blink_set(led_cdev,
|
2008-03-09 16:59:57 -04:00
|
|
|
&timer_data->delay_on, &timer_data->delay_off)) {
|
2007-10-31 10:07:12 -04:00
|
|
|
/* no hardware acceleration, blink via timer */
|
|
|
|
mod_timer(&timer_data->timer, jiffies + 1);
|
|
|
|
}
|
|
|
|
}
|
2006-05-15 12:44:17 -04:00
|
|
|
ret = count;
|
2006-03-31 05:31:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-03-09 16:59:57 -04:00
|
|
|
static ssize_t led_delay_off_show(struct device *dev,
|
2007-07-08 18:19:31 -04:00
|
|
|
struct device_attribute *attr, char *buf)
|
2006-03-31 05:31:07 -05:00
|
|
|
{
|
2007-07-08 18:19:31 -04:00
|
|
|
struct led_classdev *led_cdev = dev_get_drvdata(dev);
|
2006-03-31 05:31:07 -05:00
|
|
|
struct timer_trig_data *timer_data = led_cdev->trigger_data;
|
|
|
|
|
|
|
|
sprintf(buf, "%lu\n", timer_data->delay_off);
|
|
|
|
|
|
|
|
return strlen(buf) + 1;
|
|
|
|
}
|
|
|
|
|
2008-03-09 16:59:57 -04:00
|
|
|
static ssize_t led_delay_off_store(struct device *dev,
|
2007-07-08 18:19:31 -04:00
|
|
|
struct device_attribute *attr, const char *buf, size_t size)
|
2006-03-31 05:31:07 -05:00
|
|
|
{
|
2007-07-08 18:19:31 -04:00
|
|
|
struct led_classdev *led_cdev = dev_get_drvdata(dev);
|
2006-03-31 05:31:07 -05:00
|
|
|
struct timer_trig_data *timer_data = led_cdev->trigger_data;
|
|
|
|
int ret = -EINVAL;
|
|
|
|
char *after;
|
|
|
|
unsigned long state = simple_strtoul(buf, &after, 10);
|
2006-05-15 12:44:17 -04:00
|
|
|
size_t count = after - buf;
|
|
|
|
|
|
|
|
if (*after && isspace(*after))
|
|
|
|
count++;
|
2006-03-31 05:31:07 -05:00
|
|
|
|
2006-05-15 12:44:17 -04:00
|
|
|
if (count == size) {
|
2007-10-31 10:07:12 -04:00
|
|
|
if (timer_data->delay_off != state) {
|
|
|
|
/* the new value differs from the previous */
|
|
|
|
timer_data->delay_off = state;
|
|
|
|
|
|
|
|
/* deactivate previous settings */
|
|
|
|
del_timer_sync(&timer_data->timer);
|
|
|
|
|
|
|
|
/* try to activate hardware acceleration, if any */
|
|
|
|
if (!led_cdev->blink_set ||
|
|
|
|
led_cdev->blink_set(led_cdev,
|
2008-03-09 16:59:57 -04:00
|
|
|
&timer_data->delay_on, &timer_data->delay_off)) {
|
2007-10-31 10:07:12 -04:00
|
|
|
/* no hardware acceleration, blink via timer */
|
|
|
|
mod_timer(&timer_data->timer, jiffies + 1);
|
|
|
|
}
|
|
|
|
}
|
2006-05-15 12:44:17 -04:00
|
|
|
ret = count;
|
2006-03-31 05:31:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-07-08 18:19:31 -04:00
|
|
|
static DEVICE_ATTR(delay_on, 0644, led_delay_on_show, led_delay_on_store);
|
|
|
|
static DEVICE_ATTR(delay_off, 0644, led_delay_off_show, led_delay_off_store);
|
2006-03-31 05:31:07 -05:00
|
|
|
|
|
|
|
static void timer_trig_activate(struct led_classdev *led_cdev)
|
|
|
|
{
|
|
|
|
struct timer_trig_data *timer_data;
|
2006-10-17 03:10:20 -04:00
|
|
|
int rc;
|
2006-03-31 05:31:07 -05:00
|
|
|
|
|
|
|
timer_data = kzalloc(sizeof(struct timer_trig_data), GFP_KERNEL);
|
|
|
|
if (!timer_data)
|
|
|
|
return;
|
|
|
|
|
2008-03-09 16:54:37 -04:00
|
|
|
timer_data->brightness_on = led_get_brightness(led_cdev);
|
|
|
|
if (timer_data->brightness_on == LED_OFF)
|
|
|
|
timer_data->brightness_on = LED_FULL;
|
2006-03-31 05:31:07 -05:00
|
|
|
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;
|
|
|
|
|
2007-07-08 18:19:31 -04:00
|
|
|
rc = device_create_file(led_cdev->dev, &dev_attr_delay_on);
|
|
|
|
if (rc)
|
|
|
|
goto err_out;
|
|
|
|
rc = device_create_file(led_cdev->dev, &dev_attr_delay_off);
|
|
|
|
if (rc)
|
|
|
|
goto err_out_delayon;
|
2006-10-17 03:10:20 -04:00
|
|
|
|
2007-10-31 10:07:12 -04:00
|
|
|
/* If there is hardware support for blinking, start one
|
|
|
|
* user friendly blink rate chosen by the driver.
|
|
|
|
*/
|
|
|
|
if (led_cdev->blink_set)
|
|
|
|
led_cdev->blink_set(led_cdev,
|
|
|
|
&timer_data->delay_on, &timer_data->delay_off);
|
|
|
|
|
2006-10-17 03:10:20 -04:00
|
|
|
return;
|
|
|
|
|
|
|
|
err_out_delayon:
|
2007-07-08 18:19:31 -04:00
|
|
|
device_remove_file(led_cdev->dev, &dev_attr_delay_on);
|
2006-10-17 03:10:20 -04:00
|
|
|
err_out:
|
|
|
|
led_cdev->trigger_data = NULL;
|
|
|
|
kfree(timer_data);
|
2006-03-31 05:31:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void timer_trig_deactivate(struct led_classdev *led_cdev)
|
|
|
|
{
|
|
|
|
struct timer_trig_data *timer_data = led_cdev->trigger_data;
|
|
|
|
|
|
|
|
if (timer_data) {
|
2007-07-08 18:19:31 -04:00
|
|
|
device_remove_file(led_cdev->dev, &dev_attr_delay_on);
|
|
|
|
device_remove_file(led_cdev->dev, &dev_attr_delay_off);
|
2006-03-31 05:31:07 -05:00
|
|
|
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");
|