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/init.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/pwm.h>
46 #include <linux/clk.h>
47 #include <linux/pinctrl/consumer.h>
48 #include <linux/irqchip/chained_irq.h>
49 #include <linux/platform_device.h>
50 #include <linux/bitops.h>
55 * GPIO unit register offsets.
57 #define GPIO_OUT_OFF 0x0000
58 #define GPIO_IO_CONF_OFF 0x0004
59 #define GPIO_BLINK_EN_OFF 0x0008
60 #define GPIO_IN_POL_OFF 0x000c
61 #define GPIO_DATA_IN_OFF 0x0010
62 #define GPIO_EDGE_CAUSE_OFF 0x0014
63 #define GPIO_EDGE_MASK_OFF 0x0018
64 #define GPIO_LEVEL_MASK_OFF 0x001c
65 #define GPIO_BLINK_CNT_SELECT_OFF 0x0020
68 * PWM register offsets.
70 #define PWM_BLINK_ON_DURATION_OFF 0x0
71 #define PWM_BLINK_OFF_DURATION_OFF 0x4
74 /* The MV78200 has per-CPU registers for edge mask and level mask */
75 #define GPIO_EDGE_MASK_MV78200_OFF(cpu) ((cpu) ? 0x30 : 0x18)
76 #define GPIO_LEVEL_MASK_MV78200_OFF(cpu) ((cpu) ? 0x34 : 0x1C)
79 * The Armada XP has per-CPU registers for interrupt cause, interrupt
80 * mask and interrupt level mask. Those are relative to the
83 #define GPIO_EDGE_CAUSE_ARMADAXP_OFF(cpu) ((cpu) * 0x4)
84 #define GPIO_EDGE_MASK_ARMADAXP_OFF(cpu) (0x10 + (cpu) * 0x4)
85 #define GPIO_LEVEL_MASK_ARMADAXP_OFF(cpu) (0x20 + (cpu) * 0x4)
87 #define MVEBU_GPIO_SOC_VARIANT_ORION 0x1
88 #define MVEBU_GPIO_SOC_VARIANT_MV78200 0x2
89 #define MVEBU_GPIO_SOC_VARIANT_ARMADAXP 0x3
91 #define MVEBU_MAX_GPIO_PER_BANK 32
94 void __iomem
*membase
;
95 unsigned long clk_rate
;
96 struct gpio_desc
*gpiod
;
99 struct mvebu_gpio_chip
*mvchip
;
101 /* Used to preserve GPIO/PWM registers across suspend/resume */
103 u32 blink_on_duration
;
104 u32 blink_off_duration
;
107 struct mvebu_gpio_chip
{
108 struct gpio_chip chip
;
110 void __iomem
*membase
;
111 void __iomem
*percpu_membase
;
113 struct irq_domain
*domain
;
116 /* Used for PWM support */
118 struct mvebu_pwm
*mvpwm
;
120 /* Used to preserve GPIO registers across suspend/resume */
125 u32 edge_mask_regs
[4];
126 u32 level_mask_regs
[4];
130 * Functions returning addresses of individual registers for a given
133 static void __iomem
*mvebu_gpioreg_out(struct mvebu_gpio_chip
*mvchip
)
135 return mvchip
->membase
+ GPIO_OUT_OFF
;
138 static void __iomem
*mvebu_gpioreg_blink(struct mvebu_gpio_chip
*mvchip
)
140 return mvchip
->membase
+ GPIO_BLINK_EN_OFF
;
143 static void __iomem
*mvebu_gpioreg_blink_counter_select(struct mvebu_gpio_chip
146 return mvchip
->membase
+ GPIO_BLINK_CNT_SELECT_OFF
;
149 static void __iomem
*mvebu_gpioreg_io_conf(struct mvebu_gpio_chip
*mvchip
)
151 return mvchip
->membase
+ GPIO_IO_CONF_OFF
;
154 static void __iomem
*mvebu_gpioreg_in_pol(struct mvebu_gpio_chip
*mvchip
)
156 return mvchip
->membase
+ GPIO_IN_POL_OFF
;
159 static void __iomem
*mvebu_gpioreg_data_in(struct mvebu_gpio_chip
*mvchip
)
161 return mvchip
->membase
+ GPIO_DATA_IN_OFF
;
164 static void __iomem
*mvebu_gpioreg_edge_cause(struct mvebu_gpio_chip
*mvchip
)
168 switch (mvchip
->soc_variant
) {
169 case MVEBU_GPIO_SOC_VARIANT_ORION
:
170 case MVEBU_GPIO_SOC_VARIANT_MV78200
:
171 return mvchip
->membase
+ GPIO_EDGE_CAUSE_OFF
;
172 case MVEBU_GPIO_SOC_VARIANT_ARMADAXP
:
173 cpu
= smp_processor_id();
174 return mvchip
->percpu_membase
+
175 GPIO_EDGE_CAUSE_ARMADAXP_OFF(cpu
);
181 static void __iomem
*mvebu_gpioreg_edge_mask(struct mvebu_gpio_chip
*mvchip
)
185 switch (mvchip
->soc_variant
) {
186 case MVEBU_GPIO_SOC_VARIANT_ORION
:
187 return mvchip
->membase
+ GPIO_EDGE_MASK_OFF
;
188 case MVEBU_GPIO_SOC_VARIANT_MV78200
:
189 cpu
= smp_processor_id();
190 return mvchip
->membase
+ GPIO_EDGE_MASK_MV78200_OFF(cpu
);
191 case MVEBU_GPIO_SOC_VARIANT_ARMADAXP
:
192 cpu
= smp_processor_id();
193 return mvchip
->percpu_membase
+
194 GPIO_EDGE_MASK_ARMADAXP_OFF(cpu
);
200 static void __iomem
*mvebu_gpioreg_level_mask(struct mvebu_gpio_chip
*mvchip
)
204 switch (mvchip
->soc_variant
) {
205 case MVEBU_GPIO_SOC_VARIANT_ORION
:
206 return mvchip
->membase
+ GPIO_LEVEL_MASK_OFF
;
207 case MVEBU_GPIO_SOC_VARIANT_MV78200
:
208 cpu
= smp_processor_id();
209 return mvchip
->membase
+ GPIO_LEVEL_MASK_MV78200_OFF(cpu
);
210 case MVEBU_GPIO_SOC_VARIANT_ARMADAXP
:
211 cpu
= smp_processor_id();
212 return mvchip
->percpu_membase
+
213 GPIO_LEVEL_MASK_ARMADAXP_OFF(cpu
);
220 * Functions returning addresses of individual registers for a given
223 static void __iomem
*mvebu_pwmreg_blink_on_duration(struct mvebu_pwm
*mvpwm
)
225 return mvpwm
->membase
+ PWM_BLINK_ON_DURATION_OFF
;
228 static void __iomem
*mvebu_pwmreg_blink_off_duration(struct mvebu_pwm
*mvpwm
)
230 return mvpwm
->membase
+ PWM_BLINK_OFF_DURATION_OFF
;
234 * Functions implementing the gpio_chip methods
236 static void mvebu_gpio_set(struct gpio_chip
*chip
, unsigned int pin
, int value
)
238 struct mvebu_gpio_chip
*mvchip
= gpiochip_get_data(chip
);
242 spin_lock_irqsave(&mvchip
->lock
, flags
);
243 u
= readl_relaxed(mvebu_gpioreg_out(mvchip
));
248 writel_relaxed(u
, mvebu_gpioreg_out(mvchip
));
249 spin_unlock_irqrestore(&mvchip
->lock
, flags
);
252 static int mvebu_gpio_get(struct gpio_chip
*chip
, unsigned int pin
)
254 struct mvebu_gpio_chip
*mvchip
= gpiochip_get_data(chip
);
257 if (readl_relaxed(mvebu_gpioreg_io_conf(mvchip
)) & BIT(pin
)) {
258 u
= readl_relaxed(mvebu_gpioreg_data_in(mvchip
)) ^
259 readl_relaxed(mvebu_gpioreg_in_pol(mvchip
));
261 u
= readl_relaxed(mvebu_gpioreg_out(mvchip
));
264 return (u
>> pin
) & 1;
267 static void mvebu_gpio_blink(struct gpio_chip
*chip
, unsigned int pin
,
270 struct mvebu_gpio_chip
*mvchip
= gpiochip_get_data(chip
);
274 spin_lock_irqsave(&mvchip
->lock
, flags
);
275 u
= readl_relaxed(mvebu_gpioreg_blink(mvchip
));
280 writel_relaxed(u
, mvebu_gpioreg_blink(mvchip
));
281 spin_unlock_irqrestore(&mvchip
->lock
, flags
);
284 static int mvebu_gpio_direction_input(struct gpio_chip
*chip
, unsigned int pin
)
286 struct mvebu_gpio_chip
*mvchip
= gpiochip_get_data(chip
);
292 * Check with the pinctrl driver whether this pin is usable as
295 ret
= pinctrl_gpio_direction_input(chip
->base
+ pin
);
299 spin_lock_irqsave(&mvchip
->lock
, flags
);
300 u
= readl_relaxed(mvebu_gpioreg_io_conf(mvchip
));
302 writel_relaxed(u
, mvebu_gpioreg_io_conf(mvchip
));
303 spin_unlock_irqrestore(&mvchip
->lock
, flags
);
308 static int mvebu_gpio_direction_output(struct gpio_chip
*chip
, unsigned int pin
,
311 struct mvebu_gpio_chip
*mvchip
= gpiochip_get_data(chip
);
317 * Check with the pinctrl driver whether this pin is usable as
320 ret
= pinctrl_gpio_direction_output(chip
->base
+ pin
);
324 mvebu_gpio_blink(chip
, pin
, 0);
325 mvebu_gpio_set(chip
, pin
, value
);
327 spin_lock_irqsave(&mvchip
->lock
, flags
);
328 u
= readl_relaxed(mvebu_gpioreg_io_conf(mvchip
));
330 writel_relaxed(u
, mvebu_gpioreg_io_conf(mvchip
));
331 spin_unlock_irqrestore(&mvchip
->lock
, flags
);
336 static int mvebu_gpio_to_irq(struct gpio_chip
*chip
, unsigned int pin
)
338 struct mvebu_gpio_chip
*mvchip
= gpiochip_get_data(chip
);
340 return irq_create_mapping(mvchip
->domain
, pin
);
344 * Functions implementing the irq_chip methods
346 static void mvebu_gpio_irq_ack(struct irq_data
*d
)
348 struct irq_chip_generic
*gc
= irq_data_get_irq_chip_data(d
);
349 struct mvebu_gpio_chip
*mvchip
= gc
->private;
353 writel_relaxed(~mask
, mvebu_gpioreg_edge_cause(mvchip
));
357 static void mvebu_gpio_edge_irq_mask(struct irq_data
*d
)
359 struct irq_chip_generic
*gc
= irq_data_get_irq_chip_data(d
);
360 struct mvebu_gpio_chip
*mvchip
= gc
->private;
361 struct irq_chip_type
*ct
= irq_data_get_chip_type(d
);
365 ct
->mask_cache_priv
&= ~mask
;
367 writel_relaxed(ct
->mask_cache_priv
, mvebu_gpioreg_edge_mask(mvchip
));
371 static void mvebu_gpio_edge_irq_unmask(struct irq_data
*d
)
373 struct irq_chip_generic
*gc
= irq_data_get_irq_chip_data(d
);
374 struct mvebu_gpio_chip
*mvchip
= gc
->private;
375 struct irq_chip_type
*ct
= irq_data_get_chip_type(d
);
379 ct
->mask_cache_priv
|= mask
;
380 writel_relaxed(ct
->mask_cache_priv
, mvebu_gpioreg_edge_mask(mvchip
));
384 static void mvebu_gpio_level_irq_mask(struct irq_data
*d
)
386 struct irq_chip_generic
*gc
= irq_data_get_irq_chip_data(d
);
387 struct mvebu_gpio_chip
*mvchip
= gc
->private;
388 struct irq_chip_type
*ct
= irq_data_get_chip_type(d
);
392 ct
->mask_cache_priv
&= ~mask
;
393 writel_relaxed(ct
->mask_cache_priv
, mvebu_gpioreg_level_mask(mvchip
));
397 static void mvebu_gpio_level_irq_unmask(struct irq_data
*d
)
399 struct irq_chip_generic
*gc
= irq_data_get_irq_chip_data(d
);
400 struct mvebu_gpio_chip
*mvchip
= gc
->private;
401 struct irq_chip_type
*ct
= irq_data_get_chip_type(d
);
405 ct
->mask_cache_priv
|= mask
;
406 writel_relaxed(ct
->mask_cache_priv
, mvebu_gpioreg_level_mask(mvchip
));
410 /*****************************************************************************
413 * GPIO_IN_POL register controls whether GPIO_DATA_IN will hold the same
414 * value of the line or the opposite value.
416 * Level IRQ handlers: DATA_IN is used directly as cause register.
417 * Interrupt are masked by LEVEL_MASK registers.
418 * Edge IRQ handlers: Change in DATA_IN are latched in EDGE_CAUSE.
419 * Interrupt are masked by EDGE_MASK registers.
420 * Both-edge handlers: Similar to regular Edge handlers, but also swaps
421 * the polarity to catch the next line transaction.
422 * This is a race condition that might not perfectly
423 * work on some use cases.
425 * Every eight GPIO lines are grouped (OR'ed) before going up to main
429 * data-in /--------| |-----| |----\
430 * -----| |----- ---- to main cause reg
431 * X \----------------| |----/
432 * polarity LEVEL mask
434 ****************************************************************************/
436 static int mvebu_gpio_irq_set_type(struct irq_data
*d
, unsigned int type
)
438 struct irq_chip_generic
*gc
= irq_data_get_irq_chip_data(d
);
439 struct irq_chip_type
*ct
= irq_data_get_chip_type(d
);
440 struct mvebu_gpio_chip
*mvchip
= gc
->private;
446 u
= readl_relaxed(mvebu_gpioreg_io_conf(mvchip
)) & BIT(pin
);
450 type
&= IRQ_TYPE_SENSE_MASK
;
451 if (type
== IRQ_TYPE_NONE
)
454 /* Check if we need to change chip and handler */
455 if (!(ct
->type
& type
))
456 if (irq_setup_alt_chip(d
, type
))
460 * Configure interrupt polarity.
463 case IRQ_TYPE_EDGE_RISING
:
464 case IRQ_TYPE_LEVEL_HIGH
:
465 u
= readl_relaxed(mvebu_gpioreg_in_pol(mvchip
));
467 writel_relaxed(u
, mvebu_gpioreg_in_pol(mvchip
));
469 case IRQ_TYPE_EDGE_FALLING
:
470 case IRQ_TYPE_LEVEL_LOW
:
471 u
= readl_relaxed(mvebu_gpioreg_in_pol(mvchip
));
473 writel_relaxed(u
, mvebu_gpioreg_in_pol(mvchip
));
475 case IRQ_TYPE_EDGE_BOTH
: {
478 v
= readl_relaxed(mvebu_gpioreg_in_pol(mvchip
)) ^
479 readl_relaxed(mvebu_gpioreg_data_in(mvchip
));
482 * set initial polarity based on current input level
484 u
= readl_relaxed(mvebu_gpioreg_in_pol(mvchip
));
486 u
|= BIT(pin
); /* falling */
488 u
&= ~BIT(pin
); /* rising */
489 writel_relaxed(u
, mvebu_gpioreg_in_pol(mvchip
));
496 static void mvebu_gpio_irq_handler(struct irq_desc
*desc
)
498 struct mvebu_gpio_chip
*mvchip
= irq_desc_get_handler_data(desc
);
499 struct irq_chip
*chip
= irq_desc_get_chip(desc
);
506 chained_irq_enter(chip
, desc
);
508 cause
= readl_relaxed(mvebu_gpioreg_data_in(mvchip
)) &
509 readl_relaxed(mvebu_gpioreg_level_mask(mvchip
));
510 cause
|= readl_relaxed(mvebu_gpioreg_edge_cause(mvchip
)) &
511 readl_relaxed(mvebu_gpioreg_edge_mask(mvchip
));
513 for (i
= 0; i
< mvchip
->chip
.ngpio
; i
++) {
516 irq
= irq_find_mapping(mvchip
->domain
, i
);
518 if (!(cause
& BIT(i
)))
521 type
= irq_get_trigger_type(irq
);
522 if ((type
& IRQ_TYPE_SENSE_MASK
) == IRQ_TYPE_EDGE_BOTH
) {
523 /* Swap polarity (race with GPIO line) */
526 polarity
= readl_relaxed(mvebu_gpioreg_in_pol(mvchip
));
528 writel_relaxed(polarity
, mvebu_gpioreg_in_pol(mvchip
));
531 generic_handle_irq(irq
);
534 chained_irq_exit(chip
, desc
);
538 * Functions implementing the pwm_chip methods
540 static struct mvebu_pwm
*to_mvebu_pwm(struct pwm_chip
*chip
)
542 return container_of(chip
, struct mvebu_pwm
, chip
);
545 static int mvebu_pwm_request(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
547 struct mvebu_pwm
*mvpwm
= to_mvebu_pwm(chip
);
548 struct mvebu_gpio_chip
*mvchip
= mvpwm
->mvchip
;
549 struct gpio_desc
*desc
;
553 spin_lock_irqsave(&mvpwm
->lock
, flags
);
558 desc
= gpio_to_desc(mvchip
->chip
.base
+ pwm
->hwpwm
);
564 ret
= gpiod_request(desc
, "mvebu-pwm");
568 ret
= gpiod_direction_output(desc
, 0);
577 spin_unlock_irqrestore(&mvpwm
->lock
, flags
);
581 static void mvebu_pwm_free(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
583 struct mvebu_pwm
*mvpwm
= to_mvebu_pwm(chip
);
586 spin_lock_irqsave(&mvpwm
->lock
, flags
);
587 gpiod_free(mvpwm
->gpiod
);
589 spin_unlock_irqrestore(&mvpwm
->lock
, flags
);
592 static void mvebu_pwm_get_state(struct pwm_chip
*chip
,
593 struct pwm_device
*pwm
,
594 struct pwm_state
*state
) {
596 struct mvebu_pwm
*mvpwm
= to_mvebu_pwm(chip
);
597 struct mvebu_gpio_chip
*mvchip
= mvpwm
->mvchip
;
598 unsigned long long val
;
602 spin_lock_irqsave(&mvpwm
->lock
, flags
);
604 val
= (unsigned long long)
605 readl_relaxed(mvebu_pwmreg_blink_on_duration(mvpwm
));
607 do_div(val
, mvpwm
->clk_rate
);
609 state
->duty_cycle
= UINT_MAX
;
611 state
->duty_cycle
= val
;
613 state
->duty_cycle
= 1;
615 val
= (unsigned long long)
616 readl_relaxed(mvebu_pwmreg_blink_off_duration(mvpwm
));
618 do_div(val
, mvpwm
->clk_rate
);
619 if (val
< state
->duty_cycle
) {
622 val
-= state
->duty_cycle
;
624 state
->period
= UINT_MAX
;
631 u
= readl_relaxed(mvebu_gpioreg_blink(mvchip
));
633 state
->enabled
= true;
635 state
->enabled
= false;
637 spin_unlock_irqrestore(&mvpwm
->lock
, flags
);
640 static int mvebu_pwm_apply(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
641 struct pwm_state
*state
)
643 struct mvebu_pwm
*mvpwm
= to_mvebu_pwm(chip
);
644 struct mvebu_gpio_chip
*mvchip
= mvpwm
->mvchip
;
645 unsigned long long val
;
647 unsigned int on
, off
;
649 val
= (unsigned long long) mvpwm
->clk_rate
* state
->duty_cycle
;
650 do_div(val
, NSEC_PER_SEC
);
658 val
= (unsigned long long) mvpwm
->clk_rate
*
659 (state
->period
- state
->duty_cycle
);
660 do_div(val
, NSEC_PER_SEC
);
668 spin_lock_irqsave(&mvpwm
->lock
, flags
);
670 writel_relaxed(on
, mvebu_pwmreg_blink_on_duration(mvpwm
));
671 writel_relaxed(off
, mvebu_pwmreg_blink_off_duration(mvpwm
));
673 mvebu_gpio_blink(&mvchip
->chip
, pwm
->hwpwm
, 1);
675 mvebu_gpio_blink(&mvchip
->chip
, pwm
->hwpwm
, 0);
677 spin_unlock_irqrestore(&mvpwm
->lock
, flags
);
682 static const struct pwm_ops mvebu_pwm_ops
= {
683 .request
= mvebu_pwm_request
,
684 .free
= mvebu_pwm_free
,
685 .get_state
= mvebu_pwm_get_state
,
686 .apply
= mvebu_pwm_apply
,
687 .owner
= THIS_MODULE
,
690 static void __maybe_unused
mvebu_pwm_suspend(struct mvebu_gpio_chip
*mvchip
)
692 struct mvebu_pwm
*mvpwm
= mvchip
->mvpwm
;
694 mvpwm
->blink_select
=
695 readl_relaxed(mvebu_gpioreg_blink_counter_select(mvchip
));
696 mvpwm
->blink_on_duration
=
697 readl_relaxed(mvebu_pwmreg_blink_on_duration(mvpwm
));
698 mvpwm
->blink_off_duration
=
699 readl_relaxed(mvebu_pwmreg_blink_off_duration(mvpwm
));
702 static void __maybe_unused
mvebu_pwm_resume(struct mvebu_gpio_chip
*mvchip
)
704 struct mvebu_pwm
*mvpwm
= mvchip
->mvpwm
;
706 writel_relaxed(mvpwm
->blink_select
,
707 mvebu_gpioreg_blink_counter_select(mvchip
));
708 writel_relaxed(mvpwm
->blink_on_duration
,
709 mvebu_pwmreg_blink_on_duration(mvpwm
));
710 writel_relaxed(mvpwm
->blink_off_duration
,
711 mvebu_pwmreg_blink_off_duration(mvpwm
));
714 static int mvebu_pwm_probe(struct platform_device
*pdev
,
715 struct mvebu_gpio_chip
*mvchip
,
718 struct device
*dev
= &pdev
->dev
;
719 struct mvebu_pwm
*mvpwm
;
720 struct resource
*res
;
723 if (!of_device_is_compatible(mvchip
->chip
.of_node
,
724 "marvell,armada-370-xp-gpio"))
727 if (IS_ERR(mvchip
->clk
))
728 return PTR_ERR(mvchip
->clk
);
731 * There are only two sets of PWM configuration registers for
732 * all the GPIO lines on those SoCs which this driver reserves
733 * for the first two GPIO chips. So if the resource is missing
734 * we can't treat it as an error.
736 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "pwm");
741 * Use set A for lines of GPIO chip with id 0, B for GPIO chip
742 * with id 1. Don't allow further GPIO chips to be used for PWM.
750 writel_relaxed(0, mvebu_gpioreg_blink_counter_select(mvchip
));
752 mvpwm
= devm_kzalloc(dev
, sizeof(struct mvebu_pwm
), GFP_KERNEL
);
755 mvchip
->mvpwm
= mvpwm
;
756 mvpwm
->mvchip
= mvchip
;
758 mvpwm
->membase
= devm_ioremap_resource(dev
, res
);
759 if (IS_ERR(mvpwm
->membase
))
760 return PTR_ERR(mvpwm
->membase
);
762 mvpwm
->clk_rate
= clk_get_rate(mvchip
->clk
);
763 if (!mvpwm
->clk_rate
) {
764 dev_err(dev
, "failed to get clock rate\n");
768 mvpwm
->chip
.dev
= dev
;
769 mvpwm
->chip
.ops
= &mvebu_pwm_ops
;
770 mvpwm
->chip
.npwm
= mvchip
->chip
.ngpio
;
772 spin_lock_init(&mvpwm
->lock
);
774 return pwmchip_add(&mvpwm
->chip
);
777 #ifdef CONFIG_DEBUG_FS
778 #include <linux/seq_file.h>
780 static void mvebu_gpio_dbg_show(struct seq_file
*s
, struct gpio_chip
*chip
)
782 struct mvebu_gpio_chip
*mvchip
= gpiochip_get_data(chip
);
783 u32 out
, io_conf
, blink
, in_pol
, data_in
, cause
, edg_msk
, lvl_msk
;
786 out
= readl_relaxed(mvebu_gpioreg_out(mvchip
));
787 io_conf
= readl_relaxed(mvebu_gpioreg_io_conf(mvchip
));
788 blink
= readl_relaxed(mvebu_gpioreg_blink(mvchip
));
789 in_pol
= readl_relaxed(mvebu_gpioreg_in_pol(mvchip
));
790 data_in
= readl_relaxed(mvebu_gpioreg_data_in(mvchip
));
791 cause
= readl_relaxed(mvebu_gpioreg_edge_cause(mvchip
));
792 edg_msk
= readl_relaxed(mvebu_gpioreg_edge_mask(mvchip
));
793 lvl_msk
= readl_relaxed(mvebu_gpioreg_level_mask(mvchip
));
795 for (i
= 0; i
< chip
->ngpio
; i
++) {
800 label
= gpiochip_is_requested(chip
, i
);
805 is_out
= !(io_conf
& msk
);
807 seq_printf(s
, " gpio-%-3d (%-20.20s)", chip
->base
+ i
, label
);
810 seq_printf(s
, " out %s %s\n",
811 out
& msk
? "hi" : "lo",
812 blink
& msk
? "(blink )" : "");
816 seq_printf(s
, " in %s (act %s) - IRQ",
817 (data_in
^ in_pol
) & msk
? "hi" : "lo",
818 in_pol
& msk
? "lo" : "hi");
819 if (!((edg_msk
| lvl_msk
) & msk
)) {
820 seq_puts(s
, " disabled\n");
824 seq_puts(s
, " edge ");
826 seq_puts(s
, " level");
827 seq_printf(s
, " (%s)\n", cause
& msk
? "pending" : "clear ");
831 #define mvebu_gpio_dbg_show NULL
834 static const struct of_device_id mvebu_gpio_of_match
[] = {
836 .compatible
= "marvell,orion-gpio",
837 .data
= (void *) MVEBU_GPIO_SOC_VARIANT_ORION
,
840 .compatible
= "marvell,mv78200-gpio",
841 .data
= (void *) MVEBU_GPIO_SOC_VARIANT_MV78200
,
844 .compatible
= "marvell,armadaxp-gpio",
845 .data
= (void *) MVEBU_GPIO_SOC_VARIANT_ARMADAXP
,
848 .compatible
= "marvell,armada-370-xp-gpio",
849 .data
= (void *) MVEBU_GPIO_SOC_VARIANT_ORION
,
856 static int mvebu_gpio_suspend(struct platform_device
*pdev
, pm_message_t state
)
858 struct mvebu_gpio_chip
*mvchip
= platform_get_drvdata(pdev
);
861 mvchip
->out_reg
= readl(mvebu_gpioreg_out(mvchip
));
862 mvchip
->io_conf_reg
= readl(mvebu_gpioreg_io_conf(mvchip
));
863 mvchip
->blink_en_reg
= readl(mvebu_gpioreg_blink(mvchip
));
864 mvchip
->in_pol_reg
= readl(mvebu_gpioreg_in_pol(mvchip
));
866 switch (mvchip
->soc_variant
) {
867 case MVEBU_GPIO_SOC_VARIANT_ORION
:
868 mvchip
->edge_mask_regs
[0] =
869 readl(mvchip
->membase
+ GPIO_EDGE_MASK_OFF
);
870 mvchip
->level_mask_regs
[0] =
871 readl(mvchip
->membase
+ GPIO_LEVEL_MASK_OFF
);
873 case MVEBU_GPIO_SOC_VARIANT_MV78200
:
874 for (i
= 0; i
< 2; i
++) {
875 mvchip
->edge_mask_regs
[i
] =
876 readl(mvchip
->membase
+
877 GPIO_EDGE_MASK_MV78200_OFF(i
));
878 mvchip
->level_mask_regs
[i
] =
879 readl(mvchip
->membase
+
880 GPIO_LEVEL_MASK_MV78200_OFF(i
));
883 case MVEBU_GPIO_SOC_VARIANT_ARMADAXP
:
884 for (i
= 0; i
< 4; i
++) {
885 mvchip
->edge_mask_regs
[i
] =
886 readl(mvchip
->membase
+
887 GPIO_EDGE_MASK_ARMADAXP_OFF(i
));
888 mvchip
->level_mask_regs
[i
] =
889 readl(mvchip
->membase
+
890 GPIO_LEVEL_MASK_ARMADAXP_OFF(i
));
897 if (IS_ENABLED(CONFIG_PWM
))
898 mvebu_pwm_suspend(mvchip
);
903 static int mvebu_gpio_resume(struct platform_device
*pdev
)
905 struct mvebu_gpio_chip
*mvchip
= platform_get_drvdata(pdev
);
908 writel(mvchip
->out_reg
, mvebu_gpioreg_out(mvchip
));
909 writel(mvchip
->io_conf_reg
, mvebu_gpioreg_io_conf(mvchip
));
910 writel(mvchip
->blink_en_reg
, mvebu_gpioreg_blink(mvchip
));
911 writel(mvchip
->in_pol_reg
, mvebu_gpioreg_in_pol(mvchip
));
913 switch (mvchip
->soc_variant
) {
914 case MVEBU_GPIO_SOC_VARIANT_ORION
:
915 writel(mvchip
->edge_mask_regs
[0],
916 mvchip
->membase
+ GPIO_EDGE_MASK_OFF
);
917 writel(mvchip
->level_mask_regs
[0],
918 mvchip
->membase
+ GPIO_LEVEL_MASK_OFF
);
920 case MVEBU_GPIO_SOC_VARIANT_MV78200
:
921 for (i
= 0; i
< 2; i
++) {
922 writel(mvchip
->edge_mask_regs
[i
],
923 mvchip
->membase
+ GPIO_EDGE_MASK_MV78200_OFF(i
));
924 writel(mvchip
->level_mask_regs
[i
],
926 GPIO_LEVEL_MASK_MV78200_OFF(i
));
929 case MVEBU_GPIO_SOC_VARIANT_ARMADAXP
:
930 for (i
= 0; i
< 4; i
++) {
931 writel(mvchip
->edge_mask_regs
[i
],
933 GPIO_EDGE_MASK_ARMADAXP_OFF(i
));
934 writel(mvchip
->level_mask_regs
[i
],
936 GPIO_LEVEL_MASK_ARMADAXP_OFF(i
));
943 if (IS_ENABLED(CONFIG_PWM
))
944 mvebu_pwm_resume(mvchip
);
949 static int mvebu_gpio_probe(struct platform_device
*pdev
)
951 struct mvebu_gpio_chip
*mvchip
;
952 const struct of_device_id
*match
;
953 struct device_node
*np
= pdev
->dev
.of_node
;
954 struct resource
*res
;
955 struct irq_chip_generic
*gc
;
956 struct irq_chip_type
*ct
;
963 match
= of_match_device(mvebu_gpio_of_match
, &pdev
->dev
);
965 soc_variant
= (unsigned long) match
->data
;
967 soc_variant
= MVEBU_GPIO_SOC_VARIANT_ORION
;
969 /* Some gpio controllers do not provide irq support */
970 have_irqs
= of_irq_count(np
) != 0;
972 mvchip
= devm_kzalloc(&pdev
->dev
, sizeof(struct mvebu_gpio_chip
),
977 platform_set_drvdata(pdev
, mvchip
);
979 if (of_property_read_u32(pdev
->dev
.of_node
, "ngpios", &ngpios
)) {
980 dev_err(&pdev
->dev
, "Missing ngpios OF property\n");
984 id
= of_alias_get_id(pdev
->dev
.of_node
, "gpio");
986 dev_err(&pdev
->dev
, "Couldn't get OF id\n");
990 mvchip
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
991 /* Not all SoCs require a clock.*/
992 if (!IS_ERR(mvchip
->clk
))
993 clk_prepare_enable(mvchip
->clk
);
995 mvchip
->soc_variant
= soc_variant
;
996 mvchip
->chip
.label
= dev_name(&pdev
->dev
);
997 mvchip
->chip
.parent
= &pdev
->dev
;
998 mvchip
->chip
.request
= gpiochip_generic_request
;
999 mvchip
->chip
.free
= gpiochip_generic_free
;
1000 mvchip
->chip
.direction_input
= mvebu_gpio_direction_input
;
1001 mvchip
->chip
.get
= mvebu_gpio_get
;
1002 mvchip
->chip
.direction_output
= mvebu_gpio_direction_output
;
1003 mvchip
->chip
.set
= mvebu_gpio_set
;
1005 mvchip
->chip
.to_irq
= mvebu_gpio_to_irq
;
1006 mvchip
->chip
.base
= id
* MVEBU_MAX_GPIO_PER_BANK
;
1007 mvchip
->chip
.ngpio
= ngpios
;
1008 mvchip
->chip
.can_sleep
= false;
1009 mvchip
->chip
.of_node
= np
;
1010 mvchip
->chip
.dbg_show
= mvebu_gpio_dbg_show
;
1012 spin_lock_init(&mvchip
->lock
);
1013 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1014 mvchip
->membase
= devm_ioremap_resource(&pdev
->dev
, res
);
1015 if (IS_ERR(mvchip
->membase
))
1016 return PTR_ERR(mvchip
->membase
);
1019 * The Armada XP has a second range of registers for the
1022 if (soc_variant
== MVEBU_GPIO_SOC_VARIANT_ARMADAXP
) {
1023 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
1024 mvchip
->percpu_membase
= devm_ioremap_resource(&pdev
->dev
,
1026 if (IS_ERR(mvchip
->percpu_membase
))
1027 return PTR_ERR(mvchip
->percpu_membase
);
1031 * Mask and clear GPIO interrupts.
1033 switch (soc_variant
) {
1034 case MVEBU_GPIO_SOC_VARIANT_ORION
:
1035 writel_relaxed(0, mvchip
->membase
+ GPIO_EDGE_CAUSE_OFF
);
1036 writel_relaxed(0, mvchip
->membase
+ GPIO_EDGE_MASK_OFF
);
1037 writel_relaxed(0, mvchip
->membase
+ GPIO_LEVEL_MASK_OFF
);
1039 case MVEBU_GPIO_SOC_VARIANT_MV78200
:
1040 writel_relaxed(0, mvchip
->membase
+ GPIO_EDGE_CAUSE_OFF
);
1041 for (cpu
= 0; cpu
< 2; cpu
++) {
1042 writel_relaxed(0, mvchip
->membase
+
1043 GPIO_EDGE_MASK_MV78200_OFF(cpu
));
1044 writel_relaxed(0, mvchip
->membase
+
1045 GPIO_LEVEL_MASK_MV78200_OFF(cpu
));
1048 case MVEBU_GPIO_SOC_VARIANT_ARMADAXP
:
1049 writel_relaxed(0, mvchip
->membase
+ GPIO_EDGE_CAUSE_OFF
);
1050 writel_relaxed(0, mvchip
->membase
+ GPIO_EDGE_MASK_OFF
);
1051 writel_relaxed(0, mvchip
->membase
+ GPIO_LEVEL_MASK_OFF
);
1052 for (cpu
= 0; cpu
< 4; cpu
++) {
1053 writel_relaxed(0, mvchip
->percpu_membase
+
1054 GPIO_EDGE_CAUSE_ARMADAXP_OFF(cpu
));
1055 writel_relaxed(0, mvchip
->percpu_membase
+
1056 GPIO_EDGE_MASK_ARMADAXP_OFF(cpu
));
1057 writel_relaxed(0, mvchip
->percpu_membase
+
1058 GPIO_LEVEL_MASK_ARMADAXP_OFF(cpu
));
1065 devm_gpiochip_add_data(&pdev
->dev
, &mvchip
->chip
, mvchip
);
1067 /* Some gpio controllers do not provide irq support */
1072 irq_domain_add_linear(np
, ngpios
, &irq_generic_chip_ops
, NULL
);
1073 if (!mvchip
->domain
) {
1074 dev_err(&pdev
->dev
, "couldn't allocate irq domain %s (DT).\n",
1075 mvchip
->chip
.label
);
1079 err
= irq_alloc_domain_generic_chips(
1080 mvchip
->domain
, ngpios
, 2, np
->name
, handle_level_irq
,
1081 IRQ_NOREQUEST
| IRQ_NOPROBE
| IRQ_LEVEL
, 0, 0);
1083 dev_err(&pdev
->dev
, "couldn't allocate irq chips %s (DT).\n",
1084 mvchip
->chip
.label
);
1089 * NOTE: The common accessors cannot be used because of the percpu
1090 * access to the mask registers
1092 gc
= irq_get_domain_generic_chip(mvchip
->domain
, 0);
1093 gc
->private = mvchip
;
1094 ct
= &gc
->chip_types
[0];
1095 ct
->type
= IRQ_TYPE_LEVEL_HIGH
| IRQ_TYPE_LEVEL_LOW
;
1096 ct
->chip
.irq_mask
= mvebu_gpio_level_irq_mask
;
1097 ct
->chip
.irq_unmask
= mvebu_gpio_level_irq_unmask
;
1098 ct
->chip
.irq_set_type
= mvebu_gpio_irq_set_type
;
1099 ct
->chip
.name
= mvchip
->chip
.label
;
1101 ct
= &gc
->chip_types
[1];
1102 ct
->type
= IRQ_TYPE_EDGE_RISING
| IRQ_TYPE_EDGE_FALLING
;
1103 ct
->chip
.irq_ack
= mvebu_gpio_irq_ack
;
1104 ct
->chip
.irq_mask
= mvebu_gpio_edge_irq_mask
;
1105 ct
->chip
.irq_unmask
= mvebu_gpio_edge_irq_unmask
;
1106 ct
->chip
.irq_set_type
= mvebu_gpio_irq_set_type
;
1107 ct
->handler
= handle_edge_irq
;
1108 ct
->chip
.name
= mvchip
->chip
.label
;
1111 * Setup the interrupt handlers. Each chip can have up to 4
1112 * interrupt handlers, with each handler dealing with 8 GPIO
1115 for (i
= 0; i
< 4; i
++) {
1116 int irq
= platform_get_irq(pdev
, i
);
1120 irq_set_chained_handler_and_data(irq
, mvebu_gpio_irq_handler
,
1124 /* Armada 370/XP has simple PWM support for GPIO lines */
1125 if (IS_ENABLED(CONFIG_PWM
))
1126 return mvebu_pwm_probe(pdev
, mvchip
, id
);
1131 irq_domain_remove(mvchip
->domain
);
1136 static struct platform_driver mvebu_gpio_driver
= {
1138 .name
= "mvebu-gpio",
1139 .of_match_table
= mvebu_gpio_of_match
,
1141 .probe
= mvebu_gpio_probe
,
1142 .suspend
= mvebu_gpio_suspend
,
1143 .resume
= mvebu_gpio_resume
,
1145 builtin_platform_driver(mvebu_gpio_driver
);