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 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 static DEFINE_SPINLOCK(bcm63xx_gpio_lock
);
22 static u32 gpio_out_low
, gpio_out_high
;
24 static void bcm63xx_gpio_set(struct gpio_chip
*chip
,
25 unsigned gpio
, int val
)
32 if (gpio
>= chip
->ngpio
)
36 reg
= GPIO_DATA_LO_REG
;
40 reg
= GPIO_DATA_HI_REG
;
41 mask
= 1 << (gpio
- 32);
45 spin_lock_irqsave(&bcm63xx_gpio_lock
, flags
);
50 bcm_gpio_writel(*v
, reg
);
51 spin_unlock_irqrestore(&bcm63xx_gpio_lock
, flags
);
54 static int bcm63xx_gpio_get(struct gpio_chip
*chip
, unsigned gpio
)
59 if (gpio
>= chip
->ngpio
)
63 reg
= GPIO_DATA_LO_REG
;
66 reg
= GPIO_DATA_HI_REG
;
67 mask
= 1 << (gpio
- 32);
70 return !!(bcm_gpio_readl(reg
) & mask
);
73 static int bcm63xx_gpio_set_direction(struct gpio_chip
*chip
,
74 unsigned gpio
, int dir
)
81 if (gpio
>= chip
->ngpio
)
85 reg
= GPIO_CTL_LO_REG
;
88 reg
= GPIO_CTL_HI_REG
;
89 mask
= 1 << (gpio
- 32);
92 spin_lock_irqsave(&bcm63xx_gpio_lock
, flags
);
93 tmp
= bcm_gpio_readl(reg
);
94 if (dir
== BCM63XX_GPIO_DIR_IN
)
98 bcm_gpio_writel(tmp
, reg
);
99 spin_unlock_irqrestore(&bcm63xx_gpio_lock
, flags
);
104 static int bcm63xx_gpio_direction_input(struct gpio_chip
*chip
, unsigned gpio
)
106 return bcm63xx_gpio_set_direction(chip
, gpio
, BCM63XX_GPIO_DIR_IN
);
109 static int bcm63xx_gpio_direction_output(struct gpio_chip
*chip
,
110 unsigned gpio
, int value
)
112 bcm63xx_gpio_set(chip
, gpio
, value
);
113 return bcm63xx_gpio_set_direction(chip
, gpio
, BCM63XX_GPIO_DIR_OUT
);
117 static struct gpio_chip bcm63xx_gpio_chip
= {
118 .label
= "bcm63xx-gpio",
119 .direction_input
= bcm63xx_gpio_direction_input
,
120 .direction_output
= bcm63xx_gpio_direction_output
,
121 .get
= bcm63xx_gpio_get
,
122 .set
= bcm63xx_gpio_set
,
126 int __init
bcm63xx_gpio_init(void)
128 gpio_out_low
= bcm_gpio_readl(GPIO_DATA_LO_REG
);
129 gpio_out_high
= bcm_gpio_readl(GPIO_DATA_HI_REG
);
130 bcm63xx_gpio_chip
.ngpio
= bcm63xx_gpio_count();
131 pr_info("registering %d GPIOs\n", bcm63xx_gpio_chip
.ngpio
);
133 return gpiochip_add(&bcm63xx_gpio_chip
);