2 * GPIO driver for Marvell SoCs
4 * Copyright (C) 2012 Marvell
6 * Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
7 * Andrew Lunn <andrew@lunn.ch>
8 * Sebastian Hesselbarth <sebastian.hesselbarth@gmail.com>
10 * This file is licensed under the terms of the GNU General Public
11 * License version 2. This program is licensed "as is" without any
12 * warranty of any kind, whether express or implied.
14 * This driver is a fairly straightforward GPIO driver for the
15 * complete family of Marvell EBU SoC platforms (Orion, Dove,
16 * Kirkwood, Discovery, Armada 370/XP). The only complexity of this
17 * driver is the different register layout that exists between the
18 * non-SMP platforms (Orion, Dove, Kirkwood, Armada 370) and the SMP
19 * platforms (MV78200 from the Discovery family and the Armada
20 * XP). Therefore, this driver handles three variants of the GPIO
22 * - the basic variant, called "orion-gpio", with the simplest
23 * register set. Used on Orion, Dove, Kirkwoord, Armada 370 and
24 * non-SMP Discovery systems
25 * - the mv78200 variant for MV78200 Discovery systems. This variant
26 * turns the edge mask and level mask registers into CPU0 edge
27 * mask/level mask registers, and adds CPU1 edge mask/level mask
29 * - the armadaxp variant for Armada XP systems. This variant keeps
30 * the normal cause/edge mask/level mask registers when the global
31 * interrupts are used, but adds per-CPU cause/edge mask/level mask
32 * registers n a separate memory area for the per-CPU GPIO
36 #include <linux/module.h>
37 #include <linux/gpio.h>
38 #include <linux/irq.h>
39 #include <linux/slab.h>
40 #include <linux/irqdomain.h>
42 #include <linux/of_irq.h>
43 #include <linux/of_device.h>
44 #include <linux/pinctrl/consumer.h>
47 * GPIO unit register offsets.
49 #define GPIO_OUT_OFF 0x0000
50 #define GPIO_IO_CONF_OFF 0x0004
51 #define GPIO_BLINK_EN_OFF 0x0008
52 #define GPIO_IN_POL_OFF 0x000c
53 #define GPIO_DATA_IN_OFF 0x0010
54 #define GPIO_EDGE_CAUSE_OFF 0x0014
55 #define GPIO_EDGE_MASK_OFF 0x0018
56 #define GPIO_LEVEL_MASK_OFF 0x001c
58 /* The MV78200 has per-CPU registers for edge mask and level mask */
59 #define GPIO_EDGE_MASK_MV78200_OFF(cpu) ((cpu) ? 0x30 : 0x18)
60 #define GPIO_LEVEL_MASK_MV78200_OFF(cpu) ((cpu) ? 0x34 : 0x1C)
62 /* The Armada XP has per-CPU registers for interrupt cause, interrupt
63 * mask and interrupt level mask. Those are relative to the
65 #define GPIO_EDGE_CAUSE_ARMADAXP_OFF(cpu) ((cpu) * 0x4)
66 #define GPIO_EDGE_MASK_ARMADAXP_OFF(cpu) (0x10 + (cpu) * 0x4)
67 #define GPIO_LEVEL_MASK_ARMADAXP_OFF(cpu) (0x20 + (cpu) * 0x4)
69 #define MVEBU_GPIO_SOC_VARIANT_ORION 0x1
70 #define MVEBU_GPIO_SOC_VARIANT_MV78200 0x2
71 #define MVEBU_GPIO_SOC_VARIANT_ARMADAXP 0x3
73 #define MVEBU_MAX_GPIO_PER_BANK 32
75 struct mvebu_gpio_chip {
76 struct gpio_chip chip;
78 void __iomem *membase;
79 void __iomem *percpu_membase;
81 struct irq_domain *domain;
86 * Functions returning addresses of individual registers for a given
89 static inline void __iomem *mvebu_gpioreg_out(struct mvebu_gpio_chip *mvchip)
91 return mvchip->membase + GPIO_OUT_OFF;
94 static inline void __iomem *mvebu_gpioreg_blink(struct mvebu_gpio_chip *mvchip)
96 return mvchip->membase + GPIO_BLINK_EN_OFF;
99 static inline void __iomem *mvebu_gpioreg_io_conf(struct mvebu_gpio_chip *mvchip)
101 return mvchip->membase + GPIO_IO_CONF_OFF;
104 static inline void __iomem *mvebu_gpioreg_in_pol(struct mvebu_gpio_chip *mvchip)
106 return mvchip->membase + GPIO_IN_POL_OFF;
109 static inline void __iomem *mvebu_gpioreg_data_in(struct mvebu_gpio_chip *mvchip)
111 return mvchip->membase + GPIO_DATA_IN_OFF;
114 static inline void __iomem *mvebu_gpioreg_edge_cause(struct mvebu_gpio_chip *mvchip)
118 switch(mvchip->soc_variant) {
119 case MVEBU_GPIO_SOC_VARIANT_ORION:
120 case MVEBU_GPIO_SOC_VARIANT_MV78200:
121 return mvchip->membase + GPIO_EDGE_CAUSE_OFF;
122 case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
123 cpu = smp_processor_id();
124 return mvchip->percpu_membase + GPIO_EDGE_CAUSE_ARMADAXP_OFF(cpu);
130 static inline void __iomem *mvebu_gpioreg_edge_mask(struct mvebu_gpio_chip *mvchip)
134 switch(mvchip->soc_variant) {
135 case MVEBU_GPIO_SOC_VARIANT_ORION:
136 return mvchip->membase + GPIO_EDGE_MASK_OFF;
137 case MVEBU_GPIO_SOC_VARIANT_MV78200:
138 cpu = smp_processor_id();
139 return mvchip->membase + GPIO_EDGE_MASK_MV78200_OFF(cpu);
140 case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
141 cpu = smp_processor_id();
142 return mvchip->percpu_membase + GPIO_EDGE_MASK_ARMADAXP_OFF(cpu);
148 static void __iomem *mvebu_gpioreg_level_mask(struct mvebu_gpio_chip *mvchip)
152 switch(mvchip->soc_variant) {
153 case MVEBU_GPIO_SOC_VARIANT_ORION:
154 return mvchip->membase + GPIO_LEVEL_MASK_OFF;
155 case MVEBU_GPIO_SOC_VARIANT_MV78200:
156 cpu = smp_processor_id();
157 return mvchip->membase + GPIO_LEVEL_MASK_MV78200_OFF(cpu);
158 case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
159 cpu = smp_processor_id();
160 return mvchip->percpu_membase + GPIO_LEVEL_MASK_ARMADAXP_OFF(cpu);
167 * Functions implementing the gpio_chip methods
170 static int mvebu_gpio_request(struct gpio_chip *chip, unsigned pin)
172 return pinctrl_request_gpio(chip->base + pin);
175 static void mvebu_gpio_free(struct gpio_chip *chip, unsigned pin)
177 pinctrl_free_gpio(chip->base + pin);
180 static void mvebu_gpio_set(struct gpio_chip *chip, unsigned pin, int value)
182 struct mvebu_gpio_chip *mvchip =
183 container_of(chip, struct mvebu_gpio_chip, chip);
187 spin_lock_irqsave(&mvchip->lock, flags);
188 u = readl_relaxed(mvebu_gpioreg_out(mvchip));
193 writel_relaxed(u, mvebu_gpioreg_out(mvchip));
194 spin_unlock_irqrestore(&mvchip->lock, flags);
197 static int mvebu_gpio_get(struct gpio_chip *chip, unsigned pin)
199 struct mvebu_gpio_chip *mvchip =
200 container_of(chip, struct mvebu_gpio_chip, chip);
203 if (readl_relaxed(mvebu_gpioreg_io_conf(mvchip)) & (1 << pin)) {
204 u = readl_relaxed(mvebu_gpioreg_data_in(mvchip)) ^
205 readl_relaxed(mvebu_gpioreg_in_pol(mvchip));
207 u = readl_relaxed(mvebu_gpioreg_out(mvchip));
210 return (u >> pin) & 1;
213 static void mvebu_gpio_blink(struct gpio_chip *chip, unsigned pin, int value)
215 struct mvebu_gpio_chip *mvchip =
216 container_of(chip, struct mvebu_gpio_chip, chip);
220 spin_lock_irqsave(&mvchip->lock, flags);
221 u = readl_relaxed(mvebu_gpioreg_blink(mvchip));
226 writel_relaxed(u, mvebu_gpioreg_blink(mvchip));
227 spin_unlock_irqrestore(&mvchip->lock, flags);
230 static int mvebu_gpio_direction_input(struct gpio_chip *chip, unsigned pin)
232 struct mvebu_gpio_chip *mvchip =
233 container_of(chip, struct mvebu_gpio_chip, chip);
238 /* Check with the pinctrl driver whether this pin is usable as
240 ret = pinctrl_gpio_direction_input(chip->base + pin);
244 spin_lock_irqsave(&mvchip->lock, flags);
245 u = readl_relaxed(mvebu_gpioreg_io_conf(mvchip));
247 writel_relaxed(u, mvebu_gpioreg_io_conf(mvchip));
248 spin_unlock_irqrestore(&mvchip->lock, flags);
253 static int mvebu_gpio_direction_output(struct gpio_chip *chip, unsigned pin,
256 struct mvebu_gpio_chip *mvchip =
257 container_of(chip, struct mvebu_gpio_chip, chip);
262 /* Check with the pinctrl driver whether this pin is usable as
264 ret = pinctrl_gpio_direction_output(chip->base + pin);
268 mvebu_gpio_blink(chip, pin, 0);
269 mvebu_gpio_set(chip, pin, value);
271 spin_lock_irqsave(&mvchip->lock, flags);
272 u = readl_relaxed(mvebu_gpioreg_io_conf(mvchip));
274 writel_relaxed(u, mvebu_gpioreg_io_conf(mvchip));
275 spin_unlock_irqrestore(&mvchip->lock, flags);
280 static int mvebu_gpio_to_irq(struct gpio_chip *chip, unsigned pin)
282 struct mvebu_gpio_chip *mvchip =
283 container_of(chip, struct mvebu_gpio_chip, chip);
284 return irq_create_mapping(mvchip->domain, pin);
288 * Functions implementing the irq_chip methods
290 static void mvebu_gpio_irq_ack(struct irq_data *d)
292 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
293 struct mvebu_gpio_chip *mvchip = gc->private;
294 u32 mask = ~(1 << (d->irq - gc->irq_base));
297 writel_relaxed(mask, mvebu_gpioreg_edge_cause(mvchip));
301 static void mvebu_gpio_edge_irq_mask(struct irq_data *d)
303 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
304 struct mvebu_gpio_chip *mvchip = gc->private;
305 u32 mask = 1 << (d->irq - gc->irq_base);
308 gc->mask_cache &= ~mask;
309 writel_relaxed(gc->mask_cache, mvebu_gpioreg_edge_mask(mvchip));
313 static void mvebu_gpio_edge_irq_unmask(struct irq_data *d)
315 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
316 struct mvebu_gpio_chip *mvchip = gc->private;
317 u32 mask = 1 << (d->irq - gc->irq_base);
320 gc->mask_cache |= mask;
321 writel_relaxed(gc->mask_cache, mvebu_gpioreg_edge_mask(mvchip));
325 static void mvebu_gpio_level_irq_mask(struct irq_data *d)
327 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
328 struct mvebu_gpio_chip *mvchip = gc->private;
329 u32 mask = 1 << (d->irq - gc->irq_base);
332 gc->mask_cache &= ~mask;
333 writel_relaxed(gc->mask_cache, mvebu_gpioreg_level_mask(mvchip));
337 static void mvebu_gpio_level_irq_unmask(struct irq_data *d)
339 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
340 struct mvebu_gpio_chip *mvchip = gc->private;
341 u32 mask = 1 << (d->irq - gc->irq_base);
344 gc->mask_cache |= mask;
345 writel_relaxed(gc->mask_cache, mvebu_gpioreg_level_mask(mvchip));
349 /*****************************************************************************
352 * GPIO_IN_POL register controls whether GPIO_DATA_IN will hold the same
353 * value of the line or the opposite value.
355 * Level IRQ handlers: DATA_IN is used directly as cause register.
356 * Interrupt are masked by LEVEL_MASK registers.
357 * Edge IRQ handlers: Change in DATA_IN are latched in EDGE_CAUSE.
358 * Interrupt are masked by EDGE_MASK registers.
359 * Both-edge handlers: Similar to regular Edge handlers, but also swaps
360 * the polarity to catch the next line transaction.
361 * This is a race condition that might not perfectly
362 * work on some use cases.
364 * Every eight GPIO lines are grouped (OR'ed) before going up to main
368 * data-in /--------| |-----| |----\
369 * -----| |----- ---- to main cause reg
370 * X \----------------| |----/
371 * polarity LEVEL mask
373 ****************************************************************************/
375 static int mvebu_gpio_irq_set_type(struct irq_data *d, unsigned int type)
377 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
378 struct irq_chip_type *ct = irq_data_get_chip_type(d);
379 struct mvebu_gpio_chip *mvchip = gc->private;
385 u = readl_relaxed(mvebu_gpioreg_io_conf(mvchip)) & (1 << pin);
390 type &= IRQ_TYPE_SENSE_MASK;
391 if (type == IRQ_TYPE_NONE)
394 /* Check if we need to change chip and handler */
395 if (!(ct->type & type))
396 if (irq_setup_alt_chip(d, type))
400 * Configure interrupt polarity.
403 case IRQ_TYPE_EDGE_RISING:
404 case IRQ_TYPE_LEVEL_HIGH:
405 u = readl_relaxed(mvebu_gpioreg_in_pol(mvchip));
407 writel_relaxed(u, mvebu_gpioreg_in_pol(mvchip));
409 case IRQ_TYPE_EDGE_FALLING:
410 case IRQ_TYPE_LEVEL_LOW:
411 u = readl_relaxed(mvebu_gpioreg_in_pol(mvchip));
413 writel_relaxed(u, mvebu_gpioreg_in_pol(mvchip));
415 case IRQ_TYPE_EDGE_BOTH: {
418 v = readl_relaxed(mvebu_gpioreg_in_pol(mvchip)) ^
419 readl_relaxed(mvebu_gpioreg_data_in(mvchip));
422 * set initial polarity based on current input level
424 u = readl_relaxed(mvebu_gpioreg_in_pol(mvchip));
426 u |= 1 << pin; /* falling */
428 u &= ~(1 << pin); /* rising */
429 writel_relaxed(u, mvebu_gpioreg_in_pol(mvchip));
436 static void mvebu_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
438 struct mvebu_gpio_chip *mvchip = irq_get_handler_data(irq);
445 cause = readl_relaxed(mvebu_gpioreg_data_in(mvchip)) &
446 readl_relaxed(mvebu_gpioreg_level_mask(mvchip));
447 cause |= readl_relaxed(mvebu_gpioreg_edge_cause(mvchip)) &
448 readl_relaxed(mvebu_gpioreg_edge_mask(mvchip));
450 for (i = 0; i < mvchip->chip.ngpio; i++) {
453 irq = mvchip->irqbase + i;
455 if (!(cause & (1 << i)))
458 type = irqd_get_trigger_type(irq_get_irq_data(irq));
459 if ((type & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH) {
460 /* Swap polarity (race with GPIO line) */
463 polarity = readl_relaxed(mvebu_gpioreg_in_pol(mvchip));
465 writel_relaxed(polarity, mvebu_gpioreg_in_pol(mvchip));
467 generic_handle_irq(irq);
471 static struct of_device_id mvebu_gpio_of_match[] = {
473 .compatible = "marvell,orion-gpio",
474 .data = (void*) MVEBU_GPIO_SOC_VARIANT_ORION,
477 .compatible = "marvell,mv78200-gpio",
478 .data = (void*) MVEBU_GPIO_SOC_VARIANT_MV78200,
481 .compatible = "marvell,armadaxp-gpio",
482 .data = (void*) MVEBU_GPIO_SOC_VARIANT_ARMADAXP,
488 MODULE_DEVICE_TABLE(of, mvebu_gpio_of_match);
490 static int mvebu_gpio_probe(struct platform_device *pdev)
492 struct mvebu_gpio_chip *mvchip;
493 const struct of_device_id *match;
494 struct device_node *np = pdev->dev.of_node;
495 struct resource *res;
496 struct irq_chip_generic *gc;
497 struct irq_chip_type *ct;
502 match = of_match_device(mvebu_gpio_of_match, &pdev->dev);
504 soc_variant = (int) match->data;
506 soc_variant = MVEBU_GPIO_SOC_VARIANT_ORION;
508 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
510 dev_err(&pdev->dev, "Cannot get memory resource\n");
514 mvchip = devm_kzalloc(&pdev->dev, sizeof(struct mvebu_gpio_chip), GFP_KERNEL);
516 dev_err(&pdev->dev, "Cannot allocate memory\n");
520 if (of_property_read_u32(pdev->dev.of_node, "ngpios", &ngpios)) {
521 dev_err(&pdev->dev, "Missing ngpios OF property\n");
525 id = of_alias_get_id(pdev->dev.of_node, "gpio");
527 dev_err(&pdev->dev, "Couldn't get OF id\n");
531 mvchip->soc_variant = soc_variant;
532 mvchip->chip.label = dev_name(&pdev->dev);
533 mvchip->chip.dev = &pdev->dev;
534 mvchip->chip.request = mvebu_gpio_request;
535 mvchip->chip.free = mvebu_gpio_free;
536 mvchip->chip.direction_input = mvebu_gpio_direction_input;
537 mvchip->chip.get = mvebu_gpio_get;
538 mvchip->chip.direction_output = mvebu_gpio_direction_output;
539 mvchip->chip.set = mvebu_gpio_set;
540 mvchip->chip.to_irq = mvebu_gpio_to_irq;
541 mvchip->chip.base = id * MVEBU_MAX_GPIO_PER_BANK;
542 mvchip->chip.ngpio = ngpios;
543 mvchip->chip.can_sleep = 0;
544 mvchip->chip.of_node = np;
546 spin_lock_init(&mvchip->lock);
547 mvchip->membase = devm_request_and_ioremap(&pdev->dev, res);
548 if (! mvchip->membase) {
549 dev_err(&pdev->dev, "Cannot ioremap\n");
553 /* The Armada XP has a second range of registers for the
554 * per-CPU registers */
555 if (soc_variant == MVEBU_GPIO_SOC_VARIANT_ARMADAXP) {
556 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
558 dev_err(&pdev->dev, "Cannot get memory resource\n");
562 mvchip->percpu_membase = devm_request_and_ioremap(&pdev->dev, res);
563 if (! mvchip->percpu_membase) {
564 dev_err(&pdev->dev, "Cannot ioremap\n");
570 * Mask and clear GPIO interrupts.
572 switch(soc_variant) {
573 case MVEBU_GPIO_SOC_VARIANT_ORION:
574 writel_relaxed(0, mvchip->membase + GPIO_EDGE_CAUSE_OFF);
575 writel_relaxed(0, mvchip->membase + GPIO_EDGE_MASK_OFF);
576 writel_relaxed(0, mvchip->membase + GPIO_LEVEL_MASK_OFF);
578 case MVEBU_GPIO_SOC_VARIANT_MV78200:
579 writel_relaxed(0, mvchip->membase + GPIO_EDGE_CAUSE_OFF);
580 for (cpu = 0; cpu < 2; cpu++) {
581 writel_relaxed(0, mvchip->membase +
582 GPIO_EDGE_MASK_MV78200_OFF(cpu));
583 writel_relaxed(0, mvchip->membase +
584 GPIO_LEVEL_MASK_MV78200_OFF(cpu));
587 case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
588 writel_relaxed(0, mvchip->membase + GPIO_EDGE_CAUSE_OFF);
589 writel_relaxed(0, mvchip->membase + GPIO_EDGE_MASK_OFF);
590 writel_relaxed(0, mvchip->membase + GPIO_LEVEL_MASK_OFF);
591 for (cpu = 0; cpu < 4; cpu++) {
592 writel_relaxed(0, mvchip->percpu_membase +
593 GPIO_EDGE_CAUSE_ARMADAXP_OFF(cpu));
594 writel_relaxed(0, mvchip->percpu_membase +
595 GPIO_EDGE_MASK_ARMADAXP_OFF(cpu));
596 writel_relaxed(0, mvchip->percpu_membase +
597 GPIO_LEVEL_MASK_ARMADAXP_OFF(cpu));
604 gpiochip_add(&mvchip->chip);
606 /* Some gpio controllers do not provide irq support */
607 if (!of_irq_count(np))
610 /* Setup the interrupt handlers. Each chip can have up to 4
611 * interrupt handlers, with each handler dealing with 8 GPIO
613 for (i = 0; i < 4; i++) {
615 irq = platform_get_irq(pdev, i);
618 irq_set_handler_data(irq, mvchip);
619 irq_set_chained_handler(irq, mvebu_gpio_irq_handler);
622 mvchip->irqbase = irq_alloc_descs(-1, 0, ngpios, -1);
623 if (mvchip->irqbase < 0) {
624 dev_err(&pdev->dev, "no irqs\n");
628 gc = irq_alloc_generic_chip("mvebu_gpio_irq", 2, mvchip->irqbase,
629 mvchip->membase, handle_level_irq);
631 dev_err(&pdev->dev, "Cannot allocate generic irq_chip\n");
635 gc->private = mvchip;
636 ct = &gc->chip_types[0];
637 ct->type = IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW;
638 ct->chip.irq_mask = mvebu_gpio_level_irq_mask;
639 ct->chip.irq_unmask = mvebu_gpio_level_irq_unmask;
640 ct->chip.irq_set_type = mvebu_gpio_irq_set_type;
641 ct->chip.name = mvchip->chip.label;
643 ct = &gc->chip_types[1];
644 ct->type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
645 ct->chip.irq_ack = mvebu_gpio_irq_ack;
646 ct->chip.irq_mask = mvebu_gpio_edge_irq_mask;
647 ct->chip.irq_unmask = mvebu_gpio_edge_irq_unmask;
648 ct->chip.irq_set_type = mvebu_gpio_irq_set_type;
649 ct->handler = handle_edge_irq;
650 ct->chip.name = mvchip->chip.label;
652 irq_setup_generic_chip(gc, IRQ_MSK(ngpios), 0,
653 IRQ_NOREQUEST, IRQ_LEVEL | IRQ_NOPROBE);
655 /* Setup irq domain on top of the generic chip. */
656 mvchip->domain = irq_domain_add_simple(np, mvchip->chip.ngpio,
658 &irq_domain_simple_ops,
660 if (!mvchip->domain) {
661 dev_err(&pdev->dev, "couldn't allocate irq domain %s (DT).\n",
663 irq_remove_generic_chip(gc, IRQ_MSK(ngpios), IRQ_NOREQUEST,
664 IRQ_LEVEL | IRQ_NOPROBE);
672 static struct platform_driver mvebu_gpio_driver = {
674 .name = "mvebu-gpio",
675 .owner = THIS_MODULE,
676 .of_match_table = mvebu_gpio_of_match,
678 .probe = mvebu_gpio_probe,
681 static int __init mvebu_gpio_init(void)
683 return platform_driver_register(&mvebu_gpio_driver);
685 postcore_initcall(mvebu_gpio_init);