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