xref: /openbmc/linux/drivers/pinctrl/intel/pinctrl-intel.c (revision de23ccb1edc84f0f59f09b157b082110fb40789c)
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>
17*de23ccb1SAndy Shevchenko #include <linux/seq_file.h>
186a33a1d6SAndy Shevchenko #include <linux/time.h>
19924cf800SAndy Shevchenko 
20*de23ccb1SAndy Shevchenko #include <linux/pinctrl/consumer.h>
217981c001SMika Westerberg #include <linux/pinctrl/pinconf.h>
227981c001SMika Westerberg #include <linux/pinctrl/pinconf-generic.h>
23*de23ccb1SAndy Shevchenko #include <linux/pinctrl/pinctrl.h>
24*de23ccb1SAndy Shevchenko #include <linux/pinctrl/pinmux.h>
257981c001SMika Westerberg 
26c538b943SMika Westerberg #include "../core.h"
277981c001SMika Westerberg #include "pinctrl-intel.h"
287981c001SMika Westerberg 
297981c001SMika Westerberg /* Offset from regs */
30e57725eaSMika Westerberg #define REVID				0x000
31e57725eaSMika Westerberg #define REVID_SHIFT			16
32e57725eaSMika Westerberg #define REVID_MASK			GENMASK(31, 16)
33e57725eaSMika Westerberg 
3491d898e5SAndy Shevchenko #define CAPLIST				0x004
3591d898e5SAndy Shevchenko #define CAPLIST_ID_SHIFT		16
3691d898e5SAndy Shevchenko #define CAPLIST_ID_MASK			GENMASK(23, 16)
3791d898e5SAndy Shevchenko #define CAPLIST_ID_GPIO_HW_INFO		1
3891d898e5SAndy Shevchenko #define CAPLIST_ID_PWM			2
3991d898e5SAndy Shevchenko #define CAPLIST_ID_BLINK		3
4091d898e5SAndy Shevchenko #define CAPLIST_ID_EXP			4
4191d898e5SAndy Shevchenko #define CAPLIST_NEXT_SHIFT		0
4291d898e5SAndy Shevchenko #define CAPLIST_NEXT_MASK		GENMASK(15, 0)
4391d898e5SAndy Shevchenko 
447981c001SMika Westerberg #define PADBAR				0x00c
457981c001SMika Westerberg 
467981c001SMika Westerberg #define PADOWN_BITS			4
477981c001SMika Westerberg #define PADOWN_SHIFT(p)			((p) % 8 * PADOWN_BITS)
48e58926e7SAndy Shevchenko #define PADOWN_MASK(p)			(GENMASK(3, 0) << PADOWN_SHIFT(p))
4999a735b3SQipeng Zha #define PADOWN_GPP(p)			((p) / 8)
507981c001SMika Westerberg 
517981c001SMika Westerberg /* Offset from pad_regs */
527981c001SMika Westerberg #define PADCFG0				0x000
537981c001SMika Westerberg #define PADCFG0_RXEVCFG_SHIFT		25
54e58926e7SAndy Shevchenko #define PADCFG0_RXEVCFG_MASK		GENMASK(26, 25)
557981c001SMika Westerberg #define PADCFG0_RXEVCFG_LEVEL		0
567981c001SMika Westerberg #define PADCFG0_RXEVCFG_EDGE		1
577981c001SMika Westerberg #define PADCFG0_RXEVCFG_DISABLED	2
587981c001SMika Westerberg #define PADCFG0_RXEVCFG_EDGE_BOTH	3
59e57725eaSMika Westerberg #define PADCFG0_PREGFRXSEL		BIT(24)
607981c001SMika Westerberg #define PADCFG0_RXINV			BIT(23)
617981c001SMika Westerberg #define PADCFG0_GPIROUTIOXAPIC		BIT(20)
627981c001SMika Westerberg #define PADCFG0_GPIROUTSCI		BIT(19)
637981c001SMika Westerberg #define PADCFG0_GPIROUTSMI		BIT(18)
647981c001SMika Westerberg #define PADCFG0_GPIROUTNMI		BIT(17)
657981c001SMika Westerberg #define PADCFG0_PMODE_SHIFT		10
66e58926e7SAndy Shevchenko #define PADCFG0_PMODE_MASK		GENMASK(13, 10)
674973ddc8SAndy Shevchenko #define PADCFG0_PMODE_GPIO		0
687981c001SMika Westerberg #define PADCFG0_GPIORXDIS		BIT(9)
697981c001SMika Westerberg #define PADCFG0_GPIOTXDIS		BIT(8)
707981c001SMika Westerberg #define PADCFG0_GPIORXSTATE		BIT(1)
717981c001SMika Westerberg #define PADCFG0_GPIOTXSTATE		BIT(0)
727981c001SMika Westerberg 
737981c001SMika Westerberg #define PADCFG1				0x004
747981c001SMika Westerberg #define PADCFG1_TERM_UP			BIT(13)
757981c001SMika Westerberg #define PADCFG1_TERM_SHIFT		10
76e58926e7SAndy Shevchenko #define PADCFG1_TERM_MASK		GENMASK(12, 10)
77dd26209bSAndy Shevchenko #define PADCFG1_TERM_20K		BIT(2)
78dd26209bSAndy Shevchenko #define PADCFG1_TERM_5K			BIT(1)
79dd26209bSAndy Shevchenko #define PADCFG1_TERM_1K			BIT(0)
80dd26209bSAndy Shevchenko #define PADCFG1_TERM_833		(BIT(1) | BIT(0))
817981c001SMika Westerberg 
82e57725eaSMika Westerberg #define PADCFG2				0x008
83e57725eaSMika Westerberg #define PADCFG2_DEBEN			BIT(0)
84e57725eaSMika Westerberg #define PADCFG2_DEBOUNCE_SHIFT		1
85e57725eaSMika Westerberg #define PADCFG2_DEBOUNCE_MASK		GENMASK(4, 1)
86e57725eaSMika Westerberg 
876a33a1d6SAndy Shevchenko #define DEBOUNCE_PERIOD_NSEC		31250
88e57725eaSMika Westerberg 
897981c001SMika Westerberg struct intel_pad_context {
907981c001SMika Westerberg 	u32 padcfg0;
917981c001SMika Westerberg 	u32 padcfg1;
92e57725eaSMika Westerberg 	u32 padcfg2;
937981c001SMika Westerberg };
947981c001SMika Westerberg 
957981c001SMika Westerberg struct intel_community_context {
967981c001SMika Westerberg 	u32 *intmask;
97a0a5f766SChris Chiu 	u32 *hostown;
987981c001SMika Westerberg };
997981c001SMika Westerberg 
1007981c001SMika Westerberg #define pin_to_padno(c, p)	((p) - (c)->pin_base)
101919eb475SMika Westerberg #define padgroup_offset(g, p)	((p) - (g)->base)
1027981c001SMika Westerberg 
1037981c001SMika Westerberg static struct intel_community *intel_get_community(struct intel_pinctrl *pctrl,
10404035f7fSAndy Shevchenko 						   unsigned int pin)
1057981c001SMika Westerberg {
1067981c001SMika Westerberg 	struct intel_community *community;
1077981c001SMika Westerberg 	int i;
1087981c001SMika Westerberg 
1097981c001SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
1107981c001SMika Westerberg 		community = &pctrl->communities[i];
1117981c001SMika Westerberg 		if (pin >= community->pin_base &&
1127981c001SMika Westerberg 		    pin < community->pin_base + community->npins)
1137981c001SMika Westerberg 			return community;
1147981c001SMika Westerberg 	}
1157981c001SMika Westerberg 
1167981c001SMika Westerberg 	dev_warn(pctrl->dev, "failed to find community for pin %u\n", pin);
1177981c001SMika Westerberg 	return NULL;
1187981c001SMika Westerberg }
1197981c001SMika Westerberg 
120919eb475SMika Westerberg static const struct intel_padgroup *
121919eb475SMika Westerberg intel_community_get_padgroup(const struct intel_community *community,
12204035f7fSAndy Shevchenko 			     unsigned int pin)
123919eb475SMika Westerberg {
124919eb475SMika Westerberg 	int i;
125919eb475SMika Westerberg 
126919eb475SMika Westerberg 	for (i = 0; i < community->ngpps; i++) {
127919eb475SMika Westerberg 		const struct intel_padgroup *padgrp = &community->gpps[i];
128919eb475SMika Westerberg 
129919eb475SMika Westerberg 		if (pin >= padgrp->base && pin < padgrp->base + padgrp->size)
130919eb475SMika Westerberg 			return padgrp;
131919eb475SMika Westerberg 	}
132919eb475SMika Westerberg 
133919eb475SMika Westerberg 	return NULL;
134919eb475SMika Westerberg }
135919eb475SMika Westerberg 
13604035f7fSAndy Shevchenko static void __iomem *intel_get_padcfg(struct intel_pinctrl *pctrl,
13704035f7fSAndy Shevchenko 				      unsigned int pin, unsigned int reg)
1387981c001SMika Westerberg {
1397981c001SMika Westerberg 	const struct intel_community *community;
14004035f7fSAndy Shevchenko 	unsigned int padno;
141e57725eaSMika Westerberg 	size_t nregs;
1427981c001SMika Westerberg 
1437981c001SMika Westerberg 	community = intel_get_community(pctrl, pin);
1447981c001SMika Westerberg 	if (!community)
1457981c001SMika Westerberg 		return NULL;
1467981c001SMika Westerberg 
1477981c001SMika Westerberg 	padno = pin_to_padno(community, pin);
148e57725eaSMika Westerberg 	nregs = (community->features & PINCTRL_FEATURE_DEBOUNCE) ? 4 : 2;
149e57725eaSMika Westerberg 
1507eb7ecddSAndy Shevchenko 	if (reg >= nregs * 4)
151e57725eaSMika Westerberg 		return NULL;
152e57725eaSMika Westerberg 
153e57725eaSMika Westerberg 	return community->pad_regs + reg + padno * nregs * 4;
1547981c001SMika Westerberg }
1557981c001SMika Westerberg 
15604035f7fSAndy Shevchenko static bool intel_pad_owned_by_host(struct intel_pinctrl *pctrl, unsigned int pin)
1577981c001SMika Westerberg {
1587981c001SMika Westerberg 	const struct intel_community *community;
159919eb475SMika Westerberg 	const struct intel_padgroup *padgrp;
16004035f7fSAndy Shevchenko 	unsigned int gpp, offset, gpp_offset;
1617981c001SMika Westerberg 	void __iomem *padown;
1627981c001SMika Westerberg 
1637981c001SMika Westerberg 	community = intel_get_community(pctrl, pin);
1647981c001SMika Westerberg 	if (!community)
1657981c001SMika Westerberg 		return false;
1667981c001SMika Westerberg 	if (!community->padown_offset)
1677981c001SMika Westerberg 		return true;
1687981c001SMika Westerberg 
169919eb475SMika Westerberg 	padgrp = intel_community_get_padgroup(community, pin);
170919eb475SMika Westerberg 	if (!padgrp)
171919eb475SMika Westerberg 		return false;
172919eb475SMika Westerberg 
173919eb475SMika Westerberg 	gpp_offset = padgroup_offset(padgrp, pin);
174919eb475SMika Westerberg 	gpp = PADOWN_GPP(gpp_offset);
175919eb475SMika Westerberg 	offset = community->padown_offset + padgrp->padown_num * 4 + gpp * 4;
1767981c001SMika Westerberg 	padown = community->regs + offset;
1777981c001SMika Westerberg 
178919eb475SMika Westerberg 	return !(readl(padown) & PADOWN_MASK(gpp_offset));
1797981c001SMika Westerberg }
1807981c001SMika Westerberg 
18104035f7fSAndy Shevchenko static bool intel_pad_acpi_mode(struct intel_pinctrl *pctrl, unsigned int pin)
1827981c001SMika Westerberg {
1837981c001SMika Westerberg 	const struct intel_community *community;
184919eb475SMika Westerberg 	const struct intel_padgroup *padgrp;
18504035f7fSAndy Shevchenko 	unsigned int offset, gpp_offset;
1867981c001SMika Westerberg 	void __iomem *hostown;
1877981c001SMika Westerberg 
1887981c001SMika Westerberg 	community = intel_get_community(pctrl, pin);
1897981c001SMika Westerberg 	if (!community)
1907981c001SMika Westerberg 		return true;
1917981c001SMika Westerberg 	if (!community->hostown_offset)
1927981c001SMika Westerberg 		return false;
1937981c001SMika Westerberg 
194919eb475SMika Westerberg 	padgrp = intel_community_get_padgroup(community, pin);
195919eb475SMika Westerberg 	if (!padgrp)
196919eb475SMika Westerberg 		return true;
197919eb475SMika Westerberg 
198919eb475SMika Westerberg 	gpp_offset = padgroup_offset(padgrp, pin);
199919eb475SMika Westerberg 	offset = community->hostown_offset + padgrp->reg_num * 4;
2007981c001SMika Westerberg 	hostown = community->regs + offset;
2017981c001SMika Westerberg 
202919eb475SMika Westerberg 	return !(readl(hostown) & BIT(gpp_offset));
2037981c001SMika Westerberg }
2047981c001SMika Westerberg 
2051bd23153SAndy Shevchenko /**
2061bd23153SAndy Shevchenko  * enum - Locking variants of the pad configuration
2071bd23153SAndy Shevchenko  *
2081bd23153SAndy Shevchenko  * @PAD_UNLOCKED:	pad is fully controlled by the configuration registers
2091bd23153SAndy Shevchenko  * @PAD_LOCKED:		pad configuration registers, except TX state, are locked
2101bd23153SAndy Shevchenko  * @PAD_LOCKED_TX:	pad configuration TX state is locked
2111bd23153SAndy Shevchenko  * @PAD_LOCKED_FULL:	pad configuration registers are locked completely
2121bd23153SAndy Shevchenko  *
2131bd23153SAndy Shevchenko  * Locking is considered as read-only mode for corresponding registers and
2141bd23153SAndy Shevchenko  * their respective fields. That said, TX state bit is locked separately from
2151bd23153SAndy Shevchenko  * the main locking scheme.
2161bd23153SAndy Shevchenko  */
2171bd23153SAndy Shevchenko enum {
2181bd23153SAndy Shevchenko 	PAD_UNLOCKED	= 0,
2191bd23153SAndy Shevchenko 	PAD_LOCKED	= 1,
2201bd23153SAndy Shevchenko 	PAD_LOCKED_TX	= 2,
2211bd23153SAndy Shevchenko 	PAD_LOCKED_FULL	= PAD_LOCKED | PAD_LOCKED_TX,
2221bd23153SAndy Shevchenko };
2231bd23153SAndy Shevchenko 
2241bd23153SAndy Shevchenko static int intel_pad_locked(struct intel_pinctrl *pctrl, unsigned int pin)
2257981c001SMika Westerberg {
2267981c001SMika Westerberg 	struct intel_community *community;
227919eb475SMika Westerberg 	const struct intel_padgroup *padgrp;
22804035f7fSAndy Shevchenko 	unsigned int offset, gpp_offset;
2297981c001SMika Westerberg 	u32 value;
2301bd23153SAndy Shevchenko 	int ret = PAD_UNLOCKED;
2317981c001SMika Westerberg 
2327981c001SMika Westerberg 	community = intel_get_community(pctrl, pin);
2337981c001SMika Westerberg 	if (!community)
2341bd23153SAndy Shevchenko 		return PAD_LOCKED_FULL;
2357981c001SMika Westerberg 	if (!community->padcfglock_offset)
2361bd23153SAndy Shevchenko 		return PAD_UNLOCKED;
2377981c001SMika Westerberg 
238919eb475SMika Westerberg 	padgrp = intel_community_get_padgroup(community, pin);
239919eb475SMika Westerberg 	if (!padgrp)
2401bd23153SAndy Shevchenko 		return PAD_LOCKED_FULL;
241919eb475SMika Westerberg 
242919eb475SMika Westerberg 	gpp_offset = padgroup_offset(padgrp, pin);
2437981c001SMika Westerberg 
2447981c001SMika Westerberg 	/*
2457981c001SMika Westerberg 	 * If PADCFGLOCK and PADCFGLOCKTX bits are both clear for this pad,
2467981c001SMika Westerberg 	 * the pad is considered unlocked. Any other case means that it is
2471bd23153SAndy Shevchenko 	 * either fully or partially locked.
2487981c001SMika Westerberg 	 */
2491bd23153SAndy Shevchenko 	offset = community->padcfglock_offset + 0 + padgrp->reg_num * 8;
2507981c001SMika Westerberg 	value = readl(community->regs + offset);
251919eb475SMika Westerberg 	if (value & BIT(gpp_offset))
2521bd23153SAndy Shevchenko 		ret |= PAD_LOCKED;
2537981c001SMika Westerberg 
254919eb475SMika Westerberg 	offset = community->padcfglock_offset + 4 + padgrp->reg_num * 8;
2557981c001SMika Westerberg 	value = readl(community->regs + offset);
256919eb475SMika Westerberg 	if (value & BIT(gpp_offset))
2571bd23153SAndy Shevchenko 		ret |= PAD_LOCKED_TX;
2587981c001SMika Westerberg 
2591bd23153SAndy Shevchenko 	return ret;
2601bd23153SAndy Shevchenko }
2611bd23153SAndy Shevchenko 
2621bd23153SAndy Shevchenko static bool intel_pad_is_unlocked(struct intel_pinctrl *pctrl, unsigned int pin)
2631bd23153SAndy Shevchenko {
2641bd23153SAndy Shevchenko 	return (intel_pad_locked(pctrl, pin) & PAD_LOCKED) == PAD_UNLOCKED;
2657981c001SMika Westerberg }
2667981c001SMika Westerberg 
26704035f7fSAndy Shevchenko static bool intel_pad_usable(struct intel_pinctrl *pctrl, unsigned int pin)
2687981c001SMika Westerberg {
2691bd23153SAndy Shevchenko 	return intel_pad_owned_by_host(pctrl, pin) && intel_pad_is_unlocked(pctrl, pin);
2707981c001SMika Westerberg }
2717981c001SMika Westerberg 
2727981c001SMika Westerberg static int intel_get_groups_count(struct pinctrl_dev *pctldev)
2737981c001SMika Westerberg {
2747981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
2757981c001SMika Westerberg 
2767981c001SMika Westerberg 	return pctrl->soc->ngroups;
2777981c001SMika Westerberg }
2787981c001SMika Westerberg 
2797981c001SMika Westerberg static const char *intel_get_group_name(struct pinctrl_dev *pctldev,
28004035f7fSAndy Shevchenko 				      unsigned int group)
2817981c001SMika Westerberg {
2827981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
2837981c001SMika Westerberg 
2844426be36SAndy Shevchenko 	return pctrl->soc->groups[group].grp.name;
2857981c001SMika Westerberg }
2867981c001SMika Westerberg 
28704035f7fSAndy Shevchenko static int intel_get_group_pins(struct pinctrl_dev *pctldev, unsigned int group,
28804035f7fSAndy Shevchenko 			      const unsigned int **pins, unsigned int *npins)
2897981c001SMika Westerberg {
2907981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
2917981c001SMika Westerberg 
2924426be36SAndy Shevchenko 	*pins = pctrl->soc->groups[group].grp.pins;
2934426be36SAndy Shevchenko 	*npins = pctrl->soc->groups[group].grp.npins;
2947981c001SMika Westerberg 	return 0;
2957981c001SMika Westerberg }
2967981c001SMika Westerberg 
2977981c001SMika Westerberg static void intel_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
29804035f7fSAndy Shevchenko 			       unsigned int pin)
2997981c001SMika Westerberg {
3007981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
301e57725eaSMika Westerberg 	void __iomem *padcfg;
3027981c001SMika Westerberg 	u32 cfg0, cfg1, mode;
3031bd23153SAndy Shevchenko 	int locked;
3041bd23153SAndy Shevchenko 	bool acpi;
3057981c001SMika Westerberg 
3067981c001SMika Westerberg 	if (!intel_pad_owned_by_host(pctrl, pin)) {
3077981c001SMika Westerberg 		seq_puts(s, "not available");
3087981c001SMika Westerberg 		return;
3097981c001SMika Westerberg 	}
3107981c001SMika Westerberg 
3117981c001SMika Westerberg 	cfg0 = readl(intel_get_padcfg(pctrl, pin, PADCFG0));
3127981c001SMika Westerberg 	cfg1 = readl(intel_get_padcfg(pctrl, pin, PADCFG1));
3137981c001SMika Westerberg 
3147981c001SMika Westerberg 	mode = (cfg0 & PADCFG0_PMODE_MASK) >> PADCFG0_PMODE_SHIFT;
3154973ddc8SAndy Shevchenko 	if (mode == PADCFG0_PMODE_GPIO)
3167981c001SMika Westerberg 		seq_puts(s, "GPIO ");
3177981c001SMika Westerberg 	else
3187981c001SMika Westerberg 		seq_printf(s, "mode %d ", mode);
3197981c001SMika Westerberg 
3207981c001SMika Westerberg 	seq_printf(s, "0x%08x 0x%08x", cfg0, cfg1);
3217981c001SMika Westerberg 
322e57725eaSMika Westerberg 	/* Dump the additional PADCFG registers if available */
323e57725eaSMika Westerberg 	padcfg = intel_get_padcfg(pctrl, pin, PADCFG2);
324e57725eaSMika Westerberg 	if (padcfg)
325e57725eaSMika Westerberg 		seq_printf(s, " 0x%08x", readl(padcfg));
326e57725eaSMika Westerberg 
3277981c001SMika Westerberg 	locked = intel_pad_locked(pctrl, pin);
3284341e8a5SMika Westerberg 	acpi = intel_pad_acpi_mode(pctrl, pin);
3297981c001SMika Westerberg 
3307981c001SMika Westerberg 	if (locked || acpi) {
3317981c001SMika Westerberg 		seq_puts(s, " [");
3321bd23153SAndy Shevchenko 		if (locked)
3337981c001SMika Westerberg 			seq_puts(s, "LOCKED");
3341bd23153SAndy Shevchenko 		if ((locked & PAD_LOCKED_FULL) == PAD_LOCKED_TX)
3351bd23153SAndy Shevchenko 			seq_puts(s, " tx");
3361bd23153SAndy Shevchenko 		else if ((locked & PAD_LOCKED_FULL) == PAD_LOCKED_FULL)
3371bd23153SAndy Shevchenko 			seq_puts(s, " full");
3381bd23153SAndy Shevchenko 
3391bd23153SAndy Shevchenko 		if (locked && acpi)
3407981c001SMika Westerberg 			seq_puts(s, ", ");
3411bd23153SAndy Shevchenko 
3427981c001SMika Westerberg 		if (acpi)
3437981c001SMika Westerberg 			seq_puts(s, "ACPI");
3447981c001SMika Westerberg 		seq_puts(s, "]");
3457981c001SMika Westerberg 	}
3467981c001SMika Westerberg }
3477981c001SMika Westerberg 
3487981c001SMika Westerberg static const struct pinctrl_ops intel_pinctrl_ops = {
3497981c001SMika Westerberg 	.get_groups_count = intel_get_groups_count,
3507981c001SMika Westerberg 	.get_group_name = intel_get_group_name,
3517981c001SMika Westerberg 	.get_group_pins = intel_get_group_pins,
3527981c001SMika Westerberg 	.pin_dbg_show = intel_pin_dbg_show,
3537981c001SMika Westerberg };
3547981c001SMika Westerberg 
3557981c001SMika Westerberg static int intel_get_functions_count(struct pinctrl_dev *pctldev)
3567981c001SMika Westerberg {
3577981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
3587981c001SMika Westerberg 
3597981c001SMika Westerberg 	return pctrl->soc->nfunctions;
3607981c001SMika Westerberg }
3617981c001SMika Westerberg 
3627981c001SMika Westerberg static const char *intel_get_function_name(struct pinctrl_dev *pctldev,
36304035f7fSAndy Shevchenko 					   unsigned int function)
3647981c001SMika Westerberg {
3657981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
3667981c001SMika Westerberg 
3677981c001SMika Westerberg 	return pctrl->soc->functions[function].name;
3687981c001SMika Westerberg }
3697981c001SMika Westerberg 
3707981c001SMika Westerberg static int intel_get_function_groups(struct pinctrl_dev *pctldev,
37104035f7fSAndy Shevchenko 				     unsigned int function,
3727981c001SMika Westerberg 				     const char * const **groups,
37304035f7fSAndy Shevchenko 				     unsigned int * const ngroups)
3747981c001SMika Westerberg {
3757981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
3767981c001SMika Westerberg 
3777981c001SMika Westerberg 	*groups = pctrl->soc->functions[function].groups;
3787981c001SMika Westerberg 	*ngroups = pctrl->soc->functions[function].ngroups;
3797981c001SMika Westerberg 	return 0;
3807981c001SMika Westerberg }
3817981c001SMika Westerberg 
38204035f7fSAndy Shevchenko static int intel_pinmux_set_mux(struct pinctrl_dev *pctldev,
38304035f7fSAndy Shevchenko 				unsigned int function, unsigned int group)
3847981c001SMika Westerberg {
3857981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
3867981c001SMika Westerberg 	const struct intel_pingroup *grp = &pctrl->soc->groups[group];
3877981c001SMika Westerberg 	unsigned long flags;
3887981c001SMika Westerberg 	int i;
3897981c001SMika Westerberg 
39027d9098cSMika Westerberg 	raw_spin_lock_irqsave(&pctrl->lock, flags);
3917981c001SMika Westerberg 
3927981c001SMika Westerberg 	/*
3937981c001SMika Westerberg 	 * All pins in the groups needs to be accessible and writable
3947981c001SMika Westerberg 	 * before we can enable the mux for this group.
3957981c001SMika Westerberg 	 */
3964426be36SAndy Shevchenko 	for (i = 0; i < grp->grp.npins; i++) {
3974426be36SAndy Shevchenko 		if (!intel_pad_usable(pctrl, grp->grp.pins[i])) {
39827d9098cSMika Westerberg 			raw_spin_unlock_irqrestore(&pctrl->lock, flags);
3997981c001SMika Westerberg 			return -EBUSY;
4007981c001SMika Westerberg 		}
4017981c001SMika Westerberg 	}
4027981c001SMika Westerberg 
4037981c001SMika Westerberg 	/* Now enable the mux setting for each pin in the group */
4044426be36SAndy Shevchenko 	for (i = 0; i < grp->grp.npins; i++) {
4057981c001SMika Westerberg 		void __iomem *padcfg0;
4067981c001SMika Westerberg 		u32 value;
4077981c001SMika Westerberg 
4084426be36SAndy Shevchenko 		padcfg0 = intel_get_padcfg(pctrl, grp->grp.pins[i], PADCFG0);
4097981c001SMika Westerberg 		value = readl(padcfg0);
4107981c001SMika Westerberg 
4117981c001SMika Westerberg 		value &= ~PADCFG0_PMODE_MASK;
4121f6b419bSMika Westerberg 
4131f6b419bSMika Westerberg 		if (grp->modes)
4141f6b419bSMika Westerberg 			value |= grp->modes[i] << PADCFG0_PMODE_SHIFT;
4151f6b419bSMika Westerberg 		else
4167981c001SMika Westerberg 			value |= grp->mode << PADCFG0_PMODE_SHIFT;
4177981c001SMika Westerberg 
4187981c001SMika Westerberg 		writel(value, padcfg0);
4197981c001SMika Westerberg 	}
4207981c001SMika Westerberg 
42127d9098cSMika Westerberg 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
4227981c001SMika Westerberg 
4237981c001SMika Westerberg 	return 0;
4247981c001SMika Westerberg }
4257981c001SMika Westerberg 
42617fab473SAndy Shevchenko static void __intel_gpio_set_direction(void __iomem *padcfg0, bool input)
42717fab473SAndy Shevchenko {
42817fab473SAndy Shevchenko 	u32 value;
42917fab473SAndy Shevchenko 
43017fab473SAndy Shevchenko 	value = readl(padcfg0);
43117fab473SAndy Shevchenko 	if (input) {
43217fab473SAndy Shevchenko 		value &= ~PADCFG0_GPIORXDIS;
43317fab473SAndy Shevchenko 		value |= PADCFG0_GPIOTXDIS;
43417fab473SAndy Shevchenko 	} else {
43517fab473SAndy Shevchenko 		value &= ~PADCFG0_GPIOTXDIS;
43617fab473SAndy Shevchenko 		value |= PADCFG0_GPIORXDIS;
43717fab473SAndy Shevchenko 	}
43817fab473SAndy Shevchenko 	writel(value, padcfg0);
43917fab473SAndy Shevchenko }
44017fab473SAndy Shevchenko 
4414973ddc8SAndy Shevchenko static int intel_gpio_get_gpio_mode(void __iomem *padcfg0)
4424973ddc8SAndy Shevchenko {
4434973ddc8SAndy Shevchenko 	return (readl(padcfg0) & PADCFG0_PMODE_MASK) >> PADCFG0_PMODE_SHIFT;
4444973ddc8SAndy Shevchenko }
4454973ddc8SAndy Shevchenko 
446f5a26acfSMika Westerberg static void intel_gpio_set_gpio_mode(void __iomem *padcfg0)
447f5a26acfSMika Westerberg {
448f5a26acfSMika Westerberg 	u32 value;
449f5a26acfSMika Westerberg 
450af7e3eebSAndy Shevchenko 	value = readl(padcfg0);
451af7e3eebSAndy Shevchenko 
452f5a26acfSMika Westerberg 	/* Put the pad into GPIO mode */
453af7e3eebSAndy Shevchenko 	value &= ~PADCFG0_PMODE_MASK;
454af7e3eebSAndy Shevchenko 	value |= PADCFG0_PMODE_GPIO;
455af7e3eebSAndy Shevchenko 
456e12963c4SAndy Shevchenko 	/* Disable TX buffer and enable RX (this will be input) */
457e12963c4SAndy Shevchenko 	value &= ~PADCFG0_GPIORXDIS;
458e8873c0aSAndy Shevchenko 	value |= PADCFG0_GPIOTXDIS;
459af7e3eebSAndy Shevchenko 
460f5a26acfSMika Westerberg 	/* Disable SCI/SMI/NMI generation */
461f5a26acfSMika Westerberg 	value &= ~(PADCFG0_GPIROUTIOXAPIC | PADCFG0_GPIROUTSCI);
462f5a26acfSMika Westerberg 	value &= ~(PADCFG0_GPIROUTSMI | PADCFG0_GPIROUTNMI);
463af7e3eebSAndy Shevchenko 
464f5a26acfSMika Westerberg 	writel(value, padcfg0);
465f5a26acfSMika Westerberg }
466f5a26acfSMika Westerberg 
4677981c001SMika Westerberg static int intel_gpio_request_enable(struct pinctrl_dev *pctldev,
4687981c001SMika Westerberg 				     struct pinctrl_gpio_range *range,
46904035f7fSAndy Shevchenko 				     unsigned int pin)
4707981c001SMika Westerberg {
4717981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
4727981c001SMika Westerberg 	void __iomem *padcfg0;
4737981c001SMika Westerberg 	unsigned long flags;
4747981c001SMika Westerberg 
475f62cdde5SAndy Shevchenko 	padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
476f62cdde5SAndy Shevchenko 
47727d9098cSMika Westerberg 	raw_spin_lock_irqsave(&pctrl->lock, flags);
4787981c001SMika Westerberg 
4791bd23153SAndy Shevchenko 	if (!intel_pad_owned_by_host(pctrl, pin)) {
48027d9098cSMika Westerberg 		raw_spin_unlock_irqrestore(&pctrl->lock, flags);
4817981c001SMika Westerberg 		return -EBUSY;
4827981c001SMika Westerberg 	}
4837981c001SMika Westerberg 
4841bd23153SAndy Shevchenko 	if (!intel_pad_is_unlocked(pctrl, pin)) {
4851bd23153SAndy Shevchenko 		raw_spin_unlock_irqrestore(&pctrl->lock, flags);
4861bd23153SAndy Shevchenko 		return 0;
4871bd23153SAndy Shevchenko 	}
4881bd23153SAndy Shevchenko 
4894973ddc8SAndy Shevchenko 	/*
4904973ddc8SAndy Shevchenko 	 * If pin is already configured in GPIO mode, we assume that
4914973ddc8SAndy Shevchenko 	 * firmware provides correct settings. In such case we avoid
4924973ddc8SAndy Shevchenko 	 * potential glitches on the pin. Otherwise, for the pin in
4934973ddc8SAndy Shevchenko 	 * alternative mode, consumer has to supply respective flags.
4944973ddc8SAndy Shevchenko 	 */
4954973ddc8SAndy Shevchenko 	if (intel_gpio_get_gpio_mode(padcfg0) == PADCFG0_PMODE_GPIO) {
4964973ddc8SAndy Shevchenko 		raw_spin_unlock_irqrestore(&pctrl->lock, flags);
4974973ddc8SAndy Shevchenko 		return 0;
4984973ddc8SAndy Shevchenko 	}
4994973ddc8SAndy Shevchenko 
500f5a26acfSMika Westerberg 	intel_gpio_set_gpio_mode(padcfg0);
5014973ddc8SAndy Shevchenko 
50227d9098cSMika Westerberg 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
5037981c001SMika Westerberg 
5047981c001SMika Westerberg 	return 0;
5057981c001SMika Westerberg }
5067981c001SMika Westerberg 
5077981c001SMika Westerberg static int intel_gpio_set_direction(struct pinctrl_dev *pctldev,
5087981c001SMika Westerberg 				    struct pinctrl_gpio_range *range,
50904035f7fSAndy Shevchenko 				    unsigned int pin, bool input)
5107981c001SMika Westerberg {
5117981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
5127981c001SMika Westerberg 	void __iomem *padcfg0;
5137981c001SMika Westerberg 	unsigned long flags;
5147981c001SMika Westerberg 
5157981c001SMika Westerberg 	padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
5167981c001SMika Westerberg 
517f62cdde5SAndy Shevchenko 	raw_spin_lock_irqsave(&pctrl->lock, flags);
518f62cdde5SAndy Shevchenko 	__intel_gpio_set_direction(padcfg0, input);
51927d9098cSMika Westerberg 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
5207981c001SMika Westerberg 
5217981c001SMika Westerberg 	return 0;
5227981c001SMika Westerberg }
5237981c001SMika Westerberg 
5247981c001SMika Westerberg static const struct pinmux_ops intel_pinmux_ops = {
5257981c001SMika Westerberg 	.get_functions_count = intel_get_functions_count,
5267981c001SMika Westerberg 	.get_function_name = intel_get_function_name,
5277981c001SMika Westerberg 	.get_function_groups = intel_get_function_groups,
5287981c001SMika Westerberg 	.set_mux = intel_pinmux_set_mux,
5297981c001SMika Westerberg 	.gpio_request_enable = intel_gpio_request_enable,
5307981c001SMika Westerberg 	.gpio_set_direction = intel_gpio_set_direction,
5317981c001SMika Westerberg };
5327981c001SMika Westerberg 
53381ab5542SAndy Shevchenko static int intel_config_get_pull(struct intel_pinctrl *pctrl, unsigned int pin,
53481ab5542SAndy Shevchenko 				 enum pin_config_param param, u32 *arg)
5357981c001SMika Westerberg {
53604cc058fSMika Westerberg 	const struct intel_community *community;
53781ab5542SAndy Shevchenko 	void __iomem *padcfg1;
538e64fbfa5SAndy Shevchenko 	unsigned long flags;
5397981c001SMika Westerberg 	u32 value, term;
5407981c001SMika Westerberg 
54104cc058fSMika Westerberg 	community = intel_get_community(pctrl, pin);
54281ab5542SAndy Shevchenko 	padcfg1 = intel_get_padcfg(pctrl, pin, PADCFG1);
543e64fbfa5SAndy Shevchenko 
544e64fbfa5SAndy Shevchenko 	raw_spin_lock_irqsave(&pctrl->lock, flags);
54581ab5542SAndy Shevchenko 	value = readl(padcfg1);
546e64fbfa5SAndy Shevchenko 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
54781ab5542SAndy Shevchenko 
5487981c001SMika Westerberg 	term = (value & PADCFG1_TERM_MASK) >> PADCFG1_TERM_SHIFT;
5497981c001SMika Westerberg 
5507981c001SMika Westerberg 	switch (param) {
5517981c001SMika Westerberg 	case PIN_CONFIG_BIAS_DISABLE:
5527981c001SMika Westerberg 		if (term)
5537981c001SMika Westerberg 			return -EINVAL;
5547981c001SMika Westerberg 		break;
5557981c001SMika Westerberg 
5567981c001SMika Westerberg 	case PIN_CONFIG_BIAS_PULL_UP:
5577981c001SMika Westerberg 		if (!term || !(value & PADCFG1_TERM_UP))
5587981c001SMika Westerberg 			return -EINVAL;
5597981c001SMika Westerberg 
5607981c001SMika Westerberg 		switch (term) {
561dd26209bSAndy Shevchenko 		case PADCFG1_TERM_833:
562dd26209bSAndy Shevchenko 			*arg = 833;
563dd26209bSAndy Shevchenko 			break;
5647981c001SMika Westerberg 		case PADCFG1_TERM_1K:
56581ab5542SAndy Shevchenko 			*arg = 1000;
5667981c001SMika Westerberg 			break;
5677981c001SMika Westerberg 		case PADCFG1_TERM_5K:
56881ab5542SAndy Shevchenko 			*arg = 5000;
5697981c001SMika Westerberg 			break;
5707981c001SMika Westerberg 		case PADCFG1_TERM_20K:
57181ab5542SAndy Shevchenko 			*arg = 20000;
5727981c001SMika Westerberg 			break;
5737981c001SMika Westerberg 		}
5747981c001SMika Westerberg 
5757981c001SMika Westerberg 		break;
5767981c001SMika Westerberg 
5777981c001SMika Westerberg 	case PIN_CONFIG_BIAS_PULL_DOWN:
5787981c001SMika Westerberg 		if (!term || value & PADCFG1_TERM_UP)
5797981c001SMika Westerberg 			return -EINVAL;
5807981c001SMika Westerberg 
5817981c001SMika Westerberg 		switch (term) {
582dd26209bSAndy Shevchenko 		case PADCFG1_TERM_833:
583dd26209bSAndy Shevchenko 			if (!(community->features & PINCTRL_FEATURE_1K_PD))
584dd26209bSAndy Shevchenko 				return -EINVAL;
585dd26209bSAndy Shevchenko 			*arg = 833;
586dd26209bSAndy Shevchenko 			break;
58704cc058fSMika Westerberg 		case PADCFG1_TERM_1K:
58804cc058fSMika Westerberg 			if (!(community->features & PINCTRL_FEATURE_1K_PD))
58904cc058fSMika Westerberg 				return -EINVAL;
59081ab5542SAndy Shevchenko 			*arg = 1000;
59104cc058fSMika Westerberg 			break;
5927981c001SMika Westerberg 		case PADCFG1_TERM_5K:
59381ab5542SAndy Shevchenko 			*arg = 5000;
5947981c001SMika Westerberg 			break;
5957981c001SMika Westerberg 		case PADCFG1_TERM_20K:
59681ab5542SAndy Shevchenko 			*arg = 20000;
5977981c001SMika Westerberg 			break;
5987981c001SMika Westerberg 		}
5997981c001SMika Westerberg 
6007981c001SMika Westerberg 		break;
6017981c001SMika Westerberg 
60281ab5542SAndy Shevchenko 	default:
60381ab5542SAndy Shevchenko 		return -EINVAL;
60481ab5542SAndy Shevchenko 	}
60581ab5542SAndy Shevchenko 
60681ab5542SAndy Shevchenko 	return 0;
60781ab5542SAndy Shevchenko }
60881ab5542SAndy Shevchenko 
60981ab5542SAndy Shevchenko static int intel_config_get_debounce(struct intel_pinctrl *pctrl, unsigned int pin,
61081ab5542SAndy Shevchenko 				     enum pin_config_param param, u32 *arg)
61181ab5542SAndy Shevchenko {
612e57725eaSMika Westerberg 	void __iomem *padcfg2;
613e64fbfa5SAndy Shevchenko 	unsigned long flags;
61481ab5542SAndy Shevchenko 	unsigned long v;
61581ab5542SAndy Shevchenko 	u32 value2;
616e57725eaSMika Westerberg 
617e57725eaSMika Westerberg 	padcfg2 = intel_get_padcfg(pctrl, pin, PADCFG2);
618e57725eaSMika Westerberg 	if (!padcfg2)
619e57725eaSMika Westerberg 		return -ENOTSUPP;
620e57725eaSMika Westerberg 
621e64fbfa5SAndy Shevchenko 	raw_spin_lock_irqsave(&pctrl->lock, flags);
62281ab5542SAndy Shevchenko 	value2 = readl(padcfg2);
623e64fbfa5SAndy Shevchenko 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
62481ab5542SAndy Shevchenko 	if (!(value2 & PADCFG2_DEBEN))
625e57725eaSMika Westerberg 		return -EINVAL;
626e57725eaSMika Westerberg 
62781ab5542SAndy Shevchenko 	v = (value2 & PADCFG2_DEBOUNCE_MASK) >> PADCFG2_DEBOUNCE_SHIFT;
62881ab5542SAndy Shevchenko 	*arg = BIT(v) * DEBOUNCE_PERIOD_NSEC / NSEC_PER_USEC;
629e57725eaSMika Westerberg 
63081ab5542SAndy Shevchenko 	return 0;
631e57725eaSMika Westerberg }
632e57725eaSMika Westerberg 
63381ab5542SAndy Shevchenko static int intel_config_get(struct pinctrl_dev *pctldev, unsigned int pin,
63481ab5542SAndy Shevchenko 			    unsigned long *config)
63581ab5542SAndy Shevchenko {
63681ab5542SAndy Shevchenko 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
63781ab5542SAndy Shevchenko 	enum pin_config_param param = pinconf_to_config_param(*config);
63881ab5542SAndy Shevchenko 	u32 arg = 0;
63981ab5542SAndy Shevchenko 	int ret;
64081ab5542SAndy Shevchenko 
64181ab5542SAndy Shevchenko 	if (!intel_pad_owned_by_host(pctrl, pin))
64281ab5542SAndy Shevchenko 		return -ENOTSUPP;
64381ab5542SAndy Shevchenko 
64481ab5542SAndy Shevchenko 	switch (param) {
64581ab5542SAndy Shevchenko 	case PIN_CONFIG_BIAS_DISABLE:
64681ab5542SAndy Shevchenko 	case PIN_CONFIG_BIAS_PULL_UP:
64781ab5542SAndy Shevchenko 	case PIN_CONFIG_BIAS_PULL_DOWN:
64881ab5542SAndy Shevchenko 		ret = intel_config_get_pull(pctrl, pin, param, &arg);
64981ab5542SAndy Shevchenko 		if (ret)
65081ab5542SAndy Shevchenko 			return ret;
65181ab5542SAndy Shevchenko 		break;
65281ab5542SAndy Shevchenko 
65381ab5542SAndy Shevchenko 	case PIN_CONFIG_INPUT_DEBOUNCE:
65481ab5542SAndy Shevchenko 		ret = intel_config_get_debounce(pctrl, pin, param, &arg);
65581ab5542SAndy Shevchenko 		if (ret)
65681ab5542SAndy Shevchenko 			return ret;
65781ab5542SAndy Shevchenko 		break;
65881ab5542SAndy Shevchenko 
6597981c001SMika Westerberg 	default:
6607981c001SMika Westerberg 		return -ENOTSUPP;
6617981c001SMika Westerberg 	}
6627981c001SMika Westerberg 
6637981c001SMika Westerberg 	*config = pinconf_to_config_packed(param, arg);
6647981c001SMika Westerberg 	return 0;
6657981c001SMika Westerberg }
6667981c001SMika Westerberg 
66704035f7fSAndy Shevchenko static int intel_config_set_pull(struct intel_pinctrl *pctrl, unsigned int pin,
6687981c001SMika Westerberg 				 unsigned long config)
6697981c001SMika Westerberg {
67004035f7fSAndy Shevchenko 	unsigned int param = pinconf_to_config_param(config);
67104035f7fSAndy Shevchenko 	unsigned int arg = pinconf_to_config_argument(config);
67204cc058fSMika Westerberg 	const struct intel_community *community;
6737981c001SMika Westerberg 	void __iomem *padcfg1;
6747981c001SMika Westerberg 	unsigned long flags;
6757981c001SMika Westerberg 	int ret = 0;
6767981c001SMika Westerberg 	u32 value;
6777981c001SMika Westerberg 
67804cc058fSMika Westerberg 	community = intel_get_community(pctrl, pin);
6797981c001SMika Westerberg 	padcfg1 = intel_get_padcfg(pctrl, pin, PADCFG1);
680f62cdde5SAndy Shevchenko 
681f62cdde5SAndy Shevchenko 	raw_spin_lock_irqsave(&pctrl->lock, flags);
682f62cdde5SAndy Shevchenko 
6837981c001SMika Westerberg 	value = readl(padcfg1);
6847981c001SMika Westerberg 
6857981c001SMika Westerberg 	switch (param) {
6867981c001SMika Westerberg 	case PIN_CONFIG_BIAS_DISABLE:
6877981c001SMika Westerberg 		value &= ~(PADCFG1_TERM_MASK | PADCFG1_TERM_UP);
6887981c001SMika Westerberg 		break;
6897981c001SMika Westerberg 
6907981c001SMika Westerberg 	case PIN_CONFIG_BIAS_PULL_UP:
6917981c001SMika Westerberg 		value &= ~PADCFG1_TERM_MASK;
6927981c001SMika Westerberg 
6937981c001SMika Westerberg 		value |= PADCFG1_TERM_UP;
6947981c001SMika Westerberg 
695f3c75e7aSAndy Shevchenko 		/* Set default strength value in case none is given */
696f3c75e7aSAndy Shevchenko 		if (arg == 1)
697f3c75e7aSAndy Shevchenko 			arg = 5000;
698f3c75e7aSAndy Shevchenko 
6997981c001SMika Westerberg 		switch (arg) {
7007981c001SMika Westerberg 		case 20000:
7017981c001SMika Westerberg 			value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT;
7027981c001SMika Westerberg 			break;
7037981c001SMika Westerberg 		case 5000:
7047981c001SMika Westerberg 			value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT;
7057981c001SMika Westerberg 			break;
7067981c001SMika Westerberg 		case 1000:
7077981c001SMika Westerberg 			value |= PADCFG1_TERM_1K << PADCFG1_TERM_SHIFT;
7087981c001SMika Westerberg 			break;
709dd26209bSAndy Shevchenko 		case 833:
710dd26209bSAndy Shevchenko 			value |= PADCFG1_TERM_833 << PADCFG1_TERM_SHIFT;
711dd26209bSAndy Shevchenko 			break;
7127981c001SMika Westerberg 		default:
7137981c001SMika Westerberg 			ret = -EINVAL;
7147981c001SMika Westerberg 		}
7157981c001SMika Westerberg 
7167981c001SMika Westerberg 		break;
7177981c001SMika Westerberg 
7187981c001SMika Westerberg 	case PIN_CONFIG_BIAS_PULL_DOWN:
7197981c001SMika Westerberg 		value &= ~(PADCFG1_TERM_UP | PADCFG1_TERM_MASK);
7207981c001SMika Westerberg 
721f3c75e7aSAndy Shevchenko 		/* Set default strength value in case none is given */
722f3c75e7aSAndy Shevchenko 		if (arg == 1)
723f3c75e7aSAndy Shevchenko 			arg = 5000;
724f3c75e7aSAndy Shevchenko 
7257981c001SMika Westerberg 		switch (arg) {
7267981c001SMika Westerberg 		case 20000:
7277981c001SMika Westerberg 			value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT;
7287981c001SMika Westerberg 			break;
7297981c001SMika Westerberg 		case 5000:
7307981c001SMika Westerberg 			value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT;
7317981c001SMika Westerberg 			break;
73204cc058fSMika Westerberg 		case 1000:
733aa1dd80fSDan Carpenter 			if (!(community->features & PINCTRL_FEATURE_1K_PD)) {
734aa1dd80fSDan Carpenter 				ret = -EINVAL;
735aa1dd80fSDan Carpenter 				break;
736aa1dd80fSDan Carpenter 			}
73704cc058fSMika Westerberg 			value |= PADCFG1_TERM_1K << PADCFG1_TERM_SHIFT;
73804cc058fSMika Westerberg 			break;
739dd26209bSAndy Shevchenko 		case 833:
740dd26209bSAndy Shevchenko 			if (!(community->features & PINCTRL_FEATURE_1K_PD)) {
741dd26209bSAndy Shevchenko 				ret = -EINVAL;
742dd26209bSAndy Shevchenko 				break;
743dd26209bSAndy Shevchenko 			}
744dd26209bSAndy Shevchenko 			value |= PADCFG1_TERM_833 << PADCFG1_TERM_SHIFT;
745dd26209bSAndy Shevchenko 			break;
7467981c001SMika Westerberg 		default:
7477981c001SMika Westerberg 			ret = -EINVAL;
7487981c001SMika Westerberg 		}
7497981c001SMika Westerberg 
7507981c001SMika Westerberg 		break;
7517981c001SMika Westerberg 	}
7527981c001SMika Westerberg 
7537981c001SMika Westerberg 	if (!ret)
7547981c001SMika Westerberg 		writel(value, padcfg1);
7557981c001SMika Westerberg 
75627d9098cSMika Westerberg 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
7577981c001SMika Westerberg 
7587981c001SMika Westerberg 	return ret;
7597981c001SMika Westerberg }
7607981c001SMika Westerberg 
76104035f7fSAndy Shevchenko static int intel_config_set_debounce(struct intel_pinctrl *pctrl,
76204035f7fSAndy Shevchenko 				     unsigned int pin, unsigned int debounce)
763e57725eaSMika Westerberg {
764e57725eaSMika Westerberg 	void __iomem *padcfg0, *padcfg2;
765e57725eaSMika Westerberg 	unsigned long flags;
766e57725eaSMika Westerberg 	u32 value0, value2;
767e57725eaSMika Westerberg 
768e57725eaSMika Westerberg 	padcfg2 = intel_get_padcfg(pctrl, pin, PADCFG2);
769e57725eaSMika Westerberg 	if (!padcfg2)
770e57725eaSMika Westerberg 		return -ENOTSUPP;
771e57725eaSMika Westerberg 
772e57725eaSMika Westerberg 	padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
773e57725eaSMika Westerberg 
774e57725eaSMika Westerberg 	raw_spin_lock_irqsave(&pctrl->lock, flags);
775e57725eaSMika Westerberg 
776e57725eaSMika Westerberg 	value0 = readl(padcfg0);
777e57725eaSMika Westerberg 	value2 = readl(padcfg2);
778e57725eaSMika Westerberg 
779e57725eaSMika Westerberg 	/* Disable glitch filter and debouncer */
780e57725eaSMika Westerberg 	value0 &= ~PADCFG0_PREGFRXSEL;
781e57725eaSMika Westerberg 	value2 &= ~(PADCFG2_DEBEN | PADCFG2_DEBOUNCE_MASK);
782e57725eaSMika Westerberg 
783e57725eaSMika Westerberg 	if (debounce) {
784e57725eaSMika Westerberg 		unsigned long v;
785e57725eaSMika Westerberg 
7866a33a1d6SAndy Shevchenko 		v = order_base_2(debounce * NSEC_PER_USEC / DEBOUNCE_PERIOD_NSEC);
787e57725eaSMika Westerberg 		if (v < 3 || v > 15) {
7888fff0427SAndy Shevchenko 			raw_spin_unlock_irqrestore(&pctrl->lock, flags);
7898fff0427SAndy Shevchenko 			return -EINVAL;
790bb2f43d4SAndy Shevchenko 		}
791bb2f43d4SAndy Shevchenko 
792e57725eaSMika Westerberg 		/* Enable glitch filter and debouncer */
793e57725eaSMika Westerberg 		value0 |= PADCFG0_PREGFRXSEL;
794e57725eaSMika Westerberg 		value2 |= v << PADCFG2_DEBOUNCE_SHIFT;
795e57725eaSMika Westerberg 		value2 |= PADCFG2_DEBEN;
796e57725eaSMika Westerberg 	}
797e57725eaSMika Westerberg 
798e57725eaSMika Westerberg 	writel(value0, padcfg0);
799e57725eaSMika Westerberg 	writel(value2, padcfg2);
800e57725eaSMika Westerberg 
801e57725eaSMika Westerberg 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
802e57725eaSMika Westerberg 
8038fff0427SAndy Shevchenko 	return 0;
804e57725eaSMika Westerberg }
805e57725eaSMika Westerberg 
80604035f7fSAndy Shevchenko static int intel_config_set(struct pinctrl_dev *pctldev, unsigned int pin,
80704035f7fSAndy Shevchenko 			  unsigned long *configs, unsigned int nconfigs)
8087981c001SMika Westerberg {
8097981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
8107981c001SMika Westerberg 	int i, ret;
8117981c001SMika Westerberg 
8127981c001SMika Westerberg 	if (!intel_pad_usable(pctrl, pin))
8137981c001SMika Westerberg 		return -ENOTSUPP;
8147981c001SMika Westerberg 
8157981c001SMika Westerberg 	for (i = 0; i < nconfigs; i++) {
8167981c001SMika Westerberg 		switch (pinconf_to_config_param(configs[i])) {
8177981c001SMika Westerberg 		case PIN_CONFIG_BIAS_DISABLE:
8187981c001SMika Westerberg 		case PIN_CONFIG_BIAS_PULL_UP:
8197981c001SMika Westerberg 		case PIN_CONFIG_BIAS_PULL_DOWN:
8207981c001SMika Westerberg 			ret = intel_config_set_pull(pctrl, pin, configs[i]);
8217981c001SMika Westerberg 			if (ret)
8227981c001SMika Westerberg 				return ret;
8237981c001SMika Westerberg 			break;
8247981c001SMika Westerberg 
825e57725eaSMika Westerberg 		case PIN_CONFIG_INPUT_DEBOUNCE:
826e57725eaSMika Westerberg 			ret = intel_config_set_debounce(pctrl, pin,
827e57725eaSMika Westerberg 				pinconf_to_config_argument(configs[i]));
828e57725eaSMika Westerberg 			if (ret)
829e57725eaSMika Westerberg 				return ret;
830e57725eaSMika Westerberg 			break;
831e57725eaSMika Westerberg 
8327981c001SMika Westerberg 		default:
8337981c001SMika Westerberg 			return -ENOTSUPP;
8347981c001SMika Westerberg 		}
8357981c001SMika Westerberg 	}
8367981c001SMika Westerberg 
8377981c001SMika Westerberg 	return 0;
8387981c001SMika Westerberg }
8397981c001SMika Westerberg 
8407981c001SMika Westerberg static const struct pinconf_ops intel_pinconf_ops = {
8417981c001SMika Westerberg 	.is_generic = true,
8427981c001SMika Westerberg 	.pin_config_get = intel_config_get,
8437981c001SMika Westerberg 	.pin_config_set = intel_config_set,
8447981c001SMika Westerberg };
8457981c001SMika Westerberg 
8467981c001SMika Westerberg static const struct pinctrl_desc intel_pinctrl_desc = {
8477981c001SMika Westerberg 	.pctlops = &intel_pinctrl_ops,
8487981c001SMika Westerberg 	.pmxops = &intel_pinmux_ops,
8497981c001SMika Westerberg 	.confops = &intel_pinconf_ops,
8507981c001SMika Westerberg 	.owner = THIS_MODULE,
8517981c001SMika Westerberg };
8527981c001SMika Westerberg 
853a60eac32SMika Westerberg /**
854a60eac32SMika Westerberg  * intel_gpio_to_pin() - Translate from GPIO offset to pin number
855a60eac32SMika Westerberg  * @pctrl: Pinctrl structure
856a60eac32SMika Westerberg  * @offset: GPIO offset from gpiolib
857946ffefcSAndy Shevchenko  * @community: Community is filled here if not %NULL
858a60eac32SMika Westerberg  * @padgrp: Pad group is filled here if not %NULL
859a60eac32SMika Westerberg  *
860a60eac32SMika Westerberg  * When coming through gpiolib irqchip, the GPIO offset is not
861a60eac32SMika Westerberg  * automatically translated to pinctrl pin number. This function can be
862a60eac32SMika Westerberg  * used to find out the corresponding pinctrl pin.
8637b923e67SAndy Shevchenko  *
8647b923e67SAndy Shevchenko  * Return: a pin number and pointers to the community and pad group, which
8657b923e67SAndy Shevchenko  * the pin belongs to, or negative error code if translation can't be done.
866a60eac32SMika Westerberg  */
86704035f7fSAndy Shevchenko static int intel_gpio_to_pin(struct intel_pinctrl *pctrl, unsigned int offset,
868a60eac32SMika Westerberg 			     const struct intel_community **community,
869a60eac32SMika Westerberg 			     const struct intel_padgroup **padgrp)
870a60eac32SMika Westerberg {
871a60eac32SMika Westerberg 	int i;
872a60eac32SMika Westerberg 
873a60eac32SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
874a60eac32SMika Westerberg 		const struct intel_community *comm = &pctrl->communities[i];
875a60eac32SMika Westerberg 		int j;
876a60eac32SMika Westerberg 
877a60eac32SMika Westerberg 		for (j = 0; j < comm->ngpps; j++) {
878a60eac32SMika Westerberg 			const struct intel_padgroup *pgrp = &comm->gpps[j];
879a60eac32SMika Westerberg 
880e5a4ab6aSAndy Shevchenko 			if (pgrp->gpio_base == INTEL_GPIO_BASE_NOMAP)
881a60eac32SMika Westerberg 				continue;
882a60eac32SMika Westerberg 
883a60eac32SMika Westerberg 			if (offset >= pgrp->gpio_base &&
884a60eac32SMika Westerberg 			    offset < pgrp->gpio_base + pgrp->size) {
885a60eac32SMika Westerberg 				int pin;
886a60eac32SMika Westerberg 
887a60eac32SMika Westerberg 				pin = pgrp->base + offset - pgrp->gpio_base;
888a60eac32SMika Westerberg 				if (community)
889a60eac32SMika Westerberg 					*community = comm;
890a60eac32SMika Westerberg 				if (padgrp)
891a60eac32SMika Westerberg 					*padgrp = pgrp;
892a60eac32SMika Westerberg 
893a60eac32SMika Westerberg 				return pin;
894a60eac32SMika Westerberg 			}
895a60eac32SMika Westerberg 		}
896a60eac32SMika Westerberg 	}
897a60eac32SMika Westerberg 
898a60eac32SMika Westerberg 	return -EINVAL;
899a60eac32SMika Westerberg }
900a60eac32SMika Westerberg 
9016cb0880fSChris Chiu /**
9026cb0880fSChris Chiu  * intel_pin_to_gpio() - Translate from pin number to GPIO offset
9036cb0880fSChris Chiu  * @pctrl: Pinctrl structure
9046cb0880fSChris Chiu  * @pin: pin number
9056cb0880fSChris Chiu  *
9066cb0880fSChris Chiu  * Translate the pin number of pinctrl to GPIO offset
9077b923e67SAndy Shevchenko  *
9087b923e67SAndy Shevchenko  * Return: a GPIO offset, or negative error code if translation can't be done.
9096cb0880fSChris Chiu  */
91055dac437SArnd Bergmann static __maybe_unused int intel_pin_to_gpio(struct intel_pinctrl *pctrl, int pin)
9116cb0880fSChris Chiu {
9126cb0880fSChris Chiu 	const struct intel_community *community;
9136cb0880fSChris Chiu 	const struct intel_padgroup *padgrp;
9146cb0880fSChris Chiu 
9156cb0880fSChris Chiu 	community = intel_get_community(pctrl, pin);
9166cb0880fSChris Chiu 	if (!community)
9176cb0880fSChris Chiu 		return -EINVAL;
9186cb0880fSChris Chiu 
9196cb0880fSChris Chiu 	padgrp = intel_community_get_padgroup(community, pin);
9206cb0880fSChris Chiu 	if (!padgrp)
9216cb0880fSChris Chiu 		return -EINVAL;
9226cb0880fSChris Chiu 
9236cb0880fSChris Chiu 	return pin - padgrp->base + padgrp->gpio_base;
9246cb0880fSChris Chiu }
9256cb0880fSChris Chiu 
92604035f7fSAndy Shevchenko static int intel_gpio_get(struct gpio_chip *chip, unsigned int offset)
92755aedef5SAndy Shevchenko {
92896147db1SMika Westerberg 	struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
92996147db1SMika Westerberg 	void __iomem *reg;
93096147db1SMika Westerberg 	u32 padcfg0;
93155aedef5SAndy Shevchenko 	int pin;
93255aedef5SAndy Shevchenko 
93396147db1SMika Westerberg 	pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL);
93496147db1SMika Westerberg 	if (pin < 0)
93596147db1SMika Westerberg 		return -EINVAL;
93696147db1SMika Westerberg 
93796147db1SMika Westerberg 	reg = intel_get_padcfg(pctrl, pin, PADCFG0);
93896147db1SMika Westerberg 	if (!reg)
93996147db1SMika Westerberg 		return -EINVAL;
94096147db1SMika Westerberg 
94196147db1SMika Westerberg 	padcfg0 = readl(reg);
94296147db1SMika Westerberg 	if (!(padcfg0 & PADCFG0_GPIOTXDIS))
94396147db1SMika Westerberg 		return !!(padcfg0 & PADCFG0_GPIOTXSTATE);
94496147db1SMika Westerberg 
94596147db1SMika Westerberg 	return !!(padcfg0 & PADCFG0_GPIORXSTATE);
94655aedef5SAndy Shevchenko }
94755aedef5SAndy Shevchenko 
94804035f7fSAndy Shevchenko static void intel_gpio_set(struct gpio_chip *chip, unsigned int offset,
94904035f7fSAndy Shevchenko 			   int value)
95096147db1SMika Westerberg {
95196147db1SMika Westerberg 	struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
95296147db1SMika Westerberg 	unsigned long flags;
95396147db1SMika Westerberg 	void __iomem *reg;
95496147db1SMika Westerberg 	u32 padcfg0;
95596147db1SMika Westerberg 	int pin;
95696147db1SMika Westerberg 
95796147db1SMika Westerberg 	pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL);
95896147db1SMika Westerberg 	if (pin < 0)
95996147db1SMika Westerberg 		return;
96096147db1SMika Westerberg 
96196147db1SMika Westerberg 	reg = intel_get_padcfg(pctrl, pin, PADCFG0);
96296147db1SMika Westerberg 	if (!reg)
96396147db1SMika Westerberg 		return;
96496147db1SMika Westerberg 
96596147db1SMika Westerberg 	raw_spin_lock_irqsave(&pctrl->lock, flags);
96696147db1SMika Westerberg 	padcfg0 = readl(reg);
96796147db1SMika Westerberg 	if (value)
96896147db1SMika Westerberg 		padcfg0 |= PADCFG0_GPIOTXSTATE;
96996147db1SMika Westerberg 	else
97096147db1SMika Westerberg 		padcfg0 &= ~PADCFG0_GPIOTXSTATE;
97196147db1SMika Westerberg 	writel(padcfg0, reg);
97296147db1SMika Westerberg 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
97396147db1SMika Westerberg }
97496147db1SMika Westerberg 
97596147db1SMika Westerberg static int intel_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
97696147db1SMika Westerberg {
97796147db1SMika Westerberg 	struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
978e64fbfa5SAndy Shevchenko 	unsigned long flags;
97996147db1SMika Westerberg 	void __iomem *reg;
98096147db1SMika Westerberg 	u32 padcfg0;
98196147db1SMika Westerberg 	int pin;
98296147db1SMika Westerberg 
98396147db1SMika Westerberg 	pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL);
98496147db1SMika Westerberg 	if (pin < 0)
98596147db1SMika Westerberg 		return -EINVAL;
98696147db1SMika Westerberg 
98796147db1SMika Westerberg 	reg = intel_get_padcfg(pctrl, pin, PADCFG0);
98896147db1SMika Westerberg 	if (!reg)
98996147db1SMika Westerberg 		return -EINVAL;
99096147db1SMika Westerberg 
991e64fbfa5SAndy Shevchenko 	raw_spin_lock_irqsave(&pctrl->lock, flags);
99296147db1SMika Westerberg 	padcfg0 = readl(reg);
993e64fbfa5SAndy Shevchenko 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
99496147db1SMika Westerberg 	if (padcfg0 & PADCFG0_PMODE_MASK)
99596147db1SMika Westerberg 		return -EINVAL;
99696147db1SMika Westerberg 
9976a304752SMatti Vaittinen 	if (padcfg0 & PADCFG0_GPIOTXDIS)
9986a304752SMatti Vaittinen 		return GPIO_LINE_DIRECTION_IN;
9996a304752SMatti Vaittinen 
10006a304752SMatti Vaittinen 	return GPIO_LINE_DIRECTION_OUT;
100196147db1SMika Westerberg }
100296147db1SMika Westerberg 
100304035f7fSAndy Shevchenko static int intel_gpio_direction_input(struct gpio_chip *chip, unsigned int offset)
100496147db1SMika Westerberg {
100596147db1SMika Westerberg 	return pinctrl_gpio_direction_input(chip->base + offset);
100696147db1SMika Westerberg }
100796147db1SMika Westerberg 
100804035f7fSAndy Shevchenko static int intel_gpio_direction_output(struct gpio_chip *chip, unsigned int offset,
100996147db1SMika Westerberg 				       int value)
101096147db1SMika Westerberg {
101196147db1SMika Westerberg 	intel_gpio_set(chip, offset, value);
101296147db1SMika Westerberg 	return pinctrl_gpio_direction_output(chip->base + offset);
101396147db1SMika Westerberg }
101496147db1SMika Westerberg 
101596147db1SMika Westerberg static const struct gpio_chip intel_gpio_chip = {
101696147db1SMika Westerberg 	.owner = THIS_MODULE,
101796147db1SMika Westerberg 	.request = gpiochip_generic_request,
101896147db1SMika Westerberg 	.free = gpiochip_generic_free,
101996147db1SMika Westerberg 	.get_direction = intel_gpio_get_direction,
102096147db1SMika Westerberg 	.direction_input = intel_gpio_direction_input,
102196147db1SMika Westerberg 	.direction_output = intel_gpio_direction_output,
102296147db1SMika Westerberg 	.get = intel_gpio_get,
102396147db1SMika Westerberg 	.set = intel_gpio_set,
102496147db1SMika Westerberg 	.set_config = gpiochip_generic_config,
102596147db1SMika Westerberg };
102696147db1SMika Westerberg 
10277981c001SMika Westerberg static void intel_gpio_irq_ack(struct irq_data *d)
10287981c001SMika Westerberg {
10297981c001SMika Westerberg 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1030acfd4c63SLinus Walleij 	struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
10317981c001SMika Westerberg 	const struct intel_community *community;
1032919eb475SMika Westerberg 	const struct intel_padgroup *padgrp;
1033a60eac32SMika Westerberg 	int pin;
10347981c001SMika Westerberg 
1035a60eac32SMika Westerberg 	pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), &community, &padgrp);
1036a60eac32SMika Westerberg 	if (pin >= 0) {
103704035f7fSAndy Shevchenko 		unsigned int gpp, gpp_offset, is_offset;
1038919eb475SMika Westerberg 
1039919eb475SMika Westerberg 		gpp = padgrp->reg_num;
1040919eb475SMika Westerberg 		gpp_offset = padgroup_offset(padgrp, pin);
1041cf769bd8SMika Westerberg 		is_offset = community->is_offset + gpp * 4;
1042919eb475SMika Westerberg 
1043919eb475SMika Westerberg 		raw_spin_lock(&pctrl->lock);
1044cf769bd8SMika Westerberg 		writel(BIT(gpp_offset), community->regs + is_offset);
104527d9098cSMika Westerberg 		raw_spin_unlock(&pctrl->lock);
10467981c001SMika Westerberg 	}
1047919eb475SMika Westerberg }
10487981c001SMika Westerberg 
10496fb6f8bfSAndy Shevchenko static void intel_gpio_irq_mask_unmask(struct gpio_chip *gc, irq_hw_number_t hwirq, bool mask)
10507981c001SMika Westerberg {
1051acfd4c63SLinus Walleij 	struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
10527981c001SMika Westerberg 	const struct intel_community *community;
1053919eb475SMika Westerberg 	const struct intel_padgroup *padgrp;
1054a60eac32SMika Westerberg 	int pin;
1055a60eac32SMika Westerberg 
10566fb6f8bfSAndy Shevchenko 	pin = intel_gpio_to_pin(pctrl, hwirq, &community, &padgrp);
1057a60eac32SMika Westerberg 	if (pin >= 0) {
105804035f7fSAndy Shevchenko 		unsigned int gpp, gpp_offset;
1059919eb475SMika Westerberg 		unsigned long flags;
1060670784fbSKai-Heng Feng 		void __iomem *reg, *is;
10617981c001SMika Westerberg 		u32 value;
10627981c001SMika Westerberg 
1063919eb475SMika Westerberg 		gpp = padgrp->reg_num;
1064919eb475SMika Westerberg 		gpp_offset = padgroup_offset(padgrp, pin);
1065919eb475SMika Westerberg 
10667981c001SMika Westerberg 		reg = community->regs + community->ie_offset + gpp * 4;
1067670784fbSKai-Heng Feng 		is = community->regs + community->is_offset + gpp * 4;
1068919eb475SMika Westerberg 
1069919eb475SMika Westerberg 		raw_spin_lock_irqsave(&pctrl->lock, flags);
1070670784fbSKai-Heng Feng 
1071670784fbSKai-Heng Feng 		/* Clear interrupt status first to avoid unexpected interrupt */
1072670784fbSKai-Heng Feng 		writel(BIT(gpp_offset), is);
1073670784fbSKai-Heng Feng 
10747981c001SMika Westerberg 		value = readl(reg);
10757981c001SMika Westerberg 		if (mask)
10767981c001SMika Westerberg 			value &= ~BIT(gpp_offset);
10777981c001SMika Westerberg 		else
10787981c001SMika Westerberg 			value |= BIT(gpp_offset);
10797981c001SMika Westerberg 		writel(value, reg);
108027d9098cSMika Westerberg 		raw_spin_unlock_irqrestore(&pctrl->lock, flags);
10817981c001SMika Westerberg 	}
1082919eb475SMika Westerberg }
10837981c001SMika Westerberg 
10847981c001SMika Westerberg static void intel_gpio_irq_mask(struct irq_data *d)
10857981c001SMika Westerberg {
10866fb6f8bfSAndy Shevchenko 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
10876fb6f8bfSAndy Shevchenko 	irq_hw_number_t hwirq = irqd_to_hwirq(d);
10886fb6f8bfSAndy Shevchenko 
10896fb6f8bfSAndy Shevchenko 	intel_gpio_irq_mask_unmask(gc, hwirq, true);
10906fb6f8bfSAndy Shevchenko 	gpiochip_disable_irq(gc, hwirq);
10917981c001SMika Westerberg }
10927981c001SMika Westerberg 
10937981c001SMika Westerberg static void intel_gpio_irq_unmask(struct irq_data *d)
10947981c001SMika Westerberg {
10956fb6f8bfSAndy Shevchenko 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
10966fb6f8bfSAndy Shevchenko 	irq_hw_number_t hwirq = irqd_to_hwirq(d);
10976fb6f8bfSAndy Shevchenko 
10986fb6f8bfSAndy Shevchenko 	gpiochip_enable_irq(gc, hwirq);
10996fb6f8bfSAndy Shevchenko 	intel_gpio_irq_mask_unmask(gc, hwirq, false);
11007981c001SMika Westerberg }
11017981c001SMika Westerberg 
110204035f7fSAndy Shevchenko static int intel_gpio_irq_type(struct irq_data *d, unsigned int type)
11037981c001SMika Westerberg {
11047981c001SMika Westerberg 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1105acfd4c63SLinus Walleij 	struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
110604035f7fSAndy Shevchenko 	unsigned int pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), NULL, NULL);
11077981c001SMika Westerberg 	unsigned long flags;
11087981c001SMika Westerberg 	void __iomem *reg;
11097981c001SMika Westerberg 	u32 value;
11107981c001SMika Westerberg 
11117981c001SMika Westerberg 	reg = intel_get_padcfg(pctrl, pin, PADCFG0);
11127981c001SMika Westerberg 	if (!reg)
11137981c001SMika Westerberg 		return -EINVAL;
11147981c001SMika Westerberg 
11154341e8a5SMika Westerberg 	/*
11164341e8a5SMika Westerberg 	 * If the pin is in ACPI mode it is still usable as a GPIO but it
11174341e8a5SMika Westerberg 	 * cannot be used as IRQ because GPI_IS status bit will not be
11184341e8a5SMika Westerberg 	 * updated by the host controller hardware.
11194341e8a5SMika Westerberg 	 */
11204341e8a5SMika Westerberg 	if (intel_pad_acpi_mode(pctrl, pin)) {
11214341e8a5SMika Westerberg 		dev_warn(pctrl->dev, "pin %u cannot be used as IRQ\n", pin);
11224341e8a5SMika Westerberg 		return -EPERM;
11234341e8a5SMika Westerberg 	}
11244341e8a5SMika Westerberg 
112527d9098cSMika Westerberg 	raw_spin_lock_irqsave(&pctrl->lock, flags);
11267981c001SMika Westerberg 
1127f5a26acfSMika Westerberg 	intel_gpio_set_gpio_mode(reg);
1128f5a26acfSMika Westerberg 
11297981c001SMika Westerberg 	value = readl(reg);
11307981c001SMika Westerberg 
11317981c001SMika Westerberg 	value &= ~(PADCFG0_RXEVCFG_MASK | PADCFG0_RXINV);
11327981c001SMika Westerberg 
11337981c001SMika Westerberg 	if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
11347981c001SMika Westerberg 		value |= PADCFG0_RXEVCFG_EDGE_BOTH << PADCFG0_RXEVCFG_SHIFT;
11357981c001SMika Westerberg 	} else if (type & IRQ_TYPE_EDGE_FALLING) {
11367981c001SMika Westerberg 		value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT;
11377981c001SMika Westerberg 		value |= PADCFG0_RXINV;
11387981c001SMika Westerberg 	} else if (type & IRQ_TYPE_EDGE_RISING) {
11397981c001SMika Westerberg 		value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT;
1140bf380cfaSQipeng Zha 	} else if (type & IRQ_TYPE_LEVEL_MASK) {
1141bf380cfaSQipeng Zha 		if (type & IRQ_TYPE_LEVEL_LOW)
11427981c001SMika Westerberg 			value |= PADCFG0_RXINV;
11437981c001SMika Westerberg 	} else {
11447981c001SMika Westerberg 		value |= PADCFG0_RXEVCFG_DISABLED << PADCFG0_RXEVCFG_SHIFT;
11457981c001SMika Westerberg 	}
11467981c001SMika Westerberg 
11477981c001SMika Westerberg 	writel(value, reg);
11487981c001SMika Westerberg 
11497981c001SMika Westerberg 	if (type & IRQ_TYPE_EDGE_BOTH)
1150fc756bcdSThomas Gleixner 		irq_set_handler_locked(d, handle_edge_irq);
11517981c001SMika Westerberg 	else if (type & IRQ_TYPE_LEVEL_MASK)
1152fc756bcdSThomas Gleixner 		irq_set_handler_locked(d, handle_level_irq);
11537981c001SMika Westerberg 
115427d9098cSMika Westerberg 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
11557981c001SMika Westerberg 
11567981c001SMika Westerberg 	return 0;
11577981c001SMika Westerberg }
11587981c001SMika Westerberg 
11597981c001SMika Westerberg static int intel_gpio_irq_wake(struct irq_data *d, unsigned int on)
11607981c001SMika Westerberg {
11617981c001SMika Westerberg 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1162acfd4c63SLinus Walleij 	struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
116304035f7fSAndy Shevchenko 	unsigned int pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), NULL, NULL);
11647981c001SMika Westerberg 
11657981c001SMika Westerberg 	if (on)
116601dabe91SNilesh Bacchewar 		enable_irq_wake(pctrl->irq);
11677981c001SMika Westerberg 	else
116801dabe91SNilesh Bacchewar 		disable_irq_wake(pctrl->irq);
11699a520fd9SAndy Shevchenko 
11707981c001SMika Westerberg 	dev_dbg(pctrl->dev, "%sable wake for pin %u\n", on ? "en" : "dis", pin);
11717981c001SMika Westerberg 	return 0;
11727981c001SMika Westerberg }
11737981c001SMika Westerberg 
11746fb6f8bfSAndy Shevchenko static const struct irq_chip intel_gpio_irq_chip = {
11756fb6f8bfSAndy Shevchenko 	.name = "intel-gpio",
11766fb6f8bfSAndy Shevchenko 	.irq_ack = intel_gpio_irq_ack,
11776fb6f8bfSAndy Shevchenko 	.irq_mask = intel_gpio_irq_mask,
11786fb6f8bfSAndy Shevchenko 	.irq_unmask = intel_gpio_irq_unmask,
11796fb6f8bfSAndy Shevchenko 	.irq_set_type = intel_gpio_irq_type,
11806fb6f8bfSAndy Shevchenko 	.irq_set_wake = intel_gpio_irq_wake,
11816fb6f8bfSAndy Shevchenko 	.flags = IRQCHIP_MASK_ON_SUSPEND | IRQCHIP_IMMUTABLE,
11826fb6f8bfSAndy Shevchenko 	GPIOCHIP_IRQ_RESOURCE_HELPERS,
11836fb6f8bfSAndy Shevchenko };
11846fb6f8bfSAndy Shevchenko 
118586851bbcSAndy Shevchenko static int intel_gpio_community_irq_handler(struct intel_pinctrl *pctrl,
11867981c001SMika Westerberg 					    const struct intel_community *community)
11877981c001SMika Westerberg {
1188193b40c8SMika Westerberg 	struct gpio_chip *gc = &pctrl->chip;
118986851bbcSAndy Shevchenko 	unsigned int gpp;
119086851bbcSAndy Shevchenko 	int ret = 0;
11917981c001SMika Westerberg 
11927981c001SMika Westerberg 	for (gpp = 0; gpp < community->ngpps; gpp++) {
1193919eb475SMika Westerberg 		const struct intel_padgroup *padgrp = &community->gpps[gpp];
11947981c001SMika Westerberg 		unsigned long pending, enabled, gpp_offset;
1195e64fbfa5SAndy Shevchenko 
11965b613df3SAndy Shevchenko 		raw_spin_lock(&pctrl->lock);
11977981c001SMika Westerberg 
1198cf769bd8SMika Westerberg 		pending = readl(community->regs + community->is_offset +
1199cf769bd8SMika Westerberg 				padgrp->reg_num * 4);
12007981c001SMika Westerberg 		enabled = readl(community->regs + community->ie_offset +
1201919eb475SMika Westerberg 				padgrp->reg_num * 4);
12027981c001SMika Westerberg 
12035b613df3SAndy Shevchenko 		raw_spin_unlock(&pctrl->lock);
1204e64fbfa5SAndy Shevchenko 
12057981c001SMika Westerberg 		/* Only interrupts that are enabled */
12067981c001SMika Westerberg 		pending &= enabled;
12077981c001SMika Westerberg 
1208919eb475SMika Westerberg 		for_each_set_bit(gpp_offset, &pending, padgrp->size) {
120911b389ccSAndy Shevchenko 			unsigned int irq;
12107981c001SMika Westerberg 
1211f0fbe7bcSThierry Reding 			irq = irq_find_mapping(gc->irq.domain,
1212a60eac32SMika Westerberg 					       padgrp->gpio_base + gpp_offset);
12137981c001SMika Westerberg 			generic_handle_irq(irq);
12147981c001SMika Westerberg 		}
121586851bbcSAndy Shevchenko 
121686851bbcSAndy Shevchenko 		ret += pending ? 1 : 0;
12177981c001SMika Westerberg 	}
12187981c001SMika Westerberg 
1219193b40c8SMika Westerberg 	return ret;
1220193b40c8SMika Westerberg }
1221193b40c8SMika Westerberg 
1222193b40c8SMika Westerberg static irqreturn_t intel_gpio_irq(int irq, void *data)
12237981c001SMika Westerberg {
1224193b40c8SMika Westerberg 	const struct intel_community *community;
1225193b40c8SMika Westerberg 	struct intel_pinctrl *pctrl = data;
122686851bbcSAndy Shevchenko 	unsigned int i;
122786851bbcSAndy Shevchenko 	int ret = 0;
12287981c001SMika Westerberg 
12297981c001SMika Westerberg 	/* Need to check all communities for pending interrupts */
1230193b40c8SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
1231193b40c8SMika Westerberg 		community = &pctrl->communities[i];
123286851bbcSAndy Shevchenko 		ret += intel_gpio_community_irq_handler(pctrl, community);
1233193b40c8SMika Westerberg 	}
12347981c001SMika Westerberg 
123586851bbcSAndy Shevchenko 	return IRQ_RETVAL(ret);
12367981c001SMika Westerberg }
12377981c001SMika Westerberg 
1238e986f0e6SŁukasz Bartosik static void intel_gpio_irq_init(struct intel_pinctrl *pctrl)
1239e986f0e6SŁukasz Bartosik {
1240e986f0e6SŁukasz Bartosik 	int i;
1241e986f0e6SŁukasz Bartosik 
1242e986f0e6SŁukasz Bartosik 	for (i = 0; i < pctrl->ncommunities; i++) {
1243e986f0e6SŁukasz Bartosik 		const struct intel_community *community;
1244e986f0e6SŁukasz Bartosik 		void __iomem *base;
1245e986f0e6SŁukasz Bartosik 		unsigned int gpp;
1246e986f0e6SŁukasz Bartosik 
1247e986f0e6SŁukasz Bartosik 		community = &pctrl->communities[i];
1248e986f0e6SŁukasz Bartosik 		base = community->regs;
1249e986f0e6SŁukasz Bartosik 
1250e986f0e6SŁukasz Bartosik 		for (gpp = 0; gpp < community->ngpps; gpp++) {
1251e986f0e6SŁukasz Bartosik 			/* Mask and clear all interrupts */
1252e986f0e6SŁukasz Bartosik 			writel(0, base + community->ie_offset + gpp * 4);
1253e986f0e6SŁukasz Bartosik 			writel(0xffff, base + community->is_offset + gpp * 4);
1254e986f0e6SŁukasz Bartosik 		}
1255e986f0e6SŁukasz Bartosik 	}
1256e986f0e6SŁukasz Bartosik }
1257e986f0e6SŁukasz Bartosik 
1258e986f0e6SŁukasz Bartosik static int intel_gpio_irq_init_hw(struct gpio_chip *gc)
1259e986f0e6SŁukasz Bartosik {
1260e986f0e6SŁukasz Bartosik 	struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
1261e986f0e6SŁukasz Bartosik 
1262e986f0e6SŁukasz Bartosik 	/*
1263e986f0e6SŁukasz Bartosik 	 * Make sure the interrupt lines are in a proper state before
1264e986f0e6SŁukasz Bartosik 	 * further configuration.
1265e986f0e6SŁukasz Bartosik 	 */
1266e986f0e6SŁukasz Bartosik 	intel_gpio_irq_init(pctrl);
1267e986f0e6SŁukasz Bartosik 
1268e986f0e6SŁukasz Bartosik 	return 0;
1269e986f0e6SŁukasz Bartosik }
1270e986f0e6SŁukasz Bartosik 
12716d416b9bSLinus Walleij static int intel_gpio_add_community_ranges(struct intel_pinctrl *pctrl,
1272a60eac32SMika Westerberg 				const struct intel_community *community)
1273a60eac32SMika Westerberg {
127433b6cb58SColin Ian King 	int ret = 0, i;
1275a60eac32SMika Westerberg 
1276a60eac32SMika Westerberg 	for (i = 0; i < community->ngpps; i++) {
1277a60eac32SMika Westerberg 		const struct intel_padgroup *gpp = &community->gpps[i];
1278a60eac32SMika Westerberg 
1279e5a4ab6aSAndy Shevchenko 		if (gpp->gpio_base == INTEL_GPIO_BASE_NOMAP)
1280a60eac32SMika Westerberg 			continue;
1281a60eac32SMika Westerberg 
1282a60eac32SMika Westerberg 		ret = gpiochip_add_pin_range(&pctrl->chip, dev_name(pctrl->dev),
1283a60eac32SMika Westerberg 					     gpp->gpio_base, gpp->base,
1284a60eac32SMika Westerberg 					     gpp->size);
1285a60eac32SMika Westerberg 		if (ret)
1286a60eac32SMika Westerberg 			return ret;
1287a60eac32SMika Westerberg 	}
1288a60eac32SMika Westerberg 
1289a60eac32SMika Westerberg 	return ret;
1290a60eac32SMika Westerberg }
1291a60eac32SMika Westerberg 
12926d416b9bSLinus Walleij static int intel_gpio_add_pin_ranges(struct gpio_chip *gc)
12936d416b9bSLinus Walleij {
12946d416b9bSLinus Walleij 	struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
12956d416b9bSLinus Walleij 	int ret, i;
12966d416b9bSLinus Walleij 
12976d416b9bSLinus Walleij 	for (i = 0; i < pctrl->ncommunities; i++) {
12986d416b9bSLinus Walleij 		struct intel_community *community = &pctrl->communities[i];
12996d416b9bSLinus Walleij 
13006d416b9bSLinus Walleij 		ret = intel_gpio_add_community_ranges(pctrl, community);
13016d416b9bSLinus Walleij 		if (ret) {
13026d416b9bSLinus Walleij 			dev_err(pctrl->dev, "failed to add GPIO pin range\n");
13036d416b9bSLinus Walleij 			return ret;
13046d416b9bSLinus Walleij 		}
13056d416b9bSLinus Walleij 	}
13066d416b9bSLinus Walleij 
13076d416b9bSLinus Walleij 	return 0;
13086d416b9bSLinus Walleij }
13096d416b9bSLinus Walleij 
131011b389ccSAndy Shevchenko static unsigned int intel_gpio_ngpio(const struct intel_pinctrl *pctrl)
1311a60eac32SMika Westerberg {
1312a60eac32SMika Westerberg 	const struct intel_community *community;
131304035f7fSAndy Shevchenko 	unsigned int ngpio = 0;
1314a60eac32SMika Westerberg 	int i, j;
1315a60eac32SMika Westerberg 
1316a60eac32SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
1317a60eac32SMika Westerberg 		community = &pctrl->communities[i];
1318a60eac32SMika Westerberg 		for (j = 0; j < community->ngpps; j++) {
1319a60eac32SMika Westerberg 			const struct intel_padgroup *gpp = &community->gpps[j];
1320a60eac32SMika Westerberg 
1321e5a4ab6aSAndy Shevchenko 			if (gpp->gpio_base == INTEL_GPIO_BASE_NOMAP)
1322a60eac32SMika Westerberg 				continue;
1323a60eac32SMika Westerberg 
1324a60eac32SMika Westerberg 			if (gpp->gpio_base + gpp->size > ngpio)
1325a60eac32SMika Westerberg 				ngpio = gpp->gpio_base + gpp->size;
1326a60eac32SMika Westerberg 		}
1327a60eac32SMika Westerberg 	}
1328a60eac32SMika Westerberg 
1329a60eac32SMika Westerberg 	return ngpio;
1330a60eac32SMika Westerberg }
1331a60eac32SMika Westerberg 
13327981c001SMika Westerberg static int intel_gpio_probe(struct intel_pinctrl *pctrl, int irq)
13337981c001SMika Westerberg {
13346d416b9bSLinus Walleij 	int ret;
1335af0c5330SLinus Walleij 	struct gpio_irq_chip *girq;
13367981c001SMika Westerberg 
13377981c001SMika Westerberg 	pctrl->chip = intel_gpio_chip;
13387981c001SMika Westerberg 
133957ff2df1SAndy Shevchenko 	/* Setup GPIO chip */
1340a60eac32SMika Westerberg 	pctrl->chip.ngpio = intel_gpio_ngpio(pctrl);
13417981c001SMika Westerberg 	pctrl->chip.label = dev_name(pctrl->dev);
134258383c78SLinus Walleij 	pctrl->chip.parent = pctrl->dev;
13437981c001SMika Westerberg 	pctrl->chip.base = -1;
13446d416b9bSLinus Walleij 	pctrl->chip.add_pin_ranges = intel_gpio_add_pin_ranges;
134501dabe91SNilesh Bacchewar 	pctrl->irq = irq;
13467981c001SMika Westerberg 
1347193b40c8SMika Westerberg 	/*
1348af0c5330SLinus Walleij 	 * On some platforms several GPIO controllers share the same interrupt
1349af0c5330SLinus Walleij 	 * line.
1350193b40c8SMika Westerberg 	 */
13511a7d1cb8SMika Westerberg 	ret = devm_request_irq(pctrl->dev, irq, intel_gpio_irq,
13521a7d1cb8SMika Westerberg 			       IRQF_SHARED | IRQF_NO_THREAD,
1353193b40c8SMika Westerberg 			       dev_name(pctrl->dev), pctrl);
1354193b40c8SMika Westerberg 	if (ret) {
1355193b40c8SMika Westerberg 		dev_err(pctrl->dev, "failed to request interrupt\n");
1356f25c3aa9SMika Westerberg 		return ret;
13577981c001SMika Westerberg 	}
13587981c001SMika Westerberg 
13596fb6f8bfSAndy Shevchenko 	/* Setup IRQ chip */
1360af0c5330SLinus Walleij 	girq = &pctrl->chip.irq;
13616fb6f8bfSAndy Shevchenko 	gpio_irq_chip_set_chip(girq, &intel_gpio_irq_chip);
1362af0c5330SLinus Walleij 	/* This will let us handle the IRQ in the driver */
1363af0c5330SLinus Walleij 	girq->parent_handler = NULL;
1364af0c5330SLinus Walleij 	girq->num_parents = 0;
1365af0c5330SLinus Walleij 	girq->default_type = IRQ_TYPE_NONE;
1366af0c5330SLinus Walleij 	girq->handler = handle_bad_irq;
1367e986f0e6SŁukasz Bartosik 	girq->init_hw = intel_gpio_irq_init_hw;
1368af0c5330SLinus Walleij 
1369af0c5330SLinus Walleij 	ret = devm_gpiochip_add_data(pctrl->dev, &pctrl->chip, pctrl);
13707981c001SMika Westerberg 	if (ret) {
1371af0c5330SLinus Walleij 		dev_err(pctrl->dev, "failed to register gpiochip\n");
1372f25c3aa9SMika Westerberg 		return ret;
13737981c001SMika Westerberg 	}
13747981c001SMika Westerberg 
13757981c001SMika Westerberg 	return 0;
13767981c001SMika Westerberg }
13777981c001SMika Westerberg 
1378036e126cSAndy Shevchenko static int intel_pinctrl_add_padgroups_by_gpps(struct intel_pinctrl *pctrl,
1379919eb475SMika Westerberg 					       struct intel_community *community)
1380919eb475SMika Westerberg {
1381919eb475SMika Westerberg 	struct intel_padgroup *gpps;
138204035f7fSAndy Shevchenko 	unsigned int padown_num = 0;
1383036e126cSAndy Shevchenko 	size_t i, ngpps = community->ngpps;
1384919eb475SMika Westerberg 
1385919eb475SMika Westerberg 	gpps = devm_kcalloc(pctrl->dev, ngpps, sizeof(*gpps), GFP_KERNEL);
1386919eb475SMika Westerberg 	if (!gpps)
1387919eb475SMika Westerberg 		return -ENOMEM;
1388919eb475SMika Westerberg 
1389919eb475SMika Westerberg 	for (i = 0; i < ngpps; i++) {
1390919eb475SMika Westerberg 		gpps[i] = community->gpps[i];
1391919eb475SMika Westerberg 
1392919eb475SMika Westerberg 		if (gpps[i].size > 32)
1393919eb475SMika Westerberg 			return -EINVAL;
1394919eb475SMika Westerberg 
1395e5a4ab6aSAndy Shevchenko 		/* Special treatment for GPIO base */
1396e5a4ab6aSAndy Shevchenko 		switch (gpps[i].gpio_base) {
1397e5a4ab6aSAndy Shevchenko 			case INTEL_GPIO_BASE_MATCH:
1398a60eac32SMika Westerberg 				gpps[i].gpio_base = gpps[i].base;
1399e5a4ab6aSAndy Shevchenko 				break;
14009bd59157SAndy Shevchenko 			case INTEL_GPIO_BASE_ZERO:
14019bd59157SAndy Shevchenko 				gpps[i].gpio_base = 0;
14029bd59157SAndy Shevchenko 				break;
1403e5a4ab6aSAndy Shevchenko 			case INTEL_GPIO_BASE_NOMAP:
140477e14126SAndy Shevchenko 				break;
1405e5a4ab6aSAndy Shevchenko 			default:
1406e5a4ab6aSAndy Shevchenko 				break;
1407e5a4ab6aSAndy Shevchenko 		}
1408a60eac32SMika Westerberg 
1409919eb475SMika Westerberg 		gpps[i].padown_num = padown_num;
1410036e126cSAndy Shevchenko 		padown_num += DIV_ROUND_UP(gpps[i].size * 4, 32);
1411036e126cSAndy Shevchenko 	}
1412036e126cSAndy Shevchenko 
1413036e126cSAndy Shevchenko 	community->gpps = gpps;
1414036e126cSAndy Shevchenko 
1415036e126cSAndy Shevchenko 	return 0;
1416036e126cSAndy Shevchenko }
1417036e126cSAndy Shevchenko 
1418036e126cSAndy Shevchenko static int intel_pinctrl_add_padgroups_by_size(struct intel_pinctrl *pctrl,
1419036e126cSAndy Shevchenko 					       struct intel_community *community)
1420036e126cSAndy Shevchenko {
1421036e126cSAndy Shevchenko 	struct intel_padgroup *gpps;
1422036e126cSAndy Shevchenko 	unsigned int npins = community->npins;
1423036e126cSAndy Shevchenko 	unsigned int padown_num = 0;
1424036e126cSAndy Shevchenko 	size_t i, ngpps = DIV_ROUND_UP(npins, community->gpp_size);
1425036e126cSAndy Shevchenko 
1426036e126cSAndy Shevchenko 	if (community->gpp_size > 32)
1427036e126cSAndy Shevchenko 		return -EINVAL;
1428036e126cSAndy Shevchenko 
1429036e126cSAndy Shevchenko 	gpps = devm_kcalloc(pctrl->dev, ngpps, sizeof(*gpps), GFP_KERNEL);
1430036e126cSAndy Shevchenko 	if (!gpps)
1431036e126cSAndy Shevchenko 		return -ENOMEM;
1432036e126cSAndy Shevchenko 
1433036e126cSAndy Shevchenko 	for (i = 0; i < ngpps; i++) {
1434036e126cSAndy Shevchenko 		unsigned int gpp_size = community->gpp_size;
1435036e126cSAndy Shevchenko 
1436036e126cSAndy Shevchenko 		gpps[i].reg_num = i;
1437036e126cSAndy Shevchenko 		gpps[i].base = community->pin_base + i * gpp_size;
1438036e126cSAndy Shevchenko 		gpps[i].size = min(gpp_size, npins);
1439036e126cSAndy Shevchenko 		npins -= gpps[i].size;
1440036e126cSAndy Shevchenko 
144177e14126SAndy Shevchenko 		gpps[i].gpio_base = gpps[i].base;
1442036e126cSAndy Shevchenko 		gpps[i].padown_num = padown_num;
1443919eb475SMika Westerberg 
1444919eb475SMika Westerberg 		/*
1445919eb475SMika Westerberg 		 * In older hardware the number of padown registers per
1446919eb475SMika Westerberg 		 * group is fixed regardless of the group size.
1447919eb475SMika Westerberg 		 */
1448919eb475SMika Westerberg 		if (community->gpp_num_padown_regs)
1449919eb475SMika Westerberg 			padown_num += community->gpp_num_padown_regs;
1450919eb475SMika Westerberg 		else
1451919eb475SMika Westerberg 			padown_num += DIV_ROUND_UP(gpps[i].size * 4, 32);
1452919eb475SMika Westerberg 	}
1453919eb475SMika Westerberg 
1454919eb475SMika Westerberg 	community->ngpps = ngpps;
1455919eb475SMika Westerberg 	community->gpps = gpps;
1456919eb475SMika Westerberg 
1457919eb475SMika Westerberg 	return 0;
1458919eb475SMika Westerberg }
1459919eb475SMika Westerberg 
14607981c001SMika Westerberg static int intel_pinctrl_pm_init(struct intel_pinctrl *pctrl)
14617981c001SMika Westerberg {
14627981c001SMika Westerberg #ifdef CONFIG_PM_SLEEP
14637981c001SMika Westerberg 	const struct intel_pinctrl_soc_data *soc = pctrl->soc;
14647981c001SMika Westerberg 	struct intel_community_context *communities;
14657981c001SMika Westerberg 	struct intel_pad_context *pads;
14667981c001SMika Westerberg 	int i;
14677981c001SMika Westerberg 
14687981c001SMika Westerberg 	pads = devm_kcalloc(pctrl->dev, soc->npins, sizeof(*pads), GFP_KERNEL);
14697981c001SMika Westerberg 	if (!pads)
14707981c001SMika Westerberg 		return -ENOMEM;
14717981c001SMika Westerberg 
14727981c001SMika Westerberg 	communities = devm_kcalloc(pctrl->dev, pctrl->ncommunities,
14737981c001SMika Westerberg 				   sizeof(*communities), GFP_KERNEL);
14747981c001SMika Westerberg 	if (!communities)
14757981c001SMika Westerberg 		return -ENOMEM;
14767981c001SMika Westerberg 
14777981c001SMika Westerberg 
14787981c001SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
14797981c001SMika Westerberg 		struct intel_community *community = &pctrl->communities[i];
1480a0a5f766SChris Chiu 		u32 *intmask, *hostown;
14817981c001SMika Westerberg 
14827981c001SMika Westerberg 		intmask = devm_kcalloc(pctrl->dev, community->ngpps,
14837981c001SMika Westerberg 				       sizeof(*intmask), GFP_KERNEL);
14847981c001SMika Westerberg 		if (!intmask)
14857981c001SMika Westerberg 			return -ENOMEM;
14867981c001SMika Westerberg 
14877981c001SMika Westerberg 		communities[i].intmask = intmask;
1488a0a5f766SChris Chiu 
1489a0a5f766SChris Chiu 		hostown = devm_kcalloc(pctrl->dev, community->ngpps,
1490a0a5f766SChris Chiu 				       sizeof(*hostown), GFP_KERNEL);
1491a0a5f766SChris Chiu 		if (!hostown)
1492a0a5f766SChris Chiu 			return -ENOMEM;
1493a0a5f766SChris Chiu 
1494a0a5f766SChris Chiu 		communities[i].hostown = hostown;
14957981c001SMika Westerberg 	}
14967981c001SMika Westerberg 
14977981c001SMika Westerberg 	pctrl->context.pads = pads;
14987981c001SMika Westerberg 	pctrl->context.communities = communities;
14997981c001SMika Westerberg #endif
15007981c001SMika Westerberg 
15017981c001SMika Westerberg 	return 0;
15027981c001SMika Westerberg }
15037981c001SMika Westerberg 
15040dd519e3SAndy Shevchenko static int intel_pinctrl_probe(struct platform_device *pdev,
15057981c001SMika Westerberg 			       const struct intel_pinctrl_soc_data *soc_data)
15067981c001SMika Westerberg {
15077981c001SMika Westerberg 	struct intel_pinctrl *pctrl;
15087981c001SMika Westerberg 	int i, ret, irq;
15097981c001SMika Westerberg 
15107981c001SMika Westerberg 	pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL);
15117981c001SMika Westerberg 	if (!pctrl)
15127981c001SMika Westerberg 		return -ENOMEM;
15137981c001SMika Westerberg 
15147981c001SMika Westerberg 	pctrl->dev = &pdev->dev;
15157981c001SMika Westerberg 	pctrl->soc = soc_data;
151627d9098cSMika Westerberg 	raw_spin_lock_init(&pctrl->lock);
15177981c001SMika Westerberg 
15187981c001SMika Westerberg 	/*
15197981c001SMika Westerberg 	 * Make a copy of the communities which we can use to hold pointers
15207981c001SMika Westerberg 	 * to the registers.
15217981c001SMika Westerberg 	 */
15227981c001SMika Westerberg 	pctrl->ncommunities = pctrl->soc->ncommunities;
15237981c001SMika Westerberg 	pctrl->communities = devm_kcalloc(&pdev->dev, pctrl->ncommunities,
15247981c001SMika Westerberg 				  sizeof(*pctrl->communities), GFP_KERNEL);
15257981c001SMika Westerberg 	if (!pctrl->communities)
15267981c001SMika Westerberg 		return -ENOMEM;
15277981c001SMika Westerberg 
15287981c001SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
15297981c001SMika Westerberg 		struct intel_community *community = &pctrl->communities[i];
15307981c001SMika Westerberg 		void __iomem *regs;
153191d898e5SAndy Shevchenko 		u32 offset;
1532998c49e8SAndy Shevchenko 		u32 value;
15337981c001SMika Westerberg 
15347981c001SMika Westerberg 		*community = pctrl->soc->communities[i];
15357981c001SMika Westerberg 
15369d5b6a95SAndy Shevchenko 		regs = devm_platform_ioremap_resource(pdev, community->barno);
15377981c001SMika Westerberg 		if (IS_ERR(regs))
15387981c001SMika Westerberg 			return PTR_ERR(regs);
15397981c001SMika Westerberg 
154039c1f1bdSRoger Pau Monne 		/*
154139c1f1bdSRoger Pau Monne 		 * Determine community features based on the revision.
154239c1f1bdSRoger Pau Monne 		 * A value of all ones means the device is not present.
154339c1f1bdSRoger Pau Monne 		 */
1544998c49e8SAndy Shevchenko 		value = readl(regs + REVID);
154539c1f1bdSRoger Pau Monne 		if (value == ~0u)
154639c1f1bdSRoger Pau Monne 			return -ENODEV;
1547998c49e8SAndy Shevchenko 		if (((value & REVID_MASK) >> REVID_SHIFT) >= 0x94) {
1548e57725eaSMika Westerberg 			community->features |= PINCTRL_FEATURE_DEBOUNCE;
154904cc058fSMika Westerberg 			community->features |= PINCTRL_FEATURE_1K_PD;
155004cc058fSMika Westerberg 		}
1551e57725eaSMika Westerberg 
155291d898e5SAndy Shevchenko 		/* Determine community features based on the capabilities */
155391d898e5SAndy Shevchenko 		offset = CAPLIST;
155491d898e5SAndy Shevchenko 		do {
155591d898e5SAndy Shevchenko 			value = readl(regs + offset);
155691d898e5SAndy Shevchenko 			switch ((value & CAPLIST_ID_MASK) >> CAPLIST_ID_SHIFT) {
155791d898e5SAndy Shevchenko 			case CAPLIST_ID_GPIO_HW_INFO:
155891d898e5SAndy Shevchenko 				community->features |= PINCTRL_FEATURE_GPIO_HW_INFO;
155991d898e5SAndy Shevchenko 				break;
156091d898e5SAndy Shevchenko 			case CAPLIST_ID_PWM:
156191d898e5SAndy Shevchenko 				community->features |= PINCTRL_FEATURE_PWM;
156291d898e5SAndy Shevchenko 				break;
156391d898e5SAndy Shevchenko 			case CAPLIST_ID_BLINK:
156491d898e5SAndy Shevchenko 				community->features |= PINCTRL_FEATURE_BLINK;
156591d898e5SAndy Shevchenko 				break;
156691d898e5SAndy Shevchenko 			case CAPLIST_ID_EXP:
156791d898e5SAndy Shevchenko 				community->features |= PINCTRL_FEATURE_EXP;
156891d898e5SAndy Shevchenko 				break;
156991d898e5SAndy Shevchenko 			default:
157091d898e5SAndy Shevchenko 				break;
157191d898e5SAndy Shevchenko 			}
157291d898e5SAndy Shevchenko 			offset = (value & CAPLIST_NEXT_MASK) >> CAPLIST_NEXT_SHIFT;
157391d898e5SAndy Shevchenko 		} while (offset);
157491d898e5SAndy Shevchenko 
157591d898e5SAndy Shevchenko 		dev_dbg(&pdev->dev, "Community%d features: %#08x\n", i, community->features);
157691d898e5SAndy Shevchenko 
15777981c001SMika Westerberg 		/* Read offset of the pad configuration registers */
157891d898e5SAndy Shevchenko 		offset = readl(regs + PADBAR);
15797981c001SMika Westerberg 
15807981c001SMika Westerberg 		community->regs = regs;
158191d898e5SAndy Shevchenko 		community->pad_regs = regs + offset;
1582919eb475SMika Westerberg 
1583036e126cSAndy Shevchenko 		if (community->gpps)
1584036e126cSAndy Shevchenko 			ret = intel_pinctrl_add_padgroups_by_gpps(pctrl, community);
1585036e126cSAndy Shevchenko 		else
1586036e126cSAndy Shevchenko 			ret = intel_pinctrl_add_padgroups_by_size(pctrl, community);
1587919eb475SMika Westerberg 		if (ret)
1588919eb475SMika Westerberg 			return ret;
15897981c001SMika Westerberg 	}
15907981c001SMika Westerberg 
15917981c001SMika Westerberg 	irq = platform_get_irq(pdev, 0);
15924e73d02fSStephen Boyd 	if (irq < 0)
15937981c001SMika Westerberg 		return irq;
15947981c001SMika Westerberg 
15957981c001SMika Westerberg 	ret = intel_pinctrl_pm_init(pctrl);
15967981c001SMika Westerberg 	if (ret)
15977981c001SMika Westerberg 		return ret;
15987981c001SMika Westerberg 
15997981c001SMika Westerberg 	pctrl->pctldesc = intel_pinctrl_desc;
16007981c001SMika Westerberg 	pctrl->pctldesc.name = dev_name(&pdev->dev);
16017981c001SMika Westerberg 	pctrl->pctldesc.pins = pctrl->soc->pins;
16027981c001SMika Westerberg 	pctrl->pctldesc.npins = pctrl->soc->npins;
16037981c001SMika Westerberg 
160454d46cd7SLaxman Dewangan 	pctrl->pctldev = devm_pinctrl_register(&pdev->dev, &pctrl->pctldesc,
160554d46cd7SLaxman Dewangan 					       pctrl);
1606323de9efSMasahiro Yamada 	if (IS_ERR(pctrl->pctldev)) {
16077981c001SMika Westerberg 		dev_err(&pdev->dev, "failed to register pinctrl driver\n");
1608323de9efSMasahiro Yamada 		return PTR_ERR(pctrl->pctldev);
16097981c001SMika Westerberg 	}
16107981c001SMika Westerberg 
16117981c001SMika Westerberg 	ret = intel_gpio_probe(pctrl, irq);
161254d46cd7SLaxman Dewangan 	if (ret)
16137981c001SMika Westerberg 		return ret;
16147981c001SMika Westerberg 
16157981c001SMika Westerberg 	platform_set_drvdata(pdev, pctrl);
16167981c001SMika Westerberg 
16177981c001SMika Westerberg 	return 0;
16187981c001SMika Westerberg }
16197981c001SMika Westerberg 
162070c263c4SAndy Shevchenko int intel_pinctrl_probe_by_hid(struct platform_device *pdev)
162170c263c4SAndy Shevchenko {
162270c263c4SAndy Shevchenko 	const struct intel_pinctrl_soc_data *data;
162370c263c4SAndy Shevchenko 
162470c263c4SAndy Shevchenko 	data = device_get_match_data(&pdev->dev);
1625ff360d62SAndy Shevchenko 	if (!data)
1626ff360d62SAndy Shevchenko 		return -ENODATA;
1627ff360d62SAndy Shevchenko 
162870c263c4SAndy Shevchenko 	return intel_pinctrl_probe(pdev, data);
162970c263c4SAndy Shevchenko }
163070c263c4SAndy Shevchenko EXPORT_SYMBOL_GPL(intel_pinctrl_probe_by_hid);
163170c263c4SAndy Shevchenko 
1632924cf800SAndy Shevchenko int intel_pinctrl_probe_by_uid(struct platform_device *pdev)
1633924cf800SAndy Shevchenko {
1634ff360d62SAndy Shevchenko 	const struct intel_pinctrl_soc_data *data;
1635ff360d62SAndy Shevchenko 
1636ff360d62SAndy Shevchenko 	data = intel_pinctrl_get_soc_data(pdev);
1637ff360d62SAndy Shevchenko 	if (IS_ERR(data))
1638ff360d62SAndy Shevchenko 		return PTR_ERR(data);
1639ff360d62SAndy Shevchenko 
1640ff360d62SAndy Shevchenko 	return intel_pinctrl_probe(pdev, data);
1641ff360d62SAndy Shevchenko }
1642ff360d62SAndy Shevchenko EXPORT_SYMBOL_GPL(intel_pinctrl_probe_by_uid);
1643ff360d62SAndy Shevchenko 
1644ff360d62SAndy Shevchenko const struct intel_pinctrl_soc_data *intel_pinctrl_get_soc_data(struct platform_device *pdev)
1645ff360d62SAndy Shevchenko {
1646c551bd81SAndy Shevchenko 	const struct intel_pinctrl_soc_data * const *table;
1647924cf800SAndy Shevchenko 	const struct intel_pinctrl_soc_data *data = NULL;
1648c551bd81SAndy Shevchenko 
1649c551bd81SAndy Shevchenko 	table = device_get_match_data(&pdev->dev);
1650c551bd81SAndy Shevchenko 	if (table) {
1651c551bd81SAndy Shevchenko 		struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
1652924cf800SAndy Shevchenko 		unsigned int i;
1653924cf800SAndy Shevchenko 
1654924cf800SAndy Shevchenko 		for (i = 0; table[i]; i++) {
1655924cf800SAndy Shevchenko 			if (!strcmp(adev->pnp.unique_id, table[i]->uid)) {
1656924cf800SAndy Shevchenko 				data = table[i];
1657924cf800SAndy Shevchenko 				break;
1658924cf800SAndy Shevchenko 			}
1659924cf800SAndy Shevchenko 		}
1660924cf800SAndy Shevchenko 	} else {
1661924cf800SAndy Shevchenko 		const struct platform_device_id *id;
1662924cf800SAndy Shevchenko 
1663924cf800SAndy Shevchenko 		id = platform_get_device_id(pdev);
1664924cf800SAndy Shevchenko 		if (!id)
1665ff360d62SAndy Shevchenko 			return ERR_PTR(-ENODEV);
1666924cf800SAndy Shevchenko 
1667c551bd81SAndy Shevchenko 		table = (const struct intel_pinctrl_soc_data * const *)id->driver_data;
1668924cf800SAndy Shevchenko 		data = table[pdev->id];
1669924cf800SAndy Shevchenko 	}
1670924cf800SAndy Shevchenko 
1671ff360d62SAndy Shevchenko 	return data ?: ERR_PTR(-ENODATA);
1672924cf800SAndy Shevchenko }
1673ff360d62SAndy Shevchenko EXPORT_SYMBOL_GPL(intel_pinctrl_get_soc_data);
1674924cf800SAndy Shevchenko 
16757981c001SMika Westerberg #ifdef CONFIG_PM_SLEEP
167604035f7fSAndy Shevchenko static bool intel_pinctrl_should_save(struct intel_pinctrl *pctrl, unsigned int pin)
1677c538b943SMika Westerberg {
1678c538b943SMika Westerberg 	const struct pin_desc *pd = pin_desc_get(pctrl->pctldev, pin);
1679c538b943SMika Westerberg 
1680c538b943SMika Westerberg 	if (!pd || !intel_pad_usable(pctrl, pin))
1681c538b943SMika Westerberg 		return false;
1682c538b943SMika Westerberg 
1683c538b943SMika Westerberg 	/*
1684c538b943SMika Westerberg 	 * Only restore the pin if it is actually in use by the kernel (or
1685c538b943SMika Westerberg 	 * by userspace). It is possible that some pins are used by the
1686c538b943SMika Westerberg 	 * BIOS during resume and those are not always locked down so leave
1687c538b943SMika Westerberg 	 * them alone.
1688c538b943SMika Westerberg 	 */
1689c538b943SMika Westerberg 	if (pd->mux_owner || pd->gpio_owner ||
16906cb0880fSChris Chiu 	    gpiochip_line_is_irq(&pctrl->chip, intel_pin_to_gpio(pctrl, pin)))
1691c538b943SMika Westerberg 		return true;
1692c538b943SMika Westerberg 
1693c538b943SMika Westerberg 	return false;
1694c538b943SMika Westerberg }
1695c538b943SMika Westerberg 
16962fef3276SBinbin Wu int intel_pinctrl_suspend_noirq(struct device *dev)
16977981c001SMika Westerberg {
1698cb035d74SWolfram Sang 	struct intel_pinctrl *pctrl = dev_get_drvdata(dev);
16997981c001SMika Westerberg 	struct intel_community_context *communities;
17007981c001SMika Westerberg 	struct intel_pad_context *pads;
17017981c001SMika Westerberg 	int i;
17027981c001SMika Westerberg 
17037981c001SMika Westerberg 	pads = pctrl->context.pads;
17047981c001SMika Westerberg 	for (i = 0; i < pctrl->soc->npins; i++) {
17057981c001SMika Westerberg 		const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
1706e57725eaSMika Westerberg 		void __iomem *padcfg;
17077981c001SMika Westerberg 		u32 val;
17087981c001SMika Westerberg 
1709c538b943SMika Westerberg 		if (!intel_pinctrl_should_save(pctrl, desc->number))
17107981c001SMika Westerberg 			continue;
17117981c001SMika Westerberg 
17127981c001SMika Westerberg 		val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG0));
17137981c001SMika Westerberg 		pads[i].padcfg0 = val & ~PADCFG0_GPIORXSTATE;
17147981c001SMika Westerberg 		val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG1));
17157981c001SMika Westerberg 		pads[i].padcfg1 = val;
1716e57725eaSMika Westerberg 
1717e57725eaSMika Westerberg 		padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG2);
1718e57725eaSMika Westerberg 		if (padcfg)
1719e57725eaSMika Westerberg 			pads[i].padcfg2 = readl(padcfg);
17207981c001SMika Westerberg 	}
17217981c001SMika Westerberg 
17227981c001SMika Westerberg 	communities = pctrl->context.communities;
17237981c001SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
17247981c001SMika Westerberg 		struct intel_community *community = &pctrl->communities[i];
17257981c001SMika Westerberg 		void __iomem *base;
172604035f7fSAndy Shevchenko 		unsigned int gpp;
17277981c001SMika Westerberg 
17287981c001SMika Westerberg 		base = community->regs + community->ie_offset;
17297981c001SMika Westerberg 		for (gpp = 0; gpp < community->ngpps; gpp++)
17307981c001SMika Westerberg 			communities[i].intmask[gpp] = readl(base + gpp * 4);
1731a0a5f766SChris Chiu 
1732a0a5f766SChris Chiu 		base = community->regs + community->hostown_offset;
1733a0a5f766SChris Chiu 		for (gpp = 0; gpp < community->ngpps; gpp++)
1734a0a5f766SChris Chiu 			communities[i].hostown[gpp] = readl(base + gpp * 4);
17357981c001SMika Westerberg 	}
17367981c001SMika Westerberg 
17377981c001SMika Westerberg 	return 0;
17387981c001SMika Westerberg }
17392fef3276SBinbin Wu EXPORT_SYMBOL_GPL(intel_pinctrl_suspend_noirq);
17407981c001SMika Westerberg 
1741942c5ea4SAndy Shevchenko static bool intel_gpio_update_reg(void __iomem *reg, u32 mask, u32 value)
1742a0a5f766SChris Chiu {
17435f61d951SAndy Shevchenko 	u32 curr, updated;
1744a0a5f766SChris Chiu 
1745942c5ea4SAndy Shevchenko 	curr = readl(reg);
17465f61d951SAndy Shevchenko 
1747942c5ea4SAndy Shevchenko 	updated = (curr & ~mask) | (value & mask);
1748942c5ea4SAndy Shevchenko 	if (curr == updated)
1749942c5ea4SAndy Shevchenko 		return false;
1750942c5ea4SAndy Shevchenko 
1751942c5ea4SAndy Shevchenko 	writel(updated, reg);
1752942c5ea4SAndy Shevchenko 	return true;
1753a0a5f766SChris Chiu }
1754a0a5f766SChris Chiu 
17557101e022SAndy Shevchenko static void intel_restore_hostown(struct intel_pinctrl *pctrl, unsigned int c,
17567101e022SAndy Shevchenko 				  void __iomem *base, unsigned int gpp, u32 saved)
17577101e022SAndy Shevchenko {
17587101e022SAndy Shevchenko 	const struct intel_community *community = &pctrl->communities[c];
17597101e022SAndy Shevchenko 	const struct intel_padgroup *padgrp = &community->gpps[gpp];
17607101e022SAndy Shevchenko 	struct device *dev = pctrl->dev;
1761d1bfd022SAndy Shevchenko 	const char *dummy;
1762d1bfd022SAndy Shevchenko 	u32 requested = 0;
1763d1bfd022SAndy Shevchenko 	unsigned int i;
17647101e022SAndy Shevchenko 
1765e5a4ab6aSAndy Shevchenko 	if (padgrp->gpio_base == INTEL_GPIO_BASE_NOMAP)
17667101e022SAndy Shevchenko 		return;
17677101e022SAndy Shevchenko 
1768d1bfd022SAndy Shevchenko 	for_each_requested_gpio_in_range(&pctrl->chip, i, padgrp->gpio_base, padgrp->size, dummy)
1769d1bfd022SAndy Shevchenko 		requested |= BIT(i);
1770d1bfd022SAndy Shevchenko 
1771942c5ea4SAndy Shevchenko 	if (!intel_gpio_update_reg(base + gpp * 4, requested, saved))
17727101e022SAndy Shevchenko 		return;
17737101e022SAndy Shevchenko 
1774764cfe33SAndy Shevchenko 	dev_dbg(dev, "restored hostown %u/%u %#08x\n", c, gpp, readl(base + gpp * 4));
17757101e022SAndy Shevchenko }
17767101e022SAndy Shevchenko 
1777471dd9a9SAndy Shevchenko static void intel_restore_intmask(struct intel_pinctrl *pctrl, unsigned int c,
1778471dd9a9SAndy Shevchenko 				  void __iomem *base, unsigned int gpp, u32 saved)
1779471dd9a9SAndy Shevchenko {
1780471dd9a9SAndy Shevchenko 	struct device *dev = pctrl->dev;
1781471dd9a9SAndy Shevchenko 
1782942c5ea4SAndy Shevchenko 	if (!intel_gpio_update_reg(base + gpp * 4, ~0U, saved))
1783942c5ea4SAndy Shevchenko 		return;
1784942c5ea4SAndy Shevchenko 
1785471dd9a9SAndy Shevchenko 	dev_dbg(dev, "restored mask %u/%u %#08x\n", c, gpp, readl(base + gpp * 4));
1786471dd9a9SAndy Shevchenko }
1787471dd9a9SAndy Shevchenko 
1788f78f152aSAndy Shevchenko static void intel_restore_padcfg(struct intel_pinctrl *pctrl, unsigned int pin,
1789f78f152aSAndy Shevchenko 				 unsigned int reg, u32 saved)
1790f78f152aSAndy Shevchenko {
1791f78f152aSAndy Shevchenko 	u32 mask = (reg == PADCFG0) ? PADCFG0_GPIORXSTATE : 0;
1792f78f152aSAndy Shevchenko 	unsigned int n = reg / sizeof(u32);
1793f78f152aSAndy Shevchenko 	struct device *dev = pctrl->dev;
1794f78f152aSAndy Shevchenko 	void __iomem *padcfg;
1795f78f152aSAndy Shevchenko 
1796f78f152aSAndy Shevchenko 	padcfg = intel_get_padcfg(pctrl, pin, reg);
1797f78f152aSAndy Shevchenko 	if (!padcfg)
1798f78f152aSAndy Shevchenko 		return;
1799f78f152aSAndy Shevchenko 
1800942c5ea4SAndy Shevchenko 	if (!intel_gpio_update_reg(padcfg, ~mask, saved))
1801f78f152aSAndy Shevchenko 		return;
1802f78f152aSAndy Shevchenko 
1803f78f152aSAndy Shevchenko 	dev_dbg(dev, "restored pin %u padcfg%u %#08x\n", pin, n, readl(padcfg));
1804f78f152aSAndy Shevchenko }
1805f78f152aSAndy Shevchenko 
18062fef3276SBinbin Wu int intel_pinctrl_resume_noirq(struct device *dev)
18077981c001SMika Westerberg {
1808cb035d74SWolfram Sang 	struct intel_pinctrl *pctrl = dev_get_drvdata(dev);
18097981c001SMika Westerberg 	const struct intel_community_context *communities;
18107981c001SMika Westerberg 	const struct intel_pad_context *pads;
18117981c001SMika Westerberg 	int i;
18127981c001SMika Westerberg 
18137981c001SMika Westerberg 	/* Mask all interrupts */
18147981c001SMika Westerberg 	intel_gpio_irq_init(pctrl);
18157981c001SMika Westerberg 
18167981c001SMika Westerberg 	pads = pctrl->context.pads;
18177981c001SMika Westerberg 	for (i = 0; i < pctrl->soc->npins; i++) {
18187981c001SMika Westerberg 		const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
18197981c001SMika Westerberg 
1820c538b943SMika Westerberg 		if (!intel_pinctrl_should_save(pctrl, desc->number))
18217981c001SMika Westerberg 			continue;
18227981c001SMika Westerberg 
1823f78f152aSAndy Shevchenko 		intel_restore_padcfg(pctrl, desc->number, PADCFG0, pads[i].padcfg0);
1824f78f152aSAndy Shevchenko 		intel_restore_padcfg(pctrl, desc->number, PADCFG1, pads[i].padcfg1);
1825f78f152aSAndy Shevchenko 		intel_restore_padcfg(pctrl, desc->number, PADCFG2, pads[i].padcfg2);
18267981c001SMika Westerberg 	}
18277981c001SMika Westerberg 
18287981c001SMika Westerberg 	communities = pctrl->context.communities;
18297981c001SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
18307981c001SMika Westerberg 		struct intel_community *community = &pctrl->communities[i];
18317981c001SMika Westerberg 		void __iomem *base;
183204035f7fSAndy Shevchenko 		unsigned int gpp;
18337981c001SMika Westerberg 
18347981c001SMika Westerberg 		base = community->regs + community->ie_offset;
1835471dd9a9SAndy Shevchenko 		for (gpp = 0; gpp < community->ngpps; gpp++)
1836471dd9a9SAndy Shevchenko 			intel_restore_intmask(pctrl, i, base, gpp, communities[i].intmask[gpp]);
1837a0a5f766SChris Chiu 
1838a0a5f766SChris Chiu 		base = community->regs + community->hostown_offset;
18397101e022SAndy Shevchenko 		for (gpp = 0; gpp < community->ngpps; gpp++)
18407101e022SAndy Shevchenko 			intel_restore_hostown(pctrl, i, base, gpp, communities[i].hostown[gpp]);
18417981c001SMika Westerberg 	}
18427981c001SMika Westerberg 
18437981c001SMika Westerberg 	return 0;
18447981c001SMika Westerberg }
18452fef3276SBinbin Wu EXPORT_SYMBOL_GPL(intel_pinctrl_resume_noirq);
18467981c001SMika Westerberg #endif
18477981c001SMika Westerberg 
18487981c001SMika Westerberg MODULE_AUTHOR("Mathias Nyman <mathias.nyman@linux.intel.com>");
18497981c001SMika Westerberg MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
18507981c001SMika Westerberg MODULE_DESCRIPTION("Intel pinctrl/GPIO core driver");
18517981c001SMika Westerberg MODULE_LICENSE("GPL v2");
1852