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