Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Generic GPIO driver for logic cells found in the Nomadik SoC |
| 3 | * |
| 4 | * Copyright (C) 2008,2009 STMicroelectronics |
| 5 | * Copyright (C) 2009 Alessandro Rubini <rubini@unipv.it> |
| 6 | * Rewritten based on work by Prafulla WADASKAR <prafulla.wadaskar@st.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | */ |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/device.h> |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 16 | #include <linux/platform_device.h> |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 17 | #include <linux/io.h> |
Rabin Vincent | af7dc22 | 2010-05-06 11:14:17 +0100 | [diff] [blame] | 18 | #include <linux/clk.h> |
| 19 | #include <linux/err.h> |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 20 | #include <linux/gpio.h> |
| 21 | #include <linux/spinlock.h> |
| 22 | #include <linux/interrupt.h> |
| 23 | #include <linux/irq.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 24 | #include <linux/slab.h> |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 25 | |
Will Deacon | adfed15 | 2011-02-28 10:12:29 +0000 | [diff] [blame] | 26 | #include <asm/mach/irq.h> |
| 27 | |
Rabin Vincent | 378be06 | 2010-06-02 06:06:29 +0100 | [diff] [blame] | 28 | #include <plat/pincfg.h> |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 29 | #include <mach/hardware.h> |
| 30 | #include <mach/gpio.h> |
| 31 | |
| 32 | /* |
| 33 | * The GPIO module in the Nomadik family of Systems-on-Chip is an |
| 34 | * AMBA device, managing 32 pins and alternate functions. The logic block |
Jonas Aaberg | 9c66ee6 | 2010-10-13 13:14:17 +0200 | [diff] [blame] | 35 | * is currently used in the Nomadik and ux500. |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 36 | * |
| 37 | * Symbols in this file are called "nmk_gpio" for "nomadik gpio" |
| 38 | */ |
| 39 | |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 40 | #define NMK_GPIO_PER_CHIP 32 |
| 41 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 42 | struct nmk_gpio_chip { |
| 43 | struct gpio_chip chip; |
| 44 | void __iomem *addr; |
Rabin Vincent | af7dc22 | 2010-05-06 11:14:17 +0100 | [diff] [blame] | 45 | struct clk *clk; |
Rabin Vincent | 33b744b | 2010-10-14 10:38:03 +0530 | [diff] [blame] | 46 | unsigned int bank; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 47 | unsigned int parent_irq; |
Virupax Sadashivpetimath | 2c8bb0e | 2010-11-11 14:10:38 +0530 | [diff] [blame] | 48 | int secondary_parent_irq; |
Rabin Vincent | 33b744b | 2010-10-14 10:38:03 +0530 | [diff] [blame] | 49 | u32 (*get_secondary_status)(unsigned int bank); |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 50 | void (*set_ioforce)(bool enable); |
Rabin Vincent | c0fcb8d | 2010-03-03 04:48:54 +0100 | [diff] [blame] | 51 | spinlock_t lock; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 52 | /* Keep track of configured edges */ |
| 53 | u32 edge_rising; |
| 54 | u32 edge_falling; |
Rabin Vincent | b9df468 | 2011-02-10 11:45:58 +0530 | [diff] [blame] | 55 | u32 real_wake; |
| 56 | u32 rwimsc; |
| 57 | u32 fwimsc; |
| 58 | u32 slpm; |
Thomas Gleixner | d1118f6 | 2011-03-24 12:38:50 +0100 | [diff] [blame] | 59 | u32 enabled; |
Rickard Andersson | bc6f5cf | 2011-05-24 23:07:17 +0200 | [diff] [blame^] | 60 | u32 pull_up; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 61 | }; |
| 62 | |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 63 | static struct nmk_gpio_chip * |
| 64 | nmk_gpio_chips[DIV_ROUND_UP(ARCH_NR_GPIOS, NMK_GPIO_PER_CHIP)]; |
| 65 | |
| 66 | static DEFINE_SPINLOCK(nmk_gpio_slpm_lock); |
| 67 | |
| 68 | #define NUM_BANKS ARRAY_SIZE(nmk_gpio_chips) |
| 69 | |
Rabin Vincent | 6f9a974 | 2010-06-02 05:50:28 +0100 | [diff] [blame] | 70 | static void __nmk_gpio_set_mode(struct nmk_gpio_chip *nmk_chip, |
| 71 | unsigned offset, int gpio_mode) |
| 72 | { |
| 73 | u32 bit = 1 << offset; |
| 74 | u32 afunc, bfunc; |
| 75 | |
| 76 | afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & ~bit; |
| 77 | bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & ~bit; |
| 78 | if (gpio_mode & NMK_GPIO_ALT_A) |
| 79 | afunc |= bit; |
| 80 | if (gpio_mode & NMK_GPIO_ALT_B) |
| 81 | bfunc |= bit; |
| 82 | writel(afunc, nmk_chip->addr + NMK_GPIO_AFSLA); |
| 83 | writel(bfunc, nmk_chip->addr + NMK_GPIO_AFSLB); |
| 84 | } |
| 85 | |
Rabin Vincent | 81a3c29 | 2010-05-27 12:39:23 +0100 | [diff] [blame] | 86 | static void __nmk_gpio_set_slpm(struct nmk_gpio_chip *nmk_chip, |
| 87 | unsigned offset, enum nmk_gpio_slpm mode) |
| 88 | { |
| 89 | u32 bit = 1 << offset; |
| 90 | u32 slpm; |
| 91 | |
| 92 | slpm = readl(nmk_chip->addr + NMK_GPIO_SLPC); |
| 93 | if (mode == NMK_GPIO_SLPM_NOCHANGE) |
| 94 | slpm |= bit; |
| 95 | else |
| 96 | slpm &= ~bit; |
| 97 | writel(slpm, nmk_chip->addr + NMK_GPIO_SLPC); |
| 98 | } |
| 99 | |
Rabin Vincent | 5b327ed | 2010-05-27 12:29:50 +0100 | [diff] [blame] | 100 | static void __nmk_gpio_set_pull(struct nmk_gpio_chip *nmk_chip, |
| 101 | unsigned offset, enum nmk_gpio_pull pull) |
| 102 | { |
| 103 | u32 bit = 1 << offset; |
| 104 | u32 pdis; |
| 105 | |
| 106 | pdis = readl(nmk_chip->addr + NMK_GPIO_PDIS); |
Rickard Andersson | bc6f5cf | 2011-05-24 23:07:17 +0200 | [diff] [blame^] | 107 | if (pull == NMK_GPIO_PULL_NONE) { |
Rabin Vincent | 5b327ed | 2010-05-27 12:29:50 +0100 | [diff] [blame] | 108 | pdis |= bit; |
Rickard Andersson | bc6f5cf | 2011-05-24 23:07:17 +0200 | [diff] [blame^] | 109 | nmk_chip->pull_up &= ~bit; |
| 110 | } else { |
Rabin Vincent | 5b327ed | 2010-05-27 12:29:50 +0100 | [diff] [blame] | 111 | pdis &= ~bit; |
Rickard Andersson | bc6f5cf | 2011-05-24 23:07:17 +0200 | [diff] [blame^] | 112 | } |
| 113 | |
Rabin Vincent | 5b327ed | 2010-05-27 12:29:50 +0100 | [diff] [blame] | 114 | writel(pdis, nmk_chip->addr + NMK_GPIO_PDIS); |
| 115 | |
Rickard Andersson | bc6f5cf | 2011-05-24 23:07:17 +0200 | [diff] [blame^] | 116 | if (pull == NMK_GPIO_PULL_UP) { |
| 117 | nmk_chip->pull_up |= bit; |
Rabin Vincent | 5b327ed | 2010-05-27 12:29:50 +0100 | [diff] [blame] | 118 | writel(bit, nmk_chip->addr + NMK_GPIO_DATS); |
Rickard Andersson | bc6f5cf | 2011-05-24 23:07:17 +0200 | [diff] [blame^] | 119 | } else if (pull == NMK_GPIO_PULL_DOWN) { |
| 120 | nmk_chip->pull_up &= ~bit; |
Rabin Vincent | 5b327ed | 2010-05-27 12:29:50 +0100 | [diff] [blame] | 121 | writel(bit, nmk_chip->addr + NMK_GPIO_DATC); |
Rickard Andersson | bc6f5cf | 2011-05-24 23:07:17 +0200 | [diff] [blame^] | 122 | } |
Rabin Vincent | 5b327ed | 2010-05-27 12:29:50 +0100 | [diff] [blame] | 123 | } |
| 124 | |
Rabin Vincent | 378be06 | 2010-06-02 06:06:29 +0100 | [diff] [blame] | 125 | static void __nmk_gpio_make_input(struct nmk_gpio_chip *nmk_chip, |
| 126 | unsigned offset) |
| 127 | { |
| 128 | writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC); |
| 129 | } |
| 130 | |
Rabin Vincent | 6720db7 | 2010-09-02 11:28:48 +0100 | [diff] [blame] | 131 | static void __nmk_gpio_set_output(struct nmk_gpio_chip *nmk_chip, |
| 132 | unsigned offset, int val) |
| 133 | { |
| 134 | if (val) |
| 135 | writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATS); |
| 136 | else |
| 137 | writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATC); |
| 138 | } |
| 139 | |
| 140 | static void __nmk_gpio_make_output(struct nmk_gpio_chip *nmk_chip, |
| 141 | unsigned offset, int val) |
| 142 | { |
| 143 | writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRS); |
| 144 | __nmk_gpio_set_output(nmk_chip, offset, val); |
| 145 | } |
| 146 | |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 147 | static void __nmk_gpio_set_mode_safe(struct nmk_gpio_chip *nmk_chip, |
| 148 | unsigned offset, int gpio_mode, |
| 149 | bool glitch) |
| 150 | { |
Linus Walleij | 3c4bee0 | 2011-02-16 10:37:52 +0100 | [diff] [blame] | 151 | u32 rwimsc = readl(nmk_chip->addr + NMK_GPIO_RWIMSC); |
| 152 | u32 fwimsc = readl(nmk_chip->addr + NMK_GPIO_FWIMSC); |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 153 | |
| 154 | if (glitch && nmk_chip->set_ioforce) { |
| 155 | u32 bit = BIT(offset); |
| 156 | |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 157 | /* Prevent spurious wakeups */ |
| 158 | writel(rwimsc & ~bit, nmk_chip->addr + NMK_GPIO_RWIMSC); |
| 159 | writel(fwimsc & ~bit, nmk_chip->addr + NMK_GPIO_FWIMSC); |
| 160 | |
| 161 | nmk_chip->set_ioforce(true); |
| 162 | } |
| 163 | |
| 164 | __nmk_gpio_set_mode(nmk_chip, offset, gpio_mode); |
| 165 | |
| 166 | if (glitch && nmk_chip->set_ioforce) { |
| 167 | nmk_chip->set_ioforce(false); |
| 168 | |
| 169 | writel(rwimsc, nmk_chip->addr + NMK_GPIO_RWIMSC); |
| 170 | writel(fwimsc, nmk_chip->addr + NMK_GPIO_FWIMSC); |
| 171 | } |
| 172 | } |
| 173 | |
Rabin Vincent | 378be06 | 2010-06-02 06:06:29 +0100 | [diff] [blame] | 174 | static void __nmk_config_pin(struct nmk_gpio_chip *nmk_chip, unsigned offset, |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 175 | pin_cfg_t cfg, bool sleep, unsigned int *slpmregs) |
Rabin Vincent | 378be06 | 2010-06-02 06:06:29 +0100 | [diff] [blame] | 176 | { |
| 177 | static const char *afnames[] = { |
| 178 | [NMK_GPIO_ALT_GPIO] = "GPIO", |
| 179 | [NMK_GPIO_ALT_A] = "A", |
| 180 | [NMK_GPIO_ALT_B] = "B", |
| 181 | [NMK_GPIO_ALT_C] = "C" |
| 182 | }; |
| 183 | static const char *pullnames[] = { |
| 184 | [NMK_GPIO_PULL_NONE] = "none", |
| 185 | [NMK_GPIO_PULL_UP] = "up", |
| 186 | [NMK_GPIO_PULL_DOWN] = "down", |
| 187 | [3] /* illegal */ = "??" |
| 188 | }; |
| 189 | static const char *slpmnames[] = { |
Rabin Vincent | 7e3f7e5 | 2010-09-02 11:28:05 +0100 | [diff] [blame] | 190 | [NMK_GPIO_SLPM_INPUT] = "input/wakeup", |
| 191 | [NMK_GPIO_SLPM_NOCHANGE] = "no-change/no-wakeup", |
Rabin Vincent | 378be06 | 2010-06-02 06:06:29 +0100 | [diff] [blame] | 192 | }; |
| 193 | |
| 194 | int pin = PIN_NUM(cfg); |
| 195 | int pull = PIN_PULL(cfg); |
| 196 | int af = PIN_ALT(cfg); |
| 197 | int slpm = PIN_SLPM(cfg); |
Rabin Vincent | 6720db7 | 2010-09-02 11:28:48 +0100 | [diff] [blame] | 198 | int output = PIN_DIR(cfg); |
| 199 | int val = PIN_VAL(cfg); |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 200 | bool glitch = af == NMK_GPIO_ALT_C; |
Rabin Vincent | 378be06 | 2010-06-02 06:06:29 +0100 | [diff] [blame] | 201 | |
Rabin Vincent | dacdc96 | 2010-12-03 20:35:37 +0530 | [diff] [blame] | 202 | dev_dbg(nmk_chip->chip.dev, "pin %d [%#lx]: af %s, pull %s, slpm %s (%s%s)\n", |
| 203 | pin, cfg, afnames[af], pullnames[pull], slpmnames[slpm], |
Rabin Vincent | 6720db7 | 2010-09-02 11:28:48 +0100 | [diff] [blame] | 204 | output ? "output " : "input", |
| 205 | output ? (val ? "high" : "low") : ""); |
Rabin Vincent | 378be06 | 2010-06-02 06:06:29 +0100 | [diff] [blame] | 206 | |
Rabin Vincent | dacdc96 | 2010-12-03 20:35:37 +0530 | [diff] [blame] | 207 | if (sleep) { |
| 208 | int slpm_pull = PIN_SLPM_PULL(cfg); |
| 209 | int slpm_output = PIN_SLPM_DIR(cfg); |
| 210 | int slpm_val = PIN_SLPM_VAL(cfg); |
| 211 | |
Rabin Vincent | 3546d15 | 2010-11-25 11:38:27 +0530 | [diff] [blame] | 212 | af = NMK_GPIO_ALT_GPIO; |
| 213 | |
Rabin Vincent | dacdc96 | 2010-12-03 20:35:37 +0530 | [diff] [blame] | 214 | /* |
| 215 | * The SLPM_* values are normal values + 1 to allow zero to |
| 216 | * mean "same as normal". |
| 217 | */ |
| 218 | if (slpm_pull) |
| 219 | pull = slpm_pull - 1; |
| 220 | if (slpm_output) |
| 221 | output = slpm_output - 1; |
| 222 | if (slpm_val) |
| 223 | val = slpm_val - 1; |
| 224 | |
| 225 | dev_dbg(nmk_chip->chip.dev, "pin %d: sleep pull %s, dir %s, val %s\n", |
| 226 | pin, |
| 227 | slpm_pull ? pullnames[pull] : "same", |
| 228 | slpm_output ? (output ? "output" : "input") : "same", |
| 229 | slpm_val ? (val ? "high" : "low") : "same"); |
| 230 | } |
| 231 | |
Rabin Vincent | 6720db7 | 2010-09-02 11:28:48 +0100 | [diff] [blame] | 232 | if (output) |
| 233 | __nmk_gpio_make_output(nmk_chip, offset, val); |
| 234 | else { |
| 235 | __nmk_gpio_make_input(nmk_chip, offset); |
| 236 | __nmk_gpio_set_pull(nmk_chip, offset, pull); |
| 237 | } |
| 238 | |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 239 | /* |
| 240 | * If we've backed up the SLPM registers (glitch workaround), modify |
| 241 | * the backups since they will be restored. |
| 242 | */ |
| 243 | if (slpmregs) { |
| 244 | if (slpm == NMK_GPIO_SLPM_NOCHANGE) |
| 245 | slpmregs[nmk_chip->bank] |= BIT(offset); |
| 246 | else |
| 247 | slpmregs[nmk_chip->bank] &= ~BIT(offset); |
| 248 | } else |
| 249 | __nmk_gpio_set_slpm(nmk_chip, offset, slpm); |
| 250 | |
| 251 | __nmk_gpio_set_mode_safe(nmk_chip, offset, af, glitch); |
| 252 | } |
| 253 | |
| 254 | /* |
| 255 | * Safe sequence used to switch IOs between GPIO and Alternate-C mode: |
| 256 | * - Save SLPM registers |
| 257 | * - Set SLPM=0 for the IOs you want to switch and others to 1 |
| 258 | * - Configure the GPIO registers for the IOs that are being switched |
| 259 | * - Set IOFORCE=1 |
| 260 | * - Modify the AFLSA/B registers for the IOs that are being switched |
| 261 | * - Set IOFORCE=0 |
| 262 | * - Restore SLPM registers |
| 263 | * - Any spurious wake up event during switch sequence to be ignored and |
| 264 | * cleared |
| 265 | */ |
| 266 | static void nmk_gpio_glitch_slpm_init(unsigned int *slpm) |
| 267 | { |
| 268 | int i; |
| 269 | |
| 270 | for (i = 0; i < NUM_BANKS; i++) { |
| 271 | struct nmk_gpio_chip *chip = nmk_gpio_chips[i]; |
| 272 | unsigned int temp = slpm[i]; |
| 273 | |
| 274 | if (!chip) |
| 275 | break; |
| 276 | |
| 277 | slpm[i] = readl(chip->addr + NMK_GPIO_SLPC); |
| 278 | writel(temp, chip->addr + NMK_GPIO_SLPC); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | static void nmk_gpio_glitch_slpm_restore(unsigned int *slpm) |
| 283 | { |
| 284 | int i; |
| 285 | |
| 286 | for (i = 0; i < NUM_BANKS; i++) { |
| 287 | struct nmk_gpio_chip *chip = nmk_gpio_chips[i]; |
| 288 | |
| 289 | if (!chip) |
| 290 | break; |
| 291 | |
| 292 | writel(slpm[i], chip->addr + NMK_GPIO_SLPC); |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | static int __nmk_config_pins(pin_cfg_t *cfgs, int num, bool sleep) |
| 297 | { |
| 298 | static unsigned int slpm[NUM_BANKS]; |
| 299 | unsigned long flags; |
| 300 | bool glitch = false; |
| 301 | int ret = 0; |
| 302 | int i; |
| 303 | |
| 304 | for (i = 0; i < num; i++) { |
| 305 | if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C) { |
| 306 | glitch = true; |
| 307 | break; |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | spin_lock_irqsave(&nmk_gpio_slpm_lock, flags); |
| 312 | |
| 313 | if (glitch) { |
| 314 | memset(slpm, 0xff, sizeof(slpm)); |
| 315 | |
| 316 | for (i = 0; i < num; i++) { |
| 317 | int pin = PIN_NUM(cfgs[i]); |
| 318 | int offset = pin % NMK_GPIO_PER_CHIP; |
| 319 | |
| 320 | if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C) |
| 321 | slpm[pin / NMK_GPIO_PER_CHIP] &= ~BIT(offset); |
| 322 | } |
| 323 | |
| 324 | nmk_gpio_glitch_slpm_init(slpm); |
| 325 | } |
| 326 | |
| 327 | for (i = 0; i < num; i++) { |
| 328 | struct nmk_gpio_chip *nmk_chip; |
| 329 | int pin = PIN_NUM(cfgs[i]); |
| 330 | |
Thomas Gleixner | 6845664a | 2011-03-24 13:25:22 +0100 | [diff] [blame] | 331 | nmk_chip = irq_get_chip_data(NOMADIK_GPIO_TO_IRQ(pin)); |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 332 | if (!nmk_chip) { |
| 333 | ret = -EINVAL; |
| 334 | break; |
| 335 | } |
| 336 | |
| 337 | spin_lock(&nmk_chip->lock); |
| 338 | __nmk_config_pin(nmk_chip, pin - nmk_chip->chip.base, |
| 339 | cfgs[i], sleep, glitch ? slpm : NULL); |
| 340 | spin_unlock(&nmk_chip->lock); |
| 341 | } |
| 342 | |
| 343 | if (glitch) |
| 344 | nmk_gpio_glitch_slpm_restore(slpm); |
| 345 | |
| 346 | spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags); |
| 347 | |
| 348 | return ret; |
Rabin Vincent | 378be06 | 2010-06-02 06:06:29 +0100 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | /** |
| 352 | * nmk_config_pin - configure a pin's mux attributes |
| 353 | * @cfg: pin confguration |
| 354 | * |
| 355 | * Configures a pin's mode (alternate function or GPIO), its pull up status, |
| 356 | * and its sleep mode based on the specified configuration. The @cfg is |
| 357 | * usually one of the SoC specific macros defined in mach/<soc>-pins.h. These |
| 358 | * are constructed using, and can be further enhanced with, the macros in |
| 359 | * plat/pincfg.h. |
| 360 | * |
| 361 | * If a pin's mode is set to GPIO, it is configured as an input to avoid |
| 362 | * side-effects. The gpio can be manipulated later using standard GPIO API |
| 363 | * calls. |
| 364 | */ |
Rabin Vincent | dacdc96 | 2010-12-03 20:35:37 +0530 | [diff] [blame] | 365 | int nmk_config_pin(pin_cfg_t cfg, bool sleep) |
Rabin Vincent | 378be06 | 2010-06-02 06:06:29 +0100 | [diff] [blame] | 366 | { |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 367 | return __nmk_config_pins(&cfg, 1, sleep); |
Rabin Vincent | 378be06 | 2010-06-02 06:06:29 +0100 | [diff] [blame] | 368 | } |
| 369 | EXPORT_SYMBOL(nmk_config_pin); |
| 370 | |
| 371 | /** |
| 372 | * nmk_config_pins - configure several pins at once |
| 373 | * @cfgs: array of pin configurations |
| 374 | * @num: number of elments in the array |
| 375 | * |
| 376 | * Configures several pins using nmk_config_pin(). Refer to that function for |
| 377 | * further information. |
| 378 | */ |
| 379 | int nmk_config_pins(pin_cfg_t *cfgs, int num) |
| 380 | { |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 381 | return __nmk_config_pins(cfgs, num, false); |
Rabin Vincent | 378be06 | 2010-06-02 06:06:29 +0100 | [diff] [blame] | 382 | } |
| 383 | EXPORT_SYMBOL(nmk_config_pins); |
| 384 | |
Rabin Vincent | dacdc96 | 2010-12-03 20:35:37 +0530 | [diff] [blame] | 385 | int nmk_config_pins_sleep(pin_cfg_t *cfgs, int num) |
| 386 | { |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 387 | return __nmk_config_pins(cfgs, num, true); |
Rabin Vincent | dacdc96 | 2010-12-03 20:35:37 +0530 | [diff] [blame] | 388 | } |
| 389 | EXPORT_SYMBOL(nmk_config_pins_sleep); |
| 390 | |
Rabin Vincent | 5b327ed | 2010-05-27 12:29:50 +0100 | [diff] [blame] | 391 | /** |
Rabin Vincent | 81a3c29 | 2010-05-27 12:39:23 +0100 | [diff] [blame] | 392 | * nmk_gpio_set_slpm() - configure the sleep mode of a pin |
| 393 | * @gpio: pin number |
| 394 | * @mode: NMK_GPIO_SLPM_INPUT or NMK_GPIO_SLPM_NOCHANGE, |
| 395 | * |
| 396 | * Sets the sleep mode of a pin. If @mode is NMK_GPIO_SLPM_INPUT, the pin is |
| 397 | * changed to an input (with pullup/down enabled) in sleep and deep sleep. If |
| 398 | * @mode is NMK_GPIO_SLPM_NOCHANGE, the pin remains in the state it was |
| 399 | * configured even when in sleep and deep sleep. |
Rabin Vincent | 7e3f7e5 | 2010-09-02 11:28:05 +0100 | [diff] [blame] | 400 | * |
| 401 | * On DB8500v2 onwards, this setting loses the previous meaning and instead |
| 402 | * indicates if wakeup detection is enabled on the pin. Note that |
| 403 | * enable_irq_wake() will automatically enable wakeup detection. |
Rabin Vincent | 81a3c29 | 2010-05-27 12:39:23 +0100 | [diff] [blame] | 404 | */ |
| 405 | int nmk_gpio_set_slpm(int gpio, enum nmk_gpio_slpm mode) |
| 406 | { |
| 407 | struct nmk_gpio_chip *nmk_chip; |
| 408 | unsigned long flags; |
| 409 | |
Thomas Gleixner | 6845664a | 2011-03-24 13:25:22 +0100 | [diff] [blame] | 410 | nmk_chip = irq_get_chip_data(NOMADIK_GPIO_TO_IRQ(gpio)); |
Rabin Vincent | 81a3c29 | 2010-05-27 12:39:23 +0100 | [diff] [blame] | 411 | if (!nmk_chip) |
| 412 | return -EINVAL; |
| 413 | |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 414 | spin_lock_irqsave(&nmk_gpio_slpm_lock, flags); |
| 415 | spin_lock(&nmk_chip->lock); |
| 416 | |
Rabin Vincent | 81a3c29 | 2010-05-27 12:39:23 +0100 | [diff] [blame] | 417 | __nmk_gpio_set_slpm(nmk_chip, gpio - nmk_chip->chip.base, mode); |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 418 | |
| 419 | spin_unlock(&nmk_chip->lock); |
| 420 | spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags); |
Rabin Vincent | 81a3c29 | 2010-05-27 12:39:23 +0100 | [diff] [blame] | 421 | |
| 422 | return 0; |
| 423 | } |
| 424 | |
| 425 | /** |
Rabin Vincent | 5b327ed | 2010-05-27 12:29:50 +0100 | [diff] [blame] | 426 | * nmk_gpio_set_pull() - enable/disable pull up/down on a gpio |
| 427 | * @gpio: pin number |
| 428 | * @pull: one of NMK_GPIO_PULL_DOWN, NMK_GPIO_PULL_UP, and NMK_GPIO_PULL_NONE |
| 429 | * |
| 430 | * Enables/disables pull up/down on a specified pin. This only takes effect if |
| 431 | * the pin is configured as an input (either explicitly or by the alternate |
| 432 | * function). |
| 433 | * |
| 434 | * NOTE: If enabling the pull up/down, the caller must ensure that the GPIO is |
| 435 | * configured as an input. Otherwise, due to the way the controller registers |
| 436 | * work, this function will change the value output on the pin. |
| 437 | */ |
| 438 | int nmk_gpio_set_pull(int gpio, enum nmk_gpio_pull pull) |
| 439 | { |
| 440 | struct nmk_gpio_chip *nmk_chip; |
| 441 | unsigned long flags; |
| 442 | |
Thomas Gleixner | 6845664a | 2011-03-24 13:25:22 +0100 | [diff] [blame] | 443 | nmk_chip = irq_get_chip_data(NOMADIK_GPIO_TO_IRQ(gpio)); |
Rabin Vincent | 5b327ed | 2010-05-27 12:29:50 +0100 | [diff] [blame] | 444 | if (!nmk_chip) |
| 445 | return -EINVAL; |
| 446 | |
| 447 | spin_lock_irqsave(&nmk_chip->lock, flags); |
| 448 | __nmk_gpio_set_pull(nmk_chip, gpio - nmk_chip->chip.base, pull); |
| 449 | spin_unlock_irqrestore(&nmk_chip->lock, flags); |
| 450 | |
| 451 | return 0; |
| 452 | } |
| 453 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 454 | /* Mode functions */ |
Jonas Aaberg | 9c66ee6 | 2010-10-13 13:14:17 +0200 | [diff] [blame] | 455 | /** |
| 456 | * nmk_gpio_set_mode() - set the mux mode of a gpio pin |
| 457 | * @gpio: pin number |
| 458 | * @gpio_mode: one of NMK_GPIO_ALT_GPIO, NMK_GPIO_ALT_A, |
| 459 | * NMK_GPIO_ALT_B, and NMK_GPIO_ALT_C |
| 460 | * |
| 461 | * Sets the mode of the specified pin to one of the alternate functions or |
| 462 | * plain GPIO. |
| 463 | */ |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 464 | int nmk_gpio_set_mode(int gpio, int gpio_mode) |
| 465 | { |
| 466 | struct nmk_gpio_chip *nmk_chip; |
| 467 | unsigned long flags; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 468 | |
Thomas Gleixner | 6845664a | 2011-03-24 13:25:22 +0100 | [diff] [blame] | 469 | nmk_chip = irq_get_chip_data(NOMADIK_GPIO_TO_IRQ(gpio)); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 470 | if (!nmk_chip) |
| 471 | return -EINVAL; |
| 472 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 473 | spin_lock_irqsave(&nmk_chip->lock, flags); |
Rabin Vincent | 6f9a974 | 2010-06-02 05:50:28 +0100 | [diff] [blame] | 474 | __nmk_gpio_set_mode(nmk_chip, gpio - nmk_chip->chip.base, gpio_mode); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 475 | spin_unlock_irqrestore(&nmk_chip->lock, flags); |
| 476 | |
| 477 | return 0; |
| 478 | } |
| 479 | EXPORT_SYMBOL(nmk_gpio_set_mode); |
| 480 | |
| 481 | int nmk_gpio_get_mode(int gpio) |
| 482 | { |
| 483 | struct nmk_gpio_chip *nmk_chip; |
| 484 | u32 afunc, bfunc, bit; |
| 485 | |
Thomas Gleixner | 6845664a | 2011-03-24 13:25:22 +0100 | [diff] [blame] | 486 | nmk_chip = irq_get_chip_data(NOMADIK_GPIO_TO_IRQ(gpio)); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 487 | if (!nmk_chip) |
| 488 | return -EINVAL; |
| 489 | |
| 490 | bit = 1 << (gpio - nmk_chip->chip.base); |
| 491 | |
| 492 | afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & bit; |
| 493 | bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & bit; |
| 494 | |
| 495 | return (afunc ? NMK_GPIO_ALT_A : 0) | (bfunc ? NMK_GPIO_ALT_B : 0); |
| 496 | } |
| 497 | EXPORT_SYMBOL(nmk_gpio_get_mode); |
| 498 | |
| 499 | |
| 500 | /* IRQ functions */ |
| 501 | static inline int nmk_gpio_get_bitmask(int gpio) |
| 502 | { |
| 503 | return 1 << (gpio % 32); |
| 504 | } |
| 505 | |
Lennert Buytenhek | f272c00 | 2010-11-29 11:16:48 +0100 | [diff] [blame] | 506 | static void nmk_gpio_irq_ack(struct irq_data *d) |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 507 | { |
| 508 | int gpio; |
| 509 | struct nmk_gpio_chip *nmk_chip; |
| 510 | |
Lennert Buytenhek | f272c00 | 2010-11-29 11:16:48 +0100 | [diff] [blame] | 511 | gpio = NOMADIK_IRQ_TO_GPIO(d->irq); |
| 512 | nmk_chip = irq_data_get_irq_chip_data(d); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 513 | if (!nmk_chip) |
| 514 | return; |
| 515 | writel(nmk_gpio_get_bitmask(gpio), nmk_chip->addr + NMK_GPIO_IC); |
| 516 | } |
| 517 | |
Rabin Vincent | 4d4e20f | 2010-06-16 06:09:34 +0100 | [diff] [blame] | 518 | enum nmk_gpio_irq_type { |
| 519 | NORMAL, |
| 520 | WAKE, |
| 521 | }; |
| 522 | |
Rabin Vincent | 040e5ec | 2010-05-06 10:42:42 +0100 | [diff] [blame] | 523 | static void __nmk_gpio_irq_modify(struct nmk_gpio_chip *nmk_chip, |
Rabin Vincent | 4d4e20f | 2010-06-16 06:09:34 +0100 | [diff] [blame] | 524 | int gpio, enum nmk_gpio_irq_type which, |
| 525 | bool enable) |
Rabin Vincent | 040e5ec | 2010-05-06 10:42:42 +0100 | [diff] [blame] | 526 | { |
Rabin Vincent | 4d4e20f | 2010-06-16 06:09:34 +0100 | [diff] [blame] | 527 | u32 rimsc = which == WAKE ? NMK_GPIO_RWIMSC : NMK_GPIO_RIMSC; |
| 528 | u32 fimsc = which == WAKE ? NMK_GPIO_FWIMSC : NMK_GPIO_FIMSC; |
Rabin Vincent | 040e5ec | 2010-05-06 10:42:42 +0100 | [diff] [blame] | 529 | u32 bitmask = nmk_gpio_get_bitmask(gpio); |
| 530 | u32 reg; |
| 531 | |
| 532 | /* we must individually set/clear the two edges */ |
| 533 | if (nmk_chip->edge_rising & bitmask) { |
Rabin Vincent | 4d4e20f | 2010-06-16 06:09:34 +0100 | [diff] [blame] | 534 | reg = readl(nmk_chip->addr + rimsc); |
Rabin Vincent | 040e5ec | 2010-05-06 10:42:42 +0100 | [diff] [blame] | 535 | if (enable) |
| 536 | reg |= bitmask; |
| 537 | else |
| 538 | reg &= ~bitmask; |
Rabin Vincent | 4d4e20f | 2010-06-16 06:09:34 +0100 | [diff] [blame] | 539 | writel(reg, nmk_chip->addr + rimsc); |
Rabin Vincent | 040e5ec | 2010-05-06 10:42:42 +0100 | [diff] [blame] | 540 | } |
| 541 | if (nmk_chip->edge_falling & bitmask) { |
Rabin Vincent | 4d4e20f | 2010-06-16 06:09:34 +0100 | [diff] [blame] | 542 | reg = readl(nmk_chip->addr + fimsc); |
Rabin Vincent | 040e5ec | 2010-05-06 10:42:42 +0100 | [diff] [blame] | 543 | if (enable) |
| 544 | reg |= bitmask; |
| 545 | else |
| 546 | reg &= ~bitmask; |
Rabin Vincent | 4d4e20f | 2010-06-16 06:09:34 +0100 | [diff] [blame] | 547 | writel(reg, nmk_chip->addr + fimsc); |
Rabin Vincent | 040e5ec | 2010-05-06 10:42:42 +0100 | [diff] [blame] | 548 | } |
| 549 | } |
| 550 | |
Rabin Vincent | b9df468 | 2011-02-10 11:45:58 +0530 | [diff] [blame] | 551 | static void __nmk_gpio_set_wake(struct nmk_gpio_chip *nmk_chip, |
| 552 | int gpio, bool on) |
| 553 | { |
Rabin Vincent | b9df468 | 2011-02-10 11:45:58 +0530 | [diff] [blame] | 554 | __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, on); |
| 555 | } |
| 556 | |
| 557 | static int nmk_gpio_irq_maskunmask(struct irq_data *d, bool enable) |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 558 | { |
| 559 | int gpio; |
| 560 | struct nmk_gpio_chip *nmk_chip; |
| 561 | unsigned long flags; |
Rabin Vincent | 040e5ec | 2010-05-06 10:42:42 +0100 | [diff] [blame] | 562 | u32 bitmask; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 563 | |
Lennert Buytenhek | f272c00 | 2010-11-29 11:16:48 +0100 | [diff] [blame] | 564 | gpio = NOMADIK_IRQ_TO_GPIO(d->irq); |
| 565 | nmk_chip = irq_data_get_irq_chip_data(d); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 566 | bitmask = nmk_gpio_get_bitmask(gpio); |
| 567 | if (!nmk_chip) |
Rabin Vincent | 4d4e20f | 2010-06-16 06:09:34 +0100 | [diff] [blame] | 568 | return -EINVAL; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 569 | |
Thomas Gleixner | d1118f6 | 2011-03-24 12:38:50 +0100 | [diff] [blame] | 570 | if (enable) |
| 571 | nmk_chip->enabled |= bitmask; |
| 572 | else |
| 573 | nmk_chip->enabled &= ~bitmask; |
| 574 | |
Rabin Vincent | b9df468 | 2011-02-10 11:45:58 +0530 | [diff] [blame] | 575 | spin_lock_irqsave(&nmk_gpio_slpm_lock, flags); |
| 576 | spin_lock(&nmk_chip->lock); |
| 577 | |
| 578 | __nmk_gpio_irq_modify(nmk_chip, gpio, NORMAL, enable); |
| 579 | |
| 580 | if (!(nmk_chip->real_wake & bitmask)) |
| 581 | __nmk_gpio_set_wake(nmk_chip, gpio, enable); |
| 582 | |
| 583 | spin_unlock(&nmk_chip->lock); |
| 584 | spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags); |
Rabin Vincent | 4d4e20f | 2010-06-16 06:09:34 +0100 | [diff] [blame] | 585 | |
| 586 | return 0; |
Rabin Vincent | 040e5ec | 2010-05-06 10:42:42 +0100 | [diff] [blame] | 587 | } |
| 588 | |
Lennert Buytenhek | f272c00 | 2010-11-29 11:16:48 +0100 | [diff] [blame] | 589 | static void nmk_gpio_irq_mask(struct irq_data *d) |
Rabin Vincent | 040e5ec | 2010-05-06 10:42:42 +0100 | [diff] [blame] | 590 | { |
Rabin Vincent | b9df468 | 2011-02-10 11:45:58 +0530 | [diff] [blame] | 591 | nmk_gpio_irq_maskunmask(d, false); |
Rabin Vincent | 4d4e20f | 2010-06-16 06:09:34 +0100 | [diff] [blame] | 592 | } |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 593 | |
Lennert Buytenhek | f272c00 | 2010-11-29 11:16:48 +0100 | [diff] [blame] | 594 | static void nmk_gpio_irq_unmask(struct irq_data *d) |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 595 | { |
Rabin Vincent | b9df468 | 2011-02-10 11:45:58 +0530 | [diff] [blame] | 596 | nmk_gpio_irq_maskunmask(d, true); |
Rabin Vincent | 4d4e20f | 2010-06-16 06:09:34 +0100 | [diff] [blame] | 597 | } |
| 598 | |
Lennert Buytenhek | f272c00 | 2010-11-29 11:16:48 +0100 | [diff] [blame] | 599 | static int nmk_gpio_irq_set_wake(struct irq_data *d, unsigned int on) |
Rabin Vincent | 4d4e20f | 2010-06-16 06:09:34 +0100 | [diff] [blame] | 600 | { |
Rabin Vincent | 7e3f7e5 | 2010-09-02 11:28:05 +0100 | [diff] [blame] | 601 | struct nmk_gpio_chip *nmk_chip; |
| 602 | unsigned long flags; |
Rabin Vincent | b9df468 | 2011-02-10 11:45:58 +0530 | [diff] [blame] | 603 | u32 bitmask; |
Rabin Vincent | 7e3f7e5 | 2010-09-02 11:28:05 +0100 | [diff] [blame] | 604 | int gpio; |
| 605 | |
Lennert Buytenhek | f272c00 | 2010-11-29 11:16:48 +0100 | [diff] [blame] | 606 | gpio = NOMADIK_IRQ_TO_GPIO(d->irq); |
| 607 | nmk_chip = irq_data_get_irq_chip_data(d); |
Rabin Vincent | 7e3f7e5 | 2010-09-02 11:28:05 +0100 | [diff] [blame] | 608 | if (!nmk_chip) |
| 609 | return -EINVAL; |
Rabin Vincent | b9df468 | 2011-02-10 11:45:58 +0530 | [diff] [blame] | 610 | bitmask = nmk_gpio_get_bitmask(gpio); |
Rabin Vincent | 7e3f7e5 | 2010-09-02 11:28:05 +0100 | [diff] [blame] | 611 | |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 612 | spin_lock_irqsave(&nmk_gpio_slpm_lock, flags); |
| 613 | spin_lock(&nmk_chip->lock); |
| 614 | |
Thomas Gleixner | d1118f6 | 2011-03-24 12:38:50 +0100 | [diff] [blame] | 615 | if (!(nmk_chip->enabled & bitmask)) |
Rabin Vincent | b9df468 | 2011-02-10 11:45:58 +0530 | [diff] [blame] | 616 | __nmk_gpio_set_wake(nmk_chip, gpio, on); |
| 617 | |
| 618 | if (on) |
| 619 | nmk_chip->real_wake |= bitmask; |
| 620 | else |
| 621 | nmk_chip->real_wake &= ~bitmask; |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 622 | |
| 623 | spin_unlock(&nmk_chip->lock); |
| 624 | spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags); |
Rabin Vincent | 7e3f7e5 | 2010-09-02 11:28:05 +0100 | [diff] [blame] | 625 | |
| 626 | return 0; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 627 | } |
| 628 | |
Lennert Buytenhek | f272c00 | 2010-11-29 11:16:48 +0100 | [diff] [blame] | 629 | static int nmk_gpio_irq_set_type(struct irq_data *d, unsigned int type) |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 630 | { |
Thomas Gleixner | d1118f6 | 2011-03-24 12:38:50 +0100 | [diff] [blame] | 631 | bool enabled, wake = irqd_is_wakeup_set(d); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 632 | int gpio; |
| 633 | struct nmk_gpio_chip *nmk_chip; |
| 634 | unsigned long flags; |
| 635 | u32 bitmask; |
| 636 | |
Lennert Buytenhek | f272c00 | 2010-11-29 11:16:48 +0100 | [diff] [blame] | 637 | gpio = NOMADIK_IRQ_TO_GPIO(d->irq); |
| 638 | nmk_chip = irq_data_get_irq_chip_data(d); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 639 | bitmask = nmk_gpio_get_bitmask(gpio); |
| 640 | if (!nmk_chip) |
| 641 | return -EINVAL; |
| 642 | |
| 643 | if (type & IRQ_TYPE_LEVEL_HIGH) |
| 644 | return -EINVAL; |
| 645 | if (type & IRQ_TYPE_LEVEL_LOW) |
| 646 | return -EINVAL; |
| 647 | |
Thomas Gleixner | d1118f6 | 2011-03-24 12:38:50 +0100 | [diff] [blame] | 648 | enabled = nmk_chip->enabled & bitmask; |
| 649 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 650 | spin_lock_irqsave(&nmk_chip->lock, flags); |
| 651 | |
Rabin Vincent | 7a852d8 | 2010-05-06 10:43:55 +0100 | [diff] [blame] | 652 | if (enabled) |
Rabin Vincent | 4d4e20f | 2010-06-16 06:09:34 +0100 | [diff] [blame] | 653 | __nmk_gpio_irq_modify(nmk_chip, gpio, NORMAL, false); |
| 654 | |
Rabin Vincent | b9df468 | 2011-02-10 11:45:58 +0530 | [diff] [blame] | 655 | if (enabled || wake) |
Rabin Vincent | 4d4e20f | 2010-06-16 06:09:34 +0100 | [diff] [blame] | 656 | __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, false); |
Rabin Vincent | 7a852d8 | 2010-05-06 10:43:55 +0100 | [diff] [blame] | 657 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 658 | nmk_chip->edge_rising &= ~bitmask; |
| 659 | if (type & IRQ_TYPE_EDGE_RISING) |
| 660 | nmk_chip->edge_rising |= bitmask; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 661 | |
| 662 | nmk_chip->edge_falling &= ~bitmask; |
| 663 | if (type & IRQ_TYPE_EDGE_FALLING) |
| 664 | nmk_chip->edge_falling |= bitmask; |
Rabin Vincent | 7a852d8 | 2010-05-06 10:43:55 +0100 | [diff] [blame] | 665 | |
| 666 | if (enabled) |
Rabin Vincent | 4d4e20f | 2010-06-16 06:09:34 +0100 | [diff] [blame] | 667 | __nmk_gpio_irq_modify(nmk_chip, gpio, NORMAL, true); |
| 668 | |
Rabin Vincent | b9df468 | 2011-02-10 11:45:58 +0530 | [diff] [blame] | 669 | if (enabled || wake) |
Rabin Vincent | 4d4e20f | 2010-06-16 06:09:34 +0100 | [diff] [blame] | 670 | __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, true); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 671 | |
| 672 | spin_unlock_irqrestore(&nmk_chip->lock, flags); |
| 673 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 674 | return 0; |
| 675 | } |
| 676 | |
| 677 | static struct irq_chip nmk_gpio_irq_chip = { |
| 678 | .name = "Nomadik-GPIO", |
Lennert Buytenhek | f272c00 | 2010-11-29 11:16:48 +0100 | [diff] [blame] | 679 | .irq_ack = nmk_gpio_irq_ack, |
| 680 | .irq_mask = nmk_gpio_irq_mask, |
| 681 | .irq_unmask = nmk_gpio_irq_unmask, |
| 682 | .irq_set_type = nmk_gpio_irq_set_type, |
| 683 | .irq_set_wake = nmk_gpio_irq_set_wake, |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 684 | }; |
| 685 | |
Rabin Vincent | 33b744b | 2010-10-14 10:38:03 +0530 | [diff] [blame] | 686 | static void __nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc, |
| 687 | u32 status) |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 688 | { |
| 689 | struct nmk_gpio_chip *nmk_chip; |
Thomas Gleixner | 6845664a | 2011-03-24 13:25:22 +0100 | [diff] [blame] | 690 | struct irq_chip *host_chip = irq_get_chip(irq); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 691 | unsigned int first_irq; |
| 692 | |
Will Deacon | adfed15 | 2011-02-28 10:12:29 +0000 | [diff] [blame] | 693 | chained_irq_enter(host_chip, desc); |
Rabin Vincent | aaedaa2 | 2010-03-03 04:50:27 +0100 | [diff] [blame] | 694 | |
Thomas Gleixner | 6845664a | 2011-03-24 13:25:22 +0100 | [diff] [blame] | 695 | nmk_chip = irq_get_handler_data(irq); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 696 | first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base); |
Rabin Vincent | 33b744b | 2010-10-14 10:38:03 +0530 | [diff] [blame] | 697 | while (status) { |
| 698 | int bit = __ffs(status); |
| 699 | |
| 700 | generic_handle_irq(first_irq + bit); |
| 701 | status &= ~BIT(bit); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 702 | } |
Rabin Vincent | aaedaa2 | 2010-03-03 04:50:27 +0100 | [diff] [blame] | 703 | |
Will Deacon | adfed15 | 2011-02-28 10:12:29 +0000 | [diff] [blame] | 704 | chained_irq_exit(host_chip, desc); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 705 | } |
| 706 | |
Rabin Vincent | 33b744b | 2010-10-14 10:38:03 +0530 | [diff] [blame] | 707 | static void nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc) |
| 708 | { |
Thomas Gleixner | 6845664a | 2011-03-24 13:25:22 +0100 | [diff] [blame] | 709 | struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq); |
Rabin Vincent | 33b744b | 2010-10-14 10:38:03 +0530 | [diff] [blame] | 710 | u32 status = readl(nmk_chip->addr + NMK_GPIO_IS); |
| 711 | |
| 712 | __nmk_gpio_irq_handler(irq, desc, status); |
| 713 | } |
| 714 | |
| 715 | static void nmk_gpio_secondary_irq_handler(unsigned int irq, |
| 716 | struct irq_desc *desc) |
| 717 | { |
Thomas Gleixner | 6845664a | 2011-03-24 13:25:22 +0100 | [diff] [blame] | 718 | struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq); |
Rabin Vincent | 33b744b | 2010-10-14 10:38:03 +0530 | [diff] [blame] | 719 | u32 status = nmk_chip->get_secondary_status(nmk_chip->bank); |
| 720 | |
| 721 | __nmk_gpio_irq_handler(irq, desc, status); |
| 722 | } |
| 723 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 724 | static int nmk_gpio_init_irq(struct nmk_gpio_chip *nmk_chip) |
| 725 | { |
| 726 | unsigned int first_irq; |
| 727 | int i; |
| 728 | |
| 729 | first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base); |
Rabin Vincent | e493e06 | 2010-03-18 12:35:22 +0530 | [diff] [blame] | 730 | for (i = first_irq; i < first_irq + nmk_chip->chip.ngpio; i++) { |
Thomas Gleixner | f38c02f | 2011-03-24 13:35:09 +0100 | [diff] [blame] | 731 | irq_set_chip_and_handler(i, &nmk_gpio_irq_chip, |
| 732 | handle_edge_irq); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 733 | set_irq_flags(i, IRQF_VALID); |
Thomas Gleixner | 6845664a | 2011-03-24 13:25:22 +0100 | [diff] [blame] | 734 | irq_set_chip_data(i, nmk_chip); |
| 735 | irq_set_irq_type(i, IRQ_TYPE_EDGE_FALLING); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 736 | } |
Rabin Vincent | 33b744b | 2010-10-14 10:38:03 +0530 | [diff] [blame] | 737 | |
Thomas Gleixner | 6845664a | 2011-03-24 13:25:22 +0100 | [diff] [blame] | 738 | irq_set_chained_handler(nmk_chip->parent_irq, nmk_gpio_irq_handler); |
| 739 | irq_set_handler_data(nmk_chip->parent_irq, nmk_chip); |
Rabin Vincent | 33b744b | 2010-10-14 10:38:03 +0530 | [diff] [blame] | 740 | |
| 741 | if (nmk_chip->secondary_parent_irq >= 0) { |
Thomas Gleixner | 6845664a | 2011-03-24 13:25:22 +0100 | [diff] [blame] | 742 | irq_set_chained_handler(nmk_chip->secondary_parent_irq, |
Rabin Vincent | 33b744b | 2010-10-14 10:38:03 +0530 | [diff] [blame] | 743 | nmk_gpio_secondary_irq_handler); |
Thomas Gleixner | 6845664a | 2011-03-24 13:25:22 +0100 | [diff] [blame] | 744 | irq_set_handler_data(nmk_chip->secondary_parent_irq, nmk_chip); |
Rabin Vincent | 33b744b | 2010-10-14 10:38:03 +0530 | [diff] [blame] | 745 | } |
| 746 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 747 | return 0; |
| 748 | } |
| 749 | |
| 750 | /* I/O Functions */ |
| 751 | static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned offset) |
| 752 | { |
| 753 | struct nmk_gpio_chip *nmk_chip = |
| 754 | container_of(chip, struct nmk_gpio_chip, chip); |
| 755 | |
| 756 | writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC); |
| 757 | return 0; |
| 758 | } |
| 759 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 760 | static int nmk_gpio_get_input(struct gpio_chip *chip, unsigned offset) |
| 761 | { |
| 762 | struct nmk_gpio_chip *nmk_chip = |
| 763 | container_of(chip, struct nmk_gpio_chip, chip); |
| 764 | u32 bit = 1 << offset; |
| 765 | |
| 766 | return (readl(nmk_chip->addr + NMK_GPIO_DAT) & bit) != 0; |
| 767 | } |
| 768 | |
| 769 | static void nmk_gpio_set_output(struct gpio_chip *chip, unsigned offset, |
| 770 | int val) |
| 771 | { |
| 772 | struct nmk_gpio_chip *nmk_chip = |
| 773 | container_of(chip, struct nmk_gpio_chip, chip); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 774 | |
Rabin Vincent | 6720db7 | 2010-09-02 11:28:48 +0100 | [diff] [blame] | 775 | __nmk_gpio_set_output(nmk_chip, offset, val); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 776 | } |
| 777 | |
Rabin Vincent | 6647c6c | 2010-05-27 12:22:42 +0100 | [diff] [blame] | 778 | static int nmk_gpio_make_output(struct gpio_chip *chip, unsigned offset, |
| 779 | int val) |
| 780 | { |
| 781 | struct nmk_gpio_chip *nmk_chip = |
| 782 | container_of(chip, struct nmk_gpio_chip, chip); |
| 783 | |
Rabin Vincent | 6720db7 | 2010-09-02 11:28:48 +0100 | [diff] [blame] | 784 | __nmk_gpio_make_output(nmk_chip, offset, val); |
Rabin Vincent | 6647c6c | 2010-05-27 12:22:42 +0100 | [diff] [blame] | 785 | |
| 786 | return 0; |
| 787 | } |
| 788 | |
Rabin Vincent | 0d2aec9 | 2010-06-16 06:10:43 +0100 | [diff] [blame] | 789 | static int nmk_gpio_to_irq(struct gpio_chip *chip, unsigned offset) |
| 790 | { |
| 791 | struct nmk_gpio_chip *nmk_chip = |
| 792 | container_of(chip, struct nmk_gpio_chip, chip); |
| 793 | |
| 794 | return NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base) + offset; |
| 795 | } |
| 796 | |
Rabin Vincent | d0b543c | 2010-03-04 17:39:05 +0530 | [diff] [blame] | 797 | #ifdef CONFIG_DEBUG_FS |
| 798 | |
| 799 | #include <linux/seq_file.h> |
| 800 | |
| 801 | static void nmk_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip) |
| 802 | { |
| 803 | int mode; |
| 804 | unsigned i; |
| 805 | unsigned gpio = chip->base; |
| 806 | int is_out; |
| 807 | struct nmk_gpio_chip *nmk_chip = |
| 808 | container_of(chip, struct nmk_gpio_chip, chip); |
| 809 | const char *modes[] = { |
| 810 | [NMK_GPIO_ALT_GPIO] = "gpio", |
| 811 | [NMK_GPIO_ALT_A] = "altA", |
| 812 | [NMK_GPIO_ALT_B] = "altB", |
| 813 | [NMK_GPIO_ALT_C] = "altC", |
| 814 | }; |
| 815 | |
| 816 | for (i = 0; i < chip->ngpio; i++, gpio++) { |
| 817 | const char *label = gpiochip_is_requested(chip, i); |
| 818 | bool pull; |
| 819 | u32 bit = 1 << i; |
| 820 | |
Rabin Vincent | d0b543c | 2010-03-04 17:39:05 +0530 | [diff] [blame] | 821 | is_out = readl(nmk_chip->addr + NMK_GPIO_DIR) & bit; |
| 822 | pull = !(readl(nmk_chip->addr + NMK_GPIO_PDIS) & bit); |
| 823 | mode = nmk_gpio_get_mode(gpio); |
| 824 | seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s %s", |
Rabin Vincent | 8ea72a3 | 2011-05-24 23:07:09 +0200 | [diff] [blame] | 825 | gpio, label ?: "(none)", |
Rabin Vincent | d0b543c | 2010-03-04 17:39:05 +0530 | [diff] [blame] | 826 | is_out ? "out" : "in ", |
| 827 | chip->get |
| 828 | ? (chip->get(chip, i) ? "hi" : "lo") |
| 829 | : "? ", |
| 830 | (mode < 0) ? "unknown" : modes[mode], |
| 831 | pull ? "pull" : "none"); |
Rabin Vincent | 8ea72a3 | 2011-05-24 23:07:09 +0200 | [diff] [blame] | 832 | |
| 833 | if (label && !is_out) { |
| 834 | int irq = gpio_to_irq(gpio); |
| 835 | struct irq_desc *desc = irq_to_desc(irq); |
| 836 | |
| 837 | /* This races with request_irq(), set_irq_type(), |
| 838 | * and set_irq_wake() ... but those are "rare". |
| 839 | */ |
| 840 | if (irq >= 0 && desc->action) { |
| 841 | char *trigger; |
| 842 | u32 bitmask = nmk_gpio_get_bitmask(gpio); |
| 843 | |
| 844 | if (nmk_chip->edge_rising & bitmask) |
| 845 | trigger = "edge-rising"; |
| 846 | else if (nmk_chip->edge_falling & bitmask) |
| 847 | trigger = "edge-falling"; |
| 848 | else |
| 849 | trigger = "edge-undefined"; |
| 850 | |
| 851 | seq_printf(s, " irq-%d %s%s", |
| 852 | irq, trigger, |
| 853 | irqd_is_wakeup_set(&desc->irq_data) |
| 854 | ? " wakeup" : ""); |
| 855 | } |
| 856 | } |
| 857 | |
Rabin Vincent | d0b543c | 2010-03-04 17:39:05 +0530 | [diff] [blame] | 858 | seq_printf(s, "\n"); |
| 859 | } |
| 860 | } |
| 861 | |
| 862 | #else |
| 863 | #define nmk_gpio_dbg_show NULL |
| 864 | #endif |
| 865 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 866 | /* This structure is replicated for each GPIO block allocated at probe time */ |
| 867 | static struct gpio_chip nmk_gpio_template = { |
| 868 | .direction_input = nmk_gpio_make_input, |
| 869 | .get = nmk_gpio_get_input, |
| 870 | .direction_output = nmk_gpio_make_output, |
| 871 | .set = nmk_gpio_set_output, |
Rabin Vincent | 0d2aec9 | 2010-06-16 06:10:43 +0100 | [diff] [blame] | 872 | .to_irq = nmk_gpio_to_irq, |
Rabin Vincent | d0b543c | 2010-03-04 17:39:05 +0530 | [diff] [blame] | 873 | .dbg_show = nmk_gpio_dbg_show, |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 874 | .can_sleep = 0, |
| 875 | }; |
| 876 | |
Rabin Vincent | b9df468 | 2011-02-10 11:45:58 +0530 | [diff] [blame] | 877 | /* |
| 878 | * Called from the suspend/resume path to only keep the real wakeup interrupts |
| 879 | * (those that have had set_irq_wake() called on them) as wakeup interrupts, |
| 880 | * and not the rest of the interrupts which we needed to have as wakeups for |
| 881 | * cpuidle. |
| 882 | * |
| 883 | * PM ops are not used since this needs to be done at the end, after all the |
| 884 | * other drivers are done with their suspend callbacks. |
| 885 | */ |
| 886 | void nmk_gpio_wakeups_suspend(void) |
| 887 | { |
| 888 | int i; |
| 889 | |
| 890 | for (i = 0; i < NUM_BANKS; i++) { |
| 891 | struct nmk_gpio_chip *chip = nmk_gpio_chips[i]; |
| 892 | |
| 893 | if (!chip) |
| 894 | break; |
| 895 | |
| 896 | chip->rwimsc = readl(chip->addr + NMK_GPIO_RWIMSC); |
| 897 | chip->fwimsc = readl(chip->addr + NMK_GPIO_FWIMSC); |
| 898 | |
| 899 | writel(chip->rwimsc & chip->real_wake, |
| 900 | chip->addr + NMK_GPIO_RWIMSC); |
| 901 | writel(chip->fwimsc & chip->real_wake, |
| 902 | chip->addr + NMK_GPIO_FWIMSC); |
| 903 | |
| 904 | if (cpu_is_u8500v2()) { |
| 905 | chip->slpm = readl(chip->addr + NMK_GPIO_SLPC); |
| 906 | |
| 907 | /* 0 -> wakeup enable */ |
| 908 | writel(~chip->real_wake, chip->addr + NMK_GPIO_SLPC); |
| 909 | } |
| 910 | } |
| 911 | } |
| 912 | |
| 913 | void nmk_gpio_wakeups_resume(void) |
| 914 | { |
| 915 | int i; |
| 916 | |
| 917 | for (i = 0; i < NUM_BANKS; i++) { |
| 918 | struct nmk_gpio_chip *chip = nmk_gpio_chips[i]; |
| 919 | |
| 920 | if (!chip) |
| 921 | break; |
| 922 | |
| 923 | writel(chip->rwimsc, chip->addr + NMK_GPIO_RWIMSC); |
| 924 | writel(chip->fwimsc, chip->addr + NMK_GPIO_FWIMSC); |
| 925 | |
| 926 | if (cpu_is_u8500v2()) |
| 927 | writel(chip->slpm, chip->addr + NMK_GPIO_SLPC); |
| 928 | } |
| 929 | } |
| 930 | |
Rickard Andersson | bc6f5cf | 2011-05-24 23:07:17 +0200 | [diff] [blame^] | 931 | /* |
| 932 | * Read the pull up/pull down status. |
| 933 | * A bit set in 'pull_up' means that pull up |
| 934 | * is selected if pull is enabled in PDIS register. |
| 935 | * Note: only pull up/down set via this driver can |
| 936 | * be detected due to HW limitations. |
| 937 | */ |
| 938 | void nmk_gpio_read_pull(int gpio_bank, u32 *pull_up) |
| 939 | { |
| 940 | if (gpio_bank < NUM_BANKS) { |
| 941 | struct nmk_gpio_chip *chip = nmk_gpio_chips[gpio_bank]; |
| 942 | |
| 943 | if (!chip) |
| 944 | return; |
| 945 | |
| 946 | *pull_up = chip->pull_up; |
| 947 | } |
| 948 | } |
| 949 | |
Uwe Kleine-König | fd0d67d | 2010-09-02 16:13:35 +0100 | [diff] [blame] | 950 | static int __devinit nmk_gpio_probe(struct platform_device *dev) |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 951 | { |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 952 | struct nmk_gpio_platform_data *pdata = dev->dev.platform_data; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 953 | struct nmk_gpio_chip *nmk_chip; |
| 954 | struct gpio_chip *chip; |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 955 | struct resource *res; |
Rabin Vincent | af7dc22 | 2010-05-06 11:14:17 +0100 | [diff] [blame] | 956 | struct clk *clk; |
Rabin Vincent | 33b744b | 2010-10-14 10:38:03 +0530 | [diff] [blame] | 957 | int secondary_irq; |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 958 | int irq; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 959 | int ret; |
| 960 | |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 961 | if (!pdata) |
| 962 | return -ENODEV; |
| 963 | |
| 964 | res = platform_get_resource(dev, IORESOURCE_MEM, 0); |
| 965 | if (!res) { |
| 966 | ret = -ENOENT; |
| 967 | goto out; |
| 968 | } |
| 969 | |
| 970 | irq = platform_get_irq(dev, 0); |
| 971 | if (irq < 0) { |
| 972 | ret = irq; |
| 973 | goto out; |
| 974 | } |
| 975 | |
Rabin Vincent | 33b744b | 2010-10-14 10:38:03 +0530 | [diff] [blame] | 976 | secondary_irq = platform_get_irq(dev, 1); |
| 977 | if (secondary_irq >= 0 && !pdata->get_secondary_status) { |
| 978 | ret = -EINVAL; |
| 979 | goto out; |
| 980 | } |
| 981 | |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 982 | if (request_mem_region(res->start, resource_size(res), |
| 983 | dev_name(&dev->dev)) == NULL) { |
| 984 | ret = -EBUSY; |
| 985 | goto out; |
| 986 | } |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 987 | |
Rabin Vincent | af7dc22 | 2010-05-06 11:14:17 +0100 | [diff] [blame] | 988 | clk = clk_get(&dev->dev, NULL); |
| 989 | if (IS_ERR(clk)) { |
| 990 | ret = PTR_ERR(clk); |
| 991 | goto out_release; |
| 992 | } |
| 993 | |
| 994 | clk_enable(clk); |
| 995 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 996 | nmk_chip = kzalloc(sizeof(*nmk_chip), GFP_KERNEL); |
| 997 | if (!nmk_chip) { |
| 998 | ret = -ENOMEM; |
Rabin Vincent | af7dc22 | 2010-05-06 11:14:17 +0100 | [diff] [blame] | 999 | goto out_clk; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1000 | } |
| 1001 | /* |
| 1002 | * The virt address in nmk_chip->addr is in the nomadik register space, |
| 1003 | * so we can simply convert the resource address, without remapping |
| 1004 | */ |
Rabin Vincent | 33b744b | 2010-10-14 10:38:03 +0530 | [diff] [blame] | 1005 | nmk_chip->bank = dev->id; |
Rabin Vincent | af7dc22 | 2010-05-06 11:14:17 +0100 | [diff] [blame] | 1006 | nmk_chip->clk = clk; |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 1007 | nmk_chip->addr = io_p2v(res->start); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1008 | nmk_chip->chip = nmk_gpio_template; |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 1009 | nmk_chip->parent_irq = irq; |
Rabin Vincent | 33b744b | 2010-10-14 10:38:03 +0530 | [diff] [blame] | 1010 | nmk_chip->secondary_parent_irq = secondary_irq; |
| 1011 | nmk_chip->get_secondary_status = pdata->get_secondary_status; |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 1012 | nmk_chip->set_ioforce = pdata->set_ioforce; |
Rabin Vincent | c0fcb8d | 2010-03-03 04:48:54 +0100 | [diff] [blame] | 1013 | spin_lock_init(&nmk_chip->lock); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1014 | |
| 1015 | chip = &nmk_chip->chip; |
| 1016 | chip->base = pdata->first_gpio; |
Rabin Vincent | e493e06 | 2010-03-18 12:35:22 +0530 | [diff] [blame] | 1017 | chip->ngpio = pdata->num_gpio; |
Rabin Vincent | 8d568ae | 2010-12-08 11:07:54 +0530 | [diff] [blame] | 1018 | chip->label = pdata->name ?: dev_name(&dev->dev); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1019 | chip->dev = &dev->dev; |
| 1020 | chip->owner = THIS_MODULE; |
| 1021 | |
| 1022 | ret = gpiochip_add(&nmk_chip->chip); |
| 1023 | if (ret) |
| 1024 | goto out_free; |
| 1025 | |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 1026 | BUG_ON(nmk_chip->bank >= ARRAY_SIZE(nmk_gpio_chips)); |
| 1027 | |
| 1028 | nmk_gpio_chips[nmk_chip->bank] = nmk_chip; |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 1029 | platform_set_drvdata(dev, nmk_chip); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1030 | |
| 1031 | nmk_gpio_init_irq(nmk_chip); |
| 1032 | |
| 1033 | dev_info(&dev->dev, "Bits %i-%i at address %p\n", |
| 1034 | nmk_chip->chip.base, nmk_chip->chip.base+31, nmk_chip->addr); |
| 1035 | return 0; |
| 1036 | |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 1037 | out_free: |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1038 | kfree(nmk_chip); |
Rabin Vincent | af7dc22 | 2010-05-06 11:14:17 +0100 | [diff] [blame] | 1039 | out_clk: |
| 1040 | clk_disable(clk); |
| 1041 | clk_put(clk); |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 1042 | out_release: |
| 1043 | release_mem_region(res->start, resource_size(res)); |
| 1044 | out: |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1045 | dev_err(&dev->dev, "Failure %i for GPIO %i-%i\n", ret, |
| 1046 | pdata->first_gpio, pdata->first_gpio+31); |
| 1047 | return ret; |
| 1048 | } |
| 1049 | |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 1050 | static struct platform_driver nmk_gpio_driver = { |
| 1051 | .driver = { |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1052 | .owner = THIS_MODULE, |
| 1053 | .name = "gpio", |
Rabin Vincent | 5317e4d1 | 2011-02-10 09:29:53 +0530 | [diff] [blame] | 1054 | }, |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1055 | .probe = nmk_gpio_probe, |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1056 | }; |
| 1057 | |
| 1058 | static int __init nmk_gpio_init(void) |
| 1059 | { |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 1060 | return platform_driver_register(&nmk_gpio_driver); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1061 | } |
| 1062 | |
Rabin Vincent | 33f45ea | 2010-06-02 06:09:52 +0100 | [diff] [blame] | 1063 | core_initcall(nmk_gpio_init); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1064 | |
| 1065 | MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini"); |
| 1066 | MODULE_DESCRIPTION("Nomadik GPIO Driver"); |
| 1067 | MODULE_LICENSE("GPL"); |
| 1068 | |
| 1069 | |