2 * Intel pinctrl/GPIO core driver.
4 * Copyright (C) 2015, Intel Corporation
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/interrupt.h>
16 #include <linux/acpi.h>
17 #include <linux/gpio.h>
18 #include <linux/gpio/driver.h>
19 #include <linux/platform_device.h>
21 #include <linux/pinctrl/pinctrl.h>
22 #include <linux/pinctrl/pinmux.h>
23 #include <linux/pinctrl/pinconf.h>
24 #include <linux/pinctrl/pinconf-generic.h>
26 #include "pinctrl-intel.h"
28 /* Maximum number of pads in each group */
29 #define NPADS_IN_GPP 24
31 /* Offset from regs */
34 #define GPI_GPE_STS 0x140
35 #define GPI_GPE_EN 0x160
38 #define PADOWN_SHIFT(p) ((p) % 8 * PADOWN_BITS)
39 #define PADOWN_MASK(p) (0xf << PADOWN_SHIFT(p))
41 /* Offset from pad_regs */
43 #define PADCFG0_RXEVCFG_SHIFT 25
44 #define PADCFG0_RXEVCFG_MASK (3 << PADCFG0_RXEVCFG_SHIFT)
45 #define PADCFG0_RXEVCFG_LEVEL 0
46 #define PADCFG0_RXEVCFG_EDGE 1
47 #define PADCFG0_RXEVCFG_DISABLED 2
48 #define PADCFG0_RXEVCFG_EDGE_BOTH 3
49 #define PADCFG0_RXINV BIT(23)
50 #define PADCFG0_GPIROUTIOXAPIC BIT(20)
51 #define PADCFG0_GPIROUTSCI BIT(19)
52 #define PADCFG0_GPIROUTSMI BIT(18)
53 #define PADCFG0_GPIROUTNMI BIT(17)
54 #define PADCFG0_PMODE_SHIFT 10
55 #define PADCFG0_PMODE_MASK (0xf << PADCFG0_PMODE_SHIFT)
56 #define PADCFG0_GPIORXDIS BIT(9)
57 #define PADCFG0_GPIOTXDIS BIT(8)
58 #define PADCFG0_GPIORXSTATE BIT(1)
59 #define PADCFG0_GPIOTXSTATE BIT(0)
62 #define PADCFG1_TERM_UP BIT(13)
63 #define PADCFG1_TERM_SHIFT 10
64 #define PADCFG1_TERM_MASK (7 << PADCFG1_TERM_SHIFT)
65 #define PADCFG1_TERM_20K 4
66 #define PADCFG1_TERM_2K 3
67 #define PADCFG1_TERM_5K 2
68 #define PADCFG1_TERM_1K 1
70 struct intel_pad_context {
75 struct intel_community_context {
79 struct intel_pinctrl_context {
80 struct intel_pad_context *pads;
81 struct intel_community_context *communities;
85 * struct intel_pinctrl - Intel pinctrl private structure
86 * @dev: Pointer to the device structure
87 * @lock: Lock to serialize register access
88 * @pctldesc: Pin controller description
89 * @pctldev: Pointer to the pin controller device
90 * @chip: GPIO chip in this pin controller
91 * @soc: SoC/PCH specific pin configuration data
92 * @communities: All communities in this pin controller
93 * @ncommunities: Number of communities in this pin controller
94 * @context: Configuration saved over system sleep
96 struct intel_pinctrl {
99 struct pinctrl_desc pctldesc;
100 struct pinctrl_dev *pctldev;
101 struct gpio_chip chip;
102 const struct intel_pinctrl_soc_data *soc;
103 struct intel_community *communities;
105 struct intel_pinctrl_context context;
108 #define gpiochip_to_pinctrl(c) container_of(c, struct intel_pinctrl, chip)
109 #define pin_to_padno(c, p) ((p) - (c)->pin_base)
111 static struct intel_community *intel_get_community(struct intel_pinctrl *pctrl,
114 struct intel_community *community;
117 for (i = 0; i < pctrl->ncommunities; i++) {
118 community = &pctrl->communities[i];
119 if (pin >= community->pin_base &&
120 pin < community->pin_base + community->npins)
124 dev_warn(pctrl->dev, "failed to find community for pin %u\n", pin);
128 static void __iomem *intel_get_padcfg(struct intel_pinctrl *pctrl, unsigned pin,
131 const struct intel_community *community;
134 community = intel_get_community(pctrl, pin);
138 padno = pin_to_padno(community, pin);
139 return community->pad_regs + reg + padno * 8;
142 static bool intel_pad_owned_by_host(struct intel_pinctrl *pctrl, unsigned pin)
144 const struct intel_community *community;
145 unsigned padno, gpp, gpp_offset, offset;
146 void __iomem *padown;
148 community = intel_get_community(pctrl, pin);
151 if (!community->padown_offset)
154 padno = pin_to_padno(community, pin);
155 gpp = padno / NPADS_IN_GPP;
156 gpp_offset = padno % NPADS_IN_GPP;
157 offset = community->padown_offset + gpp * 16 + (gpp_offset / 8) * 4;
158 padown = community->regs + offset;
160 return !(readl(padown) & PADOWN_MASK(padno));
163 static bool intel_pad_acpi_mode(struct intel_pinctrl *pctrl, unsigned pin)
165 const struct intel_community *community;
166 unsigned padno, gpp, offset;
167 void __iomem *hostown;
169 community = intel_get_community(pctrl, pin);
172 if (!community->hostown_offset)
175 padno = pin_to_padno(community, pin);
176 gpp = padno / NPADS_IN_GPP;
177 offset = community->hostown_offset + gpp * 4;
178 hostown = community->regs + offset;
180 return !(readl(hostown) & BIT(padno % NPADS_IN_GPP));
183 static bool intel_pad_locked(struct intel_pinctrl *pctrl, unsigned pin)
185 struct intel_community *community;
186 unsigned padno, gpp, offset;
189 community = intel_get_community(pctrl, pin);
192 if (!community->padcfglock_offset)
195 padno = pin_to_padno(community, pin);
196 gpp = padno / NPADS_IN_GPP;
199 * If PADCFGLOCK and PADCFGLOCKTX bits are both clear for this pad,
200 * the pad is considered unlocked. Any other case means that it is
201 * either fully or partially locked and we don't touch it.
203 offset = community->padcfglock_offset + gpp * 8;
204 value = readl(community->regs + offset);
205 if (value & BIT(pin % NPADS_IN_GPP))
208 offset = community->padcfglock_offset + 4 + gpp * 8;
209 value = readl(community->regs + offset);
210 if (value & BIT(pin % NPADS_IN_GPP))
216 static bool intel_pad_usable(struct intel_pinctrl *pctrl, unsigned pin)
218 return intel_pad_owned_by_host(pctrl, pin) &&
219 !intel_pad_locked(pctrl, pin);
222 static int intel_get_groups_count(struct pinctrl_dev *pctldev)
224 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
226 return pctrl->soc->ngroups;
229 static const char *intel_get_group_name(struct pinctrl_dev *pctldev,
232 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
234 return pctrl->soc->groups[group].name;
237 static int intel_get_group_pins(struct pinctrl_dev *pctldev, unsigned group,
238 const unsigned **pins, unsigned *npins)
240 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
242 *pins = pctrl->soc->groups[group].pins;
243 *npins = pctrl->soc->groups[group].npins;
247 static void intel_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
250 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
251 u32 cfg0, cfg1, mode;
254 if (!intel_pad_owned_by_host(pctrl, pin)) {
255 seq_puts(s, "not available");
259 cfg0 = readl(intel_get_padcfg(pctrl, pin, PADCFG0));
260 cfg1 = readl(intel_get_padcfg(pctrl, pin, PADCFG1));
262 mode = (cfg0 & PADCFG0_PMODE_MASK) >> PADCFG0_PMODE_SHIFT;
264 seq_puts(s, "GPIO ");
266 seq_printf(s, "mode %d ", mode);
268 seq_printf(s, "0x%08x 0x%08x", cfg0, cfg1);
270 locked = intel_pad_locked(pctrl, pin);
271 acpi = intel_pad_acpi_mode(pctrl, pin);
273 if (locked || acpi) {
276 seq_puts(s, "LOCKED");
286 static const struct pinctrl_ops intel_pinctrl_ops = {
287 .get_groups_count = intel_get_groups_count,
288 .get_group_name = intel_get_group_name,
289 .get_group_pins = intel_get_group_pins,
290 .pin_dbg_show = intel_pin_dbg_show,
293 static int intel_get_functions_count(struct pinctrl_dev *pctldev)
295 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
297 return pctrl->soc->nfunctions;
300 static const char *intel_get_function_name(struct pinctrl_dev *pctldev,
303 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
305 return pctrl->soc->functions[function].name;
308 static int intel_get_function_groups(struct pinctrl_dev *pctldev,
310 const char * const **groups,
311 unsigned * const ngroups)
313 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
315 *groups = pctrl->soc->functions[function].groups;
316 *ngroups = pctrl->soc->functions[function].ngroups;
320 static int intel_pinmux_set_mux(struct pinctrl_dev *pctldev, unsigned function,
323 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
324 const struct intel_pingroup *grp = &pctrl->soc->groups[group];
328 spin_lock_irqsave(&pctrl->lock, flags);
331 * All pins in the groups needs to be accessible and writable
332 * before we can enable the mux for this group.
334 for (i = 0; i < grp->npins; i++) {
335 if (!intel_pad_usable(pctrl, grp->pins[i])) {
336 spin_unlock_irqrestore(&pctrl->lock, flags);
341 /* Now enable the mux setting for each pin in the group */
342 for (i = 0; i < grp->npins; i++) {
343 void __iomem *padcfg0;
346 padcfg0 = intel_get_padcfg(pctrl, grp->pins[i], PADCFG0);
347 value = readl(padcfg0);
349 value &= ~PADCFG0_PMODE_MASK;
350 value |= grp->mode << PADCFG0_PMODE_SHIFT;
352 writel(value, padcfg0);
355 spin_unlock_irqrestore(&pctrl->lock, flags);
360 static int intel_gpio_request_enable(struct pinctrl_dev *pctldev,
361 struct pinctrl_gpio_range *range,
364 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
365 void __iomem *padcfg0;
369 spin_lock_irqsave(&pctrl->lock, flags);
371 if (!intel_pad_usable(pctrl, pin)) {
372 spin_unlock_irqrestore(&pctrl->lock, flags);
376 padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
377 /* Put the pad into GPIO mode */
378 value = readl(padcfg0) & ~PADCFG0_PMODE_MASK;
379 /* Disable SCI/SMI/NMI generation */
380 value &= ~(PADCFG0_GPIROUTIOXAPIC | PADCFG0_GPIROUTSCI);
381 value &= ~(PADCFG0_GPIROUTSMI | PADCFG0_GPIROUTNMI);
382 /* Disable TX buffer and enable RX (this will be input) */
383 value &= ~PADCFG0_GPIORXDIS;
384 value |= PADCFG0_GPIOTXDIS;
385 writel(value, padcfg0);
387 spin_unlock_irqrestore(&pctrl->lock, flags);
392 static int intel_gpio_set_direction(struct pinctrl_dev *pctldev,
393 struct pinctrl_gpio_range *range,
394 unsigned pin, bool input)
396 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
397 void __iomem *padcfg0;
401 spin_lock_irqsave(&pctrl->lock, flags);
403 padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
405 value = readl(padcfg0);
407 value |= PADCFG0_GPIOTXDIS;
409 value &= ~PADCFG0_GPIOTXDIS;
410 writel(value, padcfg0);
412 spin_unlock_irqrestore(&pctrl->lock, flags);
417 static const struct pinmux_ops intel_pinmux_ops = {
418 .get_functions_count = intel_get_functions_count,
419 .get_function_name = intel_get_function_name,
420 .get_function_groups = intel_get_function_groups,
421 .set_mux = intel_pinmux_set_mux,
422 .gpio_request_enable = intel_gpio_request_enable,
423 .gpio_set_direction = intel_gpio_set_direction,
426 static int intel_config_get(struct pinctrl_dev *pctldev, unsigned pin,
427 unsigned long *config)
429 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
430 enum pin_config_param param = pinconf_to_config_param(*config);
434 if (!intel_pad_owned_by_host(pctrl, pin))
437 value = readl(intel_get_padcfg(pctrl, pin, PADCFG1));
438 term = (value & PADCFG1_TERM_MASK) >> PADCFG1_TERM_SHIFT;
441 case PIN_CONFIG_BIAS_DISABLE:
446 case PIN_CONFIG_BIAS_PULL_UP:
447 if (!term || !(value & PADCFG1_TERM_UP))
451 case PADCFG1_TERM_1K:
454 case PADCFG1_TERM_2K:
457 case PADCFG1_TERM_5K:
460 case PADCFG1_TERM_20K:
467 case PIN_CONFIG_BIAS_PULL_DOWN:
468 if (!term || value & PADCFG1_TERM_UP)
472 case PADCFG1_TERM_5K:
475 case PADCFG1_TERM_20K:
486 *config = pinconf_to_config_packed(param, arg);
490 static int intel_config_set_pull(struct intel_pinctrl *pctrl, unsigned pin,
491 unsigned long config)
493 unsigned param = pinconf_to_config_param(config);
494 unsigned arg = pinconf_to_config_argument(config);
495 void __iomem *padcfg1;
500 spin_lock_irqsave(&pctrl->lock, flags);
502 padcfg1 = intel_get_padcfg(pctrl, pin, PADCFG1);
503 value = readl(padcfg1);
506 case PIN_CONFIG_BIAS_DISABLE:
507 value &= ~(PADCFG1_TERM_MASK | PADCFG1_TERM_UP);
510 case PIN_CONFIG_BIAS_PULL_UP:
511 value &= ~PADCFG1_TERM_MASK;
513 value |= PADCFG1_TERM_UP;
517 value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT;
520 value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT;
523 value |= PADCFG1_TERM_2K << PADCFG1_TERM_SHIFT;
526 value |= PADCFG1_TERM_1K << PADCFG1_TERM_SHIFT;
534 case PIN_CONFIG_BIAS_PULL_DOWN:
535 value &= ~(PADCFG1_TERM_UP | PADCFG1_TERM_MASK);
539 value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT;
542 value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT;
552 writel(value, padcfg1);
554 spin_unlock_irqrestore(&pctrl->lock, flags);
559 static int intel_config_set(struct pinctrl_dev *pctldev, unsigned pin,
560 unsigned long *configs, unsigned nconfigs)
562 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
565 if (!intel_pad_usable(pctrl, pin))
568 for (i = 0; i < nconfigs; i++) {
569 switch (pinconf_to_config_param(configs[i])) {
570 case PIN_CONFIG_BIAS_DISABLE:
571 case PIN_CONFIG_BIAS_PULL_UP:
572 case PIN_CONFIG_BIAS_PULL_DOWN:
573 ret = intel_config_set_pull(pctrl, pin, configs[i]);
586 static const struct pinconf_ops intel_pinconf_ops = {
588 .pin_config_get = intel_config_get,
589 .pin_config_set = intel_config_set,
592 static const struct pinctrl_desc intel_pinctrl_desc = {
593 .pctlops = &intel_pinctrl_ops,
594 .pmxops = &intel_pinmux_ops,
595 .confops = &intel_pinconf_ops,
596 .owner = THIS_MODULE,
599 static int intel_gpio_request(struct gpio_chip *chip, unsigned offset)
601 return pinctrl_request_gpio(chip->base + offset);
604 static void intel_gpio_free(struct gpio_chip *chip, unsigned offset)
606 pinctrl_free_gpio(chip->base + offset);
609 static int intel_gpio_get(struct gpio_chip *chip, unsigned offset)
611 struct intel_pinctrl *pctrl = gpiochip_to_pinctrl(chip);
614 reg = intel_get_padcfg(pctrl, offset, PADCFG0);
618 return !!(readl(reg) & PADCFG0_GPIORXSTATE);
621 static void intel_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
623 struct intel_pinctrl *pctrl = gpiochip_to_pinctrl(chip);
626 reg = intel_get_padcfg(pctrl, offset, PADCFG0);
631 spin_lock_irqsave(&pctrl->lock, flags);
632 padcfg0 = readl(reg);
634 padcfg0 |= PADCFG0_GPIOTXSTATE;
636 padcfg0 &= ~PADCFG0_GPIOTXSTATE;
637 writel(padcfg0, reg);
638 spin_unlock_irqrestore(&pctrl->lock, flags);
642 static int intel_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
644 return pinctrl_gpio_direction_input(chip->base + offset);
647 static int intel_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
650 intel_gpio_set(chip, offset, value);
651 return pinctrl_gpio_direction_output(chip->base + offset);
654 static const struct gpio_chip intel_gpio_chip = {
655 .owner = THIS_MODULE,
656 .request = intel_gpio_request,
657 .free = intel_gpio_free,
658 .direction_input = intel_gpio_direction_input,
659 .direction_output = intel_gpio_direction_output,
660 .get = intel_gpio_get,
661 .set = intel_gpio_set,
664 static void intel_gpio_irq_ack(struct irq_data *d)
666 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
667 struct intel_pinctrl *pctrl = gpiochip_to_pinctrl(gc);
668 const struct intel_community *community;
669 unsigned pin = irqd_to_hwirq(d);
671 spin_lock(&pctrl->lock);
673 community = intel_get_community(pctrl, pin);
675 unsigned padno = pin_to_padno(community, pin);
676 unsigned gpp_offset = padno % NPADS_IN_GPP;
677 unsigned gpp = padno / NPADS_IN_GPP;
679 writel(BIT(gpp_offset), community->regs + GPI_IS + gpp * 4);
682 spin_unlock(&pctrl->lock);
685 static void intel_gpio_irq_mask_unmask(struct irq_data *d, bool mask)
687 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
688 struct intel_pinctrl *pctrl = gpiochip_to_pinctrl(gc);
689 const struct intel_community *community;
690 unsigned pin = irqd_to_hwirq(d);
693 spin_lock_irqsave(&pctrl->lock, flags);
695 community = intel_get_community(pctrl, pin);
697 unsigned padno = pin_to_padno(community, pin);
698 unsigned gpp_offset = padno % NPADS_IN_GPP;
699 unsigned gpp = padno / NPADS_IN_GPP;
703 reg = community->regs + community->ie_offset + gpp * 4;
706 value &= ~BIT(gpp_offset);
708 value |= BIT(gpp_offset);
712 spin_unlock_irqrestore(&pctrl->lock, flags);
715 static void intel_gpio_irq_mask(struct irq_data *d)
717 intel_gpio_irq_mask_unmask(d, true);
720 static void intel_gpio_irq_unmask(struct irq_data *d)
722 intel_gpio_irq_mask_unmask(d, false);
725 static int intel_gpio_irq_type(struct irq_data *d, unsigned type)
727 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
728 struct intel_pinctrl *pctrl = gpiochip_to_pinctrl(gc);
729 unsigned pin = irqd_to_hwirq(d);
734 reg = intel_get_padcfg(pctrl, pin, PADCFG0);
739 * If the pin is in ACPI mode it is still usable as a GPIO but it
740 * cannot be used as IRQ because GPI_IS status bit will not be
741 * updated by the host controller hardware.
743 if (intel_pad_acpi_mode(pctrl, pin)) {
744 dev_warn(pctrl->dev, "pin %u cannot be used as IRQ\n", pin);
748 spin_lock_irqsave(&pctrl->lock, flags);
752 value &= ~(PADCFG0_RXEVCFG_MASK | PADCFG0_RXINV);
754 if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
755 value |= PADCFG0_RXEVCFG_EDGE_BOTH << PADCFG0_RXEVCFG_SHIFT;
756 } else if (type & IRQ_TYPE_EDGE_FALLING) {
757 value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT;
758 value |= PADCFG0_RXINV;
759 } else if (type & IRQ_TYPE_EDGE_RISING) {
760 value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT;
761 } else if (type & IRQ_TYPE_LEVEL_LOW) {
762 value |= PADCFG0_RXINV;
764 value |= PADCFG0_RXEVCFG_DISABLED << PADCFG0_RXEVCFG_SHIFT;
769 if (type & IRQ_TYPE_EDGE_BOTH)
770 irq_set_handler_locked(d, handle_edge_irq);
771 else if (type & IRQ_TYPE_LEVEL_MASK)
772 irq_set_handler_locked(d, handle_level_irq);
774 spin_unlock_irqrestore(&pctrl->lock, flags);
779 static int intel_gpio_irq_wake(struct irq_data *d, unsigned int on)
781 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
782 struct intel_pinctrl *pctrl = gpiochip_to_pinctrl(gc);
783 const struct intel_community *community;
784 unsigned pin = irqd_to_hwirq(d);
785 unsigned padno, gpp, gpp_offset;
788 community = intel_get_community(pctrl, pin);
792 padno = pin_to_padno(community, pin);
793 gpp = padno / NPADS_IN_GPP;
794 gpp_offset = padno % NPADS_IN_GPP;
796 /* Clear the existing wake status */
797 writel(BIT(gpp_offset), community->regs + GPI_GPE_STS + gpp * 4);
800 * The controller will generate wake when GPE of the corresponding
801 * pad is enabled and it is not routed to SCI (GPIROUTSCI is not
804 gpe_en = readl(community->regs + GPI_GPE_EN + gpp * 4);
806 gpe_en |= BIT(gpp_offset);
808 gpe_en &= ~BIT(gpp_offset);
809 writel(gpe_en, community->regs + GPI_GPE_EN + gpp * 4);
811 dev_dbg(pctrl->dev, "%sable wake for pin %u\n", on ? "en" : "dis", pin);
815 static irqreturn_t intel_gpio_community_irq_handler(struct intel_pinctrl *pctrl,
816 const struct intel_community *community)
818 struct gpio_chip *gc = &pctrl->chip;
819 irqreturn_t ret = IRQ_NONE;
822 for (gpp = 0; gpp < community->ngpps; gpp++) {
823 unsigned long pending, enabled, gpp_offset;
825 pending = readl(community->regs + GPI_IS + gpp * 4);
826 enabled = readl(community->regs + community->ie_offset +
829 /* Only interrupts that are enabled */
832 for_each_set_bit(gpp_offset, &pending, NPADS_IN_GPP) {
836 * The last group in community can have less pins
839 padno = gpp_offset + gpp * NPADS_IN_GPP;
840 if (padno >= community->npins)
843 irq = irq_find_mapping(gc->irqdomain,
844 community->pin_base + padno);
845 generic_handle_irq(irq);
854 static irqreturn_t intel_gpio_irq(int irq, void *data)
856 const struct intel_community *community;
857 struct intel_pinctrl *pctrl = data;
858 irqreturn_t ret = IRQ_NONE;
861 /* Need to check all communities for pending interrupts */
862 for (i = 0; i < pctrl->ncommunities; i++) {
863 community = &pctrl->communities[i];
864 ret |= intel_gpio_community_irq_handler(pctrl, community);
870 static struct irq_chip intel_gpio_irqchip = {
871 .name = "intel-gpio",
872 .irq_ack = intel_gpio_irq_ack,
873 .irq_mask = intel_gpio_irq_mask,
874 .irq_unmask = intel_gpio_irq_unmask,
875 .irq_set_type = intel_gpio_irq_type,
876 .irq_set_wake = intel_gpio_irq_wake,
879 static int intel_gpio_probe(struct intel_pinctrl *pctrl, int irq)
883 pctrl->chip = intel_gpio_chip;
885 pctrl->chip.ngpio = pctrl->soc->npins;
886 pctrl->chip.label = dev_name(pctrl->dev);
887 pctrl->chip.dev = pctrl->dev;
888 pctrl->chip.base = -1;
890 ret = gpiochip_add(&pctrl->chip);
892 dev_err(pctrl->dev, "failed to register gpiochip\n");
896 ret = gpiochip_add_pin_range(&pctrl->chip, dev_name(pctrl->dev),
897 0, 0, pctrl->soc->npins);
899 dev_err(pctrl->dev, "failed to add GPIO pin range\n");
904 * We need to request the interrupt here (instead of providing chip
905 * to the irq directly) because on some platforms several GPIO
906 * controllers share the same interrupt line.
908 ret = devm_request_irq(pctrl->dev, irq, intel_gpio_irq, IRQF_SHARED,
909 dev_name(pctrl->dev), pctrl);
911 dev_err(pctrl->dev, "failed to request interrupt\n");
915 ret = gpiochip_irqchip_add(&pctrl->chip, &intel_gpio_irqchip, 0,
916 handle_simple_irq, IRQ_TYPE_NONE);
918 dev_err(pctrl->dev, "failed to add irqchip\n");
922 gpiochip_set_chained_irqchip(&pctrl->chip, &intel_gpio_irqchip, irq,
927 gpiochip_remove(&pctrl->chip);
932 static int intel_pinctrl_pm_init(struct intel_pinctrl *pctrl)
934 #ifdef CONFIG_PM_SLEEP
935 const struct intel_pinctrl_soc_data *soc = pctrl->soc;
936 struct intel_community_context *communities;
937 struct intel_pad_context *pads;
940 pads = devm_kcalloc(pctrl->dev, soc->npins, sizeof(*pads), GFP_KERNEL);
944 communities = devm_kcalloc(pctrl->dev, pctrl->ncommunities,
945 sizeof(*communities), GFP_KERNEL);
950 for (i = 0; i < pctrl->ncommunities; i++) {
951 struct intel_community *community = &pctrl->communities[i];
954 intmask = devm_kcalloc(pctrl->dev, community->ngpps,
955 sizeof(*intmask), GFP_KERNEL);
959 communities[i].intmask = intmask;
962 pctrl->context.pads = pads;
963 pctrl->context.communities = communities;
969 int intel_pinctrl_probe(struct platform_device *pdev,
970 const struct intel_pinctrl_soc_data *soc_data)
972 struct intel_pinctrl *pctrl;
978 pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL);
982 pctrl->dev = &pdev->dev;
983 pctrl->soc = soc_data;
984 spin_lock_init(&pctrl->lock);
987 * Make a copy of the communities which we can use to hold pointers
990 pctrl->ncommunities = pctrl->soc->ncommunities;
991 pctrl->communities = devm_kcalloc(&pdev->dev, pctrl->ncommunities,
992 sizeof(*pctrl->communities), GFP_KERNEL);
993 if (!pctrl->communities)
996 for (i = 0; i < pctrl->ncommunities; i++) {
997 struct intel_community *community = &pctrl->communities[i];
998 struct resource *res;
1002 *community = pctrl->soc->communities[i];
1004 res = platform_get_resource(pdev, IORESOURCE_MEM,
1006 regs = devm_ioremap_resource(&pdev->dev, res);
1008 return PTR_ERR(regs);
1010 /* Read offset of the pad configuration registers */
1011 padbar = readl(regs + PADBAR);
1013 community->regs = regs;
1014 community->pad_regs = regs + padbar;
1015 community->ngpps = DIV_ROUND_UP(community->npins, NPADS_IN_GPP);
1018 irq = platform_get_irq(pdev, 0);
1020 dev_err(&pdev->dev, "failed to get interrupt number\n");
1024 ret = intel_pinctrl_pm_init(pctrl);
1028 pctrl->pctldesc = intel_pinctrl_desc;
1029 pctrl->pctldesc.name = dev_name(&pdev->dev);
1030 pctrl->pctldesc.pins = pctrl->soc->pins;
1031 pctrl->pctldesc.npins = pctrl->soc->npins;
1033 pctrl->pctldev = pinctrl_register(&pctrl->pctldesc, &pdev->dev, pctrl);
1034 if (IS_ERR(pctrl->pctldev)) {
1035 dev_err(&pdev->dev, "failed to register pinctrl driver\n");
1036 return PTR_ERR(pctrl->pctldev);
1039 ret = intel_gpio_probe(pctrl, irq);
1041 pinctrl_unregister(pctrl->pctldev);
1045 platform_set_drvdata(pdev, pctrl);
1049 EXPORT_SYMBOL_GPL(intel_pinctrl_probe);
1051 int intel_pinctrl_remove(struct platform_device *pdev)
1053 struct intel_pinctrl *pctrl = platform_get_drvdata(pdev);
1055 gpiochip_remove(&pctrl->chip);
1056 pinctrl_unregister(pctrl->pctldev);
1060 EXPORT_SYMBOL_GPL(intel_pinctrl_remove);
1062 #ifdef CONFIG_PM_SLEEP
1063 int intel_pinctrl_suspend(struct device *dev)
1065 struct platform_device *pdev = to_platform_device(dev);
1066 struct intel_pinctrl *pctrl = platform_get_drvdata(pdev);
1067 struct intel_community_context *communities;
1068 struct intel_pad_context *pads;
1071 pads = pctrl->context.pads;
1072 for (i = 0; i < pctrl->soc->npins; i++) {
1073 const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
1076 if (!intel_pad_usable(pctrl, desc->number))
1079 val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG0));
1080 pads[i].padcfg0 = val & ~PADCFG0_GPIORXSTATE;
1081 val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG1));
1082 pads[i].padcfg1 = val;
1085 communities = pctrl->context.communities;
1086 for (i = 0; i < pctrl->ncommunities; i++) {
1087 struct intel_community *community = &pctrl->communities[i];
1091 base = community->regs + community->ie_offset;
1092 for (gpp = 0; gpp < community->ngpps; gpp++)
1093 communities[i].intmask[gpp] = readl(base + gpp * 4);
1098 EXPORT_SYMBOL_GPL(intel_pinctrl_suspend);
1100 static void intel_gpio_irq_init(struct intel_pinctrl *pctrl)
1104 for (i = 0; i < pctrl->ncommunities; i++) {
1105 const struct intel_community *community;
1109 community = &pctrl->communities[i];
1110 base = community->regs;
1112 for (gpp = 0; gpp < community->ngpps; gpp++) {
1113 /* Mask and clear all interrupts */
1114 writel(0, base + community->ie_offset + gpp * 4);
1115 writel(0xffff, base + GPI_IS + gpp * 4);
1120 int intel_pinctrl_resume(struct device *dev)
1122 struct platform_device *pdev = to_platform_device(dev);
1123 struct intel_pinctrl *pctrl = platform_get_drvdata(pdev);
1124 const struct intel_community_context *communities;
1125 const struct intel_pad_context *pads;
1128 /* Mask all interrupts */
1129 intel_gpio_irq_init(pctrl);
1131 pads = pctrl->context.pads;
1132 for (i = 0; i < pctrl->soc->npins; i++) {
1133 const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
1134 void __iomem *padcfg;
1137 if (!intel_pad_usable(pctrl, desc->number))
1140 padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG0);
1141 val = readl(padcfg) & ~PADCFG0_GPIORXSTATE;
1142 if (val != pads[i].padcfg0) {
1143 writel(pads[i].padcfg0, padcfg);
1144 dev_dbg(dev, "restored pin %u padcfg0 %#08x\n",
1145 desc->number, readl(padcfg));
1148 padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG1);
1149 val = readl(padcfg);
1150 if (val != pads[i].padcfg1) {
1151 writel(pads[i].padcfg1, padcfg);
1152 dev_dbg(dev, "restored pin %u padcfg1 %#08x\n",
1153 desc->number, readl(padcfg));
1157 communities = pctrl->context.communities;
1158 for (i = 0; i < pctrl->ncommunities; i++) {
1159 struct intel_community *community = &pctrl->communities[i];
1163 base = community->regs + community->ie_offset;
1164 for (gpp = 0; gpp < community->ngpps; gpp++) {
1165 writel(communities[i].intmask[gpp], base + gpp * 4);
1166 dev_dbg(dev, "restored mask %d/%u %#08x\n", i, gpp,
1167 readl(base + gpp * 4));
1173 EXPORT_SYMBOL_GPL(intel_pinctrl_resume);
1178 MODULE_DESCRIPTION("Intel pinctrl/GPIO core driver");
1179 MODULE_LICENSE("GPL v2");