2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
7 * Copyright (C) 2008-2011 Florian Fainelli <florian@openwrt.org>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/spinlock.h>
13 #include <linux/platform_device.h>
14 #include <linux/gpio.h>
16 #include <bcm63xx_cpu.h>
17 #include <bcm63xx_gpio.h>
18 #include <bcm63xx_io.h>
19 #include <bcm63xx_regs.h>
21 #ifndef BCMCPU_RUNTIME_DETECT
22 #define gpio_out_low_reg GPIO_DATA_LO_REG
23 #ifdef CONFIG_BCM63XX_CPU_6345
24 #ifdef gpio_out_low_reg
25 #undef gpio_out_low_reg
26 #define gpio_out_low_reg GPIO_DATA_LO_REG_6345
27 #endif /* gpio_out_low_reg */
28 #endif /* CONFIG_BCM63XX_CPU_6345 */
30 static inline void bcm63xx_gpio_out_low_reg_init(void)
33 #else /* ! BCMCPU_RUNTIME_DETECT */
34 static u32 gpio_out_low_reg
;
36 static void bcm63xx_gpio_out_low_reg_init(void)
38 switch (bcm63xx_get_cpu_id()) {
40 gpio_out_low_reg
= GPIO_DATA_LO_REG_6345
;
43 gpio_out_low_reg
= GPIO_DATA_LO_REG
;
47 #endif /* ! BCMCPU_RUNTIME_DETECT */
49 static DEFINE_SPINLOCK(bcm63xx_gpio_lock
);
50 static u32 gpio_out_low
, gpio_out_high
;
52 static void bcm63xx_gpio_set(struct gpio_chip
*chip
,
53 unsigned gpio
, int val
)
60 if (gpio
>= chip
->ngpio
)
64 reg
= gpio_out_low_reg
;
68 reg
= GPIO_DATA_HI_REG
;
69 mask
= 1 << (gpio
- 32);
73 spin_lock_irqsave(&bcm63xx_gpio_lock
, flags
);
78 bcm_gpio_writel(*v
, reg
);
79 spin_unlock_irqrestore(&bcm63xx_gpio_lock
, flags
);
82 static int bcm63xx_gpio_get(struct gpio_chip
*chip
, unsigned gpio
)
87 if (gpio
>= chip
->ngpio
)
91 reg
= gpio_out_low_reg
;
94 reg
= GPIO_DATA_HI_REG
;
95 mask
= 1 << (gpio
- 32);
98 return !!(bcm_gpio_readl(reg
) & mask
);
101 static int bcm63xx_gpio_set_direction(struct gpio_chip
*chip
,
102 unsigned gpio
, int dir
)
109 if (gpio
>= chip
->ngpio
)
113 reg
= GPIO_CTL_LO_REG
;
116 reg
= GPIO_CTL_HI_REG
;
117 mask
= 1 << (gpio
- 32);
120 spin_lock_irqsave(&bcm63xx_gpio_lock
, flags
);
121 tmp
= bcm_gpio_readl(reg
);
122 if (dir
== BCM63XX_GPIO_DIR_IN
)
126 bcm_gpio_writel(tmp
, reg
);
127 spin_unlock_irqrestore(&bcm63xx_gpio_lock
, flags
);
132 static int bcm63xx_gpio_direction_input(struct gpio_chip
*chip
, unsigned gpio
)
134 return bcm63xx_gpio_set_direction(chip
, gpio
, BCM63XX_GPIO_DIR_IN
);
137 static int bcm63xx_gpio_direction_output(struct gpio_chip
*chip
,
138 unsigned gpio
, int value
)
140 bcm63xx_gpio_set(chip
, gpio
, value
);
141 return bcm63xx_gpio_set_direction(chip
, gpio
, BCM63XX_GPIO_DIR_OUT
);
145 static struct gpio_chip bcm63xx_gpio_chip
= {
146 .label
= "bcm63xx-gpio",
147 .direction_input
= bcm63xx_gpio_direction_input
,
148 .direction_output
= bcm63xx_gpio_direction_output
,
149 .get
= bcm63xx_gpio_get
,
150 .set
= bcm63xx_gpio_set
,
154 int __init
bcm63xx_gpio_init(void)
156 bcm63xx_gpio_out_low_reg_init();
158 gpio_out_low
= bcm_gpio_readl(gpio_out_low_reg
);
159 if (!BCMCPU_IS_6345())
160 gpio_out_high
= bcm_gpio_readl(GPIO_DATA_HI_REG
);
161 bcm63xx_gpio_chip
.ngpio
= bcm63xx_gpio_count();
162 pr_info("registering %d GPIOs\n", bcm63xx_gpio_chip
.ngpio
);
164 return gpiochip_add(&bcm63xx_gpio_chip
);