ledtrig-heartbeat.c 3.1 KB

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