18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * LED Heartbeat Trigger 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2006 Atsushi Nemoto <anemo@mba.ocn.ne.jp> 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * Based on Richard Purdie's ledtrig-timer.c and some arch's 88c2ecf20Sopenharmony_ci * CONFIG_HEARTBEAT code. 98c2ecf20Sopenharmony_ci */ 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ci#include <linux/module.h> 128c2ecf20Sopenharmony_ci#include <linux/kernel.h> 138c2ecf20Sopenharmony_ci#include <linux/init.h> 148c2ecf20Sopenharmony_ci#include <linux/slab.h> 158c2ecf20Sopenharmony_ci#include <linux/timer.h> 168c2ecf20Sopenharmony_ci#include <linux/sched.h> 178c2ecf20Sopenharmony_ci#include <linux/sched/loadavg.h> 188c2ecf20Sopenharmony_ci#include <linux/leds.h> 198c2ecf20Sopenharmony_ci#include <linux/reboot.h> 208c2ecf20Sopenharmony_ci#include "../leds.h" 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_cistatic int panic_heartbeats; 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_cistruct heartbeat_trig_data { 258c2ecf20Sopenharmony_ci struct led_classdev *led_cdev; 268c2ecf20Sopenharmony_ci unsigned int phase; 278c2ecf20Sopenharmony_ci unsigned int period; 288c2ecf20Sopenharmony_ci struct timer_list timer; 298c2ecf20Sopenharmony_ci unsigned int invert; 308c2ecf20Sopenharmony_ci}; 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_cistatic void led_heartbeat_function(struct timer_list *t) 338c2ecf20Sopenharmony_ci{ 348c2ecf20Sopenharmony_ci struct heartbeat_trig_data *heartbeat_data = 358c2ecf20Sopenharmony_ci from_timer(heartbeat_data, t, timer); 368c2ecf20Sopenharmony_ci struct led_classdev *led_cdev; 378c2ecf20Sopenharmony_ci unsigned long brightness = LED_OFF; 388c2ecf20Sopenharmony_ci unsigned long delay = 0; 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_ci led_cdev = heartbeat_data->led_cdev; 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_ci if (unlikely(panic_heartbeats)) { 438c2ecf20Sopenharmony_ci led_set_brightness_nosleep(led_cdev, LED_OFF); 448c2ecf20Sopenharmony_ci return; 458c2ecf20Sopenharmony_ci } 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci if (test_and_clear_bit(LED_BLINK_BRIGHTNESS_CHANGE, &led_cdev->work_flags)) 488c2ecf20Sopenharmony_ci led_cdev->blink_brightness = led_cdev->new_blink_brightness; 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci /* acts like an actual heart beat -- ie thump-thump-pause... */ 518c2ecf20Sopenharmony_ci switch (heartbeat_data->phase) { 528c2ecf20Sopenharmony_ci case 0: 538c2ecf20Sopenharmony_ci /* 548c2ecf20Sopenharmony_ci * The hyperbolic function below modifies the 558c2ecf20Sopenharmony_ci * heartbeat period length in dependency of the 568c2ecf20Sopenharmony_ci * current (1min) load. It goes through the points 578c2ecf20Sopenharmony_ci * f(0)=1260, f(1)=860, f(5)=510, f(inf)->300. 588c2ecf20Sopenharmony_ci */ 598c2ecf20Sopenharmony_ci heartbeat_data->period = 300 + 608c2ecf20Sopenharmony_ci (6720 << FSHIFT) / (5 * avenrun[0] + (7 << FSHIFT)); 618c2ecf20Sopenharmony_ci heartbeat_data->period = 628c2ecf20Sopenharmony_ci msecs_to_jiffies(heartbeat_data->period); 638c2ecf20Sopenharmony_ci delay = msecs_to_jiffies(70); 648c2ecf20Sopenharmony_ci heartbeat_data->phase++; 658c2ecf20Sopenharmony_ci if (!heartbeat_data->invert) 668c2ecf20Sopenharmony_ci brightness = led_cdev->blink_brightness; 678c2ecf20Sopenharmony_ci break; 688c2ecf20Sopenharmony_ci case 1: 698c2ecf20Sopenharmony_ci delay = heartbeat_data->period / 4 - msecs_to_jiffies(70); 708c2ecf20Sopenharmony_ci heartbeat_data->phase++; 718c2ecf20Sopenharmony_ci if (heartbeat_data->invert) 728c2ecf20Sopenharmony_ci brightness = led_cdev->blink_brightness; 738c2ecf20Sopenharmony_ci break; 748c2ecf20Sopenharmony_ci case 2: 758c2ecf20Sopenharmony_ci delay = msecs_to_jiffies(70); 768c2ecf20Sopenharmony_ci heartbeat_data->phase++; 778c2ecf20Sopenharmony_ci if (!heartbeat_data->invert) 788c2ecf20Sopenharmony_ci brightness = led_cdev->blink_brightness; 798c2ecf20Sopenharmony_ci break; 808c2ecf20Sopenharmony_ci default: 818c2ecf20Sopenharmony_ci delay = heartbeat_data->period - heartbeat_data->period / 4 - 828c2ecf20Sopenharmony_ci msecs_to_jiffies(70); 838c2ecf20Sopenharmony_ci heartbeat_data->phase = 0; 848c2ecf20Sopenharmony_ci if (heartbeat_data->invert) 858c2ecf20Sopenharmony_ci brightness = led_cdev->blink_brightness; 868c2ecf20Sopenharmony_ci break; 878c2ecf20Sopenharmony_ci } 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_ci led_set_brightness_nosleep(led_cdev, brightness); 908c2ecf20Sopenharmony_ci mod_timer(&heartbeat_data->timer, jiffies + delay); 918c2ecf20Sopenharmony_ci} 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_cistatic ssize_t led_invert_show(struct device *dev, 948c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 958c2ecf20Sopenharmony_ci{ 968c2ecf20Sopenharmony_ci struct heartbeat_trig_data *heartbeat_data = 978c2ecf20Sopenharmony_ci led_trigger_get_drvdata(dev); 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ci return sprintf(buf, "%u\n", heartbeat_data->invert); 1008c2ecf20Sopenharmony_ci} 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_cistatic ssize_t led_invert_store(struct device *dev, 1038c2ecf20Sopenharmony_ci struct device_attribute *attr, const char *buf, size_t size) 1048c2ecf20Sopenharmony_ci{ 1058c2ecf20Sopenharmony_ci struct heartbeat_trig_data *heartbeat_data = 1068c2ecf20Sopenharmony_ci led_trigger_get_drvdata(dev); 1078c2ecf20Sopenharmony_ci unsigned long state; 1088c2ecf20Sopenharmony_ci int ret; 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_ci ret = kstrtoul(buf, 0, &state); 1118c2ecf20Sopenharmony_ci if (ret) 1128c2ecf20Sopenharmony_ci return ret; 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci heartbeat_data->invert = !!state; 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ci return size; 1178c2ecf20Sopenharmony_ci} 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_cistatic DEVICE_ATTR(invert, 0644, led_invert_show, led_invert_store); 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_cistatic struct attribute *heartbeat_trig_attrs[] = { 1228c2ecf20Sopenharmony_ci &dev_attr_invert.attr, 1238c2ecf20Sopenharmony_ci NULL 1248c2ecf20Sopenharmony_ci}; 1258c2ecf20Sopenharmony_ciATTRIBUTE_GROUPS(heartbeat_trig); 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_cistatic int heartbeat_trig_activate(struct led_classdev *led_cdev) 1288c2ecf20Sopenharmony_ci{ 1298c2ecf20Sopenharmony_ci struct heartbeat_trig_data *heartbeat_data; 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ci heartbeat_data = kzalloc(sizeof(*heartbeat_data), GFP_KERNEL); 1328c2ecf20Sopenharmony_ci if (!heartbeat_data) 1338c2ecf20Sopenharmony_ci return -ENOMEM; 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci led_set_trigger_data(led_cdev, heartbeat_data); 1368c2ecf20Sopenharmony_ci heartbeat_data->led_cdev = led_cdev; 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_ci timer_setup(&heartbeat_data->timer, led_heartbeat_function, 0); 1398c2ecf20Sopenharmony_ci heartbeat_data->phase = 0; 1408c2ecf20Sopenharmony_ci if (!led_cdev->blink_brightness) 1418c2ecf20Sopenharmony_ci led_cdev->blink_brightness = led_cdev->max_brightness; 1428c2ecf20Sopenharmony_ci led_heartbeat_function(&heartbeat_data->timer); 1438c2ecf20Sopenharmony_ci set_bit(LED_BLINK_SW, &led_cdev->work_flags); 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_ci return 0; 1468c2ecf20Sopenharmony_ci} 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_cistatic void heartbeat_trig_deactivate(struct led_classdev *led_cdev) 1498c2ecf20Sopenharmony_ci{ 1508c2ecf20Sopenharmony_ci struct heartbeat_trig_data *heartbeat_data = 1518c2ecf20Sopenharmony_ci led_get_trigger_data(led_cdev); 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_ci del_timer_sync(&heartbeat_data->timer); 1548c2ecf20Sopenharmony_ci kfree(heartbeat_data); 1558c2ecf20Sopenharmony_ci clear_bit(LED_BLINK_SW, &led_cdev->work_flags); 1568c2ecf20Sopenharmony_ci} 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_cistatic struct led_trigger heartbeat_led_trigger = { 1598c2ecf20Sopenharmony_ci .name = "heartbeat", 1608c2ecf20Sopenharmony_ci .activate = heartbeat_trig_activate, 1618c2ecf20Sopenharmony_ci .deactivate = heartbeat_trig_deactivate, 1628c2ecf20Sopenharmony_ci .groups = heartbeat_trig_groups, 1638c2ecf20Sopenharmony_ci}; 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_cistatic int heartbeat_reboot_notifier(struct notifier_block *nb, 1668c2ecf20Sopenharmony_ci unsigned long code, void *unused) 1678c2ecf20Sopenharmony_ci{ 1688c2ecf20Sopenharmony_ci led_trigger_unregister(&heartbeat_led_trigger); 1698c2ecf20Sopenharmony_ci return NOTIFY_DONE; 1708c2ecf20Sopenharmony_ci} 1718c2ecf20Sopenharmony_ci 1728c2ecf20Sopenharmony_cistatic int heartbeat_panic_notifier(struct notifier_block *nb, 1738c2ecf20Sopenharmony_ci unsigned long code, void *unused) 1748c2ecf20Sopenharmony_ci{ 1758c2ecf20Sopenharmony_ci panic_heartbeats = 1; 1768c2ecf20Sopenharmony_ci return NOTIFY_DONE; 1778c2ecf20Sopenharmony_ci} 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_cistatic struct notifier_block heartbeat_reboot_nb = { 1808c2ecf20Sopenharmony_ci .notifier_call = heartbeat_reboot_notifier, 1818c2ecf20Sopenharmony_ci}; 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_cistatic struct notifier_block heartbeat_panic_nb = { 1848c2ecf20Sopenharmony_ci .notifier_call = heartbeat_panic_notifier, 1858c2ecf20Sopenharmony_ci}; 1868c2ecf20Sopenharmony_ci 1878c2ecf20Sopenharmony_cistatic int __init heartbeat_trig_init(void) 1888c2ecf20Sopenharmony_ci{ 1898c2ecf20Sopenharmony_ci int rc = led_trigger_register(&heartbeat_led_trigger); 1908c2ecf20Sopenharmony_ci 1918c2ecf20Sopenharmony_ci if (!rc) { 1928c2ecf20Sopenharmony_ci atomic_notifier_chain_register(&panic_notifier_list, 1938c2ecf20Sopenharmony_ci &heartbeat_panic_nb); 1948c2ecf20Sopenharmony_ci register_reboot_notifier(&heartbeat_reboot_nb); 1958c2ecf20Sopenharmony_ci } 1968c2ecf20Sopenharmony_ci return rc; 1978c2ecf20Sopenharmony_ci} 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_cistatic void __exit heartbeat_trig_exit(void) 2008c2ecf20Sopenharmony_ci{ 2018c2ecf20Sopenharmony_ci unregister_reboot_notifier(&heartbeat_reboot_nb); 2028c2ecf20Sopenharmony_ci atomic_notifier_chain_unregister(&panic_notifier_list, 2038c2ecf20Sopenharmony_ci &heartbeat_panic_nb); 2048c2ecf20Sopenharmony_ci led_trigger_unregister(&heartbeat_led_trigger); 2058c2ecf20Sopenharmony_ci} 2068c2ecf20Sopenharmony_ci 2078c2ecf20Sopenharmony_cimodule_init(heartbeat_trig_init); 2088c2ecf20Sopenharmony_cimodule_exit(heartbeat_trig_exit); 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_ciMODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>"); 2118c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Heartbeat LED trigger"); 2128c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 213