Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * This file is subject to the terms and conditions of the GNU General Public
0003  * License.  See the file "COPYING" in the main directory of this archive
0004  * for more details.
0005  *
0006  * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
0007  * Copyright (C) 2008-2011 Florian Fainelli <florian@openwrt.org>
0008  */
0009 
0010 #include <linux/kernel.h>
0011 #include <linux/init.h>
0012 #include <linux/spinlock.h>
0013 #include <linux/platform_device.h>
0014 #include <linux/gpio/driver.h>
0015 
0016 #include <bcm63xx_cpu.h>
0017 #include <bcm63xx_gpio.h>
0018 #include <bcm63xx_io.h>
0019 #include <bcm63xx_regs.h>
0020 
0021 static u32 gpio_out_low_reg;
0022 
0023 static void bcm63xx_gpio_out_low_reg_init(void)
0024 {
0025     switch (bcm63xx_get_cpu_id()) {
0026     case BCM6345_CPU_ID:
0027         gpio_out_low_reg = GPIO_DATA_LO_REG_6345;
0028         break;
0029     default:
0030         gpio_out_low_reg = GPIO_DATA_LO_REG;
0031         break;
0032     }
0033 }
0034 
0035 static DEFINE_SPINLOCK(bcm63xx_gpio_lock);
0036 static u32 gpio_out_low, gpio_out_high;
0037 
0038 static void bcm63xx_gpio_set(struct gpio_chip *chip,
0039                  unsigned gpio, int val)
0040 {
0041     u32 reg;
0042     u32 mask;
0043     u32 *v;
0044     unsigned long flags;
0045 
0046     BUG_ON(gpio >= chip->ngpio);
0047 
0048     if (gpio < 32) {
0049         reg = gpio_out_low_reg;
0050         mask = 1 << gpio;
0051         v = &gpio_out_low;
0052     } else {
0053         reg = GPIO_DATA_HI_REG;
0054         mask = 1 << (gpio - 32);
0055         v = &gpio_out_high;
0056     }
0057 
0058     spin_lock_irqsave(&bcm63xx_gpio_lock, flags);
0059     if (val)
0060         *v |= mask;
0061     else
0062         *v &= ~mask;
0063     bcm_gpio_writel(*v, reg);
0064     spin_unlock_irqrestore(&bcm63xx_gpio_lock, flags);
0065 }
0066 
0067 static int bcm63xx_gpio_get(struct gpio_chip *chip, unsigned gpio)
0068 {
0069     u32 reg;
0070     u32 mask;
0071 
0072     BUG_ON(gpio >= chip->ngpio);
0073 
0074     if (gpio < 32) {
0075         reg = gpio_out_low_reg;
0076         mask = 1 << gpio;
0077     } else {
0078         reg = GPIO_DATA_HI_REG;
0079         mask = 1 << (gpio - 32);
0080     }
0081 
0082     return !!(bcm_gpio_readl(reg) & mask);
0083 }
0084 
0085 static int bcm63xx_gpio_set_direction(struct gpio_chip *chip,
0086                       unsigned gpio, int dir)
0087 {
0088     u32 reg;
0089     u32 mask;
0090     u32 tmp;
0091     unsigned long flags;
0092 
0093     BUG_ON(gpio >= chip->ngpio);
0094 
0095     if (gpio < 32) {
0096         reg = GPIO_CTL_LO_REG;
0097         mask = 1 << gpio;
0098     } else {
0099         reg = GPIO_CTL_HI_REG;
0100         mask = 1 << (gpio - 32);
0101     }
0102 
0103     spin_lock_irqsave(&bcm63xx_gpio_lock, flags);
0104     tmp = bcm_gpio_readl(reg);
0105     if (dir == BCM63XX_GPIO_DIR_IN)
0106         tmp &= ~mask;
0107     else
0108         tmp |= mask;
0109     bcm_gpio_writel(tmp, reg);
0110     spin_unlock_irqrestore(&bcm63xx_gpio_lock, flags);
0111 
0112     return 0;
0113 }
0114 
0115 static int bcm63xx_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
0116 {
0117     return bcm63xx_gpio_set_direction(chip, gpio, BCM63XX_GPIO_DIR_IN);
0118 }
0119 
0120 static int bcm63xx_gpio_direction_output(struct gpio_chip *chip,
0121                      unsigned gpio, int value)
0122 {
0123     bcm63xx_gpio_set(chip, gpio, value);
0124     return bcm63xx_gpio_set_direction(chip, gpio, BCM63XX_GPIO_DIR_OUT);
0125 }
0126 
0127 
0128 static struct gpio_chip bcm63xx_gpio_chip = {
0129     .label          = "bcm63xx-gpio",
0130     .direction_input    = bcm63xx_gpio_direction_input,
0131     .direction_output   = bcm63xx_gpio_direction_output,
0132     .get            = bcm63xx_gpio_get,
0133     .set            = bcm63xx_gpio_set,
0134     .base           = 0,
0135 };
0136 
0137 int __init bcm63xx_gpio_init(void)
0138 {
0139     bcm63xx_gpio_out_low_reg_init();
0140 
0141     gpio_out_low = bcm_gpio_readl(gpio_out_low_reg);
0142     if (!BCMCPU_IS_6345())
0143         gpio_out_high = bcm_gpio_readl(GPIO_DATA_HI_REG);
0144     bcm63xx_gpio_chip.ngpio = bcm63xx_gpio_count();
0145     pr_info("registering %d GPIOs\n", bcm63xx_gpio_chip.ngpio);
0146 
0147     return gpiochip_add_data(&bcm63xx_gpio_chip, NULL);
0148 }