xref: /openbmc/linux/arch/sparc/kernel/led.c (revision ae62fbe2)
109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2ee1858d3SLars Kotthoff #include <linux/kernel.h>
3ee1858d3SLars Kotthoff #include <linux/module.h>
4ee1858d3SLars Kotthoff #include <linux/init.h>
5ee1858d3SLars Kotthoff #include <linux/proc_fs.h>
6399dc43bSAlexey Dobriyan #include <linux/seq_file.h>
75a0e3ad6STejun Heo #include <linux/slab.h>
8ee1858d3SLars Kotthoff #include <linux/string.h>
9b80a7186SDavid S. Miller #include <linux/jiffies.h>
10b80a7186SDavid S. Miller #include <linux/timer.h>
11b80a7186SDavid S. Miller #include <linux/uaccess.h>
124f17722cSIngo Molnar #include <linux/sched/loadavg.h>
13ee1858d3SLars Kotthoff 
14ee1858d3SLars Kotthoff #include <asm/auxio.h>
15ee1858d3SLars Kotthoff 
16ee1858d3SLars Kotthoff #define LED_MAX_LENGTH 8 /* maximum chars written to proc file */
17ee1858d3SLars Kotthoff 
led_toggle(void)18ee1858d3SLars Kotthoff static inline void led_toggle(void)
19ee1858d3SLars Kotthoff {
20ee1858d3SLars Kotthoff 	unsigned char val = get_auxio();
21ee1858d3SLars Kotthoff 	unsigned char on, off;
22ee1858d3SLars Kotthoff 
23ee1858d3SLars Kotthoff 	if (val & AUXIO_LED) {
24ee1858d3SLars Kotthoff 		on = 0;
25ee1858d3SLars Kotthoff 		off = AUXIO_LED;
26ee1858d3SLars Kotthoff 	} else {
27ee1858d3SLars Kotthoff 		on = AUXIO_LED;
28ee1858d3SLars Kotthoff 		off = 0;
29ee1858d3SLars Kotthoff 	}
30ee1858d3SLars Kotthoff 
31ee1858d3SLars Kotthoff 	set_auxio(on, off);
32ee1858d3SLars Kotthoff }
33ee1858d3SLars Kotthoff 
34ee1858d3SLars Kotthoff static struct timer_list led_blink_timer;
35db275f2aSKees Cook static unsigned long led_blink_timer_timeout;
36ee1858d3SLars Kotthoff 
led_blink(struct timer_list * unused)37db275f2aSKees Cook static void led_blink(struct timer_list *unused)
38ee1858d3SLars Kotthoff {
39db275f2aSKees Cook 	unsigned long timeout = led_blink_timer_timeout;
40db275f2aSKees Cook 
41ee1858d3SLars Kotthoff 	led_toggle();
42ee1858d3SLars Kotthoff 
43ee1858d3SLars Kotthoff 	/* reschedule */
44ee1858d3SLars Kotthoff 	if (!timeout) { /* blink according to load */
45ee1858d3SLars Kotthoff 		led_blink_timer.expires = jiffies +
46ee1858d3SLars Kotthoff 			((1 + (avenrun[0] >> FSHIFT)) * HZ);
47ee1858d3SLars Kotthoff 	} else { /* blink at user specified interval */
48ee1858d3SLars Kotthoff 		led_blink_timer.expires = jiffies + (timeout * HZ);
49ee1858d3SLars Kotthoff 	}
50ee1858d3SLars Kotthoff 	add_timer(&led_blink_timer);
51ee1858d3SLars Kotthoff }
52ee1858d3SLars Kotthoff 
53b3554aa2SRandy Dunlap #ifdef CONFIG_PROC_FS
led_proc_show(struct seq_file * m,void * v)54399dc43bSAlexey Dobriyan static int led_proc_show(struct seq_file *m, void *v)
55ee1858d3SLars Kotthoff {
56ee1858d3SLars Kotthoff 	if (get_auxio() & AUXIO_LED)
57399dc43bSAlexey Dobriyan 		seq_puts(m, "on\n");
58ee1858d3SLars Kotthoff 	else
59399dc43bSAlexey Dobriyan 		seq_puts(m, "off\n");
60399dc43bSAlexey Dobriyan 	return 0;
61ee1858d3SLars Kotthoff }
62ee1858d3SLars Kotthoff 
led_proc_open(struct inode * inode,struct file * file)63399dc43bSAlexey Dobriyan static int led_proc_open(struct inode *inode, struct file *file)
64399dc43bSAlexey Dobriyan {
65399dc43bSAlexey Dobriyan 	return single_open(file, led_proc_show, NULL);
66399dc43bSAlexey Dobriyan }
67399dc43bSAlexey Dobriyan 
led_proc_write(struct file * file,const char __user * buffer,size_t count,loff_t * ppos)68399dc43bSAlexey Dobriyan static ssize_t led_proc_write(struct file *file, const char __user *buffer,
69399dc43bSAlexey Dobriyan 			      size_t count, loff_t *ppos)
70ee1858d3SLars Kotthoff {
71ee1858d3SLars Kotthoff 	char *buf = NULL;
72ee1858d3SLars Kotthoff 
73ee1858d3SLars Kotthoff 	if (count > LED_MAX_LENGTH)
74ee1858d3SLars Kotthoff 		count = LED_MAX_LENGTH;
75ee1858d3SLars Kotthoff 
76aed74ea0SGeliang Tang 	buf = memdup_user_nul(buffer, count);
77aed74ea0SGeliang Tang 	if (IS_ERR(buf))
78aed74ea0SGeliang Tang 		return PTR_ERR(buf);
79ee1858d3SLars Kotthoff 
80ee1858d3SLars Kotthoff 	/* work around \n when echo'ing into proc */
81ee1858d3SLars Kotthoff 	if (buf[count - 1] == '\n')
82ee1858d3SLars Kotthoff 		buf[count - 1] = '\0';
83ee1858d3SLars Kotthoff 
84ee1858d3SLars Kotthoff 	/* before we change anything we want to stop any running timers,
85ee1858d3SLars Kotthoff 	 * otherwise calls such as on will have no persistent effect
86ee1858d3SLars Kotthoff 	 */
87ee1858d3SLars Kotthoff 	del_timer_sync(&led_blink_timer);
88ee1858d3SLars Kotthoff 
89ee1858d3SLars Kotthoff 	if (!strcmp(buf, "on")) {
90ee1858d3SLars Kotthoff 		auxio_set_led(AUXIO_LED_ON);
91ee1858d3SLars Kotthoff 	} else if (!strcmp(buf, "toggle")) {
92ee1858d3SLars Kotthoff 		led_toggle();
93ee1858d3SLars Kotthoff 	} else if ((*buf > '0') && (*buf <= '9')) {
94db275f2aSKees Cook 		led_blink_timer_timeout = simple_strtoul(buf, NULL, 10);
95db275f2aSKees Cook 		led_blink(&led_blink_timer);
96ee1858d3SLars Kotthoff 	} else if (!strcmp(buf, "load")) {
97db275f2aSKees Cook 		led_blink_timer_timeout = 0;
98db275f2aSKees Cook 		led_blink(&led_blink_timer);
99ee1858d3SLars Kotthoff 	} else {
100ee1858d3SLars Kotthoff 		auxio_set_led(AUXIO_LED_OFF);
101ee1858d3SLars Kotthoff 	}
102ee1858d3SLars Kotthoff 
103ee1858d3SLars Kotthoff 	kfree(buf);
104ee1858d3SLars Kotthoff 
105ee1858d3SLars Kotthoff 	return count;
106ee1858d3SLars Kotthoff }
107ee1858d3SLars Kotthoff 
10897a32539SAlexey Dobriyan static const struct proc_ops led_proc_ops = {
10997a32539SAlexey Dobriyan 	.proc_open	= led_proc_open,
11097a32539SAlexey Dobriyan 	.proc_read	= seq_read,
11197a32539SAlexey Dobriyan 	.proc_lseek	= seq_lseek,
11297a32539SAlexey Dobriyan 	.proc_release	= single_release,
11397a32539SAlexey Dobriyan 	.proc_write	= led_proc_write,
114399dc43bSAlexey Dobriyan };
115b3554aa2SRandy Dunlap #endif
116399dc43bSAlexey Dobriyan 
117ee1858d3SLars Kotthoff #define LED_VERSION	"0.1"
118ee1858d3SLars Kotthoff 
led_init(void)119ee1858d3SLars Kotthoff static int __init led_init(void)
120ee1858d3SLars Kotthoff {
121db275f2aSKees Cook 	timer_setup(&led_blink_timer, led_blink, 0);
122ee1858d3SLars Kotthoff 
123*ae62fbe2SHans de Goede #ifdef CONFIG_PROC_FS
124*ae62fbe2SHans de Goede 	if (!proc_create("led", 0, NULL, &led_proc_ops))
125ee1858d3SLars Kotthoff 		return -ENOMEM;
126*ae62fbe2SHans de Goede #endif
127ee1858d3SLars Kotthoff 	printk(KERN_INFO
128ee1858d3SLars Kotthoff 	       "led: version %s, Lars Kotthoff <metalhead@metalhead.ws>\n",
129ee1858d3SLars Kotthoff 	       LED_VERSION);
130ee1858d3SLars Kotthoff 
131ee1858d3SLars Kotthoff 	return 0;
132ee1858d3SLars Kotthoff }
133ee1858d3SLars Kotthoff 
led_exit(void)134ee1858d3SLars Kotthoff static void __exit led_exit(void)
135ee1858d3SLars Kotthoff {
136ee1858d3SLars Kotthoff 	remove_proc_entry("led", NULL);
137ee1858d3SLars Kotthoff 	del_timer_sync(&led_blink_timer);
138ee1858d3SLars Kotthoff }
139ee1858d3SLars Kotthoff 
140ee1858d3SLars Kotthoff module_init(led_init);
141ee1858d3SLars Kotthoff module_exit(led_exit);
142ee1858d3SLars Kotthoff 
143ee1858d3SLars Kotthoff MODULE_AUTHOR("Lars Kotthoff <metalhead@metalhead.ws>");
144ee1858d3SLars Kotthoff MODULE_DESCRIPTION("Provides control of the front LED on SPARC systems.");
145ee1858d3SLars Kotthoff MODULE_LICENSE("GPL");
146ee1858d3SLars Kotthoff MODULE_VERSION(LED_VERSION);
147