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/driver.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_reg;
23 static void bcm63xx_gpio_out_low_reg_init(void)
25 switch (bcm63xx_get_cpu_id()) {
27 gpio_out_low_reg = GPIO_DATA_LO_REG_6345;
30 gpio_out_low_reg = GPIO_DATA_LO_REG;
35 static DEFINE_SPINLOCK(bcm63xx_gpio_lock);
36 static u32 gpio_out_low, gpio_out_high;
38 static void bcm63xx_gpio_set(struct gpio_chip *chip,
39 unsigned gpio, int val)
46 if (gpio >= chip->ngpio)
50 reg = gpio_out_low_reg;
54 reg = GPIO_DATA_HI_REG;
55 mask = 1 << (gpio - 32);
59 spin_lock_irqsave(&bcm63xx_gpio_lock, flags);
64 bcm_gpio_writel(*v, reg);
65 spin_unlock_irqrestore(&bcm63xx_gpio_lock, flags);
68 static int bcm63xx_gpio_get(struct gpio_chip *chip, unsigned gpio)
73 if (gpio >= chip->ngpio)
77 reg = gpio_out_low_reg;
80 reg = GPIO_DATA_HI_REG;
81 mask = 1 << (gpio - 32);
84 return !!(bcm_gpio_readl(reg) & mask);
87 static int bcm63xx_gpio_set_direction(struct gpio_chip *chip,
88 unsigned gpio, int dir)
95 if (gpio >= chip->ngpio)
99 reg = GPIO_CTL_LO_REG;
102 reg = GPIO_CTL_HI_REG;
103 mask = 1 << (gpio - 32);
106 spin_lock_irqsave(&bcm63xx_gpio_lock, flags);
107 tmp = bcm_gpio_readl(reg);
108 if (dir == BCM63XX_GPIO_DIR_IN)
112 bcm_gpio_writel(tmp, reg);
113 spin_unlock_irqrestore(&bcm63xx_gpio_lock, flags);
118 static int bcm63xx_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
120 return bcm63xx_gpio_set_direction(chip, gpio, BCM63XX_GPIO_DIR_IN);
123 static int bcm63xx_gpio_direction_output(struct gpio_chip *chip,
124 unsigned gpio, int value)
126 bcm63xx_gpio_set(chip, gpio, value);
127 return bcm63xx_gpio_set_direction(chip, gpio, BCM63XX_GPIO_DIR_OUT);
131 static struct gpio_chip bcm63xx_gpio_chip = {
132 .label = "bcm63xx-gpio",
133 .direction_input = bcm63xx_gpio_direction_input,
134 .direction_output = bcm63xx_gpio_direction_output,
135 .get = bcm63xx_gpio_get,
136 .set = bcm63xx_gpio_set,
140 int __init bcm63xx_gpio_init(void)
142 bcm63xx_gpio_out_low_reg_init();
144 gpio_out_low = bcm_gpio_readl(gpio_out_low_reg);
145 if (!BCMCPU_IS_6345())
146 gpio_out_high = bcm_gpio_readl(GPIO_DATA_HI_REG);
147 bcm63xx_gpio_chip.ngpio = bcm63xx_gpio_count();
148 pr_info("registering %d GPIOs\n", bcm63xx_gpio_chip.ngpio);
150 return gpiochip_add_data(&bcm63xx_gpio_chip, NULL);