blob: 4600f95f88422e3ebc8b9c6f2a4bb0c4f4068a60 [file] [log] [blame]
Atsushi Nemoto891c6682006-06-25 05:49:15 -07001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Atsushi Nemoto891c6682006-06-25 05:49:15 -070018#include <linux/timer.h>
19#include <linux/sched.h>
Ingo Molnarbadaff82017-02-08 08:45:17 +010020#include <linux/sched/loadavg.h>
Atsushi Nemoto891c6682006-06-25 05:49:15 -070021#include <linux/leds.h>
Alexander Holler49dca5a2012-05-29 15:07:29 -070022#include <linux/reboot.h>
Kim, Milof07fb522013-02-20 00:36:01 -080023#include "../leds.h"
Atsushi Nemoto891c6682006-06-25 05:49:15 -070024
Alexander Hollerfb31fbe2012-07-03 14:35:47 +080025static int panic_heartbeats;
26
Atsushi Nemoto891c6682006-06-25 05:49:15 -070027struct heartbeat_trig_data {
28 unsigned int phase;
29 unsigned int period;
30 struct timer_list timer;
Jiri Prchal1c7b9d02015-10-08 10:32:26 +020031 unsigned int invert;
Atsushi Nemoto891c6682006-06-25 05:49:15 -070032};
33
34static void led_heartbeat_function(unsigned long data)
35{
36 struct led_classdev *led_cdev = (struct led_classdev *) data;
37 struct heartbeat_trig_data *heartbeat_data = led_cdev->trigger_data;
38 unsigned long brightness = LED_OFF;
39 unsigned long delay = 0;
40
Alexander Hollerfb31fbe2012-07-03 14:35:47 +080041 if (unlikely(panic_heartbeats)) {
Jacek Anaszewski81fe8e52015-10-07 11:10:41 +020042 led_set_brightness_nosleep(led_cdev, LED_OFF);
Alexander Hollerfb31fbe2012-07-03 14:35:47 +080043 return;
44 }
45
Atsushi Nemoto891c6682006-06-25 05:49:15 -070046 /* acts like an actual heart beat -- ie thump-thump-pause... */
47 switch (heartbeat_data->phase) {
48 case 0:
49 /*
50 * The hyperbolic function below modifies the
51 * heartbeat period length in dependency of the
52 * current (1min) load. It goes through the points
53 * f(0)=1260, f(1)=860, f(5)=510, f(inf)->300.
54 */
55 heartbeat_data->period = 300 +
56 (6720 << FSHIFT) / (5 * avenrun[0] + (7 << FSHIFT));
57 heartbeat_data->period =
58 msecs_to_jiffies(heartbeat_data->period);
59 delay = msecs_to_jiffies(70);
60 heartbeat_data->phase++;
Jiri Prchal1c7b9d02015-10-08 10:32:26 +020061 if (!heartbeat_data->invert)
62 brightness = led_cdev->max_brightness;
Atsushi Nemoto891c6682006-06-25 05:49:15 -070063 break;
64 case 1:
65 delay = heartbeat_data->period / 4 - msecs_to_jiffies(70);
66 heartbeat_data->phase++;
Jiri Prchal1c7b9d02015-10-08 10:32:26 +020067 if (heartbeat_data->invert)
68 brightness = led_cdev->max_brightness;
Atsushi Nemoto891c6682006-06-25 05:49:15 -070069 break;
70 case 2:
71 delay = msecs_to_jiffies(70);
72 heartbeat_data->phase++;
Jiri Prchal1c7b9d02015-10-08 10:32:26 +020073 if (!heartbeat_data->invert)
74 brightness = led_cdev->max_brightness;
Atsushi Nemoto891c6682006-06-25 05:49:15 -070075 break;
76 default:
77 delay = heartbeat_data->period - heartbeat_data->period / 4 -
78 msecs_to_jiffies(70);
79 heartbeat_data->phase = 0;
Jiri Prchal1c7b9d02015-10-08 10:32:26 +020080 if (heartbeat_data->invert)
81 brightness = led_cdev->max_brightness;
Atsushi Nemoto891c6682006-06-25 05:49:15 -070082 break;
83 }
84
Jacek Anaszewski81fe8e52015-10-07 11:10:41 +020085 led_set_brightness_nosleep(led_cdev, brightness);
Atsushi Nemoto891c6682006-06-25 05:49:15 -070086 mod_timer(&heartbeat_data->timer, jiffies + delay);
87}
88
Jiri Prchal1c7b9d02015-10-08 10:32:26 +020089static ssize_t led_invert_show(struct device *dev,
90 struct device_attribute *attr, char *buf)
91{
92 struct led_classdev *led_cdev = dev_get_drvdata(dev);
93 struct heartbeat_trig_data *heartbeat_data = led_cdev->trigger_data;
94
95 return sprintf(buf, "%u\n", heartbeat_data->invert);
96}
97
98static ssize_t led_invert_store(struct device *dev,
99 struct device_attribute *attr, const char *buf, size_t size)
100{
101 struct led_classdev *led_cdev = dev_get_drvdata(dev);
102 struct heartbeat_trig_data *heartbeat_data = led_cdev->trigger_data;
103 unsigned long state;
104 int ret;
105
106 ret = kstrtoul(buf, 0, &state);
107 if (ret)
108 return ret;
109
110 heartbeat_data->invert = !!state;
111
112 return size;
113}
114
115static DEVICE_ATTR(invert, 0644, led_invert_show, led_invert_store);
116
Atsushi Nemoto891c6682006-06-25 05:49:15 -0700117static void heartbeat_trig_activate(struct led_classdev *led_cdev)
118{
119 struct heartbeat_trig_data *heartbeat_data;
Jiri Prchal1c7b9d02015-10-08 10:32:26 +0200120 int rc;
Atsushi Nemoto891c6682006-06-25 05:49:15 -0700121
122 heartbeat_data = kzalloc(sizeof(*heartbeat_data), GFP_KERNEL);
123 if (!heartbeat_data)
124 return;
125
126 led_cdev->trigger_data = heartbeat_data;
Jiri Prchal1c7b9d02015-10-08 10:32:26 +0200127 rc = device_create_file(led_cdev->dev, &dev_attr_invert);
128 if (rc) {
129 kfree(led_cdev->trigger_data);
130 return;
131 }
132
Atsushi Nemoto891c6682006-06-25 05:49:15 -0700133 setup_timer(&heartbeat_data->timer,
134 led_heartbeat_function, (unsigned long) led_cdev);
135 heartbeat_data->phase = 0;
136 led_heartbeat_function(heartbeat_data->timer.data);
Shuah Khan03c091e2012-05-29 15:07:28 -0700137 led_cdev->activated = true;
Atsushi Nemoto891c6682006-06-25 05:49:15 -0700138}
139
140static void heartbeat_trig_deactivate(struct led_classdev *led_cdev)
141{
142 struct heartbeat_trig_data *heartbeat_data = led_cdev->trigger_data;
143
Shuah Khan03c091e2012-05-29 15:07:28 -0700144 if (led_cdev->activated) {
Atsushi Nemoto891c6682006-06-25 05:49:15 -0700145 del_timer_sync(&heartbeat_data->timer);
Jiri Prchal1c7b9d02015-10-08 10:32:26 +0200146 device_remove_file(led_cdev->dev, &dev_attr_invert);
Atsushi Nemoto891c6682006-06-25 05:49:15 -0700147 kfree(heartbeat_data);
Shuah Khan03c091e2012-05-29 15:07:28 -0700148 led_cdev->activated = false;
Atsushi Nemoto891c6682006-06-25 05:49:15 -0700149 }
150}
151
152static struct led_trigger heartbeat_led_trigger = {
153 .name = "heartbeat",
154 .activate = heartbeat_trig_activate,
155 .deactivate = heartbeat_trig_deactivate,
156};
157
Alexander Holler49dca5a2012-05-29 15:07:29 -0700158static int heartbeat_reboot_notifier(struct notifier_block *nb,
159 unsigned long code, void *unused)
160{
161 led_trigger_unregister(&heartbeat_led_trigger);
162 return NOTIFY_DONE;
163}
164
Alexander Hollerfb31fbe2012-07-03 14:35:47 +0800165static int heartbeat_panic_notifier(struct notifier_block *nb,
166 unsigned long code, void *unused)
167{
168 panic_heartbeats = 1;
169 return NOTIFY_DONE;
170}
171
Alexander Holler49dca5a2012-05-29 15:07:29 -0700172static struct notifier_block heartbeat_reboot_nb = {
173 .notifier_call = heartbeat_reboot_notifier,
174};
175
176static struct notifier_block heartbeat_panic_nb = {
Alexander Hollerfb31fbe2012-07-03 14:35:47 +0800177 .notifier_call = heartbeat_panic_notifier,
Alexander Holler49dca5a2012-05-29 15:07:29 -0700178};
179
Atsushi Nemoto891c6682006-06-25 05:49:15 -0700180static int __init heartbeat_trig_init(void)
181{
Alexander Holler49dca5a2012-05-29 15:07:29 -0700182 int rc = led_trigger_register(&heartbeat_led_trigger);
183
184 if (!rc) {
185 atomic_notifier_chain_register(&panic_notifier_list,
186 &heartbeat_panic_nb);
187 register_reboot_notifier(&heartbeat_reboot_nb);
188 }
189 return rc;
Atsushi Nemoto891c6682006-06-25 05:49:15 -0700190}
191
192static void __exit heartbeat_trig_exit(void)
193{
Alexander Holler49dca5a2012-05-29 15:07:29 -0700194 unregister_reboot_notifier(&heartbeat_reboot_nb);
195 atomic_notifier_chain_unregister(&panic_notifier_list,
196 &heartbeat_panic_nb);
Atsushi Nemoto891c6682006-06-25 05:49:15 -0700197 led_trigger_unregister(&heartbeat_led_trigger);
198}
199
200module_init(heartbeat_trig_init);
201module_exit(heartbeat_trig_exit);
202
203MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>");
204MODULE_DESCRIPTION("Heartbeat LED trigger");
205MODULE_LICENSE("GPL");