Tzachi Perelstein | 01af72e | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 1 | /* |
Lennert Buytenhek | 9dd0b19 | 2008-03-27 14:51:41 -0400 | [diff] [blame] | 2 | * arch/arm/mach-orion5x/gpio.c |
Tzachi Perelstein | 01af72e | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 3 | * |
| 4 | * GPIO functions for Marvell Orion System On Chip |
| 5 | * |
| 6 | * Maintainer: Tzachi Perelstein <tzachi@marvell.com> |
| 7 | * |
Lennert Buytenhek | 159ffb3 | 2008-03-27 14:51:41 -0400 | [diff] [blame] | 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 |
Tzachi Perelstein | 01af72e | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 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> |
Russell King | fced80c | 2008-09-06 12:10:45 +0100 | [diff] [blame] | 18 | #include <linux/io.h> |
Tzachi Perelstein | 01af72e | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 19 | #include <asm/gpio.h> |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 20 | #include <mach/orion5x.h> |
Tzachi Perelstein | 01af72e | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 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 | |
Lennert Buytenhek | 19cfd5c | 2008-05-10 23:25:46 +0200 | [diff] [blame] | 27 | void __init orion5x_gpio_set_valid(unsigned pin, int valid) |
Tzachi Perelstein | 01af72e | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 28 | { |
Lennert Buytenhek | 19cfd5c | 2008-05-10 23:25:46 +0200 | [diff] [blame] | 29 | if (valid) |
| 30 | __set_bit(pin, gpio_valid); |
| 31 | else |
| 32 | __clear_bit(pin, gpio_valid); |
Tzachi Perelstein | 01af72e | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 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)) { |
Harvey Harrison | 8e86f42 | 2008-03-04 15:08:02 -0800 | [diff] [blame] | 43 | pr_debug("%s: invalid GPIO %d\n", __func__, pin); |
Tzachi Perelstein | 01af72e | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 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 | |
Lennert Buytenhek | 9dd0b19 | 2008-03-27 14:51:41 -0400 | [diff] [blame] | 56 | orion5x_setbits(GPIO_IO_CONF, 1 << pin); |
Tzachi Perelstein | 01af72e | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 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)) { |
Harvey Harrison | 8e86f42 | 2008-03-04 15:08:02 -0800 | [diff] [blame] | 69 | pr_debug("%s: invalid GPIO %d\n", __func__, pin); |
Tzachi Perelstein | 01af72e | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 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; |
Lennert Buytenhek | 9dd0b19 | 2008-03-27 14:51:41 -0400 | [diff] [blame] | 83 | orion5x_clrbits(GPIO_BLINK_EN, mask); |
Tzachi Perelstein | 01af72e | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 84 | if (value) |
Lennert Buytenhek | 9dd0b19 | 2008-03-27 14:51:41 -0400 | [diff] [blame] | 85 | orion5x_setbits(GPIO_OUT, mask); |
Tzachi Perelstein | 01af72e | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 86 | else |
Lennert Buytenhek | 9dd0b19 | 2008-03-27 14:51:41 -0400 | [diff] [blame] | 87 | orion5x_clrbits(GPIO_OUT, mask); |
| 88 | orion5x_clrbits(GPIO_IO_CONF, mask); |
Tzachi Perelstein | 01af72e | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 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 | |
Lennert Buytenhek | 79e90dd | 2008-05-28 16:43:48 +0200 | [diff] [blame] | 99 | if (readl(GPIO_IO_CONF) & mask) |
| 100 | val = readl(GPIO_DATA_IN) ^ readl(GPIO_IN_POL); |
Tzachi Perelstein | 01af72e | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 101 | else |
Lennert Buytenhek | 79e90dd | 2008-05-28 16:43:48 +0200 | [diff] [blame] | 102 | val = readl(GPIO_OUT); |
Tzachi Perelstein | 01af72e | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 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 | |
Lennert Buytenhek | 9dd0b19 | 2008-03-27 14:51:41 -0400 | [diff] [blame] | 115 | orion5x_clrbits(GPIO_BLINK_EN, mask); |
Tzachi Perelstein | 01af72e | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 116 | if (value) |
Lennert Buytenhek | 9dd0b19 | 2008-03-27 14:51:41 -0400 | [diff] [blame] | 117 | orion5x_setbits(GPIO_OUT, mask); |
Tzachi Perelstein | 01af72e | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 118 | else |
Lennert Buytenhek | 9dd0b19 | 2008-03-27 14:51:41 -0400 | [diff] [blame] | 119 | orion5x_clrbits(GPIO_OUT, mask); |
Tzachi Perelstein | 01af72e | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 120 | |
| 121 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 122 | } |
| 123 | EXPORT_SYMBOL(gpio_set_value); |
| 124 | |
Lennert Buytenhek | 9dd0b19 | 2008-03-27 14:51:41 -0400 | [diff] [blame] | 125 | void orion5x_gpio_set_blink(unsigned pin, int blink) |
Herbert Valerio Riedel | b11e9e0 | 2007-11-29 15:19:56 +0100 | [diff] [blame] | 126 | { |
| 127 | unsigned long flags; |
| 128 | int mask = 1 << pin; |
| 129 | |
| 130 | spin_lock_irqsave(&gpio_lock, flags); |
| 131 | |
Lennert Buytenhek | 9dd0b19 | 2008-03-27 14:51:41 -0400 | [diff] [blame] | 132 | orion5x_clrbits(GPIO_OUT, mask); |
Herbert Valerio Riedel | b11e9e0 | 2007-11-29 15:19:56 +0100 | [diff] [blame] | 133 | if (blink) |
Lennert Buytenhek | 9dd0b19 | 2008-03-27 14:51:41 -0400 | [diff] [blame] | 134 | orion5x_setbits(GPIO_BLINK_EN, mask); |
Herbert Valerio Riedel | b11e9e0 | 2007-11-29 15:19:56 +0100 | [diff] [blame] | 135 | else |
Lennert Buytenhek | 9dd0b19 | 2008-03-27 14:51:41 -0400 | [diff] [blame] | 136 | orion5x_clrbits(GPIO_BLINK_EN, mask); |
Herbert Valerio Riedel | b11e9e0 | 2007-11-29 15:19:56 +0100 | [diff] [blame] | 137 | |
| 138 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 139 | } |
Lennert Buytenhek | 9dd0b19 | 2008-03-27 14:51:41 -0400 | [diff] [blame] | 140 | EXPORT_SYMBOL(orion5x_gpio_set_blink); |
Herbert Valerio Riedel | b11e9e0 | 2007-11-29 15:19:56 +0100 | [diff] [blame] | 141 | |
Tzachi Perelstein | 01af72e | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 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)) { |
Harvey Harrison | 8e86f42 | 2008-03-04 15:08:02 -0800 | [diff] [blame] | 148 | pr_debug("%s: invalid GPIO %d\n", __func__, pin); |
Tzachi Perelstein | 01af72e | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 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", |
Harvey Harrison | 8e86f42 | 2008-03-04 15:08:02 -0800 | [diff] [blame] | 156 | __func__, pin, gpio_label[pin]); |
Tzachi Perelstein | 01af72e | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 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 | { |
Uwe Kleine-König | 9c65685 | 2008-10-29 14:14:54 -0700 | [diff] [blame] | 168 | might_sleep(); |
| 169 | |
Tzachi Perelstein | 01af72e | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 170 | if (pin >= GPIO_MAX || !test_bit(pin, gpio_valid)) { |
Harvey Harrison | 8e86f42 | 2008-03-04 15:08:02 -0800 | [diff] [blame] | 171 | pr_debug("%s: invalid GPIO %d\n", __func__, pin); |
Tzachi Perelstein | 01af72e | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 172 | return; |
| 173 | } |
| 174 | |
| 175 | if (!gpio_label[pin]) |
Harvey Harrison | 8e86f42 | 2008-03-04 15:08:02 -0800 | [diff] [blame] | 176 | pr_warning("%s: GPIO %d already freed\n", __func__, pin); |
Tzachi Perelstein | 01af72e | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 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]); |
Lennert Buytenhek | 79e90dd | 2008-05-28 16:43:48 +0200 | [diff] [blame] | 196 | if (readl(GPIO_IO_CONF) & (1 << i)) { |
Tzachi Perelstein | 01af72e | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 197 | printk("input, active %s, level %s, edge %s\n", |
Lennert Buytenhek | 79e90dd | 2008-05-28 16:43:48 +0200 | [diff] [blame] | 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"); |
Tzachi Perelstein | 01af72e | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 201 | } else { |
Lennert Buytenhek | 79e90dd | 2008-05-28 16:43:48 +0200 | [diff] [blame] | 202 | printk("output, val=%d\n", (readl(GPIO_OUT) >> i) & 1); |
Tzachi Perelstein | 01af72e | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | printk(KERN_DEBUG "MPP_0_7_CTRL (0x%08x) = 0x%08x\n", |
Lennert Buytenhek | 79e90dd | 2008-05-28 16:43:48 +0200 | [diff] [blame] | 208 | MPP_0_7_CTRL, readl(MPP_0_7_CTRL)); |
Tzachi Perelstein | 01af72e | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 209 | printk(KERN_DEBUG "MPP_8_15_CTRL (0x%08x) = 0x%08x\n", |
Lennert Buytenhek | 79e90dd | 2008-05-28 16:43:48 +0200 | [diff] [blame] | 210 | MPP_8_15_CTRL, readl(MPP_8_15_CTRL)); |
Tzachi Perelstein | 01af72e | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 211 | printk(KERN_DEBUG "MPP_16_19_CTRL (0x%08x) = 0x%08x\n", |
Lennert Buytenhek | 79e90dd | 2008-05-28 16:43:48 +0200 | [diff] [blame] | 212 | MPP_16_19_CTRL, readl(MPP_16_19_CTRL)); |
Tzachi Perelstein | 01af72e | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 213 | printk(KERN_DEBUG "MPP_DEV_CTRL (0x%08x) = 0x%08x\n", |
Lennert Buytenhek | 79e90dd | 2008-05-28 16:43:48 +0200 | [diff] [blame] | 214 | MPP_DEV_CTRL, readl(MPP_DEV_CTRL)); |
Tzachi Perelstein | 01af72e | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 215 | printk(KERN_DEBUG "GPIO_OUT (0x%08x) = 0x%08x\n", |
Lennert Buytenhek | 79e90dd | 2008-05-28 16:43:48 +0200 | [diff] [blame] | 216 | GPIO_OUT, readl(GPIO_OUT)); |
Tzachi Perelstein | 01af72e | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 217 | printk(KERN_DEBUG "GPIO_IO_CONF (0x%08x) = 0x%08x\n", |
Lennert Buytenhek | 79e90dd | 2008-05-28 16:43:48 +0200 | [diff] [blame] | 218 | GPIO_IO_CONF, readl(GPIO_IO_CONF)); |
Tzachi Perelstein | 01af72e | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 219 | printk(KERN_DEBUG "GPIO_BLINK_EN (0x%08x) = 0x%08x\n", |
Lennert Buytenhek | 79e90dd | 2008-05-28 16:43:48 +0200 | [diff] [blame] | 220 | GPIO_BLINK_EN, readl(GPIO_BLINK_EN)); |
Tzachi Perelstein | 01af72e | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 221 | printk(KERN_DEBUG "GPIO_IN_POL (0x%08x) = 0x%08x\n", |
Lennert Buytenhek | 79e90dd | 2008-05-28 16:43:48 +0200 | [diff] [blame] | 222 | GPIO_IN_POL, readl(GPIO_IN_POL)); |
Tzachi Perelstein | 01af72e | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 223 | printk(KERN_DEBUG "GPIO_DATA_IN (0x%08x) = 0x%08x\n", |
Lennert Buytenhek | 79e90dd | 2008-05-28 16:43:48 +0200 | [diff] [blame] | 224 | GPIO_DATA_IN, readl(GPIO_DATA_IN)); |
Tzachi Perelstein | 01af72e | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 225 | printk(KERN_DEBUG "GPIO_LEVEL_MASK (0x%08x) = 0x%08x\n", |
Lennert Buytenhek | 79e90dd | 2008-05-28 16:43:48 +0200 | [diff] [blame] | 226 | GPIO_LEVEL_MASK, readl(GPIO_LEVEL_MASK)); |
Tzachi Perelstein | 01af72e | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 227 | printk(KERN_DEBUG "GPIO_EDGE_CAUSE (0x%08x) = 0x%08x\n", |
Lennert Buytenhek | 79e90dd | 2008-05-28 16:43:48 +0200 | [diff] [blame] | 228 | GPIO_EDGE_CAUSE, readl(GPIO_EDGE_CAUSE)); |
Tzachi Perelstein | 01af72e | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 229 | printk(KERN_DEBUG "GPIO_EDGE_MASK (0x%08x) = 0x%08x\n", |
Lennert Buytenhek | 79e90dd | 2008-05-28 16:43:48 +0200 | [diff] [blame] | 230 | GPIO_EDGE_MASK, readl(GPIO_EDGE_MASK)); |
Tzachi Perelstein | 01af72e | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 231 | } |