Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / arch / arm / mach-orion / gpio.c
blobf713818c66a3dd3814f45c44fe31f02fa7e5dc64
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 pr_debug("%s: invalid GPIO %d\n", __func__, pin);
40 return -EINVAL;
43 spin_lock_irqsave(&gpio_lock, flags);
46 * Some callers might have not used the gpio_request(),
47 * so flag this pin as requested now.
49 if (!gpio_label[pin])
50 gpio_label[pin] = "?";
52 orion_setbits(GPIO_IO_CONF, 1 << pin);
54 spin_unlock_irqrestore(&gpio_lock, flags);
55 return 0;
57 EXPORT_SYMBOL(gpio_direction_input);
59 int gpio_direction_output(unsigned pin, int value)
61 unsigned long flags;
62 int mask;
64 if (pin >= GPIO_MAX || !test_bit(pin, gpio_valid)) {
65 pr_debug("%s: invalid GPIO %d\n", __func__, pin);
66 return -EINVAL;
69 spin_lock_irqsave(&gpio_lock, flags);
72 * Some callers might have not used the gpio_request(),
73 * so flag this pin as requested now.
75 if (!gpio_label[pin])
76 gpio_label[pin] = "?";
78 mask = 1 << pin;
79 orion_clrbits(GPIO_BLINK_EN, mask);
80 if (value)
81 orion_setbits(GPIO_OUT, mask);
82 else
83 orion_clrbits(GPIO_OUT, mask);
84 orion_clrbits(GPIO_IO_CONF, mask);
86 spin_unlock_irqrestore(&gpio_lock, flags);
87 return 0;
89 EXPORT_SYMBOL(gpio_direction_output);
91 int gpio_get_value(unsigned pin)
93 int val, mask = 1 << pin;
95 if (orion_read(GPIO_IO_CONF) & mask)
96 val = orion_read(GPIO_DATA_IN) ^ orion_read(GPIO_IN_POL);
97 else
98 val = orion_read(GPIO_OUT);
100 return val & mask;
102 EXPORT_SYMBOL(gpio_get_value);
104 void gpio_set_value(unsigned pin, int value)
106 unsigned long flags;
107 int mask = 1 << pin;
109 spin_lock_irqsave(&gpio_lock, flags);
111 orion_clrbits(GPIO_BLINK_EN, mask);
112 if (value)
113 orion_setbits(GPIO_OUT, mask);
114 else
115 orion_clrbits(GPIO_OUT, mask);
117 spin_unlock_irqrestore(&gpio_lock, flags);
119 EXPORT_SYMBOL(gpio_set_value);
121 void orion_gpio_set_blink(unsigned pin, int blink)
123 unsigned long flags;
124 int mask = 1 << pin;
126 spin_lock_irqsave(&gpio_lock, flags);
128 orion_clrbits(GPIO_OUT, mask);
129 if (blink)
130 orion_setbits(GPIO_BLINK_EN, mask);
131 else
132 orion_clrbits(GPIO_BLINK_EN, mask);
134 spin_unlock_irqrestore(&gpio_lock, flags);
136 EXPORT_SYMBOL(orion_gpio_set_blink);
138 int gpio_request(unsigned pin, const char *label)
140 int ret = 0;
141 unsigned long flags;
143 if (pin >= GPIO_MAX || !test_bit(pin, gpio_valid)) {
144 pr_debug("%s: invalid GPIO %d\n", __func__, pin);
145 return -EINVAL;
148 spin_lock_irqsave(&gpio_lock, flags);
150 if (gpio_label[pin]) {
151 pr_debug("%s: GPIO %d already used as %s\n",
152 __func__, pin, gpio_label[pin]);
153 ret = -EBUSY;
154 } else
155 gpio_label[pin] = label ? label : "?";
157 spin_unlock_irqrestore(&gpio_lock, flags);
158 return ret;
160 EXPORT_SYMBOL(gpio_request);
162 void gpio_free(unsigned pin)
164 if (pin >= GPIO_MAX || !test_bit(pin, gpio_valid)) {
165 pr_debug("%s: invalid GPIO %d\n", __func__, pin);
166 return;
169 if (!gpio_label[pin])
170 pr_warning("%s: GPIO %d already freed\n", __func__, pin);
171 else
172 gpio_label[pin] = NULL;
174 EXPORT_SYMBOL(gpio_free);
176 /* Debug helper */
177 void gpio_display(void)
179 int i;
181 for (i = 0; i < GPIO_MAX; i++) {
182 printk(KERN_DEBUG "Pin-%d: ", i);
184 if (!test_bit(i, gpio_valid)) {
185 printk("non-GPIO\n");
186 } else if (!gpio_label[i]) {
187 printk("GPIO, free\n");
188 } else {
189 printk("GPIO, used by %s, ", gpio_label[i]);
190 if (orion_read(GPIO_IO_CONF) & (1 << i)) {
191 printk("input, active %s, level %s, edge %s\n",
192 ((orion_read(GPIO_IN_POL) >> i) & 1) ? "low" : "high",
193 ((orion_read(GPIO_LEVEL_MASK) >> i) & 1) ? "enabled" : "masked",
194 ((orion_read(GPIO_EDGE_MASK) >> i) & 1) ? "enabled" : "masked");
195 } else {
196 printk("output, val=%d\n", (orion_read(GPIO_OUT) >> i) & 1);
201 printk(KERN_DEBUG "MPP_0_7_CTRL (0x%08x) = 0x%08x\n",
202 MPP_0_7_CTRL, orion_read(MPP_0_7_CTRL));
203 printk(KERN_DEBUG "MPP_8_15_CTRL (0x%08x) = 0x%08x\n",
204 MPP_8_15_CTRL, orion_read(MPP_8_15_CTRL));
205 printk(KERN_DEBUG "MPP_16_19_CTRL (0x%08x) = 0x%08x\n",
206 MPP_16_19_CTRL, orion_read(MPP_16_19_CTRL));
207 printk(KERN_DEBUG "MPP_DEV_CTRL (0x%08x) = 0x%08x\n",
208 MPP_DEV_CTRL, orion_read(MPP_DEV_CTRL));
209 printk(KERN_DEBUG "GPIO_OUT (0x%08x) = 0x%08x\n",
210 GPIO_OUT, orion_read(GPIO_OUT));
211 printk(KERN_DEBUG "GPIO_IO_CONF (0x%08x) = 0x%08x\n",
212 GPIO_IO_CONF, orion_read(GPIO_IO_CONF));
213 printk(KERN_DEBUG "GPIO_BLINK_EN (0x%08x) = 0x%08x\n",
214 GPIO_BLINK_EN, orion_read(GPIO_BLINK_EN));
215 printk(KERN_DEBUG "GPIO_IN_POL (0x%08x) = 0x%08x\n",
216 GPIO_IN_POL, orion_read(GPIO_IN_POL));
217 printk(KERN_DEBUG "GPIO_DATA_IN (0x%08x) = 0x%08x\n",
218 GPIO_DATA_IN, orion_read(GPIO_DATA_IN));
219 printk(KERN_DEBUG "GPIO_LEVEL_MASK (0x%08x) = 0x%08x\n",
220 GPIO_LEVEL_MASK, orion_read(GPIO_LEVEL_MASK));
221 printk(KERN_DEBUG "GPIO_EDGE_CAUSE (0x%08x) = 0x%08x\n",
222 GPIO_EDGE_CAUSE, orion_read(GPIO_EDGE_CAUSE));
223 printk(KERN_DEBUG "GPIO_EDGE_MASK (0x%08x) = 0x%08x\n",
224 GPIO_EDGE_MASK, orion_read(GPIO_EDGE_MASK));