2 * Intel pinctrl/GPIO core driver.
4 * Copyright (C) 2015, Intel Corporation
5 * Authors: Mathias Nyman <mathias.nyman@linux.intel.com>
6 * Mika Westerberg <mika.westerberg@linux.intel.com>
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/interrupt.h>
15 #include <linux/gpio/driver.h>
16 #include <linux/platform_device.h>
17 #include <linux/pinctrl/pinctrl.h>
18 #include <linux/pinctrl/pinmux.h>
19 #include <linux/pinctrl/pinconf.h>
20 #include <linux/pinctrl/pinconf-generic.h>
22 #include "pinctrl-intel.h"
24 /* Offset from regs */
27 #define GPI_GPE_STS 0x140
28 #define GPI_GPE_EN 0x160
31 #define PADOWN_SHIFT(p) ((p) % 8 * PADOWN_BITS)
32 #define PADOWN_MASK(p) (0xf << PADOWN_SHIFT(p))
33 #define PADOWN_GPP(p) ((p) / 8)
35 /* Offset from pad_regs */
37 #define PADCFG0_RXEVCFG_SHIFT 25
38 #define PADCFG0_RXEVCFG_MASK (3 << PADCFG0_RXEVCFG_SHIFT)
39 #define PADCFG0_RXEVCFG_LEVEL 0
40 #define PADCFG0_RXEVCFG_EDGE 1
41 #define PADCFG0_RXEVCFG_DISABLED 2
42 #define PADCFG0_RXEVCFG_EDGE_BOTH 3
43 #define PADCFG0_RXINV BIT(23)
44 #define PADCFG0_GPIROUTIOXAPIC BIT(20)
45 #define PADCFG0_GPIROUTSCI BIT(19)
46 #define PADCFG0_GPIROUTSMI BIT(18)
47 #define PADCFG0_GPIROUTNMI BIT(17)
48 #define PADCFG0_PMODE_SHIFT 10
49 #define PADCFG0_PMODE_MASK (0xf << PADCFG0_PMODE_SHIFT)
50 #define PADCFG0_GPIORXDIS BIT(9)
51 #define PADCFG0_GPIOTXDIS BIT(8)
52 #define PADCFG0_GPIORXSTATE BIT(1)
53 #define PADCFG0_GPIOTXSTATE BIT(0)
56 #define PADCFG1_TERM_UP BIT(13)
57 #define PADCFG1_TERM_SHIFT 10
58 #define PADCFG1_TERM_MASK (7 << PADCFG1_TERM_SHIFT)
59 #define PADCFG1_TERM_20K 4
60 #define PADCFG1_TERM_2K 3
61 #define PADCFG1_TERM_5K 2
62 #define PADCFG1_TERM_1K 1
64 struct intel_pad_context
{
69 struct intel_community_context
{
73 struct intel_pinctrl_context
{
74 struct intel_pad_context
*pads
;
75 struct intel_community_context
*communities
;
79 * struct intel_pinctrl - Intel pinctrl private structure
80 * @dev: Pointer to the device structure
81 * @lock: Lock to serialize register access
82 * @pctldesc: Pin controller description
83 * @pctldev: Pointer to the pin controller device
84 * @chip: GPIO chip in this pin controller
85 * @soc: SoC/PCH specific pin configuration data
86 * @communities: All communities in this pin controller
87 * @ncommunities: Number of communities in this pin controller
88 * @context: Configuration saved over system sleep
90 struct intel_pinctrl
{
93 struct pinctrl_desc pctldesc
;
94 struct pinctrl_dev
*pctldev
;
95 struct gpio_chip chip
;
96 const struct intel_pinctrl_soc_data
*soc
;
97 struct intel_community
*communities
;
99 struct intel_pinctrl_context context
;
102 #define pin_to_padno(c, p) ((p) - (c)->pin_base)
104 static struct intel_community
*intel_get_community(struct intel_pinctrl
*pctrl
,
107 struct intel_community
*community
;
110 for (i
= 0; i
< pctrl
->ncommunities
; i
++) {
111 community
= &pctrl
->communities
[i
];
112 if (pin
>= community
->pin_base
&&
113 pin
< community
->pin_base
+ community
->npins
)
117 dev_warn(pctrl
->dev
, "failed to find community for pin %u\n", pin
);
121 static void __iomem
*intel_get_padcfg(struct intel_pinctrl
*pctrl
, unsigned pin
,
124 const struct intel_community
*community
;
127 community
= intel_get_community(pctrl
, pin
);
131 padno
= pin_to_padno(community
, pin
);
132 return community
->pad_regs
+ reg
+ padno
* 8;
135 static bool intel_pad_owned_by_host(struct intel_pinctrl
*pctrl
, unsigned pin
)
137 const struct intel_community
*community
;
138 unsigned padno
, gpp
, offset
, group
;
139 void __iomem
*padown
;
141 community
= intel_get_community(pctrl
, pin
);
144 if (!community
->padown_offset
)
147 padno
= pin_to_padno(community
, pin
);
148 group
= padno
/ community
->gpp_size
;
149 gpp
= PADOWN_GPP(padno
% community
->gpp_size
);
150 offset
= community
->padown_offset
+ 0x10 * group
+ gpp
* 4;
151 padown
= community
->regs
+ offset
;
153 return !(readl(padown
) & PADOWN_MASK(padno
));
156 static bool intel_pad_acpi_mode(struct intel_pinctrl
*pctrl
, unsigned pin
)
158 const struct intel_community
*community
;
159 unsigned padno
, gpp
, offset
;
160 void __iomem
*hostown
;
162 community
= intel_get_community(pctrl
, pin
);
165 if (!community
->hostown_offset
)
168 padno
= pin_to_padno(community
, pin
);
169 gpp
= padno
/ community
->gpp_size
;
170 offset
= community
->hostown_offset
+ gpp
* 4;
171 hostown
= community
->regs
+ offset
;
173 return !(readl(hostown
) & BIT(padno
% community
->gpp_size
));
176 static bool intel_pad_locked(struct intel_pinctrl
*pctrl
, unsigned pin
)
178 struct intel_community
*community
;
179 unsigned padno
, gpp
, offset
;
182 community
= intel_get_community(pctrl
, pin
);
185 if (!community
->padcfglock_offset
)
188 padno
= pin_to_padno(community
, pin
);
189 gpp
= padno
/ community
->gpp_size
;
192 * If PADCFGLOCK and PADCFGLOCKTX bits are both clear for this pad,
193 * the pad is considered unlocked. Any other case means that it is
194 * either fully or partially locked and we don't touch it.
196 offset
= community
->padcfglock_offset
+ gpp
* 8;
197 value
= readl(community
->regs
+ offset
);
198 if (value
& BIT(pin
% community
->gpp_size
))
201 offset
= community
->padcfglock_offset
+ 4 + gpp
* 8;
202 value
= readl(community
->regs
+ offset
);
203 if (value
& BIT(pin
% community
->gpp_size
))
209 static bool intel_pad_usable(struct intel_pinctrl
*pctrl
, unsigned pin
)
211 return intel_pad_owned_by_host(pctrl
, pin
) &&
212 !intel_pad_locked(pctrl
, pin
);
215 static int intel_get_groups_count(struct pinctrl_dev
*pctldev
)
217 struct intel_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
219 return pctrl
->soc
->ngroups
;
222 static const char *intel_get_group_name(struct pinctrl_dev
*pctldev
,
225 struct intel_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
227 return pctrl
->soc
->groups
[group
].name
;
230 static int intel_get_group_pins(struct pinctrl_dev
*pctldev
, unsigned group
,
231 const unsigned **pins
, unsigned *npins
)
233 struct intel_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
235 *pins
= pctrl
->soc
->groups
[group
].pins
;
236 *npins
= pctrl
->soc
->groups
[group
].npins
;
240 static void intel_pin_dbg_show(struct pinctrl_dev
*pctldev
, struct seq_file
*s
,
243 struct intel_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
244 u32 cfg0
, cfg1
, mode
;
247 if (!intel_pad_owned_by_host(pctrl
, pin
)) {
248 seq_puts(s
, "not available");
252 cfg0
= readl(intel_get_padcfg(pctrl
, pin
, PADCFG0
));
253 cfg1
= readl(intel_get_padcfg(pctrl
, pin
, PADCFG1
));
255 mode
= (cfg0
& PADCFG0_PMODE_MASK
) >> PADCFG0_PMODE_SHIFT
;
257 seq_puts(s
, "GPIO ");
259 seq_printf(s
, "mode %d ", mode
);
261 seq_printf(s
, "0x%08x 0x%08x", cfg0
, cfg1
);
263 locked
= intel_pad_locked(pctrl
, pin
);
264 acpi
= intel_pad_acpi_mode(pctrl
, pin
);
266 if (locked
|| acpi
) {
269 seq_puts(s
, "LOCKED");
279 static const struct pinctrl_ops intel_pinctrl_ops
= {
280 .get_groups_count
= intel_get_groups_count
,
281 .get_group_name
= intel_get_group_name
,
282 .get_group_pins
= intel_get_group_pins
,
283 .pin_dbg_show
= intel_pin_dbg_show
,
286 static int intel_get_functions_count(struct pinctrl_dev
*pctldev
)
288 struct intel_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
290 return pctrl
->soc
->nfunctions
;
293 static const char *intel_get_function_name(struct pinctrl_dev
*pctldev
,
296 struct intel_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
298 return pctrl
->soc
->functions
[function
].name
;
301 static int intel_get_function_groups(struct pinctrl_dev
*pctldev
,
303 const char * const **groups
,
304 unsigned * const ngroups
)
306 struct intel_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
308 *groups
= pctrl
->soc
->functions
[function
].groups
;
309 *ngroups
= pctrl
->soc
->functions
[function
].ngroups
;
313 static int intel_pinmux_set_mux(struct pinctrl_dev
*pctldev
, unsigned function
,
316 struct intel_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
317 const struct intel_pingroup
*grp
= &pctrl
->soc
->groups
[group
];
321 raw_spin_lock_irqsave(&pctrl
->lock
, flags
);
324 * All pins in the groups needs to be accessible and writable
325 * before we can enable the mux for this group.
327 for (i
= 0; i
< grp
->npins
; i
++) {
328 if (!intel_pad_usable(pctrl
, grp
->pins
[i
])) {
329 raw_spin_unlock_irqrestore(&pctrl
->lock
, flags
);
334 /* Now enable the mux setting for each pin in the group */
335 for (i
= 0; i
< grp
->npins
; i
++) {
336 void __iomem
*padcfg0
;
339 padcfg0
= intel_get_padcfg(pctrl
, grp
->pins
[i
], PADCFG0
);
340 value
= readl(padcfg0
);
342 value
&= ~PADCFG0_PMODE_MASK
;
343 value
|= grp
->mode
<< PADCFG0_PMODE_SHIFT
;
345 writel(value
, padcfg0
);
348 raw_spin_unlock_irqrestore(&pctrl
->lock
, flags
);
353 static int intel_gpio_request_enable(struct pinctrl_dev
*pctldev
,
354 struct pinctrl_gpio_range
*range
,
357 struct intel_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
358 void __iomem
*padcfg0
;
362 raw_spin_lock_irqsave(&pctrl
->lock
, flags
);
364 if (!intel_pad_usable(pctrl
, pin
)) {
365 raw_spin_unlock_irqrestore(&pctrl
->lock
, flags
);
369 padcfg0
= intel_get_padcfg(pctrl
, pin
, PADCFG0
);
370 /* Put the pad into GPIO mode */
371 value
= readl(padcfg0
) & ~PADCFG0_PMODE_MASK
;
372 /* Disable SCI/SMI/NMI generation */
373 value
&= ~(PADCFG0_GPIROUTIOXAPIC
| PADCFG0_GPIROUTSCI
);
374 value
&= ~(PADCFG0_GPIROUTSMI
| PADCFG0_GPIROUTNMI
);
375 /* Disable TX buffer and enable RX (this will be input) */
376 value
&= ~PADCFG0_GPIORXDIS
;
377 value
|= PADCFG0_GPIOTXDIS
;
378 writel(value
, padcfg0
);
380 raw_spin_unlock_irqrestore(&pctrl
->lock
, flags
);
385 static int intel_gpio_set_direction(struct pinctrl_dev
*pctldev
,
386 struct pinctrl_gpio_range
*range
,
387 unsigned pin
, bool input
)
389 struct intel_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
390 void __iomem
*padcfg0
;
394 raw_spin_lock_irqsave(&pctrl
->lock
, flags
);
396 padcfg0
= intel_get_padcfg(pctrl
, pin
, PADCFG0
);
398 value
= readl(padcfg0
);
400 value
|= PADCFG0_GPIOTXDIS
;
402 value
&= ~PADCFG0_GPIOTXDIS
;
403 writel(value
, padcfg0
);
405 raw_spin_unlock_irqrestore(&pctrl
->lock
, flags
);
410 static const struct pinmux_ops intel_pinmux_ops
= {
411 .get_functions_count
= intel_get_functions_count
,
412 .get_function_name
= intel_get_function_name
,
413 .get_function_groups
= intel_get_function_groups
,
414 .set_mux
= intel_pinmux_set_mux
,
415 .gpio_request_enable
= intel_gpio_request_enable
,
416 .gpio_set_direction
= intel_gpio_set_direction
,
419 static int intel_config_get(struct pinctrl_dev
*pctldev
, unsigned pin
,
420 unsigned long *config
)
422 struct intel_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
423 enum pin_config_param param
= pinconf_to_config_param(*config
);
427 if (!intel_pad_owned_by_host(pctrl
, pin
))
430 value
= readl(intel_get_padcfg(pctrl
, pin
, PADCFG1
));
431 term
= (value
& PADCFG1_TERM_MASK
) >> PADCFG1_TERM_SHIFT
;
434 case PIN_CONFIG_BIAS_DISABLE
:
439 case PIN_CONFIG_BIAS_PULL_UP
:
440 if (!term
|| !(value
& PADCFG1_TERM_UP
))
444 case PADCFG1_TERM_1K
:
447 case PADCFG1_TERM_2K
:
450 case PADCFG1_TERM_5K
:
453 case PADCFG1_TERM_20K
:
460 case PIN_CONFIG_BIAS_PULL_DOWN
:
461 if (!term
|| value
& PADCFG1_TERM_UP
)
465 case PADCFG1_TERM_5K
:
468 case PADCFG1_TERM_20K
:
479 *config
= pinconf_to_config_packed(param
, arg
);
483 static int intel_config_set_pull(struct intel_pinctrl
*pctrl
, unsigned pin
,
484 unsigned long config
)
486 unsigned param
= pinconf_to_config_param(config
);
487 unsigned arg
= pinconf_to_config_argument(config
);
488 void __iomem
*padcfg1
;
493 raw_spin_lock_irqsave(&pctrl
->lock
, flags
);
495 padcfg1
= intel_get_padcfg(pctrl
, pin
, PADCFG1
);
496 value
= readl(padcfg1
);
499 case PIN_CONFIG_BIAS_DISABLE
:
500 value
&= ~(PADCFG1_TERM_MASK
| PADCFG1_TERM_UP
);
503 case PIN_CONFIG_BIAS_PULL_UP
:
504 value
&= ~PADCFG1_TERM_MASK
;
506 value
|= PADCFG1_TERM_UP
;
510 value
|= PADCFG1_TERM_20K
<< PADCFG1_TERM_SHIFT
;
513 value
|= PADCFG1_TERM_5K
<< PADCFG1_TERM_SHIFT
;
516 value
|= PADCFG1_TERM_2K
<< PADCFG1_TERM_SHIFT
;
519 value
|= PADCFG1_TERM_1K
<< PADCFG1_TERM_SHIFT
;
527 case PIN_CONFIG_BIAS_PULL_DOWN
:
528 value
&= ~(PADCFG1_TERM_UP
| PADCFG1_TERM_MASK
);
532 value
|= PADCFG1_TERM_20K
<< PADCFG1_TERM_SHIFT
;
535 value
|= PADCFG1_TERM_5K
<< PADCFG1_TERM_SHIFT
;
545 writel(value
, padcfg1
);
547 raw_spin_unlock_irqrestore(&pctrl
->lock
, flags
);
552 static int intel_config_set(struct pinctrl_dev
*pctldev
, unsigned pin
,
553 unsigned long *configs
, unsigned nconfigs
)
555 struct intel_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
558 if (!intel_pad_usable(pctrl
, pin
))
561 for (i
= 0; i
< nconfigs
; i
++) {
562 switch (pinconf_to_config_param(configs
[i
])) {
563 case PIN_CONFIG_BIAS_DISABLE
:
564 case PIN_CONFIG_BIAS_PULL_UP
:
565 case PIN_CONFIG_BIAS_PULL_DOWN
:
566 ret
= intel_config_set_pull(pctrl
, pin
, configs
[i
]);
579 static const struct pinconf_ops intel_pinconf_ops
= {
581 .pin_config_get
= intel_config_get
,
582 .pin_config_set
= intel_config_set
,
585 static const struct pinctrl_desc intel_pinctrl_desc
= {
586 .pctlops
= &intel_pinctrl_ops
,
587 .pmxops
= &intel_pinmux_ops
,
588 .confops
= &intel_pinconf_ops
,
589 .owner
= THIS_MODULE
,
592 static int intel_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
594 struct intel_pinctrl
*pctrl
= gpiochip_get_data(chip
);
597 reg
= intel_get_padcfg(pctrl
, offset
, PADCFG0
);
601 return !!(readl(reg
) & PADCFG0_GPIORXSTATE
);
604 static void intel_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
606 struct intel_pinctrl
*pctrl
= gpiochip_get_data(chip
);
609 reg
= intel_get_padcfg(pctrl
, offset
, PADCFG0
);
614 raw_spin_lock_irqsave(&pctrl
->lock
, flags
);
615 padcfg0
= readl(reg
);
617 padcfg0
|= PADCFG0_GPIOTXSTATE
;
619 padcfg0
&= ~PADCFG0_GPIOTXSTATE
;
620 writel(padcfg0
, reg
);
621 raw_spin_unlock_irqrestore(&pctrl
->lock
, flags
);
625 static int intel_gpio_direction_input(struct gpio_chip
*chip
, unsigned offset
)
627 return pinctrl_gpio_direction_input(chip
->base
+ offset
);
630 static int intel_gpio_direction_output(struct gpio_chip
*chip
, unsigned offset
,
633 intel_gpio_set(chip
, offset
, value
);
634 return pinctrl_gpio_direction_output(chip
->base
+ offset
);
637 static const struct gpio_chip intel_gpio_chip
= {
638 .owner
= THIS_MODULE
,
639 .request
= gpiochip_generic_request
,
640 .free
= gpiochip_generic_free
,
641 .direction_input
= intel_gpio_direction_input
,
642 .direction_output
= intel_gpio_direction_output
,
643 .get
= intel_gpio_get
,
644 .set
= intel_gpio_set
,
647 static void intel_gpio_irq_ack(struct irq_data
*d
)
649 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
650 struct intel_pinctrl
*pctrl
= gpiochip_get_data(gc
);
651 const struct intel_community
*community
;
652 unsigned pin
= irqd_to_hwirq(d
);
654 raw_spin_lock(&pctrl
->lock
);
656 community
= intel_get_community(pctrl
, pin
);
658 unsigned padno
= pin_to_padno(community
, pin
);
659 unsigned gpp_offset
= padno
% community
->gpp_size
;
660 unsigned gpp
= padno
/ community
->gpp_size
;
662 writel(BIT(gpp_offset
), community
->regs
+ GPI_IS
+ gpp
* 4);
665 raw_spin_unlock(&pctrl
->lock
);
668 static void intel_gpio_irq_enable(struct irq_data
*d
)
670 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
671 struct intel_pinctrl
*pctrl
= gpiochip_get_data(gc
);
672 const struct intel_community
*community
;
673 unsigned pin
= irqd_to_hwirq(d
);
676 raw_spin_lock_irqsave(&pctrl
->lock
, flags
);
678 community
= intel_get_community(pctrl
, pin
);
680 unsigned padno
= pin_to_padno(community
, pin
);
681 unsigned gpp_size
= community
->gpp_size
;
682 unsigned gpp_offset
= padno
% gpp_size
;
683 unsigned gpp
= padno
/ gpp_size
;
686 /* Clear interrupt status first to avoid unexpected interrupt */
687 writel(BIT(gpp_offset
), community
->regs
+ GPI_IS
+ gpp
* 4);
689 value
= readl(community
->regs
+ community
->ie_offset
+ gpp
* 4);
690 value
|= BIT(gpp_offset
);
691 writel(value
, community
->regs
+ community
->ie_offset
+ gpp
* 4);
694 raw_spin_unlock_irqrestore(&pctrl
->lock
, flags
);
697 static void intel_gpio_irq_mask_unmask(struct irq_data
*d
, bool mask
)
699 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
700 struct intel_pinctrl
*pctrl
= gpiochip_get_data(gc
);
701 const struct intel_community
*community
;
702 unsigned pin
= irqd_to_hwirq(d
);
705 raw_spin_lock_irqsave(&pctrl
->lock
, flags
);
707 community
= intel_get_community(pctrl
, pin
);
709 unsigned padno
= pin_to_padno(community
, pin
);
710 unsigned gpp_offset
= padno
% community
->gpp_size
;
711 unsigned gpp
= padno
/ community
->gpp_size
;
715 reg
= community
->regs
+ community
->ie_offset
+ gpp
* 4;
718 value
&= ~BIT(gpp_offset
);
720 value
|= BIT(gpp_offset
);
724 raw_spin_unlock_irqrestore(&pctrl
->lock
, flags
);
727 static void intel_gpio_irq_mask(struct irq_data
*d
)
729 intel_gpio_irq_mask_unmask(d
, true);
732 static void intel_gpio_irq_unmask(struct irq_data
*d
)
734 intel_gpio_irq_mask_unmask(d
, false);
737 static int intel_gpio_irq_type(struct irq_data
*d
, unsigned type
)
739 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
740 struct intel_pinctrl
*pctrl
= gpiochip_get_data(gc
);
741 unsigned pin
= irqd_to_hwirq(d
);
746 reg
= intel_get_padcfg(pctrl
, pin
, PADCFG0
);
751 * If the pin is in ACPI mode it is still usable as a GPIO but it
752 * cannot be used as IRQ because GPI_IS status bit will not be
753 * updated by the host controller hardware.
755 if (intel_pad_acpi_mode(pctrl
, pin
)) {
756 dev_warn(pctrl
->dev
, "pin %u cannot be used as IRQ\n", pin
);
760 raw_spin_lock_irqsave(&pctrl
->lock
, flags
);
764 value
&= ~(PADCFG0_RXEVCFG_MASK
| PADCFG0_RXINV
);
766 if ((type
& IRQ_TYPE_EDGE_BOTH
) == IRQ_TYPE_EDGE_BOTH
) {
767 value
|= PADCFG0_RXEVCFG_EDGE_BOTH
<< PADCFG0_RXEVCFG_SHIFT
;
768 } else if (type
& IRQ_TYPE_EDGE_FALLING
) {
769 value
|= PADCFG0_RXEVCFG_EDGE
<< PADCFG0_RXEVCFG_SHIFT
;
770 value
|= PADCFG0_RXINV
;
771 } else if (type
& IRQ_TYPE_EDGE_RISING
) {
772 value
|= PADCFG0_RXEVCFG_EDGE
<< PADCFG0_RXEVCFG_SHIFT
;
773 } else if (type
& IRQ_TYPE_LEVEL_MASK
) {
774 if (type
& IRQ_TYPE_LEVEL_LOW
)
775 value
|= PADCFG0_RXINV
;
777 value
|= PADCFG0_RXEVCFG_DISABLED
<< PADCFG0_RXEVCFG_SHIFT
;
782 if (type
& IRQ_TYPE_EDGE_BOTH
)
783 irq_set_handler_locked(d
, handle_edge_irq
);
784 else if (type
& IRQ_TYPE_LEVEL_MASK
)
785 irq_set_handler_locked(d
, handle_level_irq
);
787 raw_spin_unlock_irqrestore(&pctrl
->lock
, flags
);
792 static int intel_gpio_irq_wake(struct irq_data
*d
, unsigned int on
)
794 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
795 struct intel_pinctrl
*pctrl
= gpiochip_get_data(gc
);
796 const struct intel_community
*community
;
797 unsigned pin
= irqd_to_hwirq(d
);
798 unsigned padno
, gpp
, gpp_offset
;
802 community
= intel_get_community(pctrl
, pin
);
806 raw_spin_lock_irqsave(&pctrl
->lock
, flags
);
808 padno
= pin_to_padno(community
, pin
);
809 gpp
= padno
/ community
->gpp_size
;
810 gpp_offset
= padno
% community
->gpp_size
;
812 /* Clear the existing wake status */
813 writel(BIT(gpp_offset
), community
->regs
+ GPI_GPE_STS
+ gpp
* 4);
816 * The controller will generate wake when GPE of the corresponding
817 * pad is enabled and it is not routed to SCI (GPIROUTSCI is not
820 gpe_en
= readl(community
->regs
+ GPI_GPE_EN
+ gpp
* 4);
822 gpe_en
|= BIT(gpp_offset
);
824 gpe_en
&= ~BIT(gpp_offset
);
825 writel(gpe_en
, community
->regs
+ GPI_GPE_EN
+ gpp
* 4);
827 raw_spin_unlock_irqrestore(&pctrl
->lock
, flags
);
829 dev_dbg(pctrl
->dev
, "%sable wake for pin %u\n", on
? "en" : "dis", pin
);
833 static irqreturn_t
intel_gpio_community_irq_handler(struct intel_pinctrl
*pctrl
,
834 const struct intel_community
*community
)
836 struct gpio_chip
*gc
= &pctrl
->chip
;
837 irqreturn_t ret
= IRQ_NONE
;
840 for (gpp
= 0; gpp
< community
->ngpps
; gpp
++) {
841 unsigned long pending
, enabled
, gpp_offset
;
843 pending
= readl(community
->regs
+ GPI_IS
+ gpp
* 4);
844 enabled
= readl(community
->regs
+ community
->ie_offset
+
847 /* Only interrupts that are enabled */
850 for_each_set_bit(gpp_offset
, &pending
, community
->gpp_size
) {
854 * The last group in community can have less pins
857 padno
= gpp_offset
+ gpp
* community
->gpp_size
;
858 if (padno
>= community
->npins
)
861 irq
= irq_find_mapping(gc
->irqdomain
,
862 community
->pin_base
+ padno
);
863 generic_handle_irq(irq
);
872 static irqreturn_t
intel_gpio_irq(int irq
, void *data
)
874 const struct intel_community
*community
;
875 struct intel_pinctrl
*pctrl
= data
;
876 irqreturn_t ret
= IRQ_NONE
;
879 /* Need to check all communities for pending interrupts */
880 for (i
= 0; i
< pctrl
->ncommunities
; i
++) {
881 community
= &pctrl
->communities
[i
];
882 ret
|= intel_gpio_community_irq_handler(pctrl
, community
);
888 static struct irq_chip intel_gpio_irqchip
= {
889 .name
= "intel-gpio",
890 .irq_enable
= intel_gpio_irq_enable
,
891 .irq_ack
= intel_gpio_irq_ack
,
892 .irq_mask
= intel_gpio_irq_mask
,
893 .irq_unmask
= intel_gpio_irq_unmask
,
894 .irq_set_type
= intel_gpio_irq_type
,
895 .irq_set_wake
= intel_gpio_irq_wake
,
898 static int intel_gpio_probe(struct intel_pinctrl
*pctrl
, int irq
)
902 pctrl
->chip
= intel_gpio_chip
;
904 pctrl
->chip
.ngpio
= pctrl
->soc
->npins
;
905 pctrl
->chip
.label
= dev_name(pctrl
->dev
);
906 pctrl
->chip
.parent
= pctrl
->dev
;
907 pctrl
->chip
.base
= -1;
909 ret
= gpiochip_add_data(&pctrl
->chip
, pctrl
);
911 dev_err(pctrl
->dev
, "failed to register gpiochip\n");
915 ret
= gpiochip_add_pin_range(&pctrl
->chip
, dev_name(pctrl
->dev
),
916 0, 0, pctrl
->soc
->npins
);
918 dev_err(pctrl
->dev
, "failed to add GPIO pin range\n");
923 * We need to request the interrupt here (instead of providing chip
924 * to the irq directly) because on some platforms several GPIO
925 * controllers share the same interrupt line.
927 ret
= devm_request_irq(pctrl
->dev
, irq
, intel_gpio_irq
,
928 IRQF_SHARED
| IRQF_NO_THREAD
,
929 dev_name(pctrl
->dev
), pctrl
);
931 dev_err(pctrl
->dev
, "failed to request interrupt\n");
935 ret
= gpiochip_irqchip_add(&pctrl
->chip
, &intel_gpio_irqchip
, 0,
936 handle_simple_irq
, IRQ_TYPE_NONE
);
938 dev_err(pctrl
->dev
, "failed to add irqchip\n");
942 gpiochip_set_chained_irqchip(&pctrl
->chip
, &intel_gpio_irqchip
, irq
,
947 gpiochip_remove(&pctrl
->chip
);
952 static int intel_pinctrl_pm_init(struct intel_pinctrl
*pctrl
)
954 #ifdef CONFIG_PM_SLEEP
955 const struct intel_pinctrl_soc_data
*soc
= pctrl
->soc
;
956 struct intel_community_context
*communities
;
957 struct intel_pad_context
*pads
;
960 pads
= devm_kcalloc(pctrl
->dev
, soc
->npins
, sizeof(*pads
), GFP_KERNEL
);
964 communities
= devm_kcalloc(pctrl
->dev
, pctrl
->ncommunities
,
965 sizeof(*communities
), GFP_KERNEL
);
970 for (i
= 0; i
< pctrl
->ncommunities
; i
++) {
971 struct intel_community
*community
= &pctrl
->communities
[i
];
974 intmask
= devm_kcalloc(pctrl
->dev
, community
->ngpps
,
975 sizeof(*intmask
), GFP_KERNEL
);
979 communities
[i
].intmask
= intmask
;
982 pctrl
->context
.pads
= pads
;
983 pctrl
->context
.communities
= communities
;
989 int intel_pinctrl_probe(struct platform_device
*pdev
,
990 const struct intel_pinctrl_soc_data
*soc_data
)
992 struct intel_pinctrl
*pctrl
;
998 pctrl
= devm_kzalloc(&pdev
->dev
, sizeof(*pctrl
), GFP_KERNEL
);
1002 pctrl
->dev
= &pdev
->dev
;
1003 pctrl
->soc
= soc_data
;
1004 raw_spin_lock_init(&pctrl
->lock
);
1007 * Make a copy of the communities which we can use to hold pointers
1010 pctrl
->ncommunities
= pctrl
->soc
->ncommunities
;
1011 pctrl
->communities
= devm_kcalloc(&pdev
->dev
, pctrl
->ncommunities
,
1012 sizeof(*pctrl
->communities
), GFP_KERNEL
);
1013 if (!pctrl
->communities
)
1016 for (i
= 0; i
< pctrl
->ncommunities
; i
++) {
1017 struct intel_community
*community
= &pctrl
->communities
[i
];
1018 struct resource
*res
;
1022 *community
= pctrl
->soc
->communities
[i
];
1024 res
= platform_get_resource(pdev
, IORESOURCE_MEM
,
1026 regs
= devm_ioremap_resource(&pdev
->dev
, res
);
1028 return PTR_ERR(regs
);
1030 /* Read offset of the pad configuration registers */
1031 padbar
= readl(regs
+ PADBAR
);
1033 community
->regs
= regs
;
1034 community
->pad_regs
= regs
+ padbar
;
1035 community
->ngpps
= DIV_ROUND_UP(community
->npins
,
1036 community
->gpp_size
);
1039 irq
= platform_get_irq(pdev
, 0);
1041 dev_err(&pdev
->dev
, "failed to get interrupt number\n");
1045 ret
= intel_pinctrl_pm_init(pctrl
);
1049 pctrl
->pctldesc
= intel_pinctrl_desc
;
1050 pctrl
->pctldesc
.name
= dev_name(&pdev
->dev
);
1051 pctrl
->pctldesc
.pins
= pctrl
->soc
->pins
;
1052 pctrl
->pctldesc
.npins
= pctrl
->soc
->npins
;
1054 pctrl
->pctldev
= devm_pinctrl_register(&pdev
->dev
, &pctrl
->pctldesc
,
1056 if (IS_ERR(pctrl
->pctldev
)) {
1057 dev_err(&pdev
->dev
, "failed to register pinctrl driver\n");
1058 return PTR_ERR(pctrl
->pctldev
);
1061 ret
= intel_gpio_probe(pctrl
, irq
);
1065 platform_set_drvdata(pdev
, pctrl
);
1069 EXPORT_SYMBOL_GPL(intel_pinctrl_probe
);
1071 int intel_pinctrl_remove(struct platform_device
*pdev
)
1073 struct intel_pinctrl
*pctrl
= platform_get_drvdata(pdev
);
1075 gpiochip_remove(&pctrl
->chip
);
1079 EXPORT_SYMBOL_GPL(intel_pinctrl_remove
);
1081 #ifdef CONFIG_PM_SLEEP
1082 int intel_pinctrl_suspend(struct device
*dev
)
1084 struct platform_device
*pdev
= to_platform_device(dev
);
1085 struct intel_pinctrl
*pctrl
= platform_get_drvdata(pdev
);
1086 struct intel_community_context
*communities
;
1087 struct intel_pad_context
*pads
;
1090 pads
= pctrl
->context
.pads
;
1091 for (i
= 0; i
< pctrl
->soc
->npins
; i
++) {
1092 const struct pinctrl_pin_desc
*desc
= &pctrl
->soc
->pins
[i
];
1095 if (!intel_pad_usable(pctrl
, desc
->number
))
1098 val
= readl(intel_get_padcfg(pctrl
, desc
->number
, PADCFG0
));
1099 pads
[i
].padcfg0
= val
& ~PADCFG0_GPIORXSTATE
;
1100 val
= readl(intel_get_padcfg(pctrl
, desc
->number
, PADCFG1
));
1101 pads
[i
].padcfg1
= val
;
1104 communities
= pctrl
->context
.communities
;
1105 for (i
= 0; i
< pctrl
->ncommunities
; i
++) {
1106 struct intel_community
*community
= &pctrl
->communities
[i
];
1110 base
= community
->regs
+ community
->ie_offset
;
1111 for (gpp
= 0; gpp
< community
->ngpps
; gpp
++)
1112 communities
[i
].intmask
[gpp
] = readl(base
+ gpp
* 4);
1117 EXPORT_SYMBOL_GPL(intel_pinctrl_suspend
);
1119 static void intel_gpio_irq_init(struct intel_pinctrl
*pctrl
)
1123 for (i
= 0; i
< pctrl
->ncommunities
; i
++) {
1124 const struct intel_community
*community
;
1128 community
= &pctrl
->communities
[i
];
1129 base
= community
->regs
;
1131 for (gpp
= 0; gpp
< community
->ngpps
; gpp
++) {
1132 /* Mask and clear all interrupts */
1133 writel(0, base
+ community
->ie_offset
+ gpp
* 4);
1134 writel(0xffff, base
+ GPI_IS
+ gpp
* 4);
1139 int intel_pinctrl_resume(struct device
*dev
)
1141 struct platform_device
*pdev
= to_platform_device(dev
);
1142 struct intel_pinctrl
*pctrl
= platform_get_drvdata(pdev
);
1143 const struct intel_community_context
*communities
;
1144 const struct intel_pad_context
*pads
;
1147 /* Mask all interrupts */
1148 intel_gpio_irq_init(pctrl
);
1150 pads
= pctrl
->context
.pads
;
1151 for (i
= 0; i
< pctrl
->soc
->npins
; i
++) {
1152 const struct pinctrl_pin_desc
*desc
= &pctrl
->soc
->pins
[i
];
1153 void __iomem
*padcfg
;
1156 if (!intel_pad_usable(pctrl
, desc
->number
))
1159 padcfg
= intel_get_padcfg(pctrl
, desc
->number
, PADCFG0
);
1160 val
= readl(padcfg
) & ~PADCFG0_GPIORXSTATE
;
1161 if (val
!= pads
[i
].padcfg0
) {
1162 writel(pads
[i
].padcfg0
, padcfg
);
1163 dev_dbg(dev
, "restored pin %u padcfg0 %#08x\n",
1164 desc
->number
, readl(padcfg
));
1167 padcfg
= intel_get_padcfg(pctrl
, desc
->number
, PADCFG1
);
1168 val
= readl(padcfg
);
1169 if (val
!= pads
[i
].padcfg1
) {
1170 writel(pads
[i
].padcfg1
, padcfg
);
1171 dev_dbg(dev
, "restored pin %u padcfg1 %#08x\n",
1172 desc
->number
, readl(padcfg
));
1176 communities
= pctrl
->context
.communities
;
1177 for (i
= 0; i
< pctrl
->ncommunities
; i
++) {
1178 struct intel_community
*community
= &pctrl
->communities
[i
];
1182 base
= community
->regs
+ community
->ie_offset
;
1183 for (gpp
= 0; gpp
< community
->ngpps
; gpp
++) {
1184 writel(communities
[i
].intmask
[gpp
], base
+ gpp
* 4);
1185 dev_dbg(dev
, "restored mask %d/%u %#08x\n", i
, gpp
,
1186 readl(base
+ gpp
* 4));
1192 EXPORT_SYMBOL_GPL(intel_pinctrl_resume
);
1195 MODULE_AUTHOR("Mathias Nyman <mathias.nyman@linux.intel.com>");
1196 MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
1197 MODULE_DESCRIPTION("Intel pinctrl/GPIO core driver");
1198 MODULE_LICENSE("GPL v2");