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