1 // SPDX-License-Identifier: GPL-2.0-only
3 * ledtrig-cpu.c - LED trigger based on CPU activity
5 * This LED trigger will be registered for first 8 CPUs and named
6 * as cpu0..cpu7. There's additional trigger called cpu that
7 * is on when any CPU is active.
9 * If you want support for arbitrary number of CPUs, make it one trigger,
10 * with additional sysfs file selecting which CPU to watch.
12 * It can be bound to any LED just like other triggers using either a
13 * board file or via sysfs interface.
15 * An API named ledtrig_cpu is exported for any user, who want to add CPU
16 * activity indication in their code.
18 * Copyright 2011 Linus Walleij <linus.walleij@linaro.org>
19 * Copyright 2011 - 2012 Bryan Wu <bryan.wu@canonical.com>
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
{
35 char name
[MAX_NAME_LEN
];
36 struct led_trigger
*_trig
;
39 static DEFINE_PER_CPU(struct led_trigger_cpu
, cpu_trig
);
41 static struct led_trigger
*trig_cpu_all
;
42 static atomic_t num_active_cpus
= ATOMIC_INIT(0);
45 * ledtrig_cpu - emit a CPU event as a trigger
46 * @evt: CPU event to be emitted
48 * Emit a CPU event on a CPU core, which will trigger a
49 * bound LED to turn on or turn off.
51 void ledtrig_cpu(enum cpu_led_event ledevt
)
53 struct led_trigger_cpu
*trig
= this_cpu_ptr(&cpu_trig
);
54 bool is_active
= trig
->is_active
;
56 /* Locate the correct CPU LED */
58 case CPU_LED_IDLE_END
:
60 /* Will turn the LED on, max brightness */
64 case CPU_LED_IDLE_START
:
67 /* Will turn the LED off */
72 /* Will leave the LED as it is */
76 if (is_active
!= trig
->is_active
) {
77 unsigned int active_cpus
;
78 unsigned int total_cpus
;
80 /* Update trigger state */
81 trig
->is_active
= is_active
;
82 atomic_add(is_active
? 1 : -1, &num_active_cpus
);
83 active_cpus
= atomic_read(&num_active_cpus
);
84 total_cpus
= num_present_cpus();
86 led_trigger_event(trig
->_trig
,
87 is_active
? LED_FULL
: LED_OFF
);
90 led_trigger_event(trig_cpu_all
,
91 DIV_ROUND_UP(LED_FULL
* active_cpus
, total_cpus
));
95 EXPORT_SYMBOL(ledtrig_cpu
);
97 static int ledtrig_cpu_syscore_suspend(void)
99 ledtrig_cpu(CPU_LED_STOP
);
103 static void ledtrig_cpu_syscore_resume(void)
105 ledtrig_cpu(CPU_LED_START
);
108 static void ledtrig_cpu_syscore_shutdown(void)
110 ledtrig_cpu(CPU_LED_HALTED
);
113 static struct syscore_ops ledtrig_cpu_syscore_ops
= {
114 .shutdown
= ledtrig_cpu_syscore_shutdown
,
115 .suspend
= ledtrig_cpu_syscore_suspend
,
116 .resume
= ledtrig_cpu_syscore_resume
,
119 static int ledtrig_online_cpu(unsigned int cpu
)
121 ledtrig_cpu(CPU_LED_START
);
125 static int ledtrig_prepare_down_cpu(unsigned int cpu
)
127 ledtrig_cpu(CPU_LED_STOP
);
131 static int __init
ledtrig_cpu_init(void)
136 /* Supports up to 9999 cpu cores */
137 BUILD_BUG_ON(CONFIG_NR_CPUS
> 9999);
140 * Registering a trigger for all CPUs.
142 led_trigger_register_simple("cpu", &trig_cpu_all
);
145 * Registering CPU led trigger for each CPU core here
146 * ignores CPU hotplug, but after this CPU hotplug works
147 * fine with this trigger.
149 for_each_possible_cpu(cpu
) {
150 struct led_trigger_cpu
*trig
= &per_cpu(cpu_trig
, cpu
);
155 snprintf(trig
->name
, MAX_NAME_LEN
, "cpu%d", cpu
);
157 led_trigger_register_simple(trig
->name
, &trig
->_trig
);
160 register_syscore_ops(&ledtrig_cpu_syscore_ops
);
162 ret
= cpuhp_setup_state(CPUHP_AP_ONLINE_DYN
, "leds/trigger:starting",
163 ledtrig_online_cpu
, ledtrig_prepare_down_cpu
);
165 pr_err("CPU hotplug notifier for ledtrig-cpu could not be registered: %d\n",
168 pr_info("ledtrig-cpu: registered to indicate activity on CPUs\n");
172 device_initcall(ledtrig_cpu_init
);