Commit | Line | Data |
---|---|---|
01af72e4 | 1 | /* |
9dd0b194 | 2 | * arch/arm/mach-orion5x/gpio.c |
01af72e4 TP |
3 | * |
4 | * GPIO functions for Marvell Orion System On Chip | |
5 | * | |
6 | * Maintainer: Tzachi Perelstein <tzachi@marvell.com> | |
7 | * | |
159ffb3a LB |
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 | |
01af72e4 TP |
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> | |
b590bc5c | 19 | #include <asm/io.h> |
9dd0b194 | 20 | #include <asm/arch/orion5x.h> |
01af72e4 TP |
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 | ||
19cfd5c0 | 27 | void __init orion5x_gpio_set_valid(unsigned pin, int valid) |
01af72e4 | 28 | { |
19cfd5c0 LB |
29 | if (valid) |
30 | __set_bit(pin, gpio_valid); | |
31 | else | |
32 | __clear_bit(pin, gpio_valid); | |
01af72e4 TP |
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)) { | |
8e86f427 | 43 | pr_debug("%s: invalid GPIO %d\n", __func__, pin); |
01af72e4 TP |
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 | ||
9dd0b194 | 56 | orion5x_setbits(GPIO_IO_CONF, 1 << pin); |
01af72e4 TP |
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)) { | |
8e86f427 | 69 | pr_debug("%s: invalid GPIO %d\n", __func__, pin); |
01af72e4 TP |
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; | |
9dd0b194 | 83 | orion5x_clrbits(GPIO_BLINK_EN, mask); |
01af72e4 | 84 | if (value) |
9dd0b194 | 85 | orion5x_setbits(GPIO_OUT, mask); |
01af72e4 | 86 | else |
9dd0b194 LB |
87 | orion5x_clrbits(GPIO_OUT, mask); |
88 | orion5x_clrbits(GPIO_IO_CONF, mask); | |
01af72e4 TP |
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 | ||
9dd0b194 LB |
99 | if (orion5x_read(GPIO_IO_CONF) & mask) |
100 | val = orion5x_read(GPIO_DATA_IN) ^ orion5x_read(GPIO_IN_POL); | |
01af72e4 | 101 | else |
9dd0b194 | 102 | val = orion5x_read(GPIO_OUT); |
01af72e4 TP |
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 | ||
9dd0b194 | 115 | orion5x_clrbits(GPIO_BLINK_EN, mask); |
01af72e4 | 116 | if (value) |
9dd0b194 | 117 | orion5x_setbits(GPIO_OUT, mask); |
01af72e4 | 118 | else |
9dd0b194 | 119 | orion5x_clrbits(GPIO_OUT, mask); |
01af72e4 TP |
120 | |
121 | spin_unlock_irqrestore(&gpio_lock, flags); | |
122 | } | |
123 | EXPORT_SYMBOL(gpio_set_value); | |
124 | ||
9dd0b194 | 125 | void orion5x_gpio_set_blink(unsigned pin, int blink) |
b11e9e02 HVR |
126 | { |
127 | unsigned long flags; | |
128 | int mask = 1 << pin; | |
129 | ||
130 | spin_lock_irqsave(&gpio_lock, flags); | |
131 | ||
9dd0b194 | 132 | orion5x_clrbits(GPIO_OUT, mask); |
b11e9e02 | 133 | if (blink) |
9dd0b194 | 134 | orion5x_setbits(GPIO_BLINK_EN, mask); |
b11e9e02 | 135 | else |
9dd0b194 | 136 | orion5x_clrbits(GPIO_BLINK_EN, mask); |
b11e9e02 HVR |
137 | |
138 | spin_unlock_irqrestore(&gpio_lock, flags); | |
139 | } | |
9dd0b194 | 140 | EXPORT_SYMBOL(orion5x_gpio_set_blink); |
b11e9e02 | 141 | |
01af72e4 TP |
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)) { | |
8e86f427 | 148 | pr_debug("%s: invalid GPIO %d\n", __func__, pin); |
01af72e4 TP |
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", | |
8e86f427 | 156 | __func__, pin, gpio_label[pin]); |
01af72e4 TP |
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 | { | |
168 | if (pin >= GPIO_MAX || !test_bit(pin, gpio_valid)) { | |
8e86f427 | 169 | pr_debug("%s: invalid GPIO %d\n", __func__, pin); |
01af72e4 TP |
170 | return; |
171 | } | |
172 | ||
173 | if (!gpio_label[pin]) | |
8e86f427 | 174 | pr_warning("%s: GPIO %d already freed\n", __func__, pin); |
01af72e4 TP |
175 | else |
176 | gpio_label[pin] = NULL; | |
177 | } | |
178 | EXPORT_SYMBOL(gpio_free); | |
179 | ||
180 | /* Debug helper */ | |
181 | void gpio_display(void) | |
182 | { | |
183 | int i; | |
184 | ||
185 | for (i = 0; i < GPIO_MAX; i++) { | |
186 | printk(KERN_DEBUG "Pin-%d: ", i); | |
187 | ||
188 | if (!test_bit(i, gpio_valid)) { | |
189 | printk("non-GPIO\n"); | |
190 | } else if (!gpio_label[i]) { | |
191 | printk("GPIO, free\n"); | |
192 | } else { | |
193 | printk("GPIO, used by %s, ", gpio_label[i]); | |
9dd0b194 | 194 | if (orion5x_read(GPIO_IO_CONF) & (1 << i)) { |
01af72e4 | 195 | printk("input, active %s, level %s, edge %s\n", |
9dd0b194 LB |
196 | ((orion5x_read(GPIO_IN_POL) >> i) & 1) ? "low" : "high", |
197 | ((orion5x_read(GPIO_LEVEL_MASK) >> i) & 1) ? "enabled" : "masked", | |
198 | ((orion5x_read(GPIO_EDGE_MASK) >> i) & 1) ? "enabled" : "masked"); | |
01af72e4 | 199 | } else { |
9dd0b194 | 200 | printk("output, val=%d\n", (orion5x_read(GPIO_OUT) >> i) & 1); |
01af72e4 TP |
201 | } |
202 | } | |
203 | } | |
204 | ||
205 | printk(KERN_DEBUG "MPP_0_7_CTRL (0x%08x) = 0x%08x\n", | |
9dd0b194 | 206 | MPP_0_7_CTRL, orion5x_read(MPP_0_7_CTRL)); |
01af72e4 | 207 | printk(KERN_DEBUG "MPP_8_15_CTRL (0x%08x) = 0x%08x\n", |
9dd0b194 | 208 | MPP_8_15_CTRL, orion5x_read(MPP_8_15_CTRL)); |
01af72e4 | 209 | printk(KERN_DEBUG "MPP_16_19_CTRL (0x%08x) = 0x%08x\n", |
9dd0b194 | 210 | MPP_16_19_CTRL, orion5x_read(MPP_16_19_CTRL)); |
01af72e4 | 211 | printk(KERN_DEBUG "MPP_DEV_CTRL (0x%08x) = 0x%08x\n", |
9dd0b194 | 212 | MPP_DEV_CTRL, orion5x_read(MPP_DEV_CTRL)); |
01af72e4 | 213 | printk(KERN_DEBUG "GPIO_OUT (0x%08x) = 0x%08x\n", |
9dd0b194 | 214 | GPIO_OUT, orion5x_read(GPIO_OUT)); |
01af72e4 | 215 | printk(KERN_DEBUG "GPIO_IO_CONF (0x%08x) = 0x%08x\n", |
9dd0b194 | 216 | GPIO_IO_CONF, orion5x_read(GPIO_IO_CONF)); |
01af72e4 | 217 | printk(KERN_DEBUG "GPIO_BLINK_EN (0x%08x) = 0x%08x\n", |
9dd0b194 | 218 | GPIO_BLINK_EN, orion5x_read(GPIO_BLINK_EN)); |
01af72e4 | 219 | printk(KERN_DEBUG "GPIO_IN_POL (0x%08x) = 0x%08x\n", |
9dd0b194 | 220 | GPIO_IN_POL, orion5x_read(GPIO_IN_POL)); |
01af72e4 | 221 | printk(KERN_DEBUG "GPIO_DATA_IN (0x%08x) = 0x%08x\n", |
9dd0b194 | 222 | GPIO_DATA_IN, orion5x_read(GPIO_DATA_IN)); |
01af72e4 | 223 | printk(KERN_DEBUG "GPIO_LEVEL_MASK (0x%08x) = 0x%08x\n", |
9dd0b194 | 224 | GPIO_LEVEL_MASK, orion5x_read(GPIO_LEVEL_MASK)); |
01af72e4 | 225 | printk(KERN_DEBUG "GPIO_EDGE_CAUSE (0x%08x) = 0x%08x\n", |
9dd0b194 | 226 | GPIO_EDGE_CAUSE, orion5x_read(GPIO_EDGE_CAUSE)); |
01af72e4 | 227 | printk(KERN_DEBUG "GPIO_EDGE_MASK (0x%08x) = 0x%08x\n", |
9dd0b194 | 228 | GPIO_EDGE_MASK, orion5x_read(GPIO_EDGE_MASK)); |
01af72e4 | 229 | } |