On Tue, Nov 06, 2007 at 02:33:53AM -0800, akpm@linux-foundation.org wrote:
[mmotm.git] / drivers / leds / ledtrig-heartbeat.c
blobc1c1ea6f817b1eb7ddb7f2433cbdc917e20d164e
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/timer.h>
18 #include <linux/sched.h>
19 #include <linux/leds.h>
20 #include "leds.h"
22 struct heartbeat_trig_data {
23 unsigned int phase;
24 unsigned int period;
25 struct timer_list timer;
28 static void led_heartbeat_function(unsigned long data)
30 struct led_classdev *led_cdev = (struct led_classdev *) data;
31 struct heartbeat_trig_data *heartbeat_data = led_cdev->trigger_data;
32 unsigned long brightness = LED_OFF;
33 unsigned long delay = 0;
35 /* acts like an actual heart beat -- ie thump-thump-pause... */
36 switch (heartbeat_data->phase) {
37 case 0:
39 * The hyperbolic function below modifies the
40 * heartbeat period length in dependency of the
41 * current (1min) load. It goes through the points
42 * f(0)=1260, f(1)=860, f(5)=510, f(inf)->300.
44 heartbeat_data->period = 300 +
45 (6720 << FSHIFT) / (5 * avenrun[0] + (7 << FSHIFT));
46 heartbeat_data->period =
47 msecs_to_jiffies(heartbeat_data->period);
48 delay = msecs_to_jiffies(70);
49 heartbeat_data->phase++;
50 brightness = led_cdev->max_brightness;
51 break;
52 case 1:
53 delay = heartbeat_data->period / 4 - msecs_to_jiffies(70);
54 heartbeat_data->phase++;
55 break;
56 case 2:
57 delay = msecs_to_jiffies(70);
58 heartbeat_data->phase++;
59 brightness = led_cdev->max_brightness;
60 break;
61 default:
62 delay = heartbeat_data->period - heartbeat_data->period / 4 -
63 msecs_to_jiffies(70);
64 heartbeat_data->phase = 0;
65 break;
68 led_set_brightness(led_cdev, brightness);
69 mod_timer(&heartbeat_data->timer, jiffies + delay);
72 static void heartbeat_trig_activate(struct led_classdev *led_cdev)
74 struct heartbeat_trig_data *heartbeat_data;
76 heartbeat_data = kzalloc(sizeof(*heartbeat_data), GFP_KERNEL);
77 if (!heartbeat_data)
78 return;
80 led_cdev->trigger_data = heartbeat_data;
81 setup_timer(&heartbeat_data->timer,
82 led_heartbeat_function, (unsigned long) led_cdev);
83 heartbeat_data->phase = 0;
84 led_heartbeat_function(heartbeat_data->timer.data);
87 static void heartbeat_trig_deactivate(struct led_classdev *led_cdev)
89 struct heartbeat_trig_data *heartbeat_data = led_cdev->trigger_data;
91 if (heartbeat_data) {
92 del_timer_sync(&heartbeat_data->timer);
93 kfree(heartbeat_data);
97 static struct led_trigger heartbeat_led_trigger = {
98 .name = "heartbeat",
99 .activate = heartbeat_trig_activate,
100 .deactivate = heartbeat_trig_deactivate,
103 static int __init heartbeat_trig_init(void)
105 return led_trigger_register(&heartbeat_led_trigger);
108 static void __exit heartbeat_trig_exit(void)
110 led_trigger_unregister(&heartbeat_led_trigger);
113 module_init(heartbeat_trig_init);
114 module_exit(heartbeat_trig_exit);
116 MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>");
117 MODULE_DESCRIPTION("Heartbeat LED trigger");
118 MODULE_LICENSE("GPL");