SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 1 | /* |
Andrew Victor | 9d04126 | 2007-02-05 11:42:07 +0100 | [diff] [blame] | 2 | * linux/arch/arm/mach-at91/gpio.c |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2005 HP Labs |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | */ |
| 11 | |
Andrew Victor | f217383 | 2006-09-27 13:23:00 +0100 | [diff] [blame] | 12 | #include <linux/clk.h> |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 13 | #include <linux/errno.h> |
Thomas Gleixner | 07d265d | 2006-07-01 23:01:50 +0100 | [diff] [blame] | 14 | #include <linux/interrupt.h> |
| 15 | #include <linux/irq.h> |
Andrew Victor | b66545e | 2007-11-23 16:09:10 +0100 | [diff] [blame] | 16 | #include <linux/debugfs.h> |
| 17 | #include <linux/seq_file.h> |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 18 | #include <linux/kernel.h> |
| 19 | #include <linux/list.h> |
| 20 | #include <linux/module.h> |
Russell King | fced80c | 2008-09-06 12:10:45 +0100 | [diff] [blame] | 21 | #include <linux/io.h> |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 22 | |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 23 | #include <mach/hardware.h> |
| 24 | #include <mach/at91_pio.h> |
| 25 | #include <mach/gpio.h> |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 26 | |
Ryan Mallon | f373e8c | 2009-02-10 21:02:08 +0100 | [diff] [blame^] | 27 | #include <asm/gpio.h> |
| 28 | |
Andrew Victor | f217383 | 2006-09-27 13:23:00 +0100 | [diff] [blame] | 29 | #include "generic.h" |
| 30 | |
Ryan Mallon | f373e8c | 2009-02-10 21:02:08 +0100 | [diff] [blame^] | 31 | struct at91_gpio_chip { |
| 32 | struct gpio_chip chip; |
| 33 | struct at91_gpio_chip *next; /* Bank sharing same clock */ |
| 34 | struct at91_gpio_bank *bank; /* Bank definition */ |
| 35 | void __iomem *regbase; /* Base of register bank */ |
| 36 | }; |
Andrew Victor | f217383 | 2006-09-27 13:23:00 +0100 | [diff] [blame] | 37 | |
Ryan Mallon | f373e8c | 2009-02-10 21:02:08 +0100 | [diff] [blame^] | 38 | #define to_at91_gpio_chip(c) container_of(c, struct at91_gpio_chip, chip) |
| 39 | |
| 40 | static void at91_gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip); |
| 41 | static void at91_gpiolib_set(struct gpio_chip *chip, unsigned offset, int val); |
| 42 | static int at91_gpiolib_get(struct gpio_chip *chip, unsigned offset); |
| 43 | static int at91_gpiolib_direction_output(struct gpio_chip *chip, |
| 44 | unsigned offset, int val); |
| 45 | static int at91_gpiolib_direction_input(struct gpio_chip *chip, |
| 46 | unsigned offset); |
| 47 | static int at91_gpiolib_request(struct gpio_chip *chip, unsigned offset); |
| 48 | |
| 49 | #define AT91_GPIO_CHIP(name, base_gpio, nr_gpio) \ |
| 50 | { \ |
| 51 | .chip = { \ |
| 52 | .label = name, \ |
| 53 | .request = at91_gpiolib_request, \ |
| 54 | .direction_input = at91_gpiolib_direction_input, \ |
| 55 | .direction_output = at91_gpiolib_direction_output, \ |
| 56 | .get = at91_gpiolib_get, \ |
| 57 | .set = at91_gpiolib_set, \ |
| 58 | .dbg_show = at91_gpiolib_dbg_show, \ |
| 59 | .base = base_gpio, \ |
| 60 | .ngpio = nr_gpio, \ |
| 61 | }, \ |
| 62 | } |
| 63 | |
| 64 | static struct at91_gpio_chip gpio_chip[] = { |
| 65 | AT91_GPIO_CHIP("A", 0x00 + PIN_BASE, 32), |
| 66 | AT91_GPIO_CHIP("B", 0x20 + PIN_BASE, 32), |
| 67 | AT91_GPIO_CHIP("C", 0x40 + PIN_BASE, 32), |
| 68 | AT91_GPIO_CHIP("D", 0x60 + PIN_BASE, 32), |
| 69 | AT91_GPIO_CHIP("E", 0x80 + PIN_BASE, 32), |
| 70 | }; |
| 71 | |
Andrew Victor | f217383 | 2006-09-27 13:23:00 +0100 | [diff] [blame] | 72 | static int gpio_banks; |
| 73 | |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 74 | static inline void __iomem *pin_to_controller(unsigned pin) |
| 75 | { |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 76 | pin -= PIN_BASE; |
| 77 | pin /= 32; |
Andrew Victor | f217383 | 2006-09-27 13:23:00 +0100 | [diff] [blame] | 78 | if (likely(pin < gpio_banks)) |
Ryan Mallon | f373e8c | 2009-02-10 21:02:08 +0100 | [diff] [blame^] | 79 | return gpio_chip[pin].regbase; |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 80 | |
| 81 | return NULL; |
| 82 | } |
| 83 | |
| 84 | static inline unsigned pin_to_mask(unsigned pin) |
| 85 | { |
| 86 | pin -= PIN_BASE; |
| 87 | return 1 << (pin % 32); |
| 88 | } |
| 89 | |
| 90 | |
| 91 | /*--------------------------------------------------------------------------*/ |
| 92 | |
| 93 | /* Not all hardware capabilities are exposed through these calls; they |
| 94 | * only encapsulate the most common features and modes. (So if you |
| 95 | * want to change signals in groups, do it directly.) |
| 96 | * |
| 97 | * Bootloaders will usually handle some of the pin multiplexing setup. |
| 98 | * The intent is certainly that by the time Linux is fully booted, all |
| 99 | * pins should have been fully initialized. These setup calls should |
| 100 | * only be used by board setup routines, or possibly in driver probe(). |
| 101 | * |
| 102 | * For bootloaders doing all that setup, these calls could be inlined |
| 103 | * as NOPs so Linux won't duplicate any setup code |
| 104 | */ |
| 105 | |
| 106 | |
| 107 | /* |
David Brownell | a31c4ee | 2007-02-12 00:53:13 -0800 | [diff] [blame] | 108 | * mux the pin to the "GPIO" peripheral role. |
| 109 | */ |
| 110 | int __init_or_module at91_set_GPIO_periph(unsigned pin, int use_pullup) |
| 111 | { |
| 112 | void __iomem *pio = pin_to_controller(pin); |
| 113 | unsigned mask = pin_to_mask(pin); |
| 114 | |
| 115 | if (!pio) |
| 116 | return -EINVAL; |
| 117 | __raw_writel(mask, pio + PIO_IDR); |
| 118 | __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR)); |
| 119 | __raw_writel(mask, pio + PIO_PER); |
| 120 | return 0; |
| 121 | } |
| 122 | EXPORT_SYMBOL(at91_set_GPIO_periph); |
| 123 | |
| 124 | |
| 125 | /* |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 126 | * mux the pin to the "A" internal peripheral role. |
| 127 | */ |
| 128 | int __init_or_module at91_set_A_periph(unsigned pin, int use_pullup) |
| 129 | { |
| 130 | void __iomem *pio = pin_to_controller(pin); |
| 131 | unsigned mask = pin_to_mask(pin); |
| 132 | |
| 133 | if (!pio) |
| 134 | return -EINVAL; |
| 135 | |
| 136 | __raw_writel(mask, pio + PIO_IDR); |
| 137 | __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR)); |
| 138 | __raw_writel(mask, pio + PIO_ASR); |
| 139 | __raw_writel(mask, pio + PIO_PDR); |
| 140 | return 0; |
| 141 | } |
| 142 | EXPORT_SYMBOL(at91_set_A_periph); |
| 143 | |
| 144 | |
| 145 | /* |
| 146 | * mux the pin to the "B" internal peripheral role. |
| 147 | */ |
| 148 | int __init_or_module at91_set_B_periph(unsigned pin, int use_pullup) |
| 149 | { |
| 150 | void __iomem *pio = pin_to_controller(pin); |
| 151 | unsigned mask = pin_to_mask(pin); |
| 152 | |
| 153 | if (!pio) |
| 154 | return -EINVAL; |
| 155 | |
| 156 | __raw_writel(mask, pio + PIO_IDR); |
| 157 | __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR)); |
| 158 | __raw_writel(mask, pio + PIO_BSR); |
| 159 | __raw_writel(mask, pio + PIO_PDR); |
| 160 | return 0; |
| 161 | } |
| 162 | EXPORT_SYMBOL(at91_set_B_periph); |
| 163 | |
| 164 | |
| 165 | /* |
| 166 | * mux the pin to the gpio controller (instead of "A" or "B" peripheral), and |
| 167 | * configure it for an input. |
| 168 | */ |
| 169 | int __init_or_module at91_set_gpio_input(unsigned pin, int use_pullup) |
| 170 | { |
| 171 | void __iomem *pio = pin_to_controller(pin); |
| 172 | unsigned mask = pin_to_mask(pin); |
| 173 | |
| 174 | if (!pio) |
| 175 | return -EINVAL; |
| 176 | |
| 177 | __raw_writel(mask, pio + PIO_IDR); |
| 178 | __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR)); |
| 179 | __raw_writel(mask, pio + PIO_ODR); |
| 180 | __raw_writel(mask, pio + PIO_PER); |
| 181 | return 0; |
| 182 | } |
| 183 | EXPORT_SYMBOL(at91_set_gpio_input); |
| 184 | |
| 185 | |
| 186 | /* |
| 187 | * mux the pin to the gpio controller (instead of "A" or "B" peripheral), |
| 188 | * and configure it for an output. |
| 189 | */ |
| 190 | int __init_or_module at91_set_gpio_output(unsigned pin, int value) |
| 191 | { |
| 192 | void __iomem *pio = pin_to_controller(pin); |
| 193 | unsigned mask = pin_to_mask(pin); |
| 194 | |
| 195 | if (!pio) |
| 196 | return -EINVAL; |
| 197 | |
| 198 | __raw_writel(mask, pio + PIO_IDR); |
| 199 | __raw_writel(mask, pio + PIO_PUDR); |
| 200 | __raw_writel(mask, pio + (value ? PIO_SODR : PIO_CODR)); |
| 201 | __raw_writel(mask, pio + PIO_OER); |
| 202 | __raw_writel(mask, pio + PIO_PER); |
| 203 | return 0; |
| 204 | } |
| 205 | EXPORT_SYMBOL(at91_set_gpio_output); |
| 206 | |
| 207 | |
| 208 | /* |
| 209 | * enable/disable the glitch filter; mostly used with IRQ handling. |
| 210 | */ |
| 211 | int __init_or_module at91_set_deglitch(unsigned pin, int is_on) |
| 212 | { |
| 213 | void __iomem *pio = pin_to_controller(pin); |
| 214 | unsigned mask = pin_to_mask(pin); |
| 215 | |
| 216 | if (!pio) |
| 217 | return -EINVAL; |
| 218 | __raw_writel(mask, pio + (is_on ? PIO_IFER : PIO_IFDR)); |
| 219 | return 0; |
| 220 | } |
| 221 | EXPORT_SYMBOL(at91_set_deglitch); |
| 222 | |
Andrew Victor | df666b9 | 2006-02-22 21:23:35 +0000 | [diff] [blame] | 223 | /* |
| 224 | * enable/disable the multi-driver; This is only valid for output and |
| 225 | * allows the output pin to run as an open collector output. |
| 226 | */ |
| 227 | int __init_or_module at91_set_multi_drive(unsigned pin, int is_on) |
| 228 | { |
| 229 | void __iomem *pio = pin_to_controller(pin); |
| 230 | unsigned mask = pin_to_mask(pin); |
| 231 | |
| 232 | if (!pio) |
| 233 | return -EINVAL; |
| 234 | |
| 235 | __raw_writel(mask, pio + (is_on ? PIO_MDER : PIO_MDDR)); |
| 236 | return 0; |
| 237 | } |
| 238 | EXPORT_SYMBOL(at91_set_multi_drive); |
| 239 | |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 240 | /* |
| 241 | * assuming the pin is muxed as a gpio output, set its value. |
| 242 | */ |
| 243 | int at91_set_gpio_value(unsigned pin, int value) |
| 244 | { |
| 245 | void __iomem *pio = pin_to_controller(pin); |
| 246 | unsigned mask = pin_to_mask(pin); |
| 247 | |
| 248 | if (!pio) |
| 249 | return -EINVAL; |
| 250 | __raw_writel(mask, pio + (value ? PIO_SODR : PIO_CODR)); |
| 251 | return 0; |
| 252 | } |
| 253 | EXPORT_SYMBOL(at91_set_gpio_value); |
| 254 | |
| 255 | |
| 256 | /* |
| 257 | * read the pin's value (works even if it's not muxed as a gpio). |
| 258 | */ |
| 259 | int at91_get_gpio_value(unsigned pin) |
| 260 | { |
| 261 | void __iomem *pio = pin_to_controller(pin); |
| 262 | unsigned mask = pin_to_mask(pin); |
| 263 | u32 pdsr; |
| 264 | |
| 265 | if (!pio) |
| 266 | return -EINVAL; |
| 267 | pdsr = __raw_readl(pio + PIO_PDSR); |
| 268 | return (pdsr & mask) != 0; |
| 269 | } |
| 270 | EXPORT_SYMBOL(at91_get_gpio_value); |
| 271 | |
| 272 | /*--------------------------------------------------------------------------*/ |
| 273 | |
Andrew Victor | 814138f | 2006-06-19 15:26:54 +0100 | [diff] [blame] | 274 | #ifdef CONFIG_PM |
| 275 | |
Andrew Victor | f217383 | 2006-09-27 13:23:00 +0100 | [diff] [blame] | 276 | static u32 wakeups[MAX_GPIO_BANKS]; |
| 277 | static u32 backups[MAX_GPIO_BANKS]; |
Andrew Victor | 814138f | 2006-06-19 15:26:54 +0100 | [diff] [blame] | 278 | |
| 279 | static int gpio_irq_set_wake(unsigned pin, unsigned state) |
| 280 | { |
| 281 | unsigned mask = pin_to_mask(pin); |
Andrew Victor | 3ea163e | 2007-01-09 13:47:29 +0100 | [diff] [blame] | 282 | unsigned bank = (pin - PIN_BASE) / 32; |
Andrew Victor | 814138f | 2006-06-19 15:26:54 +0100 | [diff] [blame] | 283 | |
Andrew Victor | 3ea163e | 2007-01-09 13:47:29 +0100 | [diff] [blame] | 284 | if (unlikely(bank >= MAX_GPIO_BANKS)) |
Andrew Victor | 814138f | 2006-06-19 15:26:54 +0100 | [diff] [blame] | 285 | return -EINVAL; |
| 286 | |
| 287 | if (state) |
Andrew Victor | 3ea163e | 2007-01-09 13:47:29 +0100 | [diff] [blame] | 288 | wakeups[bank] |= mask; |
Andrew Victor | 814138f | 2006-06-19 15:26:54 +0100 | [diff] [blame] | 289 | else |
Andrew Victor | 3ea163e | 2007-01-09 13:47:29 +0100 | [diff] [blame] | 290 | wakeups[bank] &= ~mask; |
| 291 | |
Ryan Mallon | f373e8c | 2009-02-10 21:02:08 +0100 | [diff] [blame^] | 292 | set_irq_wake(gpio_chip[bank].bank->id, state); |
Andrew Victor | 814138f | 2006-06-19 15:26:54 +0100 | [diff] [blame] | 293 | |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | void at91_gpio_suspend(void) |
| 298 | { |
| 299 | int i; |
| 300 | |
Andrew Victor | f217383 | 2006-09-27 13:23:00 +0100 | [diff] [blame] | 301 | for (i = 0; i < gpio_banks; i++) { |
Ryan Mallon | f373e8c | 2009-02-10 21:02:08 +0100 | [diff] [blame^] | 302 | void __iomem *pio = gpio_chip[i].regbase; |
Andrew Victor | 814138f | 2006-06-19 15:26:54 +0100 | [diff] [blame] | 303 | |
David Brownell | e83aff5 | 2008-01-04 18:30:24 +0100 | [diff] [blame] | 304 | backups[i] = __raw_readl(pio + PIO_IMR); |
| 305 | __raw_writel(backups[i], pio + PIO_IDR); |
| 306 | __raw_writel(wakeups[i], pio + PIO_IER); |
Andrew Victor | 814138f | 2006-06-19 15:26:54 +0100 | [diff] [blame] | 307 | |
Andrew Victor | 3ea163e | 2007-01-09 13:47:29 +0100 | [diff] [blame] | 308 | if (!wakeups[i]) |
Ryan Mallon | f373e8c | 2009-02-10 21:02:08 +0100 | [diff] [blame^] | 309 | clk_disable(gpio_chip[i].bank->clock); |
Andrew Victor | 3ea163e | 2007-01-09 13:47:29 +0100 | [diff] [blame] | 310 | else { |
Andrew Victor | 814138f | 2006-06-19 15:26:54 +0100 | [diff] [blame] | 311 | #ifdef CONFIG_PM_DEBUG |
Andrew Victor | 3ea163e | 2007-01-09 13:47:29 +0100 | [diff] [blame] | 312 | printk(KERN_DEBUG "GPIO-%c may wake for %08x\n", 'A'+i, wakeups[i]); |
Andrew Victor | 814138f | 2006-06-19 15:26:54 +0100 | [diff] [blame] | 313 | #endif |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | void at91_gpio_resume(void) |
| 319 | { |
| 320 | int i; |
| 321 | |
Andrew Victor | f217383 | 2006-09-27 13:23:00 +0100 | [diff] [blame] | 322 | for (i = 0; i < gpio_banks; i++) { |
Ryan Mallon | f373e8c | 2009-02-10 21:02:08 +0100 | [diff] [blame^] | 323 | void __iomem *pio = gpio_chip[i].regbase; |
Andrew Victor | 814138f | 2006-06-19 15:26:54 +0100 | [diff] [blame] | 324 | |
Andrew Victor | 3ea163e | 2007-01-09 13:47:29 +0100 | [diff] [blame] | 325 | if (!wakeups[i]) |
Ryan Mallon | f373e8c | 2009-02-10 21:02:08 +0100 | [diff] [blame^] | 326 | clk_enable(gpio_chip[i].bank->clock); |
Andrew Victor | 3ea163e | 2007-01-09 13:47:29 +0100 | [diff] [blame] | 327 | |
David Brownell | e83aff5 | 2008-01-04 18:30:24 +0100 | [diff] [blame] | 328 | __raw_writel(wakeups[i], pio + PIO_IDR); |
| 329 | __raw_writel(backups[i], pio + PIO_IER); |
Andrew Victor | f217383 | 2006-09-27 13:23:00 +0100 | [diff] [blame] | 330 | } |
Andrew Victor | 814138f | 2006-06-19 15:26:54 +0100 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | #else |
| 334 | #define gpio_irq_set_wake NULL |
| 335 | #endif |
| 336 | |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 337 | |
| 338 | /* Several AIC controller irqs are dispatched through this GPIO handler. |
| 339 | * To use any AT91_PIN_* as an externally triggered IRQ, first call |
| 340 | * at91_set_gpio_input() then maybe enable its glitch filter. |
| 341 | * Then just request_irq() with the pin ID; it works like any ARM IRQ |
| 342 | * handler, though it always triggers on rising and falling edges. |
| 343 | * |
| 344 | * Alternatively, certain pins may be used directly as IRQ0..IRQ6 after |
| 345 | * configuring them with at91_set_a_periph() or at91_set_b_periph(). |
| 346 | * IRQ0..IRQ6 should be configurable, e.g. level vs edge triggering. |
| 347 | */ |
| 348 | |
| 349 | static void gpio_irq_mask(unsigned pin) |
| 350 | { |
| 351 | void __iomem *pio = pin_to_controller(pin); |
| 352 | unsigned mask = pin_to_mask(pin); |
| 353 | |
| 354 | if (pio) |
| 355 | __raw_writel(mask, pio + PIO_IDR); |
| 356 | } |
| 357 | |
| 358 | static void gpio_irq_unmask(unsigned pin) |
| 359 | { |
| 360 | void __iomem *pio = pin_to_controller(pin); |
| 361 | unsigned mask = pin_to_mask(pin); |
| 362 | |
| 363 | if (pio) |
| 364 | __raw_writel(mask, pio + PIO_IER); |
| 365 | } |
| 366 | |
| 367 | static int gpio_irq_type(unsigned pin, unsigned type) |
| 368 | { |
David Brownell | e83aff5 | 2008-01-04 18:30:24 +0100 | [diff] [blame] | 369 | switch (type) { |
| 370 | case IRQ_TYPE_NONE: |
| 371 | case IRQ_TYPE_EDGE_BOTH: |
| 372 | return 0; |
| 373 | default: |
| 374 | return -EINVAL; |
| 375 | } |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 376 | } |
| 377 | |
David Brownell | 38c677c | 2006-08-01 22:26:25 +0100 | [diff] [blame] | 378 | static struct irq_chip gpio_irqchip = { |
| 379 | .name = "GPIO", |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 380 | .mask = gpio_irq_mask, |
| 381 | .unmask = gpio_irq_unmask, |
| 382 | .set_type = gpio_irq_type, |
Andrew Victor | 814138f | 2006-06-19 15:26:54 +0100 | [diff] [blame] | 383 | .set_wake = gpio_irq_set_wake, |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 384 | }; |
| 385 | |
Russell King | 10dd5ce | 2006-11-23 11:41:32 +0000 | [diff] [blame] | 386 | static void gpio_irq_handler(unsigned irq, struct irq_desc *desc) |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 387 | { |
| 388 | unsigned pin; |
Russell King | 10dd5ce | 2006-11-23 11:41:32 +0000 | [diff] [blame] | 389 | struct irq_desc *gpio; |
Ryan Mallon | f373e8c | 2009-02-10 21:02:08 +0100 | [diff] [blame^] | 390 | struct at91_gpio_chip *at91_gpio; |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 391 | void __iomem *pio; |
| 392 | u32 isr; |
| 393 | |
Ryan Mallon | f373e8c | 2009-02-10 21:02:08 +0100 | [diff] [blame^] | 394 | at91_gpio = get_irq_chip_data(irq); |
| 395 | pio = at91_gpio->regbase; |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 396 | |
| 397 | /* temporarily mask (level sensitive) parent IRQ */ |
| 398 | desc->chip->ack(irq); |
| 399 | for (;;) { |
David Brownell | e83aff5 | 2008-01-04 18:30:24 +0100 | [diff] [blame] | 400 | /* Reading ISR acks pending (edge triggered) GPIO interrupts. |
| 401 | * When there none are pending, we're finished unless we need |
| 402 | * to process multiple banks (like ID_PIOCDE on sam9263). |
| 403 | */ |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 404 | isr = __raw_readl(pio + PIO_ISR) & __raw_readl(pio + PIO_IMR); |
David Brownell | e83aff5 | 2008-01-04 18:30:24 +0100 | [diff] [blame] | 405 | if (!isr) { |
Ryan Mallon | f373e8c | 2009-02-10 21:02:08 +0100 | [diff] [blame^] | 406 | if (!at91_gpio->next) |
David Brownell | e83aff5 | 2008-01-04 18:30:24 +0100 | [diff] [blame] | 407 | break; |
Ryan Mallon | f373e8c | 2009-02-10 21:02:08 +0100 | [diff] [blame^] | 408 | at91_gpio = at91_gpio->next; |
| 409 | pio = at91_gpio->regbase; |
David Brownell | e83aff5 | 2008-01-04 18:30:24 +0100 | [diff] [blame] | 410 | continue; |
| 411 | } |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 412 | |
Ryan Mallon | f373e8c | 2009-02-10 21:02:08 +0100 | [diff] [blame^] | 413 | pin = at91_gpio->chip.base; |
David Brownell | 085eefb | 2008-10-21 23:34:23 +0100 | [diff] [blame] | 414 | gpio = &irq_desc[pin]; |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 415 | |
| 416 | while (isr) { |
Andrew Victor | abbea71 | 2006-02-24 22:27:50 +0000 | [diff] [blame] | 417 | if (isr & 1) { |
Thomas Gleixner | 07d265d | 2006-07-01 23:01:50 +0100 | [diff] [blame] | 418 | if (unlikely(gpio->depth)) { |
Andrew Victor | abbea71 | 2006-02-24 22:27:50 +0000 | [diff] [blame] | 419 | /* |
| 420 | * The core ARM interrupt handler lazily disables IRQs so |
| 421 | * another IRQ must be generated before it actually gets |
| 422 | * here to be disabled on the GPIO controller. |
| 423 | */ |
| 424 | gpio_irq_mask(pin); |
| 425 | } |
| 426 | else |
Dmitry Baryshkov | d8aa025 | 2008-10-09 13:36:24 +0100 | [diff] [blame] | 427 | generic_handle_irq(pin); |
Andrew Victor | abbea71 | 2006-02-24 22:27:50 +0000 | [diff] [blame] | 428 | } |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 429 | pin++; |
| 430 | gpio++; |
| 431 | isr >>= 1; |
| 432 | } |
| 433 | } |
| 434 | desc->chip->unmask(irq); |
| 435 | /* now it may re-trigger */ |
| 436 | } |
| 437 | |
Andrew Victor | f217383 | 2006-09-27 13:23:00 +0100 | [diff] [blame] | 438 | /*--------------------------------------------------------------------------*/ |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 439 | |
David Brownell | 37aca70 | 2008-03-05 00:08:29 +0100 | [diff] [blame] | 440 | /* This lock class tells lockdep that GPIO irqs are in a different |
| 441 | * category than their parents, so it won't report false recursion. |
| 442 | */ |
| 443 | static struct lock_class_key gpio_lock_class; |
| 444 | |
Andrew Victor | f217383 | 2006-09-27 13:23:00 +0100 | [diff] [blame] | 445 | /* |
| 446 | * Called from the processor-specific init to enable GPIO interrupt support. |
| 447 | */ |
| 448 | void __init at91_gpio_irq_setup(void) |
| 449 | { |
David Brownell | e83aff5 | 2008-01-04 18:30:24 +0100 | [diff] [blame] | 450 | unsigned pioc, pin; |
Ryan Mallon | f373e8c | 2009-02-10 21:02:08 +0100 | [diff] [blame^] | 451 | struct at91_gpio_chip *this, *prev; |
Andrew Victor | f217383 | 2006-09-27 13:23:00 +0100 | [diff] [blame] | 452 | |
Ryan Mallon | f373e8c | 2009-02-10 21:02:08 +0100 | [diff] [blame^] | 453 | for (pioc = 0, pin = PIN_BASE, this = gpio_chip, prev = NULL; |
David Brownell | e83aff5 | 2008-01-04 18:30:24 +0100 | [diff] [blame] | 454 | pioc++ < gpio_banks; |
| 455 | prev = this, this++) { |
Ryan Mallon | f373e8c | 2009-02-10 21:02:08 +0100 | [diff] [blame^] | 456 | unsigned id = this->bank->id; |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 457 | unsigned i; |
| 458 | |
David Brownell | e83aff5 | 2008-01-04 18:30:24 +0100 | [diff] [blame] | 459 | /* enable PIO controller's clock */ |
Ryan Mallon | f373e8c | 2009-02-10 21:02:08 +0100 | [diff] [blame^] | 460 | clk_enable(this->bank->clock); |
Andrew Victor | f217383 | 2006-09-27 13:23:00 +0100 | [diff] [blame] | 461 | |
David Brownell | e83aff5 | 2008-01-04 18:30:24 +0100 | [diff] [blame] | 462 | __raw_writel(~0, this->regbase + PIO_IDR); |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 463 | |
Ryan Mallon | f373e8c | 2009-02-10 21:02:08 +0100 | [diff] [blame^] | 464 | for (i = 0, pin = this->chip.base; i < 32; i++, pin++) { |
David Brownell | 37aca70 | 2008-03-05 00:08:29 +0100 | [diff] [blame] | 465 | lockdep_set_class(&irq_desc[pin].lock, &gpio_lock_class); |
| 466 | |
Andrew Victor | 814138f | 2006-06-19 15:26:54 +0100 | [diff] [blame] | 467 | /* |
| 468 | * Can use the "simple" and not "edge" handler since it's |
Robert P. J. Day | 3a4fa0a | 2007-10-19 23:10:43 +0200 | [diff] [blame] | 469 | * shorter, and the AIC handles interrupts sanely. |
Andrew Victor | 814138f | 2006-06-19 15:26:54 +0100 | [diff] [blame] | 470 | */ |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 471 | set_irq_chip(pin, &gpio_irqchip); |
Russell King | 10dd5ce | 2006-11-23 11:41:32 +0000 | [diff] [blame] | 472 | set_irq_handler(pin, handle_simple_irq); |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 473 | set_irq_flags(pin, IRQF_VALID); |
| 474 | } |
| 475 | |
David Brownell | e83aff5 | 2008-01-04 18:30:24 +0100 | [diff] [blame] | 476 | /* The toplevel handler handles one bank of GPIOs, except |
| 477 | * AT91SAM9263_ID_PIOCDE handles three... PIOC is first in |
| 478 | * the list, so we only set up that handler. |
| 479 | */ |
| 480 | if (prev && prev->next == this) |
| 481 | continue; |
| 482 | |
| 483 | set_irq_chip_data(id, this); |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 484 | set_irq_chained_handler(id, gpio_irq_handler); |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 485 | } |
Andrew Victor | f217383 | 2006-09-27 13:23:00 +0100 | [diff] [blame] | 486 | pr_info("AT91: %d gpio irqs in %d banks\n", pin - PIN_BASE, gpio_banks); |
| 487 | } |
| 488 | |
Ryan Mallon | f373e8c | 2009-02-10 21:02:08 +0100 | [diff] [blame^] | 489 | /* gpiolib support */ |
| 490 | static int at91_gpiolib_direction_input(struct gpio_chip *chip, |
| 491 | unsigned offset) |
| 492 | { |
| 493 | struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip); |
| 494 | void __iomem *pio = at91_gpio->regbase; |
| 495 | unsigned mask = 1 << offset; |
| 496 | |
| 497 | __raw_writel(mask, pio + PIO_ODR); |
| 498 | return 0; |
| 499 | } |
| 500 | |
| 501 | static int at91_gpiolib_direction_output(struct gpio_chip *chip, |
| 502 | unsigned offset, int val) |
| 503 | { |
| 504 | struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip); |
| 505 | void __iomem *pio = at91_gpio->regbase; |
| 506 | unsigned mask = 1 << offset; |
| 507 | |
| 508 | __raw_writel(mask, pio + (val ? PIO_SODR : PIO_CODR)); |
| 509 | __raw_writel(mask, pio + PIO_OER); |
| 510 | return 0; |
| 511 | } |
| 512 | |
| 513 | static int at91_gpiolib_get(struct gpio_chip *chip, unsigned offset) |
| 514 | { |
| 515 | struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip); |
| 516 | void __iomem *pio = at91_gpio->regbase; |
| 517 | unsigned mask = 1 << offset; |
| 518 | u32 pdsr; |
| 519 | |
| 520 | pdsr = __raw_readl(pio + PIO_PDSR); |
| 521 | return (pdsr & mask) != 0; |
| 522 | } |
| 523 | |
| 524 | static void at91_gpiolib_set(struct gpio_chip *chip, unsigned offset, int val) |
| 525 | { |
| 526 | struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip); |
| 527 | void __iomem *pio = at91_gpio->regbase; |
| 528 | unsigned mask = 1 << offset; |
| 529 | |
| 530 | __raw_writel(mask, pio + (val ? PIO_SODR : PIO_CODR)); |
| 531 | } |
| 532 | |
| 533 | static int at91_gpiolib_request(struct gpio_chip *chip, unsigned offset) |
| 534 | { |
| 535 | unsigned pin = chip->base + offset; |
| 536 | void __iomem *pio = pin_to_controller(pin); |
| 537 | unsigned mask = pin_to_mask(pin); |
| 538 | |
| 539 | /* Cannot request GPIOs that are in alternate function mode */ |
| 540 | if (!(__raw_readl(pio + PIO_PSR) & mask)) |
| 541 | return -EPERM; |
| 542 | |
| 543 | return 0; |
| 544 | } |
| 545 | |
| 546 | static void at91_gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip) |
| 547 | { |
| 548 | int i; |
| 549 | |
| 550 | for (i = 0; i < chip->ngpio; i++) { |
| 551 | unsigned pin = chip->base + i; |
| 552 | void __iomem *pio = pin_to_controller(pin); |
| 553 | unsigned mask = pin_to_mask(pin); |
| 554 | const char *gpio_label; |
| 555 | |
| 556 | gpio_label = gpiochip_is_requested(chip, i); |
| 557 | if (gpio_label) { |
| 558 | seq_printf(s, "[%s] GPIO%s%d: ", |
| 559 | gpio_label, chip->label, i); |
| 560 | if (__raw_readl(pio + PIO_PSR) & mask) |
| 561 | seq_printf(s, "[gpio] %s\n", |
| 562 | at91_get_gpio_value(pin) ? |
| 563 | "set" : "clear"); |
| 564 | else |
| 565 | seq_printf(s, "[periph %s]\n", |
| 566 | __raw_readl(pio + PIO_ABSR) & |
| 567 | mask ? "B" : "A"); |
| 568 | } |
| 569 | } |
| 570 | } |
| 571 | |
Andrew Victor | f217383 | 2006-09-27 13:23:00 +0100 | [diff] [blame] | 572 | /* |
| 573 | * Called from the processor-specific init to enable GPIO pin support. |
| 574 | */ |
| 575 | void __init at91_gpio_init(struct at91_gpio_bank *data, int nr_banks) |
| 576 | { |
David Brownell | e83aff5 | 2008-01-04 18:30:24 +0100 | [diff] [blame] | 577 | unsigned i; |
Ryan Mallon | f373e8c | 2009-02-10 21:02:08 +0100 | [diff] [blame^] | 578 | struct at91_gpio_chip *at91_gpio, *last = NULL; |
David Brownell | e83aff5 | 2008-01-04 18:30:24 +0100 | [diff] [blame] | 579 | |
Andrew Victor | f217383 | 2006-09-27 13:23:00 +0100 | [diff] [blame] | 580 | BUG_ON(nr_banks > MAX_GPIO_BANKS); |
| 581 | |
Andrew Victor | f217383 | 2006-09-27 13:23:00 +0100 | [diff] [blame] | 582 | gpio_banks = nr_banks; |
David Brownell | e83aff5 | 2008-01-04 18:30:24 +0100 | [diff] [blame] | 583 | |
Ryan Mallon | f373e8c | 2009-02-10 21:02:08 +0100 | [diff] [blame^] | 584 | for (i = 0; i < nr_banks; i++) { |
| 585 | at91_gpio = &gpio_chip[i]; |
| 586 | |
| 587 | at91_gpio->bank = &data[i]; |
| 588 | at91_gpio->chip.base = PIN_BASE + i * 32; |
| 589 | at91_gpio->regbase = at91_gpio->bank->offset + |
| 590 | (void __iomem *)AT91_VA_BASE_SYS; |
David Brownell | e83aff5 | 2008-01-04 18:30:24 +0100 | [diff] [blame] | 591 | |
| 592 | /* AT91SAM9263_ID_PIOCDE groups PIOC, PIOD, PIOE */ |
Ryan Mallon | f373e8c | 2009-02-10 21:02:08 +0100 | [diff] [blame^] | 593 | if (last && last->bank->id == at91_gpio->bank->id) |
| 594 | last->next = at91_gpio; |
| 595 | last = at91_gpio; |
| 596 | |
| 597 | gpiochip_add(&at91_gpio->chip); |
David Brownell | e83aff5 | 2008-01-04 18:30:24 +0100 | [diff] [blame] | 598 | } |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 599 | } |