1 // SPDX-License-Identifier: GPL-2.0
3 * Intel pinctrl/GPIO core driver.
5 * Copyright (C) 2015, Intel Corporation
10 #include <linux/acpi.h>
11 #include <linux/gpio/driver.h>
12 #include <linux/interrupt.h>
13 #include <linux/log2.h>
14 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/property.h>
17 #include <linux/seq_file.h>
18 #include <linux/string_helpers.h>
19 #include <linux/time.h>
21 #include <linux/pinctrl/consumer.h>
22 #include <linux/pinctrl/pinconf.h>
23 #include <linux/pinctrl/pinconf-generic.h>
24 #include <linux/pinctrl/pinctrl.h>
25 #include <linux/pinctrl/pinmux.h>
27 #include <linux/platform_data/x86/pwm-lpss.h>
30 #include "pinctrl-intel.h"
32 /* Offset from regs */
34 #define REVID_SHIFT 16
35 #define REVID_MASK GENMASK(31, 16)
38 #define CAPLIST_ID_SHIFT 16
39 #define CAPLIST_ID_MASK GENMASK(23, 16)
40 #define CAPLIST_ID_GPIO_HW_INFO 1
41 #define CAPLIST_ID_PWM 2
42 #define CAPLIST_ID_BLINK 3
43 #define CAPLIST_ID_EXP 4
44 #define CAPLIST_NEXT_SHIFT 0
45 #define CAPLIST_NEXT_MASK GENMASK(15, 0)
50 #define PADOWN_SHIFT(p) ((p) % 8 * PADOWN_BITS)
51 #define PADOWN_MASK(p) (GENMASK(3, 0) << PADOWN_SHIFT(p))
52 #define PADOWN_GPP(p) ((p) / 8)
56 /* Offset from pad_regs */
58 #define PADCFG0_RXEVCFG_MASK GENMASK(26, 25)
59 #define PADCFG0_RXEVCFG_LEVEL (0 << 25)
60 #define PADCFG0_RXEVCFG_EDGE (1 << 25)
61 #define PADCFG0_RXEVCFG_DISABLED (2 << 25)
62 #define PADCFG0_RXEVCFG_EDGE_BOTH (3 << 25)
63 #define PADCFG0_PREGFRXSEL BIT(24)
64 #define PADCFG0_RXINV BIT(23)
65 #define PADCFG0_GPIROUTIOXAPIC BIT(20)
66 #define PADCFG0_GPIROUTSCI BIT(19)
67 #define PADCFG0_GPIROUTSMI BIT(18)
68 #define PADCFG0_GPIROUTNMI BIT(17)
69 #define PADCFG0_PMODE_SHIFT 10
70 #define PADCFG0_PMODE_MASK GENMASK(13, 10)
71 #define PADCFG0_PMODE_GPIO 0
72 #define PADCFG0_GPIORXDIS BIT(9)
73 #define PADCFG0_GPIOTXDIS BIT(8)
74 #define PADCFG0_GPIORXSTATE BIT(1)
75 #define PADCFG0_GPIOTXSTATE BIT(0)
78 #define PADCFG1_TERM_UP BIT(13)
79 #define PADCFG1_TERM_SHIFT 10
80 #define PADCFG1_TERM_MASK GENMASK(12, 10)
81 #define PADCFG1_TERM_20K BIT(2)
82 #define PADCFG1_TERM_5K BIT(1)
83 #define PADCFG1_TERM_4K (BIT(2) | BIT(1))
84 #define PADCFG1_TERM_1K BIT(0)
85 #define PADCFG1_TERM_952 (BIT(2) | BIT(0))
86 #define PADCFG1_TERM_833 (BIT(1) | BIT(0))
87 #define PADCFG1_TERM_800 (BIT(2) | BIT(1) | BIT(0))
90 #define PADCFG2_DEBOUNCE_SHIFT 1
91 #define PADCFG2_DEBOUNCE_MASK GENMASK(4, 1)
92 #define PADCFG2_DEBEN BIT(0)
94 #define DEBOUNCE_PERIOD_NSEC 31250
96 struct intel_pad_context {
102 struct intel_community_context {
107 #define pin_to_padno(c, p) ((p) - (c)->pin_base)
108 #define padgroup_offset(g, p) ((p) - (g)->base)
110 struct intel_community *intel_get_community(struct intel_pinctrl *pctrl, unsigned int pin)
112 struct intel_community *community;
115 for (i = 0; i < pctrl->ncommunities; i++) {
116 community = &pctrl->communities[i];
117 if (pin >= community->pin_base &&
118 pin < community->pin_base + community->npins)
122 dev_warn(pctrl->dev, "failed to find community for pin %u\n", pin);
125 EXPORT_SYMBOL_NS_GPL(intel_get_community, PINCTRL_INTEL);
127 static const struct intel_padgroup *
128 intel_community_get_padgroup(const struct intel_community *community,
133 for (i = 0; i < community->ngpps; i++) {
134 const struct intel_padgroup *padgrp = &community->gpps[i];
136 if (pin >= padgrp->base && pin < padgrp->base + padgrp->size)
143 static void __iomem *intel_get_padcfg(struct intel_pinctrl *pctrl,
144 unsigned int pin, unsigned int reg)
146 const struct intel_community *community;
150 community = intel_get_community(pctrl, pin);
154 padno = pin_to_padno(community, pin);
155 nregs = (community->features & PINCTRL_FEATURE_DEBOUNCE) ? 4 : 2;
157 if (reg >= nregs * 4)
160 return community->pad_regs + reg + padno * nregs * 4;
163 static bool intel_pad_owned_by_host(struct intel_pinctrl *pctrl, unsigned int pin)
165 const struct intel_community *community;
166 const struct intel_padgroup *padgrp;
167 unsigned int gpp, offset, gpp_offset;
168 void __iomem *padown;
170 community = intel_get_community(pctrl, pin);
173 if (!community->padown_offset)
176 padgrp = intel_community_get_padgroup(community, pin);
180 gpp_offset = padgroup_offset(padgrp, pin);
181 gpp = PADOWN_GPP(gpp_offset);
182 offset = community->padown_offset + padgrp->padown_num * 4 + gpp * 4;
183 padown = community->regs + offset;
185 return !(readl(padown) & PADOWN_MASK(gpp_offset));
188 static bool intel_pad_acpi_mode(struct intel_pinctrl *pctrl, unsigned int pin)
190 const struct intel_community *community;
191 const struct intel_padgroup *padgrp;
192 unsigned int offset, gpp_offset;
193 void __iomem *hostown;
195 community = intel_get_community(pctrl, pin);
198 if (!community->hostown_offset)
201 padgrp = intel_community_get_padgroup(community, pin);
205 gpp_offset = padgroup_offset(padgrp, pin);
206 offset = community->hostown_offset + padgrp->reg_num * 4;
207 hostown = community->regs + offset;
209 return !(readl(hostown) & BIT(gpp_offset));
213 * enum - Locking variants of the pad configuration
215 * @PAD_UNLOCKED: pad is fully controlled by the configuration registers
216 * @PAD_LOCKED: pad configuration registers, except TX state, are locked
217 * @PAD_LOCKED_TX: pad configuration TX state is locked
218 * @PAD_LOCKED_FULL: pad configuration registers are locked completely
220 * Locking is considered as read-only mode for corresponding registers and
221 * their respective fields. That said, TX state bit is locked separately from
222 * the main locking scheme.
228 PAD_LOCKED_FULL = PAD_LOCKED | PAD_LOCKED_TX,
231 static int intel_pad_locked(struct intel_pinctrl *pctrl, unsigned int pin)
233 struct intel_community *community;
234 const struct intel_padgroup *padgrp;
235 unsigned int offset, gpp_offset;
237 int ret = PAD_UNLOCKED;
239 community = intel_get_community(pctrl, pin);
241 return PAD_LOCKED_FULL;
242 if (!community->padcfglock_offset)
245 padgrp = intel_community_get_padgroup(community, pin);
247 return PAD_LOCKED_FULL;
249 gpp_offset = padgroup_offset(padgrp, pin);
252 * If PADCFGLOCK and PADCFGLOCKTX bits are both clear for this pad,
253 * the pad is considered unlocked. Any other case means that it is
254 * either fully or partially locked.
256 offset = community->padcfglock_offset + 0 + padgrp->reg_num * 8;
257 value = readl(community->regs + offset);
258 if (value & BIT(gpp_offset))
261 offset = community->padcfglock_offset + 4 + padgrp->reg_num * 8;
262 value = readl(community->regs + offset);
263 if (value & BIT(gpp_offset))
264 ret |= PAD_LOCKED_TX;
269 static bool intel_pad_is_unlocked(struct intel_pinctrl *pctrl, unsigned int pin)
271 return (intel_pad_locked(pctrl, pin) & PAD_LOCKED) == PAD_UNLOCKED;
274 static bool intel_pad_usable(struct intel_pinctrl *pctrl, unsigned int pin)
276 return intel_pad_owned_by_host(pctrl, pin) && intel_pad_is_unlocked(pctrl, pin);
279 int intel_get_groups_count(struct pinctrl_dev *pctldev)
281 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
283 return pctrl->soc->ngroups;
285 EXPORT_SYMBOL_NS_GPL(intel_get_groups_count, PINCTRL_INTEL);
287 const char *intel_get_group_name(struct pinctrl_dev *pctldev, unsigned int group)
289 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
291 return pctrl->soc->groups[group].grp.name;
293 EXPORT_SYMBOL_NS_GPL(intel_get_group_name, PINCTRL_INTEL);
295 int intel_get_group_pins(struct pinctrl_dev *pctldev, unsigned int group,
296 const unsigned int **pins, unsigned int *npins)
298 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
300 *pins = pctrl->soc->groups[group].grp.pins;
301 *npins = pctrl->soc->groups[group].grp.npins;
304 EXPORT_SYMBOL_NS_GPL(intel_get_group_pins, PINCTRL_INTEL);
306 static void intel_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
309 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
310 void __iomem *padcfg;
311 u32 cfg0, cfg1, mode;
315 if (!intel_pad_owned_by_host(pctrl, pin)) {
316 seq_puts(s, "not available");
320 cfg0 = readl(intel_get_padcfg(pctrl, pin, PADCFG0));
321 cfg1 = readl(intel_get_padcfg(pctrl, pin, PADCFG1));
323 mode = (cfg0 & PADCFG0_PMODE_MASK) >> PADCFG0_PMODE_SHIFT;
324 if (mode == PADCFG0_PMODE_GPIO)
325 seq_puts(s, "GPIO ");
327 seq_printf(s, "mode %d ", mode);
329 seq_printf(s, "0x%08x 0x%08x", cfg0, cfg1);
331 /* Dump the additional PADCFG registers if available */
332 padcfg = intel_get_padcfg(pctrl, pin, PADCFG2);
334 seq_printf(s, " 0x%08x", readl(padcfg));
336 locked = intel_pad_locked(pctrl, pin);
337 acpi = intel_pad_acpi_mode(pctrl, pin);
339 if (locked || acpi) {
342 seq_puts(s, "LOCKED");
343 if ((locked & PAD_LOCKED_FULL) == PAD_LOCKED_TX)
345 else if ((locked & PAD_LOCKED_FULL) == PAD_LOCKED_FULL)
346 seq_puts(s, " full");
357 static const struct pinctrl_ops intel_pinctrl_ops = {
358 .get_groups_count = intel_get_groups_count,
359 .get_group_name = intel_get_group_name,
360 .get_group_pins = intel_get_group_pins,
361 .pin_dbg_show = intel_pin_dbg_show,
364 int intel_get_functions_count(struct pinctrl_dev *pctldev)
366 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
368 return pctrl->soc->nfunctions;
370 EXPORT_SYMBOL_NS_GPL(intel_get_functions_count, PINCTRL_INTEL);
372 const char *intel_get_function_name(struct pinctrl_dev *pctldev, unsigned int function)
374 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
376 return pctrl->soc->functions[function].func.name;
378 EXPORT_SYMBOL_NS_GPL(intel_get_function_name, PINCTRL_INTEL);
380 int intel_get_function_groups(struct pinctrl_dev *pctldev, unsigned int function,
381 const char * const **groups, unsigned int * const ngroups)
383 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
385 *groups = pctrl->soc->functions[function].func.groups;
386 *ngroups = pctrl->soc->functions[function].func.ngroups;
389 EXPORT_SYMBOL_NS_GPL(intel_get_function_groups, PINCTRL_INTEL);
391 static int intel_pinmux_set_mux(struct pinctrl_dev *pctldev,
392 unsigned int function, unsigned int group)
394 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
395 const struct intel_pingroup *grp = &pctrl->soc->groups[group];
399 raw_spin_lock_irqsave(&pctrl->lock, flags);
402 * All pins in the groups needs to be accessible and writable
403 * before we can enable the mux for this group.
405 for (i = 0; i < grp->grp.npins; i++) {
406 if (!intel_pad_usable(pctrl, grp->grp.pins[i])) {
407 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
412 /* Now enable the mux setting for each pin in the group */
413 for (i = 0; i < grp->grp.npins; i++) {
414 void __iomem *padcfg0;
417 padcfg0 = intel_get_padcfg(pctrl, grp->grp.pins[i], PADCFG0);
419 value = readl(padcfg0);
420 value &= ~PADCFG0_PMODE_MASK;
423 pmode = grp->modes[i];
427 value |= pmode << PADCFG0_PMODE_SHIFT;
428 writel(value, padcfg0);
431 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
436 static void __intel_gpio_set_direction(void __iomem *padcfg0, bool input)
440 value = readl(padcfg0);
442 value &= ~PADCFG0_GPIORXDIS;
443 value |= PADCFG0_GPIOTXDIS;
445 value &= ~PADCFG0_GPIOTXDIS;
446 value |= PADCFG0_GPIORXDIS;
448 writel(value, padcfg0);
451 static int __intel_gpio_get_gpio_mode(u32 value)
453 return (value & PADCFG0_PMODE_MASK) >> PADCFG0_PMODE_SHIFT;
456 static int intel_gpio_get_gpio_mode(void __iomem *padcfg0)
458 return __intel_gpio_get_gpio_mode(readl(padcfg0));
461 static void intel_gpio_set_gpio_mode(void __iomem *padcfg0)
465 value = readl(padcfg0);
467 /* Put the pad into GPIO mode */
468 value &= ~PADCFG0_PMODE_MASK;
469 value |= PADCFG0_PMODE_GPIO;
471 /* Disable TX buffer and enable RX (this will be input) */
472 value &= ~PADCFG0_GPIORXDIS;
473 value |= PADCFG0_GPIOTXDIS;
475 /* Disable SCI/SMI/NMI generation */
476 value &= ~(PADCFG0_GPIROUTIOXAPIC | PADCFG0_GPIROUTSCI);
477 value &= ~(PADCFG0_GPIROUTSMI | PADCFG0_GPIROUTNMI);
479 writel(value, padcfg0);
482 static int intel_gpio_request_enable(struct pinctrl_dev *pctldev,
483 struct pinctrl_gpio_range *range,
486 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
487 void __iomem *padcfg0;
490 padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
492 raw_spin_lock_irqsave(&pctrl->lock, flags);
494 if (!intel_pad_owned_by_host(pctrl, pin)) {
495 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
499 if (!intel_pad_is_unlocked(pctrl, pin)) {
500 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
505 * If pin is already configured in GPIO mode, we assume that
506 * firmware provides correct settings. In such case we avoid
507 * potential glitches on the pin. Otherwise, for the pin in
508 * alternative mode, consumer has to supply respective flags.
510 if (intel_gpio_get_gpio_mode(padcfg0) == PADCFG0_PMODE_GPIO) {
511 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
515 intel_gpio_set_gpio_mode(padcfg0);
517 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
522 static int intel_gpio_set_direction(struct pinctrl_dev *pctldev,
523 struct pinctrl_gpio_range *range,
524 unsigned int pin, bool input)
526 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
527 void __iomem *padcfg0;
530 padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
532 raw_spin_lock_irqsave(&pctrl->lock, flags);
533 __intel_gpio_set_direction(padcfg0, input);
534 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
539 static const struct pinmux_ops intel_pinmux_ops = {
540 .get_functions_count = intel_get_functions_count,
541 .get_function_name = intel_get_function_name,
542 .get_function_groups = intel_get_function_groups,
543 .set_mux = intel_pinmux_set_mux,
544 .gpio_request_enable = intel_gpio_request_enable,
545 .gpio_set_direction = intel_gpio_set_direction,
548 static int intel_config_get_pull(struct intel_pinctrl *pctrl, unsigned int pin,
549 enum pin_config_param param, u32 *arg)
551 const struct intel_community *community;
552 void __iomem *padcfg1;
556 community = intel_get_community(pctrl, pin);
557 padcfg1 = intel_get_padcfg(pctrl, pin, PADCFG1);
559 raw_spin_lock_irqsave(&pctrl->lock, flags);
560 value = readl(padcfg1);
561 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
563 term = (value & PADCFG1_TERM_MASK) >> PADCFG1_TERM_SHIFT;
566 case PIN_CONFIG_BIAS_DISABLE:
571 case PIN_CONFIG_BIAS_PULL_UP:
572 if (!term || !(value & PADCFG1_TERM_UP))
576 case PADCFG1_TERM_833:
579 case PADCFG1_TERM_1K:
582 case PADCFG1_TERM_4K:
585 case PADCFG1_TERM_5K:
588 case PADCFG1_TERM_20K:
595 case PIN_CONFIG_BIAS_PULL_DOWN:
596 if (!term || value & PADCFG1_TERM_UP)
600 case PADCFG1_TERM_833:
601 if (!(community->features & PINCTRL_FEATURE_1K_PD))
605 case PADCFG1_TERM_1K:
606 if (!(community->features & PINCTRL_FEATURE_1K_PD))
610 case PADCFG1_TERM_4K:
613 case PADCFG1_TERM_5K:
616 case PADCFG1_TERM_20K:
630 static int intel_config_get_debounce(struct intel_pinctrl *pctrl, unsigned int pin,
631 enum pin_config_param param, u32 *arg)
633 void __iomem *padcfg2;
638 padcfg2 = intel_get_padcfg(pctrl, pin, PADCFG2);
642 raw_spin_lock_irqsave(&pctrl->lock, flags);
643 value2 = readl(padcfg2);
644 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
645 if (!(value2 & PADCFG2_DEBEN))
648 v = (value2 & PADCFG2_DEBOUNCE_MASK) >> PADCFG2_DEBOUNCE_SHIFT;
649 *arg = BIT(v) * DEBOUNCE_PERIOD_NSEC / NSEC_PER_USEC;
654 static int intel_config_get(struct pinctrl_dev *pctldev, unsigned int pin,
655 unsigned long *config)
657 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
658 enum pin_config_param param = pinconf_to_config_param(*config);
662 if (!intel_pad_owned_by_host(pctrl, pin))
666 case PIN_CONFIG_BIAS_DISABLE:
667 case PIN_CONFIG_BIAS_PULL_UP:
668 case PIN_CONFIG_BIAS_PULL_DOWN:
669 ret = intel_config_get_pull(pctrl, pin, param, &arg);
674 case PIN_CONFIG_INPUT_DEBOUNCE:
675 ret = intel_config_get_debounce(pctrl, pin, param, &arg);
684 *config = pinconf_to_config_packed(param, arg);
688 static int intel_config_set_pull(struct intel_pinctrl *pctrl, unsigned int pin,
689 unsigned long config)
691 unsigned int param = pinconf_to_config_param(config);
692 unsigned int arg = pinconf_to_config_argument(config);
693 const struct intel_community *community;
694 void __iomem *padcfg1;
699 community = intel_get_community(pctrl, pin);
700 padcfg1 = intel_get_padcfg(pctrl, pin, PADCFG1);
702 raw_spin_lock_irqsave(&pctrl->lock, flags);
704 value = readl(padcfg1);
705 value &= ~(PADCFG1_TERM_MASK | PADCFG1_TERM_UP);
707 /* Set default strength value in case none is given */
712 case PIN_CONFIG_BIAS_DISABLE:
715 case PIN_CONFIG_BIAS_PULL_UP:
718 value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT;
721 value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT;
724 value |= PADCFG1_TERM_4K << PADCFG1_TERM_SHIFT;
727 value |= PADCFG1_TERM_1K << PADCFG1_TERM_SHIFT;
730 value |= PADCFG1_TERM_833 << PADCFG1_TERM_SHIFT;
737 value |= PADCFG1_TERM_UP;
740 case PIN_CONFIG_BIAS_PULL_DOWN:
743 value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT;
746 value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT;
749 value |= PADCFG1_TERM_4K << PADCFG1_TERM_SHIFT;
752 if (!(community->features & PINCTRL_FEATURE_1K_PD)) {
756 value |= PADCFG1_TERM_1K << PADCFG1_TERM_SHIFT;
759 if (!(community->features & PINCTRL_FEATURE_1K_PD)) {
763 value |= PADCFG1_TERM_833 << PADCFG1_TERM_SHIFT;
778 writel(value, padcfg1);
780 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
785 static int intel_config_set_debounce(struct intel_pinctrl *pctrl,
786 unsigned int pin, unsigned int debounce)
788 void __iomem *padcfg0, *padcfg2;
792 padcfg2 = intel_get_padcfg(pctrl, pin, PADCFG2);
796 padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
798 raw_spin_lock_irqsave(&pctrl->lock, flags);
800 value0 = readl(padcfg0);
801 value2 = readl(padcfg2);
803 /* Disable glitch filter and debouncer */
804 value0 &= ~PADCFG0_PREGFRXSEL;
805 value2 &= ~(PADCFG2_DEBEN | PADCFG2_DEBOUNCE_MASK);
810 v = order_base_2(debounce * NSEC_PER_USEC / DEBOUNCE_PERIOD_NSEC);
811 if (v < 3 || v > 15) {
812 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
816 /* Enable glitch filter and debouncer */
817 value0 |= PADCFG0_PREGFRXSEL;
818 value2 |= v << PADCFG2_DEBOUNCE_SHIFT;
819 value2 |= PADCFG2_DEBEN;
822 writel(value0, padcfg0);
823 writel(value2, padcfg2);
825 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
830 static int intel_config_set(struct pinctrl_dev *pctldev, unsigned int pin,
831 unsigned long *configs, unsigned int nconfigs)
833 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
836 if (!intel_pad_usable(pctrl, pin))
839 for (i = 0; i < nconfigs; i++) {
840 switch (pinconf_to_config_param(configs[i])) {
841 case PIN_CONFIG_BIAS_DISABLE:
842 case PIN_CONFIG_BIAS_PULL_UP:
843 case PIN_CONFIG_BIAS_PULL_DOWN:
844 ret = intel_config_set_pull(pctrl, pin, configs[i]);
849 case PIN_CONFIG_INPUT_DEBOUNCE:
850 ret = intel_config_set_debounce(pctrl, pin,
851 pinconf_to_config_argument(configs[i]));
864 static const struct pinconf_ops intel_pinconf_ops = {
866 .pin_config_get = intel_config_get,
867 .pin_config_set = intel_config_set,
870 static const struct pinctrl_desc intel_pinctrl_desc = {
871 .pctlops = &intel_pinctrl_ops,
872 .pmxops = &intel_pinmux_ops,
873 .confops = &intel_pinconf_ops,
874 .owner = THIS_MODULE,
878 * intel_gpio_to_pin() - Translate from GPIO offset to pin number
879 * @pctrl: Pinctrl structure
880 * @offset: GPIO offset from gpiolib
881 * @community: Community is filled here if not %NULL
882 * @padgrp: Pad group is filled here if not %NULL
884 * When coming through gpiolib irqchip, the GPIO offset is not
885 * automatically translated to pinctrl pin number. This function can be
886 * used to find out the corresponding pinctrl pin.
888 * Return: a pin number and pointers to the community and pad group, which
889 * the pin belongs to, or negative error code if translation can't be done.
891 static int intel_gpio_to_pin(struct intel_pinctrl *pctrl, unsigned int offset,
892 const struct intel_community **community,
893 const struct intel_padgroup **padgrp)
897 for (i = 0; i < pctrl->ncommunities; i++) {
898 const struct intel_community *comm = &pctrl->communities[i];
901 for (j = 0; j < comm->ngpps; j++) {
902 const struct intel_padgroup *pgrp = &comm->gpps[j];
904 if (pgrp->gpio_base == INTEL_GPIO_BASE_NOMAP)
907 if (offset >= pgrp->gpio_base &&
908 offset < pgrp->gpio_base + pgrp->size) {
911 pin = pgrp->base + offset - pgrp->gpio_base;
926 * intel_pin_to_gpio() - Translate from pin number to GPIO offset
927 * @pctrl: Pinctrl structure
930 * Translate the pin number of pinctrl to GPIO offset
932 * Return: a GPIO offset, or negative error code if translation can't be done.
934 static __maybe_unused int intel_pin_to_gpio(struct intel_pinctrl *pctrl, int pin)
936 const struct intel_community *community;
937 const struct intel_padgroup *padgrp;
939 community = intel_get_community(pctrl, pin);
943 padgrp = intel_community_get_padgroup(community, pin);
947 return pin - padgrp->base + padgrp->gpio_base;
950 static int intel_gpio_get(struct gpio_chip *chip, unsigned int offset)
952 struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
957 pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL);
961 reg = intel_get_padcfg(pctrl, pin, PADCFG0);
965 padcfg0 = readl(reg);
966 if (!(padcfg0 & PADCFG0_GPIOTXDIS))
967 return !!(padcfg0 & PADCFG0_GPIOTXSTATE);
969 return !!(padcfg0 & PADCFG0_GPIORXSTATE);
972 static void intel_gpio_set(struct gpio_chip *chip, unsigned int offset,
975 struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
981 pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL);
985 reg = intel_get_padcfg(pctrl, pin, PADCFG0);
989 raw_spin_lock_irqsave(&pctrl->lock, flags);
990 padcfg0 = readl(reg);
992 padcfg0 |= PADCFG0_GPIOTXSTATE;
994 padcfg0 &= ~PADCFG0_GPIOTXSTATE;
995 writel(padcfg0, reg);
996 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
999 static int intel_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
1001 struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
1002 unsigned long flags;
1007 pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL);
1011 reg = intel_get_padcfg(pctrl, pin, PADCFG0);
1015 raw_spin_lock_irqsave(&pctrl->lock, flags);
1016 padcfg0 = readl(reg);
1017 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
1018 if (padcfg0 & PADCFG0_PMODE_MASK)
1021 if (padcfg0 & PADCFG0_GPIOTXDIS)
1022 return GPIO_LINE_DIRECTION_IN;
1024 return GPIO_LINE_DIRECTION_OUT;
1027 static int intel_gpio_direction_input(struct gpio_chip *chip, unsigned int offset)
1029 return pinctrl_gpio_direction_input(chip->base + offset);
1032 static int intel_gpio_direction_output(struct gpio_chip *chip, unsigned int offset,
1035 intel_gpio_set(chip, offset, value);
1036 return pinctrl_gpio_direction_output(chip->base + offset);
1039 static const struct gpio_chip intel_gpio_chip = {
1040 .owner = THIS_MODULE,
1041 .request = gpiochip_generic_request,
1042 .free = gpiochip_generic_free,
1043 .get_direction = intel_gpio_get_direction,
1044 .direction_input = intel_gpio_direction_input,
1045 .direction_output = intel_gpio_direction_output,
1046 .get = intel_gpio_get,
1047 .set = intel_gpio_set,
1048 .set_config = gpiochip_generic_config,
1051 static void intel_gpio_irq_ack(struct irq_data *d)
1053 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1054 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
1055 const struct intel_community *community;
1056 const struct intel_padgroup *padgrp;
1059 pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), &community, &padgrp);
1061 unsigned int gpp, gpp_offset, is_offset;
1063 gpp = padgrp->reg_num;
1064 gpp_offset = padgroup_offset(padgrp, pin);
1065 is_offset = community->is_offset + gpp * 4;
1067 raw_spin_lock(&pctrl->lock);
1068 writel(BIT(gpp_offset), community->regs + is_offset);
1069 raw_spin_unlock(&pctrl->lock);
1073 static void intel_gpio_irq_mask_unmask(struct gpio_chip *gc, irq_hw_number_t hwirq, bool mask)
1075 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
1076 const struct intel_community *community;
1077 const struct intel_padgroup *padgrp;
1080 pin = intel_gpio_to_pin(pctrl, hwirq, &community, &padgrp);
1082 unsigned int gpp, gpp_offset;
1083 unsigned long flags;
1084 void __iomem *reg, *is;
1087 gpp = padgrp->reg_num;
1088 gpp_offset = padgroup_offset(padgrp, pin);
1090 reg = community->regs + community->ie_offset + gpp * 4;
1091 is = community->regs + community->is_offset + gpp * 4;
1093 raw_spin_lock_irqsave(&pctrl->lock, flags);
1095 /* Clear interrupt status first to avoid unexpected interrupt */
1096 writel(BIT(gpp_offset), is);
1100 value &= ~BIT(gpp_offset);
1102 value |= BIT(gpp_offset);
1104 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
1108 static void intel_gpio_irq_mask(struct irq_data *d)
1110 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1111 irq_hw_number_t hwirq = irqd_to_hwirq(d);
1113 intel_gpio_irq_mask_unmask(gc, hwirq, true);
1114 gpiochip_disable_irq(gc, hwirq);
1117 static void intel_gpio_irq_unmask(struct irq_data *d)
1119 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1120 irq_hw_number_t hwirq = irqd_to_hwirq(d);
1122 gpiochip_enable_irq(gc, hwirq);
1123 intel_gpio_irq_mask_unmask(gc, hwirq, false);
1126 static int intel_gpio_irq_type(struct irq_data *d, unsigned int type)
1128 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1129 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
1130 unsigned int pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), NULL, NULL);
1131 u32 rxevcfg, rxinv, value;
1132 unsigned long flags;
1135 reg = intel_get_padcfg(pctrl, pin, PADCFG0);
1140 * If the pin is in ACPI mode it is still usable as a GPIO but it
1141 * cannot be used as IRQ because GPI_IS status bit will not be
1142 * updated by the host controller hardware.
1144 if (intel_pad_acpi_mode(pctrl, pin)) {
1145 dev_warn(pctrl->dev, "pin %u cannot be used as IRQ\n", pin);
1149 if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
1150 rxevcfg = PADCFG0_RXEVCFG_EDGE_BOTH;
1151 } else if (type & IRQ_TYPE_EDGE_FALLING) {
1152 rxevcfg = PADCFG0_RXEVCFG_EDGE;
1153 } else if (type & IRQ_TYPE_EDGE_RISING) {
1154 rxevcfg = PADCFG0_RXEVCFG_EDGE;
1155 } else if (type & IRQ_TYPE_LEVEL_MASK) {
1156 rxevcfg = PADCFG0_RXEVCFG_LEVEL;
1158 rxevcfg = PADCFG0_RXEVCFG_DISABLED;
1161 if (type == IRQ_TYPE_EDGE_FALLING || type == IRQ_TYPE_LEVEL_LOW)
1162 rxinv = PADCFG0_RXINV;
1166 raw_spin_lock_irqsave(&pctrl->lock, flags);
1168 intel_gpio_set_gpio_mode(reg);
1172 value = (value & ~PADCFG0_RXEVCFG_MASK) | rxevcfg;
1173 value = (value & ~PADCFG0_RXINV) | rxinv;
1177 if (type & IRQ_TYPE_EDGE_BOTH)
1178 irq_set_handler_locked(d, handle_edge_irq);
1179 else if (type & IRQ_TYPE_LEVEL_MASK)
1180 irq_set_handler_locked(d, handle_level_irq);
1182 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
1187 static int intel_gpio_irq_wake(struct irq_data *d, unsigned int on)
1189 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1190 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
1191 unsigned int pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), NULL, NULL);
1194 enable_irq_wake(pctrl->irq);
1196 disable_irq_wake(pctrl->irq);
1198 dev_dbg(pctrl->dev, "%s wake for pin %u\n", str_enable_disable(on), pin);
1202 static const struct irq_chip intel_gpio_irq_chip = {
1203 .name = "intel-gpio",
1204 .irq_ack = intel_gpio_irq_ack,
1205 .irq_mask = intel_gpio_irq_mask,
1206 .irq_unmask = intel_gpio_irq_unmask,
1207 .irq_set_type = intel_gpio_irq_type,
1208 .irq_set_wake = intel_gpio_irq_wake,
1209 .flags = IRQCHIP_MASK_ON_SUSPEND | IRQCHIP_IMMUTABLE,
1210 GPIOCHIP_IRQ_RESOURCE_HELPERS,
1213 static int intel_gpio_community_irq_handler(struct intel_pinctrl *pctrl,
1214 const struct intel_community *community)
1216 struct gpio_chip *gc = &pctrl->chip;
1220 for (gpp = 0; gpp < community->ngpps; gpp++) {
1221 const struct intel_padgroup *padgrp = &community->gpps[gpp];
1222 unsigned long pending, enabled, gpp_offset;
1224 raw_spin_lock(&pctrl->lock);
1226 pending = readl(community->regs + community->is_offset +
1227 padgrp->reg_num * 4);
1228 enabled = readl(community->regs + community->ie_offset +
1229 padgrp->reg_num * 4);
1231 raw_spin_unlock(&pctrl->lock);
1233 /* Only interrupts that are enabled */
1236 for_each_set_bit(gpp_offset, &pending, padgrp->size)
1237 generic_handle_domain_irq(gc->irq.domain, padgrp->gpio_base + gpp_offset);
1239 ret += pending ? 1 : 0;
1245 static irqreturn_t intel_gpio_irq(int irq, void *data)
1247 const struct intel_community *community;
1248 struct intel_pinctrl *pctrl = data;
1252 /* Need to check all communities for pending interrupts */
1253 for (i = 0; i < pctrl->ncommunities; i++) {
1254 community = &pctrl->communities[i];
1255 ret += intel_gpio_community_irq_handler(pctrl, community);
1258 return IRQ_RETVAL(ret);
1261 static void intel_gpio_irq_init(struct intel_pinctrl *pctrl)
1265 for (i = 0; i < pctrl->ncommunities; i++) {
1266 const struct intel_community *community;
1270 community = &pctrl->communities[i];
1271 base = community->regs;
1273 for (gpp = 0; gpp < community->ngpps; gpp++) {
1274 /* Mask and clear all interrupts */
1275 writel(0, base + community->ie_offset + gpp * 4);
1276 writel(0xffff, base + community->is_offset + gpp * 4);
1281 static int intel_gpio_irq_init_hw(struct gpio_chip *gc)
1283 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
1286 * Make sure the interrupt lines are in a proper state before
1287 * further configuration.
1289 intel_gpio_irq_init(pctrl);
1294 static int intel_gpio_add_community_ranges(struct intel_pinctrl *pctrl,
1295 const struct intel_community *community)
1299 for (i = 0; i < community->ngpps; i++) {
1300 const struct intel_padgroup *gpp = &community->gpps[i];
1302 if (gpp->gpio_base == INTEL_GPIO_BASE_NOMAP)
1305 ret = gpiochip_add_pin_range(&pctrl->chip, dev_name(pctrl->dev),
1306 gpp->gpio_base, gpp->base,
1315 static int intel_gpio_add_pin_ranges(struct gpio_chip *gc)
1317 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
1320 for (i = 0; i < pctrl->ncommunities; i++) {
1321 struct intel_community *community = &pctrl->communities[i];
1323 ret = intel_gpio_add_community_ranges(pctrl, community);
1325 dev_err(pctrl->dev, "failed to add GPIO pin range\n");
1333 static unsigned int intel_gpio_ngpio(const struct intel_pinctrl *pctrl)
1335 const struct intel_community *community;
1336 unsigned int ngpio = 0;
1339 for (i = 0; i < pctrl->ncommunities; i++) {
1340 community = &pctrl->communities[i];
1341 for (j = 0; j < community->ngpps; j++) {
1342 const struct intel_padgroup *gpp = &community->gpps[j];
1344 if (gpp->gpio_base == INTEL_GPIO_BASE_NOMAP)
1347 if (gpp->gpio_base + gpp->size > ngpio)
1348 ngpio = gpp->gpio_base + gpp->size;
1355 static int intel_gpio_probe(struct intel_pinctrl *pctrl, int irq)
1358 struct gpio_irq_chip *girq;
1360 pctrl->chip = intel_gpio_chip;
1362 /* Setup GPIO chip */
1363 pctrl->chip.ngpio = intel_gpio_ngpio(pctrl);
1364 pctrl->chip.label = dev_name(pctrl->dev);
1365 pctrl->chip.parent = pctrl->dev;
1366 pctrl->chip.base = -1;
1367 pctrl->chip.add_pin_ranges = intel_gpio_add_pin_ranges;
1371 * On some platforms several GPIO controllers share the same interrupt
1374 ret = devm_request_irq(pctrl->dev, irq, intel_gpio_irq,
1375 IRQF_SHARED | IRQF_NO_THREAD,
1376 dev_name(pctrl->dev), pctrl);
1378 dev_err(pctrl->dev, "failed to request interrupt\n");
1382 /* Setup IRQ chip */
1383 girq = &pctrl->chip.irq;
1384 gpio_irq_chip_set_chip(girq, &intel_gpio_irq_chip);
1385 /* This will let us handle the IRQ in the driver */
1386 girq->parent_handler = NULL;
1387 girq->num_parents = 0;
1388 girq->default_type = IRQ_TYPE_NONE;
1389 girq->handler = handle_bad_irq;
1390 girq->init_hw = intel_gpio_irq_init_hw;
1392 ret = devm_gpiochip_add_data(pctrl->dev, &pctrl->chip, pctrl);
1394 dev_err(pctrl->dev, "failed to register gpiochip\n");
1401 static int intel_pinctrl_add_padgroups_by_gpps(struct intel_pinctrl *pctrl,
1402 struct intel_community *community)
1404 struct intel_padgroup *gpps;
1405 unsigned int padown_num = 0;
1406 size_t i, ngpps = community->ngpps;
1408 gpps = devm_kcalloc(pctrl->dev, ngpps, sizeof(*gpps), GFP_KERNEL);
1412 for (i = 0; i < ngpps; i++) {
1413 gpps[i] = community->gpps[i];
1415 if (gpps[i].size > INTEL_PINCTRL_MAX_GPP_SIZE)
1418 /* Special treatment for GPIO base */
1419 switch (gpps[i].gpio_base) {
1420 case INTEL_GPIO_BASE_MATCH:
1421 gpps[i].gpio_base = gpps[i].base;
1423 case INTEL_GPIO_BASE_ZERO:
1424 gpps[i].gpio_base = 0;
1426 case INTEL_GPIO_BASE_NOMAP:
1432 gpps[i].padown_num = padown_num;
1433 padown_num += DIV_ROUND_UP(gpps[i].size * 4, INTEL_PINCTRL_MAX_GPP_SIZE);
1436 community->gpps = gpps;
1441 static int intel_pinctrl_add_padgroups_by_size(struct intel_pinctrl *pctrl,
1442 struct intel_community *community)
1444 struct intel_padgroup *gpps;
1445 unsigned int npins = community->npins;
1446 unsigned int padown_num = 0;
1447 size_t i, ngpps = DIV_ROUND_UP(npins, community->gpp_size);
1449 if (community->gpp_size > INTEL_PINCTRL_MAX_GPP_SIZE)
1452 gpps = devm_kcalloc(pctrl->dev, ngpps, sizeof(*gpps), GFP_KERNEL);
1456 for (i = 0; i < ngpps; i++) {
1457 unsigned int gpp_size = community->gpp_size;
1459 gpps[i].reg_num = i;
1460 gpps[i].base = community->pin_base + i * gpp_size;
1461 gpps[i].size = min(gpp_size, npins);
1462 npins -= gpps[i].size;
1464 gpps[i].gpio_base = gpps[i].base;
1465 gpps[i].padown_num = padown_num;
1467 padown_num += community->gpp_num_padown_regs;
1470 community->ngpps = ngpps;
1471 community->gpps = gpps;
1476 static int intel_pinctrl_pm_init(struct intel_pinctrl *pctrl)
1478 #ifdef CONFIG_PM_SLEEP
1479 const struct intel_pinctrl_soc_data *soc = pctrl->soc;
1480 struct intel_community_context *communities;
1481 struct intel_pad_context *pads;
1484 pads = devm_kcalloc(pctrl->dev, soc->npins, sizeof(*pads), GFP_KERNEL);
1488 communities = devm_kcalloc(pctrl->dev, pctrl->ncommunities,
1489 sizeof(*communities), GFP_KERNEL);
1494 for (i = 0; i < pctrl->ncommunities; i++) {
1495 struct intel_community *community = &pctrl->communities[i];
1496 u32 *intmask, *hostown;
1498 intmask = devm_kcalloc(pctrl->dev, community->ngpps,
1499 sizeof(*intmask), GFP_KERNEL);
1503 communities[i].intmask = intmask;
1505 hostown = devm_kcalloc(pctrl->dev, community->ngpps,
1506 sizeof(*hostown), GFP_KERNEL);
1510 communities[i].hostown = hostown;
1513 pctrl->context.pads = pads;
1514 pctrl->context.communities = communities;
1520 static int intel_pinctrl_probe_pwm(struct intel_pinctrl *pctrl,
1521 struct intel_community *community)
1523 static const struct pwm_lpss_boardinfo info = {
1524 .clk_rate = 19200000,
1526 .base_unit_bits = 22,
1529 struct pwm_lpss_chip *pwm;
1531 if (!(community->features & PINCTRL_FEATURE_PWM))
1534 if (!IS_REACHABLE(CONFIG_PWM_LPSS))
1537 pwm = devm_pwm_lpss_probe(pctrl->dev, community->regs + PWMC, &info);
1538 return PTR_ERR_OR_ZERO(pwm);
1541 static int intel_pinctrl_probe(struct platform_device *pdev,
1542 const struct intel_pinctrl_soc_data *soc_data)
1544 struct device *dev = &pdev->dev;
1545 struct intel_pinctrl *pctrl;
1548 pctrl = devm_kzalloc(dev, sizeof(*pctrl), GFP_KERNEL);
1553 pctrl->soc = soc_data;
1554 raw_spin_lock_init(&pctrl->lock);
1557 * Make a copy of the communities which we can use to hold pointers
1560 pctrl->ncommunities = pctrl->soc->ncommunities;
1561 pctrl->communities = devm_kcalloc(dev, pctrl->ncommunities,
1562 sizeof(*pctrl->communities), GFP_KERNEL);
1563 if (!pctrl->communities)
1566 for (i = 0; i < pctrl->ncommunities; i++) {
1567 struct intel_community *community = &pctrl->communities[i];
1572 *community = pctrl->soc->communities[i];
1574 regs = devm_platform_ioremap_resource(pdev, community->barno);
1576 return PTR_ERR(regs);
1579 * Determine community features based on the revision.
1580 * A value of all ones means the device is not present.
1582 value = readl(regs + REVID);
1585 if (((value & REVID_MASK) >> REVID_SHIFT) >= 0x94) {
1586 community->features |= PINCTRL_FEATURE_DEBOUNCE;
1587 community->features |= PINCTRL_FEATURE_1K_PD;
1590 /* Determine community features based on the capabilities */
1593 value = readl(regs + offset);
1594 switch ((value & CAPLIST_ID_MASK) >> CAPLIST_ID_SHIFT) {
1595 case CAPLIST_ID_GPIO_HW_INFO:
1596 community->features |= PINCTRL_FEATURE_GPIO_HW_INFO;
1598 case CAPLIST_ID_PWM:
1599 community->features |= PINCTRL_FEATURE_PWM;
1601 case CAPLIST_ID_BLINK:
1602 community->features |= PINCTRL_FEATURE_BLINK;
1604 case CAPLIST_ID_EXP:
1605 community->features |= PINCTRL_FEATURE_EXP;
1610 offset = (value & CAPLIST_NEXT_MASK) >> CAPLIST_NEXT_SHIFT;
1613 dev_dbg(dev, "Community%d features: %#08x\n", i, community->features);
1615 /* Read offset of the pad configuration registers */
1616 offset = readl(regs + PADBAR);
1618 community->regs = regs;
1619 community->pad_regs = regs + offset;
1621 if (community->gpps)
1622 ret = intel_pinctrl_add_padgroups_by_gpps(pctrl, community);
1624 ret = intel_pinctrl_add_padgroups_by_size(pctrl, community);
1628 ret = intel_pinctrl_probe_pwm(pctrl, community);
1633 irq = platform_get_irq(pdev, 0);
1637 ret = intel_pinctrl_pm_init(pctrl);
1641 pctrl->pctldesc = intel_pinctrl_desc;
1642 pctrl->pctldesc.name = dev_name(dev);
1643 pctrl->pctldesc.pins = pctrl->soc->pins;
1644 pctrl->pctldesc.npins = pctrl->soc->npins;
1646 pctrl->pctldev = devm_pinctrl_register(dev, &pctrl->pctldesc, pctrl);
1647 if (IS_ERR(pctrl->pctldev)) {
1648 dev_err(dev, "failed to register pinctrl driver\n");
1649 return PTR_ERR(pctrl->pctldev);
1652 ret = intel_gpio_probe(pctrl, irq);
1656 platform_set_drvdata(pdev, pctrl);
1661 int intel_pinctrl_probe_by_hid(struct platform_device *pdev)
1663 const struct intel_pinctrl_soc_data *data;
1665 data = device_get_match_data(&pdev->dev);
1669 return intel_pinctrl_probe(pdev, data);
1671 EXPORT_SYMBOL_NS_GPL(intel_pinctrl_probe_by_hid, PINCTRL_INTEL);
1673 int intel_pinctrl_probe_by_uid(struct platform_device *pdev)
1675 const struct intel_pinctrl_soc_data *data;
1677 data = intel_pinctrl_get_soc_data(pdev);
1679 return PTR_ERR(data);
1681 return intel_pinctrl_probe(pdev, data);
1683 EXPORT_SYMBOL_NS_GPL(intel_pinctrl_probe_by_uid, PINCTRL_INTEL);
1685 const struct intel_pinctrl_soc_data *intel_pinctrl_get_soc_data(struct platform_device *pdev)
1687 const struct intel_pinctrl_soc_data * const *table;
1688 const struct intel_pinctrl_soc_data *data = NULL;
1689 struct device *dev = &pdev->dev;
1691 table = device_get_match_data(dev);
1693 struct acpi_device *adev = ACPI_COMPANION(dev);
1696 for (i = 0; table[i]; i++) {
1697 if (!strcmp(adev->pnp.unique_id, table[i]->uid)) {
1703 const struct platform_device_id *id;
1705 id = platform_get_device_id(pdev);
1707 return ERR_PTR(-ENODEV);
1709 table = (const struct intel_pinctrl_soc_data * const *)id->driver_data;
1710 data = table[pdev->id];
1713 return data ?: ERR_PTR(-ENODATA);
1715 EXPORT_SYMBOL_NS_GPL(intel_pinctrl_get_soc_data, PINCTRL_INTEL);
1717 #ifdef CONFIG_PM_SLEEP
1718 static bool __intel_gpio_is_direct_irq(u32 value)
1720 return (value & PADCFG0_GPIROUTIOXAPIC) && (value & PADCFG0_GPIOTXDIS) &&
1721 (__intel_gpio_get_gpio_mode(value) == PADCFG0_PMODE_GPIO);
1724 static bool intel_pinctrl_should_save(struct intel_pinctrl *pctrl, unsigned int pin)
1726 const struct pin_desc *pd = pin_desc_get(pctrl->pctldev, pin);
1729 if (!pd || !intel_pad_usable(pctrl, pin))
1733 * Only restore the pin if it is actually in use by the kernel (or
1734 * by userspace). It is possible that some pins are used by the
1735 * BIOS during resume and those are not always locked down so leave
1738 if (pd->mux_owner || pd->gpio_owner ||
1739 gpiochip_line_is_irq(&pctrl->chip, intel_pin_to_gpio(pctrl, pin)))
1743 * The firmware on some systems may configure GPIO pins to be
1744 * an interrupt source in so called "direct IRQ" mode. In such
1745 * cases the GPIO controller driver has no idea if those pins
1746 * are being used or not. At the same time, there is a known bug
1747 * in the firmwares that don't restore the pin settings correctly
1748 * after suspend, i.e. by an unknown reason the Rx value becomes
1751 * Hence, let's save and restore the pins that are configured
1752 * as GPIOs in the input mode with GPIROUTIOXAPIC bit set.
1754 * See https://bugzilla.kernel.org/show_bug.cgi?id=214749.
1756 value = readl(intel_get_padcfg(pctrl, pin, PADCFG0));
1757 if (__intel_gpio_is_direct_irq(value))
1763 int intel_pinctrl_suspend_noirq(struct device *dev)
1765 struct intel_pinctrl *pctrl = dev_get_drvdata(dev);
1766 struct intel_community_context *communities;
1767 struct intel_pad_context *pads;
1770 pads = pctrl->context.pads;
1771 for (i = 0; i < pctrl->soc->npins; i++) {
1772 const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
1773 void __iomem *padcfg;
1776 if (!intel_pinctrl_should_save(pctrl, desc->number))
1779 val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG0));
1780 pads[i].padcfg0 = val & ~PADCFG0_GPIORXSTATE;
1781 val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG1));
1782 pads[i].padcfg1 = val;
1784 padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG2);
1786 pads[i].padcfg2 = readl(padcfg);
1789 communities = pctrl->context.communities;
1790 for (i = 0; i < pctrl->ncommunities; i++) {
1791 struct intel_community *community = &pctrl->communities[i];
1795 base = community->regs + community->ie_offset;
1796 for (gpp = 0; gpp < community->ngpps; gpp++)
1797 communities[i].intmask[gpp] = readl(base + gpp * 4);
1799 base = community->regs + community->hostown_offset;
1800 for (gpp = 0; gpp < community->ngpps; gpp++)
1801 communities[i].hostown[gpp] = readl(base + gpp * 4);
1806 EXPORT_SYMBOL_GPL(intel_pinctrl_suspend_noirq);
1808 static bool intel_gpio_update_reg(void __iomem *reg, u32 mask, u32 value)
1814 updated = (curr & ~mask) | (value & mask);
1815 if (curr == updated)
1818 writel(updated, reg);
1822 static void intel_restore_hostown(struct intel_pinctrl *pctrl, unsigned int c,
1823 void __iomem *base, unsigned int gpp, u32 saved)
1825 const struct intel_community *community = &pctrl->communities[c];
1826 const struct intel_padgroup *padgrp = &community->gpps[gpp];
1827 struct device *dev = pctrl->dev;
1832 if (padgrp->gpio_base == INTEL_GPIO_BASE_NOMAP)
1835 for_each_requested_gpio_in_range(&pctrl->chip, i, padgrp->gpio_base, padgrp->size, dummy)
1836 requested |= BIT(i);
1838 if (!intel_gpio_update_reg(base + gpp * 4, requested, saved))
1841 dev_dbg(dev, "restored hostown %u/%u %#08x\n", c, gpp, readl(base + gpp * 4));
1844 static void intel_restore_intmask(struct intel_pinctrl *pctrl, unsigned int c,
1845 void __iomem *base, unsigned int gpp, u32 saved)
1847 struct device *dev = pctrl->dev;
1849 if (!intel_gpio_update_reg(base + gpp * 4, ~0U, saved))
1852 dev_dbg(dev, "restored mask %u/%u %#08x\n", c, gpp, readl(base + gpp * 4));
1855 static void intel_restore_padcfg(struct intel_pinctrl *pctrl, unsigned int pin,
1856 unsigned int reg, u32 saved)
1858 u32 mask = (reg == PADCFG0) ? PADCFG0_GPIORXSTATE : 0;
1859 unsigned int n = reg / sizeof(u32);
1860 struct device *dev = pctrl->dev;
1861 void __iomem *padcfg;
1863 padcfg = intel_get_padcfg(pctrl, pin, reg);
1867 if (!intel_gpio_update_reg(padcfg, ~mask, saved))
1870 dev_dbg(dev, "restored pin %u padcfg%u %#08x\n", pin, n, readl(padcfg));
1873 int intel_pinctrl_resume_noirq(struct device *dev)
1875 struct intel_pinctrl *pctrl = dev_get_drvdata(dev);
1876 const struct intel_community_context *communities;
1877 const struct intel_pad_context *pads;
1880 /* Mask all interrupts */
1881 intel_gpio_irq_init(pctrl);
1883 pads = pctrl->context.pads;
1884 for (i = 0; i < pctrl->soc->npins; i++) {
1885 const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
1887 if (!(intel_pinctrl_should_save(pctrl, desc->number) ||
1889 * If the firmware mangled the register contents too much,
1890 * check the saved value for the Direct IRQ mode.
1892 __intel_gpio_is_direct_irq(pads[i].padcfg0)))
1895 intel_restore_padcfg(pctrl, desc->number, PADCFG0, pads[i].padcfg0);
1896 intel_restore_padcfg(pctrl, desc->number, PADCFG1, pads[i].padcfg1);
1897 intel_restore_padcfg(pctrl, desc->number, PADCFG2, pads[i].padcfg2);
1900 communities = pctrl->context.communities;
1901 for (i = 0; i < pctrl->ncommunities; i++) {
1902 struct intel_community *community = &pctrl->communities[i];
1906 base = community->regs + community->ie_offset;
1907 for (gpp = 0; gpp < community->ngpps; gpp++)
1908 intel_restore_intmask(pctrl, i, base, gpp, communities[i].intmask[gpp]);
1910 base = community->regs + community->hostown_offset;
1911 for (gpp = 0; gpp < community->ngpps; gpp++)
1912 intel_restore_hostown(pctrl, i, base, gpp, communities[i].hostown[gpp]);
1917 EXPORT_SYMBOL_GPL(intel_pinctrl_resume_noirq);
1922 MODULE_DESCRIPTION("Intel pinctrl/GPIO core driver");
1923 MODULE_LICENSE("GPL v2");