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> |
Nicolas Ferre | b134ce8 | 2012-02-11 15:56:01 +0100 | [diff] [blame] | 14 | #include <linux/device.h> |
Russell King | 2f8163b | 2011-07-26 10:53:52 +0100 | [diff] [blame] | 15 | #include <linux/gpio.h> |
Thomas Gleixner | 07d265d | 2006-07-01 23:01:50 +0100 | [diff] [blame] | 16 | #include <linux/interrupt.h> |
| 17 | #include <linux/irq.h> |
Andrew Victor | b66545e | 2007-11-23 16:09:10 +0100 | [diff] [blame] | 18 | #include <linux/debugfs.h> |
| 19 | #include <linux/seq_file.h> |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 20 | #include <linux/kernel.h> |
| 21 | #include <linux/list.h> |
| 22 | #include <linux/module.h> |
Russell King | fced80c | 2008-09-06 12:10:45 +0100 | [diff] [blame] | 23 | #include <linux/io.h> |
Nicolas Ferre | 21f8187 | 2012-02-11 15:41:40 +0100 | [diff] [blame] | 24 | #include <linux/irqdomain.h> |
| 25 | #include <linux/of_address.h> |
| 26 | #include <linux/of_irq.h> |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 27 | |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 28 | #include <mach/hardware.h> |
| 29 | #include <mach/at91_pio.h> |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 30 | |
Andrew Victor | f217383 | 2006-09-27 13:23:00 +0100 | [diff] [blame] | 31 | #include "generic.h" |
| 32 | |
Ryan Mallon | f373e8c | 2009-02-10 21:02:08 +0100 | [diff] [blame] | 33 | struct at91_gpio_chip { |
| 34 | struct gpio_chip chip; |
| 35 | struct at91_gpio_chip *next; /* Bank sharing same clock */ |
Nicolas Ferre | 4340cde | 2012-02-11 15:28:08 +0100 | [diff] [blame] | 36 | int pioc_hwirq; /* PIO bank interrupt identifier on AIC */ |
Nicolas Ferre | 21f8187 | 2012-02-11 15:41:40 +0100 | [diff] [blame] | 37 | int pioc_idx; /* PIO bank index */ |
Nicolas Ferre | 4340cde | 2012-02-11 15:28:08 +0100 | [diff] [blame] | 38 | void __iomem *regbase; /* PIO bank virtual address */ |
Jean-Christophe PLAGNIOL-VILLARD | 619d4a4 | 2011-11-13 13:00:58 +0800 | [diff] [blame] | 39 | struct clk *clock; /* associated clock */ |
Nicolas Ferre | 21f8187 | 2012-02-11 15:41:40 +0100 | [diff] [blame] | 40 | struct irq_domain *domain; /* associated irq domain */ |
Ryan Mallon | f373e8c | 2009-02-10 21:02:08 +0100 | [diff] [blame] | 41 | }; |
Andrew Victor | f217383 | 2006-09-27 13:23:00 +0100 | [diff] [blame] | 42 | |
Ryan Mallon | f373e8c | 2009-02-10 21:02:08 +0100 | [diff] [blame] | 43 | #define to_at91_gpio_chip(c) container_of(c, struct at91_gpio_chip, chip) |
| 44 | |
| 45 | static void at91_gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip); |
| 46 | static void at91_gpiolib_set(struct gpio_chip *chip, unsigned offset, int val); |
| 47 | static int at91_gpiolib_get(struct gpio_chip *chip, unsigned offset); |
| 48 | static int at91_gpiolib_direction_output(struct gpio_chip *chip, |
| 49 | unsigned offset, int val); |
| 50 | static int at91_gpiolib_direction_input(struct gpio_chip *chip, |
| 51 | unsigned offset); |
Nicolas Ferre | b134ce8 | 2012-02-11 15:56:01 +0100 | [diff] [blame] | 52 | static int at91_gpiolib_to_irq(struct gpio_chip *chip, unsigned offset); |
Ryan Mallon | f373e8c | 2009-02-10 21:02:08 +0100 | [diff] [blame] | 53 | |
Nicolas Ferre | 7530cd9 | 2011-12-08 15:35:22 +0100 | [diff] [blame^] | 54 | #define AT91_GPIO_CHIP(name, nr_gpio) \ |
Ryan Mallon | f373e8c | 2009-02-10 21:02:08 +0100 | [diff] [blame] | 55 | { \ |
| 56 | .chip = { \ |
| 57 | .label = name, \ |
Ryan Mallon | f373e8c | 2009-02-10 21:02:08 +0100 | [diff] [blame] | 58 | .direction_input = at91_gpiolib_direction_input, \ |
| 59 | .direction_output = at91_gpiolib_direction_output, \ |
| 60 | .get = at91_gpiolib_get, \ |
| 61 | .set = at91_gpiolib_set, \ |
| 62 | .dbg_show = at91_gpiolib_dbg_show, \ |
Nicolas Ferre | b134ce8 | 2012-02-11 15:56:01 +0100 | [diff] [blame] | 63 | .to_irq = at91_gpiolib_to_irq, \ |
Ryan Mallon | f373e8c | 2009-02-10 21:02:08 +0100 | [diff] [blame] | 64 | .ngpio = nr_gpio, \ |
| 65 | }, \ |
| 66 | } |
| 67 | |
| 68 | static struct at91_gpio_chip gpio_chip[] = { |
Nicolas Ferre | 7530cd9 | 2011-12-08 15:35:22 +0100 | [diff] [blame^] | 69 | AT91_GPIO_CHIP("pioA", 32), |
| 70 | AT91_GPIO_CHIP("pioB", 32), |
| 71 | AT91_GPIO_CHIP("pioC", 32), |
| 72 | AT91_GPIO_CHIP("pioD", 32), |
| 73 | AT91_GPIO_CHIP("pioE", 32), |
Ryan Mallon | f373e8c | 2009-02-10 21:02:08 +0100 | [diff] [blame] | 74 | }; |
| 75 | |
Andrew Victor | f217383 | 2006-09-27 13:23:00 +0100 | [diff] [blame] | 76 | static int gpio_banks; |
| 77 | |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 78 | static inline void __iomem *pin_to_controller(unsigned pin) |
| 79 | { |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 80 | pin /= 32; |
Andrew Victor | f217383 | 2006-09-27 13:23:00 +0100 | [diff] [blame] | 81 | if (likely(pin < gpio_banks)) |
Ryan Mallon | f373e8c | 2009-02-10 21:02:08 +0100 | [diff] [blame] | 82 | return gpio_chip[pin].regbase; |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 83 | |
| 84 | return NULL; |
| 85 | } |
| 86 | |
| 87 | static inline unsigned pin_to_mask(unsigned pin) |
| 88 | { |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 89 | return 1 << (pin % 32); |
| 90 | } |
| 91 | |
| 92 | |
| 93 | /*--------------------------------------------------------------------------*/ |
| 94 | |
| 95 | /* Not all hardware capabilities are exposed through these calls; they |
| 96 | * only encapsulate the most common features and modes. (So if you |
| 97 | * want to change signals in groups, do it directly.) |
| 98 | * |
| 99 | * Bootloaders will usually handle some of the pin multiplexing setup. |
| 100 | * The intent is certainly that by the time Linux is fully booted, all |
| 101 | * pins should have been fully initialized. These setup calls should |
| 102 | * only be used by board setup routines, or possibly in driver probe(). |
| 103 | * |
| 104 | * For bootloaders doing all that setup, these calls could be inlined |
| 105 | * as NOPs so Linux won't duplicate any setup code |
| 106 | */ |
| 107 | |
| 108 | |
| 109 | /* |
David Brownell | a31c4ee | 2007-02-12 00:53:13 -0800 | [diff] [blame] | 110 | * mux the pin to the "GPIO" peripheral role. |
| 111 | */ |
| 112 | int __init_or_module at91_set_GPIO_periph(unsigned pin, int use_pullup) |
| 113 | { |
| 114 | void __iomem *pio = pin_to_controller(pin); |
| 115 | unsigned mask = pin_to_mask(pin); |
| 116 | |
| 117 | if (!pio) |
| 118 | return -EINVAL; |
| 119 | __raw_writel(mask, pio + PIO_IDR); |
| 120 | __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR)); |
| 121 | __raw_writel(mask, pio + PIO_PER); |
| 122 | return 0; |
| 123 | } |
| 124 | EXPORT_SYMBOL(at91_set_GPIO_periph); |
| 125 | |
| 126 | |
| 127 | /* |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 128 | * mux the pin to the "A" internal peripheral role. |
| 129 | */ |
| 130 | int __init_or_module at91_set_A_periph(unsigned pin, int use_pullup) |
| 131 | { |
| 132 | void __iomem *pio = pin_to_controller(pin); |
| 133 | unsigned mask = pin_to_mask(pin); |
| 134 | |
| 135 | if (!pio) |
| 136 | return -EINVAL; |
| 137 | |
| 138 | __raw_writel(mask, pio + PIO_IDR); |
| 139 | __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR)); |
| 140 | __raw_writel(mask, pio + PIO_ASR); |
| 141 | __raw_writel(mask, pio + PIO_PDR); |
| 142 | return 0; |
| 143 | } |
| 144 | EXPORT_SYMBOL(at91_set_A_periph); |
| 145 | |
| 146 | |
| 147 | /* |
| 148 | * mux the pin to the "B" internal peripheral role. |
| 149 | */ |
| 150 | int __init_or_module at91_set_B_periph(unsigned pin, int use_pullup) |
| 151 | { |
| 152 | void __iomem *pio = pin_to_controller(pin); |
| 153 | unsigned mask = pin_to_mask(pin); |
| 154 | |
| 155 | if (!pio) |
| 156 | return -EINVAL; |
| 157 | |
| 158 | __raw_writel(mask, pio + PIO_IDR); |
| 159 | __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR)); |
| 160 | __raw_writel(mask, pio + PIO_BSR); |
| 161 | __raw_writel(mask, pio + PIO_PDR); |
| 162 | return 0; |
| 163 | } |
| 164 | EXPORT_SYMBOL(at91_set_B_periph); |
| 165 | |
| 166 | |
| 167 | /* |
| 168 | * mux the pin to the gpio controller (instead of "A" or "B" peripheral), and |
| 169 | * configure it for an input. |
| 170 | */ |
| 171 | int __init_or_module at91_set_gpio_input(unsigned pin, int use_pullup) |
| 172 | { |
| 173 | void __iomem *pio = pin_to_controller(pin); |
| 174 | unsigned mask = pin_to_mask(pin); |
| 175 | |
| 176 | if (!pio) |
| 177 | return -EINVAL; |
| 178 | |
| 179 | __raw_writel(mask, pio + PIO_IDR); |
| 180 | __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR)); |
| 181 | __raw_writel(mask, pio + PIO_ODR); |
| 182 | __raw_writel(mask, pio + PIO_PER); |
| 183 | return 0; |
| 184 | } |
| 185 | EXPORT_SYMBOL(at91_set_gpio_input); |
| 186 | |
| 187 | |
| 188 | /* |
| 189 | * mux the pin to the gpio controller (instead of "A" or "B" peripheral), |
| 190 | * and configure it for an output. |
| 191 | */ |
| 192 | int __init_or_module at91_set_gpio_output(unsigned pin, int value) |
| 193 | { |
| 194 | void __iomem *pio = pin_to_controller(pin); |
| 195 | unsigned mask = pin_to_mask(pin); |
| 196 | |
| 197 | if (!pio) |
| 198 | return -EINVAL; |
| 199 | |
| 200 | __raw_writel(mask, pio + PIO_IDR); |
| 201 | __raw_writel(mask, pio + PIO_PUDR); |
| 202 | __raw_writel(mask, pio + (value ? PIO_SODR : PIO_CODR)); |
| 203 | __raw_writel(mask, pio + PIO_OER); |
| 204 | __raw_writel(mask, pio + PIO_PER); |
| 205 | return 0; |
| 206 | } |
| 207 | EXPORT_SYMBOL(at91_set_gpio_output); |
| 208 | |
| 209 | |
| 210 | /* |
| 211 | * enable/disable the glitch filter; mostly used with IRQ handling. |
| 212 | */ |
| 213 | int __init_or_module at91_set_deglitch(unsigned pin, int is_on) |
| 214 | { |
| 215 | void __iomem *pio = pin_to_controller(pin); |
| 216 | unsigned mask = pin_to_mask(pin); |
| 217 | |
| 218 | if (!pio) |
| 219 | return -EINVAL; |
| 220 | __raw_writel(mask, pio + (is_on ? PIO_IFER : PIO_IFDR)); |
| 221 | return 0; |
| 222 | } |
| 223 | EXPORT_SYMBOL(at91_set_deglitch); |
| 224 | |
Andrew Victor | df666b9 | 2006-02-22 21:23:35 +0000 | [diff] [blame] | 225 | /* |
| 226 | * enable/disable the multi-driver; This is only valid for output and |
| 227 | * allows the output pin to run as an open collector output. |
| 228 | */ |
| 229 | int __init_or_module at91_set_multi_drive(unsigned pin, int is_on) |
| 230 | { |
| 231 | void __iomem *pio = pin_to_controller(pin); |
| 232 | unsigned mask = pin_to_mask(pin); |
| 233 | |
| 234 | if (!pio) |
| 235 | return -EINVAL; |
| 236 | |
| 237 | __raw_writel(mask, pio + (is_on ? PIO_MDER : PIO_MDDR)); |
| 238 | return 0; |
| 239 | } |
| 240 | EXPORT_SYMBOL(at91_set_multi_drive); |
| 241 | |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 242 | /* |
| 243 | * assuming the pin is muxed as a gpio output, set its value. |
| 244 | */ |
| 245 | int at91_set_gpio_value(unsigned pin, int value) |
| 246 | { |
| 247 | void __iomem *pio = pin_to_controller(pin); |
| 248 | unsigned mask = pin_to_mask(pin); |
| 249 | |
| 250 | if (!pio) |
| 251 | return -EINVAL; |
| 252 | __raw_writel(mask, pio + (value ? PIO_SODR : PIO_CODR)); |
| 253 | return 0; |
| 254 | } |
| 255 | EXPORT_SYMBOL(at91_set_gpio_value); |
| 256 | |
| 257 | |
| 258 | /* |
| 259 | * read the pin's value (works even if it's not muxed as a gpio). |
| 260 | */ |
| 261 | int at91_get_gpio_value(unsigned pin) |
| 262 | { |
| 263 | void __iomem *pio = pin_to_controller(pin); |
| 264 | unsigned mask = pin_to_mask(pin); |
| 265 | u32 pdsr; |
| 266 | |
| 267 | if (!pio) |
| 268 | return -EINVAL; |
| 269 | pdsr = __raw_readl(pio + PIO_PDSR); |
| 270 | return (pdsr & mask) != 0; |
| 271 | } |
| 272 | EXPORT_SYMBOL(at91_get_gpio_value); |
| 273 | |
| 274 | /*--------------------------------------------------------------------------*/ |
| 275 | |
Andrew Victor | 814138f | 2006-06-19 15:26:54 +0100 | [diff] [blame] | 276 | #ifdef CONFIG_PM |
| 277 | |
Andrew Victor | f217383 | 2006-09-27 13:23:00 +0100 | [diff] [blame] | 278 | static u32 wakeups[MAX_GPIO_BANKS]; |
| 279 | static u32 backups[MAX_GPIO_BANKS]; |
Andrew Victor | 814138f | 2006-06-19 15:26:54 +0100 | [diff] [blame] | 280 | |
Lennert Buytenhek | da0f940 | 2010-11-29 10:26:19 +0100 | [diff] [blame] | 281 | static int gpio_irq_set_wake(struct irq_data *d, unsigned state) |
Andrew Victor | 814138f | 2006-06-19 15:26:54 +0100 | [diff] [blame] | 282 | { |
Nicolas Ferre | 21f8187 | 2012-02-11 15:41:40 +0100 | [diff] [blame] | 283 | struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d); |
| 284 | unsigned mask = 1 << d->hwirq; |
| 285 | unsigned bank = at91_gpio->pioc_idx; |
Andrew Victor | 814138f | 2006-06-19 15:26:54 +0100 | [diff] [blame] | 286 | |
Andrew Victor | 3ea163e | 2007-01-09 13:47:29 +0100 | [diff] [blame] | 287 | if (unlikely(bank >= MAX_GPIO_BANKS)) |
Andrew Victor | 814138f | 2006-06-19 15:26:54 +0100 | [diff] [blame] | 288 | return -EINVAL; |
| 289 | |
| 290 | if (state) |
Andrew Victor | 3ea163e | 2007-01-09 13:47:29 +0100 | [diff] [blame] | 291 | wakeups[bank] |= mask; |
Andrew Victor | 814138f | 2006-06-19 15:26:54 +0100 | [diff] [blame] | 292 | else |
Andrew Victor | 3ea163e | 2007-01-09 13:47:29 +0100 | [diff] [blame] | 293 | wakeups[bank] &= ~mask; |
| 294 | |
Nicolas Ferre | 4340cde | 2012-02-11 15:28:08 +0100 | [diff] [blame] | 295 | irq_set_irq_wake(gpio_chip[bank].pioc_hwirq, state); |
Andrew Victor | 814138f | 2006-06-19 15:26:54 +0100 | [diff] [blame] | 296 | |
| 297 | return 0; |
| 298 | } |
| 299 | |
| 300 | void at91_gpio_suspend(void) |
| 301 | { |
| 302 | int i; |
| 303 | |
Andrew Victor | f217383 | 2006-09-27 13:23:00 +0100 | [diff] [blame] | 304 | for (i = 0; i < gpio_banks; i++) { |
Ryan Mallon | f373e8c | 2009-02-10 21:02:08 +0100 | [diff] [blame] | 305 | void __iomem *pio = gpio_chip[i].regbase; |
Andrew Victor | 814138f | 2006-06-19 15:26:54 +0100 | [diff] [blame] | 306 | |
David Brownell | e83aff5 | 2008-01-04 18:30:24 +0100 | [diff] [blame] | 307 | backups[i] = __raw_readl(pio + PIO_IMR); |
| 308 | __raw_writel(backups[i], pio + PIO_IDR); |
| 309 | __raw_writel(wakeups[i], pio + PIO_IER); |
Andrew Victor | 814138f | 2006-06-19 15:26:54 +0100 | [diff] [blame] | 310 | |
Nicolas Ferre | 21f8187 | 2012-02-11 15:41:40 +0100 | [diff] [blame] | 311 | if (!wakeups[i]) { |
| 312 | clk_unprepare(gpio_chip[i].clock); |
Jean-Christophe PLAGNIOL-VILLARD | 619d4a4 | 2011-11-13 13:00:58 +0800 | [diff] [blame] | 313 | clk_disable(gpio_chip[i].clock); |
Nicolas Ferre | 21f8187 | 2012-02-11 15:41:40 +0100 | [diff] [blame] | 314 | } else { |
Andrew Victor | 814138f | 2006-06-19 15:26:54 +0100 | [diff] [blame] | 315 | #ifdef CONFIG_PM_DEBUG |
Andrew Victor | 3ea163e | 2007-01-09 13:47:29 +0100 | [diff] [blame] | 316 | 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] | 317 | #endif |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | void at91_gpio_resume(void) |
| 323 | { |
| 324 | int i; |
| 325 | |
Andrew Victor | f217383 | 2006-09-27 13:23:00 +0100 | [diff] [blame] | 326 | for (i = 0; i < gpio_banks; i++) { |
Ryan Mallon | f373e8c | 2009-02-10 21:02:08 +0100 | [diff] [blame] | 327 | void __iomem *pio = gpio_chip[i].regbase; |
Andrew Victor | 814138f | 2006-06-19 15:26:54 +0100 | [diff] [blame] | 328 | |
Nicolas Ferre | 21f8187 | 2012-02-11 15:41:40 +0100 | [diff] [blame] | 329 | if (!wakeups[i]) { |
| 330 | if (clk_prepare(gpio_chip[i].clock) == 0) |
| 331 | clk_enable(gpio_chip[i].clock); |
| 332 | } |
Andrew Victor | 3ea163e | 2007-01-09 13:47:29 +0100 | [diff] [blame] | 333 | |
David Brownell | e83aff5 | 2008-01-04 18:30:24 +0100 | [diff] [blame] | 334 | __raw_writel(wakeups[i], pio + PIO_IDR); |
| 335 | __raw_writel(backups[i], pio + PIO_IER); |
Andrew Victor | f217383 | 2006-09-27 13:23:00 +0100 | [diff] [blame] | 336 | } |
Andrew Victor | 814138f | 2006-06-19 15:26:54 +0100 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | #else |
| 340 | #define gpio_irq_set_wake NULL |
| 341 | #endif |
| 342 | |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 343 | |
| 344 | /* Several AIC controller irqs are dispatched through this GPIO handler. |
| 345 | * To use any AT91_PIN_* as an externally triggered IRQ, first call |
| 346 | * at91_set_gpio_input() then maybe enable its glitch filter. |
| 347 | * Then just request_irq() with the pin ID; it works like any ARM IRQ |
| 348 | * handler, though it always triggers on rising and falling edges. |
| 349 | * |
| 350 | * Alternatively, certain pins may be used directly as IRQ0..IRQ6 after |
| 351 | * configuring them with at91_set_a_periph() or at91_set_b_periph(). |
| 352 | * IRQ0..IRQ6 should be configurable, e.g. level vs edge triggering. |
| 353 | */ |
| 354 | |
Lennert Buytenhek | da0f940 | 2010-11-29 10:26:19 +0100 | [diff] [blame] | 355 | static void gpio_irq_mask(struct irq_data *d) |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 356 | { |
Nicolas Ferre | 21f8187 | 2012-02-11 15:41:40 +0100 | [diff] [blame] | 357 | struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d); |
| 358 | void __iomem *pio = at91_gpio->regbase; |
| 359 | unsigned mask = 1 << d->hwirq; |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 360 | |
| 361 | if (pio) |
| 362 | __raw_writel(mask, pio + PIO_IDR); |
| 363 | } |
| 364 | |
Lennert Buytenhek | da0f940 | 2010-11-29 10:26:19 +0100 | [diff] [blame] | 365 | static void gpio_irq_unmask(struct irq_data *d) |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 366 | { |
Nicolas Ferre | 21f8187 | 2012-02-11 15:41:40 +0100 | [diff] [blame] | 367 | struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d); |
| 368 | void __iomem *pio = at91_gpio->regbase; |
| 369 | unsigned mask = 1 << d->hwirq; |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 370 | |
| 371 | if (pio) |
| 372 | __raw_writel(mask, pio + PIO_IER); |
| 373 | } |
| 374 | |
Lennert Buytenhek | da0f940 | 2010-11-29 10:26:19 +0100 | [diff] [blame] | 375 | static int gpio_irq_type(struct irq_data *d, unsigned type) |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 376 | { |
David Brownell | e83aff5 | 2008-01-04 18:30:24 +0100 | [diff] [blame] | 377 | switch (type) { |
| 378 | case IRQ_TYPE_NONE: |
| 379 | case IRQ_TYPE_EDGE_BOTH: |
| 380 | return 0; |
| 381 | default: |
| 382 | return -EINVAL; |
| 383 | } |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 384 | } |
| 385 | |
David Brownell | 38c677c | 2006-08-01 22:26:25 +0100 | [diff] [blame] | 386 | static struct irq_chip gpio_irqchip = { |
| 387 | .name = "GPIO", |
Thomas Gleixner | ac93cdb | 2011-03-24 12:48:18 +0100 | [diff] [blame] | 388 | .irq_disable = gpio_irq_mask, |
Lennert Buytenhek | da0f940 | 2010-11-29 10:26:19 +0100 | [diff] [blame] | 389 | .irq_mask = gpio_irq_mask, |
| 390 | .irq_unmask = gpio_irq_unmask, |
| 391 | .irq_set_type = gpio_irq_type, |
| 392 | .irq_set_wake = gpio_irq_set_wake, |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 393 | }; |
| 394 | |
Russell King | 10dd5ce | 2006-11-23 11:41:32 +0000 | [diff] [blame] | 395 | static void gpio_irq_handler(unsigned irq, struct irq_desc *desc) |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 396 | { |
Nicolas Ferre | 21f8187 | 2012-02-11 15:41:40 +0100 | [diff] [blame] | 397 | unsigned virq; |
Thomas Gleixner | ac93cdb | 2011-03-24 12:48:18 +0100 | [diff] [blame] | 398 | struct irq_data *idata = irq_desc_get_irq_data(desc); |
| 399 | struct irq_chip *chip = irq_data_get_irq_chip(idata); |
| 400 | struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(idata); |
| 401 | void __iomem *pio = at91_gpio->regbase; |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 402 | u32 isr; |
| 403 | |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 404 | /* temporarily mask (level sensitive) parent IRQ */ |
Thomas Gleixner | ac93cdb | 2011-03-24 12:48:18 +0100 | [diff] [blame] | 405 | chip->irq_ack(idata); |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 406 | for (;;) { |
David Brownell | e83aff5 | 2008-01-04 18:30:24 +0100 | [diff] [blame] | 407 | /* Reading ISR acks pending (edge triggered) GPIO interrupts. |
| 408 | * When there none are pending, we're finished unless we need |
| 409 | * to process multiple banks (like ID_PIOCDE on sam9263). |
| 410 | */ |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 411 | isr = __raw_readl(pio + PIO_ISR) & __raw_readl(pio + PIO_IMR); |
David Brownell | e83aff5 | 2008-01-04 18:30:24 +0100 | [diff] [blame] | 412 | if (!isr) { |
Ryan Mallon | f373e8c | 2009-02-10 21:02:08 +0100 | [diff] [blame] | 413 | if (!at91_gpio->next) |
David Brownell | e83aff5 | 2008-01-04 18:30:24 +0100 | [diff] [blame] | 414 | break; |
Ryan Mallon | f373e8c | 2009-02-10 21:02:08 +0100 | [diff] [blame] | 415 | at91_gpio = at91_gpio->next; |
| 416 | pio = at91_gpio->regbase; |
David Brownell | e83aff5 | 2008-01-04 18:30:24 +0100 | [diff] [blame] | 417 | continue; |
| 418 | } |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 419 | |
Nicolas Ferre | 21f8187 | 2012-02-11 15:41:40 +0100 | [diff] [blame] | 420 | virq = gpio_to_irq(at91_gpio->chip.base); |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 421 | |
| 422 | while (isr) { |
Thomas Gleixner | ac93cdb | 2011-03-24 12:48:18 +0100 | [diff] [blame] | 423 | if (isr & 1) |
Nicolas Ferre | 21f8187 | 2012-02-11 15:41:40 +0100 | [diff] [blame] | 424 | generic_handle_irq(virq); |
| 425 | virq++; |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 426 | isr >>= 1; |
| 427 | } |
| 428 | } |
Thomas Gleixner | ac93cdb | 2011-03-24 12:48:18 +0100 | [diff] [blame] | 429 | chip->irq_unmask(idata); |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 430 | /* now it may re-trigger */ |
| 431 | } |
| 432 | |
Andrew Victor | f217383 | 2006-09-27 13:23:00 +0100 | [diff] [blame] | 433 | /*--------------------------------------------------------------------------*/ |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 434 | |
Andrew Victor | b66545e | 2007-11-23 16:09:10 +0100 | [diff] [blame] | 435 | #ifdef CONFIG_DEBUG_FS |
| 436 | |
| 437 | static int at91_gpio_show(struct seq_file *s, void *unused) |
| 438 | { |
| 439 | int bank, j; |
| 440 | |
| 441 | /* print heading */ |
| 442 | seq_printf(s, "Pin\t"); |
| 443 | for (bank = 0; bank < gpio_banks; bank++) { |
| 444 | seq_printf(s, "PIO%c\t", 'A' + bank); |
| 445 | }; |
| 446 | seq_printf(s, "\n\n"); |
| 447 | |
| 448 | /* print pin status */ |
| 449 | for (j = 0; j < 32; j++) { |
| 450 | seq_printf(s, "%i:\t", j); |
| 451 | |
| 452 | for (bank = 0; bank < gpio_banks; bank++) { |
Jean-Christophe PLAGNIOL-VILLARD | d0fbda9 | 2011-09-17 21:49:36 +0800 | [diff] [blame] | 453 | unsigned pin = (32 * bank) + j; |
Andrew Victor | b66545e | 2007-11-23 16:09:10 +0100 | [diff] [blame] | 454 | void __iomem *pio = pin_to_controller(pin); |
| 455 | unsigned mask = pin_to_mask(pin); |
| 456 | |
| 457 | if (__raw_readl(pio + PIO_PSR) & mask) |
| 458 | seq_printf(s, "GPIO:%s", __raw_readl(pio + PIO_PDSR) & mask ? "1" : "0"); |
| 459 | else |
| 460 | seq_printf(s, "%s", __raw_readl(pio + PIO_ABSR) & mask ? "B" : "A"); |
| 461 | |
| 462 | seq_printf(s, "\t"); |
| 463 | } |
| 464 | |
| 465 | seq_printf(s, "\n"); |
| 466 | } |
| 467 | |
| 468 | return 0; |
| 469 | } |
| 470 | |
| 471 | static int at91_gpio_open(struct inode *inode, struct file *file) |
| 472 | { |
| 473 | return single_open(file, at91_gpio_show, NULL); |
| 474 | } |
| 475 | |
| 476 | static const struct file_operations at91_gpio_operations = { |
| 477 | .open = at91_gpio_open, |
| 478 | .read = seq_read, |
| 479 | .llseek = seq_lseek, |
| 480 | .release = single_release, |
| 481 | }; |
| 482 | |
| 483 | static int __init at91_gpio_debugfs_init(void) |
| 484 | { |
| 485 | /* /sys/kernel/debug/at91_gpio */ |
| 486 | (void) debugfs_create_file("at91_gpio", S_IFREG | S_IRUGO, NULL, NULL, &at91_gpio_operations); |
| 487 | return 0; |
| 488 | } |
| 489 | postcore_initcall(at91_gpio_debugfs_init); |
| 490 | |
| 491 | #endif |
| 492 | |
| 493 | /*--------------------------------------------------------------------------*/ |
| 494 | |
Andrew Victor | 2b768b6 | 2009-02-11 21:39:05 +0100 | [diff] [blame] | 495 | /* |
Nicolas Ferre | 21f8187 | 2012-02-11 15:41:40 +0100 | [diff] [blame] | 496 | * irqdomain initialization: pile up irqdomains on top of AIC range |
| 497 | */ |
| 498 | static void __init at91_gpio_irqdomain(struct at91_gpio_chip *at91_gpio) |
| 499 | { |
| 500 | int irq_base; |
Nicolas Ferre | 5bc067b | 2012-02-13 11:26:25 +0100 | [diff] [blame] | 501 | #if defined(CONFIG_OF) |
| 502 | struct device_node *of_node = at91_gpio->chip.of_node; |
| 503 | #else |
| 504 | struct device_node *of_node = NULL; |
| 505 | #endif |
Nicolas Ferre | 21f8187 | 2012-02-11 15:41:40 +0100 | [diff] [blame] | 506 | |
| 507 | irq_base = irq_alloc_descs(-1, 0, at91_gpio->chip.ngpio, 0); |
| 508 | if (irq_base < 0) |
| 509 | panic("at91_gpio.%d: error %d: couldn't allocate IRQ numbers.\n", |
| 510 | at91_gpio->pioc_idx, irq_base); |
Nicolas Ferre | 5bc067b | 2012-02-13 11:26:25 +0100 | [diff] [blame] | 511 | at91_gpio->domain = irq_domain_add_legacy(of_node, |
Nicolas Ferre | 21f8187 | 2012-02-11 15:41:40 +0100 | [diff] [blame] | 512 | at91_gpio->chip.ngpio, |
| 513 | irq_base, 0, |
| 514 | &irq_domain_simple_ops, NULL); |
| 515 | if (!at91_gpio->domain) |
| 516 | panic("at91_gpio.%d: couldn't allocate irq domain.\n", |
| 517 | at91_gpio->pioc_idx); |
| 518 | } |
| 519 | |
| 520 | /* |
Andrew Victor | 2b768b6 | 2009-02-11 21:39:05 +0100 | [diff] [blame] | 521 | * This lock class tells lockdep that GPIO irqs are in a different |
David Brownell | 37aca70 | 2008-03-05 00:08:29 +0100 | [diff] [blame] | 522 | * category than their parents, so it won't report false recursion. |
| 523 | */ |
| 524 | static struct lock_class_key gpio_lock_class; |
| 525 | |
Andrew Victor | f217383 | 2006-09-27 13:23:00 +0100 | [diff] [blame] | 526 | /* |
| 527 | * Called from the processor-specific init to enable GPIO interrupt support. |
| 528 | */ |
| 529 | void __init at91_gpio_irq_setup(void) |
| 530 | { |
Nicolas Ferre | 21f8187 | 2012-02-11 15:41:40 +0100 | [diff] [blame] | 531 | unsigned pioc; |
| 532 | int gpio_irqnbr = 0; |
Ryan Mallon | f373e8c | 2009-02-10 21:02:08 +0100 | [diff] [blame] | 533 | struct at91_gpio_chip *this, *prev; |
Andrew Victor | f217383 | 2006-09-27 13:23:00 +0100 | [diff] [blame] | 534 | |
Jean-Christophe PLAGNIOL-VILLARD | d0fbda9 | 2011-09-17 21:49:36 +0800 | [diff] [blame] | 535 | for (pioc = 0, this = gpio_chip, prev = NULL; |
David Brownell | e83aff5 | 2008-01-04 18:30:24 +0100 | [diff] [blame] | 536 | pioc++ < gpio_banks; |
| 537 | prev = this, this++) { |
Nicolas Ferre | 4340cde | 2012-02-11 15:28:08 +0100 | [diff] [blame] | 538 | unsigned pioc_hwirq = this->pioc_hwirq; |
Nicolas Ferre | 21f8187 | 2012-02-11 15:41:40 +0100 | [diff] [blame] | 539 | int offset; |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 540 | |
David Brownell | e83aff5 | 2008-01-04 18:30:24 +0100 | [diff] [blame] | 541 | __raw_writel(~0, this->regbase + PIO_IDR); |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 542 | |
Nicolas Ferre | 21f8187 | 2012-02-11 15:41:40 +0100 | [diff] [blame] | 543 | /* setup irq domain for this GPIO controller */ |
| 544 | at91_gpio_irqdomain(this); |
| 545 | |
| 546 | for (offset = 0; offset < this->chip.ngpio; offset++) { |
| 547 | unsigned int virq = irq_find_mapping(this->domain, offset); |
| 548 | irq_set_lockdep_class(virq, &gpio_lock_class); |
David Brownell | 37aca70 | 2008-03-05 00:08:29 +0100 | [diff] [blame] | 549 | |
Andrew Victor | 814138f | 2006-06-19 15:26:54 +0100 | [diff] [blame] | 550 | /* |
| 551 | * 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] | 552 | * shorter, and the AIC handles interrupts sanely. |
Andrew Victor | 814138f | 2006-06-19 15:26:54 +0100 | [diff] [blame] | 553 | */ |
Nicolas Ferre | 21f8187 | 2012-02-11 15:41:40 +0100 | [diff] [blame] | 554 | irq_set_chip_and_handler(virq, &gpio_irqchip, |
Thomas Gleixner | f38c02f | 2011-03-24 13:35:09 +0100 | [diff] [blame] | 555 | handle_simple_irq); |
Nicolas Ferre | 21f8187 | 2012-02-11 15:41:40 +0100 | [diff] [blame] | 556 | set_irq_flags(virq, IRQF_VALID); |
| 557 | irq_set_chip_data(virq, this); |
| 558 | |
| 559 | gpio_irqnbr++; |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 560 | } |
| 561 | |
David Brownell | e83aff5 | 2008-01-04 18:30:24 +0100 | [diff] [blame] | 562 | /* The toplevel handler handles one bank of GPIOs, except |
Nicolas Ferre | 4340cde | 2012-02-11 15:28:08 +0100 | [diff] [blame] | 563 | * on some SoC it can handles up to three... |
| 564 | * We only set up the handler for the first of the list. |
David Brownell | e83aff5 | 2008-01-04 18:30:24 +0100 | [diff] [blame] | 565 | */ |
| 566 | if (prev && prev->next == this) |
| 567 | continue; |
| 568 | |
Nicolas Ferre | 4340cde | 2012-02-11 15:28:08 +0100 | [diff] [blame] | 569 | irq_set_chip_data(pioc_hwirq, this); |
| 570 | irq_set_chained_handler(pioc_hwirq, gpio_irq_handler); |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 571 | } |
Nicolas Ferre | 21f8187 | 2012-02-11 15:41:40 +0100 | [diff] [blame] | 572 | pr_info("AT91: %d gpio irqs in %d banks\n", gpio_irqnbr, gpio_banks); |
Andrew Victor | f217383 | 2006-09-27 13:23:00 +0100 | [diff] [blame] | 573 | } |
| 574 | |
Ryan Mallon | f373e8c | 2009-02-10 21:02:08 +0100 | [diff] [blame] | 575 | /* gpiolib support */ |
| 576 | static int at91_gpiolib_direction_input(struct gpio_chip *chip, |
| 577 | unsigned offset) |
| 578 | { |
| 579 | struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip); |
| 580 | void __iomem *pio = at91_gpio->regbase; |
| 581 | unsigned mask = 1 << offset; |
| 582 | |
| 583 | __raw_writel(mask, pio + PIO_ODR); |
| 584 | return 0; |
| 585 | } |
| 586 | |
| 587 | static int at91_gpiolib_direction_output(struct gpio_chip *chip, |
| 588 | unsigned offset, int val) |
| 589 | { |
| 590 | struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip); |
| 591 | void __iomem *pio = at91_gpio->regbase; |
| 592 | unsigned mask = 1 << offset; |
| 593 | |
| 594 | __raw_writel(mask, pio + (val ? PIO_SODR : PIO_CODR)); |
| 595 | __raw_writel(mask, pio + PIO_OER); |
| 596 | return 0; |
| 597 | } |
| 598 | |
| 599 | static int at91_gpiolib_get(struct gpio_chip *chip, unsigned offset) |
| 600 | { |
| 601 | struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip); |
| 602 | void __iomem *pio = at91_gpio->regbase; |
| 603 | unsigned mask = 1 << offset; |
| 604 | u32 pdsr; |
| 605 | |
| 606 | pdsr = __raw_readl(pio + PIO_PDSR); |
| 607 | return (pdsr & mask) != 0; |
| 608 | } |
| 609 | |
| 610 | static void at91_gpiolib_set(struct gpio_chip *chip, unsigned offset, int val) |
| 611 | { |
| 612 | struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip); |
| 613 | void __iomem *pio = at91_gpio->regbase; |
| 614 | unsigned mask = 1 << offset; |
| 615 | |
| 616 | __raw_writel(mask, pio + (val ? PIO_SODR : PIO_CODR)); |
| 617 | } |
| 618 | |
Ryan Mallon | f373e8c | 2009-02-10 21:02:08 +0100 | [diff] [blame] | 619 | static void at91_gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip) |
| 620 | { |
| 621 | int i; |
| 622 | |
| 623 | for (i = 0; i < chip->ngpio; i++) { |
| 624 | unsigned pin = chip->base + i; |
| 625 | void __iomem *pio = pin_to_controller(pin); |
| 626 | unsigned mask = pin_to_mask(pin); |
| 627 | const char *gpio_label; |
| 628 | |
| 629 | gpio_label = gpiochip_is_requested(chip, i); |
| 630 | if (gpio_label) { |
| 631 | seq_printf(s, "[%s] GPIO%s%d: ", |
| 632 | gpio_label, chip->label, i); |
| 633 | if (__raw_readl(pio + PIO_PSR) & mask) |
| 634 | seq_printf(s, "[gpio] %s\n", |
| 635 | at91_get_gpio_value(pin) ? |
| 636 | "set" : "clear"); |
| 637 | else |
| 638 | seq_printf(s, "[periph %s]\n", |
| 639 | __raw_readl(pio + PIO_ABSR) & |
| 640 | mask ? "B" : "A"); |
| 641 | } |
| 642 | } |
| 643 | } |
| 644 | |
Nicolas Ferre | b134ce8 | 2012-02-11 15:56:01 +0100 | [diff] [blame] | 645 | static int at91_gpiolib_to_irq(struct gpio_chip *chip, unsigned offset) |
| 646 | { |
| 647 | struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip); |
| 648 | int virq = irq_find_mapping(at91_gpio->domain, offset); |
| 649 | |
| 650 | dev_dbg(chip->dev, "%s: request IRQ for GPIO %d, return %d\n", |
| 651 | chip->label, offset + chip->base, virq); |
| 652 | return virq; |
| 653 | } |
| 654 | |
Nicolas Ferre | 21f8187 | 2012-02-11 15:41:40 +0100 | [diff] [blame] | 655 | static int __init at91_gpio_setup_clk(int idx) |
| 656 | { |
| 657 | struct at91_gpio_chip *at91_gpio = &gpio_chip[idx]; |
| 658 | |
| 659 | /* retreive PIO controller's clock */ |
| 660 | at91_gpio->clock = clk_get_sys(NULL, at91_gpio->chip.label); |
| 661 | if (IS_ERR(at91_gpio->clock)) { |
| 662 | pr_err("at91_gpio.%d, failed to get clock, ignoring.\n", idx); |
| 663 | goto err; |
| 664 | } |
| 665 | |
| 666 | if (clk_prepare(at91_gpio->clock)) |
| 667 | goto clk_prep_err; |
| 668 | |
| 669 | /* enable PIO controller's clock */ |
| 670 | if (clk_enable(at91_gpio->clock)) { |
| 671 | pr_err("at91_gpio.%d, failed to enable clock, ignoring.\n", idx); |
| 672 | goto clk_err; |
| 673 | } |
| 674 | |
| 675 | return 0; |
| 676 | |
| 677 | clk_err: |
| 678 | clk_unprepare(at91_gpio->clock); |
| 679 | clk_prep_err: |
| 680 | clk_put(at91_gpio->clock); |
| 681 | err: |
| 682 | return -EINVAL; |
| 683 | } |
| 684 | |
| 685 | #ifdef CONFIG_OF_GPIO |
| 686 | static void __init of_at91_gpio_init_one(struct device_node *np) |
| 687 | { |
| 688 | int alias_idx; |
| 689 | struct at91_gpio_chip *at91_gpio; |
| 690 | |
| 691 | if (!np) |
| 692 | return; |
| 693 | |
| 694 | alias_idx = of_alias_get_id(np, "gpio"); |
| 695 | if (alias_idx >= MAX_GPIO_BANKS) { |
| 696 | pr_err("at91_gpio, failed alias idx(%d) > MAX_GPIO_BANKS(%d), ignoring.\n", |
| 697 | alias_idx, MAX_GPIO_BANKS); |
| 698 | return; |
| 699 | } |
| 700 | |
| 701 | at91_gpio = &gpio_chip[alias_idx]; |
| 702 | at91_gpio->chip.base = alias_idx * at91_gpio->chip.ngpio; |
| 703 | |
| 704 | at91_gpio->regbase = of_iomap(np, 0); |
| 705 | if (!at91_gpio->regbase) { |
| 706 | pr_err("at91_gpio.%d, failed to map registers, ignoring.\n", |
| 707 | alias_idx); |
| 708 | return; |
| 709 | } |
| 710 | |
| 711 | /* Get the interrupts property */ |
| 712 | if (of_property_read_u32(np, "interrupts", &at91_gpio->pioc_hwirq)) { |
| 713 | pr_err("at91_gpio.%d, failed to get interrupts property, ignoring.\n", |
| 714 | alias_idx); |
| 715 | goto ioremap_err; |
| 716 | } |
| 717 | |
| 718 | /* Setup clock */ |
| 719 | if (at91_gpio_setup_clk(alias_idx)) |
| 720 | goto ioremap_err; |
| 721 | |
| 722 | at91_gpio->chip.of_node = np; |
| 723 | gpio_banks = max(gpio_banks, alias_idx + 1); |
| 724 | at91_gpio->pioc_idx = alias_idx; |
| 725 | return; |
| 726 | |
| 727 | ioremap_err: |
| 728 | iounmap(at91_gpio->regbase); |
| 729 | } |
| 730 | |
| 731 | static int __init of_at91_gpio_init(void) |
| 732 | { |
| 733 | struct device_node *np = NULL; |
| 734 | |
| 735 | /* |
| 736 | * This isn't ideal, but it gets things hooked up until this |
| 737 | * driver is converted into a platform_device |
| 738 | */ |
| 739 | for_each_compatible_node(np, NULL, "atmel,at91rm9200-gpio") |
| 740 | of_at91_gpio_init_one(np); |
| 741 | |
| 742 | return gpio_banks > 0 ? 0 : -EINVAL; |
| 743 | } |
| 744 | #else |
| 745 | static int __init of_at91_gpio_init(void) |
| 746 | { |
| 747 | return -EINVAL; |
| 748 | } |
| 749 | #endif |
| 750 | |
| 751 | static void __init at91_gpio_init_one(int idx, u32 regbase, int pioc_hwirq) |
| 752 | { |
| 753 | struct at91_gpio_chip *at91_gpio = &gpio_chip[idx]; |
| 754 | |
| 755 | at91_gpio->chip.base = idx * at91_gpio->chip.ngpio; |
| 756 | at91_gpio->pioc_hwirq = pioc_hwirq; |
| 757 | at91_gpio->pioc_idx = idx; |
| 758 | |
| 759 | at91_gpio->regbase = ioremap(regbase, 512); |
| 760 | if (!at91_gpio->regbase) { |
| 761 | pr_err("at91_gpio.%d, failed to map registers, ignoring.\n", idx); |
| 762 | return; |
| 763 | } |
| 764 | |
| 765 | if (at91_gpio_setup_clk(idx)) |
| 766 | goto ioremap_err; |
| 767 | |
| 768 | gpio_banks = max(gpio_banks, idx + 1); |
| 769 | return; |
| 770 | |
| 771 | ioremap_err: |
| 772 | iounmap(at91_gpio->regbase); |
| 773 | } |
| 774 | |
Andrew Victor | f217383 | 2006-09-27 13:23:00 +0100 | [diff] [blame] | 775 | /* |
| 776 | * Called from the processor-specific init to enable GPIO pin support. |
| 777 | */ |
| 778 | void __init at91_gpio_init(struct at91_gpio_bank *data, int nr_banks) |
| 779 | { |
Nicolas Ferre | 21f8187 | 2012-02-11 15:41:40 +0100 | [diff] [blame] | 780 | unsigned i; |
Ryan Mallon | f373e8c | 2009-02-10 21:02:08 +0100 | [diff] [blame] | 781 | struct at91_gpio_chip *at91_gpio, *last = NULL; |
David Brownell | e83aff5 | 2008-01-04 18:30:24 +0100 | [diff] [blame] | 782 | |
Andrew Victor | f217383 | 2006-09-27 13:23:00 +0100 | [diff] [blame] | 783 | BUG_ON(nr_banks > MAX_GPIO_BANKS); |
| 784 | |
Nicolas Ferre | 21f8187 | 2012-02-11 15:41:40 +0100 | [diff] [blame] | 785 | if (of_at91_gpio_init() < 0) { |
| 786 | /* No GPIO controller found in device tree */ |
| 787 | for (i = 0; i < nr_banks; i++) |
| 788 | at91_gpio_init_one(i, data[i].regbase, data[i].id); |
| 789 | } |
David Brownell | e83aff5 | 2008-01-04 18:30:24 +0100 | [diff] [blame] | 790 | |
Nicolas Ferre | 21f8187 | 2012-02-11 15:41:40 +0100 | [diff] [blame] | 791 | for (i = 0; i < gpio_banks; i++) { |
Ryan Mallon | f373e8c | 2009-02-10 21:02:08 +0100 | [diff] [blame] | 792 | at91_gpio = &gpio_chip[i]; |
| 793 | |
Nicolas Ferre | 4340cde | 2012-02-11 15:28:08 +0100 | [diff] [blame] | 794 | /* |
| 795 | * GPIO controller are grouped on some SoC: |
| 796 | * PIOC, PIOD and PIOE can share the same IRQ line |
| 797 | */ |
| 798 | if (last && last->pioc_hwirq == at91_gpio->pioc_hwirq) |
Ryan Mallon | f373e8c | 2009-02-10 21:02:08 +0100 | [diff] [blame] | 799 | last->next = at91_gpio; |
| 800 | last = at91_gpio; |
| 801 | |
| 802 | gpiochip_add(&at91_gpio->chip); |
David Brownell | e83aff5 | 2008-01-04 18:30:24 +0100 | [diff] [blame] | 803 | } |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 804 | } |