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