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/export.h>
13 #include <linux/gpio/driver.h>
14 #include <linux/interrupt.h>
15 #include <linux/log2.h>
16 #include <linux/module.h>
17 #include <linux/platform_device.h>
18 #include <linux/property.h>
19 #include <linux/seq_file.h>
20 #include <linux/string_helpers.h>
21 #include <linux/time.h>
23 #include <linux/pinctrl/consumer.h>
24 #include <linux/pinctrl/pinconf.h>
25 #include <linux/pinctrl/pinconf-generic.h>
26 #include <linux/pinctrl/pinctrl.h>
27 #include <linux/pinctrl/pinmux.h>
29 #include <linux/platform_data/x86/pwm-lpss.h>
32 #include "pinctrl-intel.h"
34 /* Offset from regs */
36 #define REVID_SHIFT 16
37 #define REVID_MASK GENMASK(31, 16)
40 #define CAPLIST_ID_SHIFT 16
41 #define CAPLIST_ID_MASK GENMASK(23, 16)
42 #define CAPLIST_ID_GPIO_HW_INFO 1
43 #define CAPLIST_ID_PWM 2
44 #define CAPLIST_ID_BLINK 3
45 #define CAPLIST_ID_EXP 4
46 #define CAPLIST_NEXT_SHIFT 0
47 #define CAPLIST_NEXT_MASK GENMASK(15, 0)
52 #define PADOWN_SHIFT(p) ((p) % 8 * PADOWN_BITS)
53 #define PADOWN_MASK(p) (GENMASK(3, 0) << PADOWN_SHIFT(p))
54 #define PADOWN_GPP(p) ((p) / 8)
58 /* Offset from pad_regs */
60 #define PADCFG0_RXEVCFG_MASK GENMASK(26, 25)
61 #define PADCFG0_RXEVCFG_LEVEL (0 << 25)
62 #define PADCFG0_RXEVCFG_EDGE (1 << 25)
63 #define PADCFG0_RXEVCFG_DISABLED (2 << 25)
64 #define PADCFG0_RXEVCFG_EDGE_BOTH (3 << 25)
65 #define PADCFG0_PREGFRXSEL BIT(24)
66 #define PADCFG0_RXINV BIT(23)
67 #define PADCFG0_GPIROUTIOXAPIC BIT(20)
68 #define PADCFG0_GPIROUTSCI BIT(19)
69 #define PADCFG0_GPIROUTSMI BIT(18)
70 #define PADCFG0_GPIROUTNMI BIT(17)
71 #define PADCFG0_PMODE_SHIFT 10
72 #define PADCFG0_PMODE_MASK GENMASK(13, 10)
73 #define PADCFG0_PMODE_GPIO 0
74 #define PADCFG0_GPIODIS_SHIFT 8
75 #define PADCFG0_GPIODIS_MASK GENMASK(9, 8)
76 #define PADCFG0_GPIODIS_NONE 0
77 #define PADCFG0_GPIODIS_OUTPUT 1
78 #define PADCFG0_GPIODIS_INPUT 2
79 #define PADCFG0_GPIODIS_FULL 3
80 #define PADCFG0_GPIORXDIS BIT(9)
81 #define PADCFG0_GPIOTXDIS BIT(8)
82 #define PADCFG0_GPIORXSTATE BIT(1)
83 #define PADCFG0_GPIOTXSTATE BIT(0)
86 #define PADCFG1_TERM_UP BIT(13)
87 #define PADCFG1_TERM_SHIFT 10
88 #define PADCFG1_TERM_MASK GENMASK(12, 10)
90 * Bit 0 Bit 1 Bit 2 Value, Ohms
96 * 1 0 0 1000 (if supported)
97 * 1 0 1 ~952 (if supported)
98 * 1 1 0 ~833 (if supported)
99 * 1 1 1 ~800 (if supported)
101 #define PADCFG1_TERM_20K BIT(2)
102 #define PADCFG1_TERM_5K BIT(1)
103 #define PADCFG1_TERM_4K (BIT(2) | BIT(1))
104 #define PADCFG1_TERM_1K BIT(0)
105 #define PADCFG1_TERM_952 (BIT(2) | BIT(0))
106 #define PADCFG1_TERM_833 (BIT(1) | BIT(0))
107 #define PADCFG1_TERM_800 (BIT(2) | BIT(1) | BIT(0))
109 #define PADCFG2 0x008
110 #define PADCFG2_DEBOUNCE_SHIFT 1
111 #define PADCFG2_DEBOUNCE_MASK GENMASK(4, 1)
112 #define PADCFG2_DEBEN BIT(0)
114 #define DEBOUNCE_PERIOD_NSEC 31250
116 struct intel_pad_context {
122 struct intel_community_context {
127 #define pin_to_padno(c, p) ((p) - (c)->pin_base)
128 #define padgroup_offset(g, p) ((p) - (g)->base)
130 #define for_each_intel_pin_community(pctrl, community) \
131 for (unsigned int __ci = 0; \
132 __ci < pctrl->ncommunities && (community = &pctrl->communities[__ci]); \
135 #define for_each_intel_community_pad_group(community, grp) \
136 for (unsigned int __gi = 0; \
137 __gi < community->ngpps && (grp = &community->gpps[__gi]); \
140 #define for_each_intel_pad_group(pctrl, community, grp) \
141 for_each_intel_pin_community(pctrl, community) \
142 for_each_intel_community_pad_group(community, grp)
144 #define for_each_intel_gpio_group(pctrl, community, grp) \
145 for_each_intel_pad_group(pctrl, community, grp) \
146 if (grp->gpio_base == INTEL_GPIO_BASE_NOMAP) {} else
148 const struct intel_community *intel_get_community(const struct intel_pinctrl *pctrl,
151 const struct intel_community *community;
153 for_each_intel_pin_community(pctrl, community) {
154 if (pin >= community->pin_base &&
155 pin < community->pin_base + community->npins)
159 dev_warn(pctrl->dev, "failed to find community for pin %u\n", pin);
162 EXPORT_SYMBOL_NS_GPL(intel_get_community, "PINCTRL_INTEL");
164 static const struct intel_padgroup *
165 intel_community_get_padgroup(const struct intel_community *community,
168 const struct intel_padgroup *padgrp;
170 for_each_intel_community_pad_group(community, padgrp) {
171 if (pin >= padgrp->base && pin < padgrp->base + padgrp->size)
178 static void __iomem *intel_get_padcfg(struct intel_pinctrl *pctrl,
179 unsigned int pin, unsigned int reg)
181 const struct intel_community *community;
185 community = intel_get_community(pctrl, pin);
189 padno = pin_to_padno(community, pin);
190 nregs = (community->features & PINCTRL_FEATURE_DEBOUNCE) ? 4 : 2;
192 if (reg >= nregs * 4)
195 return community->pad_regs + reg + padno * nregs * 4;
198 static bool intel_pad_owned_by_host(const struct intel_pinctrl *pctrl, unsigned int pin)
200 const struct intel_community *community;
201 const struct intel_padgroup *padgrp;
202 unsigned int gpp, offset, gpp_offset;
203 void __iomem *padown;
205 community = intel_get_community(pctrl, pin);
208 if (!community->padown_offset)
211 padgrp = intel_community_get_padgroup(community, pin);
215 gpp_offset = padgroup_offset(padgrp, pin);
216 gpp = PADOWN_GPP(gpp_offset);
217 offset = community->padown_offset + padgrp->padown_num * 4 + gpp * 4;
218 padown = community->regs + offset;
220 return !(readl(padown) & PADOWN_MASK(gpp_offset));
223 static bool intel_pad_acpi_mode(const struct intel_pinctrl *pctrl, unsigned int pin)
225 const struct intel_community *community;
226 const struct intel_padgroup *padgrp;
227 unsigned int offset, gpp_offset;
228 void __iomem *hostown;
230 community = intel_get_community(pctrl, pin);
233 if (!community->hostown_offset)
236 padgrp = intel_community_get_padgroup(community, pin);
240 gpp_offset = padgroup_offset(padgrp, pin);
241 offset = community->hostown_offset + padgrp->reg_num * 4;
242 hostown = community->regs + offset;
244 return !(readl(hostown) & BIT(gpp_offset));
248 * enum - Locking variants of the pad configuration
249 * @PAD_UNLOCKED: pad is fully controlled by the configuration registers
250 * @PAD_LOCKED: pad configuration registers, except TX state, are locked
251 * @PAD_LOCKED_TX: pad configuration TX state is locked
252 * @PAD_LOCKED_FULL: pad configuration registers are locked completely
254 * Locking is considered as read-only mode for corresponding registers and
255 * their respective fields. That said, TX state bit is locked separately from
256 * the main locking scheme.
262 PAD_LOCKED_FULL = PAD_LOCKED | PAD_LOCKED_TX,
265 static int intel_pad_locked(const struct intel_pinctrl *pctrl, unsigned int pin)
267 const struct intel_community *community;
268 const struct intel_padgroup *padgrp;
269 unsigned int offset, gpp_offset;
271 int ret = PAD_UNLOCKED;
273 community = intel_get_community(pctrl, pin);
275 return PAD_LOCKED_FULL;
276 if (!community->padcfglock_offset)
279 padgrp = intel_community_get_padgroup(community, pin);
281 return PAD_LOCKED_FULL;
283 gpp_offset = padgroup_offset(padgrp, pin);
286 * If PADCFGLOCK and PADCFGLOCKTX bits are both clear for this pad,
287 * the pad is considered unlocked. Any other case means that it is
288 * either fully or partially locked.
290 offset = community->padcfglock_offset + 0 + padgrp->reg_num * 8;
291 value = readl(community->regs + offset);
292 if (value & BIT(gpp_offset))
295 offset = community->padcfglock_offset + 4 + padgrp->reg_num * 8;
296 value = readl(community->regs + offset);
297 if (value & BIT(gpp_offset))
298 ret |= PAD_LOCKED_TX;
303 static bool intel_pad_is_unlocked(const struct intel_pinctrl *pctrl, unsigned int pin)
305 return (intel_pad_locked(pctrl, pin) & PAD_LOCKED) == PAD_UNLOCKED;
308 static bool intel_pad_usable(const struct intel_pinctrl *pctrl, unsigned int pin)
310 return intel_pad_owned_by_host(pctrl, pin) && intel_pad_is_unlocked(pctrl, pin);
313 int intel_get_groups_count(struct pinctrl_dev *pctldev)
315 const struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
317 return pctrl->soc->ngroups;
319 EXPORT_SYMBOL_NS_GPL(intel_get_groups_count, "PINCTRL_INTEL");
321 const char *intel_get_group_name(struct pinctrl_dev *pctldev, unsigned int group)
323 const struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
325 return pctrl->soc->groups[group].grp.name;
327 EXPORT_SYMBOL_NS_GPL(intel_get_group_name, "PINCTRL_INTEL");
329 int intel_get_group_pins(struct pinctrl_dev *pctldev, unsigned int group,
330 const unsigned int **pins, unsigned int *npins)
332 const struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
334 *pins = pctrl->soc->groups[group].grp.pins;
335 *npins = pctrl->soc->groups[group].grp.npins;
338 EXPORT_SYMBOL_NS_GPL(intel_get_group_pins, "PINCTRL_INTEL");
340 static void intel_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
343 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
344 void __iomem *padcfg;
345 u32 cfg0, cfg1, mode;
349 if (!intel_pad_owned_by_host(pctrl, pin)) {
350 seq_puts(s, "not available");
354 cfg0 = readl(intel_get_padcfg(pctrl, pin, PADCFG0));
355 cfg1 = readl(intel_get_padcfg(pctrl, pin, PADCFG1));
357 mode = (cfg0 & PADCFG0_PMODE_MASK) >> PADCFG0_PMODE_SHIFT;
358 if (mode == PADCFG0_PMODE_GPIO)
359 seq_puts(s, "GPIO ");
361 seq_printf(s, "mode %d ", mode);
363 seq_printf(s, "0x%08x 0x%08x", cfg0, cfg1);
365 /* Dump the additional PADCFG registers if available */
366 padcfg = intel_get_padcfg(pctrl, pin, PADCFG2);
368 seq_printf(s, " 0x%08x", readl(padcfg));
370 locked = intel_pad_locked(pctrl, pin);
371 acpi = intel_pad_acpi_mode(pctrl, pin);
373 if (locked || acpi) {
376 seq_puts(s, "LOCKED");
377 if ((locked & PAD_LOCKED_FULL) == PAD_LOCKED_TX)
379 else if ((locked & PAD_LOCKED_FULL) == PAD_LOCKED_FULL)
380 seq_puts(s, " full");
391 static const struct pinctrl_ops intel_pinctrl_ops = {
392 .get_groups_count = intel_get_groups_count,
393 .get_group_name = intel_get_group_name,
394 .get_group_pins = intel_get_group_pins,
395 .pin_dbg_show = intel_pin_dbg_show,
398 int intel_get_functions_count(struct pinctrl_dev *pctldev)
400 const struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
402 return pctrl->soc->nfunctions;
404 EXPORT_SYMBOL_NS_GPL(intel_get_functions_count, "PINCTRL_INTEL");
406 const char *intel_get_function_name(struct pinctrl_dev *pctldev, unsigned int function)
408 const struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
410 return pctrl->soc->functions[function].func.name;
412 EXPORT_SYMBOL_NS_GPL(intel_get_function_name, "PINCTRL_INTEL");
414 int intel_get_function_groups(struct pinctrl_dev *pctldev, unsigned int function,
415 const char * const **groups, unsigned int * const ngroups)
417 const struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
419 *groups = pctrl->soc->functions[function].func.groups;
420 *ngroups = pctrl->soc->functions[function].func.ngroups;
423 EXPORT_SYMBOL_NS_GPL(intel_get_function_groups, "PINCTRL_INTEL");
425 static int intel_pinmux_set_mux(struct pinctrl_dev *pctldev,
426 unsigned int function, unsigned int group)
428 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
429 const struct intel_pingroup *grp = &pctrl->soc->groups[group];
432 guard(raw_spinlock_irqsave)(&pctrl->lock);
435 * All pins in the groups needs to be accessible and writable
436 * before we can enable the mux for this group.
438 for (i = 0; i < grp->grp.npins; i++) {
439 if (!intel_pad_usable(pctrl, grp->grp.pins[i]))
443 /* Now enable the mux setting for each pin in the group */
444 for (i = 0; i < grp->grp.npins; i++) {
445 void __iomem *padcfg0;
448 padcfg0 = intel_get_padcfg(pctrl, grp->grp.pins[i], PADCFG0);
450 value = readl(padcfg0);
451 value &= ~PADCFG0_PMODE_MASK;
454 pmode = grp->modes[i];
458 value |= pmode << PADCFG0_PMODE_SHIFT;
459 writel(value, padcfg0);
466 * enum - Possible pad physical connections
467 * @PAD_CONNECT_NONE: pad is fully disconnected
468 * @PAD_CONNECT_INPUT: pad is in input only mode
469 * @PAD_CONNECT_OUTPUT: pad is in output only mode
470 * @PAD_CONNECT_FULL: pad is fully connected
473 PAD_CONNECT_NONE = 0,
474 PAD_CONNECT_INPUT = 1,
475 PAD_CONNECT_OUTPUT = 2,
476 PAD_CONNECT_FULL = PAD_CONNECT_INPUT | PAD_CONNECT_OUTPUT,
479 static int __intel_gpio_get_direction(u32 value)
481 switch ((value & PADCFG0_GPIODIS_MASK) >> PADCFG0_GPIODIS_SHIFT) {
482 case PADCFG0_GPIODIS_FULL:
483 return PAD_CONNECT_NONE;
484 case PADCFG0_GPIODIS_OUTPUT:
485 return PAD_CONNECT_INPUT;
486 case PADCFG0_GPIODIS_INPUT:
487 return PAD_CONNECT_OUTPUT;
488 case PADCFG0_GPIODIS_NONE:
489 return PAD_CONNECT_FULL;
495 static u32 __intel_gpio_set_direction(u32 value, bool input, bool output)
498 value &= ~PADCFG0_GPIORXDIS;
500 value |= PADCFG0_GPIORXDIS;
503 value &= ~PADCFG0_GPIOTXDIS;
505 value |= PADCFG0_GPIOTXDIS;
510 static int __intel_gpio_get_gpio_mode(u32 value)
512 return (value & PADCFG0_PMODE_MASK) >> PADCFG0_PMODE_SHIFT;
515 static int intel_gpio_get_gpio_mode(void __iomem *padcfg0)
517 return __intel_gpio_get_gpio_mode(readl(padcfg0));
520 static void intel_gpio_set_gpio_mode(void __iomem *padcfg0)
524 value = readl(padcfg0);
526 /* Put the pad into GPIO mode */
527 value &= ~PADCFG0_PMODE_MASK;
528 value |= PADCFG0_PMODE_GPIO;
530 /* Disable TX buffer and enable RX (this will be input) */
531 value = __intel_gpio_set_direction(value, true, false);
533 /* Disable SCI/SMI/NMI generation */
534 value &= ~(PADCFG0_GPIROUTIOXAPIC | PADCFG0_GPIROUTSCI);
535 value &= ~(PADCFG0_GPIROUTSMI | PADCFG0_GPIROUTNMI);
537 writel(value, padcfg0);
540 static int intel_gpio_request_enable(struct pinctrl_dev *pctldev,
541 struct pinctrl_gpio_range *range,
544 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
545 void __iomem *padcfg0;
547 padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
549 guard(raw_spinlock_irqsave)(&pctrl->lock);
551 if (!intel_pad_owned_by_host(pctrl, pin))
554 if (!intel_pad_is_unlocked(pctrl, pin))
558 * If pin is already configured in GPIO mode, we assume that
559 * firmware provides correct settings. In such case we avoid
560 * potential glitches on the pin. Otherwise, for the pin in
561 * alternative mode, consumer has to supply respective flags.
563 if (intel_gpio_get_gpio_mode(padcfg0) == PADCFG0_PMODE_GPIO)
566 intel_gpio_set_gpio_mode(padcfg0);
571 static int intel_gpio_set_direction(struct pinctrl_dev *pctldev,
572 struct pinctrl_gpio_range *range,
573 unsigned int pin, bool input)
575 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
576 void __iomem *padcfg0;
579 padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
581 guard(raw_spinlock_irqsave)(&pctrl->lock);
583 value = readl(padcfg0);
585 value = __intel_gpio_set_direction(value, true, false);
587 value = __intel_gpio_set_direction(value, false, true);
588 writel(value, padcfg0);
593 static const struct pinmux_ops intel_pinmux_ops = {
594 .get_functions_count = intel_get_functions_count,
595 .get_function_name = intel_get_function_name,
596 .get_function_groups = intel_get_function_groups,
597 .set_mux = intel_pinmux_set_mux,
598 .gpio_request_enable = intel_gpio_request_enable,
599 .gpio_set_direction = intel_gpio_set_direction,
602 static int intel_config_get_pull(struct intel_pinctrl *pctrl, unsigned int pin,
603 enum pin_config_param param, u32 *arg)
605 void __iomem *padcfg1;
608 padcfg1 = intel_get_padcfg(pctrl, pin, PADCFG1);
610 scoped_guard(raw_spinlock_irqsave, &pctrl->lock)
611 value = readl(padcfg1);
613 term = (value & PADCFG1_TERM_MASK) >> PADCFG1_TERM_SHIFT;
616 case PIN_CONFIG_BIAS_DISABLE:
621 case PIN_CONFIG_BIAS_PULL_UP:
622 if (!term || !(value & PADCFG1_TERM_UP))
626 case PADCFG1_TERM_833:
629 case PADCFG1_TERM_1K:
632 case PADCFG1_TERM_4K:
635 case PADCFG1_TERM_5K:
638 case PADCFG1_TERM_20K:
645 case PIN_CONFIG_BIAS_PULL_DOWN: {
646 const struct intel_community *community = intel_get_community(pctrl, pin);
648 if (!term || value & PADCFG1_TERM_UP)
652 case PADCFG1_TERM_833:
653 if (!(community->features & PINCTRL_FEATURE_1K_PD))
657 case PADCFG1_TERM_1K:
658 if (!(community->features & PINCTRL_FEATURE_1K_PD))
662 case PADCFG1_TERM_4K:
665 case PADCFG1_TERM_5K:
668 case PADCFG1_TERM_20K:
683 static int intel_config_get_high_impedance(struct intel_pinctrl *pctrl, unsigned int pin,
684 enum pin_config_param param, u32 *arg)
686 void __iomem *padcfg0;
689 padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
691 scoped_guard(raw_spinlock_irqsave, &pctrl->lock)
692 value = readl(padcfg0);
694 if (__intel_gpio_get_direction(value) != PAD_CONNECT_NONE)
700 static int intel_config_get_debounce(struct intel_pinctrl *pctrl, unsigned int pin,
701 enum pin_config_param param, u32 *arg)
703 void __iomem *padcfg2;
707 padcfg2 = intel_get_padcfg(pctrl, pin, PADCFG2);
711 scoped_guard(raw_spinlock_irqsave, &pctrl->lock)
712 value2 = readl(padcfg2);
714 if (!(value2 & PADCFG2_DEBEN))
717 v = (value2 & PADCFG2_DEBOUNCE_MASK) >> PADCFG2_DEBOUNCE_SHIFT;
718 *arg = BIT(v) * DEBOUNCE_PERIOD_NSEC / NSEC_PER_USEC;
723 static int intel_config_get(struct pinctrl_dev *pctldev, unsigned int pin,
724 unsigned long *config)
726 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
727 enum pin_config_param param = pinconf_to_config_param(*config);
731 if (!intel_pad_owned_by_host(pctrl, pin))
735 case PIN_CONFIG_BIAS_DISABLE:
736 case PIN_CONFIG_BIAS_PULL_UP:
737 case PIN_CONFIG_BIAS_PULL_DOWN:
738 ret = intel_config_get_pull(pctrl, pin, param, &arg);
743 case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
744 ret = intel_config_get_high_impedance(pctrl, pin, param, &arg);
749 case PIN_CONFIG_INPUT_DEBOUNCE:
750 ret = intel_config_get_debounce(pctrl, pin, param, &arg);
759 *config = pinconf_to_config_packed(param, arg);
763 static int intel_config_set_pull(struct intel_pinctrl *pctrl, unsigned int pin,
764 unsigned long config)
766 unsigned int param = pinconf_to_config_param(config);
767 unsigned int arg = pinconf_to_config_argument(config);
768 u32 term = 0, up = 0, value;
769 void __iomem *padcfg1;
772 case PIN_CONFIG_BIAS_DISABLE:
775 case PIN_CONFIG_BIAS_PULL_UP:
778 term = PADCFG1_TERM_20K;
780 case 1: /* Set default strength value in case none is given */
782 term = PADCFG1_TERM_5K;
785 term = PADCFG1_TERM_4K;
788 term = PADCFG1_TERM_1K;
791 term = PADCFG1_TERM_833;
797 up = PADCFG1_TERM_UP;
800 case PIN_CONFIG_BIAS_PULL_DOWN: {
801 const struct intel_community *community = intel_get_community(pctrl, pin);
805 term = PADCFG1_TERM_20K;
807 case 1: /* Set default strength value in case none is given */
809 term = PADCFG1_TERM_5K;
812 term = PADCFG1_TERM_4K;
815 if (!(community->features & PINCTRL_FEATURE_1K_PD))
817 term = PADCFG1_TERM_1K;
820 if (!(community->features & PINCTRL_FEATURE_1K_PD))
822 term = PADCFG1_TERM_833;
835 padcfg1 = intel_get_padcfg(pctrl, pin, PADCFG1);
837 guard(raw_spinlock_irqsave)(&pctrl->lock);
839 value = readl(padcfg1);
840 value = (value & ~PADCFG1_TERM_MASK) | (term << PADCFG1_TERM_SHIFT);
841 value = (value & ~PADCFG1_TERM_UP) | up;
842 writel(value, padcfg1);
847 static void intel_gpio_set_high_impedance(struct intel_pinctrl *pctrl, unsigned int pin)
849 void __iomem *padcfg0;
852 padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
854 guard(raw_spinlock_irqsave)(&pctrl->lock);
856 value = readl(padcfg0);
857 value = __intel_gpio_set_direction(value, false, false);
858 writel(value, padcfg0);
861 static int intel_config_set_debounce(struct intel_pinctrl *pctrl,
862 unsigned int pin, unsigned int debounce)
864 void __iomem *padcfg0, *padcfg2;
869 v = order_base_2(debounce * NSEC_PER_USEC / DEBOUNCE_PERIOD_NSEC);
876 padcfg2 = intel_get_padcfg(pctrl, pin, PADCFG2);
880 padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
882 guard(raw_spinlock_irqsave)(&pctrl->lock);
884 value0 = readl(padcfg0);
885 value2 = readl(padcfg2);
887 value2 = (value2 & ~PADCFG2_DEBOUNCE_MASK) | (v << PADCFG2_DEBOUNCE_SHIFT);
889 /* Enable glitch filter and debouncer */
890 value0 |= PADCFG0_PREGFRXSEL;
891 value2 |= PADCFG2_DEBEN;
893 /* Disable glitch filter and debouncer */
894 value0 &= ~PADCFG0_PREGFRXSEL;
895 value2 &= ~PADCFG2_DEBEN;
898 writel(value0, padcfg0);
899 writel(value2, padcfg2);
904 static int intel_config_set(struct pinctrl_dev *pctldev, unsigned int pin,
905 unsigned long *configs, unsigned int nconfigs)
907 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
910 if (!intel_pad_usable(pctrl, pin))
913 for (i = 0; i < nconfigs; i++) {
914 switch (pinconf_to_config_param(configs[i])) {
915 case PIN_CONFIG_BIAS_DISABLE:
916 case PIN_CONFIG_BIAS_PULL_UP:
917 case PIN_CONFIG_BIAS_PULL_DOWN:
918 ret = intel_config_set_pull(pctrl, pin, configs[i]);
923 case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
924 intel_gpio_set_high_impedance(pctrl, pin);
927 case PIN_CONFIG_INPUT_DEBOUNCE:
928 ret = intel_config_set_debounce(pctrl, pin,
929 pinconf_to_config_argument(configs[i]));
942 static const struct pinconf_ops intel_pinconf_ops = {
944 .pin_config_get = intel_config_get,
945 .pin_config_set = intel_config_set,
948 static const struct pinctrl_desc intel_pinctrl_desc = {
949 .pctlops = &intel_pinctrl_ops,
950 .pmxops = &intel_pinmux_ops,
951 .confops = &intel_pinconf_ops,
952 .owner = THIS_MODULE,
956 * intel_gpio_to_pin() - Translate from GPIO offset to pin number
957 * @pctrl: Pinctrl structure
958 * @offset: GPIO offset from gpiolib
959 * @community: Community is filled here if not %NULL
960 * @padgrp: Pad group is filled here if not %NULL
962 * When coming through gpiolib irqchip, the GPIO offset is not
963 * automatically translated to pinctrl pin number. This function can be
964 * used to find out the corresponding pinctrl pin.
966 * Return: a pin number and pointers to the community and pad group, which
967 * the pin belongs to, or negative error code if translation can't be done.
969 static int intel_gpio_to_pin(const struct intel_pinctrl *pctrl, unsigned int offset,
970 const struct intel_community **community,
971 const struct intel_padgroup **padgrp)
973 const struct intel_community *comm;
974 const struct intel_padgroup *grp;
976 for_each_intel_gpio_group(pctrl, comm, grp) {
977 if (offset >= grp->gpio_base && offset < grp->gpio_base + grp->size) {
983 return grp->base + offset - grp->gpio_base;
991 * intel_pin_to_gpio() - Translate from pin number to GPIO offset
992 * @pctrl: Pinctrl structure
995 * Translate the pin number of pinctrl to GPIO offset
997 * Return: a GPIO offset, or negative error code if translation can't be done.
999 static int intel_pin_to_gpio(const struct intel_pinctrl *pctrl, int pin)
1001 const struct intel_community *community;
1002 const struct intel_padgroup *padgrp;
1004 community = intel_get_community(pctrl, pin);
1008 padgrp = intel_community_get_padgroup(community, pin);
1012 return pin - padgrp->base + padgrp->gpio_base;
1015 static int intel_gpio_get(struct gpio_chip *chip, unsigned int offset)
1017 struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
1022 pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL);
1026 reg = intel_get_padcfg(pctrl, pin, PADCFG0);
1030 padcfg0 = readl(reg);
1031 if (__intel_gpio_get_direction(padcfg0) & PAD_CONNECT_OUTPUT)
1032 return !!(padcfg0 & PADCFG0_GPIOTXSTATE);
1034 return !!(padcfg0 & PADCFG0_GPIORXSTATE);
1037 static int intel_gpio_set(struct gpio_chip *chip, unsigned int offset,
1040 struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
1045 pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL);
1049 reg = intel_get_padcfg(pctrl, pin, PADCFG0);
1053 guard(raw_spinlock_irqsave)(&pctrl->lock);
1055 padcfg0 = readl(reg);
1057 padcfg0 |= PADCFG0_GPIOTXSTATE;
1059 padcfg0 &= ~PADCFG0_GPIOTXSTATE;
1060 writel(padcfg0, reg);
1065 static int intel_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
1067 struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
1072 pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL);
1076 reg = intel_get_padcfg(pctrl, pin, PADCFG0);
1080 scoped_guard(raw_spinlock_irqsave, &pctrl->lock)
1081 padcfg0 = readl(reg);
1083 if (padcfg0 & PADCFG0_PMODE_MASK)
1086 if (__intel_gpio_get_direction(padcfg0) & PAD_CONNECT_OUTPUT)
1087 return GPIO_LINE_DIRECTION_OUT;
1089 return GPIO_LINE_DIRECTION_IN;
1092 static int intel_gpio_direction_input(struct gpio_chip *chip, unsigned int offset)
1094 return pinctrl_gpio_direction_input(chip, offset);
1097 static int intel_gpio_direction_output(struct gpio_chip *chip, unsigned int offset,
1102 ret = intel_gpio_set(chip, offset, value);
1106 return pinctrl_gpio_direction_output(chip, offset);
1109 static const struct gpio_chip intel_gpio_chip = {
1110 .owner = THIS_MODULE,
1111 .request = gpiochip_generic_request,
1112 .free = gpiochip_generic_free,
1113 .get_direction = intel_gpio_get_direction,
1114 .direction_input = intel_gpio_direction_input,
1115 .direction_output = intel_gpio_direction_output,
1116 .get = intel_gpio_get,
1117 .set = intel_gpio_set,
1118 .set_config = gpiochip_generic_config,
1121 static void intel_gpio_irq_ack(struct irq_data *d)
1123 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1124 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
1125 const struct intel_community *community;
1126 const struct intel_padgroup *padgrp;
1129 pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), &community, &padgrp);
1131 unsigned int gpp, gpp_offset;
1134 gpp = padgrp->reg_num;
1135 gpp_offset = padgroup_offset(padgrp, pin);
1137 is = community->regs + community->is_offset + gpp * 4;
1139 guard(raw_spinlock)(&pctrl->lock);
1141 writel(BIT(gpp_offset), is);
1145 static void intel_gpio_irq_mask_unmask(struct gpio_chip *gc, irq_hw_number_t hwirq, bool mask)
1147 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
1148 const struct intel_community *community;
1149 const struct intel_padgroup *padgrp;
1152 pin = intel_gpio_to_pin(pctrl, hwirq, &community, &padgrp);
1154 unsigned int gpp, gpp_offset;
1155 void __iomem *reg, *is;
1158 gpp = padgrp->reg_num;
1159 gpp_offset = padgroup_offset(padgrp, pin);
1161 reg = community->regs + community->ie_offset + gpp * 4;
1162 is = community->regs + community->is_offset + gpp * 4;
1164 guard(raw_spinlock_irqsave)(&pctrl->lock);
1166 /* Clear interrupt status first to avoid unexpected interrupt */
1167 writel(BIT(gpp_offset), is);
1171 value &= ~BIT(gpp_offset);
1173 value |= BIT(gpp_offset);
1178 static void intel_gpio_irq_mask(struct irq_data *d)
1180 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1181 irq_hw_number_t hwirq = irqd_to_hwirq(d);
1183 intel_gpio_irq_mask_unmask(gc, hwirq, true);
1184 gpiochip_disable_irq(gc, hwirq);
1187 static void intel_gpio_irq_unmask(struct irq_data *d)
1189 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1190 irq_hw_number_t hwirq = irqd_to_hwirq(d);
1192 gpiochip_enable_irq(gc, hwirq);
1193 intel_gpio_irq_mask_unmask(gc, hwirq, false);
1196 static int intel_gpio_irq_type(struct irq_data *d, unsigned int type)
1198 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1199 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
1200 unsigned int pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), NULL, NULL);
1201 u32 rxevcfg, rxinv, value;
1204 reg = intel_get_padcfg(pctrl, pin, PADCFG0);
1209 * If the pin is in ACPI mode it is still usable as a GPIO but it
1210 * cannot be used as IRQ because GPI_IS status bit will not be
1211 * updated by the host controller hardware.
1213 if (intel_pad_acpi_mode(pctrl, pin)) {
1214 dev_warn(pctrl->dev, "pin %u cannot be used as IRQ\n", pin);
1218 if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
1219 rxevcfg = PADCFG0_RXEVCFG_EDGE_BOTH;
1220 } else if (type & IRQ_TYPE_EDGE_FALLING) {
1221 rxevcfg = PADCFG0_RXEVCFG_EDGE;
1222 } else if (type & IRQ_TYPE_EDGE_RISING) {
1223 rxevcfg = PADCFG0_RXEVCFG_EDGE;
1224 } else if (type & IRQ_TYPE_LEVEL_MASK) {
1225 rxevcfg = PADCFG0_RXEVCFG_LEVEL;
1227 rxevcfg = PADCFG0_RXEVCFG_DISABLED;
1230 if (type == IRQ_TYPE_EDGE_FALLING || type == IRQ_TYPE_LEVEL_LOW)
1231 rxinv = PADCFG0_RXINV;
1235 guard(raw_spinlock_irqsave)(&pctrl->lock);
1237 intel_gpio_set_gpio_mode(reg);
1241 value = (value & ~PADCFG0_RXEVCFG_MASK) | rxevcfg;
1242 value = (value & ~PADCFG0_RXINV) | rxinv;
1246 if (type & IRQ_TYPE_EDGE_BOTH)
1247 irq_set_handler_locked(d, handle_edge_irq);
1248 else if (type & IRQ_TYPE_LEVEL_MASK)
1249 irq_set_handler_locked(d, handle_level_irq);
1254 static int intel_gpio_irq_wake(struct irq_data *d, unsigned int on)
1256 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1257 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
1258 unsigned int pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), NULL, NULL);
1261 enable_irq_wake(pctrl->irq);
1263 disable_irq_wake(pctrl->irq);
1265 dev_dbg(pctrl->dev, "%s wake for pin %u\n", str_enable_disable(on), pin);
1269 static const struct irq_chip intel_gpio_irq_chip = {
1270 .name = "intel-gpio",
1271 .irq_ack = intel_gpio_irq_ack,
1272 .irq_mask = intel_gpio_irq_mask,
1273 .irq_unmask = intel_gpio_irq_unmask,
1274 .irq_set_type = intel_gpio_irq_type,
1275 .irq_set_wake = intel_gpio_irq_wake,
1276 .flags = IRQCHIP_MASK_ON_SUSPEND | IRQCHIP_IMMUTABLE,
1277 GPIOCHIP_IRQ_RESOURCE_HELPERS,
1280 static irqreturn_t intel_gpio_irq(int irq, void *data)
1282 const struct intel_community *community;
1283 const struct intel_padgroup *padgrp;
1284 struct intel_pinctrl *pctrl = data;
1287 /* Need to check all communities for pending interrupts */
1288 for_each_intel_pad_group(pctrl, community, padgrp) {
1289 struct gpio_chip *gc = &pctrl->chip;
1290 unsigned long pending, enabled;
1291 unsigned int gpp, gpp_offset;
1292 void __iomem *reg, *is;
1294 gpp = padgrp->reg_num;
1296 reg = community->regs + community->ie_offset + gpp * 4;
1297 is = community->regs + community->is_offset + gpp * 4;
1299 scoped_guard(raw_spinlock, &pctrl->lock) {
1300 pending = readl(is);
1301 enabled = readl(reg);
1304 /* Only interrupts that are enabled */
1307 for_each_set_bit(gpp_offset, &pending, padgrp->size)
1308 generic_handle_domain_irq(gc->irq.domain, padgrp->gpio_base + gpp_offset);
1310 ret += pending ? 1 : 0;
1313 return IRQ_RETVAL(ret);
1316 static void intel_gpio_irq_init(struct intel_pinctrl *pctrl)
1318 const struct intel_community *community;
1320 for_each_intel_pin_community(pctrl, community) {
1321 void __iomem *reg, *is;
1324 for (gpp = 0; gpp < community->ngpps; gpp++) {
1325 reg = community->regs + community->ie_offset + gpp * 4;
1326 is = community->regs + community->is_offset + gpp * 4;
1328 /* Mask and clear all interrupts */
1335 static int intel_gpio_irq_init_hw(struct gpio_chip *gc)
1337 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
1340 * Make sure the interrupt lines are in a proper state before
1341 * further configuration.
1343 intel_gpio_irq_init(pctrl);
1348 static int intel_gpio_add_pin_ranges(struct gpio_chip *gc)
1350 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
1351 const struct intel_community *community;
1352 const struct intel_padgroup *grp;
1355 for_each_intel_gpio_group(pctrl, community, grp) {
1356 ret = gpiochip_add_pin_range(&pctrl->chip, dev_name(pctrl->dev),
1357 grp->gpio_base, grp->base,
1360 dev_err(pctrl->dev, "failed to add GPIO pin range\n");
1368 static unsigned int intel_gpio_ngpio(const struct intel_pinctrl *pctrl)
1370 const struct intel_community *community;
1371 const struct intel_padgroup *grp;
1372 unsigned int ngpio = 0;
1374 for_each_intel_gpio_group(pctrl, community, grp) {
1375 if (grp->gpio_base + grp->size > ngpio)
1376 ngpio = grp->gpio_base + grp->size;
1382 static int intel_gpio_probe(struct intel_pinctrl *pctrl, int irq)
1385 struct gpio_irq_chip *girq;
1387 pctrl->chip = intel_gpio_chip;
1389 /* Setup GPIO chip */
1390 pctrl->chip.ngpio = intel_gpio_ngpio(pctrl);
1391 pctrl->chip.label = dev_name(pctrl->dev);
1392 pctrl->chip.parent = pctrl->dev;
1393 pctrl->chip.base = -1;
1394 pctrl->chip.add_pin_ranges = intel_gpio_add_pin_ranges;
1398 * On some platforms several GPIO controllers share the same interrupt
1401 ret = devm_request_irq(pctrl->dev, irq, intel_gpio_irq,
1402 IRQF_SHARED | IRQF_NO_THREAD,
1403 dev_name(pctrl->dev), pctrl);
1405 dev_err(pctrl->dev, "failed to request interrupt\n");
1409 /* Setup IRQ chip */
1410 girq = &pctrl->chip.irq;
1411 gpio_irq_chip_set_chip(girq, &intel_gpio_irq_chip);
1412 /* This will let us handle the IRQ in the driver */
1413 girq->parent_handler = NULL;
1414 girq->num_parents = 0;
1415 girq->default_type = IRQ_TYPE_NONE;
1416 girq->handler = handle_bad_irq;
1417 girq->init_hw = intel_gpio_irq_init_hw;
1419 ret = devm_gpiochip_add_data(pctrl->dev, &pctrl->chip, pctrl);
1421 dev_err(pctrl->dev, "failed to register gpiochip\n");
1428 static int intel_pinctrl_add_padgroups_by_gpps(struct intel_pinctrl *pctrl,
1429 struct intel_community *community)
1431 struct intel_padgroup *gpps;
1432 unsigned int padown_num = 0;
1433 size_t i, ngpps = community->ngpps;
1435 gpps = devm_kcalloc(pctrl->dev, ngpps, sizeof(*gpps), GFP_KERNEL);
1439 for (i = 0; i < ngpps; i++) {
1440 gpps[i] = community->gpps[i];
1442 if (gpps[i].size > INTEL_PINCTRL_MAX_GPP_SIZE)
1445 /* Special treatment for GPIO base */
1446 switch (gpps[i].gpio_base) {
1447 case INTEL_GPIO_BASE_MATCH:
1448 gpps[i].gpio_base = gpps[i].base;
1450 case INTEL_GPIO_BASE_ZERO:
1451 gpps[i].gpio_base = 0;
1453 case INTEL_GPIO_BASE_NOMAP:
1459 gpps[i].padown_num = padown_num;
1460 padown_num += DIV_ROUND_UP(gpps[i].size * 4, INTEL_PINCTRL_MAX_GPP_SIZE);
1463 community->gpps = gpps;
1468 static int intel_pinctrl_add_padgroups_by_size(struct intel_pinctrl *pctrl,
1469 struct intel_community *community)
1471 struct intel_padgroup *gpps;
1472 unsigned int npins = community->npins;
1473 unsigned int padown_num = 0;
1474 size_t i, ngpps = DIV_ROUND_UP(npins, community->gpp_size);
1476 if (community->gpp_size > INTEL_PINCTRL_MAX_GPP_SIZE)
1479 gpps = devm_kcalloc(pctrl->dev, ngpps, sizeof(*gpps), GFP_KERNEL);
1483 for (i = 0; i < ngpps; i++) {
1484 unsigned int gpp_size = community->gpp_size;
1486 gpps[i].reg_num = i;
1487 gpps[i].base = community->pin_base + i * gpp_size;
1488 gpps[i].size = min(gpp_size, npins);
1489 npins -= gpps[i].size;
1491 gpps[i].gpio_base = gpps[i].base;
1492 gpps[i].padown_num = padown_num;
1494 padown_num += community->gpp_num_padown_regs;
1497 community->ngpps = ngpps;
1498 community->gpps = gpps;
1503 static int intel_pinctrl_pm_init(struct intel_pinctrl *pctrl)
1505 #ifdef CONFIG_PM_SLEEP
1506 const struct intel_pinctrl_soc_data *soc = pctrl->soc;
1507 struct intel_community_context *communities;
1508 struct intel_pad_context *pads;
1511 pads = devm_kcalloc(pctrl->dev, soc->npins, sizeof(*pads), GFP_KERNEL);
1515 communities = devm_kcalloc(pctrl->dev, pctrl->ncommunities,
1516 sizeof(*communities), GFP_KERNEL);
1521 for (i = 0; i < pctrl->ncommunities; i++) {
1522 struct intel_community *community = &pctrl->communities[i];
1523 u32 *intmask, *hostown;
1525 intmask = devm_kcalloc(pctrl->dev, community->ngpps,
1526 sizeof(*intmask), GFP_KERNEL);
1530 communities[i].intmask = intmask;
1532 hostown = devm_kcalloc(pctrl->dev, community->ngpps,
1533 sizeof(*hostown), GFP_KERNEL);
1537 communities[i].hostown = hostown;
1540 pctrl->context.pads = pads;
1541 pctrl->context.communities = communities;
1547 static int intel_pinctrl_probe_pwm(struct intel_pinctrl *pctrl,
1548 struct intel_community *community)
1550 static const struct pwm_lpss_boardinfo info = {
1551 .clk_rate = 19200000,
1553 .base_unit_bits = 22,
1555 struct pwm_chip *chip;
1557 if (!(community->features & PINCTRL_FEATURE_PWM))
1560 if (!IS_REACHABLE(CONFIG_PWM_LPSS))
1563 chip = devm_pwm_lpss_probe(pctrl->dev, community->regs + PWMC, &info);
1564 return PTR_ERR_OR_ZERO(chip);
1567 int intel_pinctrl_probe(struct platform_device *pdev,
1568 const struct intel_pinctrl_soc_data *soc_data)
1570 struct device *dev = &pdev->dev;
1571 struct intel_pinctrl *pctrl;
1574 pctrl = devm_kzalloc(dev, sizeof(*pctrl), GFP_KERNEL);
1579 pctrl->soc = soc_data;
1580 raw_spin_lock_init(&pctrl->lock);
1583 * Make a copy of the communities which we can use to hold pointers
1586 pctrl->ncommunities = pctrl->soc->ncommunities;
1587 pctrl->communities = devm_kmemdup_array(dev, pctrl->soc->communities, pctrl->ncommunities,
1588 sizeof(*pctrl->soc->communities), GFP_KERNEL);
1589 if (!pctrl->communities)
1592 for (i = 0; i < pctrl->ncommunities; i++) {
1593 struct intel_community *community = &pctrl->communities[i];
1598 regs = devm_platform_ioremap_resource(pdev, community->barno);
1600 return PTR_ERR(regs);
1603 * Determine community features based on the revision.
1604 * A value of all ones means the device is not present.
1606 value = readl(regs + REVID);
1609 if (((value & REVID_MASK) >> REVID_SHIFT) >= 0x94) {
1610 community->features |= PINCTRL_FEATURE_DEBOUNCE;
1611 community->features |= PINCTRL_FEATURE_1K_PD;
1614 /* Determine community features based on the capabilities */
1617 value = readl(regs + offset);
1618 switch ((value & CAPLIST_ID_MASK) >> CAPLIST_ID_SHIFT) {
1619 case CAPLIST_ID_GPIO_HW_INFO:
1620 community->features |= PINCTRL_FEATURE_GPIO_HW_INFO;
1622 case CAPLIST_ID_PWM:
1623 community->features |= PINCTRL_FEATURE_PWM;
1625 case CAPLIST_ID_BLINK:
1626 community->features |= PINCTRL_FEATURE_BLINK;
1628 case CAPLIST_ID_EXP:
1629 community->features |= PINCTRL_FEATURE_EXP;
1634 offset = (value & CAPLIST_NEXT_MASK) >> CAPLIST_NEXT_SHIFT;
1637 dev_dbg(dev, "Community%d features: %#08x\n", i, community->features);
1639 /* Read offset of the pad configuration registers */
1640 offset = readl(regs + PADBAR);
1642 community->regs = regs;
1643 community->pad_regs = regs + offset;
1645 if (community->gpps)
1646 ret = intel_pinctrl_add_padgroups_by_gpps(pctrl, community);
1648 ret = intel_pinctrl_add_padgroups_by_size(pctrl, community);
1652 ret = intel_pinctrl_probe_pwm(pctrl, community);
1657 irq = platform_get_irq(pdev, 0);
1661 ret = intel_pinctrl_pm_init(pctrl);
1665 pctrl->pctldesc = intel_pinctrl_desc;
1666 pctrl->pctldesc.name = dev_name(dev);
1667 pctrl->pctldesc.pins = pctrl->soc->pins;
1668 pctrl->pctldesc.npins = pctrl->soc->npins;
1670 pctrl->pctldev = devm_pinctrl_register(dev, &pctrl->pctldesc, pctrl);
1671 if (IS_ERR(pctrl->pctldev)) {
1672 dev_err(dev, "failed to register pinctrl driver\n");
1673 return PTR_ERR(pctrl->pctldev);
1676 ret = intel_gpio_probe(pctrl, irq);
1680 platform_set_drvdata(pdev, pctrl);
1684 EXPORT_SYMBOL_NS_GPL(intel_pinctrl_probe, "PINCTRL_INTEL");
1686 int intel_pinctrl_probe_by_hid(struct platform_device *pdev)
1688 const struct intel_pinctrl_soc_data *data;
1690 data = device_get_match_data(&pdev->dev);
1694 return intel_pinctrl_probe(pdev, data);
1696 EXPORT_SYMBOL_NS_GPL(intel_pinctrl_probe_by_hid, "PINCTRL_INTEL");
1698 int intel_pinctrl_probe_by_uid(struct platform_device *pdev)
1700 const struct intel_pinctrl_soc_data *data;
1702 data = intel_pinctrl_get_soc_data(pdev);
1704 return PTR_ERR(data);
1706 return intel_pinctrl_probe(pdev, data);
1708 EXPORT_SYMBOL_NS_GPL(intel_pinctrl_probe_by_uid, "PINCTRL_INTEL");
1710 const struct intel_pinctrl_soc_data *intel_pinctrl_get_soc_data(struct platform_device *pdev)
1712 const struct intel_pinctrl_soc_data * const *table;
1713 const struct intel_pinctrl_soc_data *data;
1714 struct device *dev = &pdev->dev;
1716 table = device_get_match_data(dev);
1718 struct acpi_device *adev = ACPI_COMPANION(dev);
1721 for (i = 0; table[i]; i++) {
1722 if (acpi_dev_uid_match(adev, table[i]->uid))
1727 const struct platform_device_id *id;
1729 id = platform_get_device_id(pdev);
1731 return ERR_PTR(-ENODEV);
1733 table = (const struct intel_pinctrl_soc_data * const *)id->driver_data;
1734 data = table[pdev->id];
1737 return data ?: ERR_PTR(-ENODATA);
1739 EXPORT_SYMBOL_NS_GPL(intel_pinctrl_get_soc_data, "PINCTRL_INTEL");
1741 static bool __intel_gpio_is_direct_irq(u32 value)
1743 return (value & PADCFG0_GPIROUTIOXAPIC) &&
1744 (__intel_gpio_get_direction(value) == PAD_CONNECT_INPUT) &&
1745 (__intel_gpio_get_gpio_mode(value) == PADCFG0_PMODE_GPIO);
1748 static bool intel_pinctrl_should_save(struct intel_pinctrl *pctrl, unsigned int pin)
1750 const struct pin_desc *pd = pin_desc_get(pctrl->pctldev, pin);
1753 if (!pd || !intel_pad_usable(pctrl, pin))
1757 * Only restore the pin if it is actually in use by the kernel (or
1758 * by userspace). It is possible that some pins are used by the
1759 * BIOS during resume and those are not always locked down so leave
1762 if (pd->mux_owner || pd->gpio_owner ||
1763 gpiochip_line_is_irq(&pctrl->chip, intel_pin_to_gpio(pctrl, pin)))
1767 * The firmware on some systems may configure GPIO pins to be
1768 * an interrupt source in so called "direct IRQ" mode. In such
1769 * cases the GPIO controller driver has no idea if those pins
1770 * are being used or not. At the same time, there is a known bug
1771 * in the firmwares that don't restore the pin settings correctly
1772 * after suspend, i.e. by an unknown reason the Rx value becomes
1775 * Hence, let's save and restore the pins that are configured
1776 * as GPIOs in the input mode with GPIROUTIOXAPIC bit set.
1778 * See https://bugzilla.kernel.org/show_bug.cgi?id=214749.
1780 value = readl(intel_get_padcfg(pctrl, pin, PADCFG0));
1781 if (__intel_gpio_is_direct_irq(value))
1787 static int intel_pinctrl_suspend_noirq(struct device *dev)
1789 struct intel_pinctrl *pctrl = dev_get_drvdata(dev);
1790 struct intel_community_context *communities;
1791 struct intel_pad_context *pads;
1794 pads = pctrl->context.pads;
1795 for (i = 0; i < pctrl->soc->npins; i++) {
1796 const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
1797 void __iomem *padcfg;
1800 if (!intel_pinctrl_should_save(pctrl, desc->number))
1803 val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG0));
1804 pads[i].padcfg0 = val & ~PADCFG0_GPIORXSTATE;
1805 val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG1));
1806 pads[i].padcfg1 = val;
1808 padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG2);
1810 pads[i].padcfg2 = readl(padcfg);
1813 communities = pctrl->context.communities;
1814 for (i = 0; i < pctrl->ncommunities; i++) {
1815 struct intel_community *community = &pctrl->communities[i];
1819 base = community->regs + community->ie_offset;
1820 for (gpp = 0; gpp < community->ngpps; gpp++)
1821 communities[i].intmask[gpp] = readl(base + gpp * 4);
1823 base = community->regs + community->hostown_offset;
1824 for (gpp = 0; gpp < community->ngpps; gpp++)
1825 communities[i].hostown[gpp] = readl(base + gpp * 4);
1831 static bool intel_gpio_update_reg(void __iomem *reg, u32 mask, u32 value)
1837 updated = (curr & ~mask) | (value & mask);
1838 if (curr == updated)
1841 writel(updated, reg);
1845 static void intel_restore_hostown(struct intel_pinctrl *pctrl, unsigned int c,
1846 void __iomem *base, unsigned int gpp, u32 saved)
1848 const struct intel_community *community = &pctrl->communities[c];
1849 const struct intel_padgroup *padgrp = &community->gpps[gpp];
1850 struct device *dev = pctrl->dev;
1855 if (padgrp->gpio_base == INTEL_GPIO_BASE_NOMAP)
1858 for_each_requested_gpio_in_range(&pctrl->chip, i, padgrp->gpio_base, padgrp->size, dummy)
1859 requested |= BIT(i);
1861 if (!intel_gpio_update_reg(base + gpp * 4, requested, saved))
1864 dev_dbg(dev, "restored hostown %u/%u %#08x\n", c, gpp, readl(base + gpp * 4));
1867 static void intel_restore_intmask(struct intel_pinctrl *pctrl, unsigned int c,
1868 void __iomem *base, unsigned int gpp, u32 saved)
1870 struct device *dev = pctrl->dev;
1872 if (!intel_gpio_update_reg(base + gpp * 4, ~0U, saved))
1875 dev_dbg(dev, "restored mask %u/%u %#08x\n", c, gpp, readl(base + gpp * 4));
1878 static void intel_restore_padcfg(struct intel_pinctrl *pctrl, unsigned int pin,
1879 unsigned int reg, u32 saved)
1881 u32 mask = (reg == PADCFG0) ? PADCFG0_GPIORXSTATE : 0;
1882 unsigned int n = reg / sizeof(u32);
1883 struct device *dev = pctrl->dev;
1884 void __iomem *padcfg;
1886 padcfg = intel_get_padcfg(pctrl, pin, reg);
1890 if (!intel_gpio_update_reg(padcfg, ~mask, saved))
1893 dev_dbg(dev, "restored pin %u padcfg%u %#08x\n", pin, n, readl(padcfg));
1896 static int intel_pinctrl_resume_noirq(struct device *dev)
1898 struct intel_pinctrl *pctrl = dev_get_drvdata(dev);
1899 const struct intel_community_context *communities;
1900 const struct intel_pad_context *pads;
1903 /* Mask all interrupts */
1904 intel_gpio_irq_init(pctrl);
1906 pads = pctrl->context.pads;
1907 for (i = 0; i < pctrl->soc->npins; i++) {
1908 const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
1910 if (!(intel_pinctrl_should_save(pctrl, desc->number) ||
1912 * If the firmware mangled the register contents too much,
1913 * check the saved value for the Direct IRQ mode.
1915 __intel_gpio_is_direct_irq(pads[i].padcfg0)))
1918 intel_restore_padcfg(pctrl, desc->number, PADCFG0, pads[i].padcfg0);
1919 intel_restore_padcfg(pctrl, desc->number, PADCFG1, pads[i].padcfg1);
1920 intel_restore_padcfg(pctrl, desc->number, PADCFG2, pads[i].padcfg2);
1923 communities = pctrl->context.communities;
1924 for (i = 0; i < pctrl->ncommunities; i++) {
1925 struct intel_community *community = &pctrl->communities[i];
1929 base = community->regs + community->ie_offset;
1930 for (gpp = 0; gpp < community->ngpps; gpp++)
1931 intel_restore_intmask(pctrl, i, base, gpp, communities[i].intmask[gpp]);
1933 base = community->regs + community->hostown_offset;
1934 for (gpp = 0; gpp < community->ngpps; gpp++)
1935 intel_restore_hostown(pctrl, i, base, gpp, communities[i].hostown[gpp]);
1941 EXPORT_NS_GPL_DEV_SLEEP_PM_OPS(intel_pinctrl_pm_ops, PINCTRL_INTEL) = {
1942 NOIRQ_SYSTEM_SLEEP_PM_OPS(intel_pinctrl_suspend_noirq, intel_pinctrl_resume_noirq)
1945 MODULE_AUTHOR("Mathias Nyman <mathias.nyman@linux.intel.com>");
1946 MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
1947 MODULE_DESCRIPTION("Intel pinctrl/GPIO core driver");
1948 MODULE_LICENSE("GPL v2");
1949 MODULE_IMPORT_NS("PWM_LPSS");