Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ieee1394...
[sfrench/cifs-2.6.git] / arch / arm / mach-orion5x / gpio.c
1 /*
2  * arch/arm/mach-orion5x/gpio.c
3  *
4  * GPIO functions for Marvell Orion System On Chip
5  *
6  * Maintainer: Tzachi Perelstein <tzachi@marvell.com>
7  *
8  * This file is licensed under the terms of the GNU General Public
9  * License version 2.  This program is licensed "as is" without any
10  * warranty of any kind, whether express or implied.
11  */
12
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/module.h>
16 #include <linux/spinlock.h>
17 #include <linux/bitops.h>
18 #include <asm/gpio.h>
19 #include <asm/io.h>
20 #include <asm/arch/orion5x.h>
21 #include "common.h"
22
23 static DEFINE_SPINLOCK(gpio_lock);
24 static unsigned long gpio_valid[BITS_TO_LONGS(GPIO_MAX)];
25 static const char *gpio_label[GPIO_MAX];  /* non null for allocated GPIOs */
26
27 void __init orion5x_gpio_set_valid_pins(u32 pins)
28 {
29         gpio_valid[0] = pins;
30 }
31
32 /*
33  * GENERIC_GPIO primitives
34  */
35 int gpio_direction_input(unsigned pin)
36 {
37         unsigned long flags;
38
39         if (pin >= GPIO_MAX || !test_bit(pin, gpio_valid)) {
40                 pr_debug("%s: invalid GPIO %d\n", __func__, pin);
41                 return -EINVAL;
42         }
43
44         spin_lock_irqsave(&gpio_lock, flags);
45
46         /*
47          * Some callers might have not used the gpio_request(),
48          * so flag this pin as requested now.
49          */
50         if (!gpio_label[pin])
51                 gpio_label[pin] = "?";
52
53         orion5x_setbits(GPIO_IO_CONF, 1 << pin);
54
55         spin_unlock_irqrestore(&gpio_lock, flags);
56         return 0;
57 }
58 EXPORT_SYMBOL(gpio_direction_input);
59
60 int gpio_direction_output(unsigned pin, int value)
61 {
62         unsigned long flags;
63         int mask;
64
65         if (pin >= GPIO_MAX || !test_bit(pin, gpio_valid)) {
66                 pr_debug("%s: invalid GPIO %d\n", __func__, pin);
67                 return -EINVAL;
68         }
69
70         spin_lock_irqsave(&gpio_lock, flags);
71
72         /*
73          * Some callers might have not used the gpio_request(),
74          * so flag this pin as requested now.
75          */
76         if (!gpio_label[pin])
77                 gpio_label[pin] = "?";
78
79         mask = 1 << pin;
80         orion5x_clrbits(GPIO_BLINK_EN, mask);
81         if (value)
82                 orion5x_setbits(GPIO_OUT, mask);
83         else
84                 orion5x_clrbits(GPIO_OUT, mask);
85         orion5x_clrbits(GPIO_IO_CONF, mask);
86
87         spin_unlock_irqrestore(&gpio_lock, flags);
88         return 0;
89 }
90 EXPORT_SYMBOL(gpio_direction_output);
91
92 int gpio_get_value(unsigned pin)
93 {
94         int val, mask = 1 << pin;
95
96         if (orion5x_read(GPIO_IO_CONF) & mask)
97                 val = orion5x_read(GPIO_DATA_IN) ^ orion5x_read(GPIO_IN_POL);
98         else
99                 val = orion5x_read(GPIO_OUT);
100
101         return val & mask;
102 }
103 EXPORT_SYMBOL(gpio_get_value);
104
105 void gpio_set_value(unsigned pin, int value)
106 {
107         unsigned long flags;
108         int mask = 1 << pin;
109
110         spin_lock_irqsave(&gpio_lock, flags);
111
112         orion5x_clrbits(GPIO_BLINK_EN, mask);
113         if (value)
114                 orion5x_setbits(GPIO_OUT, mask);
115         else
116                 orion5x_clrbits(GPIO_OUT, mask);
117
118         spin_unlock_irqrestore(&gpio_lock, flags);
119 }
120 EXPORT_SYMBOL(gpio_set_value);
121
122 void orion5x_gpio_set_blink(unsigned pin, int blink)
123 {
124         unsigned long flags;
125         int mask = 1 << pin;
126
127         spin_lock_irqsave(&gpio_lock, flags);
128
129         orion5x_clrbits(GPIO_OUT, mask);
130         if (blink)
131                 orion5x_setbits(GPIO_BLINK_EN, mask);
132         else
133                 orion5x_clrbits(GPIO_BLINK_EN, mask);
134
135         spin_unlock_irqrestore(&gpio_lock, flags);
136 }
137 EXPORT_SYMBOL(orion5x_gpio_set_blink);
138
139 int gpio_request(unsigned pin, const char *label)
140 {
141         int ret = 0;
142         unsigned long flags;
143
144         if (pin >= GPIO_MAX || !test_bit(pin, gpio_valid)) {
145                 pr_debug("%s: invalid GPIO %d\n", __func__, pin);
146                 return -EINVAL;
147         }
148
149         spin_lock_irqsave(&gpio_lock, flags);
150
151         if (gpio_label[pin]) {
152                 pr_debug("%s: GPIO %d already used as %s\n",
153                          __func__, pin, gpio_label[pin]);
154                 ret = -EBUSY;
155         } else
156                 gpio_label[pin] = label ? label : "?";
157
158         spin_unlock_irqrestore(&gpio_lock, flags);
159         return ret;
160 }
161 EXPORT_SYMBOL(gpio_request);
162
163 void gpio_free(unsigned pin)
164 {
165         if (pin >= GPIO_MAX || !test_bit(pin, gpio_valid)) {
166                 pr_debug("%s: invalid GPIO %d\n", __func__, pin);
167                 return;
168         }
169
170         if (!gpio_label[pin])
171                 pr_warning("%s: GPIO %d already freed\n", __func__, pin);
172         else
173                 gpio_label[pin] = NULL;
174 }
175 EXPORT_SYMBOL(gpio_free);
176
177 /* Debug helper */
178 void gpio_display(void)
179 {
180         int i;
181
182         for (i = 0; i < GPIO_MAX; i++) {
183                 printk(KERN_DEBUG "Pin-%d: ", i);
184
185                 if (!test_bit(i, gpio_valid)) {
186                         printk("non-GPIO\n");
187                 } else if (!gpio_label[i]) {
188                         printk("GPIO, free\n");
189                 } else {
190                         printk("GPIO, used by %s, ", gpio_label[i]);
191                         if (orion5x_read(GPIO_IO_CONF) & (1 << i)) {
192                                 printk("input, active %s, level %s, edge %s\n",
193                                 ((orion5x_read(GPIO_IN_POL) >> i) & 1) ? "low" : "high",
194                                 ((orion5x_read(GPIO_LEVEL_MASK) >> i) & 1) ? "enabled" : "masked",
195                                 ((orion5x_read(GPIO_EDGE_MASK) >> i) & 1) ? "enabled" : "masked");
196                         } else {
197                                 printk("output, val=%d\n", (orion5x_read(GPIO_OUT) >> i) & 1);
198                         }
199                 }
200         }
201
202         printk(KERN_DEBUG "MPP_0_7_CTRL (0x%08x) = 0x%08x\n",
203                                 MPP_0_7_CTRL, orion5x_read(MPP_0_7_CTRL));
204         printk(KERN_DEBUG "MPP_8_15_CTRL (0x%08x) = 0x%08x\n",
205                                 MPP_8_15_CTRL, orion5x_read(MPP_8_15_CTRL));
206         printk(KERN_DEBUG "MPP_16_19_CTRL (0x%08x) = 0x%08x\n",
207                                 MPP_16_19_CTRL, orion5x_read(MPP_16_19_CTRL));
208         printk(KERN_DEBUG "MPP_DEV_CTRL (0x%08x) = 0x%08x\n",
209                                 MPP_DEV_CTRL, orion5x_read(MPP_DEV_CTRL));
210         printk(KERN_DEBUG "GPIO_OUT (0x%08x) = 0x%08x\n",
211                                 GPIO_OUT, orion5x_read(GPIO_OUT));
212         printk(KERN_DEBUG "GPIO_IO_CONF (0x%08x) = 0x%08x\n",
213                                 GPIO_IO_CONF, orion5x_read(GPIO_IO_CONF));
214         printk(KERN_DEBUG "GPIO_BLINK_EN (0x%08x) = 0x%08x\n",
215                                 GPIO_BLINK_EN, orion5x_read(GPIO_BLINK_EN));
216         printk(KERN_DEBUG "GPIO_IN_POL (0x%08x) = 0x%08x\n",
217                                 GPIO_IN_POL, orion5x_read(GPIO_IN_POL));
218         printk(KERN_DEBUG "GPIO_DATA_IN (0x%08x) = 0x%08x\n",
219                                 GPIO_DATA_IN, orion5x_read(GPIO_DATA_IN));
220         printk(KERN_DEBUG "GPIO_LEVEL_MASK (0x%08x) = 0x%08x\n",
221                                 GPIO_LEVEL_MASK, orion5x_read(GPIO_LEVEL_MASK));
222         printk(KERN_DEBUG "GPIO_EDGE_CAUSE (0x%08x) = 0x%08x\n",
223                                 GPIO_EDGE_CAUSE, orion5x_read(GPIO_EDGE_CAUSE));
224         printk(KERN_DEBUG "GPIO_EDGE_MASK (0x%08x) = 0x%08x\n",
225                                 GPIO_EDGE_MASK, orion5x_read(GPIO_EDGE_MASK));
226 }