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> 7 * Copyright (C) 2011-2013 Linus Walleij <linus.walleij@linaro.org> 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> 17 #include <linux/platform_device.h> 18 #include <linux/io.h> 19 #include <linux/clk.h> 20 #include <linux/err.h> 21 #include <linux/gpio.h> 22 #include <linux/spinlock.h> 23 #include <linux/interrupt.h> 24 #include <linux/slab.h> 25 #include <linux/of_device.h> 26 #include <linux/of_address.h> 27 #include <linux/pinctrl/machine.h> 28 #include <linux/pinctrl/pinctrl.h> 29 #include <linux/pinctrl/pinmux.h> 30 #include <linux/pinctrl/pinconf.h> 31 /* Since we request GPIOs from ourself */ 32 #include <linux/pinctrl/consumer.h> 33 #include "pinctrl-nomadik.h" 34 #include "../core.h" 35 #include "../pinctrl-utils.h" 36 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 40 * is currently used in the Nomadik and ux500. 41 * 42 * Symbols in this file are called "nmk_gpio" for "nomadik gpio" 43 */ 44 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 #define NMK_MAX_BANKS DIV_ROUND_UP(ARCH_NR_GPIOS, NMK_GPIO_PER_CHIP) 207 208 /* Register in the logic block */ 209 #define NMK_GPIO_DAT 0x00 210 #define NMK_GPIO_DATS 0x04 211 #define NMK_GPIO_DATC 0x08 212 #define NMK_GPIO_PDIS 0x0c 213 #define NMK_GPIO_DIR 0x10 214 #define NMK_GPIO_DIRS 0x14 215 #define NMK_GPIO_DIRC 0x18 216 #define NMK_GPIO_SLPC 0x1c 217 #define NMK_GPIO_AFSLA 0x20 218 #define NMK_GPIO_AFSLB 0x24 219 #define NMK_GPIO_LOWEMI 0x28 220 221 #define NMK_GPIO_RIMSC 0x40 222 #define NMK_GPIO_FIMSC 0x44 223 #define NMK_GPIO_IS 0x48 224 #define NMK_GPIO_IC 0x4c 225 #define NMK_GPIO_RWIMSC 0x50 226 #define NMK_GPIO_FWIMSC 0x54 227 #define NMK_GPIO_WKS 0x58 228 /* These appear in DB8540 and later ASICs */ 229 #define NMK_GPIO_EDGELEVEL 0x5C 230 #define NMK_GPIO_LEVEL 0x60 231 232 233 /* Pull up/down values */ 234 enum nmk_gpio_pull { 235 NMK_GPIO_PULL_NONE, 236 NMK_GPIO_PULL_UP, 237 NMK_GPIO_PULL_DOWN, 238 }; 239 240 /* Sleep mode */ 241 enum nmk_gpio_slpm { 242 NMK_GPIO_SLPM_INPUT, 243 NMK_GPIO_SLPM_WAKEUP_ENABLE = NMK_GPIO_SLPM_INPUT, 244 NMK_GPIO_SLPM_NOCHANGE, 245 NMK_GPIO_SLPM_WAKEUP_DISABLE = NMK_GPIO_SLPM_NOCHANGE, 246 }; 247 248 struct nmk_gpio_chip { 249 struct gpio_chip chip; 250 struct irq_chip irqchip; 251 void __iomem *addr; 252 struct clk *clk; 253 unsigned int bank; 254 unsigned int parent_irq; 255 int latent_parent_irq; 256 u32 (*get_latent_status)(unsigned int bank); 257 void (*set_ioforce)(bool enable); 258 spinlock_t lock; 259 bool sleepmode; 260 /* Keep track of configured edges */ 261 u32 edge_rising; 262 u32 edge_falling; 263 u32 real_wake; 264 u32 rwimsc; 265 u32 fwimsc; 266 u32 rimsc; 267 u32 fimsc; 268 u32 pull_up; 269 u32 lowemi; 270 }; 271 272 /** 273 * struct nmk_pinctrl - state container for the Nomadik pin controller 274 * @dev: containing device pointer 275 * @pctl: corresponding pin controller device 276 * @soc: SoC data for this specific chip 277 * @prcm_base: PRCM register range virtual base 278 */ 279 struct nmk_pinctrl { 280 struct device *dev; 281 struct pinctrl_dev *pctl; 282 const struct nmk_pinctrl_soc_data *soc; 283 void __iomem *prcm_base; 284 }; 285 286 static struct nmk_gpio_chip *nmk_gpio_chips[NMK_MAX_BANKS]; 287 288 static DEFINE_SPINLOCK(nmk_gpio_slpm_lock); 289 290 #define NUM_BANKS ARRAY_SIZE(nmk_gpio_chips) 291 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 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 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); 329 if (pull == NMK_GPIO_PULL_NONE) { 330 pdis |= bit; 331 nmk_chip->pull_up &= ~bit; 332 } else { 333 pdis &= ~bit; 334 } 335 336 writel(pdis, nmk_chip->addr + NMK_GPIO_PDIS); 337 338 if (pull == NMK_GPIO_PULL_UP) { 339 nmk_chip->pull_up |= bit; 340 writel(bit, nmk_chip->addr + NMK_GPIO_DATS); 341 } else if (pull == NMK_GPIO_PULL_DOWN) { 342 nmk_chip->pull_up &= ~bit; 343 writel(bit, nmk_chip->addr + NMK_GPIO_DATC); 344 } 345 } 346 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 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 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 387 static void __nmk_gpio_set_mode_safe(struct nmk_gpio_chip *nmk_chip, 388 unsigned offset, int gpio_mode, 389 bool glitch) 390 { 391 u32 rwimsc = nmk_chip->rwimsc; 392 u32 fwimsc = nmk_chip->fwimsc; 393 394 if (glitch && nmk_chip->set_ioforce) { 395 u32 bit = BIT(offset); 396 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 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; 420 int irq = irq_find_mapping(nmk_chip->chip.irqdomain, offset); 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.parent, "%d: clearing interrupt mask\n", gpio); 442 } 443 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 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 463 if (!npct->prcm_base) 464 return; 465 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; 494 if (readl(npct->prcm_base + reg) & BIT(bit)) { 495 nmk_write_masked(npct->prcm_base + reg, BIT(bit), 0); 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; 523 if (readl(npct->prcm_base + reg) & BIT(bit)) { 524 nmk_write_masked(npct->prcm_base + reg, BIT(bit), 0); 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); 536 nmk_write_masked(npct->prcm_base + reg, BIT(bit), BIT(bit)); 537 } 538 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 562 clk_enable(chip->clk); 563 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); 580 581 clk_disable(chip->clk); 582 } 583 } 584 585 static int __maybe_unused nmk_prcm_gpiocr_get_mode(struct pinctrl_dev *pctldev, int gpio) 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 594 if (!npct->prcm_base) 595 return NMK_GPIO_ALT_C; 596 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; 610 if (readl(npct->prcm_base + reg) & BIT(bit)) 611 return NMK_GPIO_ALT_C+i+1; 612 } 613 } 614 return NMK_GPIO_ALT_C; 615 } 616 617 int nmk_gpio_get_mode(int gpio) 618 { 619 struct nmk_gpio_chip *nmk_chip; 620 u32 afunc, bfunc, bit; 621 622 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP]; 623 if (!nmk_chip) 624 return -EINVAL; 625 626 bit = 1 << (gpio % NMK_GPIO_PER_CHIP); 627 628 clk_enable(nmk_chip->clk); 629 630 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & bit; 631 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & bit; 632 633 clk_disable(nmk_chip->clk); 634 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 { 643 return 1 << (gpio % NMK_GPIO_PER_CHIP); 644 } 645 646 static void nmk_gpio_irq_ack(struct irq_data *d) 647 { 648 struct gpio_chip *chip = irq_data_get_irq_chip_data(d); 649 struct nmk_gpio_chip *nmk_chip = gpiochip_get_data(chip); 650 651 clk_enable(nmk_chip->clk); 652 writel(nmk_gpio_get_bitmask(d->hwirq), nmk_chip->addr + NMK_GPIO_IC); 653 clk_disable(nmk_chip->clk); 654 } 655 656 enum nmk_gpio_irq_type { 657 NORMAL, 658 WAKE, 659 }; 660 661 static void __nmk_gpio_irq_modify(struct nmk_gpio_chip *nmk_chip, 662 int gpio, enum nmk_gpio_irq_type which, 663 bool enable) 664 { 665 u32 bitmask = nmk_gpio_get_bitmask(gpio); 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 } 682 683 /* we must individually set/clear the two edges */ 684 if (nmk_chip->edge_rising & bitmask) { 685 if (enable) 686 *rimscval |= bitmask; 687 else 688 *rimscval &= ~bitmask; 689 writel(*rimscval, nmk_chip->addr + rimscreg); 690 } 691 if (nmk_chip->edge_falling & bitmask) { 692 if (enable) 693 *fimscval |= bitmask; 694 else 695 *fimscval &= ~bitmask; 696 writel(*fimscval, nmk_chip->addr + fimscreg); 697 } 698 } 699 700 static void __nmk_gpio_set_wake(struct nmk_gpio_chip *nmk_chip, 701 int gpio, bool on) 702 { 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) { 709 __nmk_gpio_set_slpm(nmk_chip, gpio % NMK_GPIO_PER_CHIP, 710 NMK_GPIO_SLPM_WAKEUP_ENABLE); 711 } 712 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) 717 { 718 struct nmk_gpio_chip *nmk_chip; 719 unsigned long flags; 720 u32 bitmask; 721 722 nmk_chip = irq_data_get_irq_chip_data(d); 723 bitmask = nmk_gpio_get_bitmask(d->hwirq); 724 if (!nmk_chip) 725 return -EINVAL; 726 727 clk_enable(nmk_chip->clk); 728 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags); 729 spin_lock(&nmk_chip->lock); 730 731 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, enable); 732 733 if (!(nmk_chip->real_wake & bitmask)) 734 __nmk_gpio_set_wake(nmk_chip, d->hwirq, enable); 735 736 spin_unlock(&nmk_chip->lock); 737 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags); 738 clk_disable(nmk_chip->clk); 739 740 return 0; 741 } 742 743 static void nmk_gpio_irq_mask(struct irq_data *d) 744 { 745 nmk_gpio_irq_maskunmask(d, false); 746 } 747 748 static void nmk_gpio_irq_unmask(struct irq_data *d) 749 { 750 nmk_gpio_irq_maskunmask(d, true); 751 } 752 753 static int nmk_gpio_irq_set_wake(struct irq_data *d, unsigned int on) 754 { 755 struct nmk_gpio_chip *nmk_chip; 756 unsigned long flags; 757 u32 bitmask; 758 759 nmk_chip = irq_data_get_irq_chip_data(d); 760 if (!nmk_chip) 761 return -EINVAL; 762 bitmask = nmk_gpio_get_bitmask(d->hwirq); 763 764 clk_enable(nmk_chip->clk); 765 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags); 766 spin_lock(&nmk_chip->lock); 767 768 if (irqd_irq_disabled(d)) 769 __nmk_gpio_set_wake(nmk_chip, d->hwirq, on); 770 771 if (on) 772 nmk_chip->real_wake |= bitmask; 773 else 774 nmk_chip->real_wake &= ~bitmask; 775 776 spin_unlock(&nmk_chip->lock); 777 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags); 778 clk_disable(nmk_chip->clk); 779 780 return 0; 781 } 782 783 static int nmk_gpio_irq_set_type(struct irq_data *d, unsigned int type) 784 { 785 bool enabled = !irqd_irq_disabled(d); 786 bool wake = irqd_is_wakeup_set(d); 787 struct nmk_gpio_chip *nmk_chip; 788 unsigned long flags; 789 u32 bitmask; 790 791 nmk_chip = irq_data_get_irq_chip_data(d); 792 bitmask = nmk_gpio_get_bitmask(d->hwirq); 793 if (!nmk_chip) 794 return -EINVAL; 795 if (type & IRQ_TYPE_LEVEL_HIGH) 796 return -EINVAL; 797 if (type & IRQ_TYPE_LEVEL_LOW) 798 return -EINVAL; 799 800 clk_enable(nmk_chip->clk); 801 spin_lock_irqsave(&nmk_chip->lock, flags); 802 803 if (enabled) 804 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, false); 805 806 if (enabled || wake) 807 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, WAKE, false); 808 809 nmk_chip->edge_rising &= ~bitmask; 810 if (type & IRQ_TYPE_EDGE_RISING) 811 nmk_chip->edge_rising |= bitmask; 812 813 nmk_chip->edge_falling &= ~bitmask; 814 if (type & IRQ_TYPE_EDGE_FALLING) 815 nmk_chip->edge_falling |= bitmask; 816 817 if (enabled) 818 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, true); 819 820 if (enabled || wake) 821 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, WAKE, true); 822 823 spin_unlock_irqrestore(&nmk_chip->lock, flags); 824 clk_disable(nmk_chip->clk); 825 826 return 0; 827 } 828 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 846 static void __nmk_gpio_irq_handler(struct irq_desc *desc, u32 status) 847 { 848 struct irq_chip *host_chip = irq_desc_get_chip(desc); 849 struct gpio_chip *chip = irq_desc_get_handler_data(desc); 850 851 chained_irq_enter(host_chip, desc); 852 853 while (status) { 854 int bit = __ffs(status); 855 856 generic_handle_irq(irq_find_mapping(chip->irqdomain, bit)); 857 status &= ~BIT(bit); 858 } 859 860 chained_irq_exit(host_chip, desc); 861 } 862 863 static void nmk_gpio_irq_handler(struct irq_desc *desc) 864 { 865 struct gpio_chip *chip = irq_desc_get_handler_data(desc); 866 struct nmk_gpio_chip *nmk_chip = gpiochip_get_data(chip); 867 u32 status; 868 869 clk_enable(nmk_chip->clk); 870 status = readl(nmk_chip->addr + NMK_GPIO_IS); 871 clk_disable(nmk_chip->clk); 872 873 __nmk_gpio_irq_handler(desc, status); 874 } 875 876 static void nmk_gpio_latent_irq_handler(struct irq_desc *desc) 877 { 878 struct gpio_chip *chip = irq_desc_get_handler_data(desc); 879 struct nmk_gpio_chip *nmk_chip = gpiochip_get_data(chip); 880 u32 status = nmk_chip->get_latent_status(nmk_chip->bank); 881 882 __nmk_gpio_irq_handler(desc, status); 883 } 884 885 /* I/O Functions */ 886 887 static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned offset) 888 { 889 struct nmk_gpio_chip *nmk_chip = gpiochip_get_data(chip); 890 891 clk_enable(nmk_chip->clk); 892 893 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC); 894 895 clk_disable(nmk_chip->clk); 896 897 return 0; 898 } 899 900 static int nmk_gpio_get_input(struct gpio_chip *chip, unsigned offset) 901 { 902 struct nmk_gpio_chip *nmk_chip = gpiochip_get_data(chip); 903 u32 bit = 1 << offset; 904 int value; 905 906 clk_enable(nmk_chip->clk); 907 908 value = (readl(nmk_chip->addr + NMK_GPIO_DAT) & bit) != 0; 909 910 clk_disable(nmk_chip->clk); 911 912 return value; 913 } 914 915 static void nmk_gpio_set_output(struct gpio_chip *chip, unsigned offset, 916 int val) 917 { 918 struct nmk_gpio_chip *nmk_chip = gpiochip_get_data(chip); 919 920 clk_enable(nmk_chip->clk); 921 922 __nmk_gpio_set_output(nmk_chip, offset, val); 923 924 clk_disable(nmk_chip->clk); 925 } 926 927 static int nmk_gpio_make_output(struct gpio_chip *chip, unsigned offset, 928 int val) 929 { 930 struct nmk_gpio_chip *nmk_chip = gpiochip_get_data(chip); 931 932 clk_enable(nmk_chip->clk); 933 934 __nmk_gpio_make_output(nmk_chip, offset, val); 935 936 clk_disable(nmk_chip->clk); 937 938 return 0; 939 } 940 941 #ifdef CONFIG_DEBUG_FS 942 943 #include <linux/seq_file.h> 944 945 static void nmk_gpio_dbg_show_one(struct seq_file *s, 946 struct pinctrl_dev *pctldev, struct gpio_chip *chip, 947 unsigned offset, unsigned gpio) 948 { 949 const char *label = gpiochip_is_requested(chip, offset); 950 struct nmk_gpio_chip *nmk_chip = gpiochip_get_data(chip); 951 int mode; 952 bool is_out; 953 bool data_out; 954 bool pull; 955 u32 bit = 1 << offset; 956 const char *modes[] = { 957 [NMK_GPIO_ALT_GPIO] = "gpio", 958 [NMK_GPIO_ALT_A] = "altA", 959 [NMK_GPIO_ALT_B] = "altB", 960 [NMK_GPIO_ALT_C] = "altC", 961 [NMK_GPIO_ALT_C+1] = "altC1", 962 [NMK_GPIO_ALT_C+2] = "altC2", 963 [NMK_GPIO_ALT_C+3] = "altC3", 964 [NMK_GPIO_ALT_C+4] = "altC4", 965 }; 966 const char *pulls[] = { 967 "none ", 968 "pull down", 969 "pull up ", 970 }; 971 972 clk_enable(nmk_chip->clk); 973 is_out = !!(readl(nmk_chip->addr + NMK_GPIO_DIR) & bit); 974 pull = !(readl(nmk_chip->addr + NMK_GPIO_PDIS) & bit); 975 data_out = !!(readl(nmk_chip->addr + NMK_GPIO_DAT) & bit); 976 mode = nmk_gpio_get_mode(gpio); 977 if ((mode == NMK_GPIO_ALT_C) && pctldev) 978 mode = nmk_prcm_gpiocr_get_mode(pctldev, gpio); 979 980 if (is_out) { 981 seq_printf(s, " gpio-%-3d (%-20.20s) out %s %s", 982 gpio, 983 label ?: "(none)", 984 data_out ? "hi" : "lo", 985 (mode < 0) ? "unknown" : modes[mode]); 986 } else { 987 int irq = gpio_to_irq(gpio); 988 struct irq_desc *desc = irq_to_desc(irq); 989 int pullidx = 0; 990 int val; 991 992 if (pull) 993 pullidx = data_out ? 2 : 1; 994 995 seq_printf(s, " gpio-%-3d (%-20.20s) in %s %s", 996 gpio, 997 label ?: "(none)", 998 pulls[pullidx], 999 (mode < 0) ? "unknown" : modes[mode]); 1000 1001 val = nmk_gpio_get_input(chip, offset); 1002 seq_printf(s, " VAL %d", val); 1003 1004 /* 1005 * This races with request_irq(), set_irq_type(), 1006 * and set_irq_wake() ... but those are "rare". 1007 */ 1008 if (irq > 0 && desc && desc->action) { 1009 char *trigger; 1010 u32 bitmask = nmk_gpio_get_bitmask(gpio); 1011 1012 if (nmk_chip->edge_rising & bitmask) 1013 trigger = "edge-rising"; 1014 else if (nmk_chip->edge_falling & bitmask) 1015 trigger = "edge-falling"; 1016 else 1017 trigger = "edge-undefined"; 1018 1019 seq_printf(s, " irq-%d %s%s", 1020 irq, trigger, 1021 irqd_is_wakeup_set(&desc->irq_data) 1022 ? " wakeup" : ""); 1023 } 1024 } 1025 clk_disable(nmk_chip->clk); 1026 } 1027 1028 static void nmk_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip) 1029 { 1030 unsigned i; 1031 unsigned gpio = chip->base; 1032 1033 for (i = 0; i < chip->ngpio; i++, gpio++) { 1034 nmk_gpio_dbg_show_one(s, NULL, chip, i, gpio); 1035 seq_printf(s, "\n"); 1036 } 1037 } 1038 1039 #else 1040 static inline void nmk_gpio_dbg_show_one(struct seq_file *s, 1041 struct pinctrl_dev *pctldev, 1042 struct gpio_chip *chip, 1043 unsigned offset, unsigned gpio) 1044 { 1045 } 1046 #define nmk_gpio_dbg_show NULL 1047 #endif 1048 1049 void nmk_gpio_clocks_enable(void) 1050 { 1051 int i; 1052 1053 for (i = 0; i < NUM_BANKS; i++) { 1054 struct nmk_gpio_chip *chip = nmk_gpio_chips[i]; 1055 1056 if (!chip) 1057 continue; 1058 1059 clk_enable(chip->clk); 1060 } 1061 } 1062 1063 void nmk_gpio_clocks_disable(void) 1064 { 1065 int i; 1066 1067 for (i = 0; i < NUM_BANKS; i++) { 1068 struct nmk_gpio_chip *chip = nmk_gpio_chips[i]; 1069 1070 if (!chip) 1071 continue; 1072 1073 clk_disable(chip->clk); 1074 } 1075 } 1076 1077 /* 1078 * Called from the suspend/resume path to only keep the real wakeup interrupts 1079 * (those that have had set_irq_wake() called on them) as wakeup interrupts, 1080 * and not the rest of the interrupts which we needed to have as wakeups for 1081 * cpuidle. 1082 * 1083 * PM ops are not used since this needs to be done at the end, after all the 1084 * other drivers are done with their suspend callbacks. 1085 */ 1086 void nmk_gpio_wakeups_suspend(void) 1087 { 1088 int i; 1089 1090 for (i = 0; i < NUM_BANKS; i++) { 1091 struct nmk_gpio_chip *chip = nmk_gpio_chips[i]; 1092 1093 if (!chip) 1094 break; 1095 1096 clk_enable(chip->clk); 1097 1098 writel(chip->rwimsc & chip->real_wake, 1099 chip->addr + NMK_GPIO_RWIMSC); 1100 writel(chip->fwimsc & chip->real_wake, 1101 chip->addr + NMK_GPIO_FWIMSC); 1102 1103 clk_disable(chip->clk); 1104 } 1105 } 1106 1107 void nmk_gpio_wakeups_resume(void) 1108 { 1109 int i; 1110 1111 for (i = 0; i < NUM_BANKS; i++) { 1112 struct nmk_gpio_chip *chip = nmk_gpio_chips[i]; 1113 1114 if (!chip) 1115 break; 1116 1117 clk_enable(chip->clk); 1118 1119 writel(chip->rwimsc, chip->addr + NMK_GPIO_RWIMSC); 1120 writel(chip->fwimsc, chip->addr + NMK_GPIO_FWIMSC); 1121 1122 clk_disable(chip->clk); 1123 } 1124 } 1125 1126 /* 1127 * Read the pull up/pull down status. 1128 * A bit set in 'pull_up' means that pull up 1129 * is selected if pull is enabled in PDIS register. 1130 * Note: only pull up/down set via this driver can 1131 * be detected due to HW limitations. 1132 */ 1133 void nmk_gpio_read_pull(int gpio_bank, u32 *pull_up) 1134 { 1135 if (gpio_bank < NUM_BANKS) { 1136 struct nmk_gpio_chip *chip = nmk_gpio_chips[gpio_bank]; 1137 1138 if (!chip) 1139 return; 1140 1141 *pull_up = chip->pull_up; 1142 } 1143 } 1144 1145 /* 1146 * We will allocate memory for the state container using devm* allocators 1147 * binding to the first device reaching this point, it doesn't matter if 1148 * it is the pin controller or GPIO driver. However we need to use the right 1149 * platform device when looking up resources so pay attention to pdev. 1150 */ 1151 static struct nmk_gpio_chip *nmk_gpio_populate_chip(struct device_node *np, 1152 struct platform_device *pdev) 1153 { 1154 struct nmk_gpio_chip *nmk_chip; 1155 struct platform_device *gpio_pdev; 1156 struct gpio_chip *chip; 1157 struct resource *res; 1158 struct clk *clk; 1159 void __iomem *base; 1160 u32 id; 1161 1162 gpio_pdev = of_find_device_by_node(np); 1163 if (!gpio_pdev) { 1164 pr_err("populate \"%s\": device not found\n", np->name); 1165 return ERR_PTR(-ENODEV); 1166 } 1167 if (of_property_read_u32(np, "gpio-bank", &id)) { 1168 dev_err(&pdev->dev, "populate: gpio-bank property not found\n"); 1169 return ERR_PTR(-EINVAL); 1170 } 1171 1172 /* Already populated? */ 1173 nmk_chip = nmk_gpio_chips[id]; 1174 if (nmk_chip) 1175 return nmk_chip; 1176 1177 nmk_chip = devm_kzalloc(&pdev->dev, sizeof(*nmk_chip), GFP_KERNEL); 1178 if (!nmk_chip) 1179 return ERR_PTR(-ENOMEM); 1180 1181 nmk_chip->bank = id; 1182 chip = &nmk_chip->chip; 1183 chip->base = id * NMK_GPIO_PER_CHIP; 1184 chip->ngpio = NMK_GPIO_PER_CHIP; 1185 chip->label = dev_name(&gpio_pdev->dev); 1186 chip->parent = &gpio_pdev->dev; 1187 1188 res = platform_get_resource(gpio_pdev, IORESOURCE_MEM, 0); 1189 base = devm_ioremap_resource(&pdev->dev, res); 1190 if (IS_ERR(base)) 1191 return base; 1192 nmk_chip->addr = base; 1193 1194 clk = clk_get(&gpio_pdev->dev, NULL); 1195 if (IS_ERR(clk)) 1196 return (void *) clk; 1197 clk_prepare(clk); 1198 nmk_chip->clk = clk; 1199 1200 BUG_ON(nmk_chip->bank >= ARRAY_SIZE(nmk_gpio_chips)); 1201 nmk_gpio_chips[id] = nmk_chip; 1202 return nmk_chip; 1203 } 1204 1205 static int nmk_gpio_probe(struct platform_device *dev) 1206 { 1207 struct device_node *np = dev->dev.of_node; 1208 struct nmk_gpio_chip *nmk_chip; 1209 struct gpio_chip *chip; 1210 struct irq_chip *irqchip; 1211 int latent_irq; 1212 bool supports_sleepmode; 1213 int irq; 1214 int ret; 1215 1216 nmk_chip = nmk_gpio_populate_chip(np, dev); 1217 if (IS_ERR(nmk_chip)) { 1218 dev_err(&dev->dev, "could not populate nmk chip struct\n"); 1219 return PTR_ERR(nmk_chip); 1220 } 1221 1222 if (of_get_property(np, "st,supports-sleepmode", NULL)) 1223 supports_sleepmode = true; 1224 else 1225 supports_sleepmode = false; 1226 1227 /* Correct platform device ID */ 1228 dev->id = nmk_chip->bank; 1229 1230 irq = platform_get_irq(dev, 0); 1231 if (irq < 0) 1232 return irq; 1233 1234 /* It's OK for this IRQ not to be present */ 1235 latent_irq = platform_get_irq(dev, 1); 1236 1237 /* 1238 * The virt address in nmk_chip->addr is in the nomadik register space, 1239 * so we can simply convert the resource address, without remapping 1240 */ 1241 nmk_chip->parent_irq = irq; 1242 nmk_chip->latent_parent_irq = latent_irq; 1243 nmk_chip->sleepmode = supports_sleepmode; 1244 spin_lock_init(&nmk_chip->lock); 1245 1246 chip = &nmk_chip->chip; 1247 chip->request = gpiochip_generic_request; 1248 chip->free = gpiochip_generic_free; 1249 chip->direction_input = nmk_gpio_make_input; 1250 chip->get = nmk_gpio_get_input; 1251 chip->direction_output = nmk_gpio_make_output; 1252 chip->set = nmk_gpio_set_output; 1253 chip->dbg_show = nmk_gpio_dbg_show; 1254 chip->can_sleep = false; 1255 chip->owner = THIS_MODULE; 1256 1257 irqchip = &nmk_chip->irqchip; 1258 irqchip->irq_ack = nmk_gpio_irq_ack; 1259 irqchip->irq_mask = nmk_gpio_irq_mask; 1260 irqchip->irq_unmask = nmk_gpio_irq_unmask; 1261 irqchip->irq_set_type = nmk_gpio_irq_set_type; 1262 irqchip->irq_set_wake = nmk_gpio_irq_set_wake; 1263 irqchip->irq_startup = nmk_gpio_irq_startup; 1264 irqchip->irq_shutdown = nmk_gpio_irq_shutdown; 1265 irqchip->flags = IRQCHIP_MASK_ON_SUSPEND; 1266 irqchip->name = kasprintf(GFP_KERNEL, "nmk%u-%u-%u", 1267 dev->id, 1268 chip->base, 1269 chip->base + chip->ngpio - 1); 1270 1271 clk_enable(nmk_chip->clk); 1272 nmk_chip->lowemi = readl_relaxed(nmk_chip->addr + NMK_GPIO_LOWEMI); 1273 clk_disable(nmk_chip->clk); 1274 chip->of_node = np; 1275 1276 ret = gpiochip_add_data(chip, nmk_chip); 1277 if (ret) 1278 return ret; 1279 1280 platform_set_drvdata(dev, nmk_chip); 1281 1282 /* 1283 * Let the generic code handle this edge IRQ, the the chained 1284 * handler will perform the actual work of handling the parent 1285 * interrupt. 1286 */ 1287 ret = gpiochip_irqchip_add(chip, 1288 irqchip, 1289 0, 1290 handle_edge_irq, 1291 IRQ_TYPE_EDGE_FALLING); 1292 if (ret) { 1293 dev_err(&dev->dev, "could not add irqchip\n"); 1294 gpiochip_remove(&nmk_chip->chip); 1295 return -ENODEV; 1296 } 1297 /* Then register the chain on the parent IRQ */ 1298 gpiochip_set_chained_irqchip(chip, 1299 irqchip, 1300 nmk_chip->parent_irq, 1301 nmk_gpio_irq_handler); 1302 if (nmk_chip->latent_parent_irq > 0) 1303 gpiochip_set_chained_irqchip(chip, 1304 irqchip, 1305 nmk_chip->latent_parent_irq, 1306 nmk_gpio_latent_irq_handler); 1307 1308 dev_info(&dev->dev, "at address %p\n", nmk_chip->addr); 1309 1310 return 0; 1311 } 1312 1313 static int nmk_get_groups_cnt(struct pinctrl_dev *pctldev) 1314 { 1315 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev); 1316 1317 return npct->soc->ngroups; 1318 } 1319 1320 static const char *nmk_get_group_name(struct pinctrl_dev *pctldev, 1321 unsigned selector) 1322 { 1323 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev); 1324 1325 return npct->soc->groups[selector].name; 1326 } 1327 1328 static int nmk_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector, 1329 const unsigned **pins, 1330 unsigned *num_pins) 1331 { 1332 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev); 1333 1334 *pins = npct->soc->groups[selector].pins; 1335 *num_pins = npct->soc->groups[selector].npins; 1336 return 0; 1337 } 1338 1339 static struct nmk_gpio_chip *find_nmk_gpio_from_pin(unsigned pin) 1340 { 1341 int i; 1342 struct nmk_gpio_chip *nmk_gpio; 1343 1344 for(i = 0; i < NMK_MAX_BANKS; i++) { 1345 nmk_gpio = nmk_gpio_chips[i]; 1346 if (!nmk_gpio) 1347 continue; 1348 if (pin >= nmk_gpio->chip.base && 1349 pin < nmk_gpio->chip.base + nmk_gpio->chip.ngpio) 1350 return nmk_gpio; 1351 } 1352 return NULL; 1353 } 1354 1355 static struct gpio_chip *find_gc_from_pin(unsigned pin) 1356 { 1357 struct nmk_gpio_chip *nmk_gpio = find_nmk_gpio_from_pin(pin); 1358 1359 if (nmk_gpio) 1360 return &nmk_gpio->chip; 1361 return NULL; 1362 } 1363 1364 static void nmk_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s, 1365 unsigned offset) 1366 { 1367 struct gpio_chip *chip = find_gc_from_pin(offset); 1368 1369 if (!chip) { 1370 seq_printf(s, "invalid pin offset"); 1371 return; 1372 } 1373 nmk_gpio_dbg_show_one(s, pctldev, chip, offset - chip->base, offset); 1374 } 1375 1376 static int nmk_dt_add_map_mux(struct pinctrl_map **map, unsigned *reserved_maps, 1377 unsigned *num_maps, const char *group, 1378 const char *function) 1379 { 1380 if (*num_maps == *reserved_maps) 1381 return -ENOSPC; 1382 1383 (*map)[*num_maps].type = PIN_MAP_TYPE_MUX_GROUP; 1384 (*map)[*num_maps].data.mux.group = group; 1385 (*map)[*num_maps].data.mux.function = function; 1386 (*num_maps)++; 1387 1388 return 0; 1389 } 1390 1391 static int nmk_dt_add_map_configs(struct pinctrl_map **map, 1392 unsigned *reserved_maps, 1393 unsigned *num_maps, const char *group, 1394 unsigned long *configs, unsigned num_configs) 1395 { 1396 unsigned long *dup_configs; 1397 1398 if (*num_maps == *reserved_maps) 1399 return -ENOSPC; 1400 1401 dup_configs = kmemdup(configs, num_configs * sizeof(*dup_configs), 1402 GFP_KERNEL); 1403 if (!dup_configs) 1404 return -ENOMEM; 1405 1406 (*map)[*num_maps].type = PIN_MAP_TYPE_CONFIGS_PIN; 1407 1408 (*map)[*num_maps].data.configs.group_or_pin = group; 1409 (*map)[*num_maps].data.configs.configs = dup_configs; 1410 (*map)[*num_maps].data.configs.num_configs = num_configs; 1411 (*num_maps)++; 1412 1413 return 0; 1414 } 1415 1416 #define NMK_CONFIG_PIN(x, y) { .property = x, .config = y, } 1417 #define NMK_CONFIG_PIN_ARRAY(x, y) { .property = x, .choice = y, \ 1418 .size = ARRAY_SIZE(y), } 1419 1420 static const unsigned long nmk_pin_input_modes[] = { 1421 PIN_INPUT_NOPULL, 1422 PIN_INPUT_PULLUP, 1423 PIN_INPUT_PULLDOWN, 1424 }; 1425 1426 static const unsigned long nmk_pin_output_modes[] = { 1427 PIN_OUTPUT_LOW, 1428 PIN_OUTPUT_HIGH, 1429 PIN_DIR_OUTPUT, 1430 }; 1431 1432 static const unsigned long nmk_pin_sleep_modes[] = { 1433 PIN_SLEEPMODE_DISABLED, 1434 PIN_SLEEPMODE_ENABLED, 1435 }; 1436 1437 static const unsigned long nmk_pin_sleep_input_modes[] = { 1438 PIN_SLPM_INPUT_NOPULL, 1439 PIN_SLPM_INPUT_PULLUP, 1440 PIN_SLPM_INPUT_PULLDOWN, 1441 PIN_SLPM_DIR_INPUT, 1442 }; 1443 1444 static const unsigned long nmk_pin_sleep_output_modes[] = { 1445 PIN_SLPM_OUTPUT_LOW, 1446 PIN_SLPM_OUTPUT_HIGH, 1447 PIN_SLPM_DIR_OUTPUT, 1448 }; 1449 1450 static const unsigned long nmk_pin_sleep_wakeup_modes[] = { 1451 PIN_SLPM_WAKEUP_DISABLE, 1452 PIN_SLPM_WAKEUP_ENABLE, 1453 }; 1454 1455 static const unsigned long nmk_pin_gpio_modes[] = { 1456 PIN_GPIOMODE_DISABLED, 1457 PIN_GPIOMODE_ENABLED, 1458 }; 1459 1460 static const unsigned long nmk_pin_sleep_pdis_modes[] = { 1461 PIN_SLPM_PDIS_DISABLED, 1462 PIN_SLPM_PDIS_ENABLED, 1463 }; 1464 1465 struct nmk_cfg_param { 1466 const char *property; 1467 unsigned long config; 1468 const unsigned long *choice; 1469 int size; 1470 }; 1471 1472 static const struct nmk_cfg_param nmk_cfg_params[] = { 1473 NMK_CONFIG_PIN_ARRAY("ste,input", nmk_pin_input_modes), 1474 NMK_CONFIG_PIN_ARRAY("ste,output", nmk_pin_output_modes), 1475 NMK_CONFIG_PIN_ARRAY("ste,sleep", nmk_pin_sleep_modes), 1476 NMK_CONFIG_PIN_ARRAY("ste,sleep-input", nmk_pin_sleep_input_modes), 1477 NMK_CONFIG_PIN_ARRAY("ste,sleep-output", nmk_pin_sleep_output_modes), 1478 NMK_CONFIG_PIN_ARRAY("ste,sleep-wakeup", nmk_pin_sleep_wakeup_modes), 1479 NMK_CONFIG_PIN_ARRAY("ste,gpio", nmk_pin_gpio_modes), 1480 NMK_CONFIG_PIN_ARRAY("ste,sleep-pull-disable", nmk_pin_sleep_pdis_modes), 1481 }; 1482 1483 static int nmk_dt_pin_config(int index, int val, unsigned long *config) 1484 { 1485 int ret = 0; 1486 1487 if (nmk_cfg_params[index].choice == NULL) 1488 *config = nmk_cfg_params[index].config; 1489 else { 1490 /* test if out of range */ 1491 if (val < nmk_cfg_params[index].size) { 1492 *config = nmk_cfg_params[index].config | 1493 nmk_cfg_params[index].choice[val]; 1494 } 1495 } 1496 return ret; 1497 } 1498 1499 static const char *nmk_find_pin_name(struct pinctrl_dev *pctldev, const char *pin_name) 1500 { 1501 int i, pin_number; 1502 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev); 1503 1504 if (sscanf((char *)pin_name, "GPIO%d", &pin_number) == 1) 1505 for (i = 0; i < npct->soc->npins; i++) 1506 if (npct->soc->pins[i].number == pin_number) 1507 return npct->soc->pins[i].name; 1508 return NULL; 1509 } 1510 1511 static bool nmk_pinctrl_dt_get_config(struct device_node *np, 1512 unsigned long *configs) 1513 { 1514 bool has_config = 0; 1515 unsigned long cfg = 0; 1516 int i, val, ret; 1517 1518 for (i = 0; i < ARRAY_SIZE(nmk_cfg_params); i++) { 1519 ret = of_property_read_u32(np, 1520 nmk_cfg_params[i].property, &val); 1521 if (ret != -EINVAL) { 1522 if (nmk_dt_pin_config(i, val, &cfg) == 0) { 1523 *configs |= cfg; 1524 has_config = 1; 1525 } 1526 } 1527 } 1528 1529 return has_config; 1530 } 1531 1532 static int nmk_pinctrl_dt_subnode_to_map(struct pinctrl_dev *pctldev, 1533 struct device_node *np, 1534 struct pinctrl_map **map, 1535 unsigned *reserved_maps, 1536 unsigned *num_maps) 1537 { 1538 int ret; 1539 const char *function = NULL; 1540 unsigned long configs = 0; 1541 bool has_config = 0; 1542 struct property *prop; 1543 struct device_node *np_config; 1544 1545 ret = of_property_read_string(np, "function", &function); 1546 if (ret >= 0) { 1547 const char *group; 1548 1549 ret = of_property_count_strings(np, "groups"); 1550 if (ret < 0) 1551 goto exit; 1552 1553 ret = pinctrl_utils_reserve_map(pctldev, map, 1554 reserved_maps, 1555 num_maps, ret); 1556 if (ret < 0) 1557 goto exit; 1558 1559 of_property_for_each_string(np, "groups", prop, group) { 1560 ret = nmk_dt_add_map_mux(map, reserved_maps, num_maps, 1561 group, function); 1562 if (ret < 0) 1563 goto exit; 1564 } 1565 } 1566 1567 has_config = nmk_pinctrl_dt_get_config(np, &configs); 1568 np_config = of_parse_phandle(np, "ste,config", 0); 1569 if (np_config) 1570 has_config |= nmk_pinctrl_dt_get_config(np_config, &configs); 1571 if (has_config) { 1572 const char *gpio_name; 1573 const char *pin; 1574 1575 ret = of_property_count_strings(np, "pins"); 1576 if (ret < 0) 1577 goto exit; 1578 ret = pinctrl_utils_reserve_map(pctldev, map, 1579 reserved_maps, 1580 num_maps, ret); 1581 if (ret < 0) 1582 goto exit; 1583 1584 of_property_for_each_string(np, "pins", prop, pin) { 1585 gpio_name = nmk_find_pin_name(pctldev, pin); 1586 1587 ret = nmk_dt_add_map_configs(map, reserved_maps, 1588 num_maps, 1589 gpio_name, &configs, 1); 1590 if (ret < 0) 1591 goto exit; 1592 } 1593 } 1594 1595 exit: 1596 return ret; 1597 } 1598 1599 static int nmk_pinctrl_dt_node_to_map(struct pinctrl_dev *pctldev, 1600 struct device_node *np_config, 1601 struct pinctrl_map **map, unsigned *num_maps) 1602 { 1603 unsigned reserved_maps; 1604 struct device_node *np; 1605 int ret; 1606 1607 reserved_maps = 0; 1608 *map = NULL; 1609 *num_maps = 0; 1610 1611 for_each_child_of_node(np_config, np) { 1612 ret = nmk_pinctrl_dt_subnode_to_map(pctldev, np, map, 1613 &reserved_maps, num_maps); 1614 if (ret < 0) { 1615 pinctrl_utils_dt_free_map(pctldev, *map, *num_maps); 1616 return ret; 1617 } 1618 } 1619 1620 return 0; 1621 } 1622 1623 static const struct pinctrl_ops nmk_pinctrl_ops = { 1624 .get_groups_count = nmk_get_groups_cnt, 1625 .get_group_name = nmk_get_group_name, 1626 .get_group_pins = nmk_get_group_pins, 1627 .pin_dbg_show = nmk_pin_dbg_show, 1628 .dt_node_to_map = nmk_pinctrl_dt_node_to_map, 1629 .dt_free_map = pinctrl_utils_dt_free_map, 1630 }; 1631 1632 static int nmk_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev) 1633 { 1634 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev); 1635 1636 return npct->soc->nfunctions; 1637 } 1638 1639 static const char *nmk_pmx_get_func_name(struct pinctrl_dev *pctldev, 1640 unsigned function) 1641 { 1642 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev); 1643 1644 return npct->soc->functions[function].name; 1645 } 1646 1647 static int nmk_pmx_get_func_groups(struct pinctrl_dev *pctldev, 1648 unsigned function, 1649 const char * const **groups, 1650 unsigned * const num_groups) 1651 { 1652 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev); 1653 1654 *groups = npct->soc->functions[function].groups; 1655 *num_groups = npct->soc->functions[function].ngroups; 1656 1657 return 0; 1658 } 1659 1660 static int nmk_pmx_set(struct pinctrl_dev *pctldev, unsigned function, 1661 unsigned group) 1662 { 1663 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev); 1664 const struct nmk_pingroup *g; 1665 static unsigned int slpm[NUM_BANKS]; 1666 unsigned long flags = 0; 1667 bool glitch; 1668 int ret = -EINVAL; 1669 int i; 1670 1671 g = &npct->soc->groups[group]; 1672 1673 if (g->altsetting < 0) 1674 return -EINVAL; 1675 1676 dev_dbg(npct->dev, "enable group %s, %u pins\n", g->name, g->npins); 1677 1678 /* 1679 * If we're setting altfunc C by setting both AFSLA and AFSLB to 1, 1680 * we may pass through an undesired state. In this case we take 1681 * some extra care. 1682 * 1683 * Safe sequence used to switch IOs between GPIO and Alternate-C mode: 1684 * - Save SLPM registers (since we have a shadow register in the 1685 * nmk_chip we're using that as backup) 1686 * - Set SLPM=0 for the IOs you want to switch and others to 1 1687 * - Configure the GPIO registers for the IOs that are being switched 1688 * - Set IOFORCE=1 1689 * - Modify the AFLSA/B registers for the IOs that are being switched 1690 * - Set IOFORCE=0 1691 * - Restore SLPM registers 1692 * - Any spurious wake up event during switch sequence to be ignored 1693 * and cleared 1694 * 1695 * We REALLY need to save ALL slpm registers, because the external 1696 * IOFORCE will switch *all* ports to their sleepmode setting to as 1697 * to avoid glitches. (Not just one port!) 1698 */ 1699 glitch = ((g->altsetting & NMK_GPIO_ALT_C) == NMK_GPIO_ALT_C); 1700 1701 if (glitch) { 1702 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags); 1703 1704 /* Initially don't put any pins to sleep when switching */ 1705 memset(slpm, 0xff, sizeof(slpm)); 1706 1707 /* 1708 * Then mask the pins that need to be sleeping now when we're 1709 * switching to the ALT C function. 1710 */ 1711 for (i = 0; i < g->npins; i++) 1712 slpm[g->pins[i] / NMK_GPIO_PER_CHIP] &= ~BIT(g->pins[i]); 1713 nmk_gpio_glitch_slpm_init(slpm); 1714 } 1715 1716 for (i = 0; i < g->npins; i++) { 1717 struct nmk_gpio_chip *nmk_chip; 1718 unsigned bit; 1719 1720 nmk_chip = find_nmk_gpio_from_pin(g->pins[i]); 1721 if (!nmk_chip) { 1722 dev_err(npct->dev, 1723 "invalid pin offset %d in group %s at index %d\n", 1724 g->pins[i], g->name, i); 1725 goto out_glitch; 1726 } 1727 dev_dbg(npct->dev, "setting pin %d to altsetting %d\n", g->pins[i], g->altsetting); 1728 1729 clk_enable(nmk_chip->clk); 1730 bit = g->pins[i] % NMK_GPIO_PER_CHIP; 1731 /* 1732 * If the pin is switching to altfunc, and there was an 1733 * interrupt installed on it which has been lazy disabled, 1734 * actually mask the interrupt to prevent spurious interrupts 1735 * that would occur while the pin is under control of the 1736 * peripheral. Only SKE does this. 1737 */ 1738 nmk_gpio_disable_lazy_irq(nmk_chip, bit); 1739 1740 __nmk_gpio_set_mode_safe(nmk_chip, bit, 1741 (g->altsetting & NMK_GPIO_ALT_C), glitch); 1742 clk_disable(nmk_chip->clk); 1743 1744 /* 1745 * Call PRCM GPIOCR config function in case ALTC 1746 * has been selected: 1747 * - If selection is a ALTCx, some bits in PRCM GPIOCR registers 1748 * must be set. 1749 * - If selection is pure ALTC and previous selection was ALTCx, 1750 * then some bits in PRCM GPIOCR registers must be cleared. 1751 */ 1752 if ((g->altsetting & NMK_GPIO_ALT_C) == NMK_GPIO_ALT_C) 1753 nmk_prcm_altcx_set_mode(npct, g->pins[i], 1754 g->altsetting >> NMK_GPIO_ALT_CX_SHIFT); 1755 } 1756 1757 /* When all pins are successfully reconfigured we get here */ 1758 ret = 0; 1759 1760 out_glitch: 1761 if (glitch) { 1762 nmk_gpio_glitch_slpm_restore(slpm); 1763 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags); 1764 } 1765 1766 return ret; 1767 } 1768 1769 static int nmk_gpio_request_enable(struct pinctrl_dev *pctldev, 1770 struct pinctrl_gpio_range *range, 1771 unsigned offset) 1772 { 1773 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev); 1774 struct nmk_gpio_chip *nmk_chip; 1775 struct gpio_chip *chip; 1776 unsigned bit; 1777 1778 if (!range) { 1779 dev_err(npct->dev, "invalid range\n"); 1780 return -EINVAL; 1781 } 1782 if (!range->gc) { 1783 dev_err(npct->dev, "missing GPIO chip in range\n"); 1784 return -EINVAL; 1785 } 1786 chip = range->gc; 1787 nmk_chip = gpiochip_get_data(chip); 1788 1789 dev_dbg(npct->dev, "enable pin %u as GPIO\n", offset); 1790 1791 clk_enable(nmk_chip->clk); 1792 bit = offset % NMK_GPIO_PER_CHIP; 1793 /* There is no glitch when converting any pin to GPIO */ 1794 __nmk_gpio_set_mode(nmk_chip, bit, NMK_GPIO_ALT_GPIO); 1795 clk_disable(nmk_chip->clk); 1796 1797 return 0; 1798 } 1799 1800 static void nmk_gpio_disable_free(struct pinctrl_dev *pctldev, 1801 struct pinctrl_gpio_range *range, 1802 unsigned offset) 1803 { 1804 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev); 1805 1806 dev_dbg(npct->dev, "disable pin %u as GPIO\n", offset); 1807 /* Set the pin to some default state, GPIO is usually default */ 1808 } 1809 1810 static const struct pinmux_ops nmk_pinmux_ops = { 1811 .get_functions_count = nmk_pmx_get_funcs_cnt, 1812 .get_function_name = nmk_pmx_get_func_name, 1813 .get_function_groups = nmk_pmx_get_func_groups, 1814 .set_mux = nmk_pmx_set, 1815 .gpio_request_enable = nmk_gpio_request_enable, 1816 .gpio_disable_free = nmk_gpio_disable_free, 1817 .strict = true, 1818 }; 1819 1820 static int nmk_pin_config_get(struct pinctrl_dev *pctldev, unsigned pin, 1821 unsigned long *config) 1822 { 1823 /* Not implemented */ 1824 return -EINVAL; 1825 } 1826 1827 static int nmk_pin_config_set(struct pinctrl_dev *pctldev, unsigned pin, 1828 unsigned long *configs, unsigned num_configs) 1829 { 1830 static const char *pullnames[] = { 1831 [NMK_GPIO_PULL_NONE] = "none", 1832 [NMK_GPIO_PULL_UP] = "up", 1833 [NMK_GPIO_PULL_DOWN] = "down", 1834 [3] /* illegal */ = "??" 1835 }; 1836 static const char *slpmnames[] = { 1837 [NMK_GPIO_SLPM_INPUT] = "input/wakeup", 1838 [NMK_GPIO_SLPM_NOCHANGE] = "no-change/no-wakeup", 1839 }; 1840 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev); 1841 struct nmk_gpio_chip *nmk_chip; 1842 unsigned bit; 1843 pin_cfg_t cfg; 1844 int pull, slpm, output, val, i; 1845 bool lowemi, gpiomode, sleep; 1846 1847 nmk_chip = find_nmk_gpio_from_pin(pin); 1848 if (!nmk_chip) { 1849 dev_err(npct->dev, 1850 "invalid pin offset %d\n", pin); 1851 return -EINVAL; 1852 } 1853 1854 for (i = 0; i < num_configs; i++) { 1855 /* 1856 * The pin config contains pin number and altfunction fields, 1857 * here we just ignore that part. It's being handled by the 1858 * framework and pinmux callback respectively. 1859 */ 1860 cfg = (pin_cfg_t) configs[i]; 1861 pull = PIN_PULL(cfg); 1862 slpm = PIN_SLPM(cfg); 1863 output = PIN_DIR(cfg); 1864 val = PIN_VAL(cfg); 1865 lowemi = PIN_LOWEMI(cfg); 1866 gpiomode = PIN_GPIOMODE(cfg); 1867 sleep = PIN_SLEEPMODE(cfg); 1868 1869 if (sleep) { 1870 int slpm_pull = PIN_SLPM_PULL(cfg); 1871 int slpm_output = PIN_SLPM_DIR(cfg); 1872 int slpm_val = PIN_SLPM_VAL(cfg); 1873 1874 /* All pins go into GPIO mode at sleep */ 1875 gpiomode = true; 1876 1877 /* 1878 * The SLPM_* values are normal values + 1 to allow zero 1879 * to mean "same as normal". 1880 */ 1881 if (slpm_pull) 1882 pull = slpm_pull - 1; 1883 if (slpm_output) 1884 output = slpm_output - 1; 1885 if (slpm_val) 1886 val = slpm_val - 1; 1887 1888 dev_dbg(nmk_chip->chip.parent, 1889 "pin %d: sleep pull %s, dir %s, val %s\n", 1890 pin, 1891 slpm_pull ? pullnames[pull] : "same", 1892 slpm_output ? (output ? "output" : "input") 1893 : "same", 1894 slpm_val ? (val ? "high" : "low") : "same"); 1895 } 1896 1897 dev_dbg(nmk_chip->chip.parent, 1898 "pin %d [%#lx]: pull %s, slpm %s (%s%s), lowemi %s\n", 1899 pin, cfg, pullnames[pull], slpmnames[slpm], 1900 output ? "output " : "input", 1901 output ? (val ? "high" : "low") : "", 1902 lowemi ? "on" : "off"); 1903 1904 clk_enable(nmk_chip->clk); 1905 bit = pin % NMK_GPIO_PER_CHIP; 1906 if (gpiomode) 1907 /* No glitch when going to GPIO mode */ 1908 __nmk_gpio_set_mode(nmk_chip, bit, NMK_GPIO_ALT_GPIO); 1909 if (output) 1910 __nmk_gpio_make_output(nmk_chip, bit, val); 1911 else { 1912 __nmk_gpio_make_input(nmk_chip, bit); 1913 __nmk_gpio_set_pull(nmk_chip, bit, pull); 1914 } 1915 /* TODO: isn't this only applicable on output pins? */ 1916 __nmk_gpio_set_lowemi(nmk_chip, bit, lowemi); 1917 1918 __nmk_gpio_set_slpm(nmk_chip, bit, slpm); 1919 clk_disable(nmk_chip->clk); 1920 } /* for each config */ 1921 1922 return 0; 1923 } 1924 1925 static const struct pinconf_ops nmk_pinconf_ops = { 1926 .pin_config_get = nmk_pin_config_get, 1927 .pin_config_set = nmk_pin_config_set, 1928 }; 1929 1930 static struct pinctrl_desc nmk_pinctrl_desc = { 1931 .name = "pinctrl-nomadik", 1932 .pctlops = &nmk_pinctrl_ops, 1933 .pmxops = &nmk_pinmux_ops, 1934 .confops = &nmk_pinconf_ops, 1935 .owner = THIS_MODULE, 1936 }; 1937 1938 static const struct of_device_id nmk_pinctrl_match[] = { 1939 { 1940 .compatible = "stericsson,stn8815-pinctrl", 1941 .data = (void *)PINCTRL_NMK_STN8815, 1942 }, 1943 { 1944 .compatible = "stericsson,db8500-pinctrl", 1945 .data = (void *)PINCTRL_NMK_DB8500, 1946 }, 1947 { 1948 .compatible = "stericsson,db8540-pinctrl", 1949 .data = (void *)PINCTRL_NMK_DB8540, 1950 }, 1951 {}, 1952 }; 1953 1954 #ifdef CONFIG_PM_SLEEP 1955 static int nmk_pinctrl_suspend(struct device *dev) 1956 { 1957 struct nmk_pinctrl *npct; 1958 1959 npct = dev_get_drvdata(dev); 1960 if (!npct) 1961 return -EINVAL; 1962 1963 return pinctrl_force_sleep(npct->pctl); 1964 } 1965 1966 static int nmk_pinctrl_resume(struct device *dev) 1967 { 1968 struct nmk_pinctrl *npct; 1969 1970 npct = dev_get_drvdata(dev); 1971 if (!npct) 1972 return -EINVAL; 1973 1974 return pinctrl_force_default(npct->pctl); 1975 } 1976 #endif 1977 1978 static int nmk_pinctrl_probe(struct platform_device *pdev) 1979 { 1980 const struct of_device_id *match; 1981 struct device_node *np = pdev->dev.of_node; 1982 struct device_node *prcm_np; 1983 struct nmk_pinctrl *npct; 1984 unsigned int version = 0; 1985 int i; 1986 1987 npct = devm_kzalloc(&pdev->dev, sizeof(*npct), GFP_KERNEL); 1988 if (!npct) 1989 return -ENOMEM; 1990 1991 match = of_match_device(nmk_pinctrl_match, &pdev->dev); 1992 if (!match) 1993 return -ENODEV; 1994 version = (unsigned int) match->data; 1995 1996 /* Poke in other ASIC variants here */ 1997 if (version == PINCTRL_NMK_STN8815) 1998 nmk_pinctrl_stn8815_init(&npct->soc); 1999 if (version == PINCTRL_NMK_DB8500) 2000 nmk_pinctrl_db8500_init(&npct->soc); 2001 if (version == PINCTRL_NMK_DB8540) 2002 nmk_pinctrl_db8540_init(&npct->soc); 2003 2004 /* 2005 * Since we depend on the GPIO chips to provide clock and register base 2006 * for the pin control operations, make sure that we have these 2007 * populated before we continue. Follow the phandles to instantiate 2008 * them. The GPIO portion of the actual hardware may be probed before 2009 * or after this point: it shouldn't matter as the APIs are orthogonal. 2010 */ 2011 for (i = 0; i < NMK_MAX_BANKS; i++) { 2012 struct device_node *gpio_np; 2013 struct nmk_gpio_chip *nmk_chip; 2014 2015 gpio_np = of_parse_phandle(np, "nomadik-gpio-chips", i); 2016 if (gpio_np) { 2017 dev_info(&pdev->dev, 2018 "populate NMK GPIO %d \"%s\"\n", 2019 i, gpio_np->name); 2020 nmk_chip = nmk_gpio_populate_chip(gpio_np, pdev); 2021 if (IS_ERR(nmk_chip)) 2022 dev_err(&pdev->dev, 2023 "could not populate nmk chip struct " 2024 "- continue anyway\n"); 2025 of_node_put(gpio_np); 2026 } 2027 } 2028 2029 prcm_np = of_parse_phandle(np, "prcm", 0); 2030 if (prcm_np) 2031 npct->prcm_base = of_iomap(prcm_np, 0); 2032 if (!npct->prcm_base) { 2033 if (version == PINCTRL_NMK_STN8815) { 2034 dev_info(&pdev->dev, 2035 "No PRCM base, " 2036 "assuming no ALT-Cx control is available\n"); 2037 } else { 2038 dev_err(&pdev->dev, "missing PRCM base address\n"); 2039 return -EINVAL; 2040 } 2041 } 2042 2043 nmk_pinctrl_desc.pins = npct->soc->pins; 2044 nmk_pinctrl_desc.npins = npct->soc->npins; 2045 npct->dev = &pdev->dev; 2046 2047 npct->pctl = pinctrl_register(&nmk_pinctrl_desc, &pdev->dev, npct); 2048 if (IS_ERR(npct->pctl)) { 2049 dev_err(&pdev->dev, "could not register Nomadik pinctrl driver\n"); 2050 return PTR_ERR(npct->pctl); 2051 } 2052 2053 platform_set_drvdata(pdev, npct); 2054 dev_info(&pdev->dev, "initialized Nomadik pin control driver\n"); 2055 2056 return 0; 2057 } 2058 2059 static const struct of_device_id nmk_gpio_match[] = { 2060 { .compatible = "st,nomadik-gpio", }, 2061 {} 2062 }; 2063 2064 static struct platform_driver nmk_gpio_driver = { 2065 .driver = { 2066 .name = "gpio", 2067 .of_match_table = nmk_gpio_match, 2068 }, 2069 .probe = nmk_gpio_probe, 2070 }; 2071 2072 static SIMPLE_DEV_PM_OPS(nmk_pinctrl_pm_ops, 2073 nmk_pinctrl_suspend, 2074 nmk_pinctrl_resume); 2075 2076 static struct platform_driver nmk_pinctrl_driver = { 2077 .driver = { 2078 .name = "pinctrl-nomadik", 2079 .of_match_table = nmk_pinctrl_match, 2080 .pm = &nmk_pinctrl_pm_ops, 2081 }, 2082 .probe = nmk_pinctrl_probe, 2083 }; 2084 2085 static int __init nmk_gpio_init(void) 2086 { 2087 return platform_driver_register(&nmk_gpio_driver); 2088 } 2089 subsys_initcall(nmk_gpio_init); 2090 2091 static int __init nmk_pinctrl_init(void) 2092 { 2093 return platform_driver_register(&nmk_pinctrl_driver); 2094 } 2095 core_initcall(nmk_pinctrl_init); 2096 2097 MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini"); 2098 MODULE_DESCRIPTION("Nomadik GPIO Driver"); 2099 MODULE_LICENSE("GPL"); 2100