xref: /openbmc/linux/drivers/pinctrl/intel/pinctrl-intel.c (revision 39c1f1bd8adf29840ef52225ce95f390e3d7b2cf)
1875a92b3SAndy Shevchenko // SPDX-License-Identifier: GPL-2.0
27981c001SMika Westerberg /*
37981c001SMika Westerberg  * Intel pinctrl/GPIO core driver.
47981c001SMika Westerberg  *
57981c001SMika Westerberg  * Copyright (C) 2015, Intel Corporation
67981c001SMika Westerberg  * Authors: Mathias Nyman <mathias.nyman@linux.intel.com>
77981c001SMika Westerberg  *          Mika Westerberg <mika.westerberg@linux.intel.com>
87981c001SMika Westerberg  */
97981c001SMika Westerberg 
10924cf800SAndy Shevchenko #include <linux/acpi.h>
117981c001SMika Westerberg #include <linux/gpio/driver.h>
1266c812d2SAndy Shevchenko #include <linux/interrupt.h>
13e57725eaSMika Westerberg #include <linux/log2.h>
146a33a1d6SAndy Shevchenko #include <linux/module.h>
157981c001SMika Westerberg #include <linux/platform_device.h>
16924cf800SAndy Shevchenko #include <linux/property.h>
176a33a1d6SAndy Shevchenko #include <linux/time.h>
18924cf800SAndy Shevchenko 
197981c001SMika Westerberg #include <linux/pinctrl/pinctrl.h>
207981c001SMika Westerberg #include <linux/pinctrl/pinmux.h>
217981c001SMika Westerberg #include <linux/pinctrl/pinconf.h>
227981c001SMika Westerberg #include <linux/pinctrl/pinconf-generic.h>
237981c001SMika Westerberg 
24c538b943SMika Westerberg #include "../core.h"
257981c001SMika Westerberg #include "pinctrl-intel.h"
267981c001SMika Westerberg 
277981c001SMika Westerberg /* Offset from regs */
28e57725eaSMika Westerberg #define REVID				0x000
29e57725eaSMika Westerberg #define REVID_SHIFT			16
30e57725eaSMika Westerberg #define REVID_MASK			GENMASK(31, 16)
31e57725eaSMika Westerberg 
3291d898e5SAndy Shevchenko #define CAPLIST				0x004
3391d898e5SAndy Shevchenko #define CAPLIST_ID_SHIFT		16
3491d898e5SAndy Shevchenko #define CAPLIST_ID_MASK			GENMASK(23, 16)
3591d898e5SAndy Shevchenko #define CAPLIST_ID_GPIO_HW_INFO		1
3691d898e5SAndy Shevchenko #define CAPLIST_ID_PWM			2
3791d898e5SAndy Shevchenko #define CAPLIST_ID_BLINK		3
3891d898e5SAndy Shevchenko #define CAPLIST_ID_EXP			4
3991d898e5SAndy Shevchenko #define CAPLIST_NEXT_SHIFT		0
4091d898e5SAndy Shevchenko #define CAPLIST_NEXT_MASK		GENMASK(15, 0)
4191d898e5SAndy Shevchenko 
427981c001SMika Westerberg #define PADBAR				0x00c
437981c001SMika Westerberg 
447981c001SMika Westerberg #define PADOWN_BITS			4
457981c001SMika Westerberg #define PADOWN_SHIFT(p)			((p) % 8 * PADOWN_BITS)
46e58926e7SAndy Shevchenko #define PADOWN_MASK(p)			(GENMASK(3, 0) << PADOWN_SHIFT(p))
4799a735b3SQipeng Zha #define PADOWN_GPP(p)			((p) / 8)
487981c001SMika Westerberg 
497981c001SMika Westerberg /* Offset from pad_regs */
507981c001SMika Westerberg #define PADCFG0				0x000
517981c001SMika Westerberg #define PADCFG0_RXEVCFG_SHIFT		25
52e58926e7SAndy Shevchenko #define PADCFG0_RXEVCFG_MASK		GENMASK(26, 25)
537981c001SMika Westerberg #define PADCFG0_RXEVCFG_LEVEL		0
547981c001SMika Westerberg #define PADCFG0_RXEVCFG_EDGE		1
557981c001SMika Westerberg #define PADCFG0_RXEVCFG_DISABLED	2
567981c001SMika Westerberg #define PADCFG0_RXEVCFG_EDGE_BOTH	3
57e57725eaSMika Westerberg #define PADCFG0_PREGFRXSEL		BIT(24)
587981c001SMika Westerberg #define PADCFG0_RXINV			BIT(23)
597981c001SMika Westerberg #define PADCFG0_GPIROUTIOXAPIC		BIT(20)
607981c001SMika Westerberg #define PADCFG0_GPIROUTSCI		BIT(19)
617981c001SMika Westerberg #define PADCFG0_GPIROUTSMI		BIT(18)
627981c001SMika Westerberg #define PADCFG0_GPIROUTNMI		BIT(17)
637981c001SMika Westerberg #define PADCFG0_PMODE_SHIFT		10
64e58926e7SAndy Shevchenko #define PADCFG0_PMODE_MASK		GENMASK(13, 10)
654973ddc8SAndy Shevchenko #define PADCFG0_PMODE_GPIO		0
667981c001SMika Westerberg #define PADCFG0_GPIORXDIS		BIT(9)
677981c001SMika Westerberg #define PADCFG0_GPIOTXDIS		BIT(8)
687981c001SMika Westerberg #define PADCFG0_GPIORXSTATE		BIT(1)
697981c001SMika Westerberg #define PADCFG0_GPIOTXSTATE		BIT(0)
707981c001SMika Westerberg 
717981c001SMika Westerberg #define PADCFG1				0x004
727981c001SMika Westerberg #define PADCFG1_TERM_UP			BIT(13)
737981c001SMika Westerberg #define PADCFG1_TERM_SHIFT		10
74e58926e7SAndy Shevchenko #define PADCFG1_TERM_MASK		GENMASK(12, 10)
75dd26209bSAndy Shevchenko #define PADCFG1_TERM_20K		BIT(2)
76dd26209bSAndy Shevchenko #define PADCFG1_TERM_5K			BIT(1)
77dd26209bSAndy Shevchenko #define PADCFG1_TERM_1K			BIT(0)
78dd26209bSAndy Shevchenko #define PADCFG1_TERM_833		(BIT(1) | BIT(0))
797981c001SMika Westerberg 
80e57725eaSMika Westerberg #define PADCFG2				0x008
81e57725eaSMika Westerberg #define PADCFG2_DEBEN			BIT(0)
82e57725eaSMika Westerberg #define PADCFG2_DEBOUNCE_SHIFT		1
83e57725eaSMika Westerberg #define PADCFG2_DEBOUNCE_MASK		GENMASK(4, 1)
84e57725eaSMika Westerberg 
856a33a1d6SAndy Shevchenko #define DEBOUNCE_PERIOD_NSEC		31250
86e57725eaSMika Westerberg 
877981c001SMika Westerberg struct intel_pad_context {
887981c001SMika Westerberg 	u32 padcfg0;
897981c001SMika Westerberg 	u32 padcfg1;
90e57725eaSMika Westerberg 	u32 padcfg2;
917981c001SMika Westerberg };
927981c001SMika Westerberg 
937981c001SMika Westerberg struct intel_community_context {
947981c001SMika Westerberg 	u32 *intmask;
95a0a5f766SChris Chiu 	u32 *hostown;
967981c001SMika Westerberg };
977981c001SMika Westerberg 
987981c001SMika Westerberg #define pin_to_padno(c, p)	((p) - (c)->pin_base)
99919eb475SMika Westerberg #define padgroup_offset(g, p)	((p) - (g)->base)
1007981c001SMika Westerberg 
1017981c001SMika Westerberg static struct intel_community *intel_get_community(struct intel_pinctrl *pctrl,
10204035f7fSAndy Shevchenko 						   unsigned int pin)
1037981c001SMika Westerberg {
1047981c001SMika Westerberg 	struct intel_community *community;
1057981c001SMika Westerberg 	int i;
1067981c001SMika Westerberg 
1077981c001SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
1087981c001SMika Westerberg 		community = &pctrl->communities[i];
1097981c001SMika Westerberg 		if (pin >= community->pin_base &&
1107981c001SMika Westerberg 		    pin < community->pin_base + community->npins)
1117981c001SMika Westerberg 			return community;
1127981c001SMika Westerberg 	}
1137981c001SMika Westerberg 
1147981c001SMika Westerberg 	dev_warn(pctrl->dev, "failed to find community for pin %u\n", pin);
1157981c001SMika Westerberg 	return NULL;
1167981c001SMika Westerberg }
1177981c001SMika Westerberg 
118919eb475SMika Westerberg static const struct intel_padgroup *
119919eb475SMika Westerberg intel_community_get_padgroup(const struct intel_community *community,
12004035f7fSAndy Shevchenko 			     unsigned int pin)
121919eb475SMika Westerberg {
122919eb475SMika Westerberg 	int i;
123919eb475SMika Westerberg 
124919eb475SMika Westerberg 	for (i = 0; i < community->ngpps; i++) {
125919eb475SMika Westerberg 		const struct intel_padgroup *padgrp = &community->gpps[i];
126919eb475SMika Westerberg 
127919eb475SMika Westerberg 		if (pin >= padgrp->base && pin < padgrp->base + padgrp->size)
128919eb475SMika Westerberg 			return padgrp;
129919eb475SMika Westerberg 	}
130919eb475SMika Westerberg 
131919eb475SMika Westerberg 	return NULL;
132919eb475SMika Westerberg }
133919eb475SMika Westerberg 
13404035f7fSAndy Shevchenko static void __iomem *intel_get_padcfg(struct intel_pinctrl *pctrl,
13504035f7fSAndy Shevchenko 				      unsigned int pin, unsigned int reg)
1367981c001SMika Westerberg {
1377981c001SMika Westerberg 	const struct intel_community *community;
13804035f7fSAndy Shevchenko 	unsigned int padno;
139e57725eaSMika Westerberg 	size_t nregs;
1407981c001SMika Westerberg 
1417981c001SMika Westerberg 	community = intel_get_community(pctrl, pin);
1427981c001SMika Westerberg 	if (!community)
1437981c001SMika Westerberg 		return NULL;
1447981c001SMika Westerberg 
1457981c001SMika Westerberg 	padno = pin_to_padno(community, pin);
146e57725eaSMika Westerberg 	nregs = (community->features & PINCTRL_FEATURE_DEBOUNCE) ? 4 : 2;
147e57725eaSMika Westerberg 
1487eb7ecddSAndy Shevchenko 	if (reg >= nregs * 4)
149e57725eaSMika Westerberg 		return NULL;
150e57725eaSMika Westerberg 
151e57725eaSMika Westerberg 	return community->pad_regs + reg + padno * nregs * 4;
1527981c001SMika Westerberg }
1537981c001SMika Westerberg 
15404035f7fSAndy Shevchenko static bool intel_pad_owned_by_host(struct intel_pinctrl *pctrl, unsigned int pin)
1557981c001SMika Westerberg {
1567981c001SMika Westerberg 	const struct intel_community *community;
157919eb475SMika Westerberg 	const struct intel_padgroup *padgrp;
15804035f7fSAndy Shevchenko 	unsigned int gpp, offset, gpp_offset;
1597981c001SMika Westerberg 	void __iomem *padown;
1607981c001SMika Westerberg 
1617981c001SMika Westerberg 	community = intel_get_community(pctrl, pin);
1627981c001SMika Westerberg 	if (!community)
1637981c001SMika Westerberg 		return false;
1647981c001SMika Westerberg 	if (!community->padown_offset)
1657981c001SMika Westerberg 		return true;
1667981c001SMika Westerberg 
167919eb475SMika Westerberg 	padgrp = intel_community_get_padgroup(community, pin);
168919eb475SMika Westerberg 	if (!padgrp)
169919eb475SMika Westerberg 		return false;
170919eb475SMika Westerberg 
171919eb475SMika Westerberg 	gpp_offset = padgroup_offset(padgrp, pin);
172919eb475SMika Westerberg 	gpp = PADOWN_GPP(gpp_offset);
173919eb475SMika Westerberg 	offset = community->padown_offset + padgrp->padown_num * 4 + gpp * 4;
1747981c001SMika Westerberg 	padown = community->regs + offset;
1757981c001SMika Westerberg 
176919eb475SMika Westerberg 	return !(readl(padown) & PADOWN_MASK(gpp_offset));
1777981c001SMika Westerberg }
1787981c001SMika Westerberg 
17904035f7fSAndy Shevchenko static bool intel_pad_acpi_mode(struct intel_pinctrl *pctrl, unsigned int pin)
1807981c001SMika Westerberg {
1817981c001SMika Westerberg 	const struct intel_community *community;
182919eb475SMika Westerberg 	const struct intel_padgroup *padgrp;
18304035f7fSAndy Shevchenko 	unsigned int offset, gpp_offset;
1847981c001SMika Westerberg 	void __iomem *hostown;
1857981c001SMika Westerberg 
1867981c001SMika Westerberg 	community = intel_get_community(pctrl, pin);
1877981c001SMika Westerberg 	if (!community)
1887981c001SMika Westerberg 		return true;
1897981c001SMika Westerberg 	if (!community->hostown_offset)
1907981c001SMika Westerberg 		return false;
1917981c001SMika Westerberg 
192919eb475SMika Westerberg 	padgrp = intel_community_get_padgroup(community, pin);
193919eb475SMika Westerberg 	if (!padgrp)
194919eb475SMika Westerberg 		return true;
195919eb475SMika Westerberg 
196919eb475SMika Westerberg 	gpp_offset = padgroup_offset(padgrp, pin);
197919eb475SMika Westerberg 	offset = community->hostown_offset + padgrp->reg_num * 4;
1987981c001SMika Westerberg 	hostown = community->regs + offset;
1997981c001SMika Westerberg 
200919eb475SMika Westerberg 	return !(readl(hostown) & BIT(gpp_offset));
2017981c001SMika Westerberg }
2027981c001SMika Westerberg 
2031bd23153SAndy Shevchenko /**
2041bd23153SAndy Shevchenko  * enum - Locking variants of the pad configuration
2051bd23153SAndy Shevchenko  *
2061bd23153SAndy Shevchenko  * @PAD_UNLOCKED:	pad is fully controlled by the configuration registers
2071bd23153SAndy Shevchenko  * @PAD_LOCKED:		pad configuration registers, except TX state, are locked
2081bd23153SAndy Shevchenko  * @PAD_LOCKED_TX:	pad configuration TX state is locked
2091bd23153SAndy Shevchenko  * @PAD_LOCKED_FULL:	pad configuration registers are locked completely
2101bd23153SAndy Shevchenko  *
2111bd23153SAndy Shevchenko  * Locking is considered as read-only mode for corresponding registers and
2121bd23153SAndy Shevchenko  * their respective fields. That said, TX state bit is locked separately from
2131bd23153SAndy Shevchenko  * the main locking scheme.
2141bd23153SAndy Shevchenko  */
2151bd23153SAndy Shevchenko enum {
2161bd23153SAndy Shevchenko 	PAD_UNLOCKED	= 0,
2171bd23153SAndy Shevchenko 	PAD_LOCKED	= 1,
2181bd23153SAndy Shevchenko 	PAD_LOCKED_TX	= 2,
2191bd23153SAndy Shevchenko 	PAD_LOCKED_FULL	= PAD_LOCKED | PAD_LOCKED_TX,
2201bd23153SAndy Shevchenko };
2211bd23153SAndy Shevchenko 
2221bd23153SAndy Shevchenko static int intel_pad_locked(struct intel_pinctrl *pctrl, unsigned int pin)
2237981c001SMika Westerberg {
2247981c001SMika Westerberg 	struct intel_community *community;
225919eb475SMika Westerberg 	const struct intel_padgroup *padgrp;
22604035f7fSAndy Shevchenko 	unsigned int offset, gpp_offset;
2277981c001SMika Westerberg 	u32 value;
2281bd23153SAndy Shevchenko 	int ret = PAD_UNLOCKED;
2297981c001SMika Westerberg 
2307981c001SMika Westerberg 	community = intel_get_community(pctrl, pin);
2317981c001SMika Westerberg 	if (!community)
2321bd23153SAndy Shevchenko 		return PAD_LOCKED_FULL;
2337981c001SMika Westerberg 	if (!community->padcfglock_offset)
2341bd23153SAndy Shevchenko 		return PAD_UNLOCKED;
2357981c001SMika Westerberg 
236919eb475SMika Westerberg 	padgrp = intel_community_get_padgroup(community, pin);
237919eb475SMika Westerberg 	if (!padgrp)
2381bd23153SAndy Shevchenko 		return PAD_LOCKED_FULL;
239919eb475SMika Westerberg 
240919eb475SMika Westerberg 	gpp_offset = padgroup_offset(padgrp, pin);
2417981c001SMika Westerberg 
2427981c001SMika Westerberg 	/*
2437981c001SMika Westerberg 	 * If PADCFGLOCK and PADCFGLOCKTX bits are both clear for this pad,
2447981c001SMika Westerberg 	 * the pad is considered unlocked. Any other case means that it is
2451bd23153SAndy Shevchenko 	 * either fully or partially locked.
2467981c001SMika Westerberg 	 */
2471bd23153SAndy Shevchenko 	offset = community->padcfglock_offset + 0 + padgrp->reg_num * 8;
2487981c001SMika Westerberg 	value = readl(community->regs + offset);
249919eb475SMika Westerberg 	if (value & BIT(gpp_offset))
2501bd23153SAndy Shevchenko 		ret |= PAD_LOCKED;
2517981c001SMika Westerberg 
252919eb475SMika Westerberg 	offset = community->padcfglock_offset + 4 + padgrp->reg_num * 8;
2537981c001SMika Westerberg 	value = readl(community->regs + offset);
254919eb475SMika Westerberg 	if (value & BIT(gpp_offset))
2551bd23153SAndy Shevchenko 		ret |= PAD_LOCKED_TX;
2567981c001SMika Westerberg 
2571bd23153SAndy Shevchenko 	return ret;
2581bd23153SAndy Shevchenko }
2591bd23153SAndy Shevchenko 
2601bd23153SAndy Shevchenko static bool intel_pad_is_unlocked(struct intel_pinctrl *pctrl, unsigned int pin)
2611bd23153SAndy Shevchenko {
2621bd23153SAndy Shevchenko 	return (intel_pad_locked(pctrl, pin) & PAD_LOCKED) == PAD_UNLOCKED;
2637981c001SMika Westerberg }
2647981c001SMika Westerberg 
26504035f7fSAndy Shevchenko static bool intel_pad_usable(struct intel_pinctrl *pctrl, unsigned int pin)
2667981c001SMika Westerberg {
2671bd23153SAndy Shevchenko 	return intel_pad_owned_by_host(pctrl, pin) && intel_pad_is_unlocked(pctrl, pin);
2687981c001SMika Westerberg }
2697981c001SMika Westerberg 
2707981c001SMika Westerberg static int intel_get_groups_count(struct pinctrl_dev *pctldev)
2717981c001SMika Westerberg {
2727981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
2737981c001SMika Westerberg 
2747981c001SMika Westerberg 	return pctrl->soc->ngroups;
2757981c001SMika Westerberg }
2767981c001SMika Westerberg 
2777981c001SMika Westerberg static const char *intel_get_group_name(struct pinctrl_dev *pctldev,
27804035f7fSAndy Shevchenko 				      unsigned int group)
2797981c001SMika Westerberg {
2807981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
2817981c001SMika Westerberg 
2827981c001SMika Westerberg 	return pctrl->soc->groups[group].name;
2837981c001SMika Westerberg }
2847981c001SMika Westerberg 
28504035f7fSAndy Shevchenko static int intel_get_group_pins(struct pinctrl_dev *pctldev, unsigned int group,
28604035f7fSAndy Shevchenko 			      const unsigned int **pins, unsigned int *npins)
2877981c001SMika Westerberg {
2887981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
2897981c001SMika Westerberg 
2907981c001SMika Westerberg 	*pins = pctrl->soc->groups[group].pins;
2917981c001SMika Westerberg 	*npins = pctrl->soc->groups[group].npins;
2927981c001SMika Westerberg 	return 0;
2937981c001SMika Westerberg }
2947981c001SMika Westerberg 
2957981c001SMika Westerberg static void intel_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
29604035f7fSAndy Shevchenko 			       unsigned int pin)
2977981c001SMika Westerberg {
2987981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
299e57725eaSMika Westerberg 	void __iomem *padcfg;
3007981c001SMika Westerberg 	u32 cfg0, cfg1, mode;
3011bd23153SAndy Shevchenko 	int locked;
3021bd23153SAndy Shevchenko 	bool acpi;
3037981c001SMika Westerberg 
3047981c001SMika Westerberg 	if (!intel_pad_owned_by_host(pctrl, pin)) {
3057981c001SMika Westerberg 		seq_puts(s, "not available");
3067981c001SMika Westerberg 		return;
3077981c001SMika Westerberg 	}
3087981c001SMika Westerberg 
3097981c001SMika Westerberg 	cfg0 = readl(intel_get_padcfg(pctrl, pin, PADCFG0));
3107981c001SMika Westerberg 	cfg1 = readl(intel_get_padcfg(pctrl, pin, PADCFG1));
3117981c001SMika Westerberg 
3127981c001SMika Westerberg 	mode = (cfg0 & PADCFG0_PMODE_MASK) >> PADCFG0_PMODE_SHIFT;
3134973ddc8SAndy Shevchenko 	if (mode == PADCFG0_PMODE_GPIO)
3147981c001SMika Westerberg 		seq_puts(s, "GPIO ");
3157981c001SMika Westerberg 	else
3167981c001SMika Westerberg 		seq_printf(s, "mode %d ", mode);
3177981c001SMika Westerberg 
3187981c001SMika Westerberg 	seq_printf(s, "0x%08x 0x%08x", cfg0, cfg1);
3197981c001SMika Westerberg 
320e57725eaSMika Westerberg 	/* Dump the additional PADCFG registers if available */
321e57725eaSMika Westerberg 	padcfg = intel_get_padcfg(pctrl, pin, PADCFG2);
322e57725eaSMika Westerberg 	if (padcfg)
323e57725eaSMika Westerberg 		seq_printf(s, " 0x%08x", readl(padcfg));
324e57725eaSMika Westerberg 
3257981c001SMika Westerberg 	locked = intel_pad_locked(pctrl, pin);
3264341e8a5SMika Westerberg 	acpi = intel_pad_acpi_mode(pctrl, pin);
3277981c001SMika Westerberg 
3287981c001SMika Westerberg 	if (locked || acpi) {
3297981c001SMika Westerberg 		seq_puts(s, " [");
3301bd23153SAndy Shevchenko 		if (locked)
3317981c001SMika Westerberg 			seq_puts(s, "LOCKED");
3321bd23153SAndy Shevchenko 		if ((locked & PAD_LOCKED_FULL) == PAD_LOCKED_TX)
3331bd23153SAndy Shevchenko 			seq_puts(s, " tx");
3341bd23153SAndy Shevchenko 		else if ((locked & PAD_LOCKED_FULL) == PAD_LOCKED_FULL)
3351bd23153SAndy Shevchenko 			seq_puts(s, " full");
3361bd23153SAndy Shevchenko 
3371bd23153SAndy Shevchenko 		if (locked && acpi)
3387981c001SMika Westerberg 			seq_puts(s, ", ");
3391bd23153SAndy Shevchenko 
3407981c001SMika Westerberg 		if (acpi)
3417981c001SMika Westerberg 			seq_puts(s, "ACPI");
3427981c001SMika Westerberg 		seq_puts(s, "]");
3437981c001SMika Westerberg 	}
3447981c001SMika Westerberg }
3457981c001SMika Westerberg 
3467981c001SMika Westerberg static const struct pinctrl_ops intel_pinctrl_ops = {
3477981c001SMika Westerberg 	.get_groups_count = intel_get_groups_count,
3487981c001SMika Westerberg 	.get_group_name = intel_get_group_name,
3497981c001SMika Westerberg 	.get_group_pins = intel_get_group_pins,
3507981c001SMika Westerberg 	.pin_dbg_show = intel_pin_dbg_show,
3517981c001SMika Westerberg };
3527981c001SMika Westerberg 
3537981c001SMika Westerberg static int intel_get_functions_count(struct pinctrl_dev *pctldev)
3547981c001SMika Westerberg {
3557981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
3567981c001SMika Westerberg 
3577981c001SMika Westerberg 	return pctrl->soc->nfunctions;
3587981c001SMika Westerberg }
3597981c001SMika Westerberg 
3607981c001SMika Westerberg static const char *intel_get_function_name(struct pinctrl_dev *pctldev,
36104035f7fSAndy Shevchenko 					   unsigned int function)
3627981c001SMika Westerberg {
3637981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
3647981c001SMika Westerberg 
3657981c001SMika Westerberg 	return pctrl->soc->functions[function].name;
3667981c001SMika Westerberg }
3677981c001SMika Westerberg 
3687981c001SMika Westerberg static int intel_get_function_groups(struct pinctrl_dev *pctldev,
36904035f7fSAndy Shevchenko 				     unsigned int function,
3707981c001SMika Westerberg 				     const char * const **groups,
37104035f7fSAndy Shevchenko 				     unsigned int * const ngroups)
3727981c001SMika Westerberg {
3737981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
3747981c001SMika Westerberg 
3757981c001SMika Westerberg 	*groups = pctrl->soc->functions[function].groups;
3767981c001SMika Westerberg 	*ngroups = pctrl->soc->functions[function].ngroups;
3777981c001SMika Westerberg 	return 0;
3787981c001SMika Westerberg }
3797981c001SMika Westerberg 
38004035f7fSAndy Shevchenko static int intel_pinmux_set_mux(struct pinctrl_dev *pctldev,
38104035f7fSAndy Shevchenko 				unsigned int function, unsigned int group)
3827981c001SMika Westerberg {
3837981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
3847981c001SMika Westerberg 	const struct intel_pingroup *grp = &pctrl->soc->groups[group];
3857981c001SMika Westerberg 	unsigned long flags;
3867981c001SMika Westerberg 	int i;
3877981c001SMika Westerberg 
38827d9098cSMika Westerberg 	raw_spin_lock_irqsave(&pctrl->lock, flags);
3897981c001SMika Westerberg 
3907981c001SMika Westerberg 	/*
3917981c001SMika Westerberg 	 * All pins in the groups needs to be accessible and writable
3927981c001SMika Westerberg 	 * before we can enable the mux for this group.
3937981c001SMika Westerberg 	 */
3947981c001SMika Westerberg 	for (i = 0; i < grp->npins; i++) {
3957981c001SMika Westerberg 		if (!intel_pad_usable(pctrl, grp->pins[i])) {
39627d9098cSMika Westerberg 			raw_spin_unlock_irqrestore(&pctrl->lock, flags);
3977981c001SMika Westerberg 			return -EBUSY;
3987981c001SMika Westerberg 		}
3997981c001SMika Westerberg 	}
4007981c001SMika Westerberg 
4017981c001SMika Westerberg 	/* Now enable the mux setting for each pin in the group */
4027981c001SMika Westerberg 	for (i = 0; i < grp->npins; i++) {
4037981c001SMika Westerberg 		void __iomem *padcfg0;
4047981c001SMika Westerberg 		u32 value;
4057981c001SMika Westerberg 
4067981c001SMika Westerberg 		padcfg0 = intel_get_padcfg(pctrl, grp->pins[i], PADCFG0);
4077981c001SMika Westerberg 		value = readl(padcfg0);
4087981c001SMika Westerberg 
4097981c001SMika Westerberg 		value &= ~PADCFG0_PMODE_MASK;
4101f6b419bSMika Westerberg 
4111f6b419bSMika Westerberg 		if (grp->modes)
4121f6b419bSMika Westerberg 			value |= grp->modes[i] << PADCFG0_PMODE_SHIFT;
4131f6b419bSMika Westerberg 		else
4147981c001SMika Westerberg 			value |= grp->mode << PADCFG0_PMODE_SHIFT;
4157981c001SMika Westerberg 
4167981c001SMika Westerberg 		writel(value, padcfg0);
4177981c001SMika Westerberg 	}
4187981c001SMika Westerberg 
41927d9098cSMika Westerberg 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
4207981c001SMika Westerberg 
4217981c001SMika Westerberg 	return 0;
4227981c001SMika Westerberg }
4237981c001SMika Westerberg 
42417fab473SAndy Shevchenko static void __intel_gpio_set_direction(void __iomem *padcfg0, bool input)
42517fab473SAndy Shevchenko {
42617fab473SAndy Shevchenko 	u32 value;
42717fab473SAndy Shevchenko 
42817fab473SAndy Shevchenko 	value = readl(padcfg0);
42917fab473SAndy Shevchenko 	if (input) {
43017fab473SAndy Shevchenko 		value &= ~PADCFG0_GPIORXDIS;
43117fab473SAndy Shevchenko 		value |= PADCFG0_GPIOTXDIS;
43217fab473SAndy Shevchenko 	} else {
43317fab473SAndy Shevchenko 		value &= ~PADCFG0_GPIOTXDIS;
43417fab473SAndy Shevchenko 		value |= PADCFG0_GPIORXDIS;
43517fab473SAndy Shevchenko 	}
43617fab473SAndy Shevchenko 	writel(value, padcfg0);
43717fab473SAndy Shevchenko }
43817fab473SAndy Shevchenko 
4394973ddc8SAndy Shevchenko static int intel_gpio_get_gpio_mode(void __iomem *padcfg0)
4404973ddc8SAndy Shevchenko {
4414973ddc8SAndy Shevchenko 	return (readl(padcfg0) & PADCFG0_PMODE_MASK) >> PADCFG0_PMODE_SHIFT;
4424973ddc8SAndy Shevchenko }
4434973ddc8SAndy Shevchenko 
444f5a26acfSMika Westerberg static void intel_gpio_set_gpio_mode(void __iomem *padcfg0)
445f5a26acfSMika Westerberg {
446f5a26acfSMika Westerberg 	u32 value;
447f5a26acfSMika Westerberg 
448af7e3eebSAndy Shevchenko 	value = readl(padcfg0);
449af7e3eebSAndy Shevchenko 
450f5a26acfSMika Westerberg 	/* Put the pad into GPIO mode */
451af7e3eebSAndy Shevchenko 	value &= ~PADCFG0_PMODE_MASK;
452af7e3eebSAndy Shevchenko 	value |= PADCFG0_PMODE_GPIO;
453af7e3eebSAndy Shevchenko 
454af7e3eebSAndy Shevchenko 	/* Disable input and output buffers */
455e8873c0aSAndy Shevchenko 	value |= PADCFG0_GPIORXDIS;
456e8873c0aSAndy Shevchenko 	value |= PADCFG0_GPIOTXDIS;
457af7e3eebSAndy Shevchenko 
458f5a26acfSMika Westerberg 	/* Disable SCI/SMI/NMI generation */
459f5a26acfSMika Westerberg 	value &= ~(PADCFG0_GPIROUTIOXAPIC | PADCFG0_GPIROUTSCI);
460f5a26acfSMika Westerberg 	value &= ~(PADCFG0_GPIROUTSMI | PADCFG0_GPIROUTNMI);
461af7e3eebSAndy Shevchenko 
462f5a26acfSMika Westerberg 	writel(value, padcfg0);
463f5a26acfSMika Westerberg }
464f5a26acfSMika Westerberg 
4657981c001SMika Westerberg static int intel_gpio_request_enable(struct pinctrl_dev *pctldev,
4667981c001SMika Westerberg 				     struct pinctrl_gpio_range *range,
46704035f7fSAndy Shevchenko 				     unsigned int pin)
4687981c001SMika Westerberg {
4697981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
4707981c001SMika Westerberg 	void __iomem *padcfg0;
4717981c001SMika Westerberg 	unsigned long flags;
4727981c001SMika Westerberg 
473f62cdde5SAndy Shevchenko 	padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
474f62cdde5SAndy Shevchenko 
47527d9098cSMika Westerberg 	raw_spin_lock_irqsave(&pctrl->lock, flags);
4767981c001SMika Westerberg 
4771bd23153SAndy Shevchenko 	if (!intel_pad_owned_by_host(pctrl, pin)) {
47827d9098cSMika Westerberg 		raw_spin_unlock_irqrestore(&pctrl->lock, flags);
4797981c001SMika Westerberg 		return -EBUSY;
4807981c001SMika Westerberg 	}
4817981c001SMika Westerberg 
4821bd23153SAndy Shevchenko 	if (!intel_pad_is_unlocked(pctrl, pin)) {
4831bd23153SAndy Shevchenko 		raw_spin_unlock_irqrestore(&pctrl->lock, flags);
4841bd23153SAndy Shevchenko 		return 0;
4851bd23153SAndy Shevchenko 	}
4861bd23153SAndy Shevchenko 
4874973ddc8SAndy Shevchenko 	/*
4884973ddc8SAndy Shevchenko 	 * If pin is already configured in GPIO mode, we assume that
4894973ddc8SAndy Shevchenko 	 * firmware provides correct settings. In such case we avoid
4904973ddc8SAndy Shevchenko 	 * potential glitches on the pin. Otherwise, for the pin in
4914973ddc8SAndy Shevchenko 	 * alternative mode, consumer has to supply respective flags.
4924973ddc8SAndy Shevchenko 	 */
4934973ddc8SAndy Shevchenko 	if (intel_gpio_get_gpio_mode(padcfg0) == PADCFG0_PMODE_GPIO) {
4944973ddc8SAndy Shevchenko 		raw_spin_unlock_irqrestore(&pctrl->lock, flags);
4954973ddc8SAndy Shevchenko 		return 0;
4964973ddc8SAndy Shevchenko 	}
4974973ddc8SAndy Shevchenko 
498f5a26acfSMika Westerberg 	intel_gpio_set_gpio_mode(padcfg0);
4994973ddc8SAndy Shevchenko 
50017fab473SAndy Shevchenko 	/* Disable TX buffer and enable RX (this will be input) */
50117fab473SAndy Shevchenko 	__intel_gpio_set_direction(padcfg0, true);
50217fab473SAndy Shevchenko 
50327d9098cSMika Westerberg 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
5047981c001SMika Westerberg 
5057981c001SMika Westerberg 	return 0;
5067981c001SMika Westerberg }
5077981c001SMika Westerberg 
5087981c001SMika Westerberg static int intel_gpio_set_direction(struct pinctrl_dev *pctldev,
5097981c001SMika Westerberg 				    struct pinctrl_gpio_range *range,
51004035f7fSAndy Shevchenko 				    unsigned int pin, bool input)
5117981c001SMika Westerberg {
5127981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
5137981c001SMika Westerberg 	void __iomem *padcfg0;
5147981c001SMika Westerberg 	unsigned long flags;
5157981c001SMika Westerberg 
5167981c001SMika Westerberg 	padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
5177981c001SMika Westerberg 
518f62cdde5SAndy Shevchenko 	raw_spin_lock_irqsave(&pctrl->lock, flags);
519f62cdde5SAndy Shevchenko 	__intel_gpio_set_direction(padcfg0, input);
52027d9098cSMika Westerberg 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
5217981c001SMika Westerberg 
5227981c001SMika Westerberg 	return 0;
5237981c001SMika Westerberg }
5247981c001SMika Westerberg 
5257981c001SMika Westerberg static const struct pinmux_ops intel_pinmux_ops = {
5267981c001SMika Westerberg 	.get_functions_count = intel_get_functions_count,
5277981c001SMika Westerberg 	.get_function_name = intel_get_function_name,
5287981c001SMika Westerberg 	.get_function_groups = intel_get_function_groups,
5297981c001SMika Westerberg 	.set_mux = intel_pinmux_set_mux,
5307981c001SMika Westerberg 	.gpio_request_enable = intel_gpio_request_enable,
5317981c001SMika Westerberg 	.gpio_set_direction = intel_gpio_set_direction,
5327981c001SMika Westerberg };
5337981c001SMika Westerberg 
53481ab5542SAndy Shevchenko static int intel_config_get_pull(struct intel_pinctrl *pctrl, unsigned int pin,
53581ab5542SAndy Shevchenko 				 enum pin_config_param param, u32 *arg)
5367981c001SMika Westerberg {
53704cc058fSMika Westerberg 	const struct intel_community *community;
53881ab5542SAndy Shevchenko 	void __iomem *padcfg1;
539e64fbfa5SAndy Shevchenko 	unsigned long flags;
5407981c001SMika Westerberg 	u32 value, term;
5417981c001SMika Westerberg 
54204cc058fSMika Westerberg 	community = intel_get_community(pctrl, pin);
54381ab5542SAndy Shevchenko 	padcfg1 = intel_get_padcfg(pctrl, pin, PADCFG1);
544e64fbfa5SAndy Shevchenko 
545e64fbfa5SAndy Shevchenko 	raw_spin_lock_irqsave(&pctrl->lock, flags);
54681ab5542SAndy Shevchenko 	value = readl(padcfg1);
547e64fbfa5SAndy Shevchenko 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
54881ab5542SAndy Shevchenko 
5497981c001SMika Westerberg 	term = (value & PADCFG1_TERM_MASK) >> PADCFG1_TERM_SHIFT;
5507981c001SMika Westerberg 
5517981c001SMika Westerberg 	switch (param) {
5527981c001SMika Westerberg 	case PIN_CONFIG_BIAS_DISABLE:
5537981c001SMika Westerberg 		if (term)
5547981c001SMika Westerberg 			return -EINVAL;
5557981c001SMika Westerberg 		break;
5567981c001SMika Westerberg 
5577981c001SMika Westerberg 	case PIN_CONFIG_BIAS_PULL_UP:
5587981c001SMika Westerberg 		if (!term || !(value & PADCFG1_TERM_UP))
5597981c001SMika Westerberg 			return -EINVAL;
5607981c001SMika Westerberg 
5617981c001SMika Westerberg 		switch (term) {
562dd26209bSAndy Shevchenko 		case PADCFG1_TERM_833:
563dd26209bSAndy Shevchenko 			*arg = 833;
564dd26209bSAndy Shevchenko 			break;
5657981c001SMika Westerberg 		case PADCFG1_TERM_1K:
56681ab5542SAndy Shevchenko 			*arg = 1000;
5677981c001SMika Westerberg 			break;
5687981c001SMika Westerberg 		case PADCFG1_TERM_5K:
56981ab5542SAndy Shevchenko 			*arg = 5000;
5707981c001SMika Westerberg 			break;
5717981c001SMika Westerberg 		case PADCFG1_TERM_20K:
57281ab5542SAndy Shevchenko 			*arg = 20000;
5737981c001SMika Westerberg 			break;
5747981c001SMika Westerberg 		}
5757981c001SMika Westerberg 
5767981c001SMika Westerberg 		break;
5777981c001SMika Westerberg 
5787981c001SMika Westerberg 	case PIN_CONFIG_BIAS_PULL_DOWN:
5797981c001SMika Westerberg 		if (!term || value & PADCFG1_TERM_UP)
5807981c001SMika Westerberg 			return -EINVAL;
5817981c001SMika Westerberg 
5827981c001SMika Westerberg 		switch (term) {
583dd26209bSAndy Shevchenko 		case PADCFG1_TERM_833:
584dd26209bSAndy Shevchenko 			if (!(community->features & PINCTRL_FEATURE_1K_PD))
585dd26209bSAndy Shevchenko 				return -EINVAL;
586dd26209bSAndy Shevchenko 			*arg = 833;
587dd26209bSAndy Shevchenko 			break;
58804cc058fSMika Westerberg 		case PADCFG1_TERM_1K:
58904cc058fSMika Westerberg 			if (!(community->features & PINCTRL_FEATURE_1K_PD))
59004cc058fSMika Westerberg 				return -EINVAL;
59181ab5542SAndy Shevchenko 			*arg = 1000;
59204cc058fSMika Westerberg 			break;
5937981c001SMika Westerberg 		case PADCFG1_TERM_5K:
59481ab5542SAndy Shevchenko 			*arg = 5000;
5957981c001SMika Westerberg 			break;
5967981c001SMika Westerberg 		case PADCFG1_TERM_20K:
59781ab5542SAndy Shevchenko 			*arg = 20000;
5987981c001SMika Westerberg 			break;
5997981c001SMika Westerberg 		}
6007981c001SMika Westerberg 
6017981c001SMika Westerberg 		break;
6027981c001SMika Westerberg 
60381ab5542SAndy Shevchenko 	default:
60481ab5542SAndy Shevchenko 		return -EINVAL;
60581ab5542SAndy Shevchenko 	}
60681ab5542SAndy Shevchenko 
60781ab5542SAndy Shevchenko 	return 0;
60881ab5542SAndy Shevchenko }
60981ab5542SAndy Shevchenko 
61081ab5542SAndy Shevchenko static int intel_config_get_debounce(struct intel_pinctrl *pctrl, unsigned int pin,
61181ab5542SAndy Shevchenko 				     enum pin_config_param param, u32 *arg)
61281ab5542SAndy Shevchenko {
613e57725eaSMika Westerberg 	void __iomem *padcfg2;
614e64fbfa5SAndy Shevchenko 	unsigned long flags;
61581ab5542SAndy Shevchenko 	unsigned long v;
61681ab5542SAndy Shevchenko 	u32 value2;
617e57725eaSMika Westerberg 
618e57725eaSMika Westerberg 	padcfg2 = intel_get_padcfg(pctrl, pin, PADCFG2);
619e57725eaSMika Westerberg 	if (!padcfg2)
620e57725eaSMika Westerberg 		return -ENOTSUPP;
621e57725eaSMika Westerberg 
622e64fbfa5SAndy Shevchenko 	raw_spin_lock_irqsave(&pctrl->lock, flags);
62381ab5542SAndy Shevchenko 	value2 = readl(padcfg2);
624e64fbfa5SAndy Shevchenko 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
62581ab5542SAndy Shevchenko 	if (!(value2 & PADCFG2_DEBEN))
626e57725eaSMika Westerberg 		return -EINVAL;
627e57725eaSMika Westerberg 
62881ab5542SAndy Shevchenko 	v = (value2 & PADCFG2_DEBOUNCE_MASK) >> PADCFG2_DEBOUNCE_SHIFT;
62981ab5542SAndy Shevchenko 	*arg = BIT(v) * DEBOUNCE_PERIOD_NSEC / NSEC_PER_USEC;
630e57725eaSMika Westerberg 
63181ab5542SAndy Shevchenko 	return 0;
632e57725eaSMika Westerberg }
633e57725eaSMika Westerberg 
63481ab5542SAndy Shevchenko static int intel_config_get(struct pinctrl_dev *pctldev, unsigned int pin,
63581ab5542SAndy Shevchenko 			    unsigned long *config)
63681ab5542SAndy Shevchenko {
63781ab5542SAndy Shevchenko 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
63881ab5542SAndy Shevchenko 	enum pin_config_param param = pinconf_to_config_param(*config);
63981ab5542SAndy Shevchenko 	u32 arg = 0;
64081ab5542SAndy Shevchenko 	int ret;
64181ab5542SAndy Shevchenko 
64281ab5542SAndy Shevchenko 	if (!intel_pad_owned_by_host(pctrl, pin))
64381ab5542SAndy Shevchenko 		return -ENOTSUPP;
64481ab5542SAndy Shevchenko 
64581ab5542SAndy Shevchenko 	switch (param) {
64681ab5542SAndy Shevchenko 	case PIN_CONFIG_BIAS_DISABLE:
64781ab5542SAndy Shevchenko 	case PIN_CONFIG_BIAS_PULL_UP:
64881ab5542SAndy Shevchenko 	case PIN_CONFIG_BIAS_PULL_DOWN:
64981ab5542SAndy Shevchenko 		ret = intel_config_get_pull(pctrl, pin, param, &arg);
65081ab5542SAndy Shevchenko 		if (ret)
65181ab5542SAndy Shevchenko 			return ret;
65281ab5542SAndy Shevchenko 		break;
65381ab5542SAndy Shevchenko 
65481ab5542SAndy Shevchenko 	case PIN_CONFIG_INPUT_DEBOUNCE:
65581ab5542SAndy Shevchenko 		ret = intel_config_get_debounce(pctrl, pin, param, &arg);
65681ab5542SAndy Shevchenko 		if (ret)
65781ab5542SAndy Shevchenko 			return ret;
65881ab5542SAndy Shevchenko 		break;
65981ab5542SAndy Shevchenko 
6607981c001SMika Westerberg 	default:
6617981c001SMika Westerberg 		return -ENOTSUPP;
6627981c001SMika Westerberg 	}
6637981c001SMika Westerberg 
6647981c001SMika Westerberg 	*config = pinconf_to_config_packed(param, arg);
6657981c001SMika Westerberg 	return 0;
6667981c001SMika Westerberg }
6677981c001SMika Westerberg 
66804035f7fSAndy Shevchenko static int intel_config_set_pull(struct intel_pinctrl *pctrl, unsigned int pin,
6697981c001SMika Westerberg 				 unsigned long config)
6707981c001SMika Westerberg {
67104035f7fSAndy Shevchenko 	unsigned int param = pinconf_to_config_param(config);
67204035f7fSAndy Shevchenko 	unsigned int arg = pinconf_to_config_argument(config);
67304cc058fSMika Westerberg 	const struct intel_community *community;
6747981c001SMika Westerberg 	void __iomem *padcfg1;
6757981c001SMika Westerberg 	unsigned long flags;
6767981c001SMika Westerberg 	int ret = 0;
6777981c001SMika Westerberg 	u32 value;
6787981c001SMika Westerberg 
67904cc058fSMika Westerberg 	community = intel_get_community(pctrl, pin);
6807981c001SMika Westerberg 	padcfg1 = intel_get_padcfg(pctrl, pin, PADCFG1);
681f62cdde5SAndy Shevchenko 
682f62cdde5SAndy Shevchenko 	raw_spin_lock_irqsave(&pctrl->lock, flags);
683f62cdde5SAndy Shevchenko 
6847981c001SMika Westerberg 	value = readl(padcfg1);
6857981c001SMika Westerberg 
6867981c001SMika Westerberg 	switch (param) {
6877981c001SMika Westerberg 	case PIN_CONFIG_BIAS_DISABLE:
6887981c001SMika Westerberg 		value &= ~(PADCFG1_TERM_MASK | PADCFG1_TERM_UP);
6897981c001SMika Westerberg 		break;
6907981c001SMika Westerberg 
6917981c001SMika Westerberg 	case PIN_CONFIG_BIAS_PULL_UP:
6927981c001SMika Westerberg 		value &= ~PADCFG1_TERM_MASK;
6937981c001SMika Westerberg 
6947981c001SMika Westerberg 		value |= PADCFG1_TERM_UP;
6957981c001SMika Westerberg 
696f3c75e7aSAndy Shevchenko 		/* Set default strength value in case none is given */
697f3c75e7aSAndy Shevchenko 		if (arg == 1)
698f3c75e7aSAndy Shevchenko 			arg = 5000;
699f3c75e7aSAndy Shevchenko 
7007981c001SMika Westerberg 		switch (arg) {
7017981c001SMika Westerberg 		case 20000:
7027981c001SMika Westerberg 			value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT;
7037981c001SMika Westerberg 			break;
7047981c001SMika Westerberg 		case 5000:
7057981c001SMika Westerberg 			value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT;
7067981c001SMika Westerberg 			break;
7077981c001SMika Westerberg 		case 1000:
7087981c001SMika Westerberg 			value |= PADCFG1_TERM_1K << PADCFG1_TERM_SHIFT;
7097981c001SMika Westerberg 			break;
710dd26209bSAndy Shevchenko 		case 833:
711dd26209bSAndy Shevchenko 			value |= PADCFG1_TERM_833 << PADCFG1_TERM_SHIFT;
712dd26209bSAndy Shevchenko 			break;
7137981c001SMika Westerberg 		default:
7147981c001SMika Westerberg 			ret = -EINVAL;
7157981c001SMika Westerberg 		}
7167981c001SMika Westerberg 
7177981c001SMika Westerberg 		break;
7187981c001SMika Westerberg 
7197981c001SMika Westerberg 	case PIN_CONFIG_BIAS_PULL_DOWN:
7207981c001SMika Westerberg 		value &= ~(PADCFG1_TERM_UP | PADCFG1_TERM_MASK);
7217981c001SMika Westerberg 
722f3c75e7aSAndy Shevchenko 		/* Set default strength value in case none is given */
723f3c75e7aSAndy Shevchenko 		if (arg == 1)
724f3c75e7aSAndy Shevchenko 			arg = 5000;
725f3c75e7aSAndy Shevchenko 
7267981c001SMika Westerberg 		switch (arg) {
7277981c001SMika Westerberg 		case 20000:
7287981c001SMika Westerberg 			value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT;
7297981c001SMika Westerberg 			break;
7307981c001SMika Westerberg 		case 5000:
7317981c001SMika Westerberg 			value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT;
7327981c001SMika Westerberg 			break;
73304cc058fSMika Westerberg 		case 1000:
734aa1dd80fSDan Carpenter 			if (!(community->features & PINCTRL_FEATURE_1K_PD)) {
735aa1dd80fSDan Carpenter 				ret = -EINVAL;
736aa1dd80fSDan Carpenter 				break;
737aa1dd80fSDan Carpenter 			}
73804cc058fSMika Westerberg 			value |= PADCFG1_TERM_1K << PADCFG1_TERM_SHIFT;
73904cc058fSMika Westerberg 			break;
740dd26209bSAndy Shevchenko 		case 833:
741dd26209bSAndy Shevchenko 			if (!(community->features & PINCTRL_FEATURE_1K_PD)) {
742dd26209bSAndy Shevchenko 				ret = -EINVAL;
743dd26209bSAndy Shevchenko 				break;
744dd26209bSAndy Shevchenko 			}
745dd26209bSAndy Shevchenko 			value |= PADCFG1_TERM_833 << PADCFG1_TERM_SHIFT;
746dd26209bSAndy Shevchenko 			break;
7477981c001SMika Westerberg 		default:
7487981c001SMika Westerberg 			ret = -EINVAL;
7497981c001SMika Westerberg 		}
7507981c001SMika Westerberg 
7517981c001SMika Westerberg 		break;
7527981c001SMika Westerberg 	}
7537981c001SMika Westerberg 
7547981c001SMika Westerberg 	if (!ret)
7557981c001SMika Westerberg 		writel(value, padcfg1);
7567981c001SMika Westerberg 
75727d9098cSMika Westerberg 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
7587981c001SMika Westerberg 
7597981c001SMika Westerberg 	return ret;
7607981c001SMika Westerberg }
7617981c001SMika Westerberg 
76204035f7fSAndy Shevchenko static int intel_config_set_debounce(struct intel_pinctrl *pctrl,
76304035f7fSAndy Shevchenko 				     unsigned int pin, unsigned int debounce)
764e57725eaSMika Westerberg {
765e57725eaSMika Westerberg 	void __iomem *padcfg0, *padcfg2;
766e57725eaSMika Westerberg 	unsigned long flags;
767e57725eaSMika Westerberg 	u32 value0, value2;
768e57725eaSMika Westerberg 
769e57725eaSMika Westerberg 	padcfg2 = intel_get_padcfg(pctrl, pin, PADCFG2);
770e57725eaSMika Westerberg 	if (!padcfg2)
771e57725eaSMika Westerberg 		return -ENOTSUPP;
772e57725eaSMika Westerberg 
773e57725eaSMika Westerberg 	padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
774e57725eaSMika Westerberg 
775e57725eaSMika Westerberg 	raw_spin_lock_irqsave(&pctrl->lock, flags);
776e57725eaSMika Westerberg 
777e57725eaSMika Westerberg 	value0 = readl(padcfg0);
778e57725eaSMika Westerberg 	value2 = readl(padcfg2);
779e57725eaSMika Westerberg 
780e57725eaSMika Westerberg 	/* Disable glitch filter and debouncer */
781e57725eaSMika Westerberg 	value0 &= ~PADCFG0_PREGFRXSEL;
782e57725eaSMika Westerberg 	value2 &= ~(PADCFG2_DEBEN | PADCFG2_DEBOUNCE_MASK);
783e57725eaSMika Westerberg 
784e57725eaSMika Westerberg 	if (debounce) {
785e57725eaSMika Westerberg 		unsigned long v;
786e57725eaSMika Westerberg 
7876a33a1d6SAndy Shevchenko 		v = order_base_2(debounce * NSEC_PER_USEC / DEBOUNCE_PERIOD_NSEC);
788e57725eaSMika Westerberg 		if (v < 3 || v > 15) {
7898fff0427SAndy Shevchenko 			raw_spin_unlock_irqrestore(&pctrl->lock, flags);
7908fff0427SAndy Shevchenko 			return -EINVAL;
791bb2f43d4SAndy Shevchenko 		}
792bb2f43d4SAndy Shevchenko 
793e57725eaSMika Westerberg 		/* Enable glitch filter and debouncer */
794e57725eaSMika Westerberg 		value0 |= PADCFG0_PREGFRXSEL;
795e57725eaSMika Westerberg 		value2 |= v << PADCFG2_DEBOUNCE_SHIFT;
796e57725eaSMika Westerberg 		value2 |= PADCFG2_DEBEN;
797e57725eaSMika Westerberg 	}
798e57725eaSMika Westerberg 
799e57725eaSMika Westerberg 	writel(value0, padcfg0);
800e57725eaSMika Westerberg 	writel(value2, padcfg2);
801e57725eaSMika Westerberg 
802e57725eaSMika Westerberg 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
803e57725eaSMika Westerberg 
8048fff0427SAndy Shevchenko 	return 0;
805e57725eaSMika Westerberg }
806e57725eaSMika Westerberg 
80704035f7fSAndy Shevchenko static int intel_config_set(struct pinctrl_dev *pctldev, unsigned int pin,
80804035f7fSAndy Shevchenko 			  unsigned long *configs, unsigned int nconfigs)
8097981c001SMika Westerberg {
8107981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
8117981c001SMika Westerberg 	int i, ret;
8127981c001SMika Westerberg 
8137981c001SMika Westerberg 	if (!intel_pad_usable(pctrl, pin))
8147981c001SMika Westerberg 		return -ENOTSUPP;
8157981c001SMika Westerberg 
8167981c001SMika Westerberg 	for (i = 0; i < nconfigs; i++) {
8177981c001SMika Westerberg 		switch (pinconf_to_config_param(configs[i])) {
8187981c001SMika Westerberg 		case PIN_CONFIG_BIAS_DISABLE:
8197981c001SMika Westerberg 		case PIN_CONFIG_BIAS_PULL_UP:
8207981c001SMika Westerberg 		case PIN_CONFIG_BIAS_PULL_DOWN:
8217981c001SMika Westerberg 			ret = intel_config_set_pull(pctrl, pin, configs[i]);
8227981c001SMika Westerberg 			if (ret)
8237981c001SMika Westerberg 				return ret;
8247981c001SMika Westerberg 			break;
8257981c001SMika Westerberg 
826e57725eaSMika Westerberg 		case PIN_CONFIG_INPUT_DEBOUNCE:
827e57725eaSMika Westerberg 			ret = intel_config_set_debounce(pctrl, pin,
828e57725eaSMika Westerberg 				pinconf_to_config_argument(configs[i]));
829e57725eaSMika Westerberg 			if (ret)
830e57725eaSMika Westerberg 				return ret;
831e57725eaSMika Westerberg 			break;
832e57725eaSMika Westerberg 
8337981c001SMika Westerberg 		default:
8347981c001SMika Westerberg 			return -ENOTSUPP;
8357981c001SMika Westerberg 		}
8367981c001SMika Westerberg 	}
8377981c001SMika Westerberg 
8387981c001SMika Westerberg 	return 0;
8397981c001SMika Westerberg }
8407981c001SMika Westerberg 
8417981c001SMika Westerberg static const struct pinconf_ops intel_pinconf_ops = {
8427981c001SMika Westerberg 	.is_generic = true,
8437981c001SMika Westerberg 	.pin_config_get = intel_config_get,
8447981c001SMika Westerberg 	.pin_config_set = intel_config_set,
8457981c001SMika Westerberg };
8467981c001SMika Westerberg 
8477981c001SMika Westerberg static const struct pinctrl_desc intel_pinctrl_desc = {
8487981c001SMika Westerberg 	.pctlops = &intel_pinctrl_ops,
8497981c001SMika Westerberg 	.pmxops = &intel_pinmux_ops,
8507981c001SMika Westerberg 	.confops = &intel_pinconf_ops,
8517981c001SMika Westerberg 	.owner = THIS_MODULE,
8527981c001SMika Westerberg };
8537981c001SMika Westerberg 
854a60eac32SMika Westerberg /**
855a60eac32SMika Westerberg  * intel_gpio_to_pin() - Translate from GPIO offset to pin number
856a60eac32SMika Westerberg  * @pctrl: Pinctrl structure
857a60eac32SMika Westerberg  * @offset: GPIO offset from gpiolib
858946ffefcSAndy Shevchenko  * @community: Community is filled here if not %NULL
859a60eac32SMika Westerberg  * @padgrp: Pad group is filled here if not %NULL
860a60eac32SMika Westerberg  *
861a60eac32SMika Westerberg  * When coming through gpiolib irqchip, the GPIO offset is not
862a60eac32SMika Westerberg  * automatically translated to pinctrl pin number. This function can be
863a60eac32SMika Westerberg  * used to find out the corresponding pinctrl pin.
864a60eac32SMika Westerberg  */
86504035f7fSAndy Shevchenko static int intel_gpio_to_pin(struct intel_pinctrl *pctrl, unsigned int offset,
866a60eac32SMika Westerberg 			     const struct intel_community **community,
867a60eac32SMika Westerberg 			     const struct intel_padgroup **padgrp)
868a60eac32SMika Westerberg {
869a60eac32SMika Westerberg 	int i;
870a60eac32SMika Westerberg 
871a60eac32SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
872a60eac32SMika Westerberg 		const struct intel_community *comm = &pctrl->communities[i];
873a60eac32SMika Westerberg 		int j;
874a60eac32SMika Westerberg 
875a60eac32SMika Westerberg 		for (j = 0; j < comm->ngpps; j++) {
876a60eac32SMika Westerberg 			const struct intel_padgroup *pgrp = &comm->gpps[j];
877a60eac32SMika Westerberg 
878e5a4ab6aSAndy Shevchenko 			if (pgrp->gpio_base == INTEL_GPIO_BASE_NOMAP)
879a60eac32SMika Westerberg 				continue;
880a60eac32SMika Westerberg 
881a60eac32SMika Westerberg 			if (offset >= pgrp->gpio_base &&
882a60eac32SMika Westerberg 			    offset < pgrp->gpio_base + pgrp->size) {
883a60eac32SMika Westerberg 				int pin;
884a60eac32SMika Westerberg 
885a60eac32SMika Westerberg 				pin = pgrp->base + offset - pgrp->gpio_base;
886a60eac32SMika Westerberg 				if (community)
887a60eac32SMika Westerberg 					*community = comm;
888a60eac32SMika Westerberg 				if (padgrp)
889a60eac32SMika Westerberg 					*padgrp = pgrp;
890a60eac32SMika Westerberg 
891a60eac32SMika Westerberg 				return pin;
892a60eac32SMika Westerberg 			}
893a60eac32SMika Westerberg 		}
894a60eac32SMika Westerberg 	}
895a60eac32SMika Westerberg 
896a60eac32SMika Westerberg 	return -EINVAL;
897a60eac32SMika Westerberg }
898a60eac32SMika Westerberg 
8996cb0880fSChris Chiu /**
9006cb0880fSChris Chiu  * intel_pin_to_gpio() - Translate from pin number to GPIO offset
9016cb0880fSChris Chiu  * @pctrl: Pinctrl structure
9026cb0880fSChris Chiu  * @pin: pin number
9036cb0880fSChris Chiu  *
9046cb0880fSChris Chiu  * Translate the pin number of pinctrl to GPIO offset
9056cb0880fSChris Chiu  */
90655dac437SArnd Bergmann static __maybe_unused int intel_pin_to_gpio(struct intel_pinctrl *pctrl, int pin)
9076cb0880fSChris Chiu {
9086cb0880fSChris Chiu 	const struct intel_community *community;
9096cb0880fSChris Chiu 	const struct intel_padgroup *padgrp;
9106cb0880fSChris Chiu 
9116cb0880fSChris Chiu 	community = intel_get_community(pctrl, pin);
9126cb0880fSChris Chiu 	if (!community)
9136cb0880fSChris Chiu 		return -EINVAL;
9146cb0880fSChris Chiu 
9156cb0880fSChris Chiu 	padgrp = intel_community_get_padgroup(community, pin);
9166cb0880fSChris Chiu 	if (!padgrp)
9176cb0880fSChris Chiu 		return -EINVAL;
9186cb0880fSChris Chiu 
9196cb0880fSChris Chiu 	return pin - padgrp->base + padgrp->gpio_base;
9206cb0880fSChris Chiu }
9216cb0880fSChris Chiu 
92204035f7fSAndy Shevchenko static int intel_gpio_get(struct gpio_chip *chip, unsigned int offset)
92355aedef5SAndy Shevchenko {
92496147db1SMika Westerberg 	struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
92596147db1SMika Westerberg 	void __iomem *reg;
92696147db1SMika Westerberg 	u32 padcfg0;
92755aedef5SAndy Shevchenko 	int pin;
92855aedef5SAndy Shevchenko 
92996147db1SMika Westerberg 	pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL);
93096147db1SMika Westerberg 	if (pin < 0)
93196147db1SMika Westerberg 		return -EINVAL;
93296147db1SMika Westerberg 
93396147db1SMika Westerberg 	reg = intel_get_padcfg(pctrl, pin, PADCFG0);
93496147db1SMika Westerberg 	if (!reg)
93596147db1SMika Westerberg 		return -EINVAL;
93696147db1SMika Westerberg 
93796147db1SMika Westerberg 	padcfg0 = readl(reg);
93896147db1SMika Westerberg 	if (!(padcfg0 & PADCFG0_GPIOTXDIS))
93996147db1SMika Westerberg 		return !!(padcfg0 & PADCFG0_GPIOTXSTATE);
94096147db1SMika Westerberg 
94196147db1SMika Westerberg 	return !!(padcfg0 & PADCFG0_GPIORXSTATE);
94255aedef5SAndy Shevchenko }
94355aedef5SAndy Shevchenko 
94404035f7fSAndy Shevchenko static void intel_gpio_set(struct gpio_chip *chip, unsigned int offset,
94504035f7fSAndy Shevchenko 			   int value)
94696147db1SMika Westerberg {
94796147db1SMika Westerberg 	struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
94896147db1SMika Westerberg 	unsigned long flags;
94996147db1SMika Westerberg 	void __iomem *reg;
95096147db1SMika Westerberg 	u32 padcfg0;
95196147db1SMika Westerberg 	int pin;
95296147db1SMika Westerberg 
95396147db1SMika Westerberg 	pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL);
95496147db1SMika Westerberg 	if (pin < 0)
95596147db1SMika Westerberg 		return;
95696147db1SMika Westerberg 
95796147db1SMika Westerberg 	reg = intel_get_padcfg(pctrl, pin, PADCFG0);
95896147db1SMika Westerberg 	if (!reg)
95996147db1SMika Westerberg 		return;
96096147db1SMika Westerberg 
96196147db1SMika Westerberg 	raw_spin_lock_irqsave(&pctrl->lock, flags);
96296147db1SMika Westerberg 	padcfg0 = readl(reg);
96396147db1SMika Westerberg 	if (value)
96496147db1SMika Westerberg 		padcfg0 |= PADCFG0_GPIOTXSTATE;
96596147db1SMika Westerberg 	else
96696147db1SMika Westerberg 		padcfg0 &= ~PADCFG0_GPIOTXSTATE;
96796147db1SMika Westerberg 	writel(padcfg0, reg);
96896147db1SMika Westerberg 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
96996147db1SMika Westerberg }
97096147db1SMika Westerberg 
97196147db1SMika Westerberg static int intel_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
97296147db1SMika Westerberg {
97396147db1SMika Westerberg 	struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
974e64fbfa5SAndy Shevchenko 	unsigned long flags;
97596147db1SMika Westerberg 	void __iomem *reg;
97696147db1SMika Westerberg 	u32 padcfg0;
97796147db1SMika Westerberg 	int pin;
97896147db1SMika Westerberg 
97996147db1SMika Westerberg 	pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL);
98096147db1SMika Westerberg 	if (pin < 0)
98196147db1SMika Westerberg 		return -EINVAL;
98296147db1SMika Westerberg 
98396147db1SMika Westerberg 	reg = intel_get_padcfg(pctrl, pin, PADCFG0);
98496147db1SMika Westerberg 	if (!reg)
98596147db1SMika Westerberg 		return -EINVAL;
98696147db1SMika Westerberg 
987e64fbfa5SAndy Shevchenko 	raw_spin_lock_irqsave(&pctrl->lock, flags);
98896147db1SMika Westerberg 	padcfg0 = readl(reg);
989e64fbfa5SAndy Shevchenko 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
99096147db1SMika Westerberg 	if (padcfg0 & PADCFG0_PMODE_MASK)
99196147db1SMika Westerberg 		return -EINVAL;
99296147db1SMika Westerberg 
9936a304752SMatti Vaittinen 	if (padcfg0 & PADCFG0_GPIOTXDIS)
9946a304752SMatti Vaittinen 		return GPIO_LINE_DIRECTION_IN;
9956a304752SMatti Vaittinen 
9966a304752SMatti Vaittinen 	return GPIO_LINE_DIRECTION_OUT;
99796147db1SMika Westerberg }
99896147db1SMika Westerberg 
99904035f7fSAndy Shevchenko static int intel_gpio_direction_input(struct gpio_chip *chip, unsigned int offset)
100096147db1SMika Westerberg {
100196147db1SMika Westerberg 	return pinctrl_gpio_direction_input(chip->base + offset);
100296147db1SMika Westerberg }
100396147db1SMika Westerberg 
100404035f7fSAndy Shevchenko static int intel_gpio_direction_output(struct gpio_chip *chip, unsigned int offset,
100596147db1SMika Westerberg 				       int value)
100696147db1SMika Westerberg {
100796147db1SMika Westerberg 	intel_gpio_set(chip, offset, value);
100896147db1SMika Westerberg 	return pinctrl_gpio_direction_output(chip->base + offset);
100996147db1SMika Westerberg }
101096147db1SMika Westerberg 
101196147db1SMika Westerberg static const struct gpio_chip intel_gpio_chip = {
101296147db1SMika Westerberg 	.owner = THIS_MODULE,
101396147db1SMika Westerberg 	.request = gpiochip_generic_request,
101496147db1SMika Westerberg 	.free = gpiochip_generic_free,
101596147db1SMika Westerberg 	.get_direction = intel_gpio_get_direction,
101696147db1SMika Westerberg 	.direction_input = intel_gpio_direction_input,
101796147db1SMika Westerberg 	.direction_output = intel_gpio_direction_output,
101896147db1SMika Westerberg 	.get = intel_gpio_get,
101996147db1SMika Westerberg 	.set = intel_gpio_set,
102096147db1SMika Westerberg 	.set_config = gpiochip_generic_config,
102196147db1SMika Westerberg };
102296147db1SMika Westerberg 
10237981c001SMika Westerberg static void intel_gpio_irq_ack(struct irq_data *d)
10247981c001SMika Westerberg {
10257981c001SMika Westerberg 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1026acfd4c63SLinus Walleij 	struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
10277981c001SMika Westerberg 	const struct intel_community *community;
1028919eb475SMika Westerberg 	const struct intel_padgroup *padgrp;
1029a60eac32SMika Westerberg 	int pin;
10307981c001SMika Westerberg 
1031a60eac32SMika Westerberg 	pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), &community, &padgrp);
1032a60eac32SMika Westerberg 	if (pin >= 0) {
103304035f7fSAndy Shevchenko 		unsigned int gpp, gpp_offset, is_offset;
1034919eb475SMika Westerberg 
1035919eb475SMika Westerberg 		gpp = padgrp->reg_num;
1036919eb475SMika Westerberg 		gpp_offset = padgroup_offset(padgrp, pin);
1037cf769bd8SMika Westerberg 		is_offset = community->is_offset + gpp * 4;
1038919eb475SMika Westerberg 
1039919eb475SMika Westerberg 		raw_spin_lock(&pctrl->lock);
1040cf769bd8SMika Westerberg 		writel(BIT(gpp_offset), community->regs + is_offset);
104127d9098cSMika Westerberg 		raw_spin_unlock(&pctrl->lock);
10427981c001SMika Westerberg 	}
1043919eb475SMika Westerberg }
10447981c001SMika Westerberg 
10457981c001SMika Westerberg static void intel_gpio_irq_mask_unmask(struct irq_data *d, bool mask)
10467981c001SMika Westerberg {
10477981c001SMika Westerberg 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1048acfd4c63SLinus Walleij 	struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
10497981c001SMika Westerberg 	const struct intel_community *community;
1050919eb475SMika Westerberg 	const struct intel_padgroup *padgrp;
1051a60eac32SMika Westerberg 	int pin;
1052a60eac32SMika Westerberg 
1053a60eac32SMika Westerberg 	pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), &community, &padgrp);
1054a60eac32SMika Westerberg 	if (pin >= 0) {
105504035f7fSAndy Shevchenko 		unsigned int gpp, gpp_offset;
1056919eb475SMika Westerberg 		unsigned long flags;
1057670784fbSKai-Heng Feng 		void __iomem *reg, *is;
10587981c001SMika Westerberg 		u32 value;
10597981c001SMika Westerberg 
1060919eb475SMika Westerberg 		gpp = padgrp->reg_num;
1061919eb475SMika Westerberg 		gpp_offset = padgroup_offset(padgrp, pin);
1062919eb475SMika Westerberg 
10637981c001SMika Westerberg 		reg = community->regs + community->ie_offset + gpp * 4;
1064670784fbSKai-Heng Feng 		is = community->regs + community->is_offset + gpp * 4;
1065919eb475SMika Westerberg 
1066919eb475SMika Westerberg 		raw_spin_lock_irqsave(&pctrl->lock, flags);
1067670784fbSKai-Heng Feng 
1068670784fbSKai-Heng Feng 		/* Clear interrupt status first to avoid unexpected interrupt */
1069670784fbSKai-Heng Feng 		writel(BIT(gpp_offset), is);
1070670784fbSKai-Heng Feng 
10717981c001SMika Westerberg 		value = readl(reg);
10727981c001SMika Westerberg 		if (mask)
10737981c001SMika Westerberg 			value &= ~BIT(gpp_offset);
10747981c001SMika Westerberg 		else
10757981c001SMika Westerberg 			value |= BIT(gpp_offset);
10767981c001SMika Westerberg 		writel(value, reg);
107727d9098cSMika Westerberg 		raw_spin_unlock_irqrestore(&pctrl->lock, flags);
10787981c001SMika Westerberg 	}
1079919eb475SMika Westerberg }
10807981c001SMika Westerberg 
10817981c001SMika Westerberg static void intel_gpio_irq_mask(struct irq_data *d)
10827981c001SMika Westerberg {
10837981c001SMika Westerberg 	intel_gpio_irq_mask_unmask(d, true);
10847981c001SMika Westerberg }
10857981c001SMika Westerberg 
10867981c001SMika Westerberg static void intel_gpio_irq_unmask(struct irq_data *d)
10877981c001SMika Westerberg {
10887981c001SMika Westerberg 	intel_gpio_irq_mask_unmask(d, false);
10897981c001SMika Westerberg }
10907981c001SMika Westerberg 
109104035f7fSAndy Shevchenko static int intel_gpio_irq_type(struct irq_data *d, unsigned int type)
10927981c001SMika Westerberg {
10937981c001SMika Westerberg 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1094acfd4c63SLinus Walleij 	struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
109504035f7fSAndy Shevchenko 	unsigned int pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), NULL, NULL);
10967981c001SMika Westerberg 	unsigned long flags;
10977981c001SMika Westerberg 	void __iomem *reg;
10987981c001SMika Westerberg 	u32 value;
10997981c001SMika Westerberg 
11007981c001SMika Westerberg 	reg = intel_get_padcfg(pctrl, pin, PADCFG0);
11017981c001SMika Westerberg 	if (!reg)
11027981c001SMika Westerberg 		return -EINVAL;
11037981c001SMika Westerberg 
11044341e8a5SMika Westerberg 	/*
11054341e8a5SMika Westerberg 	 * If the pin is in ACPI mode it is still usable as a GPIO but it
11064341e8a5SMika Westerberg 	 * cannot be used as IRQ because GPI_IS status bit will not be
11074341e8a5SMika Westerberg 	 * updated by the host controller hardware.
11084341e8a5SMika Westerberg 	 */
11094341e8a5SMika Westerberg 	if (intel_pad_acpi_mode(pctrl, pin)) {
11104341e8a5SMika Westerberg 		dev_warn(pctrl->dev, "pin %u cannot be used as IRQ\n", pin);
11114341e8a5SMika Westerberg 		return -EPERM;
11124341e8a5SMika Westerberg 	}
11134341e8a5SMika Westerberg 
111427d9098cSMika Westerberg 	raw_spin_lock_irqsave(&pctrl->lock, flags);
11157981c001SMika Westerberg 
1116f5a26acfSMika Westerberg 	intel_gpio_set_gpio_mode(reg);
1117f5a26acfSMika Westerberg 
1118af7e3eebSAndy Shevchenko 	/* Disable TX buffer and enable RX (this will be input) */
1119af7e3eebSAndy Shevchenko 	__intel_gpio_set_direction(reg, true);
1120af7e3eebSAndy Shevchenko 
11217981c001SMika Westerberg 	value = readl(reg);
11227981c001SMika Westerberg 
11237981c001SMika Westerberg 	value &= ~(PADCFG0_RXEVCFG_MASK | PADCFG0_RXINV);
11247981c001SMika Westerberg 
11257981c001SMika Westerberg 	if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
11267981c001SMika Westerberg 		value |= PADCFG0_RXEVCFG_EDGE_BOTH << PADCFG0_RXEVCFG_SHIFT;
11277981c001SMika Westerberg 	} else if (type & IRQ_TYPE_EDGE_FALLING) {
11287981c001SMika Westerberg 		value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT;
11297981c001SMika Westerberg 		value |= PADCFG0_RXINV;
11307981c001SMika Westerberg 	} else if (type & IRQ_TYPE_EDGE_RISING) {
11317981c001SMika Westerberg 		value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT;
1132bf380cfaSQipeng Zha 	} else if (type & IRQ_TYPE_LEVEL_MASK) {
1133bf380cfaSQipeng Zha 		if (type & IRQ_TYPE_LEVEL_LOW)
11347981c001SMika Westerberg 			value |= PADCFG0_RXINV;
11357981c001SMika Westerberg 	} else {
11367981c001SMika Westerberg 		value |= PADCFG0_RXEVCFG_DISABLED << PADCFG0_RXEVCFG_SHIFT;
11377981c001SMika Westerberg 	}
11387981c001SMika Westerberg 
11397981c001SMika Westerberg 	writel(value, reg);
11407981c001SMika Westerberg 
11417981c001SMika Westerberg 	if (type & IRQ_TYPE_EDGE_BOTH)
1142fc756bcdSThomas Gleixner 		irq_set_handler_locked(d, handle_edge_irq);
11437981c001SMika Westerberg 	else if (type & IRQ_TYPE_LEVEL_MASK)
1144fc756bcdSThomas Gleixner 		irq_set_handler_locked(d, handle_level_irq);
11457981c001SMika Westerberg 
114627d9098cSMika Westerberg 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
11477981c001SMika Westerberg 
11487981c001SMika Westerberg 	return 0;
11497981c001SMika Westerberg }
11507981c001SMika Westerberg 
11517981c001SMika Westerberg static int intel_gpio_irq_wake(struct irq_data *d, unsigned int on)
11527981c001SMika Westerberg {
11537981c001SMika Westerberg 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1154acfd4c63SLinus Walleij 	struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
115504035f7fSAndy Shevchenko 	unsigned int pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), NULL, NULL);
11567981c001SMika Westerberg 
11577981c001SMika Westerberg 	if (on)
115801dabe91SNilesh Bacchewar 		enable_irq_wake(pctrl->irq);
11597981c001SMika Westerberg 	else
116001dabe91SNilesh Bacchewar 		disable_irq_wake(pctrl->irq);
11619a520fd9SAndy Shevchenko 
11627981c001SMika Westerberg 	dev_dbg(pctrl->dev, "%sable wake for pin %u\n", on ? "en" : "dis", pin);
11637981c001SMika Westerberg 	return 0;
11647981c001SMika Westerberg }
11657981c001SMika Westerberg 
116686851bbcSAndy Shevchenko static int intel_gpio_community_irq_handler(struct intel_pinctrl *pctrl,
11677981c001SMika Westerberg 					    const struct intel_community *community)
11687981c001SMika Westerberg {
1169193b40c8SMika Westerberg 	struct gpio_chip *gc = &pctrl->chip;
117086851bbcSAndy Shevchenko 	unsigned int gpp;
117186851bbcSAndy Shevchenko 	int ret = 0;
11727981c001SMika Westerberg 
11737981c001SMika Westerberg 	for (gpp = 0; gpp < community->ngpps; gpp++) {
1174919eb475SMika Westerberg 		const struct intel_padgroup *padgrp = &community->gpps[gpp];
11757981c001SMika Westerberg 		unsigned long pending, enabled, gpp_offset;
1176e64fbfa5SAndy Shevchenko 		unsigned long flags;
1177e64fbfa5SAndy Shevchenko 
1178e64fbfa5SAndy Shevchenko 		raw_spin_lock_irqsave(&pctrl->lock, flags);
11797981c001SMika Westerberg 
1180cf769bd8SMika Westerberg 		pending = readl(community->regs + community->is_offset +
1181cf769bd8SMika Westerberg 				padgrp->reg_num * 4);
11827981c001SMika Westerberg 		enabled = readl(community->regs + community->ie_offset +
1183919eb475SMika Westerberg 				padgrp->reg_num * 4);
11847981c001SMika Westerberg 
1185e64fbfa5SAndy Shevchenko 		raw_spin_unlock_irqrestore(&pctrl->lock, flags);
1186e64fbfa5SAndy Shevchenko 
11877981c001SMika Westerberg 		/* Only interrupts that are enabled */
11887981c001SMika Westerberg 		pending &= enabled;
11897981c001SMika Westerberg 
1190919eb475SMika Westerberg 		for_each_set_bit(gpp_offset, &pending, padgrp->size) {
119111b389ccSAndy Shevchenko 			unsigned int irq;
11927981c001SMika Westerberg 
1193f0fbe7bcSThierry Reding 			irq = irq_find_mapping(gc->irq.domain,
1194a60eac32SMika Westerberg 					       padgrp->gpio_base + gpp_offset);
11957981c001SMika Westerberg 			generic_handle_irq(irq);
11967981c001SMika Westerberg 		}
119786851bbcSAndy Shevchenko 
119886851bbcSAndy Shevchenko 		ret += pending ? 1 : 0;
11997981c001SMika Westerberg 	}
12007981c001SMika Westerberg 
1201193b40c8SMika Westerberg 	return ret;
1202193b40c8SMika Westerberg }
1203193b40c8SMika Westerberg 
1204193b40c8SMika Westerberg static irqreturn_t intel_gpio_irq(int irq, void *data)
12057981c001SMika Westerberg {
1206193b40c8SMika Westerberg 	const struct intel_community *community;
1207193b40c8SMika Westerberg 	struct intel_pinctrl *pctrl = data;
120886851bbcSAndy Shevchenko 	unsigned int i;
120986851bbcSAndy Shevchenko 	int ret = 0;
12107981c001SMika Westerberg 
12117981c001SMika Westerberg 	/* Need to check all communities for pending interrupts */
1212193b40c8SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
1213193b40c8SMika Westerberg 		community = &pctrl->communities[i];
121486851bbcSAndy Shevchenko 		ret += intel_gpio_community_irq_handler(pctrl, community);
1215193b40c8SMika Westerberg 	}
12167981c001SMika Westerberg 
121786851bbcSAndy Shevchenko 	return IRQ_RETVAL(ret);
12187981c001SMika Westerberg }
12197981c001SMika Westerberg 
12206d416b9bSLinus Walleij static int intel_gpio_add_community_ranges(struct intel_pinctrl *pctrl,
1221a60eac32SMika Westerberg 				const struct intel_community *community)
1222a60eac32SMika Westerberg {
122333b6cb58SColin Ian King 	int ret = 0, i;
1224a60eac32SMika Westerberg 
1225a60eac32SMika Westerberg 	for (i = 0; i < community->ngpps; i++) {
1226a60eac32SMika Westerberg 		const struct intel_padgroup *gpp = &community->gpps[i];
1227a60eac32SMika Westerberg 
1228e5a4ab6aSAndy Shevchenko 		if (gpp->gpio_base == INTEL_GPIO_BASE_NOMAP)
1229a60eac32SMika Westerberg 			continue;
1230a60eac32SMika Westerberg 
1231a60eac32SMika Westerberg 		ret = gpiochip_add_pin_range(&pctrl->chip, dev_name(pctrl->dev),
1232a60eac32SMika Westerberg 					     gpp->gpio_base, gpp->base,
1233a60eac32SMika Westerberg 					     gpp->size);
1234a60eac32SMika Westerberg 		if (ret)
1235a60eac32SMika Westerberg 			return ret;
1236a60eac32SMika Westerberg 	}
1237a60eac32SMika Westerberg 
1238a60eac32SMika Westerberg 	return ret;
1239a60eac32SMika Westerberg }
1240a60eac32SMika Westerberg 
12416d416b9bSLinus Walleij static int intel_gpio_add_pin_ranges(struct gpio_chip *gc)
12426d416b9bSLinus Walleij {
12436d416b9bSLinus Walleij 	struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
12446d416b9bSLinus Walleij 	int ret, i;
12456d416b9bSLinus Walleij 
12466d416b9bSLinus Walleij 	for (i = 0; i < pctrl->ncommunities; i++) {
12476d416b9bSLinus Walleij 		struct intel_community *community = &pctrl->communities[i];
12486d416b9bSLinus Walleij 
12496d416b9bSLinus Walleij 		ret = intel_gpio_add_community_ranges(pctrl, community);
12506d416b9bSLinus Walleij 		if (ret) {
12516d416b9bSLinus Walleij 			dev_err(pctrl->dev, "failed to add GPIO pin range\n");
12526d416b9bSLinus Walleij 			return ret;
12536d416b9bSLinus Walleij 		}
12546d416b9bSLinus Walleij 	}
12556d416b9bSLinus Walleij 
12566d416b9bSLinus Walleij 	return 0;
12576d416b9bSLinus Walleij }
12586d416b9bSLinus Walleij 
125911b389ccSAndy Shevchenko static unsigned int intel_gpio_ngpio(const struct intel_pinctrl *pctrl)
1260a60eac32SMika Westerberg {
1261a60eac32SMika Westerberg 	const struct intel_community *community;
126204035f7fSAndy Shevchenko 	unsigned int ngpio = 0;
1263a60eac32SMika Westerberg 	int i, j;
1264a60eac32SMika Westerberg 
1265a60eac32SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
1266a60eac32SMika Westerberg 		community = &pctrl->communities[i];
1267a60eac32SMika Westerberg 		for (j = 0; j < community->ngpps; j++) {
1268a60eac32SMika Westerberg 			const struct intel_padgroup *gpp = &community->gpps[j];
1269a60eac32SMika Westerberg 
1270e5a4ab6aSAndy Shevchenko 			if (gpp->gpio_base == INTEL_GPIO_BASE_NOMAP)
1271a60eac32SMika Westerberg 				continue;
1272a60eac32SMika Westerberg 
1273a60eac32SMika Westerberg 			if (gpp->gpio_base + gpp->size > ngpio)
1274a60eac32SMika Westerberg 				ngpio = gpp->gpio_base + gpp->size;
1275a60eac32SMika Westerberg 		}
1276a60eac32SMika Westerberg 	}
1277a60eac32SMika Westerberg 
1278a60eac32SMika Westerberg 	return ngpio;
1279a60eac32SMika Westerberg }
1280a60eac32SMika Westerberg 
12817981c001SMika Westerberg static int intel_gpio_probe(struct intel_pinctrl *pctrl, int irq)
12827981c001SMika Westerberg {
12836d416b9bSLinus Walleij 	int ret;
1284af0c5330SLinus Walleij 	struct gpio_irq_chip *girq;
12857981c001SMika Westerberg 
12867981c001SMika Westerberg 	pctrl->chip = intel_gpio_chip;
12877981c001SMika Westerberg 
128857ff2df1SAndy Shevchenko 	/* Setup GPIO chip */
1289a60eac32SMika Westerberg 	pctrl->chip.ngpio = intel_gpio_ngpio(pctrl);
12907981c001SMika Westerberg 	pctrl->chip.label = dev_name(pctrl->dev);
129158383c78SLinus Walleij 	pctrl->chip.parent = pctrl->dev;
12927981c001SMika Westerberg 	pctrl->chip.base = -1;
12936d416b9bSLinus Walleij 	pctrl->chip.add_pin_ranges = intel_gpio_add_pin_ranges;
129401dabe91SNilesh Bacchewar 	pctrl->irq = irq;
12957981c001SMika Westerberg 
129657ff2df1SAndy Shevchenko 	/* Setup IRQ chip */
129757ff2df1SAndy Shevchenko 	pctrl->irqchip.name = dev_name(pctrl->dev);
129857ff2df1SAndy Shevchenko 	pctrl->irqchip.irq_ack = intel_gpio_irq_ack;
129957ff2df1SAndy Shevchenko 	pctrl->irqchip.irq_mask = intel_gpio_irq_mask;
130057ff2df1SAndy Shevchenko 	pctrl->irqchip.irq_unmask = intel_gpio_irq_unmask;
130157ff2df1SAndy Shevchenko 	pctrl->irqchip.irq_set_type = intel_gpio_irq_type;
130257ff2df1SAndy Shevchenko 	pctrl->irqchip.irq_set_wake = intel_gpio_irq_wake;
130357ff2df1SAndy Shevchenko 	pctrl->irqchip.flags = IRQCHIP_MASK_ON_SUSPEND;
130457ff2df1SAndy Shevchenko 
1305193b40c8SMika Westerberg 	/*
1306af0c5330SLinus Walleij 	 * On some platforms several GPIO controllers share the same interrupt
1307af0c5330SLinus Walleij 	 * line.
1308193b40c8SMika Westerberg 	 */
13091a7d1cb8SMika Westerberg 	ret = devm_request_irq(pctrl->dev, irq, intel_gpio_irq,
13101a7d1cb8SMika Westerberg 			       IRQF_SHARED | IRQF_NO_THREAD,
1311193b40c8SMika Westerberg 			       dev_name(pctrl->dev), pctrl);
1312193b40c8SMika Westerberg 	if (ret) {
1313193b40c8SMika Westerberg 		dev_err(pctrl->dev, "failed to request interrupt\n");
1314f25c3aa9SMika Westerberg 		return ret;
13157981c001SMika Westerberg 	}
13167981c001SMika Westerberg 
1317af0c5330SLinus Walleij 	girq = &pctrl->chip.irq;
1318af0c5330SLinus Walleij 	girq->chip = &pctrl->irqchip;
1319af0c5330SLinus Walleij 	/* This will let us handle the IRQ in the driver */
1320af0c5330SLinus Walleij 	girq->parent_handler = NULL;
1321af0c5330SLinus Walleij 	girq->num_parents = 0;
1322af0c5330SLinus Walleij 	girq->default_type = IRQ_TYPE_NONE;
1323af0c5330SLinus Walleij 	girq->handler = handle_bad_irq;
1324af0c5330SLinus Walleij 
1325af0c5330SLinus Walleij 	ret = devm_gpiochip_add_data(pctrl->dev, &pctrl->chip, pctrl);
13267981c001SMika Westerberg 	if (ret) {
1327af0c5330SLinus Walleij 		dev_err(pctrl->dev, "failed to register gpiochip\n");
1328f25c3aa9SMika Westerberg 		return ret;
13297981c001SMika Westerberg 	}
13307981c001SMika Westerberg 
13317981c001SMika Westerberg 	return 0;
13327981c001SMika Westerberg }
13337981c001SMika Westerberg 
1334036e126cSAndy Shevchenko static int intel_pinctrl_add_padgroups_by_gpps(struct intel_pinctrl *pctrl,
1335919eb475SMika Westerberg 					       struct intel_community *community)
1336919eb475SMika Westerberg {
1337919eb475SMika Westerberg 	struct intel_padgroup *gpps;
133804035f7fSAndy Shevchenko 	unsigned int padown_num = 0;
1339036e126cSAndy Shevchenko 	size_t i, ngpps = community->ngpps;
1340919eb475SMika Westerberg 
1341919eb475SMika Westerberg 	gpps = devm_kcalloc(pctrl->dev, ngpps, sizeof(*gpps), GFP_KERNEL);
1342919eb475SMika Westerberg 	if (!gpps)
1343919eb475SMika Westerberg 		return -ENOMEM;
1344919eb475SMika Westerberg 
1345919eb475SMika Westerberg 	for (i = 0; i < ngpps; i++) {
1346919eb475SMika Westerberg 		gpps[i] = community->gpps[i];
1347919eb475SMika Westerberg 
1348919eb475SMika Westerberg 		if (gpps[i].size > 32)
1349919eb475SMika Westerberg 			return -EINVAL;
1350919eb475SMika Westerberg 
1351e5a4ab6aSAndy Shevchenko 		/* Special treatment for GPIO base */
1352e5a4ab6aSAndy Shevchenko 		switch (gpps[i].gpio_base) {
1353e5a4ab6aSAndy Shevchenko 			case INTEL_GPIO_BASE_MATCH:
1354a60eac32SMika Westerberg 				gpps[i].gpio_base = gpps[i].base;
1355e5a4ab6aSAndy Shevchenko 				break;
13569bd59157SAndy Shevchenko 			case INTEL_GPIO_BASE_ZERO:
13579bd59157SAndy Shevchenko 				gpps[i].gpio_base = 0;
13589bd59157SAndy Shevchenko 				break;
1359e5a4ab6aSAndy Shevchenko 			case INTEL_GPIO_BASE_NOMAP:
136077e14126SAndy Shevchenko 				break;
1361e5a4ab6aSAndy Shevchenko 			default:
1362e5a4ab6aSAndy Shevchenko 				break;
1363e5a4ab6aSAndy Shevchenko 		}
1364a60eac32SMika Westerberg 
1365919eb475SMika Westerberg 		gpps[i].padown_num = padown_num;
1366036e126cSAndy Shevchenko 		padown_num += DIV_ROUND_UP(gpps[i].size * 4, 32);
1367036e126cSAndy Shevchenko 	}
1368036e126cSAndy Shevchenko 
1369036e126cSAndy Shevchenko 	community->gpps = gpps;
1370036e126cSAndy Shevchenko 
1371036e126cSAndy Shevchenko 	return 0;
1372036e126cSAndy Shevchenko }
1373036e126cSAndy Shevchenko 
1374036e126cSAndy Shevchenko static int intel_pinctrl_add_padgroups_by_size(struct intel_pinctrl *pctrl,
1375036e126cSAndy Shevchenko 					       struct intel_community *community)
1376036e126cSAndy Shevchenko {
1377036e126cSAndy Shevchenko 	struct intel_padgroup *gpps;
1378036e126cSAndy Shevchenko 	unsigned int npins = community->npins;
1379036e126cSAndy Shevchenko 	unsigned int padown_num = 0;
1380036e126cSAndy Shevchenko 	size_t i, ngpps = DIV_ROUND_UP(npins, community->gpp_size);
1381036e126cSAndy Shevchenko 
1382036e126cSAndy Shevchenko 	if (community->gpp_size > 32)
1383036e126cSAndy Shevchenko 		return -EINVAL;
1384036e126cSAndy Shevchenko 
1385036e126cSAndy Shevchenko 	gpps = devm_kcalloc(pctrl->dev, ngpps, sizeof(*gpps), GFP_KERNEL);
1386036e126cSAndy Shevchenko 	if (!gpps)
1387036e126cSAndy Shevchenko 		return -ENOMEM;
1388036e126cSAndy Shevchenko 
1389036e126cSAndy Shevchenko 	for (i = 0; i < ngpps; i++) {
1390036e126cSAndy Shevchenko 		unsigned int gpp_size = community->gpp_size;
1391036e126cSAndy Shevchenko 
1392036e126cSAndy Shevchenko 		gpps[i].reg_num = i;
1393036e126cSAndy Shevchenko 		gpps[i].base = community->pin_base + i * gpp_size;
1394036e126cSAndy Shevchenko 		gpps[i].size = min(gpp_size, npins);
1395036e126cSAndy Shevchenko 		npins -= gpps[i].size;
1396036e126cSAndy Shevchenko 
139777e14126SAndy Shevchenko 		gpps[i].gpio_base = gpps[i].base;
1398036e126cSAndy Shevchenko 		gpps[i].padown_num = padown_num;
1399919eb475SMika Westerberg 
1400919eb475SMika Westerberg 		/*
1401919eb475SMika Westerberg 		 * In older hardware the number of padown registers per
1402919eb475SMika Westerberg 		 * group is fixed regardless of the group size.
1403919eb475SMika Westerberg 		 */
1404919eb475SMika Westerberg 		if (community->gpp_num_padown_regs)
1405919eb475SMika Westerberg 			padown_num += community->gpp_num_padown_regs;
1406919eb475SMika Westerberg 		else
1407919eb475SMika Westerberg 			padown_num += DIV_ROUND_UP(gpps[i].size * 4, 32);
1408919eb475SMika Westerberg 	}
1409919eb475SMika Westerberg 
1410919eb475SMika Westerberg 	community->ngpps = ngpps;
1411919eb475SMika Westerberg 	community->gpps = gpps;
1412919eb475SMika Westerberg 
1413919eb475SMika Westerberg 	return 0;
1414919eb475SMika Westerberg }
1415919eb475SMika Westerberg 
14167981c001SMika Westerberg static int intel_pinctrl_pm_init(struct intel_pinctrl *pctrl)
14177981c001SMika Westerberg {
14187981c001SMika Westerberg #ifdef CONFIG_PM_SLEEP
14197981c001SMika Westerberg 	const struct intel_pinctrl_soc_data *soc = pctrl->soc;
14207981c001SMika Westerberg 	struct intel_community_context *communities;
14217981c001SMika Westerberg 	struct intel_pad_context *pads;
14227981c001SMika Westerberg 	int i;
14237981c001SMika Westerberg 
14247981c001SMika Westerberg 	pads = devm_kcalloc(pctrl->dev, soc->npins, sizeof(*pads), GFP_KERNEL);
14257981c001SMika Westerberg 	if (!pads)
14267981c001SMika Westerberg 		return -ENOMEM;
14277981c001SMika Westerberg 
14287981c001SMika Westerberg 	communities = devm_kcalloc(pctrl->dev, pctrl->ncommunities,
14297981c001SMika Westerberg 				   sizeof(*communities), GFP_KERNEL);
14307981c001SMika Westerberg 	if (!communities)
14317981c001SMika Westerberg 		return -ENOMEM;
14327981c001SMika Westerberg 
14337981c001SMika Westerberg 
14347981c001SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
14357981c001SMika Westerberg 		struct intel_community *community = &pctrl->communities[i];
1436a0a5f766SChris Chiu 		u32 *intmask, *hostown;
14377981c001SMika Westerberg 
14387981c001SMika Westerberg 		intmask = devm_kcalloc(pctrl->dev, community->ngpps,
14397981c001SMika Westerberg 				       sizeof(*intmask), GFP_KERNEL);
14407981c001SMika Westerberg 		if (!intmask)
14417981c001SMika Westerberg 			return -ENOMEM;
14427981c001SMika Westerberg 
14437981c001SMika Westerberg 		communities[i].intmask = intmask;
1444a0a5f766SChris Chiu 
1445a0a5f766SChris Chiu 		hostown = devm_kcalloc(pctrl->dev, community->ngpps,
1446a0a5f766SChris Chiu 				       sizeof(*hostown), GFP_KERNEL);
1447a0a5f766SChris Chiu 		if (!hostown)
1448a0a5f766SChris Chiu 			return -ENOMEM;
1449a0a5f766SChris Chiu 
1450a0a5f766SChris Chiu 		communities[i].hostown = hostown;
14517981c001SMika Westerberg 	}
14527981c001SMika Westerberg 
14537981c001SMika Westerberg 	pctrl->context.pads = pads;
14547981c001SMika Westerberg 	pctrl->context.communities = communities;
14557981c001SMika Westerberg #endif
14567981c001SMika Westerberg 
14577981c001SMika Westerberg 	return 0;
14587981c001SMika Westerberg }
14597981c001SMika Westerberg 
14600dd519e3SAndy Shevchenko static int intel_pinctrl_probe(struct platform_device *pdev,
14617981c001SMika Westerberg 			       const struct intel_pinctrl_soc_data *soc_data)
14627981c001SMika Westerberg {
14637981c001SMika Westerberg 	struct intel_pinctrl *pctrl;
14647981c001SMika Westerberg 	int i, ret, irq;
14657981c001SMika Westerberg 
14667981c001SMika Westerberg 	pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL);
14677981c001SMika Westerberg 	if (!pctrl)
14687981c001SMika Westerberg 		return -ENOMEM;
14697981c001SMika Westerberg 
14707981c001SMika Westerberg 	pctrl->dev = &pdev->dev;
14717981c001SMika Westerberg 	pctrl->soc = soc_data;
147227d9098cSMika Westerberg 	raw_spin_lock_init(&pctrl->lock);
14737981c001SMika Westerberg 
14747981c001SMika Westerberg 	/*
14757981c001SMika Westerberg 	 * Make a copy of the communities which we can use to hold pointers
14767981c001SMika Westerberg 	 * to the registers.
14777981c001SMika Westerberg 	 */
14787981c001SMika Westerberg 	pctrl->ncommunities = pctrl->soc->ncommunities;
14797981c001SMika Westerberg 	pctrl->communities = devm_kcalloc(&pdev->dev, pctrl->ncommunities,
14807981c001SMika Westerberg 				  sizeof(*pctrl->communities), GFP_KERNEL);
14817981c001SMika Westerberg 	if (!pctrl->communities)
14827981c001SMika Westerberg 		return -ENOMEM;
14837981c001SMika Westerberg 
14847981c001SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
14857981c001SMika Westerberg 		struct intel_community *community = &pctrl->communities[i];
14867981c001SMika Westerberg 		void __iomem *regs;
148791d898e5SAndy Shevchenko 		u32 offset;
1488998c49e8SAndy Shevchenko 		u32 value;
14897981c001SMika Westerberg 
14907981c001SMika Westerberg 		*community = pctrl->soc->communities[i];
14917981c001SMika Westerberg 
14929d5b6a95SAndy Shevchenko 		regs = devm_platform_ioremap_resource(pdev, community->barno);
14937981c001SMika Westerberg 		if (IS_ERR(regs))
14947981c001SMika Westerberg 			return PTR_ERR(regs);
14957981c001SMika Westerberg 
1496*39c1f1bdSRoger Pau Monne 		/*
1497*39c1f1bdSRoger Pau Monne 		 * Determine community features based on the revision.
1498*39c1f1bdSRoger Pau Monne 		 * A value of all ones means the device is not present.
1499*39c1f1bdSRoger Pau Monne 		 */
1500998c49e8SAndy Shevchenko 		value = readl(regs + REVID);
1501*39c1f1bdSRoger Pau Monne 		if (value == ~0u)
1502*39c1f1bdSRoger Pau Monne 			return -ENODEV;
1503998c49e8SAndy Shevchenko 		if (((value & REVID_MASK) >> REVID_SHIFT) >= 0x94) {
1504e57725eaSMika Westerberg 			community->features |= PINCTRL_FEATURE_DEBOUNCE;
150504cc058fSMika Westerberg 			community->features |= PINCTRL_FEATURE_1K_PD;
150604cc058fSMika Westerberg 		}
1507e57725eaSMika Westerberg 
150891d898e5SAndy Shevchenko 		/* Determine community features based on the capabilities */
150991d898e5SAndy Shevchenko 		offset = CAPLIST;
151091d898e5SAndy Shevchenko 		do {
151191d898e5SAndy Shevchenko 			value = readl(regs + offset);
151291d898e5SAndy Shevchenko 			switch ((value & CAPLIST_ID_MASK) >> CAPLIST_ID_SHIFT) {
151391d898e5SAndy Shevchenko 			case CAPLIST_ID_GPIO_HW_INFO:
151491d898e5SAndy Shevchenko 				community->features |= PINCTRL_FEATURE_GPIO_HW_INFO;
151591d898e5SAndy Shevchenko 				break;
151691d898e5SAndy Shevchenko 			case CAPLIST_ID_PWM:
151791d898e5SAndy Shevchenko 				community->features |= PINCTRL_FEATURE_PWM;
151891d898e5SAndy Shevchenko 				break;
151991d898e5SAndy Shevchenko 			case CAPLIST_ID_BLINK:
152091d898e5SAndy Shevchenko 				community->features |= PINCTRL_FEATURE_BLINK;
152191d898e5SAndy Shevchenko 				break;
152291d898e5SAndy Shevchenko 			case CAPLIST_ID_EXP:
152391d898e5SAndy Shevchenko 				community->features |= PINCTRL_FEATURE_EXP;
152491d898e5SAndy Shevchenko 				break;
152591d898e5SAndy Shevchenko 			default:
152691d898e5SAndy Shevchenko 				break;
152791d898e5SAndy Shevchenko 			}
152891d898e5SAndy Shevchenko 			offset = (value & CAPLIST_NEXT_MASK) >> CAPLIST_NEXT_SHIFT;
152991d898e5SAndy Shevchenko 		} while (offset);
153091d898e5SAndy Shevchenko 
153191d898e5SAndy Shevchenko 		dev_dbg(&pdev->dev, "Community%d features: %#08x\n", i, community->features);
153291d898e5SAndy Shevchenko 
15337981c001SMika Westerberg 		/* Read offset of the pad configuration registers */
153491d898e5SAndy Shevchenko 		offset = readl(regs + PADBAR);
15357981c001SMika Westerberg 
15367981c001SMika Westerberg 		community->regs = regs;
153791d898e5SAndy Shevchenko 		community->pad_regs = regs + offset;
1538919eb475SMika Westerberg 
1539036e126cSAndy Shevchenko 		if (community->gpps)
1540036e126cSAndy Shevchenko 			ret = intel_pinctrl_add_padgroups_by_gpps(pctrl, community);
1541036e126cSAndy Shevchenko 		else
1542036e126cSAndy Shevchenko 			ret = intel_pinctrl_add_padgroups_by_size(pctrl, community);
1543919eb475SMika Westerberg 		if (ret)
1544919eb475SMika Westerberg 			return ret;
15457981c001SMika Westerberg 	}
15467981c001SMika Westerberg 
15477981c001SMika Westerberg 	irq = platform_get_irq(pdev, 0);
15484e73d02fSStephen Boyd 	if (irq < 0)
15497981c001SMika Westerberg 		return irq;
15507981c001SMika Westerberg 
15517981c001SMika Westerberg 	ret = intel_pinctrl_pm_init(pctrl);
15527981c001SMika Westerberg 	if (ret)
15537981c001SMika Westerberg 		return ret;
15547981c001SMika Westerberg 
15557981c001SMika Westerberg 	pctrl->pctldesc = intel_pinctrl_desc;
15567981c001SMika Westerberg 	pctrl->pctldesc.name = dev_name(&pdev->dev);
15577981c001SMika Westerberg 	pctrl->pctldesc.pins = pctrl->soc->pins;
15587981c001SMika Westerberg 	pctrl->pctldesc.npins = pctrl->soc->npins;
15597981c001SMika Westerberg 
156054d46cd7SLaxman Dewangan 	pctrl->pctldev = devm_pinctrl_register(&pdev->dev, &pctrl->pctldesc,
156154d46cd7SLaxman Dewangan 					       pctrl);
1562323de9efSMasahiro Yamada 	if (IS_ERR(pctrl->pctldev)) {
15637981c001SMika Westerberg 		dev_err(&pdev->dev, "failed to register pinctrl driver\n");
1564323de9efSMasahiro Yamada 		return PTR_ERR(pctrl->pctldev);
15657981c001SMika Westerberg 	}
15667981c001SMika Westerberg 
15677981c001SMika Westerberg 	ret = intel_gpio_probe(pctrl, irq);
156854d46cd7SLaxman Dewangan 	if (ret)
15697981c001SMika Westerberg 		return ret;
15707981c001SMika Westerberg 
15717981c001SMika Westerberg 	platform_set_drvdata(pdev, pctrl);
15727981c001SMika Westerberg 
15737981c001SMika Westerberg 	return 0;
15747981c001SMika Westerberg }
15757981c001SMika Westerberg 
157670c263c4SAndy Shevchenko int intel_pinctrl_probe_by_hid(struct platform_device *pdev)
157770c263c4SAndy Shevchenko {
157870c263c4SAndy Shevchenko 	const struct intel_pinctrl_soc_data *data;
157970c263c4SAndy Shevchenko 
158070c263c4SAndy Shevchenko 	data = device_get_match_data(&pdev->dev);
1581ff360d62SAndy Shevchenko 	if (!data)
1582ff360d62SAndy Shevchenko 		return -ENODATA;
1583ff360d62SAndy Shevchenko 
158470c263c4SAndy Shevchenko 	return intel_pinctrl_probe(pdev, data);
158570c263c4SAndy Shevchenko }
158670c263c4SAndy Shevchenko EXPORT_SYMBOL_GPL(intel_pinctrl_probe_by_hid);
158770c263c4SAndy Shevchenko 
1588924cf800SAndy Shevchenko int intel_pinctrl_probe_by_uid(struct platform_device *pdev)
1589924cf800SAndy Shevchenko {
1590ff360d62SAndy Shevchenko 	const struct intel_pinctrl_soc_data *data;
1591ff360d62SAndy Shevchenko 
1592ff360d62SAndy Shevchenko 	data = intel_pinctrl_get_soc_data(pdev);
1593ff360d62SAndy Shevchenko 	if (IS_ERR(data))
1594ff360d62SAndy Shevchenko 		return PTR_ERR(data);
1595ff360d62SAndy Shevchenko 
1596ff360d62SAndy Shevchenko 	return intel_pinctrl_probe(pdev, data);
1597ff360d62SAndy Shevchenko }
1598ff360d62SAndy Shevchenko EXPORT_SYMBOL_GPL(intel_pinctrl_probe_by_uid);
1599ff360d62SAndy Shevchenko 
1600ff360d62SAndy Shevchenko const struct intel_pinctrl_soc_data *intel_pinctrl_get_soc_data(struct platform_device *pdev)
1601ff360d62SAndy Shevchenko {
1602924cf800SAndy Shevchenko 	const struct intel_pinctrl_soc_data *data = NULL;
1603924cf800SAndy Shevchenko 	const struct intel_pinctrl_soc_data **table;
1604924cf800SAndy Shevchenko 	struct acpi_device *adev;
1605924cf800SAndy Shevchenko 	unsigned int i;
1606924cf800SAndy Shevchenko 
1607924cf800SAndy Shevchenko 	adev = ACPI_COMPANION(&pdev->dev);
1608924cf800SAndy Shevchenko 	if (adev) {
1609924cf800SAndy Shevchenko 		const void *match = device_get_match_data(&pdev->dev);
1610924cf800SAndy Shevchenko 
1611924cf800SAndy Shevchenko 		table = (const struct intel_pinctrl_soc_data **)match;
1612924cf800SAndy Shevchenko 		for (i = 0; table[i]; i++) {
1613924cf800SAndy Shevchenko 			if (!strcmp(adev->pnp.unique_id, table[i]->uid)) {
1614924cf800SAndy Shevchenko 				data = table[i];
1615924cf800SAndy Shevchenko 				break;
1616924cf800SAndy Shevchenko 			}
1617924cf800SAndy Shevchenko 		}
1618924cf800SAndy Shevchenko 	} else {
1619924cf800SAndy Shevchenko 		const struct platform_device_id *id;
1620924cf800SAndy Shevchenko 
1621924cf800SAndy Shevchenko 		id = platform_get_device_id(pdev);
1622924cf800SAndy Shevchenko 		if (!id)
1623ff360d62SAndy Shevchenko 			return ERR_PTR(-ENODEV);
1624924cf800SAndy Shevchenko 
1625924cf800SAndy Shevchenko 		table = (const struct intel_pinctrl_soc_data **)id->driver_data;
1626924cf800SAndy Shevchenko 		data = table[pdev->id];
1627924cf800SAndy Shevchenko 	}
1628924cf800SAndy Shevchenko 
1629ff360d62SAndy Shevchenko 	return data ?: ERR_PTR(-ENODATA);
1630924cf800SAndy Shevchenko }
1631ff360d62SAndy Shevchenko EXPORT_SYMBOL_GPL(intel_pinctrl_get_soc_data);
1632924cf800SAndy Shevchenko 
16337981c001SMika Westerberg #ifdef CONFIG_PM_SLEEP
163404035f7fSAndy Shevchenko static bool intel_pinctrl_should_save(struct intel_pinctrl *pctrl, unsigned int pin)
1635c538b943SMika Westerberg {
1636c538b943SMika Westerberg 	const struct pin_desc *pd = pin_desc_get(pctrl->pctldev, pin);
1637c538b943SMika Westerberg 
1638c538b943SMika Westerberg 	if (!pd || !intel_pad_usable(pctrl, pin))
1639c538b943SMika Westerberg 		return false;
1640c538b943SMika Westerberg 
1641c538b943SMika Westerberg 	/*
1642c538b943SMika Westerberg 	 * Only restore the pin if it is actually in use by the kernel (or
1643c538b943SMika Westerberg 	 * by userspace). It is possible that some pins are used by the
1644c538b943SMika Westerberg 	 * BIOS during resume and those are not always locked down so leave
1645c538b943SMika Westerberg 	 * them alone.
1646c538b943SMika Westerberg 	 */
1647c538b943SMika Westerberg 	if (pd->mux_owner || pd->gpio_owner ||
16486cb0880fSChris Chiu 	    gpiochip_line_is_irq(&pctrl->chip, intel_pin_to_gpio(pctrl, pin)))
1649c538b943SMika Westerberg 		return true;
1650c538b943SMika Westerberg 
1651c538b943SMika Westerberg 	return false;
1652c538b943SMika Westerberg }
1653c538b943SMika Westerberg 
16542fef3276SBinbin Wu int intel_pinctrl_suspend_noirq(struct device *dev)
16557981c001SMika Westerberg {
1656cb035d74SWolfram Sang 	struct intel_pinctrl *pctrl = dev_get_drvdata(dev);
16577981c001SMika Westerberg 	struct intel_community_context *communities;
16587981c001SMika Westerberg 	struct intel_pad_context *pads;
16597981c001SMika Westerberg 	int i;
16607981c001SMika Westerberg 
16617981c001SMika Westerberg 	pads = pctrl->context.pads;
16627981c001SMika Westerberg 	for (i = 0; i < pctrl->soc->npins; i++) {
16637981c001SMika Westerberg 		const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
1664e57725eaSMika Westerberg 		void __iomem *padcfg;
16657981c001SMika Westerberg 		u32 val;
16667981c001SMika Westerberg 
1667c538b943SMika Westerberg 		if (!intel_pinctrl_should_save(pctrl, desc->number))
16687981c001SMika Westerberg 			continue;
16697981c001SMika Westerberg 
16707981c001SMika Westerberg 		val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG0));
16717981c001SMika Westerberg 		pads[i].padcfg0 = val & ~PADCFG0_GPIORXSTATE;
16727981c001SMika Westerberg 		val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG1));
16737981c001SMika Westerberg 		pads[i].padcfg1 = val;
1674e57725eaSMika Westerberg 
1675e57725eaSMika Westerberg 		padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG2);
1676e57725eaSMika Westerberg 		if (padcfg)
1677e57725eaSMika Westerberg 			pads[i].padcfg2 = readl(padcfg);
16787981c001SMika Westerberg 	}
16797981c001SMika Westerberg 
16807981c001SMika Westerberg 	communities = pctrl->context.communities;
16817981c001SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
16827981c001SMika Westerberg 		struct intel_community *community = &pctrl->communities[i];
16837981c001SMika Westerberg 		void __iomem *base;
168404035f7fSAndy Shevchenko 		unsigned int gpp;
16857981c001SMika Westerberg 
16867981c001SMika Westerberg 		base = community->regs + community->ie_offset;
16877981c001SMika Westerberg 		for (gpp = 0; gpp < community->ngpps; gpp++)
16887981c001SMika Westerberg 			communities[i].intmask[gpp] = readl(base + gpp * 4);
1689a0a5f766SChris Chiu 
1690a0a5f766SChris Chiu 		base = community->regs + community->hostown_offset;
1691a0a5f766SChris Chiu 		for (gpp = 0; gpp < community->ngpps; gpp++)
1692a0a5f766SChris Chiu 			communities[i].hostown[gpp] = readl(base + gpp * 4);
16937981c001SMika Westerberg 	}
16947981c001SMika Westerberg 
16957981c001SMika Westerberg 	return 0;
16967981c001SMika Westerberg }
16972fef3276SBinbin Wu EXPORT_SYMBOL_GPL(intel_pinctrl_suspend_noirq);
16987981c001SMika Westerberg 
1699f487bbf3SMika Westerberg static void intel_gpio_irq_init(struct intel_pinctrl *pctrl)
1700f487bbf3SMika Westerberg {
1701f487bbf3SMika Westerberg 	size_t i;
1702f487bbf3SMika Westerberg 
1703f487bbf3SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
1704f487bbf3SMika Westerberg 		const struct intel_community *community;
1705f487bbf3SMika Westerberg 		void __iomem *base;
170604035f7fSAndy Shevchenko 		unsigned int gpp;
1707f487bbf3SMika Westerberg 
1708f487bbf3SMika Westerberg 		community = &pctrl->communities[i];
1709f487bbf3SMika Westerberg 		base = community->regs;
1710f487bbf3SMika Westerberg 
1711f487bbf3SMika Westerberg 		for (gpp = 0; gpp < community->ngpps; gpp++) {
1712f487bbf3SMika Westerberg 			/* Mask and clear all interrupts */
1713f487bbf3SMika Westerberg 			writel(0, base + community->ie_offset + gpp * 4);
1714cf769bd8SMika Westerberg 			writel(0xffff, base + community->is_offset + gpp * 4);
1715f487bbf3SMika Westerberg 		}
1716f487bbf3SMika Westerberg 	}
1717f487bbf3SMika Westerberg }
1718f487bbf3SMika Westerberg 
1719942c5ea4SAndy Shevchenko static bool intel_gpio_update_reg(void __iomem *reg, u32 mask, u32 value)
1720a0a5f766SChris Chiu {
17215f61d951SAndy Shevchenko 	u32 curr, updated;
1722a0a5f766SChris Chiu 
1723942c5ea4SAndy Shevchenko 	curr = readl(reg);
17245f61d951SAndy Shevchenko 
1725942c5ea4SAndy Shevchenko 	updated = (curr & ~mask) | (value & mask);
1726942c5ea4SAndy Shevchenko 	if (curr == updated)
1727942c5ea4SAndy Shevchenko 		return false;
1728942c5ea4SAndy Shevchenko 
1729942c5ea4SAndy Shevchenko 	writel(updated, reg);
1730942c5ea4SAndy Shevchenko 	return true;
1731a0a5f766SChris Chiu }
1732a0a5f766SChris Chiu 
17337101e022SAndy Shevchenko static void intel_restore_hostown(struct intel_pinctrl *pctrl, unsigned int c,
17347101e022SAndy Shevchenko 				  void __iomem *base, unsigned int gpp, u32 saved)
17357101e022SAndy Shevchenko {
17367101e022SAndy Shevchenko 	const struct intel_community *community = &pctrl->communities[c];
17377101e022SAndy Shevchenko 	const struct intel_padgroup *padgrp = &community->gpps[gpp];
17387101e022SAndy Shevchenko 	struct device *dev = pctrl->dev;
1739d1bfd022SAndy Shevchenko 	const char *dummy;
1740d1bfd022SAndy Shevchenko 	u32 requested = 0;
1741d1bfd022SAndy Shevchenko 	unsigned int i;
17427101e022SAndy Shevchenko 
1743e5a4ab6aSAndy Shevchenko 	if (padgrp->gpio_base == INTEL_GPIO_BASE_NOMAP)
17447101e022SAndy Shevchenko 		return;
17457101e022SAndy Shevchenko 
1746d1bfd022SAndy Shevchenko 	for_each_requested_gpio_in_range(&pctrl->chip, i, padgrp->gpio_base, padgrp->size, dummy)
1747d1bfd022SAndy Shevchenko 		requested |= BIT(i);
1748d1bfd022SAndy Shevchenko 
1749942c5ea4SAndy Shevchenko 	if (!intel_gpio_update_reg(base + gpp * 4, requested, saved))
17507101e022SAndy Shevchenko 		return;
17517101e022SAndy Shevchenko 
1752764cfe33SAndy Shevchenko 	dev_dbg(dev, "restored hostown %u/%u %#08x\n", c, gpp, readl(base + gpp * 4));
17537101e022SAndy Shevchenko }
17547101e022SAndy Shevchenko 
1755471dd9a9SAndy Shevchenko static void intel_restore_intmask(struct intel_pinctrl *pctrl, unsigned int c,
1756471dd9a9SAndy Shevchenko 				  void __iomem *base, unsigned int gpp, u32 saved)
1757471dd9a9SAndy Shevchenko {
1758471dd9a9SAndy Shevchenko 	struct device *dev = pctrl->dev;
1759471dd9a9SAndy Shevchenko 
1760942c5ea4SAndy Shevchenko 	if (!intel_gpio_update_reg(base + gpp * 4, ~0U, saved))
1761942c5ea4SAndy Shevchenko 		return;
1762942c5ea4SAndy Shevchenko 
1763471dd9a9SAndy Shevchenko 	dev_dbg(dev, "restored mask %u/%u %#08x\n", c, gpp, readl(base + gpp * 4));
1764471dd9a9SAndy Shevchenko }
1765471dd9a9SAndy Shevchenko 
1766f78f152aSAndy Shevchenko static void intel_restore_padcfg(struct intel_pinctrl *pctrl, unsigned int pin,
1767f78f152aSAndy Shevchenko 				 unsigned int reg, u32 saved)
1768f78f152aSAndy Shevchenko {
1769f78f152aSAndy Shevchenko 	u32 mask = (reg == PADCFG0) ? PADCFG0_GPIORXSTATE : 0;
1770f78f152aSAndy Shevchenko 	unsigned int n = reg / sizeof(u32);
1771f78f152aSAndy Shevchenko 	struct device *dev = pctrl->dev;
1772f78f152aSAndy Shevchenko 	void __iomem *padcfg;
1773f78f152aSAndy Shevchenko 
1774f78f152aSAndy Shevchenko 	padcfg = intel_get_padcfg(pctrl, pin, reg);
1775f78f152aSAndy Shevchenko 	if (!padcfg)
1776f78f152aSAndy Shevchenko 		return;
1777f78f152aSAndy Shevchenko 
1778942c5ea4SAndy Shevchenko 	if (!intel_gpio_update_reg(padcfg, ~mask, saved))
1779f78f152aSAndy Shevchenko 		return;
1780f78f152aSAndy Shevchenko 
1781f78f152aSAndy Shevchenko 	dev_dbg(dev, "restored pin %u padcfg%u %#08x\n", pin, n, readl(padcfg));
1782f78f152aSAndy Shevchenko }
1783f78f152aSAndy Shevchenko 
17842fef3276SBinbin Wu int intel_pinctrl_resume_noirq(struct device *dev)
17857981c001SMika Westerberg {
1786cb035d74SWolfram Sang 	struct intel_pinctrl *pctrl = dev_get_drvdata(dev);
17877981c001SMika Westerberg 	const struct intel_community_context *communities;
17887981c001SMika Westerberg 	const struct intel_pad_context *pads;
17897981c001SMika Westerberg 	int i;
17907981c001SMika Westerberg 
17917981c001SMika Westerberg 	/* Mask all interrupts */
17927981c001SMika Westerberg 	intel_gpio_irq_init(pctrl);
17937981c001SMika Westerberg 
17947981c001SMika Westerberg 	pads = pctrl->context.pads;
17957981c001SMika Westerberg 	for (i = 0; i < pctrl->soc->npins; i++) {
17967981c001SMika Westerberg 		const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
17977981c001SMika Westerberg 
1798c538b943SMika Westerberg 		if (!intel_pinctrl_should_save(pctrl, desc->number))
17997981c001SMika Westerberg 			continue;
18007981c001SMika Westerberg 
1801f78f152aSAndy Shevchenko 		intel_restore_padcfg(pctrl, desc->number, PADCFG0, pads[i].padcfg0);
1802f78f152aSAndy Shevchenko 		intel_restore_padcfg(pctrl, desc->number, PADCFG1, pads[i].padcfg1);
1803f78f152aSAndy Shevchenko 		intel_restore_padcfg(pctrl, desc->number, PADCFG2, pads[i].padcfg2);
18047981c001SMika Westerberg 	}
18057981c001SMika Westerberg 
18067981c001SMika Westerberg 	communities = pctrl->context.communities;
18077981c001SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
18087981c001SMika Westerberg 		struct intel_community *community = &pctrl->communities[i];
18097981c001SMika Westerberg 		void __iomem *base;
181004035f7fSAndy Shevchenko 		unsigned int gpp;
18117981c001SMika Westerberg 
18127981c001SMika Westerberg 		base = community->regs + community->ie_offset;
1813471dd9a9SAndy Shevchenko 		for (gpp = 0; gpp < community->ngpps; gpp++)
1814471dd9a9SAndy Shevchenko 			intel_restore_intmask(pctrl, i, base, gpp, communities[i].intmask[gpp]);
1815a0a5f766SChris Chiu 
1816a0a5f766SChris Chiu 		base = community->regs + community->hostown_offset;
18177101e022SAndy Shevchenko 		for (gpp = 0; gpp < community->ngpps; gpp++)
18187101e022SAndy Shevchenko 			intel_restore_hostown(pctrl, i, base, gpp, communities[i].hostown[gpp]);
18197981c001SMika Westerberg 	}
18207981c001SMika Westerberg 
18217981c001SMika Westerberg 	return 0;
18227981c001SMika Westerberg }
18232fef3276SBinbin Wu EXPORT_SYMBOL_GPL(intel_pinctrl_resume_noirq);
18247981c001SMika Westerberg #endif
18257981c001SMika Westerberg 
18267981c001SMika Westerberg MODULE_AUTHOR("Mathias Nyman <mathias.nyman@linux.intel.com>");
18277981c001SMika Westerberg MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
18287981c001SMika Westerberg MODULE_DESCRIPTION("Intel pinctrl/GPIO core driver");
18297981c001SMika Westerberg MODULE_LICENSE("GPL v2");
1830