Linux 3.3-rc6
[linux/fpc-iii.git] / drivers / leds / ledtrig-heartbeat.c
blob759c0bba4a8fa54d95b12d5434ad72a643a25f88
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/slab.h>
18 #include <linux/timer.h>
19 #include <linux/sched.h>
20 #include <linux/leds.h>
21 #include "leds.h"
23 struct heartbeat_trig_data {
24 unsigned int phase;
25 unsigned int period;
26 struct timer_list timer;
29 static void led_heartbeat_function(unsigned long data)
31 struct led_classdev *led_cdev = (struct led_classdev *) data;
32 struct heartbeat_trig_data *heartbeat_data = led_cdev->trigger_data;
33 unsigned long brightness = LED_OFF;
34 unsigned long delay = 0;
36 /* acts like an actual heart beat -- ie thump-thump-pause... */
37 switch (heartbeat_data->phase) {
38 case 0:
40 * The hyperbolic function below modifies the
41 * heartbeat period length in dependency of the
42 * current (1min) load. It goes through the points
43 * f(0)=1260, f(1)=860, f(5)=510, f(inf)->300.
45 heartbeat_data->period = 300 +
46 (6720 << FSHIFT) / (5 * avenrun[0] + (7 << FSHIFT));
47 heartbeat_data->period =
48 msecs_to_jiffies(heartbeat_data->period);
49 delay = msecs_to_jiffies(70);
50 heartbeat_data->phase++;
51 brightness = led_cdev->max_brightness;
52 break;
53 case 1:
54 delay = heartbeat_data->period / 4 - msecs_to_jiffies(70);
55 heartbeat_data->phase++;
56 break;
57 case 2:
58 delay = msecs_to_jiffies(70);
59 heartbeat_data->phase++;
60 brightness = led_cdev->max_brightness;
61 break;
62 default:
63 delay = heartbeat_data->period - heartbeat_data->period / 4 -
64 msecs_to_jiffies(70);
65 heartbeat_data->phase = 0;
66 break;
69 led_set_brightness(led_cdev, brightness);
70 mod_timer(&heartbeat_data->timer, jiffies + delay);
73 static void heartbeat_trig_activate(struct led_classdev *led_cdev)
75 struct heartbeat_trig_data *heartbeat_data;
77 heartbeat_data = kzalloc(sizeof(*heartbeat_data), GFP_KERNEL);
78 if (!heartbeat_data)
79 return;
81 led_cdev->trigger_data = heartbeat_data;
82 setup_timer(&heartbeat_data->timer,
83 led_heartbeat_function, (unsigned long) led_cdev);
84 heartbeat_data->phase = 0;
85 led_heartbeat_function(heartbeat_data->timer.data);
88 static void heartbeat_trig_deactivate(struct led_classdev *led_cdev)
90 struct heartbeat_trig_data *heartbeat_data = led_cdev->trigger_data;
92 if (heartbeat_data) {
93 del_timer_sync(&heartbeat_data->timer);
94 kfree(heartbeat_data);
98 static struct led_trigger heartbeat_led_trigger = {
99 .name = "heartbeat",
100 .activate = heartbeat_trig_activate,
101 .deactivate = heartbeat_trig_deactivate,
104 static int __init heartbeat_trig_init(void)
106 return led_trigger_register(&heartbeat_led_trigger);
109 static void __exit heartbeat_trig_exit(void)
111 led_trigger_unregister(&heartbeat_led_trigger);
114 module_init(heartbeat_trig_init);
115 module_exit(heartbeat_trig_exit);
117 MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>");
118 MODULE_DESCRIPTION("Heartbeat LED trigger");
119 MODULE_LICENSE("GPL");