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