2 * ADM5120 generic GPIO API support via GPIOLIB
4 * Copyright (C) 2007-2008 Gabor Juhos <juhosg@openwrt.org>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation.
12 #include <linux/autoconf.h>
13 #include <linux/init.h>
14 #include <linux/types.h>
15 #include <linux/module.h>
16 #include <linux/irq.h>
17 #include <linux/delay.h>
18 #include <linux/platform_device.h>
20 #include <linux/gpio.h>
22 #include <asm/addrspace.h>
24 #include <asm/mach-adm5120/adm5120_defs.h>
25 #include <asm/mach-adm5120/adm5120_info.h>
26 #include <asm/mach-adm5120/adm5120_switch.h>
28 #define GPIO_REG(r) (void __iomem *)(KSEG1ADDR(ADM5120_SWITCH_BASE) + r)
31 void __iomem
*reg
; /* register address */
32 u8 iv_shift
; /* shift amount for input bit */
33 u8 mode_shift
; /* shift amount for mode bits */
36 #define GPIO1_DESC(p, l) { \
37 .reg = GPIO_REG(SWITCH_REG_PORT0_LED + ((p) * 4)), \
38 .iv_shift = LED0_IV_SHIFT + (l), \
39 .mode_shift = (l) * 4 \
42 static struct gpio1_desc gpio1_table
[15] = {
43 GPIO1_DESC(0, 0), GPIO1_DESC(0, 1), GPIO1_DESC(0, 2),
44 GPIO1_DESC(1, 0), GPIO1_DESC(1, 1), GPIO1_DESC(1, 2),
45 GPIO1_DESC(2, 0), GPIO1_DESC(2, 1), GPIO1_DESC(2, 2),
46 GPIO1_DESC(3, 0), GPIO1_DESC(3, 1), GPIO1_DESC(3, 2),
47 GPIO1_DESC(4, 0), GPIO1_DESC(4, 1), GPIO1_DESC(4, 2)
50 static u32 gpio_conf2
;
52 int adm5120_gpio_to_irq(unsigned gpio
)
57 case ADM5120_GPIO_PIN2
:
58 ret
= ADM5120_IRQ_GPIO2
;
60 case ADM5120_GPIO_PIN4
:
61 ret
= ADM5120_IRQ_GPIO4
;
70 EXPORT_SYMBOL(adm5120_gpio_to_irq
);
72 int adm5120_irq_to_gpio(unsigned irq
)
77 case ADM5120_IRQ_GPIO2
:
78 ret
= ADM5120_GPIO_PIN2
;
80 case ADM5120_IRQ_GPIO4
:
81 ret
= ADM5120_GPIO_PIN4
;
90 EXPORT_SYMBOL(adm5120_irq_to_gpio
);
93 * Helpers for GPIO lines in GPIO_CONF0 register
95 #define PIN_IM(p) ((1 << GPIO_CONF0_IM_SHIFT) << p)
96 #define PIN_IV(p) ((1 << GPIO_CONF0_IV_SHIFT) << p)
97 #define PIN_OE(p) ((1 << GPIO_CONF0_OE_SHIFT) << p)
98 #define PIN_OV(p) ((1 << GPIO_CONF0_OV_SHIFT) << p)
100 int __adm5120_gpio0_get_value(unsigned offset
)
105 reg
= GPIO_REG(SWITCH_REG_GPIO_CONF0
);
107 t
= __raw_readl(reg
);
108 if ((t
& PIN_IM(offset
)) != 0)
115 EXPORT_SYMBOL(__adm5120_gpio0_get_value
);
117 void __adm5120_gpio0_set_value(unsigned offset
, int value
)
122 reg
= GPIO_REG(SWITCH_REG_GPIO_CONF0
);
124 t
= __raw_readl(reg
);
126 t
&= ~(PIN_OV(offset
));
130 __raw_writel(t
, reg
);
132 EXPORT_SYMBOL(__adm5120_gpio0_set_value
);
134 static int adm5120_gpio0_get_value(struct gpio_chip
*chip
, unsigned offset
)
136 return __adm5120_gpio0_get_value(offset
);
139 static void adm5120_gpio0_set_value(struct gpio_chip
*chip
,
140 unsigned offset
, int value
)
142 __adm5120_gpio0_set_value(offset
, value
);
145 static int adm5120_gpio0_direction_input(struct gpio_chip
*chip
,
151 reg
= GPIO_REG(SWITCH_REG_GPIO_CONF0
);
153 t
= __raw_readl(reg
);
154 t
&= ~(PIN_OE(offset
));
156 __raw_writel(t
, reg
);
161 static int adm5120_gpio0_direction_output(struct gpio_chip
*chip
,
162 unsigned offset
, int value
)
167 reg
= GPIO_REG(SWITCH_REG_GPIO_CONF0
);
169 t
= __raw_readl(reg
);
170 t
&= ~(PIN_IM(offset
) | PIN_OV(offset
));
176 __raw_writel(t
, reg
);
181 static struct gpio_chip adm5120_gpio0_chip
= {
182 .label
= "adm5120 gpio0",
183 .get
= adm5120_gpio0_get_value
,
184 .set
= adm5120_gpio0_set_value
,
185 .direction_input
= adm5120_gpio0_direction_input
,
186 .direction_output
= adm5120_gpio0_direction_output
,
187 .base
= ADM5120_GPIO_PIN0
,
188 .ngpio
= ADM5120_GPIO_PIN7
- ADM5120_GPIO_PIN0
+ 1,
191 int __adm5120_gpio1_get_value(unsigned offset
)
196 reg
= gpio1_table
[offset
].reg
;
198 t
= __raw_readl(reg
);
199 m
= (t
>> gpio1_table
[offset
].mode_shift
) & LED_MODE_MASK
;
200 if (m
== LED_MODE_INPUT
)
201 return (t
>> gpio1_table
[offset
].iv_shift
) & 1;
203 if (m
== LED_MODE_OUT_LOW
)
208 EXPORT_SYMBOL(__adm5120_gpio1_get_value
);
210 void __adm5120_gpio1_set_value(unsigned offset
, int value
)
215 reg
= gpio1_table
[offset
].reg
;
216 s
= gpio1_table
[offset
].mode_shift
;
218 t
= __raw_readl(reg
);
219 t
&= ~(LED_MODE_MASK
<< s
);
222 case ADM5120_GPIO_LOW
:
223 t
|= (LED_MODE_OUT_LOW
<< s
);
225 case ADM5120_GPIO_FLASH
:
226 case ADM5120_GPIO_LINK
:
227 case ADM5120_GPIO_SPEED
:
228 case ADM5120_GPIO_DUPLEX
:
229 case ADM5120_GPIO_ACT
:
230 case ADM5120_GPIO_COLL
:
231 case ADM5120_GPIO_LINK_ACT
:
232 case ADM5120_GPIO_DUPLEX_COLL
:
233 case ADM5120_GPIO_10M_ACT
:
234 case ADM5120_GPIO_100M_ACT
:
235 t
|= ((value
& LED_MODE_MASK
) << s
);
238 t
|= (LED_MODE_OUT_HIGH
<< s
);
242 __raw_writel(t
, reg
);
244 EXPORT_SYMBOL(__adm5120_gpio1_set_value
);
246 static int adm5120_gpio1_get_value(struct gpio_chip
*chip
, unsigned offset
)
248 return __adm5120_gpio1_get_value(offset
);
251 static void adm5120_gpio1_set_value(struct gpio_chip
*chip
,
252 unsigned offset
, int value
)
254 __adm5120_gpio1_set_value(offset
, value
);
257 static int adm5120_gpio1_direction_input(struct gpio_chip
*chip
,
263 reg
= gpio1_table
[offset
].reg
;
264 t
= __raw_readl(reg
);
265 t
&= ~(LED_MODE_MASK
<< gpio1_table
[offset
].mode_shift
);
266 __raw_writel(t
, reg
);
271 static int adm5120_gpio1_direction_output(struct gpio_chip
*chip
,
272 unsigned offset
, int value
)
274 __adm5120_gpio1_set_value(offset
, value
);
278 static struct gpio_chip adm5120_gpio1_chip
= {
279 .label
= "adm5120 gpio1",
280 .get
= adm5120_gpio1_get_value
,
281 .set
= adm5120_gpio1_set_value
,
282 .direction_input
= adm5120_gpio1_direction_input
,
283 .direction_output
= adm5120_gpio1_direction_output
,
284 .base
= ADM5120_GPIO_P0L0
,
285 .ngpio
= ADM5120_GPIO_P4L2
- ADM5120_GPIO_P0L0
+ 1,
288 void __init
adm5120_gpio_csx0_enable(void)
290 gpio_conf2
|= GPIO_CONF2_CSX0
;
291 SW_WRITE_REG(SWITCH_REG_GPIO_CONF2
, gpio_conf2
);
293 gpio_request(ADM5120_GPIO_PIN1
, "CSX0");
296 void __init
adm5120_gpio_csx1_enable(void)
298 gpio_conf2
|= GPIO_CONF2_CSX1
;
299 SW_WRITE_REG(SWITCH_REG_GPIO_CONF2
, gpio_conf2
);
301 gpio_request(ADM5120_GPIO_PIN3
, "CSX1");
304 void __init
adm5120_gpio_ew_enable(void)
306 gpio_conf2
|= GPIO_CONF2_EW
;
307 SW_WRITE_REG(SWITCH_REG_GPIO_CONF2
, gpio_conf2
);
309 gpio_request(ADM5120_GPIO_PIN0
, "EW");
312 void __init
adm5120_gpio_init(void)
316 SW_WRITE_REG(SWITCH_REG_GPIO_CONF2
, gpio_conf2
);
318 if (adm5120_package_pqfp()) {
319 gpiochip_reserve(ADM5120_GPIO_PIN4
, 4);
320 adm5120_gpio0_chip
.ngpio
= 4;
323 err
= gpiochip_add(&adm5120_gpio0_chip
);
325 panic("cannot add ADM5120 GPIO0 chip, error=%d", err
);
327 err
= gpiochip_add(&adm5120_gpio1_chip
);
329 panic("cannot add ADM5120 GPIO1 chip, error=%d", err
);