1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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>
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};
37
38static DEFINE_PER_CPU(struct led_trigger_cpu, cpu_trig);
39
40
41
42
43
44
45
46
47void ledtrig_cpu(enum cpu_led_event ledevt)
48{
49 struct led_trigger_cpu *trig = this_cpu_ptr(&cpu_trig);
50
51
52 switch (ledevt) {
53 case CPU_LED_IDLE_END:
54 case CPU_LED_START:
55
56 led_trigger_event(trig->_trig, LED_FULL);
57 break;
58
59 case CPU_LED_IDLE_START:
60 case CPU_LED_STOP:
61 case CPU_LED_HALTED:
62
63 led_trigger_event(trig->_trig, LED_OFF);
64 break;
65
66 default:
67
68 break;
69 }
70}
71EXPORT_SYMBOL(ledtrig_cpu);
72
73static int ledtrig_cpu_syscore_suspend(void)
74{
75 ledtrig_cpu(CPU_LED_STOP);
76 return 0;
77}
78
79static void ledtrig_cpu_syscore_resume(void)
80{
81 ledtrig_cpu(CPU_LED_START);
82}
83
84static void ledtrig_cpu_syscore_shutdown(void)
85{
86 ledtrig_cpu(CPU_LED_HALTED);
87}
88
89static 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,
93};
94
95static int ledtrig_online_cpu(unsigned int cpu)
96{
97 ledtrig_cpu(CPU_LED_START);
98 return 0;
99}
100
101static int ledtrig_prepare_down_cpu(unsigned int cpu)
102{
103 ledtrig_cpu(CPU_LED_STOP);
104 return 0;
105}
106
107static int __init ledtrig_cpu_init(void)
108{
109 int cpu;
110 int ret;
111
112
113 BUILD_BUG_ON(CONFIG_NR_CPUS > 9999);
114
115
116
117
118
119
120 for_each_possible_cpu(cpu) {
121 struct led_trigger_cpu *trig = &per_cpu(cpu_trig, cpu);
122
123 snprintf(trig->name, MAX_NAME_LEN, "cpu%d", cpu);
124
125 led_trigger_register_simple(trig->name, &trig->_trig);
126 }
127
128 register_syscore_ops(&ledtrig_cpu_syscore_ops);
129
130 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "AP_LEDTRIG_STARTING",
131 ledtrig_online_cpu, ledtrig_prepare_down_cpu);
132 if (ret < 0)
133 pr_err("CPU hotplug notifier for ledtrig-cpu could not be registered: %d\n",
134 ret);
135
136 pr_info("ledtrig-cpu: registered to indicate activity on CPUs\n");
137
138 return 0;
139}
140device_initcall(ledtrig_cpu_init);
141