Linux 4.16.11
[linux/fpc-iii.git] / drivers / leds / trigger / ledtrig-heartbeat.c
blobf0896de410b87c700c1200c32eb44f8d907bde02
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 led_classdev *led_cdev = dev_get_drvdata(dev);
100 struct heartbeat_trig_data *heartbeat_data = led_cdev->trigger_data;
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 led_classdev *led_cdev = dev_get_drvdata(dev);
109 struct heartbeat_trig_data *heartbeat_data = led_cdev->trigger_data;
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 void heartbeat_trig_activate(struct led_classdev *led_cdev)
126 struct heartbeat_trig_data *heartbeat_data;
127 int rc;
129 heartbeat_data = kzalloc(sizeof(*heartbeat_data), GFP_KERNEL);
130 if (!heartbeat_data)
131 return;
133 led_cdev->trigger_data = heartbeat_data;
134 heartbeat_data->led_cdev = led_cdev;
135 rc = device_create_file(led_cdev->dev, &dev_attr_invert);
136 if (rc) {
137 kfree(led_cdev->trigger_data);
138 return;
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);
147 led_cdev->activated = true;
150 static void heartbeat_trig_deactivate(struct led_classdev *led_cdev)
152 struct heartbeat_trig_data *heartbeat_data = led_cdev->trigger_data;
154 if (led_cdev->activated) {
155 del_timer_sync(&heartbeat_data->timer);
156 device_remove_file(led_cdev->dev, &dev_attr_invert);
157 kfree(heartbeat_data);
158 clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
159 led_cdev->activated = false;
163 static struct led_trigger heartbeat_led_trigger = {
164 .name = "heartbeat",
165 .activate = heartbeat_trig_activate,
166 .deactivate = heartbeat_trig_deactivate,
169 static int heartbeat_reboot_notifier(struct notifier_block *nb,
170 unsigned long code, void *unused)
172 led_trigger_unregister(&heartbeat_led_trigger);
173 return NOTIFY_DONE;
176 static int heartbeat_panic_notifier(struct notifier_block *nb,
177 unsigned long code, void *unused)
179 panic_heartbeats = 1;
180 return NOTIFY_DONE;
183 static struct notifier_block heartbeat_reboot_nb = {
184 .notifier_call = heartbeat_reboot_notifier,
187 static struct notifier_block heartbeat_panic_nb = {
188 .notifier_call = heartbeat_panic_notifier,
191 static int __init heartbeat_trig_init(void)
193 int rc = led_trigger_register(&heartbeat_led_trigger);
195 if (!rc) {
196 atomic_notifier_chain_register(&panic_notifier_list,
197 &heartbeat_panic_nb);
198 register_reboot_notifier(&heartbeat_reboot_nb);
200 return rc;
203 static void __exit heartbeat_trig_exit(void)
205 unregister_reboot_notifier(&heartbeat_reboot_nb);
206 atomic_notifier_chain_unregister(&panic_notifier_list,
207 &heartbeat_panic_nb);
208 led_trigger_unregister(&heartbeat_led_trigger);
211 module_init(heartbeat_trig_init);
212 module_exit(heartbeat_trig_exit);
214 MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>");
215 MODULE_DESCRIPTION("Heartbeat LED trigger");
216 MODULE_LICENSE("GPL");