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> |
Lee Jones | 855f80c | 2012-05-26 06:09:29 +0100 | [diff] [blame] | 27 | #include <linux/of_device.h> |
Linus Walleij | e98ea77 | 2012-04-26 23:57:25 +0200 | [diff] [blame] | 28 | #include <linux/pinctrl/pinctrl.h> |
Linus Walleij | dbfe8ca | 2012-05-02 22:56:47 +0200 | [diff] [blame] | 29 | #include <linux/pinctrl/pinmux.h> |
Linus Walleij | d41af62 | 2012-05-03 15:58:12 +0200 | [diff] [blame] | 30 | #include <linux/pinctrl/pinconf.h> |
Linus Walleij | dbfe8ca | 2012-05-02 22:56:47 +0200 | [diff] [blame] | 31 | /* Since we request GPIOs from ourself */ |
| 32 | #include <linux/pinctrl/consumer.h> |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 33 | |
Will Deacon | adfed15 | 2011-02-28 10:12:29 +0000 | [diff] [blame] | 34 | #include <asm/mach/irq.h> |
| 35 | |
Rabin Vincent | 378be06 | 2010-06-02 06:06:29 +0100 | [diff] [blame] | 36 | #include <plat/pincfg.h> |
Linus Walleij | 0f33286 | 2011-08-22 08:33:30 +0100 | [diff] [blame] | 37 | #include <plat/gpio-nomadik.h> |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 38 | |
Linus Walleij | e98ea77 | 2012-04-26 23:57:25 +0200 | [diff] [blame] | 39 | #include "pinctrl-nomadik.h" |
| 40 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 41 | /* |
| 42 | * The GPIO module in the Nomadik family of Systems-on-Chip is an |
| 43 | * AMBA device, managing 32 pins and alternate functions. The logic block |
Jonas Aaberg | 9c66ee6 | 2010-10-13 13:14:17 +0200 | [diff] [blame] | 44 | * is currently used in the Nomadik and ux500. |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 45 | * |
| 46 | * Symbols in this file are called "nmk_gpio" for "nomadik gpio" |
| 47 | */ |
| 48 | |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 49 | #define NMK_GPIO_PER_CHIP 32 |
| 50 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 51 | struct nmk_gpio_chip { |
| 52 | struct gpio_chip chip; |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 53 | struct irq_domain *domain; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 54 | void __iomem *addr; |
Rabin Vincent | af7dc22 | 2010-05-06 11:14:17 +0100 | [diff] [blame] | 55 | struct clk *clk; |
Rabin Vincent | 33b744b | 2010-10-14 10:38:03 +0530 | [diff] [blame] | 56 | unsigned int bank; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 57 | unsigned int parent_irq; |
Virupax Sadashivpetimath | 2c8bb0e | 2010-11-11 14:10:38 +0530 | [diff] [blame] | 58 | int secondary_parent_irq; |
Rabin Vincent | 33b744b | 2010-10-14 10:38:03 +0530 | [diff] [blame] | 59 | u32 (*get_secondary_status)(unsigned int bank); |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 60 | void (*set_ioforce)(bool enable); |
Rabin Vincent | c0fcb8d | 2010-03-03 04:48:54 +0100 | [diff] [blame] | 61 | spinlock_t lock; |
Linus Walleij | 33d7864 | 2011-06-09 11:08:47 +0200 | [diff] [blame] | 62 | bool sleepmode; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 63 | /* Keep track of configured edges */ |
| 64 | u32 edge_rising; |
| 65 | u32 edge_falling; |
Rabin Vincent | b9df468 | 2011-02-10 11:45:58 +0530 | [diff] [blame] | 66 | u32 real_wake; |
| 67 | u32 rwimsc; |
| 68 | u32 fwimsc; |
Rabin Vincent | 6c12fe8 | 2011-05-23 12:13:33 +0530 | [diff] [blame] | 69 | u32 rimsc; |
| 70 | u32 fimsc; |
Rickard Andersson | bc6f5cf | 2011-05-24 23:07:17 +0200 | [diff] [blame] | 71 | u32 pull_up; |
Rabin Vincent | ebc6178 | 2011-09-28 15:49:11 +0530 | [diff] [blame] | 72 | u32 lowemi; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 73 | }; |
| 74 | |
Linus Walleij | e98ea77 | 2012-04-26 23:57:25 +0200 | [diff] [blame] | 75 | struct nmk_pinctrl { |
| 76 | struct device *dev; |
| 77 | struct pinctrl_dev *pctl; |
| 78 | const struct nmk_pinctrl_soc_data *soc; |
| 79 | }; |
| 80 | |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 81 | static struct nmk_gpio_chip * |
| 82 | nmk_gpio_chips[DIV_ROUND_UP(ARCH_NR_GPIOS, NMK_GPIO_PER_CHIP)]; |
| 83 | |
| 84 | static DEFINE_SPINLOCK(nmk_gpio_slpm_lock); |
| 85 | |
| 86 | #define NUM_BANKS ARRAY_SIZE(nmk_gpio_chips) |
| 87 | |
Rabin Vincent | 6f9a974 | 2010-06-02 05:50:28 +0100 | [diff] [blame] | 88 | static void __nmk_gpio_set_mode(struct nmk_gpio_chip *nmk_chip, |
| 89 | unsigned offset, int gpio_mode) |
| 90 | { |
| 91 | u32 bit = 1 << offset; |
| 92 | u32 afunc, bfunc; |
| 93 | |
| 94 | afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & ~bit; |
| 95 | bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & ~bit; |
| 96 | if (gpio_mode & NMK_GPIO_ALT_A) |
| 97 | afunc |= bit; |
| 98 | if (gpio_mode & NMK_GPIO_ALT_B) |
| 99 | bfunc |= bit; |
| 100 | writel(afunc, nmk_chip->addr + NMK_GPIO_AFSLA); |
| 101 | writel(bfunc, nmk_chip->addr + NMK_GPIO_AFSLB); |
| 102 | } |
| 103 | |
Rabin Vincent | 81a3c29 | 2010-05-27 12:39:23 +0100 | [diff] [blame] | 104 | static void __nmk_gpio_set_slpm(struct nmk_gpio_chip *nmk_chip, |
| 105 | unsigned offset, enum nmk_gpio_slpm mode) |
| 106 | { |
| 107 | u32 bit = 1 << offset; |
| 108 | u32 slpm; |
| 109 | |
| 110 | slpm = readl(nmk_chip->addr + NMK_GPIO_SLPC); |
| 111 | if (mode == NMK_GPIO_SLPM_NOCHANGE) |
| 112 | slpm |= bit; |
| 113 | else |
| 114 | slpm &= ~bit; |
| 115 | writel(slpm, nmk_chip->addr + NMK_GPIO_SLPC); |
| 116 | } |
| 117 | |
Rabin Vincent | 5b327ed | 2010-05-27 12:29:50 +0100 | [diff] [blame] | 118 | static void __nmk_gpio_set_pull(struct nmk_gpio_chip *nmk_chip, |
| 119 | unsigned offset, enum nmk_gpio_pull pull) |
| 120 | { |
| 121 | u32 bit = 1 << offset; |
| 122 | u32 pdis; |
| 123 | |
| 124 | pdis = readl(nmk_chip->addr + NMK_GPIO_PDIS); |
Rickard Andersson | bc6f5cf | 2011-05-24 23:07:17 +0200 | [diff] [blame] | 125 | if (pull == NMK_GPIO_PULL_NONE) { |
Rabin Vincent | 5b327ed | 2010-05-27 12:29:50 +0100 | [diff] [blame] | 126 | pdis |= bit; |
Rickard Andersson | bc6f5cf | 2011-05-24 23:07:17 +0200 | [diff] [blame] | 127 | nmk_chip->pull_up &= ~bit; |
| 128 | } else { |
Rabin Vincent | 5b327ed | 2010-05-27 12:29:50 +0100 | [diff] [blame] | 129 | pdis &= ~bit; |
Rickard Andersson | bc6f5cf | 2011-05-24 23:07:17 +0200 | [diff] [blame] | 130 | } |
| 131 | |
Rabin Vincent | 5b327ed | 2010-05-27 12:29:50 +0100 | [diff] [blame] | 132 | writel(pdis, nmk_chip->addr + NMK_GPIO_PDIS); |
| 133 | |
Rickard Andersson | bc6f5cf | 2011-05-24 23:07:17 +0200 | [diff] [blame] | 134 | if (pull == NMK_GPIO_PULL_UP) { |
| 135 | nmk_chip->pull_up |= bit; |
Rabin Vincent | 5b327ed | 2010-05-27 12:29:50 +0100 | [diff] [blame] | 136 | writel(bit, nmk_chip->addr + NMK_GPIO_DATS); |
Rickard Andersson | bc6f5cf | 2011-05-24 23:07:17 +0200 | [diff] [blame] | 137 | } else if (pull == NMK_GPIO_PULL_DOWN) { |
| 138 | nmk_chip->pull_up &= ~bit; |
Rabin Vincent | 5b327ed | 2010-05-27 12:29:50 +0100 | [diff] [blame] | 139 | writel(bit, nmk_chip->addr + NMK_GPIO_DATC); |
Rickard Andersson | bc6f5cf | 2011-05-24 23:07:17 +0200 | [diff] [blame] | 140 | } |
Rabin Vincent | 5b327ed | 2010-05-27 12:29:50 +0100 | [diff] [blame] | 141 | } |
| 142 | |
Rabin Vincent | ebc6178 | 2011-09-28 15:49:11 +0530 | [diff] [blame] | 143 | static void __nmk_gpio_set_lowemi(struct nmk_gpio_chip *nmk_chip, |
| 144 | unsigned offset, bool lowemi) |
| 145 | { |
| 146 | u32 bit = BIT(offset); |
| 147 | bool enabled = nmk_chip->lowemi & bit; |
| 148 | |
| 149 | if (lowemi == enabled) |
| 150 | return; |
| 151 | |
| 152 | if (lowemi) |
| 153 | nmk_chip->lowemi |= bit; |
| 154 | else |
| 155 | nmk_chip->lowemi &= ~bit; |
| 156 | |
| 157 | writel_relaxed(nmk_chip->lowemi, |
| 158 | nmk_chip->addr + NMK_GPIO_LOWEMI); |
| 159 | } |
| 160 | |
Rabin Vincent | 378be06 | 2010-06-02 06:06:29 +0100 | [diff] [blame] | 161 | static void __nmk_gpio_make_input(struct nmk_gpio_chip *nmk_chip, |
| 162 | unsigned offset) |
| 163 | { |
| 164 | writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC); |
| 165 | } |
| 166 | |
Rabin Vincent | 6720db7 | 2010-09-02 11:28:48 +0100 | [diff] [blame] | 167 | static void __nmk_gpio_set_output(struct nmk_gpio_chip *nmk_chip, |
| 168 | unsigned offset, int val) |
| 169 | { |
| 170 | if (val) |
| 171 | writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATS); |
| 172 | else |
| 173 | writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATC); |
| 174 | } |
| 175 | |
| 176 | static void __nmk_gpio_make_output(struct nmk_gpio_chip *nmk_chip, |
| 177 | unsigned offset, int val) |
| 178 | { |
| 179 | writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRS); |
| 180 | __nmk_gpio_set_output(nmk_chip, offset, val); |
| 181 | } |
| 182 | |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 183 | static void __nmk_gpio_set_mode_safe(struct nmk_gpio_chip *nmk_chip, |
| 184 | unsigned offset, int gpio_mode, |
| 185 | bool glitch) |
| 186 | { |
Rabin Vincent | 6c12fe8 | 2011-05-23 12:13:33 +0530 | [diff] [blame] | 187 | u32 rwimsc = nmk_chip->rwimsc; |
| 188 | u32 fwimsc = nmk_chip->fwimsc; |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 189 | |
| 190 | if (glitch && nmk_chip->set_ioforce) { |
| 191 | u32 bit = BIT(offset); |
| 192 | |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 193 | /* Prevent spurious wakeups */ |
| 194 | writel(rwimsc & ~bit, nmk_chip->addr + NMK_GPIO_RWIMSC); |
| 195 | writel(fwimsc & ~bit, nmk_chip->addr + NMK_GPIO_FWIMSC); |
| 196 | |
| 197 | nmk_chip->set_ioforce(true); |
| 198 | } |
| 199 | |
| 200 | __nmk_gpio_set_mode(nmk_chip, offset, gpio_mode); |
| 201 | |
| 202 | if (glitch && nmk_chip->set_ioforce) { |
| 203 | nmk_chip->set_ioforce(false); |
| 204 | |
| 205 | writel(rwimsc, nmk_chip->addr + NMK_GPIO_RWIMSC); |
| 206 | writel(fwimsc, nmk_chip->addr + NMK_GPIO_FWIMSC); |
| 207 | } |
| 208 | } |
| 209 | |
Rabin Vincent | 6c42ad1 | 2011-05-23 12:22:18 +0530 | [diff] [blame] | 210 | static void |
| 211 | nmk_gpio_disable_lazy_irq(struct nmk_gpio_chip *nmk_chip, unsigned offset) |
| 212 | { |
| 213 | u32 falling = nmk_chip->fimsc & BIT(offset); |
| 214 | u32 rising = nmk_chip->rimsc & BIT(offset); |
| 215 | int gpio = nmk_chip->chip.base + offset; |
| 216 | int irq = NOMADIK_GPIO_TO_IRQ(gpio); |
| 217 | struct irq_data *d = irq_get_irq_data(irq); |
| 218 | |
| 219 | if (!rising && !falling) |
| 220 | return; |
| 221 | |
| 222 | if (!d || !irqd_irq_disabled(d)) |
| 223 | return; |
| 224 | |
| 225 | if (rising) { |
| 226 | nmk_chip->rimsc &= ~BIT(offset); |
| 227 | writel_relaxed(nmk_chip->rimsc, |
| 228 | nmk_chip->addr + NMK_GPIO_RIMSC); |
| 229 | } |
| 230 | |
| 231 | if (falling) { |
| 232 | nmk_chip->fimsc &= ~BIT(offset); |
| 233 | writel_relaxed(nmk_chip->fimsc, |
| 234 | nmk_chip->addr + NMK_GPIO_FIMSC); |
| 235 | } |
| 236 | |
| 237 | dev_dbg(nmk_chip->chip.dev, "%d: clearing interrupt mask\n", gpio); |
| 238 | } |
| 239 | |
Rabin Vincent | 378be06 | 2010-06-02 06:06:29 +0100 | [diff] [blame] | 240 | 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] | 241 | pin_cfg_t cfg, bool sleep, unsigned int *slpmregs) |
Rabin Vincent | 378be06 | 2010-06-02 06:06:29 +0100 | [diff] [blame] | 242 | { |
| 243 | static const char *afnames[] = { |
| 244 | [NMK_GPIO_ALT_GPIO] = "GPIO", |
| 245 | [NMK_GPIO_ALT_A] = "A", |
| 246 | [NMK_GPIO_ALT_B] = "B", |
| 247 | [NMK_GPIO_ALT_C] = "C" |
| 248 | }; |
| 249 | static const char *pullnames[] = { |
| 250 | [NMK_GPIO_PULL_NONE] = "none", |
| 251 | [NMK_GPIO_PULL_UP] = "up", |
| 252 | [NMK_GPIO_PULL_DOWN] = "down", |
| 253 | [3] /* illegal */ = "??" |
| 254 | }; |
| 255 | static const char *slpmnames[] = { |
Rabin Vincent | 7e3f7e5 | 2010-09-02 11:28:05 +0100 | [diff] [blame] | 256 | [NMK_GPIO_SLPM_INPUT] = "input/wakeup", |
| 257 | [NMK_GPIO_SLPM_NOCHANGE] = "no-change/no-wakeup", |
Rabin Vincent | 378be06 | 2010-06-02 06:06:29 +0100 | [diff] [blame] | 258 | }; |
| 259 | |
| 260 | int pin = PIN_NUM(cfg); |
| 261 | int pull = PIN_PULL(cfg); |
| 262 | int af = PIN_ALT(cfg); |
| 263 | int slpm = PIN_SLPM(cfg); |
Rabin Vincent | 6720db7 | 2010-09-02 11:28:48 +0100 | [diff] [blame] | 264 | int output = PIN_DIR(cfg); |
| 265 | int val = PIN_VAL(cfg); |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 266 | bool glitch = af == NMK_GPIO_ALT_C; |
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 | dev_dbg(nmk_chip->chip.dev, "pin %d [%#lx]: af %s, pull %s, slpm %s (%s%s)\n", |
| 269 | pin, cfg, afnames[af], pullnames[pull], slpmnames[slpm], |
Rabin Vincent | 6720db7 | 2010-09-02 11:28:48 +0100 | [diff] [blame] | 270 | output ? "output " : "input", |
| 271 | output ? (val ? "high" : "low") : ""); |
Rabin Vincent | 378be06 | 2010-06-02 06:06:29 +0100 | [diff] [blame] | 272 | |
Rabin Vincent | dacdc96 | 2010-12-03 20:35:37 +0530 | [diff] [blame] | 273 | if (sleep) { |
| 274 | int slpm_pull = PIN_SLPM_PULL(cfg); |
| 275 | int slpm_output = PIN_SLPM_DIR(cfg); |
| 276 | int slpm_val = PIN_SLPM_VAL(cfg); |
| 277 | |
Rabin Vincent | 3546d15 | 2010-11-25 11:38:27 +0530 | [diff] [blame] | 278 | af = NMK_GPIO_ALT_GPIO; |
| 279 | |
Rabin Vincent | dacdc96 | 2010-12-03 20:35:37 +0530 | [diff] [blame] | 280 | /* |
| 281 | * The SLPM_* values are normal values + 1 to allow zero to |
| 282 | * mean "same as normal". |
| 283 | */ |
| 284 | if (slpm_pull) |
| 285 | pull = slpm_pull - 1; |
| 286 | if (slpm_output) |
| 287 | output = slpm_output - 1; |
| 288 | if (slpm_val) |
| 289 | val = slpm_val - 1; |
| 290 | |
| 291 | dev_dbg(nmk_chip->chip.dev, "pin %d: sleep pull %s, dir %s, val %s\n", |
| 292 | pin, |
| 293 | slpm_pull ? pullnames[pull] : "same", |
| 294 | slpm_output ? (output ? "output" : "input") : "same", |
| 295 | slpm_val ? (val ? "high" : "low") : "same"); |
| 296 | } |
| 297 | |
Rabin Vincent | 6720db7 | 2010-09-02 11:28:48 +0100 | [diff] [blame] | 298 | if (output) |
| 299 | __nmk_gpio_make_output(nmk_chip, offset, val); |
| 300 | else { |
| 301 | __nmk_gpio_make_input(nmk_chip, offset); |
| 302 | __nmk_gpio_set_pull(nmk_chip, offset, pull); |
| 303 | } |
| 304 | |
Rabin Vincent | ebc6178 | 2011-09-28 15:49:11 +0530 | [diff] [blame] | 305 | __nmk_gpio_set_lowemi(nmk_chip, offset, PIN_LOWEMI(cfg)); |
| 306 | |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 307 | /* |
Rabin Vincent | 6c42ad1 | 2011-05-23 12:22:18 +0530 | [diff] [blame] | 308 | * If the pin is switching to altfunc, and there was an interrupt |
| 309 | * installed on it which has been lazy disabled, actually mask the |
| 310 | * interrupt to prevent spurious interrupts that would occur while the |
| 311 | * pin is under control of the peripheral. Only SKE does this. |
| 312 | */ |
| 313 | if (af != NMK_GPIO_ALT_GPIO) |
| 314 | nmk_gpio_disable_lazy_irq(nmk_chip, offset); |
| 315 | |
| 316 | /* |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 317 | * If we've backed up the SLPM registers (glitch workaround), modify |
| 318 | * the backups since they will be restored. |
| 319 | */ |
| 320 | if (slpmregs) { |
| 321 | if (slpm == NMK_GPIO_SLPM_NOCHANGE) |
| 322 | slpmregs[nmk_chip->bank] |= BIT(offset); |
| 323 | else |
| 324 | slpmregs[nmk_chip->bank] &= ~BIT(offset); |
| 325 | } else |
| 326 | __nmk_gpio_set_slpm(nmk_chip, offset, slpm); |
| 327 | |
| 328 | __nmk_gpio_set_mode_safe(nmk_chip, offset, af, glitch); |
| 329 | } |
| 330 | |
| 331 | /* |
| 332 | * Safe sequence used to switch IOs between GPIO and Alternate-C mode: |
| 333 | * - Save SLPM registers |
| 334 | * - Set SLPM=0 for the IOs you want to switch and others to 1 |
| 335 | * - Configure the GPIO registers for the IOs that are being switched |
| 336 | * - Set IOFORCE=1 |
| 337 | * - Modify the AFLSA/B registers for the IOs that are being switched |
| 338 | * - Set IOFORCE=0 |
| 339 | * - Restore SLPM registers |
| 340 | * - Any spurious wake up event during switch sequence to be ignored and |
| 341 | * cleared |
| 342 | */ |
| 343 | static void nmk_gpio_glitch_slpm_init(unsigned int *slpm) |
| 344 | { |
| 345 | int i; |
| 346 | |
| 347 | for (i = 0; i < NUM_BANKS; i++) { |
| 348 | struct nmk_gpio_chip *chip = nmk_gpio_chips[i]; |
| 349 | unsigned int temp = slpm[i]; |
| 350 | |
| 351 | if (!chip) |
| 352 | break; |
| 353 | |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 354 | clk_enable(chip->clk); |
| 355 | |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 356 | slpm[i] = readl(chip->addr + NMK_GPIO_SLPC); |
| 357 | writel(temp, chip->addr + NMK_GPIO_SLPC); |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | static void nmk_gpio_glitch_slpm_restore(unsigned int *slpm) |
| 362 | { |
| 363 | int i; |
| 364 | |
| 365 | for (i = 0; i < NUM_BANKS; i++) { |
| 366 | struct nmk_gpio_chip *chip = nmk_gpio_chips[i]; |
| 367 | |
| 368 | if (!chip) |
| 369 | break; |
| 370 | |
| 371 | writel(slpm[i], chip->addr + NMK_GPIO_SLPC); |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 372 | |
| 373 | clk_disable(chip->clk); |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 374 | } |
| 375 | } |
| 376 | |
| 377 | static int __nmk_config_pins(pin_cfg_t *cfgs, int num, bool sleep) |
| 378 | { |
| 379 | static unsigned int slpm[NUM_BANKS]; |
| 380 | unsigned long flags; |
| 381 | bool glitch = false; |
| 382 | int ret = 0; |
| 383 | int i; |
| 384 | |
| 385 | for (i = 0; i < num; i++) { |
| 386 | if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C) { |
| 387 | glitch = true; |
| 388 | break; |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | spin_lock_irqsave(&nmk_gpio_slpm_lock, flags); |
| 393 | |
| 394 | if (glitch) { |
| 395 | memset(slpm, 0xff, sizeof(slpm)); |
| 396 | |
| 397 | for (i = 0; i < num; i++) { |
| 398 | int pin = PIN_NUM(cfgs[i]); |
| 399 | int offset = pin % NMK_GPIO_PER_CHIP; |
| 400 | |
| 401 | if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C) |
| 402 | slpm[pin / NMK_GPIO_PER_CHIP] &= ~BIT(offset); |
| 403 | } |
| 404 | |
| 405 | nmk_gpio_glitch_slpm_init(slpm); |
| 406 | } |
| 407 | |
| 408 | for (i = 0; i < num; i++) { |
| 409 | struct nmk_gpio_chip *nmk_chip; |
| 410 | int pin = PIN_NUM(cfgs[i]); |
| 411 | |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 412 | nmk_chip = nmk_gpio_chips[pin / NMK_GPIO_PER_CHIP]; |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 413 | if (!nmk_chip) { |
| 414 | ret = -EINVAL; |
| 415 | break; |
| 416 | } |
| 417 | |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 418 | clk_enable(nmk_chip->clk); |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 419 | spin_lock(&nmk_chip->lock); |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 420 | __nmk_config_pin(nmk_chip, pin % NMK_GPIO_PER_CHIP, |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 421 | cfgs[i], sleep, glitch ? slpm : NULL); |
| 422 | spin_unlock(&nmk_chip->lock); |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 423 | clk_disable(nmk_chip->clk); |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | if (glitch) |
| 427 | nmk_gpio_glitch_slpm_restore(slpm); |
| 428 | |
| 429 | spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags); |
| 430 | |
| 431 | return ret; |
Rabin Vincent | 378be06 | 2010-06-02 06:06:29 +0100 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | /** |
| 435 | * nmk_config_pin - configure a pin's mux attributes |
| 436 | * @cfg: pin confguration |
Linus Walleij | 50bcd47 | 2012-07-04 11:25:36 +0200 | [diff] [blame] | 437 | * @sleep: Non-zero to apply the sleep mode configuration |
Rabin Vincent | 378be06 | 2010-06-02 06:06:29 +0100 | [diff] [blame] | 438 | * Configures a pin's mode (alternate function or GPIO), its pull up status, |
| 439 | * and its sleep mode based on the specified configuration. The @cfg is |
| 440 | * usually one of the SoC specific macros defined in mach/<soc>-pins.h. These |
| 441 | * are constructed using, and can be further enhanced with, the macros in |
| 442 | * plat/pincfg.h. |
| 443 | * |
| 444 | * If a pin's mode is set to GPIO, it is configured as an input to avoid |
| 445 | * side-effects. The gpio can be manipulated later using standard GPIO API |
| 446 | * calls. |
| 447 | */ |
Rabin Vincent | dacdc96 | 2010-12-03 20:35:37 +0530 | [diff] [blame] | 448 | int nmk_config_pin(pin_cfg_t cfg, bool sleep) |
Rabin Vincent | 378be06 | 2010-06-02 06:06:29 +0100 | [diff] [blame] | 449 | { |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 450 | return __nmk_config_pins(&cfg, 1, sleep); |
Rabin Vincent | 378be06 | 2010-06-02 06:06:29 +0100 | [diff] [blame] | 451 | } |
| 452 | EXPORT_SYMBOL(nmk_config_pin); |
| 453 | |
| 454 | /** |
| 455 | * nmk_config_pins - configure several pins at once |
| 456 | * @cfgs: array of pin configurations |
| 457 | * @num: number of elments in the array |
| 458 | * |
| 459 | * Configures several pins using nmk_config_pin(). Refer to that function for |
| 460 | * further information. |
| 461 | */ |
| 462 | int nmk_config_pins(pin_cfg_t *cfgs, int num) |
| 463 | { |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 464 | return __nmk_config_pins(cfgs, num, false); |
Rabin Vincent | 378be06 | 2010-06-02 06:06:29 +0100 | [diff] [blame] | 465 | } |
| 466 | EXPORT_SYMBOL(nmk_config_pins); |
| 467 | |
Rabin Vincent | dacdc96 | 2010-12-03 20:35:37 +0530 | [diff] [blame] | 468 | int nmk_config_pins_sleep(pin_cfg_t *cfgs, int num) |
| 469 | { |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 470 | return __nmk_config_pins(cfgs, num, true); |
Rabin Vincent | dacdc96 | 2010-12-03 20:35:37 +0530 | [diff] [blame] | 471 | } |
| 472 | EXPORT_SYMBOL(nmk_config_pins_sleep); |
| 473 | |
Rabin Vincent | 5b327ed | 2010-05-27 12:29:50 +0100 | [diff] [blame] | 474 | /** |
Rabin Vincent | 81a3c29 | 2010-05-27 12:39:23 +0100 | [diff] [blame] | 475 | * nmk_gpio_set_slpm() - configure the sleep mode of a pin |
| 476 | * @gpio: pin number |
| 477 | * @mode: NMK_GPIO_SLPM_INPUT or NMK_GPIO_SLPM_NOCHANGE, |
| 478 | * |
Linus Walleij | 33d7864 | 2011-06-09 11:08:47 +0200 | [diff] [blame] | 479 | * This register is actually in the pinmux layer, not the GPIO block itself. |
| 480 | * The GPIO1B_SLPM register defines the GPIO mode when SLEEP/DEEP-SLEEP |
| 481 | * mode is entered (i.e. when signal IOFORCE is HIGH by the platform code). |
| 482 | * Each GPIO can be configured to be forced into GPIO mode when IOFORCE is |
| 483 | * HIGH, overriding the normal setting defined by GPIO_AFSELx registers. |
| 484 | * When IOFORCE returns LOW (by software, after SLEEP/DEEP-SLEEP exit), |
| 485 | * the GPIOs return to the normal setting defined by GPIO_AFSELx registers. |
Rabin Vincent | 7e3f7e5 | 2010-09-02 11:28:05 +0100 | [diff] [blame] | 486 | * |
Linus Walleij | 33d7864 | 2011-06-09 11:08:47 +0200 | [diff] [blame] | 487 | * If @mode is NMK_GPIO_SLPM_INPUT, the corresponding GPIO is switched to GPIO |
| 488 | * mode when signal IOFORCE is HIGH (i.e. when SLEEP/DEEP-SLEEP mode is |
| 489 | * entered) regardless of the altfunction selected. Also wake-up detection is |
| 490 | * ENABLED. |
| 491 | * |
| 492 | * If @mode is NMK_GPIO_SLPM_NOCHANGE, the corresponding GPIO remains |
| 493 | * controlled by NMK_GPIO_DATC, NMK_GPIO_DATS, NMK_GPIO_DIR, NMK_GPIO_PDIS |
| 494 | * (for altfunction GPIO) or respective on-chip peripherals (for other |
| 495 | * altfuncs) when IOFORCE is HIGH. Also wake-up detection DISABLED. |
| 496 | * |
| 497 | * Note that enable_irq_wake() will automatically enable wakeup detection. |
Rabin Vincent | 81a3c29 | 2010-05-27 12:39:23 +0100 | [diff] [blame] | 498 | */ |
| 499 | int nmk_gpio_set_slpm(int gpio, enum nmk_gpio_slpm mode) |
| 500 | { |
| 501 | struct nmk_gpio_chip *nmk_chip; |
| 502 | unsigned long flags; |
| 503 | |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 504 | nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP]; |
Rabin Vincent | 81a3c29 | 2010-05-27 12:39:23 +0100 | [diff] [blame] | 505 | if (!nmk_chip) |
| 506 | return -EINVAL; |
| 507 | |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 508 | clk_enable(nmk_chip->clk); |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 509 | spin_lock_irqsave(&nmk_gpio_slpm_lock, flags); |
| 510 | spin_lock(&nmk_chip->lock); |
| 511 | |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 512 | __nmk_gpio_set_slpm(nmk_chip, gpio % NMK_GPIO_PER_CHIP, mode); |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 513 | |
| 514 | spin_unlock(&nmk_chip->lock); |
| 515 | spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags); |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 516 | clk_disable(nmk_chip->clk); |
Rabin Vincent | 81a3c29 | 2010-05-27 12:39:23 +0100 | [diff] [blame] | 517 | |
| 518 | return 0; |
| 519 | } |
| 520 | |
| 521 | /** |
Rabin Vincent | 5b327ed | 2010-05-27 12:29:50 +0100 | [diff] [blame] | 522 | * nmk_gpio_set_pull() - enable/disable pull up/down on a gpio |
| 523 | * @gpio: pin number |
| 524 | * @pull: one of NMK_GPIO_PULL_DOWN, NMK_GPIO_PULL_UP, and NMK_GPIO_PULL_NONE |
| 525 | * |
| 526 | * Enables/disables pull up/down on a specified pin. This only takes effect if |
| 527 | * the pin is configured as an input (either explicitly or by the alternate |
| 528 | * function). |
| 529 | * |
| 530 | * NOTE: If enabling the pull up/down, the caller must ensure that the GPIO is |
| 531 | * configured as an input. Otherwise, due to the way the controller registers |
| 532 | * work, this function will change the value output on the pin. |
| 533 | */ |
| 534 | int nmk_gpio_set_pull(int gpio, enum nmk_gpio_pull pull) |
| 535 | { |
| 536 | struct nmk_gpio_chip *nmk_chip; |
| 537 | unsigned long flags; |
| 538 | |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 539 | nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP]; |
Rabin Vincent | 5b327ed | 2010-05-27 12:29:50 +0100 | [diff] [blame] | 540 | if (!nmk_chip) |
| 541 | return -EINVAL; |
| 542 | |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 543 | clk_enable(nmk_chip->clk); |
Rabin Vincent | 5b327ed | 2010-05-27 12:29:50 +0100 | [diff] [blame] | 544 | spin_lock_irqsave(&nmk_chip->lock, flags); |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 545 | __nmk_gpio_set_pull(nmk_chip, gpio % NMK_GPIO_PER_CHIP, pull); |
Rabin Vincent | 5b327ed | 2010-05-27 12:29:50 +0100 | [diff] [blame] | 546 | spin_unlock_irqrestore(&nmk_chip->lock, flags); |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 547 | clk_disable(nmk_chip->clk); |
Rabin Vincent | 5b327ed | 2010-05-27 12:29:50 +0100 | [diff] [blame] | 548 | |
| 549 | return 0; |
| 550 | } |
| 551 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 552 | /* Mode functions */ |
Jonas Aaberg | 9c66ee6 | 2010-10-13 13:14:17 +0200 | [diff] [blame] | 553 | /** |
| 554 | * nmk_gpio_set_mode() - set the mux mode of a gpio pin |
| 555 | * @gpio: pin number |
| 556 | * @gpio_mode: one of NMK_GPIO_ALT_GPIO, NMK_GPIO_ALT_A, |
| 557 | * NMK_GPIO_ALT_B, and NMK_GPIO_ALT_C |
| 558 | * |
| 559 | * Sets the mode of the specified pin to one of the alternate functions or |
| 560 | * plain GPIO. |
| 561 | */ |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 562 | int nmk_gpio_set_mode(int gpio, int gpio_mode) |
| 563 | { |
| 564 | struct nmk_gpio_chip *nmk_chip; |
| 565 | unsigned long flags; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 566 | |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 567 | nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP]; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 568 | if (!nmk_chip) |
| 569 | return -EINVAL; |
| 570 | |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 571 | clk_enable(nmk_chip->clk); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 572 | spin_lock_irqsave(&nmk_chip->lock, flags); |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 573 | __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] | 574 | spin_unlock_irqrestore(&nmk_chip->lock, flags); |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 575 | clk_disable(nmk_chip->clk); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 576 | |
| 577 | return 0; |
| 578 | } |
| 579 | EXPORT_SYMBOL(nmk_gpio_set_mode); |
| 580 | |
| 581 | int nmk_gpio_get_mode(int gpio) |
| 582 | { |
| 583 | struct nmk_gpio_chip *nmk_chip; |
| 584 | u32 afunc, bfunc, bit; |
| 585 | |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 586 | nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP]; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 587 | if (!nmk_chip) |
| 588 | return -EINVAL; |
| 589 | |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 590 | bit = 1 << (gpio % NMK_GPIO_PER_CHIP); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 591 | |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 592 | clk_enable(nmk_chip->clk); |
| 593 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 594 | afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & bit; |
| 595 | bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & bit; |
| 596 | |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 597 | clk_disable(nmk_chip->clk); |
| 598 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 599 | return (afunc ? NMK_GPIO_ALT_A : 0) | (bfunc ? NMK_GPIO_ALT_B : 0); |
| 600 | } |
| 601 | EXPORT_SYMBOL(nmk_gpio_get_mode); |
| 602 | |
| 603 | |
| 604 | /* IRQ functions */ |
| 605 | static inline int nmk_gpio_get_bitmask(int gpio) |
| 606 | { |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 607 | return 1 << (gpio % NMK_GPIO_PER_CHIP); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 608 | } |
| 609 | |
Lennert Buytenhek | f272c00 | 2010-11-29 11:16:48 +0100 | [diff] [blame] | 610 | static void nmk_gpio_irq_ack(struct irq_data *d) |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 611 | { |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 612 | struct nmk_gpio_chip *nmk_chip; |
| 613 | |
Lennert Buytenhek | f272c00 | 2010-11-29 11:16:48 +0100 | [diff] [blame] | 614 | nmk_chip = irq_data_get_irq_chip_data(d); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 615 | if (!nmk_chip) |
| 616 | return; |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 617 | |
| 618 | clk_enable(nmk_chip->clk); |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 619 | 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] | 620 | clk_disable(nmk_chip->clk); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 621 | } |
| 622 | |
Rabin Vincent | 4d4e20f | 2010-06-16 06:09:34 +0100 | [diff] [blame] | 623 | enum nmk_gpio_irq_type { |
| 624 | NORMAL, |
| 625 | WAKE, |
| 626 | }; |
| 627 | |
Rabin Vincent | 040e5ec | 2010-05-06 10:42:42 +0100 | [diff] [blame] | 628 | static void __nmk_gpio_irq_modify(struct nmk_gpio_chip *nmk_chip, |
Rabin Vincent | 4d4e20f | 2010-06-16 06:09:34 +0100 | [diff] [blame] | 629 | int gpio, enum nmk_gpio_irq_type which, |
| 630 | bool enable) |
Rabin Vincent | 040e5ec | 2010-05-06 10:42:42 +0100 | [diff] [blame] | 631 | { |
| 632 | u32 bitmask = nmk_gpio_get_bitmask(gpio); |
Rabin Vincent | 6c12fe8 | 2011-05-23 12:13:33 +0530 | [diff] [blame] | 633 | u32 *rimscval; |
| 634 | u32 *fimscval; |
| 635 | u32 rimscreg; |
| 636 | u32 fimscreg; |
| 637 | |
| 638 | if (which == NORMAL) { |
| 639 | rimscreg = NMK_GPIO_RIMSC; |
| 640 | fimscreg = NMK_GPIO_FIMSC; |
| 641 | rimscval = &nmk_chip->rimsc; |
| 642 | fimscval = &nmk_chip->fimsc; |
| 643 | } else { |
| 644 | rimscreg = NMK_GPIO_RWIMSC; |
| 645 | fimscreg = NMK_GPIO_FWIMSC; |
| 646 | rimscval = &nmk_chip->rwimsc; |
| 647 | fimscval = &nmk_chip->fwimsc; |
| 648 | } |
Rabin Vincent | 040e5ec | 2010-05-06 10:42:42 +0100 | [diff] [blame] | 649 | |
| 650 | /* we must individually set/clear the two edges */ |
| 651 | if (nmk_chip->edge_rising & bitmask) { |
Rabin Vincent | 040e5ec | 2010-05-06 10:42:42 +0100 | [diff] [blame] | 652 | if (enable) |
Rabin Vincent | 6c12fe8 | 2011-05-23 12:13:33 +0530 | [diff] [blame] | 653 | *rimscval |= bitmask; |
Rabin Vincent | 040e5ec | 2010-05-06 10:42:42 +0100 | [diff] [blame] | 654 | else |
Rabin Vincent | 6c12fe8 | 2011-05-23 12:13:33 +0530 | [diff] [blame] | 655 | *rimscval &= ~bitmask; |
| 656 | writel(*rimscval, nmk_chip->addr + rimscreg); |
Rabin Vincent | 040e5ec | 2010-05-06 10:42:42 +0100 | [diff] [blame] | 657 | } |
| 658 | if (nmk_chip->edge_falling & bitmask) { |
Rabin Vincent | 040e5ec | 2010-05-06 10:42:42 +0100 | [diff] [blame] | 659 | if (enable) |
Rabin Vincent | 6c12fe8 | 2011-05-23 12:13:33 +0530 | [diff] [blame] | 660 | *fimscval |= bitmask; |
Rabin Vincent | 040e5ec | 2010-05-06 10:42:42 +0100 | [diff] [blame] | 661 | else |
Rabin Vincent | 6c12fe8 | 2011-05-23 12:13:33 +0530 | [diff] [blame] | 662 | *fimscval &= ~bitmask; |
| 663 | writel(*fimscval, nmk_chip->addr + fimscreg); |
Rabin Vincent | 040e5ec | 2010-05-06 10:42:42 +0100 | [diff] [blame] | 664 | } |
| 665 | } |
| 666 | |
Rabin Vincent | b9df468 | 2011-02-10 11:45:58 +0530 | [diff] [blame] | 667 | static void __nmk_gpio_set_wake(struct nmk_gpio_chip *nmk_chip, |
| 668 | int gpio, bool on) |
| 669 | { |
Rabin Vincent | b982ff0 | 2011-04-26 09:03:27 +0530 | [diff] [blame] | 670 | /* |
| 671 | * Ensure WAKEUP_ENABLE is on. No need to disable it if wakeup is |
| 672 | * disabled, since setting SLPM to 1 increases power consumption, and |
| 673 | * wakeup is anyhow controlled by the RIMSC and FIMSC registers. |
| 674 | */ |
| 675 | if (nmk_chip->sleepmode && on) { |
Linus Walleij | e85bbc1 | 2012-06-12 12:43:06 +0200 | [diff] [blame] | 676 | __nmk_gpio_set_slpm(nmk_chip, gpio % NMK_GPIO_PER_CHIP, |
Rabin Vincent | b982ff0 | 2011-04-26 09:03:27 +0530 | [diff] [blame] | 677 | NMK_GPIO_SLPM_WAKEUP_ENABLE); |
Linus Walleij | 33d7864 | 2011-06-09 11:08:47 +0200 | [diff] [blame] | 678 | } |
| 679 | |
Rabin Vincent | b9df468 | 2011-02-10 11:45:58 +0530 | [diff] [blame] | 680 | __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, on); |
| 681 | } |
| 682 | |
| 683 | static int nmk_gpio_irq_maskunmask(struct irq_data *d, bool enable) |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 684 | { |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 685 | struct nmk_gpio_chip *nmk_chip; |
| 686 | unsigned long flags; |
Rabin Vincent | 040e5ec | 2010-05-06 10:42:42 +0100 | [diff] [blame] | 687 | u32 bitmask; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 688 | |
Lennert Buytenhek | f272c00 | 2010-11-29 11:16:48 +0100 | [diff] [blame] | 689 | nmk_chip = irq_data_get_irq_chip_data(d); |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 690 | bitmask = nmk_gpio_get_bitmask(d->hwirq); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 691 | if (!nmk_chip) |
Rabin Vincent | 4d4e20f | 2010-06-16 06:09:34 +0100 | [diff] [blame] | 692 | return -EINVAL; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 693 | |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 694 | clk_enable(nmk_chip->clk); |
Rabin Vincent | b9df468 | 2011-02-10 11:45:58 +0530 | [diff] [blame] | 695 | spin_lock_irqsave(&nmk_gpio_slpm_lock, flags); |
| 696 | spin_lock(&nmk_chip->lock); |
| 697 | |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 698 | __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, enable); |
Rabin Vincent | b9df468 | 2011-02-10 11:45:58 +0530 | [diff] [blame] | 699 | |
| 700 | if (!(nmk_chip->real_wake & bitmask)) |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 701 | __nmk_gpio_set_wake(nmk_chip, d->hwirq, enable); |
Rabin Vincent | b9df468 | 2011-02-10 11:45:58 +0530 | [diff] [blame] | 702 | |
| 703 | spin_unlock(&nmk_chip->lock); |
| 704 | spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags); |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 705 | clk_disable(nmk_chip->clk); |
Rabin Vincent | 4d4e20f | 2010-06-16 06:09:34 +0100 | [diff] [blame] | 706 | |
| 707 | return 0; |
Rabin Vincent | 040e5ec | 2010-05-06 10:42:42 +0100 | [diff] [blame] | 708 | } |
| 709 | |
Lennert Buytenhek | f272c00 | 2010-11-29 11:16:48 +0100 | [diff] [blame] | 710 | static void nmk_gpio_irq_mask(struct irq_data *d) |
Rabin Vincent | 040e5ec | 2010-05-06 10:42:42 +0100 | [diff] [blame] | 711 | { |
Rabin Vincent | b9df468 | 2011-02-10 11:45:58 +0530 | [diff] [blame] | 712 | nmk_gpio_irq_maskunmask(d, false); |
Rabin Vincent | 4d4e20f | 2010-06-16 06:09:34 +0100 | [diff] [blame] | 713 | } |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 714 | |
Lennert Buytenhek | f272c00 | 2010-11-29 11:16:48 +0100 | [diff] [blame] | 715 | static void nmk_gpio_irq_unmask(struct irq_data *d) |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 716 | { |
Rabin Vincent | b9df468 | 2011-02-10 11:45:58 +0530 | [diff] [blame] | 717 | nmk_gpio_irq_maskunmask(d, true); |
Rabin Vincent | 4d4e20f | 2010-06-16 06:09:34 +0100 | [diff] [blame] | 718 | } |
| 719 | |
Lennert Buytenhek | f272c00 | 2010-11-29 11:16:48 +0100 | [diff] [blame] | 720 | 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] | 721 | { |
Rabin Vincent | 7e3f7e5 | 2010-09-02 11:28:05 +0100 | [diff] [blame] | 722 | struct nmk_gpio_chip *nmk_chip; |
| 723 | unsigned long flags; |
Rabin Vincent | b9df468 | 2011-02-10 11:45:58 +0530 | [diff] [blame] | 724 | u32 bitmask; |
Rabin Vincent | 7e3f7e5 | 2010-09-02 11:28:05 +0100 | [diff] [blame] | 725 | |
Lennert Buytenhek | f272c00 | 2010-11-29 11:16:48 +0100 | [diff] [blame] | 726 | nmk_chip = irq_data_get_irq_chip_data(d); |
Rabin Vincent | 7e3f7e5 | 2010-09-02 11:28:05 +0100 | [diff] [blame] | 727 | if (!nmk_chip) |
| 728 | return -EINVAL; |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 729 | bitmask = nmk_gpio_get_bitmask(d->hwirq); |
Rabin Vincent | 7e3f7e5 | 2010-09-02 11:28:05 +0100 | [diff] [blame] | 730 | |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 731 | clk_enable(nmk_chip->clk); |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 732 | spin_lock_irqsave(&nmk_gpio_slpm_lock, flags); |
| 733 | spin_lock(&nmk_chip->lock); |
| 734 | |
Linus Walleij | 479a0c7 | 2011-09-20 10:50:15 +0200 | [diff] [blame] | 735 | if (irqd_irq_disabled(d)) |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 736 | __nmk_gpio_set_wake(nmk_chip, d->hwirq, on); |
Rabin Vincent | b9df468 | 2011-02-10 11:45:58 +0530 | [diff] [blame] | 737 | |
| 738 | if (on) |
| 739 | nmk_chip->real_wake |= bitmask; |
| 740 | else |
| 741 | nmk_chip->real_wake &= ~bitmask; |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 742 | |
| 743 | spin_unlock(&nmk_chip->lock); |
| 744 | spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags); |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 745 | clk_disable(nmk_chip->clk); |
Rabin Vincent | 7e3f7e5 | 2010-09-02 11:28:05 +0100 | [diff] [blame] | 746 | |
| 747 | return 0; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 748 | } |
| 749 | |
Lennert Buytenhek | f272c00 | 2010-11-29 11:16:48 +0100 | [diff] [blame] | 750 | 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] | 751 | { |
Linus Walleij | 479a0c7 | 2011-09-20 10:50:15 +0200 | [diff] [blame] | 752 | bool enabled = !irqd_irq_disabled(d); |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 753 | bool wake = irqd_is_wakeup_set(d); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 754 | struct nmk_gpio_chip *nmk_chip; |
| 755 | unsigned long flags; |
| 756 | u32 bitmask; |
| 757 | |
Lennert Buytenhek | f272c00 | 2010-11-29 11:16:48 +0100 | [diff] [blame] | 758 | nmk_chip = irq_data_get_irq_chip_data(d); |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 759 | bitmask = nmk_gpio_get_bitmask(d->hwirq); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 760 | if (!nmk_chip) |
| 761 | return -EINVAL; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 762 | if (type & IRQ_TYPE_LEVEL_HIGH) |
| 763 | return -EINVAL; |
| 764 | if (type & IRQ_TYPE_LEVEL_LOW) |
| 765 | return -EINVAL; |
| 766 | |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 767 | clk_enable(nmk_chip->clk); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 768 | spin_lock_irqsave(&nmk_chip->lock, flags); |
| 769 | |
Rabin Vincent | 7a852d8 | 2010-05-06 10:43:55 +0100 | [diff] [blame] | 770 | if (enabled) |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 771 | __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, false); |
Rabin Vincent | 4d4e20f | 2010-06-16 06:09:34 +0100 | [diff] [blame] | 772 | |
Rabin Vincent | b9df468 | 2011-02-10 11:45:58 +0530 | [diff] [blame] | 773 | if (enabled || wake) |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 774 | __nmk_gpio_irq_modify(nmk_chip, d->hwirq, WAKE, false); |
Rabin Vincent | 7a852d8 | 2010-05-06 10:43:55 +0100 | [diff] [blame] | 775 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 776 | nmk_chip->edge_rising &= ~bitmask; |
| 777 | if (type & IRQ_TYPE_EDGE_RISING) |
| 778 | nmk_chip->edge_rising |= bitmask; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 779 | |
| 780 | nmk_chip->edge_falling &= ~bitmask; |
| 781 | if (type & IRQ_TYPE_EDGE_FALLING) |
| 782 | nmk_chip->edge_falling |= bitmask; |
Rabin Vincent | 7a852d8 | 2010-05-06 10:43:55 +0100 | [diff] [blame] | 783 | |
| 784 | if (enabled) |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 785 | __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, true); |
Rabin Vincent | 4d4e20f | 2010-06-16 06:09:34 +0100 | [diff] [blame] | 786 | |
Rabin Vincent | b9df468 | 2011-02-10 11:45:58 +0530 | [diff] [blame] | 787 | if (enabled || wake) |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 788 | __nmk_gpio_irq_modify(nmk_chip, d->hwirq, WAKE, true); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 789 | |
| 790 | spin_unlock_irqrestore(&nmk_chip->lock, flags); |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 791 | clk_disable(nmk_chip->clk); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 792 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 793 | return 0; |
| 794 | } |
| 795 | |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 796 | static unsigned int nmk_gpio_irq_startup(struct irq_data *d) |
| 797 | { |
| 798 | struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d); |
| 799 | |
| 800 | clk_enable(nmk_chip->clk); |
| 801 | nmk_gpio_irq_unmask(d); |
| 802 | return 0; |
| 803 | } |
| 804 | |
| 805 | static void nmk_gpio_irq_shutdown(struct irq_data *d) |
| 806 | { |
| 807 | struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d); |
| 808 | |
| 809 | nmk_gpio_irq_mask(d); |
| 810 | clk_disable(nmk_chip->clk); |
| 811 | } |
| 812 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 813 | static struct irq_chip nmk_gpio_irq_chip = { |
| 814 | .name = "Nomadik-GPIO", |
Lennert Buytenhek | f272c00 | 2010-11-29 11:16:48 +0100 | [diff] [blame] | 815 | .irq_ack = nmk_gpio_irq_ack, |
| 816 | .irq_mask = nmk_gpio_irq_mask, |
| 817 | .irq_unmask = nmk_gpio_irq_unmask, |
| 818 | .irq_set_type = nmk_gpio_irq_set_type, |
| 819 | .irq_set_wake = nmk_gpio_irq_set_wake, |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 820 | .irq_startup = nmk_gpio_irq_startup, |
| 821 | .irq_shutdown = nmk_gpio_irq_shutdown, |
Etienne Carriere | 4921e745 | 2012-08-22 10:44:16 +0200 | [diff] [blame] | 822 | .flags = IRQCHIP_MASK_ON_SUSPEND, |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 823 | }; |
| 824 | |
Rabin Vincent | 33b744b | 2010-10-14 10:38:03 +0530 | [diff] [blame] | 825 | static void __nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc, |
| 826 | u32 status) |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 827 | { |
| 828 | struct nmk_gpio_chip *nmk_chip; |
Thomas Gleixner | 6845664a | 2011-03-24 13:25:22 +0100 | [diff] [blame] | 829 | struct irq_chip *host_chip = irq_get_chip(irq); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 830 | |
Will Deacon | adfed15 | 2011-02-28 10:12:29 +0000 | [diff] [blame] | 831 | chained_irq_enter(host_chip, desc); |
Rabin Vincent | aaedaa2 | 2010-03-03 04:50:27 +0100 | [diff] [blame] | 832 | |
Thomas Gleixner | 6845664a | 2011-03-24 13:25:22 +0100 | [diff] [blame] | 833 | nmk_chip = irq_get_handler_data(irq); |
Rabin Vincent | 33b744b | 2010-10-14 10:38:03 +0530 | [diff] [blame] | 834 | while (status) { |
| 835 | int bit = __ffs(status); |
| 836 | |
Linus Walleij | 95f0bc9 | 2012-09-27 14:14:09 +0200 | [diff] [blame] | 837 | generic_handle_irq(irq_find_mapping(nmk_chip->domain, bit)); |
Rabin Vincent | 33b744b | 2010-10-14 10:38:03 +0530 | [diff] [blame] | 838 | status &= ~BIT(bit); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 839 | } |
Rabin Vincent | aaedaa2 | 2010-03-03 04:50:27 +0100 | [diff] [blame] | 840 | |
Will Deacon | adfed15 | 2011-02-28 10:12:29 +0000 | [diff] [blame] | 841 | chained_irq_exit(host_chip, desc); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 842 | } |
| 843 | |
Rabin Vincent | 33b744b | 2010-10-14 10:38:03 +0530 | [diff] [blame] | 844 | static void nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc) |
| 845 | { |
Thomas Gleixner | 6845664a | 2011-03-24 13:25:22 +0100 | [diff] [blame] | 846 | struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq); |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 847 | u32 status; |
| 848 | |
| 849 | clk_enable(nmk_chip->clk); |
| 850 | status = readl(nmk_chip->addr + NMK_GPIO_IS); |
| 851 | clk_disable(nmk_chip->clk); |
Rabin Vincent | 33b744b | 2010-10-14 10:38:03 +0530 | [diff] [blame] | 852 | |
| 853 | __nmk_gpio_irq_handler(irq, desc, status); |
| 854 | } |
| 855 | |
| 856 | static void nmk_gpio_secondary_irq_handler(unsigned int irq, |
| 857 | struct irq_desc *desc) |
| 858 | { |
Thomas Gleixner | 6845664a | 2011-03-24 13:25:22 +0100 | [diff] [blame] | 859 | struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq); |
Rabin Vincent | 33b744b | 2010-10-14 10:38:03 +0530 | [diff] [blame] | 860 | u32 status = nmk_chip->get_secondary_status(nmk_chip->bank); |
| 861 | |
| 862 | __nmk_gpio_irq_handler(irq, desc, status); |
| 863 | } |
| 864 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 865 | static int nmk_gpio_init_irq(struct nmk_gpio_chip *nmk_chip) |
| 866 | { |
Thomas Gleixner | 6845664a | 2011-03-24 13:25:22 +0100 | [diff] [blame] | 867 | irq_set_chained_handler(nmk_chip->parent_irq, nmk_gpio_irq_handler); |
| 868 | irq_set_handler_data(nmk_chip->parent_irq, nmk_chip); |
Rabin Vincent | 33b744b | 2010-10-14 10:38:03 +0530 | [diff] [blame] | 869 | |
| 870 | if (nmk_chip->secondary_parent_irq >= 0) { |
Thomas Gleixner | 6845664a | 2011-03-24 13:25:22 +0100 | [diff] [blame] | 871 | irq_set_chained_handler(nmk_chip->secondary_parent_irq, |
Rabin Vincent | 33b744b | 2010-10-14 10:38:03 +0530 | [diff] [blame] | 872 | nmk_gpio_secondary_irq_handler); |
Thomas Gleixner | 6845664a | 2011-03-24 13:25:22 +0100 | [diff] [blame] | 873 | irq_set_handler_data(nmk_chip->secondary_parent_irq, nmk_chip); |
Rabin Vincent | 33b744b | 2010-10-14 10:38:03 +0530 | [diff] [blame] | 874 | } |
| 875 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 876 | return 0; |
| 877 | } |
| 878 | |
| 879 | /* I/O Functions */ |
Linus Walleij | dbfe8ca | 2012-05-02 22:56:47 +0200 | [diff] [blame] | 880 | |
| 881 | static int nmk_gpio_request(struct gpio_chip *chip, unsigned offset) |
| 882 | { |
| 883 | /* |
| 884 | * Map back to global GPIO space and request muxing, the direction |
| 885 | * parameter does not matter for this controller. |
| 886 | */ |
| 887 | int gpio = chip->base + offset; |
| 888 | |
| 889 | return pinctrl_request_gpio(gpio); |
| 890 | } |
| 891 | |
| 892 | static void nmk_gpio_free(struct gpio_chip *chip, unsigned offset) |
| 893 | { |
| 894 | int gpio = chip->base + offset; |
| 895 | |
| 896 | pinctrl_free_gpio(gpio); |
| 897 | } |
| 898 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 899 | static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned offset) |
| 900 | { |
| 901 | struct nmk_gpio_chip *nmk_chip = |
| 902 | container_of(chip, struct nmk_gpio_chip, chip); |
| 903 | |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 904 | clk_enable(nmk_chip->clk); |
| 905 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 906 | writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC); |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 907 | |
| 908 | clk_disable(nmk_chip->clk); |
| 909 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 910 | return 0; |
| 911 | } |
| 912 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 913 | static int nmk_gpio_get_input(struct gpio_chip *chip, unsigned offset) |
| 914 | { |
| 915 | struct nmk_gpio_chip *nmk_chip = |
| 916 | container_of(chip, struct nmk_gpio_chip, chip); |
| 917 | u32 bit = 1 << offset; |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 918 | int value; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 919 | |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 920 | clk_enable(nmk_chip->clk); |
| 921 | |
| 922 | value = (readl(nmk_chip->addr + NMK_GPIO_DAT) & bit) != 0; |
| 923 | |
| 924 | clk_disable(nmk_chip->clk); |
| 925 | |
| 926 | return value; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | static void nmk_gpio_set_output(struct gpio_chip *chip, unsigned offset, |
| 930 | int val) |
| 931 | { |
| 932 | struct nmk_gpio_chip *nmk_chip = |
| 933 | container_of(chip, struct nmk_gpio_chip, chip); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 934 | |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 935 | clk_enable(nmk_chip->clk); |
| 936 | |
Rabin Vincent | 6720db7 | 2010-09-02 11:28:48 +0100 | [diff] [blame] | 937 | __nmk_gpio_set_output(nmk_chip, offset, val); |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 938 | |
| 939 | clk_disable(nmk_chip->clk); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 940 | } |
| 941 | |
Rabin Vincent | 6647c6c | 2010-05-27 12:22:42 +0100 | [diff] [blame] | 942 | static int nmk_gpio_make_output(struct gpio_chip *chip, unsigned offset, |
| 943 | int val) |
| 944 | { |
| 945 | struct nmk_gpio_chip *nmk_chip = |
| 946 | container_of(chip, struct nmk_gpio_chip, chip); |
| 947 | |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 948 | clk_enable(nmk_chip->clk); |
| 949 | |
Rabin Vincent | 6720db7 | 2010-09-02 11:28:48 +0100 | [diff] [blame] | 950 | __nmk_gpio_make_output(nmk_chip, offset, val); |
Rabin Vincent | 6647c6c | 2010-05-27 12:22:42 +0100 | [diff] [blame] | 951 | |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 952 | clk_disable(nmk_chip->clk); |
| 953 | |
Rabin Vincent | 6647c6c | 2010-05-27 12:22:42 +0100 | [diff] [blame] | 954 | return 0; |
| 955 | } |
| 956 | |
Rabin Vincent | 0d2aec9 | 2010-06-16 06:10:43 +0100 | [diff] [blame] | 957 | static int nmk_gpio_to_irq(struct gpio_chip *chip, unsigned offset) |
| 958 | { |
| 959 | struct nmk_gpio_chip *nmk_chip = |
| 960 | container_of(chip, struct nmk_gpio_chip, chip); |
| 961 | |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 962 | return irq_find_mapping(nmk_chip->domain, offset); |
Rabin Vincent | 0d2aec9 | 2010-06-16 06:10:43 +0100 | [diff] [blame] | 963 | } |
| 964 | |
Rabin Vincent | d0b543c | 2010-03-04 17:39:05 +0530 | [diff] [blame] | 965 | #ifdef CONFIG_DEBUG_FS |
| 966 | |
| 967 | #include <linux/seq_file.h> |
| 968 | |
Linus Walleij | 6f4350a | 2012-05-02 21:06:13 +0200 | [diff] [blame] | 969 | static void nmk_gpio_dbg_show_one(struct seq_file *s, struct gpio_chip *chip, |
| 970 | unsigned offset, unsigned gpio) |
Rabin Vincent | d0b543c | 2010-03-04 17:39:05 +0530 | [diff] [blame] | 971 | { |
Linus Walleij | 6f4350a | 2012-05-02 21:06:13 +0200 | [diff] [blame] | 972 | const char *label = gpiochip_is_requested(chip, offset); |
Rabin Vincent | d0b543c | 2010-03-04 17:39:05 +0530 | [diff] [blame] | 973 | struct nmk_gpio_chip *nmk_chip = |
| 974 | container_of(chip, struct nmk_gpio_chip, chip); |
Linus Walleij | 6f4350a | 2012-05-02 21:06:13 +0200 | [diff] [blame] | 975 | int mode; |
| 976 | bool is_out; |
| 977 | bool pull; |
| 978 | u32 bit = 1 << offset; |
Rabin Vincent | d0b543c | 2010-03-04 17:39:05 +0530 | [diff] [blame] | 979 | const char *modes[] = { |
| 980 | [NMK_GPIO_ALT_GPIO] = "gpio", |
| 981 | [NMK_GPIO_ALT_A] = "altA", |
| 982 | [NMK_GPIO_ALT_B] = "altB", |
| 983 | [NMK_GPIO_ALT_C] = "altC", |
| 984 | }; |
| 985 | |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 986 | clk_enable(nmk_chip->clk); |
Linus Walleij | 6f4350a | 2012-05-02 21:06:13 +0200 | [diff] [blame] | 987 | is_out = !!(readl(nmk_chip->addr + NMK_GPIO_DIR) & bit); |
| 988 | pull = !(readl(nmk_chip->addr + NMK_GPIO_PDIS) & bit); |
| 989 | mode = nmk_gpio_get_mode(gpio); |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 990 | |
Linus Walleij | 6f4350a | 2012-05-02 21:06:13 +0200 | [diff] [blame] | 991 | seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s %s", |
| 992 | gpio, label ?: "(none)", |
| 993 | is_out ? "out" : "in ", |
| 994 | chip->get |
| 995 | ? (chip->get(chip, offset) ? "hi" : "lo") |
| 996 | : "? ", |
| 997 | (mode < 0) ? "unknown" : modes[mode], |
| 998 | pull ? "pull" : "none"); |
Rabin Vincent | d0b543c | 2010-03-04 17:39:05 +0530 | [diff] [blame] | 999 | |
Linus Walleij | 6f4350a | 2012-05-02 21:06:13 +0200 | [diff] [blame] | 1000 | if (label && !is_out) { |
| 1001 | int irq = gpio_to_irq(gpio); |
| 1002 | struct irq_desc *desc = irq_to_desc(irq); |
Rabin Vincent | 8ea72a3 | 2011-05-24 23:07:09 +0200 | [diff] [blame] | 1003 | |
Linus Walleij | 6f4350a | 2012-05-02 21:06:13 +0200 | [diff] [blame] | 1004 | /* This races with request_irq(), set_irq_type(), |
| 1005 | * and set_irq_wake() ... but those are "rare". |
| 1006 | */ |
| 1007 | if (irq >= 0 && desc->action) { |
| 1008 | char *trigger; |
| 1009 | u32 bitmask = nmk_gpio_get_bitmask(gpio); |
Rabin Vincent | 8ea72a3 | 2011-05-24 23:07:09 +0200 | [diff] [blame] | 1010 | |
Linus Walleij | 6f4350a | 2012-05-02 21:06:13 +0200 | [diff] [blame] | 1011 | if (nmk_chip->edge_rising & bitmask) |
| 1012 | trigger = "edge-rising"; |
| 1013 | else if (nmk_chip->edge_falling & bitmask) |
| 1014 | trigger = "edge-falling"; |
| 1015 | else |
| 1016 | trigger = "edge-undefined"; |
Rabin Vincent | 8ea72a3 | 2011-05-24 23:07:09 +0200 | [diff] [blame] | 1017 | |
Linus Walleij | 6f4350a | 2012-05-02 21:06:13 +0200 | [diff] [blame] | 1018 | seq_printf(s, " irq-%d %s%s", |
| 1019 | irq, trigger, |
| 1020 | irqd_is_wakeup_set(&desc->irq_data) |
| 1021 | ? " wakeup" : ""); |
Rabin Vincent | 8ea72a3 | 2011-05-24 23:07:09 +0200 | [diff] [blame] | 1022 | } |
Rabin Vincent | d0b543c | 2010-03-04 17:39:05 +0530 | [diff] [blame] | 1023 | } |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 1024 | clk_disable(nmk_chip->clk); |
Rabin Vincent | d0b543c | 2010-03-04 17:39:05 +0530 | [diff] [blame] | 1025 | } |
| 1026 | |
Linus Walleij | 6f4350a | 2012-05-02 21:06:13 +0200 | [diff] [blame] | 1027 | static void nmk_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip) |
| 1028 | { |
| 1029 | unsigned i; |
| 1030 | unsigned gpio = chip->base; |
| 1031 | |
| 1032 | for (i = 0; i < chip->ngpio; i++, gpio++) { |
| 1033 | nmk_gpio_dbg_show_one(s, chip, i, gpio); |
| 1034 | seq_printf(s, "\n"); |
| 1035 | } |
| 1036 | } |
| 1037 | |
Rabin Vincent | d0b543c | 2010-03-04 17:39:05 +0530 | [diff] [blame] | 1038 | #else |
Linus Walleij | 6f4350a | 2012-05-02 21:06:13 +0200 | [diff] [blame] | 1039 | static inline void nmk_gpio_dbg_show_one(struct seq_file *s, |
| 1040 | struct gpio_chip *chip, |
| 1041 | unsigned offset, unsigned gpio) |
| 1042 | { |
| 1043 | } |
Rabin Vincent | d0b543c | 2010-03-04 17:39:05 +0530 | [diff] [blame] | 1044 | #define nmk_gpio_dbg_show NULL |
| 1045 | #endif |
| 1046 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1047 | /* This structure is replicated for each GPIO block allocated at probe time */ |
| 1048 | static struct gpio_chip nmk_gpio_template = { |
Linus Walleij | dbfe8ca | 2012-05-02 22:56:47 +0200 | [diff] [blame] | 1049 | .request = nmk_gpio_request, |
| 1050 | .free = nmk_gpio_free, |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1051 | .direction_input = nmk_gpio_make_input, |
| 1052 | .get = nmk_gpio_get_input, |
| 1053 | .direction_output = nmk_gpio_make_output, |
| 1054 | .set = nmk_gpio_set_output, |
Rabin Vincent | 0d2aec9 | 2010-06-16 06:10:43 +0100 | [diff] [blame] | 1055 | .to_irq = nmk_gpio_to_irq, |
Rabin Vincent | d0b543c | 2010-03-04 17:39:05 +0530 | [diff] [blame] | 1056 | .dbg_show = nmk_gpio_dbg_show, |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1057 | .can_sleep = 0, |
| 1058 | }; |
| 1059 | |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 1060 | void nmk_gpio_clocks_enable(void) |
| 1061 | { |
| 1062 | int i; |
| 1063 | |
| 1064 | for (i = 0; i < NUM_BANKS; i++) { |
| 1065 | struct nmk_gpio_chip *chip = nmk_gpio_chips[i]; |
| 1066 | |
| 1067 | if (!chip) |
| 1068 | continue; |
| 1069 | |
| 1070 | clk_enable(chip->clk); |
| 1071 | } |
| 1072 | } |
| 1073 | |
| 1074 | void nmk_gpio_clocks_disable(void) |
| 1075 | { |
| 1076 | int i; |
| 1077 | |
| 1078 | for (i = 0; i < NUM_BANKS; i++) { |
| 1079 | struct nmk_gpio_chip *chip = nmk_gpio_chips[i]; |
| 1080 | |
| 1081 | if (!chip) |
| 1082 | continue; |
| 1083 | |
| 1084 | clk_disable(chip->clk); |
| 1085 | } |
| 1086 | } |
| 1087 | |
Rabin Vincent | b9df468 | 2011-02-10 11:45:58 +0530 | [diff] [blame] | 1088 | /* |
| 1089 | * Called from the suspend/resume path to only keep the real wakeup interrupts |
| 1090 | * (those that have had set_irq_wake() called on them) as wakeup interrupts, |
| 1091 | * and not the rest of the interrupts which we needed to have as wakeups for |
| 1092 | * cpuidle. |
| 1093 | * |
| 1094 | * PM ops are not used since this needs to be done at the end, after all the |
| 1095 | * other drivers are done with their suspend callbacks. |
| 1096 | */ |
| 1097 | void nmk_gpio_wakeups_suspend(void) |
| 1098 | { |
| 1099 | int i; |
| 1100 | |
| 1101 | for (i = 0; i < NUM_BANKS; i++) { |
| 1102 | struct nmk_gpio_chip *chip = nmk_gpio_chips[i]; |
| 1103 | |
| 1104 | if (!chip) |
| 1105 | break; |
| 1106 | |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 1107 | clk_enable(chip->clk); |
| 1108 | |
Rabin Vincent | b9df468 | 2011-02-10 11:45:58 +0530 | [diff] [blame] | 1109 | writel(chip->rwimsc & chip->real_wake, |
| 1110 | chip->addr + NMK_GPIO_RWIMSC); |
| 1111 | writel(chip->fwimsc & chip->real_wake, |
| 1112 | chip->addr + NMK_GPIO_FWIMSC); |
| 1113 | |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 1114 | clk_disable(chip->clk); |
Rabin Vincent | b9df468 | 2011-02-10 11:45:58 +0530 | [diff] [blame] | 1115 | } |
| 1116 | } |
| 1117 | |
| 1118 | void nmk_gpio_wakeups_resume(void) |
| 1119 | { |
| 1120 | int i; |
| 1121 | |
| 1122 | for (i = 0; i < NUM_BANKS; i++) { |
| 1123 | struct nmk_gpio_chip *chip = nmk_gpio_chips[i]; |
| 1124 | |
| 1125 | if (!chip) |
| 1126 | break; |
| 1127 | |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 1128 | clk_enable(chip->clk); |
| 1129 | |
Rabin Vincent | b9df468 | 2011-02-10 11:45:58 +0530 | [diff] [blame] | 1130 | writel(chip->rwimsc, chip->addr + NMK_GPIO_RWIMSC); |
| 1131 | writel(chip->fwimsc, chip->addr + NMK_GPIO_FWIMSC); |
| 1132 | |
Rabin Vincent | 3c0227d | 2011-09-20 10:50:03 +0200 | [diff] [blame] | 1133 | clk_disable(chip->clk); |
Rabin Vincent | b9df468 | 2011-02-10 11:45:58 +0530 | [diff] [blame] | 1134 | } |
| 1135 | } |
| 1136 | |
Rickard Andersson | bc6f5cf | 2011-05-24 23:07:17 +0200 | [diff] [blame] | 1137 | /* |
| 1138 | * Read the pull up/pull down status. |
| 1139 | * A bit set in 'pull_up' means that pull up |
| 1140 | * is selected if pull is enabled in PDIS register. |
| 1141 | * Note: only pull up/down set via this driver can |
| 1142 | * be detected due to HW limitations. |
| 1143 | */ |
| 1144 | void nmk_gpio_read_pull(int gpio_bank, u32 *pull_up) |
| 1145 | { |
| 1146 | if (gpio_bank < NUM_BANKS) { |
| 1147 | struct nmk_gpio_chip *chip = nmk_gpio_chips[gpio_bank]; |
| 1148 | |
| 1149 | if (!chip) |
| 1150 | return; |
| 1151 | |
| 1152 | *pull_up = chip->pull_up; |
| 1153 | } |
| 1154 | } |
| 1155 | |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 1156 | int nmk_gpio_irq_map(struct irq_domain *d, unsigned int irq, |
| 1157 | irq_hw_number_t hwirq) |
| 1158 | { |
| 1159 | struct nmk_gpio_chip *nmk_chip = d->host_data; |
| 1160 | |
| 1161 | if (!nmk_chip) |
| 1162 | return -EINVAL; |
| 1163 | |
| 1164 | irq_set_chip_and_handler(irq, &nmk_gpio_irq_chip, handle_edge_irq); |
| 1165 | set_irq_flags(irq, IRQF_VALID); |
| 1166 | irq_set_chip_data(irq, nmk_chip); |
| 1167 | irq_set_irq_type(irq, IRQ_TYPE_EDGE_FALLING); |
| 1168 | |
| 1169 | return 0; |
| 1170 | } |
| 1171 | |
| 1172 | const struct irq_domain_ops nmk_gpio_irq_simple_ops = { |
| 1173 | .map = nmk_gpio_irq_map, |
| 1174 | .xlate = irq_domain_xlate_twocell, |
| 1175 | }; |
| 1176 | |
Uwe Kleine-König | fd0d67d | 2010-09-02 16:13:35 +0100 | [diff] [blame] | 1177 | static int __devinit nmk_gpio_probe(struct platform_device *dev) |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1178 | { |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 1179 | struct nmk_gpio_platform_data *pdata = dev->dev.platform_data; |
Lee Jones | 513c27f | 2012-04-13 15:05:05 +0100 | [diff] [blame] | 1180 | struct device_node *np = dev->dev.of_node; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1181 | struct nmk_gpio_chip *nmk_chip; |
| 1182 | struct gpio_chip *chip; |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 1183 | struct resource *res; |
Rabin Vincent | af7dc22 | 2010-05-06 11:14:17 +0100 | [diff] [blame] | 1184 | struct clk *clk; |
Rabin Vincent | 33b744b | 2010-10-14 10:38:03 +0530 | [diff] [blame] | 1185 | int secondary_irq; |
Linus Walleij | 8d91771 | 2012-04-17 10:15:54 +0200 | [diff] [blame] | 1186 | void __iomem *base; |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 1187 | int irq; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1188 | int ret; |
| 1189 | |
Lee Jones | 513c27f | 2012-04-13 15:05:05 +0100 | [diff] [blame] | 1190 | if (!pdata && !np) { |
| 1191 | dev_err(&dev->dev, "No platform data or device tree found\n"); |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 1192 | return -ENODEV; |
Lee Jones | 513c27f | 2012-04-13 15:05:05 +0100 | [diff] [blame] | 1193 | } |
| 1194 | |
| 1195 | if (np) { |
Linus Walleij | 5e754f3 | 2012-07-03 23:05:14 +0200 | [diff] [blame] | 1196 | pdata = devm_kzalloc(&dev->dev, sizeof(*pdata), GFP_KERNEL); |
Lee Jones | 513c27f | 2012-04-13 15:05:05 +0100 | [diff] [blame] | 1197 | if (!pdata) |
| 1198 | return -ENOMEM; |
| 1199 | |
Lee Jones | 612e1d5 | 2012-06-14 11:27:56 +0100 | [diff] [blame] | 1200 | if (of_get_property(np, "st,supports-sleepmode", NULL)) |
Lee Jones | 513c27f | 2012-04-13 15:05:05 +0100 | [diff] [blame] | 1201 | pdata->supports_sleepmode = true; |
| 1202 | |
| 1203 | if (of_property_read_u32(np, "gpio-bank", &dev->id)) { |
| 1204 | dev_err(&dev->dev, "gpio-bank property not found\n"); |
| 1205 | ret = -EINVAL; |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 1206 | goto out; |
Lee Jones | 513c27f | 2012-04-13 15:05:05 +0100 | [diff] [blame] | 1207 | } |
| 1208 | |
| 1209 | pdata->first_gpio = dev->id * NMK_GPIO_PER_CHIP; |
| 1210 | pdata->num_gpio = NMK_GPIO_PER_CHIP; |
| 1211 | } |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 1212 | |
| 1213 | res = platform_get_resource(dev, IORESOURCE_MEM, 0); |
| 1214 | if (!res) { |
| 1215 | ret = -ENOENT; |
| 1216 | goto out; |
| 1217 | } |
| 1218 | |
| 1219 | irq = platform_get_irq(dev, 0); |
| 1220 | if (irq < 0) { |
| 1221 | ret = irq; |
| 1222 | goto out; |
| 1223 | } |
| 1224 | |
Rabin Vincent | 33b744b | 2010-10-14 10:38:03 +0530 | [diff] [blame] | 1225 | secondary_irq = platform_get_irq(dev, 1); |
| 1226 | if (secondary_irq >= 0 && !pdata->get_secondary_status) { |
| 1227 | ret = -EINVAL; |
| 1228 | goto out; |
| 1229 | } |
| 1230 | |
Linus Walleij | 5e754f3 | 2012-07-03 23:05:14 +0200 | [diff] [blame] | 1231 | base = devm_request_and_ioremap(&dev->dev, res); |
| 1232 | if (!base) { |
| 1233 | ret = -ENOMEM; |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 1234 | goto out; |
| 1235 | } |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1236 | |
Linus Walleij | 5e754f3 | 2012-07-03 23:05:14 +0200 | [diff] [blame] | 1237 | clk = devm_clk_get(&dev->dev, NULL); |
Rabin Vincent | af7dc22 | 2010-05-06 11:14:17 +0100 | [diff] [blame] | 1238 | if (IS_ERR(clk)) { |
| 1239 | ret = PTR_ERR(clk); |
Linus Walleij | 5e754f3 | 2012-07-03 23:05:14 +0200 | [diff] [blame] | 1240 | goto out; |
Rabin Vincent | af7dc22 | 2010-05-06 11:14:17 +0100 | [diff] [blame] | 1241 | } |
Linus Walleij | efec381 | 2012-06-06 22:50:41 +0200 | [diff] [blame] | 1242 | clk_prepare(clk); |
Rabin Vincent | af7dc22 | 2010-05-06 11:14:17 +0100 | [diff] [blame] | 1243 | |
Linus Walleij | 5e754f3 | 2012-07-03 23:05:14 +0200 | [diff] [blame] | 1244 | nmk_chip = devm_kzalloc(&dev->dev, sizeof(*nmk_chip), GFP_KERNEL); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1245 | if (!nmk_chip) { |
| 1246 | ret = -ENOMEM; |
Linus Walleij | 5e754f3 | 2012-07-03 23:05:14 +0200 | [diff] [blame] | 1247 | goto out; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1248 | } |
Lee Jones | 513c27f | 2012-04-13 15:05:05 +0100 | [diff] [blame] | 1249 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1250 | /* |
| 1251 | * The virt address in nmk_chip->addr is in the nomadik register space, |
| 1252 | * so we can simply convert the resource address, without remapping |
| 1253 | */ |
Rabin Vincent | 33b744b | 2010-10-14 10:38:03 +0530 | [diff] [blame] | 1254 | nmk_chip->bank = dev->id; |
Rabin Vincent | af7dc22 | 2010-05-06 11:14:17 +0100 | [diff] [blame] | 1255 | nmk_chip->clk = clk; |
Linus Walleij | 8d91771 | 2012-04-17 10:15:54 +0200 | [diff] [blame] | 1256 | nmk_chip->addr = base; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1257 | nmk_chip->chip = nmk_gpio_template; |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 1258 | nmk_chip->parent_irq = irq; |
Rabin Vincent | 33b744b | 2010-10-14 10:38:03 +0530 | [diff] [blame] | 1259 | nmk_chip->secondary_parent_irq = secondary_irq; |
| 1260 | nmk_chip->get_secondary_status = pdata->get_secondary_status; |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 1261 | nmk_chip->set_ioforce = pdata->set_ioforce; |
Linus Walleij | 33d7864 | 2011-06-09 11:08:47 +0200 | [diff] [blame] | 1262 | nmk_chip->sleepmode = pdata->supports_sleepmode; |
Rabin Vincent | c0fcb8d | 2010-03-03 04:48:54 +0100 | [diff] [blame] | 1263 | spin_lock_init(&nmk_chip->lock); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1264 | |
| 1265 | chip = &nmk_chip->chip; |
| 1266 | chip->base = pdata->first_gpio; |
Rabin Vincent | e493e06 | 2010-03-18 12:35:22 +0530 | [diff] [blame] | 1267 | chip->ngpio = pdata->num_gpio; |
Rabin Vincent | 8d568ae | 2010-12-08 11:07:54 +0530 | [diff] [blame] | 1268 | chip->label = pdata->name ?: dev_name(&dev->dev); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1269 | chip->dev = &dev->dev; |
| 1270 | chip->owner = THIS_MODULE; |
| 1271 | |
Rabin Vincent | ebc6178 | 2011-09-28 15:49:11 +0530 | [diff] [blame] | 1272 | clk_enable(nmk_chip->clk); |
| 1273 | nmk_chip->lowemi = readl_relaxed(nmk_chip->addr + NMK_GPIO_LOWEMI); |
| 1274 | clk_disable(nmk_chip->clk); |
| 1275 | |
Arnd Bergmann | 072e82a | 2012-05-10 13:39:52 +0200 | [diff] [blame] | 1276 | #ifdef CONFIG_OF_GPIO |
Lee Jones | 513c27f | 2012-04-13 15:05:05 +0100 | [diff] [blame] | 1277 | chip->of_node = np; |
Arnd Bergmann | 072e82a | 2012-05-10 13:39:52 +0200 | [diff] [blame] | 1278 | #endif |
Lee Jones | 513c27f | 2012-04-13 15:05:05 +0100 | [diff] [blame] | 1279 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1280 | ret = gpiochip_add(&nmk_chip->chip); |
| 1281 | if (ret) |
Linus Walleij | 5e754f3 | 2012-07-03 23:05:14 +0200 | [diff] [blame] | 1282 | goto out; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1283 | |
Rabin Vincent | 01727e6 | 2010-12-13 12:02:40 +0530 | [diff] [blame] | 1284 | BUG_ON(nmk_chip->bank >= ARRAY_SIZE(nmk_gpio_chips)); |
| 1285 | |
| 1286 | nmk_gpio_chips[nmk_chip->bank] = nmk_chip; |
Lee Jones | 513c27f | 2012-04-13 15:05:05 +0100 | [diff] [blame] | 1287 | |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 1288 | platform_set_drvdata(dev, nmk_chip); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1289 | |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 1290 | nmk_chip->domain = irq_domain_add_legacy(np, NMK_GPIO_PER_CHIP, |
| 1291 | NOMADIK_GPIO_TO_IRQ(pdata->first_gpio), |
| 1292 | 0, &nmk_gpio_irq_simple_ops, nmk_chip); |
| 1293 | if (!nmk_chip->domain) { |
Linus Walleij | 2ee38d4 | 2012-08-10 11:07:51 +0200 | [diff] [blame] | 1294 | dev_err(&dev->dev, "failed to create irqdomain\n"); |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 1295 | ret = -ENOSYS; |
Linus Walleij | 5e754f3 | 2012-07-03 23:05:14 +0200 | [diff] [blame] | 1296 | goto out; |
Lee Jones | a60b57e | 2012-04-19 21:36:31 +0100 | [diff] [blame] | 1297 | } |
| 1298 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1299 | nmk_gpio_init_irq(nmk_chip); |
| 1300 | |
Lee Jones | 513c27f | 2012-04-13 15:05:05 +0100 | [diff] [blame] | 1301 | dev_info(&dev->dev, "at address %p\n", nmk_chip->addr); |
| 1302 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1303 | return 0; |
| 1304 | |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 1305 | out: |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1306 | dev_err(&dev->dev, "Failure %i for GPIO %i-%i\n", ret, |
| 1307 | pdata->first_gpio, pdata->first_gpio+31); |
Lee Jones | 513c27f | 2012-04-13 15:05:05 +0100 | [diff] [blame] | 1308 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1309 | return ret; |
| 1310 | } |
| 1311 | |
Linus Walleij | e98ea77 | 2012-04-26 23:57:25 +0200 | [diff] [blame] | 1312 | static int nmk_get_groups_cnt(struct pinctrl_dev *pctldev) |
| 1313 | { |
| 1314 | struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev); |
| 1315 | |
| 1316 | return npct->soc->ngroups; |
| 1317 | } |
| 1318 | |
| 1319 | static const char *nmk_get_group_name(struct pinctrl_dev *pctldev, |
| 1320 | unsigned selector) |
| 1321 | { |
| 1322 | struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev); |
| 1323 | |
| 1324 | return npct->soc->groups[selector].name; |
| 1325 | } |
| 1326 | |
| 1327 | static int nmk_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector, |
| 1328 | const unsigned **pins, |
| 1329 | unsigned *num_pins) |
| 1330 | { |
| 1331 | struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev); |
| 1332 | |
| 1333 | *pins = npct->soc->groups[selector].pins; |
| 1334 | *num_pins = npct->soc->groups[selector].npins; |
| 1335 | return 0; |
| 1336 | } |
| 1337 | |
Linus Walleij | 24cbdd7 | 2012-05-02 21:28:00 +0200 | [diff] [blame] | 1338 | static struct pinctrl_gpio_range * |
| 1339 | nmk_match_gpio_range(struct pinctrl_dev *pctldev, unsigned offset) |
| 1340 | { |
| 1341 | struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev); |
| 1342 | int i; |
| 1343 | |
| 1344 | for (i = 0; i < npct->soc->gpio_num_ranges; i++) { |
| 1345 | struct pinctrl_gpio_range *range; |
| 1346 | |
| 1347 | range = &npct->soc->gpio_ranges[i]; |
| 1348 | if (offset >= range->pin_base && |
| 1349 | offset <= (range->pin_base + range->npins - 1)) |
| 1350 | return range; |
| 1351 | } |
| 1352 | return NULL; |
| 1353 | } |
| 1354 | |
Linus Walleij | e98ea77 | 2012-04-26 23:57:25 +0200 | [diff] [blame] | 1355 | static void nmk_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s, |
| 1356 | unsigned offset) |
| 1357 | { |
Linus Walleij | 24cbdd7 | 2012-05-02 21:28:00 +0200 | [diff] [blame] | 1358 | struct pinctrl_gpio_range *range; |
| 1359 | struct gpio_chip *chip; |
| 1360 | |
| 1361 | range = nmk_match_gpio_range(pctldev, offset); |
| 1362 | if (!range || !range->gc) { |
| 1363 | seq_printf(s, "invalid pin offset"); |
| 1364 | return; |
| 1365 | } |
| 1366 | chip = range->gc; |
| 1367 | nmk_gpio_dbg_show_one(s, chip, offset - chip->base, offset); |
Linus Walleij | e98ea77 | 2012-04-26 23:57:25 +0200 | [diff] [blame] | 1368 | } |
| 1369 | |
| 1370 | static struct pinctrl_ops nmk_pinctrl_ops = { |
| 1371 | .get_groups_count = nmk_get_groups_cnt, |
| 1372 | .get_group_name = nmk_get_group_name, |
| 1373 | .get_group_pins = nmk_get_group_pins, |
| 1374 | .pin_dbg_show = nmk_pin_dbg_show, |
| 1375 | }; |
| 1376 | |
Linus Walleij | dbfe8ca | 2012-05-02 22:56:47 +0200 | [diff] [blame] | 1377 | static int nmk_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev) |
| 1378 | { |
| 1379 | struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev); |
| 1380 | |
| 1381 | return npct->soc->nfunctions; |
| 1382 | } |
| 1383 | |
| 1384 | static const char *nmk_pmx_get_func_name(struct pinctrl_dev *pctldev, |
| 1385 | unsigned function) |
| 1386 | { |
| 1387 | struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev); |
| 1388 | |
| 1389 | return npct->soc->functions[function].name; |
| 1390 | } |
| 1391 | |
| 1392 | static int nmk_pmx_get_func_groups(struct pinctrl_dev *pctldev, |
| 1393 | unsigned function, |
| 1394 | const char * const **groups, |
| 1395 | unsigned * const num_groups) |
| 1396 | { |
| 1397 | struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev); |
| 1398 | |
| 1399 | *groups = npct->soc->functions[function].groups; |
| 1400 | *num_groups = npct->soc->functions[function].ngroups; |
| 1401 | |
| 1402 | return 0; |
| 1403 | } |
| 1404 | |
| 1405 | static int nmk_pmx_enable(struct pinctrl_dev *pctldev, unsigned function, |
| 1406 | unsigned group) |
| 1407 | { |
| 1408 | struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev); |
| 1409 | const struct nmk_pingroup *g; |
| 1410 | static unsigned int slpm[NUM_BANKS]; |
| 1411 | unsigned long flags; |
| 1412 | bool glitch; |
| 1413 | int ret = -EINVAL; |
| 1414 | int i; |
| 1415 | |
| 1416 | g = &npct->soc->groups[group]; |
| 1417 | |
| 1418 | if (g->altsetting < 0) |
| 1419 | return -EINVAL; |
| 1420 | |
| 1421 | dev_dbg(npct->dev, "enable group %s, %u pins\n", g->name, g->npins); |
| 1422 | |
Linus Walleij | daf7317 | 2012-05-22 11:46:45 +0200 | [diff] [blame] | 1423 | /* |
| 1424 | * If we're setting altfunc C by setting both AFSLA and AFSLB to 1, |
| 1425 | * we may pass through an undesired state. In this case we take |
| 1426 | * some extra care. |
| 1427 | * |
| 1428 | * Safe sequence used to switch IOs between GPIO and Alternate-C mode: |
| 1429 | * - Save SLPM registers (since we have a shadow register in the |
| 1430 | * nmk_chip we're using that as backup) |
| 1431 | * - Set SLPM=0 for the IOs you want to switch and others to 1 |
| 1432 | * - Configure the GPIO registers for the IOs that are being switched |
| 1433 | * - Set IOFORCE=1 |
| 1434 | * - Modify the AFLSA/B registers for the IOs that are being switched |
| 1435 | * - Set IOFORCE=0 |
| 1436 | * - Restore SLPM registers |
| 1437 | * - Any spurious wake up event during switch sequence to be ignored |
| 1438 | * and cleared |
| 1439 | * |
| 1440 | * We REALLY need to save ALL slpm registers, because the external |
| 1441 | * IOFORCE will switch *all* ports to their sleepmode setting to as |
| 1442 | * to avoid glitches. (Not just one port!) |
| 1443 | */ |
Linus Walleij | dbfe8ca | 2012-05-02 22:56:47 +0200 | [diff] [blame] | 1444 | glitch = (g->altsetting == NMK_GPIO_ALT_C); |
| 1445 | |
| 1446 | if (glitch) { |
| 1447 | spin_lock_irqsave(&nmk_gpio_slpm_lock, flags); |
| 1448 | |
| 1449 | /* Initially don't put any pins to sleep when switching */ |
| 1450 | memset(slpm, 0xff, sizeof(slpm)); |
| 1451 | |
| 1452 | /* |
| 1453 | * Then mask the pins that need to be sleeping now when we're |
| 1454 | * switching to the ALT C function. |
| 1455 | */ |
| 1456 | for (i = 0; i < g->npins; i++) |
| 1457 | slpm[g->pins[i] / NMK_GPIO_PER_CHIP] &= ~BIT(g->pins[i]); |
| 1458 | nmk_gpio_glitch_slpm_init(slpm); |
| 1459 | } |
| 1460 | |
| 1461 | for (i = 0; i < g->npins; i++) { |
| 1462 | struct pinctrl_gpio_range *range; |
| 1463 | struct nmk_gpio_chip *nmk_chip; |
| 1464 | struct gpio_chip *chip; |
| 1465 | unsigned bit; |
| 1466 | |
| 1467 | range = nmk_match_gpio_range(pctldev, g->pins[i]); |
| 1468 | if (!range) { |
| 1469 | dev_err(npct->dev, |
| 1470 | "invalid pin offset %d in group %s at index %d\n", |
| 1471 | g->pins[i], g->name, i); |
| 1472 | goto out_glitch; |
| 1473 | } |
| 1474 | if (!range->gc) { |
| 1475 | dev_err(npct->dev, "GPIO chip missing in range for pin offset %d in group %s at index %d\n", |
| 1476 | g->pins[i], g->name, i); |
| 1477 | goto out_glitch; |
| 1478 | } |
| 1479 | chip = range->gc; |
| 1480 | nmk_chip = container_of(chip, struct nmk_gpio_chip, chip); |
| 1481 | dev_dbg(npct->dev, "setting pin %d to altsetting %d\n", g->pins[i], g->altsetting); |
| 1482 | |
| 1483 | clk_enable(nmk_chip->clk); |
| 1484 | bit = g->pins[i] % NMK_GPIO_PER_CHIP; |
| 1485 | /* |
| 1486 | * If the pin is switching to altfunc, and there was an |
| 1487 | * interrupt installed on it which has been lazy disabled, |
| 1488 | * actually mask the interrupt to prevent spurious interrupts |
| 1489 | * that would occur while the pin is under control of the |
| 1490 | * peripheral. Only SKE does this. |
| 1491 | */ |
| 1492 | nmk_gpio_disable_lazy_irq(nmk_chip, bit); |
| 1493 | |
| 1494 | __nmk_gpio_set_mode_safe(nmk_chip, bit, g->altsetting, glitch); |
| 1495 | clk_disable(nmk_chip->clk); |
| 1496 | } |
| 1497 | |
| 1498 | /* When all pins are successfully reconfigured we get here */ |
| 1499 | ret = 0; |
| 1500 | |
| 1501 | out_glitch: |
| 1502 | if (glitch) { |
| 1503 | nmk_gpio_glitch_slpm_restore(slpm); |
| 1504 | spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags); |
| 1505 | } |
| 1506 | |
| 1507 | return ret; |
| 1508 | } |
| 1509 | |
| 1510 | static void nmk_pmx_disable(struct pinctrl_dev *pctldev, |
| 1511 | unsigned function, unsigned group) |
| 1512 | { |
| 1513 | struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev); |
| 1514 | const struct nmk_pingroup *g; |
| 1515 | |
| 1516 | g = &npct->soc->groups[group]; |
| 1517 | |
| 1518 | if (g->altsetting < 0) |
| 1519 | return; |
| 1520 | |
| 1521 | /* Poke out the mux, set the pin to some default state? */ |
| 1522 | dev_dbg(npct->dev, "disable group %s, %u pins\n", g->name, g->npins); |
| 1523 | } |
| 1524 | |
| 1525 | int nmk_gpio_request_enable(struct pinctrl_dev *pctldev, |
| 1526 | struct pinctrl_gpio_range *range, |
| 1527 | unsigned offset) |
| 1528 | { |
| 1529 | struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev); |
| 1530 | struct nmk_gpio_chip *nmk_chip; |
| 1531 | struct gpio_chip *chip; |
| 1532 | unsigned bit; |
| 1533 | |
| 1534 | if (!range) { |
| 1535 | dev_err(npct->dev, "invalid range\n"); |
| 1536 | return -EINVAL; |
| 1537 | } |
| 1538 | if (!range->gc) { |
| 1539 | dev_err(npct->dev, "missing GPIO chip in range\n"); |
| 1540 | return -EINVAL; |
| 1541 | } |
| 1542 | chip = range->gc; |
| 1543 | nmk_chip = container_of(chip, struct nmk_gpio_chip, chip); |
| 1544 | |
| 1545 | dev_dbg(npct->dev, "enable pin %u as GPIO\n", offset); |
| 1546 | |
| 1547 | clk_enable(nmk_chip->clk); |
| 1548 | bit = offset % NMK_GPIO_PER_CHIP; |
| 1549 | /* There is no glitch when converting any pin to GPIO */ |
| 1550 | __nmk_gpio_set_mode(nmk_chip, bit, NMK_GPIO_ALT_GPIO); |
| 1551 | clk_disable(nmk_chip->clk); |
| 1552 | |
| 1553 | return 0; |
| 1554 | } |
| 1555 | |
| 1556 | void nmk_gpio_disable_free(struct pinctrl_dev *pctldev, |
| 1557 | struct pinctrl_gpio_range *range, |
| 1558 | unsigned offset) |
| 1559 | { |
| 1560 | struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev); |
| 1561 | |
| 1562 | dev_dbg(npct->dev, "disable pin %u as GPIO\n", offset); |
| 1563 | /* Set the pin to some default state, GPIO is usually default */ |
| 1564 | } |
| 1565 | |
| 1566 | static struct pinmux_ops nmk_pinmux_ops = { |
| 1567 | .get_functions_count = nmk_pmx_get_funcs_cnt, |
| 1568 | .get_function_name = nmk_pmx_get_func_name, |
| 1569 | .get_function_groups = nmk_pmx_get_func_groups, |
| 1570 | .enable = nmk_pmx_enable, |
| 1571 | .disable = nmk_pmx_disable, |
| 1572 | .gpio_request_enable = nmk_gpio_request_enable, |
| 1573 | .gpio_disable_free = nmk_gpio_disable_free, |
| 1574 | }; |
| 1575 | |
Linus Walleij | d41af62 | 2012-05-03 15:58:12 +0200 | [diff] [blame] | 1576 | int nmk_pin_config_get(struct pinctrl_dev *pctldev, |
| 1577 | unsigned pin, |
| 1578 | unsigned long *config) |
| 1579 | { |
| 1580 | /* Not implemented */ |
| 1581 | return -EINVAL; |
| 1582 | } |
| 1583 | |
| 1584 | int nmk_pin_config_set(struct pinctrl_dev *pctldev, |
| 1585 | unsigned pin, |
| 1586 | unsigned long config) |
| 1587 | { |
| 1588 | static const char *pullnames[] = { |
| 1589 | [NMK_GPIO_PULL_NONE] = "none", |
| 1590 | [NMK_GPIO_PULL_UP] = "up", |
| 1591 | [NMK_GPIO_PULL_DOWN] = "down", |
| 1592 | [3] /* illegal */ = "??" |
| 1593 | }; |
| 1594 | static const char *slpmnames[] = { |
| 1595 | [NMK_GPIO_SLPM_INPUT] = "input/wakeup", |
| 1596 | [NMK_GPIO_SLPM_NOCHANGE] = "no-change/no-wakeup", |
| 1597 | }; |
| 1598 | struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev); |
| 1599 | struct nmk_gpio_chip *nmk_chip; |
| 1600 | struct pinctrl_gpio_range *range; |
| 1601 | struct gpio_chip *chip; |
| 1602 | unsigned bit; |
| 1603 | |
| 1604 | /* |
| 1605 | * The pin config contains pin number and altfunction fields, here |
| 1606 | * we just ignore that part. It's being handled by the framework and |
| 1607 | * pinmux callback respectively. |
| 1608 | */ |
| 1609 | pin_cfg_t cfg = (pin_cfg_t) config; |
| 1610 | int pull = PIN_PULL(cfg); |
| 1611 | int slpm = PIN_SLPM(cfg); |
| 1612 | int output = PIN_DIR(cfg); |
| 1613 | int val = PIN_VAL(cfg); |
| 1614 | bool lowemi = PIN_LOWEMI(cfg); |
| 1615 | bool gpiomode = PIN_GPIOMODE(cfg); |
| 1616 | bool sleep = PIN_SLEEPMODE(cfg); |
| 1617 | |
| 1618 | range = nmk_match_gpio_range(pctldev, pin); |
| 1619 | if (!range) { |
| 1620 | dev_err(npct->dev, "invalid pin offset %d\n", pin); |
| 1621 | return -EINVAL; |
| 1622 | } |
| 1623 | if (!range->gc) { |
| 1624 | dev_err(npct->dev, "GPIO chip missing in range for pin %d\n", |
| 1625 | pin); |
| 1626 | return -EINVAL; |
| 1627 | } |
| 1628 | chip = range->gc; |
| 1629 | nmk_chip = container_of(chip, struct nmk_gpio_chip, chip); |
| 1630 | |
| 1631 | if (sleep) { |
| 1632 | int slpm_pull = PIN_SLPM_PULL(cfg); |
| 1633 | int slpm_output = PIN_SLPM_DIR(cfg); |
| 1634 | int slpm_val = PIN_SLPM_VAL(cfg); |
| 1635 | |
| 1636 | /* All pins go into GPIO mode at sleep */ |
| 1637 | gpiomode = true; |
| 1638 | |
| 1639 | /* |
| 1640 | * The SLPM_* values are normal values + 1 to allow zero to |
| 1641 | * mean "same as normal". |
| 1642 | */ |
| 1643 | if (slpm_pull) |
| 1644 | pull = slpm_pull - 1; |
| 1645 | if (slpm_output) |
| 1646 | output = slpm_output - 1; |
| 1647 | if (slpm_val) |
| 1648 | val = slpm_val - 1; |
| 1649 | |
| 1650 | dev_dbg(nmk_chip->chip.dev, "pin %d: sleep pull %s, dir %s, val %s\n", |
| 1651 | pin, |
| 1652 | slpm_pull ? pullnames[pull] : "same", |
| 1653 | slpm_output ? (output ? "output" : "input") : "same", |
| 1654 | slpm_val ? (val ? "high" : "low") : "same"); |
| 1655 | } |
| 1656 | |
| 1657 | dev_dbg(nmk_chip->chip.dev, "pin %d [%#lx]: pull %s, slpm %s (%s%s), lowemi %s\n", |
| 1658 | pin, cfg, pullnames[pull], slpmnames[slpm], |
| 1659 | output ? "output " : "input", |
| 1660 | output ? (val ? "high" : "low") : "", |
| 1661 | lowemi ? "on" : "off" ); |
| 1662 | |
| 1663 | clk_enable(nmk_chip->clk); |
| 1664 | bit = pin % NMK_GPIO_PER_CHIP; |
| 1665 | if (gpiomode) |
| 1666 | /* No glitch when going to GPIO mode */ |
| 1667 | __nmk_gpio_set_mode(nmk_chip, bit, NMK_GPIO_ALT_GPIO); |
| 1668 | if (output) |
| 1669 | __nmk_gpio_make_output(nmk_chip, bit, val); |
| 1670 | else { |
| 1671 | __nmk_gpio_make_input(nmk_chip, bit); |
| 1672 | __nmk_gpio_set_pull(nmk_chip, bit, pull); |
| 1673 | } |
| 1674 | /* TODO: isn't this only applicable on output pins? */ |
| 1675 | __nmk_gpio_set_lowemi(nmk_chip, bit, lowemi); |
| 1676 | |
| 1677 | __nmk_gpio_set_slpm(nmk_chip, bit, slpm); |
| 1678 | clk_disable(nmk_chip->clk); |
| 1679 | return 0; |
| 1680 | } |
| 1681 | |
| 1682 | static struct pinconf_ops nmk_pinconf_ops = { |
| 1683 | .pin_config_get = nmk_pin_config_get, |
| 1684 | .pin_config_set = nmk_pin_config_set, |
| 1685 | }; |
| 1686 | |
Linus Walleij | e98ea77 | 2012-04-26 23:57:25 +0200 | [diff] [blame] | 1687 | static struct pinctrl_desc nmk_pinctrl_desc = { |
| 1688 | .name = "pinctrl-nomadik", |
| 1689 | .pctlops = &nmk_pinctrl_ops, |
Linus Walleij | dbfe8ca | 2012-05-02 22:56:47 +0200 | [diff] [blame] | 1690 | .pmxops = &nmk_pinmux_ops, |
Linus Walleij | d41af62 | 2012-05-03 15:58:12 +0200 | [diff] [blame] | 1691 | .confops = &nmk_pinconf_ops, |
Linus Walleij | e98ea77 | 2012-04-26 23:57:25 +0200 | [diff] [blame] | 1692 | .owner = THIS_MODULE, |
| 1693 | }; |
| 1694 | |
Lee Jones | 855f80c | 2012-05-26 06:09:29 +0100 | [diff] [blame] | 1695 | static const struct of_device_id nmk_pinctrl_match[] = { |
| 1696 | { |
| 1697 | .compatible = "stericsson,nmk_pinctrl", |
| 1698 | .data = (void *)PINCTRL_NMK_DB8500, |
| 1699 | }, |
| 1700 | {}, |
| 1701 | }; |
| 1702 | |
Linus Walleij | e98ea77 | 2012-04-26 23:57:25 +0200 | [diff] [blame] | 1703 | static int __devinit nmk_pinctrl_probe(struct platform_device *pdev) |
| 1704 | { |
| 1705 | const struct platform_device_id *platid = platform_get_device_id(pdev); |
Lee Jones | 855f80c | 2012-05-26 06:09:29 +0100 | [diff] [blame] | 1706 | struct device_node *np = pdev->dev.of_node; |
Linus Walleij | e98ea77 | 2012-04-26 23:57:25 +0200 | [diff] [blame] | 1707 | struct nmk_pinctrl *npct; |
Lee Jones | 855f80c | 2012-05-26 06:09:29 +0100 | [diff] [blame] | 1708 | unsigned int version = 0; |
Linus Walleij | e98ea77 | 2012-04-26 23:57:25 +0200 | [diff] [blame] | 1709 | int i; |
| 1710 | |
| 1711 | npct = devm_kzalloc(&pdev->dev, sizeof(*npct), GFP_KERNEL); |
| 1712 | if (!npct) |
| 1713 | return -ENOMEM; |
| 1714 | |
Lee Jones | 855f80c | 2012-05-26 06:09:29 +0100 | [diff] [blame] | 1715 | if (platid) |
| 1716 | version = platid->driver_data; |
| 1717 | else if (np) |
| 1718 | version = (unsigned int) |
| 1719 | of_match_device(nmk_pinctrl_match, &pdev->dev)->data; |
| 1720 | |
Linus Walleij | e98ea77 | 2012-04-26 23:57:25 +0200 | [diff] [blame] | 1721 | /* Poke in other ASIC variants here */ |
Linus Walleij | f79c5ed | 2012-08-10 00:43:28 +0200 | [diff] [blame] | 1722 | if (version == PINCTRL_NMK_STN8815) |
| 1723 | nmk_pinctrl_stn8815_init(&npct->soc); |
Lee Jones | 855f80c | 2012-05-26 06:09:29 +0100 | [diff] [blame] | 1724 | if (version == PINCTRL_NMK_DB8500) |
Linus Walleij | e98ea77 | 2012-04-26 23:57:25 +0200 | [diff] [blame] | 1725 | nmk_pinctrl_db8500_init(&npct->soc); |
Patrice Chotard | 45a1b53 | 2012-07-20 15:45:22 +0200 | [diff] [blame] | 1726 | if (version == PINCTRL_NMK_DB8540) |
| 1727 | nmk_pinctrl_db8540_init(&npct->soc); |
Linus Walleij | e98ea77 | 2012-04-26 23:57:25 +0200 | [diff] [blame] | 1728 | |
| 1729 | /* |
| 1730 | * We need all the GPIO drivers to probe FIRST, or we will not be able |
| 1731 | * to obtain references to the struct gpio_chip * for them, and we |
| 1732 | * need this to proceed. |
| 1733 | */ |
| 1734 | for (i = 0; i < npct->soc->gpio_num_ranges; i++) { |
| 1735 | if (!nmk_gpio_chips[i]) { |
| 1736 | dev_warn(&pdev->dev, "GPIO chip %d not registered yet\n", i); |
Linus Walleij | e98ea77 | 2012-04-26 23:57:25 +0200 | [diff] [blame] | 1737 | return -EPROBE_DEFER; |
| 1738 | } |
| 1739 | npct->soc->gpio_ranges[i].gc = &nmk_gpio_chips[i]->chip; |
| 1740 | } |
| 1741 | |
| 1742 | nmk_pinctrl_desc.pins = npct->soc->pins; |
| 1743 | nmk_pinctrl_desc.npins = npct->soc->npins; |
| 1744 | npct->dev = &pdev->dev; |
| 1745 | npct->pctl = pinctrl_register(&nmk_pinctrl_desc, &pdev->dev, npct); |
| 1746 | if (!npct->pctl) { |
| 1747 | dev_err(&pdev->dev, "could not register Nomadik pinctrl driver\n"); |
| 1748 | return -EINVAL; |
| 1749 | } |
| 1750 | |
| 1751 | /* We will handle a range of GPIO pins */ |
| 1752 | for (i = 0; i < npct->soc->gpio_num_ranges; i++) |
| 1753 | pinctrl_add_gpio_range(npct->pctl, &npct->soc->gpio_ranges[i]); |
| 1754 | |
| 1755 | platform_set_drvdata(pdev, npct); |
| 1756 | dev_info(&pdev->dev, "initialized Nomadik pin control driver\n"); |
| 1757 | |
| 1758 | return 0; |
| 1759 | } |
| 1760 | |
Lee Jones | 513c27f | 2012-04-13 15:05:05 +0100 | [diff] [blame] | 1761 | static const struct of_device_id nmk_gpio_match[] = { |
| 1762 | { .compatible = "st,nomadik-gpio", }, |
| 1763 | {} |
| 1764 | }; |
| 1765 | |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 1766 | static struct platform_driver nmk_gpio_driver = { |
| 1767 | .driver = { |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1768 | .owner = THIS_MODULE, |
| 1769 | .name = "gpio", |
Lee Jones | 513c27f | 2012-04-13 15:05:05 +0100 | [diff] [blame] | 1770 | .of_match_table = nmk_gpio_match, |
Rabin Vincent | 5317e4d1 | 2011-02-10 09:29:53 +0530 | [diff] [blame] | 1771 | }, |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1772 | .probe = nmk_gpio_probe, |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1773 | }; |
| 1774 | |
Linus Walleij | e98ea77 | 2012-04-26 23:57:25 +0200 | [diff] [blame] | 1775 | static const struct platform_device_id nmk_pinctrl_id[] = { |
| 1776 | { "pinctrl-stn8815", PINCTRL_NMK_STN8815 }, |
| 1777 | { "pinctrl-db8500", PINCTRL_NMK_DB8500 }, |
Patrice Chotard | 45a1b53 | 2012-07-20 15:45:22 +0200 | [diff] [blame] | 1778 | { "pinctrl-db8540", PINCTRL_NMK_DB8540 }, |
Linus Walleij | e98ea77 | 2012-04-26 23:57:25 +0200 | [diff] [blame] | 1779 | }; |
| 1780 | |
| 1781 | static struct platform_driver nmk_pinctrl_driver = { |
| 1782 | .driver = { |
| 1783 | .owner = THIS_MODULE, |
| 1784 | .name = "pinctrl-nomadik", |
Lee Jones | 855f80c | 2012-05-26 06:09:29 +0100 | [diff] [blame] | 1785 | .of_match_table = nmk_pinctrl_match, |
Linus Walleij | e98ea77 | 2012-04-26 23:57:25 +0200 | [diff] [blame] | 1786 | }, |
| 1787 | .probe = nmk_pinctrl_probe, |
| 1788 | .id_table = nmk_pinctrl_id, |
| 1789 | }; |
| 1790 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1791 | static int __init nmk_gpio_init(void) |
| 1792 | { |
Linus Walleij | e98ea77 | 2012-04-26 23:57:25 +0200 | [diff] [blame] | 1793 | int ret; |
| 1794 | |
| 1795 | ret = platform_driver_register(&nmk_gpio_driver); |
| 1796 | if (ret) |
| 1797 | return ret; |
| 1798 | return platform_driver_register(&nmk_pinctrl_driver); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1799 | } |
| 1800 | |
Rabin Vincent | 33f45ea | 2010-06-02 06:09:52 +0100 | [diff] [blame] | 1801 | core_initcall(nmk_gpio_init); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1802 | |
| 1803 | MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini"); |
| 1804 | MODULE_DESCRIPTION("Nomadik GPIO Driver"); |
| 1805 | MODULE_LICENSE("GPL"); |