blob: 22f0634dd3fa4de4f4f525f0696420c17374ec35 [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
Bryan Wu8f887312011-06-25 18:33:50 +080022#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>
Pawel Mollfba14ae2014-01-22 08:32:02 -080028#include <linux/cpu.h>
Kim, Milof07fb522013-02-20 00:36:01 -080029#include "../leds.h"
Bryan Wu8f887312011-06-25 18:33:50 +080030
31#define MAX_NAME_LEN 8
32
33struct led_trigger_cpu {
34 char name[MAX_NAME_LEN];
35 struct led_trigger *_trig;
Bryan Wu8f887312011-06-25 18:33:50 +080036};
37
38static DEFINE_PER_CPU(struct led_trigger_cpu, cpu_trig);
39
40/**
41 * ledtrig_cpu - emit a CPU event as a trigger
42 * @evt: CPU event to be emitted
43 *
44 * Emit a CPU event on a CPU core, which will trigger a
45 * binded LED to turn on or turn off.
46 */
47void ledtrig_cpu(enum cpu_led_event ledevt)
48{
Christoph Lameter24c93012014-05-05 09:48:38 -070049 struct led_trigger_cpu *trig = this_cpu_ptr(&cpu_trig);
Bryan Wu8f887312011-06-25 18:33:50 +080050
Bryan Wu8f887312011-06-25 18:33:50 +080051 /* Locate the correct CPU LED */
52 switch (ledevt) {
53 case CPU_LED_IDLE_END:
54 case CPU_LED_START:
55 /* Will turn the LED on, max brightness */
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 /* Will turn the LED off */
63 led_trigger_event(trig->_trig, LED_OFF);
64 break;
65
66 default:
67 /* Will leave the LED as it is */
68 break;
69 }
Bryan Wu8f887312011-06-25 18:33:50 +080070}
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
Sebastian Andrzej Siewiorcd894f12016-07-20 17:24:55 +020095static int ledtrig_online_cpu(unsigned int cpu)
Pawel Mollfba14ae2014-01-22 08:32:02 -080096{
Richard Cochran911a3592016-07-13 17:16:45 +000097 ledtrig_cpu(CPU_LED_START);
98 return 0;
Pawel Mollfba14ae2014-01-22 08:32:02 -080099}
100
Sebastian Andrzej Siewiorcd894f12016-07-20 17:24:55 +0200101static int ledtrig_prepare_down_cpu(unsigned int cpu)
Richard Cochran911a3592016-07-13 17:16:45 +0000102{
103 ledtrig_cpu(CPU_LED_STOP);
104 return 0;
105}
Pawel Mollfba14ae2014-01-22 08:32:02 -0800106
Bryan Wu8f887312011-06-25 18:33:50 +0800107static int __init ledtrig_cpu_init(void)
108{
109 int cpu;
Sebastian Andrzej Siewiorcd894f12016-07-20 17:24:55 +0200110 int ret;
Bryan Wu8f887312011-06-25 18:33:50 +0800111
112 /* Supports up to 9999 cpu cores */
113 BUILD_BUG_ON(CONFIG_NR_CPUS > 9999);
114
115 /*
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.
119 */
120 for_each_possible_cpu(cpu) {
121 struct led_trigger_cpu *trig = &per_cpu(cpu_trig, cpu);
122
Bryan Wu8f887312011-06-25 18:33:50 +0800123 snprintf(trig->name, MAX_NAME_LEN, "cpu%d", cpu);
124
Bryan Wu8f887312011-06-25 18:33:50 +0800125 led_trigger_register_simple(trig->name, &trig->_trig);
Bryan Wu8f887312011-06-25 18:33:50 +0800126 }
127
128 register_syscore_ops(&ledtrig_cpu_syscore_ops);
Richard Cochran911a3592016-07-13 17:16:45 +0000129
Sebastian Andrzej Siewiorcd894f12016-07-20 17:24:55 +0200130 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);
Bryan Wu8f887312011-06-25 18:33:50 +0800135
136 pr_info("ledtrig-cpu: registered to indicate activity on CPUs\n");
137
138 return 0;
139}
Paul Gortmaker227a0ca2015-12-13 16:45:49 -0500140device_initcall(ledtrig_cpu_init);