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