| 1 |
/* |
| 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 |
| 4 |
* for more details. |
| 5 |
* |
| 6 |
* Copyright (C) 2008 Maxime Bizon <[email protected]> |
| 7 |
* Copyright (C) 2008-2011 Florian Fainelli <[email protected]> |
| 8 |
*/ |
| 9 |
|
| 10 |
#include <linux/kernel.h> |
| 11 |
#include <linux/init.h> |
| 12 |
#include <linux/spinlock.h> |
| 13 |
#include <linux/platform_device.h> |
| 14 |
#include <linux/gpio/driver.h> |
| 15 |
|
| 16 |
#include <bcm63xx_cpu.h> |
| 17 |
#include <bcm63xx_gpio.h> |
| 18 |
#include <bcm63xx_io.h> |
| 19 |
#include <bcm63xx_regs.h> |
| 20 |
|
| 21 |
static u32 gpio_out_low_reg; |
| 22 |
|
| 23 |
static void bcm63xx_gpio_out_low_reg_init(void) |
| 24 |
{ |
| 25 |
switch (bcm63xx_get_cpu_id()) { |
| 26 |
case BCM6345_CPU_ID: |
| 27 |
gpio_out_low_reg = GPIO_DATA_LO_REG_6345; |
| 28 |
break; |
| 29 |
default: |
| 30 |
gpio_out_low_reg = GPIO_DATA_LO_REG; |
| 31 |
break; |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
static DEFINE_SPINLOCK(bcm63xx_gpio_lock); |
| 36 |
static u32 gpio_out_low, gpio_out_high; |
| 37 |
|
| 38 |
static int bcm63xx_gpio_set(struct gpio_chip *chip, unsigned int gpio, int val) |
| 39 |
{ |
| 40 |
u32 reg; |
| 41 |
u32 mask; |
| 42 |
u32 *v; |
| 43 |
unsigned long flags; |
| 44 |
|
| 45 |
BUG_ON(gpio >= chip->ngpio); |
| 46 |
|
| 47 |
if (gpio < 32) { |
| 48 |
reg = gpio_out_low_reg; |
| 49 |
mask = 1 << gpio; |
| 50 |
v = &gpio_out_low; |
| 51 |
} else { |
| 52 |
reg = GPIO_DATA_HI_REG; |
| 53 |
mask = 1 << (gpio - 32); |
| 54 |
v = &gpio_out_high; |
| 55 |
} |
| 56 |
|
| 57 |
spin_lock_irqsave(&bcm63xx_gpio_lock, flags); |
| 58 |
if (val) |
| 59 |
*v |= mask; |
| 60 |
else |
| 61 |
*v &= ~mask; |
| 62 |
bcm_gpio_writel(*v, reg); |
| 63 |
spin_unlock_irqrestore(&bcm63xx_gpio_lock, flags); |
| 64 |
|
| 65 |
return 0; |
| 66 |
} |
| 67 |
|
| 68 |
static int bcm63xx_gpio_get(struct gpio_chip *chip, unsigned gpio) |
| 69 |
{ |
| 70 |
u32 reg; |
| 71 |
u32 mask; |
| 72 |
|
| 73 |
BUG_ON(gpio >= chip->ngpio); |
| 74 |
|
| 75 |
if (gpio < 32) { |
| 76 |
reg = gpio_out_low_reg; |
| 77 |
mask = 1 << gpio; |
| 78 |
} else { |
| 79 |
reg = GPIO_DATA_HI_REG; |
| 80 |
mask = 1 << (gpio - 32); |
| 81 |
} |
| 82 |
|
| 83 |
return !!(bcm_gpio_readl(reg) & mask); |
| 84 |
} |
| 85 |
|
| 86 |
static int bcm63xx_gpio_set_direction(struct gpio_chip *chip, |
| 87 |
unsigned gpio, int dir) |
| 88 |
{ |
| 89 |
u32 reg; |
| 90 |
u32 mask; |
| 91 |
u32 tmp; |
| 92 |
unsigned long flags; |
| 93 |
|
| 94 |
BUG_ON(gpio >= chip->ngpio); |
| 95 |
|
| 96 |
if (gpio < 32) { |
| 97 |
reg = GPIO_CTL_LO_REG; |
| 98 |
mask = 1 << gpio; |
| 99 |
} else { |
| 100 |
reg = GPIO_CTL_HI_REG; |
| 101 |
mask = 1 << (gpio - 32); |
| 102 |
} |
| 103 |
|
| 104 |
spin_lock_irqsave(&bcm63xx_gpio_lock, flags); |
| 105 |
tmp = bcm_gpio_readl(reg); |
| 106 |
if (dir == BCM63XX_GPIO_DIR_IN) |
| 107 |
tmp &= ~mask; |
| 108 |
else |
| 109 |
tmp |= mask; |
| 110 |
bcm_gpio_writel(tmp, reg); |
| 111 |
spin_unlock_irqrestore(&bcm63xx_gpio_lock, flags); |
| 112 |
|
| 113 |
return 0; |
| 114 |
} |
| 115 |
|
| 116 |
static int bcm63xx_gpio_direction_input(struct gpio_chip *chip, unsigned gpio) |
| 117 |
{ |
| 118 |
return bcm63xx_gpio_set_direction(chip, gpio, BCM63XX_GPIO_DIR_IN); |
| 119 |
} |
| 120 |
|
| 121 |
static int bcm63xx_gpio_direction_output(struct gpio_chip *chip, |
| 122 |
unsigned gpio, int value) |
| 123 |
{ |
| 124 |
bcm63xx_gpio_set(chip, gpio, value); |
| 125 |
return bcm63xx_gpio_set_direction(chip, gpio, BCM63XX_GPIO_DIR_OUT); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
static struct gpio_chip bcm63xx_gpio_chip = { |
| 130 |
.label = "bcm63xx-gpio", |
| 131 |
.direction_input = bcm63xx_gpio_direction_input, |
| 132 |
.direction_output = bcm63xx_gpio_direction_output, |
| 133 |
.get = bcm63xx_gpio_get, |
| 134 |
.set = bcm63xx_gpio_set, |
| 135 |
.base = 0, |
| 136 |
}; |
| 137 |
|
| 138 |
int __init bcm63xx_gpio_init(void) |
| 139 |
{ |
| 140 |
bcm63xx_gpio_out_low_reg_init(); |
| 141 |
|
| 142 |
gpio_out_low = bcm_gpio_readl(gpio_out_low_reg); |
| 143 |
if (!BCMCPU_IS_6345()) |
| 144 |
gpio_out_high = bcm_gpio_readl(GPIO_DATA_HI_REG); |
| 145 |
bcm63xx_gpio_chip.ngpio = bcm63xx_gpio_count(); |
| 146 |
pr_info("registering %d GPIOs\n", bcm63xx_gpio_chip.ngpio); |
| 147 |
|
| 148 |
return gpiochip_add_data(&bcm63xx_gpio_chip, NULL); |
| 149 |
} |