Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / arch / arm / mach-orion / gpio.c
blob6be65d38dc627632c8eff744e39d1730af41ce3d
1 /*
2 * arch/arm/mach-orion/gpio.c
4 * GPIO functions for Marvell Orion System On Chip
6 * Maintainer: Tzachi Perelstein <tzachi@marvell.com>
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.
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/arch/orion.h>
20 #include "common.h"
22 static DEFINE_SPINLOCK(gpio_lock);
23 static unsigned long gpio_valid[BITS_TO_LONGS(GPIO_MAX)];
24 static const char *gpio_label[GPIO_MAX]; /* non null for allocated GPIOs */
26 void __init orion_gpio_set_valid_pins(u32 pins)
28 gpio_valid[0] = pins;
32 * GENERIC_GPIO primitives
34 int gpio_direction_input(unsigned pin)
36 unsigned long flags;
38 if (pin >= GPIO_MAX || !test_bit(pin, gpio_valid)) {
39 <<<<<<< HEAD:arch/arm/mach-orion/gpio.c
40 pr_debug("%s: invalid GPIO %d\n", __FUNCTION__, pin);
41 =======
42 pr_debug("%s: invalid GPIO %d\n", __func__, pin);
43 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/arm/mach-orion/gpio.c
44 return -EINVAL;
47 spin_lock_irqsave(&gpio_lock, flags);
50 * Some callers might have not used the gpio_request(),
51 * so flag this pin as requested now.
53 if (!gpio_label[pin])
54 gpio_label[pin] = "?";
56 orion_setbits(GPIO_IO_CONF, 1 << pin);
58 spin_unlock_irqrestore(&gpio_lock, flags);
59 return 0;
61 EXPORT_SYMBOL(gpio_direction_input);
63 int gpio_direction_output(unsigned pin, int value)
65 unsigned long flags;
66 int mask;
68 if (pin >= GPIO_MAX || !test_bit(pin, gpio_valid)) {
69 <<<<<<< HEAD:arch/arm/mach-orion/gpio.c
70 pr_debug("%s: invalid GPIO %d\n", __FUNCTION__, pin);
71 =======
72 pr_debug("%s: invalid GPIO %d\n", __func__, pin);
73 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/arm/mach-orion/gpio.c
74 return -EINVAL;
77 spin_lock_irqsave(&gpio_lock, flags);
80 * Some callers might have not used the gpio_request(),
81 * so flag this pin as requested now.
83 if (!gpio_label[pin])
84 gpio_label[pin] = "?";
86 mask = 1 << pin;
87 orion_clrbits(GPIO_BLINK_EN, mask);
88 if (value)
89 orion_setbits(GPIO_OUT, mask);
90 else
91 orion_clrbits(GPIO_OUT, mask);
92 orion_clrbits(GPIO_IO_CONF, mask);
94 spin_unlock_irqrestore(&gpio_lock, flags);
95 return 0;
97 EXPORT_SYMBOL(gpio_direction_output);
99 int gpio_get_value(unsigned pin)
101 int val, mask = 1 << pin;
103 if (orion_read(GPIO_IO_CONF) & mask)
104 val = orion_read(GPIO_DATA_IN) ^ orion_read(GPIO_IN_POL);
105 else
106 val = orion_read(GPIO_OUT);
108 return val & mask;
110 EXPORT_SYMBOL(gpio_get_value);
112 void gpio_set_value(unsigned pin, int value)
114 unsigned long flags;
115 int mask = 1 << pin;
117 spin_lock_irqsave(&gpio_lock, flags);
119 orion_clrbits(GPIO_BLINK_EN, mask);
120 if (value)
121 orion_setbits(GPIO_OUT, mask);
122 else
123 orion_clrbits(GPIO_OUT, mask);
125 spin_unlock_irqrestore(&gpio_lock, flags);
127 EXPORT_SYMBOL(gpio_set_value);
129 void orion_gpio_set_blink(unsigned pin, int blink)
131 unsigned long flags;
132 int mask = 1 << pin;
134 spin_lock_irqsave(&gpio_lock, flags);
136 orion_clrbits(GPIO_OUT, mask);
137 if (blink)
138 orion_setbits(GPIO_BLINK_EN, mask);
139 else
140 orion_clrbits(GPIO_BLINK_EN, mask);
142 spin_unlock_irqrestore(&gpio_lock, flags);
144 EXPORT_SYMBOL(orion_gpio_set_blink);
146 int gpio_request(unsigned pin, const char *label)
148 int ret = 0;
149 unsigned long flags;
151 if (pin >= GPIO_MAX || !test_bit(pin, gpio_valid)) {
152 <<<<<<< HEAD:arch/arm/mach-orion/gpio.c
153 pr_debug("%s: invalid GPIO %d\n", __FUNCTION__, pin);
154 =======
155 pr_debug("%s: invalid GPIO %d\n", __func__, pin);
156 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/arm/mach-orion/gpio.c
157 return -EINVAL;
160 spin_lock_irqsave(&gpio_lock, flags);
162 if (gpio_label[pin]) {
163 pr_debug("%s: GPIO %d already used as %s\n",
164 <<<<<<< HEAD:arch/arm/mach-orion/gpio.c
165 __FUNCTION__, pin, gpio_label[pin]);
166 =======
167 __func__, pin, gpio_label[pin]);
168 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/arm/mach-orion/gpio.c
169 ret = -EBUSY;
170 } else
171 gpio_label[pin] = label ? label : "?";
173 spin_unlock_irqrestore(&gpio_lock, flags);
174 return ret;
176 EXPORT_SYMBOL(gpio_request);
178 void gpio_free(unsigned pin)
180 if (pin >= GPIO_MAX || !test_bit(pin, gpio_valid)) {
181 <<<<<<< HEAD:arch/arm/mach-orion/gpio.c
182 pr_debug("%s: invalid GPIO %d\n", __FUNCTION__, pin);
183 =======
184 pr_debug("%s: invalid GPIO %d\n", __func__, pin);
185 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/arm/mach-orion/gpio.c
186 return;
189 if (!gpio_label[pin])
190 <<<<<<< HEAD:arch/arm/mach-orion/gpio.c
191 pr_warning("%s: GPIO %d already freed\n", __FUNCTION__, pin);
192 =======
193 pr_warning("%s: GPIO %d already freed\n", __func__, pin);
194 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/arm/mach-orion/gpio.c
195 else
196 gpio_label[pin] = NULL;
198 EXPORT_SYMBOL(gpio_free);
200 /* Debug helper */
201 void gpio_display(void)
203 int i;
205 for (i = 0; i < GPIO_MAX; i++) {
206 printk(KERN_DEBUG "Pin-%d: ", i);
208 if (!test_bit(i, gpio_valid)) {
209 printk("non-GPIO\n");
210 } else if (!gpio_label[i]) {
211 printk("GPIO, free\n");
212 } else {
213 printk("GPIO, used by %s, ", gpio_label[i]);
214 if (orion_read(GPIO_IO_CONF) & (1 << i)) {
215 printk("input, active %s, level %s, edge %s\n",
216 ((orion_read(GPIO_IN_POL) >> i) & 1) ? "low" : "high",
217 ((orion_read(GPIO_LEVEL_MASK) >> i) & 1) ? "enabled" : "masked",
218 ((orion_read(GPIO_EDGE_MASK) >> i) & 1) ? "enabled" : "masked");
219 } else {
220 printk("output, val=%d\n", (orion_read(GPIO_OUT) >> i) & 1);
225 printk(KERN_DEBUG "MPP_0_7_CTRL (0x%08x) = 0x%08x\n",
226 MPP_0_7_CTRL, orion_read(MPP_0_7_CTRL));
227 printk(KERN_DEBUG "MPP_8_15_CTRL (0x%08x) = 0x%08x\n",
228 MPP_8_15_CTRL, orion_read(MPP_8_15_CTRL));
229 printk(KERN_DEBUG "MPP_16_19_CTRL (0x%08x) = 0x%08x\n",
230 MPP_16_19_CTRL, orion_read(MPP_16_19_CTRL));
231 printk(KERN_DEBUG "MPP_DEV_CTRL (0x%08x) = 0x%08x\n",
232 MPP_DEV_CTRL, orion_read(MPP_DEV_CTRL));
233 printk(KERN_DEBUG "GPIO_OUT (0x%08x) = 0x%08x\n",
234 GPIO_OUT, orion_read(GPIO_OUT));
235 printk(KERN_DEBUG "GPIO_IO_CONF (0x%08x) = 0x%08x\n",
236 GPIO_IO_CONF, orion_read(GPIO_IO_CONF));
237 printk(KERN_DEBUG "GPIO_BLINK_EN (0x%08x) = 0x%08x\n",
238 GPIO_BLINK_EN, orion_read(GPIO_BLINK_EN));
239 printk(KERN_DEBUG "GPIO_IN_POL (0x%08x) = 0x%08x\n",
240 GPIO_IN_POL, orion_read(GPIO_IN_POL));
241 printk(KERN_DEBUG "GPIO_DATA_IN (0x%08x) = 0x%08x\n",
242 GPIO_DATA_IN, orion_read(GPIO_DATA_IN));
243 printk(KERN_DEBUG "GPIO_LEVEL_MASK (0x%08x) = 0x%08x\n",
244 GPIO_LEVEL_MASK, orion_read(GPIO_LEVEL_MASK));
245 printk(KERN_DEBUG "GPIO_EDGE_CAUSE (0x%08x) = 0x%08x\n",
246 GPIO_EDGE_CAUSE, orion_read(GPIO_EDGE_CAUSE));
247 printk(KERN_DEBUG "GPIO_EDGE_MASK (0x%08x) = 0x%08x\n",
248 GPIO_EDGE_MASK, orion_read(GPIO_EDGE_MASK));