fat: make sure to set d_ops in fat_get_parent
[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 <linux/io.h>
19 #include <asm/gpio.h>
20 #include <mach/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(unsigned pin, int valid)
28 {
29         if (valid)
30                 __set_bit(pin, gpio_valid);
31         else
32                 __clear_bit(pin, gpio_valid);
33 }
34
35 /*
36  * GENERIC_GPIO primitives
37  */
38 int gpio_direction_input(unsigned pin)
39 {
40         unsigned long flags;
41
42         if (pin >= GPIO_MAX || !test_bit(pin, gpio_valid)) {
43                 pr_debug("%s: invalid GPIO %d\n", __func__, pin);
44                 return -EINVAL;
45         }
46
47         spin_lock_irqsave(&gpio_lock, flags);
48
49         /*
50          * Some callers might have not used the gpio_request(),
51          * so flag this pin as requested now.
52          */
53         if (!gpio_label[pin])
54                 gpio_label[pin] = "?";
55
56         orion5x_setbits(GPIO_IO_CONF, 1 << pin);
57
58         spin_unlock_irqrestore(&gpio_lock, flags);
59         return 0;
60 }
61 EXPORT_SYMBOL(gpio_direction_input);
62
63 int gpio_direction_output(unsigned pin, int value)
64 {
65         unsigned long flags;
66         int mask;
67
68         if (pin >= GPIO_MAX || !test_bit(pin, gpio_valid)) {
69                 pr_debug("%s: invalid GPIO %d\n", __func__, pin);
70                 return -EINVAL;
71         }
72
73         spin_lock_irqsave(&gpio_lock, flags);
74
75         /*
76          * Some callers might have not used the gpio_request(),
77          * so flag this pin as requested now.
78          */
79         if (!gpio_label[pin])
80                 gpio_label[pin] = "?";
81
82         mask = 1 << pin;
83         orion5x_clrbits(GPIO_BLINK_EN, mask);
84         if (value)
85                 orion5x_setbits(GPIO_OUT, mask);
86         else
87                 orion5x_clrbits(GPIO_OUT, mask);
88         orion5x_clrbits(GPIO_IO_CONF, mask);
89
90         spin_unlock_irqrestore(&gpio_lock, flags);
91         return 0;
92 }
93 EXPORT_SYMBOL(gpio_direction_output);
94
95 int gpio_get_value(unsigned pin)
96 {
97         int val, mask = 1 << pin;
98
99         if (readl(GPIO_IO_CONF) & mask)
100                 val = readl(GPIO_DATA_IN) ^ readl(GPIO_IN_POL);
101         else
102                 val = readl(GPIO_OUT);
103
104         return val & mask;
105 }
106 EXPORT_SYMBOL(gpio_get_value);
107
108 void gpio_set_value(unsigned pin, int value)
109 {
110         unsigned long flags;
111         int mask = 1 << pin;
112
113         spin_lock_irqsave(&gpio_lock, flags);
114
115         orion5x_clrbits(GPIO_BLINK_EN, mask);
116         if (value)
117                 orion5x_setbits(GPIO_OUT, mask);
118         else
119                 orion5x_clrbits(GPIO_OUT, mask);
120
121         spin_unlock_irqrestore(&gpio_lock, flags);
122 }
123 EXPORT_SYMBOL(gpio_set_value);
124
125 void orion5x_gpio_set_blink(unsigned pin, int blink)
126 {
127         unsigned long flags;
128         int mask = 1 << pin;
129
130         spin_lock_irqsave(&gpio_lock, flags);
131
132         orion5x_clrbits(GPIO_OUT, mask);
133         if (blink)
134                 orion5x_setbits(GPIO_BLINK_EN, mask);
135         else
136                 orion5x_clrbits(GPIO_BLINK_EN, mask);
137
138         spin_unlock_irqrestore(&gpio_lock, flags);
139 }
140 EXPORT_SYMBOL(orion5x_gpio_set_blink);
141
142 int gpio_request(unsigned pin, const char *label)
143 {
144         int ret = 0;
145         unsigned long flags;
146
147         if (pin >= GPIO_MAX || !test_bit(pin, gpio_valid)) {
148                 pr_debug("%s: invalid GPIO %d\n", __func__, pin);
149                 return -EINVAL;
150         }
151
152         spin_lock_irqsave(&gpio_lock, flags);
153
154         if (gpio_label[pin]) {
155                 pr_debug("%s: GPIO %d already used as %s\n",
156                          __func__, pin, gpio_label[pin]);
157                 ret = -EBUSY;
158         } else
159                 gpio_label[pin] = label ? label : "?";
160
161         spin_unlock_irqrestore(&gpio_lock, flags);
162         return ret;
163 }
164 EXPORT_SYMBOL(gpio_request);
165
166 void gpio_free(unsigned pin)
167 {
168         might_sleep();
169
170         if (pin >= GPIO_MAX || !test_bit(pin, gpio_valid)) {
171                 pr_debug("%s: invalid GPIO %d\n", __func__, pin);
172                 return;
173         }
174
175         if (!gpio_label[pin])
176                 pr_warning("%s: GPIO %d already freed\n", __func__, pin);
177         else
178                 gpio_label[pin] = NULL;
179 }
180 EXPORT_SYMBOL(gpio_free);
181
182 /* Debug helper */
183 void gpio_display(void)
184 {
185         int i;
186
187         for (i = 0; i < GPIO_MAX; i++) {
188                 printk(KERN_DEBUG "Pin-%d: ", i);
189
190                 if (!test_bit(i, gpio_valid)) {
191                         printk("non-GPIO\n");
192                 } else if (!gpio_label[i]) {
193                         printk("GPIO, free\n");
194                 } else {
195                         printk("GPIO, used by %s, ", gpio_label[i]);
196                         if (readl(GPIO_IO_CONF) & (1 << i)) {
197                                 printk("input, active %s, level %s, edge %s\n",
198                                 ((readl(GPIO_IN_POL) >> i) & 1) ? "low" : "high",
199                                 ((readl(GPIO_LEVEL_MASK) >> i) & 1) ? "enabled" : "masked",
200                                 ((readl(GPIO_EDGE_MASK) >> i) & 1) ? "enabled" : "masked");
201                         } else {
202                                 printk("output, val=%d\n", (readl(GPIO_OUT) >> i) & 1);
203                         }
204                 }
205         }
206
207         printk(KERN_DEBUG "MPP_0_7_CTRL (0x%08x) = 0x%08x\n",
208                                 MPP_0_7_CTRL, readl(MPP_0_7_CTRL));
209         printk(KERN_DEBUG "MPP_8_15_CTRL (0x%08x) = 0x%08x\n",
210                                 MPP_8_15_CTRL, readl(MPP_8_15_CTRL));
211         printk(KERN_DEBUG "MPP_16_19_CTRL (0x%08x) = 0x%08x\n",
212                                 MPP_16_19_CTRL, readl(MPP_16_19_CTRL));
213         printk(KERN_DEBUG "MPP_DEV_CTRL (0x%08x) = 0x%08x\n",
214                                 MPP_DEV_CTRL, readl(MPP_DEV_CTRL));
215         printk(KERN_DEBUG "GPIO_OUT (0x%08x) = 0x%08x\n",
216                                 GPIO_OUT, readl(GPIO_OUT));
217         printk(KERN_DEBUG "GPIO_IO_CONF (0x%08x) = 0x%08x\n",
218                                 GPIO_IO_CONF, readl(GPIO_IO_CONF));
219         printk(KERN_DEBUG "GPIO_BLINK_EN (0x%08x) = 0x%08x\n",
220                                 GPIO_BLINK_EN, readl(GPIO_BLINK_EN));
221         printk(KERN_DEBUG "GPIO_IN_POL (0x%08x) = 0x%08x\n",
222                                 GPIO_IN_POL, readl(GPIO_IN_POL));
223         printk(KERN_DEBUG "GPIO_DATA_IN (0x%08x) = 0x%08x\n",
224                                 GPIO_DATA_IN, readl(GPIO_DATA_IN));
225         printk(KERN_DEBUG "GPIO_LEVEL_MASK (0x%08x) = 0x%08x\n",
226                                 GPIO_LEVEL_MASK, readl(GPIO_LEVEL_MASK));
227         printk(KERN_DEBUG "GPIO_EDGE_CAUSE (0x%08x) = 0x%08x\n",
228                                 GPIO_EDGE_CAUSE, readl(GPIO_EDGE_CAUSE));
229         printk(KERN_DEBUG "GPIO_EDGE_MASK (0x%08x) = 0x%08x\n",
230                                 GPIO_EDGE_MASK, readl(GPIO_EDGE_MASK));
231 }