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/err.h>
37 #include <linux/module.h>
38 #include <linux/gpio.h>
39 #include <linux/irq.h>
40 #include <linux/slab.h>
41 #include <linux/irqdomain.h>
43 #include <linux/of_irq.h>
44 #include <linux/of_device.h>
45 #include <linux/clk.h>
46 #include <linux/pinctrl/consumer.h>
47 #include <linux/irqchip/chained_irq.h>
50 * GPIO unit register offsets.
52 #define GPIO_OUT_OFF 0x0000
53 #define GPIO_IO_CONF_OFF 0x0004
54 #define GPIO_BLINK_EN_OFF 0x0008
55 #define GPIO_IN_POL_OFF 0x000c
56 #define GPIO_DATA_IN_OFF 0x0010
57 #define GPIO_EDGE_CAUSE_OFF 0x0014
58 #define GPIO_EDGE_MASK_OFF 0x0018
59 #define GPIO_LEVEL_MASK_OFF 0x001c
61 /* The MV78200 has per-CPU registers for edge mask and level mask */
62 #define GPIO_EDGE_MASK_MV78200_OFF(cpu) ((cpu) ? 0x30 : 0x18)
63 #define GPIO_LEVEL_MASK_MV78200_OFF(cpu) ((cpu) ? 0x34 : 0x1C)
65 /* The Armada XP has per-CPU registers for interrupt cause, interrupt
66 * mask and interrupt level mask. Those are relative to the
68 #define GPIO_EDGE_CAUSE_ARMADAXP_OFF(cpu) ((cpu) * 0x4)
69 #define GPIO_EDGE_MASK_ARMADAXP_OFF(cpu) (0x10 + (cpu) * 0x4)
70 #define GPIO_LEVEL_MASK_ARMADAXP_OFF(cpu) (0x20 + (cpu) * 0x4)
72 #define MVEBU_GPIO_SOC_VARIANT_ORION 0x1
73 #define MVEBU_GPIO_SOC_VARIANT_MV78200 0x2
74 #define MVEBU_GPIO_SOC_VARIANT_ARMADAXP 0x3
76 #define MVEBU_MAX_GPIO_PER_BANK 32
78 struct mvebu_gpio_chip
{
79 struct gpio_chip chip
;
81 void __iomem
*membase
;
82 void __iomem
*percpu_membase
;
84 struct irq_domain
*domain
;
87 /* Used to preserve GPIO registers across suspend/resume */
92 u32 edge_mask_regs
[4];
93 u32 level_mask_regs
[4];
97 * Functions returning addresses of individual registers for a given
100 static inline void __iomem
*mvebu_gpioreg_out(struct mvebu_gpio_chip
*mvchip
)
102 return mvchip
->membase
+ GPIO_OUT_OFF
;
105 static inline void __iomem
*mvebu_gpioreg_blink(struct mvebu_gpio_chip
*mvchip
)
107 return mvchip
->membase
+ GPIO_BLINK_EN_OFF
;
110 static inline void __iomem
*
111 mvebu_gpioreg_io_conf(struct mvebu_gpio_chip
*mvchip
)
113 return mvchip
->membase
+ GPIO_IO_CONF_OFF
;
116 static inline void __iomem
*mvebu_gpioreg_in_pol(struct mvebu_gpio_chip
*mvchip
)
118 return mvchip
->membase
+ GPIO_IN_POL_OFF
;
121 static inline void __iomem
*
122 mvebu_gpioreg_data_in(struct mvebu_gpio_chip
*mvchip
)
124 return mvchip
->membase
+ GPIO_DATA_IN_OFF
;
127 static inline void __iomem
*
128 mvebu_gpioreg_edge_cause(struct mvebu_gpio_chip
*mvchip
)
132 switch (mvchip
->soc_variant
) {
133 case MVEBU_GPIO_SOC_VARIANT_ORION
:
134 case MVEBU_GPIO_SOC_VARIANT_MV78200
:
135 return mvchip
->membase
+ GPIO_EDGE_CAUSE_OFF
;
136 case MVEBU_GPIO_SOC_VARIANT_ARMADAXP
:
137 cpu
= smp_processor_id();
138 return mvchip
->percpu_membase
+
139 GPIO_EDGE_CAUSE_ARMADAXP_OFF(cpu
);
145 static inline void __iomem
*
146 mvebu_gpioreg_edge_mask(struct mvebu_gpio_chip
*mvchip
)
150 switch (mvchip
->soc_variant
) {
151 case MVEBU_GPIO_SOC_VARIANT_ORION
:
152 return mvchip
->membase
+ GPIO_EDGE_MASK_OFF
;
153 case MVEBU_GPIO_SOC_VARIANT_MV78200
:
154 cpu
= smp_processor_id();
155 return mvchip
->membase
+ GPIO_EDGE_MASK_MV78200_OFF(cpu
);
156 case MVEBU_GPIO_SOC_VARIANT_ARMADAXP
:
157 cpu
= smp_processor_id();
158 return mvchip
->percpu_membase
+
159 GPIO_EDGE_MASK_ARMADAXP_OFF(cpu
);
165 static void __iomem
*mvebu_gpioreg_level_mask(struct mvebu_gpio_chip
*mvchip
)
169 switch (mvchip
->soc_variant
) {
170 case MVEBU_GPIO_SOC_VARIANT_ORION
:
171 return mvchip
->membase
+ GPIO_LEVEL_MASK_OFF
;
172 case MVEBU_GPIO_SOC_VARIANT_MV78200
:
173 cpu
= smp_processor_id();
174 return mvchip
->membase
+ GPIO_LEVEL_MASK_MV78200_OFF(cpu
);
175 case MVEBU_GPIO_SOC_VARIANT_ARMADAXP
:
176 cpu
= smp_processor_id();
177 return mvchip
->percpu_membase
+
178 GPIO_LEVEL_MASK_ARMADAXP_OFF(cpu
);
185 * Functions implementing the gpio_chip methods
188 static void mvebu_gpio_set(struct gpio_chip
*chip
, unsigned pin
, int value
)
190 struct mvebu_gpio_chip
*mvchip
=
191 container_of(chip
, struct mvebu_gpio_chip
, chip
);
195 spin_lock_irqsave(&mvchip
->lock
, flags
);
196 u
= readl_relaxed(mvebu_gpioreg_out(mvchip
));
201 writel_relaxed(u
, mvebu_gpioreg_out(mvchip
));
202 spin_unlock_irqrestore(&mvchip
->lock
, flags
);
205 static int mvebu_gpio_get(struct gpio_chip
*chip
, unsigned pin
)
207 struct mvebu_gpio_chip
*mvchip
=
208 container_of(chip
, struct mvebu_gpio_chip
, chip
);
211 if (readl_relaxed(mvebu_gpioreg_io_conf(mvchip
)) & (1 << pin
)) {
212 u
= readl_relaxed(mvebu_gpioreg_data_in(mvchip
)) ^
213 readl_relaxed(mvebu_gpioreg_in_pol(mvchip
));
215 u
= readl_relaxed(mvebu_gpioreg_out(mvchip
));
218 return (u
>> pin
) & 1;
221 static void mvebu_gpio_blink(struct gpio_chip
*chip
, unsigned pin
, int value
)
223 struct mvebu_gpio_chip
*mvchip
=
224 container_of(chip
, struct mvebu_gpio_chip
, chip
);
228 spin_lock_irqsave(&mvchip
->lock
, flags
);
229 u
= readl_relaxed(mvebu_gpioreg_blink(mvchip
));
234 writel_relaxed(u
, mvebu_gpioreg_blink(mvchip
));
235 spin_unlock_irqrestore(&mvchip
->lock
, flags
);
238 static int mvebu_gpio_direction_input(struct gpio_chip
*chip
, unsigned pin
)
240 struct mvebu_gpio_chip
*mvchip
=
241 container_of(chip
, struct mvebu_gpio_chip
, chip
);
246 /* Check with the pinctrl driver whether this pin is usable as
248 ret
= pinctrl_gpio_direction_input(chip
->base
+ pin
);
252 spin_lock_irqsave(&mvchip
->lock
, flags
);
253 u
= readl_relaxed(mvebu_gpioreg_io_conf(mvchip
));
255 writel_relaxed(u
, mvebu_gpioreg_io_conf(mvchip
));
256 spin_unlock_irqrestore(&mvchip
->lock
, flags
);
261 static int mvebu_gpio_direction_output(struct gpio_chip
*chip
, unsigned pin
,
264 struct mvebu_gpio_chip
*mvchip
=
265 container_of(chip
, struct mvebu_gpio_chip
, chip
);
270 /* Check with the pinctrl driver whether this pin is usable as
272 ret
= pinctrl_gpio_direction_output(chip
->base
+ pin
);
276 mvebu_gpio_blink(chip
, pin
, 0);
277 mvebu_gpio_set(chip
, pin
, value
);
279 spin_lock_irqsave(&mvchip
->lock
, flags
);
280 u
= readl_relaxed(mvebu_gpioreg_io_conf(mvchip
));
282 writel_relaxed(u
, mvebu_gpioreg_io_conf(mvchip
));
283 spin_unlock_irqrestore(&mvchip
->lock
, flags
);
288 static int mvebu_gpio_to_irq(struct gpio_chip
*chip
, unsigned pin
)
290 struct mvebu_gpio_chip
*mvchip
=
291 container_of(chip
, struct mvebu_gpio_chip
, chip
);
292 return irq_create_mapping(mvchip
->domain
, pin
);
296 * Functions implementing the irq_chip methods
298 static void mvebu_gpio_irq_ack(struct irq_data
*d
)
300 struct irq_chip_generic
*gc
= irq_data_get_irq_chip_data(d
);
301 struct mvebu_gpio_chip
*mvchip
= gc
->private;
302 u32 mask
= ~(1 << (d
->irq
- gc
->irq_base
));
305 writel_relaxed(mask
, mvebu_gpioreg_edge_cause(mvchip
));
309 static void mvebu_gpio_edge_irq_mask(struct irq_data
*d
)
311 struct irq_chip_generic
*gc
= irq_data_get_irq_chip_data(d
);
312 struct mvebu_gpio_chip
*mvchip
= gc
->private;
313 struct irq_chip_type
*ct
= irq_data_get_chip_type(d
);
314 u32 mask
= 1 << (d
->irq
- gc
->irq_base
);
317 ct
->mask_cache_priv
&= ~mask
;
319 writel_relaxed(ct
->mask_cache_priv
, mvebu_gpioreg_edge_mask(mvchip
));
323 static void mvebu_gpio_edge_irq_unmask(struct irq_data
*d
)
325 struct irq_chip_generic
*gc
= irq_data_get_irq_chip_data(d
);
326 struct mvebu_gpio_chip
*mvchip
= gc
->private;
327 struct irq_chip_type
*ct
= irq_data_get_chip_type(d
);
329 u32 mask
= 1 << (d
->irq
- gc
->irq_base
);
332 ct
->mask_cache_priv
|= mask
;
333 writel_relaxed(ct
->mask_cache_priv
, mvebu_gpioreg_edge_mask(mvchip
));
337 static void mvebu_gpio_level_irq_mask(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 struct irq_chip_type
*ct
= irq_data_get_chip_type(d
);
343 u32 mask
= 1 << (d
->irq
- gc
->irq_base
);
346 ct
->mask_cache_priv
&= ~mask
;
347 writel_relaxed(ct
->mask_cache_priv
, mvebu_gpioreg_level_mask(mvchip
));
351 static void mvebu_gpio_level_irq_unmask(struct irq_data
*d
)
353 struct irq_chip_generic
*gc
= irq_data_get_irq_chip_data(d
);
354 struct mvebu_gpio_chip
*mvchip
= gc
->private;
355 struct irq_chip_type
*ct
= irq_data_get_chip_type(d
);
357 u32 mask
= 1 << (d
->irq
- gc
->irq_base
);
360 ct
->mask_cache_priv
|= mask
;
361 writel_relaxed(ct
->mask_cache_priv
, mvebu_gpioreg_level_mask(mvchip
));
365 /*****************************************************************************
368 * GPIO_IN_POL register controls whether GPIO_DATA_IN will hold the same
369 * value of the line or the opposite value.
371 * Level IRQ handlers: DATA_IN is used directly as cause register.
372 * Interrupt are masked by LEVEL_MASK registers.
373 * Edge IRQ handlers: Change in DATA_IN are latched in EDGE_CAUSE.
374 * Interrupt are masked by EDGE_MASK registers.
375 * Both-edge handlers: Similar to regular Edge handlers, but also swaps
376 * the polarity to catch the next line transaction.
377 * This is a race condition that might not perfectly
378 * work on some use cases.
380 * Every eight GPIO lines are grouped (OR'ed) before going up to main
384 * data-in /--------| |-----| |----\
385 * -----| |----- ---- to main cause reg
386 * X \----------------| |----/
387 * polarity LEVEL mask
389 ****************************************************************************/
391 static int mvebu_gpio_irq_set_type(struct irq_data
*d
, unsigned int type
)
393 struct irq_chip_generic
*gc
= irq_data_get_irq_chip_data(d
);
394 struct irq_chip_type
*ct
= irq_data_get_chip_type(d
);
395 struct mvebu_gpio_chip
*mvchip
= gc
->private;
401 u
= readl_relaxed(mvebu_gpioreg_io_conf(mvchip
)) & (1 << pin
);
405 type
&= IRQ_TYPE_SENSE_MASK
;
406 if (type
== IRQ_TYPE_NONE
)
409 /* Check if we need to change chip and handler */
410 if (!(ct
->type
& type
))
411 if (irq_setup_alt_chip(d
, type
))
415 * Configure interrupt polarity.
418 case IRQ_TYPE_EDGE_RISING
:
419 case IRQ_TYPE_LEVEL_HIGH
:
420 u
= readl_relaxed(mvebu_gpioreg_in_pol(mvchip
));
422 writel_relaxed(u
, mvebu_gpioreg_in_pol(mvchip
));
424 case IRQ_TYPE_EDGE_FALLING
:
425 case IRQ_TYPE_LEVEL_LOW
:
426 u
= readl_relaxed(mvebu_gpioreg_in_pol(mvchip
));
428 writel_relaxed(u
, mvebu_gpioreg_in_pol(mvchip
));
430 case IRQ_TYPE_EDGE_BOTH
: {
433 v
= readl_relaxed(mvebu_gpioreg_in_pol(mvchip
)) ^
434 readl_relaxed(mvebu_gpioreg_data_in(mvchip
));
437 * set initial polarity based on current input level
439 u
= readl_relaxed(mvebu_gpioreg_in_pol(mvchip
));
441 u
|= 1 << pin
; /* falling */
443 u
&= ~(1 << pin
); /* rising */
444 writel_relaxed(u
, mvebu_gpioreg_in_pol(mvchip
));
451 static void mvebu_gpio_irq_handler(struct irq_desc
*desc
)
453 struct mvebu_gpio_chip
*mvchip
= irq_desc_get_handler_data(desc
);
454 struct irq_chip
*chip
= irq_desc_get_chip(desc
);
461 chained_irq_enter(chip
, desc
);
463 cause
= readl_relaxed(mvebu_gpioreg_data_in(mvchip
)) &
464 readl_relaxed(mvebu_gpioreg_level_mask(mvchip
));
465 cause
|= readl_relaxed(mvebu_gpioreg_edge_cause(mvchip
)) &
466 readl_relaxed(mvebu_gpioreg_edge_mask(mvchip
));
468 for (i
= 0; i
< mvchip
->chip
.ngpio
; i
++) {
471 irq
= mvchip
->irqbase
+ i
;
473 if (!(cause
& (1 << i
)))
476 type
= irq_get_trigger_type(irq
);
477 if ((type
& IRQ_TYPE_SENSE_MASK
) == IRQ_TYPE_EDGE_BOTH
) {
478 /* Swap polarity (race with GPIO line) */
481 polarity
= readl_relaxed(mvebu_gpioreg_in_pol(mvchip
));
483 writel_relaxed(polarity
, mvebu_gpioreg_in_pol(mvchip
));
486 generic_handle_irq(irq
);
489 chained_irq_exit(chip
, desc
);
492 #ifdef CONFIG_DEBUG_FS
493 #include <linux/seq_file.h>
495 static void mvebu_gpio_dbg_show(struct seq_file
*s
, struct gpio_chip
*chip
)
497 struct mvebu_gpio_chip
*mvchip
=
498 container_of(chip
, struct mvebu_gpio_chip
, chip
);
499 u32 out
, io_conf
, blink
, in_pol
, data_in
, cause
, edg_msk
, lvl_msk
;
502 out
= readl_relaxed(mvebu_gpioreg_out(mvchip
));
503 io_conf
= readl_relaxed(mvebu_gpioreg_io_conf(mvchip
));
504 blink
= readl_relaxed(mvebu_gpioreg_blink(mvchip
));
505 in_pol
= readl_relaxed(mvebu_gpioreg_in_pol(mvchip
));
506 data_in
= readl_relaxed(mvebu_gpioreg_data_in(mvchip
));
507 cause
= readl_relaxed(mvebu_gpioreg_edge_cause(mvchip
));
508 edg_msk
= readl_relaxed(mvebu_gpioreg_edge_mask(mvchip
));
509 lvl_msk
= readl_relaxed(mvebu_gpioreg_level_mask(mvchip
));
511 for (i
= 0; i
< chip
->ngpio
; i
++) {
516 label
= gpiochip_is_requested(chip
, i
);
521 is_out
= !(io_conf
& msk
);
523 seq_printf(s
, " gpio-%-3d (%-20.20s)", chip
->base
+ i
, label
);
526 seq_printf(s
, " out %s %s\n",
527 out
& msk
? "hi" : "lo",
528 blink
& msk
? "(blink )" : "");
532 seq_printf(s
, " in %s (act %s) - IRQ",
533 (data_in
^ in_pol
) & msk
? "hi" : "lo",
534 in_pol
& msk
? "lo" : "hi");
535 if (!((edg_msk
| lvl_msk
) & msk
)) {
536 seq_puts(s
, " disabled\n");
540 seq_puts(s
, " edge ");
542 seq_puts(s
, " level");
543 seq_printf(s
, " (%s)\n", cause
& msk
? "pending" : "clear ");
547 #define mvebu_gpio_dbg_show NULL
550 static const struct of_device_id mvebu_gpio_of_match
[] = {
552 .compatible
= "marvell,orion-gpio",
553 .data
= (void *) MVEBU_GPIO_SOC_VARIANT_ORION
,
556 .compatible
= "marvell,mv78200-gpio",
557 .data
= (void *) MVEBU_GPIO_SOC_VARIANT_MV78200
,
560 .compatible
= "marvell,armadaxp-gpio",
561 .data
= (void *) MVEBU_GPIO_SOC_VARIANT_ARMADAXP
,
567 MODULE_DEVICE_TABLE(of
, mvebu_gpio_of_match
);
569 static int mvebu_gpio_suspend(struct platform_device
*pdev
, pm_message_t state
)
571 struct mvebu_gpio_chip
*mvchip
= platform_get_drvdata(pdev
);
574 mvchip
->out_reg
= readl(mvebu_gpioreg_out(mvchip
));
575 mvchip
->io_conf_reg
= readl(mvebu_gpioreg_io_conf(mvchip
));
576 mvchip
->blink_en_reg
= readl(mvebu_gpioreg_blink(mvchip
));
577 mvchip
->in_pol_reg
= readl(mvebu_gpioreg_in_pol(mvchip
));
579 switch (mvchip
->soc_variant
) {
580 case MVEBU_GPIO_SOC_VARIANT_ORION
:
581 mvchip
->edge_mask_regs
[0] =
582 readl(mvchip
->membase
+ GPIO_EDGE_MASK_OFF
);
583 mvchip
->level_mask_regs
[0] =
584 readl(mvchip
->membase
+ GPIO_LEVEL_MASK_OFF
);
586 case MVEBU_GPIO_SOC_VARIANT_MV78200
:
587 for (i
= 0; i
< 2; i
++) {
588 mvchip
->edge_mask_regs
[i
] =
589 readl(mvchip
->membase
+
590 GPIO_EDGE_MASK_MV78200_OFF(i
));
591 mvchip
->level_mask_regs
[i
] =
592 readl(mvchip
->membase
+
593 GPIO_LEVEL_MASK_MV78200_OFF(i
));
596 case MVEBU_GPIO_SOC_VARIANT_ARMADAXP
:
597 for (i
= 0; i
< 4; i
++) {
598 mvchip
->edge_mask_regs
[i
] =
599 readl(mvchip
->membase
+
600 GPIO_EDGE_MASK_ARMADAXP_OFF(i
));
601 mvchip
->level_mask_regs
[i
] =
602 readl(mvchip
->membase
+
603 GPIO_LEVEL_MASK_ARMADAXP_OFF(i
));
613 static int mvebu_gpio_resume(struct platform_device
*pdev
)
615 struct mvebu_gpio_chip
*mvchip
= platform_get_drvdata(pdev
);
618 writel(mvchip
->out_reg
, mvebu_gpioreg_out(mvchip
));
619 writel(mvchip
->io_conf_reg
, mvebu_gpioreg_io_conf(mvchip
));
620 writel(mvchip
->blink_en_reg
, mvebu_gpioreg_blink(mvchip
));
621 writel(mvchip
->in_pol_reg
, mvebu_gpioreg_in_pol(mvchip
));
623 switch (mvchip
->soc_variant
) {
624 case MVEBU_GPIO_SOC_VARIANT_ORION
:
625 writel(mvchip
->edge_mask_regs
[0],
626 mvchip
->membase
+ GPIO_EDGE_MASK_OFF
);
627 writel(mvchip
->level_mask_regs
[0],
628 mvchip
->membase
+ GPIO_LEVEL_MASK_OFF
);
630 case MVEBU_GPIO_SOC_VARIANT_MV78200
:
631 for (i
= 0; i
< 2; i
++) {
632 writel(mvchip
->edge_mask_regs
[i
],
633 mvchip
->membase
+ GPIO_EDGE_MASK_MV78200_OFF(i
));
634 writel(mvchip
->level_mask_regs
[i
],
636 GPIO_LEVEL_MASK_MV78200_OFF(i
));
639 case MVEBU_GPIO_SOC_VARIANT_ARMADAXP
:
640 for (i
= 0; i
< 4; i
++) {
641 writel(mvchip
->edge_mask_regs
[i
],
643 GPIO_EDGE_MASK_ARMADAXP_OFF(i
));
644 writel(mvchip
->level_mask_regs
[i
],
646 GPIO_LEVEL_MASK_ARMADAXP_OFF(i
));
656 static int mvebu_gpio_probe(struct platform_device
*pdev
)
658 struct mvebu_gpio_chip
*mvchip
;
659 const struct of_device_id
*match
;
660 struct device_node
*np
= pdev
->dev
.of_node
;
661 struct resource
*res
;
662 struct irq_chip_generic
*gc
;
663 struct irq_chip_type
*ct
;
670 match
= of_match_device(mvebu_gpio_of_match
, &pdev
->dev
);
672 soc_variant
= (int) match
->data
;
674 soc_variant
= MVEBU_GPIO_SOC_VARIANT_ORION
;
676 mvchip
= devm_kzalloc(&pdev
->dev
, sizeof(struct mvebu_gpio_chip
),
681 platform_set_drvdata(pdev
, mvchip
);
683 if (of_property_read_u32(pdev
->dev
.of_node
, "ngpios", &ngpios
)) {
684 dev_err(&pdev
->dev
, "Missing ngpios OF property\n");
688 id
= of_alias_get_id(pdev
->dev
.of_node
, "gpio");
690 dev_err(&pdev
->dev
, "Couldn't get OF id\n");
694 clk
= devm_clk_get(&pdev
->dev
, NULL
);
695 /* Not all SoCs require a clock.*/
697 clk_prepare_enable(clk
);
699 mvchip
->soc_variant
= soc_variant
;
700 mvchip
->chip
.label
= dev_name(&pdev
->dev
);
701 mvchip
->chip
.dev
= &pdev
->dev
;
702 mvchip
->chip
.request
= gpiochip_generic_request
;
703 mvchip
->chip
.free
= gpiochip_generic_free
;
704 mvchip
->chip
.direction_input
= mvebu_gpio_direction_input
;
705 mvchip
->chip
.get
= mvebu_gpio_get
;
706 mvchip
->chip
.direction_output
= mvebu_gpio_direction_output
;
707 mvchip
->chip
.set
= mvebu_gpio_set
;
708 mvchip
->chip
.to_irq
= mvebu_gpio_to_irq
;
709 mvchip
->chip
.base
= id
* MVEBU_MAX_GPIO_PER_BANK
;
710 mvchip
->chip
.ngpio
= ngpios
;
711 mvchip
->chip
.can_sleep
= false;
712 mvchip
->chip
.of_node
= np
;
713 mvchip
->chip
.dbg_show
= mvebu_gpio_dbg_show
;
715 spin_lock_init(&mvchip
->lock
);
716 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
717 mvchip
->membase
= devm_ioremap_resource(&pdev
->dev
, res
);
718 if (IS_ERR(mvchip
->membase
))
719 return PTR_ERR(mvchip
->membase
);
721 /* The Armada XP has a second range of registers for the
722 * per-CPU registers */
723 if (soc_variant
== MVEBU_GPIO_SOC_VARIANT_ARMADAXP
) {
724 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
725 mvchip
->percpu_membase
= devm_ioremap_resource(&pdev
->dev
,
727 if (IS_ERR(mvchip
->percpu_membase
))
728 return PTR_ERR(mvchip
->percpu_membase
);
732 * Mask and clear GPIO interrupts.
734 switch (soc_variant
) {
735 case MVEBU_GPIO_SOC_VARIANT_ORION
:
736 writel_relaxed(0, mvchip
->membase
+ GPIO_EDGE_CAUSE_OFF
);
737 writel_relaxed(0, mvchip
->membase
+ GPIO_EDGE_MASK_OFF
);
738 writel_relaxed(0, mvchip
->membase
+ GPIO_LEVEL_MASK_OFF
);
740 case MVEBU_GPIO_SOC_VARIANT_MV78200
:
741 writel_relaxed(0, mvchip
->membase
+ GPIO_EDGE_CAUSE_OFF
);
742 for (cpu
= 0; cpu
< 2; cpu
++) {
743 writel_relaxed(0, mvchip
->membase
+
744 GPIO_EDGE_MASK_MV78200_OFF(cpu
));
745 writel_relaxed(0, mvchip
->membase
+
746 GPIO_LEVEL_MASK_MV78200_OFF(cpu
));
749 case MVEBU_GPIO_SOC_VARIANT_ARMADAXP
:
750 writel_relaxed(0, mvchip
->membase
+ GPIO_EDGE_CAUSE_OFF
);
751 writel_relaxed(0, mvchip
->membase
+ GPIO_EDGE_MASK_OFF
);
752 writel_relaxed(0, mvchip
->membase
+ GPIO_LEVEL_MASK_OFF
);
753 for (cpu
= 0; cpu
< 4; cpu
++) {
754 writel_relaxed(0, mvchip
->percpu_membase
+
755 GPIO_EDGE_CAUSE_ARMADAXP_OFF(cpu
));
756 writel_relaxed(0, mvchip
->percpu_membase
+
757 GPIO_EDGE_MASK_ARMADAXP_OFF(cpu
));
758 writel_relaxed(0, mvchip
->percpu_membase
+
759 GPIO_LEVEL_MASK_ARMADAXP_OFF(cpu
));
766 gpiochip_add(&mvchip
->chip
);
768 /* Some gpio controllers do not provide irq support */
769 if (!of_irq_count(np
))
772 /* Setup the interrupt handlers. Each chip can have up to 4
773 * interrupt handlers, with each handler dealing with 8 GPIO
775 for (i
= 0; i
< 4; i
++) {
776 int irq
= platform_get_irq(pdev
, i
);
780 irq_set_chained_handler_and_data(irq
, mvebu_gpio_irq_handler
,
784 mvchip
->irqbase
= irq_alloc_descs(-1, 0, ngpios
, -1);
785 if (mvchip
->irqbase
< 0) {
786 dev_err(&pdev
->dev
, "no irqs\n");
787 err
= mvchip
->irqbase
;
788 goto err_gpiochip_add
;
791 gc
= irq_alloc_generic_chip("mvebu_gpio_irq", 2, mvchip
->irqbase
,
792 mvchip
->membase
, handle_level_irq
);
794 dev_err(&pdev
->dev
, "Cannot allocate generic irq_chip\n");
796 goto err_gpiochip_add
;
799 gc
->private = mvchip
;
800 ct
= &gc
->chip_types
[0];
801 ct
->type
= IRQ_TYPE_LEVEL_HIGH
| IRQ_TYPE_LEVEL_LOW
;
802 ct
->chip
.irq_mask
= mvebu_gpio_level_irq_mask
;
803 ct
->chip
.irq_unmask
= mvebu_gpio_level_irq_unmask
;
804 ct
->chip
.irq_set_type
= mvebu_gpio_irq_set_type
;
805 ct
->chip
.name
= mvchip
->chip
.label
;
807 ct
= &gc
->chip_types
[1];
808 ct
->type
= IRQ_TYPE_EDGE_RISING
| IRQ_TYPE_EDGE_FALLING
;
809 ct
->chip
.irq_ack
= mvebu_gpio_irq_ack
;
810 ct
->chip
.irq_mask
= mvebu_gpio_edge_irq_mask
;
811 ct
->chip
.irq_unmask
= mvebu_gpio_edge_irq_unmask
;
812 ct
->chip
.irq_set_type
= mvebu_gpio_irq_set_type
;
813 ct
->handler
= handle_edge_irq
;
814 ct
->chip
.name
= mvchip
->chip
.label
;
816 irq_setup_generic_chip(gc
, IRQ_MSK(ngpios
), 0,
817 IRQ_NOREQUEST
, IRQ_LEVEL
| IRQ_NOPROBE
);
819 /* Setup irq domain on top of the generic chip. */
820 mvchip
->domain
= irq_domain_add_simple(np
, mvchip
->chip
.ngpio
,
822 &irq_domain_simple_ops
,
824 if (!mvchip
->domain
) {
825 dev_err(&pdev
->dev
, "couldn't allocate irq domain %s (DT).\n",
828 goto err_generic_chip
;
834 irq_remove_generic_chip(gc
, IRQ_MSK(ngpios
), IRQ_NOREQUEST
,
835 IRQ_LEVEL
| IRQ_NOPROBE
);
839 gpiochip_remove(&mvchip
->chip
);
844 static struct platform_driver mvebu_gpio_driver
= {
846 .name
= "mvebu-gpio",
847 .of_match_table
= mvebu_gpio_of_match
,
849 .probe
= mvebu_gpio_probe
,
850 .suspend
= mvebu_gpio_suspend
,
851 .resume
= mvebu_gpio_resume
,
853 module_platform_driver(mvebu_gpio_driver
);