powerpc/fadump: Do not allow hot-remove memory from fadump reserved area.
[linux/fpc-iii.git] / drivers / leds / trigger / ledtrig-heartbeat.c
blob7a2b12e19329478cc6b31dda347b29e18201b8b0
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/sched/loadavg.h>
21 #include <linux/leds.h>
22 #include <linux/reboot.h>
23 #include "../leds.h"
25 static int panic_heartbeats;
27 struct heartbeat_trig_data {
28 struct led_classdev *led_cdev;
29 unsigned int phase;
30 unsigned int period;
31 struct timer_list timer;
32 unsigned int invert;
35 static void led_heartbeat_function(struct timer_list *t)
37 struct heartbeat_trig_data *heartbeat_data =
38 from_timer(heartbeat_data, t, timer);
39 struct led_classdev *led_cdev;
40 unsigned long brightness = LED_OFF;
41 unsigned long delay = 0;
43 led_cdev = heartbeat_data->led_cdev;
45 if (unlikely(panic_heartbeats)) {
46 led_set_brightness_nosleep(led_cdev, LED_OFF);
47 return;
50 if (test_and_clear_bit(LED_BLINK_BRIGHTNESS_CHANGE, &led_cdev->work_flags))
51 led_cdev->blink_brightness = led_cdev->new_blink_brightness;
53 /* acts like an actual heart beat -- ie thump-thump-pause... */
54 switch (heartbeat_data->phase) {
55 case 0:
57 * The hyperbolic function below modifies the
58 * heartbeat period length in dependency of the
59 * current (1min) load. It goes through the points
60 * f(0)=1260, f(1)=860, f(5)=510, f(inf)->300.
62 heartbeat_data->period = 300 +
63 (6720 << FSHIFT) / (5 * avenrun[0] + (7 << FSHIFT));
64 heartbeat_data->period =
65 msecs_to_jiffies(heartbeat_data->period);
66 delay = msecs_to_jiffies(70);
67 heartbeat_data->phase++;
68 if (!heartbeat_data->invert)
69 brightness = led_cdev->blink_brightness;
70 break;
71 case 1:
72 delay = heartbeat_data->period / 4 - msecs_to_jiffies(70);
73 heartbeat_data->phase++;
74 if (heartbeat_data->invert)
75 brightness = led_cdev->blink_brightness;
76 break;
77 case 2:
78 delay = msecs_to_jiffies(70);
79 heartbeat_data->phase++;
80 if (!heartbeat_data->invert)
81 brightness = led_cdev->blink_brightness;
82 break;
83 default:
84 delay = heartbeat_data->period - heartbeat_data->period / 4 -
85 msecs_to_jiffies(70);
86 heartbeat_data->phase = 0;
87 if (heartbeat_data->invert)
88 brightness = led_cdev->blink_brightness;
89 break;
92 led_set_brightness_nosleep(led_cdev, brightness);
93 mod_timer(&heartbeat_data->timer, jiffies + delay);
96 static ssize_t led_invert_show(struct device *dev,
97 struct device_attribute *attr, char *buf)
99 struct heartbeat_trig_data *heartbeat_data =
100 led_trigger_get_drvdata(dev);
102 return sprintf(buf, "%u\n", heartbeat_data->invert);
105 static ssize_t led_invert_store(struct device *dev,
106 struct device_attribute *attr, const char *buf, size_t size)
108 struct heartbeat_trig_data *heartbeat_data =
109 led_trigger_get_drvdata(dev);
110 unsigned long state;
111 int ret;
113 ret = kstrtoul(buf, 0, &state);
114 if (ret)
115 return ret;
117 heartbeat_data->invert = !!state;
119 return size;
122 static DEVICE_ATTR(invert, 0644, led_invert_show, led_invert_store);
124 static struct attribute *heartbeat_trig_attrs[] = {
125 &dev_attr_invert.attr,
126 NULL
128 ATTRIBUTE_GROUPS(heartbeat_trig);
130 static int heartbeat_trig_activate(struct led_classdev *led_cdev)
132 struct heartbeat_trig_data *heartbeat_data;
134 heartbeat_data = kzalloc(sizeof(*heartbeat_data), GFP_KERNEL);
135 if (!heartbeat_data)
136 return -ENOMEM;
138 led_set_trigger_data(led_cdev, heartbeat_data);
139 heartbeat_data->led_cdev = led_cdev;
141 timer_setup(&heartbeat_data->timer, led_heartbeat_function, 0);
142 heartbeat_data->phase = 0;
143 if (!led_cdev->blink_brightness)
144 led_cdev->blink_brightness = led_cdev->max_brightness;
145 led_heartbeat_function(&heartbeat_data->timer);
146 set_bit(LED_BLINK_SW, &led_cdev->work_flags);
148 return 0;
151 static void heartbeat_trig_deactivate(struct led_classdev *led_cdev)
153 struct heartbeat_trig_data *heartbeat_data =
154 led_get_trigger_data(led_cdev);
156 del_timer_sync(&heartbeat_data->timer);
157 kfree(heartbeat_data);
158 clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
161 static struct led_trigger heartbeat_led_trigger = {
162 .name = "heartbeat",
163 .activate = heartbeat_trig_activate,
164 .deactivate = heartbeat_trig_deactivate,
165 .groups = heartbeat_trig_groups,
168 static int heartbeat_reboot_notifier(struct notifier_block *nb,
169 unsigned long code, void *unused)
171 led_trigger_unregister(&heartbeat_led_trigger);
172 return NOTIFY_DONE;
175 static int heartbeat_panic_notifier(struct notifier_block *nb,
176 unsigned long code, void *unused)
178 panic_heartbeats = 1;
179 return NOTIFY_DONE;
182 static struct notifier_block heartbeat_reboot_nb = {
183 .notifier_call = heartbeat_reboot_notifier,
186 static struct notifier_block heartbeat_panic_nb = {
187 .notifier_call = heartbeat_panic_notifier,
190 static int __init heartbeat_trig_init(void)
192 int rc = led_trigger_register(&heartbeat_led_trigger);
194 if (!rc) {
195 atomic_notifier_chain_register(&panic_notifier_list,
196 &heartbeat_panic_nb);
197 register_reboot_notifier(&heartbeat_reboot_nb);
199 return rc;
202 static void __exit heartbeat_trig_exit(void)
204 unregister_reboot_notifier(&heartbeat_reboot_nb);
205 atomic_notifier_chain_unregister(&panic_notifier_list,
206 &heartbeat_panic_nb);
207 led_trigger_unregister(&heartbeat_led_trigger);
210 module_init(heartbeat_trig_init);
211 module_exit(heartbeat_trig_exit);
213 MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>");
214 MODULE_DESCRIPTION("Heartbeat LED trigger");
215 MODULE_LICENSE("GPL v2");