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/module.h>
11 #include <linux/interrupt.h>
12 #include <linux/gpio/driver.h>
13 #include <linux/log2.h>
14 #include <linux/platform_device.h>
15 #include <linux/pinctrl/pinctrl.h>
16 #include <linux/pinctrl/pinmux.h>
17 #include <linux/pinctrl/pinconf.h>
18 #include <linux/pinctrl/pinconf-generic.h>
21 #include "pinctrl-intel.h"
23 /* Offset from regs */
25 #define REVID_SHIFT 16
26 #define REVID_MASK GENMASK(31, 16)
32 #define PADOWN_SHIFT(p) ((p) % 8 * PADOWN_BITS)
33 #define PADOWN_MASK(p) (0xf << PADOWN_SHIFT(p))
34 #define PADOWN_GPP(p) ((p) / 8)
36 /* Offset from pad_regs */
38 #define PADCFG0_RXEVCFG_SHIFT 25
39 #define PADCFG0_RXEVCFG_MASK (3 << PADCFG0_RXEVCFG_SHIFT)
40 #define PADCFG0_RXEVCFG_LEVEL 0
41 #define PADCFG0_RXEVCFG_EDGE 1
42 #define PADCFG0_RXEVCFG_DISABLED 2
43 #define PADCFG0_RXEVCFG_EDGE_BOTH 3
44 #define PADCFG0_PREGFRXSEL BIT(24)
45 #define PADCFG0_RXINV BIT(23)
46 #define PADCFG0_GPIROUTIOXAPIC BIT(20)
47 #define PADCFG0_GPIROUTSCI BIT(19)
48 #define PADCFG0_GPIROUTSMI BIT(18)
49 #define PADCFG0_GPIROUTNMI BIT(17)
50 #define PADCFG0_PMODE_SHIFT 10
51 #define PADCFG0_PMODE_MASK (0xf << PADCFG0_PMODE_SHIFT)
52 #define PADCFG0_PMODE_GPIO 0
53 #define PADCFG0_GPIORXDIS BIT(9)
54 #define PADCFG0_GPIOTXDIS BIT(8)
55 #define PADCFG0_GPIORXSTATE BIT(1)
56 #define PADCFG0_GPIOTXSTATE BIT(0)
59 #define PADCFG1_TERM_UP BIT(13)
60 #define PADCFG1_TERM_SHIFT 10
61 #define PADCFG1_TERM_MASK (7 << PADCFG1_TERM_SHIFT)
62 #define PADCFG1_TERM_20K 4
63 #define PADCFG1_TERM_2K 3
64 #define PADCFG1_TERM_5K 2
65 #define PADCFG1_TERM_1K 1
68 #define PADCFG2_DEBEN BIT(0)
69 #define PADCFG2_DEBOUNCE_SHIFT 1
70 #define PADCFG2_DEBOUNCE_MASK GENMASK(4, 1)
72 #define DEBOUNCE_PERIOD 31250 /* ns */
74 struct intel_pad_context {
80 struct intel_community_context {
84 struct intel_pinctrl_context {
85 struct intel_pad_context *pads;
86 struct intel_community_context *communities;
90 * struct intel_pinctrl - Intel pinctrl private structure
91 * @dev: Pointer to the device structure
92 * @lock: Lock to serialize register access
93 * @pctldesc: Pin controller description
94 * @pctldev: Pointer to the pin controller device
95 * @chip: GPIO chip in this pin controller
96 * @soc: SoC/PCH specific pin configuration data
97 * @communities: All communities in this pin controller
98 * @ncommunities: Number of communities in this pin controller
99 * @context: Configuration saved over system sleep
100 * @irq: pinctrl/GPIO chip irq number
102 struct intel_pinctrl {
105 struct pinctrl_desc pctldesc;
106 struct pinctrl_dev *pctldev;
107 struct gpio_chip chip;
108 const struct intel_pinctrl_soc_data *soc;
109 struct intel_community *communities;
111 struct intel_pinctrl_context context;
115 #define pin_to_padno(c, p) ((p) - (c)->pin_base)
116 #define padgroup_offset(g, p) ((p) - (g)->base)
118 static struct intel_community *intel_get_community(struct intel_pinctrl *pctrl,
121 struct intel_community *community;
124 for (i = 0; i < pctrl->ncommunities; i++) {
125 community = &pctrl->communities[i];
126 if (pin >= community->pin_base &&
127 pin < community->pin_base + community->npins)
131 dev_warn(pctrl->dev, "failed to find community for pin %u\n", pin);
135 static const struct intel_padgroup *
136 intel_community_get_padgroup(const struct intel_community *community,
141 for (i = 0; i < community->ngpps; i++) {
142 const struct intel_padgroup *padgrp = &community->gpps[i];
144 if (pin >= padgrp->base && pin < padgrp->base + padgrp->size)
151 static void __iomem *intel_get_padcfg(struct intel_pinctrl *pctrl, unsigned pin,
154 const struct intel_community *community;
158 community = intel_get_community(pctrl, pin);
162 padno = pin_to_padno(community, pin);
163 nregs = (community->features & PINCTRL_FEATURE_DEBOUNCE) ? 4 : 2;
165 if (reg == PADCFG2 && !(community->features & PINCTRL_FEATURE_DEBOUNCE))
168 return community->pad_regs + reg + padno * nregs * 4;
171 static bool intel_pad_owned_by_host(struct intel_pinctrl *pctrl, unsigned pin)
173 const struct intel_community *community;
174 const struct intel_padgroup *padgrp;
175 unsigned gpp, offset, gpp_offset;
176 void __iomem *padown;
178 community = intel_get_community(pctrl, pin);
181 if (!community->padown_offset)
184 padgrp = intel_community_get_padgroup(community, pin);
188 gpp_offset = padgroup_offset(padgrp, pin);
189 gpp = PADOWN_GPP(gpp_offset);
190 offset = community->padown_offset + padgrp->padown_num * 4 + gpp * 4;
191 padown = community->regs + offset;
193 return !(readl(padown) & PADOWN_MASK(gpp_offset));
196 static bool intel_pad_acpi_mode(struct intel_pinctrl *pctrl, unsigned pin)
198 const struct intel_community *community;
199 const struct intel_padgroup *padgrp;
200 unsigned offset, gpp_offset;
201 void __iomem *hostown;
203 community = intel_get_community(pctrl, pin);
206 if (!community->hostown_offset)
209 padgrp = intel_community_get_padgroup(community, pin);
213 gpp_offset = padgroup_offset(padgrp, pin);
214 offset = community->hostown_offset + padgrp->reg_num * 4;
215 hostown = community->regs + offset;
217 return !(readl(hostown) & BIT(gpp_offset));
220 static bool intel_pad_locked(struct intel_pinctrl *pctrl, unsigned pin)
222 struct intel_community *community;
223 const struct intel_padgroup *padgrp;
224 unsigned offset, gpp_offset;
227 community = intel_get_community(pctrl, pin);
230 if (!community->padcfglock_offset)
233 padgrp = intel_community_get_padgroup(community, pin);
237 gpp_offset = padgroup_offset(padgrp, pin);
240 * If PADCFGLOCK and PADCFGLOCKTX bits are both clear for this pad,
241 * the pad is considered unlocked. Any other case means that it is
242 * either fully or partially locked and we don't touch it.
244 offset = community->padcfglock_offset + padgrp->reg_num * 8;
245 value = readl(community->regs + offset);
246 if (value & BIT(gpp_offset))
249 offset = community->padcfglock_offset + 4 + padgrp->reg_num * 8;
250 value = readl(community->regs + offset);
251 if (value & BIT(gpp_offset))
257 static bool intel_pad_usable(struct intel_pinctrl *pctrl, unsigned pin)
259 return intel_pad_owned_by_host(pctrl, pin) &&
260 !intel_pad_locked(pctrl, pin);
263 static int intel_get_groups_count(struct pinctrl_dev *pctldev)
265 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
267 return pctrl->soc->ngroups;
270 static const char *intel_get_group_name(struct pinctrl_dev *pctldev,
273 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
275 return pctrl->soc->groups[group].name;
278 static int intel_get_group_pins(struct pinctrl_dev *pctldev, unsigned group,
279 const unsigned **pins, unsigned *npins)
281 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
283 *pins = pctrl->soc->groups[group].pins;
284 *npins = pctrl->soc->groups[group].npins;
288 static void intel_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
291 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
292 void __iomem *padcfg;
293 u32 cfg0, cfg1, mode;
296 if (!intel_pad_owned_by_host(pctrl, pin)) {
297 seq_puts(s, "not available");
301 cfg0 = readl(intel_get_padcfg(pctrl, pin, PADCFG0));
302 cfg1 = readl(intel_get_padcfg(pctrl, pin, PADCFG1));
304 mode = (cfg0 & PADCFG0_PMODE_MASK) >> PADCFG0_PMODE_SHIFT;
305 if (mode == PADCFG0_PMODE_GPIO)
306 seq_puts(s, "GPIO ");
308 seq_printf(s, "mode %d ", mode);
310 seq_printf(s, "0x%08x 0x%08x", cfg0, cfg1);
312 /* Dump the additional PADCFG registers if available */
313 padcfg = intel_get_padcfg(pctrl, pin, PADCFG2);
315 seq_printf(s, " 0x%08x", readl(padcfg));
317 locked = intel_pad_locked(pctrl, pin);
318 acpi = intel_pad_acpi_mode(pctrl, pin);
320 if (locked || acpi) {
323 seq_puts(s, "LOCKED");
333 static const struct pinctrl_ops intel_pinctrl_ops = {
334 .get_groups_count = intel_get_groups_count,
335 .get_group_name = intel_get_group_name,
336 .get_group_pins = intel_get_group_pins,
337 .pin_dbg_show = intel_pin_dbg_show,
340 static int intel_get_functions_count(struct pinctrl_dev *pctldev)
342 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
344 return pctrl->soc->nfunctions;
347 static const char *intel_get_function_name(struct pinctrl_dev *pctldev,
350 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
352 return pctrl->soc->functions[function].name;
355 static int intel_get_function_groups(struct pinctrl_dev *pctldev,
357 const char * const **groups,
358 unsigned * const ngroups)
360 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
362 *groups = pctrl->soc->functions[function].groups;
363 *ngroups = pctrl->soc->functions[function].ngroups;
367 static int intel_pinmux_set_mux(struct pinctrl_dev *pctldev, unsigned function,
370 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
371 const struct intel_pingroup *grp = &pctrl->soc->groups[group];
375 raw_spin_lock_irqsave(&pctrl->lock, flags);
378 * All pins in the groups needs to be accessible and writable
379 * before we can enable the mux for this group.
381 for (i = 0; i < grp->npins; i++) {
382 if (!intel_pad_usable(pctrl, grp->pins[i])) {
383 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
388 /* Now enable the mux setting for each pin in the group */
389 for (i = 0; i < grp->npins; i++) {
390 void __iomem *padcfg0;
393 padcfg0 = intel_get_padcfg(pctrl, grp->pins[i], PADCFG0);
394 value = readl(padcfg0);
396 value &= ~PADCFG0_PMODE_MASK;
399 value |= grp->modes[i] << PADCFG0_PMODE_SHIFT;
401 value |= grp->mode << PADCFG0_PMODE_SHIFT;
403 writel(value, padcfg0);
406 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
411 static void __intel_gpio_set_direction(void __iomem *padcfg0, bool input)
415 value = readl(padcfg0);
417 value &= ~PADCFG0_GPIORXDIS;
418 value |= PADCFG0_GPIOTXDIS;
420 value &= ~PADCFG0_GPIOTXDIS;
421 value |= PADCFG0_GPIORXDIS;
423 writel(value, padcfg0);
426 static int intel_gpio_get_gpio_mode(void __iomem *padcfg0)
428 return (readl(padcfg0) & PADCFG0_PMODE_MASK) >> PADCFG0_PMODE_SHIFT;
431 static void intel_gpio_set_gpio_mode(void __iomem *padcfg0)
435 /* Put the pad into GPIO mode */
436 value = readl(padcfg0) & ~PADCFG0_PMODE_MASK;
437 /* Disable SCI/SMI/NMI generation */
438 value &= ~(PADCFG0_GPIROUTIOXAPIC | PADCFG0_GPIROUTSCI);
439 value &= ~(PADCFG0_GPIROUTSMI | PADCFG0_GPIROUTNMI);
440 writel(value, padcfg0);
443 static int intel_gpio_request_enable(struct pinctrl_dev *pctldev,
444 struct pinctrl_gpio_range *range,
447 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
448 void __iomem *padcfg0;
451 raw_spin_lock_irqsave(&pctrl->lock, flags);
453 if (!intel_pad_usable(pctrl, pin)) {
454 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
458 padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
461 * If pin is already configured in GPIO mode, we assume that
462 * firmware provides correct settings. In such case we avoid
463 * potential glitches on the pin. Otherwise, for the pin in
464 * alternative mode, consumer has to supply respective flags.
466 if (intel_gpio_get_gpio_mode(padcfg0) == PADCFG0_PMODE_GPIO) {
467 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
471 intel_gpio_set_gpio_mode(padcfg0);
473 /* Disable TX buffer and enable RX (this will be input) */
474 __intel_gpio_set_direction(padcfg0, true);
476 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
481 static int intel_gpio_set_direction(struct pinctrl_dev *pctldev,
482 struct pinctrl_gpio_range *range,
483 unsigned pin, bool input)
485 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
486 void __iomem *padcfg0;
489 raw_spin_lock_irqsave(&pctrl->lock, flags);
491 padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
492 __intel_gpio_set_direction(padcfg0, input);
494 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
499 static const struct pinmux_ops intel_pinmux_ops = {
500 .get_functions_count = intel_get_functions_count,
501 .get_function_name = intel_get_function_name,
502 .get_function_groups = intel_get_function_groups,
503 .set_mux = intel_pinmux_set_mux,
504 .gpio_request_enable = intel_gpio_request_enable,
505 .gpio_set_direction = intel_gpio_set_direction,
508 static int intel_config_get(struct pinctrl_dev *pctldev, unsigned pin,
509 unsigned long *config)
511 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
512 enum pin_config_param param = pinconf_to_config_param(*config);
513 const struct intel_community *community;
517 if (!intel_pad_owned_by_host(pctrl, pin))
520 community = intel_get_community(pctrl, pin);
521 value = readl(intel_get_padcfg(pctrl, pin, PADCFG1));
522 term = (value & PADCFG1_TERM_MASK) >> PADCFG1_TERM_SHIFT;
525 case PIN_CONFIG_BIAS_DISABLE:
530 case PIN_CONFIG_BIAS_PULL_UP:
531 if (!term || !(value & PADCFG1_TERM_UP))
535 case PADCFG1_TERM_1K:
538 case PADCFG1_TERM_2K:
541 case PADCFG1_TERM_5K:
544 case PADCFG1_TERM_20K:
551 case PIN_CONFIG_BIAS_PULL_DOWN:
552 if (!term || value & PADCFG1_TERM_UP)
556 case PADCFG1_TERM_1K:
557 if (!(community->features & PINCTRL_FEATURE_1K_PD))
561 case PADCFG1_TERM_5K:
564 case PADCFG1_TERM_20K:
571 case PIN_CONFIG_INPUT_DEBOUNCE: {
572 void __iomem *padcfg2;
575 padcfg2 = intel_get_padcfg(pctrl, pin, PADCFG2);
580 if (!(v & PADCFG2_DEBEN))
583 v = (v & PADCFG2_DEBOUNCE_MASK) >> PADCFG2_DEBOUNCE_SHIFT;
584 arg = BIT(v) * DEBOUNCE_PERIOD / 1000;
593 *config = pinconf_to_config_packed(param, arg);
597 static int intel_config_set_pull(struct intel_pinctrl *pctrl, unsigned pin,
598 unsigned long config)
600 unsigned param = pinconf_to_config_param(config);
601 unsigned arg = pinconf_to_config_argument(config);
602 const struct intel_community *community;
603 void __iomem *padcfg1;
608 raw_spin_lock_irqsave(&pctrl->lock, flags);
610 community = intel_get_community(pctrl, pin);
611 padcfg1 = intel_get_padcfg(pctrl, pin, PADCFG1);
612 value = readl(padcfg1);
615 case PIN_CONFIG_BIAS_DISABLE:
616 value &= ~(PADCFG1_TERM_MASK | PADCFG1_TERM_UP);
619 case PIN_CONFIG_BIAS_PULL_UP:
620 value &= ~PADCFG1_TERM_MASK;
622 value |= PADCFG1_TERM_UP;
624 /* Set default strength value in case none is given */
630 value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT;
633 value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT;
636 value |= PADCFG1_TERM_2K << PADCFG1_TERM_SHIFT;
639 value |= PADCFG1_TERM_1K << PADCFG1_TERM_SHIFT;
647 case PIN_CONFIG_BIAS_PULL_DOWN:
648 value &= ~(PADCFG1_TERM_UP | PADCFG1_TERM_MASK);
650 /* Set default strength value in case none is given */
656 value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT;
659 value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT;
662 if (!(community->features & PINCTRL_FEATURE_1K_PD)) {
666 value |= PADCFG1_TERM_1K << PADCFG1_TERM_SHIFT;
676 writel(value, padcfg1);
678 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
683 static int intel_config_set_debounce(struct intel_pinctrl *pctrl, unsigned pin,
686 void __iomem *padcfg0, *padcfg2;
691 padcfg2 = intel_get_padcfg(pctrl, pin, PADCFG2);
695 padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
697 raw_spin_lock_irqsave(&pctrl->lock, flags);
699 value0 = readl(padcfg0);
700 value2 = readl(padcfg2);
702 /* Disable glitch filter and debouncer */
703 value0 &= ~PADCFG0_PREGFRXSEL;
704 value2 &= ~(PADCFG2_DEBEN | PADCFG2_DEBOUNCE_MASK);
709 v = order_base_2(debounce * 1000 / DEBOUNCE_PERIOD);
710 if (v < 3 || v > 15) {
714 /* Enable glitch filter and debouncer */
715 value0 |= PADCFG0_PREGFRXSEL;
716 value2 |= v << PADCFG2_DEBOUNCE_SHIFT;
717 value2 |= PADCFG2_DEBEN;
721 writel(value0, padcfg0);
722 writel(value2, padcfg2);
725 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
730 static int intel_config_set(struct pinctrl_dev *pctldev, unsigned pin,
731 unsigned long *configs, unsigned nconfigs)
733 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
736 if (!intel_pad_usable(pctrl, pin))
739 for (i = 0; i < nconfigs; i++) {
740 switch (pinconf_to_config_param(configs[i])) {
741 case PIN_CONFIG_BIAS_DISABLE:
742 case PIN_CONFIG_BIAS_PULL_UP:
743 case PIN_CONFIG_BIAS_PULL_DOWN:
744 ret = intel_config_set_pull(pctrl, pin, configs[i]);
749 case PIN_CONFIG_INPUT_DEBOUNCE:
750 ret = intel_config_set_debounce(pctrl, pin,
751 pinconf_to_config_argument(configs[i]));
764 static const struct pinconf_ops intel_pinconf_ops = {
766 .pin_config_get = intel_config_get,
767 .pin_config_set = intel_config_set,
770 static const struct pinctrl_desc intel_pinctrl_desc = {
771 .pctlops = &intel_pinctrl_ops,
772 .pmxops = &intel_pinmux_ops,
773 .confops = &intel_pinconf_ops,
774 .owner = THIS_MODULE,
778 * intel_gpio_to_pin() - Translate from GPIO offset to pin number
779 * @pctrl: Pinctrl structure
780 * @offset: GPIO offset from gpiolib
781 * @commmunity: Community is filled here if not %NULL
782 * @padgrp: Pad group is filled here if not %NULL
784 * When coming through gpiolib irqchip, the GPIO offset is not
785 * automatically translated to pinctrl pin number. This function can be
786 * used to find out the corresponding pinctrl pin.
788 static int intel_gpio_to_pin(struct intel_pinctrl *pctrl, unsigned offset,
789 const struct intel_community **community,
790 const struct intel_padgroup **padgrp)
794 for (i = 0; i < pctrl->ncommunities; i++) {
795 const struct intel_community *comm = &pctrl->communities[i];
798 for (j = 0; j < comm->ngpps; j++) {
799 const struct intel_padgroup *pgrp = &comm->gpps[j];
801 if (pgrp->gpio_base < 0)
804 if (offset >= pgrp->gpio_base &&
805 offset < pgrp->gpio_base + pgrp->size) {
808 pin = pgrp->base + offset - pgrp->gpio_base;
822 static int intel_gpio_get(struct gpio_chip *chip, unsigned offset)
824 struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
829 pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL);
833 reg = intel_get_padcfg(pctrl, pin, PADCFG0);
837 padcfg0 = readl(reg);
838 if (!(padcfg0 & PADCFG0_GPIOTXDIS))
839 return !!(padcfg0 & PADCFG0_GPIOTXSTATE);
841 return !!(padcfg0 & PADCFG0_GPIORXSTATE);
844 static void intel_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
846 struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
852 pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL);
856 reg = intel_get_padcfg(pctrl, pin, PADCFG0);
860 raw_spin_lock_irqsave(&pctrl->lock, flags);
861 padcfg0 = readl(reg);
863 padcfg0 |= PADCFG0_GPIOTXSTATE;
865 padcfg0 &= ~PADCFG0_GPIOTXSTATE;
866 writel(padcfg0, reg);
867 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
870 static int intel_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
872 struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
877 pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL);
881 reg = intel_get_padcfg(pctrl, pin, PADCFG0);
885 padcfg0 = readl(reg);
887 if (padcfg0 & PADCFG0_PMODE_MASK)
890 return !!(padcfg0 & PADCFG0_GPIOTXDIS);
893 static int intel_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
895 return pinctrl_gpio_direction_input(chip->base + offset);
898 static int intel_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
901 intel_gpio_set(chip, offset, value);
902 return pinctrl_gpio_direction_output(chip->base + offset);
905 static const struct gpio_chip intel_gpio_chip = {
906 .owner = THIS_MODULE,
907 .request = gpiochip_generic_request,
908 .free = gpiochip_generic_free,
909 .get_direction = intel_gpio_get_direction,
910 .direction_input = intel_gpio_direction_input,
911 .direction_output = intel_gpio_direction_output,
912 .get = intel_gpio_get,
913 .set = intel_gpio_set,
914 .set_config = gpiochip_generic_config,
917 static void intel_gpio_irq_ack(struct irq_data *d)
919 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
920 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
921 const struct intel_community *community;
922 const struct intel_padgroup *padgrp;
925 pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), &community, &padgrp);
927 unsigned gpp, gpp_offset, is_offset;
929 gpp = padgrp->reg_num;
930 gpp_offset = padgroup_offset(padgrp, pin);
931 is_offset = community->is_offset + gpp * 4;
933 raw_spin_lock(&pctrl->lock);
934 writel(BIT(gpp_offset), community->regs + is_offset);
935 raw_spin_unlock(&pctrl->lock);
939 static void intel_gpio_irq_enable(struct irq_data *d)
941 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
942 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
943 const struct intel_community *community;
944 const struct intel_padgroup *padgrp;
947 pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), &community, &padgrp);
949 unsigned gpp, gpp_offset, is_offset;
953 gpp = padgrp->reg_num;
954 gpp_offset = padgroup_offset(padgrp, pin);
955 is_offset = community->is_offset + gpp * 4;
957 raw_spin_lock_irqsave(&pctrl->lock, flags);
958 /* Clear interrupt status first to avoid unexpected interrupt */
959 writel(BIT(gpp_offset), community->regs + is_offset);
961 value = readl(community->regs + community->ie_offset + gpp * 4);
962 value |= BIT(gpp_offset);
963 writel(value, community->regs + community->ie_offset + gpp * 4);
964 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
968 static void intel_gpio_irq_mask_unmask(struct irq_data *d, bool mask)
970 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
971 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
972 const struct intel_community *community;
973 const struct intel_padgroup *padgrp;
976 pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), &community, &padgrp);
978 unsigned gpp, gpp_offset;
983 gpp = padgrp->reg_num;
984 gpp_offset = padgroup_offset(padgrp, pin);
986 reg = community->regs + community->ie_offset + gpp * 4;
988 raw_spin_lock_irqsave(&pctrl->lock, flags);
991 value &= ~BIT(gpp_offset);
993 value |= BIT(gpp_offset);
995 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
999 static void intel_gpio_irq_mask(struct irq_data *d)
1001 intel_gpio_irq_mask_unmask(d, true);
1004 static void intel_gpio_irq_unmask(struct irq_data *d)
1006 intel_gpio_irq_mask_unmask(d, false);
1009 static int intel_gpio_irq_type(struct irq_data *d, unsigned type)
1011 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1012 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
1013 unsigned pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), NULL, NULL);
1014 unsigned long flags;
1018 reg = intel_get_padcfg(pctrl, pin, PADCFG0);
1023 * If the pin is in ACPI mode it is still usable as a GPIO but it
1024 * cannot be used as IRQ because GPI_IS status bit will not be
1025 * updated by the host controller hardware.
1027 if (intel_pad_acpi_mode(pctrl, pin)) {
1028 dev_warn(pctrl->dev, "pin %u cannot be used as IRQ\n", pin);
1032 raw_spin_lock_irqsave(&pctrl->lock, flags);
1034 intel_gpio_set_gpio_mode(reg);
1038 value &= ~(PADCFG0_RXEVCFG_MASK | PADCFG0_RXINV);
1040 if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
1041 value |= PADCFG0_RXEVCFG_EDGE_BOTH << PADCFG0_RXEVCFG_SHIFT;
1042 } else if (type & IRQ_TYPE_EDGE_FALLING) {
1043 value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT;
1044 value |= PADCFG0_RXINV;
1045 } else if (type & IRQ_TYPE_EDGE_RISING) {
1046 value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT;
1047 } else if (type & IRQ_TYPE_LEVEL_MASK) {
1048 if (type & IRQ_TYPE_LEVEL_LOW)
1049 value |= PADCFG0_RXINV;
1051 value |= PADCFG0_RXEVCFG_DISABLED << PADCFG0_RXEVCFG_SHIFT;
1056 if (type & IRQ_TYPE_EDGE_BOTH)
1057 irq_set_handler_locked(d, handle_edge_irq);
1058 else if (type & IRQ_TYPE_LEVEL_MASK)
1059 irq_set_handler_locked(d, handle_level_irq);
1061 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
1066 static int intel_gpio_irq_wake(struct irq_data *d, unsigned int on)
1068 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1069 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
1070 unsigned pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), NULL, NULL);
1073 enable_irq_wake(pctrl->irq);
1075 disable_irq_wake(pctrl->irq);
1077 dev_dbg(pctrl->dev, "%sable wake for pin %u\n", on ? "en" : "dis", pin);
1081 static irqreturn_t intel_gpio_community_irq_handler(struct intel_pinctrl *pctrl,
1082 const struct intel_community *community)
1084 struct gpio_chip *gc = &pctrl->chip;
1085 irqreturn_t ret = IRQ_NONE;
1088 for (gpp = 0; gpp < community->ngpps; gpp++) {
1089 const struct intel_padgroup *padgrp = &community->gpps[gpp];
1090 unsigned long pending, enabled, gpp_offset;
1092 pending = readl(community->regs + community->is_offset +
1093 padgrp->reg_num * 4);
1094 enabled = readl(community->regs + community->ie_offset +
1095 padgrp->reg_num * 4);
1097 /* Only interrupts that are enabled */
1100 for_each_set_bit(gpp_offset, &pending, padgrp->size) {
1103 irq = irq_find_mapping(gc->irq.domain,
1104 padgrp->gpio_base + gpp_offset);
1105 generic_handle_irq(irq);
1114 static irqreturn_t intel_gpio_irq(int irq, void *data)
1116 const struct intel_community *community;
1117 struct intel_pinctrl *pctrl = data;
1118 irqreturn_t ret = IRQ_NONE;
1121 /* Need to check all communities for pending interrupts */
1122 for (i = 0; i < pctrl->ncommunities; i++) {
1123 community = &pctrl->communities[i];
1124 ret |= intel_gpio_community_irq_handler(pctrl, community);
1130 static struct irq_chip intel_gpio_irqchip = {
1131 .name = "intel-gpio",
1132 .irq_enable = intel_gpio_irq_enable,
1133 .irq_ack = intel_gpio_irq_ack,
1134 .irq_mask = intel_gpio_irq_mask,
1135 .irq_unmask = intel_gpio_irq_unmask,
1136 .irq_set_type = intel_gpio_irq_type,
1137 .irq_set_wake = intel_gpio_irq_wake,
1138 .flags = IRQCHIP_MASK_ON_SUSPEND,
1141 static int intel_gpio_add_pin_ranges(struct intel_pinctrl *pctrl,
1142 const struct intel_community *community)
1146 for (i = 0; i < community->ngpps; i++) {
1147 const struct intel_padgroup *gpp = &community->gpps[i];
1149 if (gpp->gpio_base < 0)
1152 ret = gpiochip_add_pin_range(&pctrl->chip, dev_name(pctrl->dev),
1153 gpp->gpio_base, gpp->base,
1162 static unsigned intel_gpio_ngpio(const struct intel_pinctrl *pctrl)
1164 const struct intel_community *community;
1168 for (i = 0; i < pctrl->ncommunities; i++) {
1169 community = &pctrl->communities[i];
1170 for (j = 0; j < community->ngpps; j++) {
1171 const struct intel_padgroup *gpp = &community->gpps[j];
1173 if (gpp->gpio_base < 0)
1176 if (gpp->gpio_base + gpp->size > ngpio)
1177 ngpio = gpp->gpio_base + gpp->size;
1184 static int intel_gpio_probe(struct intel_pinctrl *pctrl, int irq)
1188 pctrl->chip = intel_gpio_chip;
1190 pctrl->chip.ngpio = intel_gpio_ngpio(pctrl);
1191 pctrl->chip.label = dev_name(pctrl->dev);
1192 pctrl->chip.parent = pctrl->dev;
1193 pctrl->chip.base = -1;
1196 ret = devm_gpiochip_add_data(pctrl->dev, &pctrl->chip, pctrl);
1198 dev_err(pctrl->dev, "failed to register gpiochip\n");
1202 for (i = 0; i < pctrl->ncommunities; i++) {
1203 struct intel_community *community = &pctrl->communities[i];
1205 ret = intel_gpio_add_pin_ranges(pctrl, community);
1207 dev_err(pctrl->dev, "failed to add GPIO pin range\n");
1213 * We need to request the interrupt here (instead of providing chip
1214 * to the irq directly) because on some platforms several GPIO
1215 * controllers share the same interrupt line.
1217 ret = devm_request_irq(pctrl->dev, irq, intel_gpio_irq,
1218 IRQF_SHARED | IRQF_NO_THREAD,
1219 dev_name(pctrl->dev), pctrl);
1221 dev_err(pctrl->dev, "failed to request interrupt\n");
1225 ret = gpiochip_irqchip_add(&pctrl->chip, &intel_gpio_irqchip, 0,
1226 handle_bad_irq, IRQ_TYPE_NONE);
1228 dev_err(pctrl->dev, "failed to add irqchip\n");
1232 gpiochip_set_chained_irqchip(&pctrl->chip, &intel_gpio_irqchip, irq,
1237 static int intel_pinctrl_add_padgroups(struct intel_pinctrl *pctrl,
1238 struct intel_community *community)
1240 struct intel_padgroup *gpps;
1241 unsigned npins = community->npins;
1242 unsigned padown_num = 0;
1245 if (community->gpps)
1246 ngpps = community->ngpps;
1248 ngpps = DIV_ROUND_UP(community->npins, community->gpp_size);
1250 gpps = devm_kcalloc(pctrl->dev, ngpps, sizeof(*gpps), GFP_KERNEL);
1254 for (i = 0; i < ngpps; i++) {
1255 if (community->gpps) {
1256 gpps[i] = community->gpps[i];
1258 unsigned gpp_size = community->gpp_size;
1260 gpps[i].reg_num = i;
1261 gpps[i].base = community->pin_base + i * gpp_size;
1262 gpps[i].size = min(gpp_size, npins);
1263 npins -= gpps[i].size;
1266 if (gpps[i].size > 32)
1269 if (!gpps[i].gpio_base)
1270 gpps[i].gpio_base = gpps[i].base;
1272 gpps[i].padown_num = padown_num;
1275 * In older hardware the number of padown registers per
1276 * group is fixed regardless of the group size.
1278 if (community->gpp_num_padown_regs)
1279 padown_num += community->gpp_num_padown_regs;
1281 padown_num += DIV_ROUND_UP(gpps[i].size * 4, 32);
1284 community->ngpps = ngpps;
1285 community->gpps = gpps;
1290 static int intel_pinctrl_pm_init(struct intel_pinctrl *pctrl)
1292 #ifdef CONFIG_PM_SLEEP
1293 const struct intel_pinctrl_soc_data *soc = pctrl->soc;
1294 struct intel_community_context *communities;
1295 struct intel_pad_context *pads;
1298 pads = devm_kcalloc(pctrl->dev, soc->npins, sizeof(*pads), GFP_KERNEL);
1302 communities = devm_kcalloc(pctrl->dev, pctrl->ncommunities,
1303 sizeof(*communities), GFP_KERNEL);
1308 for (i = 0; i < pctrl->ncommunities; i++) {
1309 struct intel_community *community = &pctrl->communities[i];
1312 intmask = devm_kcalloc(pctrl->dev, community->ngpps,
1313 sizeof(*intmask), GFP_KERNEL);
1317 communities[i].intmask = intmask;
1320 pctrl->context.pads = pads;
1321 pctrl->context.communities = communities;
1327 int intel_pinctrl_probe(struct platform_device *pdev,
1328 const struct intel_pinctrl_soc_data *soc_data)
1330 struct intel_pinctrl *pctrl;
1336 pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL);
1340 pctrl->dev = &pdev->dev;
1341 pctrl->soc = soc_data;
1342 raw_spin_lock_init(&pctrl->lock);
1345 * Make a copy of the communities which we can use to hold pointers
1348 pctrl->ncommunities = pctrl->soc->ncommunities;
1349 pctrl->communities = devm_kcalloc(&pdev->dev, pctrl->ncommunities,
1350 sizeof(*pctrl->communities), GFP_KERNEL);
1351 if (!pctrl->communities)
1354 for (i = 0; i < pctrl->ncommunities; i++) {
1355 struct intel_community *community = &pctrl->communities[i];
1356 struct resource *res;
1360 *community = pctrl->soc->communities[i];
1362 res = platform_get_resource(pdev, IORESOURCE_MEM,
1364 regs = devm_ioremap_resource(&pdev->dev, res);
1366 return PTR_ERR(regs);
1369 * Determine community features based on the revision if
1370 * not specified already.
1372 if (!community->features) {
1375 rev = (readl(regs + REVID) & REVID_MASK) >> REVID_SHIFT;
1377 community->features |= PINCTRL_FEATURE_DEBOUNCE;
1378 community->features |= PINCTRL_FEATURE_1K_PD;
1382 /* Read offset of the pad configuration registers */
1383 padbar = readl(regs + PADBAR);
1385 community->regs = regs;
1386 community->pad_regs = regs + padbar;
1388 if (!community->is_offset)
1389 community->is_offset = GPI_IS;
1391 ret = intel_pinctrl_add_padgroups(pctrl, community);
1396 irq = platform_get_irq(pdev, 0);
1398 dev_err(&pdev->dev, "failed to get interrupt number\n");
1402 ret = intel_pinctrl_pm_init(pctrl);
1406 pctrl->pctldesc = intel_pinctrl_desc;
1407 pctrl->pctldesc.name = dev_name(&pdev->dev);
1408 pctrl->pctldesc.pins = pctrl->soc->pins;
1409 pctrl->pctldesc.npins = pctrl->soc->npins;
1411 pctrl->pctldev = devm_pinctrl_register(&pdev->dev, &pctrl->pctldesc,
1413 if (IS_ERR(pctrl->pctldev)) {
1414 dev_err(&pdev->dev, "failed to register pinctrl driver\n");
1415 return PTR_ERR(pctrl->pctldev);
1418 ret = intel_gpio_probe(pctrl, irq);
1422 platform_set_drvdata(pdev, pctrl);
1426 EXPORT_SYMBOL_GPL(intel_pinctrl_probe);
1428 #ifdef CONFIG_PM_SLEEP
1429 static bool intel_pinctrl_should_save(struct intel_pinctrl *pctrl, unsigned pin)
1431 const struct pin_desc *pd = pin_desc_get(pctrl->pctldev, pin);
1433 if (!pd || !intel_pad_usable(pctrl, pin))
1437 * Only restore the pin if it is actually in use by the kernel (or
1438 * by userspace). It is possible that some pins are used by the
1439 * BIOS during resume and those are not always locked down so leave
1442 if (pd->mux_owner || pd->gpio_owner ||
1443 gpiochip_line_is_irq(&pctrl->chip, pin))
1449 int intel_pinctrl_suspend(struct device *dev)
1451 struct platform_device *pdev = to_platform_device(dev);
1452 struct intel_pinctrl *pctrl = platform_get_drvdata(pdev);
1453 struct intel_community_context *communities;
1454 struct intel_pad_context *pads;
1457 pads = pctrl->context.pads;
1458 for (i = 0; i < pctrl->soc->npins; i++) {
1459 const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
1460 void __iomem *padcfg;
1463 if (!intel_pinctrl_should_save(pctrl, desc->number))
1466 val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG0));
1467 pads[i].padcfg0 = val & ~PADCFG0_GPIORXSTATE;
1468 val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG1));
1469 pads[i].padcfg1 = val;
1471 padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG2);
1473 pads[i].padcfg2 = readl(padcfg);
1476 communities = pctrl->context.communities;
1477 for (i = 0; i < pctrl->ncommunities; i++) {
1478 struct intel_community *community = &pctrl->communities[i];
1482 base = community->regs + community->ie_offset;
1483 for (gpp = 0; gpp < community->ngpps; gpp++)
1484 communities[i].intmask[gpp] = readl(base + gpp * 4);
1489 EXPORT_SYMBOL_GPL(intel_pinctrl_suspend);
1491 static void intel_gpio_irq_init(struct intel_pinctrl *pctrl)
1495 for (i = 0; i < pctrl->ncommunities; i++) {
1496 const struct intel_community *community;
1500 community = &pctrl->communities[i];
1501 base = community->regs;
1503 for (gpp = 0; gpp < community->ngpps; gpp++) {
1504 /* Mask and clear all interrupts */
1505 writel(0, base + community->ie_offset + gpp * 4);
1506 writel(0xffff, base + community->is_offset + gpp * 4);
1511 int intel_pinctrl_resume(struct device *dev)
1513 struct platform_device *pdev = to_platform_device(dev);
1514 struct intel_pinctrl *pctrl = platform_get_drvdata(pdev);
1515 const struct intel_community_context *communities;
1516 const struct intel_pad_context *pads;
1519 /* Mask all interrupts */
1520 intel_gpio_irq_init(pctrl);
1522 pads = pctrl->context.pads;
1523 for (i = 0; i < pctrl->soc->npins; i++) {
1524 const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
1525 void __iomem *padcfg;
1528 if (!intel_pinctrl_should_save(pctrl, desc->number))
1531 padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG0);
1532 val = readl(padcfg) & ~PADCFG0_GPIORXSTATE;
1533 if (val != pads[i].padcfg0) {
1534 writel(pads[i].padcfg0, padcfg);
1535 dev_dbg(dev, "restored pin %u padcfg0 %#08x\n",
1536 desc->number, readl(padcfg));
1539 padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG1);
1540 val = readl(padcfg);
1541 if (val != pads[i].padcfg1) {
1542 writel(pads[i].padcfg1, padcfg);
1543 dev_dbg(dev, "restored pin %u padcfg1 %#08x\n",
1544 desc->number, readl(padcfg));
1547 padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG2);
1549 val = readl(padcfg);
1550 if (val != pads[i].padcfg2) {
1551 writel(pads[i].padcfg2, padcfg);
1552 dev_dbg(dev, "restored pin %u padcfg2 %#08x\n",
1553 desc->number, readl(padcfg));
1558 communities = pctrl->context.communities;
1559 for (i = 0; i < pctrl->ncommunities; i++) {
1560 struct intel_community *community = &pctrl->communities[i];
1564 base = community->regs + community->ie_offset;
1565 for (gpp = 0; gpp < community->ngpps; gpp++) {
1566 writel(communities[i].intmask[gpp], base + gpp * 4);
1567 dev_dbg(dev, "restored mask %d/%u %#08x\n", i, gpp,
1568 readl(base + gpp * 4));
1574 EXPORT_SYMBOL_GPL(intel_pinctrl_resume);
1577 MODULE_AUTHOR("Mathias Nyman <mathias.nyman@linux.intel.com>");
1578 MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
1579 MODULE_DESCRIPTION("Intel pinctrl/GPIO core driver");
1580 MODULE_LICENSE("GPL v2");