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