Merge branch 'fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/evalenti/linux...
[linux/fpc-iii.git] / drivers / leds / trigger / ledtrig-heartbeat.c
blob410c39c62dc7f1c3bdfa3b5dd305bbef031aef25
1 /*
2 * LED Heartbeat Trigger
4 * Copyright (C) 2006 Atsushi Nemoto <anemo@mba.ocn.ne.jp>
6 * Based on Richard Purdie's ledtrig-timer.c and some arch's
7 * CONFIG_HEARTBEAT code.
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.
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"
24 static int panic_heartbeats;
26 struct heartbeat_trig_data {
27 unsigned int phase;
28 unsigned int period;
29 struct timer_list timer;
30 unsigned int invert;
33 static void led_heartbeat_function(unsigned long data)
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;
40 if (unlikely(panic_heartbeats)) {
41 led_set_brightness_nosleep(led_cdev, LED_OFF);
42 return;
45 /* acts like an actual heart beat -- ie thump-thump-pause... */
46 switch (heartbeat_data->phase) {
47 case 0:
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.
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;
84 led_set_brightness_nosleep(led_cdev, brightness);
85 mod_timer(&heartbeat_data->timer, jiffies + delay);
88 static ssize_t led_invert_show(struct device *dev,
89 struct device_attribute *attr, char *buf)
91 struct led_classdev *led_cdev = dev_get_drvdata(dev);
92 struct heartbeat_trig_data *heartbeat_data = led_cdev->trigger_data;
94 return sprintf(buf, "%u\n", heartbeat_data->invert);
97 static ssize_t led_invert_store(struct device *dev,
98 struct device_attribute *attr, const char *buf, size_t size)
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;
105 ret = kstrtoul(buf, 0, &state);
106 if (ret)
107 return ret;
109 heartbeat_data->invert = !!state;
111 return size;
114 static DEVICE_ATTR(invert, 0644, led_invert_show, led_invert_store);
116 static void heartbeat_trig_activate(struct led_classdev *led_cdev)
118 struct heartbeat_trig_data *heartbeat_data;
119 int rc;
121 heartbeat_data = kzalloc(sizeof(*heartbeat_data), GFP_KERNEL);
122 if (!heartbeat_data)
123 return;
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;
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;
139 static void heartbeat_trig_deactivate(struct led_classdev *led_cdev)
141 struct heartbeat_trig_data *heartbeat_data = led_cdev->trigger_data;
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;
151 static struct led_trigger heartbeat_led_trigger = {
152 .name = "heartbeat",
153 .activate = heartbeat_trig_activate,
154 .deactivate = heartbeat_trig_deactivate,
157 static int heartbeat_reboot_notifier(struct notifier_block *nb,
158 unsigned long code, void *unused)
160 led_trigger_unregister(&heartbeat_led_trigger);
161 return NOTIFY_DONE;
164 static int heartbeat_panic_notifier(struct notifier_block *nb,
165 unsigned long code, void *unused)
167 panic_heartbeats = 1;
168 return NOTIFY_DONE;
171 static struct notifier_block heartbeat_reboot_nb = {
172 .notifier_call = heartbeat_reboot_notifier,
175 static struct notifier_block heartbeat_panic_nb = {
176 .notifier_call = heartbeat_panic_notifier,
179 static int __init heartbeat_trig_init(void)
181 int rc = led_trigger_register(&heartbeat_led_trigger);
183 if (!rc) {
184 atomic_notifier_chain_register(&panic_notifier_list,
185 &heartbeat_panic_nb);
186 register_reboot_notifier(&heartbeat_reboot_nb);
188 return rc;
191 static void __exit heartbeat_trig_exit(void)
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);
199 module_init(heartbeat_trig_init);
200 module_exit(heartbeat_trig_exit);
202 MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>");
203 MODULE_DESCRIPTION("Heartbeat LED trigger");
204 MODULE_LICENSE("GPL");