Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * ledtrig-cpu.c - LED trigger based on CPU activity
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * This LED trigger will be registered for first 8 CPUs and named
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * as cpu0..cpu7. There's additional trigger called cpu that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * is on when any CPU is active.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * If you want support for arbitrary number of CPUs, make it one trigger,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * with additional sysfs file selecting which CPU to watch.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * It can be bound to any LED just like other triggers using either a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  * board file or via sysfs interface.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  * An API named ledtrig_cpu is exported for any user, who want to add CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  * activity indication in their code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  * Copyright 2011 Linus Walleij <linus.walleij@linaro.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  * Copyright 2011 - 2012 Bryan Wu <bryan.wu@canonical.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <linux/percpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include <linux/syscore_ops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include <linux/rwsem.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #include <linux/cpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #include "../leds.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #define MAX_NAME_LEN	8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) struct led_trigger_cpu {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	bool is_active;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	char name[MAX_NAME_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	struct led_trigger *_trig;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) static DEFINE_PER_CPU(struct led_trigger_cpu, cpu_trig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) static struct led_trigger *trig_cpu_all;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) static atomic_t num_active_cpus = ATOMIC_INIT(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45)  * ledtrig_cpu - emit a CPU event as a trigger
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46)  * @evt: CPU event to be emitted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48)  * Emit a CPU event on a CPU core, which will trigger a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49)  * bound LED to turn on or turn off.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) void ledtrig_cpu(enum cpu_led_event ledevt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	struct led_trigger_cpu *trig = this_cpu_ptr(&cpu_trig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	bool is_active = trig->is_active;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	/* Locate the correct CPU LED */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	switch (ledevt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	case CPU_LED_IDLE_END:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	case CPU_LED_START:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		/* Will turn the LED on, max brightness */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		is_active = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	case CPU_LED_IDLE_START:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	case CPU_LED_STOP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	case CPU_LED_HALTED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		/* Will turn the LED off */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		is_active = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		/* Will leave the LED as it is */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	if (is_active != trig->is_active) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		unsigned int active_cpus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		unsigned int total_cpus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		/* Update trigger state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		trig->is_active = is_active;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		atomic_add(is_active ? 1 : -1, &num_active_cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		active_cpus = atomic_read(&num_active_cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		total_cpus = num_present_cpus();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		led_trigger_event(trig->_trig,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 			is_active ? LED_FULL : LED_OFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		led_trigger_event(trig_cpu_all,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 			DIV_ROUND_UP(LED_FULL * active_cpus, total_cpus));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) EXPORT_SYMBOL(ledtrig_cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) static int ledtrig_cpu_syscore_suspend(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	ledtrig_cpu(CPU_LED_STOP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static void ledtrig_cpu_syscore_resume(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	ledtrig_cpu(CPU_LED_START);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) static void ledtrig_cpu_syscore_shutdown(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	ledtrig_cpu(CPU_LED_HALTED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) static struct syscore_ops ledtrig_cpu_syscore_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	.shutdown	= ledtrig_cpu_syscore_shutdown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	.suspend	= ledtrig_cpu_syscore_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	.resume		= ledtrig_cpu_syscore_resume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static int ledtrig_online_cpu(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	ledtrig_cpu(CPU_LED_START);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) static int ledtrig_prepare_down_cpu(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	ledtrig_cpu(CPU_LED_STOP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) static int __init ledtrig_cpu_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	/* Supports up to 9999 cpu cores */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	BUILD_BUG_ON(CONFIG_NR_CPUS > 9999);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	 * Registering a trigger for all CPUs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	led_trigger_register_simple("cpu", &trig_cpu_all);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	 * Registering CPU led trigger for each CPU core here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	 * ignores CPU hotplug, but after this CPU hotplug works
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	 * fine with this trigger.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	for_each_possible_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		struct led_trigger_cpu *trig = &per_cpu(cpu_trig, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		if (cpu >= 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		snprintf(trig->name, MAX_NAME_LEN, "cpu%d", cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		led_trigger_register_simple(trig->name, &trig->_trig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	register_syscore_ops(&ledtrig_cpu_syscore_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "leds/trigger:starting",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 				ledtrig_online_cpu, ledtrig_prepare_down_cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		pr_err("CPU hotplug notifier for ledtrig-cpu could not be registered: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		       ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	pr_info("ledtrig-cpu: registered to indicate activity on CPUs\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) device_initcall(ledtrig_cpu_init);