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 d = d->parent_data; 1016 1017 if (d) 1018 d->chip->irq_eoi(d); 1019 } 1020 1021 static bool msm_gpio_needs_dual_edge_parent_workaround(struct irq_data *d, 1022 unsigned int type) 1023 { 1024 struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 1025 struct msm_pinctrl *pctrl = gpiochip_get_data(gc); 1026 1027 return type == IRQ_TYPE_EDGE_BOTH && 1028 pctrl->soc->wakeirq_dual_edge_errata && d->parent_data && 1029 test_bit(d->hwirq, pctrl->skip_wake_irqs); 1030 } 1031 1032 static void msm_gpio_irq_init_valid_mask(struct gpio_chip *gc, 1033 unsigned long *valid_mask, 1034 unsigned int ngpios) 1035 { 1036 struct msm_pinctrl *pctrl = gpiochip_get_data(gc); 1037 const struct msm_pingroup *g; 1038 int i; 1039 1040 bitmap_fill(valid_mask, ngpios); 1041 1042 for (i = 0; i < ngpios; i++) { 1043 g = &pctrl->soc->groups[i]; 1044 1045 if (g->intr_detection_width != 1 && 1046 g->intr_detection_width != 2) 1047 clear_bit(i, valid_mask); 1048 } 1049 } 1050 1051 static int msm_gpio_irq_set_type(struct irq_data *d, unsigned int type) 1052 { 1053 struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 1054 struct msm_pinctrl *pctrl = gpiochip_get_data(gc); 1055 const struct msm_pingroup *g; 1056 u32 intr_target_mask = GENMASK(2, 0); 1057 unsigned long flags; 1058 u32 val, oldval; 1059 1060 if (msm_gpio_needs_dual_edge_parent_workaround(d, type)) { 1061 set_bit(d->hwirq, pctrl->dual_edge_irqs); 1062 irq_set_handler_locked(d, handle_fasteoi_ack_irq); 1063 msm_gpio_update_dual_edge_parent(d); 1064 return 0; 1065 } 1066 1067 if (d->parent_data) 1068 irq_chip_set_type_parent(d, type); 1069 1070 if (test_bit(d->hwirq, pctrl->skip_wake_irqs)) { 1071 clear_bit(d->hwirq, pctrl->dual_edge_irqs); 1072 irq_set_handler_locked(d, handle_fasteoi_irq); 1073 return 0; 1074 } 1075 1076 g = &pctrl->soc->groups[d->hwirq]; 1077 1078 raw_spin_lock_irqsave(&pctrl->lock, flags); 1079 1080 /* 1081 * For hw without possibility of detecting both edges 1082 */ 1083 if (g->intr_detection_width == 1 && type == IRQ_TYPE_EDGE_BOTH) 1084 set_bit(d->hwirq, pctrl->dual_edge_irqs); 1085 else 1086 clear_bit(d->hwirq, pctrl->dual_edge_irqs); 1087 1088 /* Route interrupts to application cpu. 1089 * With intr_target_use_scm interrupts are routed to 1090 * application cpu using scm calls. 1091 */ 1092 if (g->intr_target_width) 1093 intr_target_mask = GENMASK(g->intr_target_width - 1, 0); 1094 1095 if (pctrl->intr_target_use_scm) { 1096 u32 reg = g->intr_target_reg ? g->intr_target_reg : g->intr_cfg_reg; 1097 u32 addr = pctrl->phys_base[0] + reg; 1098 int ret; 1099 1100 qcom_scm_io_readl(addr, &val); 1101 val &= ~(intr_target_mask << g->intr_target_bit); 1102 val |= g->intr_target_kpss_val << g->intr_target_bit; 1103 1104 ret = qcom_scm_io_writel(addr, val); 1105 if (ret) 1106 dev_err(pctrl->dev, 1107 "Failed routing %lu interrupt to Apps proc", 1108 d->hwirq); 1109 } else { 1110 val = msm_readl_intr_target(pctrl, g); 1111 val &= ~(intr_target_mask << g->intr_target_bit); 1112 val |= g->intr_target_kpss_val << g->intr_target_bit; 1113 msm_writel_intr_target(val, pctrl, g); 1114 } 1115 1116 /* Update configuration for gpio. 1117 * RAW_STATUS_EN is left on for all gpio irqs. Due to the 1118 * internal circuitry of TLMM, toggling the RAW_STATUS 1119 * could cause the INTR_STATUS to be set for EDGE interrupts. 1120 */ 1121 val = oldval = msm_readl_intr_cfg(pctrl, g); 1122 val |= BIT(g->intr_raw_status_bit); 1123 if (g->intr_detection_width == 2) { 1124 val &= ~(3 << g->intr_detection_bit); 1125 val &= ~(1 << g->intr_polarity_bit); 1126 switch (type) { 1127 case IRQ_TYPE_EDGE_RISING: 1128 val |= 1 << g->intr_detection_bit; 1129 val |= BIT(g->intr_polarity_bit); 1130 break; 1131 case IRQ_TYPE_EDGE_FALLING: 1132 val |= 2 << g->intr_detection_bit; 1133 val |= BIT(g->intr_polarity_bit); 1134 break; 1135 case IRQ_TYPE_EDGE_BOTH: 1136 val |= 3 << g->intr_detection_bit; 1137 val |= BIT(g->intr_polarity_bit); 1138 break; 1139 case IRQ_TYPE_LEVEL_LOW: 1140 break; 1141 case IRQ_TYPE_LEVEL_HIGH: 1142 val |= BIT(g->intr_polarity_bit); 1143 break; 1144 } 1145 } else if (g->intr_detection_width == 1) { 1146 val &= ~(1 << g->intr_detection_bit); 1147 val &= ~(1 << g->intr_polarity_bit); 1148 switch (type) { 1149 case IRQ_TYPE_EDGE_RISING: 1150 val |= BIT(g->intr_detection_bit); 1151 val |= BIT(g->intr_polarity_bit); 1152 break; 1153 case IRQ_TYPE_EDGE_FALLING: 1154 val |= BIT(g->intr_detection_bit); 1155 break; 1156 case IRQ_TYPE_EDGE_BOTH: 1157 val |= BIT(g->intr_detection_bit); 1158 val |= BIT(g->intr_polarity_bit); 1159 break; 1160 case IRQ_TYPE_LEVEL_LOW: 1161 break; 1162 case IRQ_TYPE_LEVEL_HIGH: 1163 val |= BIT(g->intr_polarity_bit); 1164 break; 1165 } 1166 } else { 1167 BUG(); 1168 } 1169 msm_writel_intr_cfg(val, pctrl, g); 1170 1171 /* 1172 * The first time we set RAW_STATUS_EN it could trigger an interrupt. 1173 * Clear the interrupt. This is safe because we have 1174 * IRQCHIP_SET_TYPE_MASKED. When changing the interrupt type, we could 1175 * also still have a non-matching interrupt latched, so clear whenever 1176 * making changes to the interrupt configuration. 1177 */ 1178 if (val != oldval) 1179 msm_ack_intr_status(pctrl, g); 1180 1181 if (test_bit(d->hwirq, pctrl->dual_edge_irqs)) 1182 msm_gpio_update_dual_edge_pos(pctrl, g, d); 1183 1184 raw_spin_unlock_irqrestore(&pctrl->lock, flags); 1185 1186 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH)) 1187 irq_set_handler_locked(d, handle_level_irq); 1188 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING)) 1189 irq_set_handler_locked(d, handle_edge_irq); 1190 1191 return 0; 1192 } 1193 1194 static int msm_gpio_irq_set_wake(struct irq_data *d, unsigned int on) 1195 { 1196 struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 1197 struct msm_pinctrl *pctrl = gpiochip_get_data(gc); 1198 1199 /* 1200 * While they may not wake up when the TLMM is powered off, 1201 * some GPIOs would like to wakeup the system from suspend 1202 * when TLMM is powered on. To allow that, enable the GPIO 1203 * summary line to be wakeup capable at GIC. 1204 */ 1205 if (d->parent_data && test_bit(d->hwirq, pctrl->skip_wake_irqs)) 1206 return irq_chip_set_wake_parent(d, on); 1207 1208 return irq_set_irq_wake(pctrl->irq, on); 1209 } 1210 1211 static int msm_gpio_irq_reqres(struct irq_data *d) 1212 { 1213 struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 1214 struct msm_pinctrl *pctrl = gpiochip_get_data(gc); 1215 const struct msm_pingroup *g = &pctrl->soc->groups[d->hwirq]; 1216 unsigned long flags; 1217 int ret; 1218 1219 if (!try_module_get(gc->owner)) 1220 return -ENODEV; 1221 1222 ret = msm_pinmux_request_gpio(pctrl->pctrl, NULL, d->hwirq); 1223 if (ret) 1224 goto out; 1225 msm_gpio_direction_input(gc, d->hwirq); 1226 1227 if (gpiochip_lock_as_irq(gc, d->hwirq)) { 1228 dev_err(gc->parent, 1229 "unable to lock HW IRQ %lu for IRQ\n", 1230 d->hwirq); 1231 ret = -EINVAL; 1232 goto out; 1233 } 1234 1235 /* 1236 * The disable / clear-enable workaround we do in msm_pinmux_set_mux() 1237 * only works if disable is not lazy since we only clear any bogus 1238 * interrupt in hardware. Explicitly mark the interrupt as UNLAZY. 1239 */ 1240 irq_set_status_flags(d->irq, IRQ_DISABLE_UNLAZY); 1241 1242 /* 1243 * If the wakeup_enable bit is present and marked as available for the 1244 * requested GPIO, it should be enabled when the GPIO is marked as 1245 * wake irq in order to allow the interrupt event to be transfered to 1246 * the PDC HW. 1247 * While the name implies only the wakeup event, it's also required for 1248 * the interrupt event. 1249 */ 1250 if (test_bit(d->hwirq, pctrl->skip_wake_irqs) && g->intr_wakeup_present_bit) { 1251 u32 intr_cfg; 1252 1253 raw_spin_lock_irqsave(&pctrl->lock, flags); 1254 1255 intr_cfg = msm_readl_intr_cfg(pctrl, g); 1256 if (intr_cfg & BIT(g->intr_wakeup_present_bit)) { 1257 intr_cfg |= BIT(g->intr_wakeup_enable_bit); 1258 msm_writel_intr_cfg(intr_cfg, pctrl, g); 1259 } 1260 1261 raw_spin_unlock_irqrestore(&pctrl->lock, flags); 1262 } 1263 1264 return 0; 1265 out: 1266 module_put(gc->owner); 1267 return ret; 1268 } 1269 1270 static void msm_gpio_irq_relres(struct irq_data *d) 1271 { 1272 struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 1273 struct msm_pinctrl *pctrl = gpiochip_get_data(gc); 1274 const struct msm_pingroup *g = &pctrl->soc->groups[d->hwirq]; 1275 unsigned long flags; 1276 1277 /* Disable the wakeup_enable bit if it has been set in msm_gpio_irq_reqres() */ 1278 if (test_bit(d->hwirq, pctrl->skip_wake_irqs) && g->intr_wakeup_present_bit) { 1279 u32 intr_cfg; 1280 1281 raw_spin_lock_irqsave(&pctrl->lock, flags); 1282 1283 intr_cfg = msm_readl_intr_cfg(pctrl, g); 1284 if (intr_cfg & BIT(g->intr_wakeup_present_bit)) { 1285 intr_cfg &= ~BIT(g->intr_wakeup_enable_bit); 1286 msm_writel_intr_cfg(intr_cfg, pctrl, g); 1287 } 1288 1289 raw_spin_unlock_irqrestore(&pctrl->lock, flags); 1290 } 1291 1292 gpiochip_unlock_as_irq(gc, d->hwirq); 1293 module_put(gc->owner); 1294 } 1295 1296 static int msm_gpio_irq_set_affinity(struct irq_data *d, 1297 const struct cpumask *dest, bool force) 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_affinity_parent(d, dest, force); 1304 1305 return -EINVAL; 1306 } 1307 1308 static int msm_gpio_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu_info) 1309 { 1310 struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 1311 struct msm_pinctrl *pctrl = gpiochip_get_data(gc); 1312 1313 if (d->parent_data && test_bit(d->hwirq, pctrl->skip_wake_irqs)) 1314 return irq_chip_set_vcpu_affinity_parent(d, vcpu_info); 1315 1316 return -EINVAL; 1317 } 1318 1319 static void msm_gpio_irq_handler(struct irq_desc *desc) 1320 { 1321 struct gpio_chip *gc = irq_desc_get_handler_data(desc); 1322 const struct msm_pingroup *g; 1323 struct msm_pinctrl *pctrl = gpiochip_get_data(gc); 1324 struct irq_chip *chip = irq_desc_get_chip(desc); 1325 int handled = 0; 1326 u32 val; 1327 int i; 1328 1329 chained_irq_enter(chip, desc); 1330 1331 /* 1332 * Each pin has it's own IRQ status register, so use 1333 * enabled_irq bitmap to limit the number of reads. 1334 */ 1335 for_each_set_bit(i, pctrl->enabled_irqs, pctrl->chip.ngpio) { 1336 g = &pctrl->soc->groups[i]; 1337 val = msm_readl_intr_status(pctrl, g); 1338 if (val & BIT(g->intr_status_bit)) { 1339 generic_handle_domain_irq(gc->irq.domain, i); 1340 handled++; 1341 } 1342 } 1343 1344 /* No interrupts were flagged */ 1345 if (handled == 0) 1346 handle_bad_irq(desc); 1347 1348 chained_irq_exit(chip, desc); 1349 } 1350 1351 static int msm_gpio_wakeirq(struct gpio_chip *gc, 1352 unsigned int child, 1353 unsigned int child_type, 1354 unsigned int *parent, 1355 unsigned int *parent_type) 1356 { 1357 struct msm_pinctrl *pctrl = gpiochip_get_data(gc); 1358 const struct msm_gpio_wakeirq_map *map; 1359 int i; 1360 1361 *parent = GPIO_NO_WAKE_IRQ; 1362 *parent_type = IRQ_TYPE_EDGE_RISING; 1363 1364 for (i = 0; i < pctrl->soc->nwakeirq_map; i++) { 1365 map = &pctrl->soc->wakeirq_map[i]; 1366 if (map->gpio == child) { 1367 *parent = map->wakeirq; 1368 break; 1369 } 1370 } 1371 1372 return 0; 1373 } 1374 1375 static bool msm_gpio_needs_valid_mask(struct msm_pinctrl *pctrl) 1376 { 1377 if (pctrl->soc->reserved_gpios) 1378 return true; 1379 1380 return device_property_count_u16(pctrl->dev, "gpios") > 0; 1381 } 1382 1383 static const struct irq_chip msm_gpio_irq_chip = { 1384 .name = "msmgpio", 1385 .irq_enable = msm_gpio_irq_enable, 1386 .irq_disable = msm_gpio_irq_disable, 1387 .irq_mask = msm_gpio_irq_mask, 1388 .irq_unmask = msm_gpio_irq_unmask, 1389 .irq_ack = msm_gpio_irq_ack, 1390 .irq_eoi = msm_gpio_irq_eoi, 1391 .irq_set_type = msm_gpio_irq_set_type, 1392 .irq_set_wake = msm_gpio_irq_set_wake, 1393 .irq_request_resources = msm_gpio_irq_reqres, 1394 .irq_release_resources = msm_gpio_irq_relres, 1395 .irq_set_affinity = msm_gpio_irq_set_affinity, 1396 .irq_set_vcpu_affinity = msm_gpio_irq_set_vcpu_affinity, 1397 .flags = (IRQCHIP_MASK_ON_SUSPEND | 1398 IRQCHIP_SET_TYPE_MASKED | 1399 IRQCHIP_ENABLE_WAKEUP_ON_SUSPEND | 1400 IRQCHIP_IMMUTABLE), 1401 }; 1402 1403 static int msm_gpio_init(struct msm_pinctrl *pctrl) 1404 { 1405 struct gpio_chip *chip; 1406 struct gpio_irq_chip *girq; 1407 int i, ret; 1408 unsigned gpio, ngpio = pctrl->soc->ngpios; 1409 struct device_node *np; 1410 bool skip; 1411 1412 if (WARN_ON(ngpio > MAX_NR_GPIO)) 1413 return -EINVAL; 1414 1415 chip = &pctrl->chip; 1416 chip->base = -1; 1417 chip->ngpio = ngpio; 1418 chip->label = dev_name(pctrl->dev); 1419 chip->parent = pctrl->dev; 1420 chip->owner = THIS_MODULE; 1421 if (msm_gpio_needs_valid_mask(pctrl)) 1422 chip->init_valid_mask = msm_gpio_init_valid_mask; 1423 1424 np = of_parse_phandle(pctrl->dev->of_node, "wakeup-parent", 0); 1425 if (np) { 1426 chip->irq.parent_domain = irq_find_matching_host(np, 1427 DOMAIN_BUS_WAKEUP); 1428 of_node_put(np); 1429 if (!chip->irq.parent_domain) 1430 return -EPROBE_DEFER; 1431 chip->irq.child_to_parent_hwirq = msm_gpio_wakeirq; 1432 /* 1433 * Let's skip handling the GPIOs, if the parent irqchip 1434 * is handling the direct connect IRQ of the GPIO. 1435 */ 1436 skip = irq_domain_qcom_handle_wakeup(chip->irq.parent_domain); 1437 for (i = 0; skip && i < pctrl->soc->nwakeirq_map; i++) { 1438 gpio = pctrl->soc->wakeirq_map[i].gpio; 1439 set_bit(gpio, pctrl->skip_wake_irqs); 1440 } 1441 } 1442 1443 girq = &chip->irq; 1444 gpio_irq_chip_set_chip(girq, &msm_gpio_irq_chip); 1445 girq->parent_handler = msm_gpio_irq_handler; 1446 girq->fwnode = dev_fwnode(pctrl->dev); 1447 girq->num_parents = 1; 1448 girq->parents = devm_kcalloc(pctrl->dev, 1, sizeof(*girq->parents), 1449 GFP_KERNEL); 1450 if (!girq->parents) 1451 return -ENOMEM; 1452 girq->default_type = IRQ_TYPE_NONE; 1453 girq->handler = handle_bad_irq; 1454 girq->parents[0] = pctrl->irq; 1455 girq->init_valid_mask = msm_gpio_irq_init_valid_mask; 1456 1457 ret = devm_gpiochip_add_data(pctrl->dev, &pctrl->chip, pctrl); 1458 if (ret) { 1459 dev_err(pctrl->dev, "Failed register gpiochip\n"); 1460 return ret; 1461 } 1462 1463 /* 1464 * For DeviceTree-supported systems, the gpio core checks the 1465 * pinctrl's device node for the "gpio-ranges" property. 1466 * If it is present, it takes care of adding the pin ranges 1467 * for the driver. In this case the driver can skip ahead. 1468 * 1469 * In order to remain compatible with older, existing DeviceTree 1470 * files which don't set the "gpio-ranges" property or systems that 1471 * utilize ACPI the driver has to call gpiochip_add_pin_range(). 1472 */ 1473 if (!of_property_present(pctrl->dev->of_node, "gpio-ranges")) { 1474 ret = gpiochip_add_pin_range(&pctrl->chip, 1475 dev_name(pctrl->dev), 0, 0, chip->ngpio); 1476 if (ret) { 1477 dev_err(pctrl->dev, "Failed to add pin range\n"); 1478 return ret; 1479 } 1480 } 1481 1482 return 0; 1483 } 1484 1485 static int msm_ps_hold_restart(struct sys_off_data *data) 1486 { 1487 struct msm_pinctrl *pctrl = data->cb_data; 1488 1489 writel(0, pctrl->regs[0] + PS_HOLD_OFFSET); 1490 mdelay(1000); 1491 return NOTIFY_DONE; 1492 } 1493 1494 static struct msm_pinctrl *poweroff_pctrl; 1495 1496 static void msm_ps_hold_poweroff(void) 1497 { 1498 struct sys_off_data data = { 1499 .cb_data = poweroff_pctrl, 1500 }; 1501 1502 msm_ps_hold_restart(&data); 1503 } 1504 1505 static void msm_pinctrl_setup_pm_reset(struct msm_pinctrl *pctrl) 1506 { 1507 int i; 1508 const struct pinfunction *func = pctrl->soc->functions; 1509 1510 for (i = 0; i < pctrl->soc->nfunctions; i++) 1511 if (!strcmp(func[i].name, "ps_hold")) { 1512 if (devm_register_sys_off_handler(pctrl->dev, 1513 SYS_OFF_MODE_RESTART, 1514 128, 1515 msm_ps_hold_restart, 1516 pctrl)) 1517 dev_err(pctrl->dev, 1518 "failed to setup restart handler.\n"); 1519 poweroff_pctrl = pctrl; 1520 pm_power_off = msm_ps_hold_poweroff; 1521 break; 1522 } 1523 } 1524 1525 static __maybe_unused int msm_pinctrl_suspend(struct device *dev) 1526 { 1527 struct msm_pinctrl *pctrl = dev_get_drvdata(dev); 1528 1529 return pinctrl_force_sleep(pctrl->pctrl); 1530 } 1531 1532 static __maybe_unused int msm_pinctrl_resume(struct device *dev) 1533 { 1534 struct msm_pinctrl *pctrl = dev_get_drvdata(dev); 1535 1536 return pinctrl_force_default(pctrl->pctrl); 1537 } 1538 1539 SIMPLE_DEV_PM_OPS(msm_pinctrl_dev_pm_ops, msm_pinctrl_suspend, 1540 msm_pinctrl_resume); 1541 1542 EXPORT_SYMBOL(msm_pinctrl_dev_pm_ops); 1543 1544 int msm_pinctrl_probe(struct platform_device *pdev, 1545 const struct msm_pinctrl_soc_data *soc_data) 1546 { 1547 const struct pinfunction *func; 1548 struct msm_pinctrl *pctrl; 1549 struct resource *res; 1550 int ret; 1551 int i; 1552 1553 pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL); 1554 if (!pctrl) 1555 return -ENOMEM; 1556 1557 pctrl->dev = &pdev->dev; 1558 pctrl->soc = soc_data; 1559 pctrl->chip = msm_gpio_template; 1560 pctrl->intr_target_use_scm = of_device_is_compatible( 1561 pctrl->dev->of_node, 1562 "qcom,ipq8064-pinctrl"); 1563 1564 raw_spin_lock_init(&pctrl->lock); 1565 1566 if (soc_data->tiles) { 1567 for (i = 0; i < soc_data->ntiles; i++) { 1568 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, 1569 soc_data->tiles[i]); 1570 pctrl->regs[i] = devm_ioremap_resource(&pdev->dev, res); 1571 if (IS_ERR(pctrl->regs[i])) 1572 return PTR_ERR(pctrl->regs[i]); 1573 } 1574 } else { 1575 pctrl->regs[0] = devm_platform_get_and_ioremap_resource(pdev, 0, &res); 1576 if (IS_ERR(pctrl->regs[0])) 1577 return PTR_ERR(pctrl->regs[0]); 1578 1579 pctrl->phys_base[0] = res->start; 1580 } 1581 1582 msm_pinctrl_setup_pm_reset(pctrl); 1583 1584 pctrl->irq = platform_get_irq(pdev, 0); 1585 if (pctrl->irq < 0) 1586 return pctrl->irq; 1587 1588 pctrl->desc.owner = THIS_MODULE; 1589 pctrl->desc.pctlops = &msm_pinctrl_ops; 1590 pctrl->desc.pmxops = &msm_pinmux_ops; 1591 pctrl->desc.confops = &msm_pinconf_ops; 1592 pctrl->desc.name = dev_name(&pdev->dev); 1593 pctrl->desc.pins = pctrl->soc->pins; 1594 pctrl->desc.npins = pctrl->soc->npins; 1595 1596 pctrl->pctrl = devm_pinctrl_register(&pdev->dev, &pctrl->desc, pctrl); 1597 if (IS_ERR(pctrl->pctrl)) { 1598 dev_err(&pdev->dev, "Couldn't register pinctrl driver\n"); 1599 return PTR_ERR(pctrl->pctrl); 1600 } 1601 1602 for (i = 0; i < soc_data->nfunctions; i++) { 1603 func = &soc_data->functions[i]; 1604 1605 ret = pinmux_generic_add_pinfunction(pctrl->pctrl, func, NULL); 1606 if (ret < 0) 1607 return ret; 1608 } 1609 1610 ret = msm_gpio_init(pctrl); 1611 if (ret) 1612 return ret; 1613 1614 platform_set_drvdata(pdev, pctrl); 1615 1616 dev_dbg(&pdev->dev, "Probed Qualcomm pinctrl driver\n"); 1617 1618 return 0; 1619 } 1620 EXPORT_SYMBOL(msm_pinctrl_probe); 1621 1622 MODULE_DESCRIPTION("Qualcomm Technologies, Inc. TLMM driver"); 1623 MODULE_LICENSE("GPL v2"); 1624