xref: /openbmc/linux/drivers/pinctrl/intel/pinctrl-intel.c (revision 57ff2df1b952c7934d7b0e1d3a2ec403ec76edec)
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>
11193b40c8SMika Westerberg #include <linux/interrupt.h>
127981c001SMika Westerberg #include <linux/gpio/driver.h>
13e57725eaSMika Westerberg #include <linux/log2.h>
146a33a1d6SAndy Shevchenko #include <linux/module.h>
157981c001SMika Westerberg #include <linux/platform_device.h>
16924cf800SAndy Shevchenko #include <linux/property.h>
176a33a1d6SAndy Shevchenko #include <linux/time.h>
18924cf800SAndy Shevchenko 
197981c001SMika Westerberg #include <linux/pinctrl/pinctrl.h>
207981c001SMika Westerberg #include <linux/pinctrl/pinmux.h>
217981c001SMika Westerberg #include <linux/pinctrl/pinconf.h>
227981c001SMika Westerberg #include <linux/pinctrl/pinconf-generic.h>
237981c001SMika Westerberg 
24c538b943SMika Westerberg #include "../core.h"
257981c001SMika Westerberg #include "pinctrl-intel.h"
267981c001SMika Westerberg 
277981c001SMika Westerberg /* Offset from regs */
28e57725eaSMika Westerberg #define REVID				0x000
29e57725eaSMika Westerberg #define REVID_SHIFT			16
30e57725eaSMika Westerberg #define REVID_MASK			GENMASK(31, 16)
31e57725eaSMika Westerberg 
327981c001SMika Westerberg #define PADBAR				0x00c
337981c001SMika Westerberg 
347981c001SMika Westerberg #define PADOWN_BITS			4
357981c001SMika Westerberg #define PADOWN_SHIFT(p)			((p) % 8 * PADOWN_BITS)
36e58926e7SAndy Shevchenko #define PADOWN_MASK(p)			(GENMASK(3, 0) << PADOWN_SHIFT(p))
3799a735b3SQipeng Zha #define PADOWN_GPP(p)			((p) / 8)
387981c001SMika Westerberg 
397981c001SMika Westerberg /* Offset from pad_regs */
407981c001SMika Westerberg #define PADCFG0				0x000
417981c001SMika Westerberg #define PADCFG0_RXEVCFG_SHIFT		25
42e58926e7SAndy Shevchenko #define PADCFG0_RXEVCFG_MASK		GENMASK(26, 25)
437981c001SMika Westerberg #define PADCFG0_RXEVCFG_LEVEL		0
447981c001SMika Westerberg #define PADCFG0_RXEVCFG_EDGE		1
457981c001SMika Westerberg #define PADCFG0_RXEVCFG_DISABLED	2
467981c001SMika Westerberg #define PADCFG0_RXEVCFG_EDGE_BOTH	3
47e57725eaSMika Westerberg #define PADCFG0_PREGFRXSEL		BIT(24)
487981c001SMika Westerberg #define PADCFG0_RXINV			BIT(23)
497981c001SMika Westerberg #define PADCFG0_GPIROUTIOXAPIC		BIT(20)
507981c001SMika Westerberg #define PADCFG0_GPIROUTSCI		BIT(19)
517981c001SMika Westerberg #define PADCFG0_GPIROUTSMI		BIT(18)
527981c001SMika Westerberg #define PADCFG0_GPIROUTNMI		BIT(17)
537981c001SMika Westerberg #define PADCFG0_PMODE_SHIFT		10
54e58926e7SAndy Shevchenko #define PADCFG0_PMODE_MASK		GENMASK(13, 10)
557981c001SMika Westerberg #define PADCFG0_GPIORXDIS		BIT(9)
567981c001SMika Westerberg #define PADCFG0_GPIOTXDIS		BIT(8)
577981c001SMika Westerberg #define PADCFG0_GPIORXSTATE		BIT(1)
587981c001SMika Westerberg #define PADCFG0_GPIOTXSTATE		BIT(0)
597981c001SMika Westerberg 
607981c001SMika Westerberg #define PADCFG1				0x004
617981c001SMika Westerberg #define PADCFG1_TERM_UP			BIT(13)
627981c001SMika Westerberg #define PADCFG1_TERM_SHIFT		10
63e58926e7SAndy Shevchenko #define PADCFG1_TERM_MASK		GENMASK(12, 10)
647981c001SMika Westerberg #define PADCFG1_TERM_20K		4
657981c001SMika Westerberg #define PADCFG1_TERM_2K			3
667981c001SMika Westerberg #define PADCFG1_TERM_5K			2
677981c001SMika Westerberg #define PADCFG1_TERM_1K			1
687981c001SMika Westerberg 
69e57725eaSMika Westerberg #define PADCFG2				0x008
70e57725eaSMika Westerberg #define PADCFG2_DEBEN			BIT(0)
71e57725eaSMika Westerberg #define PADCFG2_DEBOUNCE_SHIFT		1
72e57725eaSMika Westerberg #define PADCFG2_DEBOUNCE_MASK		GENMASK(4, 1)
73e57725eaSMika Westerberg 
746a33a1d6SAndy Shevchenko #define DEBOUNCE_PERIOD_NSEC		31250
75e57725eaSMika Westerberg 
767981c001SMika Westerberg struct intel_pad_context {
777981c001SMika Westerberg 	u32 padcfg0;
787981c001SMika Westerberg 	u32 padcfg1;
79e57725eaSMika Westerberg 	u32 padcfg2;
807981c001SMika Westerberg };
817981c001SMika Westerberg 
827981c001SMika Westerberg struct intel_community_context {
837981c001SMika Westerberg 	u32 *intmask;
84a0a5f766SChris Chiu 	u32 *hostown;
857981c001SMika Westerberg };
867981c001SMika Westerberg 
877981c001SMika Westerberg struct intel_pinctrl_context {
887981c001SMika Westerberg 	struct intel_pad_context *pads;
897981c001SMika Westerberg 	struct intel_community_context *communities;
907981c001SMika Westerberg };
917981c001SMika Westerberg 
927981c001SMika Westerberg /**
937981c001SMika Westerberg  * struct intel_pinctrl - Intel pinctrl private structure
947981c001SMika Westerberg  * @dev: Pointer to the device structure
957981c001SMika Westerberg  * @lock: Lock to serialize register access
967981c001SMika Westerberg  * @pctldesc: Pin controller description
977981c001SMika Westerberg  * @pctldev: Pointer to the pin controller device
987981c001SMika Westerberg  * @chip: GPIO chip in this pin controller
99*57ff2df1SAndy Shevchenko  * @irqchip: IRQ chip in this pin controller
1007981c001SMika Westerberg  * @soc: SoC/PCH specific pin configuration data
1017981c001SMika Westerberg  * @communities: All communities in this pin controller
1027981c001SMika Westerberg  * @ncommunities: Number of communities in this pin controller
1037981c001SMika Westerberg  * @context: Configuration saved over system sleep
10401dabe91SNilesh Bacchewar  * @irq: pinctrl/GPIO chip irq number
1057981c001SMika Westerberg  */
1067981c001SMika Westerberg struct intel_pinctrl {
1077981c001SMika Westerberg 	struct device *dev;
10827d9098cSMika Westerberg 	raw_spinlock_t lock;
1097981c001SMika Westerberg 	struct pinctrl_desc pctldesc;
1107981c001SMika Westerberg 	struct pinctrl_dev *pctldev;
1117981c001SMika Westerberg 	struct gpio_chip chip;
112*57ff2df1SAndy Shevchenko 	struct irq_chip irqchip;
1137981c001SMika Westerberg 	const struct intel_pinctrl_soc_data *soc;
1147981c001SMika Westerberg 	struct intel_community *communities;
1157981c001SMika Westerberg 	size_t ncommunities;
1167981c001SMika Westerberg 	struct intel_pinctrl_context context;
11701dabe91SNilesh Bacchewar 	int irq;
1187981c001SMika Westerberg };
1197981c001SMika Westerberg 
1207981c001SMika Westerberg #define pin_to_padno(c, p)	((p) - (c)->pin_base)
121919eb475SMika Westerberg #define padgroup_offset(g, p)	((p) - (g)->base)
1227981c001SMika Westerberg 
1237981c001SMika Westerberg static struct intel_community *intel_get_community(struct intel_pinctrl *pctrl,
12404035f7fSAndy Shevchenko 						   unsigned int pin)
1257981c001SMika Westerberg {
1267981c001SMika Westerberg 	struct intel_community *community;
1277981c001SMika Westerberg 	int i;
1287981c001SMika Westerberg 
1297981c001SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
1307981c001SMika Westerberg 		community = &pctrl->communities[i];
1317981c001SMika Westerberg 		if (pin >= community->pin_base &&
1327981c001SMika Westerberg 		    pin < community->pin_base + community->npins)
1337981c001SMika Westerberg 			return community;
1347981c001SMika Westerberg 	}
1357981c001SMika Westerberg 
1367981c001SMika Westerberg 	dev_warn(pctrl->dev, "failed to find community for pin %u\n", pin);
1377981c001SMika Westerberg 	return NULL;
1387981c001SMika Westerberg }
1397981c001SMika Westerberg 
140919eb475SMika Westerberg static const struct intel_padgroup *
141919eb475SMika Westerberg intel_community_get_padgroup(const struct intel_community *community,
14204035f7fSAndy Shevchenko 			     unsigned int pin)
143919eb475SMika Westerberg {
144919eb475SMika Westerberg 	int i;
145919eb475SMika Westerberg 
146919eb475SMika Westerberg 	for (i = 0; i < community->ngpps; i++) {
147919eb475SMika Westerberg 		const struct intel_padgroup *padgrp = &community->gpps[i];
148919eb475SMika Westerberg 
149919eb475SMika Westerberg 		if (pin >= padgrp->base && pin < padgrp->base + padgrp->size)
150919eb475SMika Westerberg 			return padgrp;
151919eb475SMika Westerberg 	}
152919eb475SMika Westerberg 
153919eb475SMika Westerberg 	return NULL;
154919eb475SMika Westerberg }
155919eb475SMika Westerberg 
15604035f7fSAndy Shevchenko static void __iomem *intel_get_padcfg(struct intel_pinctrl *pctrl,
15704035f7fSAndy Shevchenko 				      unsigned int pin, unsigned int reg)
1587981c001SMika Westerberg {
1597981c001SMika Westerberg 	const struct intel_community *community;
16004035f7fSAndy Shevchenko 	unsigned int padno;
161e57725eaSMika Westerberg 	size_t nregs;
1627981c001SMika Westerberg 
1637981c001SMika Westerberg 	community = intel_get_community(pctrl, pin);
1647981c001SMika Westerberg 	if (!community)
1657981c001SMika Westerberg 		return NULL;
1667981c001SMika Westerberg 
1677981c001SMika Westerberg 	padno = pin_to_padno(community, pin);
168e57725eaSMika Westerberg 	nregs = (community->features & PINCTRL_FEATURE_DEBOUNCE) ? 4 : 2;
169e57725eaSMika Westerberg 
1707eb7ecddSAndy Shevchenko 	if (reg >= nregs * 4)
171e57725eaSMika Westerberg 		return NULL;
172e57725eaSMika Westerberg 
173e57725eaSMika Westerberg 	return community->pad_regs + reg + padno * nregs * 4;
1747981c001SMika Westerberg }
1757981c001SMika Westerberg 
17604035f7fSAndy Shevchenko static bool intel_pad_owned_by_host(struct intel_pinctrl *pctrl, unsigned int pin)
1777981c001SMika Westerberg {
1787981c001SMika Westerberg 	const struct intel_community *community;
179919eb475SMika Westerberg 	const struct intel_padgroup *padgrp;
18004035f7fSAndy Shevchenko 	unsigned int gpp, offset, gpp_offset;
1817981c001SMika Westerberg 	void __iomem *padown;
1827981c001SMika Westerberg 
1837981c001SMika Westerberg 	community = intel_get_community(pctrl, pin);
1847981c001SMika Westerberg 	if (!community)
1857981c001SMika Westerberg 		return false;
1867981c001SMika Westerberg 	if (!community->padown_offset)
1877981c001SMika Westerberg 		return true;
1887981c001SMika Westerberg 
189919eb475SMika Westerberg 	padgrp = intel_community_get_padgroup(community, pin);
190919eb475SMika Westerberg 	if (!padgrp)
191919eb475SMika Westerberg 		return false;
192919eb475SMika Westerberg 
193919eb475SMika Westerberg 	gpp_offset = padgroup_offset(padgrp, pin);
194919eb475SMika Westerberg 	gpp = PADOWN_GPP(gpp_offset);
195919eb475SMika Westerberg 	offset = community->padown_offset + padgrp->padown_num * 4 + gpp * 4;
1967981c001SMika Westerberg 	padown = community->regs + offset;
1977981c001SMika Westerberg 
198919eb475SMika Westerberg 	return !(readl(padown) & PADOWN_MASK(gpp_offset));
1997981c001SMika Westerberg }
2007981c001SMika Westerberg 
20104035f7fSAndy Shevchenko static bool intel_pad_acpi_mode(struct intel_pinctrl *pctrl, unsigned int pin)
2027981c001SMika Westerberg {
2037981c001SMika Westerberg 	const struct intel_community *community;
204919eb475SMika Westerberg 	const struct intel_padgroup *padgrp;
20504035f7fSAndy Shevchenko 	unsigned int offset, gpp_offset;
2067981c001SMika Westerberg 	void __iomem *hostown;
2077981c001SMika Westerberg 
2087981c001SMika Westerberg 	community = intel_get_community(pctrl, pin);
2097981c001SMika Westerberg 	if (!community)
2107981c001SMika Westerberg 		return true;
2117981c001SMika Westerberg 	if (!community->hostown_offset)
2127981c001SMika Westerberg 		return false;
2137981c001SMika Westerberg 
214919eb475SMika Westerberg 	padgrp = intel_community_get_padgroup(community, pin);
215919eb475SMika Westerberg 	if (!padgrp)
216919eb475SMika Westerberg 		return true;
217919eb475SMika Westerberg 
218919eb475SMika Westerberg 	gpp_offset = padgroup_offset(padgrp, pin);
219919eb475SMika Westerberg 	offset = community->hostown_offset + padgrp->reg_num * 4;
2207981c001SMika Westerberg 	hostown = community->regs + offset;
2217981c001SMika Westerberg 
222919eb475SMika Westerberg 	return !(readl(hostown) & BIT(gpp_offset));
2237981c001SMika Westerberg }
2247981c001SMika Westerberg 
2251bd23153SAndy Shevchenko /**
2261bd23153SAndy Shevchenko  * enum - Locking variants of the pad configuration
2271bd23153SAndy Shevchenko  *
2281bd23153SAndy Shevchenko  * @PAD_UNLOCKED:	pad is fully controlled by the configuration registers
2291bd23153SAndy Shevchenko  * @PAD_LOCKED:		pad configuration registers, except TX state, are locked
2301bd23153SAndy Shevchenko  * @PAD_LOCKED_TX:	pad configuration TX state is locked
2311bd23153SAndy Shevchenko  * @PAD_LOCKED_FULL:	pad configuration registers are locked completely
2321bd23153SAndy Shevchenko  *
2331bd23153SAndy Shevchenko  * Locking is considered as read-only mode for corresponding registers and
2341bd23153SAndy Shevchenko  * their respective fields. That said, TX state bit is locked separately from
2351bd23153SAndy Shevchenko  * the main locking scheme.
2361bd23153SAndy Shevchenko  */
2371bd23153SAndy Shevchenko enum {
2381bd23153SAndy Shevchenko 	PAD_UNLOCKED	= 0,
2391bd23153SAndy Shevchenko 	PAD_LOCKED	= 1,
2401bd23153SAndy Shevchenko 	PAD_LOCKED_TX	= 2,
2411bd23153SAndy Shevchenko 	PAD_LOCKED_FULL	= PAD_LOCKED | PAD_LOCKED_TX,
2421bd23153SAndy Shevchenko };
2431bd23153SAndy Shevchenko 
2441bd23153SAndy Shevchenko static int intel_pad_locked(struct intel_pinctrl *pctrl, unsigned int pin)
2457981c001SMika Westerberg {
2467981c001SMika Westerberg 	struct intel_community *community;
247919eb475SMika Westerberg 	const struct intel_padgroup *padgrp;
24804035f7fSAndy Shevchenko 	unsigned int offset, gpp_offset;
2497981c001SMika Westerberg 	u32 value;
2501bd23153SAndy Shevchenko 	int ret = PAD_UNLOCKED;
2517981c001SMika Westerberg 
2527981c001SMika Westerberg 	community = intel_get_community(pctrl, pin);
2537981c001SMika Westerberg 	if (!community)
2541bd23153SAndy Shevchenko 		return PAD_LOCKED_FULL;
2557981c001SMika Westerberg 	if (!community->padcfglock_offset)
2561bd23153SAndy Shevchenko 		return PAD_UNLOCKED;
2577981c001SMika Westerberg 
258919eb475SMika Westerberg 	padgrp = intel_community_get_padgroup(community, pin);
259919eb475SMika Westerberg 	if (!padgrp)
2601bd23153SAndy Shevchenko 		return PAD_LOCKED_FULL;
261919eb475SMika Westerberg 
262919eb475SMika Westerberg 	gpp_offset = padgroup_offset(padgrp, pin);
2637981c001SMika Westerberg 
2647981c001SMika Westerberg 	/*
2657981c001SMika Westerberg 	 * If PADCFGLOCK and PADCFGLOCKTX bits are both clear for this pad,
2667981c001SMika Westerberg 	 * the pad is considered unlocked. Any other case means that it is
2671bd23153SAndy Shevchenko 	 * either fully or partially locked.
2687981c001SMika Westerberg 	 */
2691bd23153SAndy Shevchenko 	offset = community->padcfglock_offset + 0 + padgrp->reg_num * 8;
2707981c001SMika Westerberg 	value = readl(community->regs + offset);
271919eb475SMika Westerberg 	if (value & BIT(gpp_offset))
2721bd23153SAndy Shevchenko 		ret |= PAD_LOCKED;
2737981c001SMika Westerberg 
274919eb475SMika Westerberg 	offset = community->padcfglock_offset + 4 + padgrp->reg_num * 8;
2757981c001SMika Westerberg 	value = readl(community->regs + offset);
276919eb475SMika Westerberg 	if (value & BIT(gpp_offset))
2771bd23153SAndy Shevchenko 		ret |= PAD_LOCKED_TX;
2787981c001SMika Westerberg 
2791bd23153SAndy Shevchenko 	return ret;
2801bd23153SAndy Shevchenko }
2811bd23153SAndy Shevchenko 
2821bd23153SAndy Shevchenko static bool intel_pad_is_unlocked(struct intel_pinctrl *pctrl, unsigned int pin)
2831bd23153SAndy Shevchenko {
2841bd23153SAndy Shevchenko 	return (intel_pad_locked(pctrl, pin) & PAD_LOCKED) == PAD_UNLOCKED;
2857981c001SMika Westerberg }
2867981c001SMika Westerberg 
28704035f7fSAndy Shevchenko static bool intel_pad_usable(struct intel_pinctrl *pctrl, unsigned int pin)
2887981c001SMika Westerberg {
2891bd23153SAndy Shevchenko 	return intel_pad_owned_by_host(pctrl, pin) && intel_pad_is_unlocked(pctrl, pin);
2907981c001SMika Westerberg }
2917981c001SMika Westerberg 
2927981c001SMika Westerberg static int intel_get_groups_count(struct pinctrl_dev *pctldev)
2937981c001SMika Westerberg {
2947981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
2957981c001SMika Westerberg 
2967981c001SMika Westerberg 	return pctrl->soc->ngroups;
2977981c001SMika Westerberg }
2987981c001SMika Westerberg 
2997981c001SMika Westerberg static const char *intel_get_group_name(struct pinctrl_dev *pctldev,
30004035f7fSAndy Shevchenko 				      unsigned int group)
3017981c001SMika Westerberg {
3027981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
3037981c001SMika Westerberg 
3047981c001SMika Westerberg 	return pctrl->soc->groups[group].name;
3057981c001SMika Westerberg }
3067981c001SMika Westerberg 
30704035f7fSAndy Shevchenko static int intel_get_group_pins(struct pinctrl_dev *pctldev, unsigned int group,
30804035f7fSAndy Shevchenko 			      const unsigned int **pins, unsigned int *npins)
3097981c001SMika Westerberg {
3107981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
3117981c001SMika Westerberg 
3127981c001SMika Westerberg 	*pins = pctrl->soc->groups[group].pins;
3137981c001SMika Westerberg 	*npins = pctrl->soc->groups[group].npins;
3147981c001SMika Westerberg 	return 0;
3157981c001SMika Westerberg }
3167981c001SMika Westerberg 
3177981c001SMika Westerberg static void intel_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
31804035f7fSAndy Shevchenko 			       unsigned int pin)
3197981c001SMika Westerberg {
3207981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
321e57725eaSMika Westerberg 	void __iomem *padcfg;
3227981c001SMika Westerberg 	u32 cfg0, cfg1, mode;
3231bd23153SAndy Shevchenko 	int locked;
3241bd23153SAndy Shevchenko 	bool acpi;
3257981c001SMika Westerberg 
3267981c001SMika Westerberg 	if (!intel_pad_owned_by_host(pctrl, pin)) {
3277981c001SMika Westerberg 		seq_puts(s, "not available");
3287981c001SMika Westerberg 		return;
3297981c001SMika Westerberg 	}
3307981c001SMika Westerberg 
3317981c001SMika Westerberg 	cfg0 = readl(intel_get_padcfg(pctrl, pin, PADCFG0));
3327981c001SMika Westerberg 	cfg1 = readl(intel_get_padcfg(pctrl, pin, PADCFG1));
3337981c001SMika Westerberg 
3347981c001SMika Westerberg 	mode = (cfg0 & PADCFG0_PMODE_MASK) >> PADCFG0_PMODE_SHIFT;
3357981c001SMika Westerberg 	if (!mode)
3367981c001SMika Westerberg 		seq_puts(s, "GPIO ");
3377981c001SMika Westerberg 	else
3387981c001SMika Westerberg 		seq_printf(s, "mode %d ", mode);
3397981c001SMika Westerberg 
3407981c001SMika Westerberg 	seq_printf(s, "0x%08x 0x%08x", cfg0, cfg1);
3417981c001SMika Westerberg 
342e57725eaSMika Westerberg 	/* Dump the additional PADCFG registers if available */
343e57725eaSMika Westerberg 	padcfg = intel_get_padcfg(pctrl, pin, PADCFG2);
344e57725eaSMika Westerberg 	if (padcfg)
345e57725eaSMika Westerberg 		seq_printf(s, " 0x%08x", readl(padcfg));
346e57725eaSMika Westerberg 
3477981c001SMika Westerberg 	locked = intel_pad_locked(pctrl, pin);
3484341e8a5SMika Westerberg 	acpi = intel_pad_acpi_mode(pctrl, pin);
3497981c001SMika Westerberg 
3507981c001SMika Westerberg 	if (locked || acpi) {
3517981c001SMika Westerberg 		seq_puts(s, " [");
3521bd23153SAndy Shevchenko 		if (locked)
3537981c001SMika Westerberg 			seq_puts(s, "LOCKED");
3541bd23153SAndy Shevchenko 		if ((locked & PAD_LOCKED_FULL) == PAD_LOCKED_TX)
3551bd23153SAndy Shevchenko 			seq_puts(s, " tx");
3561bd23153SAndy Shevchenko 		else if ((locked & PAD_LOCKED_FULL) == PAD_LOCKED_FULL)
3571bd23153SAndy Shevchenko 			seq_puts(s, " full");
3581bd23153SAndy Shevchenko 
3591bd23153SAndy Shevchenko 		if (locked && acpi)
3607981c001SMika Westerberg 			seq_puts(s, ", ");
3611bd23153SAndy Shevchenko 
3627981c001SMika Westerberg 		if (acpi)
3637981c001SMika Westerberg 			seq_puts(s, "ACPI");
3647981c001SMika Westerberg 		seq_puts(s, "]");
3657981c001SMika Westerberg 	}
3667981c001SMika Westerberg }
3677981c001SMika Westerberg 
3687981c001SMika Westerberg static const struct pinctrl_ops intel_pinctrl_ops = {
3697981c001SMika Westerberg 	.get_groups_count = intel_get_groups_count,
3707981c001SMika Westerberg 	.get_group_name = intel_get_group_name,
3717981c001SMika Westerberg 	.get_group_pins = intel_get_group_pins,
3727981c001SMika Westerberg 	.pin_dbg_show = intel_pin_dbg_show,
3737981c001SMika Westerberg };
3747981c001SMika Westerberg 
3757981c001SMika Westerberg static int intel_get_functions_count(struct pinctrl_dev *pctldev)
3767981c001SMika Westerberg {
3777981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
3787981c001SMika Westerberg 
3797981c001SMika Westerberg 	return pctrl->soc->nfunctions;
3807981c001SMika Westerberg }
3817981c001SMika Westerberg 
3827981c001SMika Westerberg static const char *intel_get_function_name(struct pinctrl_dev *pctldev,
38304035f7fSAndy Shevchenko 					   unsigned int function)
3847981c001SMika Westerberg {
3857981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
3867981c001SMika Westerberg 
3877981c001SMika Westerberg 	return pctrl->soc->functions[function].name;
3887981c001SMika Westerberg }
3897981c001SMika Westerberg 
3907981c001SMika Westerberg static int intel_get_function_groups(struct pinctrl_dev *pctldev,
39104035f7fSAndy Shevchenko 				     unsigned int function,
3927981c001SMika Westerberg 				     const char * const **groups,
39304035f7fSAndy Shevchenko 				     unsigned int * const ngroups)
3947981c001SMika Westerberg {
3957981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
3967981c001SMika Westerberg 
3977981c001SMika Westerberg 	*groups = pctrl->soc->functions[function].groups;
3987981c001SMika Westerberg 	*ngroups = pctrl->soc->functions[function].ngroups;
3997981c001SMika Westerberg 	return 0;
4007981c001SMika Westerberg }
4017981c001SMika Westerberg 
40204035f7fSAndy Shevchenko static int intel_pinmux_set_mux(struct pinctrl_dev *pctldev,
40304035f7fSAndy Shevchenko 				unsigned int function, unsigned int group)
4047981c001SMika Westerberg {
4057981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
4067981c001SMika Westerberg 	const struct intel_pingroup *grp = &pctrl->soc->groups[group];
4077981c001SMika Westerberg 	unsigned long flags;
4087981c001SMika Westerberg 	int i;
4097981c001SMika Westerberg 
41027d9098cSMika Westerberg 	raw_spin_lock_irqsave(&pctrl->lock, flags);
4117981c001SMika Westerberg 
4127981c001SMika Westerberg 	/*
4137981c001SMika Westerberg 	 * All pins in the groups needs to be accessible and writable
4147981c001SMika Westerberg 	 * before we can enable the mux for this group.
4157981c001SMika Westerberg 	 */
4167981c001SMika Westerberg 	for (i = 0; i < grp->npins; i++) {
4177981c001SMika Westerberg 		if (!intel_pad_usable(pctrl, grp->pins[i])) {
41827d9098cSMika Westerberg 			raw_spin_unlock_irqrestore(&pctrl->lock, flags);
4197981c001SMika Westerberg 			return -EBUSY;
4207981c001SMika Westerberg 		}
4217981c001SMika Westerberg 	}
4227981c001SMika Westerberg 
4237981c001SMika Westerberg 	/* Now enable the mux setting for each pin in the group */
4247981c001SMika Westerberg 	for (i = 0; i < grp->npins; i++) {
4257981c001SMika Westerberg 		void __iomem *padcfg0;
4267981c001SMika Westerberg 		u32 value;
4277981c001SMika Westerberg 
4287981c001SMika Westerberg 		padcfg0 = intel_get_padcfg(pctrl, grp->pins[i], PADCFG0);
4297981c001SMika Westerberg 		value = readl(padcfg0);
4307981c001SMika Westerberg 
4317981c001SMika Westerberg 		value &= ~PADCFG0_PMODE_MASK;
4321f6b419bSMika Westerberg 
4331f6b419bSMika Westerberg 		if (grp->modes)
4341f6b419bSMika Westerberg 			value |= grp->modes[i] << PADCFG0_PMODE_SHIFT;
4351f6b419bSMika Westerberg 		else
4367981c001SMika Westerberg 			value |= grp->mode << PADCFG0_PMODE_SHIFT;
4377981c001SMika Westerberg 
4387981c001SMika Westerberg 		writel(value, padcfg0);
4397981c001SMika Westerberg 	}
4407981c001SMika Westerberg 
44127d9098cSMika Westerberg 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
4427981c001SMika Westerberg 
4437981c001SMika Westerberg 	return 0;
4447981c001SMika Westerberg }
4457981c001SMika Westerberg 
44617fab473SAndy Shevchenko static void __intel_gpio_set_direction(void __iomem *padcfg0, bool input)
44717fab473SAndy Shevchenko {
44817fab473SAndy Shevchenko 	u32 value;
44917fab473SAndy Shevchenko 
45017fab473SAndy Shevchenko 	value = readl(padcfg0);
45117fab473SAndy Shevchenko 	if (input) {
45217fab473SAndy Shevchenko 		value &= ~PADCFG0_GPIORXDIS;
45317fab473SAndy Shevchenko 		value |= PADCFG0_GPIOTXDIS;
45417fab473SAndy Shevchenko 	} else {
45517fab473SAndy Shevchenko 		value &= ~PADCFG0_GPIOTXDIS;
45617fab473SAndy Shevchenko 		value |= PADCFG0_GPIORXDIS;
45717fab473SAndy Shevchenko 	}
45817fab473SAndy Shevchenko 	writel(value, padcfg0);
45917fab473SAndy Shevchenko }
46017fab473SAndy Shevchenko 
461f5a26acfSMika Westerberg static void intel_gpio_set_gpio_mode(void __iomem *padcfg0)
462f5a26acfSMika Westerberg {
463f5a26acfSMika Westerberg 	u32 value;
464f5a26acfSMika Westerberg 
465f5a26acfSMika Westerberg 	/* Put the pad into GPIO mode */
466f5a26acfSMika Westerberg 	value = readl(padcfg0) & ~PADCFG0_PMODE_MASK;
467f5a26acfSMika Westerberg 	/* Disable SCI/SMI/NMI generation */
468f5a26acfSMika Westerberg 	value &= ~(PADCFG0_GPIROUTIOXAPIC | PADCFG0_GPIROUTSCI);
469f5a26acfSMika Westerberg 	value &= ~(PADCFG0_GPIROUTSMI | PADCFG0_GPIROUTNMI);
470f5a26acfSMika Westerberg 	writel(value, padcfg0);
471f5a26acfSMika Westerberg }
472f5a26acfSMika Westerberg 
4737981c001SMika Westerberg static int intel_gpio_request_enable(struct pinctrl_dev *pctldev,
4747981c001SMika Westerberg 				     struct pinctrl_gpio_range *range,
47504035f7fSAndy Shevchenko 				     unsigned int pin)
4767981c001SMika Westerberg {
4777981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
4787981c001SMika Westerberg 	void __iomem *padcfg0;
4797981c001SMika Westerberg 	unsigned long flags;
4807981c001SMika Westerberg 
48127d9098cSMika Westerberg 	raw_spin_lock_irqsave(&pctrl->lock, flags);
4827981c001SMika Westerberg 
4831bd23153SAndy Shevchenko 	if (!intel_pad_owned_by_host(pctrl, pin)) {
48427d9098cSMika Westerberg 		raw_spin_unlock_irqrestore(&pctrl->lock, flags);
4857981c001SMika Westerberg 		return -EBUSY;
4867981c001SMika Westerberg 	}
4877981c001SMika Westerberg 
4881bd23153SAndy Shevchenko 	if (!intel_pad_is_unlocked(pctrl, pin)) {
4891bd23153SAndy Shevchenko 		raw_spin_unlock_irqrestore(&pctrl->lock, flags);
4901bd23153SAndy Shevchenko 		return 0;
4911bd23153SAndy Shevchenko 	}
4921bd23153SAndy Shevchenko 
4937981c001SMika Westerberg 	padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
494f5a26acfSMika Westerberg 	intel_gpio_set_gpio_mode(padcfg0);
49517fab473SAndy Shevchenko 	/* Disable TX buffer and enable RX (this will be input) */
49617fab473SAndy Shevchenko 	__intel_gpio_set_direction(padcfg0, true);
49717fab473SAndy Shevchenko 
49827d9098cSMika Westerberg 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
4997981c001SMika Westerberg 
5007981c001SMika Westerberg 	return 0;
5017981c001SMika Westerberg }
5027981c001SMika Westerberg 
5037981c001SMika Westerberg static int intel_gpio_set_direction(struct pinctrl_dev *pctldev,
5047981c001SMika Westerberg 				    struct pinctrl_gpio_range *range,
50504035f7fSAndy Shevchenko 				    unsigned int pin, bool input)
5067981c001SMika Westerberg {
5077981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
5087981c001SMika Westerberg 	void __iomem *padcfg0;
5097981c001SMika Westerberg 	unsigned long flags;
5107981c001SMika Westerberg 
51127d9098cSMika Westerberg 	raw_spin_lock_irqsave(&pctrl->lock, flags);
5127981c001SMika Westerberg 
5137981c001SMika Westerberg 	padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
51417fab473SAndy Shevchenko 	__intel_gpio_set_direction(padcfg0, input);
5157981c001SMika Westerberg 
51627d9098cSMika Westerberg 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
5177981c001SMika Westerberg 
5187981c001SMika Westerberg 	return 0;
5197981c001SMika Westerberg }
5207981c001SMika Westerberg 
5217981c001SMika Westerberg static const struct pinmux_ops intel_pinmux_ops = {
5227981c001SMika Westerberg 	.get_functions_count = intel_get_functions_count,
5237981c001SMika Westerberg 	.get_function_name = intel_get_function_name,
5247981c001SMika Westerberg 	.get_function_groups = intel_get_function_groups,
5257981c001SMika Westerberg 	.set_mux = intel_pinmux_set_mux,
5267981c001SMika Westerberg 	.gpio_request_enable = intel_gpio_request_enable,
5277981c001SMika Westerberg 	.gpio_set_direction = intel_gpio_set_direction,
5287981c001SMika Westerberg };
5297981c001SMika Westerberg 
53004035f7fSAndy Shevchenko static int intel_config_get(struct pinctrl_dev *pctldev, unsigned int pin,
5317981c001SMika Westerberg 			    unsigned long *config)
5327981c001SMika Westerberg {
5337981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
5347981c001SMika Westerberg 	enum pin_config_param param = pinconf_to_config_param(*config);
53504cc058fSMika Westerberg 	const struct intel_community *community;
5367981c001SMika Westerberg 	u32 value, term;
537e57725eaSMika Westerberg 	u32 arg = 0;
5387981c001SMika Westerberg 
5397981c001SMika Westerberg 	if (!intel_pad_owned_by_host(pctrl, pin))
5407981c001SMika Westerberg 		return -ENOTSUPP;
5417981c001SMika Westerberg 
54204cc058fSMika Westerberg 	community = intel_get_community(pctrl, pin);
5437981c001SMika Westerberg 	value = readl(intel_get_padcfg(pctrl, pin, PADCFG1));
5447981c001SMika Westerberg 	term = (value & PADCFG1_TERM_MASK) >> PADCFG1_TERM_SHIFT;
5457981c001SMika Westerberg 
5467981c001SMika Westerberg 	switch (param) {
5477981c001SMika Westerberg 	case PIN_CONFIG_BIAS_DISABLE:
5487981c001SMika Westerberg 		if (term)
5497981c001SMika Westerberg 			return -EINVAL;
5507981c001SMika Westerberg 		break;
5517981c001SMika Westerberg 
5527981c001SMika Westerberg 	case PIN_CONFIG_BIAS_PULL_UP:
5537981c001SMika Westerberg 		if (!term || !(value & PADCFG1_TERM_UP))
5547981c001SMika Westerberg 			return -EINVAL;
5557981c001SMika Westerberg 
5567981c001SMika Westerberg 		switch (term) {
5577981c001SMika Westerberg 		case PADCFG1_TERM_1K:
5587981c001SMika Westerberg 			arg = 1000;
5597981c001SMika Westerberg 			break;
5607981c001SMika Westerberg 		case PADCFG1_TERM_2K:
5617981c001SMika Westerberg 			arg = 2000;
5627981c001SMika Westerberg 			break;
5637981c001SMika Westerberg 		case PADCFG1_TERM_5K:
5647981c001SMika Westerberg 			arg = 5000;
5657981c001SMika Westerberg 			break;
5667981c001SMika Westerberg 		case PADCFG1_TERM_20K:
5677981c001SMika Westerberg 			arg = 20000;
5687981c001SMika Westerberg 			break;
5697981c001SMika Westerberg 		}
5707981c001SMika Westerberg 
5717981c001SMika Westerberg 		break;
5727981c001SMika Westerberg 
5737981c001SMika Westerberg 	case PIN_CONFIG_BIAS_PULL_DOWN:
5747981c001SMika Westerberg 		if (!term || value & PADCFG1_TERM_UP)
5757981c001SMika Westerberg 			return -EINVAL;
5767981c001SMika Westerberg 
5777981c001SMika Westerberg 		switch (term) {
57804cc058fSMika Westerberg 		case PADCFG1_TERM_1K:
57904cc058fSMika Westerberg 			if (!(community->features & PINCTRL_FEATURE_1K_PD))
58004cc058fSMika Westerberg 				return -EINVAL;
58104cc058fSMika Westerberg 			arg = 1000;
58204cc058fSMika Westerberg 			break;
5837981c001SMika Westerberg 		case PADCFG1_TERM_5K:
5847981c001SMika Westerberg 			arg = 5000;
5857981c001SMika Westerberg 			break;
5867981c001SMika Westerberg 		case PADCFG1_TERM_20K:
5877981c001SMika Westerberg 			arg = 20000;
5887981c001SMika Westerberg 			break;
5897981c001SMika Westerberg 		}
5907981c001SMika Westerberg 
5917981c001SMika Westerberg 		break;
5927981c001SMika Westerberg 
593e57725eaSMika Westerberg 	case PIN_CONFIG_INPUT_DEBOUNCE: {
594e57725eaSMika Westerberg 		void __iomem *padcfg2;
595e57725eaSMika Westerberg 		u32 v;
596e57725eaSMika Westerberg 
597e57725eaSMika Westerberg 		padcfg2 = intel_get_padcfg(pctrl, pin, PADCFG2);
598e57725eaSMika Westerberg 		if (!padcfg2)
599e57725eaSMika Westerberg 			return -ENOTSUPP;
600e57725eaSMika Westerberg 
601e57725eaSMika Westerberg 		v = readl(padcfg2);
602e57725eaSMika Westerberg 		if (!(v & PADCFG2_DEBEN))
603e57725eaSMika Westerberg 			return -EINVAL;
604e57725eaSMika Westerberg 
605e57725eaSMika Westerberg 		v = (v & PADCFG2_DEBOUNCE_MASK) >> PADCFG2_DEBOUNCE_SHIFT;
6066a33a1d6SAndy Shevchenko 		arg = BIT(v) * DEBOUNCE_PERIOD_NSEC / NSEC_PER_USEC;
607e57725eaSMika Westerberg 
608e57725eaSMika Westerberg 		break;
609e57725eaSMika Westerberg 	}
610e57725eaSMika Westerberg 
6117981c001SMika Westerberg 	default:
6127981c001SMika Westerberg 		return -ENOTSUPP;
6137981c001SMika Westerberg 	}
6147981c001SMika Westerberg 
6157981c001SMika Westerberg 	*config = pinconf_to_config_packed(param, arg);
6167981c001SMika Westerberg 	return 0;
6177981c001SMika Westerberg }
6187981c001SMika Westerberg 
61904035f7fSAndy Shevchenko static int intel_config_set_pull(struct intel_pinctrl *pctrl, unsigned int pin,
6207981c001SMika Westerberg 				 unsigned long config)
6217981c001SMika Westerberg {
62204035f7fSAndy Shevchenko 	unsigned int param = pinconf_to_config_param(config);
62304035f7fSAndy Shevchenko 	unsigned int arg = pinconf_to_config_argument(config);
62404cc058fSMika Westerberg 	const struct intel_community *community;
6257981c001SMika Westerberg 	void __iomem *padcfg1;
6267981c001SMika Westerberg 	unsigned long flags;
6277981c001SMika Westerberg 	int ret = 0;
6287981c001SMika Westerberg 	u32 value;
6297981c001SMika Westerberg 
63027d9098cSMika Westerberg 	raw_spin_lock_irqsave(&pctrl->lock, flags);
6317981c001SMika Westerberg 
63204cc058fSMika Westerberg 	community = intel_get_community(pctrl, pin);
6337981c001SMika Westerberg 	padcfg1 = intel_get_padcfg(pctrl, pin, PADCFG1);
6347981c001SMika Westerberg 	value = readl(padcfg1);
6357981c001SMika Westerberg 
6367981c001SMika Westerberg 	switch (param) {
6377981c001SMika Westerberg 	case PIN_CONFIG_BIAS_DISABLE:
6387981c001SMika Westerberg 		value &= ~(PADCFG1_TERM_MASK | PADCFG1_TERM_UP);
6397981c001SMika Westerberg 		break;
6407981c001SMika Westerberg 
6417981c001SMika Westerberg 	case PIN_CONFIG_BIAS_PULL_UP:
6427981c001SMika Westerberg 		value &= ~PADCFG1_TERM_MASK;
6437981c001SMika Westerberg 
6447981c001SMika Westerberg 		value |= PADCFG1_TERM_UP;
6457981c001SMika Westerberg 
6467981c001SMika Westerberg 		switch (arg) {
6477981c001SMika Westerberg 		case 20000:
6487981c001SMika Westerberg 			value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT;
6497981c001SMika Westerberg 			break;
6507981c001SMika Westerberg 		case 5000:
6517981c001SMika Westerberg 			value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT;
6527981c001SMika Westerberg 			break;
6537981c001SMika Westerberg 		case 2000:
6547981c001SMika Westerberg 			value |= PADCFG1_TERM_2K << PADCFG1_TERM_SHIFT;
6557981c001SMika Westerberg 			break;
6567981c001SMika Westerberg 		case 1000:
6577981c001SMika Westerberg 			value |= PADCFG1_TERM_1K << PADCFG1_TERM_SHIFT;
6587981c001SMika Westerberg 			break;
6597981c001SMika Westerberg 		default:
6607981c001SMika Westerberg 			ret = -EINVAL;
6617981c001SMika Westerberg 		}
6627981c001SMika Westerberg 
6637981c001SMika Westerberg 		break;
6647981c001SMika Westerberg 
6657981c001SMika Westerberg 	case PIN_CONFIG_BIAS_PULL_DOWN:
6667981c001SMika Westerberg 		value &= ~(PADCFG1_TERM_UP | PADCFG1_TERM_MASK);
6677981c001SMika Westerberg 
6687981c001SMika Westerberg 		switch (arg) {
6697981c001SMika Westerberg 		case 20000:
6707981c001SMika Westerberg 			value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT;
6717981c001SMika Westerberg 			break;
6727981c001SMika Westerberg 		case 5000:
6737981c001SMika Westerberg 			value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT;
6747981c001SMika Westerberg 			break;
67504cc058fSMika Westerberg 		case 1000:
676aa1dd80fSDan Carpenter 			if (!(community->features & PINCTRL_FEATURE_1K_PD)) {
677aa1dd80fSDan Carpenter 				ret = -EINVAL;
678aa1dd80fSDan Carpenter 				break;
679aa1dd80fSDan Carpenter 			}
68004cc058fSMika Westerberg 			value |= PADCFG1_TERM_1K << PADCFG1_TERM_SHIFT;
68104cc058fSMika Westerberg 			break;
6827981c001SMika Westerberg 		default:
6837981c001SMika Westerberg 			ret = -EINVAL;
6847981c001SMika Westerberg 		}
6857981c001SMika Westerberg 
6867981c001SMika Westerberg 		break;
6877981c001SMika Westerberg 	}
6887981c001SMika Westerberg 
6897981c001SMika Westerberg 	if (!ret)
6907981c001SMika Westerberg 		writel(value, padcfg1);
6917981c001SMika Westerberg 
69227d9098cSMika Westerberg 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
6937981c001SMika Westerberg 
6947981c001SMika Westerberg 	return ret;
6957981c001SMika Westerberg }
6967981c001SMika Westerberg 
69704035f7fSAndy Shevchenko static int intel_config_set_debounce(struct intel_pinctrl *pctrl,
69804035f7fSAndy Shevchenko 				     unsigned int pin, unsigned int debounce)
699e57725eaSMika Westerberg {
700e57725eaSMika Westerberg 	void __iomem *padcfg0, *padcfg2;
701e57725eaSMika Westerberg 	unsigned long flags;
702e57725eaSMika Westerberg 	u32 value0, value2;
703e57725eaSMika Westerberg 	int ret = 0;
704e57725eaSMika Westerberg 
705e57725eaSMika Westerberg 	padcfg2 = intel_get_padcfg(pctrl, pin, PADCFG2);
706e57725eaSMika Westerberg 	if (!padcfg2)
707e57725eaSMika Westerberg 		return -ENOTSUPP;
708e57725eaSMika Westerberg 
709e57725eaSMika Westerberg 	padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
710e57725eaSMika Westerberg 
711e57725eaSMika Westerberg 	raw_spin_lock_irqsave(&pctrl->lock, flags);
712e57725eaSMika Westerberg 
713e57725eaSMika Westerberg 	value0 = readl(padcfg0);
714e57725eaSMika Westerberg 	value2 = readl(padcfg2);
715e57725eaSMika Westerberg 
716e57725eaSMika Westerberg 	/* Disable glitch filter and debouncer */
717e57725eaSMika Westerberg 	value0 &= ~PADCFG0_PREGFRXSEL;
718e57725eaSMika Westerberg 	value2 &= ~(PADCFG2_DEBEN | PADCFG2_DEBOUNCE_MASK);
719e57725eaSMika Westerberg 
720e57725eaSMika Westerberg 	if (debounce) {
721e57725eaSMika Westerberg 		unsigned long v;
722e57725eaSMika Westerberg 
7236a33a1d6SAndy Shevchenko 		v = order_base_2(debounce * NSEC_PER_USEC / DEBOUNCE_PERIOD_NSEC);
724e57725eaSMika Westerberg 		if (v < 3 || v > 15) {
725e57725eaSMika Westerberg 			ret = -EINVAL;
726e57725eaSMika Westerberg 			goto exit_unlock;
727e57725eaSMika Westerberg 		} else {
728e57725eaSMika Westerberg 			/* Enable glitch filter and debouncer */
729e57725eaSMika Westerberg 			value0 |= PADCFG0_PREGFRXSEL;
730e57725eaSMika Westerberg 			value2 |= v << PADCFG2_DEBOUNCE_SHIFT;
731e57725eaSMika Westerberg 			value2 |= PADCFG2_DEBEN;
732e57725eaSMika Westerberg 		}
733e57725eaSMika Westerberg 	}
734e57725eaSMika Westerberg 
735e57725eaSMika Westerberg 	writel(value0, padcfg0);
736e57725eaSMika Westerberg 	writel(value2, padcfg2);
737e57725eaSMika Westerberg 
738e57725eaSMika Westerberg exit_unlock:
739e57725eaSMika Westerberg 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
740e57725eaSMika Westerberg 
741e57725eaSMika Westerberg 	return ret;
742e57725eaSMika Westerberg }
743e57725eaSMika Westerberg 
74404035f7fSAndy Shevchenko static int intel_config_set(struct pinctrl_dev *pctldev, unsigned int pin,
74504035f7fSAndy Shevchenko 			  unsigned long *configs, unsigned int nconfigs)
7467981c001SMika Westerberg {
7477981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
7487981c001SMika Westerberg 	int i, ret;
7497981c001SMika Westerberg 
7507981c001SMika Westerberg 	if (!intel_pad_usable(pctrl, pin))
7517981c001SMika Westerberg 		return -ENOTSUPP;
7527981c001SMika Westerberg 
7537981c001SMika Westerberg 	for (i = 0; i < nconfigs; i++) {
7547981c001SMika Westerberg 		switch (pinconf_to_config_param(configs[i])) {
7557981c001SMika Westerberg 		case PIN_CONFIG_BIAS_DISABLE:
7567981c001SMika Westerberg 		case PIN_CONFIG_BIAS_PULL_UP:
7577981c001SMika Westerberg 		case PIN_CONFIG_BIAS_PULL_DOWN:
7587981c001SMika Westerberg 			ret = intel_config_set_pull(pctrl, pin, configs[i]);
7597981c001SMika Westerberg 			if (ret)
7607981c001SMika Westerberg 				return ret;
7617981c001SMika Westerberg 			break;
7627981c001SMika Westerberg 
763e57725eaSMika Westerberg 		case PIN_CONFIG_INPUT_DEBOUNCE:
764e57725eaSMika Westerberg 			ret = intel_config_set_debounce(pctrl, pin,
765e57725eaSMika Westerberg 				pinconf_to_config_argument(configs[i]));
766e57725eaSMika Westerberg 			if (ret)
767e57725eaSMika Westerberg 				return ret;
768e57725eaSMika Westerberg 			break;
769e57725eaSMika Westerberg 
7707981c001SMika Westerberg 		default:
7717981c001SMika Westerberg 			return -ENOTSUPP;
7727981c001SMika Westerberg 		}
7737981c001SMika Westerberg 	}
7747981c001SMika Westerberg 
7757981c001SMika Westerberg 	return 0;
7767981c001SMika Westerberg }
7777981c001SMika Westerberg 
7787981c001SMika Westerberg static const struct pinconf_ops intel_pinconf_ops = {
7797981c001SMika Westerberg 	.is_generic = true,
7807981c001SMika Westerberg 	.pin_config_get = intel_config_get,
7817981c001SMika Westerberg 	.pin_config_set = intel_config_set,
7827981c001SMika Westerberg };
7837981c001SMika Westerberg 
7847981c001SMika Westerberg static const struct pinctrl_desc intel_pinctrl_desc = {
7857981c001SMika Westerberg 	.pctlops = &intel_pinctrl_ops,
7867981c001SMika Westerberg 	.pmxops = &intel_pinmux_ops,
7877981c001SMika Westerberg 	.confops = &intel_pinconf_ops,
7887981c001SMika Westerberg 	.owner = THIS_MODULE,
7897981c001SMika Westerberg };
7907981c001SMika Westerberg 
791a60eac32SMika Westerberg /**
792a60eac32SMika Westerberg  * intel_gpio_to_pin() - Translate from GPIO offset to pin number
793a60eac32SMika Westerberg  * @pctrl: Pinctrl structure
794a60eac32SMika Westerberg  * @offset: GPIO offset from gpiolib
795946ffefcSAndy Shevchenko  * @community: Community is filled here if not %NULL
796a60eac32SMika Westerberg  * @padgrp: Pad group is filled here if not %NULL
797a60eac32SMika Westerberg  *
798a60eac32SMika Westerberg  * When coming through gpiolib irqchip, the GPIO offset is not
799a60eac32SMika Westerberg  * automatically translated to pinctrl pin number. This function can be
800a60eac32SMika Westerberg  * used to find out the corresponding pinctrl pin.
801a60eac32SMika Westerberg  */
80204035f7fSAndy Shevchenko static int intel_gpio_to_pin(struct intel_pinctrl *pctrl, unsigned int offset,
803a60eac32SMika Westerberg 			     const struct intel_community **community,
804a60eac32SMika Westerberg 			     const struct intel_padgroup **padgrp)
805a60eac32SMika Westerberg {
806a60eac32SMika Westerberg 	int i;
807a60eac32SMika Westerberg 
808a60eac32SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
809a60eac32SMika Westerberg 		const struct intel_community *comm = &pctrl->communities[i];
810a60eac32SMika Westerberg 		int j;
811a60eac32SMika Westerberg 
812a60eac32SMika Westerberg 		for (j = 0; j < comm->ngpps; j++) {
813a60eac32SMika Westerberg 			const struct intel_padgroup *pgrp = &comm->gpps[j];
814a60eac32SMika Westerberg 
815a60eac32SMika Westerberg 			if (pgrp->gpio_base < 0)
816a60eac32SMika Westerberg 				continue;
817a60eac32SMika Westerberg 
818a60eac32SMika Westerberg 			if (offset >= pgrp->gpio_base &&
819a60eac32SMika Westerberg 			    offset < pgrp->gpio_base + pgrp->size) {
820a60eac32SMika Westerberg 				int pin;
821a60eac32SMika Westerberg 
822a60eac32SMika Westerberg 				pin = pgrp->base + offset - pgrp->gpio_base;
823a60eac32SMika Westerberg 				if (community)
824a60eac32SMika Westerberg 					*community = comm;
825a60eac32SMika Westerberg 				if (padgrp)
826a60eac32SMika Westerberg 					*padgrp = pgrp;
827a60eac32SMika Westerberg 
828a60eac32SMika Westerberg 				return pin;
829a60eac32SMika Westerberg 			}
830a60eac32SMika Westerberg 		}
831a60eac32SMika Westerberg 	}
832a60eac32SMika Westerberg 
833a60eac32SMika Westerberg 	return -EINVAL;
834a60eac32SMika Westerberg }
835a60eac32SMika Westerberg 
8366cb0880fSChris Chiu /**
8376cb0880fSChris Chiu  * intel_pin_to_gpio() - Translate from pin number to GPIO offset
8386cb0880fSChris Chiu  * @pctrl: Pinctrl structure
8396cb0880fSChris Chiu  * @pin: pin number
8406cb0880fSChris Chiu  *
8416cb0880fSChris Chiu  * Translate the pin number of pinctrl to GPIO offset
8426cb0880fSChris Chiu  */
84355dac437SArnd Bergmann static __maybe_unused int intel_pin_to_gpio(struct intel_pinctrl *pctrl, int pin)
8446cb0880fSChris Chiu {
8456cb0880fSChris Chiu 	const struct intel_community *community;
8466cb0880fSChris Chiu 	const struct intel_padgroup *padgrp;
8476cb0880fSChris Chiu 
8486cb0880fSChris Chiu 	community = intel_get_community(pctrl, pin);
8496cb0880fSChris Chiu 	if (!community)
8506cb0880fSChris Chiu 		return -EINVAL;
8516cb0880fSChris Chiu 
8526cb0880fSChris Chiu 	padgrp = intel_community_get_padgroup(community, pin);
8536cb0880fSChris Chiu 	if (!padgrp)
8546cb0880fSChris Chiu 		return -EINVAL;
8556cb0880fSChris Chiu 
8566cb0880fSChris Chiu 	return pin - padgrp->base + padgrp->gpio_base;
8576cb0880fSChris Chiu }
8586cb0880fSChris Chiu 
85904035f7fSAndy Shevchenko static int intel_gpio_get(struct gpio_chip *chip, unsigned int offset)
86055aedef5SAndy Shevchenko {
86196147db1SMika Westerberg 	struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
86296147db1SMika Westerberg 	void __iomem *reg;
86396147db1SMika Westerberg 	u32 padcfg0;
86455aedef5SAndy Shevchenko 	int pin;
86555aedef5SAndy Shevchenko 
86696147db1SMika Westerberg 	pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL);
86796147db1SMika Westerberg 	if (pin < 0)
86896147db1SMika Westerberg 		return -EINVAL;
86996147db1SMika Westerberg 
87096147db1SMika Westerberg 	reg = intel_get_padcfg(pctrl, pin, PADCFG0);
87196147db1SMika Westerberg 	if (!reg)
87296147db1SMika Westerberg 		return -EINVAL;
87396147db1SMika Westerberg 
87496147db1SMika Westerberg 	padcfg0 = readl(reg);
87596147db1SMika Westerberg 	if (!(padcfg0 & PADCFG0_GPIOTXDIS))
87696147db1SMika Westerberg 		return !!(padcfg0 & PADCFG0_GPIOTXSTATE);
87796147db1SMika Westerberg 
87896147db1SMika Westerberg 	return !!(padcfg0 & PADCFG0_GPIORXSTATE);
87955aedef5SAndy Shevchenko }
88055aedef5SAndy Shevchenko 
88104035f7fSAndy Shevchenko static void intel_gpio_set(struct gpio_chip *chip, unsigned int offset,
88204035f7fSAndy Shevchenko 			   int value)
88396147db1SMika Westerberg {
88496147db1SMika Westerberg 	struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
88596147db1SMika Westerberg 	unsigned long flags;
88696147db1SMika Westerberg 	void __iomem *reg;
88796147db1SMika Westerberg 	u32 padcfg0;
88896147db1SMika Westerberg 	int pin;
88996147db1SMika Westerberg 
89096147db1SMika Westerberg 	pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL);
89196147db1SMika Westerberg 	if (pin < 0)
89296147db1SMika Westerberg 		return;
89396147db1SMika Westerberg 
89496147db1SMika Westerberg 	reg = intel_get_padcfg(pctrl, pin, PADCFG0);
89596147db1SMika Westerberg 	if (!reg)
89696147db1SMika Westerberg 		return;
89796147db1SMika Westerberg 
89896147db1SMika Westerberg 	raw_spin_lock_irqsave(&pctrl->lock, flags);
89996147db1SMika Westerberg 	padcfg0 = readl(reg);
90096147db1SMika Westerberg 	if (value)
90196147db1SMika Westerberg 		padcfg0 |= PADCFG0_GPIOTXSTATE;
90296147db1SMika Westerberg 	else
90396147db1SMika Westerberg 		padcfg0 &= ~PADCFG0_GPIOTXSTATE;
90496147db1SMika Westerberg 	writel(padcfg0, reg);
90596147db1SMika Westerberg 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
90696147db1SMika Westerberg }
90796147db1SMika Westerberg 
90896147db1SMika Westerberg static int intel_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
90996147db1SMika Westerberg {
91096147db1SMika Westerberg 	struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
91196147db1SMika Westerberg 	void __iomem *reg;
91296147db1SMika Westerberg 	u32 padcfg0;
91396147db1SMika Westerberg 	int pin;
91496147db1SMika Westerberg 
91596147db1SMika Westerberg 	pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL);
91696147db1SMika Westerberg 	if (pin < 0)
91796147db1SMika Westerberg 		return -EINVAL;
91896147db1SMika Westerberg 
91996147db1SMika Westerberg 	reg = intel_get_padcfg(pctrl, pin, PADCFG0);
92096147db1SMika Westerberg 	if (!reg)
92196147db1SMika Westerberg 		return -EINVAL;
92296147db1SMika Westerberg 
92396147db1SMika Westerberg 	padcfg0 = readl(reg);
92496147db1SMika Westerberg 
92596147db1SMika Westerberg 	if (padcfg0 & PADCFG0_PMODE_MASK)
92696147db1SMika Westerberg 		return -EINVAL;
92796147db1SMika Westerberg 
92896147db1SMika Westerberg 	return !!(padcfg0 & PADCFG0_GPIOTXDIS);
92996147db1SMika Westerberg }
93096147db1SMika Westerberg 
93104035f7fSAndy Shevchenko static int intel_gpio_direction_input(struct gpio_chip *chip, unsigned int offset)
93296147db1SMika Westerberg {
93396147db1SMika Westerberg 	return pinctrl_gpio_direction_input(chip->base + offset);
93496147db1SMika Westerberg }
93596147db1SMika Westerberg 
93604035f7fSAndy Shevchenko static int intel_gpio_direction_output(struct gpio_chip *chip, unsigned int offset,
93796147db1SMika Westerberg 				       int value)
93896147db1SMika Westerberg {
93996147db1SMika Westerberg 	intel_gpio_set(chip, offset, value);
94096147db1SMika Westerberg 	return pinctrl_gpio_direction_output(chip->base + offset);
94196147db1SMika Westerberg }
94296147db1SMika Westerberg 
94396147db1SMika Westerberg static const struct gpio_chip intel_gpio_chip = {
94496147db1SMika Westerberg 	.owner = THIS_MODULE,
94596147db1SMika Westerberg 	.request = gpiochip_generic_request,
94696147db1SMika Westerberg 	.free = gpiochip_generic_free,
94796147db1SMika Westerberg 	.get_direction = intel_gpio_get_direction,
94896147db1SMika Westerberg 	.direction_input = intel_gpio_direction_input,
94996147db1SMika Westerberg 	.direction_output = intel_gpio_direction_output,
95096147db1SMika Westerberg 	.get = intel_gpio_get,
95196147db1SMika Westerberg 	.set = intel_gpio_set,
95296147db1SMika Westerberg 	.set_config = gpiochip_generic_config,
95396147db1SMika Westerberg };
95496147db1SMika Westerberg 
9557981c001SMika Westerberg static void intel_gpio_irq_ack(struct irq_data *d)
9567981c001SMika Westerberg {
9577981c001SMika Westerberg 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
958acfd4c63SLinus Walleij 	struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
9597981c001SMika Westerberg 	const struct intel_community *community;
960919eb475SMika Westerberg 	const struct intel_padgroup *padgrp;
961a60eac32SMika Westerberg 	int pin;
9627981c001SMika Westerberg 
963a60eac32SMika Westerberg 	pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), &community, &padgrp);
964a60eac32SMika Westerberg 	if (pin >= 0) {
96504035f7fSAndy Shevchenko 		unsigned int gpp, gpp_offset, is_offset;
966919eb475SMika Westerberg 
967919eb475SMika Westerberg 		gpp = padgrp->reg_num;
968919eb475SMika Westerberg 		gpp_offset = padgroup_offset(padgrp, pin);
969cf769bd8SMika Westerberg 		is_offset = community->is_offset + gpp * 4;
970919eb475SMika Westerberg 
971919eb475SMika Westerberg 		raw_spin_lock(&pctrl->lock);
972cf769bd8SMika Westerberg 		writel(BIT(gpp_offset), community->regs + is_offset);
97327d9098cSMika Westerberg 		raw_spin_unlock(&pctrl->lock);
9747981c001SMika Westerberg 	}
975919eb475SMika Westerberg }
9767981c001SMika Westerberg 
9777981c001SMika Westerberg static void intel_gpio_irq_mask_unmask(struct irq_data *d, bool mask)
9787981c001SMika Westerberg {
9797981c001SMika Westerberg 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
980acfd4c63SLinus Walleij 	struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
9817981c001SMika Westerberg 	const struct intel_community *community;
982919eb475SMika Westerberg 	const struct intel_padgroup *padgrp;
983a60eac32SMika Westerberg 	int pin;
984a60eac32SMika Westerberg 
985a60eac32SMika Westerberg 	pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), &community, &padgrp);
986a60eac32SMika Westerberg 	if (pin >= 0) {
98704035f7fSAndy Shevchenko 		unsigned int gpp, gpp_offset;
988919eb475SMika Westerberg 		unsigned long flags;
989670784fbSKai-Heng Feng 		void __iomem *reg, *is;
9907981c001SMika Westerberg 		u32 value;
9917981c001SMika Westerberg 
992919eb475SMika Westerberg 		gpp = padgrp->reg_num;
993919eb475SMika Westerberg 		gpp_offset = padgroup_offset(padgrp, pin);
994919eb475SMika Westerberg 
9957981c001SMika Westerberg 		reg = community->regs + community->ie_offset + gpp * 4;
996670784fbSKai-Heng Feng 		is = community->regs + community->is_offset + gpp * 4;
997919eb475SMika Westerberg 
998919eb475SMika Westerberg 		raw_spin_lock_irqsave(&pctrl->lock, flags);
999670784fbSKai-Heng Feng 
1000670784fbSKai-Heng Feng 		/* Clear interrupt status first to avoid unexpected interrupt */
1001670784fbSKai-Heng Feng 		writel(BIT(gpp_offset), is);
1002670784fbSKai-Heng Feng 
10037981c001SMika Westerberg 		value = readl(reg);
10047981c001SMika Westerberg 		if (mask)
10057981c001SMika Westerberg 			value &= ~BIT(gpp_offset);
10067981c001SMika Westerberg 		else
10077981c001SMika Westerberg 			value |= BIT(gpp_offset);
10087981c001SMika Westerberg 		writel(value, reg);
100927d9098cSMika Westerberg 		raw_spin_unlock_irqrestore(&pctrl->lock, flags);
10107981c001SMika Westerberg 	}
1011919eb475SMika Westerberg }
10127981c001SMika Westerberg 
10137981c001SMika Westerberg static void intel_gpio_irq_mask(struct irq_data *d)
10147981c001SMika Westerberg {
10157981c001SMika Westerberg 	intel_gpio_irq_mask_unmask(d, true);
10167981c001SMika Westerberg }
10177981c001SMika Westerberg 
10187981c001SMika Westerberg static void intel_gpio_irq_unmask(struct irq_data *d)
10197981c001SMika Westerberg {
10207981c001SMika Westerberg 	intel_gpio_irq_mask_unmask(d, false);
10217981c001SMika Westerberg }
10227981c001SMika Westerberg 
102304035f7fSAndy Shevchenko static int intel_gpio_irq_type(struct irq_data *d, unsigned int type)
10247981c001SMika Westerberg {
10257981c001SMika Westerberg 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1026acfd4c63SLinus Walleij 	struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
102704035f7fSAndy Shevchenko 	unsigned int pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), NULL, NULL);
10287981c001SMika Westerberg 	unsigned long flags;
10297981c001SMika Westerberg 	void __iomem *reg;
10307981c001SMika Westerberg 	u32 value;
10317981c001SMika Westerberg 
10327981c001SMika Westerberg 	reg = intel_get_padcfg(pctrl, pin, PADCFG0);
10337981c001SMika Westerberg 	if (!reg)
10347981c001SMika Westerberg 		return -EINVAL;
10357981c001SMika Westerberg 
10364341e8a5SMika Westerberg 	/*
10374341e8a5SMika Westerberg 	 * If the pin is in ACPI mode it is still usable as a GPIO but it
10384341e8a5SMika Westerberg 	 * cannot be used as IRQ because GPI_IS status bit will not be
10394341e8a5SMika Westerberg 	 * updated by the host controller hardware.
10404341e8a5SMika Westerberg 	 */
10414341e8a5SMika Westerberg 	if (intel_pad_acpi_mode(pctrl, pin)) {
10424341e8a5SMika Westerberg 		dev_warn(pctrl->dev, "pin %u cannot be used as IRQ\n", pin);
10434341e8a5SMika Westerberg 		return -EPERM;
10444341e8a5SMika Westerberg 	}
10454341e8a5SMika Westerberg 
104627d9098cSMika Westerberg 	raw_spin_lock_irqsave(&pctrl->lock, flags);
10477981c001SMika Westerberg 
1048f5a26acfSMika Westerberg 	intel_gpio_set_gpio_mode(reg);
1049f5a26acfSMika Westerberg 
10507981c001SMika Westerberg 	value = readl(reg);
10517981c001SMika Westerberg 
10527981c001SMika Westerberg 	value &= ~(PADCFG0_RXEVCFG_MASK | PADCFG0_RXINV);
10537981c001SMika Westerberg 
10547981c001SMika Westerberg 	if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
10557981c001SMika Westerberg 		value |= PADCFG0_RXEVCFG_EDGE_BOTH << PADCFG0_RXEVCFG_SHIFT;
10567981c001SMika Westerberg 	} else if (type & IRQ_TYPE_EDGE_FALLING) {
10577981c001SMika Westerberg 		value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT;
10587981c001SMika Westerberg 		value |= PADCFG0_RXINV;
10597981c001SMika Westerberg 	} else if (type & IRQ_TYPE_EDGE_RISING) {
10607981c001SMika Westerberg 		value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT;
1061bf380cfaSQipeng Zha 	} else if (type & IRQ_TYPE_LEVEL_MASK) {
1062bf380cfaSQipeng Zha 		if (type & IRQ_TYPE_LEVEL_LOW)
10637981c001SMika Westerberg 			value |= PADCFG0_RXINV;
10647981c001SMika Westerberg 	} else {
10657981c001SMika Westerberg 		value |= PADCFG0_RXEVCFG_DISABLED << PADCFG0_RXEVCFG_SHIFT;
10667981c001SMika Westerberg 	}
10677981c001SMika Westerberg 
10687981c001SMika Westerberg 	writel(value, reg);
10697981c001SMika Westerberg 
10707981c001SMika Westerberg 	if (type & IRQ_TYPE_EDGE_BOTH)
1071fc756bcdSThomas Gleixner 		irq_set_handler_locked(d, handle_edge_irq);
10727981c001SMika Westerberg 	else if (type & IRQ_TYPE_LEVEL_MASK)
1073fc756bcdSThomas Gleixner 		irq_set_handler_locked(d, handle_level_irq);
10747981c001SMika Westerberg 
107527d9098cSMika Westerberg 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
10767981c001SMika Westerberg 
10777981c001SMika Westerberg 	return 0;
10787981c001SMika Westerberg }
10797981c001SMika Westerberg 
10807981c001SMika Westerberg static int intel_gpio_irq_wake(struct irq_data *d, unsigned int on)
10817981c001SMika Westerberg {
10827981c001SMika Westerberg 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1083acfd4c63SLinus Walleij 	struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
108404035f7fSAndy Shevchenko 	unsigned int pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), NULL, NULL);
10857981c001SMika Westerberg 
10867981c001SMika Westerberg 	if (on)
108701dabe91SNilesh Bacchewar 		enable_irq_wake(pctrl->irq);
10887981c001SMika Westerberg 	else
108901dabe91SNilesh Bacchewar 		disable_irq_wake(pctrl->irq);
10909a520fd9SAndy Shevchenko 
10917981c001SMika Westerberg 	dev_dbg(pctrl->dev, "%sable wake for pin %u\n", on ? "en" : "dis", pin);
10927981c001SMika Westerberg 	return 0;
10937981c001SMika Westerberg }
10947981c001SMika Westerberg 
1095193b40c8SMika Westerberg static irqreturn_t intel_gpio_community_irq_handler(struct intel_pinctrl *pctrl,
10967981c001SMika Westerberg 	const struct intel_community *community)
10977981c001SMika Westerberg {
1098193b40c8SMika Westerberg 	struct gpio_chip *gc = &pctrl->chip;
1099193b40c8SMika Westerberg 	irqreturn_t ret = IRQ_NONE;
11007981c001SMika Westerberg 	int gpp;
11017981c001SMika Westerberg 
11027981c001SMika Westerberg 	for (gpp = 0; gpp < community->ngpps; gpp++) {
1103919eb475SMika Westerberg 		const struct intel_padgroup *padgrp = &community->gpps[gpp];
11047981c001SMika Westerberg 		unsigned long pending, enabled, gpp_offset;
11057981c001SMika Westerberg 
1106cf769bd8SMika Westerberg 		pending = readl(community->regs + community->is_offset +
1107cf769bd8SMika Westerberg 				padgrp->reg_num * 4);
11087981c001SMika Westerberg 		enabled = readl(community->regs + community->ie_offset +
1109919eb475SMika Westerberg 				padgrp->reg_num * 4);
11107981c001SMika Westerberg 
11117981c001SMika Westerberg 		/* Only interrupts that are enabled */
11127981c001SMika Westerberg 		pending &= enabled;
11137981c001SMika Westerberg 
1114919eb475SMika Westerberg 		for_each_set_bit(gpp_offset, &pending, padgrp->size) {
1115a60eac32SMika Westerberg 			unsigned irq;
11167981c001SMika Westerberg 
1117f0fbe7bcSThierry Reding 			irq = irq_find_mapping(gc->irq.domain,
1118a60eac32SMika Westerberg 					       padgrp->gpio_base + gpp_offset);
11197981c001SMika Westerberg 			generic_handle_irq(irq);
1120193b40c8SMika Westerberg 
1121193b40c8SMika Westerberg 			ret |= IRQ_HANDLED;
11227981c001SMika Westerberg 		}
11237981c001SMika Westerberg 	}
11247981c001SMika Westerberg 
1125193b40c8SMika Westerberg 	return ret;
1126193b40c8SMika Westerberg }
1127193b40c8SMika Westerberg 
1128193b40c8SMika Westerberg static irqreturn_t intel_gpio_irq(int irq, void *data)
11297981c001SMika Westerberg {
1130193b40c8SMika Westerberg 	const struct intel_community *community;
1131193b40c8SMika Westerberg 	struct intel_pinctrl *pctrl = data;
1132193b40c8SMika Westerberg 	irqreturn_t ret = IRQ_NONE;
11337981c001SMika Westerberg 	int i;
11347981c001SMika Westerberg 
11357981c001SMika Westerberg 	/* Need to check all communities for pending interrupts */
1136193b40c8SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
1137193b40c8SMika Westerberg 		community = &pctrl->communities[i];
1138193b40c8SMika Westerberg 		ret |= intel_gpio_community_irq_handler(pctrl, community);
1139193b40c8SMika Westerberg 	}
11407981c001SMika Westerberg 
1141193b40c8SMika Westerberg 	return ret;
11427981c001SMika Westerberg }
11437981c001SMika Westerberg 
1144a60eac32SMika Westerberg static int intel_gpio_add_pin_ranges(struct intel_pinctrl *pctrl,
1145a60eac32SMika Westerberg 				     const struct intel_community *community)
1146a60eac32SMika Westerberg {
114733b6cb58SColin Ian King 	int ret = 0, i;
1148a60eac32SMika Westerberg 
1149a60eac32SMika Westerberg 	for (i = 0; i < community->ngpps; i++) {
1150a60eac32SMika Westerberg 		const struct intel_padgroup *gpp = &community->gpps[i];
1151a60eac32SMika Westerberg 
1152a60eac32SMika Westerberg 		if (gpp->gpio_base < 0)
1153a60eac32SMika Westerberg 			continue;
1154a60eac32SMika Westerberg 
1155a60eac32SMika Westerberg 		ret = gpiochip_add_pin_range(&pctrl->chip, dev_name(pctrl->dev),
1156a60eac32SMika Westerberg 					     gpp->gpio_base, gpp->base,
1157a60eac32SMika Westerberg 					     gpp->size);
1158a60eac32SMika Westerberg 		if (ret)
1159a60eac32SMika Westerberg 			return ret;
1160a60eac32SMika Westerberg 	}
1161a60eac32SMika Westerberg 
1162a60eac32SMika Westerberg 	return ret;
1163a60eac32SMika Westerberg }
1164a60eac32SMika Westerberg 
1165a60eac32SMika Westerberg static unsigned intel_gpio_ngpio(const struct intel_pinctrl *pctrl)
1166a60eac32SMika Westerberg {
1167a60eac32SMika Westerberg 	const struct intel_community *community;
116804035f7fSAndy Shevchenko 	unsigned int ngpio = 0;
1169a60eac32SMika Westerberg 	int i, j;
1170a60eac32SMika Westerberg 
1171a60eac32SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
1172a60eac32SMika Westerberg 		community = &pctrl->communities[i];
1173a60eac32SMika Westerberg 		for (j = 0; j < community->ngpps; j++) {
1174a60eac32SMika Westerberg 			const struct intel_padgroup *gpp = &community->gpps[j];
1175a60eac32SMika Westerberg 
1176a60eac32SMika Westerberg 			if (gpp->gpio_base < 0)
1177a60eac32SMika Westerberg 				continue;
1178a60eac32SMika Westerberg 
1179a60eac32SMika Westerberg 			if (gpp->gpio_base + gpp->size > ngpio)
1180a60eac32SMika Westerberg 				ngpio = gpp->gpio_base + gpp->size;
1181a60eac32SMika Westerberg 		}
1182a60eac32SMika Westerberg 	}
1183a60eac32SMika Westerberg 
1184a60eac32SMika Westerberg 	return ngpio;
1185a60eac32SMika Westerberg }
1186a60eac32SMika Westerberg 
11877981c001SMika Westerberg static int intel_gpio_probe(struct intel_pinctrl *pctrl, int irq)
11887981c001SMika Westerberg {
1189a60eac32SMika Westerberg 	int ret, i;
11907981c001SMika Westerberg 
11917981c001SMika Westerberg 	pctrl->chip = intel_gpio_chip;
11927981c001SMika Westerberg 
1193*57ff2df1SAndy Shevchenko 	/* Setup GPIO chip */
1194a60eac32SMika Westerberg 	pctrl->chip.ngpio = intel_gpio_ngpio(pctrl);
11957981c001SMika Westerberg 	pctrl->chip.label = dev_name(pctrl->dev);
119658383c78SLinus Walleij 	pctrl->chip.parent = pctrl->dev;
11977981c001SMika Westerberg 	pctrl->chip.base = -1;
119801dabe91SNilesh Bacchewar 	pctrl->irq = irq;
11997981c001SMika Westerberg 
1200*57ff2df1SAndy Shevchenko 	/* Setup IRQ chip */
1201*57ff2df1SAndy Shevchenko 	pctrl->irqchip.name = dev_name(pctrl->dev);
1202*57ff2df1SAndy Shevchenko 	pctrl->irqchip.irq_ack = intel_gpio_irq_ack;
1203*57ff2df1SAndy Shevchenko 	pctrl->irqchip.irq_mask = intel_gpio_irq_mask;
1204*57ff2df1SAndy Shevchenko 	pctrl->irqchip.irq_unmask = intel_gpio_irq_unmask;
1205*57ff2df1SAndy Shevchenko 	pctrl->irqchip.irq_set_type = intel_gpio_irq_type;
1206*57ff2df1SAndy Shevchenko 	pctrl->irqchip.irq_set_wake = intel_gpio_irq_wake;
1207*57ff2df1SAndy Shevchenko 	pctrl->irqchip.flags = IRQCHIP_MASK_ON_SUSPEND;
1208*57ff2df1SAndy Shevchenko 
1209f25c3aa9SMika Westerberg 	ret = devm_gpiochip_add_data(pctrl->dev, &pctrl->chip, pctrl);
12107981c001SMika Westerberg 	if (ret) {
12117981c001SMika Westerberg 		dev_err(pctrl->dev, "failed to register gpiochip\n");
12127981c001SMika Westerberg 		return ret;
12137981c001SMika Westerberg 	}
12147981c001SMika Westerberg 
1215a60eac32SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
1216a60eac32SMika Westerberg 		struct intel_community *community = &pctrl->communities[i];
1217a60eac32SMika Westerberg 
1218a60eac32SMika Westerberg 		ret = intel_gpio_add_pin_ranges(pctrl, community);
12197981c001SMika Westerberg 		if (ret) {
12207981c001SMika Westerberg 			dev_err(pctrl->dev, "failed to add GPIO pin range\n");
1221f25c3aa9SMika Westerberg 			return ret;
1222193b40c8SMika Westerberg 		}
1223a60eac32SMika Westerberg 	}
1224193b40c8SMika Westerberg 
1225193b40c8SMika Westerberg 	/*
1226193b40c8SMika Westerberg 	 * We need to request the interrupt here (instead of providing chip
1227193b40c8SMika Westerberg 	 * to the irq directly) because on some platforms several GPIO
1228193b40c8SMika Westerberg 	 * controllers share the same interrupt line.
1229193b40c8SMika Westerberg 	 */
12301a7d1cb8SMika Westerberg 	ret = devm_request_irq(pctrl->dev, irq, intel_gpio_irq,
12311a7d1cb8SMika Westerberg 			       IRQF_SHARED | IRQF_NO_THREAD,
1232193b40c8SMika Westerberg 			       dev_name(pctrl->dev), pctrl);
1233193b40c8SMika Westerberg 	if (ret) {
1234193b40c8SMika Westerberg 		dev_err(pctrl->dev, "failed to request interrupt\n");
1235f25c3aa9SMika Westerberg 		return ret;
12367981c001SMika Westerberg 	}
12377981c001SMika Westerberg 
1238*57ff2df1SAndy Shevchenko 	ret = gpiochip_irqchip_add(&pctrl->chip, &pctrl->irqchip, 0,
12393ae02c14SAndy Shevchenko 				   handle_bad_irq, IRQ_TYPE_NONE);
12407981c001SMika Westerberg 	if (ret) {
12417981c001SMika Westerberg 		dev_err(pctrl->dev, "failed to add irqchip\n");
1242f25c3aa9SMika Westerberg 		return ret;
12437981c001SMika Westerberg 	}
12447981c001SMika Westerberg 
1245*57ff2df1SAndy Shevchenko 	gpiochip_set_chained_irqchip(&pctrl->chip, &pctrl->irqchip, irq, NULL);
12467981c001SMika Westerberg 	return 0;
12477981c001SMika Westerberg }
12487981c001SMika Westerberg 
1249919eb475SMika Westerberg static int intel_pinctrl_add_padgroups(struct intel_pinctrl *pctrl,
1250919eb475SMika Westerberg 				       struct intel_community *community)
1251919eb475SMika Westerberg {
1252919eb475SMika Westerberg 	struct intel_padgroup *gpps;
125304035f7fSAndy Shevchenko 	unsigned int npins = community->npins;
125404035f7fSAndy Shevchenko 	unsigned int padown_num = 0;
1255919eb475SMika Westerberg 	size_t ngpps, i;
1256919eb475SMika Westerberg 
1257919eb475SMika Westerberg 	if (community->gpps)
1258919eb475SMika Westerberg 		ngpps = community->ngpps;
1259919eb475SMika Westerberg 	else
1260919eb475SMika Westerberg 		ngpps = DIV_ROUND_UP(community->npins, community->gpp_size);
1261919eb475SMika Westerberg 
1262919eb475SMika Westerberg 	gpps = devm_kcalloc(pctrl->dev, ngpps, sizeof(*gpps), GFP_KERNEL);
1263919eb475SMika Westerberg 	if (!gpps)
1264919eb475SMika Westerberg 		return -ENOMEM;
1265919eb475SMika Westerberg 
1266919eb475SMika Westerberg 	for (i = 0; i < ngpps; i++) {
1267919eb475SMika Westerberg 		if (community->gpps) {
1268919eb475SMika Westerberg 			gpps[i] = community->gpps[i];
1269919eb475SMika Westerberg 		} else {
127004035f7fSAndy Shevchenko 			unsigned int gpp_size = community->gpp_size;
1271919eb475SMika Westerberg 
1272919eb475SMika Westerberg 			gpps[i].reg_num = i;
1273919eb475SMika Westerberg 			gpps[i].base = community->pin_base + i * gpp_size;
1274919eb475SMika Westerberg 			gpps[i].size = min(gpp_size, npins);
1275919eb475SMika Westerberg 			npins -= gpps[i].size;
1276919eb475SMika Westerberg 		}
1277919eb475SMika Westerberg 
1278919eb475SMika Westerberg 		if (gpps[i].size > 32)
1279919eb475SMika Westerberg 			return -EINVAL;
1280919eb475SMika Westerberg 
1281a60eac32SMika Westerberg 		if (!gpps[i].gpio_base)
1282a60eac32SMika Westerberg 			gpps[i].gpio_base = gpps[i].base;
1283a60eac32SMika Westerberg 
1284919eb475SMika Westerberg 		gpps[i].padown_num = padown_num;
1285919eb475SMika Westerberg 
1286919eb475SMika Westerberg 		/*
1287919eb475SMika Westerberg 		 * In older hardware the number of padown registers per
1288919eb475SMika Westerberg 		 * group is fixed regardless of the group size.
1289919eb475SMika Westerberg 		 */
1290919eb475SMika Westerberg 		if (community->gpp_num_padown_regs)
1291919eb475SMika Westerberg 			padown_num += community->gpp_num_padown_regs;
1292919eb475SMika Westerberg 		else
1293919eb475SMika Westerberg 			padown_num += DIV_ROUND_UP(gpps[i].size * 4, 32);
1294919eb475SMika Westerberg 	}
1295919eb475SMika Westerberg 
1296919eb475SMika Westerberg 	community->ngpps = ngpps;
1297919eb475SMika Westerberg 	community->gpps = gpps;
1298919eb475SMika Westerberg 
1299919eb475SMika Westerberg 	return 0;
1300919eb475SMika Westerberg }
1301919eb475SMika Westerberg 
13027981c001SMika Westerberg static int intel_pinctrl_pm_init(struct intel_pinctrl *pctrl)
13037981c001SMika Westerberg {
13047981c001SMika Westerberg #ifdef CONFIG_PM_SLEEP
13057981c001SMika Westerberg 	const struct intel_pinctrl_soc_data *soc = pctrl->soc;
13067981c001SMika Westerberg 	struct intel_community_context *communities;
13077981c001SMika Westerberg 	struct intel_pad_context *pads;
13087981c001SMika Westerberg 	int i;
13097981c001SMika Westerberg 
13107981c001SMika Westerberg 	pads = devm_kcalloc(pctrl->dev, soc->npins, sizeof(*pads), GFP_KERNEL);
13117981c001SMika Westerberg 	if (!pads)
13127981c001SMika Westerberg 		return -ENOMEM;
13137981c001SMika Westerberg 
13147981c001SMika Westerberg 	communities = devm_kcalloc(pctrl->dev, pctrl->ncommunities,
13157981c001SMika Westerberg 				   sizeof(*communities), GFP_KERNEL);
13167981c001SMika Westerberg 	if (!communities)
13177981c001SMika Westerberg 		return -ENOMEM;
13187981c001SMika Westerberg 
13197981c001SMika Westerberg 
13207981c001SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
13217981c001SMika Westerberg 		struct intel_community *community = &pctrl->communities[i];
1322a0a5f766SChris Chiu 		u32 *intmask, *hostown;
13237981c001SMika Westerberg 
13247981c001SMika Westerberg 		intmask = devm_kcalloc(pctrl->dev, community->ngpps,
13257981c001SMika Westerberg 				       sizeof(*intmask), GFP_KERNEL);
13267981c001SMika Westerberg 		if (!intmask)
13277981c001SMika Westerberg 			return -ENOMEM;
13287981c001SMika Westerberg 
13297981c001SMika Westerberg 		communities[i].intmask = intmask;
1330a0a5f766SChris Chiu 
1331a0a5f766SChris Chiu 		hostown = devm_kcalloc(pctrl->dev, community->ngpps,
1332a0a5f766SChris Chiu 				       sizeof(*hostown), GFP_KERNEL);
1333a0a5f766SChris Chiu 		if (!hostown)
1334a0a5f766SChris Chiu 			return -ENOMEM;
1335a0a5f766SChris Chiu 
1336a0a5f766SChris Chiu 		communities[i].hostown = hostown;
13377981c001SMika Westerberg 	}
13387981c001SMika Westerberg 
13397981c001SMika Westerberg 	pctrl->context.pads = pads;
13407981c001SMika Westerberg 	pctrl->context.communities = communities;
13417981c001SMika Westerberg #endif
13427981c001SMika Westerberg 
13437981c001SMika Westerberg 	return 0;
13447981c001SMika Westerberg }
13457981c001SMika Westerberg 
13460dd519e3SAndy Shevchenko static int intel_pinctrl_probe(struct platform_device *pdev,
13477981c001SMika Westerberg 			       const struct intel_pinctrl_soc_data *soc_data)
13487981c001SMika Westerberg {
13497981c001SMika Westerberg 	struct intel_pinctrl *pctrl;
13507981c001SMika Westerberg 	int i, ret, irq;
13517981c001SMika Westerberg 
13527981c001SMika Westerberg 	if (!soc_data)
13537981c001SMika Westerberg 		return -EINVAL;
13547981c001SMika Westerberg 
13557981c001SMika Westerberg 	pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL);
13567981c001SMika Westerberg 	if (!pctrl)
13577981c001SMika Westerberg 		return -ENOMEM;
13587981c001SMika Westerberg 
13597981c001SMika Westerberg 	pctrl->dev = &pdev->dev;
13607981c001SMika Westerberg 	pctrl->soc = soc_data;
136127d9098cSMika Westerberg 	raw_spin_lock_init(&pctrl->lock);
13627981c001SMika Westerberg 
13637981c001SMika Westerberg 	/*
13647981c001SMika Westerberg 	 * Make a copy of the communities which we can use to hold pointers
13657981c001SMika Westerberg 	 * to the registers.
13667981c001SMika Westerberg 	 */
13677981c001SMika Westerberg 	pctrl->ncommunities = pctrl->soc->ncommunities;
13687981c001SMika Westerberg 	pctrl->communities = devm_kcalloc(&pdev->dev, pctrl->ncommunities,
13697981c001SMika Westerberg 				  sizeof(*pctrl->communities), GFP_KERNEL);
13707981c001SMika Westerberg 	if (!pctrl->communities)
13717981c001SMika Westerberg 		return -ENOMEM;
13727981c001SMika Westerberg 
13737981c001SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
13747981c001SMika Westerberg 		struct intel_community *community = &pctrl->communities[i];
13757981c001SMika Westerberg 		void __iomem *regs;
13767981c001SMika Westerberg 		u32 padbar;
13777981c001SMika Westerberg 
13787981c001SMika Westerberg 		*community = pctrl->soc->communities[i];
13797981c001SMika Westerberg 
13809d5b6a95SAndy Shevchenko 		regs = devm_platform_ioremap_resource(pdev, community->barno);
13817981c001SMika Westerberg 		if (IS_ERR(regs))
13827981c001SMika Westerberg 			return PTR_ERR(regs);
13837981c001SMika Westerberg 
1384e57725eaSMika Westerberg 		/*
1385e57725eaSMika Westerberg 		 * Determine community features based on the revision if
1386e57725eaSMika Westerberg 		 * not specified already.
1387e57725eaSMika Westerberg 		 */
1388e57725eaSMika Westerberg 		if (!community->features) {
1389e57725eaSMika Westerberg 			u32 rev;
1390e57725eaSMika Westerberg 
1391e57725eaSMika Westerberg 			rev = (readl(regs + REVID) & REVID_MASK) >> REVID_SHIFT;
139204cc058fSMika Westerberg 			if (rev >= 0x94) {
1393e57725eaSMika Westerberg 				community->features |= PINCTRL_FEATURE_DEBOUNCE;
139404cc058fSMika Westerberg 				community->features |= PINCTRL_FEATURE_1K_PD;
139504cc058fSMika Westerberg 			}
1396e57725eaSMika Westerberg 		}
1397e57725eaSMika Westerberg 
13987981c001SMika Westerberg 		/* Read offset of the pad configuration registers */
13997981c001SMika Westerberg 		padbar = readl(regs + PADBAR);
14007981c001SMika Westerberg 
14017981c001SMika Westerberg 		community->regs = regs;
14027981c001SMika Westerberg 		community->pad_regs = regs + padbar;
1403919eb475SMika Westerberg 
1404919eb475SMika Westerberg 		ret = intel_pinctrl_add_padgroups(pctrl, community);
1405919eb475SMika Westerberg 		if (ret)
1406919eb475SMika Westerberg 			return ret;
14077981c001SMika Westerberg 	}
14087981c001SMika Westerberg 
14097981c001SMika Westerberg 	irq = platform_get_irq(pdev, 0);
14104e73d02fSStephen Boyd 	if (irq < 0)
14117981c001SMika Westerberg 		return irq;
14127981c001SMika Westerberg 
14137981c001SMika Westerberg 	ret = intel_pinctrl_pm_init(pctrl);
14147981c001SMika Westerberg 	if (ret)
14157981c001SMika Westerberg 		return ret;
14167981c001SMika Westerberg 
14177981c001SMika Westerberg 	pctrl->pctldesc = intel_pinctrl_desc;
14187981c001SMika Westerberg 	pctrl->pctldesc.name = dev_name(&pdev->dev);
14197981c001SMika Westerberg 	pctrl->pctldesc.pins = pctrl->soc->pins;
14207981c001SMika Westerberg 	pctrl->pctldesc.npins = pctrl->soc->npins;
14217981c001SMika Westerberg 
142254d46cd7SLaxman Dewangan 	pctrl->pctldev = devm_pinctrl_register(&pdev->dev, &pctrl->pctldesc,
142354d46cd7SLaxman Dewangan 					       pctrl);
1424323de9efSMasahiro Yamada 	if (IS_ERR(pctrl->pctldev)) {
14257981c001SMika Westerberg 		dev_err(&pdev->dev, "failed to register pinctrl driver\n");
1426323de9efSMasahiro Yamada 		return PTR_ERR(pctrl->pctldev);
14277981c001SMika Westerberg 	}
14287981c001SMika Westerberg 
14297981c001SMika Westerberg 	ret = intel_gpio_probe(pctrl, irq);
143054d46cd7SLaxman Dewangan 	if (ret)
14317981c001SMika Westerberg 		return ret;
14327981c001SMika Westerberg 
14337981c001SMika Westerberg 	platform_set_drvdata(pdev, pctrl);
14347981c001SMika Westerberg 
14357981c001SMika Westerberg 	return 0;
14367981c001SMika Westerberg }
14377981c001SMika Westerberg 
143870c263c4SAndy Shevchenko int intel_pinctrl_probe_by_hid(struct platform_device *pdev)
143970c263c4SAndy Shevchenko {
144070c263c4SAndy Shevchenko 	const struct intel_pinctrl_soc_data *data;
144170c263c4SAndy Shevchenko 
144270c263c4SAndy Shevchenko 	data = device_get_match_data(&pdev->dev);
144370c263c4SAndy Shevchenko 	return intel_pinctrl_probe(pdev, data);
144470c263c4SAndy Shevchenko }
144570c263c4SAndy Shevchenko EXPORT_SYMBOL_GPL(intel_pinctrl_probe_by_hid);
144670c263c4SAndy Shevchenko 
1447924cf800SAndy Shevchenko int intel_pinctrl_probe_by_uid(struct platform_device *pdev)
1448924cf800SAndy Shevchenko {
1449924cf800SAndy Shevchenko 	const struct intel_pinctrl_soc_data *data = NULL;
1450924cf800SAndy Shevchenko 	const struct intel_pinctrl_soc_data **table;
1451924cf800SAndy Shevchenko 	struct acpi_device *adev;
1452924cf800SAndy Shevchenko 	unsigned int i;
1453924cf800SAndy Shevchenko 
1454924cf800SAndy Shevchenko 	adev = ACPI_COMPANION(&pdev->dev);
1455924cf800SAndy Shevchenko 	if (adev) {
1456924cf800SAndy Shevchenko 		const void *match = device_get_match_data(&pdev->dev);
1457924cf800SAndy Shevchenko 
1458924cf800SAndy Shevchenko 		table = (const struct intel_pinctrl_soc_data **)match;
1459924cf800SAndy Shevchenko 		for (i = 0; table[i]; i++) {
1460924cf800SAndy Shevchenko 			if (!strcmp(adev->pnp.unique_id, table[i]->uid)) {
1461924cf800SAndy Shevchenko 				data = table[i];
1462924cf800SAndy Shevchenko 				break;
1463924cf800SAndy Shevchenko 			}
1464924cf800SAndy Shevchenko 		}
1465924cf800SAndy Shevchenko 	} else {
1466924cf800SAndy Shevchenko 		const struct platform_device_id *id;
1467924cf800SAndy Shevchenko 
1468924cf800SAndy Shevchenko 		id = platform_get_device_id(pdev);
1469924cf800SAndy Shevchenko 		if (!id)
1470924cf800SAndy Shevchenko 			return -ENODEV;
1471924cf800SAndy Shevchenko 
1472924cf800SAndy Shevchenko 		table = (const struct intel_pinctrl_soc_data **)id->driver_data;
1473924cf800SAndy Shevchenko 		data = table[pdev->id];
1474924cf800SAndy Shevchenko 	}
1475924cf800SAndy Shevchenko 
1476924cf800SAndy Shevchenko 	return intel_pinctrl_probe(pdev, data);
1477924cf800SAndy Shevchenko }
1478924cf800SAndy Shevchenko EXPORT_SYMBOL_GPL(intel_pinctrl_probe_by_uid);
1479924cf800SAndy Shevchenko 
14807981c001SMika Westerberg #ifdef CONFIG_PM_SLEEP
148104035f7fSAndy Shevchenko static bool intel_pinctrl_should_save(struct intel_pinctrl *pctrl, unsigned int pin)
1482c538b943SMika Westerberg {
1483c538b943SMika Westerberg 	const struct pin_desc *pd = pin_desc_get(pctrl->pctldev, pin);
1484c538b943SMika Westerberg 
1485c538b943SMika Westerberg 	if (!pd || !intel_pad_usable(pctrl, pin))
1486c538b943SMika Westerberg 		return false;
1487c538b943SMika Westerberg 
1488c538b943SMika Westerberg 	/*
1489c538b943SMika Westerberg 	 * Only restore the pin if it is actually in use by the kernel (or
1490c538b943SMika Westerberg 	 * by userspace). It is possible that some pins are used by the
1491c538b943SMika Westerberg 	 * BIOS during resume and those are not always locked down so leave
1492c538b943SMika Westerberg 	 * them alone.
1493c538b943SMika Westerberg 	 */
1494c538b943SMika Westerberg 	if (pd->mux_owner || pd->gpio_owner ||
14956cb0880fSChris Chiu 	    gpiochip_line_is_irq(&pctrl->chip, intel_pin_to_gpio(pctrl, pin)))
1496c538b943SMika Westerberg 		return true;
1497c538b943SMika Westerberg 
1498c538b943SMika Westerberg 	return false;
1499c538b943SMika Westerberg }
1500c538b943SMika Westerberg 
15012fef3276SBinbin Wu int intel_pinctrl_suspend_noirq(struct device *dev)
15027981c001SMika Westerberg {
1503cb035d74SWolfram Sang 	struct intel_pinctrl *pctrl = dev_get_drvdata(dev);
15047981c001SMika Westerberg 	struct intel_community_context *communities;
15057981c001SMika Westerberg 	struct intel_pad_context *pads;
15067981c001SMika Westerberg 	int i;
15077981c001SMika Westerberg 
15087981c001SMika Westerberg 	pads = pctrl->context.pads;
15097981c001SMika Westerberg 	for (i = 0; i < pctrl->soc->npins; i++) {
15107981c001SMika Westerberg 		const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
1511e57725eaSMika Westerberg 		void __iomem *padcfg;
15127981c001SMika Westerberg 		u32 val;
15137981c001SMika Westerberg 
1514c538b943SMika Westerberg 		if (!intel_pinctrl_should_save(pctrl, desc->number))
15157981c001SMika Westerberg 			continue;
15167981c001SMika Westerberg 
15177981c001SMika Westerberg 		val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG0));
15187981c001SMika Westerberg 		pads[i].padcfg0 = val & ~PADCFG0_GPIORXSTATE;
15197981c001SMika Westerberg 		val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG1));
15207981c001SMika Westerberg 		pads[i].padcfg1 = val;
1521e57725eaSMika Westerberg 
1522e57725eaSMika Westerberg 		padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG2);
1523e57725eaSMika Westerberg 		if (padcfg)
1524e57725eaSMika Westerberg 			pads[i].padcfg2 = readl(padcfg);
15257981c001SMika Westerberg 	}
15267981c001SMika Westerberg 
15277981c001SMika Westerberg 	communities = pctrl->context.communities;
15287981c001SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
15297981c001SMika Westerberg 		struct intel_community *community = &pctrl->communities[i];
15307981c001SMika Westerberg 		void __iomem *base;
153104035f7fSAndy Shevchenko 		unsigned int gpp;
15327981c001SMika Westerberg 
15337981c001SMika Westerberg 		base = community->regs + community->ie_offset;
15347981c001SMika Westerberg 		for (gpp = 0; gpp < community->ngpps; gpp++)
15357981c001SMika Westerberg 			communities[i].intmask[gpp] = readl(base + gpp * 4);
1536a0a5f766SChris Chiu 
1537a0a5f766SChris Chiu 		base = community->regs + community->hostown_offset;
1538a0a5f766SChris Chiu 		for (gpp = 0; gpp < community->ngpps; gpp++)
1539a0a5f766SChris Chiu 			communities[i].hostown[gpp] = readl(base + gpp * 4);
15407981c001SMika Westerberg 	}
15417981c001SMika Westerberg 
15427981c001SMika Westerberg 	return 0;
15437981c001SMika Westerberg }
15442fef3276SBinbin Wu EXPORT_SYMBOL_GPL(intel_pinctrl_suspend_noirq);
15457981c001SMika Westerberg 
1546f487bbf3SMika Westerberg static void intel_gpio_irq_init(struct intel_pinctrl *pctrl)
1547f487bbf3SMika Westerberg {
1548f487bbf3SMika Westerberg 	size_t i;
1549f487bbf3SMika Westerberg 
1550f487bbf3SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
1551f487bbf3SMika Westerberg 		const struct intel_community *community;
1552f487bbf3SMika Westerberg 		void __iomem *base;
155304035f7fSAndy Shevchenko 		unsigned int gpp;
1554f487bbf3SMika Westerberg 
1555f487bbf3SMika Westerberg 		community = &pctrl->communities[i];
1556f487bbf3SMika Westerberg 		base = community->regs;
1557f487bbf3SMika Westerberg 
1558f487bbf3SMika Westerberg 		for (gpp = 0; gpp < community->ngpps; gpp++) {
1559f487bbf3SMika Westerberg 			/* Mask and clear all interrupts */
1560f487bbf3SMika Westerberg 			writel(0, base + community->ie_offset + gpp * 4);
1561cf769bd8SMika Westerberg 			writel(0xffff, base + community->is_offset + gpp * 4);
1562f487bbf3SMika Westerberg 		}
1563f487bbf3SMika Westerberg 	}
1564f487bbf3SMika Westerberg }
1565f487bbf3SMika Westerberg 
1566a0a5f766SChris Chiu static u32
1567a0a5f766SChris Chiu intel_gpio_is_requested(struct gpio_chip *chip, int base, unsigned int size)
1568a0a5f766SChris Chiu {
1569a0a5f766SChris Chiu 	u32 requested = 0;
1570a0a5f766SChris Chiu 	unsigned int i;
1571a0a5f766SChris Chiu 
1572a0a5f766SChris Chiu 	for (i = 0; i < size; i++)
1573a0a5f766SChris Chiu 		if (gpiochip_is_requested(chip, base + i))
1574a0a5f766SChris Chiu 			requested |= BIT(i);
1575a0a5f766SChris Chiu 
1576a0a5f766SChris Chiu 	return requested;
1577a0a5f766SChris Chiu }
1578a0a5f766SChris Chiu 
1579a0a5f766SChris Chiu static u32
1580a0a5f766SChris Chiu intel_gpio_update_pad_mode(void __iomem *hostown, u32 mask, u32 value)
1581a0a5f766SChris Chiu {
15825f61d951SAndy Shevchenko 	u32 curr, updated;
1583a0a5f766SChris Chiu 
15845f61d951SAndy Shevchenko 	curr = readl(hostown);
15855f61d951SAndy Shevchenko 	updated = (curr & ~mask) | (value & mask);
1586a0a5f766SChris Chiu 	writel(updated, hostown);
15875f61d951SAndy Shevchenko 
1588a0a5f766SChris Chiu 	return curr;
1589a0a5f766SChris Chiu }
1590a0a5f766SChris Chiu 
15912fef3276SBinbin Wu int intel_pinctrl_resume_noirq(struct device *dev)
15927981c001SMika Westerberg {
1593cb035d74SWolfram Sang 	struct intel_pinctrl *pctrl = dev_get_drvdata(dev);
15947981c001SMika Westerberg 	const struct intel_community_context *communities;
15957981c001SMika Westerberg 	const struct intel_pad_context *pads;
15967981c001SMika Westerberg 	int i;
15977981c001SMika Westerberg 
15987981c001SMika Westerberg 	/* Mask all interrupts */
15997981c001SMika Westerberg 	intel_gpio_irq_init(pctrl);
16007981c001SMika Westerberg 
16017981c001SMika Westerberg 	pads = pctrl->context.pads;
16027981c001SMika Westerberg 	for (i = 0; i < pctrl->soc->npins; i++) {
16037981c001SMika Westerberg 		const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
16047981c001SMika Westerberg 		void __iomem *padcfg;
16057981c001SMika Westerberg 		u32 val;
16067981c001SMika Westerberg 
1607c538b943SMika Westerberg 		if (!intel_pinctrl_should_save(pctrl, desc->number))
16087981c001SMika Westerberg 			continue;
16097981c001SMika Westerberg 
16107981c001SMika Westerberg 		padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG0);
16117981c001SMika Westerberg 		val = readl(padcfg) & ~PADCFG0_GPIORXSTATE;
16127981c001SMika Westerberg 		if (val != pads[i].padcfg0) {
16137981c001SMika Westerberg 			writel(pads[i].padcfg0, padcfg);
16147981c001SMika Westerberg 			dev_dbg(dev, "restored pin %u padcfg0 %#08x\n",
16157981c001SMika Westerberg 				desc->number, readl(padcfg));
16167981c001SMika Westerberg 		}
16177981c001SMika Westerberg 
16187981c001SMika Westerberg 		padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG1);
16197981c001SMika Westerberg 		val = readl(padcfg);
16207981c001SMika Westerberg 		if (val != pads[i].padcfg1) {
16217981c001SMika Westerberg 			writel(pads[i].padcfg1, padcfg);
16227981c001SMika Westerberg 			dev_dbg(dev, "restored pin %u padcfg1 %#08x\n",
16237981c001SMika Westerberg 				desc->number, readl(padcfg));
16247981c001SMika Westerberg 		}
1625e57725eaSMika Westerberg 
1626e57725eaSMika Westerberg 		padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG2);
1627e57725eaSMika Westerberg 		if (padcfg) {
1628e57725eaSMika Westerberg 			val = readl(padcfg);
1629e57725eaSMika Westerberg 			if (val != pads[i].padcfg2) {
1630e57725eaSMika Westerberg 				writel(pads[i].padcfg2, padcfg);
1631e57725eaSMika Westerberg 				dev_dbg(dev, "restored pin %u padcfg2 %#08x\n",
1632e57725eaSMika Westerberg 					desc->number, readl(padcfg));
1633e57725eaSMika Westerberg 			}
1634e57725eaSMika Westerberg 		}
16357981c001SMika Westerberg 	}
16367981c001SMika Westerberg 
16377981c001SMika Westerberg 	communities = pctrl->context.communities;
16387981c001SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
16397981c001SMika Westerberg 		struct intel_community *community = &pctrl->communities[i];
16407981c001SMika Westerberg 		void __iomem *base;
164104035f7fSAndy Shevchenko 		unsigned int gpp;
16427981c001SMika Westerberg 
16437981c001SMika Westerberg 		base = community->regs + community->ie_offset;
16447981c001SMika Westerberg 		for (gpp = 0; gpp < community->ngpps; gpp++) {
16457981c001SMika Westerberg 			writel(communities[i].intmask[gpp], base + gpp * 4);
16467981c001SMika Westerberg 			dev_dbg(dev, "restored mask %d/%u %#08x\n", i, gpp,
16477981c001SMika Westerberg 				readl(base + gpp * 4));
16487981c001SMika Westerberg 		}
1649a0a5f766SChris Chiu 
1650a0a5f766SChris Chiu 		base = community->regs + community->hostown_offset;
1651a0a5f766SChris Chiu 		for (gpp = 0; gpp < community->ngpps; gpp++) {
1652a0a5f766SChris Chiu 			const struct intel_padgroup *padgrp = &community->gpps[gpp];
1653a0a5f766SChris Chiu 			u32 requested = 0, value = 0;
1654a0a5f766SChris Chiu 			u32 saved = communities[i].hostown[gpp];
1655a0a5f766SChris Chiu 
1656a0a5f766SChris Chiu 			if (padgrp->gpio_base < 0)
1657a0a5f766SChris Chiu 				continue;
1658a0a5f766SChris Chiu 
1659a0a5f766SChris Chiu 			requested = intel_gpio_is_requested(&pctrl->chip,
1660a0a5f766SChris Chiu 					padgrp->gpio_base, padgrp->size);
1661a0a5f766SChris Chiu 			value = intel_gpio_update_pad_mode(base + gpp * 4,
1662a0a5f766SChris Chiu 					requested, saved);
1663a0a5f766SChris Chiu 			if ((value ^ saved) & requested) {
1664a0a5f766SChris Chiu 				dev_warn(dev, "restore hostown %d/%u %#8x->%#8x\n",
1665a0a5f766SChris Chiu 					i, gpp, value, saved);
1666a0a5f766SChris Chiu 			}
1667a0a5f766SChris Chiu 		}
16687981c001SMika Westerberg 	}
16697981c001SMika Westerberg 
16707981c001SMika Westerberg 	return 0;
16717981c001SMika Westerberg }
16722fef3276SBinbin Wu EXPORT_SYMBOL_GPL(intel_pinctrl_resume_noirq);
16737981c001SMika Westerberg #endif
16747981c001SMika Westerberg 
16757981c001SMika Westerberg MODULE_AUTHOR("Mathias Nyman <mathias.nyman@linux.intel.com>");
16767981c001SMika Westerberg MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
16777981c001SMika Westerberg MODULE_DESCRIPTION("Intel pinctrl/GPIO core driver");
16787981c001SMika Westerberg MODULE_LICENSE("GPL v2");
1679