blob: 8d09327b5719382da9da6b0f844232c699dacca2 [file] [log] [blame]
Richard Purdie6655c6f2006-03-31 02:31:07 -08001/*
2 * LED Kernel Timer Trigger
3 *
4 * Copyright 2005-2006 Openedhand Ltd.
5 *
6 * Author: Richard Purdie <rpurdie@openedhand.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 */
13
Richard Purdie6655c6f2006-03-31 02:31:07 -080014#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/init.h>
Richard Purdie6655c6f2006-03-31 02:31:07 -080017#include <linux/device.h>
Richard Purdie3dc7b822006-05-15 09:44:17 -070018#include <linux/ctype.h>
Richard Purdie6655c6f2006-03-31 02:31:07 -080019#include <linux/leds.h>
Richard Purdie6655c6f2006-03-31 02:31:07 -080020
Németh Márton4d404fd2008-03-09 20:59:57 +000021static ssize_t led_delay_on_show(struct device *dev,
Richard Purdief8a7c6f2007-07-08 23:19:31 +010022 struct device_attribute *attr, char *buf)
Richard Purdie6655c6f2006-03-31 02:31:07 -080023{
Richard Purdief8a7c6f2007-07-08 23:19:31 +010024 struct led_classdev *led_cdev = dev_get_drvdata(dev);
Richard Purdie6655c6f2006-03-31 02:31:07 -080025
Johannes Berg5ada28b2010-11-11 14:05:21 -080026 return sprintf(buf, "%lu\n", led_cdev->blink_delay_on);
Richard Purdie6655c6f2006-03-31 02:31:07 -080027}
28
Németh Márton4d404fd2008-03-09 20:59:57 +000029static ssize_t led_delay_on_store(struct device *dev,
Richard Purdief8a7c6f2007-07-08 23:19:31 +010030 struct device_attribute *attr, const char *buf, size_t size)
Richard Purdie6655c6f2006-03-31 02:31:07 -080031{
Richard Purdief8a7c6f2007-07-08 23:19:31 +010032 struct led_classdev *led_cdev = dev_get_drvdata(dev);
Shuah Khan872b86b2012-05-29 15:07:26 -070033 unsigned long state;
34 ssize_t ret = -EINVAL;
Richard Purdie6655c6f2006-03-31 02:31:07 -080035
Shuah Khan872b86b2012-05-29 15:07:26 -070036 ret = kstrtoul(buf, 10, &state);
37 if (ret)
38 return ret;
Richard Purdie3dc7b822006-05-15 09:44:17 -070039
Shuah Khan872b86b2012-05-29 15:07:26 -070040 led_blink_set(led_cdev, &state, &led_cdev->blink_delay_off);
41 led_cdev->blink_delay_on = state;
Richard Purdie6655c6f2006-03-31 02:31:07 -080042
Shuah Khan872b86b2012-05-29 15:07:26 -070043 return size;
Richard Purdie6655c6f2006-03-31 02:31:07 -080044}
45
Németh Márton4d404fd2008-03-09 20:59:57 +000046static ssize_t led_delay_off_show(struct device *dev,
Richard Purdief8a7c6f2007-07-08 23:19:31 +010047 struct device_attribute *attr, char *buf)
Richard Purdie6655c6f2006-03-31 02:31:07 -080048{
Richard Purdief8a7c6f2007-07-08 23:19:31 +010049 struct led_classdev *led_cdev = dev_get_drvdata(dev);
Richard Purdie6655c6f2006-03-31 02:31:07 -080050
Johannes Berg5ada28b2010-11-11 14:05:21 -080051 return sprintf(buf, "%lu\n", led_cdev->blink_delay_off);
Richard Purdie6655c6f2006-03-31 02:31:07 -080052}
53
Németh Márton4d404fd2008-03-09 20:59:57 +000054static ssize_t led_delay_off_store(struct device *dev,
Richard Purdief8a7c6f2007-07-08 23:19:31 +010055 struct device_attribute *attr, const char *buf, size_t size)
Richard Purdie6655c6f2006-03-31 02:31:07 -080056{
Richard Purdief8a7c6f2007-07-08 23:19:31 +010057 struct led_classdev *led_cdev = dev_get_drvdata(dev);
Shuah Khan872b86b2012-05-29 15:07:26 -070058 unsigned long state;
59 ssize_t ret = -EINVAL;
Richard Purdie6655c6f2006-03-31 02:31:07 -080060
Shuah Khan872b86b2012-05-29 15:07:26 -070061 ret = kstrtoul(buf, 10, &state);
62 if (ret)
63 return ret;
Richard Purdie3dc7b822006-05-15 09:44:17 -070064
Shuah Khan872b86b2012-05-29 15:07:26 -070065 led_blink_set(led_cdev, &led_cdev->blink_delay_on, &state);
66 led_cdev->blink_delay_off = state;
Richard Purdie6655c6f2006-03-31 02:31:07 -080067
Shuah Khan872b86b2012-05-29 15:07:26 -070068 return size;
Richard Purdie6655c6f2006-03-31 02:31:07 -080069}
70
Richard Purdief8a7c6f2007-07-08 23:19:31 +010071static DEVICE_ATTR(delay_on, 0644, led_delay_on_show, led_delay_on_store);
72static DEVICE_ATTR(delay_off, 0644, led_delay_off_show, led_delay_off_store);
Richard Purdie6655c6f2006-03-31 02:31:07 -080073
74static void timer_trig_activate(struct led_classdev *led_cdev)
75{
Jeff Garzik12fda162006-10-17 00:10:20 -070076 int rc;
Richard Purdie6655c6f2006-03-31 02:31:07 -080077
Johannes Berg5ada28b2010-11-11 14:05:21 -080078 led_cdev->trigger_data = NULL;
Richard Purdie6655c6f2006-03-31 02:31:07 -080079
Richard Purdief8a7c6f2007-07-08 23:19:31 +010080 rc = device_create_file(led_cdev->dev, &dev_attr_delay_on);
81 if (rc)
Johannes Berg5ada28b2010-11-11 14:05:21 -080082 return;
Richard Purdief8a7c6f2007-07-08 23:19:31 +010083 rc = device_create_file(led_cdev->dev, &dev_attr_delay_off);
84 if (rc)
85 goto err_out_delayon;
Jeff Garzik12fda162006-10-17 00:10:20 -070086
Esben Haabendalfff26f82011-05-24 17:13:22 -070087 led_blink_set(led_cdev, &led_cdev->blink_delay_on,
88 &led_cdev->blink_delay_off);
Shuah Khan13811872012-05-29 15:07:28 -070089 led_cdev->activated = true;
Márton Németh4c791412007-10-31 15:07:12 +010090
Jeff Garzik12fda162006-10-17 00:10:20 -070091 return;
92
93err_out_delayon:
Richard Purdief8a7c6f2007-07-08 23:19:31 +010094 device_remove_file(led_cdev->dev, &dev_attr_delay_on);
Richard Purdie6655c6f2006-03-31 02:31:07 -080095}
96
97static void timer_trig_deactivate(struct led_classdev *led_cdev)
98{
Shuah Khan13811872012-05-29 15:07:28 -070099 if (led_cdev->activated) {
Richard Purdief8a7c6f2007-07-08 23:19:31 +0100100 device_remove_file(led_cdev->dev, &dev_attr_delay_on);
101 device_remove_file(led_cdev->dev, &dev_attr_delay_off);
Shuah Khan13811872012-05-29 15:07:28 -0700102 led_cdev->activated = false;
Richard Purdie6655c6f2006-03-31 02:31:07 -0800103 }
Rodolfo Giomettice70a242009-01-08 19:50:15 +0000104
Johannes Berg5ada28b2010-11-11 14:05:21 -0800105 /* Stop blinking */
Shuah Khan19cd67e2012-06-14 04:34:30 +0800106 led_set_brightness(led_cdev, LED_OFF);
Richard Purdie6655c6f2006-03-31 02:31:07 -0800107}
108
109static struct led_trigger timer_led_trigger = {
110 .name = "timer",
111 .activate = timer_trig_activate,
112 .deactivate = timer_trig_deactivate,
113};
114
115static int __init timer_trig_init(void)
116{
117 return led_trigger_register(&timer_led_trigger);
118}
119
120static void __exit timer_trig_exit(void)
121{
122 led_trigger_unregister(&timer_led_trigger);
123}
124
125module_init(timer_trig_init);
126module_exit(timer_trig_exit);
127
128MODULE_AUTHOR("Richard Purdie <rpurdie@openedhand.com>");
129MODULE_DESCRIPTION("Timer LED trigger");
130MODULE_LICENSE("GPL");