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