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/interrupt.h>
12 #include <linux/gpio/driver.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_GPIORXDIS BIT(9)
56 #define PADCFG0_GPIOTXDIS BIT(8)
57 #define PADCFG0_GPIORXSTATE BIT(1)
58 #define PADCFG0_GPIOTXSTATE BIT(0)
61 #define PADCFG1_TERM_UP BIT(13)
62 #define PADCFG1_TERM_SHIFT 10
63 #define PADCFG1_TERM_MASK GENMASK(12, 10)
64 #define PADCFG1_TERM_20K 4
65 #define PADCFG1_TERM_2K 3
66 #define PADCFG1_TERM_5K 2
67 #define PADCFG1_TERM_1K 1
70 #define PADCFG2_DEBEN BIT(0)
71 #define PADCFG2_DEBOUNCE_SHIFT 1
72 #define PADCFG2_DEBOUNCE_MASK GENMASK(4, 1)
74 #define DEBOUNCE_PERIOD_NSEC 31250
76 struct intel_pad_context {
82 struct intel_community_context {
87 struct intel_pinctrl_context {
88 struct intel_pad_context *pads;
89 struct intel_community_context *communities;
93 * struct intel_pinctrl - Intel pinctrl private structure
94 * @dev: Pointer to the device structure
95 * @lock: Lock to serialize register access
96 * @pctldesc: Pin controller description
97 * @pctldev: Pointer to the pin controller device
98 * @chip: GPIO chip in this pin controller
99 * @soc: SoC/PCH specific pin configuration data
100 * @communities: All communities in this pin controller
101 * @ncommunities: Number of communities in this pin controller
102 * @context: Configuration saved over system sleep
103 * @irq: pinctrl/GPIO chip irq number
105 struct intel_pinctrl {
108 struct pinctrl_desc pctldesc;
109 struct pinctrl_dev *pctldev;
110 struct gpio_chip chip;
111 const struct intel_pinctrl_soc_data *soc;
112 struct intel_community *communities;
114 struct intel_pinctrl_context context;
118 #define pin_to_padno(c, p) ((p) - (c)->pin_base)
119 #define padgroup_offset(g, p) ((p) - (g)->base)
121 static struct intel_community *intel_get_community(struct intel_pinctrl *pctrl,
124 struct intel_community *community;
127 for (i = 0; i < pctrl->ncommunities; i++) {
128 community = &pctrl->communities[i];
129 if (pin >= community->pin_base &&
130 pin < community->pin_base + community->npins)
134 dev_warn(pctrl->dev, "failed to find community for pin %u\n", pin);
138 static const struct intel_padgroup *
139 intel_community_get_padgroup(const struct intel_community *community,
144 for (i = 0; i < community->ngpps; i++) {
145 const struct intel_padgroup *padgrp = &community->gpps[i];
147 if (pin >= padgrp->base && pin < padgrp->base + padgrp->size)
154 static void __iomem *intel_get_padcfg(struct intel_pinctrl *pctrl,
155 unsigned int pin, unsigned int reg)
157 const struct intel_community *community;
161 community = intel_get_community(pctrl, pin);
165 padno = pin_to_padno(community, pin);
166 nregs = (community->features & PINCTRL_FEATURE_DEBOUNCE) ? 4 : 2;
168 if (reg >= nregs * 4)
171 return community->pad_regs + reg + padno * nregs * 4;
174 static bool intel_pad_owned_by_host(struct intel_pinctrl *pctrl, unsigned int pin)
176 const struct intel_community *community;
177 const struct intel_padgroup *padgrp;
178 unsigned int gpp, offset, gpp_offset;
179 void __iomem *padown;
181 community = intel_get_community(pctrl, pin);
184 if (!community->padown_offset)
187 padgrp = intel_community_get_padgroup(community, pin);
191 gpp_offset = padgroup_offset(padgrp, pin);
192 gpp = PADOWN_GPP(gpp_offset);
193 offset = community->padown_offset + padgrp->padown_num * 4 + gpp * 4;
194 padown = community->regs + offset;
196 return !(readl(padown) & PADOWN_MASK(gpp_offset));
199 static bool intel_pad_acpi_mode(struct intel_pinctrl *pctrl, unsigned int pin)
201 const struct intel_community *community;
202 const struct intel_padgroup *padgrp;
203 unsigned int offset, gpp_offset;
204 void __iomem *hostown;
206 community = intel_get_community(pctrl, pin);
209 if (!community->hostown_offset)
212 padgrp = intel_community_get_padgroup(community, pin);
216 gpp_offset = padgroup_offset(padgrp, pin);
217 offset = community->hostown_offset + padgrp->reg_num * 4;
218 hostown = community->regs + offset;
220 return !(readl(hostown) & BIT(gpp_offset));
224 * enum - Locking variants of the pad configuration
226 * @PAD_UNLOCKED: pad is fully controlled by the configuration registers
227 * @PAD_LOCKED: pad configuration registers, except TX state, are locked
228 * @PAD_LOCKED_TX: pad configuration TX state is locked
229 * @PAD_LOCKED_FULL: pad configuration registers are locked completely
231 * Locking is considered as read-only mode for corresponding registers and
232 * their respective fields. That said, TX state bit is locked separately from
233 * the main locking scheme.
239 PAD_LOCKED_FULL = PAD_LOCKED | PAD_LOCKED_TX,
242 static int intel_pad_locked(struct intel_pinctrl *pctrl, unsigned int pin)
244 struct intel_community *community;
245 const struct intel_padgroup *padgrp;
246 unsigned int offset, gpp_offset;
248 int ret = PAD_UNLOCKED;
250 community = intel_get_community(pctrl, pin);
252 return PAD_LOCKED_FULL;
253 if (!community->padcfglock_offset)
256 padgrp = intel_community_get_padgroup(community, pin);
258 return PAD_LOCKED_FULL;
260 gpp_offset = padgroup_offset(padgrp, pin);
263 * If PADCFGLOCK and PADCFGLOCKTX bits are both clear for this pad,
264 * the pad is considered unlocked. Any other case means that it is
265 * either fully or partially locked.
267 offset = community->padcfglock_offset + 0 + padgrp->reg_num * 8;
268 value = readl(community->regs + offset);
269 if (value & BIT(gpp_offset))
272 offset = community->padcfglock_offset + 4 + padgrp->reg_num * 8;
273 value = readl(community->regs + offset);
274 if (value & BIT(gpp_offset))
275 ret |= PAD_LOCKED_TX;
280 static bool intel_pad_is_unlocked(struct intel_pinctrl *pctrl, unsigned int pin)
282 return (intel_pad_locked(pctrl, pin) & PAD_LOCKED) == PAD_UNLOCKED;
285 static bool intel_pad_usable(struct intel_pinctrl *pctrl, unsigned int pin)
287 return intel_pad_owned_by_host(pctrl, pin) && intel_pad_is_unlocked(pctrl, pin);
290 static int intel_get_groups_count(struct pinctrl_dev *pctldev)
292 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
294 return pctrl->soc->ngroups;
297 static const char *intel_get_group_name(struct pinctrl_dev *pctldev,
300 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
302 return pctrl->soc->groups[group].name;
305 static int intel_get_group_pins(struct pinctrl_dev *pctldev, unsigned int group,
306 const unsigned int **pins, unsigned int *npins)
308 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
310 *pins = pctrl->soc->groups[group].pins;
311 *npins = pctrl->soc->groups[group].npins;
315 static void intel_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
318 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
319 void __iomem *padcfg;
320 u32 cfg0, cfg1, mode;
324 if (!intel_pad_owned_by_host(pctrl, pin)) {
325 seq_puts(s, "not available");
329 cfg0 = readl(intel_get_padcfg(pctrl, pin, PADCFG0));
330 cfg1 = readl(intel_get_padcfg(pctrl, pin, PADCFG1));
332 mode = (cfg0 & PADCFG0_PMODE_MASK) >> PADCFG0_PMODE_SHIFT;
334 seq_puts(s, "GPIO ");
336 seq_printf(s, "mode %d ", mode);
338 seq_printf(s, "0x%08x 0x%08x", cfg0, cfg1);
340 /* Dump the additional PADCFG registers if available */
341 padcfg = intel_get_padcfg(pctrl, pin, PADCFG2);
343 seq_printf(s, " 0x%08x", readl(padcfg));
345 locked = intel_pad_locked(pctrl, pin);
346 acpi = intel_pad_acpi_mode(pctrl, pin);
348 if (locked || acpi) {
351 seq_puts(s, "LOCKED");
352 if ((locked & PAD_LOCKED_FULL) == PAD_LOCKED_TX)
354 else if ((locked & PAD_LOCKED_FULL) == PAD_LOCKED_FULL)
355 seq_puts(s, " full");
366 static const struct pinctrl_ops intel_pinctrl_ops = {
367 .get_groups_count = intel_get_groups_count,
368 .get_group_name = intel_get_group_name,
369 .get_group_pins = intel_get_group_pins,
370 .pin_dbg_show = intel_pin_dbg_show,
373 static int intel_get_functions_count(struct pinctrl_dev *pctldev)
375 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
377 return pctrl->soc->nfunctions;
380 static const char *intel_get_function_name(struct pinctrl_dev *pctldev,
381 unsigned int function)
383 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
385 return pctrl->soc->functions[function].name;
388 static int intel_get_function_groups(struct pinctrl_dev *pctldev,
389 unsigned int function,
390 const char * const **groups,
391 unsigned int * const ngroups)
393 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
395 *groups = pctrl->soc->functions[function].groups;
396 *ngroups = pctrl->soc->functions[function].ngroups;
400 static int intel_pinmux_set_mux(struct pinctrl_dev *pctldev,
401 unsigned int function, unsigned int group)
403 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
404 const struct intel_pingroup *grp = &pctrl->soc->groups[group];
408 raw_spin_lock_irqsave(&pctrl->lock, flags);
411 * All pins in the groups needs to be accessible and writable
412 * before we can enable the mux for this group.
414 for (i = 0; i < grp->npins; i++) {
415 if (!intel_pad_usable(pctrl, grp->pins[i])) {
416 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
421 /* Now enable the mux setting for each pin in the group */
422 for (i = 0; i < grp->npins; i++) {
423 void __iomem *padcfg0;
426 padcfg0 = intel_get_padcfg(pctrl, grp->pins[i], PADCFG0);
427 value = readl(padcfg0);
429 value &= ~PADCFG0_PMODE_MASK;
432 value |= grp->modes[i] << PADCFG0_PMODE_SHIFT;
434 value |= grp->mode << PADCFG0_PMODE_SHIFT;
436 writel(value, padcfg0);
439 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
444 static void __intel_gpio_set_direction(void __iomem *padcfg0, bool input)
448 value = readl(padcfg0);
450 value &= ~PADCFG0_GPIORXDIS;
451 value |= PADCFG0_GPIOTXDIS;
453 value &= ~PADCFG0_GPIOTXDIS;
454 value |= PADCFG0_GPIORXDIS;
456 writel(value, padcfg0);
459 static void intel_gpio_set_gpio_mode(void __iomem *padcfg0)
463 /* Put the pad into GPIO mode */
464 value = readl(padcfg0) & ~PADCFG0_PMODE_MASK;
465 /* Disable SCI/SMI/NMI generation */
466 value &= ~(PADCFG0_GPIROUTIOXAPIC | PADCFG0_GPIROUTSCI);
467 value &= ~(PADCFG0_GPIROUTSMI | PADCFG0_GPIROUTNMI);
468 writel(value, padcfg0);
471 static int intel_gpio_request_enable(struct pinctrl_dev *pctldev,
472 struct pinctrl_gpio_range *range,
475 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
476 void __iomem *padcfg0;
479 raw_spin_lock_irqsave(&pctrl->lock, flags);
481 if (!intel_pad_owned_by_host(pctrl, pin)) {
482 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
486 if (!intel_pad_is_unlocked(pctrl, pin)) {
487 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
491 padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
492 intel_gpio_set_gpio_mode(padcfg0);
493 /* Disable TX buffer and enable RX (this will be input) */
494 __intel_gpio_set_direction(padcfg0, true);
496 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
501 static int intel_gpio_set_direction(struct pinctrl_dev *pctldev,
502 struct pinctrl_gpio_range *range,
503 unsigned int pin, bool input)
505 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
506 void __iomem *padcfg0;
509 raw_spin_lock_irqsave(&pctrl->lock, flags);
511 padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
512 __intel_gpio_set_direction(padcfg0, input);
514 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
519 static const struct pinmux_ops intel_pinmux_ops = {
520 .get_functions_count = intel_get_functions_count,
521 .get_function_name = intel_get_function_name,
522 .get_function_groups = intel_get_function_groups,
523 .set_mux = intel_pinmux_set_mux,
524 .gpio_request_enable = intel_gpio_request_enable,
525 .gpio_set_direction = intel_gpio_set_direction,
528 static int intel_config_get(struct pinctrl_dev *pctldev, unsigned int pin,
529 unsigned long *config)
531 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
532 enum pin_config_param param = pinconf_to_config_param(*config);
533 const struct intel_community *community;
537 if (!intel_pad_owned_by_host(pctrl, pin))
540 community = intel_get_community(pctrl, pin);
541 value = readl(intel_get_padcfg(pctrl, pin, PADCFG1));
542 term = (value & PADCFG1_TERM_MASK) >> PADCFG1_TERM_SHIFT;
545 case PIN_CONFIG_BIAS_DISABLE:
550 case PIN_CONFIG_BIAS_PULL_UP:
551 if (!term || !(value & PADCFG1_TERM_UP))
555 case PADCFG1_TERM_1K:
558 case PADCFG1_TERM_2K:
561 case PADCFG1_TERM_5K:
564 case PADCFG1_TERM_20K:
571 case PIN_CONFIG_BIAS_PULL_DOWN:
572 if (!term || value & PADCFG1_TERM_UP)
576 case PADCFG1_TERM_1K:
577 if (!(community->features & PINCTRL_FEATURE_1K_PD))
581 case PADCFG1_TERM_5K:
584 case PADCFG1_TERM_20K:
591 case PIN_CONFIG_INPUT_DEBOUNCE: {
592 void __iomem *padcfg2;
595 padcfg2 = intel_get_padcfg(pctrl, pin, PADCFG2);
600 if (!(v & PADCFG2_DEBEN))
603 v = (v & PADCFG2_DEBOUNCE_MASK) >> PADCFG2_DEBOUNCE_SHIFT;
604 arg = BIT(v) * DEBOUNCE_PERIOD_NSEC / NSEC_PER_USEC;
613 *config = pinconf_to_config_packed(param, arg);
617 static int intel_config_set_pull(struct intel_pinctrl *pctrl, unsigned int pin,
618 unsigned long config)
620 unsigned int param = pinconf_to_config_param(config);
621 unsigned int arg = pinconf_to_config_argument(config);
622 const struct intel_community *community;
623 void __iomem *padcfg1;
628 raw_spin_lock_irqsave(&pctrl->lock, flags);
630 community = intel_get_community(pctrl, pin);
631 padcfg1 = intel_get_padcfg(pctrl, pin, PADCFG1);
632 value = readl(padcfg1);
635 case PIN_CONFIG_BIAS_DISABLE:
636 value &= ~(PADCFG1_TERM_MASK | PADCFG1_TERM_UP);
639 case PIN_CONFIG_BIAS_PULL_UP:
640 value &= ~PADCFG1_TERM_MASK;
642 value |= PADCFG1_TERM_UP;
646 value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT;
649 value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT;
652 value |= PADCFG1_TERM_2K << PADCFG1_TERM_SHIFT;
655 value |= PADCFG1_TERM_1K << PADCFG1_TERM_SHIFT;
663 case PIN_CONFIG_BIAS_PULL_DOWN:
664 value &= ~(PADCFG1_TERM_UP | PADCFG1_TERM_MASK);
668 value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT;
671 value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT;
674 if (!(community->features & PINCTRL_FEATURE_1K_PD)) {
678 value |= PADCFG1_TERM_1K << PADCFG1_TERM_SHIFT;
688 writel(value, padcfg1);
690 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
695 static int intel_config_set_debounce(struct intel_pinctrl *pctrl,
696 unsigned int pin, unsigned int debounce)
698 void __iomem *padcfg0, *padcfg2;
703 padcfg2 = intel_get_padcfg(pctrl, pin, PADCFG2);
707 padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
709 raw_spin_lock_irqsave(&pctrl->lock, flags);
711 value0 = readl(padcfg0);
712 value2 = readl(padcfg2);
714 /* Disable glitch filter and debouncer */
715 value0 &= ~PADCFG0_PREGFRXSEL;
716 value2 &= ~(PADCFG2_DEBEN | PADCFG2_DEBOUNCE_MASK);
721 v = order_base_2(debounce * NSEC_PER_USEC / DEBOUNCE_PERIOD_NSEC);
722 if (v < 3 || v > 15) {
726 /* Enable glitch filter and debouncer */
727 value0 |= PADCFG0_PREGFRXSEL;
728 value2 |= v << PADCFG2_DEBOUNCE_SHIFT;
729 value2 |= PADCFG2_DEBEN;
733 writel(value0, padcfg0);
734 writel(value2, padcfg2);
737 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
742 static int intel_config_set(struct pinctrl_dev *pctldev, unsigned int pin,
743 unsigned long *configs, unsigned int nconfigs)
745 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
748 if (!intel_pad_usable(pctrl, pin))
751 for (i = 0; i < nconfigs; i++) {
752 switch (pinconf_to_config_param(configs[i])) {
753 case PIN_CONFIG_BIAS_DISABLE:
754 case PIN_CONFIG_BIAS_PULL_UP:
755 case PIN_CONFIG_BIAS_PULL_DOWN:
756 ret = intel_config_set_pull(pctrl, pin, configs[i]);
761 case PIN_CONFIG_INPUT_DEBOUNCE:
762 ret = intel_config_set_debounce(pctrl, pin,
763 pinconf_to_config_argument(configs[i]));
776 static const struct pinconf_ops intel_pinconf_ops = {
778 .pin_config_get = intel_config_get,
779 .pin_config_set = intel_config_set,
782 static const struct pinctrl_desc intel_pinctrl_desc = {
783 .pctlops = &intel_pinctrl_ops,
784 .pmxops = &intel_pinmux_ops,
785 .confops = &intel_pinconf_ops,
786 .owner = THIS_MODULE,
790 * intel_gpio_to_pin() - Translate from GPIO offset to pin number
791 * @pctrl: Pinctrl structure
792 * @offset: GPIO offset from gpiolib
793 * @community: Community is filled here if not %NULL
794 * @padgrp: Pad group is filled here if not %NULL
796 * When coming through gpiolib irqchip, the GPIO offset is not
797 * automatically translated to pinctrl pin number. This function can be
798 * used to find out the corresponding pinctrl pin.
800 static int intel_gpio_to_pin(struct intel_pinctrl *pctrl, unsigned int offset,
801 const struct intel_community **community,
802 const struct intel_padgroup **padgrp)
806 for (i = 0; i < pctrl->ncommunities; i++) {
807 const struct intel_community *comm = &pctrl->communities[i];
810 for (j = 0; j < comm->ngpps; j++) {
811 const struct intel_padgroup *pgrp = &comm->gpps[j];
813 if (pgrp->gpio_base < 0)
816 if (offset >= pgrp->gpio_base &&
817 offset < pgrp->gpio_base + pgrp->size) {
820 pin = pgrp->base + offset - pgrp->gpio_base;
834 static int intel_gpio_get(struct gpio_chip *chip, unsigned int offset)
836 struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
841 pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL);
845 reg = intel_get_padcfg(pctrl, pin, PADCFG0);
849 padcfg0 = readl(reg);
850 if (!(padcfg0 & PADCFG0_GPIOTXDIS))
851 return !!(padcfg0 & PADCFG0_GPIOTXSTATE);
853 return !!(padcfg0 & PADCFG0_GPIORXSTATE);
856 static void intel_gpio_set(struct gpio_chip *chip, unsigned int offset,
859 struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
865 pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL);
869 reg = intel_get_padcfg(pctrl, pin, PADCFG0);
873 raw_spin_lock_irqsave(&pctrl->lock, flags);
874 padcfg0 = readl(reg);
876 padcfg0 |= PADCFG0_GPIOTXSTATE;
878 padcfg0 &= ~PADCFG0_GPIOTXSTATE;
879 writel(padcfg0, reg);
880 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
883 static int intel_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
885 struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
890 pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL);
894 reg = intel_get_padcfg(pctrl, pin, PADCFG0);
898 padcfg0 = readl(reg);
900 if (padcfg0 & PADCFG0_PMODE_MASK)
903 return !!(padcfg0 & PADCFG0_GPIOTXDIS);
906 static int intel_gpio_direction_input(struct gpio_chip *chip, unsigned int offset)
908 return pinctrl_gpio_direction_input(chip->base + offset);
911 static int intel_gpio_direction_output(struct gpio_chip *chip, unsigned int offset,
914 intel_gpio_set(chip, offset, value);
915 return pinctrl_gpio_direction_output(chip->base + offset);
918 static const struct gpio_chip intel_gpio_chip = {
919 .owner = THIS_MODULE,
920 .request = gpiochip_generic_request,
921 .free = gpiochip_generic_free,
922 .get_direction = intel_gpio_get_direction,
923 .direction_input = intel_gpio_direction_input,
924 .direction_output = intel_gpio_direction_output,
925 .get = intel_gpio_get,
926 .set = intel_gpio_set,
927 .set_config = gpiochip_generic_config,
930 static void intel_gpio_irq_ack(struct irq_data *d)
932 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
933 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
934 const struct intel_community *community;
935 const struct intel_padgroup *padgrp;
938 pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), &community, &padgrp);
940 unsigned int gpp, gpp_offset, is_offset;
942 gpp = padgrp->reg_num;
943 gpp_offset = padgroup_offset(padgrp, pin);
944 is_offset = community->is_offset + gpp * 4;
946 raw_spin_lock(&pctrl->lock);
947 writel(BIT(gpp_offset), community->regs + is_offset);
948 raw_spin_unlock(&pctrl->lock);
952 static void intel_gpio_irq_mask_unmask(struct irq_data *d, bool mask)
954 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
955 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
956 const struct intel_community *community;
957 const struct intel_padgroup *padgrp;
960 pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), &community, &padgrp);
962 unsigned int gpp, gpp_offset;
964 void __iomem *reg, *is;
967 gpp = padgrp->reg_num;
968 gpp_offset = padgroup_offset(padgrp, pin);
970 reg = community->regs + community->ie_offset + gpp * 4;
971 is = community->regs + community->is_offset + gpp * 4;
973 raw_spin_lock_irqsave(&pctrl->lock, flags);
975 /* Clear interrupt status first to avoid unexpected interrupt */
976 writel(BIT(gpp_offset), is);
980 value &= ~BIT(gpp_offset);
982 value |= BIT(gpp_offset);
984 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
988 static void intel_gpio_irq_mask(struct irq_data *d)
990 intel_gpio_irq_mask_unmask(d, true);
993 static void intel_gpio_irq_unmask(struct irq_data *d)
995 intel_gpio_irq_mask_unmask(d, false);
998 static int intel_gpio_irq_type(struct irq_data *d, unsigned int type)
1000 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1001 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
1002 unsigned int pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), NULL, NULL);
1003 unsigned long flags;
1007 reg = intel_get_padcfg(pctrl, pin, PADCFG0);
1012 * If the pin is in ACPI mode it is still usable as a GPIO but it
1013 * cannot be used as IRQ because GPI_IS status bit will not be
1014 * updated by the host controller hardware.
1016 if (intel_pad_acpi_mode(pctrl, pin)) {
1017 dev_warn(pctrl->dev, "pin %u cannot be used as IRQ\n", pin);
1021 raw_spin_lock_irqsave(&pctrl->lock, flags);
1023 intel_gpio_set_gpio_mode(reg);
1027 value &= ~(PADCFG0_RXEVCFG_MASK | PADCFG0_RXINV);
1029 if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
1030 value |= PADCFG0_RXEVCFG_EDGE_BOTH << PADCFG0_RXEVCFG_SHIFT;
1031 } else if (type & IRQ_TYPE_EDGE_FALLING) {
1032 value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT;
1033 value |= PADCFG0_RXINV;
1034 } else if (type & IRQ_TYPE_EDGE_RISING) {
1035 value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT;
1036 } else if (type & IRQ_TYPE_LEVEL_MASK) {
1037 if (type & IRQ_TYPE_LEVEL_LOW)
1038 value |= PADCFG0_RXINV;
1040 value |= PADCFG0_RXEVCFG_DISABLED << PADCFG0_RXEVCFG_SHIFT;
1045 if (type & IRQ_TYPE_EDGE_BOTH)
1046 irq_set_handler_locked(d, handle_edge_irq);
1047 else if (type & IRQ_TYPE_LEVEL_MASK)
1048 irq_set_handler_locked(d, handle_level_irq);
1050 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
1055 static int intel_gpio_irq_wake(struct irq_data *d, unsigned int on)
1057 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1058 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
1059 unsigned int pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), NULL, NULL);
1062 enable_irq_wake(pctrl->irq);
1064 disable_irq_wake(pctrl->irq);
1066 dev_dbg(pctrl->dev, "%sable wake for pin %u\n", on ? "en" : "dis", pin);
1070 static irqreturn_t intel_gpio_community_irq_handler(struct intel_pinctrl *pctrl,
1071 const struct intel_community *community)
1073 struct gpio_chip *gc = &pctrl->chip;
1074 irqreturn_t ret = IRQ_NONE;
1077 for (gpp = 0; gpp < community->ngpps; gpp++) {
1078 const struct intel_padgroup *padgrp = &community->gpps[gpp];
1079 unsigned long pending, enabled, gpp_offset;
1081 pending = readl(community->regs + community->is_offset +
1082 padgrp->reg_num * 4);
1083 enabled = readl(community->regs + community->ie_offset +
1084 padgrp->reg_num * 4);
1086 /* Only interrupts that are enabled */
1089 for_each_set_bit(gpp_offset, &pending, padgrp->size) {
1092 irq = irq_find_mapping(gc->irq.domain,
1093 padgrp->gpio_base + gpp_offset);
1094 generic_handle_irq(irq);
1103 static irqreturn_t intel_gpio_irq(int irq, void *data)
1105 const struct intel_community *community;
1106 struct intel_pinctrl *pctrl = data;
1107 irqreturn_t ret = IRQ_NONE;
1110 /* Need to check all communities for pending interrupts */
1111 for (i = 0; i < pctrl->ncommunities; i++) {
1112 community = &pctrl->communities[i];
1113 ret |= intel_gpio_community_irq_handler(pctrl, community);
1119 static struct irq_chip intel_gpio_irqchip = {
1120 .name = "intel-gpio",
1121 .irq_ack = intel_gpio_irq_ack,
1122 .irq_mask = intel_gpio_irq_mask,
1123 .irq_unmask = intel_gpio_irq_unmask,
1124 .irq_set_type = intel_gpio_irq_type,
1125 .irq_set_wake = intel_gpio_irq_wake,
1126 .flags = IRQCHIP_MASK_ON_SUSPEND,
1129 static int intel_gpio_add_pin_ranges(struct intel_pinctrl *pctrl,
1130 const struct intel_community *community)
1134 for (i = 0; i < community->ngpps; i++) {
1135 const struct intel_padgroup *gpp = &community->gpps[i];
1137 if (gpp->gpio_base < 0)
1140 ret = gpiochip_add_pin_range(&pctrl->chip, dev_name(pctrl->dev),
1141 gpp->gpio_base, gpp->base,
1150 static unsigned intel_gpio_ngpio(const struct intel_pinctrl *pctrl)
1152 const struct intel_community *community;
1153 unsigned int ngpio = 0;
1156 for (i = 0; i < pctrl->ncommunities; i++) {
1157 community = &pctrl->communities[i];
1158 for (j = 0; j < community->ngpps; j++) {
1159 const struct intel_padgroup *gpp = &community->gpps[j];
1161 if (gpp->gpio_base < 0)
1164 if (gpp->gpio_base + gpp->size > ngpio)
1165 ngpio = gpp->gpio_base + gpp->size;
1172 static int intel_gpio_probe(struct intel_pinctrl *pctrl, int irq)
1176 pctrl->chip = intel_gpio_chip;
1178 pctrl->chip.ngpio = intel_gpio_ngpio(pctrl);
1179 pctrl->chip.label = dev_name(pctrl->dev);
1180 pctrl->chip.parent = pctrl->dev;
1181 pctrl->chip.base = -1;
1184 ret = devm_gpiochip_add_data(pctrl->dev, &pctrl->chip, pctrl);
1186 dev_err(pctrl->dev, "failed to register gpiochip\n");
1190 for (i = 0; i < pctrl->ncommunities; i++) {
1191 struct intel_community *community = &pctrl->communities[i];
1193 ret = intel_gpio_add_pin_ranges(pctrl, community);
1195 dev_err(pctrl->dev, "failed to add GPIO pin range\n");
1201 * We need to request the interrupt here (instead of providing chip
1202 * to the irq directly) because on some platforms several GPIO
1203 * controllers share the same interrupt line.
1205 ret = devm_request_irq(pctrl->dev, irq, intel_gpio_irq,
1206 IRQF_SHARED | IRQF_NO_THREAD,
1207 dev_name(pctrl->dev), pctrl);
1209 dev_err(pctrl->dev, "failed to request interrupt\n");
1213 ret = gpiochip_irqchip_add(&pctrl->chip, &intel_gpio_irqchip, 0,
1214 handle_bad_irq, IRQ_TYPE_NONE);
1216 dev_err(pctrl->dev, "failed to add irqchip\n");
1220 gpiochip_set_chained_irqchip(&pctrl->chip, &intel_gpio_irqchip, irq,
1225 static int intel_pinctrl_add_padgroups(struct intel_pinctrl *pctrl,
1226 struct intel_community *community)
1228 struct intel_padgroup *gpps;
1229 unsigned int npins = community->npins;
1230 unsigned int padown_num = 0;
1233 if (community->gpps)
1234 ngpps = community->ngpps;
1236 ngpps = DIV_ROUND_UP(community->npins, community->gpp_size);
1238 gpps = devm_kcalloc(pctrl->dev, ngpps, sizeof(*gpps), GFP_KERNEL);
1242 for (i = 0; i < ngpps; i++) {
1243 if (community->gpps) {
1244 gpps[i] = community->gpps[i];
1246 unsigned int gpp_size = community->gpp_size;
1248 gpps[i].reg_num = i;
1249 gpps[i].base = community->pin_base + i * gpp_size;
1250 gpps[i].size = min(gpp_size, npins);
1251 npins -= gpps[i].size;
1254 if (gpps[i].size > 32)
1257 if (!gpps[i].gpio_base)
1258 gpps[i].gpio_base = gpps[i].base;
1260 gpps[i].padown_num = padown_num;
1263 * In older hardware the number of padown registers per
1264 * group is fixed regardless of the group size.
1266 if (community->gpp_num_padown_regs)
1267 padown_num += community->gpp_num_padown_regs;
1269 padown_num += DIV_ROUND_UP(gpps[i].size * 4, 32);
1272 community->ngpps = ngpps;
1273 community->gpps = gpps;
1278 static int intel_pinctrl_pm_init(struct intel_pinctrl *pctrl)
1280 #ifdef CONFIG_PM_SLEEP
1281 const struct intel_pinctrl_soc_data *soc = pctrl->soc;
1282 struct intel_community_context *communities;
1283 struct intel_pad_context *pads;
1286 pads = devm_kcalloc(pctrl->dev, soc->npins, sizeof(*pads), GFP_KERNEL);
1290 communities = devm_kcalloc(pctrl->dev, pctrl->ncommunities,
1291 sizeof(*communities), GFP_KERNEL);
1296 for (i = 0; i < pctrl->ncommunities; i++) {
1297 struct intel_community *community = &pctrl->communities[i];
1298 u32 *intmask, *hostown;
1300 intmask = devm_kcalloc(pctrl->dev, community->ngpps,
1301 sizeof(*intmask), GFP_KERNEL);
1305 communities[i].intmask = intmask;
1307 hostown = devm_kcalloc(pctrl->dev, community->ngpps,
1308 sizeof(*hostown), GFP_KERNEL);
1312 communities[i].hostown = hostown;
1315 pctrl->context.pads = pads;
1316 pctrl->context.communities = communities;
1322 static int intel_pinctrl_probe(struct platform_device *pdev,
1323 const struct intel_pinctrl_soc_data *soc_data)
1325 struct intel_pinctrl *pctrl;
1331 pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL);
1335 pctrl->dev = &pdev->dev;
1336 pctrl->soc = soc_data;
1337 raw_spin_lock_init(&pctrl->lock);
1340 * Make a copy of the communities which we can use to hold pointers
1343 pctrl->ncommunities = pctrl->soc->ncommunities;
1344 pctrl->communities = devm_kcalloc(&pdev->dev, pctrl->ncommunities,
1345 sizeof(*pctrl->communities), GFP_KERNEL);
1346 if (!pctrl->communities)
1349 for (i = 0; i < pctrl->ncommunities; i++) {
1350 struct intel_community *community = &pctrl->communities[i];
1354 *community = pctrl->soc->communities[i];
1356 regs = devm_platform_ioremap_resource(pdev, community->barno);
1358 return PTR_ERR(regs);
1361 * Determine community features based on the revision if
1362 * not specified already.
1364 if (!community->features) {
1367 rev = (readl(regs + REVID) & REVID_MASK) >> REVID_SHIFT;
1369 community->features |= PINCTRL_FEATURE_DEBOUNCE;
1370 community->features |= PINCTRL_FEATURE_1K_PD;
1374 /* Read offset of the pad configuration registers */
1375 padbar = readl(regs + PADBAR);
1377 community->regs = regs;
1378 community->pad_regs = regs + padbar;
1380 ret = intel_pinctrl_add_padgroups(pctrl, community);
1385 irq = platform_get_irq(pdev, 0);
1389 ret = intel_pinctrl_pm_init(pctrl);
1393 pctrl->pctldesc = intel_pinctrl_desc;
1394 pctrl->pctldesc.name = dev_name(&pdev->dev);
1395 pctrl->pctldesc.pins = pctrl->soc->pins;
1396 pctrl->pctldesc.npins = pctrl->soc->npins;
1398 pctrl->pctldev = devm_pinctrl_register(&pdev->dev, &pctrl->pctldesc,
1400 if (IS_ERR(pctrl->pctldev)) {
1401 dev_err(&pdev->dev, "failed to register pinctrl driver\n");
1402 return PTR_ERR(pctrl->pctldev);
1405 ret = intel_gpio_probe(pctrl, irq);
1409 platform_set_drvdata(pdev, pctrl);
1414 int intel_pinctrl_probe_by_hid(struct platform_device *pdev)
1416 const struct intel_pinctrl_soc_data *data;
1418 data = device_get_match_data(&pdev->dev);
1419 return intel_pinctrl_probe(pdev, data);
1421 EXPORT_SYMBOL_GPL(intel_pinctrl_probe_by_hid);
1423 int intel_pinctrl_probe_by_uid(struct platform_device *pdev)
1425 const struct intel_pinctrl_soc_data *data = NULL;
1426 const struct intel_pinctrl_soc_data **table;
1427 struct acpi_device *adev;
1430 adev = ACPI_COMPANION(&pdev->dev);
1432 const void *match = device_get_match_data(&pdev->dev);
1434 table = (const struct intel_pinctrl_soc_data **)match;
1435 for (i = 0; table[i]; i++) {
1436 if (!strcmp(adev->pnp.unique_id, table[i]->uid)) {
1442 const struct platform_device_id *id;
1444 id = platform_get_device_id(pdev);
1448 table = (const struct intel_pinctrl_soc_data **)id->driver_data;
1449 data = table[pdev->id];
1452 return intel_pinctrl_probe(pdev, data);
1454 EXPORT_SYMBOL_GPL(intel_pinctrl_probe_by_uid);
1456 #ifdef CONFIG_PM_SLEEP
1457 static bool intel_pinctrl_should_save(struct intel_pinctrl *pctrl, unsigned int pin)
1459 const struct pin_desc *pd = pin_desc_get(pctrl->pctldev, pin);
1461 if (!pd || !intel_pad_usable(pctrl, pin))
1465 * Only restore the pin if it is actually in use by the kernel (or
1466 * by userspace). It is possible that some pins are used by the
1467 * BIOS during resume and those are not always locked down so leave
1470 if (pd->mux_owner || pd->gpio_owner ||
1471 gpiochip_line_is_irq(&pctrl->chip, pin))
1477 int intel_pinctrl_suspend_noirq(struct device *dev)
1479 struct intel_pinctrl *pctrl = dev_get_drvdata(dev);
1480 struct intel_community_context *communities;
1481 struct intel_pad_context *pads;
1484 pads = pctrl->context.pads;
1485 for (i = 0; i < pctrl->soc->npins; i++) {
1486 const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
1487 void __iomem *padcfg;
1490 if (!intel_pinctrl_should_save(pctrl, desc->number))
1493 val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG0));
1494 pads[i].padcfg0 = val & ~PADCFG0_GPIORXSTATE;
1495 val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG1));
1496 pads[i].padcfg1 = val;
1498 padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG2);
1500 pads[i].padcfg2 = readl(padcfg);
1503 communities = pctrl->context.communities;
1504 for (i = 0; i < pctrl->ncommunities; i++) {
1505 struct intel_community *community = &pctrl->communities[i];
1509 base = community->regs + community->ie_offset;
1510 for (gpp = 0; gpp < community->ngpps; gpp++)
1511 communities[i].intmask[gpp] = readl(base + gpp * 4);
1513 base = community->regs + community->hostown_offset;
1514 for (gpp = 0; gpp < community->ngpps; gpp++)
1515 communities[i].hostown[gpp] = readl(base + gpp * 4);
1520 EXPORT_SYMBOL_GPL(intel_pinctrl_suspend_noirq);
1522 static void intel_gpio_irq_init(struct intel_pinctrl *pctrl)
1526 for (i = 0; i < pctrl->ncommunities; i++) {
1527 const struct intel_community *community;
1531 community = &pctrl->communities[i];
1532 base = community->regs;
1534 for (gpp = 0; gpp < community->ngpps; gpp++) {
1535 /* Mask and clear all interrupts */
1536 writel(0, base + community->ie_offset + gpp * 4);
1537 writel(0xffff, base + community->is_offset + gpp * 4);
1543 intel_gpio_is_requested(struct gpio_chip *chip, int base, unsigned int size)
1548 for (i = 0; i < size; i++)
1549 if (gpiochip_is_requested(chip, base + i))
1550 requested |= BIT(i);
1556 intel_gpio_update_pad_mode(void __iomem *hostown, u32 mask, u32 value)
1560 curr = readl(hostown);
1561 updated = (curr & ~mask) | (value & mask);
1562 writel(updated, hostown);
1567 int intel_pinctrl_resume_noirq(struct device *dev)
1569 struct intel_pinctrl *pctrl = dev_get_drvdata(dev);
1570 const struct intel_community_context *communities;
1571 const struct intel_pad_context *pads;
1574 /* Mask all interrupts */
1575 intel_gpio_irq_init(pctrl);
1577 pads = pctrl->context.pads;
1578 for (i = 0; i < pctrl->soc->npins; i++) {
1579 const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
1580 void __iomem *padcfg;
1583 if (!intel_pinctrl_should_save(pctrl, desc->number))
1586 padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG0);
1587 val = readl(padcfg) & ~PADCFG0_GPIORXSTATE;
1588 if (val != pads[i].padcfg0) {
1589 writel(pads[i].padcfg0, padcfg);
1590 dev_dbg(dev, "restored pin %u padcfg0 %#08x\n",
1591 desc->number, readl(padcfg));
1594 padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG1);
1595 val = readl(padcfg);
1596 if (val != pads[i].padcfg1) {
1597 writel(pads[i].padcfg1, padcfg);
1598 dev_dbg(dev, "restored pin %u padcfg1 %#08x\n",
1599 desc->number, readl(padcfg));
1602 padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG2);
1604 val = readl(padcfg);
1605 if (val != pads[i].padcfg2) {
1606 writel(pads[i].padcfg2, padcfg);
1607 dev_dbg(dev, "restored pin %u padcfg2 %#08x\n",
1608 desc->number, readl(padcfg));
1613 communities = pctrl->context.communities;
1614 for (i = 0; i < pctrl->ncommunities; i++) {
1615 struct intel_community *community = &pctrl->communities[i];
1619 base = community->regs + community->ie_offset;
1620 for (gpp = 0; gpp < community->ngpps; gpp++) {
1621 writel(communities[i].intmask[gpp], base + gpp * 4);
1622 dev_dbg(dev, "restored mask %d/%u %#08x\n", i, gpp,
1623 readl(base + gpp * 4));
1626 base = community->regs + community->hostown_offset;
1627 for (gpp = 0; gpp < community->ngpps; gpp++) {
1628 const struct intel_padgroup *padgrp = &community->gpps[gpp];
1629 u32 requested = 0, value = 0;
1630 u32 saved = communities[i].hostown[gpp];
1632 if (padgrp->gpio_base < 0)
1635 requested = intel_gpio_is_requested(&pctrl->chip,
1636 padgrp->gpio_base, padgrp->size);
1637 value = intel_gpio_update_pad_mode(base + gpp * 4,
1639 if ((value ^ saved) & requested) {
1640 dev_warn(dev, "restore hostown %d/%u %#8x->%#8x\n",
1641 i, gpp, value, saved);
1648 EXPORT_SYMBOL_GPL(intel_pinctrl_resume_noirq);
1651 MODULE_AUTHOR("Mathias Nyman <mathias.nyman@linux.intel.com>");
1652 MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
1653 MODULE_DESCRIPTION("Intel pinctrl/GPIO core driver");
1654 MODULE_LICENSE("GPL v2");