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
);
208 t
|= (LED_MODE_OUT_HIGH
<< s
);
210 t
|= (LED_MODE_OUT_LOW
<< s
);
217 static inline int leds_get_value(unsigned led
)
222 reg
= led_table
[led
].reg
;
225 m
= (t
>> led_table
[led
].mode_shift
) & LED_MODE_MASK
;
226 if (m
== LED_MODE_INPUT
)
227 return (t
>> led_table
[led
].iv_shift
) & 1;
229 if (m
== LED_MODE_OUT_LOW
)
235 static inline void leds_set_value(unsigned led
, int value
)
240 reg
= led_table
[led
].reg
;
241 s
= led_table
[led
].mode_shift
;
244 t
&= ~(LED_MODE_MASK
<< s
);
246 t
|= (LED_MODE_OUT_HIGH
<< s
);
248 t
|= (LED_MODE_OUT_LOW
<< s
);
254 * Main GPIO support routines
256 int adm5120_gpio_direction_input(unsigned gpio
)
258 if (gpio_is_invalid(gpio
))
261 if (gpio
< ADM5120_GPIO_P0L0
)
262 return pins_direction_input(gpio
);
264 gpio
-= ADM5120_GPIO_P0L0
;
265 return leds_direction_input(gpio
);
268 int adm5120_gpio_direction_output(unsigned gpio
, int value
)
270 if (gpio_is_invalid(gpio
))
273 if (gpio
< ADM5120_GPIO_P0L0
)
274 return pins_direction_output(gpio
, value
);
276 gpio
-= ADM5120_GPIO_P0L0
;
277 return leds_direction_output(gpio
, value
);
280 int adm5120_gpio_get_value(unsigned gpio
)
282 if (gpio
< ADM5120_GPIO_P0L0
)
283 return pins_get_value(gpio
);
285 gpio
-= ADM5120_GPIO_P0L0
;
286 return leds_get_value(gpio
);
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_set_value(gpio
, value
);
300 int adm5120_gpio_request(unsigned gpio
, const char *label
)
302 if (gpio_is_invalid(gpio
))
305 if (gpio_is_used(gpio
))
308 adm5120_gpio_map
[gpio
].flags
|= GPIO_FLAG_USED
;
309 adm5120_gpio_map
[gpio
].label
= label
;
314 void adm5120_gpio_free(unsigned gpio
)
316 if (gpio_is_invalid(gpio
))
319 adm5120_gpio_map
[gpio
].flags
&= ~GPIO_FLAG_USED
;
320 adm5120_gpio_map
[gpio
].label
= NULL
;
323 int adm5120_gpio_to_irq(unsigned gpio
)
325 /* FIXME: not yet implemented */
329 int adm5120_irq_to_gpio(unsigned irq
)
331 /* FIXME: not yet implemented */
335 static int __init
adm5120_gpio_init(void)
339 if (adm5120_package_pqfp()) {
340 /* GPIO pins 4-7 are unavailable in ADM5120P */
341 for (i
=ADM5120_GPIO_PIN4
; i
<=ADM5120_GPIO_PIN7
; i
++)
342 adm5120_gpio_map
[i
].flags
&= ~GPIO_FLAG_VALID
;
348 pure_initcall(adm5120_gpio_init
);
350 EXPORT_SYMBOL(adm5120_gpio_direction_output
);
351 EXPORT_SYMBOL(adm5120_gpio_direction_input
);
352 EXPORT_SYMBOL(adm5120_gpio_get_value
);
353 EXPORT_SYMBOL(adm5120_gpio_set_value
);
354 EXPORT_SYMBOL(adm5120_gpio_request
);
355 EXPORT_SYMBOL(adm5120_gpio_free
);
356 EXPORT_SYMBOL(adm5120_gpio_to_irq
);
357 EXPORT_SYMBOL(adm5120_irq_to_gpio
);