0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022 #include <linux/kernel.h>
0023 #include <linux/init.h>
0024 #include <linux/slab.h>
0025 #include <linux/percpu.h>
0026 #include <linux/syscore_ops.h>
0027 #include <linux/rwsem.h>
0028 #include <linux/cpu.h>
0029 #include "../leds.h"
0030
0031 #define MAX_NAME_LEN 8
0032
0033 struct led_trigger_cpu {
0034 bool is_active;
0035 char name[MAX_NAME_LEN];
0036 struct led_trigger *_trig;
0037 };
0038
0039 static DEFINE_PER_CPU(struct led_trigger_cpu, cpu_trig);
0040
0041 static struct led_trigger *trig_cpu_all;
0042 static atomic_t num_active_cpus = ATOMIC_INIT(0);
0043
0044
0045
0046
0047
0048
0049
0050
0051 void ledtrig_cpu(enum cpu_led_event ledevt)
0052 {
0053 struct led_trigger_cpu *trig = this_cpu_ptr(&cpu_trig);
0054 bool is_active = trig->is_active;
0055
0056
0057 switch (ledevt) {
0058 case CPU_LED_IDLE_END:
0059 case CPU_LED_START:
0060
0061 is_active = true;
0062 break;
0063
0064 case CPU_LED_IDLE_START:
0065 case CPU_LED_STOP:
0066 case CPU_LED_HALTED:
0067
0068 is_active = false;
0069 break;
0070
0071 default:
0072
0073 break;
0074 }
0075
0076 if (is_active != trig->is_active) {
0077 unsigned int active_cpus;
0078 unsigned int total_cpus;
0079
0080
0081 trig->is_active = is_active;
0082 atomic_add(is_active ? 1 : -1, &num_active_cpus);
0083 active_cpus = atomic_read(&num_active_cpus);
0084 total_cpus = num_present_cpus();
0085
0086 led_trigger_event(trig->_trig,
0087 is_active ? LED_FULL : LED_OFF);
0088
0089
0090 led_trigger_event(trig_cpu_all,
0091 DIV_ROUND_UP(LED_FULL * active_cpus, total_cpus));
0092
0093 }
0094 }
0095 EXPORT_SYMBOL(ledtrig_cpu);
0096
0097 static int ledtrig_cpu_syscore_suspend(void)
0098 {
0099 ledtrig_cpu(CPU_LED_STOP);
0100 return 0;
0101 }
0102
0103 static void ledtrig_cpu_syscore_resume(void)
0104 {
0105 ledtrig_cpu(CPU_LED_START);
0106 }
0107
0108 static void ledtrig_cpu_syscore_shutdown(void)
0109 {
0110 ledtrig_cpu(CPU_LED_HALTED);
0111 }
0112
0113 static struct syscore_ops ledtrig_cpu_syscore_ops = {
0114 .shutdown = ledtrig_cpu_syscore_shutdown,
0115 .suspend = ledtrig_cpu_syscore_suspend,
0116 .resume = ledtrig_cpu_syscore_resume,
0117 };
0118
0119 static int ledtrig_online_cpu(unsigned int cpu)
0120 {
0121 ledtrig_cpu(CPU_LED_START);
0122 return 0;
0123 }
0124
0125 static int ledtrig_prepare_down_cpu(unsigned int cpu)
0126 {
0127 ledtrig_cpu(CPU_LED_STOP);
0128 return 0;
0129 }
0130
0131 static int __init ledtrig_cpu_init(void)
0132 {
0133 int cpu;
0134 int ret;
0135
0136
0137 BUILD_BUG_ON(CONFIG_NR_CPUS > 9999);
0138
0139
0140
0141
0142 led_trigger_register_simple("cpu", &trig_cpu_all);
0143
0144
0145
0146
0147
0148
0149 for_each_possible_cpu(cpu) {
0150 struct led_trigger_cpu *trig = &per_cpu(cpu_trig, cpu);
0151
0152 if (cpu >= 8)
0153 continue;
0154
0155 snprintf(trig->name, MAX_NAME_LEN, "cpu%d", cpu);
0156
0157 led_trigger_register_simple(trig->name, &trig->_trig);
0158 }
0159
0160 register_syscore_ops(&ledtrig_cpu_syscore_ops);
0161
0162 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "leds/trigger:starting",
0163 ledtrig_online_cpu, ledtrig_prepare_down_cpu);
0164 if (ret < 0)
0165 pr_err("CPU hotplug notifier for ledtrig-cpu could not be registered: %d\n",
0166 ret);
0167
0168 pr_info("ledtrig-cpu: registered to indicate activity on CPUs\n");
0169
0170 return 0;
0171 }
0172 device_initcall(ledtrig_cpu_init);