ledtrig-cpu.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * ledtrig-cpu.c - LED trigger based on CPU activity
  3. *
  4. * This LED trigger will be registered for each possible CPU and named as
  5. * cpu0, cpu1, cpu2, cpu3, etc.
  6. *
  7. * It can be bound to any LED just like other triggers using either a
  8. * board file or via sysfs interface.
  9. *
  10. * An API named ledtrig_cpu is exported for any user, who want to add CPU
  11. * activity indication in their code
  12. *
  13. * Copyright 2011 Linus Walleij <linus.walleij@linaro.org>
  14. * Copyright 2011 - 2012 Bryan Wu <bryan.wu@canonical.com>
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License version 2 as
  18. * published by the Free Software Foundation.
  19. *
  20. */
  21. #include <linux/module.h>
  22. #include <linux/kernel.h>
  23. #include <linux/init.h>
  24. #include <linux/slab.h>
  25. #include <linux/percpu.h>
  26. #include <linux/syscore_ops.h>
  27. #include <linux/rwsem.h>
  28. #include <linux/cpu.h>
  29. #include "../leds.h"
  30. #define MAX_NAME_LEN 8
  31. struct led_trigger_cpu {
  32. char name[MAX_NAME_LEN];
  33. struct led_trigger *_trig;
  34. };
  35. static DEFINE_PER_CPU(struct led_trigger_cpu, cpu_trig);
  36. /**
  37. * ledtrig_cpu - emit a CPU event as a trigger
  38. * @evt: CPU event to be emitted
  39. *
  40. * Emit a CPU event on a CPU core, which will trigger a
  41. * binded LED to turn on or turn off.
  42. */
  43. void ledtrig_cpu(enum cpu_led_event ledevt)
  44. {
  45. struct led_trigger_cpu *trig = this_cpu_ptr(&cpu_trig);
  46. /* Locate the correct CPU LED */
  47. switch (ledevt) {
  48. case CPU_LED_IDLE_END:
  49. case CPU_LED_START:
  50. /* Will turn the LED on, max brightness */
  51. led_trigger_event(trig->_trig, LED_FULL);
  52. break;
  53. case CPU_LED_IDLE_START:
  54. case CPU_LED_STOP:
  55. case CPU_LED_HALTED:
  56. /* Will turn the LED off */
  57. led_trigger_event(trig->_trig, LED_OFF);
  58. break;
  59. default:
  60. /* Will leave the LED as it is */
  61. break;
  62. }
  63. }
  64. EXPORT_SYMBOL(ledtrig_cpu);
  65. static int ledtrig_cpu_syscore_suspend(void)
  66. {
  67. ledtrig_cpu(CPU_LED_STOP);
  68. return 0;
  69. }
  70. static void ledtrig_cpu_syscore_resume(void)
  71. {
  72. ledtrig_cpu(CPU_LED_START);
  73. }
  74. static void ledtrig_cpu_syscore_shutdown(void)
  75. {
  76. ledtrig_cpu(CPU_LED_HALTED);
  77. }
  78. static struct syscore_ops ledtrig_cpu_syscore_ops = {
  79. .shutdown = ledtrig_cpu_syscore_shutdown,
  80. .suspend = ledtrig_cpu_syscore_suspend,
  81. .resume = ledtrig_cpu_syscore_resume,
  82. };
  83. static int ledtrig_cpu_notify(struct notifier_block *self,
  84. unsigned long action, void *hcpu)
  85. {
  86. switch (action & ~CPU_TASKS_FROZEN) {
  87. case CPU_STARTING:
  88. ledtrig_cpu(CPU_LED_START);
  89. break;
  90. case CPU_DYING:
  91. ledtrig_cpu(CPU_LED_STOP);
  92. break;
  93. }
  94. return NOTIFY_OK;
  95. }
  96. static struct notifier_block ledtrig_cpu_nb = {
  97. .notifier_call = ledtrig_cpu_notify,
  98. };
  99. static int __init ledtrig_cpu_init(void)
  100. {
  101. int cpu;
  102. /* Supports up to 9999 cpu cores */
  103. BUILD_BUG_ON(CONFIG_NR_CPUS > 9999);
  104. /*
  105. * Registering CPU led trigger for each CPU core here
  106. * ignores CPU hotplug, but after this CPU hotplug works
  107. * fine with this trigger.
  108. */
  109. for_each_possible_cpu(cpu) {
  110. struct led_trigger_cpu *trig = &per_cpu(cpu_trig, cpu);
  111. snprintf(trig->name, MAX_NAME_LEN, "cpu%d", cpu);
  112. led_trigger_register_simple(trig->name, &trig->_trig);
  113. }
  114. register_syscore_ops(&ledtrig_cpu_syscore_ops);
  115. register_cpu_notifier(&ledtrig_cpu_nb);
  116. pr_info("ledtrig-cpu: registered to indicate activity on CPUs\n");
  117. return 0;
  118. }
  119. module_init(ledtrig_cpu_init);
  120. static void __exit ledtrig_cpu_exit(void)
  121. {
  122. int cpu;
  123. unregister_cpu_notifier(&ledtrig_cpu_nb);
  124. for_each_possible_cpu(cpu) {
  125. struct led_trigger_cpu *trig = &per_cpu(cpu_trig, cpu);
  126. led_trigger_unregister_simple(trig->_trig);
  127. trig->_trig = NULL;
  128. memset(trig->name, 0, MAX_NAME_LEN);
  129. }
  130. unregister_syscore_ops(&ledtrig_cpu_syscore_ops);
  131. }
  132. module_exit(ledtrig_cpu_exit);
  133. MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");
  134. MODULE_AUTHOR("Bryan Wu <bryan.wu@canonical.com>");
  135. MODULE_DESCRIPTION("CPU LED trigger");
  136. MODULE_LICENSE("GPL");