77636aa86c27a03ed5e3ba816d628dd9c88830a1
[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 u32 gpio_out_low, gpio_out_high;
22
23 static void bcm63xx_gpio_set(struct gpio_chip *chip,
24                                 unsigned gpio, int val)
25 {
26         u32 reg;
27         u32 mask;
28         u32 *v;
29         unsigned long flags;
30
31         if (gpio >= chip->ngpio)
32                 BUG();
33
34         if (gpio < 32) {
35                 reg = GPIO_DATA_LO_REG;
36                 mask = 1 << gpio;
37                 v = &gpio_out_low;
38         } else {
39                 reg = GPIO_DATA_HI_REG;
40                 mask = 1 << (gpio - 32);
41                 v = &gpio_out_high;
42         }
43
44         local_irq_save(flags);
45         if (val)
46                 *v |= mask;
47         else
48                 *v &= ~mask;
49         bcm_gpio_writel(*v, reg);
50         local_irq_restore(flags);
51 }
52
53 static int bcm63xx_gpio_get(struct gpio_chip *chip, unsigned gpio)
54 {
55         u32 reg;
56         u32 mask;
57
58         if (gpio >= chip->ngpio)
59                 BUG();
60
61         if (gpio < 32) {
62                 reg = GPIO_DATA_LO_REG;
63                 mask = 1 << gpio;
64         } else {
65                 reg = GPIO_DATA_HI_REG;
66                 mask = 1 << (gpio - 32);
67         }
68
69         return !!(bcm_gpio_readl(reg) & mask);
70 }
71
72 static int bcm63xx_gpio_set_direction(struct gpio_chip *chip,
73                                         unsigned gpio, int dir)
74 {
75         u32 reg;
76         u32 mask;
77         u32 tmp;
78         unsigned long flags;
79
80         if (gpio >= chip->ngpio)
81                 BUG();
82
83         if (gpio < 32) {
84                 reg = GPIO_CTL_LO_REG;
85                 mask = 1 << gpio;
86         } else {
87                 reg = GPIO_CTL_HI_REG;
88                 mask = 1 << (gpio - 32);
89         }
90
91         local_irq_save(flags);
92         tmp = bcm_gpio_readl(reg);
93         if (dir == GPIO_DIR_IN)
94                 tmp &= ~mask;
95         else
96                 tmp |= mask;
97         bcm_gpio_writel(tmp, reg);
98         local_irq_restore(flags);
99
100         return 0;
101 }
102
103 static int bcm63xx_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
104 {
105         return bcm63xx_gpio_set_direction(chip, gpio, GPIO_DIR_IN);
106 }
107
108 static int bcm63xx_gpio_direction_output(struct gpio_chip *chip,
109                                         unsigned gpio, int value)
110 {
111         bcm63xx_gpio_set(chip, gpio, value);
112         return bcm63xx_gpio_set_direction(chip, gpio, GPIO_DIR_OUT);
113 }
114
115
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,
122         .base                   = 0,
123 };
124
125 int __init bcm63xx_gpio_init(void)
126 {
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);
130 }