6 * Copyright (C) 2007 OpenWrt.org
7 * Copyright (C) 2007 Gabor Juhos <juhosg at openwrt.org>
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.
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.
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.
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>
34 #include <asm/addrspace.h>
37 #include <adm5120_defs.h>
38 #include <adm5120_info.h>
39 #include <adm5120_switch.h>
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)
46 struct adm5120_gpio_line {
51 #define GPIO_FLAG_VALID 0x01
52 #define GPIO_FLAG_USED 0x02
55 void __iomem *reg; /* LED register address */
56 u8 iv_shift; /* shift amount for input bit */
57 u8 mode_shift; /* shift amount for mode bits */
60 #define LED_DESC(p, l) { \
61 .reg = GPIO_REG(SWITCH_REG_PORT0_LED+((p) * 4)), \
62 .iv_shift = LED0_IV_SHIFT + (l), \
63 .mode_shift = (l) * 4 \
66 static struct led_desc led_table[15] = {
67 LED_DESC(0, 0), LED_DESC(0, 1), LED_DESC(0, 2),
68 LED_DESC(1, 0), LED_DESC(1, 1), LED_DESC(1, 2),
69 LED_DESC(2, 0), LED_DESC(2, 1), LED_DESC(2, 2),
70 LED_DESC(3, 0), LED_DESC(3, 1), LED_DESC(3, 2),
71 LED_DESC(4, 0), LED_DESC(4, 1), LED_DESC(4, 2)
74 static struct adm5120_gpio_line adm5120_gpio_map[ADM5120_GPIO_COUNT] = {
75 [ADM5120_GPIO_PIN0] = {.flags = GPIO_FLAG_VALID},
76 [ADM5120_GPIO_PIN1] = {.flags = GPIO_FLAG_VALID},
77 [ADM5120_GPIO_PIN2] = {.flags = GPIO_FLAG_VALID},
78 [ADM5120_GPIO_PIN3] = {.flags = GPIO_FLAG_VALID},
79 [ADM5120_GPIO_PIN4] = {.flags = GPIO_FLAG_VALID},
80 [ADM5120_GPIO_PIN5] = {.flags = GPIO_FLAG_VALID},
81 [ADM5120_GPIO_PIN6] = {.flags = GPIO_FLAG_VALID},
82 [ADM5120_GPIO_PIN7] = {.flags = GPIO_FLAG_VALID},
83 [ADM5120_GPIO_P0L0] = {.flags = GPIO_FLAG_VALID},
84 [ADM5120_GPIO_P0L1] = {.flags = GPIO_FLAG_VALID},
85 [ADM5120_GPIO_P0L2] = {.flags = GPIO_FLAG_VALID},
86 [ADM5120_GPIO_P1L0] = {.flags = GPIO_FLAG_VALID},
87 [ADM5120_GPIO_P1L1] = {.flags = GPIO_FLAG_VALID},
88 [ADM5120_GPIO_P1L2] = {.flags = GPIO_FLAG_VALID},
89 [ADM5120_GPIO_P2L0] = {.flags = GPIO_FLAG_VALID},
90 [ADM5120_GPIO_P2L1] = {.flags = GPIO_FLAG_VALID},
91 [ADM5120_GPIO_P2L2] = {.flags = GPIO_FLAG_VALID},
92 [ADM5120_GPIO_P3L0] = {.flags = GPIO_FLAG_VALID},
93 [ADM5120_GPIO_P3L1] = {.flags = GPIO_FLAG_VALID},
94 [ADM5120_GPIO_P3L2] = {.flags = GPIO_FLAG_VALID},
95 [ADM5120_GPIO_P4L0] = {.flags = GPIO_FLAG_VALID},
96 [ADM5120_GPIO_P4L1] = {.flags = GPIO_FLAG_VALID},
97 [ADM5120_GPIO_P4L2] = {.flags = GPIO_FLAG_VALID}
100 #define gpio_is_invalid(g) ((g) > ADM5120_GPIO_MAX || \
101 ((adm5120_gpio_map[(g)].flags & GPIO_FLAG_VALID) == 0))
103 #define gpio_is_used(g) ((adm5120_gpio_map[(g)].flags & GPIO_FLAG_USED) != 0)
106 * Helpers for GPIO lines in GPIO_CONF0 register
108 #define PIN_IM(p) ((1 << GPIO_CONF0_IM_SHIFT) << p)
109 #define PIN_IV(p) ((1 << GPIO_CONF0_IV_SHIFT) << p)
110 #define PIN_OE(p) ((1 << GPIO_CONF0_OE_SHIFT) << p)
111 #define PIN_OV(p) ((1 << GPIO_CONF0_OV_SHIFT) << p)
113 static inline int pins_direction_input(unsigned pin)
118 reg = GPIO_REG(SWITCH_REG_GPIO_CONF0);
128 static inline int pins_direction_output(unsigned pin, int value)
133 reg = GPIO_REG(SWITCH_REG_GPIO_CONF0);
136 t &= ~(PIN_IM(pin) | PIN_OV(pin));
147 static inline int pins_get_value(unsigned pin)
152 reg = GPIO_REG(SWITCH_REG_GPIO_CONF0);
155 if ((t & PIN_IM(pin)) != 0)
163 static inline void pins_set_value(unsigned pin, int value)
168 reg = GPIO_REG(SWITCH_REG_GPIO_CONF0);
180 * Helpers for GPIO lines in PORTx_LED registers
182 static inline int leds_direction_input(unsigned led)
187 reg = led_table[led].reg;
189 t &= ~(LED_MODE_MASK << led_table[led].mode_shift);
195 static inline int leds_direction_output(unsigned led, int value)
200 reg = led_table[led].reg;
201 s = led_table[led].mode_shift;
204 t &= ~(LED_MODE_MASK << s);
207 case ADM5120_GPIO_LOW:
208 t |= (LED_MODE_OUT_LOW << s);
210 case ADM5120_GPIO_FLASH:
211 case ADM5120_GPIO_LINK:
212 case ADM5120_GPIO_SPEED:
213 case ADM5120_GPIO_DUPLEX:
214 case ADM5120_GPIO_ACT:
215 case ADM5120_GPIO_COLL:
216 case ADM5120_GPIO_LINK_ACT:
217 case ADM5120_GPIO_DUPLEX_COLL:
218 case ADM5120_GPIO_10M_ACT:
219 case ADM5120_GPIO_100M_ACT:
220 t |= ((value & LED_MODE_MASK) << s);
223 t |= (LED_MODE_OUT_HIGH << s);
232 static inline int leds_get_value(unsigned led)
237 reg = led_table[led].reg;
240 m = (t >> led_table[led].mode_shift) & LED_MODE_MASK;
241 if (m == LED_MODE_INPUT)
242 return (t >> led_table[led].iv_shift) & 1;
244 if (m == LED_MODE_OUT_LOW)
251 * Main GPIO support routines
253 int adm5120_gpio_direction_input(unsigned gpio)
255 if (gpio_is_invalid(gpio))
258 if (gpio < ADM5120_GPIO_P0L0)
259 return pins_direction_input(gpio);
261 gpio -= ADM5120_GPIO_P0L0;
262 return leds_direction_input(gpio);
264 EXPORT_SYMBOL(adm5120_gpio_direction_input);
266 int adm5120_gpio_direction_output(unsigned gpio, int value)
268 if (gpio_is_invalid(gpio))
271 if (gpio < ADM5120_GPIO_P0L0)
272 return pins_direction_output(gpio, value);
274 gpio -= ADM5120_GPIO_P0L0;
275 return leds_direction_output(gpio, value);
277 EXPORT_SYMBOL(adm5120_gpio_direction_output);
279 int adm5120_gpio_get_value(unsigned gpio)
281 if (gpio < ADM5120_GPIO_P0L0)
282 return pins_get_value(gpio);
284 gpio -= ADM5120_GPIO_P0L0;
285 return leds_get_value(gpio);
287 EXPORT_SYMBOL(adm5120_gpio_get_value);
289 void adm5120_gpio_set_value(unsigned gpio, int value)
291 if (gpio < ADM5120_GPIO_P0L0) {
292 pins_set_value(gpio, value);
296 gpio -= ADM5120_GPIO_P0L0;
297 leds_direction_output(gpio, value);
299 EXPORT_SYMBOL(adm5120_gpio_set_value);
301 int adm5120_gpio_request(unsigned gpio, const char *label)
303 if (gpio_is_invalid(gpio))
306 if (gpio_is_used(gpio))
309 adm5120_gpio_map[gpio].flags |= GPIO_FLAG_USED;
310 adm5120_gpio_map[gpio].label = label;
314 EXPORT_SYMBOL(adm5120_gpio_request);
316 void adm5120_gpio_free(unsigned gpio)
318 if (gpio_is_invalid(gpio))
321 adm5120_gpio_map[gpio].flags &= ~GPIO_FLAG_USED;
322 adm5120_gpio_map[gpio].label = NULL;
324 EXPORT_SYMBOL(adm5120_gpio_free);
326 int adm5120_gpio_to_irq(unsigned gpio)
328 /* FIXME: not yet implemented */
331 EXPORT_SYMBOL(adm5120_gpio_to_irq);
333 int adm5120_irq_to_gpio(unsigned irq)
335 /* FIXME: not yet implemented */
338 EXPORT_SYMBOL(adm5120_irq_to_gpio);
340 static int __init adm5120_gpio_init(void)
344 if (adm5120_package_pqfp()) {
345 /* GPIO pins 4-7 are unavailable in ADM5120P */
346 for (i = ADM5120_GPIO_PIN4; i <= ADM5120_GPIO_PIN7; i++)
347 adm5120_gpio_map[i].flags &= ~GPIO_FLAG_VALID;
353 pure_initcall(adm5120_gpio_init);