1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2013, Sony Mobile Communications AB. 4 * Copyright (c) 2013, The Linux Foundation. All rights reserved. 5 */ 6 7 #include <linux/delay.h> 8 #include <linux/err.h> 9 #include <linux/gpio/driver.h> 10 #include <linux/interrupt.h> 11 #include <linux/io.h> 12 #include <linux/log2.h> 13 #include <linux/module.h> 14 #include <linux/of.h> 15 #include <linux/platform_device.h> 16 #include <linux/pm.h> 17 #include <linux/firmware/qcom/qcom_scm.h> 18 #include <linux/reboot.h> 19 #include <linux/seq_file.h> 20 #include <linux/slab.h> 21 #include <linux/spinlock.h> 22 #include <linux/string_choices.h> 23 24 #include <linux/pinctrl/machine.h> 25 #include <linux/pinctrl/pinconf-generic.h> 26 #include <linux/pinctrl/pinconf.h> 27 #include <linux/pinctrl/pinmux.h> 28 29 #include <linux/soc/qcom/irq.h> 30 31 #include "../core.h" 32 #include "../pinconf.h" 33 #include "../pinctrl-utils.h" 34 #include "../pinmux.h" 35 36 #include "pinctrl-msm.h" 37 38 #define MAX_NR_GPIO 300 39 #define MAX_NR_TILES 4 40 #define PS_HOLD_OFFSET 0x820 41 42 /** 43 * struct msm_pinctrl - state for a pinctrl-msm device 44 * @dev: device handle. 45 * @pctrl: pinctrl handle. 46 * @chip: gpiochip handle. 47 * @desc: pin controller descriptor 48 * @irq: parent irq for the TLMM irq_chip. 49 * @intr_target_use_scm: route irq to application cpu using scm calls 50 * @lock: Spinlock to protect register resources as well 51 * as msm_pinctrl data structures. 52 * @enabled_irqs: Bitmap of currently enabled irqs. 53 * @dual_edge_irqs: Bitmap of irqs that need sw emulated dual edge 54 * detection. 55 * @skip_wake_irqs: Skip IRQs that are handled by wakeup interrupt controller 56 * @disabled_for_mux: These IRQs were disabled because we muxed away. 57 * @ever_gpio: This bit is set the first time we mux a pin to gpio_func. 58 * @soc: Reference to soc_data of platform specific data. 59 * @regs: Base addresses for the TLMM tiles. 60 * @phys_base: Physical base address 61 */ 62 struct msm_pinctrl { 63 struct device *dev; 64 struct pinctrl_dev *pctrl; 65 struct gpio_chip chip; 66 struct pinctrl_desc desc; 67 68 int irq; 69 70 bool intr_target_use_scm; 71 72 raw_spinlock_t lock; 73 74 DECLARE_BITMAP(dual_edge_irqs, MAX_NR_GPIO); 75 DECLARE_BITMAP(enabled_irqs, MAX_NR_GPIO); 76 DECLARE_BITMAP(skip_wake_irqs, MAX_NR_GPIO); 77 DECLARE_BITMAP(disabled_for_mux, MAX_NR_GPIO); 78 DECLARE_BITMAP(ever_gpio, MAX_NR_GPIO); 79 80 const struct msm_pinctrl_soc_data *soc; 81 void __iomem *regs[MAX_NR_TILES]; 82 u32 phys_base[MAX_NR_TILES]; 83 }; 84 85 #define MSM_ACCESSOR(name) \ 86 static u32 msm_readl_##name(struct msm_pinctrl *pctrl, \ 87 const struct msm_pingroup *g) \ 88 { \ 89 return readl(pctrl->regs[g->tile] + g->name##_reg); \ 90 } \ 91 static void msm_writel_##name(u32 val, struct msm_pinctrl *pctrl, \ 92 const struct msm_pingroup *g) \ 93 { \ 94 writel(val, pctrl->regs[g->tile] + g->name##_reg); \ 95 } 96 97 MSM_ACCESSOR(ctl) 98 MSM_ACCESSOR(io) 99 MSM_ACCESSOR(intr_cfg) 100 MSM_ACCESSOR(intr_status) 101 102 static u32 msm_readl_intr_target(struct msm_pinctrl *pctrl, 103 const struct msm_pingroup *g) 104 { 105 u32 reg = g->intr_target_reg ? g->intr_target_reg : g->intr_cfg_reg; 106 107 return readl(pctrl->regs[g->tile] + reg); 108 } 109 110 static void msm_writel_intr_target(u32 val, struct msm_pinctrl *pctrl, 111 const struct msm_pingroup *g) 112 { 113 u32 reg = g->intr_target_reg ? g->intr_target_reg : g->intr_cfg_reg; 114 115 writel(val, pctrl->regs[g->tile] + reg); 116 } 117 118 static void msm_ack_intr_status(struct msm_pinctrl *pctrl, 119 const struct msm_pingroup *g) 120 { 121 u32 val = g->intr_ack_high ? BIT(g->intr_status_bit) : 0; 122 123 msm_writel_intr_status(val, pctrl, g); 124 } 125 126 static int msm_get_groups_count(struct pinctrl_dev *pctldev) 127 { 128 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); 129 130 return pctrl->soc->ngroups; 131 } 132 133 static const char *msm_get_group_name(struct pinctrl_dev *pctldev, 134 unsigned group) 135 { 136 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); 137 138 return pctrl->soc->groups[group].grp.name; 139 } 140 141 static int msm_get_group_pins(struct pinctrl_dev *pctldev, 142 unsigned group, 143 const unsigned **pins, 144 unsigned *num_pins) 145 { 146 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); 147 148 *pins = pctrl->soc->groups[group].grp.pins; 149 *num_pins = pctrl->soc->groups[group].grp.npins; 150 return 0; 151 } 152 153 static const struct pinctrl_ops msm_pinctrl_ops = { 154 .get_groups_count = msm_get_groups_count, 155 .get_group_name = msm_get_group_name, 156 .get_group_pins = msm_get_group_pins, 157 .dt_node_to_map = pinconf_generic_dt_node_to_map_group, 158 .dt_free_map = pinctrl_utils_free_map, 159 }; 160 161 static int msm_pinmux_request(struct pinctrl_dev *pctldev, unsigned offset) 162 { 163 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); 164 struct gpio_chip *chip = &pctrl->chip; 165 166 return gpiochip_line_is_valid(chip, offset) ? 0 : -EINVAL; 167 } 168 169 static int msm_pinmux_set_mux(struct pinctrl_dev *pctldev, 170 unsigned function, 171 unsigned group) 172 { 173 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); 174 struct gpio_chip *gc = &pctrl->chip; 175 unsigned int irq = irq_find_mapping(gc->irq.domain, group); 176 struct irq_data *d = irq_get_irq_data(irq); 177 unsigned int gpio_func = pctrl->soc->gpio_func; 178 unsigned int egpio_func = pctrl->soc->egpio_func; 179 const struct msm_pingroup *g; 180 unsigned long flags; 181 u32 val, mask; 182 int i; 183 184 g = &pctrl->soc->groups[group]; 185 mask = GENMASK(g->mux_bit + order_base_2(g->nfuncs) - 1, g->mux_bit); 186 187 for (i = 0; i < g->nfuncs; i++) { 188 if (g->funcs[i] == function) 189 break; 190 } 191 192 if (WARN_ON(i == g->nfuncs)) 193 return -EINVAL; 194 195 /* 196 * If an GPIO interrupt is setup on this pin then we need special 197 * handling. Specifically interrupt detection logic will still see 198 * the pin twiddle even when we're muxed away. 199 * 200 * When we see a pin with an interrupt setup on it then we'll disable 201 * (mask) interrupts on it when we mux away until we mux back. Note 202 * that disable_irq() refcounts and interrupts are disabled as long as 203 * at least one disable_irq() has been called. 204 */ 205 if (d && i != gpio_func && 206 !test_and_set_bit(d->hwirq, pctrl->disabled_for_mux)) 207 disable_irq_nosync(irq); 208 209 raw_spin_lock_irqsave(&pctrl->lock, flags); 210 211 val = msm_readl_ctl(pctrl, g); 212 213 /* 214 * If this is the first time muxing to GPIO and the direction is 215 * output, make sure that we're not going to be glitching the pin 216 * by reading the current state of the pin and setting it as the 217 * output. 218 */ 219 if (i == gpio_func && (val & BIT(g->oe_bit)) && 220 !test_and_set_bit(group, pctrl->ever_gpio)) { 221 u32 io_val = msm_readl_io(pctrl, g); 222 223 if (io_val & BIT(g->in_bit)) { 224 if (!(io_val & BIT(g->out_bit))) 225 msm_writel_io(io_val | BIT(g->out_bit), pctrl, g); 226 } else { 227 if (io_val & BIT(g->out_bit)) 228 msm_writel_io(io_val & ~BIT(g->out_bit), pctrl, g); 229 } 230 } 231 232 if (egpio_func && i == egpio_func) { 233 if (val & BIT(g->egpio_present)) 234 val &= ~BIT(g->egpio_enable); 235 } else { 236 val &= ~mask; 237 val |= i << g->mux_bit; 238 /* Claim ownership of pin if egpio capable */ 239 if (egpio_func && val & BIT(g->egpio_present)) 240 val |= BIT(g->egpio_enable); 241 } 242 243 msm_writel_ctl(val, pctrl, g); 244 245 raw_spin_unlock_irqrestore(&pctrl->lock, flags); 246 247 if (d && i == gpio_func && 248 test_and_clear_bit(d->hwirq, pctrl->disabled_for_mux)) { 249 /* 250 * Clear interrupts detected while not GPIO since we only 251 * masked things. 252 */ 253 if (d->parent_data && test_bit(d->hwirq, pctrl->skip_wake_irqs)) 254 irq_chip_set_parent_state(d, IRQCHIP_STATE_PENDING, false); 255 else 256 msm_ack_intr_status(pctrl, g); 257 258 enable_irq(irq); 259 } 260 261 return 0; 262 } 263 264 static int msm_pinmux_request_gpio(struct pinctrl_dev *pctldev, 265 struct pinctrl_gpio_range *range, 266 unsigned offset) 267 { 268 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); 269 const struct msm_pingroup *g = &pctrl->soc->groups[offset]; 270 271 /* No funcs? Probably ACPI so can't do anything here */ 272 if (!g->nfuncs) 273 return 0; 274 275 return msm_pinmux_set_mux(pctldev, g->funcs[pctrl->soc->gpio_func], offset); 276 } 277 278 static const struct pinmux_ops msm_pinmux_ops = { 279 .request = msm_pinmux_request, 280 .get_functions_count = pinmux_generic_get_function_count, 281 .get_function_name = pinmux_generic_get_function_name, 282 .get_function_groups = pinmux_generic_get_function_groups, 283 .function_is_gpio = pinmux_generic_function_is_gpio, 284 .gpio_request_enable = msm_pinmux_request_gpio, 285 .set_mux = msm_pinmux_set_mux, 286 .strict = true, 287 }; 288 289 static int msm_config_reg(struct msm_pinctrl *pctrl, 290 const struct msm_pingroup *g, 291 unsigned param, 292 unsigned *mask, 293 unsigned *bit) 294 { 295 switch (param) { 296 case PIN_CONFIG_BIAS_DISABLE: 297 case PIN_CONFIG_BIAS_PULL_DOWN: 298 case PIN_CONFIG_BIAS_BUS_HOLD: 299 case PIN_CONFIG_BIAS_PULL_UP: 300 *bit = g->pull_bit; 301 *mask = 3; 302 if (g->i2c_pull_bit) 303 *mask |= BIT(g->i2c_pull_bit) >> *bit; 304 break; 305 case PIN_CONFIG_DRIVE_OPEN_DRAIN: 306 *bit = g->od_bit; 307 *mask = 1; 308 break; 309 case PIN_CONFIG_DRIVE_STRENGTH: 310 *bit = g->drv_bit; 311 *mask = 7; 312 break; 313 case PIN_CONFIG_LEVEL: 314 case PIN_CONFIG_INPUT_ENABLE: 315 case PIN_CONFIG_OUTPUT_ENABLE: 316 *bit = g->oe_bit; 317 *mask = 1; 318 break; 319 default: 320 return -ENOTSUPP; 321 } 322 323 return 0; 324 } 325 326 #define MSM_NO_PULL 0 327 #define MSM_PULL_DOWN 1 328 #define MSM_KEEPER 2 329 #define MSM_PULL_UP_NO_KEEPER 2 330 #define MSM_PULL_UP 3 331 #define MSM_I2C_STRONG_PULL_UP 2200 332 333 static unsigned msm_regval_to_drive(u32 val) 334 { 335 return (val + 1) * 2; 336 } 337 338 static int msm_config_group_get(struct pinctrl_dev *pctldev, 339 unsigned int group, 340 unsigned long *config) 341 { 342 const struct msm_pingroup *g; 343 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); 344 unsigned param = pinconf_to_config_param(*config); 345 unsigned mask; 346 unsigned arg; 347 unsigned bit; 348 int ret; 349 u32 val; 350 351 /* Pin information can only be requested from valid pin groups */ 352 if (!gpiochip_line_is_valid(&pctrl->chip, group)) 353 return -EINVAL; 354 355 g = &pctrl->soc->groups[group]; 356 357 ret = msm_config_reg(pctrl, g, param, &mask, &bit); 358 if (ret < 0) 359 return ret; 360 361 val = msm_readl_ctl(pctrl, g); 362 arg = (val >> bit) & mask; 363 364 /* Convert register value to pinconf value */ 365 switch (param) { 366 case PIN_CONFIG_BIAS_DISABLE: 367 if (arg != MSM_NO_PULL) 368 return -EINVAL; 369 arg = 1; 370 break; 371 case PIN_CONFIG_BIAS_PULL_DOWN: 372 if (arg != MSM_PULL_DOWN) 373 return -EINVAL; 374 arg = 1; 375 break; 376 case PIN_CONFIG_BIAS_BUS_HOLD: 377 if (pctrl->soc->pull_no_keeper) 378 return -ENOTSUPP; 379 380 if (arg != MSM_KEEPER) 381 return -EINVAL; 382 arg = 1; 383 break; 384 case PIN_CONFIG_BIAS_PULL_UP: 385 if (pctrl->soc->pull_no_keeper) 386 arg = arg == MSM_PULL_UP_NO_KEEPER; 387 else if (arg & BIT(g->i2c_pull_bit)) 388 arg = MSM_I2C_STRONG_PULL_UP; 389 else 390 arg = arg == MSM_PULL_UP; 391 if (!arg) 392 return -EINVAL; 393 break; 394 case PIN_CONFIG_DRIVE_OPEN_DRAIN: 395 /* Pin is not open-drain */ 396 if (!arg) 397 return -EINVAL; 398 arg = 1; 399 break; 400 case PIN_CONFIG_DRIVE_STRENGTH: 401 arg = msm_regval_to_drive(arg); 402 break; 403 case PIN_CONFIG_LEVEL: 404 /* Pin is not output */ 405 if (!arg) 406 return -EINVAL; 407 408 val = msm_readl_io(pctrl, g); 409 arg = !!(val & BIT(g->in_bit)); 410 break; 411 case PIN_CONFIG_OUTPUT_ENABLE: 412 if (!arg) 413 return -EINVAL; 414 break; 415 default: 416 return -ENOTSUPP; 417 } 418 419 *config = pinconf_to_config_packed(param, arg); 420 421 return 0; 422 } 423 424 static int msm_config_group_set(struct pinctrl_dev *pctldev, 425 unsigned group, 426 unsigned long *configs, 427 unsigned num_configs) 428 { 429 const struct msm_pingroup *g; 430 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); 431 unsigned long flags; 432 unsigned param; 433 unsigned mask; 434 unsigned arg; 435 unsigned bit; 436 int ret; 437 u32 val; 438 int i; 439 440 g = &pctrl->soc->groups[group]; 441 442 for (i = 0; i < num_configs; i++) { 443 param = pinconf_to_config_param(configs[i]); 444 arg = pinconf_to_config_argument(configs[i]); 445 446 ret = msm_config_reg(pctrl, g, param, &mask, &bit); 447 if (ret < 0) 448 return ret; 449 450 /* Convert pinconf values to register values */ 451 switch (param) { 452 case PIN_CONFIG_BIAS_DISABLE: 453 arg = MSM_NO_PULL; 454 break; 455 case PIN_CONFIG_BIAS_PULL_DOWN: 456 arg = MSM_PULL_DOWN; 457 break; 458 case PIN_CONFIG_BIAS_BUS_HOLD: 459 if (pctrl->soc->pull_no_keeper) 460 return -ENOTSUPP; 461 462 arg = MSM_KEEPER; 463 break; 464 case PIN_CONFIG_BIAS_PULL_UP: 465 if (pctrl->soc->pull_no_keeper) 466 arg = MSM_PULL_UP_NO_KEEPER; 467 else if (g->i2c_pull_bit && arg == MSM_I2C_STRONG_PULL_UP) 468 arg = BIT(g->i2c_pull_bit) | MSM_PULL_UP; 469 else 470 arg = MSM_PULL_UP; 471 break; 472 case PIN_CONFIG_DRIVE_OPEN_DRAIN: 473 arg = 1; 474 break; 475 case PIN_CONFIG_DRIVE_STRENGTH: 476 /* Check for invalid values */ 477 if (arg > 16 || arg < 2 || (arg % 2) != 0) 478 arg = -1; 479 else 480 arg = (arg / 2) - 1; 481 break; 482 case PIN_CONFIG_LEVEL: 483 /* set output value */ 484 raw_spin_lock_irqsave(&pctrl->lock, flags); 485 val = msm_readl_io(pctrl, g); 486 if (arg) 487 val |= BIT(g->out_bit); 488 else 489 val &= ~BIT(g->out_bit); 490 msm_writel_io(val, pctrl, g); 491 raw_spin_unlock_irqrestore(&pctrl->lock, flags); 492 493 /* enable output */ 494 arg = 1; 495 break; 496 case PIN_CONFIG_INPUT_ENABLE: 497 /* 498 * According to pinctrl documentation this should 499 * actually be a no-op. 500 * 501 * The docs are explicit that "this does not affect 502 * the pin's ability to drive output" but what we do 503 * here is to modify the output enable bit. Thus, to 504 * follow the docs we should remove that. 505 * 506 * The docs say that we should enable any relevant 507 * input buffer, but TLMM there is no input buffer that 508 * can be enabled/disabled. It's always on. 509 * 510 * The points above, explain why this _should_ be a 511 * no-op. However, for historical reasons and to 512 * support old device trees, we'll violate the docs 513 * and still affect the output. 514 * 515 * It should further be noted that this old historical 516 * behavior actually overrides arg to 0. That means 517 * that "input-enable" and "input-disable" in a device 518 * tree would _both_ disable the output. We'll 519 * continue to preserve this behavior as well since 520 * we have no other use for this attribute. 521 */ 522 arg = 0; 523 break; 524 case PIN_CONFIG_OUTPUT_ENABLE: 525 arg = !!arg; 526 break; 527 default: 528 dev_err(pctrl->dev, "Unsupported config parameter: %x\n", 529 param); 530 return -EINVAL; 531 } 532 533 /* Range-check user-supplied value */ 534 if (arg & ~mask) { 535 dev_err(pctrl->dev, "config %x: %x is invalid\n", param, arg); 536 return -EINVAL; 537 } 538 539 raw_spin_lock_irqsave(&pctrl->lock, flags); 540 val = msm_readl_ctl(pctrl, g); 541 val &= ~(mask << bit); 542 val |= arg << bit; 543 msm_writel_ctl(val, pctrl, g); 544 raw_spin_unlock_irqrestore(&pctrl->lock, flags); 545 } 546 547 return 0; 548 } 549 550 static const struct pinconf_ops msm_pinconf_ops = { 551 .is_generic = true, 552 .pin_config_group_get = msm_config_group_get, 553 .pin_config_group_set = msm_config_group_set, 554 }; 555 556 static int msm_gpio_direction_input(struct gpio_chip *chip, unsigned offset) 557 { 558 const struct msm_pingroup *g; 559 struct msm_pinctrl *pctrl = gpiochip_get_data(chip); 560 unsigned long flags; 561 u32 val; 562 563 g = &pctrl->soc->groups[offset]; 564 565 raw_spin_lock_irqsave(&pctrl->lock, flags); 566 567 val = msm_readl_ctl(pctrl, g); 568 val &= ~BIT(g->oe_bit); 569 msm_writel_ctl(val, pctrl, g); 570 571 raw_spin_unlock_irqrestore(&pctrl->lock, flags); 572 573 return 0; 574 } 575 576 static int msm_gpio_direction_output(struct gpio_chip *chip, unsigned offset, int value) 577 { 578 const struct msm_pingroup *g; 579 struct msm_pinctrl *pctrl = gpiochip_get_data(chip); 580 unsigned long flags; 581 u32 val; 582 583 g = &pctrl->soc->groups[offset]; 584 585 raw_spin_lock_irqsave(&pctrl->lock, flags); 586 587 val = msm_readl_io(pctrl, g); 588 if (value) 589 val |= BIT(g->out_bit); 590 else 591 val &= ~BIT(g->out_bit); 592 msm_writel_io(val, pctrl, g); 593 594 val = msm_readl_ctl(pctrl, g); 595 val |= BIT(g->oe_bit); 596 msm_writel_ctl(val, pctrl, g); 597 598 raw_spin_unlock_irqrestore(&pctrl->lock, flags); 599 600 return 0; 601 } 602 603 static int msm_gpio_get_direction(struct gpio_chip *chip, unsigned int offset) 604 { 605 struct msm_pinctrl *pctrl = gpiochip_get_data(chip); 606 const struct msm_pingroup *g; 607 u32 val; 608 609 g = &pctrl->soc->groups[offset]; 610 611 val = msm_readl_ctl(pctrl, g); 612 613 return val & BIT(g->oe_bit) ? GPIO_LINE_DIRECTION_OUT : 614 GPIO_LINE_DIRECTION_IN; 615 } 616 617 static int msm_gpio_get(struct gpio_chip *chip, unsigned offset) 618 { 619 const struct msm_pingroup *g; 620 struct msm_pinctrl *pctrl = gpiochip_get_data(chip); 621 u32 val; 622 623 g = &pctrl->soc->groups[offset]; 624 625 val = msm_readl_io(pctrl, g); 626 return !!(val & BIT(g->in_bit)); 627 } 628 629 static int msm_gpio_set(struct gpio_chip *chip, unsigned int offset, int value) 630 { 631 const struct msm_pingroup *g; 632 struct msm_pinctrl *pctrl = gpiochip_get_data(chip); 633 unsigned long flags; 634 u32 val; 635 636 g = &pctrl->soc->groups[offset]; 637 638 raw_spin_lock_irqsave(&pctrl->lock, flags); 639 640 val = msm_readl_io(pctrl, g); 641 if (value) 642 val |= BIT(g->out_bit); 643 else 644 val &= ~BIT(g->out_bit); 645 msm_writel_io(val, pctrl, g); 646 647 raw_spin_unlock_irqrestore(&pctrl->lock, flags); 648 649 return 0; 650 } 651 652 #ifdef CONFIG_DEBUG_FS 653 654 static void msm_gpio_dbg_show_one(struct seq_file *s, 655 struct pinctrl_dev *pctldev, 656 struct gpio_chip *chip, 657 unsigned offset, 658 unsigned gpio) 659 { 660 const struct msm_pingroup *g; 661 struct msm_pinctrl *pctrl = gpiochip_get_data(chip); 662 unsigned func; 663 int is_out; 664 int drive; 665 int pull; 666 int val; 667 int egpio_enable; 668 u32 ctl_reg, io_reg; 669 670 static const char * const pulls_keeper[] = { 671 "no pull", 672 "pull down", 673 "keeper", 674 "pull up" 675 }; 676 677 static const char * const pulls_no_keeper[] = { 678 "no pull", 679 "pull down", 680 "pull up", 681 }; 682 683 if (!gpiochip_line_is_valid(chip, offset)) 684 return; 685 686 g = &pctrl->soc->groups[offset]; 687 ctl_reg = msm_readl_ctl(pctrl, g); 688 io_reg = msm_readl_io(pctrl, g); 689 690 is_out = !!(ctl_reg & BIT(g->oe_bit)); 691 func = (ctl_reg >> g->mux_bit) & 7; 692 drive = (ctl_reg >> g->drv_bit) & 7; 693 pull = (ctl_reg >> g->pull_bit) & 3; 694 egpio_enable = 0; 695 if (pctrl->soc->egpio_func && ctl_reg & BIT(g->egpio_present)) 696 egpio_enable = !(ctl_reg & BIT(g->egpio_enable)); 697 698 if (is_out) 699 val = !!(io_reg & BIT(g->out_bit)); 700 else 701 val = !!(io_reg & BIT(g->in_bit)); 702 703 if (egpio_enable) { 704 seq_printf(s, " %-8s: egpio\n", g->grp.name); 705 return; 706 } 707 708 seq_printf(s, " %-8s: %-3s", g->grp.name, is_out ? "out" : "in"); 709 seq_printf(s, " %-4s func%d", str_high_low(val), func); 710 seq_printf(s, " %dmA", msm_regval_to_drive(drive)); 711 if (pctrl->soc->pull_no_keeper) 712 seq_printf(s, " %s", pulls_no_keeper[pull]); 713 else 714 seq_printf(s, " %s", pulls_keeper[pull]); 715 seq_puts(s, "\n"); 716 } 717 718 static void msm_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip) 719 { 720 unsigned gpio = chip->base; 721 unsigned i; 722 723 for (i = 0; i < chip->ngpio; i++, gpio++) 724 msm_gpio_dbg_show_one(s, NULL, chip, i, gpio); 725 } 726 727 #else 728 #define msm_gpio_dbg_show NULL 729 #endif 730 731 static int msm_gpio_init_valid_mask(struct gpio_chip *gc, 732 unsigned long *valid_mask, 733 unsigned int ngpios) 734 { 735 struct msm_pinctrl *pctrl = gpiochip_get_data(gc); 736 int ret; 737 unsigned int len, i; 738 const int *reserved = pctrl->soc->reserved_gpios; 739 u16 *tmp; 740 741 /* Remove driver-provided reserved GPIOs from valid_mask */ 742 if (reserved) { 743 for (i = 0; reserved[i] >= 0; i++) { 744 if (i >= ngpios || reserved[i] >= ngpios) { 745 dev_err(pctrl->dev, "invalid list of reserved GPIOs\n"); 746 return -EINVAL; 747 } 748 clear_bit(reserved[i], valid_mask); 749 } 750 751 return 0; 752 } 753 754 /* The number of GPIOs in the ACPI tables */ 755 len = ret = device_property_count_u16(pctrl->dev, "gpios"); 756 if (ret < 0) 757 return 0; 758 759 if (ret > ngpios) 760 return -EINVAL; 761 762 tmp = kmalloc_array(len, sizeof(*tmp), GFP_KERNEL); 763 if (!tmp) 764 return -ENOMEM; 765 766 ret = device_property_read_u16_array(pctrl->dev, "gpios", tmp, len); 767 if (ret < 0) { 768 dev_err(pctrl->dev, "could not read list of GPIOs\n"); 769 goto out; 770 } 771 772 bitmap_zero(valid_mask, ngpios); 773 for (i = 0; i < len; i++) 774 set_bit(tmp[i], valid_mask); 775 776 out: 777 kfree(tmp); 778 return ret; 779 } 780 781 static const struct gpio_chip msm_gpio_template = { 782 .direction_input = msm_gpio_direction_input, 783 .direction_output = msm_gpio_direction_output, 784 .get_direction = msm_gpio_get_direction, 785 .get = msm_gpio_get, 786 .set = msm_gpio_set, 787 .request = gpiochip_generic_request, 788 .free = gpiochip_generic_free, 789 .dbg_show = msm_gpio_dbg_show, 790 }; 791 792 /* For dual-edge interrupts in software, since some hardware has no 793 * such support: 794 * 795 * At appropriate moments, this function may be called to flip the polarity 796 * settings of both-edge irq lines to try and catch the next edge. 797 * 798 * The attempt is considered successful if: 799 * - the status bit goes high, indicating that an edge was caught, or 800 * - the input value of the gpio doesn't change during the attempt. 801 * If the value changes twice during the process, that would cause the first 802 * test to fail but would force the second, as two opposite 803 * transitions would cause a detection no matter the polarity setting. 804 * 805 * The do-loop tries to sledge-hammer closed the timing hole between 806 * the initial value-read and the polarity-write - if the line value changes 807 * during that window, an interrupt is lost, the new polarity setting is 808 * incorrect, and the first success test will fail, causing a retry. 809 * 810 * Algorithm comes from Google's msmgpio driver. 811 */ 812 static void msm_gpio_update_dual_edge_pos(struct msm_pinctrl *pctrl, 813 const struct msm_pingroup *g, 814 struct irq_data *d) 815 { 816 int loop_limit = 100; 817 unsigned val, val2, intstat; 818 unsigned pol; 819 820 do { 821 val = msm_readl_io(pctrl, g) & BIT(g->in_bit); 822 823 pol = msm_readl_intr_cfg(pctrl, g); 824 pol ^= BIT(g->intr_polarity_bit); 825 msm_writel_intr_cfg(pol, pctrl, g); 826 827 val2 = msm_readl_io(pctrl, g) & BIT(g->in_bit); 828 intstat = msm_readl_intr_status(pctrl, g); 829 if (intstat || (val == val2)) 830 return; 831 } while (loop_limit-- > 0); 832 dev_err(pctrl->dev, "dual-edge irq failed to stabilize, %#08x != %#08x\n", 833 val, val2); 834 } 835 836 static void msm_gpio_irq_mask(struct irq_data *d) 837 { 838 struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 839 struct msm_pinctrl *pctrl = gpiochip_get_data(gc); 840 const struct msm_pingroup *g; 841 unsigned long flags; 842 u32 val; 843 844 if (d->parent_data) 845 irq_chip_mask_parent(d); 846 847 if (test_bit(d->hwirq, pctrl->skip_wake_irqs)) 848 return; 849 850 g = &pctrl->soc->groups[d->hwirq]; 851 852 raw_spin_lock_irqsave(&pctrl->lock, flags); 853 854 val = msm_readl_intr_cfg(pctrl, g); 855 /* 856 * There are two bits that control interrupt forwarding to the CPU. The 857 * RAW_STATUS_EN bit causes the level or edge sensed on the line to be 858 * latched into the interrupt status register when the hardware detects 859 * an irq that it's configured for (either edge for edge type or level 860 * for level type irq). The 'non-raw' status enable bit causes the 861 * hardware to assert the summary interrupt to the CPU if the latched 862 * status bit is set. There's a bug though, the edge detection logic 863 * seems to have a problem where toggling the RAW_STATUS_EN bit may 864 * cause the status bit to latch spuriously when there isn't any edge 865 * so we can't touch that bit for edge type irqs and we have to keep 866 * the bit set anyway so that edges are latched while the line is masked. 867 * 868 * To make matters more complicated, leaving the RAW_STATUS_EN bit 869 * enabled all the time causes level interrupts to re-latch into the 870 * status register because the level is still present on the line after 871 * we ack it. We clear the raw status enable bit during mask here and 872 * set the bit on unmask so the interrupt can't latch into the hardware 873 * while it's masked. 874 */ 875 if (irqd_get_trigger_type(d) & IRQ_TYPE_LEVEL_MASK) 876 val &= ~BIT(g->intr_raw_status_bit); 877 878 val &= ~BIT(g->intr_enable_bit); 879 msm_writel_intr_cfg(val, pctrl, g); 880 881 clear_bit(d->hwirq, pctrl->enabled_irqs); 882 883 raw_spin_unlock_irqrestore(&pctrl->lock, flags); 884 } 885 886 static void msm_gpio_irq_unmask(struct irq_data *d) 887 { 888 struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 889 struct msm_pinctrl *pctrl = gpiochip_get_data(gc); 890 const struct msm_pingroup *g; 891 unsigned long flags; 892 u32 val; 893 894 if (d->parent_data) 895 irq_chip_unmask_parent(d); 896 897 if (test_bit(d->hwirq, pctrl->skip_wake_irqs)) 898 return; 899 900 g = &pctrl->soc->groups[d->hwirq]; 901 902 raw_spin_lock_irqsave(&pctrl->lock, flags); 903 904 val = msm_readl_intr_cfg(pctrl, g); 905 val |= BIT(g->intr_raw_status_bit); 906 val |= BIT(g->intr_enable_bit); 907 msm_writel_intr_cfg(val, pctrl, g); 908 909 set_bit(d->hwirq, pctrl->enabled_irqs); 910 911 raw_spin_unlock_irqrestore(&pctrl->lock, flags); 912 } 913 914 static void msm_gpio_irq_enable(struct irq_data *d) 915 { 916 struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 917 struct msm_pinctrl *pctrl = gpiochip_get_data(gc); 918 919 gpiochip_enable_irq(gc, d->hwirq); 920 921 if (d->parent_data) 922 irq_chip_enable_parent(d); 923 924 if (!test_bit(d->hwirq, pctrl->skip_wake_irqs)) 925 msm_gpio_irq_unmask(d); 926 } 927 928 static void msm_gpio_irq_disable(struct irq_data *d) 929 { 930 struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 931 struct msm_pinctrl *pctrl = gpiochip_get_data(gc); 932 933 if (d->parent_data) 934 irq_chip_disable_parent(d); 935 936 if (!test_bit(d->hwirq, pctrl->skip_wake_irqs)) 937 msm_gpio_irq_mask(d); 938 939 gpiochip_disable_irq(gc, d->hwirq); 940 } 941 942 /** 943 * msm_gpio_update_dual_edge_parent() - Prime next edge for IRQs handled by parent. 944 * @d: The irq dta. 945 * 946 * This is much like msm_gpio_update_dual_edge_pos() but for IRQs that are 947 * normally handled by the parent irqchip. The logic here is slightly 948 * different due to what's easy to do with our parent, but in principle it's 949 * the same. 950 */ 951 static void msm_gpio_update_dual_edge_parent(struct irq_data *d) 952 { 953 struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 954 struct msm_pinctrl *pctrl = gpiochip_get_data(gc); 955 const struct msm_pingroup *g = &pctrl->soc->groups[d->hwirq]; 956 int loop_limit = 100; 957 unsigned int val; 958 unsigned int type; 959 960 /* Read the value and make a guess about what edge we need to catch */ 961 val = msm_readl_io(pctrl, g) & BIT(g->in_bit); 962 type = val ? IRQ_TYPE_EDGE_FALLING : IRQ_TYPE_EDGE_RISING; 963 964 do { 965 /* Set the parent to catch the next edge */ 966 irq_chip_set_type_parent(d, type); 967 968 /* 969 * Possibly the line changed between when we last read "val" 970 * (and decided what edge we needed) and when set the edge. 971 * If the value didn't change (or changed and then changed 972 * back) then we're done. 973 */ 974 val = msm_readl_io(pctrl, g) & BIT(g->in_bit); 975 if (type == IRQ_TYPE_EDGE_RISING) { 976 if (!val) 977 return; 978 type = IRQ_TYPE_EDGE_FALLING; 979 } else if (type == IRQ_TYPE_EDGE_FALLING) { 980 if (val) 981 return; 982 type = IRQ_TYPE_EDGE_RISING; 983 } 984 } while (loop_limit-- > 0); 985 dev_warn_once(pctrl->dev, "dual-edge irq failed to stabilize\n"); 986 } 987 988 static void msm_gpio_irq_ack(struct irq_data *d) 989 { 990 struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 991 struct msm_pinctrl *pctrl = gpiochip_get_data(gc); 992 const struct msm_pingroup *g; 993 unsigned long flags; 994 995 if (test_bit(d->hwirq, pctrl->skip_wake_irqs)) { 996 if (test_bit(d->hwirq, pctrl->dual_edge_irqs)) 997 msm_gpio_update_dual_edge_parent(d); 998 return; 999 } 1000 1001 g = &pctrl->soc->groups[d->hwirq]; 1002 1003 raw_spin_lock_irqsave(&pctrl->lock, flags); 1004 1005 msm_ack_intr_status(pctrl, g); 1006 1007 if (test_bit(d->hwirq, pctrl->dual_edge_irqs)) 1008 msm_gpio_update_dual_edge_pos(pctrl, g, d); 1009 1010 raw_spin_unlock_irqrestore(&pctrl->lock, flags); 1011 } 1012 1013 static void msm_gpio_irq_eoi(struct irq_data *d) 1014 { 1015 if (d->parent_data) 1016 irq_chip_eoi_parent(d); 1017 } 1018 1019 static bool msm_gpio_needs_dual_edge_parent_workaround(struct irq_data *d, 1020 unsigned int type) 1021 { 1022 struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 1023 struct msm_pinctrl *pctrl = gpiochip_get_data(gc); 1024 1025 return type == IRQ_TYPE_EDGE_BOTH && 1026 pctrl->soc->wakeirq_dual_edge_errata && d->parent_data && 1027 test_bit(d->hwirq, pctrl->skip_wake_irqs); 1028 } 1029 1030 static void msm_gpio_irq_init_valid_mask(struct gpio_chip *gc, 1031 unsigned long *valid_mask, 1032 unsigned int ngpios) 1033 { 1034 struct msm_pinctrl *pctrl = gpiochip_get_data(gc); 1035 const struct msm_pingroup *g; 1036 int i; 1037 1038 bitmap_fill(valid_mask, ngpios); 1039 1040 for (i = 0; i < ngpios; i++) { 1041 g = &pctrl->soc->groups[i]; 1042 1043 if (g->intr_detection_width != 1 && 1044 g->intr_detection_width != 2) 1045 clear_bit(i, valid_mask); 1046 } 1047 } 1048 1049 static int msm_gpio_irq_set_type(struct irq_data *d, unsigned int type) 1050 { 1051 struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 1052 struct msm_pinctrl *pctrl = gpiochip_get_data(gc); 1053 const struct msm_pingroup *g; 1054 u32 intr_target_mask = GENMASK(2, 0); 1055 unsigned long flags; 1056 u32 val, oldval; 1057 1058 if (msm_gpio_needs_dual_edge_parent_workaround(d, type)) { 1059 set_bit(d->hwirq, pctrl->dual_edge_irqs); 1060 irq_set_handler_locked(d, handle_fasteoi_ack_irq); 1061 msm_gpio_update_dual_edge_parent(d); 1062 return 0; 1063 } 1064 1065 if (d->parent_data) 1066 irq_chip_set_type_parent(d, type); 1067 1068 if (test_bit(d->hwirq, pctrl->skip_wake_irqs)) { 1069 clear_bit(d->hwirq, pctrl->dual_edge_irqs); 1070 irq_set_handler_locked(d, handle_fasteoi_irq); 1071 return 0; 1072 } 1073 1074 g = &pctrl->soc->groups[d->hwirq]; 1075 1076 raw_spin_lock_irqsave(&pctrl->lock, flags); 1077 1078 /* 1079 * For hw without possibility of detecting both edges 1080 */ 1081 if (g->intr_detection_width == 1 && type == IRQ_TYPE_EDGE_BOTH) 1082 set_bit(d->hwirq, pctrl->dual_edge_irqs); 1083 else 1084 clear_bit(d->hwirq, pctrl->dual_edge_irqs); 1085 1086 /* Route interrupts to application cpu. 1087 * With intr_target_use_scm interrupts are routed to 1088 * application cpu using scm calls. 1089 */ 1090 if (g->intr_target_width) 1091 intr_target_mask = GENMASK(g->intr_target_width - 1, 0); 1092 1093 if (pctrl->intr_target_use_scm) { 1094 u32 reg = g->intr_target_reg ? g->intr_target_reg : g->intr_cfg_reg; 1095 u32 addr = pctrl->phys_base[0] + reg; 1096 int ret; 1097 1098 qcom_scm_io_readl(addr, &val); 1099 val &= ~(intr_target_mask << g->intr_target_bit); 1100 val |= g->intr_target_kpss_val << g->intr_target_bit; 1101 1102 ret = qcom_scm_io_writel(addr, val); 1103 if (ret) 1104 dev_err(pctrl->dev, 1105 "Failed routing %lu interrupt to Apps proc", 1106 d->hwirq); 1107 } else { 1108 val = msm_readl_intr_target(pctrl, g); 1109 val &= ~(intr_target_mask << g->intr_target_bit); 1110 val |= g->intr_target_kpss_val << g->intr_target_bit; 1111 msm_writel_intr_target(val, pctrl, g); 1112 } 1113 1114 /* Update configuration for gpio. 1115 * RAW_STATUS_EN is left on for all gpio irqs. Due to the 1116 * internal circuitry of TLMM, toggling the RAW_STATUS 1117 * could cause the INTR_STATUS to be set for EDGE interrupts. 1118 */ 1119 val = oldval = msm_readl_intr_cfg(pctrl, g); 1120 val |= BIT(g->intr_raw_status_bit); 1121 if (g->intr_detection_width == 2) { 1122 val &= ~(3 << g->intr_detection_bit); 1123 val &= ~(1 << g->intr_polarity_bit); 1124 switch (type) { 1125 case IRQ_TYPE_EDGE_RISING: 1126 val |= 1 << g->intr_detection_bit; 1127 val |= BIT(g->intr_polarity_bit); 1128 break; 1129 case IRQ_TYPE_EDGE_FALLING: 1130 val |= 2 << g->intr_detection_bit; 1131 val |= BIT(g->intr_polarity_bit); 1132 break; 1133 case IRQ_TYPE_EDGE_BOTH: 1134 val |= 3 << g->intr_detection_bit; 1135 val |= BIT(g->intr_polarity_bit); 1136 break; 1137 case IRQ_TYPE_LEVEL_LOW: 1138 break; 1139 case IRQ_TYPE_LEVEL_HIGH: 1140 val |= BIT(g->intr_polarity_bit); 1141 break; 1142 } 1143 } else if (g->intr_detection_width == 1) { 1144 val &= ~(1 << g->intr_detection_bit); 1145 val &= ~(1 << g->intr_polarity_bit); 1146 switch (type) { 1147 case IRQ_TYPE_EDGE_RISING: 1148 val |= BIT(g->intr_detection_bit); 1149 val |= BIT(g->intr_polarity_bit); 1150 break; 1151 case IRQ_TYPE_EDGE_FALLING: 1152 val |= BIT(g->intr_detection_bit); 1153 break; 1154 case IRQ_TYPE_EDGE_BOTH: 1155 val |= BIT(g->intr_detection_bit); 1156 val |= BIT(g->intr_polarity_bit); 1157 break; 1158 case IRQ_TYPE_LEVEL_LOW: 1159 break; 1160 case IRQ_TYPE_LEVEL_HIGH: 1161 val |= BIT(g->intr_polarity_bit); 1162 break; 1163 } 1164 } else { 1165 BUG(); 1166 } 1167 msm_writel_intr_cfg(val, pctrl, g); 1168 1169 /* 1170 * The first time we set RAW_STATUS_EN it could trigger an interrupt. 1171 * Clear the interrupt. This is safe because we have 1172 * IRQCHIP_SET_TYPE_MASKED. When changing the interrupt type, we could 1173 * also still have a non-matching interrupt latched, so clear whenever 1174 * making changes to the interrupt configuration. 1175 */ 1176 if (val != oldval) 1177 msm_ack_intr_status(pctrl, g); 1178 1179 if (test_bit(d->hwirq, pctrl->dual_edge_irqs)) 1180 msm_gpio_update_dual_edge_pos(pctrl, g, d); 1181 1182 raw_spin_unlock_irqrestore(&pctrl->lock, flags); 1183 1184 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH)) 1185 irq_set_handler_locked(d, handle_level_irq); 1186 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING)) 1187 irq_set_handler_locked(d, handle_edge_irq); 1188 1189 return 0; 1190 } 1191 1192 static int msm_gpio_irq_set_wake(struct irq_data *d, unsigned int on) 1193 { 1194 struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 1195 struct msm_pinctrl *pctrl = gpiochip_get_data(gc); 1196 1197 /* 1198 * While they may not wake up when the TLMM is powered off, 1199 * some GPIOs would like to wakeup the system from suspend 1200 * when TLMM is powered on. To allow that, enable the GPIO 1201 * summary line to be wakeup capable at GIC. 1202 */ 1203 if (d->parent_data && test_bit(d->hwirq, pctrl->skip_wake_irqs)) 1204 return irq_chip_set_wake_parent(d, on); 1205 1206 return irq_set_irq_wake(pctrl->irq, on); 1207 } 1208 1209 static int msm_gpio_irq_reqres(struct irq_data *d) 1210 { 1211 struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 1212 struct msm_pinctrl *pctrl = gpiochip_get_data(gc); 1213 const struct msm_pingroup *g = &pctrl->soc->groups[d->hwirq]; 1214 unsigned long flags; 1215 int ret; 1216 1217 if (!try_module_get(gc->owner)) 1218 return -ENODEV; 1219 1220 ret = msm_pinmux_request_gpio(pctrl->pctrl, NULL, d->hwirq); 1221 if (ret) 1222 goto out; 1223 msm_gpio_direction_input(gc, d->hwirq); 1224 1225 if (gpiochip_lock_as_irq(gc, d->hwirq)) { 1226 dev_err(gc->parent, 1227 "unable to lock HW IRQ %lu for IRQ\n", 1228 d->hwirq); 1229 ret = -EINVAL; 1230 goto out; 1231 } 1232 1233 /* 1234 * The disable / clear-enable workaround we do in msm_pinmux_set_mux() 1235 * only works if disable is not lazy since we only clear any bogus 1236 * interrupt in hardware. Explicitly mark the interrupt as UNLAZY. 1237 */ 1238 irq_set_status_flags(d->irq, IRQ_DISABLE_UNLAZY); 1239 1240 /* 1241 * If the wakeup_enable bit is present and marked as available for the 1242 * requested GPIO, it should be enabled when the GPIO is marked as 1243 * wake irq in order to allow the interrupt event to be transfered to 1244 * the PDC HW. 1245 * While the name implies only the wakeup event, it's also required for 1246 * the interrupt event. 1247 */ 1248 if (test_bit(d->hwirq, pctrl->skip_wake_irqs) && g->intr_wakeup_present_bit) { 1249 u32 intr_cfg; 1250 1251 raw_spin_lock_irqsave(&pctrl->lock, flags); 1252 1253 intr_cfg = msm_readl_intr_cfg(pctrl, g); 1254 if (intr_cfg & BIT(g->intr_wakeup_present_bit)) { 1255 intr_cfg |= BIT(g->intr_wakeup_enable_bit); 1256 msm_writel_intr_cfg(intr_cfg, pctrl, g); 1257 } 1258 1259 raw_spin_unlock_irqrestore(&pctrl->lock, flags); 1260 } 1261 1262 return 0; 1263 out: 1264 module_put(gc->owner); 1265 return ret; 1266 } 1267 1268 static void msm_gpio_irq_relres(struct irq_data *d) 1269 { 1270 struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 1271 struct msm_pinctrl *pctrl = gpiochip_get_data(gc); 1272 const struct msm_pingroup *g = &pctrl->soc->groups[d->hwirq]; 1273 unsigned long flags; 1274 1275 /* Disable the wakeup_enable bit if it has been set in msm_gpio_irq_reqres() */ 1276 if (test_bit(d->hwirq, pctrl->skip_wake_irqs) && g->intr_wakeup_present_bit) { 1277 u32 intr_cfg; 1278 1279 raw_spin_lock_irqsave(&pctrl->lock, flags); 1280 1281 intr_cfg = msm_readl_intr_cfg(pctrl, g); 1282 if (intr_cfg & BIT(g->intr_wakeup_present_bit)) { 1283 intr_cfg &= ~BIT(g->intr_wakeup_enable_bit); 1284 msm_writel_intr_cfg(intr_cfg, pctrl, g); 1285 } 1286 1287 raw_spin_unlock_irqrestore(&pctrl->lock, flags); 1288 } 1289 1290 gpiochip_unlock_as_irq(gc, d->hwirq); 1291 module_put(gc->owner); 1292 } 1293 1294 static int msm_gpio_irq_set_affinity(struct irq_data *d, 1295 const struct cpumask *dest, bool force) 1296 { 1297 struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 1298 struct msm_pinctrl *pctrl = gpiochip_get_data(gc); 1299 1300 if (d->parent_data && test_bit(d->hwirq, pctrl->skip_wake_irqs)) 1301 return irq_chip_set_affinity_parent(d, dest, force); 1302 1303 return -EINVAL; 1304 } 1305 1306 static int msm_gpio_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu_info) 1307 { 1308 struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 1309 struct msm_pinctrl *pctrl = gpiochip_get_data(gc); 1310 1311 if (d->parent_data && test_bit(d->hwirq, pctrl->skip_wake_irqs)) 1312 return irq_chip_set_vcpu_affinity_parent(d, vcpu_info); 1313 1314 return -EINVAL; 1315 } 1316 1317 static void msm_gpio_irq_handler(struct irq_desc *desc) 1318 { 1319 struct gpio_chip *gc = irq_desc_get_handler_data(desc); 1320 const struct msm_pingroup *g; 1321 struct msm_pinctrl *pctrl = gpiochip_get_data(gc); 1322 struct irq_chip *chip = irq_desc_get_chip(desc); 1323 int handled = 0; 1324 u32 val; 1325 int i; 1326 1327 chained_irq_enter(chip, desc); 1328 1329 /* 1330 * Each pin has it's own IRQ status register, so use 1331 * enabled_irq bitmap to limit the number of reads. 1332 */ 1333 for_each_set_bit(i, pctrl->enabled_irqs, pctrl->chip.ngpio) { 1334 g = &pctrl->soc->groups[i]; 1335 val = msm_readl_intr_status(pctrl, g); 1336 if (val & BIT(g->intr_status_bit)) { 1337 generic_handle_domain_irq(gc->irq.domain, i); 1338 handled++; 1339 } 1340 } 1341 1342 /* No interrupts were flagged */ 1343 if (handled == 0) 1344 handle_bad_irq(desc); 1345 1346 chained_irq_exit(chip, desc); 1347 } 1348 1349 static int msm_gpio_wakeirq(struct gpio_chip *gc, 1350 unsigned int child, 1351 unsigned int child_type, 1352 unsigned int *parent, 1353 unsigned int *parent_type) 1354 { 1355 struct msm_pinctrl *pctrl = gpiochip_get_data(gc); 1356 const struct msm_gpio_wakeirq_map *map; 1357 int i; 1358 1359 *parent = GPIO_NO_WAKE_IRQ; 1360 *parent_type = IRQ_TYPE_EDGE_RISING; 1361 1362 for (i = 0; i < pctrl->soc->nwakeirq_map; i++) { 1363 map = &pctrl->soc->wakeirq_map[i]; 1364 if (map->gpio == child) { 1365 *parent = map->wakeirq; 1366 break; 1367 } 1368 } 1369 1370 return 0; 1371 } 1372 1373 static bool msm_gpio_needs_valid_mask(struct msm_pinctrl *pctrl) 1374 { 1375 if (pctrl->soc->reserved_gpios) 1376 return true; 1377 1378 return device_property_count_u16(pctrl->dev, "gpios") > 0; 1379 } 1380 1381 static const struct irq_chip msm_gpio_irq_chip = { 1382 .name = "msmgpio", 1383 .irq_enable = msm_gpio_irq_enable, 1384 .irq_disable = msm_gpio_irq_disable, 1385 .irq_mask = msm_gpio_irq_mask, 1386 .irq_unmask = msm_gpio_irq_unmask, 1387 .irq_ack = msm_gpio_irq_ack, 1388 .irq_eoi = msm_gpio_irq_eoi, 1389 .irq_set_type = msm_gpio_irq_set_type, 1390 .irq_set_wake = msm_gpio_irq_set_wake, 1391 .irq_request_resources = msm_gpio_irq_reqres, 1392 .irq_release_resources = msm_gpio_irq_relres, 1393 .irq_set_affinity = msm_gpio_irq_set_affinity, 1394 .irq_set_vcpu_affinity = msm_gpio_irq_set_vcpu_affinity, 1395 .flags = (IRQCHIP_MASK_ON_SUSPEND | 1396 IRQCHIP_SET_TYPE_MASKED | 1397 IRQCHIP_ENABLE_WAKEUP_ON_SUSPEND | 1398 IRQCHIP_IMMUTABLE), 1399 }; 1400 1401 static int msm_gpio_init(struct msm_pinctrl *pctrl) 1402 { 1403 struct gpio_chip *chip; 1404 struct gpio_irq_chip *girq; 1405 int i, ret; 1406 unsigned gpio, ngpio = pctrl->soc->ngpios; 1407 struct device_node *np; 1408 bool skip; 1409 1410 if (WARN_ON(ngpio > MAX_NR_GPIO)) 1411 return -EINVAL; 1412 1413 chip = &pctrl->chip; 1414 chip->base = -1; 1415 chip->ngpio = ngpio; 1416 chip->label = dev_name(pctrl->dev); 1417 chip->parent = pctrl->dev; 1418 chip->owner = THIS_MODULE; 1419 if (msm_gpio_needs_valid_mask(pctrl)) 1420 chip->init_valid_mask = msm_gpio_init_valid_mask; 1421 1422 np = of_parse_phandle(pctrl->dev->of_node, "wakeup-parent", 0); 1423 if (np) { 1424 chip->irq.parent_domain = irq_find_matching_host(np, 1425 DOMAIN_BUS_WAKEUP); 1426 of_node_put(np); 1427 if (!chip->irq.parent_domain) 1428 return -EPROBE_DEFER; 1429 chip->irq.child_to_parent_hwirq = msm_gpio_wakeirq; 1430 /* 1431 * Let's skip handling the GPIOs, if the parent irqchip 1432 * is handling the direct connect IRQ of the GPIO. 1433 */ 1434 skip = irq_domain_qcom_handle_wakeup(chip->irq.parent_domain); 1435 for (i = 0; skip && i < pctrl->soc->nwakeirq_map; i++) { 1436 gpio = pctrl->soc->wakeirq_map[i].gpio; 1437 set_bit(gpio, pctrl->skip_wake_irqs); 1438 } 1439 } 1440 1441 girq = &chip->irq; 1442 gpio_irq_chip_set_chip(girq, &msm_gpio_irq_chip); 1443 girq->parent_handler = msm_gpio_irq_handler; 1444 girq->fwnode = dev_fwnode(pctrl->dev); 1445 girq->num_parents = 1; 1446 girq->parents = devm_kcalloc(pctrl->dev, 1, sizeof(*girq->parents), 1447 GFP_KERNEL); 1448 if (!girq->parents) 1449 return -ENOMEM; 1450 girq->default_type = IRQ_TYPE_NONE; 1451 girq->handler = handle_bad_irq; 1452 girq->parents[0] = pctrl->irq; 1453 girq->init_valid_mask = msm_gpio_irq_init_valid_mask; 1454 1455 ret = devm_gpiochip_add_data(pctrl->dev, &pctrl->chip, pctrl); 1456 if (ret) { 1457 dev_err(pctrl->dev, "Failed register gpiochip\n"); 1458 return ret; 1459 } 1460 1461 /* 1462 * For DeviceTree-supported systems, the gpio core checks the 1463 * pinctrl's device node for the "gpio-ranges" property. 1464 * If it is present, it takes care of adding the pin ranges 1465 * for the driver. In this case the driver can skip ahead. 1466 * 1467 * In order to remain compatible with older, existing DeviceTree 1468 * files which don't set the "gpio-ranges" property or systems that 1469 * utilize ACPI the driver has to call gpiochip_add_pin_range(). 1470 */ 1471 if (!of_property_present(pctrl->dev->of_node, "gpio-ranges")) { 1472 ret = gpiochip_add_pin_range(&pctrl->chip, 1473 dev_name(pctrl->dev), 0, 0, chip->ngpio); 1474 if (ret) { 1475 dev_err(pctrl->dev, "Failed to add pin range\n"); 1476 return ret; 1477 } 1478 } 1479 1480 return 0; 1481 } 1482 1483 static int msm_ps_hold_restart(struct sys_off_data *data) 1484 { 1485 struct msm_pinctrl *pctrl = data->cb_data; 1486 1487 writel(0, pctrl->regs[0] + PS_HOLD_OFFSET); 1488 mdelay(1000); 1489 return NOTIFY_DONE; 1490 } 1491 1492 static struct msm_pinctrl *poweroff_pctrl; 1493 1494 static void msm_ps_hold_poweroff(void) 1495 { 1496 struct sys_off_data data = { 1497 .cb_data = poweroff_pctrl, 1498 }; 1499 1500 msm_ps_hold_restart(&data); 1501 } 1502 1503 static void msm_pinctrl_setup_pm_reset(struct msm_pinctrl *pctrl) 1504 { 1505 int i; 1506 const struct pinfunction *func = pctrl->soc->functions; 1507 1508 for (i = 0; i < pctrl->soc->nfunctions; i++) 1509 if (!strcmp(func[i].name, "ps_hold")) { 1510 if (devm_register_sys_off_handler(pctrl->dev, 1511 SYS_OFF_MODE_RESTART, 1512 128, 1513 msm_ps_hold_restart, 1514 pctrl)) 1515 dev_err(pctrl->dev, 1516 "failed to setup restart handler.\n"); 1517 poweroff_pctrl = pctrl; 1518 pm_power_off = msm_ps_hold_poweroff; 1519 break; 1520 } 1521 } 1522 1523 static __maybe_unused int msm_pinctrl_suspend(struct device *dev) 1524 { 1525 struct msm_pinctrl *pctrl = dev_get_drvdata(dev); 1526 1527 return pinctrl_force_sleep(pctrl->pctrl); 1528 } 1529 1530 static __maybe_unused int msm_pinctrl_resume(struct device *dev) 1531 { 1532 struct msm_pinctrl *pctrl = dev_get_drvdata(dev); 1533 1534 return pinctrl_force_default(pctrl->pctrl); 1535 } 1536 1537 SIMPLE_DEV_PM_OPS(msm_pinctrl_dev_pm_ops, msm_pinctrl_suspend, 1538 msm_pinctrl_resume); 1539 1540 EXPORT_SYMBOL(msm_pinctrl_dev_pm_ops); 1541 1542 int msm_pinctrl_probe(struct platform_device *pdev, 1543 const struct msm_pinctrl_soc_data *soc_data) 1544 { 1545 const struct pinfunction *func; 1546 struct msm_pinctrl *pctrl; 1547 struct resource *res; 1548 int ret; 1549 int i; 1550 1551 pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL); 1552 if (!pctrl) 1553 return -ENOMEM; 1554 1555 pctrl->dev = &pdev->dev; 1556 pctrl->soc = soc_data; 1557 pctrl->chip = msm_gpio_template; 1558 pctrl->intr_target_use_scm = of_device_is_compatible( 1559 pctrl->dev->of_node, 1560 "qcom,ipq8064-pinctrl"); 1561 1562 raw_spin_lock_init(&pctrl->lock); 1563 1564 if (soc_data->tiles) { 1565 for (i = 0; i < soc_data->ntiles; i++) { 1566 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, 1567 soc_data->tiles[i]); 1568 pctrl->regs[i] = devm_ioremap_resource(&pdev->dev, res); 1569 if (IS_ERR(pctrl->regs[i])) 1570 return PTR_ERR(pctrl->regs[i]); 1571 } 1572 } else { 1573 pctrl->regs[0] = devm_platform_get_and_ioremap_resource(pdev, 0, &res); 1574 if (IS_ERR(pctrl->regs[0])) 1575 return PTR_ERR(pctrl->regs[0]); 1576 1577 pctrl->phys_base[0] = res->start; 1578 } 1579 1580 msm_pinctrl_setup_pm_reset(pctrl); 1581 1582 pctrl->irq = platform_get_irq(pdev, 0); 1583 if (pctrl->irq < 0) 1584 return pctrl->irq; 1585 1586 pctrl->desc.owner = THIS_MODULE; 1587 pctrl->desc.pctlops = &msm_pinctrl_ops; 1588 pctrl->desc.pmxops = &msm_pinmux_ops; 1589 pctrl->desc.confops = &msm_pinconf_ops; 1590 pctrl->desc.name = dev_name(&pdev->dev); 1591 pctrl->desc.pins = pctrl->soc->pins; 1592 pctrl->desc.npins = pctrl->soc->npins; 1593 1594 ret = devm_pinctrl_register_and_init(&pdev->dev, &pctrl->desc, 1595 pctrl, &pctrl->pctrl); 1596 if (ret) 1597 return dev_err_probe(&pdev->dev, ret, 1598 "Couldn't register pinctrl driver\n"); 1599 1600 for (i = 0; i < soc_data->nfunctions; i++) { 1601 func = &soc_data->functions[i]; 1602 1603 ret = pinmux_generic_add_pinfunction(pctrl->pctrl, func, NULL); 1604 if (ret < 0) 1605 return ret; 1606 } 1607 1608 ret = pinctrl_enable(pctrl->pctrl); 1609 if (ret) 1610 return dev_err_probe(&pdev->dev, ret, 1611 "Couldn't enable pinctrl driver\n"); 1612 1613 ret = msm_gpio_init(pctrl); 1614 if (ret) 1615 return ret; 1616 1617 platform_set_drvdata(pdev, pctrl); 1618 1619 dev_dbg(&pdev->dev, "Probed Qualcomm pinctrl driver\n"); 1620 1621 return 0; 1622 } 1623 EXPORT_SYMBOL(msm_pinctrl_probe); 1624 1625 MODULE_DESCRIPTION("Qualcomm Technologies, Inc. TLMM driver"); 1626 MODULE_LICENSE("GPL v2"); 1627