Philipp Zabel | 1c44f5f | 2008-02-04 22:28:22 -0800 | [diff] [blame] | 1 | /* |
Eric Miao | 38f539a | 2009-01-20 12:09:06 +0800 | [diff] [blame] | 2 | * linux/arch/arm/plat-pxa/gpio.c |
Philipp Zabel | 1c44f5f | 2008-02-04 22:28:22 -0800 | [diff] [blame] | 3 | * |
| 4 | * Generic PXA GPIO handling |
| 5 | * |
| 6 | * Author: Nicolas Pitre |
| 7 | * Created: Jun 15, 2001 |
| 8 | * Copyright: MontaVista Software Inc. |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License version 2 as |
| 12 | * published by the Free Software Foundation. |
| 13 | */ |
Russell King | 2f8163b | 2011-07-26 10:53:52 +0100 | [diff] [blame] | 14 | #include <linux/gpio.h> |
Philipp Zabel | 1c44f5f | 2008-02-04 22:28:22 -0800 | [diff] [blame] | 15 | #include <linux/init.h> |
eric miao | e3630db | 2008-03-04 11:42:26 +0800 | [diff] [blame] | 16 | #include <linux/irq.h> |
Russell King | fced80c | 2008-09-06 12:10:45 +0100 | [diff] [blame] | 17 | #include <linux/io.h> |
Rafael J. Wysocki | 2eaa03b | 2011-04-22 22:03:11 +0200 | [diff] [blame] | 18 | #include <linux/syscore_ops.h> |
Daniel Mack | 4aa7826 | 2009-06-19 22:56:09 +0200 | [diff] [blame] | 19 | #include <linux/slab.h> |
Philipp Zabel | 1c44f5f | 2008-02-04 22:28:22 -0800 | [diff] [blame] | 20 | |
Linus Walleij | f55be1b | 2011-09-28 09:11:30 +0100 | [diff] [blame] | 21 | #include <mach/gpio-pxa.h> |
Philipp Zabel | 1c44f5f | 2008-02-04 22:28:22 -0800 | [diff] [blame] | 22 | |
Eric Miao | 3b8e285 | 2009-01-07 11:30:49 +0800 | [diff] [blame] | 23 | int pxa_last_gpio; |
| 24 | |
Philipp Zabel | 1c44f5f | 2008-02-04 22:28:22 -0800 | [diff] [blame] | 25 | struct pxa_gpio_chip { |
| 26 | struct gpio_chip chip; |
Eric Miao | 0807da5 | 2009-01-07 18:01:51 +0800 | [diff] [blame] | 27 | void __iomem *regbase; |
| 28 | char label[10]; |
| 29 | |
| 30 | unsigned long irq_mask; |
| 31 | unsigned long irq_edge_rise; |
| 32 | unsigned long irq_edge_fall; |
| 33 | |
| 34 | #ifdef CONFIG_PM |
| 35 | unsigned long saved_gplr; |
| 36 | unsigned long saved_gpdr; |
| 37 | unsigned long saved_grer; |
| 38 | unsigned long saved_gfer; |
| 39 | #endif |
Philipp Zabel | 1c44f5f | 2008-02-04 22:28:22 -0800 | [diff] [blame] | 40 | }; |
| 41 | |
Haojian Zhuang | 4929f5a | 2011-10-10 16:03:51 +0800 | [diff] [blame^] | 42 | enum { |
| 43 | PXA25X_GPIO = 0, |
| 44 | PXA26X_GPIO, |
| 45 | PXA27X_GPIO, |
| 46 | PXA3XX_GPIO, |
| 47 | PXA93X_GPIO, |
| 48 | MMP_GPIO = 0x10, |
| 49 | MMP2_GPIO, |
| 50 | }; |
| 51 | |
Eric Miao | 0807da5 | 2009-01-07 18:01:51 +0800 | [diff] [blame] | 52 | static DEFINE_SPINLOCK(gpio_lock); |
| 53 | static struct pxa_gpio_chip *pxa_gpio_chips; |
Haojian Zhuang | 4929f5a | 2011-10-10 16:03:51 +0800 | [diff] [blame^] | 54 | static int gpio_type; |
Eric Miao | 0807da5 | 2009-01-07 18:01:51 +0800 | [diff] [blame] | 55 | |
| 56 | #define for_each_gpio_chip(i, c) \ |
| 57 | for (i = 0, c = &pxa_gpio_chips[0]; i <= pxa_last_gpio; i += 32, c++) |
| 58 | |
| 59 | static inline void __iomem *gpio_chip_base(struct gpio_chip *c) |
| 60 | { |
| 61 | return container_of(c, struct pxa_gpio_chip, chip)->regbase; |
| 62 | } |
| 63 | |
Linus Walleij | a065685 | 2011-06-13 10:42:19 +0200 | [diff] [blame] | 64 | static inline struct pxa_gpio_chip *gpio_to_pxachip(unsigned gpio) |
Eric Miao | 0807da5 | 2009-01-07 18:01:51 +0800 | [diff] [blame] | 65 | { |
| 66 | return &pxa_gpio_chips[gpio_to_bank(gpio)]; |
| 67 | } |
| 68 | |
Haojian Zhuang | 4929f5a | 2011-10-10 16:03:51 +0800 | [diff] [blame^] | 69 | static inline int gpio_is_pxa_type(int type) |
| 70 | { |
| 71 | return (type & MMP_GPIO) == 0; |
| 72 | } |
| 73 | |
| 74 | static inline int gpio_is_mmp_type(int type) |
| 75 | { |
| 76 | return (type & MMP_GPIO) != 0; |
| 77 | } |
| 78 | |
| 79 | #ifdef CONFIG_ARCH_PXA |
| 80 | static inline int __pxa_gpio_to_irq(int gpio) |
| 81 | { |
| 82 | if (gpio_is_pxa_type(gpio_type)) |
| 83 | return PXA_GPIO_TO_IRQ(gpio); |
| 84 | return -1; |
| 85 | } |
| 86 | |
| 87 | static inline int __pxa_irq_to_gpio(int irq) |
| 88 | { |
| 89 | if (gpio_is_pxa_type(gpio_type)) |
| 90 | return irq - PXA_GPIO_TO_IRQ(0); |
| 91 | return -1; |
| 92 | } |
| 93 | #else |
| 94 | static inline int __pxa_gpio_to_irq(int gpio) { return -1; } |
| 95 | static inline int __pxa_irq_to_gpio(int irq) { return -1; } |
| 96 | #endif |
| 97 | |
| 98 | #ifdef CONFIG_ARCH_MMP |
| 99 | static inline int __mmp_gpio_to_irq(int gpio) |
| 100 | { |
| 101 | if (gpio_is_mmp_type(gpio_type)) |
| 102 | return MMP_GPIO_TO_IRQ(gpio); |
| 103 | return -1; |
| 104 | } |
| 105 | |
| 106 | static inline int __mmp_irq_to_gpio(int irq) |
| 107 | { |
| 108 | if (gpio_is_mmp_type(gpio_type)) |
| 109 | return irq - MMP_GPIO_TO_IRQ(0); |
| 110 | return -1; |
| 111 | } |
| 112 | #else |
| 113 | static inline int __mmp_gpio_to_irq(int gpio) { return -1; } |
| 114 | static inline int __mmp_irq_to_gpio(int irq) { return -1; } |
| 115 | #endif |
| 116 | |
| 117 | static int pxa_gpio_to_irq(struct gpio_chip *chip, unsigned offset) |
| 118 | { |
| 119 | int gpio, ret; |
| 120 | |
| 121 | gpio = chip->base + offset; |
| 122 | ret = __pxa_gpio_to_irq(gpio); |
| 123 | if (ret >= 0) |
| 124 | return ret; |
| 125 | return __mmp_gpio_to_irq(gpio); |
| 126 | } |
| 127 | |
| 128 | int pxa_irq_to_gpio(int irq) |
| 129 | { |
| 130 | int ret; |
| 131 | |
| 132 | ret = __pxa_irq_to_gpio(irq); |
| 133 | if (ret >= 0) |
| 134 | return ret; |
| 135 | return __mmp_irq_to_gpio(irq); |
| 136 | } |
| 137 | |
Philipp Zabel | 1c44f5f | 2008-02-04 22:28:22 -0800 | [diff] [blame] | 138 | static int pxa_gpio_direction_input(struct gpio_chip *chip, unsigned offset) |
| 139 | { |
Eric Miao | 0807da5 | 2009-01-07 18:01:51 +0800 | [diff] [blame] | 140 | void __iomem *base = gpio_chip_base(chip); |
| 141 | uint32_t value, mask = 1 << offset; |
| 142 | unsigned long flags; |
Philipp Zabel | 1c44f5f | 2008-02-04 22:28:22 -0800 | [diff] [blame] | 143 | |
Eric Miao | 0807da5 | 2009-01-07 18:01:51 +0800 | [diff] [blame] | 144 | spin_lock_irqsave(&gpio_lock, flags); |
| 145 | |
| 146 | value = __raw_readl(base + GPDR_OFFSET); |
Eric Miao | 067455a | 2008-11-26 18:12:04 +0800 | [diff] [blame] | 147 | if (__gpio_is_inverted(chip->base + offset)) |
| 148 | value |= mask; |
| 149 | else |
| 150 | value &= ~mask; |
Eric Miao | 0807da5 | 2009-01-07 18:01:51 +0800 | [diff] [blame] | 151 | __raw_writel(value, base + GPDR_OFFSET); |
Philipp Zabel | 1c44f5f | 2008-02-04 22:28:22 -0800 | [diff] [blame] | 152 | |
Eric Miao | 0807da5 | 2009-01-07 18:01:51 +0800 | [diff] [blame] | 153 | spin_unlock_irqrestore(&gpio_lock, flags); |
Philipp Zabel | 1c44f5f | 2008-02-04 22:28:22 -0800 | [diff] [blame] | 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | static int pxa_gpio_direction_output(struct gpio_chip *chip, |
Eric Miao | 0807da5 | 2009-01-07 18:01:51 +0800 | [diff] [blame] | 158 | unsigned offset, int value) |
Philipp Zabel | 1c44f5f | 2008-02-04 22:28:22 -0800 | [diff] [blame] | 159 | { |
Eric Miao | 0807da5 | 2009-01-07 18:01:51 +0800 | [diff] [blame] | 160 | void __iomem *base = gpio_chip_base(chip); |
| 161 | uint32_t tmp, mask = 1 << offset; |
| 162 | unsigned long flags; |
Philipp Zabel | 1c44f5f | 2008-02-04 22:28:22 -0800 | [diff] [blame] | 163 | |
Eric Miao | 0807da5 | 2009-01-07 18:01:51 +0800 | [diff] [blame] | 164 | __raw_writel(mask, base + (value ? GPSR_OFFSET : GPCR_OFFSET)); |
| 165 | |
| 166 | spin_lock_irqsave(&gpio_lock, flags); |
| 167 | |
| 168 | tmp = __raw_readl(base + GPDR_OFFSET); |
Eric Miao | 067455a | 2008-11-26 18:12:04 +0800 | [diff] [blame] | 169 | if (__gpio_is_inverted(chip->base + offset)) |
| 170 | tmp &= ~mask; |
| 171 | else |
| 172 | tmp |= mask; |
Eric Miao | 0807da5 | 2009-01-07 18:01:51 +0800 | [diff] [blame] | 173 | __raw_writel(tmp, base + GPDR_OFFSET); |
Philipp Zabel | 1c44f5f | 2008-02-04 22:28:22 -0800 | [diff] [blame] | 174 | |
Eric Miao | 0807da5 | 2009-01-07 18:01:51 +0800 | [diff] [blame] | 175 | spin_unlock_irqrestore(&gpio_lock, flags); |
Philipp Zabel | 1c44f5f | 2008-02-04 22:28:22 -0800 | [diff] [blame] | 176 | return 0; |
| 177 | } |
| 178 | |
Philipp Zabel | 1c44f5f | 2008-02-04 22:28:22 -0800 | [diff] [blame] | 179 | static int pxa_gpio_get(struct gpio_chip *chip, unsigned offset) |
| 180 | { |
Eric Miao | 0807da5 | 2009-01-07 18:01:51 +0800 | [diff] [blame] | 181 | return __raw_readl(gpio_chip_base(chip) + GPLR_OFFSET) & (1 << offset); |
Philipp Zabel | 1c44f5f | 2008-02-04 22:28:22 -0800 | [diff] [blame] | 182 | } |
| 183 | |
Philipp Zabel | 1c44f5f | 2008-02-04 22:28:22 -0800 | [diff] [blame] | 184 | static void pxa_gpio_set(struct gpio_chip *chip, unsigned offset, int value) |
| 185 | { |
Eric Miao | 0807da5 | 2009-01-07 18:01:51 +0800 | [diff] [blame] | 186 | __raw_writel(1 << offset, gpio_chip_base(chip) + |
| 187 | (value ? GPSR_OFFSET : GPCR_OFFSET)); |
Philipp Zabel | 1c44f5f | 2008-02-04 22:28:22 -0800 | [diff] [blame] | 188 | } |
| 189 | |
Eric Miao | 0807da5 | 2009-01-07 18:01:51 +0800 | [diff] [blame] | 190 | static int __init pxa_init_gpio_chip(int gpio_end) |
Eric Miao | a58fbcd | 2009-01-06 17:37:37 +0800 | [diff] [blame] | 191 | { |
Eric Miao | 0807da5 | 2009-01-07 18:01:51 +0800 | [diff] [blame] | 192 | int i, gpio, nbanks = gpio_to_bank(gpio_end) + 1; |
| 193 | struct pxa_gpio_chip *chips; |
Eric Miao | a58fbcd | 2009-01-06 17:37:37 +0800 | [diff] [blame] | 194 | |
Daniel Mack | 4aa7826 | 2009-06-19 22:56:09 +0200 | [diff] [blame] | 195 | chips = kzalloc(nbanks * sizeof(struct pxa_gpio_chip), GFP_KERNEL); |
Eric Miao | 0807da5 | 2009-01-07 18:01:51 +0800 | [diff] [blame] | 196 | if (chips == NULL) { |
| 197 | pr_err("%s: failed to allocate GPIO chips\n", __func__); |
| 198 | return -ENOMEM; |
Eric Miao | a58fbcd | 2009-01-06 17:37:37 +0800 | [diff] [blame] | 199 | } |
Eric Miao | 0807da5 | 2009-01-07 18:01:51 +0800 | [diff] [blame] | 200 | |
| 201 | for (i = 0, gpio = 0; i < nbanks; i++, gpio += 32) { |
| 202 | struct gpio_chip *c = &chips[i].chip; |
| 203 | |
| 204 | sprintf(chips[i].label, "gpio-%d", i); |
Arnd Bergmann | 97b09da | 2011-10-01 22:03:45 +0200 | [diff] [blame] | 205 | chips[i].regbase = GPIO_BANK(i); |
Eric Miao | 0807da5 | 2009-01-07 18:01:51 +0800 | [diff] [blame] | 206 | |
| 207 | c->base = gpio; |
| 208 | c->label = chips[i].label; |
| 209 | |
| 210 | c->direction_input = pxa_gpio_direction_input; |
| 211 | c->direction_output = pxa_gpio_direction_output; |
| 212 | c->get = pxa_gpio_get; |
| 213 | c->set = pxa_gpio_set; |
Haojian Zhuang | 4929f5a | 2011-10-10 16:03:51 +0800 | [diff] [blame^] | 214 | c->to_irq = pxa_gpio_to_irq; |
Eric Miao | 0807da5 | 2009-01-07 18:01:51 +0800 | [diff] [blame] | 215 | |
| 216 | /* number of GPIOs on last bank may be less than 32 */ |
| 217 | c->ngpio = (gpio + 31 > gpio_end) ? (gpio_end - gpio + 1) : 32; |
| 218 | gpiochip_add(c); |
| 219 | } |
| 220 | pxa_gpio_chips = chips; |
| 221 | return 0; |
Eric Miao | a58fbcd | 2009-01-06 17:37:37 +0800 | [diff] [blame] | 222 | } |
| 223 | |
Eric Miao | a8f6fae | 2009-04-21 14:39:07 +0800 | [diff] [blame] | 224 | /* Update only those GRERx and GFERx edge detection register bits if those |
| 225 | * bits are set in c->irq_mask |
| 226 | */ |
| 227 | static inline void update_edge_detect(struct pxa_gpio_chip *c) |
| 228 | { |
| 229 | uint32_t grer, gfer; |
| 230 | |
| 231 | grer = __raw_readl(c->regbase + GRER_OFFSET) & ~c->irq_mask; |
| 232 | gfer = __raw_readl(c->regbase + GFER_OFFSET) & ~c->irq_mask; |
| 233 | grer |= c->irq_edge_rise & c->irq_mask; |
| 234 | gfer |= c->irq_edge_fall & c->irq_mask; |
| 235 | __raw_writel(grer, c->regbase + GRER_OFFSET); |
| 236 | __raw_writel(gfer, c->regbase + GFER_OFFSET); |
| 237 | } |
| 238 | |
Lennert Buytenhek | a3f4c92 | 2010-11-29 11:18:26 +0100 | [diff] [blame] | 239 | static int pxa_gpio_irq_type(struct irq_data *d, unsigned int type) |
eric miao | e3630db | 2008-03-04 11:42:26 +0800 | [diff] [blame] | 240 | { |
Eric Miao | 0807da5 | 2009-01-07 18:01:51 +0800 | [diff] [blame] | 241 | struct pxa_gpio_chip *c; |
Haojian Zhuang | 4929f5a | 2011-10-10 16:03:51 +0800 | [diff] [blame^] | 242 | int gpio = pxa_irq_to_gpio(d->irq); |
Eric Miao | 0807da5 | 2009-01-07 18:01:51 +0800 | [diff] [blame] | 243 | unsigned long gpdr, mask = GPIO_bit(gpio); |
eric miao | e3630db | 2008-03-04 11:42:26 +0800 | [diff] [blame] | 244 | |
Linus Walleij | a065685 | 2011-06-13 10:42:19 +0200 | [diff] [blame] | 245 | c = gpio_to_pxachip(gpio); |
eric miao | e3630db | 2008-03-04 11:42:26 +0800 | [diff] [blame] | 246 | |
| 247 | if (type == IRQ_TYPE_PROBE) { |
| 248 | /* Don't mess with enabled GPIOs using preconfigured edges or |
| 249 | * GPIOs set to alternate function or to output during probe |
| 250 | */ |
Eric Miao | 0807da5 | 2009-01-07 18:01:51 +0800 | [diff] [blame] | 251 | if ((c->irq_edge_rise | c->irq_edge_fall) & GPIO_bit(gpio)) |
eric miao | e3630db | 2008-03-04 11:42:26 +0800 | [diff] [blame] | 252 | return 0; |
eric miao | 689c04a | 2008-03-04 17:18:38 +0800 | [diff] [blame] | 253 | |
| 254 | if (__gpio_is_occupied(gpio)) |
eric miao | e3630db | 2008-03-04 11:42:26 +0800 | [diff] [blame] | 255 | return 0; |
eric miao | 689c04a | 2008-03-04 17:18:38 +0800 | [diff] [blame] | 256 | |
eric miao | e3630db | 2008-03-04 11:42:26 +0800 | [diff] [blame] | 257 | type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING; |
| 258 | } |
| 259 | |
Eric Miao | 0807da5 | 2009-01-07 18:01:51 +0800 | [diff] [blame] | 260 | gpdr = __raw_readl(c->regbase + GPDR_OFFSET); |
| 261 | |
Eric Miao | 067455a | 2008-11-26 18:12:04 +0800 | [diff] [blame] | 262 | if (__gpio_is_inverted(gpio)) |
Eric Miao | 0807da5 | 2009-01-07 18:01:51 +0800 | [diff] [blame] | 263 | __raw_writel(gpdr | mask, c->regbase + GPDR_OFFSET); |
Eric Miao | 067455a | 2008-11-26 18:12:04 +0800 | [diff] [blame] | 264 | else |
Eric Miao | 0807da5 | 2009-01-07 18:01:51 +0800 | [diff] [blame] | 265 | __raw_writel(gpdr & ~mask, c->regbase + GPDR_OFFSET); |
eric miao | e3630db | 2008-03-04 11:42:26 +0800 | [diff] [blame] | 266 | |
| 267 | if (type & IRQ_TYPE_EDGE_RISING) |
Eric Miao | 0807da5 | 2009-01-07 18:01:51 +0800 | [diff] [blame] | 268 | c->irq_edge_rise |= mask; |
eric miao | e3630db | 2008-03-04 11:42:26 +0800 | [diff] [blame] | 269 | else |
Eric Miao | 0807da5 | 2009-01-07 18:01:51 +0800 | [diff] [blame] | 270 | c->irq_edge_rise &= ~mask; |
eric miao | e3630db | 2008-03-04 11:42:26 +0800 | [diff] [blame] | 271 | |
| 272 | if (type & IRQ_TYPE_EDGE_FALLING) |
Eric Miao | 0807da5 | 2009-01-07 18:01:51 +0800 | [diff] [blame] | 273 | c->irq_edge_fall |= mask; |
eric miao | e3630db | 2008-03-04 11:42:26 +0800 | [diff] [blame] | 274 | else |
Eric Miao | 0807da5 | 2009-01-07 18:01:51 +0800 | [diff] [blame] | 275 | c->irq_edge_fall &= ~mask; |
eric miao | e3630db | 2008-03-04 11:42:26 +0800 | [diff] [blame] | 276 | |
Eric Miao | a8f6fae | 2009-04-21 14:39:07 +0800 | [diff] [blame] | 277 | update_edge_detect(c); |
eric miao | e3630db | 2008-03-04 11:42:26 +0800 | [diff] [blame] | 278 | |
Lennert Buytenhek | a3f4c92 | 2010-11-29 11:18:26 +0100 | [diff] [blame] | 279 | pr_debug("%s: IRQ%d (GPIO%d) - edge%s%s\n", __func__, d->irq, gpio, |
eric miao | e3630db | 2008-03-04 11:42:26 +0800 | [diff] [blame] | 280 | ((type & IRQ_TYPE_EDGE_RISING) ? " rising" : ""), |
| 281 | ((type & IRQ_TYPE_EDGE_FALLING) ? " falling" : "")); |
| 282 | return 0; |
| 283 | } |
| 284 | |
eric miao | e3630db | 2008-03-04 11:42:26 +0800 | [diff] [blame] | 285 | static void pxa_gpio_demux_handler(unsigned int irq, struct irq_desc *desc) |
| 286 | { |
Eric Miao | 0807da5 | 2009-01-07 18:01:51 +0800 | [diff] [blame] | 287 | struct pxa_gpio_chip *c; |
| 288 | int loop, gpio, gpio_base, n; |
| 289 | unsigned long gedr; |
eric miao | e3630db | 2008-03-04 11:42:26 +0800 | [diff] [blame] | 290 | |
| 291 | do { |
eric miao | e3630db | 2008-03-04 11:42:26 +0800 | [diff] [blame] | 292 | loop = 0; |
Eric Miao | 0807da5 | 2009-01-07 18:01:51 +0800 | [diff] [blame] | 293 | for_each_gpio_chip(gpio, c) { |
| 294 | gpio_base = c->chip.base; |
eric miao | e3630db | 2008-03-04 11:42:26 +0800 | [diff] [blame] | 295 | |
Eric Miao | 0807da5 | 2009-01-07 18:01:51 +0800 | [diff] [blame] | 296 | gedr = __raw_readl(c->regbase + GEDR_OFFSET); |
| 297 | gedr = gedr & c->irq_mask; |
| 298 | __raw_writel(gedr, c->regbase + GEDR_OFFSET); |
eric miao | e3630db | 2008-03-04 11:42:26 +0800 | [diff] [blame] | 299 | |
Eric Miao | 0807da5 | 2009-01-07 18:01:51 +0800 | [diff] [blame] | 300 | n = find_first_bit(&gedr, BITS_PER_LONG); |
| 301 | while (n < BITS_PER_LONG) { |
| 302 | loop = 1; |
| 303 | |
| 304 | generic_handle_irq(gpio_to_irq(gpio_base + n)); |
| 305 | n = find_next_bit(&gedr, BITS_PER_LONG, n + 1); |
| 306 | } |
eric miao | e3630db | 2008-03-04 11:42:26 +0800 | [diff] [blame] | 307 | } |
| 308 | } while (loop); |
| 309 | } |
| 310 | |
Lennert Buytenhek | a3f4c92 | 2010-11-29 11:18:26 +0100 | [diff] [blame] | 311 | static void pxa_ack_muxed_gpio(struct irq_data *d) |
eric miao | e3630db | 2008-03-04 11:42:26 +0800 | [diff] [blame] | 312 | { |
Haojian Zhuang | 4929f5a | 2011-10-10 16:03:51 +0800 | [diff] [blame^] | 313 | int gpio = pxa_irq_to_gpio(d->irq); |
Linus Walleij | a065685 | 2011-06-13 10:42:19 +0200 | [diff] [blame] | 314 | struct pxa_gpio_chip *c = gpio_to_pxachip(gpio); |
Eric Miao | 0807da5 | 2009-01-07 18:01:51 +0800 | [diff] [blame] | 315 | |
| 316 | __raw_writel(GPIO_bit(gpio), c->regbase + GEDR_OFFSET); |
eric miao | e3630db | 2008-03-04 11:42:26 +0800 | [diff] [blame] | 317 | } |
| 318 | |
Lennert Buytenhek | a3f4c92 | 2010-11-29 11:18:26 +0100 | [diff] [blame] | 319 | static void pxa_mask_muxed_gpio(struct irq_data *d) |
eric miao | e3630db | 2008-03-04 11:42:26 +0800 | [diff] [blame] | 320 | { |
Haojian Zhuang | 4929f5a | 2011-10-10 16:03:51 +0800 | [diff] [blame^] | 321 | int gpio = pxa_irq_to_gpio(d->irq); |
Linus Walleij | a065685 | 2011-06-13 10:42:19 +0200 | [diff] [blame] | 322 | struct pxa_gpio_chip *c = gpio_to_pxachip(gpio); |
Eric Miao | 0807da5 | 2009-01-07 18:01:51 +0800 | [diff] [blame] | 323 | uint32_t grer, gfer; |
| 324 | |
| 325 | c->irq_mask &= ~GPIO_bit(gpio); |
| 326 | |
| 327 | grer = __raw_readl(c->regbase + GRER_OFFSET) & ~GPIO_bit(gpio); |
| 328 | gfer = __raw_readl(c->regbase + GFER_OFFSET) & ~GPIO_bit(gpio); |
| 329 | __raw_writel(grer, c->regbase + GRER_OFFSET); |
| 330 | __raw_writel(gfer, c->regbase + GFER_OFFSET); |
eric miao | e3630db | 2008-03-04 11:42:26 +0800 | [diff] [blame] | 331 | } |
| 332 | |
Lennert Buytenhek | a3f4c92 | 2010-11-29 11:18:26 +0100 | [diff] [blame] | 333 | static void pxa_unmask_muxed_gpio(struct irq_data *d) |
eric miao | e3630db | 2008-03-04 11:42:26 +0800 | [diff] [blame] | 334 | { |
Haojian Zhuang | 4929f5a | 2011-10-10 16:03:51 +0800 | [diff] [blame^] | 335 | int gpio = pxa_irq_to_gpio(d->irq); |
Linus Walleij | a065685 | 2011-06-13 10:42:19 +0200 | [diff] [blame] | 336 | struct pxa_gpio_chip *c = gpio_to_pxachip(gpio); |
Eric Miao | 0807da5 | 2009-01-07 18:01:51 +0800 | [diff] [blame] | 337 | |
| 338 | c->irq_mask |= GPIO_bit(gpio); |
Eric Miao | a8f6fae | 2009-04-21 14:39:07 +0800 | [diff] [blame] | 339 | update_edge_detect(c); |
eric miao | e3630db | 2008-03-04 11:42:26 +0800 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | static struct irq_chip pxa_muxed_gpio_chip = { |
| 343 | .name = "GPIO", |
Lennert Buytenhek | a3f4c92 | 2010-11-29 11:18:26 +0100 | [diff] [blame] | 344 | .irq_ack = pxa_ack_muxed_gpio, |
| 345 | .irq_mask = pxa_mask_muxed_gpio, |
| 346 | .irq_unmask = pxa_unmask_muxed_gpio, |
| 347 | .irq_set_type = pxa_gpio_irq_type, |
eric miao | e3630db | 2008-03-04 11:42:26 +0800 | [diff] [blame] | 348 | }; |
| 349 | |
Eric Miao | a58fbcd | 2009-01-06 17:37:37 +0800 | [diff] [blame] | 350 | void __init pxa_init_gpio(int mux_irq, int start, int end, set_wake_t fn) |
eric miao | e3630db | 2008-03-04 11:42:26 +0800 | [diff] [blame] | 351 | { |
Eric Miao | 0807da5 | 2009-01-07 18:01:51 +0800 | [diff] [blame] | 352 | struct pxa_gpio_chip *c; |
| 353 | int gpio, irq; |
eric miao | e3630db | 2008-03-04 11:42:26 +0800 | [diff] [blame] | 354 | |
Eric Miao | a58fbcd | 2009-01-06 17:37:37 +0800 | [diff] [blame] | 355 | pxa_last_gpio = end; |
eric miao | e3630db | 2008-03-04 11:42:26 +0800 | [diff] [blame] | 356 | |
Eric Miao | 0807da5 | 2009-01-07 18:01:51 +0800 | [diff] [blame] | 357 | /* Initialize GPIO chips */ |
| 358 | pxa_init_gpio_chip(end); |
| 359 | |
eric miao | e3630db | 2008-03-04 11:42:26 +0800 | [diff] [blame] | 360 | /* clear all GPIO edge detects */ |
Eric Miao | 0807da5 | 2009-01-07 18:01:51 +0800 | [diff] [blame] | 361 | for_each_gpio_chip(gpio, c) { |
| 362 | __raw_writel(0, c->regbase + GFER_OFFSET); |
| 363 | __raw_writel(0, c->regbase + GRER_OFFSET); |
| 364 | __raw_writel(~0,c->regbase + GEDR_OFFSET); |
eric miao | e3630db | 2008-03-04 11:42:26 +0800 | [diff] [blame] | 365 | } |
| 366 | |
Haojian Zhuang | 87c49e2 | 2011-10-10 14:38:46 +0800 | [diff] [blame] | 367 | #ifdef CONFIG_ARCH_PXA |
| 368 | irq = gpio_to_irq(0); |
| 369 | irq_set_chip_and_handler(irq, &pxa_muxed_gpio_chip, |
| 370 | handle_edge_irq); |
| 371 | set_irq_flags(irq, IRQF_VALID | IRQF_PROBE); |
| 372 | irq_set_chained_handler(IRQ_GPIO0, pxa_gpio_demux_handler); |
| 373 | |
| 374 | irq = gpio_to_irq(1); |
| 375 | irq_set_chip_and_handler(irq, &pxa_muxed_gpio_chip, |
| 376 | handle_edge_irq); |
| 377 | set_irq_flags(irq, IRQF_VALID | IRQF_PROBE); |
| 378 | irq_set_chained_handler(IRQ_GPIO1, pxa_gpio_demux_handler); |
| 379 | #endif |
| 380 | |
Eric Miao | a58fbcd | 2009-01-06 17:37:37 +0800 | [diff] [blame] | 381 | for (irq = gpio_to_irq(start); irq <= gpio_to_irq(end); irq++) { |
Thomas Gleixner | f38c02f | 2011-03-24 13:35:09 +0100 | [diff] [blame] | 382 | irq_set_chip_and_handler(irq, &pxa_muxed_gpio_chip, |
| 383 | handle_edge_irq); |
eric miao | e3630db | 2008-03-04 11:42:26 +0800 | [diff] [blame] | 384 | set_irq_flags(irq, IRQF_VALID | IRQF_PROBE); |
| 385 | } |
| 386 | |
| 387 | /* Install handler for GPIO>=2 edge detect interrupts */ |
Thomas Gleixner | 6845664a | 2011-03-24 13:25:22 +0100 | [diff] [blame] | 388 | irq_set_chained_handler(mux_irq, pxa_gpio_demux_handler); |
Lennert Buytenhek | a3f4c92 | 2010-11-29 11:18:26 +0100 | [diff] [blame] | 389 | pxa_muxed_gpio_chip.irq_set_wake = fn; |
eric miao | e3630db | 2008-03-04 11:42:26 +0800 | [diff] [blame] | 390 | } |
eric miao | 663707c | 2008-03-04 16:13:58 +0800 | [diff] [blame] | 391 | |
| 392 | #ifdef CONFIG_PM |
Rafael J. Wysocki | 2eaa03b | 2011-04-22 22:03:11 +0200 | [diff] [blame] | 393 | static int pxa_gpio_suspend(void) |
eric miao | 663707c | 2008-03-04 16:13:58 +0800 | [diff] [blame] | 394 | { |
Eric Miao | 0807da5 | 2009-01-07 18:01:51 +0800 | [diff] [blame] | 395 | struct pxa_gpio_chip *c; |
| 396 | int gpio; |
eric miao | 663707c | 2008-03-04 16:13:58 +0800 | [diff] [blame] | 397 | |
Eric Miao | 0807da5 | 2009-01-07 18:01:51 +0800 | [diff] [blame] | 398 | for_each_gpio_chip(gpio, c) { |
| 399 | c->saved_gplr = __raw_readl(c->regbase + GPLR_OFFSET); |
| 400 | c->saved_gpdr = __raw_readl(c->regbase + GPDR_OFFSET); |
| 401 | c->saved_grer = __raw_readl(c->regbase + GRER_OFFSET); |
| 402 | c->saved_gfer = __raw_readl(c->regbase + GFER_OFFSET); |
eric miao | 663707c | 2008-03-04 16:13:58 +0800 | [diff] [blame] | 403 | |
| 404 | /* Clear GPIO transition detect bits */ |
Eric Miao | 0807da5 | 2009-01-07 18:01:51 +0800 | [diff] [blame] | 405 | __raw_writel(0xffffffff, c->regbase + GEDR_OFFSET); |
eric miao | 663707c | 2008-03-04 16:13:58 +0800 | [diff] [blame] | 406 | } |
| 407 | return 0; |
| 408 | } |
| 409 | |
Rafael J. Wysocki | 2eaa03b | 2011-04-22 22:03:11 +0200 | [diff] [blame] | 410 | static void pxa_gpio_resume(void) |
eric miao | 663707c | 2008-03-04 16:13:58 +0800 | [diff] [blame] | 411 | { |
Eric Miao | 0807da5 | 2009-01-07 18:01:51 +0800 | [diff] [blame] | 412 | struct pxa_gpio_chip *c; |
| 413 | int gpio; |
eric miao | 663707c | 2008-03-04 16:13:58 +0800 | [diff] [blame] | 414 | |
Eric Miao | 0807da5 | 2009-01-07 18:01:51 +0800 | [diff] [blame] | 415 | for_each_gpio_chip(gpio, c) { |
eric miao | 663707c | 2008-03-04 16:13:58 +0800 | [diff] [blame] | 416 | /* restore level with set/clear */ |
Eric Miao | 0807da5 | 2009-01-07 18:01:51 +0800 | [diff] [blame] | 417 | __raw_writel( c->saved_gplr, c->regbase + GPSR_OFFSET); |
| 418 | __raw_writel(~c->saved_gplr, c->regbase + GPCR_OFFSET); |
eric miao | 663707c | 2008-03-04 16:13:58 +0800 | [diff] [blame] | 419 | |
Eric Miao | 0807da5 | 2009-01-07 18:01:51 +0800 | [diff] [blame] | 420 | __raw_writel(c->saved_grer, c->regbase + GRER_OFFSET); |
| 421 | __raw_writel(c->saved_gfer, c->regbase + GFER_OFFSET); |
| 422 | __raw_writel(c->saved_gpdr, c->regbase + GPDR_OFFSET); |
eric miao | 663707c | 2008-03-04 16:13:58 +0800 | [diff] [blame] | 423 | } |
eric miao | 663707c | 2008-03-04 16:13:58 +0800 | [diff] [blame] | 424 | } |
| 425 | #else |
| 426 | #define pxa_gpio_suspend NULL |
| 427 | #define pxa_gpio_resume NULL |
| 428 | #endif |
| 429 | |
Rafael J. Wysocki | 2eaa03b | 2011-04-22 22:03:11 +0200 | [diff] [blame] | 430 | struct syscore_ops pxa_gpio_syscore_ops = { |
eric miao | 663707c | 2008-03-04 16:13:58 +0800 | [diff] [blame] | 431 | .suspend = pxa_gpio_suspend, |
| 432 | .resume = pxa_gpio_resume, |
| 433 | }; |