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>
24 struct heartbeat_trig_data {
27 struct timer_list timer;
30 static void led_heartbeat_function(unsigned long data)
32 struct led_classdev *led_cdev = (struct led_classdev *) data;
33 struct heartbeat_trig_data *heartbeat_data = led_cdev->trigger_data;
34 unsigned long brightness = LED_OFF;
35 unsigned long delay = 0;
37 /* acts like an actual heart beat -- ie thump-thump-pause... */
38 switch (heartbeat_data->phase) {
41 * The hyperbolic function below modifies the
42 * heartbeat period length in dependency of the
43 * current (1min) load. It goes through the points
44 * f(0)=1260, f(1)=860, f(5)=510, f(inf)->300.
46 heartbeat_data->period = 300 +
47 (6720 << FSHIFT) / (5 * avenrun[0] + (7 << FSHIFT));
48 heartbeat_data->period =
49 msecs_to_jiffies(heartbeat_data->period);
50 delay = msecs_to_jiffies(70);
51 heartbeat_data->phase++;
52 brightness = led_cdev->max_brightness;
55 delay = heartbeat_data->period / 4 - msecs_to_jiffies(70);
56 heartbeat_data->phase++;
59 delay = msecs_to_jiffies(70);
60 heartbeat_data->phase++;
61 brightness = led_cdev->max_brightness;
64 delay = heartbeat_data->period - heartbeat_data->period / 4 -
66 heartbeat_data->phase = 0;
70 led_set_brightness(led_cdev, brightness);
71 mod_timer(&heartbeat_data->timer, jiffies + delay);
74 static void heartbeat_trig_activate(struct led_classdev *led_cdev)
76 struct heartbeat_trig_data *heartbeat_data;
78 heartbeat_data = kzalloc(sizeof(*heartbeat_data), GFP_KERNEL);
82 led_cdev->trigger_data = heartbeat_data;
83 setup_timer(&heartbeat_data->timer,
84 led_heartbeat_function, (unsigned long) led_cdev);
85 heartbeat_data->phase = 0;
86 led_heartbeat_function(heartbeat_data->timer.data);
87 led_cdev->activated = true;
90 static void heartbeat_trig_deactivate(struct led_classdev *led_cdev)
92 struct heartbeat_trig_data *heartbeat_data = led_cdev->trigger_data;
94 if (led_cdev->activated) {
95 del_timer_sync(&heartbeat_data->timer);
96 kfree(heartbeat_data);
97 led_cdev->activated = false;
101 static struct led_trigger heartbeat_led_trigger = {
103 .activate = heartbeat_trig_activate,
104 .deactivate = heartbeat_trig_deactivate,
107 static int heartbeat_reboot_notifier(struct notifier_block *nb,
108 unsigned long code, void *unused)
110 led_trigger_unregister(&heartbeat_led_trigger);
114 static struct notifier_block heartbeat_reboot_nb = {
115 .notifier_call = heartbeat_reboot_notifier,
118 static struct notifier_block heartbeat_panic_nb = {
119 .notifier_call = heartbeat_reboot_notifier,
122 static int __init heartbeat_trig_init(void)
124 int rc = led_trigger_register(&heartbeat_led_trigger);
127 atomic_notifier_chain_register(&panic_notifier_list,
128 &heartbeat_panic_nb);
129 register_reboot_notifier(&heartbeat_reboot_nb);
134 static void __exit heartbeat_trig_exit(void)
136 unregister_reboot_notifier(&heartbeat_reboot_nb);
137 atomic_notifier_chain_unregister(&panic_notifier_list,
138 &heartbeat_panic_nb);
139 led_trigger_unregister(&heartbeat_led_trigger);
142 module_init(heartbeat_trig_init);
143 module_exit(heartbeat_trig_exit);
145 MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>");
146 MODULE_DESCRIPTION("Heartbeat LED trigger");
147 MODULE_LICENSE("GPL");