Merge tag 'iio-for-4.6a' of git://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio...
[deliverable/linux.git] / drivers / leds / trigger / ledtrig-heartbeat.c
1 /*
2 * LED Heartbeat Trigger
3 *
4 * Copyright (C) 2006 Atsushi Nemoto <anemo@mba.ocn.ne.jp>
5 *
6 * Based on Richard Purdie's ledtrig-timer.c and some arch's
7 * CONFIG_HEARTBEAT code.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 */
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/init.h>
17 #include <linux/slab.h>
18 #include <linux/timer.h>
19 #include <linux/sched.h>
20 #include <linux/leds.h>
21 #include <linux/reboot.h>
22 #include "../leds.h"
23
24 static int panic_heartbeats;
25
26 struct heartbeat_trig_data {
27 unsigned int phase;
28 unsigned int period;
29 struct timer_list timer;
30 unsigned int invert;
31 };
32
33 static void led_heartbeat_function(unsigned long data)
34 {
35 struct led_classdev *led_cdev = (struct led_classdev *) data;
36 struct heartbeat_trig_data *heartbeat_data = led_cdev->trigger_data;
37 unsigned long brightness = LED_OFF;
38 unsigned long delay = 0;
39
40 if (unlikely(panic_heartbeats)) {
41 led_set_brightness_nosleep(led_cdev, LED_OFF);
42 return;
43 }
44
45 /* acts like an actual heart beat -- ie thump-thump-pause... */
46 switch (heartbeat_data->phase) {
47 case 0:
48 /*
49 * The hyperbolic function below modifies the
50 * heartbeat period length in dependency of the
51 * current (1min) load. It goes through the points
52 * f(0)=1260, f(1)=860, f(5)=510, f(inf)->300.
53 */
54 heartbeat_data->period = 300 +
55 (6720 << FSHIFT) / (5 * avenrun[0] + (7 << FSHIFT));
56 heartbeat_data->period =
57 msecs_to_jiffies(heartbeat_data->period);
58 delay = msecs_to_jiffies(70);
59 heartbeat_data->phase++;
60 if (!heartbeat_data->invert)
61 brightness = led_cdev->max_brightness;
62 break;
63 case 1:
64 delay = heartbeat_data->period / 4 - msecs_to_jiffies(70);
65 heartbeat_data->phase++;
66 if (heartbeat_data->invert)
67 brightness = led_cdev->max_brightness;
68 break;
69 case 2:
70 delay = msecs_to_jiffies(70);
71 heartbeat_data->phase++;
72 if (!heartbeat_data->invert)
73 brightness = led_cdev->max_brightness;
74 break;
75 default:
76 delay = heartbeat_data->period - heartbeat_data->period / 4 -
77 msecs_to_jiffies(70);
78 heartbeat_data->phase = 0;
79 if (heartbeat_data->invert)
80 brightness = led_cdev->max_brightness;
81 break;
82 }
83
84 led_set_brightness_nosleep(led_cdev, brightness);
85 mod_timer(&heartbeat_data->timer, jiffies + delay);
86 }
87
88 static ssize_t led_invert_show(struct device *dev,
89 struct device_attribute *attr, char *buf)
90 {
91 struct led_classdev *led_cdev = dev_get_drvdata(dev);
92 struct heartbeat_trig_data *heartbeat_data = led_cdev->trigger_data;
93
94 return sprintf(buf, "%u\n", heartbeat_data->invert);
95 }
96
97 static ssize_t led_invert_store(struct device *dev,
98 struct device_attribute *attr, const char *buf, size_t size)
99 {
100 struct led_classdev *led_cdev = dev_get_drvdata(dev);
101 struct heartbeat_trig_data *heartbeat_data = led_cdev->trigger_data;
102 unsigned long state;
103 int ret;
104
105 ret = kstrtoul(buf, 0, &state);
106 if (ret)
107 return ret;
108
109 heartbeat_data->invert = !!state;
110
111 return size;
112 }
113
114 static DEVICE_ATTR(invert, 0644, led_invert_show, led_invert_store);
115
116 static void heartbeat_trig_activate(struct led_classdev *led_cdev)
117 {
118 struct heartbeat_trig_data *heartbeat_data;
119 int rc;
120
121 heartbeat_data = kzalloc(sizeof(*heartbeat_data), GFP_KERNEL);
122 if (!heartbeat_data)
123 return;
124
125 led_cdev->trigger_data = heartbeat_data;
126 rc = device_create_file(led_cdev->dev, &dev_attr_invert);
127 if (rc) {
128 kfree(led_cdev->trigger_data);
129 return;
130 }
131
132 setup_timer(&heartbeat_data->timer,
133 led_heartbeat_function, (unsigned long) led_cdev);
134 heartbeat_data->phase = 0;
135 led_heartbeat_function(heartbeat_data->timer.data);
136 led_cdev->activated = true;
137 }
138
139 static void heartbeat_trig_deactivate(struct led_classdev *led_cdev)
140 {
141 struct heartbeat_trig_data *heartbeat_data = led_cdev->trigger_data;
142
143 if (led_cdev->activated) {
144 del_timer_sync(&heartbeat_data->timer);
145 device_remove_file(led_cdev->dev, &dev_attr_invert);
146 kfree(heartbeat_data);
147 led_cdev->activated = false;
148 }
149 }
150
151 static struct led_trigger heartbeat_led_trigger = {
152 .name = "heartbeat",
153 .activate = heartbeat_trig_activate,
154 .deactivate = heartbeat_trig_deactivate,
155 };
156
157 static int heartbeat_reboot_notifier(struct notifier_block *nb,
158 unsigned long code, void *unused)
159 {
160 led_trigger_unregister(&heartbeat_led_trigger);
161 return NOTIFY_DONE;
162 }
163
164 static int heartbeat_panic_notifier(struct notifier_block *nb,
165 unsigned long code, void *unused)
166 {
167 panic_heartbeats = 1;
168 return NOTIFY_DONE;
169 }
170
171 static struct notifier_block heartbeat_reboot_nb = {
172 .notifier_call = heartbeat_reboot_notifier,
173 };
174
175 static struct notifier_block heartbeat_panic_nb = {
176 .notifier_call = heartbeat_panic_notifier,
177 };
178
179 static int __init heartbeat_trig_init(void)
180 {
181 int rc = led_trigger_register(&heartbeat_led_trigger);
182
183 if (!rc) {
184 atomic_notifier_chain_register(&panic_notifier_list,
185 &heartbeat_panic_nb);
186 register_reboot_notifier(&heartbeat_reboot_nb);
187 }
188 return rc;
189 }
190
191 static void __exit heartbeat_trig_exit(void)
192 {
193 unregister_reboot_notifier(&heartbeat_reboot_nb);
194 atomic_notifier_chain_unregister(&panic_notifier_list,
195 &heartbeat_panic_nb);
196 led_trigger_unregister(&heartbeat_led_trigger);
197 }
198
199 module_init(heartbeat_trig_init);
200 module_exit(heartbeat_trig_exit);
201
202 MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>");
203 MODULE_DESCRIPTION("Heartbeat LED trigger");
204 MODULE_LICENSE("GPL");
This page took 0.052741 seconds and 5 git commands to generate.