ledtrig-cpu.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 "leds.h"
  29. #define MAX_NAME_LEN 8
  30. struct led_trigger_cpu {
  31. char name[MAX_NAME_LEN];
  32. struct led_trigger *_trig;
  33. struct mutex lock;
  34. int lock_is_inited;
  35. };
  36. static DEFINE_PER_CPU(struct led_trigger_cpu, cpu_trig);
  37. /**
  38. * ledtrig_cpu - emit a CPU event as a trigger
  39. * @evt: CPU event to be emitted
  40. *
  41. * Emit a CPU event on a CPU core, which will trigger a
  42. * binded LED to turn on or turn off.
  43. */
  44. void ledtrig_cpu(enum cpu_led_event ledevt)
  45. {
  46. struct led_trigger_cpu *trig = &__get_cpu_var(cpu_trig);
  47. /* mutex lock should be initialized before calling mutex_call() */
  48. if (!trig->lock_is_inited)
  49. return;
  50. mutex_lock(&trig->lock);
  51. /* Locate the correct CPU LED */
  52. switch (ledevt) {
  53. case CPU_LED_IDLE_END:
  54. case CPU_LED_START:
  55. /* Will turn the LED on, max brightness */
  56. led_trigger_event(trig->_trig, LED_FULL);
  57. break;
  58. case CPU_LED_IDLE_START:
  59. case CPU_LED_STOP:
  60. case CPU_LED_HALTED:
  61. /* Will turn the LED off */
  62. led_trigger_event(trig->_trig, LED_OFF);
  63. break;
  64. default:
  65. /* Will leave the LED as it is */
  66. break;
  67. }
  68. mutex_unlock(&trig->lock);
  69. }
  70. EXPORT_SYMBOL(ledtrig_cpu);
  71. static int ledtrig_cpu_syscore_suspend(void)
  72. {
  73. ledtrig_cpu(CPU_LED_STOP);
  74. return 0;
  75. }
  76. static void ledtrig_cpu_syscore_resume(void)
  77. {
  78. ledtrig_cpu(CPU_LED_START);
  79. }
  80. static void ledtrig_cpu_syscore_shutdown(void)
  81. {
  82. ledtrig_cpu(CPU_LED_HALTED);
  83. }
  84. static struct syscore_ops ledtrig_cpu_syscore_ops = {
  85. .shutdown = ledtrig_cpu_syscore_shutdown,
  86. .suspend = ledtrig_cpu_syscore_suspend,
  87. .resume = ledtrig_cpu_syscore_resume,
  88. };
  89. static int __init ledtrig_cpu_init(void)
  90. {
  91. int cpu;
  92. /* Supports up to 9999 cpu cores */
  93. BUILD_BUG_ON(CONFIG_NR_CPUS > 9999);
  94. /*
  95. * Registering CPU led trigger for each CPU core here
  96. * ignores CPU hotplug, but after this CPU hotplug works
  97. * fine with this trigger.
  98. */
  99. for_each_possible_cpu(cpu) {
  100. struct led_trigger_cpu *trig = &per_cpu(cpu_trig, cpu);
  101. mutex_init(&trig->lock);
  102. snprintf(trig->name, MAX_NAME_LEN, "cpu%d", cpu);
  103. mutex_lock(&trig->lock);
  104. led_trigger_register_simple(trig->name, &trig->_trig);
  105. trig->lock_is_inited = 1;
  106. mutex_unlock(&trig->lock);
  107. }
  108. register_syscore_ops(&ledtrig_cpu_syscore_ops);
  109. pr_info("ledtrig-cpu: registered to indicate activity on CPUs\n");
  110. return 0;
  111. }
  112. module_init(ledtrig_cpu_init);
  113. static void __exit ledtrig_cpu_exit(void)
  114. {
  115. int cpu;
  116. for_each_possible_cpu(cpu) {
  117. struct led_trigger_cpu *trig = &per_cpu(cpu_trig, cpu);
  118. mutex_lock(&trig->lock);
  119. led_trigger_unregister_simple(trig->_trig);
  120. trig->_trig = NULL;
  121. memset(trig->name, 0, MAX_NAME_LEN);
  122. trig->lock_is_inited = 0;
  123. mutex_unlock(&trig->lock);
  124. mutex_destroy(&trig->lock);
  125. }
  126. unregister_syscore_ops(&ledtrig_cpu_syscore_ops);
  127. }
  128. module_exit(ledtrig_cpu_exit);
  129. MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");
  130. MODULE_AUTHOR("Bryan Wu <bryan.wu@canonical.com>");
  131. MODULE_DESCRIPTION("CPU LED trigger");
  132. MODULE_LICENSE("GPL");