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