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/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/time.h>
19 #include <linux/pinctrl/pinctrl.h>
20 #include <linux/pinctrl/pinmux.h>
21 #include <linux/pinctrl/pinconf.h>
22 #include <linux/pinctrl/pinconf-generic.h>
25 #include "pinctrl-intel.h"
27 /* Offset from regs */
29 #define REVID_SHIFT 16
30 #define REVID_MASK GENMASK(31, 16)
35 #define PADOWN_SHIFT(p) ((p) % 8 * PADOWN_BITS)
36 #define PADOWN_MASK(p) (GENMASK(3, 0) << PADOWN_SHIFT(p))
37 #define PADOWN_GPP(p) ((p) / 8)
39 /* Offset from pad_regs */
41 #define PADCFG0_RXEVCFG_SHIFT 25
42 #define PADCFG0_RXEVCFG_MASK GENMASK(26, 25)
43 #define PADCFG0_RXEVCFG_LEVEL 0
44 #define PADCFG0_RXEVCFG_EDGE 1
45 #define PADCFG0_RXEVCFG_DISABLED 2
46 #define PADCFG0_RXEVCFG_EDGE_BOTH 3
47 #define PADCFG0_PREGFRXSEL BIT(24)
48 #define PADCFG0_RXINV BIT(23)
49 #define PADCFG0_GPIROUTIOXAPIC BIT(20)
50 #define PADCFG0_GPIROUTSCI BIT(19)
51 #define PADCFG0_GPIROUTSMI BIT(18)
52 #define PADCFG0_GPIROUTNMI BIT(17)
53 #define PADCFG0_PMODE_SHIFT 10
54 #define PADCFG0_PMODE_MASK GENMASK(13, 10)
55 #define PADCFG0_PMODE_GPIO 0
56 #define PADCFG0_GPIORXDIS BIT(9)
57 #define PADCFG0_GPIOTXDIS BIT(8)
58 #define PADCFG0_GPIORXSTATE BIT(1)
59 #define PADCFG0_GPIOTXSTATE BIT(0)
62 #define PADCFG1_TERM_UP BIT(13)
63 #define PADCFG1_TERM_SHIFT 10
64 #define PADCFG1_TERM_MASK GENMASK(12, 10)
65 #define PADCFG1_TERM_20K BIT(2)
66 #define PADCFG1_TERM_5K BIT(1)
67 #define PADCFG1_TERM_1K BIT(0)
68 #define PADCFG1_TERM_833 (BIT(1) | BIT(0))
71 #define PADCFG2_DEBEN BIT(0)
72 #define PADCFG2_DEBOUNCE_SHIFT 1
73 #define PADCFG2_DEBOUNCE_MASK GENMASK(4, 1)
75 #define DEBOUNCE_PERIOD_NSEC 31250
77 struct intel_pad_context {
83 struct intel_community_context {
88 #define pin_to_padno(c, p) ((p) - (c)->pin_base)
89 #define padgroup_offset(g, p) ((p) - (g)->base)
91 static struct intel_community *intel_get_community(struct intel_pinctrl *pctrl,
94 struct intel_community *community;
97 for (i = 0; i < pctrl->ncommunities; i++) {
98 community = &pctrl->communities[i];
99 if (pin >= community->pin_base &&
100 pin < community->pin_base + community->npins)
104 dev_warn(pctrl->dev, "failed to find community for pin %u\n", pin);
108 static const struct intel_padgroup *
109 intel_community_get_padgroup(const struct intel_community *community,
114 for (i = 0; i < community->ngpps; i++) {
115 const struct intel_padgroup *padgrp = &community->gpps[i];
117 if (pin >= padgrp->base && pin < padgrp->base + padgrp->size)
124 static void __iomem *intel_get_padcfg(struct intel_pinctrl *pctrl,
125 unsigned int pin, unsigned int reg)
127 const struct intel_community *community;
131 community = intel_get_community(pctrl, pin);
135 padno = pin_to_padno(community, pin);
136 nregs = (community->features & PINCTRL_FEATURE_DEBOUNCE) ? 4 : 2;
138 if (reg >= nregs * 4)
141 return community->pad_regs + reg + padno * nregs * 4;
144 static bool intel_pad_owned_by_host(struct intel_pinctrl *pctrl, unsigned int pin)
146 const struct intel_community *community;
147 const struct intel_padgroup *padgrp;
148 unsigned int gpp, offset, gpp_offset;
149 void __iomem *padown;
151 community = intel_get_community(pctrl, pin);
154 if (!community->padown_offset)
157 padgrp = intel_community_get_padgroup(community, pin);
161 gpp_offset = padgroup_offset(padgrp, pin);
162 gpp = PADOWN_GPP(gpp_offset);
163 offset = community->padown_offset + padgrp->padown_num * 4 + gpp * 4;
164 padown = community->regs + offset;
166 return !(readl(padown) & PADOWN_MASK(gpp_offset));
169 static bool intel_pad_acpi_mode(struct intel_pinctrl *pctrl, unsigned int pin)
171 const struct intel_community *community;
172 const struct intel_padgroup *padgrp;
173 unsigned int offset, gpp_offset;
174 void __iomem *hostown;
176 community = intel_get_community(pctrl, pin);
179 if (!community->hostown_offset)
182 padgrp = intel_community_get_padgroup(community, pin);
186 gpp_offset = padgroup_offset(padgrp, pin);
187 offset = community->hostown_offset + padgrp->reg_num * 4;
188 hostown = community->regs + offset;
190 return !(readl(hostown) & BIT(gpp_offset));
194 * enum - Locking variants of the pad configuration
196 * @PAD_UNLOCKED: pad is fully controlled by the configuration registers
197 * @PAD_LOCKED: pad configuration registers, except TX state, are locked
198 * @PAD_LOCKED_TX: pad configuration TX state is locked
199 * @PAD_LOCKED_FULL: pad configuration registers are locked completely
201 * Locking is considered as read-only mode for corresponding registers and
202 * their respective fields. That said, TX state bit is locked separately from
203 * the main locking scheme.
209 PAD_LOCKED_FULL = PAD_LOCKED | PAD_LOCKED_TX,
212 static int intel_pad_locked(struct intel_pinctrl *pctrl, unsigned int pin)
214 struct intel_community *community;
215 const struct intel_padgroup *padgrp;
216 unsigned int offset, gpp_offset;
218 int ret = PAD_UNLOCKED;
220 community = intel_get_community(pctrl, pin);
222 return PAD_LOCKED_FULL;
223 if (!community->padcfglock_offset)
226 padgrp = intel_community_get_padgroup(community, pin);
228 return PAD_LOCKED_FULL;
230 gpp_offset = padgroup_offset(padgrp, pin);
233 * If PADCFGLOCK and PADCFGLOCKTX bits are both clear for this pad,
234 * the pad is considered unlocked. Any other case means that it is
235 * either fully or partially locked.
237 offset = community->padcfglock_offset + 0 + padgrp->reg_num * 8;
238 value = readl(community->regs + offset);
239 if (value & BIT(gpp_offset))
242 offset = community->padcfglock_offset + 4 + padgrp->reg_num * 8;
243 value = readl(community->regs + offset);
244 if (value & BIT(gpp_offset))
245 ret |= PAD_LOCKED_TX;
250 static bool intel_pad_is_unlocked(struct intel_pinctrl *pctrl, unsigned int pin)
252 return (intel_pad_locked(pctrl, pin) & PAD_LOCKED) == PAD_UNLOCKED;
255 static bool intel_pad_usable(struct intel_pinctrl *pctrl, unsigned int pin)
257 return intel_pad_owned_by_host(pctrl, pin) && intel_pad_is_unlocked(pctrl, pin);
260 static int intel_get_groups_count(struct pinctrl_dev *pctldev)
262 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
264 return pctrl->soc->ngroups;
267 static const char *intel_get_group_name(struct pinctrl_dev *pctldev,
270 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
272 return pctrl->soc->groups[group].name;
275 static int intel_get_group_pins(struct pinctrl_dev *pctldev, unsigned int group,
276 const unsigned int **pins, unsigned int *npins)
278 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
280 *pins = pctrl->soc->groups[group].pins;
281 *npins = pctrl->soc->groups[group].npins;
285 static void intel_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
288 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
289 void __iomem *padcfg;
290 u32 cfg0, cfg1, mode;
294 if (!intel_pad_owned_by_host(pctrl, pin)) {
295 seq_puts(s, "not available");
299 cfg0 = readl(intel_get_padcfg(pctrl, pin, PADCFG0));
300 cfg1 = readl(intel_get_padcfg(pctrl, pin, PADCFG1));
302 mode = (cfg0 & PADCFG0_PMODE_MASK) >> PADCFG0_PMODE_SHIFT;
303 if (mode == PADCFG0_PMODE_GPIO)
304 seq_puts(s, "GPIO ");
306 seq_printf(s, "mode %d ", mode);
308 seq_printf(s, "0x%08x 0x%08x", cfg0, cfg1);
310 /* Dump the additional PADCFG registers if available */
311 padcfg = intel_get_padcfg(pctrl, pin, PADCFG2);
313 seq_printf(s, " 0x%08x", readl(padcfg));
315 locked = intel_pad_locked(pctrl, pin);
316 acpi = intel_pad_acpi_mode(pctrl, pin);
318 if (locked || acpi) {
321 seq_puts(s, "LOCKED");
322 if ((locked & PAD_LOCKED_FULL) == PAD_LOCKED_TX)
324 else if ((locked & PAD_LOCKED_FULL) == PAD_LOCKED_FULL)
325 seq_puts(s, " full");
336 static const struct pinctrl_ops intel_pinctrl_ops = {
337 .get_groups_count = intel_get_groups_count,
338 .get_group_name = intel_get_group_name,
339 .get_group_pins = intel_get_group_pins,
340 .pin_dbg_show = intel_pin_dbg_show,
343 static int intel_get_functions_count(struct pinctrl_dev *pctldev)
345 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
347 return pctrl->soc->nfunctions;
350 static const char *intel_get_function_name(struct pinctrl_dev *pctldev,
351 unsigned int function)
353 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
355 return pctrl->soc->functions[function].name;
358 static int intel_get_function_groups(struct pinctrl_dev *pctldev,
359 unsigned int function,
360 const char * const **groups,
361 unsigned int * const ngroups)
363 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
365 *groups = pctrl->soc->functions[function].groups;
366 *ngroups = pctrl->soc->functions[function].ngroups;
370 static int intel_pinmux_set_mux(struct pinctrl_dev *pctldev,
371 unsigned int function, unsigned int group)
373 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
374 const struct intel_pingroup *grp = &pctrl->soc->groups[group];
378 raw_spin_lock_irqsave(&pctrl->lock, flags);
381 * All pins in the groups needs to be accessible and writable
382 * before we can enable the mux for this group.
384 for (i = 0; i < grp->npins; i++) {
385 if (!intel_pad_usable(pctrl, grp->pins[i])) {
386 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
391 /* Now enable the mux setting for each pin in the group */
392 for (i = 0; i < grp->npins; i++) {
393 void __iomem *padcfg0;
396 padcfg0 = intel_get_padcfg(pctrl, grp->pins[i], PADCFG0);
397 value = readl(padcfg0);
399 value &= ~PADCFG0_PMODE_MASK;
402 value |= grp->modes[i] << PADCFG0_PMODE_SHIFT;
404 value |= grp->mode << PADCFG0_PMODE_SHIFT;
406 writel(value, padcfg0);
409 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
414 static void __intel_gpio_set_direction(void __iomem *padcfg0, bool input)
418 value = readl(padcfg0);
420 value &= ~PADCFG0_GPIORXDIS;
421 value |= PADCFG0_GPIOTXDIS;
423 value &= ~PADCFG0_GPIOTXDIS;
424 value |= PADCFG0_GPIORXDIS;
426 writel(value, padcfg0);
429 static int intel_gpio_get_gpio_mode(void __iomem *padcfg0)
431 return (readl(padcfg0) & PADCFG0_PMODE_MASK) >> PADCFG0_PMODE_SHIFT;
434 static void intel_gpio_set_gpio_mode(void __iomem *padcfg0)
438 value = readl(padcfg0);
440 /* Put the pad into GPIO mode */
441 value &= ~PADCFG0_PMODE_MASK;
442 value |= PADCFG0_PMODE_GPIO;
444 /* Disable TX buffer and enable RX (this will be input) */
445 value &= ~PADCFG0_GPIORXDIS;
446 value |= PADCFG0_GPIOTXDIS;
448 /* Disable SCI/SMI/NMI generation */
449 value &= ~(PADCFG0_GPIROUTIOXAPIC | PADCFG0_GPIROUTSCI);
450 value &= ~(PADCFG0_GPIROUTSMI | PADCFG0_GPIROUTNMI);
452 writel(value, padcfg0);
455 static int intel_gpio_request_enable(struct pinctrl_dev *pctldev,
456 struct pinctrl_gpio_range *range,
459 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
460 void __iomem *padcfg0;
463 padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
465 raw_spin_lock_irqsave(&pctrl->lock, flags);
467 if (!intel_pad_owned_by_host(pctrl, pin)) {
468 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
472 if (!intel_pad_is_unlocked(pctrl, pin)) {
473 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
478 * If pin is already configured in GPIO mode, we assume that
479 * firmware provides correct settings. In such case we avoid
480 * potential glitches on the pin. Otherwise, for the pin in
481 * alternative mode, consumer has to supply respective flags.
483 if (intel_gpio_get_gpio_mode(padcfg0) == PADCFG0_PMODE_GPIO) {
484 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
488 intel_gpio_set_gpio_mode(padcfg0);
490 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
495 static int intel_gpio_set_direction(struct pinctrl_dev *pctldev,
496 struct pinctrl_gpio_range *range,
497 unsigned int pin, bool input)
499 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
500 void __iomem *padcfg0;
503 padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
505 raw_spin_lock_irqsave(&pctrl->lock, flags);
506 __intel_gpio_set_direction(padcfg0, input);
507 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
512 static const struct pinmux_ops intel_pinmux_ops = {
513 .get_functions_count = intel_get_functions_count,
514 .get_function_name = intel_get_function_name,
515 .get_function_groups = intel_get_function_groups,
516 .set_mux = intel_pinmux_set_mux,
517 .gpio_request_enable = intel_gpio_request_enable,
518 .gpio_set_direction = intel_gpio_set_direction,
521 static int intel_config_get_pull(struct intel_pinctrl *pctrl, unsigned int pin,
522 enum pin_config_param param, u32 *arg)
524 const struct intel_community *community;
525 void __iomem *padcfg1;
529 community = intel_get_community(pctrl, pin);
530 padcfg1 = intel_get_padcfg(pctrl, pin, PADCFG1);
532 raw_spin_lock_irqsave(&pctrl->lock, flags);
533 value = readl(padcfg1);
534 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
536 term = (value & PADCFG1_TERM_MASK) >> PADCFG1_TERM_SHIFT;
539 case PIN_CONFIG_BIAS_DISABLE:
544 case PIN_CONFIG_BIAS_PULL_UP:
545 if (!term || !(value & PADCFG1_TERM_UP))
549 case PADCFG1_TERM_833:
552 case PADCFG1_TERM_1K:
555 case PADCFG1_TERM_5K:
558 case PADCFG1_TERM_20K:
565 case PIN_CONFIG_BIAS_PULL_DOWN:
566 if (!term || value & PADCFG1_TERM_UP)
570 case PADCFG1_TERM_833:
571 if (!(community->features & PINCTRL_FEATURE_1K_PD))
575 case PADCFG1_TERM_1K:
576 if (!(community->features & PINCTRL_FEATURE_1K_PD))
580 case PADCFG1_TERM_5K:
583 case PADCFG1_TERM_20K:
597 static int intel_config_get_debounce(struct intel_pinctrl *pctrl, unsigned int pin,
598 enum pin_config_param param, u32 *arg)
600 void __iomem *padcfg2;
605 padcfg2 = intel_get_padcfg(pctrl, pin, PADCFG2);
609 raw_spin_lock_irqsave(&pctrl->lock, flags);
610 value2 = readl(padcfg2);
611 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
612 if (!(value2 & PADCFG2_DEBEN))
615 v = (value2 & PADCFG2_DEBOUNCE_MASK) >> PADCFG2_DEBOUNCE_SHIFT;
616 *arg = BIT(v) * DEBOUNCE_PERIOD_NSEC / NSEC_PER_USEC;
621 static int intel_config_get(struct pinctrl_dev *pctldev, unsigned int pin,
622 unsigned long *config)
624 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
625 enum pin_config_param param = pinconf_to_config_param(*config);
629 if (!intel_pad_owned_by_host(pctrl, pin))
633 case PIN_CONFIG_BIAS_DISABLE:
634 case PIN_CONFIG_BIAS_PULL_UP:
635 case PIN_CONFIG_BIAS_PULL_DOWN:
636 ret = intel_config_get_pull(pctrl, pin, param, &arg);
641 case PIN_CONFIG_INPUT_DEBOUNCE:
642 ret = intel_config_get_debounce(pctrl, pin, param, &arg);
651 *config = pinconf_to_config_packed(param, arg);
655 static int intel_config_set_pull(struct intel_pinctrl *pctrl, unsigned int pin,
656 unsigned long config)
658 unsigned int param = pinconf_to_config_param(config);
659 unsigned int arg = pinconf_to_config_argument(config);
660 const struct intel_community *community;
661 void __iomem *padcfg1;
666 community = intel_get_community(pctrl, pin);
667 padcfg1 = intel_get_padcfg(pctrl, pin, PADCFG1);
669 raw_spin_lock_irqsave(&pctrl->lock, flags);
671 value = readl(padcfg1);
674 case PIN_CONFIG_BIAS_DISABLE:
675 value &= ~(PADCFG1_TERM_MASK | PADCFG1_TERM_UP);
678 case PIN_CONFIG_BIAS_PULL_UP:
679 value &= ~PADCFG1_TERM_MASK;
681 value |= PADCFG1_TERM_UP;
683 /* Set default strength value in case none is given */
689 value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT;
692 value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT;
695 value |= PADCFG1_TERM_1K << PADCFG1_TERM_SHIFT;
698 value |= PADCFG1_TERM_833 << PADCFG1_TERM_SHIFT;
706 case PIN_CONFIG_BIAS_PULL_DOWN:
707 value &= ~(PADCFG1_TERM_UP | PADCFG1_TERM_MASK);
709 /* Set default strength value in case none is given */
715 value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT;
718 value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT;
721 if (!(community->features & PINCTRL_FEATURE_1K_PD)) {
725 value |= PADCFG1_TERM_1K << PADCFG1_TERM_SHIFT;
728 if (!(community->features & PINCTRL_FEATURE_1K_PD)) {
732 value |= PADCFG1_TERM_833 << PADCFG1_TERM_SHIFT;
742 writel(value, padcfg1);
744 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
749 static int intel_config_set_debounce(struct intel_pinctrl *pctrl,
750 unsigned int pin, unsigned int debounce)
752 void __iomem *padcfg0, *padcfg2;
756 padcfg2 = intel_get_padcfg(pctrl, pin, PADCFG2);
760 padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
762 raw_spin_lock_irqsave(&pctrl->lock, flags);
764 value0 = readl(padcfg0);
765 value2 = readl(padcfg2);
767 /* Disable glitch filter and debouncer */
768 value0 &= ~PADCFG0_PREGFRXSEL;
769 value2 &= ~(PADCFG2_DEBEN | PADCFG2_DEBOUNCE_MASK);
774 v = order_base_2(debounce * NSEC_PER_USEC / DEBOUNCE_PERIOD_NSEC);
775 if (v < 3 || v > 15) {
776 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
780 /* Enable glitch filter and debouncer */
781 value0 |= PADCFG0_PREGFRXSEL;
782 value2 |= v << PADCFG2_DEBOUNCE_SHIFT;
783 value2 |= PADCFG2_DEBEN;
786 writel(value0, padcfg0);
787 writel(value2, padcfg2);
789 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
794 static int intel_config_set(struct pinctrl_dev *pctldev, unsigned int pin,
795 unsigned long *configs, unsigned int nconfigs)
797 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
800 if (!intel_pad_usable(pctrl, pin))
803 for (i = 0; i < nconfigs; i++) {
804 switch (pinconf_to_config_param(configs[i])) {
805 case PIN_CONFIG_BIAS_DISABLE:
806 case PIN_CONFIG_BIAS_PULL_UP:
807 case PIN_CONFIG_BIAS_PULL_DOWN:
808 ret = intel_config_set_pull(pctrl, pin, configs[i]);
813 case PIN_CONFIG_INPUT_DEBOUNCE:
814 ret = intel_config_set_debounce(pctrl, pin,
815 pinconf_to_config_argument(configs[i]));
828 static const struct pinconf_ops intel_pinconf_ops = {
830 .pin_config_get = intel_config_get,
831 .pin_config_set = intel_config_set,
834 static const struct pinctrl_desc intel_pinctrl_desc = {
835 .pctlops = &intel_pinctrl_ops,
836 .pmxops = &intel_pinmux_ops,
837 .confops = &intel_pinconf_ops,
838 .owner = THIS_MODULE,
842 * intel_gpio_to_pin() - Translate from GPIO offset to pin number
843 * @pctrl: Pinctrl structure
844 * @offset: GPIO offset from gpiolib
845 * @community: Community is filled here if not %NULL
846 * @padgrp: Pad group is filled here if not %NULL
848 * When coming through gpiolib irqchip, the GPIO offset is not
849 * automatically translated to pinctrl pin number. This function can be
850 * used to find out the corresponding pinctrl pin.
852 static int intel_gpio_to_pin(struct intel_pinctrl *pctrl, unsigned int offset,
853 const struct intel_community **community,
854 const struct intel_padgroup **padgrp)
858 for (i = 0; i < pctrl->ncommunities; i++) {
859 const struct intel_community *comm = &pctrl->communities[i];
862 for (j = 0; j < comm->ngpps; j++) {
863 const struct intel_padgroup *pgrp = &comm->gpps[j];
865 if (pgrp->gpio_base == INTEL_GPIO_BASE_NOMAP)
868 if (offset >= pgrp->gpio_base &&
869 offset < pgrp->gpio_base + pgrp->size) {
872 pin = pgrp->base + offset - pgrp->gpio_base;
887 * intel_pin_to_gpio() - Translate from pin number to GPIO offset
888 * @pctrl: Pinctrl structure
891 * Translate the pin number of pinctrl to GPIO offset
893 static __maybe_unused int intel_pin_to_gpio(struct intel_pinctrl *pctrl, int pin)
895 const struct intel_community *community;
896 const struct intel_padgroup *padgrp;
898 community = intel_get_community(pctrl, pin);
902 padgrp = intel_community_get_padgroup(community, pin);
906 return pin - padgrp->base + padgrp->gpio_base;
909 static int intel_gpio_get(struct gpio_chip *chip, unsigned int offset)
911 struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
916 pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL);
920 reg = intel_get_padcfg(pctrl, pin, PADCFG0);
924 padcfg0 = readl(reg);
925 if (!(padcfg0 & PADCFG0_GPIOTXDIS))
926 return !!(padcfg0 & PADCFG0_GPIOTXSTATE);
928 return !!(padcfg0 & PADCFG0_GPIORXSTATE);
931 static void intel_gpio_set(struct gpio_chip *chip, unsigned int offset,
934 struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
940 pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL);
944 reg = intel_get_padcfg(pctrl, pin, PADCFG0);
948 raw_spin_lock_irqsave(&pctrl->lock, flags);
949 padcfg0 = readl(reg);
951 padcfg0 |= PADCFG0_GPIOTXSTATE;
953 padcfg0 &= ~PADCFG0_GPIOTXSTATE;
954 writel(padcfg0, reg);
955 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
958 static int intel_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
960 struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
966 pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL);
970 reg = intel_get_padcfg(pctrl, pin, PADCFG0);
974 raw_spin_lock_irqsave(&pctrl->lock, flags);
975 padcfg0 = readl(reg);
976 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
977 if (padcfg0 & PADCFG0_PMODE_MASK)
980 if (padcfg0 & PADCFG0_GPIOTXDIS)
981 return GPIO_LINE_DIRECTION_IN;
983 return GPIO_LINE_DIRECTION_OUT;
986 static int intel_gpio_direction_input(struct gpio_chip *chip, unsigned int offset)
988 return pinctrl_gpio_direction_input(chip->base + offset);
991 static int intel_gpio_direction_output(struct gpio_chip *chip, unsigned int offset,
994 intel_gpio_set(chip, offset, value);
995 return pinctrl_gpio_direction_output(chip->base + offset);
998 static const struct gpio_chip intel_gpio_chip = {
999 .owner = THIS_MODULE,
1000 .request = gpiochip_generic_request,
1001 .free = gpiochip_generic_free,
1002 .get_direction = intel_gpio_get_direction,
1003 .direction_input = intel_gpio_direction_input,
1004 .direction_output = intel_gpio_direction_output,
1005 .get = intel_gpio_get,
1006 .set = intel_gpio_set,
1007 .set_config = gpiochip_generic_config,
1010 static void intel_gpio_irq_ack(struct irq_data *d)
1012 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1013 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
1014 const struct intel_community *community;
1015 const struct intel_padgroup *padgrp;
1018 pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), &community, &padgrp);
1020 unsigned int gpp, gpp_offset, is_offset;
1022 gpp = padgrp->reg_num;
1023 gpp_offset = padgroup_offset(padgrp, pin);
1024 is_offset = community->is_offset + gpp * 4;
1026 raw_spin_lock(&pctrl->lock);
1027 writel(BIT(gpp_offset), community->regs + is_offset);
1028 raw_spin_unlock(&pctrl->lock);
1032 static void intel_gpio_irq_mask_unmask(struct irq_data *d, bool mask)
1034 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1035 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
1036 const struct intel_community *community;
1037 const struct intel_padgroup *padgrp;
1040 pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), &community, &padgrp);
1042 unsigned int gpp, gpp_offset;
1043 unsigned long flags;
1044 void __iomem *reg, *is;
1047 gpp = padgrp->reg_num;
1048 gpp_offset = padgroup_offset(padgrp, pin);
1050 reg = community->regs + community->ie_offset + gpp * 4;
1051 is = community->regs + community->is_offset + gpp * 4;
1053 raw_spin_lock_irqsave(&pctrl->lock, flags);
1055 /* Clear interrupt status first to avoid unexpected interrupt */
1056 writel(BIT(gpp_offset), is);
1060 value &= ~BIT(gpp_offset);
1062 value |= BIT(gpp_offset);
1064 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
1068 static void intel_gpio_irq_mask(struct irq_data *d)
1070 intel_gpio_irq_mask_unmask(d, true);
1073 static void intel_gpio_irq_unmask(struct irq_data *d)
1075 intel_gpio_irq_mask_unmask(d, false);
1078 static int intel_gpio_irq_type(struct irq_data *d, unsigned int type)
1080 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1081 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
1082 unsigned int pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), NULL, NULL);
1083 unsigned long flags;
1087 reg = intel_get_padcfg(pctrl, pin, PADCFG0);
1092 * If the pin is in ACPI mode it is still usable as a GPIO but it
1093 * cannot be used as IRQ because GPI_IS status bit will not be
1094 * updated by the host controller hardware.
1096 if (intel_pad_acpi_mode(pctrl, pin)) {
1097 dev_warn(pctrl->dev, "pin %u cannot be used as IRQ\n", pin);
1101 raw_spin_lock_irqsave(&pctrl->lock, flags);
1103 intel_gpio_set_gpio_mode(reg);
1107 value &= ~(PADCFG0_RXEVCFG_MASK | PADCFG0_RXINV);
1109 if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
1110 value |= PADCFG0_RXEVCFG_EDGE_BOTH << PADCFG0_RXEVCFG_SHIFT;
1111 } else if (type & IRQ_TYPE_EDGE_FALLING) {
1112 value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT;
1113 value |= PADCFG0_RXINV;
1114 } else if (type & IRQ_TYPE_EDGE_RISING) {
1115 value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT;
1116 } else if (type & IRQ_TYPE_LEVEL_MASK) {
1117 if (type & IRQ_TYPE_LEVEL_LOW)
1118 value |= PADCFG0_RXINV;
1120 value |= PADCFG0_RXEVCFG_DISABLED << PADCFG0_RXEVCFG_SHIFT;
1125 if (type & IRQ_TYPE_EDGE_BOTH)
1126 irq_set_handler_locked(d, handle_edge_irq);
1127 else if (type & IRQ_TYPE_LEVEL_MASK)
1128 irq_set_handler_locked(d, handle_level_irq);
1130 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
1135 static int intel_gpio_irq_wake(struct irq_data *d, unsigned int on)
1137 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1138 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
1139 unsigned int pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), NULL, NULL);
1142 enable_irq_wake(pctrl->irq);
1144 disable_irq_wake(pctrl->irq);
1146 dev_dbg(pctrl->dev, "%sable wake for pin %u\n", on ? "en" : "dis", pin);
1150 static int intel_gpio_community_irq_handler(struct intel_pinctrl *pctrl,
1151 const struct intel_community *community)
1153 struct gpio_chip *gc = &pctrl->chip;
1157 for (gpp = 0; gpp < community->ngpps; gpp++) {
1158 const struct intel_padgroup *padgrp = &community->gpps[gpp];
1159 unsigned long pending, enabled, gpp_offset;
1160 unsigned long flags;
1162 raw_spin_lock_irqsave(&pctrl->lock, flags);
1164 pending = readl(community->regs + community->is_offset +
1165 padgrp->reg_num * 4);
1166 enabled = readl(community->regs + community->ie_offset +
1167 padgrp->reg_num * 4);
1169 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
1171 /* Only interrupts that are enabled */
1174 for_each_set_bit(gpp_offset, &pending, padgrp->size) {
1177 irq = irq_find_mapping(gc->irq.domain,
1178 padgrp->gpio_base + gpp_offset);
1179 generic_handle_irq(irq);
1182 ret += pending ? 1 : 0;
1188 static irqreturn_t intel_gpio_irq(int irq, void *data)
1190 const struct intel_community *community;
1191 struct intel_pinctrl *pctrl = data;
1195 /* Need to check all communities for pending interrupts */
1196 for (i = 0; i < pctrl->ncommunities; i++) {
1197 community = &pctrl->communities[i];
1198 ret += intel_gpio_community_irq_handler(pctrl, community);
1201 return IRQ_RETVAL(ret);
1204 static void intel_gpio_irq_init(struct intel_pinctrl *pctrl)
1208 for (i = 0; i < pctrl->ncommunities; i++) {
1209 const struct intel_community *community;
1213 community = &pctrl->communities[i];
1214 base = community->regs;
1216 for (gpp = 0; gpp < community->ngpps; gpp++) {
1217 /* Mask and clear all interrupts */
1218 writel(0, base + community->ie_offset + gpp * 4);
1219 writel(0xffff, base + community->is_offset + gpp * 4);
1224 static int intel_gpio_irq_init_hw(struct gpio_chip *gc)
1226 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
1229 * Make sure the interrupt lines are in a proper state before
1230 * further configuration.
1232 intel_gpio_irq_init(pctrl);
1237 static int intel_gpio_add_community_ranges(struct intel_pinctrl *pctrl,
1238 const struct intel_community *community)
1242 for (i = 0; i < community->ngpps; i++) {
1243 const struct intel_padgroup *gpp = &community->gpps[i];
1245 if (gpp->gpio_base == INTEL_GPIO_BASE_NOMAP)
1248 ret = gpiochip_add_pin_range(&pctrl->chip, dev_name(pctrl->dev),
1249 gpp->gpio_base, gpp->base,
1258 static int intel_gpio_add_pin_ranges(struct gpio_chip *gc)
1260 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
1263 for (i = 0; i < pctrl->ncommunities; i++) {
1264 struct intel_community *community = &pctrl->communities[i];
1266 ret = intel_gpio_add_community_ranges(pctrl, community);
1268 dev_err(pctrl->dev, "failed to add GPIO pin range\n");
1276 static unsigned int intel_gpio_ngpio(const struct intel_pinctrl *pctrl)
1278 const struct intel_community *community;
1279 unsigned int ngpio = 0;
1282 for (i = 0; i < pctrl->ncommunities; i++) {
1283 community = &pctrl->communities[i];
1284 for (j = 0; j < community->ngpps; j++) {
1285 const struct intel_padgroup *gpp = &community->gpps[j];
1287 if (gpp->gpio_base == INTEL_GPIO_BASE_NOMAP)
1290 if (gpp->gpio_base + gpp->size > ngpio)
1291 ngpio = gpp->gpio_base + gpp->size;
1298 static int intel_gpio_probe(struct intel_pinctrl *pctrl, int irq)
1301 struct gpio_irq_chip *girq;
1303 pctrl->chip = intel_gpio_chip;
1305 /* Setup GPIO chip */
1306 pctrl->chip.ngpio = intel_gpio_ngpio(pctrl);
1307 pctrl->chip.label = dev_name(pctrl->dev);
1308 pctrl->chip.parent = pctrl->dev;
1309 pctrl->chip.base = -1;
1310 pctrl->chip.add_pin_ranges = intel_gpio_add_pin_ranges;
1313 /* Setup IRQ chip */
1314 pctrl->irqchip.name = dev_name(pctrl->dev);
1315 pctrl->irqchip.irq_ack = intel_gpio_irq_ack;
1316 pctrl->irqchip.irq_mask = intel_gpio_irq_mask;
1317 pctrl->irqchip.irq_unmask = intel_gpio_irq_unmask;
1318 pctrl->irqchip.irq_set_type = intel_gpio_irq_type;
1319 pctrl->irqchip.irq_set_wake = intel_gpio_irq_wake;
1320 pctrl->irqchip.flags = IRQCHIP_MASK_ON_SUSPEND;
1323 * On some platforms several GPIO controllers share the same interrupt
1326 ret = devm_request_irq(pctrl->dev, irq, intel_gpio_irq,
1327 IRQF_SHARED | IRQF_NO_THREAD,
1328 dev_name(pctrl->dev), pctrl);
1330 dev_err(pctrl->dev, "failed to request interrupt\n");
1334 girq = &pctrl->chip.irq;
1335 girq->chip = &pctrl->irqchip;
1336 /* This will let us handle the IRQ in the driver */
1337 girq->parent_handler = NULL;
1338 girq->num_parents = 0;
1339 girq->default_type = IRQ_TYPE_NONE;
1340 girq->handler = handle_bad_irq;
1341 girq->init_hw = intel_gpio_irq_init_hw;
1343 ret = devm_gpiochip_add_data(pctrl->dev, &pctrl->chip, pctrl);
1345 dev_err(pctrl->dev, "failed to register gpiochip\n");
1352 static int intel_pinctrl_add_padgroups(struct intel_pinctrl *pctrl,
1353 struct intel_community *community)
1355 struct intel_padgroup *gpps;
1356 unsigned int npins = community->npins;
1357 unsigned int padown_num = 0;
1360 if (community->gpps)
1361 ngpps = community->ngpps;
1363 ngpps = DIV_ROUND_UP(community->npins, community->gpp_size);
1365 gpps = devm_kcalloc(pctrl->dev, ngpps, sizeof(*gpps), GFP_KERNEL);
1369 for (i = 0; i < ngpps; i++) {
1370 if (community->gpps) {
1371 gpps[i] = community->gpps[i];
1373 unsigned int gpp_size = community->gpp_size;
1375 gpps[i].reg_num = i;
1376 gpps[i].base = community->pin_base + i * gpp_size;
1377 gpps[i].size = min(gpp_size, npins);
1378 npins -= gpps[i].size;
1381 if (gpps[i].size > 32)
1384 /* Special treatment for GPIO base */
1385 switch (gpps[i].gpio_base) {
1386 case INTEL_GPIO_BASE_MATCH:
1387 gpps[i].gpio_base = gpps[i].base;
1389 case INTEL_GPIO_BASE_ZERO:
1390 gpps[i].gpio_base = 0;
1392 case INTEL_GPIO_BASE_NOMAP:
1397 gpps[i].padown_num = padown_num;
1400 * In older hardware the number of padown registers per
1401 * group is fixed regardless of the group size.
1403 if (community->gpp_num_padown_regs)
1404 padown_num += community->gpp_num_padown_regs;
1406 padown_num += DIV_ROUND_UP(gpps[i].size * 4, 32);
1409 community->ngpps = ngpps;
1410 community->gpps = gpps;
1415 static int intel_pinctrl_pm_init(struct intel_pinctrl *pctrl)
1417 #ifdef CONFIG_PM_SLEEP
1418 const struct intel_pinctrl_soc_data *soc = pctrl->soc;
1419 struct intel_community_context *communities;
1420 struct intel_pad_context *pads;
1423 pads = devm_kcalloc(pctrl->dev, soc->npins, sizeof(*pads), GFP_KERNEL);
1427 communities = devm_kcalloc(pctrl->dev, pctrl->ncommunities,
1428 sizeof(*communities), GFP_KERNEL);
1433 for (i = 0; i < pctrl->ncommunities; i++) {
1434 struct intel_community *community = &pctrl->communities[i];
1435 u32 *intmask, *hostown;
1437 intmask = devm_kcalloc(pctrl->dev, community->ngpps,
1438 sizeof(*intmask), GFP_KERNEL);
1442 communities[i].intmask = intmask;
1444 hostown = devm_kcalloc(pctrl->dev, community->ngpps,
1445 sizeof(*hostown), GFP_KERNEL);
1449 communities[i].hostown = hostown;
1452 pctrl->context.pads = pads;
1453 pctrl->context.communities = communities;
1459 static int intel_pinctrl_probe(struct platform_device *pdev,
1460 const struct intel_pinctrl_soc_data *soc_data)
1462 struct intel_pinctrl *pctrl;
1465 pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL);
1469 pctrl->dev = &pdev->dev;
1470 pctrl->soc = soc_data;
1471 raw_spin_lock_init(&pctrl->lock);
1474 * Make a copy of the communities which we can use to hold pointers
1477 pctrl->ncommunities = pctrl->soc->ncommunities;
1478 pctrl->communities = devm_kcalloc(&pdev->dev, pctrl->ncommunities,
1479 sizeof(*pctrl->communities), GFP_KERNEL);
1480 if (!pctrl->communities)
1483 for (i = 0; i < pctrl->ncommunities; i++) {
1484 struct intel_community *community = &pctrl->communities[i];
1488 *community = pctrl->soc->communities[i];
1490 regs = devm_platform_ioremap_resource(pdev, community->barno);
1492 return PTR_ERR(regs);
1495 * Determine community features based on the revision if
1496 * not specified already.
1498 if (!community->features) {
1501 rev = (readl(regs + REVID) & REVID_MASK) >> REVID_SHIFT;
1503 community->features |= PINCTRL_FEATURE_DEBOUNCE;
1504 community->features |= PINCTRL_FEATURE_1K_PD;
1508 /* Read offset of the pad configuration registers */
1509 padbar = readl(regs + PADBAR);
1511 community->regs = regs;
1512 community->pad_regs = regs + padbar;
1514 ret = intel_pinctrl_add_padgroups(pctrl, community);
1519 irq = platform_get_irq(pdev, 0);
1523 ret = intel_pinctrl_pm_init(pctrl);
1527 pctrl->pctldesc = intel_pinctrl_desc;
1528 pctrl->pctldesc.name = dev_name(&pdev->dev);
1529 pctrl->pctldesc.pins = pctrl->soc->pins;
1530 pctrl->pctldesc.npins = pctrl->soc->npins;
1532 pctrl->pctldev = devm_pinctrl_register(&pdev->dev, &pctrl->pctldesc,
1534 if (IS_ERR(pctrl->pctldev)) {
1535 dev_err(&pdev->dev, "failed to register pinctrl driver\n");
1536 return PTR_ERR(pctrl->pctldev);
1539 ret = intel_gpio_probe(pctrl, irq);
1543 platform_set_drvdata(pdev, pctrl);
1548 int intel_pinctrl_probe_by_hid(struct platform_device *pdev)
1550 const struct intel_pinctrl_soc_data *data;
1552 data = device_get_match_data(&pdev->dev);
1556 return intel_pinctrl_probe(pdev, data);
1558 EXPORT_SYMBOL_GPL(intel_pinctrl_probe_by_hid);
1560 int intel_pinctrl_probe_by_uid(struct platform_device *pdev)
1562 const struct intel_pinctrl_soc_data *data;
1564 data = intel_pinctrl_get_soc_data(pdev);
1566 return PTR_ERR(data);
1568 return intel_pinctrl_probe(pdev, data);
1570 EXPORT_SYMBOL_GPL(intel_pinctrl_probe_by_uid);
1572 const struct intel_pinctrl_soc_data *intel_pinctrl_get_soc_data(struct platform_device *pdev)
1574 const struct intel_pinctrl_soc_data * const *table;
1575 const struct intel_pinctrl_soc_data *data = NULL;
1577 table = device_get_match_data(&pdev->dev);
1579 struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
1582 for (i = 0; table[i]; i++) {
1583 if (!strcmp(adev->pnp.unique_id, table[i]->uid)) {
1589 const struct platform_device_id *id;
1591 id = platform_get_device_id(pdev);
1593 return ERR_PTR(-ENODEV);
1595 table = (const struct intel_pinctrl_soc_data * const *)id->driver_data;
1596 data = table[pdev->id];
1599 return data ?: ERR_PTR(-ENODATA);
1601 EXPORT_SYMBOL_GPL(intel_pinctrl_get_soc_data);
1603 #ifdef CONFIG_PM_SLEEP
1604 static bool intel_pinctrl_should_save(struct intel_pinctrl *pctrl, unsigned int pin)
1606 const struct pin_desc *pd = pin_desc_get(pctrl->pctldev, pin);
1608 if (!pd || !intel_pad_usable(pctrl, pin))
1612 * Only restore the pin if it is actually in use by the kernel (or
1613 * by userspace). It is possible that some pins are used by the
1614 * BIOS during resume and those are not always locked down so leave
1617 if (pd->mux_owner || pd->gpio_owner ||
1618 gpiochip_line_is_irq(&pctrl->chip, intel_pin_to_gpio(pctrl, pin)))
1624 int intel_pinctrl_suspend_noirq(struct device *dev)
1626 struct intel_pinctrl *pctrl = dev_get_drvdata(dev);
1627 struct intel_community_context *communities;
1628 struct intel_pad_context *pads;
1631 pads = pctrl->context.pads;
1632 for (i = 0; i < pctrl->soc->npins; i++) {
1633 const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
1634 void __iomem *padcfg;
1637 if (!intel_pinctrl_should_save(pctrl, desc->number))
1640 val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG0));
1641 pads[i].padcfg0 = val & ~PADCFG0_GPIORXSTATE;
1642 val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG1));
1643 pads[i].padcfg1 = val;
1645 padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG2);
1647 pads[i].padcfg2 = readl(padcfg);
1650 communities = pctrl->context.communities;
1651 for (i = 0; i < pctrl->ncommunities; i++) {
1652 struct intel_community *community = &pctrl->communities[i];
1656 base = community->regs + community->ie_offset;
1657 for (gpp = 0; gpp < community->ngpps; gpp++)
1658 communities[i].intmask[gpp] = readl(base + gpp * 4);
1660 base = community->regs + community->hostown_offset;
1661 for (gpp = 0; gpp < community->ngpps; gpp++)
1662 communities[i].hostown[gpp] = readl(base + gpp * 4);
1667 EXPORT_SYMBOL_GPL(intel_pinctrl_suspend_noirq);
1669 static bool intel_gpio_update_reg(void __iomem *reg, u32 mask, u32 value)
1675 updated = (curr & ~mask) | (value & mask);
1676 if (curr == updated)
1679 writel(updated, reg);
1683 static void intel_restore_hostown(struct intel_pinctrl *pctrl, unsigned int c,
1684 void __iomem *base, unsigned int gpp, u32 saved)
1686 const struct intel_community *community = &pctrl->communities[c];
1687 const struct intel_padgroup *padgrp = &community->gpps[gpp];
1688 struct device *dev = pctrl->dev;
1693 if (padgrp->gpio_base == INTEL_GPIO_BASE_NOMAP)
1696 for_each_requested_gpio_in_range(&pctrl->chip, i, padgrp->gpio_base, padgrp->size, dummy)
1697 requested |= BIT(i);
1699 if (!intel_gpio_update_reg(base + gpp * 4, requested, saved))
1702 dev_dbg(dev, "restored hostown %u/%u %#08x\n", c, gpp, readl(base + gpp * 4));
1705 static void intel_restore_intmask(struct intel_pinctrl *pctrl, unsigned int c,
1706 void __iomem *base, unsigned int gpp, u32 saved)
1708 struct device *dev = pctrl->dev;
1710 if (!intel_gpio_update_reg(base + gpp * 4, ~0U, saved))
1713 dev_dbg(dev, "restored mask %u/%u %#08x\n", c, gpp, readl(base + gpp * 4));
1716 static void intel_restore_padcfg(struct intel_pinctrl *pctrl, unsigned int pin,
1717 unsigned int reg, u32 saved)
1719 u32 mask = (reg == PADCFG0) ? PADCFG0_GPIORXSTATE : 0;
1720 unsigned int n = reg / sizeof(u32);
1721 struct device *dev = pctrl->dev;
1722 void __iomem *padcfg;
1724 padcfg = intel_get_padcfg(pctrl, pin, reg);
1728 if (!intel_gpio_update_reg(padcfg, ~mask, saved))
1731 dev_dbg(dev, "restored pin %u padcfg%u %#08x\n", pin, n, readl(padcfg));
1734 int intel_pinctrl_resume_noirq(struct device *dev)
1736 struct intel_pinctrl *pctrl = dev_get_drvdata(dev);
1737 const struct intel_community_context *communities;
1738 const struct intel_pad_context *pads;
1741 /* Mask all interrupts */
1742 intel_gpio_irq_init(pctrl);
1744 pads = pctrl->context.pads;
1745 for (i = 0; i < pctrl->soc->npins; i++) {
1746 const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
1748 if (!intel_pinctrl_should_save(pctrl, desc->number))
1751 intel_restore_padcfg(pctrl, desc->number, PADCFG0, pads[i].padcfg0);
1752 intel_restore_padcfg(pctrl, desc->number, PADCFG1, pads[i].padcfg1);
1753 intel_restore_padcfg(pctrl, desc->number, PADCFG2, pads[i].padcfg2);
1756 communities = pctrl->context.communities;
1757 for (i = 0; i < pctrl->ncommunities; i++) {
1758 struct intel_community *community = &pctrl->communities[i];
1762 base = community->regs + community->ie_offset;
1763 for (gpp = 0; gpp < community->ngpps; gpp++)
1764 intel_restore_intmask(pctrl, i, base, gpp, communities[i].intmask[gpp]);
1766 base = community->regs + community->hostown_offset;
1767 for (gpp = 0; gpp < community->ngpps; gpp++)
1768 intel_restore_hostown(pctrl, i, base, gpp, communities[i].hostown[gpp]);
1773 EXPORT_SYMBOL_GPL(intel_pinctrl_resume_noirq);
1776 MODULE_AUTHOR("Mathias Nyman <mathias.nyman@linux.intel.com>");
1777 MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
1778 MODULE_DESCRIPTION("Intel pinctrl/GPIO core driver");
1779 MODULE_LICENSE("GPL v2");