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> |
Linus Walleij | 33d7864 | 2011-06-09 11:08:47 +0200 | [diff] [blame] | 7 | * Copyright (C) 2011 Linus Walleij <linus.walleij@linaro.org> |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | */ |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/device.h> |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 17 | #include <linux/platform_device.h> |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 18 | #include <linux/io.h> |
Rabin Vincent | af7dc22 | 2010-05-06 11:14:17 +0100 | [diff] [blame] | 19 | #include <linux/clk.h> |
| 20 | #include <linux/err.h> |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 21 | #include <linux/gpio.h> |
| 22 | #include <linux/spinlock.h> |
| 23 | #include <linux/interrupt.h> |
| 24 | #include <linux/irq.h> |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 25 | #include <linux/irqdomain.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 26 | #include <linux/slab.h> |
Linus Walleij | e98ea77 | 2012-04-26 23:57:25 +0200 | [diff] [blame^] | 27 | #include <linux/pinctrl/pinctrl.h> |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 28 | |
Will Deacon | adfed15 | 2011-02-28 10:12:29 +0000 | [diff] [blame] | 29 | #include <asm/mach/irq.h> |
| 30 | |
Rabin Vincent | 378be06 | 2010-06-02 06:06:29 +0100 | [diff] [blame] | 31 | #include <plat/pincfg.h> |
Linus Walleij | 0f33286 | 2011-08-22 08:33:30 +0100 | [diff] [blame] | 32 | #include <plat/gpio-nomadik.h> |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 33 | |
Linus Walleij | e98ea77 | 2012-04-26 23:57:25 +0200 | [diff] [blame^] | 34 | #include "pinctrl-nomadik.h" |
| 35 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 36 | /* |
| 37 | * The GPIO module in the Nomadik family of Systems-on-Chip is an |
| 38 | * AMBA device, managing 32 pins and alternate functions. The logic block |
Jonas Aaberg | 9c66ee6 | 2010-10-13 13:14:17 +0200 | [diff] [blame] | 39 | * is currently used in the Nomadik and ux500. |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 40 | * |
| 41 | * Symbols in this file are called "nmk_gpio" for "nomadik gpio" |
| 42 | */ |
| 43 | |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 44 | #define NMK_GPIO_PER_CHIP 32 |
| 45 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 46 | struct nmk_gpio_chip { |
| 47 | struct gpio_chip chip; |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 48 | struct irq_domain *domain; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 49 | void __iomem *addr; |
Rabin Vincent | af7dc22 | 2010-05-06 11:14:17 +0100 | [diff] [blame] | 50 | struct clk *clk; |
Rabin Vincent | 33b744b | 2010-10-14 10:38:03 +0530 | [diff] [blame] | 51 | unsigned int bank; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 52 | unsigned int parent_irq; |
Virupax Sadashivpetimath | 2c8bb0e | 2010-11-11 14:10:38 +0530 | [diff] [blame] | 53 | int secondary_parent_irq; |
Rabin Vincent | 33b744b | 2010-10-14 10:38:03 +0530 | [diff] [blame] | 54 | u32 (*get_secondary_status)(unsigned int bank); |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 55 | void (*set_ioforce)(bool enable); |
Rabin Vincent | c0fcb8d | 2010-03-03 04:48:54 +0100 | [diff] [blame] | 56 | spinlock_t lock; |
Linus Walleij | 33d7864 | 2011-06-09 11:08:47 +0200 | [diff] [blame] | 57 | bool sleepmode; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 58 | /* Keep track of configured edges */ |
| 59 | u32 edge_rising; |
| 60 | u32 edge_falling; |
Rabin Vincent | b9df468 | 2011-02-10 11:45:58 +0530 | [diff] [blame] | 61 | u32 real_wake; |
| 62 | u32 rwimsc; |
| 63 | u32 fwimsc; |
Rabin Vincent | 6c12fe8 | 2011-05-23 12:13:33 +0530 | [diff] [blame] | 64 | u32 rimsc; |
| 65 | u32 fimsc; |
Rickard Andersson | bc6f5cf | 2011-05-24 23:07:17 +0200 | [diff] [blame] | 66 | u32 pull_up; |
Rabin Vincent | ebc6178 | 2011-09-28 15:49:11 +0530 | [diff] [blame] | 67 | u32 lowemi; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 68 | }; |
| 69 | |
Linus Walleij | e98ea77 | 2012-04-26 23:57:25 +0200 | [diff] [blame^] | 70 | struct nmk_pinctrl { |
| 71 | struct device *dev; |
| 72 | struct pinctrl_dev *pctl; |
| 73 | const struct nmk_pinctrl_soc_data *soc; |
| 74 | }; |
| 75 | |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 76 | static struct nmk_gpio_chip * |
| 77 | nmk_gpio_chips[DIV_ROUND_UP(ARCH_NR_GPIOS, NMK_GPIO_PER_CHIP)]; |
| 78 | |
| 79 | static DEFINE_SPINLOCK(nmk_gpio_slpm_lock); |
| 80 | |
| 81 | #define NUM_BANKS ARRAY_SIZE(nmk_gpio_chips) |
| 82 | |
Rabin Vincent | 6f9a974 | 2010-06-02 05:50:28 +0100 | [diff] [blame] | 83 | static void __nmk_gpio_set_mode(struct nmk_gpio_chip *nmk_chip, |
| 84 | unsigned offset, int gpio_mode) |
| 85 | { |
| 86 | u32 bit = 1 << offset; |
| 87 | u32 afunc, bfunc; |
| 88 | |
| 89 | afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & ~bit; |
| 90 | bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & ~bit; |
| 91 | if (gpio_mode & NMK_GPIO_ALT_A) |
| 92 | afunc |= bit; |
| 93 | if (gpio_mode & NMK_GPIO_ALT_B) |
| 94 | bfunc |= bit; |
| 95 | writel(afunc, nmk_chip->addr + NMK_GPIO_AFSLA); |
| 96 | writel(bfunc, nmk_chip->addr + NMK_GPIO_AFSLB); |
| 97 | } |
| 98 | |
Rabin Vincent | 81a3c29 | 2010-05-27 12:39:23 +0100 | [diff] [blame] | 99 | static void __nmk_gpio_set_slpm(struct nmk_gpio_chip *nmk_chip, |
| 100 | unsigned offset, enum nmk_gpio_slpm mode) |
| 101 | { |
| 102 | u32 bit = 1 << offset; |
| 103 | u32 slpm; |
| 104 | |
| 105 | slpm = readl(nmk_chip->addr + NMK_GPIO_SLPC); |
| 106 | if (mode == NMK_GPIO_SLPM_NOCHANGE) |
| 107 | slpm |= bit; |
| 108 | else |
| 109 | slpm &= ~bit; |
| 110 | writel(slpm, nmk_chip->addr + NMK_GPIO_SLPC); |
| 111 | } |
| 112 | |
Rabin Vincent | 5b327ed | 2010-05-27 12:29:50 +0100 | [diff] [blame] | 113 | static void __nmk_gpio_set_pull(struct nmk_gpio_chip *nmk_chip, |
| 114 | unsigned offset, enum nmk_gpio_pull pull) |
| 115 | { |
| 116 | u32 bit = 1 << offset; |
| 117 | u32 pdis; |
| 118 | |
| 119 | pdis = readl(nmk_chip->addr + NMK_GPIO_PDIS); |
Rickard Andersson | bc6f5cf | 2011-05-24 23:07:17 +0200 | [diff] [blame] | 120 | if (pull == NMK_GPIO_PULL_NONE) { |
Rabin Vincent | 5b327ed | 2010-05-27 12:29:50 +0100 | [diff] [blame] | 121 | pdis |= bit; |
Rickard Andersson | bc6f5cf | 2011-05-24 23:07:17 +0200 | [diff] [blame] | 122 | nmk_chip->pull_up &= ~bit; |
| 123 | } else { |
Rabin Vincent | 5b327ed | 2010-05-27 12:29:50 +0100 | [diff] [blame] | 124 | pdis &= ~bit; |
Rickard Andersson | bc6f5cf | 2011-05-24 23:07:17 +0200 | [diff] [blame] | 125 | } |
| 126 | |
Rabin Vincent | 5b327ed | 2010-05-27 12:29:50 +0100 | [diff] [blame] | 127 | writel(pdis, nmk_chip->addr + NMK_GPIO_PDIS); |
| 128 | |
Rickard Andersson | bc6f5cf | 2011-05-24 23:07:17 +0200 | [diff] [blame] | 129 | if (pull == NMK_GPIO_PULL_UP) { |
| 130 | nmk_chip->pull_up |= bit; |
Rabin Vincent | 5b327ed | 2010-05-27 12:29:50 +0100 | [diff] [blame] | 131 | writel(bit, nmk_chip->addr + NMK_GPIO_DATS); |
Rickard Andersson | bc6f5cf | 2011-05-24 23:07:17 +0200 | [diff] [blame] | 132 | } else if (pull == NMK_GPIO_PULL_DOWN) { |
| 133 | nmk_chip->pull_up &= ~bit; |
Rabin Vincent | 5b327ed | 2010-05-27 12:29:50 +0100 | [diff] [blame] | 134 | writel(bit, nmk_chip->addr + NMK_GPIO_DATC); |
Rickard Andersson | bc6f5cf | 2011-05-24 23:07:17 +0200 | [diff] [blame] | 135 | } |
Rabin Vincent | 5b327ed | 2010-05-27 12:29:50 +0100 | [diff] [blame] | 136 | } |
| 137 | |
Rabin Vincent | ebc6178 | 2011-09-28 15:49:11 +0530 | [diff] [blame] | 138 | static void __nmk_gpio_set_lowemi(struct nmk_gpio_chip *nmk_chip, |
| 139 | unsigned offset, bool lowemi) |
| 140 | { |
| 141 | u32 bit = BIT(offset); |
| 142 | bool enabled = nmk_chip->lowemi & bit; |
| 143 | |
| 144 | if (lowemi == enabled) |
| 145 | return; |
| 146 | |
| 147 | if (lowemi) |
| 148 | nmk_chip->lowemi |= bit; |
| 149 | else |
| 150 | nmk_chip->lowemi &= ~bit; |
| 151 | |
| 152 | writel_relaxed(nmk_chip->lowemi, |
| 153 | nmk_chip->addr + NMK_GPIO_LOWEMI); |
| 154 | } |
| 155 | |
Rabin Vincent | 378be06 | 2010-06-02 06:06:29 +0100 | [diff] [blame] | 156 | static void __nmk_gpio_make_input(struct nmk_gpio_chip *nmk_chip, |
| 157 | unsigned offset) |
| 158 | { |
| 159 | writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC); |
| 160 | } |
| 161 | |
Rabin Vincent | 6720db7 | 2010-09-02 11:28:48 +0100 | [diff] [blame] | 162 | static void __nmk_gpio_set_output(struct nmk_gpio_chip *nmk_chip, |
| 163 | unsigned offset, int val) |
| 164 | { |
| 165 | if (val) |
| 166 | writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATS); |
| 167 | else |
| 168 | writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATC); |
| 169 | } |
| 170 | |
| 171 | static void __nmk_gpio_make_output(struct nmk_gpio_chip *nmk_chip, |
| 172 | unsigned offset, int val) |
| 173 | { |
| 174 | writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRS); |
| 175 | __nmk_gpio_set_output(nmk_chip, offset, val); |
| 176 | } |
| 177 | |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 178 | static void __nmk_gpio_set_mode_safe(struct nmk_gpio_chip *nmk_chip, |
| 179 | unsigned offset, int gpio_mode, |
| 180 | bool glitch) |
| 181 | { |
Rabin Vincent | 6c12fe8 | 2011-05-23 12:13:33 +0530 | [diff] [blame] | 182 | u32 rwimsc = nmk_chip->rwimsc; |
| 183 | u32 fwimsc = nmk_chip->fwimsc; |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 184 | |
| 185 | if (glitch && nmk_chip->set_ioforce) { |
| 186 | u32 bit = BIT(offset); |
| 187 | |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 188 | /* Prevent spurious wakeups */ |
| 189 | writel(rwimsc & ~bit, nmk_chip->addr + NMK_GPIO_RWIMSC); |
| 190 | writel(fwimsc & ~bit, nmk_chip->addr + NMK_GPIO_FWIMSC); |
| 191 | |
| 192 | nmk_chip->set_ioforce(true); |
| 193 | } |
| 194 | |
| 195 | __nmk_gpio_set_mode(nmk_chip, offset, gpio_mode); |
| 196 | |
| 197 | if (glitch && nmk_chip->set_ioforce) { |
| 198 | nmk_chip->set_ioforce(false); |
| 199 | |
| 200 | writel(rwimsc, nmk_chip->addr + NMK_GPIO_RWIMSC); |
| 201 | writel(fwimsc, nmk_chip->addr + NMK_GPIO_FWIMSC); |
| 202 | } |
| 203 | } |
| 204 | |
Rabin Vincent | 6c42ad1 | 2011-05-23 12:22:18 +0530 | [diff] [blame] | 205 | static void |
| 206 | nmk_gpio_disable_lazy_irq(struct nmk_gpio_chip *nmk_chip, unsigned offset) |
| 207 | { |
| 208 | u32 falling = nmk_chip->fimsc & BIT(offset); |
| 209 | u32 rising = nmk_chip->rimsc & BIT(offset); |
| 210 | int gpio = nmk_chip->chip.base + offset; |
| 211 | int irq = NOMADIK_GPIO_TO_IRQ(gpio); |
| 212 | struct irq_data *d = irq_get_irq_data(irq); |
| 213 | |
| 214 | if (!rising && !falling) |
| 215 | return; |
| 216 | |
| 217 | if (!d || !irqd_irq_disabled(d)) |
| 218 | return; |
| 219 | |
| 220 | if (rising) { |
| 221 | nmk_chip->rimsc &= ~BIT(offset); |
| 222 | writel_relaxed(nmk_chip->rimsc, |
| 223 | nmk_chip->addr + NMK_GPIO_RIMSC); |
| 224 | } |
| 225 | |
| 226 | if (falling) { |
| 227 | nmk_chip->fimsc &= ~BIT(offset); |
| 228 | writel_relaxed(nmk_chip->fimsc, |
| 229 | nmk_chip->addr + NMK_GPIO_FIMSC); |
| 230 | } |
| 231 | |
| 232 | dev_dbg(nmk_chip->chip.dev, "%d: clearing interrupt mask\n", gpio); |
| 233 | } |
| 234 | |
Rabin Vincent | 378be06 | 2010-06-02 06:06:29 +0100 | [diff] [blame] | 235 | 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] | 236 | pin_cfg_t cfg, bool sleep, unsigned int *slpmregs) |
Rabin Vincent | 378be06 | 2010-06-02 06:06:29 +0100 | [diff] [blame] | 237 | { |
| 238 | static const char *afnames[] = { |
| 239 | [NMK_GPIO_ALT_GPIO] = "GPIO", |
| 240 | [NMK_GPIO_ALT_A] = "A", |
| 241 | [NMK_GPIO_ALT_B] = "B", |
| 242 | [NMK_GPIO_ALT_C] = "C" |
| 243 | }; |
| 244 | static const char *pullnames[] = { |
| 245 | [NMK_GPIO_PULL_NONE] = "none", |
| 246 | [NMK_GPIO_PULL_UP] = "up", |
| 247 | [NMK_GPIO_PULL_DOWN] = "down", |
| 248 | [3] /* illegal */ = "??" |
| 249 | }; |
| 250 | static const char *slpmnames[] = { |
Rabin Vincent | 7e3f7e5 | 2010-09-02 11:28:05 +0100 | [diff] [blame] | 251 | [NMK_GPIO_SLPM_INPUT] = "input/wakeup", |
| 252 | [NMK_GPIO_SLPM_NOCHANGE] = "no-change/no-wakeup", |
Rabin Vincent | 378be06 | 2010-06-02 06:06:29 +0100 | [diff] [blame] | 253 | }; |
| 254 | |
| 255 | int pin = PIN_NUM(cfg); |
| 256 | int pull = PIN_PULL(cfg); |
| 257 | int af = PIN_ALT(cfg); |
| 258 | int slpm = PIN_SLPM(cfg); |
Rabin Vincent | 6720db7 | 2010-09-02 11:28:48 +0100 | [diff] [blame] | 259 | int output = PIN_DIR(cfg); |
| 260 | int val = PIN_VAL(cfg); |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 261 | bool glitch = af == NMK_GPIO_ALT_C; |
Rabin Vincent | 378be06 | 2010-06-02 06:06:29 +0100 | [diff] [blame] | 262 | |
Rabin Vincent | dacdc96 | 2010-12-03 20:35:37 +0530 | [diff] [blame] | 263 | dev_dbg(nmk_chip->chip.dev, "pin %d [%#lx]: af %s, pull %s, slpm %s (%s%s)\n", |
| 264 | pin, cfg, afnames[af], pullnames[pull], slpmnames[slpm], |
Rabin Vincent | 6720db7 | 2010-09-02 11:28:48 +0100 | [diff] [blame] | 265 | output ? "output " : "input", |
| 266 | output ? (val ? "high" : "low") : ""); |
Rabin Vincent | 378be06 | 2010-06-02 06:06:29 +0100 | [diff] [blame] | 267 | |
Rabin Vincent | dacdc96 | 2010-12-03 20:35:37 +0530 | [diff] [blame] | 268 | if (sleep) { |
| 269 | int slpm_pull = PIN_SLPM_PULL(cfg); |
| 270 | int slpm_output = PIN_SLPM_DIR(cfg); |
| 271 | int slpm_val = PIN_SLPM_VAL(cfg); |
| 272 | |
Rabin Vincent | 3546d15 | 2010-11-25 11:38:27 +0530 | [diff] [blame] | 273 | af = NMK_GPIO_ALT_GPIO; |
| 274 | |
Rabin Vincent | dacdc96 | 2010-12-03 20:35:37 +0530 | [diff] [blame] | 275 | /* |
| 276 | * The SLPM_* values are normal values + 1 to allow zero to |
| 277 | * mean "same as normal". |
| 278 | */ |
| 279 | if (slpm_pull) |
| 280 | pull = slpm_pull - 1; |
| 281 | if (slpm_output) |
| 282 | output = slpm_output - 1; |
| 283 | if (slpm_val) |
| 284 | val = slpm_val - 1; |
| 285 | |
| 286 | dev_dbg(nmk_chip->chip.dev, "pin %d: sleep pull %s, dir %s, val %s\n", |
| 287 | pin, |
| 288 | slpm_pull ? pullnames[pull] : "same", |
| 289 | slpm_output ? (output ? "output" : "input") : "same", |
| 290 | slpm_val ? (val ? "high" : "low") : "same"); |
| 291 | } |
| 292 | |
Rabin Vincent | 6720db7 | 2010-09-02 11:28:48 +0100 | [diff] [blame] | 293 | if (output) |
| 294 | __nmk_gpio_make_output(nmk_chip, offset, val); |
| 295 | else { |
| 296 | __nmk_gpio_make_input(nmk_chip, offset); |
| 297 | __nmk_gpio_set_pull(nmk_chip, offset, pull); |
| 298 | } |
| 299 | |
Rabin Vincent | ebc6178 | 2011-09-28 15:49:11 +0530 | [diff] [blame] | 300 | __nmk_gpio_set_lowemi(nmk_chip, offset, PIN_LOWEMI(cfg)); |
| 301 | |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 302 | /* |
Rabin Vincent | 6c42ad1 | 2011-05-23 12:22:18 +0530 | [diff] [blame] | 303 | * If the pin is switching to altfunc, and there was an interrupt |
| 304 | * installed on it which has been lazy disabled, actually mask the |
| 305 | * interrupt to prevent spurious interrupts that would occur while the |
| 306 | * pin is under control of the peripheral. Only SKE does this. |
| 307 | */ |
| 308 | if (af != NMK_GPIO_ALT_GPIO) |
| 309 | nmk_gpio_disable_lazy_irq(nmk_chip, offset); |
| 310 | |
| 311 | /* |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 312 | * If we've backed up the SLPM registers (glitch workaround), modify |
| 313 | * the backups since they will be restored. |
| 314 | */ |
| 315 | if (slpmregs) { |
| 316 | if (slpm == NMK_GPIO_SLPM_NOCHANGE) |
| 317 | slpmregs[nmk_chip->bank] |= BIT(offset); |
| 318 | else |
| 319 | slpmregs[nmk_chip->bank] &= ~BIT(offset); |
| 320 | } else |
| 321 | __nmk_gpio_set_slpm(nmk_chip, offset, slpm); |
| 322 | |
| 323 | __nmk_gpio_set_mode_safe(nmk_chip, offset, af, glitch); |
| 324 | } |
| 325 | |
| 326 | /* |
| 327 | * Safe sequence used to switch IOs between GPIO and Alternate-C mode: |
| 328 | * - Save SLPM registers |
| 329 | * - Set SLPM=0 for the IOs you want to switch and others to 1 |
| 330 | * - Configure the GPIO registers for the IOs that are being switched |
| 331 | * - Set IOFORCE=1 |
| 332 | * - Modify the AFLSA/B registers for the IOs that are being switched |
| 333 | * - Set IOFORCE=0 |
| 334 | * - Restore SLPM registers |
| 335 | * - Any spurious wake up event during switch sequence to be ignored and |
| 336 | * cleared |
| 337 | */ |
| 338 | static void nmk_gpio_glitch_slpm_init(unsigned int *slpm) |
| 339 | { |
| 340 | int i; |
| 341 | |
| 342 | for (i = 0; i < NUM_BANKS; i++) { |
| 343 | struct nmk_gpio_chip *chip = nmk_gpio_chips[i]; |
| 344 | unsigned int temp = slpm[i]; |
| 345 | |
| 346 | if (!chip) |
| 347 | break; |
| 348 | |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 349 | clk_enable(chip->clk); |
| 350 | |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 351 | slpm[i] = readl(chip->addr + NMK_GPIO_SLPC); |
| 352 | writel(temp, chip->addr + NMK_GPIO_SLPC); |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | static void nmk_gpio_glitch_slpm_restore(unsigned int *slpm) |
| 357 | { |
| 358 | int i; |
| 359 | |
| 360 | for (i = 0; i < NUM_BANKS; i++) { |
| 361 | struct nmk_gpio_chip *chip = nmk_gpio_chips[i]; |
| 362 | |
| 363 | if (!chip) |
| 364 | break; |
| 365 | |
| 366 | writel(slpm[i], chip->addr + NMK_GPIO_SLPC); |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 367 | |
| 368 | clk_disable(chip->clk); |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 369 | } |
| 370 | } |
| 371 | |
| 372 | static int __nmk_config_pins(pin_cfg_t *cfgs, int num, bool sleep) |
| 373 | { |
| 374 | static unsigned int slpm[NUM_BANKS]; |
| 375 | unsigned long flags; |
| 376 | bool glitch = false; |
| 377 | int ret = 0; |
| 378 | int i; |
| 379 | |
| 380 | for (i = 0; i < num; i++) { |
| 381 | if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C) { |
| 382 | glitch = true; |
| 383 | break; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | spin_lock_irqsave(&nmk_gpio_slpm_lock, flags); |
| 388 | |
| 389 | if (glitch) { |
| 390 | memset(slpm, 0xff, sizeof(slpm)); |
| 391 | |
| 392 | for (i = 0; i < num; i++) { |
| 393 | int pin = PIN_NUM(cfgs[i]); |
| 394 | int offset = pin % NMK_GPIO_PER_CHIP; |
| 395 | |
| 396 | if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C) |
| 397 | slpm[pin / NMK_GPIO_PER_CHIP] &= ~BIT(offset); |
| 398 | } |
| 399 | |
| 400 | nmk_gpio_glitch_slpm_init(slpm); |
| 401 | } |
| 402 | |
| 403 | for (i = 0; i < num; i++) { |
| 404 | struct nmk_gpio_chip *nmk_chip; |
| 405 | int pin = PIN_NUM(cfgs[i]); |
| 406 | |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 407 | nmk_chip = nmk_gpio_chips[pin / NMK_GPIO_PER_CHIP]; |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 408 | if (!nmk_chip) { |
| 409 | ret = -EINVAL; |
| 410 | break; |
| 411 | } |
| 412 | |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 413 | clk_enable(nmk_chip->clk); |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 414 | spin_lock(&nmk_chip->lock); |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 415 | __nmk_config_pin(nmk_chip, pin % NMK_GPIO_PER_CHIP, |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 416 | cfgs[i], sleep, glitch ? slpm : NULL); |
| 417 | spin_unlock(&nmk_chip->lock); |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 418 | clk_disable(nmk_chip->clk); |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | if (glitch) |
| 422 | nmk_gpio_glitch_slpm_restore(slpm); |
| 423 | |
| 424 | spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags); |
| 425 | |
| 426 | return ret; |
Rabin Vincent | 378be06 | 2010-06-02 06:06:29 +0100 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | /** |
| 430 | * nmk_config_pin - configure a pin's mux attributes |
| 431 | * @cfg: pin confguration |
| 432 | * |
| 433 | * Configures a pin's mode (alternate function or GPIO), its pull up status, |
| 434 | * and its sleep mode based on the specified configuration. The @cfg is |
| 435 | * usually one of the SoC specific macros defined in mach/<soc>-pins.h. These |
| 436 | * are constructed using, and can be further enhanced with, the macros in |
| 437 | * plat/pincfg.h. |
| 438 | * |
| 439 | * If a pin's mode is set to GPIO, it is configured as an input to avoid |
| 440 | * side-effects. The gpio can be manipulated later using standard GPIO API |
| 441 | * calls. |
| 442 | */ |
Rabin Vincent | dacdc96 | 2010-12-03 20:35:37 +0530 | [diff] [blame] | 443 | int nmk_config_pin(pin_cfg_t cfg, bool sleep) |
Rabin Vincent | 378be06 | 2010-06-02 06:06:29 +0100 | [diff] [blame] | 444 | { |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 445 | return __nmk_config_pins(&cfg, 1, sleep); |
Rabin Vincent | 378be06 | 2010-06-02 06:06:29 +0100 | [diff] [blame] | 446 | } |
| 447 | EXPORT_SYMBOL(nmk_config_pin); |
| 448 | |
| 449 | /** |
| 450 | * nmk_config_pins - configure several pins at once |
| 451 | * @cfgs: array of pin configurations |
| 452 | * @num: number of elments in the array |
| 453 | * |
| 454 | * Configures several pins using nmk_config_pin(). Refer to that function for |
| 455 | * further information. |
| 456 | */ |
| 457 | int nmk_config_pins(pin_cfg_t *cfgs, int num) |
| 458 | { |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 459 | return __nmk_config_pins(cfgs, num, false); |
Rabin Vincent | 378be06 | 2010-06-02 06:06:29 +0100 | [diff] [blame] | 460 | } |
| 461 | EXPORT_SYMBOL(nmk_config_pins); |
| 462 | |
Rabin Vincent | dacdc96 | 2010-12-03 20:35:37 +0530 | [diff] [blame] | 463 | int nmk_config_pins_sleep(pin_cfg_t *cfgs, int num) |
| 464 | { |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 465 | return __nmk_config_pins(cfgs, num, true); |
Rabin Vincent | dacdc96 | 2010-12-03 20:35:37 +0530 | [diff] [blame] | 466 | } |
| 467 | EXPORT_SYMBOL(nmk_config_pins_sleep); |
| 468 | |
Rabin Vincent | 5b327ed | 2010-05-27 12:29:50 +0100 | [diff] [blame] | 469 | /** |
Rabin Vincent | 81a3c29 | 2010-05-27 12:39:23 +0100 | [diff] [blame] | 470 | * nmk_gpio_set_slpm() - configure the sleep mode of a pin |
| 471 | * @gpio: pin number |
| 472 | * @mode: NMK_GPIO_SLPM_INPUT or NMK_GPIO_SLPM_NOCHANGE, |
| 473 | * |
Linus Walleij | 33d7864 | 2011-06-09 11:08:47 +0200 | [diff] [blame] | 474 | * This register is actually in the pinmux layer, not the GPIO block itself. |
| 475 | * The GPIO1B_SLPM register defines the GPIO mode when SLEEP/DEEP-SLEEP |
| 476 | * mode is entered (i.e. when signal IOFORCE is HIGH by the platform code). |
| 477 | * Each GPIO can be configured to be forced into GPIO mode when IOFORCE is |
| 478 | * HIGH, overriding the normal setting defined by GPIO_AFSELx registers. |
| 479 | * When IOFORCE returns LOW (by software, after SLEEP/DEEP-SLEEP exit), |
| 480 | * the GPIOs return to the normal setting defined by GPIO_AFSELx registers. |
Rabin Vincent | 7e3f7e5 | 2010-09-02 11:28:05 +0100 | [diff] [blame] | 481 | * |
Linus Walleij | 33d7864 | 2011-06-09 11:08:47 +0200 | [diff] [blame] | 482 | * If @mode is NMK_GPIO_SLPM_INPUT, the corresponding GPIO is switched to GPIO |
| 483 | * mode when signal IOFORCE is HIGH (i.e. when SLEEP/DEEP-SLEEP mode is |
| 484 | * entered) regardless of the altfunction selected. Also wake-up detection is |
| 485 | * ENABLED. |
| 486 | * |
| 487 | * If @mode is NMK_GPIO_SLPM_NOCHANGE, the corresponding GPIO remains |
| 488 | * controlled by NMK_GPIO_DATC, NMK_GPIO_DATS, NMK_GPIO_DIR, NMK_GPIO_PDIS |
| 489 | * (for altfunction GPIO) or respective on-chip peripherals (for other |
| 490 | * altfuncs) when IOFORCE is HIGH. Also wake-up detection DISABLED. |
| 491 | * |
| 492 | * Note that enable_irq_wake() will automatically enable wakeup detection. |
Rabin Vincent | 81a3c29 | 2010-05-27 12:39:23 +0100 | [diff] [blame] | 493 | */ |
| 494 | int nmk_gpio_set_slpm(int gpio, enum nmk_gpio_slpm mode) |
| 495 | { |
| 496 | struct nmk_gpio_chip *nmk_chip; |
| 497 | unsigned long flags; |
| 498 | |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 499 | nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP]; |
Rabin Vincent | 81a3c29 | 2010-05-27 12:39:23 +0100 | [diff] [blame] | 500 | if (!nmk_chip) |
| 501 | return -EINVAL; |
| 502 | |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 503 | clk_enable(nmk_chip->clk); |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 504 | spin_lock_irqsave(&nmk_gpio_slpm_lock, flags); |
| 505 | spin_lock(&nmk_chip->lock); |
| 506 | |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 507 | __nmk_gpio_set_slpm(nmk_chip, gpio % NMK_GPIO_PER_CHIP, mode); |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 508 | |
| 509 | spin_unlock(&nmk_chip->lock); |
| 510 | spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags); |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 511 | clk_disable(nmk_chip->clk); |
Rabin Vincent | 81a3c29 | 2010-05-27 12:39:23 +0100 | [diff] [blame] | 512 | |
| 513 | return 0; |
| 514 | } |
| 515 | |
| 516 | /** |
Rabin Vincent | 5b327ed | 2010-05-27 12:29:50 +0100 | [diff] [blame] | 517 | * nmk_gpio_set_pull() - enable/disable pull up/down on a gpio |
| 518 | * @gpio: pin number |
| 519 | * @pull: one of NMK_GPIO_PULL_DOWN, NMK_GPIO_PULL_UP, and NMK_GPIO_PULL_NONE |
| 520 | * |
| 521 | * Enables/disables pull up/down on a specified pin. This only takes effect if |
| 522 | * the pin is configured as an input (either explicitly or by the alternate |
| 523 | * function). |
| 524 | * |
| 525 | * NOTE: If enabling the pull up/down, the caller must ensure that the GPIO is |
| 526 | * configured as an input. Otherwise, due to the way the controller registers |
| 527 | * work, this function will change the value output on the pin. |
| 528 | */ |
| 529 | int nmk_gpio_set_pull(int gpio, enum nmk_gpio_pull pull) |
| 530 | { |
| 531 | struct nmk_gpio_chip *nmk_chip; |
| 532 | unsigned long flags; |
| 533 | |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 534 | nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP]; |
Rabin Vincent | 5b327ed | 2010-05-27 12:29:50 +0100 | [diff] [blame] | 535 | if (!nmk_chip) |
| 536 | return -EINVAL; |
| 537 | |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 538 | clk_enable(nmk_chip->clk); |
Rabin Vincent | 5b327ed | 2010-05-27 12:29:50 +0100 | [diff] [blame] | 539 | spin_lock_irqsave(&nmk_chip->lock, flags); |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 540 | __nmk_gpio_set_pull(nmk_chip, gpio % NMK_GPIO_PER_CHIP, pull); |
Rabin Vincent | 5b327ed | 2010-05-27 12:29:50 +0100 | [diff] [blame] | 541 | spin_unlock_irqrestore(&nmk_chip->lock, flags); |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 542 | clk_disable(nmk_chip->clk); |
Rabin Vincent | 5b327ed | 2010-05-27 12:29:50 +0100 | [diff] [blame] | 543 | |
| 544 | return 0; |
| 545 | } |
| 546 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 547 | /* Mode functions */ |
Jonas Aaberg | 9c66ee6 | 2010-10-13 13:14:17 +0200 | [diff] [blame] | 548 | /** |
| 549 | * nmk_gpio_set_mode() - set the mux mode of a gpio pin |
| 550 | * @gpio: pin number |
| 551 | * @gpio_mode: one of NMK_GPIO_ALT_GPIO, NMK_GPIO_ALT_A, |
| 552 | * NMK_GPIO_ALT_B, and NMK_GPIO_ALT_C |
| 553 | * |
| 554 | * Sets the mode of the specified pin to one of the alternate functions or |
| 555 | * plain GPIO. |
| 556 | */ |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 557 | int nmk_gpio_set_mode(int gpio, int gpio_mode) |
| 558 | { |
| 559 | struct nmk_gpio_chip *nmk_chip; |
| 560 | unsigned long flags; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 561 | |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 562 | nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP]; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 563 | if (!nmk_chip) |
| 564 | return -EINVAL; |
| 565 | |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 566 | clk_enable(nmk_chip->clk); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 567 | spin_lock_irqsave(&nmk_chip->lock, flags); |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 568 | __nmk_gpio_set_mode(nmk_chip, gpio % NMK_GPIO_PER_CHIP, gpio_mode); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 569 | spin_unlock_irqrestore(&nmk_chip->lock, flags); |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 570 | clk_disable(nmk_chip->clk); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 571 | |
| 572 | return 0; |
| 573 | } |
| 574 | EXPORT_SYMBOL(nmk_gpio_set_mode); |
| 575 | |
| 576 | int nmk_gpio_get_mode(int gpio) |
| 577 | { |
| 578 | struct nmk_gpio_chip *nmk_chip; |
| 579 | u32 afunc, bfunc, bit; |
| 580 | |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 581 | nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP]; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 582 | if (!nmk_chip) |
| 583 | return -EINVAL; |
| 584 | |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 585 | bit = 1 << (gpio % NMK_GPIO_PER_CHIP); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 586 | |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 587 | clk_enable(nmk_chip->clk); |
| 588 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 589 | afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & bit; |
| 590 | bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & bit; |
| 591 | |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 592 | clk_disable(nmk_chip->clk); |
| 593 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 594 | return (afunc ? NMK_GPIO_ALT_A : 0) | (bfunc ? NMK_GPIO_ALT_B : 0); |
| 595 | } |
| 596 | EXPORT_SYMBOL(nmk_gpio_get_mode); |
| 597 | |
| 598 | |
| 599 | /* IRQ functions */ |
| 600 | static inline int nmk_gpio_get_bitmask(int gpio) |
| 601 | { |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 602 | return 1 << (gpio % NMK_GPIO_PER_CHIP); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 603 | } |
| 604 | |
Lennert Buytenhek | f272c00 | 2010-11-29 11:16:48 +0100 | [diff] [blame] | 605 | static void nmk_gpio_irq_ack(struct irq_data *d) |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 606 | { |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 607 | struct nmk_gpio_chip *nmk_chip; |
| 608 | |
Lennert Buytenhek | f272c00 | 2010-11-29 11:16:48 +0100 | [diff] [blame] | 609 | nmk_chip = irq_data_get_irq_chip_data(d); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 610 | if (!nmk_chip) |
| 611 | return; |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 612 | |
| 613 | clk_enable(nmk_chip->clk); |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 614 | writel(nmk_gpio_get_bitmask(d->hwirq), nmk_chip->addr + NMK_GPIO_IC); |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 615 | clk_disable(nmk_chip->clk); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 616 | } |
| 617 | |
Rabin Vincent | 4d4e20f | 2010-06-16 06:09:34 +0100 | [diff] [blame] | 618 | enum nmk_gpio_irq_type { |
| 619 | NORMAL, |
| 620 | WAKE, |
| 621 | }; |
| 622 | |
Rabin Vincent | 040e5ec | 2010-05-06 10:42:42 +0100 | [diff] [blame] | 623 | static void __nmk_gpio_irq_modify(struct nmk_gpio_chip *nmk_chip, |
Rabin Vincent | 4d4e20f | 2010-06-16 06:09:34 +0100 | [diff] [blame] | 624 | int gpio, enum nmk_gpio_irq_type which, |
| 625 | bool enable) |
Rabin Vincent | 040e5ec | 2010-05-06 10:42:42 +0100 | [diff] [blame] | 626 | { |
| 627 | u32 bitmask = nmk_gpio_get_bitmask(gpio); |
Rabin Vincent | 6c12fe8 | 2011-05-23 12:13:33 +0530 | [diff] [blame] | 628 | u32 *rimscval; |
| 629 | u32 *fimscval; |
| 630 | u32 rimscreg; |
| 631 | u32 fimscreg; |
| 632 | |
| 633 | if (which == NORMAL) { |
| 634 | rimscreg = NMK_GPIO_RIMSC; |
| 635 | fimscreg = NMK_GPIO_FIMSC; |
| 636 | rimscval = &nmk_chip->rimsc; |
| 637 | fimscval = &nmk_chip->fimsc; |
| 638 | } else { |
| 639 | rimscreg = NMK_GPIO_RWIMSC; |
| 640 | fimscreg = NMK_GPIO_FWIMSC; |
| 641 | rimscval = &nmk_chip->rwimsc; |
| 642 | fimscval = &nmk_chip->fwimsc; |
| 643 | } |
Rabin Vincent | 040e5ec | 2010-05-06 10:42:42 +0100 | [diff] [blame] | 644 | |
| 645 | /* we must individually set/clear the two edges */ |
| 646 | if (nmk_chip->edge_rising & bitmask) { |
Rabin Vincent | 040e5ec | 2010-05-06 10:42:42 +0100 | [diff] [blame] | 647 | if (enable) |
Rabin Vincent | 6c12fe8 | 2011-05-23 12:13:33 +0530 | [diff] [blame] | 648 | *rimscval |= bitmask; |
Rabin Vincent | 040e5ec | 2010-05-06 10:42:42 +0100 | [diff] [blame] | 649 | else |
Rabin Vincent | 6c12fe8 | 2011-05-23 12:13:33 +0530 | [diff] [blame] | 650 | *rimscval &= ~bitmask; |
| 651 | writel(*rimscval, nmk_chip->addr + rimscreg); |
Rabin Vincent | 040e5ec | 2010-05-06 10:42:42 +0100 | [diff] [blame] | 652 | } |
| 653 | if (nmk_chip->edge_falling & bitmask) { |
Rabin Vincent | 040e5ec | 2010-05-06 10:42:42 +0100 | [diff] [blame] | 654 | if (enable) |
Rabin Vincent | 6c12fe8 | 2011-05-23 12:13:33 +0530 | [diff] [blame] | 655 | *fimscval |= bitmask; |
Rabin Vincent | 040e5ec | 2010-05-06 10:42:42 +0100 | [diff] [blame] | 656 | else |
Rabin Vincent | 6c12fe8 | 2011-05-23 12:13:33 +0530 | [diff] [blame] | 657 | *fimscval &= ~bitmask; |
| 658 | writel(*fimscval, nmk_chip->addr + fimscreg); |
Rabin Vincent | 040e5ec | 2010-05-06 10:42:42 +0100 | [diff] [blame] | 659 | } |
| 660 | } |
| 661 | |
Rabin Vincent | b9df468 | 2011-02-10 11:45:58 +0530 | [diff] [blame] | 662 | static void __nmk_gpio_set_wake(struct nmk_gpio_chip *nmk_chip, |
| 663 | int gpio, bool on) |
| 664 | { |
Rabin Vincent | b982ff0 | 2011-04-26 09:03:27 +0530 | [diff] [blame] | 665 | /* |
| 666 | * Ensure WAKEUP_ENABLE is on. No need to disable it if wakeup is |
| 667 | * disabled, since setting SLPM to 1 increases power consumption, and |
| 668 | * wakeup is anyhow controlled by the RIMSC and FIMSC registers. |
| 669 | */ |
| 670 | if (nmk_chip->sleepmode && on) { |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 671 | __nmk_gpio_set_slpm(nmk_chip, gpio % nmk_chip->chip.base, |
Rabin Vincent | b982ff0 | 2011-04-26 09:03:27 +0530 | [diff] [blame] | 672 | NMK_GPIO_SLPM_WAKEUP_ENABLE); |
Linus Walleij | 33d7864 | 2011-06-09 11:08:47 +0200 | [diff] [blame] | 673 | } |
| 674 | |
Rabin Vincent | b9df468 | 2011-02-10 11:45:58 +0530 | [diff] [blame] | 675 | __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, on); |
| 676 | } |
| 677 | |
| 678 | static int nmk_gpio_irq_maskunmask(struct irq_data *d, bool enable) |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 679 | { |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 680 | struct nmk_gpio_chip *nmk_chip; |
| 681 | unsigned long flags; |
Rabin Vincent | 040e5ec | 2010-05-06 10:42:42 +0100 | [diff] [blame] | 682 | u32 bitmask; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 683 | |
Lennert Buytenhek | f272c00 | 2010-11-29 11:16:48 +0100 | [diff] [blame] | 684 | nmk_chip = irq_data_get_irq_chip_data(d); |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 685 | bitmask = nmk_gpio_get_bitmask(d->hwirq); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 686 | if (!nmk_chip) |
Rabin Vincent | 4d4e20f | 2010-06-16 06:09:34 +0100 | [diff] [blame] | 687 | return -EINVAL; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 688 | |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 689 | clk_enable(nmk_chip->clk); |
Rabin Vincent | b9df468 | 2011-02-10 11:45:58 +0530 | [diff] [blame] | 690 | spin_lock_irqsave(&nmk_gpio_slpm_lock, flags); |
| 691 | spin_lock(&nmk_chip->lock); |
| 692 | |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 693 | __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, enable); |
Rabin Vincent | b9df468 | 2011-02-10 11:45:58 +0530 | [diff] [blame] | 694 | |
| 695 | if (!(nmk_chip->real_wake & bitmask)) |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 696 | __nmk_gpio_set_wake(nmk_chip, d->hwirq, enable); |
Rabin Vincent | b9df468 | 2011-02-10 11:45:58 +0530 | [diff] [blame] | 697 | |
| 698 | spin_unlock(&nmk_chip->lock); |
| 699 | spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags); |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 700 | clk_disable(nmk_chip->clk); |
Rabin Vincent | 4d4e20f | 2010-06-16 06:09:34 +0100 | [diff] [blame] | 701 | |
| 702 | return 0; |
Rabin Vincent | 040e5ec | 2010-05-06 10:42:42 +0100 | [diff] [blame] | 703 | } |
| 704 | |
Lennert Buytenhek | f272c00 | 2010-11-29 11:16:48 +0100 | [diff] [blame] | 705 | static void nmk_gpio_irq_mask(struct irq_data *d) |
Rabin Vincent | 040e5ec | 2010-05-06 10:42:42 +0100 | [diff] [blame] | 706 | { |
Rabin Vincent | b9df468 | 2011-02-10 11:45:58 +0530 | [diff] [blame] | 707 | nmk_gpio_irq_maskunmask(d, false); |
Rabin Vincent | 4d4e20f | 2010-06-16 06:09:34 +0100 | [diff] [blame] | 708 | } |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 709 | |
Lennert Buytenhek | f272c00 | 2010-11-29 11:16:48 +0100 | [diff] [blame] | 710 | static void nmk_gpio_irq_unmask(struct irq_data *d) |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 711 | { |
Rabin Vincent | b9df468 | 2011-02-10 11:45:58 +0530 | [diff] [blame] | 712 | nmk_gpio_irq_maskunmask(d, true); |
Rabin Vincent | 4d4e20f | 2010-06-16 06:09:34 +0100 | [diff] [blame] | 713 | } |
| 714 | |
Lennert Buytenhek | f272c00 | 2010-11-29 11:16:48 +0100 | [diff] [blame] | 715 | 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] | 716 | { |
Rabin Vincent | 7e3f7e5 | 2010-09-02 11:28:05 +0100 | [diff] [blame] | 717 | struct nmk_gpio_chip *nmk_chip; |
| 718 | unsigned long flags; |
Rabin Vincent | b9df468 | 2011-02-10 11:45:58 +0530 | [diff] [blame] | 719 | u32 bitmask; |
Rabin Vincent | 7e3f7e5 | 2010-09-02 11:28:05 +0100 | [diff] [blame] | 720 | |
Lennert Buytenhek | f272c00 | 2010-11-29 11:16:48 +0100 | [diff] [blame] | 721 | nmk_chip = irq_data_get_irq_chip_data(d); |
Rabin Vincent | 7e3f7e5 | 2010-09-02 11:28:05 +0100 | [diff] [blame] | 722 | if (!nmk_chip) |
| 723 | return -EINVAL; |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 724 | bitmask = nmk_gpio_get_bitmask(d->hwirq); |
Rabin Vincent | 7e3f7e5 | 2010-09-02 11:28:05 +0100 | [diff] [blame] | 725 | |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 726 | clk_enable(nmk_chip->clk); |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 727 | spin_lock_irqsave(&nmk_gpio_slpm_lock, flags); |
| 728 | spin_lock(&nmk_chip->lock); |
| 729 | |
Linus Walleij | 479a0c7 | 2011-09-20 10:50:15 +0200 | [diff] [blame] | 730 | if (irqd_irq_disabled(d)) |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 731 | __nmk_gpio_set_wake(nmk_chip, d->hwirq, on); |
Rabin Vincent | b9df468 | 2011-02-10 11:45:58 +0530 | [diff] [blame] | 732 | |
| 733 | if (on) |
| 734 | nmk_chip->real_wake |= bitmask; |
| 735 | else |
| 736 | nmk_chip->real_wake &= ~bitmask; |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 737 | |
| 738 | spin_unlock(&nmk_chip->lock); |
| 739 | spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags); |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 740 | clk_disable(nmk_chip->clk); |
Rabin Vincent | 7e3f7e5 | 2010-09-02 11:28:05 +0100 | [diff] [blame] | 741 | |
| 742 | return 0; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 743 | } |
| 744 | |
Lennert Buytenhek | f272c00 | 2010-11-29 11:16:48 +0100 | [diff] [blame] | 745 | 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] | 746 | { |
Linus Walleij | 479a0c7 | 2011-09-20 10:50:15 +0200 | [diff] [blame] | 747 | bool enabled = !irqd_irq_disabled(d); |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 748 | bool wake = irqd_is_wakeup_set(d); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 749 | struct nmk_gpio_chip *nmk_chip; |
| 750 | unsigned long flags; |
| 751 | u32 bitmask; |
| 752 | |
Lennert Buytenhek | f272c00 | 2010-11-29 11:16:48 +0100 | [diff] [blame] | 753 | nmk_chip = irq_data_get_irq_chip_data(d); |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 754 | bitmask = nmk_gpio_get_bitmask(d->hwirq); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 755 | if (!nmk_chip) |
| 756 | return -EINVAL; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 757 | if (type & IRQ_TYPE_LEVEL_HIGH) |
| 758 | return -EINVAL; |
| 759 | if (type & IRQ_TYPE_LEVEL_LOW) |
| 760 | return -EINVAL; |
| 761 | |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 762 | clk_enable(nmk_chip->clk); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 763 | spin_lock_irqsave(&nmk_chip->lock, flags); |
| 764 | |
Rabin Vincent | 7a852d8 | 2010-05-06 10:43:55 +0100 | [diff] [blame] | 765 | if (enabled) |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 766 | __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, false); |
Rabin Vincent | 4d4e20f | 2010-06-16 06:09:34 +0100 | [diff] [blame] | 767 | |
Rabin Vincent | b9df468 | 2011-02-10 11:45:58 +0530 | [diff] [blame] | 768 | if (enabled || wake) |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 769 | __nmk_gpio_irq_modify(nmk_chip, d->hwirq, WAKE, false); |
Rabin Vincent | 7a852d8 | 2010-05-06 10:43:55 +0100 | [diff] [blame] | 770 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 771 | nmk_chip->edge_rising &= ~bitmask; |
| 772 | if (type & IRQ_TYPE_EDGE_RISING) |
| 773 | nmk_chip->edge_rising |= bitmask; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 774 | |
| 775 | nmk_chip->edge_falling &= ~bitmask; |
| 776 | if (type & IRQ_TYPE_EDGE_FALLING) |
| 777 | nmk_chip->edge_falling |= bitmask; |
Rabin Vincent | 7a852d8 | 2010-05-06 10:43:55 +0100 | [diff] [blame] | 778 | |
| 779 | if (enabled) |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 780 | __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, true); |
Rabin Vincent | 4d4e20f | 2010-06-16 06:09:34 +0100 | [diff] [blame] | 781 | |
Rabin Vincent | b9df468 | 2011-02-10 11:45:58 +0530 | [diff] [blame] | 782 | if (enabled || wake) |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 783 | __nmk_gpio_irq_modify(nmk_chip, d->hwirq, WAKE, true); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 784 | |
| 785 | spin_unlock_irqrestore(&nmk_chip->lock, flags); |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 786 | clk_disable(nmk_chip->clk); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 787 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 788 | return 0; |
| 789 | } |
| 790 | |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 791 | static unsigned int nmk_gpio_irq_startup(struct irq_data *d) |
| 792 | { |
| 793 | struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d); |
| 794 | |
| 795 | clk_enable(nmk_chip->clk); |
| 796 | nmk_gpio_irq_unmask(d); |
| 797 | return 0; |
| 798 | } |
| 799 | |
| 800 | static void nmk_gpio_irq_shutdown(struct irq_data *d) |
| 801 | { |
| 802 | struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d); |
| 803 | |
| 804 | nmk_gpio_irq_mask(d); |
| 805 | clk_disable(nmk_chip->clk); |
| 806 | } |
| 807 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 808 | static struct irq_chip nmk_gpio_irq_chip = { |
| 809 | .name = "Nomadik-GPIO", |
Lennert Buytenhek | f272c00 | 2010-11-29 11:16:48 +0100 | [diff] [blame] | 810 | .irq_ack = nmk_gpio_irq_ack, |
| 811 | .irq_mask = nmk_gpio_irq_mask, |
| 812 | .irq_unmask = nmk_gpio_irq_unmask, |
| 813 | .irq_set_type = nmk_gpio_irq_set_type, |
| 814 | .irq_set_wake = nmk_gpio_irq_set_wake, |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 815 | .irq_startup = nmk_gpio_irq_startup, |
| 816 | .irq_shutdown = nmk_gpio_irq_shutdown, |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 817 | }; |
| 818 | |
Rabin Vincent | 33b744b | 2010-10-14 10:38:03 +0530 | [diff] [blame] | 819 | static void __nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc, |
| 820 | u32 status) |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 821 | { |
| 822 | struct nmk_gpio_chip *nmk_chip; |
Thomas Gleixner | 6845664a | 2011-03-24 13:25:22 +0100 | [diff] [blame] | 823 | struct irq_chip *host_chip = irq_get_chip(irq); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 824 | unsigned int first_irq; |
| 825 | |
Will Deacon | adfed15 | 2011-02-28 10:12:29 +0000 | [diff] [blame] | 826 | chained_irq_enter(host_chip, desc); |
Rabin Vincent | aaedaa2 | 2010-03-03 04:50:27 +0100 | [diff] [blame] | 827 | |
Thomas Gleixner | 6845664a | 2011-03-24 13:25:22 +0100 | [diff] [blame] | 828 | nmk_chip = irq_get_handler_data(irq); |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 829 | first_irq = nmk_chip->domain->revmap_data.legacy.first_irq; |
Rabin Vincent | 33b744b | 2010-10-14 10:38:03 +0530 | [diff] [blame] | 830 | while (status) { |
| 831 | int bit = __ffs(status); |
| 832 | |
| 833 | generic_handle_irq(first_irq + bit); |
| 834 | status &= ~BIT(bit); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 835 | } |
Rabin Vincent | aaedaa2 | 2010-03-03 04:50:27 +0100 | [diff] [blame] | 836 | |
Will Deacon | adfed15 | 2011-02-28 10:12:29 +0000 | [diff] [blame] | 837 | chained_irq_exit(host_chip, desc); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 838 | } |
| 839 | |
Rabin Vincent | 33b744b | 2010-10-14 10:38:03 +0530 | [diff] [blame] | 840 | static void nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc) |
| 841 | { |
Thomas Gleixner | 6845664a | 2011-03-24 13:25:22 +0100 | [diff] [blame] | 842 | struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq); |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 843 | u32 status; |
| 844 | |
| 845 | clk_enable(nmk_chip->clk); |
| 846 | status = readl(nmk_chip->addr + NMK_GPIO_IS); |
| 847 | clk_disable(nmk_chip->clk); |
Rabin Vincent | 33b744b | 2010-10-14 10:38:03 +0530 | [diff] [blame] | 848 | |
| 849 | __nmk_gpio_irq_handler(irq, desc, status); |
| 850 | } |
| 851 | |
| 852 | static void nmk_gpio_secondary_irq_handler(unsigned int irq, |
| 853 | struct irq_desc *desc) |
| 854 | { |
Thomas Gleixner | 6845664a | 2011-03-24 13:25:22 +0100 | [diff] [blame] | 855 | struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq); |
Rabin Vincent | 33b744b | 2010-10-14 10:38:03 +0530 | [diff] [blame] | 856 | u32 status = nmk_chip->get_secondary_status(nmk_chip->bank); |
| 857 | |
| 858 | __nmk_gpio_irq_handler(irq, desc, status); |
| 859 | } |
| 860 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 861 | static int nmk_gpio_init_irq(struct nmk_gpio_chip *nmk_chip) |
| 862 | { |
Thomas Gleixner | 6845664a | 2011-03-24 13:25:22 +0100 | [diff] [blame] | 863 | irq_set_chained_handler(nmk_chip->parent_irq, nmk_gpio_irq_handler); |
| 864 | irq_set_handler_data(nmk_chip->parent_irq, nmk_chip); |
Rabin Vincent | 33b744b | 2010-10-14 10:38:03 +0530 | [diff] [blame] | 865 | |
| 866 | if (nmk_chip->secondary_parent_irq >= 0) { |
Thomas Gleixner | 6845664a | 2011-03-24 13:25:22 +0100 | [diff] [blame] | 867 | irq_set_chained_handler(nmk_chip->secondary_parent_irq, |
Rabin Vincent | 33b744b | 2010-10-14 10:38:03 +0530 | [diff] [blame] | 868 | nmk_gpio_secondary_irq_handler); |
Thomas Gleixner | 6845664a | 2011-03-24 13:25:22 +0100 | [diff] [blame] | 869 | irq_set_handler_data(nmk_chip->secondary_parent_irq, nmk_chip); |
Rabin Vincent | 33b744b | 2010-10-14 10:38:03 +0530 | [diff] [blame] | 870 | } |
| 871 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 872 | return 0; |
| 873 | } |
| 874 | |
| 875 | /* I/O Functions */ |
| 876 | static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned offset) |
| 877 | { |
| 878 | struct nmk_gpio_chip *nmk_chip = |
| 879 | container_of(chip, struct nmk_gpio_chip, chip); |
| 880 | |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 881 | clk_enable(nmk_chip->clk); |
| 882 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 883 | writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC); |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 884 | |
| 885 | clk_disable(nmk_chip->clk); |
| 886 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 887 | return 0; |
| 888 | } |
| 889 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 890 | static int nmk_gpio_get_input(struct gpio_chip *chip, unsigned offset) |
| 891 | { |
| 892 | struct nmk_gpio_chip *nmk_chip = |
| 893 | container_of(chip, struct nmk_gpio_chip, chip); |
| 894 | u32 bit = 1 << offset; |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 895 | int value; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 896 | |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 897 | clk_enable(nmk_chip->clk); |
| 898 | |
| 899 | value = (readl(nmk_chip->addr + NMK_GPIO_DAT) & bit) != 0; |
| 900 | |
| 901 | clk_disable(nmk_chip->clk); |
| 902 | |
| 903 | return value; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | static void nmk_gpio_set_output(struct gpio_chip *chip, unsigned offset, |
| 907 | int val) |
| 908 | { |
| 909 | struct nmk_gpio_chip *nmk_chip = |
| 910 | container_of(chip, struct nmk_gpio_chip, chip); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 911 | |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 912 | clk_enable(nmk_chip->clk); |
| 913 | |
Rabin Vincent | 6720db7 | 2010-09-02 11:28:48 +0100 | [diff] [blame] | 914 | __nmk_gpio_set_output(nmk_chip, offset, val); |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 915 | |
| 916 | clk_disable(nmk_chip->clk); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 917 | } |
| 918 | |
Rabin Vincent | 6647c6c | 2010-05-27 12:22:42 +0100 | [diff] [blame] | 919 | static int nmk_gpio_make_output(struct gpio_chip *chip, unsigned offset, |
| 920 | int val) |
| 921 | { |
| 922 | struct nmk_gpio_chip *nmk_chip = |
| 923 | container_of(chip, struct nmk_gpio_chip, chip); |
| 924 | |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 925 | clk_enable(nmk_chip->clk); |
| 926 | |
Rabin Vincent | 6720db7 | 2010-09-02 11:28:48 +0100 | [diff] [blame] | 927 | __nmk_gpio_make_output(nmk_chip, offset, val); |
Rabin Vincent | 6647c6c | 2010-05-27 12:22:42 +0100 | [diff] [blame] | 928 | |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 929 | clk_disable(nmk_chip->clk); |
| 930 | |
Rabin Vincent | 6647c6c | 2010-05-27 12:22:42 +0100 | [diff] [blame] | 931 | return 0; |
| 932 | } |
| 933 | |
Rabin Vincent | 0d2aec9 | 2010-06-16 06:10:43 +0100 | [diff] [blame] | 934 | static int nmk_gpio_to_irq(struct gpio_chip *chip, unsigned offset) |
| 935 | { |
| 936 | struct nmk_gpio_chip *nmk_chip = |
| 937 | container_of(chip, struct nmk_gpio_chip, chip); |
| 938 | |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 939 | return irq_find_mapping(nmk_chip->domain, offset); |
Rabin Vincent | 0d2aec9 | 2010-06-16 06:10:43 +0100 | [diff] [blame] | 940 | } |
| 941 | |
Rabin Vincent | d0b543c | 2010-03-04 17:39:05 +0530 | [diff] [blame] | 942 | #ifdef CONFIG_DEBUG_FS |
| 943 | |
| 944 | #include <linux/seq_file.h> |
| 945 | |
| 946 | static void nmk_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip) |
| 947 | { |
| 948 | int mode; |
| 949 | unsigned i; |
| 950 | unsigned gpio = chip->base; |
| 951 | int is_out; |
| 952 | struct nmk_gpio_chip *nmk_chip = |
| 953 | container_of(chip, struct nmk_gpio_chip, chip); |
| 954 | const char *modes[] = { |
| 955 | [NMK_GPIO_ALT_GPIO] = "gpio", |
| 956 | [NMK_GPIO_ALT_A] = "altA", |
| 957 | [NMK_GPIO_ALT_B] = "altB", |
| 958 | [NMK_GPIO_ALT_C] = "altC", |
| 959 | }; |
| 960 | |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 961 | clk_enable(nmk_chip->clk); |
| 962 | |
Rabin Vincent | d0b543c | 2010-03-04 17:39:05 +0530 | [diff] [blame] | 963 | for (i = 0; i < chip->ngpio; i++, gpio++) { |
| 964 | const char *label = gpiochip_is_requested(chip, i); |
| 965 | bool pull; |
| 966 | u32 bit = 1 << i; |
| 967 | |
Rabin Vincent | d0b543c | 2010-03-04 17:39:05 +0530 | [diff] [blame] | 968 | is_out = readl(nmk_chip->addr + NMK_GPIO_DIR) & bit; |
| 969 | pull = !(readl(nmk_chip->addr + NMK_GPIO_PDIS) & bit); |
| 970 | mode = nmk_gpio_get_mode(gpio); |
| 971 | seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s %s", |
Rabin Vincent | 8ea72a3 | 2011-05-24 23:07:09 +0200 | [diff] [blame] | 972 | gpio, label ?: "(none)", |
Rabin Vincent | d0b543c | 2010-03-04 17:39:05 +0530 | [diff] [blame] | 973 | is_out ? "out" : "in ", |
| 974 | chip->get |
| 975 | ? (chip->get(chip, i) ? "hi" : "lo") |
| 976 | : "? ", |
| 977 | (mode < 0) ? "unknown" : modes[mode], |
| 978 | pull ? "pull" : "none"); |
Rabin Vincent | 8ea72a3 | 2011-05-24 23:07:09 +0200 | [diff] [blame] | 979 | |
| 980 | if (label && !is_out) { |
| 981 | int irq = gpio_to_irq(gpio); |
| 982 | struct irq_desc *desc = irq_to_desc(irq); |
| 983 | |
| 984 | /* This races with request_irq(), set_irq_type(), |
| 985 | * and set_irq_wake() ... but those are "rare". |
| 986 | */ |
| 987 | if (irq >= 0 && desc->action) { |
| 988 | char *trigger; |
| 989 | u32 bitmask = nmk_gpio_get_bitmask(gpio); |
| 990 | |
| 991 | if (nmk_chip->edge_rising & bitmask) |
| 992 | trigger = "edge-rising"; |
| 993 | else if (nmk_chip->edge_falling & bitmask) |
| 994 | trigger = "edge-falling"; |
| 995 | else |
| 996 | trigger = "edge-undefined"; |
| 997 | |
| 998 | seq_printf(s, " irq-%d %s%s", |
| 999 | irq, trigger, |
| 1000 | irqd_is_wakeup_set(&desc->irq_data) |
| 1001 | ? " wakeup" : ""); |
| 1002 | } |
| 1003 | } |
| 1004 | |
Rabin Vincent | d0b543c | 2010-03-04 17:39:05 +0530 | [diff] [blame] | 1005 | seq_printf(s, "\n"); |
| 1006 | } |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 1007 | |
| 1008 | clk_disable(nmk_chip->clk); |
Rabin Vincent | d0b543c | 2010-03-04 17:39:05 +0530 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | #else |
| 1012 | #define nmk_gpio_dbg_show NULL |
| 1013 | #endif |
| 1014 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1015 | /* This structure is replicated for each GPIO block allocated at probe time */ |
| 1016 | static struct gpio_chip nmk_gpio_template = { |
| 1017 | .direction_input = nmk_gpio_make_input, |
| 1018 | .get = nmk_gpio_get_input, |
| 1019 | .direction_output = nmk_gpio_make_output, |
| 1020 | .set = nmk_gpio_set_output, |
Rabin Vincent | 0d2aec9 | 2010-06-16 06:10:43 +0100 | [diff] [blame] | 1021 | .to_irq = nmk_gpio_to_irq, |
Rabin Vincent | d0b543c | 2010-03-04 17:39:05 +0530 | [diff] [blame] | 1022 | .dbg_show = nmk_gpio_dbg_show, |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1023 | .can_sleep = 0, |
| 1024 | }; |
| 1025 | |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 1026 | void nmk_gpio_clocks_enable(void) |
| 1027 | { |
| 1028 | int i; |
| 1029 | |
| 1030 | for (i = 0; i < NUM_BANKS; i++) { |
| 1031 | struct nmk_gpio_chip *chip = nmk_gpio_chips[i]; |
| 1032 | |
| 1033 | if (!chip) |
| 1034 | continue; |
| 1035 | |
| 1036 | clk_enable(chip->clk); |
| 1037 | } |
| 1038 | } |
| 1039 | |
| 1040 | void nmk_gpio_clocks_disable(void) |
| 1041 | { |
| 1042 | int i; |
| 1043 | |
| 1044 | for (i = 0; i < NUM_BANKS; i++) { |
| 1045 | struct nmk_gpio_chip *chip = nmk_gpio_chips[i]; |
| 1046 | |
| 1047 | if (!chip) |
| 1048 | continue; |
| 1049 | |
| 1050 | clk_disable(chip->clk); |
| 1051 | } |
| 1052 | } |
| 1053 | |
Rabin Vincent | b9df468 | 2011-02-10 11:45:58 +0530 | [diff] [blame] | 1054 | /* |
| 1055 | * Called from the suspend/resume path to only keep the real wakeup interrupts |
| 1056 | * (those that have had set_irq_wake() called on them) as wakeup interrupts, |
| 1057 | * and not the rest of the interrupts which we needed to have as wakeups for |
| 1058 | * cpuidle. |
| 1059 | * |
| 1060 | * PM ops are not used since this needs to be done at the end, after all the |
| 1061 | * other drivers are done with their suspend callbacks. |
| 1062 | */ |
| 1063 | void nmk_gpio_wakeups_suspend(void) |
| 1064 | { |
| 1065 | int i; |
| 1066 | |
| 1067 | for (i = 0; i < NUM_BANKS; i++) { |
| 1068 | struct nmk_gpio_chip *chip = nmk_gpio_chips[i]; |
| 1069 | |
| 1070 | if (!chip) |
| 1071 | break; |
| 1072 | |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 1073 | clk_enable(chip->clk); |
| 1074 | |
Rabin Vincent | b9df468 | 2011-02-10 11:45:58 +0530 | [diff] [blame] | 1075 | writel(chip->rwimsc & chip->real_wake, |
| 1076 | chip->addr + NMK_GPIO_RWIMSC); |
| 1077 | writel(chip->fwimsc & chip->real_wake, |
| 1078 | chip->addr + NMK_GPIO_FWIMSC); |
| 1079 | |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 1080 | clk_disable(chip->clk); |
Rabin Vincent | b9df468 | 2011-02-10 11:45:58 +0530 | [diff] [blame] | 1081 | } |
| 1082 | } |
| 1083 | |
| 1084 | void nmk_gpio_wakeups_resume(void) |
| 1085 | { |
| 1086 | int i; |
| 1087 | |
| 1088 | for (i = 0; i < NUM_BANKS; i++) { |
| 1089 | struct nmk_gpio_chip *chip = nmk_gpio_chips[i]; |
| 1090 | |
| 1091 | if (!chip) |
| 1092 | break; |
| 1093 | |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 1094 | clk_enable(chip->clk); |
| 1095 | |
Rabin Vincent | b9df468 | 2011-02-10 11:45:58 +0530 | [diff] [blame] | 1096 | writel(chip->rwimsc, chip->addr + NMK_GPIO_RWIMSC); |
| 1097 | writel(chip->fwimsc, chip->addr + NMK_GPIO_FWIMSC); |
| 1098 | |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 1099 | clk_disable(chip->clk); |
Rabin Vincent | b9df468 | 2011-02-10 11:45:58 +0530 | [diff] [blame] | 1100 | } |
| 1101 | } |
| 1102 | |
Rickard Andersson | bc6f5cf | 2011-05-24 23:07:17 +0200 | [diff] [blame] | 1103 | /* |
| 1104 | * Read the pull up/pull down status. |
| 1105 | * A bit set in 'pull_up' means that pull up |
| 1106 | * is selected if pull is enabled in PDIS register. |
| 1107 | * Note: only pull up/down set via this driver can |
| 1108 | * be detected due to HW limitations. |
| 1109 | */ |
| 1110 | void nmk_gpio_read_pull(int gpio_bank, u32 *pull_up) |
| 1111 | { |
| 1112 | if (gpio_bank < NUM_BANKS) { |
| 1113 | struct nmk_gpio_chip *chip = nmk_gpio_chips[gpio_bank]; |
| 1114 | |
| 1115 | if (!chip) |
| 1116 | return; |
| 1117 | |
| 1118 | *pull_up = chip->pull_up; |
| 1119 | } |
| 1120 | } |
| 1121 | |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 1122 | int nmk_gpio_irq_map(struct irq_domain *d, unsigned int irq, |
| 1123 | irq_hw_number_t hwirq) |
| 1124 | { |
| 1125 | struct nmk_gpio_chip *nmk_chip = d->host_data; |
| 1126 | |
| 1127 | if (!nmk_chip) |
| 1128 | return -EINVAL; |
| 1129 | |
| 1130 | irq_set_chip_and_handler(irq, &nmk_gpio_irq_chip, handle_edge_irq); |
| 1131 | set_irq_flags(irq, IRQF_VALID); |
| 1132 | irq_set_chip_data(irq, nmk_chip); |
| 1133 | irq_set_irq_type(irq, IRQ_TYPE_EDGE_FALLING); |
| 1134 | |
| 1135 | return 0; |
| 1136 | } |
| 1137 | |
| 1138 | const struct irq_domain_ops nmk_gpio_irq_simple_ops = { |
| 1139 | .map = nmk_gpio_irq_map, |
| 1140 | .xlate = irq_domain_xlate_twocell, |
| 1141 | }; |
| 1142 | |
Uwe Kleine-König | fd0d67d | 2010-09-02 16:13:35 +0100 | [diff] [blame] | 1143 | static int __devinit nmk_gpio_probe(struct platform_device *dev) |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1144 | { |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 1145 | struct nmk_gpio_platform_data *pdata = dev->dev.platform_data; |
Lee Jones | 513c27f | 2012-04-13 15:05:05 +0100 | [diff] [blame] | 1146 | struct device_node *np = dev->dev.of_node; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1147 | struct nmk_gpio_chip *nmk_chip; |
| 1148 | struct gpio_chip *chip; |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 1149 | struct resource *res; |
Rabin Vincent | af7dc22 | 2010-05-06 11:14:17 +0100 | [diff] [blame] | 1150 | struct clk *clk; |
Rabin Vincent | 33b744b | 2010-10-14 10:38:03 +0530 | [diff] [blame] | 1151 | int secondary_irq; |
Linus Walleij | 8d91771 | 2012-04-17 10:15:54 +0200 | [diff] [blame] | 1152 | void __iomem *base; |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 1153 | int irq; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1154 | int ret; |
| 1155 | |
Lee Jones | 513c27f | 2012-04-13 15:05:05 +0100 | [diff] [blame] | 1156 | if (!pdata && !np) { |
| 1157 | dev_err(&dev->dev, "No platform data or device tree found\n"); |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 1158 | return -ENODEV; |
Lee Jones | 513c27f | 2012-04-13 15:05:05 +0100 | [diff] [blame] | 1159 | } |
| 1160 | |
| 1161 | if (np) { |
| 1162 | pdata = kzalloc(sizeof(*pdata), GFP_KERNEL); |
| 1163 | if (!pdata) |
| 1164 | return -ENOMEM; |
| 1165 | |
| 1166 | if (of_get_property(np, "supports-sleepmode", NULL)) |
| 1167 | pdata->supports_sleepmode = true; |
| 1168 | |
| 1169 | if (of_property_read_u32(np, "gpio-bank", &dev->id)) { |
| 1170 | dev_err(&dev->dev, "gpio-bank property not found\n"); |
| 1171 | ret = -EINVAL; |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 1172 | goto out; |
Lee Jones | 513c27f | 2012-04-13 15:05:05 +0100 | [diff] [blame] | 1173 | } |
| 1174 | |
| 1175 | pdata->first_gpio = dev->id * NMK_GPIO_PER_CHIP; |
| 1176 | pdata->num_gpio = NMK_GPIO_PER_CHIP; |
| 1177 | } |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 1178 | |
| 1179 | res = platform_get_resource(dev, IORESOURCE_MEM, 0); |
| 1180 | if (!res) { |
| 1181 | ret = -ENOENT; |
| 1182 | goto out; |
| 1183 | } |
| 1184 | |
| 1185 | irq = platform_get_irq(dev, 0); |
| 1186 | if (irq < 0) { |
| 1187 | ret = irq; |
| 1188 | goto out; |
| 1189 | } |
| 1190 | |
Rabin Vincent | 33b744b | 2010-10-14 10:38:03 +0530 | [diff] [blame] | 1191 | secondary_irq = platform_get_irq(dev, 1); |
| 1192 | if (secondary_irq >= 0 && !pdata->get_secondary_status) { |
| 1193 | ret = -EINVAL; |
| 1194 | goto out; |
| 1195 | } |
| 1196 | |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 1197 | if (request_mem_region(res->start, resource_size(res), |
| 1198 | dev_name(&dev->dev)) == NULL) { |
| 1199 | ret = -EBUSY; |
| 1200 | goto out; |
| 1201 | } |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1202 | |
Linus Walleij | 8d91771 | 2012-04-17 10:15:54 +0200 | [diff] [blame] | 1203 | base = ioremap(res->start, resource_size(res)); |
| 1204 | if (!base) { |
| 1205 | ret = -ENOMEM; |
| 1206 | goto out_release; |
| 1207 | } |
| 1208 | |
Rabin Vincent | af7dc22 | 2010-05-06 11:14:17 +0100 | [diff] [blame] | 1209 | clk = clk_get(&dev->dev, NULL); |
| 1210 | if (IS_ERR(clk)) { |
| 1211 | ret = PTR_ERR(clk); |
Linus Walleij | 8d91771 | 2012-04-17 10:15:54 +0200 | [diff] [blame] | 1212 | goto out_unmap; |
Rabin Vincent | af7dc22 | 2010-05-06 11:14:17 +0100 | [diff] [blame] | 1213 | } |
| 1214 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1215 | nmk_chip = kzalloc(sizeof(*nmk_chip), GFP_KERNEL); |
| 1216 | if (!nmk_chip) { |
| 1217 | ret = -ENOMEM; |
Rabin Vincent | af7dc22 | 2010-05-06 11:14:17 +0100 | [diff] [blame] | 1218 | goto out_clk; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1219 | } |
Lee Jones | 513c27f | 2012-04-13 15:05:05 +0100 | [diff] [blame] | 1220 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1221 | /* |
| 1222 | * The virt address in nmk_chip->addr is in the nomadik register space, |
| 1223 | * so we can simply convert the resource address, without remapping |
| 1224 | */ |
Rabin Vincent | 33b744b | 2010-10-14 10:38:03 +0530 | [diff] [blame] | 1225 | nmk_chip->bank = dev->id; |
Rabin Vincent | af7dc22 | 2010-05-06 11:14:17 +0100 | [diff] [blame] | 1226 | nmk_chip->clk = clk; |
Linus Walleij | 8d91771 | 2012-04-17 10:15:54 +0200 | [diff] [blame] | 1227 | nmk_chip->addr = base; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1228 | nmk_chip->chip = nmk_gpio_template; |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 1229 | nmk_chip->parent_irq = irq; |
Rabin Vincent | 33b744b | 2010-10-14 10:38:03 +0530 | [diff] [blame] | 1230 | nmk_chip->secondary_parent_irq = secondary_irq; |
| 1231 | nmk_chip->get_secondary_status = pdata->get_secondary_status; |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 1232 | nmk_chip->set_ioforce = pdata->set_ioforce; |
Linus Walleij | 33d7864 | 2011-06-09 11:08:47 +0200 | [diff] [blame] | 1233 | nmk_chip->sleepmode = pdata->supports_sleepmode; |
Rabin Vincent | c0fcb8d | 2010-03-03 04:48:54 +0100 | [diff] [blame] | 1234 | spin_lock_init(&nmk_chip->lock); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1235 | |
| 1236 | chip = &nmk_chip->chip; |
| 1237 | chip->base = pdata->first_gpio; |
Rabin Vincent | e493e06 | 2010-03-18 12:35:22 +0530 | [diff] [blame] | 1238 | chip->ngpio = pdata->num_gpio; |
Rabin Vincent | 8d568ae | 2010-12-08 11:07:54 +0530 | [diff] [blame] | 1239 | chip->label = pdata->name ?: dev_name(&dev->dev); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1240 | chip->dev = &dev->dev; |
| 1241 | chip->owner = THIS_MODULE; |
| 1242 | |
Rabin Vincent | ebc6178 | 2011-09-28 15:49:11 +0530 | [diff] [blame] | 1243 | clk_enable(nmk_chip->clk); |
| 1244 | nmk_chip->lowemi = readl_relaxed(nmk_chip->addr + NMK_GPIO_LOWEMI); |
| 1245 | clk_disable(nmk_chip->clk); |
| 1246 | |
Arnd Bergmann | 072e82a | 2012-05-10 13:39:52 +0200 | [diff] [blame] | 1247 | #ifdef CONFIG_OF_GPIO |
Lee Jones | 513c27f | 2012-04-13 15:05:05 +0100 | [diff] [blame] | 1248 | chip->of_node = np; |
Arnd Bergmann | 072e82a | 2012-05-10 13:39:52 +0200 | [diff] [blame] | 1249 | #endif |
Lee Jones | 513c27f | 2012-04-13 15:05:05 +0100 | [diff] [blame] | 1250 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1251 | ret = gpiochip_add(&nmk_chip->chip); |
| 1252 | if (ret) |
| 1253 | goto out_free; |
| 1254 | |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 1255 | BUG_ON(nmk_chip->bank >= ARRAY_SIZE(nmk_gpio_chips)); |
| 1256 | |
| 1257 | nmk_gpio_chips[nmk_chip->bank] = nmk_chip; |
Lee Jones | 513c27f | 2012-04-13 15:05:05 +0100 | [diff] [blame] | 1258 | |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 1259 | platform_set_drvdata(dev, nmk_chip); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1260 | |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 1261 | nmk_chip->domain = irq_domain_add_legacy(np, NMK_GPIO_PER_CHIP, |
| 1262 | NOMADIK_GPIO_TO_IRQ(pdata->first_gpio), |
| 1263 | 0, &nmk_gpio_irq_simple_ops, nmk_chip); |
| 1264 | if (!nmk_chip->domain) { |
| 1265 | pr_err("%s: Failed to create irqdomain\n", np->full_name); |
| 1266 | ret = -ENOSYS; |
| 1267 | goto out_free; |
| 1268 | } |
| 1269 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1270 | nmk_gpio_init_irq(nmk_chip); |
| 1271 | |
Lee Jones | 513c27f | 2012-04-13 15:05:05 +0100 | [diff] [blame] | 1272 | dev_info(&dev->dev, "at address %p\n", nmk_chip->addr); |
| 1273 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1274 | return 0; |
| 1275 | |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 1276 | out_free: |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1277 | kfree(nmk_chip); |
Rabin Vincent | af7dc22 | 2010-05-06 11:14:17 +0100 | [diff] [blame] | 1278 | out_clk: |
| 1279 | clk_disable(clk); |
| 1280 | clk_put(clk); |
Linus Walleij | 8d91771 | 2012-04-17 10:15:54 +0200 | [diff] [blame] | 1281 | out_unmap: |
| 1282 | iounmap(base); |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 1283 | out_release: |
| 1284 | release_mem_region(res->start, resource_size(res)); |
| 1285 | out: |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1286 | dev_err(&dev->dev, "Failure %i for GPIO %i-%i\n", ret, |
| 1287 | pdata->first_gpio, pdata->first_gpio+31); |
Lee Jones | 513c27f | 2012-04-13 15:05:05 +0100 | [diff] [blame] | 1288 | if (np) |
| 1289 | kfree(pdata); |
| 1290 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1291 | return ret; |
| 1292 | } |
| 1293 | |
Linus Walleij | e98ea77 | 2012-04-26 23:57:25 +0200 | [diff] [blame^] | 1294 | static int nmk_get_groups_cnt(struct pinctrl_dev *pctldev) |
| 1295 | { |
| 1296 | struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev); |
| 1297 | |
| 1298 | return npct->soc->ngroups; |
| 1299 | } |
| 1300 | |
| 1301 | static const char *nmk_get_group_name(struct pinctrl_dev *pctldev, |
| 1302 | unsigned selector) |
| 1303 | { |
| 1304 | struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev); |
| 1305 | |
| 1306 | return npct->soc->groups[selector].name; |
| 1307 | } |
| 1308 | |
| 1309 | static int nmk_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector, |
| 1310 | const unsigned **pins, |
| 1311 | unsigned *num_pins) |
| 1312 | { |
| 1313 | struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev); |
| 1314 | |
| 1315 | *pins = npct->soc->groups[selector].pins; |
| 1316 | *num_pins = npct->soc->groups[selector].npins; |
| 1317 | return 0; |
| 1318 | } |
| 1319 | |
| 1320 | static void nmk_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s, |
| 1321 | unsigned offset) |
| 1322 | { |
| 1323 | seq_printf(s, " Nomadik GPIO"); |
| 1324 | } |
| 1325 | |
| 1326 | static struct pinctrl_ops nmk_pinctrl_ops = { |
| 1327 | .get_groups_count = nmk_get_groups_cnt, |
| 1328 | .get_group_name = nmk_get_group_name, |
| 1329 | .get_group_pins = nmk_get_group_pins, |
| 1330 | .pin_dbg_show = nmk_pin_dbg_show, |
| 1331 | }; |
| 1332 | |
| 1333 | static struct pinctrl_desc nmk_pinctrl_desc = { |
| 1334 | .name = "pinctrl-nomadik", |
| 1335 | .pctlops = &nmk_pinctrl_ops, |
| 1336 | .owner = THIS_MODULE, |
| 1337 | }; |
| 1338 | |
| 1339 | static int __devinit nmk_pinctrl_probe(struct platform_device *pdev) |
| 1340 | { |
| 1341 | const struct platform_device_id *platid = platform_get_device_id(pdev); |
| 1342 | struct nmk_pinctrl *npct; |
| 1343 | int i; |
| 1344 | |
| 1345 | npct = devm_kzalloc(&pdev->dev, sizeof(*npct), GFP_KERNEL); |
| 1346 | if (!npct) |
| 1347 | return -ENOMEM; |
| 1348 | |
| 1349 | /* Poke in other ASIC variants here */ |
| 1350 | if (platid->driver_data == PINCTRL_NMK_DB8500) |
| 1351 | nmk_pinctrl_db8500_init(&npct->soc); |
| 1352 | |
| 1353 | /* |
| 1354 | * We need all the GPIO drivers to probe FIRST, or we will not be able |
| 1355 | * to obtain references to the struct gpio_chip * for them, and we |
| 1356 | * need this to proceed. |
| 1357 | */ |
| 1358 | for (i = 0; i < npct->soc->gpio_num_ranges; i++) { |
| 1359 | if (!nmk_gpio_chips[i]) { |
| 1360 | dev_warn(&pdev->dev, "GPIO chip %d not registered yet\n", i); |
| 1361 | devm_kfree(&pdev->dev, npct); |
| 1362 | return -EPROBE_DEFER; |
| 1363 | } |
| 1364 | npct->soc->gpio_ranges[i].gc = &nmk_gpio_chips[i]->chip; |
| 1365 | } |
| 1366 | |
| 1367 | nmk_pinctrl_desc.pins = npct->soc->pins; |
| 1368 | nmk_pinctrl_desc.npins = npct->soc->npins; |
| 1369 | npct->dev = &pdev->dev; |
| 1370 | npct->pctl = pinctrl_register(&nmk_pinctrl_desc, &pdev->dev, npct); |
| 1371 | if (!npct->pctl) { |
| 1372 | dev_err(&pdev->dev, "could not register Nomadik pinctrl driver\n"); |
| 1373 | return -EINVAL; |
| 1374 | } |
| 1375 | |
| 1376 | /* We will handle a range of GPIO pins */ |
| 1377 | for (i = 0; i < npct->soc->gpio_num_ranges; i++) |
| 1378 | pinctrl_add_gpio_range(npct->pctl, &npct->soc->gpio_ranges[i]); |
| 1379 | |
| 1380 | platform_set_drvdata(pdev, npct); |
| 1381 | dev_info(&pdev->dev, "initialized Nomadik pin control driver\n"); |
| 1382 | |
| 1383 | return 0; |
| 1384 | } |
| 1385 | |
Lee Jones | 513c27f | 2012-04-13 15:05:05 +0100 | [diff] [blame] | 1386 | static const struct of_device_id nmk_gpio_match[] = { |
| 1387 | { .compatible = "st,nomadik-gpio", }, |
| 1388 | {} |
| 1389 | }; |
| 1390 | |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 1391 | static struct platform_driver nmk_gpio_driver = { |
| 1392 | .driver = { |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1393 | .owner = THIS_MODULE, |
| 1394 | .name = "gpio", |
Lee Jones | 513c27f | 2012-04-13 15:05:05 +0100 | [diff] [blame] | 1395 | .of_match_table = nmk_gpio_match, |
Rabin Vincent | 5317e4d1 | 2011-02-10 09:29:53 +0530 | [diff] [blame] | 1396 | }, |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1397 | .probe = nmk_gpio_probe, |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1398 | }; |
| 1399 | |
Linus Walleij | e98ea77 | 2012-04-26 23:57:25 +0200 | [diff] [blame^] | 1400 | static const struct platform_device_id nmk_pinctrl_id[] = { |
| 1401 | { "pinctrl-stn8815", PINCTRL_NMK_STN8815 }, |
| 1402 | { "pinctrl-db8500", PINCTRL_NMK_DB8500 }, |
| 1403 | }; |
| 1404 | |
| 1405 | static struct platform_driver nmk_pinctrl_driver = { |
| 1406 | .driver = { |
| 1407 | .owner = THIS_MODULE, |
| 1408 | .name = "pinctrl-nomadik", |
| 1409 | }, |
| 1410 | .probe = nmk_pinctrl_probe, |
| 1411 | .id_table = nmk_pinctrl_id, |
| 1412 | }; |
| 1413 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1414 | static int __init nmk_gpio_init(void) |
| 1415 | { |
Linus Walleij | e98ea77 | 2012-04-26 23:57:25 +0200 | [diff] [blame^] | 1416 | int ret; |
| 1417 | |
| 1418 | ret = platform_driver_register(&nmk_gpio_driver); |
| 1419 | if (ret) |
| 1420 | return ret; |
| 1421 | return platform_driver_register(&nmk_pinctrl_driver); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1422 | } |
| 1423 | |
Rabin Vincent | 33f45ea | 2010-06-02 06:09:52 +0100 | [diff] [blame] | 1424 | core_initcall(nmk_gpio_init); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1425 | |
| 1426 | MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini"); |
| 1427 | MODULE_DESCRIPTION("Nomadik GPIO Driver"); |
| 1428 | MODULE_LICENSE("GPL"); |