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