blob: f774d05922042a69fd470b9ede566423a736c846 [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>
20#include "leds.h"
21
Németh Márton4d404fd2008-03-09 20:59:57 +000022static ssize_t led_delay_on_show(struct device *dev,
Richard Purdief8a7c6f2007-07-08 23:19:31 +010023 struct device_attribute *attr, char *buf)
Richard Purdie6655c6f2006-03-31 02:31:07 -080024{
Richard Purdief8a7c6f2007-07-08 23:19:31 +010025 struct led_classdev *led_cdev = dev_get_drvdata(dev);
Richard Purdie6655c6f2006-03-31 02:31:07 -080026
Johannes Berg5ada28b2010-11-11 14:05:21 -080027 return sprintf(buf, "%lu\n", led_cdev->blink_delay_on);
Richard Purdie6655c6f2006-03-31 02:31:07 -080028}
29
Németh Márton4d404fd2008-03-09 20:59:57 +000030static ssize_t led_delay_on_store(struct device *dev,
Richard Purdief8a7c6f2007-07-08 23:19:31 +010031 struct device_attribute *attr, const char *buf, size_t size)
Richard Purdie6655c6f2006-03-31 02:31:07 -080032{
Richard Purdief8a7c6f2007-07-08 23:19:31 +010033 struct led_classdev *led_cdev = dev_get_drvdata(dev);
Shuah Khan872b86b2012-05-29 15:07:26 -070034 unsigned long state;
35 ssize_t ret = -EINVAL;
Richard Purdie6655c6f2006-03-31 02:31:07 -080036
Shuah Khan872b86b2012-05-29 15:07:26 -070037 ret = kstrtoul(buf, 10, &state);
38 if (ret)
39 return ret;
Richard Purdie3dc7b822006-05-15 09:44:17 -070040
Shuah Khan872b86b2012-05-29 15:07:26 -070041 led_blink_set(led_cdev, &state, &led_cdev->blink_delay_off);
42 led_cdev->blink_delay_on = state;
Richard Purdie6655c6f2006-03-31 02:31:07 -080043
Shuah Khan872b86b2012-05-29 15:07:26 -070044 return size;
Richard Purdie6655c6f2006-03-31 02:31:07 -080045}
46
Németh Márton4d404fd2008-03-09 20:59:57 +000047static ssize_t led_delay_off_show(struct device *dev,
Richard Purdief8a7c6f2007-07-08 23:19:31 +010048 struct device_attribute *attr, char *buf)
Richard Purdie6655c6f2006-03-31 02:31:07 -080049{
Richard Purdief8a7c6f2007-07-08 23:19:31 +010050 struct led_classdev *led_cdev = dev_get_drvdata(dev);
Richard Purdie6655c6f2006-03-31 02:31:07 -080051
Johannes Berg5ada28b2010-11-11 14:05:21 -080052 return sprintf(buf, "%lu\n", led_cdev->blink_delay_off);
Richard Purdie6655c6f2006-03-31 02:31:07 -080053}
54
Németh Márton4d404fd2008-03-09 20:59:57 +000055static ssize_t led_delay_off_store(struct device *dev,
Richard Purdief8a7c6f2007-07-08 23:19:31 +010056 struct device_attribute *attr, const char *buf, size_t size)
Richard Purdie6655c6f2006-03-31 02:31:07 -080057{
Richard Purdief8a7c6f2007-07-08 23:19:31 +010058 struct led_classdev *led_cdev = dev_get_drvdata(dev);
Shuah Khan872b86b2012-05-29 15:07:26 -070059 unsigned long state;
60 ssize_t ret = -EINVAL;
Richard Purdie6655c6f2006-03-31 02:31:07 -080061
Shuah Khan872b86b2012-05-29 15:07:26 -070062 ret = kstrtoul(buf, 10, &state);
63 if (ret)
64 return ret;
Richard Purdie3dc7b822006-05-15 09:44:17 -070065
Shuah Khan872b86b2012-05-29 15:07:26 -070066 led_blink_set(led_cdev, &led_cdev->blink_delay_on, &state);
67 led_cdev->blink_delay_off = state;
Richard Purdie6655c6f2006-03-31 02:31:07 -080068
Shuah Khan872b86b2012-05-29 15:07:26 -070069 return size;
Richard Purdie6655c6f2006-03-31 02:31:07 -080070}
71
Richard Purdief8a7c6f2007-07-08 23:19:31 +010072static DEVICE_ATTR(delay_on, 0644, led_delay_on_show, led_delay_on_store);
73static DEVICE_ATTR(delay_off, 0644, led_delay_off_show, led_delay_off_store);
Richard Purdie6655c6f2006-03-31 02:31:07 -080074
75static void timer_trig_activate(struct led_classdev *led_cdev)
76{
Jeff Garzik12fda162006-10-17 00:10:20 -070077 int rc;
Richard Purdie6655c6f2006-03-31 02:31:07 -080078
Johannes Berg5ada28b2010-11-11 14:05:21 -080079 led_cdev->trigger_data = NULL;
Richard Purdie6655c6f2006-03-31 02:31:07 -080080
Richard Purdief8a7c6f2007-07-08 23:19:31 +010081 rc = device_create_file(led_cdev->dev, &dev_attr_delay_on);
82 if (rc)
Johannes Berg5ada28b2010-11-11 14:05:21 -080083 return;
Richard Purdief8a7c6f2007-07-08 23:19:31 +010084 rc = device_create_file(led_cdev->dev, &dev_attr_delay_off);
85 if (rc)
86 goto err_out_delayon;
Jeff Garzik12fda162006-10-17 00:10:20 -070087
Esben Haabendalfff26f82011-05-24 17:13:22 -070088 led_blink_set(led_cdev, &led_cdev->blink_delay_on,
89 &led_cdev->blink_delay_off);
Shuah Khan13811872012-05-29 15:07:28 -070090 led_cdev->activated = true;
Márton Németh4c791412007-10-31 15:07:12 +010091
Jeff Garzik12fda162006-10-17 00:10:20 -070092 return;
93
94err_out_delayon:
Richard Purdief8a7c6f2007-07-08 23:19:31 +010095 device_remove_file(led_cdev->dev, &dev_attr_delay_on);
Richard Purdie6655c6f2006-03-31 02:31:07 -080096}
97
98static void timer_trig_deactivate(struct led_classdev *led_cdev)
99{
Shuah Khan13811872012-05-29 15:07:28 -0700100 if (led_cdev->activated) {
Richard Purdief8a7c6f2007-07-08 23:19:31 +0100101 device_remove_file(led_cdev->dev, &dev_attr_delay_on);
102 device_remove_file(led_cdev->dev, &dev_attr_delay_off);
Shuah Khan13811872012-05-29 15:07:28 -0700103 led_cdev->activated = false;
Richard Purdie6655c6f2006-03-31 02:31:07 -0800104 }
Rodolfo Giomettice70a242009-01-08 19:50:15 +0000105
Johannes Berg5ada28b2010-11-11 14:05:21 -0800106 /* Stop blinking */
Shuah Khan19cd67e2012-06-14 04:34:30 +0800107 led_set_brightness(led_cdev, LED_OFF);
Richard Purdie6655c6f2006-03-31 02:31:07 -0800108}
109
110static struct led_trigger timer_led_trigger = {
111 .name = "timer",
112 .activate = timer_trig_activate,
113 .deactivate = timer_trig_deactivate,
114};
115
116static int __init timer_trig_init(void)
117{
118 return led_trigger_register(&timer_led_trigger);
119}
120
121static void __exit timer_trig_exit(void)
122{
123 led_trigger_unregister(&timer_led_trigger);
124}
125
126module_init(timer_trig_init);
127module_exit(timer_trig_exit);
128
129MODULE_AUTHOR("Richard Purdie <rpurdie@openedhand.com>");
130MODULE_DESCRIPTION("Timer LED trigger");
131MODULE_LICENSE("GPL");