blob: 328c64c0841cdda76f0d4ac7ed2b13cf801bec7b [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);
Richard Purdie6655c6f2006-03-31 02:31:07 -080034 int ret = -EINVAL;
35 char *after;
36 unsigned long state = simple_strtoul(buf, &after, 10);
Richard Purdie3dc7b822006-05-15 09:44:17 -070037 size_t count = after - buf;
Richard Purdie6655c6f2006-03-31 02:31:07 -080038
André Goddard Rosae7d28602009-12-14 18:01:06 -080039 if (isspace(*after))
Richard Purdie3dc7b822006-05-15 09:44:17 -070040 count++;
41
42 if (count == size) {
Johannes Berg5ada28b2010-11-11 14:05:21 -080043 led_blink_set(led_cdev, &state, &led_cdev->blink_delay_off);
Johan Hovold7a5caab2011-09-14 16:22:16 -070044 led_cdev->blink_delay_on = state;
Richard Purdie3dc7b822006-05-15 09:44:17 -070045 ret = count;
Richard Purdie6655c6f2006-03-31 02:31:07 -080046 }
47
48 return ret;
49}
50
Németh Márton4d404fd2008-03-09 20:59:57 +000051static ssize_t led_delay_off_show(struct device *dev,
Richard Purdief8a7c6f2007-07-08 23:19:31 +010052 struct device_attribute *attr, char *buf)
Richard Purdie6655c6f2006-03-31 02:31:07 -080053{
Richard Purdief8a7c6f2007-07-08 23:19:31 +010054 struct led_classdev *led_cdev = dev_get_drvdata(dev);
Richard Purdie6655c6f2006-03-31 02:31:07 -080055
Johannes Berg5ada28b2010-11-11 14:05:21 -080056 return sprintf(buf, "%lu\n", led_cdev->blink_delay_off);
Richard Purdie6655c6f2006-03-31 02:31:07 -080057}
58
Németh Márton4d404fd2008-03-09 20:59:57 +000059static ssize_t led_delay_off_store(struct device *dev,
Richard Purdief8a7c6f2007-07-08 23:19:31 +010060 struct device_attribute *attr, const char *buf, size_t size)
Richard Purdie6655c6f2006-03-31 02:31:07 -080061{
Richard Purdief8a7c6f2007-07-08 23:19:31 +010062 struct led_classdev *led_cdev = dev_get_drvdata(dev);
Richard Purdie6655c6f2006-03-31 02:31:07 -080063 int ret = -EINVAL;
64 char *after;
65 unsigned long state = simple_strtoul(buf, &after, 10);
Richard Purdie3dc7b822006-05-15 09:44:17 -070066 size_t count = after - buf;
Richard Purdie6655c6f2006-03-31 02:31:07 -080067
André Goddard Rosae7d28602009-12-14 18:01:06 -080068 if (isspace(*after))
Richard Purdie3dc7b822006-05-15 09:44:17 -070069 count++;
70
71 if (count == size) {
Johannes Berg5ada28b2010-11-11 14:05:21 -080072 led_blink_set(led_cdev, &led_cdev->blink_delay_on, &state);
Johan Hovold7a5caab2011-09-14 16:22:16 -070073 led_cdev->blink_delay_off = state;
Richard Purdie3dc7b822006-05-15 09:44:17 -070074 ret = count;
Richard Purdie6655c6f2006-03-31 02:31:07 -080075 }
76
77 return ret;
78}
79
Richard Purdief8a7c6f2007-07-08 23:19:31 +010080static DEVICE_ATTR(delay_on, 0644, led_delay_on_show, led_delay_on_store);
81static DEVICE_ATTR(delay_off, 0644, led_delay_off_show, led_delay_off_store);
Richard Purdie6655c6f2006-03-31 02:31:07 -080082
83static void timer_trig_activate(struct led_classdev *led_cdev)
84{
Jeff Garzik12fda162006-10-17 00:10:20 -070085 int rc;
Richard Purdie6655c6f2006-03-31 02:31:07 -080086
Johannes Berg5ada28b2010-11-11 14:05:21 -080087 led_cdev->trigger_data = NULL;
Richard Purdie6655c6f2006-03-31 02:31:07 -080088
Richard Purdief8a7c6f2007-07-08 23:19:31 +010089 rc = device_create_file(led_cdev->dev, &dev_attr_delay_on);
90 if (rc)
Johannes Berg5ada28b2010-11-11 14:05:21 -080091 return;
Richard Purdief8a7c6f2007-07-08 23:19:31 +010092 rc = device_create_file(led_cdev->dev, &dev_attr_delay_off);
93 if (rc)
94 goto err_out_delayon;
Jeff Garzik12fda162006-10-17 00:10:20 -070095
Esben Haabendalfff26f82011-05-24 17:13:22 -070096 led_blink_set(led_cdev, &led_cdev->blink_delay_on,
97 &led_cdev->blink_delay_off);
98
Johannes Berg5ada28b2010-11-11 14:05:21 -080099 led_cdev->trigger_data = (void *)1;
Márton Németh4c791412007-10-31 15:07:12 +0100100
Jeff Garzik12fda162006-10-17 00:10:20 -0700101 return;
102
103err_out_delayon:
Richard Purdief8a7c6f2007-07-08 23:19:31 +0100104 device_remove_file(led_cdev->dev, &dev_attr_delay_on);
Richard Purdie6655c6f2006-03-31 02:31:07 -0800105}
106
107static void timer_trig_deactivate(struct led_classdev *led_cdev)
108{
Johannes Berg5ada28b2010-11-11 14:05:21 -0800109 if (led_cdev->trigger_data) {
Richard Purdief8a7c6f2007-07-08 23:19:31 +0100110 device_remove_file(led_cdev->dev, &dev_attr_delay_on);
111 device_remove_file(led_cdev->dev, &dev_attr_delay_off);
Richard Purdie6655c6f2006-03-31 02:31:07 -0800112 }
Rodolfo Giomettice70a242009-01-08 19:50:15 +0000113
Johannes Berg5ada28b2010-11-11 14:05:21 -0800114 /* Stop blinking */
115 led_brightness_set(led_cdev, LED_OFF);
Richard Purdie6655c6f2006-03-31 02:31:07 -0800116}
117
118static struct led_trigger timer_led_trigger = {
119 .name = "timer",
120 .activate = timer_trig_activate,
121 .deactivate = timer_trig_deactivate,
122};
123
124static int __init timer_trig_init(void)
125{
126 return led_trigger_register(&timer_led_trigger);
127}
128
129static void __exit timer_trig_exit(void)
130{
131 led_trigger_unregister(&timer_led_trigger);
132}
133
134module_init(timer_trig_init);
135module_exit(timer_trig_exit);
136
137MODULE_AUTHOR("Richard Purdie <rpurdie@openedhand.com>");
138MODULE_DESCRIPTION("Timer LED trigger");
139MODULE_LICENSE("GPL");