Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 1 | /* |
| 2 | * GPIO driver for Marvell SoCs |
| 3 | * |
| 4 | * Copyright (C) 2012 Marvell |
| 5 | * |
| 6 | * Thomas Petazzoni <thomas.petazzoni@free-electrons.com> |
| 7 | * Andrew Lunn <andrew@lunn.ch> |
| 8 | * Sebastian Hesselbarth <sebastian.hesselbarth@gmail.com> |
| 9 | * |
| 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. |
| 13 | * |
| 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 |
| 21 | * block: |
| 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 |
| 28 | * registers. |
| 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 |
| 33 | * interrupts. |
| 34 | */ |
| 35 | |
Thierry Reding | 641d034 | 2013-01-21 11:09:01 +0100 | [diff] [blame] | 36 | #include <linux/err.h> |
Paul Gortmaker | ed329f3 | 2016-03-27 11:44:45 -0400 | [diff] [blame] | 37 | #include <linux/init.h> |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 38 | #include <linux/gpio.h> |
| 39 | #include <linux/irq.h> |
| 40 | #include <linux/slab.h> |
| 41 | #include <linux/irqdomain.h> |
| 42 | #include <linux/io.h> |
| 43 | #include <linux/of_irq.h> |
| 44 | #include <linux/of_device.h> |
Andrew Lunn | de88747 | 2013-02-03 11:34:26 +0100 | [diff] [blame] | 45 | #include <linux/clk.h> |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 46 | #include <linux/pinctrl/consumer.h> |
Thomas Petazzoni | 01ca59f | 2014-02-07 12:29:19 +0100 | [diff] [blame] | 47 | #include <linux/irqchip/chained_irq.h> |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 48 | |
| 49 | /* |
| 50 | * GPIO unit register offsets. |
| 51 | */ |
| 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 |
| 60 | |
| 61 | /* The MV78200 has per-CPU registers for edge mask and level mask */ |
Andrew Lunn | a4319a6 | 2015-01-10 00:34:47 +0100 | [diff] [blame] | 62 | #define GPIO_EDGE_MASK_MV78200_OFF(cpu) ((cpu) ? 0x30 : 0x18) |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 63 | #define GPIO_LEVEL_MASK_MV78200_OFF(cpu) ((cpu) ? 0x34 : 0x1C) |
| 64 | |
Ralph Sennhauser | 7077f4c | 2017-03-16 07:33:56 +0100 | [diff] [blame] | 65 | /* |
| 66 | * The Armada XP has per-CPU registers for interrupt cause, interrupt |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 67 | * mask and interrupt level mask. Those are relative to the |
Ralph Sennhauser | 7077f4c | 2017-03-16 07:33:56 +0100 | [diff] [blame] | 68 | * percpu_membase. |
| 69 | */ |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 70 | #define GPIO_EDGE_CAUSE_ARMADAXP_OFF(cpu) ((cpu) * 0x4) |
| 71 | #define GPIO_EDGE_MASK_ARMADAXP_OFF(cpu) (0x10 + (cpu) * 0x4) |
| 72 | #define GPIO_LEVEL_MASK_ARMADAXP_OFF(cpu) (0x20 + (cpu) * 0x4) |
| 73 | |
Andrew Lunn | a4319a6 | 2015-01-10 00:34:47 +0100 | [diff] [blame] | 74 | #define MVEBU_GPIO_SOC_VARIANT_ORION 0x1 |
| 75 | #define MVEBU_GPIO_SOC_VARIANT_MV78200 0x2 |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 76 | #define MVEBU_GPIO_SOC_VARIANT_ARMADAXP 0x3 |
| 77 | |
Andrew Lunn | a4319a6 | 2015-01-10 00:34:47 +0100 | [diff] [blame] | 78 | #define MVEBU_MAX_GPIO_PER_BANK 32 |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 79 | |
| 80 | struct mvebu_gpio_chip { |
| 81 | struct gpio_chip chip; |
| 82 | spinlock_t lock; |
| 83 | void __iomem *membase; |
| 84 | void __iomem *percpu_membase; |
Dan Carpenter | d535922 | 2013-11-07 10:50:19 +0300 | [diff] [blame] | 85 | int irqbase; |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 86 | struct irq_domain *domain; |
Andrew Lunn | a4319a6 | 2015-01-10 00:34:47 +0100 | [diff] [blame] | 87 | int soc_variant; |
Thomas Petazzoni | b5b7b48 | 2014-10-24 13:59:19 +0200 | [diff] [blame] | 88 | |
Andrew Lunn | a4319a6 | 2015-01-10 00:34:47 +0100 | [diff] [blame] | 89 | /* Used to preserve GPIO registers across suspend/resume */ |
Ralph Sennhauser | f4c240c | 2017-03-16 07:34:00 +0100 | [diff] [blame^] | 90 | u32 out_reg; |
| 91 | u32 io_conf_reg; |
| 92 | u32 blink_en_reg; |
| 93 | u32 in_pol_reg; |
| 94 | u32 edge_mask_regs[4]; |
| 95 | u32 level_mask_regs[4]; |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | /* |
| 99 | * Functions returning addresses of individual registers for a given |
| 100 | * GPIO controller. |
| 101 | */ |
| 102 | static inline void __iomem *mvebu_gpioreg_out(struct mvebu_gpio_chip *mvchip) |
| 103 | { |
| 104 | return mvchip->membase + GPIO_OUT_OFF; |
| 105 | } |
| 106 | |
Jamie Lentin | e913376 | 2012-10-28 12:23:24 +0000 | [diff] [blame] | 107 | static inline void __iomem *mvebu_gpioreg_blink(struct mvebu_gpio_chip *mvchip) |
| 108 | { |
| 109 | return mvchip->membase + GPIO_BLINK_EN_OFF; |
| 110 | } |
| 111 | |
Andrew Lunn | a4319a6 | 2015-01-10 00:34:47 +0100 | [diff] [blame] | 112 | static inline void __iomem * |
| 113 | mvebu_gpioreg_io_conf(struct mvebu_gpio_chip *mvchip) |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 114 | { |
| 115 | return mvchip->membase + GPIO_IO_CONF_OFF; |
| 116 | } |
| 117 | |
| 118 | static inline void __iomem *mvebu_gpioreg_in_pol(struct mvebu_gpio_chip *mvchip) |
| 119 | { |
| 120 | return mvchip->membase + GPIO_IN_POL_OFF; |
| 121 | } |
| 122 | |
Andrew Lunn | a4319a6 | 2015-01-10 00:34:47 +0100 | [diff] [blame] | 123 | static inline void __iomem * |
| 124 | mvebu_gpioreg_data_in(struct mvebu_gpio_chip *mvchip) |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 125 | { |
| 126 | return mvchip->membase + GPIO_DATA_IN_OFF; |
| 127 | } |
| 128 | |
Andrew Lunn | a4319a6 | 2015-01-10 00:34:47 +0100 | [diff] [blame] | 129 | static inline void __iomem * |
| 130 | mvebu_gpioreg_edge_cause(struct mvebu_gpio_chip *mvchip) |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 131 | { |
| 132 | int cpu; |
| 133 | |
Laurent Navet | f4dcd2d9 | 2013-03-20 13:15:56 +0100 | [diff] [blame] | 134 | switch (mvchip->soc_variant) { |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 135 | case MVEBU_GPIO_SOC_VARIANT_ORION: |
| 136 | case MVEBU_GPIO_SOC_VARIANT_MV78200: |
| 137 | return mvchip->membase + GPIO_EDGE_CAUSE_OFF; |
| 138 | case MVEBU_GPIO_SOC_VARIANT_ARMADAXP: |
| 139 | cpu = smp_processor_id(); |
Andrew Lunn | a4319a6 | 2015-01-10 00:34:47 +0100 | [diff] [blame] | 140 | return mvchip->percpu_membase + |
| 141 | GPIO_EDGE_CAUSE_ARMADAXP_OFF(cpu); |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 142 | default: |
| 143 | BUG(); |
| 144 | } |
| 145 | } |
| 146 | |
Andrew Lunn | a4319a6 | 2015-01-10 00:34:47 +0100 | [diff] [blame] | 147 | static inline void __iomem * |
| 148 | mvebu_gpioreg_edge_mask(struct mvebu_gpio_chip *mvchip) |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 149 | { |
| 150 | int cpu; |
| 151 | |
Laurent Navet | f4dcd2d9 | 2013-03-20 13:15:56 +0100 | [diff] [blame] | 152 | switch (mvchip->soc_variant) { |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 153 | case MVEBU_GPIO_SOC_VARIANT_ORION: |
| 154 | return mvchip->membase + GPIO_EDGE_MASK_OFF; |
| 155 | case MVEBU_GPIO_SOC_VARIANT_MV78200: |
| 156 | cpu = smp_processor_id(); |
| 157 | return mvchip->membase + GPIO_EDGE_MASK_MV78200_OFF(cpu); |
| 158 | case MVEBU_GPIO_SOC_VARIANT_ARMADAXP: |
| 159 | cpu = smp_processor_id(); |
Andrew Lunn | a4319a6 | 2015-01-10 00:34:47 +0100 | [diff] [blame] | 160 | return mvchip->percpu_membase + |
| 161 | GPIO_EDGE_MASK_ARMADAXP_OFF(cpu); |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 162 | default: |
| 163 | BUG(); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | static void __iomem *mvebu_gpioreg_level_mask(struct mvebu_gpio_chip *mvchip) |
| 168 | { |
| 169 | int cpu; |
| 170 | |
Laurent Navet | f4dcd2d9 | 2013-03-20 13:15:56 +0100 | [diff] [blame] | 171 | switch (mvchip->soc_variant) { |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 172 | case MVEBU_GPIO_SOC_VARIANT_ORION: |
| 173 | return mvchip->membase + GPIO_LEVEL_MASK_OFF; |
| 174 | case MVEBU_GPIO_SOC_VARIANT_MV78200: |
| 175 | cpu = smp_processor_id(); |
| 176 | return mvchip->membase + GPIO_LEVEL_MASK_MV78200_OFF(cpu); |
| 177 | case MVEBU_GPIO_SOC_VARIANT_ARMADAXP: |
| 178 | cpu = smp_processor_id(); |
Andrew Lunn | a4319a6 | 2015-01-10 00:34:47 +0100 | [diff] [blame] | 179 | return mvchip->percpu_membase + |
| 180 | GPIO_LEVEL_MASK_ARMADAXP_OFF(cpu); |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 181 | default: |
| 182 | BUG(); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | * Functions implementing the gpio_chip methods |
| 188 | */ |
Ralph Sennhauser | d276de7 | 2017-03-16 07:33:58 +0100 | [diff] [blame] | 189 | static void mvebu_gpio_set(struct gpio_chip *chip, unsigned int pin, int value) |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 190 | { |
Linus Walleij | bbe7600 | 2015-12-07 11:09:24 +0100 | [diff] [blame] | 191 | struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip); |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 192 | unsigned long flags; |
| 193 | u32 u; |
| 194 | |
| 195 | spin_lock_irqsave(&mvchip->lock, flags); |
| 196 | u = readl_relaxed(mvebu_gpioreg_out(mvchip)); |
| 197 | if (value) |
| 198 | u |= 1 << pin; |
| 199 | else |
| 200 | u &= ~(1 << pin); |
| 201 | writel_relaxed(u, mvebu_gpioreg_out(mvchip)); |
| 202 | spin_unlock_irqrestore(&mvchip->lock, flags); |
| 203 | } |
| 204 | |
Ralph Sennhauser | d276de7 | 2017-03-16 07:33:58 +0100 | [diff] [blame] | 205 | static int mvebu_gpio_get(struct gpio_chip *chip, unsigned int pin) |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 206 | { |
Linus Walleij | bbe7600 | 2015-12-07 11:09:24 +0100 | [diff] [blame] | 207 | struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip); |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 208 | u32 u; |
| 209 | |
| 210 | if (readl_relaxed(mvebu_gpioreg_io_conf(mvchip)) & (1 << pin)) { |
| 211 | u = readl_relaxed(mvebu_gpioreg_data_in(mvchip)) ^ |
| 212 | readl_relaxed(mvebu_gpioreg_in_pol(mvchip)); |
| 213 | } else { |
| 214 | u = readl_relaxed(mvebu_gpioreg_out(mvchip)); |
| 215 | } |
| 216 | |
| 217 | return (u >> pin) & 1; |
| 218 | } |
| 219 | |
Ralph Sennhauser | d276de7 | 2017-03-16 07:33:58 +0100 | [diff] [blame] | 220 | static void mvebu_gpio_blink(struct gpio_chip *chip, unsigned int pin, |
| 221 | int value) |
Jamie Lentin | e913376 | 2012-10-28 12:23:24 +0000 | [diff] [blame] | 222 | { |
Linus Walleij | bbe7600 | 2015-12-07 11:09:24 +0100 | [diff] [blame] | 223 | struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip); |
Jamie Lentin | e913376 | 2012-10-28 12:23:24 +0000 | [diff] [blame] | 224 | unsigned long flags; |
| 225 | u32 u; |
| 226 | |
| 227 | spin_lock_irqsave(&mvchip->lock, flags); |
| 228 | u = readl_relaxed(mvebu_gpioreg_blink(mvchip)); |
| 229 | if (value) |
| 230 | u |= 1 << pin; |
| 231 | else |
| 232 | u &= ~(1 << pin); |
| 233 | writel_relaxed(u, mvebu_gpioreg_blink(mvchip)); |
| 234 | spin_unlock_irqrestore(&mvchip->lock, flags); |
| 235 | } |
| 236 | |
Ralph Sennhauser | d276de7 | 2017-03-16 07:33:58 +0100 | [diff] [blame] | 237 | static int mvebu_gpio_direction_input(struct gpio_chip *chip, unsigned int pin) |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 238 | { |
Linus Walleij | bbe7600 | 2015-12-07 11:09:24 +0100 | [diff] [blame] | 239 | struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip); |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 240 | unsigned long flags; |
| 241 | int ret; |
| 242 | u32 u; |
| 243 | |
Ralph Sennhauser | 7077f4c | 2017-03-16 07:33:56 +0100 | [diff] [blame] | 244 | /* |
| 245 | * Check with the pinctrl driver whether this pin is usable as |
| 246 | * an input GPIO |
| 247 | */ |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 248 | ret = pinctrl_gpio_direction_input(chip->base + pin); |
| 249 | if (ret) |
| 250 | return ret; |
| 251 | |
| 252 | spin_lock_irqsave(&mvchip->lock, flags); |
| 253 | u = readl_relaxed(mvebu_gpioreg_io_conf(mvchip)); |
| 254 | u |= 1 << pin; |
| 255 | writel_relaxed(u, mvebu_gpioreg_io_conf(mvchip)); |
| 256 | spin_unlock_irqrestore(&mvchip->lock, flags); |
| 257 | |
| 258 | return 0; |
| 259 | } |
| 260 | |
Ralph Sennhauser | d276de7 | 2017-03-16 07:33:58 +0100 | [diff] [blame] | 261 | static int mvebu_gpio_direction_output(struct gpio_chip *chip, unsigned int pin, |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 262 | int value) |
| 263 | { |
Linus Walleij | bbe7600 | 2015-12-07 11:09:24 +0100 | [diff] [blame] | 264 | struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip); |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 265 | unsigned long flags; |
| 266 | int ret; |
| 267 | u32 u; |
| 268 | |
Ralph Sennhauser | 7077f4c | 2017-03-16 07:33:56 +0100 | [diff] [blame] | 269 | /* |
| 270 | * Check with the pinctrl driver whether this pin is usable as |
| 271 | * an output GPIO |
| 272 | */ |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 273 | ret = pinctrl_gpio_direction_output(chip->base + pin); |
| 274 | if (ret) |
| 275 | return ret; |
| 276 | |
Jamie Lentin | e913376 | 2012-10-28 12:23:24 +0000 | [diff] [blame] | 277 | mvebu_gpio_blink(chip, pin, 0); |
Thomas Petazzoni | c57d75c | 2012-10-23 10:17:05 +0200 | [diff] [blame] | 278 | mvebu_gpio_set(chip, pin, value); |
| 279 | |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 280 | spin_lock_irqsave(&mvchip->lock, flags); |
| 281 | u = readl_relaxed(mvebu_gpioreg_io_conf(mvchip)); |
| 282 | u &= ~(1 << pin); |
| 283 | writel_relaxed(u, mvebu_gpioreg_io_conf(mvchip)); |
| 284 | spin_unlock_irqrestore(&mvchip->lock, flags); |
| 285 | |
| 286 | return 0; |
| 287 | } |
| 288 | |
Ralph Sennhauser | d276de7 | 2017-03-16 07:33:58 +0100 | [diff] [blame] | 289 | static int mvebu_gpio_to_irq(struct gpio_chip *chip, unsigned int pin) |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 290 | { |
Linus Walleij | bbe7600 | 2015-12-07 11:09:24 +0100 | [diff] [blame] | 291 | struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip); |
Ralph Sennhauser | 163ad36 | 2017-03-16 07:33:59 +0100 | [diff] [blame] | 292 | |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 293 | return irq_create_mapping(mvchip->domain, pin); |
| 294 | } |
| 295 | |
| 296 | /* |
| 297 | * Functions implementing the irq_chip methods |
| 298 | */ |
| 299 | static void mvebu_gpio_irq_ack(struct irq_data *d) |
| 300 | { |
| 301 | struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); |
| 302 | struct mvebu_gpio_chip *mvchip = gc->private; |
Jason Gunthorpe | 812d478 | 2016-10-19 15:03:41 -0600 | [diff] [blame] | 303 | u32 mask = d->mask; |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 304 | |
| 305 | irq_gc_lock(gc); |
Jason Gunthorpe | 812d478 | 2016-10-19 15:03:41 -0600 | [diff] [blame] | 306 | writel_relaxed(~mask, mvebu_gpioreg_edge_cause(mvchip)); |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 307 | irq_gc_unlock(gc); |
| 308 | } |
| 309 | |
| 310 | static void mvebu_gpio_edge_irq_mask(struct irq_data *d) |
| 311 | { |
| 312 | struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); |
| 313 | struct mvebu_gpio_chip *mvchip = gc->private; |
Gregory CLEMENT | 6181954 | 2015-04-02 17:11:11 +0200 | [diff] [blame] | 314 | struct irq_chip_type *ct = irq_data_get_chip_type(d); |
Jason Gunthorpe | 812d478 | 2016-10-19 15:03:41 -0600 | [diff] [blame] | 315 | u32 mask = d->mask; |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 316 | |
| 317 | irq_gc_lock(gc); |
Gregory CLEMENT | 6181954 | 2015-04-02 17:11:11 +0200 | [diff] [blame] | 318 | ct->mask_cache_priv &= ~mask; |
| 319 | |
| 320 | writel_relaxed(ct->mask_cache_priv, mvebu_gpioreg_edge_mask(mvchip)); |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 321 | irq_gc_unlock(gc); |
| 322 | } |
| 323 | |
| 324 | static void mvebu_gpio_edge_irq_unmask(struct irq_data *d) |
| 325 | { |
| 326 | struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); |
| 327 | struct mvebu_gpio_chip *mvchip = gc->private; |
Gregory CLEMENT | 6181954 | 2015-04-02 17:11:11 +0200 | [diff] [blame] | 328 | struct irq_chip_type *ct = irq_data_get_chip_type(d); |
Jason Gunthorpe | 812d478 | 2016-10-19 15:03:41 -0600 | [diff] [blame] | 329 | u32 mask = d->mask; |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 330 | |
| 331 | irq_gc_lock(gc); |
Gregory CLEMENT | 6181954 | 2015-04-02 17:11:11 +0200 | [diff] [blame] | 332 | ct->mask_cache_priv |= mask; |
| 333 | writel_relaxed(ct->mask_cache_priv, mvebu_gpioreg_edge_mask(mvchip)); |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 334 | irq_gc_unlock(gc); |
| 335 | } |
| 336 | |
| 337 | static void mvebu_gpio_level_irq_mask(struct irq_data *d) |
| 338 | { |
| 339 | struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); |
| 340 | struct mvebu_gpio_chip *mvchip = gc->private; |
Gregory CLEMENT | 6181954 | 2015-04-02 17:11:11 +0200 | [diff] [blame] | 341 | struct irq_chip_type *ct = irq_data_get_chip_type(d); |
Jason Gunthorpe | 812d478 | 2016-10-19 15:03:41 -0600 | [diff] [blame] | 342 | u32 mask = d->mask; |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 343 | |
| 344 | irq_gc_lock(gc); |
Gregory CLEMENT | 6181954 | 2015-04-02 17:11:11 +0200 | [diff] [blame] | 345 | ct->mask_cache_priv &= ~mask; |
| 346 | writel_relaxed(ct->mask_cache_priv, mvebu_gpioreg_level_mask(mvchip)); |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 347 | irq_gc_unlock(gc); |
| 348 | } |
| 349 | |
| 350 | static void mvebu_gpio_level_irq_unmask(struct irq_data *d) |
| 351 | { |
| 352 | struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); |
| 353 | struct mvebu_gpio_chip *mvchip = gc->private; |
Gregory CLEMENT | 6181954 | 2015-04-02 17:11:11 +0200 | [diff] [blame] | 354 | struct irq_chip_type *ct = irq_data_get_chip_type(d); |
Jason Gunthorpe | 812d478 | 2016-10-19 15:03:41 -0600 | [diff] [blame] | 355 | u32 mask = d->mask; |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 356 | |
| 357 | irq_gc_lock(gc); |
Gregory CLEMENT | 6181954 | 2015-04-02 17:11:11 +0200 | [diff] [blame] | 358 | ct->mask_cache_priv |= mask; |
| 359 | writel_relaxed(ct->mask_cache_priv, mvebu_gpioreg_level_mask(mvchip)); |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 360 | irq_gc_unlock(gc); |
| 361 | } |
| 362 | |
| 363 | /***************************************************************************** |
| 364 | * MVEBU GPIO IRQ |
| 365 | * |
| 366 | * GPIO_IN_POL register controls whether GPIO_DATA_IN will hold the same |
| 367 | * value of the line or the opposite value. |
| 368 | * |
| 369 | * Level IRQ handlers: DATA_IN is used directly as cause register. |
Andrew Lunn | a4319a6 | 2015-01-10 00:34:47 +0100 | [diff] [blame] | 370 | * Interrupt are masked by LEVEL_MASK registers. |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 371 | * Edge IRQ handlers: Change in DATA_IN are latched in EDGE_CAUSE. |
Andrew Lunn | a4319a6 | 2015-01-10 00:34:47 +0100 | [diff] [blame] | 372 | * Interrupt are masked by EDGE_MASK registers. |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 373 | * Both-edge handlers: Similar to regular Edge handlers, but also swaps |
Andrew Lunn | a4319a6 | 2015-01-10 00:34:47 +0100 | [diff] [blame] | 374 | * the polarity to catch the next line transaction. |
| 375 | * This is a race condition that might not perfectly |
| 376 | * work on some use cases. |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 377 | * |
| 378 | * Every eight GPIO lines are grouped (OR'ed) before going up to main |
| 379 | * cause register. |
| 380 | * |
Andrew Lunn | a4319a6 | 2015-01-10 00:34:47 +0100 | [diff] [blame] | 381 | * EDGE cause mask |
| 382 | * data-in /--------| |-----| |----\ |
| 383 | * -----| |----- ---- to main cause reg |
| 384 | * X \----------------| |----/ |
| 385 | * polarity LEVEL mask |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 386 | * |
| 387 | ****************************************************************************/ |
| 388 | |
| 389 | static int mvebu_gpio_irq_set_type(struct irq_data *d, unsigned int type) |
| 390 | { |
| 391 | struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); |
| 392 | struct irq_chip_type *ct = irq_data_get_chip_type(d); |
| 393 | struct mvebu_gpio_chip *mvchip = gc->private; |
| 394 | int pin; |
| 395 | u32 u; |
| 396 | |
| 397 | pin = d->hwirq; |
| 398 | |
| 399 | u = readl_relaxed(mvebu_gpioreg_io_conf(mvchip)) & (1 << pin); |
Andrew Lunn | a4319a6 | 2015-01-10 00:34:47 +0100 | [diff] [blame] | 400 | if (!u) |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 401 | return -EINVAL; |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 402 | |
| 403 | type &= IRQ_TYPE_SENSE_MASK; |
| 404 | if (type == IRQ_TYPE_NONE) |
| 405 | return -EINVAL; |
| 406 | |
| 407 | /* Check if we need to change chip and handler */ |
| 408 | if (!(ct->type & type)) |
| 409 | if (irq_setup_alt_chip(d, type)) |
| 410 | return -EINVAL; |
| 411 | |
| 412 | /* |
| 413 | * Configure interrupt polarity. |
| 414 | */ |
Laurent Navet | f4dcd2d9 | 2013-03-20 13:15:56 +0100 | [diff] [blame] | 415 | switch (type) { |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 416 | case IRQ_TYPE_EDGE_RISING: |
| 417 | case IRQ_TYPE_LEVEL_HIGH: |
| 418 | u = readl_relaxed(mvebu_gpioreg_in_pol(mvchip)); |
| 419 | u &= ~(1 << pin); |
| 420 | writel_relaxed(u, mvebu_gpioreg_in_pol(mvchip)); |
Axel Lin | 7cf8c9f | 2012-09-30 16:23:27 +0800 | [diff] [blame] | 421 | break; |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 422 | case IRQ_TYPE_EDGE_FALLING: |
| 423 | case IRQ_TYPE_LEVEL_LOW: |
| 424 | u = readl_relaxed(mvebu_gpioreg_in_pol(mvchip)); |
| 425 | u |= 1 << pin; |
| 426 | writel_relaxed(u, mvebu_gpioreg_in_pol(mvchip)); |
Axel Lin | 7cf8c9f | 2012-09-30 16:23:27 +0800 | [diff] [blame] | 427 | break; |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 428 | case IRQ_TYPE_EDGE_BOTH: { |
| 429 | u32 v; |
| 430 | |
| 431 | v = readl_relaxed(mvebu_gpioreg_in_pol(mvchip)) ^ |
| 432 | readl_relaxed(mvebu_gpioreg_data_in(mvchip)); |
| 433 | |
| 434 | /* |
| 435 | * set initial polarity based on current input level |
| 436 | */ |
| 437 | u = readl_relaxed(mvebu_gpioreg_in_pol(mvchip)); |
| 438 | if (v & (1 << pin)) |
| 439 | u |= 1 << pin; /* falling */ |
| 440 | else |
| 441 | u &= ~(1 << pin); /* rising */ |
| 442 | writel_relaxed(u, mvebu_gpioreg_in_pol(mvchip)); |
Axel Lin | 7cf8c9f | 2012-09-30 16:23:27 +0800 | [diff] [blame] | 443 | break; |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 444 | } |
| 445 | } |
| 446 | return 0; |
| 447 | } |
| 448 | |
Thomas Gleixner | bd0b9ac | 2015-09-14 10:42:37 +0200 | [diff] [blame] | 449 | static void mvebu_gpio_irq_handler(struct irq_desc *desc) |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 450 | { |
Jiang Liu | 476f8b4 | 2015-06-04 12:13:15 +0800 | [diff] [blame] | 451 | struct mvebu_gpio_chip *mvchip = irq_desc_get_handler_data(desc); |
Thomas Petazzoni | 01ca59f | 2014-02-07 12:29:19 +0100 | [diff] [blame] | 452 | struct irq_chip *chip = irq_desc_get_chip(desc); |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 453 | u32 cause, type; |
| 454 | int i; |
| 455 | |
| 456 | if (mvchip == NULL) |
| 457 | return; |
| 458 | |
Thomas Petazzoni | 01ca59f | 2014-02-07 12:29:19 +0100 | [diff] [blame] | 459 | chained_irq_enter(chip, desc); |
| 460 | |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 461 | cause = readl_relaxed(mvebu_gpioreg_data_in(mvchip)) & |
| 462 | readl_relaxed(mvebu_gpioreg_level_mask(mvchip)); |
| 463 | cause |= readl_relaxed(mvebu_gpioreg_edge_cause(mvchip)) & |
| 464 | readl_relaxed(mvebu_gpioreg_edge_mask(mvchip)); |
| 465 | |
| 466 | for (i = 0; i < mvchip->chip.ngpio; i++) { |
| 467 | int irq; |
| 468 | |
Jason Gunthorpe | 812d478 | 2016-10-19 15:03:41 -0600 | [diff] [blame] | 469 | irq = irq_find_mapping(mvchip->domain, i); |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 470 | |
| 471 | if (!(cause & (1 << i))) |
| 472 | continue; |
| 473 | |
Javier Martinez Canillas | fb90c22 | 2013-06-14 18:40:44 +0200 | [diff] [blame] | 474 | type = irq_get_trigger_type(irq); |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 475 | if ((type & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH) { |
| 476 | /* Swap polarity (race with GPIO line) */ |
| 477 | u32 polarity; |
| 478 | |
| 479 | polarity = readl_relaxed(mvebu_gpioreg_in_pol(mvchip)); |
| 480 | polarity ^= 1 << i; |
| 481 | writel_relaxed(polarity, mvebu_gpioreg_in_pol(mvchip)); |
| 482 | } |
Thomas Petazzoni | 01ca59f | 2014-02-07 12:29:19 +0100 | [diff] [blame] | 483 | |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 484 | generic_handle_irq(irq); |
| 485 | } |
Thomas Petazzoni | 01ca59f | 2014-02-07 12:29:19 +0100 | [diff] [blame] | 486 | |
| 487 | chained_irq_exit(chip, desc); |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 488 | } |
| 489 | |
Simon Guinot | a4ba5e1 | 2013-03-24 15:45:29 +0100 | [diff] [blame] | 490 | #ifdef CONFIG_DEBUG_FS |
| 491 | #include <linux/seq_file.h> |
| 492 | |
| 493 | static void mvebu_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip) |
| 494 | { |
Linus Walleij | bbe7600 | 2015-12-07 11:09:24 +0100 | [diff] [blame] | 495 | struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip); |
Simon Guinot | a4ba5e1 | 2013-03-24 15:45:29 +0100 | [diff] [blame] | 496 | u32 out, io_conf, blink, in_pol, data_in, cause, edg_msk, lvl_msk; |
| 497 | int i; |
| 498 | |
| 499 | out = readl_relaxed(mvebu_gpioreg_out(mvchip)); |
| 500 | io_conf = readl_relaxed(mvebu_gpioreg_io_conf(mvchip)); |
| 501 | blink = readl_relaxed(mvebu_gpioreg_blink(mvchip)); |
| 502 | in_pol = readl_relaxed(mvebu_gpioreg_in_pol(mvchip)); |
| 503 | data_in = readl_relaxed(mvebu_gpioreg_data_in(mvchip)); |
| 504 | cause = readl_relaxed(mvebu_gpioreg_edge_cause(mvchip)); |
| 505 | edg_msk = readl_relaxed(mvebu_gpioreg_edge_mask(mvchip)); |
| 506 | lvl_msk = readl_relaxed(mvebu_gpioreg_level_mask(mvchip)); |
| 507 | |
| 508 | for (i = 0; i < chip->ngpio; i++) { |
| 509 | const char *label; |
| 510 | u32 msk; |
| 511 | bool is_out; |
| 512 | |
| 513 | label = gpiochip_is_requested(chip, i); |
| 514 | if (!label) |
| 515 | continue; |
| 516 | |
| 517 | msk = 1 << i; |
| 518 | is_out = !(io_conf & msk); |
| 519 | |
| 520 | seq_printf(s, " gpio-%-3d (%-20.20s)", chip->base + i, label); |
| 521 | |
| 522 | if (is_out) { |
| 523 | seq_printf(s, " out %s %s\n", |
| 524 | out & msk ? "hi" : "lo", |
| 525 | blink & msk ? "(blink )" : ""); |
| 526 | continue; |
| 527 | } |
| 528 | |
| 529 | seq_printf(s, " in %s (act %s) - IRQ", |
| 530 | (data_in ^ in_pol) & msk ? "hi" : "lo", |
| 531 | in_pol & msk ? "lo" : "hi"); |
| 532 | if (!((edg_msk | lvl_msk) & msk)) { |
Andrew Lunn | a4319a6 | 2015-01-10 00:34:47 +0100 | [diff] [blame] | 533 | seq_puts(s, " disabled\n"); |
Simon Guinot | a4ba5e1 | 2013-03-24 15:45:29 +0100 | [diff] [blame] | 534 | continue; |
| 535 | } |
| 536 | if (edg_msk & msk) |
Andrew Lunn | a4319a6 | 2015-01-10 00:34:47 +0100 | [diff] [blame] | 537 | seq_puts(s, " edge "); |
Simon Guinot | a4ba5e1 | 2013-03-24 15:45:29 +0100 | [diff] [blame] | 538 | if (lvl_msk & msk) |
Andrew Lunn | a4319a6 | 2015-01-10 00:34:47 +0100 | [diff] [blame] | 539 | seq_puts(s, " level"); |
Simon Guinot | a4ba5e1 | 2013-03-24 15:45:29 +0100 | [diff] [blame] | 540 | seq_printf(s, " (%s)\n", cause & msk ? "pending" : "clear "); |
| 541 | } |
| 542 | } |
| 543 | #else |
| 544 | #define mvebu_gpio_dbg_show NULL |
| 545 | #endif |
| 546 | |
Jingoo Han | 271b17b | 2014-05-07 18:06:08 +0900 | [diff] [blame] | 547 | static const struct of_device_id mvebu_gpio_of_match[] = { |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 548 | { |
| 549 | .compatible = "marvell,orion-gpio", |
Andrew Lunn | a4319a6 | 2015-01-10 00:34:47 +0100 | [diff] [blame] | 550 | .data = (void *) MVEBU_GPIO_SOC_VARIANT_ORION, |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 551 | }, |
| 552 | { |
| 553 | .compatible = "marvell,mv78200-gpio", |
Andrew Lunn | a4319a6 | 2015-01-10 00:34:47 +0100 | [diff] [blame] | 554 | .data = (void *) MVEBU_GPIO_SOC_VARIANT_MV78200, |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 555 | }, |
| 556 | { |
| 557 | .compatible = "marvell,armadaxp-gpio", |
Andrew Lunn | a4319a6 | 2015-01-10 00:34:47 +0100 | [diff] [blame] | 558 | .data = (void *) MVEBU_GPIO_SOC_VARIANT_ARMADAXP, |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 559 | }, |
| 560 | { |
| 561 | /* sentinel */ |
| 562 | }, |
| 563 | }; |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 564 | |
Thomas Petazzoni | b5b7b48 | 2014-10-24 13:59:19 +0200 | [diff] [blame] | 565 | static int mvebu_gpio_suspend(struct platform_device *pdev, pm_message_t state) |
| 566 | { |
| 567 | struct mvebu_gpio_chip *mvchip = platform_get_drvdata(pdev); |
| 568 | int i; |
| 569 | |
| 570 | mvchip->out_reg = readl(mvebu_gpioreg_out(mvchip)); |
| 571 | mvchip->io_conf_reg = readl(mvebu_gpioreg_io_conf(mvchip)); |
| 572 | mvchip->blink_en_reg = readl(mvebu_gpioreg_blink(mvchip)); |
| 573 | mvchip->in_pol_reg = readl(mvebu_gpioreg_in_pol(mvchip)); |
| 574 | |
| 575 | switch (mvchip->soc_variant) { |
| 576 | case MVEBU_GPIO_SOC_VARIANT_ORION: |
| 577 | mvchip->edge_mask_regs[0] = |
| 578 | readl(mvchip->membase + GPIO_EDGE_MASK_OFF); |
| 579 | mvchip->level_mask_regs[0] = |
| 580 | readl(mvchip->membase + GPIO_LEVEL_MASK_OFF); |
| 581 | break; |
| 582 | case MVEBU_GPIO_SOC_VARIANT_MV78200: |
| 583 | for (i = 0; i < 2; i++) { |
| 584 | mvchip->edge_mask_regs[i] = |
| 585 | readl(mvchip->membase + |
| 586 | GPIO_EDGE_MASK_MV78200_OFF(i)); |
| 587 | mvchip->level_mask_regs[i] = |
| 588 | readl(mvchip->membase + |
| 589 | GPIO_LEVEL_MASK_MV78200_OFF(i)); |
| 590 | } |
| 591 | break; |
| 592 | case MVEBU_GPIO_SOC_VARIANT_ARMADAXP: |
| 593 | for (i = 0; i < 4; i++) { |
| 594 | mvchip->edge_mask_regs[i] = |
| 595 | readl(mvchip->membase + |
| 596 | GPIO_EDGE_MASK_ARMADAXP_OFF(i)); |
| 597 | mvchip->level_mask_regs[i] = |
| 598 | readl(mvchip->membase + |
| 599 | GPIO_LEVEL_MASK_ARMADAXP_OFF(i)); |
| 600 | } |
| 601 | break; |
| 602 | default: |
| 603 | BUG(); |
| 604 | } |
| 605 | |
| 606 | return 0; |
| 607 | } |
| 608 | |
| 609 | static int mvebu_gpio_resume(struct platform_device *pdev) |
| 610 | { |
| 611 | struct mvebu_gpio_chip *mvchip = platform_get_drvdata(pdev); |
| 612 | int i; |
| 613 | |
| 614 | writel(mvchip->out_reg, mvebu_gpioreg_out(mvchip)); |
| 615 | writel(mvchip->io_conf_reg, mvebu_gpioreg_io_conf(mvchip)); |
| 616 | writel(mvchip->blink_en_reg, mvebu_gpioreg_blink(mvchip)); |
| 617 | writel(mvchip->in_pol_reg, mvebu_gpioreg_in_pol(mvchip)); |
| 618 | |
| 619 | switch (mvchip->soc_variant) { |
| 620 | case MVEBU_GPIO_SOC_VARIANT_ORION: |
| 621 | writel(mvchip->edge_mask_regs[0], |
| 622 | mvchip->membase + GPIO_EDGE_MASK_OFF); |
| 623 | writel(mvchip->level_mask_regs[0], |
| 624 | mvchip->membase + GPIO_LEVEL_MASK_OFF); |
| 625 | break; |
| 626 | case MVEBU_GPIO_SOC_VARIANT_MV78200: |
| 627 | for (i = 0; i < 2; i++) { |
| 628 | writel(mvchip->edge_mask_regs[i], |
| 629 | mvchip->membase + GPIO_EDGE_MASK_MV78200_OFF(i)); |
| 630 | writel(mvchip->level_mask_regs[i], |
| 631 | mvchip->membase + |
| 632 | GPIO_LEVEL_MASK_MV78200_OFF(i)); |
| 633 | } |
| 634 | break; |
| 635 | case MVEBU_GPIO_SOC_VARIANT_ARMADAXP: |
| 636 | for (i = 0; i < 4; i++) { |
| 637 | writel(mvchip->edge_mask_regs[i], |
| 638 | mvchip->membase + |
| 639 | GPIO_EDGE_MASK_ARMADAXP_OFF(i)); |
| 640 | writel(mvchip->level_mask_regs[i], |
| 641 | mvchip->membase + |
| 642 | GPIO_LEVEL_MASK_ARMADAXP_OFF(i)); |
| 643 | } |
| 644 | break; |
| 645 | default: |
| 646 | BUG(); |
| 647 | } |
| 648 | |
| 649 | return 0; |
| 650 | } |
| 651 | |
Bill Pemberton | 3836309 | 2012-11-19 13:22:34 -0500 | [diff] [blame] | 652 | static int mvebu_gpio_probe(struct platform_device *pdev) |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 653 | { |
| 654 | struct mvebu_gpio_chip *mvchip; |
| 655 | const struct of_device_id *match; |
| 656 | struct device_node *np = pdev->dev.of_node; |
| 657 | struct resource *res; |
| 658 | struct irq_chip_generic *gc; |
| 659 | struct irq_chip_type *ct; |
Andrew Lunn | de88747 | 2013-02-03 11:34:26 +0100 | [diff] [blame] | 660 | struct clk *clk; |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 661 | unsigned int ngpios; |
Jason Gunthorpe | 812d478 | 2016-10-19 15:03:41 -0600 | [diff] [blame] | 662 | bool have_irqs; |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 663 | int soc_variant; |
| 664 | int i, cpu, id; |
Andrew Lunn | f1d2d08 | 2015-01-10 00:34:48 +0100 | [diff] [blame] | 665 | int err; |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 666 | |
| 667 | match = of_match_device(mvebu_gpio_of_match, &pdev->dev); |
| 668 | if (match) |
Russell King | f0d5046 | 2017-01-10 22:53:28 +0000 | [diff] [blame] | 669 | soc_variant = (unsigned long) match->data; |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 670 | else |
| 671 | soc_variant = MVEBU_GPIO_SOC_VARIANT_ORION; |
| 672 | |
Jason Gunthorpe | 812d478 | 2016-10-19 15:03:41 -0600 | [diff] [blame] | 673 | /* Some gpio controllers do not provide irq support */ |
| 674 | have_irqs = of_irq_count(np) != 0; |
| 675 | |
Andrew Lunn | a4319a6 | 2015-01-10 00:34:47 +0100 | [diff] [blame] | 676 | mvchip = devm_kzalloc(&pdev->dev, sizeof(struct mvebu_gpio_chip), |
| 677 | GFP_KERNEL); |
Jingoo Han | 6c8365f | 2014-04-29 17:38:21 +0900 | [diff] [blame] | 678 | if (!mvchip) |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 679 | return -ENOMEM; |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 680 | |
Thomas Petazzoni | b5b7b48 | 2014-10-24 13:59:19 +0200 | [diff] [blame] | 681 | platform_set_drvdata(pdev, mvchip); |
| 682 | |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 683 | if (of_property_read_u32(pdev->dev.of_node, "ngpios", &ngpios)) { |
| 684 | dev_err(&pdev->dev, "Missing ngpios OF property\n"); |
| 685 | return -ENODEV; |
| 686 | } |
| 687 | |
| 688 | id = of_alias_get_id(pdev->dev.of_node, "gpio"); |
| 689 | if (id < 0) { |
| 690 | dev_err(&pdev->dev, "Couldn't get OF id\n"); |
| 691 | return id; |
| 692 | } |
| 693 | |
Andrew Lunn | de88747 | 2013-02-03 11:34:26 +0100 | [diff] [blame] | 694 | clk = devm_clk_get(&pdev->dev, NULL); |
| 695 | /* Not all SoCs require a clock.*/ |
| 696 | if (!IS_ERR(clk)) |
| 697 | clk_prepare_enable(clk); |
| 698 | |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 699 | mvchip->soc_variant = soc_variant; |
| 700 | mvchip->chip.label = dev_name(&pdev->dev); |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 701 | mvchip->chip.parent = &pdev->dev; |
Jonas Gorski | 203f0da | 2015-10-11 17:34:16 +0200 | [diff] [blame] | 702 | mvchip->chip.request = gpiochip_generic_request; |
| 703 | mvchip->chip.free = gpiochip_generic_free; |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 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; |
Jason Gunthorpe | 812d478 | 2016-10-19 15:03:41 -0600 | [diff] [blame] | 708 | if (have_irqs) |
| 709 | mvchip->chip.to_irq = mvebu_gpio_to_irq; |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 710 | mvchip->chip.base = id * MVEBU_MAX_GPIO_PER_BANK; |
| 711 | mvchip->chip.ngpio = ngpios; |
Linus Walleij | 9fb1f39 | 2013-12-04 14:42:46 +0100 | [diff] [blame] | 712 | mvchip->chip.can_sleep = false; |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 713 | mvchip->chip.of_node = np; |
Simon Guinot | a4ba5e1 | 2013-03-24 15:45:29 +0100 | [diff] [blame] | 714 | mvchip->chip.dbg_show = mvebu_gpio_dbg_show; |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 715 | |
| 716 | spin_lock_init(&mvchip->lock); |
Julia Lawall | 08a67a5 | 2013-08-14 11:11:07 +0200 | [diff] [blame] | 717 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Thierry Reding | 641d034 | 2013-01-21 11:09:01 +0100 | [diff] [blame] | 718 | mvchip->membase = devm_ioremap_resource(&pdev->dev, res); |
Greg Kroah-Hartman | 422d26b | 2013-01-25 21:06:30 -0800 | [diff] [blame] | 719 | if (IS_ERR(mvchip->membase)) |
Thierry Reding | 641d034 | 2013-01-21 11:09:01 +0100 | [diff] [blame] | 720 | return PTR_ERR(mvchip->membase); |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 721 | |
Ralph Sennhauser | 7077f4c | 2017-03-16 07:33:56 +0100 | [diff] [blame] | 722 | /* |
| 723 | * The Armada XP has a second range of registers for the |
| 724 | * per-CPU registers |
| 725 | */ |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 726 | if (soc_variant == MVEBU_GPIO_SOC_VARIANT_ARMADAXP) { |
| 727 | res = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
Thierry Reding | 641d034 | 2013-01-21 11:09:01 +0100 | [diff] [blame] | 728 | mvchip->percpu_membase = devm_ioremap_resource(&pdev->dev, |
| 729 | res); |
Laurent Navet | f4dcd2d9 | 2013-03-20 13:15:56 +0100 | [diff] [blame] | 730 | if (IS_ERR(mvchip->percpu_membase)) |
Thierry Reding | 641d034 | 2013-01-21 11:09:01 +0100 | [diff] [blame] | 731 | return PTR_ERR(mvchip->percpu_membase); |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | /* |
| 735 | * Mask and clear GPIO interrupts. |
| 736 | */ |
Laurent Navet | f4dcd2d9 | 2013-03-20 13:15:56 +0100 | [diff] [blame] | 737 | switch (soc_variant) { |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 738 | case MVEBU_GPIO_SOC_VARIANT_ORION: |
| 739 | writel_relaxed(0, mvchip->membase + GPIO_EDGE_CAUSE_OFF); |
| 740 | writel_relaxed(0, mvchip->membase + GPIO_EDGE_MASK_OFF); |
| 741 | writel_relaxed(0, mvchip->membase + GPIO_LEVEL_MASK_OFF); |
| 742 | break; |
| 743 | case MVEBU_GPIO_SOC_VARIANT_MV78200: |
| 744 | writel_relaxed(0, mvchip->membase + GPIO_EDGE_CAUSE_OFF); |
| 745 | for (cpu = 0; cpu < 2; cpu++) { |
| 746 | writel_relaxed(0, mvchip->membase + |
| 747 | GPIO_EDGE_MASK_MV78200_OFF(cpu)); |
| 748 | writel_relaxed(0, mvchip->membase + |
| 749 | GPIO_LEVEL_MASK_MV78200_OFF(cpu)); |
| 750 | } |
| 751 | break; |
| 752 | case MVEBU_GPIO_SOC_VARIANT_ARMADAXP: |
| 753 | writel_relaxed(0, mvchip->membase + GPIO_EDGE_CAUSE_OFF); |
| 754 | writel_relaxed(0, mvchip->membase + GPIO_EDGE_MASK_OFF); |
| 755 | writel_relaxed(0, mvchip->membase + GPIO_LEVEL_MASK_OFF); |
| 756 | for (cpu = 0; cpu < 4; cpu++) { |
| 757 | writel_relaxed(0, mvchip->percpu_membase + |
| 758 | GPIO_EDGE_CAUSE_ARMADAXP_OFF(cpu)); |
| 759 | writel_relaxed(0, mvchip->percpu_membase + |
| 760 | GPIO_EDGE_MASK_ARMADAXP_OFF(cpu)); |
| 761 | writel_relaxed(0, mvchip->percpu_membase + |
| 762 | GPIO_LEVEL_MASK_ARMADAXP_OFF(cpu)); |
| 763 | } |
| 764 | break; |
| 765 | default: |
| 766 | BUG(); |
| 767 | } |
| 768 | |
Laxman Dewangan | 00b9ab4 | 2016-02-22 17:43:28 +0530 | [diff] [blame] | 769 | devm_gpiochip_add_data(&pdev->dev, &mvchip->chip, mvchip); |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 770 | |
| 771 | /* Some gpio controllers do not provide irq support */ |
Jason Gunthorpe | 812d478 | 2016-10-19 15:03:41 -0600 | [diff] [blame] | 772 | if (!have_irqs) |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 773 | return 0; |
| 774 | |
Jason Gunthorpe | 812d478 | 2016-10-19 15:03:41 -0600 | [diff] [blame] | 775 | mvchip->domain = |
| 776 | irq_domain_add_linear(np, ngpios, &irq_generic_chip_ops, NULL); |
| 777 | if (!mvchip->domain) { |
| 778 | dev_err(&pdev->dev, "couldn't allocate irq domain %s (DT).\n", |
| 779 | mvchip->chip.label); |
| 780 | return -ENODEV; |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 781 | } |
| 782 | |
Jason Gunthorpe | 812d478 | 2016-10-19 15:03:41 -0600 | [diff] [blame] | 783 | err = irq_alloc_domain_generic_chips( |
| 784 | mvchip->domain, ngpios, 2, np->name, handle_level_irq, |
| 785 | IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_LEVEL, 0, 0); |
| 786 | if (err) { |
| 787 | dev_err(&pdev->dev, "couldn't allocate irq chips %s (DT).\n", |
| 788 | mvchip->chip.label); |
| 789 | goto err_domain; |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 790 | } |
| 791 | |
Ralph Sennhauser | 899c37e | 2017-03-16 07:33:57 +0100 | [diff] [blame] | 792 | /* |
| 793 | * NOTE: The common accessors cannot be used because of the percpu |
Jason Gunthorpe | 812d478 | 2016-10-19 15:03:41 -0600 | [diff] [blame] | 794 | * access to the mask registers |
| 795 | */ |
| 796 | gc = irq_get_domain_generic_chip(mvchip->domain, 0); |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 797 | gc->private = mvchip; |
| 798 | ct = &gc->chip_types[0]; |
| 799 | ct->type = IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW; |
| 800 | ct->chip.irq_mask = mvebu_gpio_level_irq_mask; |
| 801 | ct->chip.irq_unmask = mvebu_gpio_level_irq_unmask; |
| 802 | ct->chip.irq_set_type = mvebu_gpio_irq_set_type; |
| 803 | ct->chip.name = mvchip->chip.label; |
| 804 | |
| 805 | ct = &gc->chip_types[1]; |
| 806 | ct->type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING; |
| 807 | ct->chip.irq_ack = mvebu_gpio_irq_ack; |
| 808 | ct->chip.irq_mask = mvebu_gpio_edge_irq_mask; |
| 809 | ct->chip.irq_unmask = mvebu_gpio_edge_irq_unmask; |
| 810 | ct->chip.irq_set_type = mvebu_gpio_irq_set_type; |
| 811 | ct->handler = handle_edge_irq; |
| 812 | ct->chip.name = mvchip->chip.label; |
| 813 | |
Ralph Sennhauser | 899c37e | 2017-03-16 07:33:57 +0100 | [diff] [blame] | 814 | /* |
| 815 | * Setup the interrupt handlers. Each chip can have up to 4 |
Jason Gunthorpe | 812d478 | 2016-10-19 15:03:41 -0600 | [diff] [blame] | 816 | * interrupt handlers, with each handler dealing with 8 GPIO |
| 817 | * pins. |
| 818 | */ |
| 819 | for (i = 0; i < 4; i++) { |
| 820 | int irq = platform_get_irq(pdev, i); |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 821 | |
Jason Gunthorpe | 812d478 | 2016-10-19 15:03:41 -0600 | [diff] [blame] | 822 | if (irq < 0) |
| 823 | continue; |
| 824 | irq_set_chained_handler_and_data(irq, mvebu_gpio_irq_handler, |
| 825 | mvchip); |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | return 0; |
Andrew Lunn | f1d2d08 | 2015-01-10 00:34:48 +0100 | [diff] [blame] | 829 | |
Jason Gunthorpe | 812d478 | 2016-10-19 15:03:41 -0600 | [diff] [blame] | 830 | err_domain: |
| 831 | irq_domain_remove(mvchip->domain); |
Andrew Lunn | f1d2d08 | 2015-01-10 00:34:48 +0100 | [diff] [blame] | 832 | |
Andrew Lunn | f1d2d08 | 2015-01-10 00:34:48 +0100 | [diff] [blame] | 833 | return err; |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | static struct platform_driver mvebu_gpio_driver = { |
| 837 | .driver = { |
Andrew Lunn | a4319a6 | 2015-01-10 00:34:47 +0100 | [diff] [blame] | 838 | .name = "mvebu-gpio", |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 839 | .of_match_table = mvebu_gpio_of_match, |
| 840 | }, |
| 841 | .probe = mvebu_gpio_probe, |
Thomas Petazzoni | b5b7b48 | 2014-10-24 13:59:19 +0200 | [diff] [blame] | 842 | .suspend = mvebu_gpio_suspend, |
| 843 | .resume = mvebu_gpio_resume, |
Thomas Petazzoni | fefe7b0 | 2012-09-19 22:52:58 +0200 | [diff] [blame] | 844 | }; |
Paul Gortmaker | ed329f3 | 2016-03-27 11:44:45 -0400 | [diff] [blame] | 845 | builtin_platform_driver(mvebu_gpio_driver); |