2 * arch/arm/plat-orion/gpio.c
4 * Marvell Orion SoC GPIO handling.
6 * This file is licensed under the terms of the GNU General Public
7 * License version 2. This program is licensed "as is" without any
8 * warranty of any kind, whether express or implied.
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/irq.h>
14 #include <linux/module.h>
15 #include <linux/spinlock.h>
16 #include <linux/bitops.h>
20 static DEFINE_SPINLOCK(gpio_lock);
21 static const char *gpio_label[GPIO_MAX]; /* non null for allocated GPIOs */
22 static unsigned long gpio_valid_input[BITS_TO_LONGS(GPIO_MAX)];
23 static unsigned long gpio_valid_output[BITS_TO_LONGS(GPIO_MAX)];
25 static inline void __set_direction(unsigned pin, int input)
29 u = readl(GPIO_IO_CONF(pin));
33 u &= ~(1 << (pin & 31));
34 writel(u, GPIO_IO_CONF(pin));
37 static void __set_level(unsigned pin, int high)
41 u = readl(GPIO_OUT(pin));
45 u &= ~(1 << (pin & 31));
46 writel(u, GPIO_OUT(pin));
51 * GENERIC_GPIO primitives.
53 int gpio_direction_input(unsigned pin)
57 if (pin >= GPIO_MAX || !test_bit(pin, gpio_valid_input)) {
58 pr_debug("%s: invalid GPIO %d\n", __func__, pin);
62 spin_lock_irqsave(&gpio_lock, flags);
65 * Some callers might not have used gpio_request(),
66 * so flag this pin as requested now.
68 if (gpio_label[pin] == NULL)
69 gpio_label[pin] = "?";
72 * Configure GPIO direction.
74 __set_direction(pin, 1);
76 spin_unlock_irqrestore(&gpio_lock, flags);
80 EXPORT_SYMBOL(gpio_direction_input);
82 int gpio_direction_output(unsigned pin, int value)
87 if (pin >= GPIO_MAX || !test_bit(pin, gpio_valid_output)) {
88 pr_debug("%s: invalid GPIO %d\n", __func__, pin);
92 spin_lock_irqsave(&gpio_lock, flags);
95 * Some callers might not have used gpio_request(),
96 * so flag this pin as requested now.
98 if (gpio_label[pin] == NULL)
99 gpio_label[pin] = "?";
104 u = readl(GPIO_BLINK_EN(pin));
105 u &= ~(1 << (pin & 31));
106 writel(u, GPIO_BLINK_EN(pin));
109 * Configure GPIO output value.
111 __set_level(pin, value);
114 * Configure GPIO direction.
116 __set_direction(pin, 0);
118 spin_unlock_irqrestore(&gpio_lock, flags);
122 EXPORT_SYMBOL(gpio_direction_output);
124 int gpio_get_value(unsigned pin)
128 if (readl(GPIO_IO_CONF(pin)) & (1 << (pin & 31)))
129 val = readl(GPIO_DATA_IN(pin)) ^ readl(GPIO_IN_POL(pin));
131 val = readl(GPIO_OUT(pin));
133 return (val >> (pin & 31)) & 1;
135 EXPORT_SYMBOL(gpio_get_value);
137 void gpio_set_value(unsigned pin, int value)
142 spin_lock_irqsave(&gpio_lock, flags);
147 u = readl(GPIO_BLINK_EN(pin));
148 u &= ~(1 << (pin & 31));
149 writel(u, GPIO_BLINK_EN(pin));
152 * Configure GPIO output value.
154 __set_level(pin, value);
156 spin_unlock_irqrestore(&gpio_lock, flags);
158 EXPORT_SYMBOL(gpio_set_value);
160 int gpio_request(unsigned pin, const char *label)
165 if (pin >= GPIO_MAX ||
166 !(test_bit(pin, gpio_valid_input) ||
167 test_bit(pin, gpio_valid_output))) {
168 pr_debug("%s: invalid GPIO %d\n", __func__, pin);
172 spin_lock_irqsave(&gpio_lock, flags);
173 if (gpio_label[pin] == NULL) {
174 gpio_label[pin] = label ? label : "?";
177 pr_debug("%s: GPIO %d already used as %s\n",
178 __func__, pin, gpio_label[pin]);
181 spin_unlock_irqrestore(&gpio_lock, flags);
185 EXPORT_SYMBOL(gpio_request);
187 void gpio_free(unsigned pin)
189 if (pin >= GPIO_MAX ||
190 !(test_bit(pin, gpio_valid_input) ||
191 test_bit(pin, gpio_valid_output))) {
192 pr_debug("%s: invalid GPIO %d\n", __func__, pin);
196 if (gpio_label[pin] == NULL)
197 pr_warning("%s: GPIO %d already freed\n", __func__, pin);
199 gpio_label[pin] = NULL;
201 EXPORT_SYMBOL(gpio_free);
205 * Orion-specific GPIO API extensions.
207 void __init orion_gpio_set_unused(unsigned pin)
210 * Configure as output, drive low.
213 __set_direction(pin, 0);
216 void __init orion_gpio_set_valid(unsigned pin, int mode)
219 mode = GPIO_INPUT_OK | GPIO_OUTPUT_OK;
220 if (mode & GPIO_INPUT_OK)
221 __set_bit(pin, gpio_valid_input);
223 __clear_bit(pin, gpio_valid_input);
224 if (mode & GPIO_OUTPUT_OK)
225 __set_bit(pin, gpio_valid_output);
227 __clear_bit(pin, gpio_valid_output);
230 void orion_gpio_set_blink(unsigned pin, int blink)
235 spin_lock_irqsave(&gpio_lock, flags);
238 * Set output value to zero.
242 u = readl(GPIO_BLINK_EN(pin));
244 u |= 1 << (pin & 31);
246 u &= ~(1 << (pin & 31));
247 writel(u, GPIO_BLINK_EN(pin));
249 spin_unlock_irqrestore(&gpio_lock, flags);
251 EXPORT_SYMBOL(orion_gpio_set_blink);
254 /*****************************************************************************
257 * GPIO_IN_POL register controls whether GPIO_DATA_IN will hold the same
258 * value of the line or the opposite value.
260 * Level IRQ handlers: DATA_IN is used directly as cause register.
261 * Interrupt are masked by LEVEL_MASK registers.
262 * Edge IRQ handlers: Change in DATA_IN are latched in EDGE_CAUSE.
263 * Interrupt are masked by EDGE_MASK registers.
264 * Both-edge handlers: Similar to regular Edge handlers, but also swaps
265 * the polarity to catch the next line transaction.
266 * This is a race condition that might not perfectly
267 * work on some use cases.
269 * Every eight GPIO lines are grouped (OR'ed) before going up to main
273 * data-in /--------| |-----| |----\
274 * -----| |----- ---- to main cause reg
275 * X \----------------| |----/
276 * polarity LEVEL mask
278 ****************************************************************************/
280 static void gpio_irq_ack(u32 irq)
282 int type = irq_desc[irq].status & IRQ_TYPE_SENSE_MASK;
283 if (type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING)) {
284 int pin = irq_to_gpio(irq);
285 writel(~(1 << (pin & 31)), GPIO_EDGE_CAUSE(pin));
289 static void gpio_irq_mask(u32 irq)
291 int pin = irq_to_gpio(irq);
292 int type = irq_desc[irq].status & IRQ_TYPE_SENSE_MASK;
293 u32 reg = (type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING)) ?
294 GPIO_EDGE_MASK(pin) : GPIO_LEVEL_MASK(pin);
296 u &= ~(1 << (pin & 31));
300 static void gpio_irq_unmask(u32 irq)
302 int pin = irq_to_gpio(irq);
303 int type = irq_desc[irq].status & IRQ_TYPE_SENSE_MASK;
304 u32 reg = (type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING)) ?
305 GPIO_EDGE_MASK(pin) : GPIO_LEVEL_MASK(pin);
307 u |= 1 << (pin & 31);
311 static int gpio_irq_set_type(u32 irq, u32 type)
313 int pin = irq_to_gpio(irq);
314 struct irq_desc *desc;
317 u = readl(GPIO_IO_CONF(pin)) & (1 << (pin & 31));
319 printk(KERN_ERR "orion gpio_irq_set_type failed "
320 "(irq %d, pin %d).\n", irq, pin);
324 desc = irq_desc + irq;
327 * Set edge/level type.
329 if (type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING)) {
330 desc->handle_irq = handle_edge_irq;
331 } else if (type & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW)) {
332 desc->handle_irq = handle_level_irq;
334 printk(KERN_ERR "failed to set irq=%d (type=%d)\n", irq, type);
339 * Configure interrupt polarity.
341 if (type == IRQ_TYPE_EDGE_RISING || type == IRQ_TYPE_LEVEL_HIGH) {
342 u = readl(GPIO_IN_POL(pin));
343 u &= ~(1 << (pin & 31));
344 writel(u, GPIO_IN_POL(pin));
345 } else if (type == IRQ_TYPE_EDGE_FALLING || type == IRQ_TYPE_LEVEL_LOW) {
346 u = readl(GPIO_IN_POL(pin));
347 u |= 1 << (pin & 31);
348 writel(u, GPIO_IN_POL(pin));
349 } else if (type == IRQ_TYPE_EDGE_BOTH) {
352 v = readl(GPIO_IN_POL(pin)) ^ readl(GPIO_DATA_IN(pin));
355 * set initial polarity based on current input level
357 u = readl(GPIO_IN_POL(pin));
358 if (v & (1 << (pin & 31)))
359 u |= 1 << (pin & 31); /* falling */
361 u &= ~(1 << (pin & 31)); /* rising */
362 writel(u, GPIO_IN_POL(pin));
365 desc->status = (desc->status & ~IRQ_TYPE_SENSE_MASK) | type;
370 struct irq_chip orion_gpio_irq_chip = {
371 .name = "orion_gpio",
373 .mask = gpio_irq_mask,
374 .unmask = gpio_irq_unmask,
375 .set_type = gpio_irq_set_type,
378 void orion_gpio_irq_handler(int pinoff)
383 cause = readl(GPIO_DATA_IN(pinoff)) & readl(GPIO_LEVEL_MASK(pinoff));
384 cause |= readl(GPIO_EDGE_CAUSE(pinoff)) & readl(GPIO_EDGE_MASK(pinoff));
386 for (pin = pinoff; pin < pinoff + 8; pin++) {
387 int irq = gpio_to_irq(pin);
388 struct irq_desc *desc = irq_desc + irq;
390 if (!(cause & (1 << (pin & 31))))
393 if ((desc->status & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH) {
394 /* Swap polarity (race with GPIO line) */
397 polarity = readl(GPIO_IN_POL(pin));
398 polarity ^= 1 << (pin & 31);
399 writel(polarity, GPIO_IN_POL(pin));
401 desc_handle_irq(irq, desc);