Merge tag 'omapdss-for-3.5-rc2' of git://gitorious.org/linux-omap-dss2/linux
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / leds / ledtrig-heartbeat.c
CommitLineData
891c668b
AN
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>
5a0e3ad6 17#include <linux/slab.h>
891c668b
AN
18#include <linux/timer.h>
19#include <linux/sched.h>
20#include <linux/leds.h>
49dca5ae 21#include <linux/reboot.h>
891c668b
AN
22#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++;
1bd465e6 52 brightness = led_cdev->max_brightness;
891c668b
AN
53 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++;
1bd465e6 61 brightness = led_cdev->max_brightness;
891c668b
AN
62 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);
03c091e5 87 led_cdev->activated = true;
891c668b
AN
88}
89
90static void heartbeat_trig_deactivate(struct led_classdev *led_cdev)
91{
92 struct heartbeat_trig_data *heartbeat_data = led_cdev->trigger_data;
93
03c091e5 94 if (led_cdev->activated) {
891c668b
AN
95 del_timer_sync(&heartbeat_data->timer);
96 kfree(heartbeat_data);
03c091e5 97 led_cdev->activated = false;
891c668b
AN
98 }
99}
100
101static struct led_trigger heartbeat_led_trigger = {
102 .name = "heartbeat",
103 .activate = heartbeat_trig_activate,
104 .deactivate = heartbeat_trig_deactivate,
105};
106
49dca5ae
AH
107static 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
891c668b
AN
122static int __init heartbeat_trig_init(void)
123{
49dca5ae
AH
124 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;
891c668b
AN
132}
133
134static void __exit heartbeat_trig_exit(void)
135{
49dca5ae
AH
136 unregister_reboot_notifier(&heartbeat_reboot_nb);
137 atomic_notifier_chain_unregister(&panic_notifier_list,
138 &heartbeat_panic_nb);
891c668b
AN
139 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");