blob: 1edc7463ce8381141d1d23abc779e4c5af9aa19d [file] [log] [blame]
Atsushi Nemoto891c6682006-06-25 05:49:15 -07001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Atsushi Nemoto891c6682006-06-25 05:49:15 -070018#include <linux/timer.h>
19#include <linux/sched.h>
20#include <linux/leds.h>
Alexander Holler49dca5a2012-05-29 15:07:29 -070021#include <linux/reboot.h>
Atsushi Nemoto891c6682006-06-25 05:49:15 -070022#include "leds.h"
23
Alexander Hollerfb31fbe2012-07-03 14:35:47 +080024static int panic_heartbeats;
25
Atsushi Nemoto891c6682006-06-25 05:49:15 -070026struct heartbeat_trig_data {
27 unsigned int phase;
28 unsigned int period;
29 struct timer_list timer;
30};
31
32static void led_heartbeat_function(unsigned long data)
33{
34 struct led_classdev *led_cdev = (struct led_classdev *) data;
35 struct heartbeat_trig_data *heartbeat_data = led_cdev->trigger_data;
36 unsigned long brightness = LED_OFF;
37 unsigned long delay = 0;
38
Alexander Hollerfb31fbe2012-07-03 14:35:47 +080039 if (unlikely(panic_heartbeats)) {
40 led_set_brightness(led_cdev, LED_OFF);
41 return;
42 }
43
Atsushi Nemoto891c6682006-06-25 05:49:15 -070044 /* acts like an actual heart beat -- ie thump-thump-pause... */
45 switch (heartbeat_data->phase) {
46 case 0:
47 /*
48 * The hyperbolic function below modifies the
49 * heartbeat period length in dependency of the
50 * current (1min) load. It goes through the points
51 * f(0)=1260, f(1)=860, f(5)=510, f(inf)->300.
52 */
53 heartbeat_data->period = 300 +
54 (6720 << FSHIFT) / (5 * avenrun[0] + (7 << FSHIFT));
55 heartbeat_data->period =
56 msecs_to_jiffies(heartbeat_data->period);
57 delay = msecs_to_jiffies(70);
58 heartbeat_data->phase++;
Guennadi Liakhovetski1bd465e2009-01-10 18:54:39 +000059 brightness = led_cdev->max_brightness;
Atsushi Nemoto891c6682006-06-25 05:49:15 -070060 break;
61 case 1:
62 delay = heartbeat_data->period / 4 - msecs_to_jiffies(70);
63 heartbeat_data->phase++;
64 break;
65 case 2:
66 delay = msecs_to_jiffies(70);
67 heartbeat_data->phase++;
Guennadi Liakhovetski1bd465e2009-01-10 18:54:39 +000068 brightness = led_cdev->max_brightness;
Atsushi Nemoto891c6682006-06-25 05:49:15 -070069 break;
70 default:
71 delay = heartbeat_data->period - heartbeat_data->period / 4 -
72 msecs_to_jiffies(70);
73 heartbeat_data->phase = 0;
74 break;
75 }
76
Shuah Khan0da3e652012-06-13 10:01:37 +080077 __led_set_brightness(led_cdev, brightness);
Atsushi Nemoto891c6682006-06-25 05:49:15 -070078 mod_timer(&heartbeat_data->timer, jiffies + delay);
79}
80
81static void heartbeat_trig_activate(struct led_classdev *led_cdev)
82{
83 struct heartbeat_trig_data *heartbeat_data;
84
85 heartbeat_data = kzalloc(sizeof(*heartbeat_data), GFP_KERNEL);
86 if (!heartbeat_data)
87 return;
88
89 led_cdev->trigger_data = heartbeat_data;
90 setup_timer(&heartbeat_data->timer,
91 led_heartbeat_function, (unsigned long) led_cdev);
92 heartbeat_data->phase = 0;
93 led_heartbeat_function(heartbeat_data->timer.data);
Shuah Khan03c091e2012-05-29 15:07:28 -070094 led_cdev->activated = true;
Atsushi Nemoto891c6682006-06-25 05:49:15 -070095}
96
97static void heartbeat_trig_deactivate(struct led_classdev *led_cdev)
98{
99 struct heartbeat_trig_data *heartbeat_data = led_cdev->trigger_data;
100
Shuah Khan03c091e2012-05-29 15:07:28 -0700101 if (led_cdev->activated) {
Atsushi Nemoto891c6682006-06-25 05:49:15 -0700102 del_timer_sync(&heartbeat_data->timer);
103 kfree(heartbeat_data);
Shuah Khan03c091e2012-05-29 15:07:28 -0700104 led_cdev->activated = false;
Atsushi Nemoto891c6682006-06-25 05:49:15 -0700105 }
106}
107
108static struct led_trigger heartbeat_led_trigger = {
109 .name = "heartbeat",
110 .activate = heartbeat_trig_activate,
111 .deactivate = heartbeat_trig_deactivate,
112};
113
Alexander Holler49dca5a2012-05-29 15:07:29 -0700114static int heartbeat_reboot_notifier(struct notifier_block *nb,
115 unsigned long code, void *unused)
116{
117 led_trigger_unregister(&heartbeat_led_trigger);
118 return NOTIFY_DONE;
119}
120
Alexander Hollerfb31fbe2012-07-03 14:35:47 +0800121static int heartbeat_panic_notifier(struct notifier_block *nb,
122 unsigned long code, void *unused)
123{
124 panic_heartbeats = 1;
125 return NOTIFY_DONE;
126}
127
Alexander Holler49dca5a2012-05-29 15:07:29 -0700128static struct notifier_block heartbeat_reboot_nb = {
129 .notifier_call = heartbeat_reboot_notifier,
130};
131
132static struct notifier_block heartbeat_panic_nb = {
Alexander Hollerfb31fbe2012-07-03 14:35:47 +0800133 .notifier_call = heartbeat_panic_notifier,
Alexander Holler49dca5a2012-05-29 15:07:29 -0700134};
135
Atsushi Nemoto891c6682006-06-25 05:49:15 -0700136static int __init heartbeat_trig_init(void)
137{
Alexander Holler49dca5a2012-05-29 15:07:29 -0700138 int rc = led_trigger_register(&heartbeat_led_trigger);
139
140 if (!rc) {
141 atomic_notifier_chain_register(&panic_notifier_list,
142 &heartbeat_panic_nb);
143 register_reboot_notifier(&heartbeat_reboot_nb);
144 }
145 return rc;
Atsushi Nemoto891c6682006-06-25 05:49:15 -0700146}
147
148static void __exit heartbeat_trig_exit(void)
149{
Alexander Holler49dca5a2012-05-29 15:07:29 -0700150 unregister_reboot_notifier(&heartbeat_reboot_nb);
151 atomic_notifier_chain_unregister(&panic_notifier_list,
152 &heartbeat_panic_nb);
Atsushi Nemoto891c6682006-06-25 05:49:15 -0700153 led_trigger_unregister(&heartbeat_led_trigger);
154}
155
156module_init(heartbeat_trig_init);
157module_exit(heartbeat_trig_exit);
158
159MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>");
160MODULE_DESCRIPTION("Heartbeat LED trigger");
161MODULE_LICENSE("GPL");