[brcm63xx] register gpiochip earlier, allowing gpio-based runtime detection to be...
[openwrt.git] / target / linux / brcm63xx / files / arch / mips / bcm63xx / gpio.c
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 <mbizon@freebox.fr>
7  * Copyright (C) 2008 Florian Fainelli <florian@openwrt.org>
8  */
9
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>
15
16 #include <bcm63xx_cpu.h>
17 #include <bcm63xx_gpio.h>
18 #include <bcm63xx_io.h>
19 #include <bcm63xx_regs.h>
20
21 static void bcm63xx_gpio_set(struct gpio_chip *chip,
22                                 unsigned gpio, int val)
23 {
24         u32 reg;
25         u32 mask;
26         u32 tmp;
27         unsigned long flags;
28
29         if (gpio >= chip->ngpio)
30                 BUG();
31
32         if (gpio < 32) {
33                 reg = GPIO_DATA_LO_REG;
34                 mask = 1 << gpio;
35         } else {
36                 reg = GPIO_DATA_HI_REG;
37                 mask = 1 << (gpio - 32);
38         }
39
40         local_irq_save(flags);
41         tmp = bcm_gpio_readl(reg);
42         if (val)
43                 tmp |= mask;
44         else
45                 tmp &= ~mask;
46         bcm_gpio_writel(tmp, reg);
47         local_irq_restore(flags);
48 }
49
50 static int bcm63xx_gpio_get(struct gpio_chip *chip, unsigned gpio)
51 {
52         u32 reg;
53         u32 mask;
54
55         if (gpio >= chip->ngpio)
56                 BUG();
57
58         if (gpio < 32) {
59                 reg = GPIO_DATA_LO_REG;
60                 mask = 1 << gpio;
61         } else {
62                 reg = GPIO_DATA_HI_REG;
63                 mask = 1 << (gpio - 32);
64         }
65
66         return !!(bcm_gpio_readl(reg) & mask);
67 }
68
69 static int bcm63xx_gpio_set_direction(struct gpio_chip *chip,
70                                         unsigned gpio, int dir)
71 {
72         u32 reg;
73         u32 mask;
74         u32 tmp;
75         unsigned long flags;
76
77         if (gpio >= chip->ngpio)
78                 BUG();
79
80         if (gpio < 32) {
81                 reg = GPIO_CTL_LO_REG;
82                 mask = 1 << gpio;
83         } else {
84                 reg = GPIO_CTL_HI_REG;
85                 mask = 1 << (gpio - 32);
86         }
87
88         local_irq_save(flags);
89         tmp = bcm_gpio_readl(reg);
90         if (dir == GPIO_DIR_IN)
91                 tmp &= ~mask;
92         else
93                 tmp |= mask;
94         bcm_gpio_writel(tmp, reg);
95         local_irq_restore(flags);
96
97         return 0;
98 }
99
100 static int bcm63xx_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
101 {
102         return bcm63xx_gpio_set_direction(chip, gpio, GPIO_DIR_IN);
103 }
104
105 static int bcm63xx_gpio_direction_output(struct gpio_chip *chip,
106                                         unsigned gpio, int value)
107 {
108         bcm63xx_gpio_set(chip, gpio, value);
109         return bcm63xx_gpio_set_direction(chip, gpio, GPIO_DIR_OUT);
110 }
111
112
113 static struct gpio_chip bcm63xx_gpio_chip = {
114         .label                  = "bcm63xx-gpio",
115         .direction_input        = bcm63xx_gpio_direction_input,
116         .direction_output       = bcm63xx_gpio_direction_output,
117         .get                    = bcm63xx_gpio_get,
118         .set                    = bcm63xx_gpio_set,
119         .base                   = 0,
120         .ngpio                  = BCM63XX_GPIO_COUNT,
121 };
122
123 int __init bcm63xx_gpio_init(void)
124 {
125         printk(KERN_INFO "registering %d GPIOs\n", BCM63XX_GPIO_COUNT);
126         return gpiochip_add(&bcm63xx_gpio_chip);
127 }