01d5a87c4e50d9c00998dc97f7c9dbbb0b12b710
[15.05/openwrt.git] / target / linux / adm5120 / files / arch / mips / adm5120 / gpio.c
1 /*
2  *  $Id$
3  *
4  *  ADM5120 GPIO support
5  *
6  *  Copyright (C) 2007 OpenWrt.org
7  *  Copyright (C) 2007 Gabor Juhos <juhosg at openwrt.org>
8  *
9  *  This program is free software; you can redistribute it and/or
10  *  modify it under the terms of the GNU General Public License
11  *  as published by the Free Software Foundation; either version 2
12  *  of the License, or (at your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the
21  *  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22  *  Boston, MA  02110-1301, USA.
23  *
24  */
25
26 #include <linux/autoconf.h>
27 #include <linux/init.h>
28 #include <linux/types.h>
29 #include <linux/module.h>
30 #include <linux/delay.h>
31 #include <linux/platform_device.h>
32 #include <linux/io.h>
33
34 #include <asm/addrspace.h>
35 #include <asm/gpio.h>
36
37 #include <adm5120_defs.h>
38 #include <adm5120_info.h>
39 #include <adm5120_switch.h>
40 #include <adm5120_irq.h>
41
42 #define GPIO_READ(r)            readl((r))
43 #define GPIO_WRITE(v, r)        writel((v), (r))
44 #define GPIO_REG(r)     (void __iomem *)(KSEG1ADDR(ADM5120_SWITCH_BASE)+r)
45
46 struct adm5120_gpio_line {
47         u32 flags;
48         const char *label;
49         int irq;
50 };
51
52 #define GPIO_FLAG_VALID         0x01
53 #define GPIO_FLAG_USED          0x02
54
55 struct led_desc {
56         void __iomem *reg;      /* LED register address */
57         u8 iv_shift;            /* shift amount for input bit */
58         u8 mode_shift;          /* shift amount for mode bits */
59 };
60
61 #define LED_DESC(p, l) { \
62                 .reg = GPIO_REG(SWITCH_REG_PORT0_LED+((p) * 4)), \
63                 .iv_shift = LED0_IV_SHIFT + (l), \
64                 .mode_shift = (l) * 4 \
65         }
66
67 static struct led_desc led_table[15] = {
68         LED_DESC(0, 0), LED_DESC(0, 1), LED_DESC(0, 2),
69         LED_DESC(1, 0), LED_DESC(1, 1), LED_DESC(1, 2),
70         LED_DESC(2, 0), LED_DESC(2, 1), LED_DESC(2, 2),
71         LED_DESC(3, 0), LED_DESC(3, 1), LED_DESC(3, 2),
72         LED_DESC(4, 0), LED_DESC(4, 1), LED_DESC(4, 2)
73 };
74
75 static struct adm5120_gpio_line adm5120_gpio_map[ADM5120_GPIO_COUNT];
76
77 static u32 gpio_conf2;
78
79 /*-------------------------------------------------------------------------*/
80
81 static inline int gpio_is_invalid(unsigned gpio)
82 {
83         if ((gpio > ADM5120_GPIO_MAX) ||
84                 (adm5120_gpio_map[gpio].flags & GPIO_FLAG_VALID) == 0);
85                 return 0;
86
87         return 1;
88 }
89
90 static inline int gpio_is_used(unsigned gpio)
91 {
92         return ((adm5120_gpio_map[gpio].flags & GPIO_FLAG_USED) != 0);
93 }
94
95 /*-------------------------------------------------------------------------*/
96
97 /*
98  * Helpers for GPIO lines in GPIO_CONF0 register
99  */
100 #define PIN_IM(p)       ((1 << GPIO_CONF0_IM_SHIFT) << p)
101 #define PIN_IV(p)       ((1 << GPIO_CONF0_IV_SHIFT) << p)
102 #define PIN_OE(p)       ((1 << GPIO_CONF0_OE_SHIFT) << p)
103 #define PIN_OV(p)       ((1 << GPIO_CONF0_OV_SHIFT) << p)
104
105 static inline int pins_direction_input(unsigned pin)
106 {
107         void __iomem **reg;
108         u32 t;
109
110         reg = GPIO_REG(SWITCH_REG_GPIO_CONF0);
111
112         t = GPIO_READ(reg);
113         t &= ~(PIN_OE(pin));
114         t |= PIN_IM(pin);
115         GPIO_WRITE(t, reg);
116
117         return 0;
118 }
119
120 static inline int pins_direction_output(unsigned pin, int value)
121 {
122         void __iomem **reg;
123         u32 t;
124
125         reg = GPIO_REG(SWITCH_REG_GPIO_CONF0);
126
127         t = GPIO_READ(reg);
128         t &= ~(PIN_IM(pin) | PIN_OV(pin));
129         t |= PIN_OE(pin);
130
131         if (value)
132                 t |= PIN_OV(pin);
133
134         GPIO_WRITE(t, reg);
135
136         return 0;
137 }
138
139 static inline int pins_get_value(unsigned pin)
140 {
141         void __iomem **reg;
142         u32 t;
143
144         reg = GPIO_REG(SWITCH_REG_GPIO_CONF0);
145
146         t = GPIO_READ(reg);
147         if ((t & PIN_IM(pin)) != 0)
148                 t &= PIN_IV(pin);
149         else
150                 t &= PIN_OV(pin);
151
152         return (t) ? 1 : 0;
153 }
154
155 static inline void pins_set_value(unsigned pin, int value)
156 {
157         void __iomem **reg;
158         u32 t;
159
160         reg = GPIO_REG(SWITCH_REG_GPIO_CONF0);
161
162         t = GPIO_READ(reg);
163         if (value == 0)
164                 t &= ~(PIN_OV(pin));
165         else
166                 t |= PIN_OV(pin);
167
168         GPIO_WRITE(t, reg);
169 }
170
171 /*
172  * Helpers for GPIO lines in PORTx_LED registers
173  */
174 static inline int leds_direction_input(unsigned led)
175 {
176         void __iomem **reg;
177         u32 t;
178
179         reg = led_table[led].reg;
180         t = GPIO_READ(reg);
181         t &= ~(LED_MODE_MASK << led_table[led].mode_shift);
182         GPIO_WRITE(t, reg);
183
184         return 0;
185 }
186
187 static inline int leds_direction_output(unsigned led, int value)
188 {
189         void __iomem **reg;
190         u32 t, s;
191
192         reg = led_table[led].reg;
193         s = led_table[led].mode_shift;
194
195         t = GPIO_READ(reg);
196         t &= ~(LED_MODE_MASK << s);
197
198         switch (value) {
199         case ADM5120_GPIO_LOW:
200                 t |= (LED_MODE_OUT_LOW << s);
201                 break;
202         case ADM5120_GPIO_FLASH:
203         case ADM5120_GPIO_LINK:
204         case ADM5120_GPIO_SPEED:
205         case ADM5120_GPIO_DUPLEX:
206         case ADM5120_GPIO_ACT:
207         case ADM5120_GPIO_COLL:
208         case ADM5120_GPIO_LINK_ACT:
209         case ADM5120_GPIO_DUPLEX_COLL:
210         case ADM5120_GPIO_10M_ACT:
211         case ADM5120_GPIO_100M_ACT:
212                 t |= ((value & LED_MODE_MASK) << s);
213                 break;
214         default:
215                 t |= (LED_MODE_OUT_HIGH << s);
216                 break;
217         }
218
219         GPIO_WRITE(t, reg);
220
221         return 0;
222 }
223
224 static inline int leds_get_value(unsigned led)
225 {
226         void __iomem **reg;
227         u32 t, m;
228
229         reg = led_table[led].reg;
230
231         t = GPIO_READ(reg);
232         m = (t >> led_table[led].mode_shift) & LED_MODE_MASK;
233         if (m == LED_MODE_INPUT)
234                 return (t >> led_table[led].iv_shift) & 1;
235
236         if (m == LED_MODE_OUT_LOW)
237                 return 0;
238
239         return 1;
240 }
241
242 /*-------------------------------------------------------------------------*/
243
244 /*
245  * Main GPIO support routines
246  */
247 int adm5120_gpio_direction_input(unsigned gpio)
248 {
249         if (gpio_is_invalid(gpio))
250                 return -EINVAL;
251
252         if (gpio < ADM5120_GPIO_P0L0)
253                 return pins_direction_input(gpio);
254
255         gpio -= ADM5120_GPIO_P0L0;
256         return leds_direction_input(gpio);
257 }
258 EXPORT_SYMBOL(adm5120_gpio_direction_input);
259
260 int adm5120_gpio_direction_output(unsigned gpio, int value)
261 {
262         if (gpio_is_invalid(gpio))
263                 return -EINVAL;
264
265         if (gpio < ADM5120_GPIO_P0L0)
266                 return pins_direction_output(gpio, value);
267
268         gpio -= ADM5120_GPIO_P0L0;
269         return leds_direction_output(gpio, value);
270 }
271 EXPORT_SYMBOL(adm5120_gpio_direction_output);
272
273 int adm5120_gpio_get_value(unsigned gpio)
274 {
275         if (gpio < ADM5120_GPIO_P0L0)
276                 return pins_get_value(gpio);
277
278         gpio -= ADM5120_GPIO_P0L0;
279         return leds_get_value(gpio);
280 }
281 EXPORT_SYMBOL(adm5120_gpio_get_value);
282
283 void adm5120_gpio_set_value(unsigned gpio, int value)
284 {
285         if (gpio < ADM5120_GPIO_P0L0) {
286                 pins_set_value(gpio, value);
287                 return;
288         }
289
290         gpio -= ADM5120_GPIO_P0L0;
291         leds_direction_output(gpio, value);
292 }
293 EXPORT_SYMBOL(adm5120_gpio_set_value);
294
295 int adm5120_gpio_request(unsigned gpio, const char *label)
296 {
297         if (gpio_is_invalid(gpio))
298                 return -EINVAL;
299
300         if (gpio_is_used(gpio))
301                 return -EBUSY;
302
303         adm5120_gpio_map[gpio].flags |= GPIO_FLAG_USED;
304         adm5120_gpio_map[gpio].label = label;
305
306         return 0;
307 }
308 EXPORT_SYMBOL(adm5120_gpio_request);
309
310 void adm5120_gpio_free(unsigned gpio)
311 {
312         if (gpio_is_invalid(gpio))
313                 return;
314
315         adm5120_gpio_map[gpio].flags &= ~GPIO_FLAG_USED;
316         adm5120_gpio_map[gpio].label = NULL;
317 }
318 EXPORT_SYMBOL(adm5120_gpio_free);
319
320 int adm5120_gpio_to_irq(unsigned gpio)
321 {
322         if (gpio > ADM5120_GPIO_MAX)
323                 return -EINVAL;
324
325         return adm5120_gpio_map[gpio].irq;
326 }
327 EXPORT_SYMBOL(adm5120_gpio_to_irq);
328
329 int adm5120_irq_to_gpio(unsigned irq)
330 {
331         int i;
332
333         for (i = 0; i < ADM5120_GPIO_COUNT; i++)
334                 if (adm5120_gpio_map[i].irq == irq)
335                         return i;
336
337         return -EINVAL;
338 }
339 EXPORT_SYMBOL(adm5120_irq_to_gpio);
340
341 /*-------------------------------------------------------------------------*/
342
343 void __init adm5120_gpio_csx0_enable(void)
344 {
345         gpio_conf2 |= GPIO_CONF2_CSX0;
346         SW_WRITE_REG(GPIO_CONF2, gpio_conf2);
347
348         adm5120_gpio_map[ADM5120_GPIO_PIN1].flags &= ~GPIO_FLAG_VALID;
349         adm5120_gpio_map[ADM5120_GPIO_PIN2].irq = ADM5120_IRQ_GPIO2;
350 }
351
352 void __init adm5120_gpio_csx1_enable(void)
353 {
354         gpio_conf2 |= GPIO_CONF2_CSX1;
355         SW_WRITE_REG(GPIO_CONF2, gpio_conf2);
356
357         adm5120_gpio_map[ADM5120_GPIO_PIN3].flags &= ~GPIO_FLAG_VALID;
358         if (adm5120_package_bga())
359                 adm5120_gpio_map[ADM5120_GPIO_PIN4].irq = ADM5120_IRQ_GPIO4;
360 }
361
362 void __init adm5120_gpio_ew_enable(void)
363 {
364         gpio_conf2 |= GPIO_CONF2_EW;
365         SW_WRITE_REG(GPIO_CONF2, gpio_conf2);
366
367         adm5120_gpio_map[ADM5120_GPIO_PIN0].flags &= ~GPIO_FLAG_VALID;
368 }
369
370 void __init adm5120_gpio_init(void)
371 {
372         int i;
373
374         gpio_conf2 = 0;
375         SW_WRITE_REG(GPIO_CONF2, gpio_conf2);
376
377         for (i = 0; i < ADM5120_GPIO_COUNT; i++)
378                 adm5120_gpio_map[i].flags = GPIO_FLAG_VALID;
379
380         if (adm5120_package_pqfp()) {
381                 adm5120_gpio_map[ADM5120_GPIO_PIN4].flags &= ~GPIO_FLAG_VALID;
382                 adm5120_gpio_map[ADM5120_GPIO_PIN5].flags &= ~GPIO_FLAG_VALID;
383                 adm5120_gpio_map[ADM5120_GPIO_PIN6].flags &= ~GPIO_FLAG_VALID;
384                 adm5120_gpio_map[ADM5120_GPIO_PIN7].flags &= ~GPIO_FLAG_VALID;
385         }
386 }