blob: b312056da14d3c5f9ef602a455d8bbc80fc8c125 [file] [log] [blame]
Bryan Wu8f887312011-06-25 18:33:50 +08001/*
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");