1 // SPDX-License-Identifier: GPL-2.0
3 * Intel pinctrl/GPIO core driver.
5 * Copyright (C) 2015, Intel Corporation
10 #include <linux/acpi.h>
11 #include <linux/module.h>
12 #include <linux/interrupt.h>
13 #include <linux/gpio/driver.h>
14 #include <linux/log2.h>
15 #include <linux/platform_device.h>
16 #include <linux/property.h>
18 #include <linux/pinctrl/pinctrl.h>
19 #include <linux/pinctrl/pinmux.h>
20 #include <linux/pinctrl/pinconf.h>
21 #include <linux/pinctrl/pinconf-generic.h>
24 #include "pinctrl-intel.h"
26 /* Offset from regs */
28 #define REVID_SHIFT 16
29 #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 31250 /* ns */
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 == PADCFG2 && !(community->features & PINCTRL_FEATURE_DEBOUNCE))
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));
223 static bool intel_pad_locked(struct intel_pinctrl *pctrl, unsigned int pin)
225 struct intel_community *community;
226 const struct intel_padgroup *padgrp;
227 unsigned int offset, gpp_offset;
230 community = intel_get_community(pctrl, pin);
233 if (!community->padcfglock_offset)
236 padgrp = intel_community_get_padgroup(community, pin);
240 gpp_offset = padgroup_offset(padgrp, pin);
243 * If PADCFGLOCK and PADCFGLOCKTX bits are both clear for this pad,
244 * the pad is considered unlocked. Any other case means that it is
245 * either fully or partially locked and we don't touch it.
247 offset = community->padcfglock_offset + padgrp->reg_num * 8;
248 value = readl(community->regs + offset);
249 if (value & BIT(gpp_offset))
252 offset = community->padcfglock_offset + 4 + padgrp->reg_num * 8;
253 value = readl(community->regs + offset);
254 if (value & BIT(gpp_offset))
260 static bool intel_pad_usable(struct intel_pinctrl *pctrl, unsigned int pin)
262 return intel_pad_owned_by_host(pctrl, pin) &&
263 !intel_pad_locked(pctrl, pin);
266 static int intel_get_groups_count(struct pinctrl_dev *pctldev)
268 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
270 return pctrl->soc->ngroups;
273 static const char *intel_get_group_name(struct pinctrl_dev *pctldev,
276 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
278 return pctrl->soc->groups[group].name;
281 static int intel_get_group_pins(struct pinctrl_dev *pctldev, unsigned int group,
282 const unsigned int **pins, unsigned int *npins)
284 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
286 *pins = pctrl->soc->groups[group].pins;
287 *npins = pctrl->soc->groups[group].npins;
291 static void intel_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
294 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
295 void __iomem *padcfg;
296 u32 cfg0, cfg1, mode;
299 if (!intel_pad_owned_by_host(pctrl, pin)) {
300 seq_puts(s, "not available");
304 cfg0 = readl(intel_get_padcfg(pctrl, pin, PADCFG0));
305 cfg1 = readl(intel_get_padcfg(pctrl, pin, PADCFG1));
307 mode = (cfg0 & PADCFG0_PMODE_MASK) >> PADCFG0_PMODE_SHIFT;
309 seq_puts(s, "GPIO ");
311 seq_printf(s, "mode %d ", mode);
313 seq_printf(s, "0x%08x 0x%08x", cfg0, cfg1);
315 /* Dump the additional PADCFG registers if available */
316 padcfg = intel_get_padcfg(pctrl, pin, PADCFG2);
318 seq_printf(s, " 0x%08x", readl(padcfg));
320 locked = intel_pad_locked(pctrl, pin);
321 acpi = intel_pad_acpi_mode(pctrl, pin);
323 if (locked || acpi) {
326 seq_puts(s, "LOCKED");
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 void intel_gpio_set_gpio_mode(void __iomem *padcfg0)
433 /* Put the pad into GPIO mode */
434 value = readl(padcfg0) & ~PADCFG0_PMODE_MASK;
435 /* Disable SCI/SMI/NMI generation */
436 value &= ~(PADCFG0_GPIROUTIOXAPIC | PADCFG0_GPIROUTSCI);
437 value &= ~(PADCFG0_GPIROUTSMI | PADCFG0_GPIROUTNMI);
438 writel(value, padcfg0);
441 static int intel_gpio_request_enable(struct pinctrl_dev *pctldev,
442 struct pinctrl_gpio_range *range,
445 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
446 void __iomem *padcfg0;
449 raw_spin_lock_irqsave(&pctrl->lock, flags);
451 if (!intel_pad_usable(pctrl, pin)) {
452 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
456 padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
457 intel_gpio_set_gpio_mode(padcfg0);
458 /* Disable TX buffer and enable RX (this will be input) */
459 __intel_gpio_set_direction(padcfg0, true);
461 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
466 static int intel_gpio_set_direction(struct pinctrl_dev *pctldev,
467 struct pinctrl_gpio_range *range,
468 unsigned int pin, bool input)
470 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
471 void __iomem *padcfg0;
474 raw_spin_lock_irqsave(&pctrl->lock, flags);
476 padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
477 __intel_gpio_set_direction(padcfg0, input);
479 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
484 static const struct pinmux_ops intel_pinmux_ops = {
485 .get_functions_count = intel_get_functions_count,
486 .get_function_name = intel_get_function_name,
487 .get_function_groups = intel_get_function_groups,
488 .set_mux = intel_pinmux_set_mux,
489 .gpio_request_enable = intel_gpio_request_enable,
490 .gpio_set_direction = intel_gpio_set_direction,
493 static int intel_config_get(struct pinctrl_dev *pctldev, unsigned int pin,
494 unsigned long *config)
496 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
497 enum pin_config_param param = pinconf_to_config_param(*config);
498 const struct intel_community *community;
502 if (!intel_pad_owned_by_host(pctrl, pin))
505 community = intel_get_community(pctrl, pin);
506 value = readl(intel_get_padcfg(pctrl, pin, PADCFG1));
507 term = (value & PADCFG1_TERM_MASK) >> PADCFG1_TERM_SHIFT;
510 case PIN_CONFIG_BIAS_DISABLE:
515 case PIN_CONFIG_BIAS_PULL_UP:
516 if (!term || !(value & PADCFG1_TERM_UP))
520 case PADCFG1_TERM_1K:
523 case PADCFG1_TERM_2K:
526 case PADCFG1_TERM_5K:
529 case PADCFG1_TERM_20K:
536 case PIN_CONFIG_BIAS_PULL_DOWN:
537 if (!term || value & PADCFG1_TERM_UP)
541 case PADCFG1_TERM_1K:
542 if (!(community->features & PINCTRL_FEATURE_1K_PD))
546 case PADCFG1_TERM_5K:
549 case PADCFG1_TERM_20K:
556 case PIN_CONFIG_INPUT_DEBOUNCE: {
557 void __iomem *padcfg2;
560 padcfg2 = intel_get_padcfg(pctrl, pin, PADCFG2);
565 if (!(v & PADCFG2_DEBEN))
568 v = (v & PADCFG2_DEBOUNCE_MASK) >> PADCFG2_DEBOUNCE_SHIFT;
569 arg = BIT(v) * DEBOUNCE_PERIOD / 1000;
578 *config = pinconf_to_config_packed(param, arg);
582 static int intel_config_set_pull(struct intel_pinctrl *pctrl, unsigned int pin,
583 unsigned long config)
585 unsigned int param = pinconf_to_config_param(config);
586 unsigned int arg = pinconf_to_config_argument(config);
587 const struct intel_community *community;
588 void __iomem *padcfg1;
593 raw_spin_lock_irqsave(&pctrl->lock, flags);
595 community = intel_get_community(pctrl, pin);
596 padcfg1 = intel_get_padcfg(pctrl, pin, PADCFG1);
597 value = readl(padcfg1);
600 case PIN_CONFIG_BIAS_DISABLE:
601 value &= ~(PADCFG1_TERM_MASK | PADCFG1_TERM_UP);
604 case PIN_CONFIG_BIAS_PULL_UP:
605 value &= ~PADCFG1_TERM_MASK;
607 value |= PADCFG1_TERM_UP;
611 value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT;
614 value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT;
617 value |= PADCFG1_TERM_2K << PADCFG1_TERM_SHIFT;
620 value |= PADCFG1_TERM_1K << PADCFG1_TERM_SHIFT;
628 case PIN_CONFIG_BIAS_PULL_DOWN:
629 value &= ~(PADCFG1_TERM_UP | PADCFG1_TERM_MASK);
633 value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT;
636 value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT;
639 if (!(community->features & PINCTRL_FEATURE_1K_PD)) {
643 value |= PADCFG1_TERM_1K << PADCFG1_TERM_SHIFT;
653 writel(value, padcfg1);
655 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
660 static int intel_config_set_debounce(struct intel_pinctrl *pctrl,
661 unsigned int pin, unsigned int debounce)
663 void __iomem *padcfg0, *padcfg2;
668 padcfg2 = intel_get_padcfg(pctrl, pin, PADCFG2);
672 padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
674 raw_spin_lock_irqsave(&pctrl->lock, flags);
676 value0 = readl(padcfg0);
677 value2 = readl(padcfg2);
679 /* Disable glitch filter and debouncer */
680 value0 &= ~PADCFG0_PREGFRXSEL;
681 value2 &= ~(PADCFG2_DEBEN | PADCFG2_DEBOUNCE_MASK);
686 v = order_base_2(debounce * 1000 / DEBOUNCE_PERIOD);
687 if (v < 3 || v > 15) {
691 /* Enable glitch filter and debouncer */
692 value0 |= PADCFG0_PREGFRXSEL;
693 value2 |= v << PADCFG2_DEBOUNCE_SHIFT;
694 value2 |= PADCFG2_DEBEN;
698 writel(value0, padcfg0);
699 writel(value2, padcfg2);
702 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
707 static int intel_config_set(struct pinctrl_dev *pctldev, unsigned int pin,
708 unsigned long *configs, unsigned int nconfigs)
710 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
713 if (!intel_pad_usable(pctrl, pin))
716 for (i = 0; i < nconfigs; i++) {
717 switch (pinconf_to_config_param(configs[i])) {
718 case PIN_CONFIG_BIAS_DISABLE:
719 case PIN_CONFIG_BIAS_PULL_UP:
720 case PIN_CONFIG_BIAS_PULL_DOWN:
721 ret = intel_config_set_pull(pctrl, pin, configs[i]);
726 case PIN_CONFIG_INPUT_DEBOUNCE:
727 ret = intel_config_set_debounce(pctrl, pin,
728 pinconf_to_config_argument(configs[i]));
741 static const struct pinconf_ops intel_pinconf_ops = {
743 .pin_config_get = intel_config_get,
744 .pin_config_set = intel_config_set,
747 static const struct pinctrl_desc intel_pinctrl_desc = {
748 .pctlops = &intel_pinctrl_ops,
749 .pmxops = &intel_pinmux_ops,
750 .confops = &intel_pinconf_ops,
751 .owner = THIS_MODULE,
755 * intel_gpio_to_pin() - Translate from GPIO offset to pin number
756 * @pctrl: Pinctrl structure
757 * @offset: GPIO offset from gpiolib
758 * @community: Community is filled here if not %NULL
759 * @padgrp: Pad group is filled here if not %NULL
761 * When coming through gpiolib irqchip, the GPIO offset is not
762 * automatically translated to pinctrl pin number. This function can be
763 * used to find out the corresponding pinctrl pin.
765 static int intel_gpio_to_pin(struct intel_pinctrl *pctrl, unsigned int offset,
766 const struct intel_community **community,
767 const struct intel_padgroup **padgrp)
771 for (i = 0; i < pctrl->ncommunities; i++) {
772 const struct intel_community *comm = &pctrl->communities[i];
775 for (j = 0; j < comm->ngpps; j++) {
776 const struct intel_padgroup *pgrp = &comm->gpps[j];
778 if (pgrp->gpio_base < 0)
781 if (offset >= pgrp->gpio_base &&
782 offset < pgrp->gpio_base + pgrp->size) {
785 pin = pgrp->base + offset - pgrp->gpio_base;
799 static int intel_gpio_get(struct gpio_chip *chip, unsigned int offset)
801 struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
806 pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL);
810 reg = intel_get_padcfg(pctrl, pin, PADCFG0);
814 padcfg0 = readl(reg);
815 if (!(padcfg0 & PADCFG0_GPIOTXDIS))
816 return !!(padcfg0 & PADCFG0_GPIOTXSTATE);
818 return !!(padcfg0 & PADCFG0_GPIORXSTATE);
821 static void intel_gpio_set(struct gpio_chip *chip, unsigned int offset,
824 struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
830 pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL);
834 reg = intel_get_padcfg(pctrl, pin, PADCFG0);
838 raw_spin_lock_irqsave(&pctrl->lock, flags);
839 padcfg0 = readl(reg);
841 padcfg0 |= PADCFG0_GPIOTXSTATE;
843 padcfg0 &= ~PADCFG0_GPIOTXSTATE;
844 writel(padcfg0, reg);
845 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
848 static int intel_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
850 struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
855 pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL);
859 reg = intel_get_padcfg(pctrl, pin, PADCFG0);
863 padcfg0 = readl(reg);
865 if (padcfg0 & PADCFG0_PMODE_MASK)
868 return !!(padcfg0 & PADCFG0_GPIOTXDIS);
871 static int intel_gpio_direction_input(struct gpio_chip *chip, unsigned int offset)
873 return pinctrl_gpio_direction_input(chip->base + offset);
876 static int intel_gpio_direction_output(struct gpio_chip *chip, unsigned int offset,
879 intel_gpio_set(chip, offset, value);
880 return pinctrl_gpio_direction_output(chip->base + offset);
883 static const struct gpio_chip intel_gpio_chip = {
884 .owner = THIS_MODULE,
885 .request = gpiochip_generic_request,
886 .free = gpiochip_generic_free,
887 .get_direction = intel_gpio_get_direction,
888 .direction_input = intel_gpio_direction_input,
889 .direction_output = intel_gpio_direction_output,
890 .get = intel_gpio_get,
891 .set = intel_gpio_set,
892 .set_config = gpiochip_generic_config,
895 static void intel_gpio_irq_ack(struct irq_data *d)
897 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
898 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
899 const struct intel_community *community;
900 const struct intel_padgroup *padgrp;
903 pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), &community, &padgrp);
905 unsigned int gpp, gpp_offset, is_offset;
907 gpp = padgrp->reg_num;
908 gpp_offset = padgroup_offset(padgrp, pin);
909 is_offset = community->is_offset + gpp * 4;
911 raw_spin_lock(&pctrl->lock);
912 writel(BIT(gpp_offset), community->regs + is_offset);
913 raw_spin_unlock(&pctrl->lock);
917 static void intel_gpio_irq_mask_unmask(struct irq_data *d, bool mask)
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 int gpp, gpp_offset;
929 void __iomem *reg, *is;
932 gpp = padgrp->reg_num;
933 gpp_offset = padgroup_offset(padgrp, pin);
935 reg = community->regs + community->ie_offset + gpp * 4;
936 is = community->regs + community->is_offset + gpp * 4;
938 raw_spin_lock_irqsave(&pctrl->lock, flags);
940 /* Clear interrupt status first to avoid unexpected interrupt */
941 writel(BIT(gpp_offset), is);
945 value &= ~BIT(gpp_offset);
947 value |= BIT(gpp_offset);
949 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
953 static void intel_gpio_irq_mask(struct irq_data *d)
955 intel_gpio_irq_mask_unmask(d, true);
958 static void intel_gpio_irq_unmask(struct irq_data *d)
960 intel_gpio_irq_mask_unmask(d, false);
963 static int intel_gpio_irq_type(struct irq_data *d, unsigned int type)
965 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
966 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
967 unsigned int pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), NULL, NULL);
972 reg = intel_get_padcfg(pctrl, pin, PADCFG0);
977 * If the pin is in ACPI mode it is still usable as a GPIO but it
978 * cannot be used as IRQ because GPI_IS status bit will not be
979 * updated by the host controller hardware.
981 if (intel_pad_acpi_mode(pctrl, pin)) {
982 dev_warn(pctrl->dev, "pin %u cannot be used as IRQ\n", pin);
986 raw_spin_lock_irqsave(&pctrl->lock, flags);
988 intel_gpio_set_gpio_mode(reg);
992 value &= ~(PADCFG0_RXEVCFG_MASK | PADCFG0_RXINV);
994 if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
995 value |= PADCFG0_RXEVCFG_EDGE_BOTH << PADCFG0_RXEVCFG_SHIFT;
996 } else if (type & IRQ_TYPE_EDGE_FALLING) {
997 value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT;
998 value |= PADCFG0_RXINV;
999 } else if (type & IRQ_TYPE_EDGE_RISING) {
1000 value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT;
1001 } else if (type & IRQ_TYPE_LEVEL_MASK) {
1002 if (type & IRQ_TYPE_LEVEL_LOW)
1003 value |= PADCFG0_RXINV;
1005 value |= PADCFG0_RXEVCFG_DISABLED << PADCFG0_RXEVCFG_SHIFT;
1010 if (type & IRQ_TYPE_EDGE_BOTH)
1011 irq_set_handler_locked(d, handle_edge_irq);
1012 else if (type & IRQ_TYPE_LEVEL_MASK)
1013 irq_set_handler_locked(d, handle_level_irq);
1015 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
1020 static int intel_gpio_irq_wake(struct irq_data *d, unsigned int on)
1022 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1023 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
1024 unsigned int pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), NULL, NULL);
1027 enable_irq_wake(pctrl->irq);
1029 disable_irq_wake(pctrl->irq);
1031 dev_dbg(pctrl->dev, "%sable wake for pin %u\n", on ? "en" : "dis", pin);
1035 static irqreturn_t intel_gpio_community_irq_handler(struct intel_pinctrl *pctrl,
1036 const struct intel_community *community)
1038 struct gpio_chip *gc = &pctrl->chip;
1039 irqreturn_t ret = IRQ_NONE;
1042 for (gpp = 0; gpp < community->ngpps; gpp++) {
1043 const struct intel_padgroup *padgrp = &community->gpps[gpp];
1044 unsigned long pending, enabled, gpp_offset;
1046 pending = readl(community->regs + community->is_offset +
1047 padgrp->reg_num * 4);
1048 enabled = readl(community->regs + community->ie_offset +
1049 padgrp->reg_num * 4);
1051 /* Only interrupts that are enabled */
1054 for_each_set_bit(gpp_offset, &pending, padgrp->size) {
1057 irq = irq_find_mapping(gc->irq.domain,
1058 padgrp->gpio_base + gpp_offset);
1059 generic_handle_irq(irq);
1068 static irqreturn_t intel_gpio_irq(int irq, void *data)
1070 const struct intel_community *community;
1071 struct intel_pinctrl *pctrl = data;
1072 irqreturn_t ret = IRQ_NONE;
1075 /* Need to check all communities for pending interrupts */
1076 for (i = 0; i < pctrl->ncommunities; i++) {
1077 community = &pctrl->communities[i];
1078 ret |= intel_gpio_community_irq_handler(pctrl, community);
1084 static struct irq_chip intel_gpio_irqchip = {
1085 .name = "intel-gpio",
1086 .irq_ack = intel_gpio_irq_ack,
1087 .irq_mask = intel_gpio_irq_mask,
1088 .irq_unmask = intel_gpio_irq_unmask,
1089 .irq_set_type = intel_gpio_irq_type,
1090 .irq_set_wake = intel_gpio_irq_wake,
1091 .flags = IRQCHIP_MASK_ON_SUSPEND,
1094 static int intel_gpio_add_pin_ranges(struct intel_pinctrl *pctrl,
1095 const struct intel_community *community)
1099 for (i = 0; i < community->ngpps; i++) {
1100 const struct intel_padgroup *gpp = &community->gpps[i];
1102 if (gpp->gpio_base < 0)
1105 ret = gpiochip_add_pin_range(&pctrl->chip, dev_name(pctrl->dev),
1106 gpp->gpio_base, gpp->base,
1115 static unsigned intel_gpio_ngpio(const struct intel_pinctrl *pctrl)
1117 const struct intel_community *community;
1118 unsigned int ngpio = 0;
1121 for (i = 0; i < pctrl->ncommunities; i++) {
1122 community = &pctrl->communities[i];
1123 for (j = 0; j < community->ngpps; j++) {
1124 const struct intel_padgroup *gpp = &community->gpps[j];
1126 if (gpp->gpio_base < 0)
1129 if (gpp->gpio_base + gpp->size > ngpio)
1130 ngpio = gpp->gpio_base + gpp->size;
1137 static int intel_gpio_probe(struct intel_pinctrl *pctrl, int irq)
1141 pctrl->chip = intel_gpio_chip;
1143 pctrl->chip.ngpio = intel_gpio_ngpio(pctrl);
1144 pctrl->chip.label = dev_name(pctrl->dev);
1145 pctrl->chip.parent = pctrl->dev;
1146 pctrl->chip.base = -1;
1149 ret = devm_gpiochip_add_data(pctrl->dev, &pctrl->chip, pctrl);
1151 dev_err(pctrl->dev, "failed to register gpiochip\n");
1155 for (i = 0; i < pctrl->ncommunities; i++) {
1156 struct intel_community *community = &pctrl->communities[i];
1158 ret = intel_gpio_add_pin_ranges(pctrl, community);
1160 dev_err(pctrl->dev, "failed to add GPIO pin range\n");
1166 * We need to request the interrupt here (instead of providing chip
1167 * to the irq directly) because on some platforms several GPIO
1168 * controllers share the same interrupt line.
1170 ret = devm_request_irq(pctrl->dev, irq, intel_gpio_irq,
1171 IRQF_SHARED | IRQF_NO_THREAD,
1172 dev_name(pctrl->dev), pctrl);
1174 dev_err(pctrl->dev, "failed to request interrupt\n");
1178 ret = gpiochip_irqchip_add(&pctrl->chip, &intel_gpio_irqchip, 0,
1179 handle_bad_irq, IRQ_TYPE_NONE);
1181 dev_err(pctrl->dev, "failed to add irqchip\n");
1185 gpiochip_set_chained_irqchip(&pctrl->chip, &intel_gpio_irqchip, irq,
1190 static int intel_pinctrl_add_padgroups(struct intel_pinctrl *pctrl,
1191 struct intel_community *community)
1193 struct intel_padgroup *gpps;
1194 unsigned int npins = community->npins;
1195 unsigned int padown_num = 0;
1198 if (community->gpps)
1199 ngpps = community->ngpps;
1201 ngpps = DIV_ROUND_UP(community->npins, community->gpp_size);
1203 gpps = devm_kcalloc(pctrl->dev, ngpps, sizeof(*gpps), GFP_KERNEL);
1207 for (i = 0; i < ngpps; i++) {
1208 if (community->gpps) {
1209 gpps[i] = community->gpps[i];
1211 unsigned int gpp_size = community->gpp_size;
1213 gpps[i].reg_num = i;
1214 gpps[i].base = community->pin_base + i * gpp_size;
1215 gpps[i].size = min(gpp_size, npins);
1216 npins -= gpps[i].size;
1219 if (gpps[i].size > 32)
1222 if (!gpps[i].gpio_base)
1223 gpps[i].gpio_base = gpps[i].base;
1225 gpps[i].padown_num = padown_num;
1228 * In older hardware the number of padown registers per
1229 * group is fixed regardless of the group size.
1231 if (community->gpp_num_padown_regs)
1232 padown_num += community->gpp_num_padown_regs;
1234 padown_num += DIV_ROUND_UP(gpps[i].size * 4, 32);
1237 community->ngpps = ngpps;
1238 community->gpps = gpps;
1243 static int intel_pinctrl_pm_init(struct intel_pinctrl *pctrl)
1245 #ifdef CONFIG_PM_SLEEP
1246 const struct intel_pinctrl_soc_data *soc = pctrl->soc;
1247 struct intel_community_context *communities;
1248 struct intel_pad_context *pads;
1251 pads = devm_kcalloc(pctrl->dev, soc->npins, sizeof(*pads), GFP_KERNEL);
1255 communities = devm_kcalloc(pctrl->dev, pctrl->ncommunities,
1256 sizeof(*communities), GFP_KERNEL);
1261 for (i = 0; i < pctrl->ncommunities; i++) {
1262 struct intel_community *community = &pctrl->communities[i];
1263 u32 *intmask, *hostown;
1265 intmask = devm_kcalloc(pctrl->dev, community->ngpps,
1266 sizeof(*intmask), GFP_KERNEL);
1270 communities[i].intmask = intmask;
1272 hostown = devm_kcalloc(pctrl->dev, community->ngpps,
1273 sizeof(*hostown), GFP_KERNEL);
1277 communities[i].hostown = hostown;
1280 pctrl->context.pads = pads;
1281 pctrl->context.communities = communities;
1287 static int intel_pinctrl_probe(struct platform_device *pdev,
1288 const struct intel_pinctrl_soc_data *soc_data)
1290 struct intel_pinctrl *pctrl;
1296 pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL);
1300 pctrl->dev = &pdev->dev;
1301 pctrl->soc = soc_data;
1302 raw_spin_lock_init(&pctrl->lock);
1305 * Make a copy of the communities which we can use to hold pointers
1308 pctrl->ncommunities = pctrl->soc->ncommunities;
1309 pctrl->communities = devm_kcalloc(&pdev->dev, pctrl->ncommunities,
1310 sizeof(*pctrl->communities), GFP_KERNEL);
1311 if (!pctrl->communities)
1314 for (i = 0; i < pctrl->ncommunities; i++) {
1315 struct intel_community *community = &pctrl->communities[i];
1316 struct resource *res;
1320 *community = pctrl->soc->communities[i];
1322 res = platform_get_resource(pdev, IORESOURCE_MEM,
1324 regs = devm_ioremap_resource(&pdev->dev, res);
1326 return PTR_ERR(regs);
1329 * Determine community features based on the revision if
1330 * not specified already.
1332 if (!community->features) {
1335 rev = (readl(regs + REVID) & REVID_MASK) >> REVID_SHIFT;
1337 community->features |= PINCTRL_FEATURE_DEBOUNCE;
1338 community->features |= PINCTRL_FEATURE_1K_PD;
1342 /* Read offset of the pad configuration registers */
1343 padbar = readl(regs + PADBAR);
1345 community->regs = regs;
1346 community->pad_regs = regs + padbar;
1348 if (!community->is_offset)
1349 community->is_offset = GPI_IS;
1351 ret = intel_pinctrl_add_padgroups(pctrl, community);
1356 irq = platform_get_irq(pdev, 0);
1358 dev_err(&pdev->dev, "failed to get interrupt number\n");
1362 ret = intel_pinctrl_pm_init(pctrl);
1366 pctrl->pctldesc = intel_pinctrl_desc;
1367 pctrl->pctldesc.name = dev_name(&pdev->dev);
1368 pctrl->pctldesc.pins = pctrl->soc->pins;
1369 pctrl->pctldesc.npins = pctrl->soc->npins;
1371 pctrl->pctldev = devm_pinctrl_register(&pdev->dev, &pctrl->pctldesc,
1373 if (IS_ERR(pctrl->pctldev)) {
1374 dev_err(&pdev->dev, "failed to register pinctrl driver\n");
1375 return PTR_ERR(pctrl->pctldev);
1378 ret = intel_gpio_probe(pctrl, irq);
1382 platform_set_drvdata(pdev, pctrl);
1387 int intel_pinctrl_probe_by_hid(struct platform_device *pdev)
1389 const struct intel_pinctrl_soc_data *data;
1391 data = device_get_match_data(&pdev->dev);
1392 return intel_pinctrl_probe(pdev, data);
1394 EXPORT_SYMBOL_GPL(intel_pinctrl_probe_by_hid);
1396 int intel_pinctrl_probe_by_uid(struct platform_device *pdev)
1398 const struct intel_pinctrl_soc_data *data = NULL;
1399 const struct intel_pinctrl_soc_data **table;
1400 struct acpi_device *adev;
1403 adev = ACPI_COMPANION(&pdev->dev);
1405 const void *match = device_get_match_data(&pdev->dev);
1407 table = (const struct intel_pinctrl_soc_data **)match;
1408 for (i = 0; table[i]; i++) {
1409 if (!strcmp(adev->pnp.unique_id, table[i]->uid)) {
1415 const struct platform_device_id *id;
1417 id = platform_get_device_id(pdev);
1421 table = (const struct intel_pinctrl_soc_data **)id->driver_data;
1422 data = table[pdev->id];
1427 return intel_pinctrl_probe(pdev, data);
1429 EXPORT_SYMBOL_GPL(intel_pinctrl_probe_by_uid);
1431 #ifdef CONFIG_PM_SLEEP
1432 static bool intel_pinctrl_should_save(struct intel_pinctrl *pctrl, unsigned int pin)
1434 const struct pin_desc *pd = pin_desc_get(pctrl->pctldev, pin);
1436 if (!pd || !intel_pad_usable(pctrl, pin))
1440 * Only restore the pin if it is actually in use by the kernel (or
1441 * by userspace). It is possible that some pins are used by the
1442 * BIOS during resume and those are not always locked down so leave
1445 if (pd->mux_owner || pd->gpio_owner ||
1446 gpiochip_line_is_irq(&pctrl->chip, pin))
1452 int intel_pinctrl_suspend_noirq(struct device *dev)
1454 struct intel_pinctrl *pctrl = dev_get_drvdata(dev);
1455 struct intel_community_context *communities;
1456 struct intel_pad_context *pads;
1459 pads = pctrl->context.pads;
1460 for (i = 0; i < pctrl->soc->npins; i++) {
1461 const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
1462 void __iomem *padcfg;
1465 if (!intel_pinctrl_should_save(pctrl, desc->number))
1468 val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG0));
1469 pads[i].padcfg0 = val & ~PADCFG0_GPIORXSTATE;
1470 val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG1));
1471 pads[i].padcfg1 = val;
1473 padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG2);
1475 pads[i].padcfg2 = readl(padcfg);
1478 communities = pctrl->context.communities;
1479 for (i = 0; i < pctrl->ncommunities; i++) {
1480 struct intel_community *community = &pctrl->communities[i];
1484 base = community->regs + community->ie_offset;
1485 for (gpp = 0; gpp < community->ngpps; gpp++)
1486 communities[i].intmask[gpp] = readl(base + gpp * 4);
1488 base = community->regs + community->hostown_offset;
1489 for (gpp = 0; gpp < community->ngpps; gpp++)
1490 communities[i].hostown[gpp] = readl(base + gpp * 4);
1495 EXPORT_SYMBOL_GPL(intel_pinctrl_suspend_noirq);
1497 static void intel_gpio_irq_init(struct intel_pinctrl *pctrl)
1501 for (i = 0; i < pctrl->ncommunities; i++) {
1502 const struct intel_community *community;
1506 community = &pctrl->communities[i];
1507 base = community->regs;
1509 for (gpp = 0; gpp < community->ngpps; gpp++) {
1510 /* Mask and clear all interrupts */
1511 writel(0, base + community->ie_offset + gpp * 4);
1512 writel(0xffff, base + community->is_offset + gpp * 4);
1518 intel_gpio_is_requested(struct gpio_chip *chip, int base, unsigned int size)
1523 for (i = 0; i < size; i++)
1524 if (gpiochip_is_requested(chip, base + i))
1525 requested |= BIT(i);
1531 intel_gpio_update_pad_mode(void __iomem *hostown, u32 mask, u32 value)
1535 curr = readl(hostown);
1536 updated = (curr & ~mask) | (value & mask);
1537 writel(updated, hostown);
1542 int intel_pinctrl_resume_noirq(struct device *dev)
1544 struct intel_pinctrl *pctrl = dev_get_drvdata(dev);
1545 const struct intel_community_context *communities;
1546 const struct intel_pad_context *pads;
1549 /* Mask all interrupts */
1550 intel_gpio_irq_init(pctrl);
1552 pads = pctrl->context.pads;
1553 for (i = 0; i < pctrl->soc->npins; i++) {
1554 const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
1555 void __iomem *padcfg;
1558 if (!intel_pinctrl_should_save(pctrl, desc->number))
1561 padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG0);
1562 val = readl(padcfg) & ~PADCFG0_GPIORXSTATE;
1563 if (val != pads[i].padcfg0) {
1564 writel(pads[i].padcfg0, padcfg);
1565 dev_dbg(dev, "restored pin %u padcfg0 %#08x\n",
1566 desc->number, readl(padcfg));
1569 padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG1);
1570 val = readl(padcfg);
1571 if (val != pads[i].padcfg1) {
1572 writel(pads[i].padcfg1, padcfg);
1573 dev_dbg(dev, "restored pin %u padcfg1 %#08x\n",
1574 desc->number, readl(padcfg));
1577 padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG2);
1579 val = readl(padcfg);
1580 if (val != pads[i].padcfg2) {
1581 writel(pads[i].padcfg2, padcfg);
1582 dev_dbg(dev, "restored pin %u padcfg2 %#08x\n",
1583 desc->number, readl(padcfg));
1588 communities = pctrl->context.communities;
1589 for (i = 0; i < pctrl->ncommunities; i++) {
1590 struct intel_community *community = &pctrl->communities[i];
1594 base = community->regs + community->ie_offset;
1595 for (gpp = 0; gpp < community->ngpps; gpp++) {
1596 writel(communities[i].intmask[gpp], base + gpp * 4);
1597 dev_dbg(dev, "restored mask %d/%u %#08x\n", i, gpp,
1598 readl(base + gpp * 4));
1601 base = community->regs + community->hostown_offset;
1602 for (gpp = 0; gpp < community->ngpps; gpp++) {
1603 const struct intel_padgroup *padgrp = &community->gpps[gpp];
1604 u32 requested = 0, value = 0;
1605 u32 saved = communities[i].hostown[gpp];
1607 if (padgrp->gpio_base < 0)
1610 requested = intel_gpio_is_requested(&pctrl->chip,
1611 padgrp->gpio_base, padgrp->size);
1612 value = intel_gpio_update_pad_mode(base + gpp * 4,
1614 if ((value ^ saved) & requested) {
1615 dev_warn(dev, "restore hostown %d/%u %#8x->%#8x\n",
1616 i, gpp, value, saved);
1623 EXPORT_SYMBOL_GPL(intel_pinctrl_resume_noirq);
1628 MODULE_DESCRIPTION("Intel pinctrl/GPIO core driver");
1629 MODULE_LICENSE("GPL v2");