1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Intel pinctrl/GPIO core driver. 4 * 5 * Copyright (C) 2015, Intel Corporation 6 * Authors: Mathias Nyman <mathias.nyman@linux.intel.com> 7 * Mika Westerberg <mika.westerberg@linux.intel.com> 8 */ 9 10 #include <linux/acpi.h> 11 #include <linux/module.h> 12 #include <linux/interrupt.h> 13 #include <linux/gpio/driver.h> 14 #include <linux/log2.h> 15 #include <linux/platform_device.h> 16 #include <linux/property.h> 17 18 #include <linux/pinctrl/pinctrl.h> 19 #include <linux/pinctrl/pinmux.h> 20 #include <linux/pinctrl/pinconf.h> 21 #include <linux/pinctrl/pinconf-generic.h> 22 23 #include "../core.h" 24 #include "pinctrl-intel.h" 25 26 /* Offset from regs */ 27 #define REVID 0x000 28 #define REVID_SHIFT 16 29 #define REVID_MASK GENMASK(31, 16) 30 31 #define PADBAR 0x00c 32 #define GPI_IS 0x100 33 34 #define PADOWN_BITS 4 35 #define PADOWN_SHIFT(p) ((p) % 8 * PADOWN_BITS) 36 #define PADOWN_MASK(p) (0xf << PADOWN_SHIFT(p)) 37 #define PADOWN_GPP(p) ((p) / 8) 38 39 /* Offset from pad_regs */ 40 #define PADCFG0 0x000 41 #define PADCFG0_RXEVCFG_SHIFT 25 42 #define PADCFG0_RXEVCFG_MASK (3 << PADCFG0_RXEVCFG_SHIFT) 43 #define PADCFG0_RXEVCFG_LEVEL 0 44 #define PADCFG0_RXEVCFG_EDGE 1 45 #define PADCFG0_RXEVCFG_DISABLED 2 46 #define PADCFG0_RXEVCFG_EDGE_BOTH 3 47 #define PADCFG0_PREGFRXSEL BIT(24) 48 #define PADCFG0_RXINV BIT(23) 49 #define PADCFG0_GPIROUTIOXAPIC BIT(20) 50 #define PADCFG0_GPIROUTSCI BIT(19) 51 #define PADCFG0_GPIROUTSMI BIT(18) 52 #define PADCFG0_GPIROUTNMI BIT(17) 53 #define PADCFG0_PMODE_SHIFT 10 54 #define PADCFG0_PMODE_MASK (0xf << PADCFG0_PMODE_SHIFT) 55 #define PADCFG0_GPIORXDIS BIT(9) 56 #define PADCFG0_GPIOTXDIS BIT(8) 57 #define PADCFG0_GPIORXSTATE BIT(1) 58 #define PADCFG0_GPIOTXSTATE BIT(0) 59 60 #define PADCFG1 0x004 61 #define PADCFG1_TERM_UP BIT(13) 62 #define PADCFG1_TERM_SHIFT 10 63 #define PADCFG1_TERM_MASK (7 << PADCFG1_TERM_SHIFT) 64 #define PADCFG1_TERM_20K 4 65 #define PADCFG1_TERM_2K 3 66 #define PADCFG1_TERM_5K 2 67 #define PADCFG1_TERM_1K 1 68 69 #define PADCFG2 0x008 70 #define PADCFG2_DEBEN BIT(0) 71 #define PADCFG2_DEBOUNCE_SHIFT 1 72 #define PADCFG2_DEBOUNCE_MASK GENMASK(4, 1) 73 74 #define DEBOUNCE_PERIOD 31250 /* ns */ 75 76 struct intel_pad_context { 77 u32 padcfg0; 78 u32 padcfg1; 79 u32 padcfg2; 80 }; 81 82 struct intel_community_context { 83 u32 *intmask; 84 u32 *hostown; 85 }; 86 87 struct intel_pinctrl_context { 88 struct intel_pad_context *pads; 89 struct intel_community_context *communities; 90 }; 91 92 /** 93 * struct intel_pinctrl - Intel pinctrl private structure 94 * @dev: Pointer to the device structure 95 * @lock: Lock to serialize register access 96 * @pctldesc: Pin controller description 97 * @pctldev: Pointer to the pin controller device 98 * @chip: GPIO chip in this pin controller 99 * @soc: SoC/PCH specific pin configuration data 100 * @communities: All communities in this pin controller 101 * @ncommunities: Number of communities in this pin controller 102 * @context: Configuration saved over system sleep 103 * @irq: pinctrl/GPIO chip irq number 104 */ 105 struct intel_pinctrl { 106 struct device *dev; 107 raw_spinlock_t lock; 108 struct pinctrl_desc pctldesc; 109 struct pinctrl_dev *pctldev; 110 struct gpio_chip chip; 111 const struct intel_pinctrl_soc_data *soc; 112 struct intel_community *communities; 113 size_t ncommunities; 114 struct intel_pinctrl_context context; 115 int irq; 116 }; 117 118 #define pin_to_padno(c, p) ((p) - (c)->pin_base) 119 #define padgroup_offset(g, p) ((p) - (g)->base) 120 121 static struct intel_community *intel_get_community(struct intel_pinctrl *pctrl, 122 unsigned int pin) 123 { 124 struct intel_community *community; 125 int i; 126 127 for (i = 0; i < pctrl->ncommunities; i++) { 128 community = &pctrl->communities[i]; 129 if (pin >= community->pin_base && 130 pin < community->pin_base + community->npins) 131 return community; 132 } 133 134 dev_warn(pctrl->dev, "failed to find community for pin %u\n", pin); 135 return NULL; 136 } 137 138 static const struct intel_padgroup * 139 intel_community_get_padgroup(const struct intel_community *community, 140 unsigned int pin) 141 { 142 int i; 143 144 for (i = 0; i < community->ngpps; i++) { 145 const struct intel_padgroup *padgrp = &community->gpps[i]; 146 147 if (pin >= padgrp->base && pin < padgrp->base + padgrp->size) 148 return padgrp; 149 } 150 151 return NULL; 152 } 153 154 static void __iomem *intel_get_padcfg(struct intel_pinctrl *pctrl, 155 unsigned int pin, unsigned int reg) 156 { 157 const struct intel_community *community; 158 unsigned int padno; 159 size_t nregs; 160 161 community = intel_get_community(pctrl, pin); 162 if (!community) 163 return NULL; 164 165 padno = pin_to_padno(community, pin); 166 nregs = (community->features & PINCTRL_FEATURE_DEBOUNCE) ? 4 : 2; 167 168 if (reg == PADCFG2 && !(community->features & PINCTRL_FEATURE_DEBOUNCE)) 169 return NULL; 170 171 return community->pad_regs + reg + padno * nregs * 4; 172 } 173 174 static bool intel_pad_owned_by_host(struct intel_pinctrl *pctrl, unsigned int pin) 175 { 176 const struct intel_community *community; 177 const struct intel_padgroup *padgrp; 178 unsigned int gpp, offset, gpp_offset; 179 void __iomem *padown; 180 181 community = intel_get_community(pctrl, pin); 182 if (!community) 183 return false; 184 if (!community->padown_offset) 185 return true; 186 187 padgrp = intel_community_get_padgroup(community, pin); 188 if (!padgrp) 189 return false; 190 191 gpp_offset = padgroup_offset(padgrp, pin); 192 gpp = PADOWN_GPP(gpp_offset); 193 offset = community->padown_offset + padgrp->padown_num * 4 + gpp * 4; 194 padown = community->regs + offset; 195 196 return !(readl(padown) & PADOWN_MASK(gpp_offset)); 197 } 198 199 static bool intel_pad_acpi_mode(struct intel_pinctrl *pctrl, unsigned int pin) 200 { 201 const struct intel_community *community; 202 const struct intel_padgroup *padgrp; 203 unsigned int offset, gpp_offset; 204 void __iomem *hostown; 205 206 community = intel_get_community(pctrl, pin); 207 if (!community) 208 return true; 209 if (!community->hostown_offset) 210 return false; 211 212 padgrp = intel_community_get_padgroup(community, pin); 213 if (!padgrp) 214 return true; 215 216 gpp_offset = padgroup_offset(padgrp, pin); 217 offset = community->hostown_offset + padgrp->reg_num * 4; 218 hostown = community->regs + offset; 219 220 return !(readl(hostown) & BIT(gpp_offset)); 221 } 222 223 static bool intel_pad_locked(struct intel_pinctrl *pctrl, unsigned int pin) 224 { 225 struct intel_community *community; 226 const struct intel_padgroup *padgrp; 227 unsigned int offset, gpp_offset; 228 u32 value; 229 230 community = intel_get_community(pctrl, pin); 231 if (!community) 232 return true; 233 if (!community->padcfglock_offset) 234 return false; 235 236 padgrp = intel_community_get_padgroup(community, pin); 237 if (!padgrp) 238 return true; 239 240 gpp_offset = padgroup_offset(padgrp, pin); 241 242 /* 243 * If PADCFGLOCK and PADCFGLOCKTX bits are both clear for this pad, 244 * the pad is considered unlocked. Any other case means that it is 245 * either fully or partially locked and we don't touch it. 246 */ 247 offset = community->padcfglock_offset + padgrp->reg_num * 8; 248 value = readl(community->regs + offset); 249 if (value & BIT(gpp_offset)) 250 return true; 251 252 offset = community->padcfglock_offset + 4 + padgrp->reg_num * 8; 253 value = readl(community->regs + offset); 254 if (value & BIT(gpp_offset)) 255 return true; 256 257 return false; 258 } 259 260 static bool intel_pad_usable(struct intel_pinctrl *pctrl, unsigned int pin) 261 { 262 return intel_pad_owned_by_host(pctrl, pin) && 263 !intel_pad_locked(pctrl, pin); 264 } 265 266 static int intel_get_groups_count(struct pinctrl_dev *pctldev) 267 { 268 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); 269 270 return pctrl->soc->ngroups; 271 } 272 273 static const char *intel_get_group_name(struct pinctrl_dev *pctldev, 274 unsigned int group) 275 { 276 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); 277 278 return pctrl->soc->groups[group].name; 279 } 280 281 static int intel_get_group_pins(struct pinctrl_dev *pctldev, unsigned int group, 282 const unsigned int **pins, unsigned int *npins) 283 { 284 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); 285 286 *pins = pctrl->soc->groups[group].pins; 287 *npins = pctrl->soc->groups[group].npins; 288 return 0; 289 } 290 291 static void intel_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s, 292 unsigned int pin) 293 { 294 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); 295 void __iomem *padcfg; 296 u32 cfg0, cfg1, mode; 297 bool locked, acpi; 298 299 if (!intel_pad_owned_by_host(pctrl, pin)) { 300 seq_puts(s, "not available"); 301 return; 302 } 303 304 cfg0 = readl(intel_get_padcfg(pctrl, pin, PADCFG0)); 305 cfg1 = readl(intel_get_padcfg(pctrl, pin, PADCFG1)); 306 307 mode = (cfg0 & PADCFG0_PMODE_MASK) >> PADCFG0_PMODE_SHIFT; 308 if (!mode) 309 seq_puts(s, "GPIO "); 310 else 311 seq_printf(s, "mode %d ", mode); 312 313 seq_printf(s, "0x%08x 0x%08x", cfg0, cfg1); 314 315 /* Dump the additional PADCFG registers if available */ 316 padcfg = intel_get_padcfg(pctrl, pin, PADCFG2); 317 if (padcfg) 318 seq_printf(s, " 0x%08x", readl(padcfg)); 319 320 locked = intel_pad_locked(pctrl, pin); 321 acpi = intel_pad_acpi_mode(pctrl, pin); 322 323 if (locked || acpi) { 324 seq_puts(s, " ["); 325 if (locked) { 326 seq_puts(s, "LOCKED"); 327 if (acpi) 328 seq_puts(s, ", "); 329 } 330 if (acpi) 331 seq_puts(s, "ACPI"); 332 seq_puts(s, "]"); 333 } 334 } 335 336 static const struct pinctrl_ops intel_pinctrl_ops = { 337 .get_groups_count = intel_get_groups_count, 338 .get_group_name = intel_get_group_name, 339 .get_group_pins = intel_get_group_pins, 340 .pin_dbg_show = intel_pin_dbg_show, 341 }; 342 343 static int intel_get_functions_count(struct pinctrl_dev *pctldev) 344 { 345 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); 346 347 return pctrl->soc->nfunctions; 348 } 349 350 static const char *intel_get_function_name(struct pinctrl_dev *pctldev, 351 unsigned int function) 352 { 353 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); 354 355 return pctrl->soc->functions[function].name; 356 } 357 358 static int intel_get_function_groups(struct pinctrl_dev *pctldev, 359 unsigned int function, 360 const char * const **groups, 361 unsigned int * const ngroups) 362 { 363 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); 364 365 *groups = pctrl->soc->functions[function].groups; 366 *ngroups = pctrl->soc->functions[function].ngroups; 367 return 0; 368 } 369 370 static int intel_pinmux_set_mux(struct pinctrl_dev *pctldev, 371 unsigned int function, unsigned int group) 372 { 373 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); 374 const struct intel_pingroup *grp = &pctrl->soc->groups[group]; 375 unsigned long flags; 376 int i; 377 378 raw_spin_lock_irqsave(&pctrl->lock, flags); 379 380 /* 381 * All pins in the groups needs to be accessible and writable 382 * before we can enable the mux for this group. 383 */ 384 for (i = 0; i < grp->npins; i++) { 385 if (!intel_pad_usable(pctrl, grp->pins[i])) { 386 raw_spin_unlock_irqrestore(&pctrl->lock, flags); 387 return -EBUSY; 388 } 389 } 390 391 /* Now enable the mux setting for each pin in the group */ 392 for (i = 0; i < grp->npins; i++) { 393 void __iomem *padcfg0; 394 u32 value; 395 396 padcfg0 = intel_get_padcfg(pctrl, grp->pins[i], PADCFG0); 397 value = readl(padcfg0); 398 399 value &= ~PADCFG0_PMODE_MASK; 400 401 if (grp->modes) 402 value |= grp->modes[i] << PADCFG0_PMODE_SHIFT; 403 else 404 value |= grp->mode << PADCFG0_PMODE_SHIFT; 405 406 writel(value, padcfg0); 407 } 408 409 raw_spin_unlock_irqrestore(&pctrl->lock, flags); 410 411 return 0; 412 } 413 414 static void __intel_gpio_set_direction(void __iomem *padcfg0, bool input) 415 { 416 u32 value; 417 418 value = readl(padcfg0); 419 if (input) { 420 value &= ~PADCFG0_GPIORXDIS; 421 value |= PADCFG0_GPIOTXDIS; 422 } else { 423 value &= ~PADCFG0_GPIOTXDIS; 424 value |= PADCFG0_GPIORXDIS; 425 } 426 writel(value, padcfg0); 427 } 428 429 static void intel_gpio_set_gpio_mode(void __iomem *padcfg0) 430 { 431 u32 value; 432 433 /* Put the pad into GPIO mode */ 434 value = readl(padcfg0) & ~PADCFG0_PMODE_MASK; 435 /* Disable SCI/SMI/NMI generation */ 436 value &= ~(PADCFG0_GPIROUTIOXAPIC | PADCFG0_GPIROUTSCI); 437 value &= ~(PADCFG0_GPIROUTSMI | PADCFG0_GPIROUTNMI); 438 writel(value, padcfg0); 439 } 440 441 static int intel_gpio_request_enable(struct pinctrl_dev *pctldev, 442 struct pinctrl_gpio_range *range, 443 unsigned int pin) 444 { 445 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); 446 void __iomem *padcfg0; 447 unsigned long flags; 448 449 raw_spin_lock_irqsave(&pctrl->lock, flags); 450 451 if (!intel_pad_usable(pctrl, pin)) { 452 raw_spin_unlock_irqrestore(&pctrl->lock, flags); 453 return -EBUSY; 454 } 455 456 padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0); 457 intel_gpio_set_gpio_mode(padcfg0); 458 /* Disable TX buffer and enable RX (this will be input) */ 459 __intel_gpio_set_direction(padcfg0, true); 460 461 raw_spin_unlock_irqrestore(&pctrl->lock, flags); 462 463 return 0; 464 } 465 466 static int intel_gpio_set_direction(struct pinctrl_dev *pctldev, 467 struct pinctrl_gpio_range *range, 468 unsigned int pin, bool input) 469 { 470 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); 471 void __iomem *padcfg0; 472 unsigned long flags; 473 474 raw_spin_lock_irqsave(&pctrl->lock, flags); 475 476 padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0); 477 __intel_gpio_set_direction(padcfg0, input); 478 479 raw_spin_unlock_irqrestore(&pctrl->lock, flags); 480 481 return 0; 482 } 483 484 static const struct pinmux_ops intel_pinmux_ops = { 485 .get_functions_count = intel_get_functions_count, 486 .get_function_name = intel_get_function_name, 487 .get_function_groups = intel_get_function_groups, 488 .set_mux = intel_pinmux_set_mux, 489 .gpio_request_enable = intel_gpio_request_enable, 490 .gpio_set_direction = intel_gpio_set_direction, 491 }; 492 493 static int intel_config_get(struct pinctrl_dev *pctldev, unsigned int pin, 494 unsigned long *config) 495 { 496 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); 497 enum pin_config_param param = pinconf_to_config_param(*config); 498 const struct intel_community *community; 499 u32 value, term; 500 u32 arg = 0; 501 502 if (!intel_pad_owned_by_host(pctrl, pin)) 503 return -ENOTSUPP; 504 505 community = intel_get_community(pctrl, pin); 506 value = readl(intel_get_padcfg(pctrl, pin, PADCFG1)); 507 term = (value & PADCFG1_TERM_MASK) >> PADCFG1_TERM_SHIFT; 508 509 switch (param) { 510 case PIN_CONFIG_BIAS_DISABLE: 511 if (term) 512 return -EINVAL; 513 break; 514 515 case PIN_CONFIG_BIAS_PULL_UP: 516 if (!term || !(value & PADCFG1_TERM_UP)) 517 return -EINVAL; 518 519 switch (term) { 520 case PADCFG1_TERM_1K: 521 arg = 1000; 522 break; 523 case PADCFG1_TERM_2K: 524 arg = 2000; 525 break; 526 case PADCFG1_TERM_5K: 527 arg = 5000; 528 break; 529 case PADCFG1_TERM_20K: 530 arg = 20000; 531 break; 532 } 533 534 break; 535 536 case PIN_CONFIG_BIAS_PULL_DOWN: 537 if (!term || value & PADCFG1_TERM_UP) 538 return -EINVAL; 539 540 switch (term) { 541 case PADCFG1_TERM_1K: 542 if (!(community->features & PINCTRL_FEATURE_1K_PD)) 543 return -EINVAL; 544 arg = 1000; 545 break; 546 case PADCFG1_TERM_5K: 547 arg = 5000; 548 break; 549 case PADCFG1_TERM_20K: 550 arg = 20000; 551 break; 552 } 553 554 break; 555 556 case PIN_CONFIG_INPUT_DEBOUNCE: { 557 void __iomem *padcfg2; 558 u32 v; 559 560 padcfg2 = intel_get_padcfg(pctrl, pin, PADCFG2); 561 if (!padcfg2) 562 return -ENOTSUPP; 563 564 v = readl(padcfg2); 565 if (!(v & PADCFG2_DEBEN)) 566 return -EINVAL; 567 568 v = (v & PADCFG2_DEBOUNCE_MASK) >> PADCFG2_DEBOUNCE_SHIFT; 569 arg = BIT(v) * DEBOUNCE_PERIOD / 1000; 570 571 break; 572 } 573 574 default: 575 return -ENOTSUPP; 576 } 577 578 *config = pinconf_to_config_packed(param, arg); 579 return 0; 580 } 581 582 static int intel_config_set_pull(struct intel_pinctrl *pctrl, unsigned int pin, 583 unsigned long config) 584 { 585 unsigned int param = pinconf_to_config_param(config); 586 unsigned int arg = pinconf_to_config_argument(config); 587 const struct intel_community *community; 588 void __iomem *padcfg1; 589 unsigned long flags; 590 int ret = 0; 591 u32 value; 592 593 raw_spin_lock_irqsave(&pctrl->lock, flags); 594 595 community = intel_get_community(pctrl, pin); 596 padcfg1 = intel_get_padcfg(pctrl, pin, PADCFG1); 597 value = readl(padcfg1); 598 599 switch (param) { 600 case PIN_CONFIG_BIAS_DISABLE: 601 value &= ~(PADCFG1_TERM_MASK | PADCFG1_TERM_UP); 602 break; 603 604 case PIN_CONFIG_BIAS_PULL_UP: 605 value &= ~PADCFG1_TERM_MASK; 606 607 value |= PADCFG1_TERM_UP; 608 609 switch (arg) { 610 case 20000: 611 value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT; 612 break; 613 case 5000: 614 value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT; 615 break; 616 case 2000: 617 value |= PADCFG1_TERM_2K << PADCFG1_TERM_SHIFT; 618 break; 619 case 1000: 620 value |= PADCFG1_TERM_1K << PADCFG1_TERM_SHIFT; 621 break; 622 default: 623 ret = -EINVAL; 624 } 625 626 break; 627 628 case PIN_CONFIG_BIAS_PULL_DOWN: 629 value &= ~(PADCFG1_TERM_UP | PADCFG1_TERM_MASK); 630 631 switch (arg) { 632 case 20000: 633 value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT; 634 break; 635 case 5000: 636 value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT; 637 break; 638 case 1000: 639 if (!(community->features & PINCTRL_FEATURE_1K_PD)) { 640 ret = -EINVAL; 641 break; 642 } 643 value |= PADCFG1_TERM_1K << PADCFG1_TERM_SHIFT; 644 break; 645 default: 646 ret = -EINVAL; 647 } 648 649 break; 650 } 651 652 if (!ret) 653 writel(value, padcfg1); 654 655 raw_spin_unlock_irqrestore(&pctrl->lock, flags); 656 657 return ret; 658 } 659 660 static int intel_config_set_debounce(struct intel_pinctrl *pctrl, 661 unsigned int pin, unsigned int debounce) 662 { 663 void __iomem *padcfg0, *padcfg2; 664 unsigned long flags; 665 u32 value0, value2; 666 int ret = 0; 667 668 padcfg2 = intel_get_padcfg(pctrl, pin, PADCFG2); 669 if (!padcfg2) 670 return -ENOTSUPP; 671 672 padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0); 673 674 raw_spin_lock_irqsave(&pctrl->lock, flags); 675 676 value0 = readl(padcfg0); 677 value2 = readl(padcfg2); 678 679 /* Disable glitch filter and debouncer */ 680 value0 &= ~PADCFG0_PREGFRXSEL; 681 value2 &= ~(PADCFG2_DEBEN | PADCFG2_DEBOUNCE_MASK); 682 683 if (debounce) { 684 unsigned long v; 685 686 v = order_base_2(debounce * 1000 / DEBOUNCE_PERIOD); 687 if (v < 3 || v > 15) { 688 ret = -EINVAL; 689 goto exit_unlock; 690 } else { 691 /* Enable glitch filter and debouncer */ 692 value0 |= PADCFG0_PREGFRXSEL; 693 value2 |= v << PADCFG2_DEBOUNCE_SHIFT; 694 value2 |= PADCFG2_DEBEN; 695 } 696 } 697 698 writel(value0, padcfg0); 699 writel(value2, padcfg2); 700 701 exit_unlock: 702 raw_spin_unlock_irqrestore(&pctrl->lock, flags); 703 704 return ret; 705 } 706 707 static int intel_config_set(struct pinctrl_dev *pctldev, unsigned int pin, 708 unsigned long *configs, unsigned int nconfigs) 709 { 710 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); 711 int i, ret; 712 713 if (!intel_pad_usable(pctrl, pin)) 714 return -ENOTSUPP; 715 716 for (i = 0; i < nconfigs; i++) { 717 switch (pinconf_to_config_param(configs[i])) { 718 case PIN_CONFIG_BIAS_DISABLE: 719 case PIN_CONFIG_BIAS_PULL_UP: 720 case PIN_CONFIG_BIAS_PULL_DOWN: 721 ret = intel_config_set_pull(pctrl, pin, configs[i]); 722 if (ret) 723 return ret; 724 break; 725 726 case PIN_CONFIG_INPUT_DEBOUNCE: 727 ret = intel_config_set_debounce(pctrl, pin, 728 pinconf_to_config_argument(configs[i])); 729 if (ret) 730 return ret; 731 break; 732 733 default: 734 return -ENOTSUPP; 735 } 736 } 737 738 return 0; 739 } 740 741 static const struct pinconf_ops intel_pinconf_ops = { 742 .is_generic = true, 743 .pin_config_get = intel_config_get, 744 .pin_config_set = intel_config_set, 745 }; 746 747 static const struct pinctrl_desc intel_pinctrl_desc = { 748 .pctlops = &intel_pinctrl_ops, 749 .pmxops = &intel_pinmux_ops, 750 .confops = &intel_pinconf_ops, 751 .owner = THIS_MODULE, 752 }; 753 754 /** 755 * intel_gpio_to_pin() - Translate from GPIO offset to pin number 756 * @pctrl: Pinctrl structure 757 * @offset: GPIO offset from gpiolib 758 * @community: Community is filled here if not %NULL 759 * @padgrp: Pad group is filled here if not %NULL 760 * 761 * When coming through gpiolib irqchip, the GPIO offset is not 762 * automatically translated to pinctrl pin number. This function can be 763 * used to find out the corresponding pinctrl pin. 764 */ 765 static int intel_gpio_to_pin(struct intel_pinctrl *pctrl, unsigned int offset, 766 const struct intel_community **community, 767 const struct intel_padgroup **padgrp) 768 { 769 int i; 770 771 for (i = 0; i < pctrl->ncommunities; i++) { 772 const struct intel_community *comm = &pctrl->communities[i]; 773 int j; 774 775 for (j = 0; j < comm->ngpps; j++) { 776 const struct intel_padgroup *pgrp = &comm->gpps[j]; 777 778 if (pgrp->gpio_base < 0) 779 continue; 780 781 if (offset >= pgrp->gpio_base && 782 offset < pgrp->gpio_base + pgrp->size) { 783 int pin; 784 785 pin = pgrp->base + offset - pgrp->gpio_base; 786 if (community) 787 *community = comm; 788 if (padgrp) 789 *padgrp = pgrp; 790 791 return pin; 792 } 793 } 794 } 795 796 return -EINVAL; 797 } 798 799 static int intel_gpio_get(struct gpio_chip *chip, unsigned int offset) 800 { 801 struct intel_pinctrl *pctrl = gpiochip_get_data(chip); 802 void __iomem *reg; 803 u32 padcfg0; 804 int pin; 805 806 pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL); 807 if (pin < 0) 808 return -EINVAL; 809 810 reg = intel_get_padcfg(pctrl, pin, PADCFG0); 811 if (!reg) 812 return -EINVAL; 813 814 padcfg0 = readl(reg); 815 if (!(padcfg0 & PADCFG0_GPIOTXDIS)) 816 return !!(padcfg0 & PADCFG0_GPIOTXSTATE); 817 818 return !!(padcfg0 & PADCFG0_GPIORXSTATE); 819 } 820 821 static void intel_gpio_set(struct gpio_chip *chip, unsigned int offset, 822 int value) 823 { 824 struct intel_pinctrl *pctrl = gpiochip_get_data(chip); 825 unsigned long flags; 826 void __iomem *reg; 827 u32 padcfg0; 828 int pin; 829 830 pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL); 831 if (pin < 0) 832 return; 833 834 reg = intel_get_padcfg(pctrl, pin, PADCFG0); 835 if (!reg) 836 return; 837 838 raw_spin_lock_irqsave(&pctrl->lock, flags); 839 padcfg0 = readl(reg); 840 if (value) 841 padcfg0 |= PADCFG0_GPIOTXSTATE; 842 else 843 padcfg0 &= ~PADCFG0_GPIOTXSTATE; 844 writel(padcfg0, reg); 845 raw_spin_unlock_irqrestore(&pctrl->lock, flags); 846 } 847 848 static int intel_gpio_get_direction(struct gpio_chip *chip, unsigned int offset) 849 { 850 struct intel_pinctrl *pctrl = gpiochip_get_data(chip); 851 void __iomem *reg; 852 u32 padcfg0; 853 int pin; 854 855 pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL); 856 if (pin < 0) 857 return -EINVAL; 858 859 reg = intel_get_padcfg(pctrl, pin, PADCFG0); 860 if (!reg) 861 return -EINVAL; 862 863 padcfg0 = readl(reg); 864 865 if (padcfg0 & PADCFG0_PMODE_MASK) 866 return -EINVAL; 867 868 return !!(padcfg0 & PADCFG0_GPIOTXDIS); 869 } 870 871 static int intel_gpio_direction_input(struct gpio_chip *chip, unsigned int offset) 872 { 873 return pinctrl_gpio_direction_input(chip->base + offset); 874 } 875 876 static int intel_gpio_direction_output(struct gpio_chip *chip, unsigned int offset, 877 int value) 878 { 879 intel_gpio_set(chip, offset, value); 880 return pinctrl_gpio_direction_output(chip->base + offset); 881 } 882 883 static const struct gpio_chip intel_gpio_chip = { 884 .owner = THIS_MODULE, 885 .request = gpiochip_generic_request, 886 .free = gpiochip_generic_free, 887 .get_direction = intel_gpio_get_direction, 888 .direction_input = intel_gpio_direction_input, 889 .direction_output = intel_gpio_direction_output, 890 .get = intel_gpio_get, 891 .set = intel_gpio_set, 892 .set_config = gpiochip_generic_config, 893 }; 894 895 static void intel_gpio_irq_ack(struct irq_data *d) 896 { 897 struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 898 struct intel_pinctrl *pctrl = gpiochip_get_data(gc); 899 const struct intel_community *community; 900 const struct intel_padgroup *padgrp; 901 int pin; 902 903 pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), &community, &padgrp); 904 if (pin >= 0) { 905 unsigned int gpp, gpp_offset, is_offset; 906 907 gpp = padgrp->reg_num; 908 gpp_offset = padgroup_offset(padgrp, pin); 909 is_offset = community->is_offset + gpp * 4; 910 911 raw_spin_lock(&pctrl->lock); 912 writel(BIT(gpp_offset), community->regs + is_offset); 913 raw_spin_unlock(&pctrl->lock); 914 } 915 } 916 917 static void intel_gpio_irq_enable(struct irq_data *d) 918 { 919 struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 920 struct intel_pinctrl *pctrl = gpiochip_get_data(gc); 921 const struct intel_community *community; 922 const struct intel_padgroup *padgrp; 923 int pin; 924 925 pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), &community, &padgrp); 926 if (pin >= 0) { 927 unsigned int gpp, gpp_offset, is_offset; 928 unsigned long flags; 929 u32 value; 930 931 gpp = padgrp->reg_num; 932 gpp_offset = padgroup_offset(padgrp, pin); 933 is_offset = community->is_offset + gpp * 4; 934 935 raw_spin_lock_irqsave(&pctrl->lock, flags); 936 /* Clear interrupt status first to avoid unexpected interrupt */ 937 writel(BIT(gpp_offset), community->regs + is_offset); 938 939 value = readl(community->regs + community->ie_offset + gpp * 4); 940 value |= BIT(gpp_offset); 941 writel(value, community->regs + community->ie_offset + gpp * 4); 942 raw_spin_unlock_irqrestore(&pctrl->lock, flags); 943 } 944 } 945 946 static void intel_gpio_irq_mask_unmask(struct irq_data *d, bool mask) 947 { 948 struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 949 struct intel_pinctrl *pctrl = gpiochip_get_data(gc); 950 const struct intel_community *community; 951 const struct intel_padgroup *padgrp; 952 int pin; 953 954 pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), &community, &padgrp); 955 if (pin >= 0) { 956 unsigned int gpp, gpp_offset; 957 unsigned long flags; 958 void __iomem *reg; 959 u32 value; 960 961 gpp = padgrp->reg_num; 962 gpp_offset = padgroup_offset(padgrp, pin); 963 964 reg = community->regs + community->ie_offset + gpp * 4; 965 966 raw_spin_lock_irqsave(&pctrl->lock, flags); 967 value = readl(reg); 968 if (mask) 969 value &= ~BIT(gpp_offset); 970 else 971 value |= BIT(gpp_offset); 972 writel(value, reg); 973 raw_spin_unlock_irqrestore(&pctrl->lock, flags); 974 } 975 } 976 977 static void intel_gpio_irq_mask(struct irq_data *d) 978 { 979 intel_gpio_irq_mask_unmask(d, true); 980 } 981 982 static void intel_gpio_irq_unmask(struct irq_data *d) 983 { 984 intel_gpio_irq_mask_unmask(d, false); 985 } 986 987 static int intel_gpio_irq_type(struct irq_data *d, unsigned int type) 988 { 989 struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 990 struct intel_pinctrl *pctrl = gpiochip_get_data(gc); 991 unsigned int pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), NULL, NULL); 992 unsigned long flags; 993 void __iomem *reg; 994 u32 value; 995 996 reg = intel_get_padcfg(pctrl, pin, PADCFG0); 997 if (!reg) 998 return -EINVAL; 999 1000 /* 1001 * If the pin is in ACPI mode it is still usable as a GPIO but it 1002 * cannot be used as IRQ because GPI_IS status bit will not be 1003 * updated by the host controller hardware. 1004 */ 1005 if (intel_pad_acpi_mode(pctrl, pin)) { 1006 dev_warn(pctrl->dev, "pin %u cannot be used as IRQ\n", pin); 1007 return -EPERM; 1008 } 1009 1010 raw_spin_lock_irqsave(&pctrl->lock, flags); 1011 1012 intel_gpio_set_gpio_mode(reg); 1013 1014 value = readl(reg); 1015 1016 value &= ~(PADCFG0_RXEVCFG_MASK | PADCFG0_RXINV); 1017 1018 if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) { 1019 value |= PADCFG0_RXEVCFG_EDGE_BOTH << PADCFG0_RXEVCFG_SHIFT; 1020 } else if (type & IRQ_TYPE_EDGE_FALLING) { 1021 value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT; 1022 value |= PADCFG0_RXINV; 1023 } else if (type & IRQ_TYPE_EDGE_RISING) { 1024 value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT; 1025 } else if (type & IRQ_TYPE_LEVEL_MASK) { 1026 if (type & IRQ_TYPE_LEVEL_LOW) 1027 value |= PADCFG0_RXINV; 1028 } else { 1029 value |= PADCFG0_RXEVCFG_DISABLED << PADCFG0_RXEVCFG_SHIFT; 1030 } 1031 1032 writel(value, reg); 1033 1034 if (type & IRQ_TYPE_EDGE_BOTH) 1035 irq_set_handler_locked(d, handle_edge_irq); 1036 else if (type & IRQ_TYPE_LEVEL_MASK) 1037 irq_set_handler_locked(d, handle_level_irq); 1038 1039 raw_spin_unlock_irqrestore(&pctrl->lock, flags); 1040 1041 return 0; 1042 } 1043 1044 static int intel_gpio_irq_wake(struct irq_data *d, unsigned int on) 1045 { 1046 struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 1047 struct intel_pinctrl *pctrl = gpiochip_get_data(gc); 1048 unsigned int pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), NULL, NULL); 1049 1050 if (on) 1051 enable_irq_wake(pctrl->irq); 1052 else 1053 disable_irq_wake(pctrl->irq); 1054 1055 dev_dbg(pctrl->dev, "%sable wake for pin %u\n", on ? "en" : "dis", pin); 1056 return 0; 1057 } 1058 1059 static irqreturn_t intel_gpio_community_irq_handler(struct intel_pinctrl *pctrl, 1060 const struct intel_community *community) 1061 { 1062 struct gpio_chip *gc = &pctrl->chip; 1063 irqreturn_t ret = IRQ_NONE; 1064 int gpp; 1065 1066 for (gpp = 0; gpp < community->ngpps; gpp++) { 1067 const struct intel_padgroup *padgrp = &community->gpps[gpp]; 1068 unsigned long pending, enabled, gpp_offset; 1069 1070 pending = readl(community->regs + community->is_offset + 1071 padgrp->reg_num * 4); 1072 enabled = readl(community->regs + community->ie_offset + 1073 padgrp->reg_num * 4); 1074 1075 /* Only interrupts that are enabled */ 1076 pending &= enabled; 1077 1078 for_each_set_bit(gpp_offset, &pending, padgrp->size) { 1079 unsigned irq; 1080 1081 irq = irq_find_mapping(gc->irq.domain, 1082 padgrp->gpio_base + gpp_offset); 1083 generic_handle_irq(irq); 1084 1085 ret |= IRQ_HANDLED; 1086 } 1087 } 1088 1089 return ret; 1090 } 1091 1092 static irqreturn_t intel_gpio_irq(int irq, void *data) 1093 { 1094 const struct intel_community *community; 1095 struct intel_pinctrl *pctrl = data; 1096 irqreturn_t ret = IRQ_NONE; 1097 int i; 1098 1099 /* Need to check all communities for pending interrupts */ 1100 for (i = 0; i < pctrl->ncommunities; i++) { 1101 community = &pctrl->communities[i]; 1102 ret |= intel_gpio_community_irq_handler(pctrl, community); 1103 } 1104 1105 return ret; 1106 } 1107 1108 static struct irq_chip intel_gpio_irqchip = { 1109 .name = "intel-gpio", 1110 .irq_enable = intel_gpio_irq_enable, 1111 .irq_ack = intel_gpio_irq_ack, 1112 .irq_mask = intel_gpio_irq_mask, 1113 .irq_unmask = intel_gpio_irq_unmask, 1114 .irq_set_type = intel_gpio_irq_type, 1115 .irq_set_wake = intel_gpio_irq_wake, 1116 .flags = IRQCHIP_MASK_ON_SUSPEND, 1117 }; 1118 1119 static int intel_gpio_add_pin_ranges(struct intel_pinctrl *pctrl, 1120 const struct intel_community *community) 1121 { 1122 int ret = 0, i; 1123 1124 for (i = 0; i < community->ngpps; i++) { 1125 const struct intel_padgroup *gpp = &community->gpps[i]; 1126 1127 if (gpp->gpio_base < 0) 1128 continue; 1129 1130 ret = gpiochip_add_pin_range(&pctrl->chip, dev_name(pctrl->dev), 1131 gpp->gpio_base, gpp->base, 1132 gpp->size); 1133 if (ret) 1134 return ret; 1135 } 1136 1137 return ret; 1138 } 1139 1140 static unsigned intel_gpio_ngpio(const struct intel_pinctrl *pctrl) 1141 { 1142 const struct intel_community *community; 1143 unsigned int ngpio = 0; 1144 int i, j; 1145 1146 for (i = 0; i < pctrl->ncommunities; i++) { 1147 community = &pctrl->communities[i]; 1148 for (j = 0; j < community->ngpps; j++) { 1149 const struct intel_padgroup *gpp = &community->gpps[j]; 1150 1151 if (gpp->gpio_base < 0) 1152 continue; 1153 1154 if (gpp->gpio_base + gpp->size > ngpio) 1155 ngpio = gpp->gpio_base + gpp->size; 1156 } 1157 } 1158 1159 return ngpio; 1160 } 1161 1162 static int intel_gpio_probe(struct intel_pinctrl *pctrl, int irq) 1163 { 1164 int ret, i; 1165 1166 pctrl->chip = intel_gpio_chip; 1167 1168 pctrl->chip.ngpio = intel_gpio_ngpio(pctrl); 1169 pctrl->chip.label = dev_name(pctrl->dev); 1170 pctrl->chip.parent = pctrl->dev; 1171 pctrl->chip.base = -1; 1172 pctrl->irq = irq; 1173 1174 ret = devm_gpiochip_add_data(pctrl->dev, &pctrl->chip, pctrl); 1175 if (ret) { 1176 dev_err(pctrl->dev, "failed to register gpiochip\n"); 1177 return ret; 1178 } 1179 1180 for (i = 0; i < pctrl->ncommunities; i++) { 1181 struct intel_community *community = &pctrl->communities[i]; 1182 1183 ret = intel_gpio_add_pin_ranges(pctrl, community); 1184 if (ret) { 1185 dev_err(pctrl->dev, "failed to add GPIO pin range\n"); 1186 return ret; 1187 } 1188 } 1189 1190 /* 1191 * We need to request the interrupt here (instead of providing chip 1192 * to the irq directly) because on some platforms several GPIO 1193 * controllers share the same interrupt line. 1194 */ 1195 ret = devm_request_irq(pctrl->dev, irq, intel_gpio_irq, 1196 IRQF_SHARED | IRQF_NO_THREAD, 1197 dev_name(pctrl->dev), pctrl); 1198 if (ret) { 1199 dev_err(pctrl->dev, "failed to request interrupt\n"); 1200 return ret; 1201 } 1202 1203 ret = gpiochip_irqchip_add(&pctrl->chip, &intel_gpio_irqchip, 0, 1204 handle_bad_irq, IRQ_TYPE_NONE); 1205 if (ret) { 1206 dev_err(pctrl->dev, "failed to add irqchip\n"); 1207 return ret; 1208 } 1209 1210 gpiochip_set_chained_irqchip(&pctrl->chip, &intel_gpio_irqchip, irq, 1211 NULL); 1212 return 0; 1213 } 1214 1215 static int intel_pinctrl_add_padgroups(struct intel_pinctrl *pctrl, 1216 struct intel_community *community) 1217 { 1218 struct intel_padgroup *gpps; 1219 unsigned int npins = community->npins; 1220 unsigned int padown_num = 0; 1221 size_t ngpps, i; 1222 1223 if (community->gpps) 1224 ngpps = community->ngpps; 1225 else 1226 ngpps = DIV_ROUND_UP(community->npins, community->gpp_size); 1227 1228 gpps = devm_kcalloc(pctrl->dev, ngpps, sizeof(*gpps), GFP_KERNEL); 1229 if (!gpps) 1230 return -ENOMEM; 1231 1232 for (i = 0; i < ngpps; i++) { 1233 if (community->gpps) { 1234 gpps[i] = community->gpps[i]; 1235 } else { 1236 unsigned int gpp_size = community->gpp_size; 1237 1238 gpps[i].reg_num = i; 1239 gpps[i].base = community->pin_base + i * gpp_size; 1240 gpps[i].size = min(gpp_size, npins); 1241 npins -= gpps[i].size; 1242 } 1243 1244 if (gpps[i].size > 32) 1245 return -EINVAL; 1246 1247 if (!gpps[i].gpio_base) 1248 gpps[i].gpio_base = gpps[i].base; 1249 1250 gpps[i].padown_num = padown_num; 1251 1252 /* 1253 * In older hardware the number of padown registers per 1254 * group is fixed regardless of the group size. 1255 */ 1256 if (community->gpp_num_padown_regs) 1257 padown_num += community->gpp_num_padown_regs; 1258 else 1259 padown_num += DIV_ROUND_UP(gpps[i].size * 4, 32); 1260 } 1261 1262 community->ngpps = ngpps; 1263 community->gpps = gpps; 1264 1265 return 0; 1266 } 1267 1268 static int intel_pinctrl_pm_init(struct intel_pinctrl *pctrl) 1269 { 1270 #ifdef CONFIG_PM_SLEEP 1271 const struct intel_pinctrl_soc_data *soc = pctrl->soc; 1272 struct intel_community_context *communities; 1273 struct intel_pad_context *pads; 1274 int i; 1275 1276 pads = devm_kcalloc(pctrl->dev, soc->npins, sizeof(*pads), GFP_KERNEL); 1277 if (!pads) 1278 return -ENOMEM; 1279 1280 communities = devm_kcalloc(pctrl->dev, pctrl->ncommunities, 1281 sizeof(*communities), GFP_KERNEL); 1282 if (!communities) 1283 return -ENOMEM; 1284 1285 1286 for (i = 0; i < pctrl->ncommunities; i++) { 1287 struct intel_community *community = &pctrl->communities[i]; 1288 u32 *intmask, *hostown; 1289 1290 intmask = devm_kcalloc(pctrl->dev, community->ngpps, 1291 sizeof(*intmask), GFP_KERNEL); 1292 if (!intmask) 1293 return -ENOMEM; 1294 1295 communities[i].intmask = intmask; 1296 1297 hostown = devm_kcalloc(pctrl->dev, community->ngpps, 1298 sizeof(*hostown), GFP_KERNEL); 1299 if (!hostown) 1300 return -ENOMEM; 1301 1302 communities[i].hostown = hostown; 1303 } 1304 1305 pctrl->context.pads = pads; 1306 pctrl->context.communities = communities; 1307 #endif 1308 1309 return 0; 1310 } 1311 1312 static int intel_pinctrl_probe(struct platform_device *pdev, 1313 const struct intel_pinctrl_soc_data *soc_data) 1314 { 1315 struct intel_pinctrl *pctrl; 1316 int i, ret, irq; 1317 1318 if (!soc_data) 1319 return -EINVAL; 1320 1321 pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL); 1322 if (!pctrl) 1323 return -ENOMEM; 1324 1325 pctrl->dev = &pdev->dev; 1326 pctrl->soc = soc_data; 1327 raw_spin_lock_init(&pctrl->lock); 1328 1329 /* 1330 * Make a copy of the communities which we can use to hold pointers 1331 * to the registers. 1332 */ 1333 pctrl->ncommunities = pctrl->soc->ncommunities; 1334 pctrl->communities = devm_kcalloc(&pdev->dev, pctrl->ncommunities, 1335 sizeof(*pctrl->communities), GFP_KERNEL); 1336 if (!pctrl->communities) 1337 return -ENOMEM; 1338 1339 for (i = 0; i < pctrl->ncommunities; i++) { 1340 struct intel_community *community = &pctrl->communities[i]; 1341 struct resource *res; 1342 void __iomem *regs; 1343 u32 padbar; 1344 1345 *community = pctrl->soc->communities[i]; 1346 1347 res = platform_get_resource(pdev, IORESOURCE_MEM, 1348 community->barno); 1349 regs = devm_ioremap_resource(&pdev->dev, res); 1350 if (IS_ERR(regs)) 1351 return PTR_ERR(regs); 1352 1353 /* 1354 * Determine community features based on the revision if 1355 * not specified already. 1356 */ 1357 if (!community->features) { 1358 u32 rev; 1359 1360 rev = (readl(regs + REVID) & REVID_MASK) >> REVID_SHIFT; 1361 if (rev >= 0x94) { 1362 community->features |= PINCTRL_FEATURE_DEBOUNCE; 1363 community->features |= PINCTRL_FEATURE_1K_PD; 1364 } 1365 } 1366 1367 /* Read offset of the pad configuration registers */ 1368 padbar = readl(regs + PADBAR); 1369 1370 community->regs = regs; 1371 community->pad_regs = regs + padbar; 1372 1373 if (!community->is_offset) 1374 community->is_offset = GPI_IS; 1375 1376 ret = intel_pinctrl_add_padgroups(pctrl, community); 1377 if (ret) 1378 return ret; 1379 } 1380 1381 irq = platform_get_irq(pdev, 0); 1382 if (irq < 0) { 1383 dev_err(&pdev->dev, "failed to get interrupt number\n"); 1384 return irq; 1385 } 1386 1387 ret = intel_pinctrl_pm_init(pctrl); 1388 if (ret) 1389 return ret; 1390 1391 pctrl->pctldesc = intel_pinctrl_desc; 1392 pctrl->pctldesc.name = dev_name(&pdev->dev); 1393 pctrl->pctldesc.pins = pctrl->soc->pins; 1394 pctrl->pctldesc.npins = pctrl->soc->npins; 1395 1396 pctrl->pctldev = devm_pinctrl_register(&pdev->dev, &pctrl->pctldesc, 1397 pctrl); 1398 if (IS_ERR(pctrl->pctldev)) { 1399 dev_err(&pdev->dev, "failed to register pinctrl driver\n"); 1400 return PTR_ERR(pctrl->pctldev); 1401 } 1402 1403 ret = intel_gpio_probe(pctrl, irq); 1404 if (ret) 1405 return ret; 1406 1407 platform_set_drvdata(pdev, pctrl); 1408 1409 return 0; 1410 } 1411 1412 int intel_pinctrl_probe_by_hid(struct platform_device *pdev) 1413 { 1414 const struct intel_pinctrl_soc_data *data; 1415 1416 data = device_get_match_data(&pdev->dev); 1417 return intel_pinctrl_probe(pdev, data); 1418 } 1419 EXPORT_SYMBOL_GPL(intel_pinctrl_probe_by_hid); 1420 1421 int intel_pinctrl_probe_by_uid(struct platform_device *pdev) 1422 { 1423 const struct intel_pinctrl_soc_data *data = NULL; 1424 const struct intel_pinctrl_soc_data **table; 1425 struct acpi_device *adev; 1426 unsigned int i; 1427 1428 adev = ACPI_COMPANION(&pdev->dev); 1429 if (adev) { 1430 const void *match = device_get_match_data(&pdev->dev); 1431 1432 table = (const struct intel_pinctrl_soc_data **)match; 1433 for (i = 0; table[i]; i++) { 1434 if (!strcmp(adev->pnp.unique_id, table[i]->uid)) { 1435 data = table[i]; 1436 break; 1437 } 1438 } 1439 } else { 1440 const struct platform_device_id *id; 1441 1442 id = platform_get_device_id(pdev); 1443 if (!id) 1444 return -ENODEV; 1445 1446 table = (const struct intel_pinctrl_soc_data **)id->driver_data; 1447 data = table[pdev->id]; 1448 } 1449 if (!data) 1450 return -ENODEV; 1451 1452 return intel_pinctrl_probe(pdev, data); 1453 } 1454 EXPORT_SYMBOL_GPL(intel_pinctrl_probe_by_uid); 1455 1456 #ifdef CONFIG_PM_SLEEP 1457 static bool intel_pinctrl_should_save(struct intel_pinctrl *pctrl, unsigned int pin) 1458 { 1459 const struct pin_desc *pd = pin_desc_get(pctrl->pctldev, pin); 1460 1461 if (!pd || !intel_pad_usable(pctrl, pin)) 1462 return false; 1463 1464 /* 1465 * Only restore the pin if it is actually in use by the kernel (or 1466 * by userspace). It is possible that some pins are used by the 1467 * BIOS during resume and those are not always locked down so leave 1468 * them alone. 1469 */ 1470 if (pd->mux_owner || pd->gpio_owner || 1471 gpiochip_line_is_irq(&pctrl->chip, pin)) 1472 return true; 1473 1474 return false; 1475 } 1476 1477 int intel_pinctrl_suspend_noirq(struct device *dev) 1478 { 1479 struct intel_pinctrl *pctrl = dev_get_drvdata(dev); 1480 struct intel_community_context *communities; 1481 struct intel_pad_context *pads; 1482 int i; 1483 1484 pads = pctrl->context.pads; 1485 for (i = 0; i < pctrl->soc->npins; i++) { 1486 const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i]; 1487 void __iomem *padcfg; 1488 u32 val; 1489 1490 if (!intel_pinctrl_should_save(pctrl, desc->number)) 1491 continue; 1492 1493 val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG0)); 1494 pads[i].padcfg0 = val & ~PADCFG0_GPIORXSTATE; 1495 val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG1)); 1496 pads[i].padcfg1 = val; 1497 1498 padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG2); 1499 if (padcfg) 1500 pads[i].padcfg2 = readl(padcfg); 1501 } 1502 1503 communities = pctrl->context.communities; 1504 for (i = 0; i < pctrl->ncommunities; i++) { 1505 struct intel_community *community = &pctrl->communities[i]; 1506 void __iomem *base; 1507 unsigned int gpp; 1508 1509 base = community->regs + community->ie_offset; 1510 for (gpp = 0; gpp < community->ngpps; gpp++) 1511 communities[i].intmask[gpp] = readl(base + gpp * 4); 1512 1513 base = community->regs + community->hostown_offset; 1514 for (gpp = 0; gpp < community->ngpps; gpp++) 1515 communities[i].hostown[gpp] = readl(base + gpp * 4); 1516 } 1517 1518 return 0; 1519 } 1520 EXPORT_SYMBOL_GPL(intel_pinctrl_suspend_noirq); 1521 1522 static void intel_gpio_irq_init(struct intel_pinctrl *pctrl) 1523 { 1524 size_t i; 1525 1526 for (i = 0; i < pctrl->ncommunities; i++) { 1527 const struct intel_community *community; 1528 void __iomem *base; 1529 unsigned int gpp; 1530 1531 community = &pctrl->communities[i]; 1532 base = community->regs; 1533 1534 for (gpp = 0; gpp < community->ngpps; gpp++) { 1535 /* Mask and clear all interrupts */ 1536 writel(0, base + community->ie_offset + gpp * 4); 1537 writel(0xffff, base + community->is_offset + gpp * 4); 1538 } 1539 } 1540 } 1541 1542 static u32 1543 intel_gpio_is_requested(struct gpio_chip *chip, int base, unsigned int size) 1544 { 1545 u32 requested = 0; 1546 unsigned int i; 1547 1548 for (i = 0; i < size; i++) 1549 if (gpiochip_is_requested(chip, base + i)) 1550 requested |= BIT(i); 1551 1552 return requested; 1553 } 1554 1555 static u32 1556 intel_gpio_update_pad_mode(void __iomem *hostown, u32 mask, u32 value) 1557 { 1558 u32 curr, updated; 1559 1560 curr = readl(hostown); 1561 updated = (curr & ~mask) | (value & mask); 1562 writel(updated, hostown); 1563 1564 return curr; 1565 } 1566 1567 int intel_pinctrl_resume_noirq(struct device *dev) 1568 { 1569 struct intel_pinctrl *pctrl = dev_get_drvdata(dev); 1570 const struct intel_community_context *communities; 1571 const struct intel_pad_context *pads; 1572 int i; 1573 1574 /* Mask all interrupts */ 1575 intel_gpio_irq_init(pctrl); 1576 1577 pads = pctrl->context.pads; 1578 for (i = 0; i < pctrl->soc->npins; i++) { 1579 const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i]; 1580 void __iomem *padcfg; 1581 u32 val; 1582 1583 if (!intel_pinctrl_should_save(pctrl, desc->number)) 1584 continue; 1585 1586 padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG0); 1587 val = readl(padcfg) & ~PADCFG0_GPIORXSTATE; 1588 if (val != pads[i].padcfg0) { 1589 writel(pads[i].padcfg0, padcfg); 1590 dev_dbg(dev, "restored pin %u padcfg0 %#08x\n", 1591 desc->number, readl(padcfg)); 1592 } 1593 1594 padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG1); 1595 val = readl(padcfg); 1596 if (val != pads[i].padcfg1) { 1597 writel(pads[i].padcfg1, padcfg); 1598 dev_dbg(dev, "restored pin %u padcfg1 %#08x\n", 1599 desc->number, readl(padcfg)); 1600 } 1601 1602 padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG2); 1603 if (padcfg) { 1604 val = readl(padcfg); 1605 if (val != pads[i].padcfg2) { 1606 writel(pads[i].padcfg2, padcfg); 1607 dev_dbg(dev, "restored pin %u padcfg2 %#08x\n", 1608 desc->number, readl(padcfg)); 1609 } 1610 } 1611 } 1612 1613 communities = pctrl->context.communities; 1614 for (i = 0; i < pctrl->ncommunities; i++) { 1615 struct intel_community *community = &pctrl->communities[i]; 1616 void __iomem *base; 1617 unsigned int gpp; 1618 1619 base = community->regs + community->ie_offset; 1620 for (gpp = 0; gpp < community->ngpps; gpp++) { 1621 writel(communities[i].intmask[gpp], base + gpp * 4); 1622 dev_dbg(dev, "restored mask %d/%u %#08x\n", i, gpp, 1623 readl(base + gpp * 4)); 1624 } 1625 1626 base = community->regs + community->hostown_offset; 1627 for (gpp = 0; gpp < community->ngpps; gpp++) { 1628 const struct intel_padgroup *padgrp = &community->gpps[gpp]; 1629 u32 requested = 0, value = 0; 1630 u32 saved = communities[i].hostown[gpp]; 1631 1632 if (padgrp->gpio_base < 0) 1633 continue; 1634 1635 requested = intel_gpio_is_requested(&pctrl->chip, 1636 padgrp->gpio_base, padgrp->size); 1637 value = intel_gpio_update_pad_mode(base + gpp * 4, 1638 requested, saved); 1639 if ((value ^ saved) & requested) { 1640 dev_warn(dev, "restore hostown %d/%u %#8x->%#8x\n", 1641 i, gpp, value, saved); 1642 } 1643 } 1644 } 1645 1646 return 0; 1647 } 1648 EXPORT_SYMBOL_GPL(intel_pinctrl_resume_noirq); 1649 #endif 1650 1651 MODULE_AUTHOR("Mathias Nyman <mathias.nyman@linux.intel.com>"); 1652 MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>"); 1653 MODULE_DESCRIPTION("Intel pinctrl/GPIO core driver"); 1654 MODULE_LICENSE("GPL v2"); 1655