1 // SPDX-License-Identifier: GPL-2.0
3 * Intel pinctrl/GPIO core driver.
5 * Copyright (C) 2015, Intel Corporation
6 * Authors: Mathias Nyman <mathias.nyman@linux.intel.com>
7 * Mika Westerberg <mika.westerberg@linux.intel.com>
10 #include <linux/acpi.h>
11 #include <linux/cleanup.h>
12 #include <linux/gpio/driver.h>
13 #include <linux/interrupt.h>
14 #include <linux/log2.h>
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/property.h>
18 #include <linux/seq_file.h>
19 #include <linux/string_helpers.h>
20 #include <linux/time.h>
22 #include <linux/pinctrl/consumer.h>
23 #include <linux/pinctrl/pinconf.h>
24 #include <linux/pinctrl/pinconf-generic.h>
25 #include <linux/pinctrl/pinctrl.h>
26 #include <linux/pinctrl/pinmux.h>
28 #include <linux/platform_data/x86/pwm-lpss.h>
31 #include "pinctrl-intel.h"
33 /* Offset from regs */
35 #define REVID_SHIFT 16
36 #define REVID_MASK GENMASK(31, 16)
39 #define CAPLIST_ID_SHIFT 16
40 #define CAPLIST_ID_MASK GENMASK(23, 16)
41 #define CAPLIST_ID_GPIO_HW_INFO 1
42 #define CAPLIST_ID_PWM 2
43 #define CAPLIST_ID_BLINK 3
44 #define CAPLIST_ID_EXP 4
45 #define CAPLIST_NEXT_SHIFT 0
46 #define CAPLIST_NEXT_MASK GENMASK(15, 0)
51 #define PADOWN_SHIFT(p) ((p) % 8 * PADOWN_BITS)
52 #define PADOWN_MASK(p) (GENMASK(3, 0) << PADOWN_SHIFT(p))
53 #define PADOWN_GPP(p) ((p) / 8)
57 /* Offset from pad_regs */
59 #define PADCFG0_RXEVCFG_MASK GENMASK(26, 25)
60 #define PADCFG0_RXEVCFG_LEVEL (0 << 25)
61 #define PADCFG0_RXEVCFG_EDGE (1 << 25)
62 #define PADCFG0_RXEVCFG_DISABLED (2 << 25)
63 #define PADCFG0_RXEVCFG_EDGE_BOTH (3 << 25)
64 #define PADCFG0_PREGFRXSEL BIT(24)
65 #define PADCFG0_RXINV BIT(23)
66 #define PADCFG0_GPIROUTIOXAPIC BIT(20)
67 #define PADCFG0_GPIROUTSCI BIT(19)
68 #define PADCFG0_GPIROUTSMI BIT(18)
69 #define PADCFG0_GPIROUTNMI BIT(17)
70 #define PADCFG0_PMODE_SHIFT 10
71 #define PADCFG0_PMODE_MASK GENMASK(13, 10)
72 #define PADCFG0_PMODE_GPIO 0
73 #define PADCFG0_GPIODIS_SHIFT 8
74 #define PADCFG0_GPIODIS_MASK GENMASK(9, 8)
75 #define PADCFG0_GPIODIS_NONE 0
76 #define PADCFG0_GPIODIS_OUTPUT 1
77 #define PADCFG0_GPIODIS_INPUT 2
78 #define PADCFG0_GPIODIS_FULL 3
79 #define PADCFG0_GPIORXDIS BIT(9)
80 #define PADCFG0_GPIOTXDIS BIT(8)
81 #define PADCFG0_GPIORXSTATE BIT(1)
82 #define PADCFG0_GPIOTXSTATE BIT(0)
85 #define PADCFG1_TERM_UP BIT(13)
86 #define PADCFG1_TERM_SHIFT 10
87 #define PADCFG1_TERM_MASK GENMASK(12, 10)
88 #define PADCFG1_TERM_20K BIT(2)
89 #define PADCFG1_TERM_5K BIT(1)
90 #define PADCFG1_TERM_4K (BIT(2) | BIT(1))
91 #define PADCFG1_TERM_1K BIT(0)
92 #define PADCFG1_TERM_952 (BIT(2) | BIT(0))
93 #define PADCFG1_TERM_833 (BIT(1) | BIT(0))
94 #define PADCFG1_TERM_800 (BIT(2) | BIT(1) | BIT(0))
97 #define PADCFG2_DEBOUNCE_SHIFT 1
98 #define PADCFG2_DEBOUNCE_MASK GENMASK(4, 1)
99 #define PADCFG2_DEBEN BIT(0)
101 #define DEBOUNCE_PERIOD_NSEC 31250
103 struct intel_pad_context {
109 struct intel_community_context {
114 #define pin_to_padno(c, p) ((p) - (c)->pin_base)
115 #define padgroup_offset(g, p) ((p) - (g)->base)
117 #define for_each_intel_pin_community(pctrl, community) \
118 for (unsigned int __ci = 0; \
119 __ci < pctrl->ncommunities && (community = &pctrl->communities[__ci]); \
122 #define for_each_intel_community_pad_group(community, grp) \
123 for (unsigned int __gi = 0; \
124 __gi < community->ngpps && (grp = &community->gpps[__gi]); \
127 #define for_each_intel_pad_group(pctrl, community, grp) \
128 for_each_intel_pin_community(pctrl, community) \
129 for_each_intel_community_pad_group(community, grp)
131 #define for_each_intel_gpio_group(pctrl, community, grp) \
132 for_each_intel_pad_group(pctrl, community, grp) \
133 if (grp->gpio_base == INTEL_GPIO_BASE_NOMAP) {} else
135 const struct intel_community *intel_get_community(const struct intel_pinctrl *pctrl,
138 const struct intel_community *community;
140 for_each_intel_pin_community(pctrl, community) {
141 if (pin >= community->pin_base &&
142 pin < community->pin_base + community->npins)
146 dev_warn(pctrl->dev, "failed to find community for pin %u\n", pin);
149 EXPORT_SYMBOL_NS_GPL(intel_get_community, PINCTRL_INTEL);
151 static const struct intel_padgroup *
152 intel_community_get_padgroup(const struct intel_community *community,
155 const struct intel_padgroup *padgrp;
157 for_each_intel_community_pad_group(community, padgrp) {
158 if (pin >= padgrp->base && pin < padgrp->base + padgrp->size)
165 static void __iomem *intel_get_padcfg(struct intel_pinctrl *pctrl,
166 unsigned int pin, unsigned int reg)
168 const struct intel_community *community;
172 community = intel_get_community(pctrl, pin);
176 padno = pin_to_padno(community, pin);
177 nregs = (community->features & PINCTRL_FEATURE_DEBOUNCE) ? 4 : 2;
179 if (reg >= nregs * 4)
182 return community->pad_regs + reg + padno * nregs * 4;
185 static bool intel_pad_owned_by_host(const struct intel_pinctrl *pctrl, unsigned int pin)
187 const struct intel_community *community;
188 const struct intel_padgroup *padgrp;
189 unsigned int gpp, offset, gpp_offset;
190 void __iomem *padown;
192 community = intel_get_community(pctrl, pin);
195 if (!community->padown_offset)
198 padgrp = intel_community_get_padgroup(community, pin);
202 gpp_offset = padgroup_offset(padgrp, pin);
203 gpp = PADOWN_GPP(gpp_offset);
204 offset = community->padown_offset + padgrp->padown_num * 4 + gpp * 4;
205 padown = community->regs + offset;
207 return !(readl(padown) & PADOWN_MASK(gpp_offset));
210 static bool intel_pad_acpi_mode(const struct intel_pinctrl *pctrl, unsigned int pin)
212 const struct intel_community *community;
213 const struct intel_padgroup *padgrp;
214 unsigned int offset, gpp_offset;
215 void __iomem *hostown;
217 community = intel_get_community(pctrl, pin);
220 if (!community->hostown_offset)
223 padgrp = intel_community_get_padgroup(community, pin);
227 gpp_offset = padgroup_offset(padgrp, pin);
228 offset = community->hostown_offset + padgrp->reg_num * 4;
229 hostown = community->regs + offset;
231 return !(readl(hostown) & BIT(gpp_offset));
235 * enum - Locking variants of the pad configuration
236 * @PAD_UNLOCKED: pad is fully controlled by the configuration registers
237 * @PAD_LOCKED: pad configuration registers, except TX state, are locked
238 * @PAD_LOCKED_TX: pad configuration TX state is locked
239 * @PAD_LOCKED_FULL: pad configuration registers are locked completely
241 * Locking is considered as read-only mode for corresponding registers and
242 * their respective fields. That said, TX state bit is locked separately from
243 * the main locking scheme.
249 PAD_LOCKED_FULL = PAD_LOCKED | PAD_LOCKED_TX,
252 static int intel_pad_locked(const struct intel_pinctrl *pctrl, unsigned int pin)
254 const struct intel_community *community;
255 const struct intel_padgroup *padgrp;
256 unsigned int offset, gpp_offset;
258 int ret = PAD_UNLOCKED;
260 community = intel_get_community(pctrl, pin);
262 return PAD_LOCKED_FULL;
263 if (!community->padcfglock_offset)
266 padgrp = intel_community_get_padgroup(community, pin);
268 return PAD_LOCKED_FULL;
270 gpp_offset = padgroup_offset(padgrp, pin);
273 * If PADCFGLOCK and PADCFGLOCKTX bits are both clear for this pad,
274 * the pad is considered unlocked. Any other case means that it is
275 * either fully or partially locked.
277 offset = community->padcfglock_offset + 0 + padgrp->reg_num * 8;
278 value = readl(community->regs + offset);
279 if (value & BIT(gpp_offset))
282 offset = community->padcfglock_offset + 4 + padgrp->reg_num * 8;
283 value = readl(community->regs + offset);
284 if (value & BIT(gpp_offset))
285 ret |= PAD_LOCKED_TX;
290 static bool intel_pad_is_unlocked(const struct intel_pinctrl *pctrl, unsigned int pin)
292 return (intel_pad_locked(pctrl, pin) & PAD_LOCKED) == PAD_UNLOCKED;
295 static bool intel_pad_usable(const struct intel_pinctrl *pctrl, unsigned int pin)
297 return intel_pad_owned_by_host(pctrl, pin) && intel_pad_is_unlocked(pctrl, pin);
300 int intel_get_groups_count(struct pinctrl_dev *pctldev)
302 const struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
304 return pctrl->soc->ngroups;
306 EXPORT_SYMBOL_NS_GPL(intel_get_groups_count, PINCTRL_INTEL);
308 const char *intel_get_group_name(struct pinctrl_dev *pctldev, unsigned int group)
310 const struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
312 return pctrl->soc->groups[group].grp.name;
314 EXPORT_SYMBOL_NS_GPL(intel_get_group_name, PINCTRL_INTEL);
316 int intel_get_group_pins(struct pinctrl_dev *pctldev, unsigned int group,
317 const unsigned int **pins, unsigned int *npins)
319 const struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
321 *pins = pctrl->soc->groups[group].grp.pins;
322 *npins = pctrl->soc->groups[group].grp.npins;
325 EXPORT_SYMBOL_NS_GPL(intel_get_group_pins, PINCTRL_INTEL);
327 static void intel_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
330 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
331 void __iomem *padcfg;
332 u32 cfg0, cfg1, mode;
336 if (!intel_pad_owned_by_host(pctrl, pin)) {
337 seq_puts(s, "not available");
341 cfg0 = readl(intel_get_padcfg(pctrl, pin, PADCFG0));
342 cfg1 = readl(intel_get_padcfg(pctrl, pin, PADCFG1));
344 mode = (cfg0 & PADCFG0_PMODE_MASK) >> PADCFG0_PMODE_SHIFT;
345 if (mode == PADCFG0_PMODE_GPIO)
346 seq_puts(s, "GPIO ");
348 seq_printf(s, "mode %d ", mode);
350 seq_printf(s, "0x%08x 0x%08x", cfg0, cfg1);
352 /* Dump the additional PADCFG registers if available */
353 padcfg = intel_get_padcfg(pctrl, pin, PADCFG2);
355 seq_printf(s, " 0x%08x", readl(padcfg));
357 locked = intel_pad_locked(pctrl, pin);
358 acpi = intel_pad_acpi_mode(pctrl, pin);
360 if (locked || acpi) {
363 seq_puts(s, "LOCKED");
364 if ((locked & PAD_LOCKED_FULL) == PAD_LOCKED_TX)
366 else if ((locked & PAD_LOCKED_FULL) == PAD_LOCKED_FULL)
367 seq_puts(s, " full");
378 static const struct pinctrl_ops intel_pinctrl_ops = {
379 .get_groups_count = intel_get_groups_count,
380 .get_group_name = intel_get_group_name,
381 .get_group_pins = intel_get_group_pins,
382 .pin_dbg_show = intel_pin_dbg_show,
385 int intel_get_functions_count(struct pinctrl_dev *pctldev)
387 const struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
389 return pctrl->soc->nfunctions;
391 EXPORT_SYMBOL_NS_GPL(intel_get_functions_count, PINCTRL_INTEL);
393 const char *intel_get_function_name(struct pinctrl_dev *pctldev, unsigned int function)
395 const struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
397 return pctrl->soc->functions[function].func.name;
399 EXPORT_SYMBOL_NS_GPL(intel_get_function_name, PINCTRL_INTEL);
401 int intel_get_function_groups(struct pinctrl_dev *pctldev, unsigned int function,
402 const char * const **groups, unsigned int * const ngroups)
404 const struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
406 *groups = pctrl->soc->functions[function].func.groups;
407 *ngroups = pctrl->soc->functions[function].func.ngroups;
410 EXPORT_SYMBOL_NS_GPL(intel_get_function_groups, PINCTRL_INTEL);
412 static int intel_pinmux_set_mux(struct pinctrl_dev *pctldev,
413 unsigned int function, unsigned int group)
415 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
416 const struct intel_pingroup *grp = &pctrl->soc->groups[group];
419 guard(raw_spinlock_irqsave)(&pctrl->lock);
422 * All pins in the groups needs to be accessible and writable
423 * before we can enable the mux for this group.
425 for (i = 0; i < grp->grp.npins; i++) {
426 if (!intel_pad_usable(pctrl, grp->grp.pins[i]))
430 /* Now enable the mux setting for each pin in the group */
431 for (i = 0; i < grp->grp.npins; i++) {
432 void __iomem *padcfg0;
435 padcfg0 = intel_get_padcfg(pctrl, grp->grp.pins[i], PADCFG0);
437 value = readl(padcfg0);
438 value &= ~PADCFG0_PMODE_MASK;
441 pmode = grp->modes[i];
445 value |= pmode << PADCFG0_PMODE_SHIFT;
446 writel(value, padcfg0);
453 * enum - Possible pad physical connections
454 * @PAD_CONNECT_NONE: pad is fully disconnected
455 * @PAD_CONNECT_INPUT: pad is in input only mode
456 * @PAD_CONNECT_OUTPUT: pad is in output only mode
457 * @PAD_CONNECT_FULL: pad is fully connected
460 PAD_CONNECT_NONE = 0,
461 PAD_CONNECT_INPUT = 1,
462 PAD_CONNECT_OUTPUT = 2,
463 PAD_CONNECT_FULL = PAD_CONNECT_INPUT | PAD_CONNECT_OUTPUT,
466 static int __intel_gpio_get_direction(u32 value)
468 switch ((value & PADCFG0_GPIODIS_MASK) >> PADCFG0_GPIODIS_SHIFT) {
469 case PADCFG0_GPIODIS_FULL:
470 return PAD_CONNECT_NONE;
471 case PADCFG0_GPIODIS_OUTPUT:
472 return PAD_CONNECT_INPUT;
473 case PADCFG0_GPIODIS_INPUT:
474 return PAD_CONNECT_OUTPUT;
475 case PADCFG0_GPIODIS_NONE:
476 return PAD_CONNECT_FULL;
482 static u32 __intel_gpio_set_direction(u32 value, bool input, bool output)
485 value &= ~PADCFG0_GPIORXDIS;
487 value |= PADCFG0_GPIORXDIS;
490 value &= ~PADCFG0_GPIOTXDIS;
492 value |= PADCFG0_GPIOTXDIS;
497 static int __intel_gpio_get_gpio_mode(u32 value)
499 return (value & PADCFG0_PMODE_MASK) >> PADCFG0_PMODE_SHIFT;
502 static int intel_gpio_get_gpio_mode(void __iomem *padcfg0)
504 return __intel_gpio_get_gpio_mode(readl(padcfg0));
507 static void intel_gpio_set_gpio_mode(void __iomem *padcfg0)
511 value = readl(padcfg0);
513 /* Put the pad into GPIO mode */
514 value &= ~PADCFG0_PMODE_MASK;
515 value |= PADCFG0_PMODE_GPIO;
517 /* Disable TX buffer and enable RX (this will be input) */
518 value = __intel_gpio_set_direction(value, true, false);
520 /* Disable SCI/SMI/NMI generation */
521 value &= ~(PADCFG0_GPIROUTIOXAPIC | PADCFG0_GPIROUTSCI);
522 value &= ~(PADCFG0_GPIROUTSMI | PADCFG0_GPIROUTNMI);
524 writel(value, padcfg0);
527 static int intel_gpio_request_enable(struct pinctrl_dev *pctldev,
528 struct pinctrl_gpio_range *range,
531 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
532 void __iomem *padcfg0;
534 padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
536 guard(raw_spinlock_irqsave)(&pctrl->lock);
538 if (!intel_pad_owned_by_host(pctrl, pin))
541 if (!intel_pad_is_unlocked(pctrl, pin))
545 * If pin is already configured in GPIO mode, we assume that
546 * firmware provides correct settings. In such case we avoid
547 * potential glitches on the pin. Otherwise, for the pin in
548 * alternative mode, consumer has to supply respective flags.
550 if (intel_gpio_get_gpio_mode(padcfg0) == PADCFG0_PMODE_GPIO)
553 intel_gpio_set_gpio_mode(padcfg0);
558 static int intel_gpio_set_direction(struct pinctrl_dev *pctldev,
559 struct pinctrl_gpio_range *range,
560 unsigned int pin, bool input)
562 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
563 void __iomem *padcfg0;
566 padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
568 guard(raw_spinlock_irqsave)(&pctrl->lock);
570 value = readl(padcfg0);
572 value = __intel_gpio_set_direction(value, true, false);
574 value = __intel_gpio_set_direction(value, false, true);
575 writel(value, padcfg0);
580 static const struct pinmux_ops intel_pinmux_ops = {
581 .get_functions_count = intel_get_functions_count,
582 .get_function_name = intel_get_function_name,
583 .get_function_groups = intel_get_function_groups,
584 .set_mux = intel_pinmux_set_mux,
585 .gpio_request_enable = intel_gpio_request_enable,
586 .gpio_set_direction = intel_gpio_set_direction,
589 static int intel_config_get_pull(struct intel_pinctrl *pctrl, unsigned int pin,
590 enum pin_config_param param, u32 *arg)
592 void __iomem *padcfg1;
595 padcfg1 = intel_get_padcfg(pctrl, pin, PADCFG1);
597 scoped_guard(raw_spinlock_irqsave, &pctrl->lock)
598 value = readl(padcfg1);
600 term = (value & PADCFG1_TERM_MASK) >> PADCFG1_TERM_SHIFT;
603 case PIN_CONFIG_BIAS_DISABLE:
608 case PIN_CONFIG_BIAS_PULL_UP:
609 if (!term || !(value & PADCFG1_TERM_UP))
613 case PADCFG1_TERM_833:
616 case PADCFG1_TERM_1K:
619 case PADCFG1_TERM_4K:
622 case PADCFG1_TERM_5K:
625 case PADCFG1_TERM_20K:
632 case PIN_CONFIG_BIAS_PULL_DOWN: {
633 const struct intel_community *community = intel_get_community(pctrl, pin);
635 if (!term || value & PADCFG1_TERM_UP)
639 case PADCFG1_TERM_833:
640 if (!(community->features & PINCTRL_FEATURE_1K_PD))
644 case PADCFG1_TERM_1K:
645 if (!(community->features & PINCTRL_FEATURE_1K_PD))
649 case PADCFG1_TERM_4K:
652 case PADCFG1_TERM_5K:
655 case PADCFG1_TERM_20K:
670 static int intel_config_get_high_impedance(struct intel_pinctrl *pctrl, unsigned int pin,
671 enum pin_config_param param, u32 *arg)
673 void __iomem *padcfg0;
676 padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
678 scoped_guard(raw_spinlock_irqsave, &pctrl->lock)
679 value = readl(padcfg0);
681 if (__intel_gpio_get_direction(value) != PAD_CONNECT_NONE)
687 static int intel_config_get_debounce(struct intel_pinctrl *pctrl, unsigned int pin,
688 enum pin_config_param param, u32 *arg)
690 void __iomem *padcfg2;
694 padcfg2 = intel_get_padcfg(pctrl, pin, PADCFG2);
698 scoped_guard(raw_spinlock_irqsave, &pctrl->lock)
699 value2 = readl(padcfg2);
701 if (!(value2 & PADCFG2_DEBEN))
704 v = (value2 & PADCFG2_DEBOUNCE_MASK) >> PADCFG2_DEBOUNCE_SHIFT;
705 *arg = BIT(v) * DEBOUNCE_PERIOD_NSEC / NSEC_PER_USEC;
710 static int intel_config_get(struct pinctrl_dev *pctldev, unsigned int pin,
711 unsigned long *config)
713 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
714 enum pin_config_param param = pinconf_to_config_param(*config);
718 if (!intel_pad_owned_by_host(pctrl, pin))
722 case PIN_CONFIG_BIAS_DISABLE:
723 case PIN_CONFIG_BIAS_PULL_UP:
724 case PIN_CONFIG_BIAS_PULL_DOWN:
725 ret = intel_config_get_pull(pctrl, pin, param, &arg);
730 case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
731 ret = intel_config_get_high_impedance(pctrl, pin, param, &arg);
736 case PIN_CONFIG_INPUT_DEBOUNCE:
737 ret = intel_config_get_debounce(pctrl, pin, param, &arg);
746 *config = pinconf_to_config_packed(param, arg);
750 static int intel_config_set_pull(struct intel_pinctrl *pctrl, unsigned int pin,
751 unsigned long config)
753 unsigned int param = pinconf_to_config_param(config);
754 unsigned int arg = pinconf_to_config_argument(config);
755 u32 term = 0, up = 0, value;
756 void __iomem *padcfg1;
759 case PIN_CONFIG_BIAS_DISABLE:
762 case PIN_CONFIG_BIAS_PULL_UP:
765 term = PADCFG1_TERM_20K;
767 case 1: /* Set default strength value in case none is given */
769 term = PADCFG1_TERM_5K;
772 term = PADCFG1_TERM_4K;
775 term = PADCFG1_TERM_1K;
778 term = PADCFG1_TERM_833;
784 up = PADCFG1_TERM_UP;
787 case PIN_CONFIG_BIAS_PULL_DOWN: {
788 const struct intel_community *community = intel_get_community(pctrl, pin);
792 term = PADCFG1_TERM_20K;
794 case 1: /* Set default strength value in case none is given */
796 term = PADCFG1_TERM_5K;
799 term = PADCFG1_TERM_4K;
802 if (!(community->features & PINCTRL_FEATURE_1K_PD))
804 term = PADCFG1_TERM_1K;
807 if (!(community->features & PINCTRL_FEATURE_1K_PD))
809 term = PADCFG1_TERM_833;
822 padcfg1 = intel_get_padcfg(pctrl, pin, PADCFG1);
824 guard(raw_spinlock_irqsave)(&pctrl->lock);
826 value = readl(padcfg1);
827 value = (value & ~PADCFG1_TERM_MASK) | (term << PADCFG1_TERM_SHIFT);
828 value = (value & ~PADCFG1_TERM_UP) | up;
829 writel(value, padcfg1);
834 static void intel_gpio_set_high_impedance(struct intel_pinctrl *pctrl, unsigned int pin)
836 void __iomem *padcfg0;
839 padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
841 guard(raw_spinlock_irqsave)(&pctrl->lock);
843 value = readl(padcfg0);
844 value = __intel_gpio_set_direction(value, false, false);
845 writel(value, padcfg0);
848 static int intel_config_set_debounce(struct intel_pinctrl *pctrl,
849 unsigned int pin, unsigned int debounce)
851 void __iomem *padcfg0, *padcfg2;
856 v = order_base_2(debounce * NSEC_PER_USEC / DEBOUNCE_PERIOD_NSEC);
863 padcfg2 = intel_get_padcfg(pctrl, pin, PADCFG2);
867 padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
869 guard(raw_spinlock_irqsave)(&pctrl->lock);
871 value0 = readl(padcfg0);
872 value2 = readl(padcfg2);
874 value2 = (value2 & ~PADCFG2_DEBOUNCE_MASK) | (v << PADCFG2_DEBOUNCE_SHIFT);
876 /* Enable glitch filter and debouncer */
877 value0 |= PADCFG0_PREGFRXSEL;
878 value2 |= PADCFG2_DEBEN;
880 /* Disable glitch filter and debouncer */
881 value0 &= ~PADCFG0_PREGFRXSEL;
882 value2 &= ~PADCFG2_DEBEN;
885 writel(value0, padcfg0);
886 writel(value2, padcfg2);
891 static int intel_config_set(struct pinctrl_dev *pctldev, unsigned int pin,
892 unsigned long *configs, unsigned int nconfigs)
894 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
897 if (!intel_pad_usable(pctrl, pin))
900 for (i = 0; i < nconfigs; i++) {
901 switch (pinconf_to_config_param(configs[i])) {
902 case PIN_CONFIG_BIAS_DISABLE:
903 case PIN_CONFIG_BIAS_PULL_UP:
904 case PIN_CONFIG_BIAS_PULL_DOWN:
905 ret = intel_config_set_pull(pctrl, pin, configs[i]);
910 case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
911 intel_gpio_set_high_impedance(pctrl, pin);
914 case PIN_CONFIG_INPUT_DEBOUNCE:
915 ret = intel_config_set_debounce(pctrl, pin,
916 pinconf_to_config_argument(configs[i]));
929 static const struct pinconf_ops intel_pinconf_ops = {
931 .pin_config_get = intel_config_get,
932 .pin_config_set = intel_config_set,
935 static const struct pinctrl_desc intel_pinctrl_desc = {
936 .pctlops = &intel_pinctrl_ops,
937 .pmxops = &intel_pinmux_ops,
938 .confops = &intel_pinconf_ops,
939 .owner = THIS_MODULE,
943 * intel_gpio_to_pin() - Translate from GPIO offset to pin number
944 * @pctrl: Pinctrl structure
945 * @offset: GPIO offset from gpiolib
946 * @community: Community is filled here if not %NULL
947 * @padgrp: Pad group is filled here if not %NULL
949 * When coming through gpiolib irqchip, the GPIO offset is not
950 * automatically translated to pinctrl pin number. This function can be
951 * used to find out the corresponding pinctrl pin.
953 * Return: a pin number and pointers to the community and pad group, which
954 * the pin belongs to, or negative error code if translation can't be done.
956 static int intel_gpio_to_pin(const struct intel_pinctrl *pctrl, unsigned int offset,
957 const struct intel_community **community,
958 const struct intel_padgroup **padgrp)
960 const struct intel_community *comm;
961 const struct intel_padgroup *grp;
963 for_each_intel_gpio_group(pctrl, comm, grp) {
964 if (offset >= grp->gpio_base && offset < grp->gpio_base + grp->size) {
970 return grp->base + offset - grp->gpio_base;
978 * intel_pin_to_gpio() - Translate from pin number to GPIO offset
979 * @pctrl: Pinctrl structure
982 * Translate the pin number of pinctrl to GPIO offset
984 * Return: a GPIO offset, or negative error code if translation can't be done.
986 static int intel_pin_to_gpio(const struct intel_pinctrl *pctrl, int pin)
988 const struct intel_community *community;
989 const struct intel_padgroup *padgrp;
991 community = intel_get_community(pctrl, pin);
995 padgrp = intel_community_get_padgroup(community, pin);
999 return pin - padgrp->base + padgrp->gpio_base;
1002 static int intel_gpio_get(struct gpio_chip *chip, unsigned int offset)
1004 struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
1009 pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL);
1013 reg = intel_get_padcfg(pctrl, pin, PADCFG0);
1017 padcfg0 = readl(reg);
1018 if (__intel_gpio_get_direction(padcfg0) & PAD_CONNECT_OUTPUT)
1019 return !!(padcfg0 & PADCFG0_GPIOTXSTATE);
1021 return !!(padcfg0 & PADCFG0_GPIORXSTATE);
1024 static void intel_gpio_set(struct gpio_chip *chip, unsigned int offset,
1027 struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
1032 pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL);
1036 reg = intel_get_padcfg(pctrl, pin, PADCFG0);
1040 guard(raw_spinlock_irqsave)(&pctrl->lock);
1042 padcfg0 = readl(reg);
1044 padcfg0 |= PADCFG0_GPIOTXSTATE;
1046 padcfg0 &= ~PADCFG0_GPIOTXSTATE;
1047 writel(padcfg0, reg);
1050 static int intel_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
1052 struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
1057 pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL);
1061 reg = intel_get_padcfg(pctrl, pin, PADCFG0);
1065 scoped_guard(raw_spinlock_irqsave, &pctrl->lock)
1066 padcfg0 = readl(reg);
1068 if (padcfg0 & PADCFG0_PMODE_MASK)
1071 if (__intel_gpio_get_direction(padcfg0) & PAD_CONNECT_OUTPUT)
1072 return GPIO_LINE_DIRECTION_OUT;
1074 return GPIO_LINE_DIRECTION_IN;
1077 static int intel_gpio_direction_input(struct gpio_chip *chip, unsigned int offset)
1079 return pinctrl_gpio_direction_input(chip, offset);
1082 static int intel_gpio_direction_output(struct gpio_chip *chip, unsigned int offset,
1085 intel_gpio_set(chip, offset, value);
1086 return pinctrl_gpio_direction_output(chip, offset);
1089 static const struct gpio_chip intel_gpio_chip = {
1090 .owner = THIS_MODULE,
1091 .request = gpiochip_generic_request,
1092 .free = gpiochip_generic_free,
1093 .get_direction = intel_gpio_get_direction,
1094 .direction_input = intel_gpio_direction_input,
1095 .direction_output = intel_gpio_direction_output,
1096 .get = intel_gpio_get,
1097 .set = intel_gpio_set,
1098 .set_config = gpiochip_generic_config,
1101 static void intel_gpio_irq_ack(struct irq_data *d)
1103 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1104 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
1105 const struct intel_community *community;
1106 const struct intel_padgroup *padgrp;
1109 pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), &community, &padgrp);
1111 unsigned int gpp, gpp_offset;
1114 gpp = padgrp->reg_num;
1115 gpp_offset = padgroup_offset(padgrp, pin);
1117 is = community->regs + community->is_offset + gpp * 4;
1119 guard(raw_spinlock)(&pctrl->lock);
1121 writel(BIT(gpp_offset), is);
1125 static void intel_gpio_irq_mask_unmask(struct gpio_chip *gc, irq_hw_number_t hwirq, bool mask)
1127 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
1128 const struct intel_community *community;
1129 const struct intel_padgroup *padgrp;
1132 pin = intel_gpio_to_pin(pctrl, hwirq, &community, &padgrp);
1134 unsigned int gpp, gpp_offset;
1135 void __iomem *reg, *is;
1138 gpp = padgrp->reg_num;
1139 gpp_offset = padgroup_offset(padgrp, pin);
1141 reg = community->regs + community->ie_offset + gpp * 4;
1142 is = community->regs + community->is_offset + gpp * 4;
1144 guard(raw_spinlock_irqsave)(&pctrl->lock);
1146 /* Clear interrupt status first to avoid unexpected interrupt */
1147 writel(BIT(gpp_offset), is);
1151 value &= ~BIT(gpp_offset);
1153 value |= BIT(gpp_offset);
1158 static void intel_gpio_irq_mask(struct irq_data *d)
1160 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1161 irq_hw_number_t hwirq = irqd_to_hwirq(d);
1163 intel_gpio_irq_mask_unmask(gc, hwirq, true);
1164 gpiochip_disable_irq(gc, hwirq);
1167 static void intel_gpio_irq_unmask(struct irq_data *d)
1169 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1170 irq_hw_number_t hwirq = irqd_to_hwirq(d);
1172 gpiochip_enable_irq(gc, hwirq);
1173 intel_gpio_irq_mask_unmask(gc, hwirq, false);
1176 static int intel_gpio_irq_type(struct irq_data *d, unsigned int type)
1178 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1179 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
1180 unsigned int pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), NULL, NULL);
1181 u32 rxevcfg, rxinv, value;
1184 reg = intel_get_padcfg(pctrl, pin, PADCFG0);
1189 * If the pin is in ACPI mode it is still usable as a GPIO but it
1190 * cannot be used as IRQ because GPI_IS status bit will not be
1191 * updated by the host controller hardware.
1193 if (intel_pad_acpi_mode(pctrl, pin)) {
1194 dev_warn(pctrl->dev, "pin %u cannot be used as IRQ\n", pin);
1198 if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
1199 rxevcfg = PADCFG0_RXEVCFG_EDGE_BOTH;
1200 } else if (type & IRQ_TYPE_EDGE_FALLING) {
1201 rxevcfg = PADCFG0_RXEVCFG_EDGE;
1202 } else if (type & IRQ_TYPE_EDGE_RISING) {
1203 rxevcfg = PADCFG0_RXEVCFG_EDGE;
1204 } else if (type & IRQ_TYPE_LEVEL_MASK) {
1205 rxevcfg = PADCFG0_RXEVCFG_LEVEL;
1207 rxevcfg = PADCFG0_RXEVCFG_DISABLED;
1210 if (type == IRQ_TYPE_EDGE_FALLING || type == IRQ_TYPE_LEVEL_LOW)
1211 rxinv = PADCFG0_RXINV;
1215 guard(raw_spinlock_irqsave)(&pctrl->lock);
1217 intel_gpio_set_gpio_mode(reg);
1221 value = (value & ~PADCFG0_RXEVCFG_MASK) | rxevcfg;
1222 value = (value & ~PADCFG0_RXINV) | rxinv;
1226 if (type & IRQ_TYPE_EDGE_BOTH)
1227 irq_set_handler_locked(d, handle_edge_irq);
1228 else if (type & IRQ_TYPE_LEVEL_MASK)
1229 irq_set_handler_locked(d, handle_level_irq);
1234 static int intel_gpio_irq_wake(struct irq_data *d, unsigned int on)
1236 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1237 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
1238 unsigned int pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), NULL, NULL);
1241 enable_irq_wake(pctrl->irq);
1243 disable_irq_wake(pctrl->irq);
1245 dev_dbg(pctrl->dev, "%s wake for pin %u\n", str_enable_disable(on), pin);
1249 static const struct irq_chip intel_gpio_irq_chip = {
1250 .name = "intel-gpio",
1251 .irq_ack = intel_gpio_irq_ack,
1252 .irq_mask = intel_gpio_irq_mask,
1253 .irq_unmask = intel_gpio_irq_unmask,
1254 .irq_set_type = intel_gpio_irq_type,
1255 .irq_set_wake = intel_gpio_irq_wake,
1256 .flags = IRQCHIP_MASK_ON_SUSPEND | IRQCHIP_IMMUTABLE,
1257 GPIOCHIP_IRQ_RESOURCE_HELPERS,
1260 static irqreturn_t intel_gpio_irq(int irq, void *data)
1262 const struct intel_community *community;
1263 const struct intel_padgroup *padgrp;
1264 struct intel_pinctrl *pctrl = data;
1267 /* Need to check all communities for pending interrupts */
1268 for_each_intel_pad_group(pctrl, community, padgrp) {
1269 struct gpio_chip *gc = &pctrl->chip;
1270 unsigned long pending, enabled;
1271 unsigned int gpp, gpp_offset;
1272 void __iomem *reg, *is;
1274 gpp = padgrp->reg_num;
1276 reg = community->regs + community->ie_offset + gpp * 4;
1277 is = community->regs + community->is_offset + gpp * 4;
1279 scoped_guard(raw_spinlock, &pctrl->lock) {
1280 pending = readl(is);
1281 enabled = readl(reg);
1284 /* Only interrupts that are enabled */
1287 for_each_set_bit(gpp_offset, &pending, padgrp->size)
1288 generic_handle_domain_irq(gc->irq.domain, padgrp->gpio_base + gpp_offset);
1290 ret += pending ? 1 : 0;
1293 return IRQ_RETVAL(ret);
1296 static void intel_gpio_irq_init(struct intel_pinctrl *pctrl)
1298 const struct intel_community *community;
1300 for_each_intel_pin_community(pctrl, community) {
1301 void __iomem *reg, *is;
1304 for (gpp = 0; gpp < community->ngpps; gpp++) {
1305 reg = community->regs + community->ie_offset + gpp * 4;
1306 is = community->regs + community->is_offset + gpp * 4;
1308 /* Mask and clear all interrupts */
1315 static int intel_gpio_irq_init_hw(struct gpio_chip *gc)
1317 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
1320 * Make sure the interrupt lines are in a proper state before
1321 * further configuration.
1323 intel_gpio_irq_init(pctrl);
1328 static int intel_gpio_add_pin_ranges(struct gpio_chip *gc)
1330 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
1331 const struct intel_community *community;
1332 const struct intel_padgroup *grp;
1335 for_each_intel_gpio_group(pctrl, community, grp) {
1336 ret = gpiochip_add_pin_range(&pctrl->chip, dev_name(pctrl->dev),
1337 grp->gpio_base, grp->base,
1340 dev_err(pctrl->dev, "failed to add GPIO pin range\n");
1348 static unsigned int intel_gpio_ngpio(const struct intel_pinctrl *pctrl)
1350 const struct intel_community *community;
1351 const struct intel_padgroup *grp;
1352 unsigned int ngpio = 0;
1354 for_each_intel_gpio_group(pctrl, community, grp) {
1355 if (grp->gpio_base + grp->size > ngpio)
1356 ngpio = grp->gpio_base + grp->size;
1362 static int intel_gpio_probe(struct intel_pinctrl *pctrl, int irq)
1365 struct gpio_irq_chip *girq;
1367 pctrl->chip = intel_gpio_chip;
1369 /* Setup GPIO chip */
1370 pctrl->chip.ngpio = intel_gpio_ngpio(pctrl);
1371 pctrl->chip.label = dev_name(pctrl->dev);
1372 pctrl->chip.parent = pctrl->dev;
1373 pctrl->chip.base = -1;
1374 pctrl->chip.add_pin_ranges = intel_gpio_add_pin_ranges;
1378 * On some platforms several GPIO controllers share the same interrupt
1381 ret = devm_request_irq(pctrl->dev, irq, intel_gpio_irq,
1382 IRQF_SHARED | IRQF_NO_THREAD,
1383 dev_name(pctrl->dev), pctrl);
1385 dev_err(pctrl->dev, "failed to request interrupt\n");
1389 /* Setup IRQ chip */
1390 girq = &pctrl->chip.irq;
1391 gpio_irq_chip_set_chip(girq, &intel_gpio_irq_chip);
1392 /* This will let us handle the IRQ in the driver */
1393 girq->parent_handler = NULL;
1394 girq->num_parents = 0;
1395 girq->default_type = IRQ_TYPE_NONE;
1396 girq->handler = handle_bad_irq;
1397 girq->init_hw = intel_gpio_irq_init_hw;
1399 ret = devm_gpiochip_add_data(pctrl->dev, &pctrl->chip, pctrl);
1401 dev_err(pctrl->dev, "failed to register gpiochip\n");
1408 static int intel_pinctrl_add_padgroups_by_gpps(struct intel_pinctrl *pctrl,
1409 struct intel_community *community)
1411 struct intel_padgroup *gpps;
1412 unsigned int padown_num = 0;
1413 size_t i, ngpps = community->ngpps;
1415 gpps = devm_kcalloc(pctrl->dev, ngpps, sizeof(*gpps), GFP_KERNEL);
1419 for (i = 0; i < ngpps; i++) {
1420 gpps[i] = community->gpps[i];
1422 if (gpps[i].size > INTEL_PINCTRL_MAX_GPP_SIZE)
1425 /* Special treatment for GPIO base */
1426 switch (gpps[i].gpio_base) {
1427 case INTEL_GPIO_BASE_MATCH:
1428 gpps[i].gpio_base = gpps[i].base;
1430 case INTEL_GPIO_BASE_ZERO:
1431 gpps[i].gpio_base = 0;
1433 case INTEL_GPIO_BASE_NOMAP:
1439 gpps[i].padown_num = padown_num;
1440 padown_num += DIV_ROUND_UP(gpps[i].size * 4, INTEL_PINCTRL_MAX_GPP_SIZE);
1443 community->gpps = gpps;
1448 static int intel_pinctrl_add_padgroups_by_size(struct intel_pinctrl *pctrl,
1449 struct intel_community *community)
1451 struct intel_padgroup *gpps;
1452 unsigned int npins = community->npins;
1453 unsigned int padown_num = 0;
1454 size_t i, ngpps = DIV_ROUND_UP(npins, community->gpp_size);
1456 if (community->gpp_size > INTEL_PINCTRL_MAX_GPP_SIZE)
1459 gpps = devm_kcalloc(pctrl->dev, ngpps, sizeof(*gpps), GFP_KERNEL);
1463 for (i = 0; i < ngpps; i++) {
1464 unsigned int gpp_size = community->gpp_size;
1466 gpps[i].reg_num = i;
1467 gpps[i].base = community->pin_base + i * gpp_size;
1468 gpps[i].size = min(gpp_size, npins);
1469 npins -= gpps[i].size;
1471 gpps[i].gpio_base = gpps[i].base;
1472 gpps[i].padown_num = padown_num;
1474 padown_num += community->gpp_num_padown_regs;
1477 community->ngpps = ngpps;
1478 community->gpps = gpps;
1483 static int intel_pinctrl_pm_init(struct intel_pinctrl *pctrl)
1485 #ifdef CONFIG_PM_SLEEP
1486 const struct intel_pinctrl_soc_data *soc = pctrl->soc;
1487 struct intel_community_context *communities;
1488 struct intel_pad_context *pads;
1491 pads = devm_kcalloc(pctrl->dev, soc->npins, sizeof(*pads), GFP_KERNEL);
1495 communities = devm_kcalloc(pctrl->dev, pctrl->ncommunities,
1496 sizeof(*communities), GFP_KERNEL);
1501 for (i = 0; i < pctrl->ncommunities; i++) {
1502 struct intel_community *community = &pctrl->communities[i];
1503 u32 *intmask, *hostown;
1505 intmask = devm_kcalloc(pctrl->dev, community->ngpps,
1506 sizeof(*intmask), GFP_KERNEL);
1510 communities[i].intmask = intmask;
1512 hostown = devm_kcalloc(pctrl->dev, community->ngpps,
1513 sizeof(*hostown), GFP_KERNEL);
1517 communities[i].hostown = hostown;
1520 pctrl->context.pads = pads;
1521 pctrl->context.communities = communities;
1527 static int intel_pinctrl_probe_pwm(struct intel_pinctrl *pctrl,
1528 struct intel_community *community)
1530 static const struct pwm_lpss_boardinfo info = {
1531 .clk_rate = 19200000,
1533 .base_unit_bits = 22,
1536 struct pwm_chip *chip;
1538 if (!(community->features & PINCTRL_FEATURE_PWM))
1541 if (!IS_REACHABLE(CONFIG_PWM_LPSS))
1544 chip = devm_pwm_lpss_probe(pctrl->dev, community->regs + PWMC, &info);
1545 return PTR_ERR_OR_ZERO(chip);
1548 int intel_pinctrl_probe(struct platform_device *pdev,
1549 const struct intel_pinctrl_soc_data *soc_data)
1551 struct device *dev = &pdev->dev;
1552 struct intel_pinctrl *pctrl;
1555 pctrl = devm_kzalloc(dev, sizeof(*pctrl), GFP_KERNEL);
1560 pctrl->soc = soc_data;
1561 raw_spin_lock_init(&pctrl->lock);
1564 * Make a copy of the communities which we can use to hold pointers
1567 pctrl->ncommunities = pctrl->soc->ncommunities;
1568 pctrl->communities = devm_kcalloc(dev, pctrl->ncommunities,
1569 sizeof(*pctrl->communities), GFP_KERNEL);
1570 if (!pctrl->communities)
1573 for (i = 0; i < pctrl->ncommunities; i++) {
1574 struct intel_community *community = &pctrl->communities[i];
1579 *community = pctrl->soc->communities[i];
1581 regs = devm_platform_ioremap_resource(pdev, community->barno);
1583 return PTR_ERR(regs);
1586 * Determine community features based on the revision.
1587 * A value of all ones means the device is not present.
1589 value = readl(regs + REVID);
1592 if (((value & REVID_MASK) >> REVID_SHIFT) >= 0x94) {
1593 community->features |= PINCTRL_FEATURE_DEBOUNCE;
1594 community->features |= PINCTRL_FEATURE_1K_PD;
1597 /* Determine community features based on the capabilities */
1600 value = readl(regs + offset);
1601 switch ((value & CAPLIST_ID_MASK) >> CAPLIST_ID_SHIFT) {
1602 case CAPLIST_ID_GPIO_HW_INFO:
1603 community->features |= PINCTRL_FEATURE_GPIO_HW_INFO;
1605 case CAPLIST_ID_PWM:
1606 community->features |= PINCTRL_FEATURE_PWM;
1608 case CAPLIST_ID_BLINK:
1609 community->features |= PINCTRL_FEATURE_BLINK;
1611 case CAPLIST_ID_EXP:
1612 community->features |= PINCTRL_FEATURE_EXP;
1617 offset = (value & CAPLIST_NEXT_MASK) >> CAPLIST_NEXT_SHIFT;
1620 dev_dbg(dev, "Community%d features: %#08x\n", i, community->features);
1622 /* Read offset of the pad configuration registers */
1623 offset = readl(regs + PADBAR);
1625 community->regs = regs;
1626 community->pad_regs = regs + offset;
1628 if (community->gpps)
1629 ret = intel_pinctrl_add_padgroups_by_gpps(pctrl, community);
1631 ret = intel_pinctrl_add_padgroups_by_size(pctrl, community);
1635 ret = intel_pinctrl_probe_pwm(pctrl, community);
1640 irq = platform_get_irq(pdev, 0);
1644 ret = intel_pinctrl_pm_init(pctrl);
1648 pctrl->pctldesc = intel_pinctrl_desc;
1649 pctrl->pctldesc.name = dev_name(dev);
1650 pctrl->pctldesc.pins = pctrl->soc->pins;
1651 pctrl->pctldesc.npins = pctrl->soc->npins;
1653 pctrl->pctldev = devm_pinctrl_register(dev, &pctrl->pctldesc, pctrl);
1654 if (IS_ERR(pctrl->pctldev)) {
1655 dev_err(dev, "failed to register pinctrl driver\n");
1656 return PTR_ERR(pctrl->pctldev);
1659 ret = intel_gpio_probe(pctrl, irq);
1663 platform_set_drvdata(pdev, pctrl);
1667 EXPORT_SYMBOL_NS_GPL(intel_pinctrl_probe, PINCTRL_INTEL);
1669 int intel_pinctrl_probe_by_hid(struct platform_device *pdev)
1671 const struct intel_pinctrl_soc_data *data;
1673 data = device_get_match_data(&pdev->dev);
1677 return intel_pinctrl_probe(pdev, data);
1679 EXPORT_SYMBOL_NS_GPL(intel_pinctrl_probe_by_hid, PINCTRL_INTEL);
1681 int intel_pinctrl_probe_by_uid(struct platform_device *pdev)
1683 const struct intel_pinctrl_soc_data *data;
1685 data = intel_pinctrl_get_soc_data(pdev);
1687 return PTR_ERR(data);
1689 return intel_pinctrl_probe(pdev, data);
1691 EXPORT_SYMBOL_NS_GPL(intel_pinctrl_probe_by_uid, PINCTRL_INTEL);
1693 const struct intel_pinctrl_soc_data *intel_pinctrl_get_soc_data(struct platform_device *pdev)
1695 const struct intel_pinctrl_soc_data * const *table;
1696 const struct intel_pinctrl_soc_data *data;
1697 struct device *dev = &pdev->dev;
1699 table = device_get_match_data(dev);
1701 struct acpi_device *adev = ACPI_COMPANION(dev);
1704 for (i = 0; table[i]; i++) {
1705 if (acpi_dev_uid_match(adev, table[i]->uid))
1710 const struct platform_device_id *id;
1712 id = platform_get_device_id(pdev);
1714 return ERR_PTR(-ENODEV);
1716 table = (const struct intel_pinctrl_soc_data * const *)id->driver_data;
1717 data = table[pdev->id];
1720 return data ?: ERR_PTR(-ENODATA);
1722 EXPORT_SYMBOL_NS_GPL(intel_pinctrl_get_soc_data, PINCTRL_INTEL);
1724 static bool __intel_gpio_is_direct_irq(u32 value)
1726 return (value & PADCFG0_GPIROUTIOXAPIC) &&
1727 (__intel_gpio_get_direction(value) == PAD_CONNECT_INPUT) &&
1728 (__intel_gpio_get_gpio_mode(value) == PADCFG0_PMODE_GPIO);
1731 static bool intel_pinctrl_should_save(struct intel_pinctrl *pctrl, unsigned int pin)
1733 const struct pin_desc *pd = pin_desc_get(pctrl->pctldev, pin);
1736 if (!pd || !intel_pad_usable(pctrl, pin))
1740 * Only restore the pin if it is actually in use by the kernel (or
1741 * by userspace). It is possible that some pins are used by the
1742 * BIOS during resume and those are not always locked down so leave
1745 if (pd->mux_owner || pd->gpio_owner ||
1746 gpiochip_line_is_irq(&pctrl->chip, intel_pin_to_gpio(pctrl, pin)))
1750 * The firmware on some systems may configure GPIO pins to be
1751 * an interrupt source in so called "direct IRQ" mode. In such
1752 * cases the GPIO controller driver has no idea if those pins
1753 * are being used or not. At the same time, there is a known bug
1754 * in the firmwares that don't restore the pin settings correctly
1755 * after suspend, i.e. by an unknown reason the Rx value becomes
1758 * Hence, let's save and restore the pins that are configured
1759 * as GPIOs in the input mode with GPIROUTIOXAPIC bit set.
1761 * See https://bugzilla.kernel.org/show_bug.cgi?id=214749.
1763 value = readl(intel_get_padcfg(pctrl, pin, PADCFG0));
1764 if (__intel_gpio_is_direct_irq(value))
1770 static int intel_pinctrl_suspend_noirq(struct device *dev)
1772 struct intel_pinctrl *pctrl = dev_get_drvdata(dev);
1773 struct intel_community_context *communities;
1774 struct intel_pad_context *pads;
1777 pads = pctrl->context.pads;
1778 for (i = 0; i < pctrl->soc->npins; i++) {
1779 const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
1780 void __iomem *padcfg;
1783 if (!intel_pinctrl_should_save(pctrl, desc->number))
1786 val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG0));
1787 pads[i].padcfg0 = val & ~PADCFG0_GPIORXSTATE;
1788 val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG1));
1789 pads[i].padcfg1 = val;
1791 padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG2);
1793 pads[i].padcfg2 = readl(padcfg);
1796 communities = pctrl->context.communities;
1797 for (i = 0; i < pctrl->ncommunities; i++) {
1798 struct intel_community *community = &pctrl->communities[i];
1802 base = community->regs + community->ie_offset;
1803 for (gpp = 0; gpp < community->ngpps; gpp++)
1804 communities[i].intmask[gpp] = readl(base + gpp * 4);
1806 base = community->regs + community->hostown_offset;
1807 for (gpp = 0; gpp < community->ngpps; gpp++)
1808 communities[i].hostown[gpp] = readl(base + gpp * 4);
1814 static bool intel_gpio_update_reg(void __iomem *reg, u32 mask, u32 value)
1820 updated = (curr & ~mask) | (value & mask);
1821 if (curr == updated)
1824 writel(updated, reg);
1828 static void intel_restore_hostown(struct intel_pinctrl *pctrl, unsigned int c,
1829 void __iomem *base, unsigned int gpp, u32 saved)
1831 const struct intel_community *community = &pctrl->communities[c];
1832 const struct intel_padgroup *padgrp = &community->gpps[gpp];
1833 struct device *dev = pctrl->dev;
1838 if (padgrp->gpio_base == INTEL_GPIO_BASE_NOMAP)
1841 for_each_requested_gpio_in_range(&pctrl->chip, i, padgrp->gpio_base, padgrp->size, dummy)
1842 requested |= BIT(i);
1844 if (!intel_gpio_update_reg(base + gpp * 4, requested, saved))
1847 dev_dbg(dev, "restored hostown %u/%u %#08x\n", c, gpp, readl(base + gpp * 4));
1850 static void intel_restore_intmask(struct intel_pinctrl *pctrl, unsigned int c,
1851 void __iomem *base, unsigned int gpp, u32 saved)
1853 struct device *dev = pctrl->dev;
1855 if (!intel_gpio_update_reg(base + gpp * 4, ~0U, saved))
1858 dev_dbg(dev, "restored mask %u/%u %#08x\n", c, gpp, readl(base + gpp * 4));
1861 static void intel_restore_padcfg(struct intel_pinctrl *pctrl, unsigned int pin,
1862 unsigned int reg, u32 saved)
1864 u32 mask = (reg == PADCFG0) ? PADCFG0_GPIORXSTATE : 0;
1865 unsigned int n = reg / sizeof(u32);
1866 struct device *dev = pctrl->dev;
1867 void __iomem *padcfg;
1869 padcfg = intel_get_padcfg(pctrl, pin, reg);
1873 if (!intel_gpio_update_reg(padcfg, ~mask, saved))
1876 dev_dbg(dev, "restored pin %u padcfg%u %#08x\n", pin, n, readl(padcfg));
1879 static int intel_pinctrl_resume_noirq(struct device *dev)
1881 struct intel_pinctrl *pctrl = dev_get_drvdata(dev);
1882 const struct intel_community_context *communities;
1883 const struct intel_pad_context *pads;
1886 /* Mask all interrupts */
1887 intel_gpio_irq_init(pctrl);
1889 pads = pctrl->context.pads;
1890 for (i = 0; i < pctrl->soc->npins; i++) {
1891 const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
1893 if (!(intel_pinctrl_should_save(pctrl, desc->number) ||
1895 * If the firmware mangled the register contents too much,
1896 * check the saved value for the Direct IRQ mode.
1898 __intel_gpio_is_direct_irq(pads[i].padcfg0)))
1901 intel_restore_padcfg(pctrl, desc->number, PADCFG0, pads[i].padcfg0);
1902 intel_restore_padcfg(pctrl, desc->number, PADCFG1, pads[i].padcfg1);
1903 intel_restore_padcfg(pctrl, desc->number, PADCFG2, pads[i].padcfg2);
1906 communities = pctrl->context.communities;
1907 for (i = 0; i < pctrl->ncommunities; i++) {
1908 struct intel_community *community = &pctrl->communities[i];
1912 base = community->regs + community->ie_offset;
1913 for (gpp = 0; gpp < community->ngpps; gpp++)
1914 intel_restore_intmask(pctrl, i, base, gpp, communities[i].intmask[gpp]);
1916 base = community->regs + community->hostown_offset;
1917 for (gpp = 0; gpp < community->ngpps; gpp++)
1918 intel_restore_hostown(pctrl, i, base, gpp, communities[i].hostown[gpp]);
1924 EXPORT_NS_GPL_DEV_SLEEP_PM_OPS(intel_pinctrl_pm_ops, PINCTRL_INTEL) = {
1925 NOIRQ_SYSTEM_SLEEP_PM_OPS(intel_pinctrl_suspend_noirq, intel_pinctrl_resume_noirq)
1928 MODULE_AUTHOR("Mathias Nyman <mathias.nyman@linux.intel.com>");
1929 MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
1930 MODULE_DESCRIPTION("Intel pinctrl/GPIO core driver");
1931 MODULE_LICENSE("GPL v2");