blob: 41dc76db43118a347e4a8a06923b0fd076a12dc3 [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
24struct heartbeat_trig_data {
25 unsigned int phase;
26 unsigned int period;
27 struct timer_list timer;
28};
29
30static void led_heartbeat_function(unsigned long data)
31{
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;
36
37 /* acts like an actual heart beat -- ie thump-thump-pause... */
38 switch (heartbeat_data->phase) {
39 case 0:
40 /*
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.
45 */
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++;
Guennadi Liakhovetski1bd465e2009-01-10 18:54:39 +000052 brightness = led_cdev->max_brightness;
Atsushi Nemoto891c6682006-06-25 05:49:15 -070053 break;
54 case 1:
55 delay = heartbeat_data->period / 4 - msecs_to_jiffies(70);
56 heartbeat_data->phase++;
57 break;
58 case 2:
59 delay = msecs_to_jiffies(70);
60 heartbeat_data->phase++;
Guennadi Liakhovetski1bd465e2009-01-10 18:54:39 +000061 brightness = led_cdev->max_brightness;
Atsushi Nemoto891c6682006-06-25 05:49:15 -070062 break;
63 default:
64 delay = heartbeat_data->period - heartbeat_data->period / 4 -
65 msecs_to_jiffies(70);
66 heartbeat_data->phase = 0;
67 break;
68 }
69
70 led_set_brightness(led_cdev, brightness);
71 mod_timer(&heartbeat_data->timer, jiffies + delay);
72}
73
74static void heartbeat_trig_activate(struct led_classdev *led_cdev)
75{
76 struct heartbeat_trig_data *heartbeat_data;
77
78 heartbeat_data = kzalloc(sizeof(*heartbeat_data), GFP_KERNEL);
79 if (!heartbeat_data)
80 return;
81
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);
Shuah Khan03c091e2012-05-29 15:07:28 -070087 led_cdev->activated = true;
Atsushi Nemoto891c6682006-06-25 05:49:15 -070088}
89
90static void heartbeat_trig_deactivate(struct led_classdev *led_cdev)
91{
92 struct heartbeat_trig_data *heartbeat_data = led_cdev->trigger_data;
93
Shuah Khan03c091e2012-05-29 15:07:28 -070094 if (led_cdev->activated) {
Atsushi Nemoto891c6682006-06-25 05:49:15 -070095 del_timer_sync(&heartbeat_data->timer);
96 kfree(heartbeat_data);
Shuah Khan03c091e2012-05-29 15:07:28 -070097 led_cdev->activated = false;
Atsushi Nemoto891c6682006-06-25 05:49:15 -070098 }
99}
100
101static struct led_trigger heartbeat_led_trigger = {
102 .name = "heartbeat",
103 .activate = heartbeat_trig_activate,
104 .deactivate = heartbeat_trig_deactivate,
105};
106
Alexander Holler49dca5a2012-05-29 15:07:29 -0700107static int heartbeat_reboot_notifier(struct notifier_block *nb,
108 unsigned long code, void *unused)
109{
110 led_trigger_unregister(&heartbeat_led_trigger);
111 return NOTIFY_DONE;
112}
113
114static struct notifier_block heartbeat_reboot_nb = {
115 .notifier_call = heartbeat_reboot_notifier,
116};
117
118static struct notifier_block heartbeat_panic_nb = {
119 .notifier_call = heartbeat_reboot_notifier,
120};
121
Atsushi Nemoto891c6682006-06-25 05:49:15 -0700122static int __init heartbeat_trig_init(void)
123{
Alexander Holler49dca5a2012-05-29 15:07:29 -0700124 int rc = led_trigger_register(&heartbeat_led_trigger);
125
126 if (!rc) {
127 atomic_notifier_chain_register(&panic_notifier_list,
128 &heartbeat_panic_nb);
129 register_reboot_notifier(&heartbeat_reboot_nb);
130 }
131 return rc;
Atsushi Nemoto891c6682006-06-25 05:49:15 -0700132}
133
134static void __exit heartbeat_trig_exit(void)
135{
Alexander Holler49dca5a2012-05-29 15:07:29 -0700136 unregister_reboot_notifier(&heartbeat_reboot_nb);
137 atomic_notifier_chain_unregister(&panic_notifier_list,
138 &heartbeat_panic_nb);
Atsushi Nemoto891c6682006-06-25 05:49:15 -0700139 led_trigger_unregister(&heartbeat_led_trigger);
140}
141
142module_init(heartbeat_trig_init);
143module_exit(heartbeat_trig_exit);
144
145MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>");
146MODULE_DESCRIPTION("Heartbeat LED trigger");
147MODULE_LICENSE("GPL");