2 * ledtrig-cpu.c - LED trigger based on CPU activity
4 * This LED trigger will be registered for each possible CPU and named as
5 * cpu0, cpu1, cpu2, cpu3, etc.
7 * It can be bound to any LED just like other triggers using either a
8 * board file or via sysfs interface.
10 * An API named ledtrig_cpu is exported for any user, who want to add CPU
11 * activity indication in their code
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.
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>
31 #define MAX_NAME_LEN 8
33 struct led_trigger_cpu {
34 char name[MAX_NAME_LEN];
35 struct led_trigger *_trig;
38 static DEFINE_PER_CPU(struct led_trigger_cpu, cpu_trig);
41 * ledtrig_cpu - emit a CPU event as a trigger
42 * @evt: CPU event to be emitted
44 * Emit a CPU event on a CPU core, which will trigger a
45 * bound LED to turn on or turn off.
47 void ledtrig_cpu(enum cpu_led_event ledevt)
49 struct led_trigger_cpu *trig = this_cpu_ptr(&cpu_trig);
51 /* Locate the correct CPU LED */
53 case CPU_LED_IDLE_END:
55 /* Will turn the LED on, max brightness */
56 led_trigger_event(trig->_trig, LED_FULL);
59 case CPU_LED_IDLE_START:
62 /* Will turn the LED off */
63 led_trigger_event(trig->_trig, LED_OFF);
67 /* Will leave the LED as it is */
71 EXPORT_SYMBOL(ledtrig_cpu);
73 static int ledtrig_cpu_syscore_suspend(void)
75 ledtrig_cpu(CPU_LED_STOP);
79 static void ledtrig_cpu_syscore_resume(void)
81 ledtrig_cpu(CPU_LED_START);
84 static void ledtrig_cpu_syscore_shutdown(void)
86 ledtrig_cpu(CPU_LED_HALTED);
89 static struct syscore_ops ledtrig_cpu_syscore_ops = {
90 .shutdown = ledtrig_cpu_syscore_shutdown,
91 .suspend = ledtrig_cpu_syscore_suspend,
92 .resume = ledtrig_cpu_syscore_resume,
95 static int ledtrig_online_cpu(unsigned int cpu)
97 ledtrig_cpu(CPU_LED_START);
101 static int ledtrig_prepare_down_cpu(unsigned int cpu)
103 ledtrig_cpu(CPU_LED_STOP);
107 static int __init ledtrig_cpu_init(void)
112 /* Supports up to 9999 cpu cores */
113 BUILD_BUG_ON(CONFIG_NR_CPUS > 9999);
116 * Registering CPU led trigger for each CPU core here
117 * ignores CPU hotplug, but after this CPU hotplug works
118 * fine with this trigger.
120 for_each_possible_cpu(cpu) {
121 struct led_trigger_cpu *trig = &per_cpu(cpu_trig, cpu);
123 snprintf(trig->name, MAX_NAME_LEN, "cpu%d", cpu);
125 led_trigger_register_simple(trig->name, &trig->_trig);
128 register_syscore_ops(&ledtrig_cpu_syscore_ops);
130 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "leds/trigger:starting",
131 ledtrig_online_cpu, ledtrig_prepare_down_cpu);
133 pr_err("CPU hotplug notifier for ledtrig-cpu could not be registered: %d\n",
136 pr_info("ledtrig-cpu: registered to indicate activity on CPUs\n");
140 device_initcall(ledtrig_cpu_init);