6 * Copyright (C) 2007 OpenWrt.org
7 * Copyright (C) 2007 Gabor Juhos <juhosg@freemail.hu>
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>
33 #include <asm/addrspace.h>
37 #include <asm/mach-adm5120/adm5120_defs.h>
38 #include <asm/mach-adm5120/adm5120_info.h>
39 #include <asm/mach-adm5120/adm5120_switch.h>
41 typedef void __iomem
* gpio_reg_t
;
42 #define GPIO_READ(r) readl((r))
43 #define GPIO_WRITE(v,r) writel((v),(r))
44 #define GPIO_REG(r) (gpio_reg_t)(KSEG1ADDR(ADM5120_SWITCH_BASE)+r)
46 struct adm5120_gpio_line
{
51 #define GPIO_FLAG_VALID 0x01
52 #define GPIO_FLAG_USED 0x02
55 gpio_reg_t 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(_port,_led) { \
61 .reg = GPIO_REG(SWITCH_REG_PORT0_LED+_port*4), \
62 .iv_shift = LED0_IV_SHIFT+_led, \
63 .mode_shift = _led*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) ( \
101 (g) > ADM5120_GPIO_MAX || \
102 ((adm5120_gpio_map[(g)].flags & GPIO_FLAG_VALID) == 0) \
105 #define gpio_is_used(g) ((adm5120_gpio_map[(g)].flags & GPIO_FLAG_USED) != 0)
108 * Helpers for GPIO lines in GPIO_CONF0 register
110 #define PIN_IM(p) ((1 << GPIO_CONF0_IM_SHIFT) << p)
111 #define PIN_IV(p) ((1 << GPIO_CONF0_IV_SHIFT) << p)
112 #define PIN_OE(p) ((1 << GPIO_CONF0_OE_SHIFT) << p)
113 #define PIN_OV(p) ((1 << GPIO_CONF0_OV_SHIFT) << p)
115 static inline int pins_direction_input(unsigned pin
)
120 reg
= GPIO_REG(SWITCH_REG_GPIO_CONF0
);
130 static inline int pins_direction_output(unsigned pin
, int value
)
135 reg
= GPIO_REG(SWITCH_REG_GPIO_CONF0
);
138 t
&= ~(PIN_IM(pin
) | PIN_OV(pin
));
149 static inline int pins_get_value(unsigned pin
)
154 reg
= GPIO_REG(SWITCH_REG_GPIO_CONF0
);
157 if ((t
& PIN_IM(pin
)) != 0)
165 static inline void pins_set_value(unsigned pin
, int value
)
170 reg
= GPIO_REG(SWITCH_REG_GPIO_CONF0
);
182 * Helpers for GPIO lines in PORTx_LED registers
184 static inline int leds_direction_input(unsigned led
)
189 reg
= led_table
[led
].reg
;
191 t
&= ~(LED_MODE_MASK
<< led_table
[led
].mode_shift
);
197 static inline int leds_direction_output(unsigned led
, int value
)
202 reg
= led_table
[led
].reg
;
203 s
= led_table
[led
].mode_shift
;
206 t
&= ~(LED_MODE_MASK
<< s
);
209 case ADM5120_GPIO_LOW
:
210 t
|= (LED_MODE_OUT_LOW
<< s
);
212 case ADM5120_GPIO_FLASH
:
213 case ADM5120_GPIO_LINK
:
214 case ADM5120_GPIO_SPEED
:
215 case ADM5120_GPIO_DUPLEX
:
216 case ADM5120_GPIO_ACT
:
217 case ADM5120_GPIO_COLL
:
218 case ADM5120_GPIO_LINK_ACT
:
219 case ADM5120_GPIO_DUPLEX_COLL
:
220 case ADM5120_GPIO_10M_ACT
:
221 case ADM5120_GPIO_100M_ACT
:
222 t
|= ((value
& LED_MODE_MASK
) << s
);
225 t
|= (LED_MODE_OUT_HIGH
<< s
);
234 static inline int leds_get_value(unsigned led
)
239 reg
= led_table
[led
].reg
;
242 m
= (t
>> led_table
[led
].mode_shift
) & LED_MODE_MASK
;
243 if (m
== LED_MODE_INPUT
)
244 return (t
>> led_table
[led
].iv_shift
) & 1;
246 if (m
== LED_MODE_OUT_LOW
)
253 * Main GPIO support routines
255 int adm5120_gpio_direction_input(unsigned gpio
)
257 if (gpio_is_invalid(gpio
))
260 if (gpio
< ADM5120_GPIO_P0L0
)
261 return pins_direction_input(gpio
);
263 gpio
-= ADM5120_GPIO_P0L0
;
264 return leds_direction_input(gpio
);
267 int adm5120_gpio_direction_output(unsigned gpio
, int value
)
269 if (gpio_is_invalid(gpio
))
272 if (gpio
< ADM5120_GPIO_P0L0
)
273 return pins_direction_output(gpio
, value
);
275 gpio
-= ADM5120_GPIO_P0L0
;
276 return leds_direction_output(gpio
, value
);
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
);
288 void adm5120_gpio_set_value(unsigned gpio
, int value
)
290 if (gpio
< ADM5120_GPIO_P0L0
) {
291 pins_set_value(gpio
, value
);
295 gpio
-= ADM5120_GPIO_P0L0
;
296 leds_direction_output(gpio
, value
);
299 int adm5120_gpio_request(unsigned gpio
, const char *label
)
301 if (gpio_is_invalid(gpio
))
304 if (gpio_is_used(gpio
))
307 adm5120_gpio_map
[gpio
].flags
|= GPIO_FLAG_USED
;
308 adm5120_gpio_map
[gpio
].label
= label
;
313 void adm5120_gpio_free(unsigned gpio
)
315 if (gpio_is_invalid(gpio
))
318 adm5120_gpio_map
[gpio
].flags
&= ~GPIO_FLAG_USED
;
319 adm5120_gpio_map
[gpio
].label
= NULL
;
322 int adm5120_gpio_to_irq(unsigned gpio
)
324 /* FIXME: not yet implemented */
328 int adm5120_irq_to_gpio(unsigned irq
)
330 /* FIXME: not yet implemented */
334 static int __init
adm5120_gpio_init(void)
338 if (adm5120_package_pqfp()) {
339 /* GPIO pins 4-7 are unavailable in ADM5120P */
340 for (i
=ADM5120_GPIO_PIN4
; i
<=ADM5120_GPIO_PIN7
; i
++)
341 adm5120_gpio_map
[i
].flags
&= ~GPIO_FLAG_VALID
;
347 pure_initcall(adm5120_gpio_init
);
349 EXPORT_SYMBOL(adm5120_gpio_direction_output
);
350 EXPORT_SYMBOL(adm5120_gpio_direction_input
);
351 EXPORT_SYMBOL(adm5120_gpio_get_value
);
352 EXPORT_SYMBOL(adm5120_gpio_set_value
);
353 EXPORT_SYMBOL(adm5120_gpio_request
);
354 EXPORT_SYMBOL(adm5120_gpio_free
);
355 EXPORT_SYMBOL(adm5120_gpio_to_irq
);
356 EXPORT_SYMBOL(adm5120_irq_to_gpio
);