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