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 u32 gpio_out_low, gpio_out_high;
23 static void bcm63xx_gpio_set(struct gpio_chip *chip,
24 unsigned gpio, int val)
31 if (gpio >= chip->ngpio)
35 reg = GPIO_DATA_LO_REG;
39 reg = GPIO_DATA_HI_REG;
40 mask = 1 << (gpio - 32);
44 local_irq_save(flags);
49 bcm_gpio_writel(*v, reg);
50 local_irq_restore(flags);
53 static int bcm63xx_gpio_get(struct gpio_chip *chip, unsigned gpio)
58 if (gpio >= chip->ngpio)
62 reg = GPIO_DATA_LO_REG;
65 reg = GPIO_DATA_HI_REG;
66 mask = 1 << (gpio - 32);
69 return !!(bcm_gpio_readl(reg) & mask);
72 static int bcm63xx_gpio_set_direction(struct gpio_chip *chip,
73 unsigned gpio, int dir)
80 if (gpio >= chip->ngpio)
84 reg = GPIO_CTL_LO_REG;
87 reg = GPIO_CTL_HI_REG;
88 mask = 1 << (gpio - 32);
91 local_irq_save(flags);
92 tmp = bcm_gpio_readl(reg);
93 if (dir == GPIO_DIR_IN)
97 bcm_gpio_writel(tmp, reg);
98 local_irq_restore(flags);
103 static int bcm63xx_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
105 return bcm63xx_gpio_set_direction(chip, gpio, GPIO_DIR_IN);
108 static int bcm63xx_gpio_direction_output(struct gpio_chip *chip,
109 unsigned gpio, int value)
111 bcm63xx_gpio_set(chip, gpio, value);
112 return bcm63xx_gpio_set_direction(chip, gpio, GPIO_DIR_OUT);
116 static struct gpio_chip bcm63xx_gpio_chip = {
117 .label = "bcm63xx-gpio",
118 .direction_input = bcm63xx_gpio_direction_input,
119 .direction_output = bcm63xx_gpio_direction_output,
120 .get = bcm63xx_gpio_get,
121 .set = bcm63xx_gpio_set,
125 int __init bcm63xx_gpio_init(void)
127 bcm63xx_gpio_chip.ngpio = bcm63xx_gpio_count();
128 printk(KERN_INFO "registering %d GPIOs\n", bcm63xx_gpio_chip.ngpio);
129 return gpiochip_add(&bcm63xx_gpio_chip);