1 /*
2  * arch/arm/mach-shark/leds.c
3  * by Alexander Schulz
4  *
5  * derived from:
6  * arch/arm/kernel/leds-footbridge.c
7  * Copyright (C) 1998-1999 Russell King
8  *
9  * DIGITAL Shark LED control routines.
10  *
11  * The leds use is as follows:
12  *  - Green front - toggles state every 50 timer interrupts
13  *  - Amber front - Unused, this is a dual color led (Amber/Green)
14  *  - Amber back  - On if system is not idle
15  *
16  * Changelog:
17  */
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/spinlock.h>
22 #include <linux/ioport.h>
23 #include <linux/io.h>
24 
25 #include <asm/leds.h>
26 
27 #define LED_STATE_ENABLED	1
28 #define LED_STATE_CLAIMED	2
29 
30 #define SEQUOIA_LED_GREEN       (1<<6)
31 #define SEQUOIA_LED_AMBER       (1<<5)
32 #define SEQUOIA_LED_BACK        (1<<7)
33 
34 static char led_state;
35 static short hw_led_state;
36 static short saved_state;
37 
38 static DEFINE_RAW_SPINLOCK(leds_lock);
39 
sequoia_read(int addr)40 short sequoia_read(int addr) {
41   outw(addr,0x24);
42   return inw(0x26);
43 }
44 
sequoia_write(short value,short addr)45 void sequoia_write(short value,short addr) {
46   outw(addr,0x24);
47   outw(value,0x26);
48 }
49 
sequoia_leds_event(led_event_t evt)50 static void sequoia_leds_event(led_event_t evt)
51 {
52 	unsigned long flags;
53 
54 	raw_spin_lock_irqsave(&leds_lock, flags);
55 
56 	hw_led_state = sequoia_read(0x09);
57 
58 	switch (evt) {
59 	case led_start:
60 		hw_led_state |= SEQUOIA_LED_GREEN;
61 		hw_led_state |= SEQUOIA_LED_AMBER;
62 #ifdef CONFIG_LEDS_CPU
63 		hw_led_state |= SEQUOIA_LED_BACK;
64 #else
65 		hw_led_state &= ~SEQUOIA_LED_BACK;
66 #endif
67 		led_state |= LED_STATE_ENABLED;
68 		break;
69 
70 	case led_stop:
71 		hw_led_state &= ~SEQUOIA_LED_BACK;
72 		hw_led_state |= SEQUOIA_LED_GREEN;
73 		hw_led_state |= SEQUOIA_LED_AMBER;
74 		led_state &= ~LED_STATE_ENABLED;
75 		break;
76 
77 	case led_claim:
78 		led_state |= LED_STATE_CLAIMED;
79 		saved_state = hw_led_state;
80 		hw_led_state &= ~SEQUOIA_LED_BACK;
81 		hw_led_state |= SEQUOIA_LED_GREEN;
82 		hw_led_state |= SEQUOIA_LED_AMBER;
83 		break;
84 
85 	case led_release:
86 		led_state &= ~LED_STATE_CLAIMED;
87 		hw_led_state = saved_state;
88 		break;
89 
90 #ifdef CONFIG_LEDS_TIMER
91 	case led_timer:
92 		if (!(led_state & LED_STATE_CLAIMED))
93 			hw_led_state ^= SEQUOIA_LED_GREEN;
94 		break;
95 #endif
96 
97 #ifdef CONFIG_LEDS_CPU
98 	case led_idle_start:
99 		if (!(led_state & LED_STATE_CLAIMED))
100 			hw_led_state &= ~SEQUOIA_LED_BACK;
101 		break;
102 
103 	case led_idle_end:
104 		if (!(led_state & LED_STATE_CLAIMED))
105 			hw_led_state |= SEQUOIA_LED_BACK;
106 		break;
107 #endif
108 
109 	case led_green_on:
110 		if (led_state & LED_STATE_CLAIMED)
111 			hw_led_state &= ~SEQUOIA_LED_GREEN;
112 		break;
113 
114 	case led_green_off:
115 		if (led_state & LED_STATE_CLAIMED)
116 			hw_led_state |= SEQUOIA_LED_GREEN;
117 		break;
118 
119 	case led_amber_on:
120 		if (led_state & LED_STATE_CLAIMED)
121 			hw_led_state &= ~SEQUOIA_LED_AMBER;
122 		break;
123 
124 	case led_amber_off:
125 		if (led_state & LED_STATE_CLAIMED)
126 			hw_led_state |= SEQUOIA_LED_AMBER;
127 		break;
128 
129 	case led_red_on:
130 		if (led_state & LED_STATE_CLAIMED)
131 			hw_led_state |= SEQUOIA_LED_BACK;
132 		break;
133 
134 	case led_red_off:
135 		if (led_state & LED_STATE_CLAIMED)
136 			hw_led_state &= ~SEQUOIA_LED_BACK;
137 		break;
138 
139 	default:
140 		break;
141 	}
142 
143 	if  (led_state & LED_STATE_ENABLED)
144 		sequoia_write(hw_led_state,0x09);
145 
146 	raw_spin_unlock_irqrestore(&leds_lock, flags);
147 }
148 
leds_init(void)149 static int __init leds_init(void)
150 {
151 	extern void (*leds_event)(led_event_t);
152 	short temp;
153 
154 	leds_event = sequoia_leds_event;
155 
156 	/* Make LEDs independent of power-state */
157 	request_region(0x24,4,"sequoia");
158 	temp = sequoia_read(0x09);
159 	temp |= 1<<10;
160 	sequoia_write(temp,0x09);
161 	leds_event(led_start);
162 	return 0;
163 }
164 
165 __initcall(leds_init);
166