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[BITS_TO_LONGS(GPIO_MAX)];
24 static inline void __set_direction(unsigned pin, int input)
28 u = readl(GPIO_IO_CONF(pin));
32 u &= ~(1 << (pin & 31));
33 writel(u, GPIO_IO_CONF(pin));
36 static void __set_level(unsigned pin, int high)
40 u = readl(GPIO_OUT(pin));
44 u &= ~(1 << (pin & 31));
45 writel(u, GPIO_OUT(pin));
50 * GENERIC_GPIO primitives.
52 int gpio_direction_input(unsigned pin)
56 if (pin >= GPIO_MAX || !test_bit(pin, gpio_valid)) {
57 pr_debug("%s: invalid GPIO %d\n", __func__, pin);
61 spin_lock_irqsave(&gpio_lock, flags);
64 * Some callers might not have used gpio_request(),
65 * so flag this pin as requested now.
67 if (gpio_label[pin] == NULL)
68 gpio_label[pin] = "?";
71 * Configure GPIO direction.
73 __set_direction(pin, 1);
75 spin_unlock_irqrestore(&gpio_lock, flags);
79 EXPORT_SYMBOL(gpio_direction_input);
81 int gpio_direction_output(unsigned pin, int value)
86 if (pin >= GPIO_MAX || !test_bit(pin, gpio_valid)) {
87 pr_debug("%s: invalid GPIO %d\n", __func__, pin);
91 spin_lock_irqsave(&gpio_lock, flags);
94 * Some callers might not have used gpio_request(),
95 * so flag this pin as requested now.
97 if (gpio_label[pin] == NULL)
98 gpio_label[pin] = "?";
103 u = readl(GPIO_BLINK_EN(pin));
104 u &= ~(1 << (pin & 31));
105 writel(u, GPIO_BLINK_EN(pin));
108 * Configure GPIO output value.
110 __set_level(pin, value);
113 * Configure GPIO direction.
115 __set_direction(pin, 0);
117 spin_unlock_irqrestore(&gpio_lock, flags);
121 EXPORT_SYMBOL(gpio_direction_output);
123 int gpio_get_value(unsigned pin)
127 if (readl(GPIO_IO_CONF(pin)) & (1 << (pin & 31)))
128 val = readl(GPIO_DATA_IN(pin)) ^ readl(GPIO_IN_POL(pin));
130 val = readl(GPIO_OUT(pin));
132 return (val >> (pin & 31)) & 1;
134 EXPORT_SYMBOL(gpio_get_value);
136 void gpio_set_value(unsigned pin, int value)
141 spin_lock_irqsave(&gpio_lock, flags);
146 u = readl(GPIO_BLINK_EN(pin));
147 u &= ~(1 << (pin & 31));
148 writel(u, GPIO_BLINK_EN(pin));
151 * Configure GPIO output value.
153 __set_level(pin, value);
155 spin_unlock_irqrestore(&gpio_lock, flags);
157 EXPORT_SYMBOL(gpio_set_value);
159 int gpio_request(unsigned pin, const char *label)
164 if (pin >= GPIO_MAX || !test_bit(pin, gpio_valid)) {
165 pr_debug("%s: invalid GPIO %d\n", __func__, pin);
169 spin_lock_irqsave(&gpio_lock, flags);
170 if (gpio_label[pin] == NULL) {
171 gpio_label[pin] = label ? label : "?";
174 pr_debug("%s: GPIO %d already used as %s\n",
175 __func__, pin, gpio_label[pin]);
178 spin_unlock_irqrestore(&gpio_lock, flags);
182 EXPORT_SYMBOL(gpio_request);
184 void gpio_free(unsigned pin)
186 if (pin >= GPIO_MAX || !test_bit(pin, gpio_valid)) {
187 pr_debug("%s: invalid GPIO %d\n", __func__, pin);
191 if (gpio_label[pin] == NULL)
192 pr_warning("%s: GPIO %d already freed\n", __func__, pin);
194 gpio_label[pin] = NULL;
196 EXPORT_SYMBOL(gpio_free);
200 * Orion-specific GPIO API extensions.
202 void __init orion_gpio_set_unused(unsigned pin)
205 * Configure as output, drive low.
208 __set_direction(pin, 0);
211 void __init orion_gpio_set_valid(unsigned pin, int valid)
214 __set_bit(pin, gpio_valid);
216 __clear_bit(pin, gpio_valid);
219 void orion_gpio_set_blink(unsigned pin, int blink)
224 spin_lock_irqsave(&gpio_lock, flags);
227 * Set output value to zero.
231 u = readl(GPIO_BLINK_EN(pin));
233 u |= 1 << (pin & 31);
235 u &= ~(1 << (pin & 31));
236 writel(u, GPIO_BLINK_EN(pin));
238 spin_unlock_irqrestore(&gpio_lock, flags);
240 EXPORT_SYMBOL(orion_gpio_set_blink);
243 /*****************************************************************************
246 * GPIO_IN_POL register controls whether GPIO_DATA_IN will hold the same
247 * value of the line or the opposite value.
249 * Level IRQ handlers: DATA_IN is used directly as cause register.
250 * Interrupt are masked by LEVEL_MASK registers.
251 * Edge IRQ handlers: Change in DATA_IN are latched in EDGE_CAUSE.
252 * Interrupt are masked by EDGE_MASK registers.
253 * Both-edge handlers: Similar to regular Edge handlers, but also swaps
254 * the polarity to catch the next line transaction.
255 * This is a race condition that might not perfectly
256 * work on some use cases.
258 * Every eight GPIO lines are grouped (OR'ed) before going up to main
262 * data-in /--------| |-----| |----\
263 * -----| |----- ---- to main cause reg
264 * X \----------------| |----/
265 * polarity LEVEL mask
267 ****************************************************************************/
268 static void gpio_irq_edge_ack(u32 irq)
270 int pin = irq_to_gpio(irq);
272 writel(~(1 << (pin & 31)), GPIO_EDGE_CAUSE(pin));
275 static void gpio_irq_edge_mask(u32 irq)
277 int pin = irq_to_gpio(irq);
280 u = readl(GPIO_EDGE_MASK(pin));
281 u &= ~(1 << (pin & 31));
282 writel(u, GPIO_EDGE_MASK(pin));
285 static void gpio_irq_edge_unmask(u32 irq)
287 int pin = irq_to_gpio(irq);
290 u = readl(GPIO_EDGE_MASK(pin));
291 u |= 1 << (pin & 31);
292 writel(u, GPIO_EDGE_MASK(pin));
295 static void gpio_irq_level_mask(u32 irq)
297 int pin = irq_to_gpio(irq);
300 u = readl(GPIO_LEVEL_MASK(pin));
301 u &= ~(1 << (pin & 31));
302 writel(u, GPIO_LEVEL_MASK(pin));
305 static void gpio_irq_level_unmask(u32 irq)
307 int pin = irq_to_gpio(irq);
310 u = readl(GPIO_LEVEL_MASK(pin));
311 u |= 1 << (pin & 31);
312 writel(u, GPIO_LEVEL_MASK(pin));
315 static int gpio_irq_set_type(u32 irq, u32 type)
317 int pin = irq_to_gpio(irq);
318 struct irq_desc *desc;
321 u = readl(GPIO_IO_CONF(pin)) & (1 << (pin & 31));
323 printk(KERN_ERR "orion gpio_irq_set_type failed "
324 "(irq %d, pin %d).\n", irq, pin);
328 desc = irq_desc + irq;
331 * Set edge/level type.
333 if (type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING)) {
334 desc->chip = &orion_gpio_irq_edge_chip;
335 } else if (type & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW)) {
336 desc->chip = &orion_gpio_irq_level_chip;
338 printk(KERN_ERR "failed to set irq=%d (type=%d)\n", irq, type);
343 * Configure interrupt polarity.
345 if (type == IRQ_TYPE_EDGE_RISING || type == IRQ_TYPE_LEVEL_HIGH) {
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_FALLING || type == IRQ_TYPE_LEVEL_LOW) {
350 u = readl(GPIO_IN_POL(pin));
351 u |= 1 << (pin & 31);
352 writel(u, GPIO_IN_POL(pin));
353 } else if (type == IRQ_TYPE_EDGE_BOTH) {
356 v = readl(GPIO_IN_POL(pin)) ^ readl(GPIO_DATA_IN(pin));
359 * set initial polarity based on current input level
361 u = readl(GPIO_IN_POL(pin));
362 if (v & (1 << (pin & 31)))
363 u |= 1 << (pin & 31); /* falling */
365 u &= ~(1 << (pin & 31)); /* rising */
366 writel(u, GPIO_IN_POL(pin));
369 desc->status = (desc->status & ~IRQ_TYPE_SENSE_MASK) | type;
374 struct irq_chip orion_gpio_irq_edge_chip = {
375 .name = "orion_gpio_irq_edge",
376 .ack = gpio_irq_edge_ack,
377 .mask = gpio_irq_edge_mask,
378 .unmask = gpio_irq_edge_unmask,
379 .set_type = gpio_irq_set_type,
382 struct irq_chip orion_gpio_irq_level_chip = {
383 .name = "orion_gpio_irq_level",
384 .mask = gpio_irq_level_mask,
385 .mask_ack = gpio_irq_level_mask,
386 .unmask = gpio_irq_level_unmask,
387 .set_type = gpio_irq_set_type,
390 void orion_gpio_irq_handler(int pinoff)
395 cause = readl(GPIO_DATA_IN(pinoff)) & readl(GPIO_LEVEL_MASK(pinoff));
396 cause |= readl(GPIO_EDGE_CAUSE(pinoff)) & readl(GPIO_EDGE_MASK(pinoff));
398 for (pin = pinoff; pin < pinoff + 8; pin++) {
399 int irq = gpio_to_irq(pin);
400 struct irq_desc *desc = irq_desc + irq;
402 if (!(cause & (1 << (pin & 31))))
405 if ((desc->status & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH) {
406 /* Swap polarity (race with GPIO line) */
409 polarity = readl(GPIO_IN_POL(pin));
410 polarity ^= 1 << (pin & 31);
411 writel(polarity, GPIO_IN_POL(pin));
413 desc_handle_irq(irq, desc);