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>
18 #include <linux/gpio.h>
19 #include <linux/leds.h>
22 * GPIO unit register offsets.
24 #define GPIO_OUT_OFF 0x0000
25 #define GPIO_IO_CONF_OFF 0x0004
26 #define GPIO_BLINK_EN_OFF 0x0008
27 #define GPIO_IN_POL_OFF 0x000c
28 #define GPIO_DATA_IN_OFF 0x0010
29 #define GPIO_EDGE_CAUSE_OFF 0x0014
30 #define GPIO_EDGE_MASK_OFF 0x0018
31 #define GPIO_LEVEL_MASK_OFF 0x001c
33 struct orion_gpio_chip
{
34 struct gpio_chip chip
;
37 unsigned long valid_input
;
38 unsigned long valid_output
;
40 int secondary_irq_base
;
43 static void __iomem
*GPIO_OUT(struct orion_gpio_chip
*ochip
)
45 return ochip
->base
+ GPIO_OUT_OFF
;
48 static void __iomem
*GPIO_IO_CONF(struct orion_gpio_chip
*ochip
)
50 return ochip
->base
+ GPIO_IO_CONF_OFF
;
53 static void __iomem
*GPIO_BLINK_EN(struct orion_gpio_chip
*ochip
)
55 return ochip
->base
+ GPIO_BLINK_EN_OFF
;
58 static void __iomem
*GPIO_IN_POL(struct orion_gpio_chip
*ochip
)
60 return ochip
->base
+ GPIO_IN_POL_OFF
;
63 static void __iomem
*GPIO_DATA_IN(struct orion_gpio_chip
*ochip
)
65 return ochip
->base
+ GPIO_DATA_IN_OFF
;
68 static void __iomem
*GPIO_EDGE_CAUSE(struct orion_gpio_chip
*ochip
)
70 return ochip
->base
+ GPIO_EDGE_CAUSE_OFF
;
73 static void __iomem
*GPIO_EDGE_MASK(struct orion_gpio_chip
*ochip
)
75 return ochip
->base
+ ochip
->mask_offset
+ GPIO_EDGE_MASK_OFF
;
78 static void __iomem
*GPIO_LEVEL_MASK(struct orion_gpio_chip
*ochip
)
80 return ochip
->base
+ ochip
->mask_offset
+ GPIO_LEVEL_MASK_OFF
;
84 static struct orion_gpio_chip orion_gpio_chips
[2];
85 static int orion_gpio_chip_count
;
88 __set_direction(struct orion_gpio_chip
*ochip
, unsigned pin
, int input
)
92 u
= readl(GPIO_IO_CONF(ochip
));
97 writel(u
, GPIO_IO_CONF(ochip
));
100 static void __set_level(struct orion_gpio_chip
*ochip
, unsigned pin
, int high
)
104 u
= readl(GPIO_OUT(ochip
));
109 writel(u
, GPIO_OUT(ochip
));
113 __set_blinking(struct orion_gpio_chip
*ochip
, unsigned pin
, int blink
)
117 u
= readl(GPIO_BLINK_EN(ochip
));
122 writel(u
, GPIO_BLINK_EN(ochip
));
126 orion_gpio_is_valid(struct orion_gpio_chip
*ochip
, unsigned pin
, int mode
)
128 if (pin
>= ochip
->chip
.ngpio
)
131 if ((mode
& GPIO_INPUT_OK
) && !test_bit(pin
, &ochip
->valid_input
))
134 if ((mode
& GPIO_OUTPUT_OK
) && !test_bit(pin
, &ochip
->valid_output
))
140 pr_debug("%s: invalid GPIO %d\n", __func__
, pin
);
145 * GENERIC_GPIO primitives.
147 static int orion_gpio_request(struct gpio_chip
*chip
, unsigned pin
)
149 struct orion_gpio_chip
*ochip
=
150 container_of(chip
, struct orion_gpio_chip
, chip
);
152 if (orion_gpio_is_valid(ochip
, pin
, GPIO_INPUT_OK
) ||
153 orion_gpio_is_valid(ochip
, pin
, GPIO_OUTPUT_OK
))
159 static int orion_gpio_direction_input(struct gpio_chip
*chip
, unsigned pin
)
161 struct orion_gpio_chip
*ochip
=
162 container_of(chip
, struct orion_gpio_chip
, chip
);
165 if (!orion_gpio_is_valid(ochip
, pin
, GPIO_INPUT_OK
))
168 spin_lock_irqsave(&ochip
->lock
, flags
);
169 __set_direction(ochip
, pin
, 1);
170 spin_unlock_irqrestore(&ochip
->lock
, flags
);
175 static int orion_gpio_get(struct gpio_chip
*chip
, unsigned pin
)
177 struct orion_gpio_chip
*ochip
=
178 container_of(chip
, struct orion_gpio_chip
, chip
);
181 if (readl(GPIO_IO_CONF(ochip
)) & (1 << pin
)) {
182 val
= readl(GPIO_DATA_IN(ochip
)) ^ readl(GPIO_IN_POL(ochip
));
184 val
= readl(GPIO_OUT(ochip
));
187 return (val
>> pin
) & 1;
191 orion_gpio_direction_output(struct gpio_chip
*chip
, unsigned pin
, int value
)
193 struct orion_gpio_chip
*ochip
=
194 container_of(chip
, struct orion_gpio_chip
, chip
);
197 if (!orion_gpio_is_valid(ochip
, pin
, GPIO_OUTPUT_OK
))
200 spin_lock_irqsave(&ochip
->lock
, flags
);
201 __set_blinking(ochip
, pin
, 0);
202 __set_level(ochip
, pin
, value
);
203 __set_direction(ochip
, pin
, 0);
204 spin_unlock_irqrestore(&ochip
->lock
, flags
);
209 static void orion_gpio_set(struct gpio_chip
*chip
, unsigned pin
, int value
)
211 struct orion_gpio_chip
*ochip
=
212 container_of(chip
, struct orion_gpio_chip
, chip
);
215 spin_lock_irqsave(&ochip
->lock
, flags
);
216 __set_level(ochip
, pin
, value
);
217 spin_unlock_irqrestore(&ochip
->lock
, flags
);
220 static int orion_gpio_to_irq(struct gpio_chip
*chip
, unsigned pin
)
222 struct orion_gpio_chip
*ochip
=
223 container_of(chip
, struct orion_gpio_chip
, chip
);
225 return ochip
->secondary_irq_base
+ pin
;
230 * Orion-specific GPIO API extensions.
232 static struct orion_gpio_chip
*orion_gpio_chip_find(int pin
)
236 for (i
= 0; i
< orion_gpio_chip_count
; i
++) {
237 struct orion_gpio_chip
*ochip
= orion_gpio_chips
+ i
;
238 struct gpio_chip
*chip
= &ochip
->chip
;
240 if (pin
>= chip
->base
&& pin
< chip
->base
+ chip
->ngpio
)
247 void __init
orion_gpio_set_unused(unsigned pin
)
249 struct orion_gpio_chip
*ochip
= orion_gpio_chip_find(pin
);
254 pin
-= ochip
->chip
.base
;
256 /* Configure as output, drive low. */
257 __set_level(ochip
, pin
, 0);
258 __set_direction(ochip
, pin
, 0);
261 void __init
orion_gpio_set_valid(unsigned pin
, int mode
)
263 struct orion_gpio_chip
*ochip
= orion_gpio_chip_find(pin
);
268 pin
-= ochip
->chip
.base
;
271 mode
= GPIO_INPUT_OK
| GPIO_OUTPUT_OK
;
273 if (mode
& GPIO_INPUT_OK
)
274 __set_bit(pin
, &ochip
->valid_input
);
276 __clear_bit(pin
, &ochip
->valid_input
);
278 if (mode
& GPIO_OUTPUT_OK
)
279 __set_bit(pin
, &ochip
->valid_output
);
281 __clear_bit(pin
, &ochip
->valid_output
);
284 void orion_gpio_set_blink(unsigned pin
, int blink
)
286 struct orion_gpio_chip
*ochip
= orion_gpio_chip_find(pin
);
292 spin_lock_irqsave(&ochip
->lock
, flags
);
293 __set_level(ochip
, pin
& 31, 0);
294 __set_blinking(ochip
, pin
& 31, blink
);
295 spin_unlock_irqrestore(&ochip
->lock
, flags
);
297 EXPORT_SYMBOL(orion_gpio_set_blink
);
299 #define ORION_BLINK_HALF_PERIOD 100 /* ms */
301 int orion_gpio_led_blink_set(unsigned gpio
, int state
,
302 unsigned long *delay_on
, unsigned long *delay_off
)
305 if (delay_on
&& delay_off
&& !*delay_on
&& !*delay_off
)
306 *delay_on
= *delay_off
= ORION_BLINK_HALF_PERIOD
;
309 case GPIO_LED_NO_BLINK_LOW
:
310 case GPIO_LED_NO_BLINK_HIGH
:
311 orion_gpio_set_blink(gpio
, 0);
312 gpio_set_value(gpio
, state
);
315 orion_gpio_set_blink(gpio
, 1);
319 EXPORT_SYMBOL_GPL(orion_gpio_led_blink_set
);
322 /*****************************************************************************
325 * GPIO_IN_POL register controls whether GPIO_DATA_IN will hold the same
326 * value of the line or the opposite value.
328 * Level IRQ handlers: DATA_IN is used directly as cause register.
329 * Interrupt are masked by LEVEL_MASK registers.
330 * Edge IRQ handlers: Change in DATA_IN are latched in EDGE_CAUSE.
331 * Interrupt are masked by EDGE_MASK registers.
332 * Both-edge handlers: Similar to regular Edge handlers, but also swaps
333 * the polarity to catch the next line transaction.
334 * This is a race condition that might not perfectly
335 * work on some use cases.
337 * Every eight GPIO lines are grouped (OR'ed) before going up to main
341 * data-in /--------| |-----| |----\
342 * -----| |----- ---- to main cause reg
343 * X \----------------| |----/
344 * polarity LEVEL mask
346 ****************************************************************************/
348 static int gpio_irq_set_type(struct irq_data
*d
, u32 type
)
350 struct irq_chip_generic
*gc
= irq_data_get_irq_chip_data(d
);
351 struct irq_chip_type
*ct
= irq_data_get_chip_type(d
);
352 struct orion_gpio_chip
*ochip
= gc
->private;
356 pin
= d
->irq
- gc
->irq_base
;
358 u
= readl(GPIO_IO_CONF(ochip
)) & (1 << pin
);
360 printk(KERN_ERR
"orion gpio_irq_set_type failed "
361 "(irq %d, pin %d).\n", d
->irq
, pin
);
365 type
&= IRQ_TYPE_SENSE_MASK
;
366 if (type
== IRQ_TYPE_NONE
)
369 /* Check if we need to change chip and handler */
370 if (!(ct
->type
& type
))
371 if (irq_setup_alt_chip(d
, type
))
375 * Configure interrupt polarity.
377 if (type
== IRQ_TYPE_EDGE_RISING
|| type
== IRQ_TYPE_LEVEL_HIGH
) {
378 u
= readl(GPIO_IN_POL(ochip
));
380 writel(u
, GPIO_IN_POL(ochip
));
381 } else if (type
== IRQ_TYPE_EDGE_FALLING
|| type
== IRQ_TYPE_LEVEL_LOW
) {
382 u
= readl(GPIO_IN_POL(ochip
));
384 writel(u
, GPIO_IN_POL(ochip
));
385 } else if (type
== IRQ_TYPE_EDGE_BOTH
) {
388 v
= readl(GPIO_IN_POL(ochip
)) ^ readl(GPIO_DATA_IN(ochip
));
391 * set initial polarity based on current input level
393 u
= readl(GPIO_IN_POL(ochip
));
395 u
|= 1 << pin
; /* falling */
397 u
&= ~(1 << pin
); /* rising */
398 writel(u
, GPIO_IN_POL(ochip
));
404 void __init
orion_gpio_init(int gpio_base
, int ngpio
,
405 u32 base
, int mask_offset
, int secondary_irq_base
)
407 struct orion_gpio_chip
*ochip
;
408 struct irq_chip_generic
*gc
;
409 struct irq_chip_type
*ct
;
412 if (orion_gpio_chip_count
== ARRAY_SIZE(orion_gpio_chips
))
415 snprintf(gc_label
, sizeof(gc_label
), "orion_gpio%d",
416 orion_gpio_chip_count
);
418 ochip
= orion_gpio_chips
+ orion_gpio_chip_count
;
419 ochip
->chip
.label
= kstrdup(gc_label
, GFP_KERNEL
);
420 ochip
->chip
.request
= orion_gpio_request
;
421 ochip
->chip
.direction_input
= orion_gpio_direction_input
;
422 ochip
->chip
.get
= orion_gpio_get
;
423 ochip
->chip
.direction_output
= orion_gpio_direction_output
;
424 ochip
->chip
.set
= orion_gpio_set
;
425 ochip
->chip
.to_irq
= orion_gpio_to_irq
;
426 ochip
->chip
.base
= gpio_base
;
427 ochip
->chip
.ngpio
= ngpio
;
428 ochip
->chip
.can_sleep
= 0;
429 spin_lock_init(&ochip
->lock
);
430 ochip
->base
= (void __iomem
*)base
;
431 ochip
->valid_input
= 0;
432 ochip
->valid_output
= 0;
433 ochip
->mask_offset
= mask_offset
;
434 ochip
->secondary_irq_base
= secondary_irq_base
;
436 gpiochip_add(&ochip
->chip
);
438 orion_gpio_chip_count
++;
441 * Mask and clear GPIO interrupts.
443 writel(0, GPIO_EDGE_CAUSE(ochip
));
444 writel(0, GPIO_EDGE_MASK(ochip
));
445 writel(0, GPIO_LEVEL_MASK(ochip
));
447 gc
= irq_alloc_generic_chip("orion_gpio_irq", 2, secondary_irq_base
,
448 ochip
->base
, handle_level_irq
);
452 ct
->regs
.mask
= ochip
->mask_offset
+ GPIO_LEVEL_MASK_OFF
;
453 ct
->type
= IRQ_TYPE_LEVEL_HIGH
| IRQ_TYPE_LEVEL_LOW
;
454 ct
->chip
.irq_mask
= irq_gc_mask_clr_bit
;
455 ct
->chip
.irq_unmask
= irq_gc_mask_set_bit
;
456 ct
->chip
.irq_set_type
= gpio_irq_set_type
;
459 ct
->regs
.mask
= ochip
->mask_offset
+ GPIO_EDGE_MASK_OFF
;
460 ct
->regs
.ack
= GPIO_EDGE_CAUSE_OFF
;
461 ct
->type
= IRQ_TYPE_EDGE_RISING
| IRQ_TYPE_EDGE_FALLING
;
462 ct
->chip
.irq_ack
= irq_gc_ack_clr_bit
;
463 ct
->chip
.irq_mask
= irq_gc_mask_clr_bit
;
464 ct
->chip
.irq_unmask
= irq_gc_mask_set_bit
;
465 ct
->chip
.irq_set_type
= gpio_irq_set_type
;
466 ct
->handler
= handle_edge_irq
;
468 irq_setup_generic_chip(gc
, IRQ_MSK(ngpio
), IRQ_GC_INIT_MASK_CACHE
,
469 IRQ_NOREQUEST
, IRQ_LEVEL
| IRQ_NOPROBE
);
472 void orion_gpio_irq_handler(int pinoff
)
474 struct orion_gpio_chip
*ochip
;
478 ochip
= orion_gpio_chip_find(pinoff
);
482 cause
= readl(GPIO_DATA_IN(ochip
)) & readl(GPIO_LEVEL_MASK(ochip
));
483 cause
|= readl(GPIO_EDGE_CAUSE(ochip
)) & readl(GPIO_EDGE_MASK(ochip
));
485 for (i
= 0; i
< ochip
->chip
.ngpio
; i
++) {
488 irq
= ochip
->secondary_irq_base
+ i
;
490 if (!(cause
& (1 << i
)))
493 type
= irqd_get_trigger_type(irq_get_irq_data(irq
));
494 if ((type
& IRQ_TYPE_SENSE_MASK
) == IRQ_TYPE_EDGE_BOTH
) {
495 /* Swap polarity (race with GPIO line) */
498 polarity
= readl(GPIO_IN_POL(ochip
));
500 writel(polarity
, GPIO_IN_POL(ochip
));
502 generic_handle_irq(irq
);