2 * arch/arm/mach-orion5x/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>
20 #include <mach/orion5x.h>
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 */
27 void __init orion5x_gpio_set_valid(unsigned pin, int valid)
30 __set_bit(pin, gpio_valid);
32 __clear_bit(pin, gpio_valid);
36 * GENERIC_GPIO primitives
38 int gpio_direction_input(unsigned pin)
42 if (pin >= GPIO_MAX || !test_bit(pin, gpio_valid)) {
43 pr_debug("%s: invalid GPIO %d\n", __func__, pin);
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.
54 gpio_label[pin] = "?";
56 orion5x_setbits(GPIO_IO_CONF, 1 << pin);
58 spin_unlock_irqrestore(&gpio_lock, flags);
61 EXPORT_SYMBOL(gpio_direction_input);
63 int gpio_direction_output(unsigned pin, int value)
68 if (pin >= GPIO_MAX || !test_bit(pin, gpio_valid)) {
69 pr_debug("%s: invalid GPIO %d\n", __func__, pin);
73 spin_lock_irqsave(&gpio_lock, flags);
76 * Some callers might have not used the gpio_request(),
77 * so flag this pin as requested now.
80 gpio_label[pin] = "?";
83 orion5x_clrbits(GPIO_BLINK_EN, mask);
85 orion5x_setbits(GPIO_OUT, mask);
87 orion5x_clrbits(GPIO_OUT, mask);
88 orion5x_clrbits(GPIO_IO_CONF, mask);
90 spin_unlock_irqrestore(&gpio_lock, flags);
93 EXPORT_SYMBOL(gpio_direction_output);
95 int gpio_get_value(unsigned pin)
97 int val, mask = 1 << pin;
99 if (readl(GPIO_IO_CONF) & mask)
100 val = readl(GPIO_DATA_IN) ^ readl(GPIO_IN_POL);
102 val = readl(GPIO_OUT);
106 EXPORT_SYMBOL(gpio_get_value);
108 void gpio_set_value(unsigned pin, int value)
113 spin_lock_irqsave(&gpio_lock, flags);
115 orion5x_clrbits(GPIO_BLINK_EN, mask);
117 orion5x_setbits(GPIO_OUT, mask);
119 orion5x_clrbits(GPIO_OUT, mask);
121 spin_unlock_irqrestore(&gpio_lock, flags);
123 EXPORT_SYMBOL(gpio_set_value);
125 void orion5x_gpio_set_blink(unsigned pin, int blink)
130 spin_lock_irqsave(&gpio_lock, flags);
132 orion5x_clrbits(GPIO_OUT, mask);
134 orion5x_setbits(GPIO_BLINK_EN, mask);
136 orion5x_clrbits(GPIO_BLINK_EN, mask);
138 spin_unlock_irqrestore(&gpio_lock, flags);
140 EXPORT_SYMBOL(orion5x_gpio_set_blink);
142 int gpio_request(unsigned pin, const char *label)
147 if (pin >= GPIO_MAX || !test_bit(pin, gpio_valid)) {
148 pr_debug("%s: invalid GPIO %d\n", __func__, pin);
152 spin_lock_irqsave(&gpio_lock, flags);
154 if (gpio_label[pin]) {
155 pr_debug("%s: GPIO %d already used as %s\n",
156 __func__, pin, gpio_label[pin]);
159 gpio_label[pin] = label ? label : "?";
161 spin_unlock_irqrestore(&gpio_lock, flags);
164 EXPORT_SYMBOL(gpio_request);
166 void gpio_free(unsigned pin)
168 if (pin >= GPIO_MAX || !test_bit(pin, gpio_valid)) {
169 pr_debug("%s: invalid GPIO %d\n", __func__, pin);
173 if (!gpio_label[pin])
174 pr_warning("%s: GPIO %d already freed\n", __func__, pin);
176 gpio_label[pin] = NULL;
178 EXPORT_SYMBOL(gpio_free);
181 void gpio_display(void)
185 for (i = 0; i < GPIO_MAX; i++) {
186 printk(KERN_DEBUG "Pin-%d: ", i);
188 if (!test_bit(i, gpio_valid)) {
189 printk("non-GPIO\n");
190 } else if (!gpio_label[i]) {
191 printk("GPIO, free\n");
193 printk("GPIO, used by %s, ", gpio_label[i]);
194 if (readl(GPIO_IO_CONF) & (1 << i)) {
195 printk("input, active %s, level %s, edge %s\n",
196 ((readl(GPIO_IN_POL) >> i) & 1) ? "low" : "high",
197 ((readl(GPIO_LEVEL_MASK) >> i) & 1) ? "enabled" : "masked",
198 ((readl(GPIO_EDGE_MASK) >> i) & 1) ? "enabled" : "masked");
200 printk("output, val=%d\n", (readl(GPIO_OUT) >> i) & 1);
205 printk(KERN_DEBUG "MPP_0_7_CTRL (0x%08x) = 0x%08x\n",
206 MPP_0_7_CTRL, readl(MPP_0_7_CTRL));
207 printk(KERN_DEBUG "MPP_8_15_CTRL (0x%08x) = 0x%08x\n",
208 MPP_8_15_CTRL, readl(MPP_8_15_CTRL));
209 printk(KERN_DEBUG "MPP_16_19_CTRL (0x%08x) = 0x%08x\n",
210 MPP_16_19_CTRL, readl(MPP_16_19_CTRL));
211 printk(KERN_DEBUG "MPP_DEV_CTRL (0x%08x) = 0x%08x\n",
212 MPP_DEV_CTRL, readl(MPP_DEV_CTRL));
213 printk(KERN_DEBUG "GPIO_OUT (0x%08x) = 0x%08x\n",
214 GPIO_OUT, readl(GPIO_OUT));
215 printk(KERN_DEBUG "GPIO_IO_CONF (0x%08x) = 0x%08x\n",
216 GPIO_IO_CONF, readl(GPIO_IO_CONF));
217 printk(KERN_DEBUG "GPIO_BLINK_EN (0x%08x) = 0x%08x\n",
218 GPIO_BLINK_EN, readl(GPIO_BLINK_EN));
219 printk(KERN_DEBUG "GPIO_IN_POL (0x%08x) = 0x%08x\n",
220 GPIO_IN_POL, readl(GPIO_IN_POL));
221 printk(KERN_DEBUG "GPIO_DATA_IN (0x%08x) = 0x%08x\n",
222 GPIO_DATA_IN, readl(GPIO_DATA_IN));
223 printk(KERN_DEBUG "GPIO_LEVEL_MASK (0x%08x) = 0x%08x\n",
224 GPIO_LEVEL_MASK, readl(GPIO_LEVEL_MASK));
225 printk(KERN_DEBUG "GPIO_EDGE_CAUSE (0x%08x) = 0x%08x\n",
226 GPIO_EDGE_CAUSE, readl(GPIO_EDGE_CAUSE));
227 printk(KERN_DEBUG "GPIO_EDGE_MASK (0x%08x) = 0x%08x\n",
228 GPIO_EDGE_MASK, readl(GPIO_EDGE_MASK));