aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--drivers/leds/Kconfig10
-rw-r--r--drivers/leds/Makefile1
-rw-r--r--drivers/leds/ledtrig-cpu.c163
-rw-r--r--include/linux/leds.h16
4 files changed, 190 insertions, 0 deletions
diff --git a/drivers/leds/Kconfig b/drivers/leds/Kconfig
index 12b2b55c519e..c96fd29199da 100644
--- a/drivers/leds/Kconfig
+++ b/drivers/leds/Kconfig
@@ -469,6 +469,16 @@ config LEDS_TRIGGER_BACKLIGHT
469 469
470 If unsure, say N. 470 If unsure, say N.
471 471
472config LEDS_TRIGGER_CPU
473 bool "LED CPU Trigger"
474 depends on LEDS_TRIGGERS
475 help
476 This allows LEDs to be controlled by active CPUs. This shows
477 the active CPUs across an array of LEDs so you can see which
478 CPUs are active on the system at any given moment.
479
480 If unsure, say N.
481
472config LEDS_TRIGGER_GPIO 482config LEDS_TRIGGER_GPIO
473 tristate "LED GPIO Trigger" 483 tristate "LED GPIO Trigger"
474 depends on LEDS_TRIGGERS 484 depends on LEDS_TRIGGERS
diff --git a/drivers/leds/Makefile b/drivers/leds/Makefile
index f8958cd6cf6e..fa0f53626618 100644
--- a/drivers/leds/Makefile
+++ b/drivers/leds/Makefile
@@ -57,5 +57,6 @@ obj-$(CONFIG_LEDS_TRIGGER_IDE_DISK) += ledtrig-ide-disk.o
57obj-$(CONFIG_LEDS_TRIGGER_HEARTBEAT) += ledtrig-heartbeat.o 57obj-$(CONFIG_LEDS_TRIGGER_HEARTBEAT) += ledtrig-heartbeat.o
58obj-$(CONFIG_LEDS_TRIGGER_BACKLIGHT) += ledtrig-backlight.o 58obj-$(CONFIG_LEDS_TRIGGER_BACKLIGHT) += ledtrig-backlight.o
59obj-$(CONFIG_LEDS_TRIGGER_GPIO) += ledtrig-gpio.o 59obj-$(CONFIG_LEDS_TRIGGER_GPIO) += ledtrig-gpio.o
60obj-$(CONFIG_LEDS_TRIGGER_CPU) += ledtrig-cpu.o
60obj-$(CONFIG_LEDS_TRIGGER_DEFAULT_ON) += ledtrig-default-on.o 61obj-$(CONFIG_LEDS_TRIGGER_DEFAULT_ON) += ledtrig-default-on.o
61obj-$(CONFIG_LEDS_TRIGGER_TRANSIENT) += ledtrig-transient.o 62obj-$(CONFIG_LEDS_TRIGGER_TRANSIENT) += ledtrig-transient.o
diff --git a/drivers/leds/ledtrig-cpu.c b/drivers/leds/ledtrig-cpu.c
new file mode 100644
index 000000000000..b312056da14d
--- /dev/null
+++ b/drivers/leds/ledtrig-cpu.c
@@ -0,0 +1,163 @@
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
22#include <linux/module.h>
23#include <linux/kernel.h>
24#include <linux/init.h>
25#include <linux/slab.h>
26#include <linux/percpu.h>
27#include <linux/syscore_ops.h>
28#include <linux/rwsem.h>
29#include "leds.h"
30
31#define MAX_NAME_LEN 8
32
33struct led_trigger_cpu {
34 char name[MAX_NAME_LEN];
35 struct led_trigger *_trig;
36 struct mutex lock;
37 int lock_is_inited;
38};
39
40static DEFINE_PER_CPU(struct led_trigger_cpu, cpu_trig);
41
42/**
43 * ledtrig_cpu - emit a CPU event as a trigger
44 * @evt: CPU event to be emitted
45 *
46 * Emit a CPU event on a CPU core, which will trigger a
47 * binded LED to turn on or turn off.
48 */
49void ledtrig_cpu(enum cpu_led_event ledevt)
50{
51 struct led_trigger_cpu *trig = &__get_cpu_var(cpu_trig);
52
53 /* mutex lock should be initialized before calling mutex_call() */
54 if (!trig->lock_is_inited)
55 return;
56
57 mutex_lock(&trig->lock);
58
59 /* Locate the correct CPU LED */
60 switch (ledevt) {
61 case CPU_LED_IDLE_END:
62 case CPU_LED_START:
63 /* Will turn the LED on, max brightness */
64 led_trigger_event(trig->_trig, LED_FULL);
65 break;
66
67 case CPU_LED_IDLE_START:
68 case CPU_LED_STOP:
69 case CPU_LED_HALTED:
70 /* Will turn the LED off */
71 led_trigger_event(trig->_trig, LED_OFF);
72 break;
73
74 default:
75 /* Will leave the LED as it is */
76 break;
77 }
78
79 mutex_unlock(&trig->lock);
80}
81EXPORT_SYMBOL(ledtrig_cpu);
82
83static int ledtrig_cpu_syscore_suspend(void)
84{
85 ledtrig_cpu(CPU_LED_STOP);
86 return 0;
87}
88
89static void ledtrig_cpu_syscore_resume(void)
90{
91 ledtrig_cpu(CPU_LED_START);
92}
93
94static void ledtrig_cpu_syscore_shutdown(void)
95{
96 ledtrig_cpu(CPU_LED_HALTED);
97}
98
99static struct syscore_ops ledtrig_cpu_syscore_ops = {
100 .shutdown = ledtrig_cpu_syscore_shutdown,
101 .suspend = ledtrig_cpu_syscore_suspend,
102 .resume = ledtrig_cpu_syscore_resume,
103};
104
105static int __init ledtrig_cpu_init(void)
106{
107 int cpu;
108
109 /* Supports up to 9999 cpu cores */
110 BUILD_BUG_ON(CONFIG_NR_CPUS > 9999);
111
112 /*
113 * Registering CPU led trigger for each CPU core here
114 * ignores CPU hotplug, but after this CPU hotplug works
115 * fine with this trigger.
116 */
117 for_each_possible_cpu(cpu) {
118 struct led_trigger_cpu *trig = &per_cpu(cpu_trig, cpu);
119
120 mutex_init(&trig->lock);
121
122 snprintf(trig->name, MAX_NAME_LEN, "cpu%d", cpu);
123
124 mutex_lock(&trig->lock);
125 led_trigger_register_simple(trig->name, &trig->_trig);
126 trig->lock_is_inited = 1;
127 mutex_unlock(&trig->lock);
128 }
129
130 register_syscore_ops(&ledtrig_cpu_syscore_ops);
131
132 pr_info("ledtrig-cpu: registered to indicate activity on CPUs\n");
133
134 return 0;
135}
136module_init(ledtrig_cpu_init);
137
138static void __exit ledtrig_cpu_exit(void)
139{
140 int cpu;
141
142 for_each_possible_cpu(cpu) {
143 struct led_trigger_cpu *trig = &per_cpu(cpu_trig, cpu);
144
145 mutex_lock(&trig->lock);
146
147 led_trigger_unregister_simple(trig->_trig);
148 trig->_trig = NULL;
149 memset(trig->name, 0, MAX_NAME_LEN);
150 trig->lock_is_inited = 0;
151
152 mutex_unlock(&trig->lock);
153 mutex_destroy(&trig->lock);
154 }
155
156 unregister_syscore_ops(&ledtrig_cpu_syscore_ops);
157}
158module_exit(ledtrig_cpu_exit);
159
160MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");
161MODULE_AUTHOR("Bryan Wu <bryan.wu@canonical.com>");
162MODULE_DESCRIPTION("CPU LED trigger");
163MODULE_LICENSE("GPL");
diff --git a/include/linux/leds.h b/include/linux/leds.h
index 39eee41d8c6f..cd545580e069 100644
--- a/include/linux/leds.h
+++ b/include/linux/leds.h
@@ -212,4 +212,20 @@ struct gpio_led_platform_data {
212struct platform_device *gpio_led_register_device( 212struct platform_device *gpio_led_register_device(
213 int id, const struct gpio_led_platform_data *pdata); 213 int id, const struct gpio_led_platform_data *pdata);
214 214
215enum cpu_led_event {
216 CPU_LED_IDLE_START, /* CPU enters idle */
217 CPU_LED_IDLE_END, /* CPU idle ends */
218 CPU_LED_START, /* Machine starts, especially resume */
219 CPU_LED_STOP, /* Machine stops, especially suspend */
220 CPU_LED_HALTED, /* Machine shutdown */
221};
222#ifdef CONFIG_LEDS_TRIGGER_CPU
223extern void ledtrig_cpu(enum cpu_led_event evt);
224#else
225static inline void ledtrig_cpu(enum cpu_led_event evt)
226{
227 return;
228}
229#endif
230
215#endif /* __LINUX_LEDS_H_INCLUDED */ 231#endif /* __LINUX_LEDS_H_INCLUDED */