xref: /openbmc/linux/drivers/pinctrl/intel/pinctrl-intel.c (revision 8fff0427d1b2b47469496dbcf3f846dab0cccc7b)
1875a92b3SAndy Shevchenko // SPDX-License-Identifier: GPL-2.0
27981c001SMika Westerberg /*
37981c001SMika Westerberg  * Intel pinctrl/GPIO core driver.
47981c001SMika Westerberg  *
57981c001SMika Westerberg  * Copyright (C) 2015, Intel Corporation
67981c001SMika Westerberg  * Authors: Mathias Nyman <mathias.nyman@linux.intel.com>
77981c001SMika Westerberg  *          Mika Westerberg <mika.westerberg@linux.intel.com>
87981c001SMika Westerberg  */
97981c001SMika Westerberg 
10924cf800SAndy Shevchenko #include <linux/acpi.h>
117981c001SMika Westerberg #include <linux/gpio/driver.h>
1266c812d2SAndy Shevchenko #include <linux/interrupt.h>
13e57725eaSMika Westerberg #include <linux/log2.h>
146a33a1d6SAndy Shevchenko #include <linux/module.h>
157981c001SMika Westerberg #include <linux/platform_device.h>
16924cf800SAndy Shevchenko #include <linux/property.h>
176a33a1d6SAndy Shevchenko #include <linux/time.h>
18924cf800SAndy Shevchenko 
197981c001SMika Westerberg #include <linux/pinctrl/pinctrl.h>
207981c001SMika Westerberg #include <linux/pinctrl/pinmux.h>
217981c001SMika Westerberg #include <linux/pinctrl/pinconf.h>
227981c001SMika Westerberg #include <linux/pinctrl/pinconf-generic.h>
237981c001SMika Westerberg 
24c538b943SMika Westerberg #include "../core.h"
257981c001SMika Westerberg #include "pinctrl-intel.h"
267981c001SMika Westerberg 
277981c001SMika Westerberg /* Offset from regs */
28e57725eaSMika Westerberg #define REVID				0x000
29e57725eaSMika Westerberg #define REVID_SHIFT			16
30e57725eaSMika Westerberg #define REVID_MASK			GENMASK(31, 16)
31e57725eaSMika Westerberg 
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)
554973ddc8SAndy Shevchenko #define PADCFG0_PMODE_GPIO		0
567981c001SMika Westerberg #define PADCFG0_GPIORXDIS		BIT(9)
577981c001SMika Westerberg #define PADCFG0_GPIOTXDIS		BIT(8)
587981c001SMika Westerberg #define PADCFG0_GPIORXSTATE		BIT(1)
597981c001SMika Westerberg #define PADCFG0_GPIOTXSTATE		BIT(0)
607981c001SMika Westerberg 
617981c001SMika Westerberg #define PADCFG1				0x004
627981c001SMika Westerberg #define PADCFG1_TERM_UP			BIT(13)
637981c001SMika Westerberg #define PADCFG1_TERM_SHIFT		10
64e58926e7SAndy Shevchenko #define PADCFG1_TERM_MASK		GENMASK(12, 10)
657981c001SMika Westerberg #define PADCFG1_TERM_20K		4
667981c001SMika Westerberg #define PADCFG1_TERM_2K			3
677981c001SMika Westerberg #define PADCFG1_TERM_5K			2
687981c001SMika Westerberg #define PADCFG1_TERM_1K			1
697981c001SMika Westerberg 
70e57725eaSMika Westerberg #define PADCFG2				0x008
71e57725eaSMika Westerberg #define PADCFG2_DEBEN			BIT(0)
72e57725eaSMika Westerberg #define PADCFG2_DEBOUNCE_SHIFT		1
73e57725eaSMika Westerberg #define PADCFG2_DEBOUNCE_MASK		GENMASK(4, 1)
74e57725eaSMika Westerberg 
756a33a1d6SAndy Shevchenko #define DEBOUNCE_PERIOD_NSEC		31250
76e57725eaSMika Westerberg 
777981c001SMika Westerberg struct intel_pad_context {
787981c001SMika Westerberg 	u32 padcfg0;
797981c001SMika Westerberg 	u32 padcfg1;
80e57725eaSMika Westerberg 	u32 padcfg2;
817981c001SMika Westerberg };
827981c001SMika Westerberg 
837981c001SMika Westerberg struct intel_community_context {
847981c001SMika Westerberg 	u32 *intmask;
85a0a5f766SChris Chiu 	u32 *hostown;
867981c001SMika Westerberg };
877981c001SMika Westerberg 
887981c001SMika Westerberg #define pin_to_padno(c, p)	((p) - (c)->pin_base)
89919eb475SMika Westerberg #define padgroup_offset(g, p)	((p) - (g)->base)
907981c001SMika Westerberg 
917981c001SMika Westerberg static struct intel_community *intel_get_community(struct intel_pinctrl *pctrl,
9204035f7fSAndy Shevchenko 						   unsigned int pin)
937981c001SMika Westerberg {
947981c001SMika Westerberg 	struct intel_community *community;
957981c001SMika Westerberg 	int i;
967981c001SMika Westerberg 
977981c001SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
987981c001SMika Westerberg 		community = &pctrl->communities[i];
997981c001SMika Westerberg 		if (pin >= community->pin_base &&
1007981c001SMika Westerberg 		    pin < community->pin_base + community->npins)
1017981c001SMika Westerberg 			return community;
1027981c001SMika Westerberg 	}
1037981c001SMika Westerberg 
1047981c001SMika Westerberg 	dev_warn(pctrl->dev, "failed to find community for pin %u\n", pin);
1057981c001SMika Westerberg 	return NULL;
1067981c001SMika Westerberg }
1077981c001SMika Westerberg 
108919eb475SMika Westerberg static const struct intel_padgroup *
109919eb475SMika Westerberg intel_community_get_padgroup(const struct intel_community *community,
11004035f7fSAndy Shevchenko 			     unsigned int pin)
111919eb475SMika Westerberg {
112919eb475SMika Westerberg 	int i;
113919eb475SMika Westerberg 
114919eb475SMika Westerberg 	for (i = 0; i < community->ngpps; i++) {
115919eb475SMika Westerberg 		const struct intel_padgroup *padgrp = &community->gpps[i];
116919eb475SMika Westerberg 
117919eb475SMika Westerberg 		if (pin >= padgrp->base && pin < padgrp->base + padgrp->size)
118919eb475SMika Westerberg 			return padgrp;
119919eb475SMika Westerberg 	}
120919eb475SMika Westerberg 
121919eb475SMika Westerberg 	return NULL;
122919eb475SMika Westerberg }
123919eb475SMika Westerberg 
12404035f7fSAndy Shevchenko static void __iomem *intel_get_padcfg(struct intel_pinctrl *pctrl,
12504035f7fSAndy Shevchenko 				      unsigned int pin, unsigned int reg)
1267981c001SMika Westerberg {
1277981c001SMika Westerberg 	const struct intel_community *community;
12804035f7fSAndy Shevchenko 	unsigned int padno;
129e57725eaSMika Westerberg 	size_t nregs;
1307981c001SMika Westerberg 
1317981c001SMika Westerberg 	community = intel_get_community(pctrl, pin);
1327981c001SMika Westerberg 	if (!community)
1337981c001SMika Westerberg 		return NULL;
1347981c001SMika Westerberg 
1357981c001SMika Westerberg 	padno = pin_to_padno(community, pin);
136e57725eaSMika Westerberg 	nregs = (community->features & PINCTRL_FEATURE_DEBOUNCE) ? 4 : 2;
137e57725eaSMika Westerberg 
1387eb7ecddSAndy Shevchenko 	if (reg >= nregs * 4)
139e57725eaSMika Westerberg 		return NULL;
140e57725eaSMika Westerberg 
141e57725eaSMika Westerberg 	return community->pad_regs + reg + padno * nregs * 4;
1427981c001SMika Westerberg }
1437981c001SMika Westerberg 
14404035f7fSAndy Shevchenko static bool intel_pad_owned_by_host(struct intel_pinctrl *pctrl, unsigned int pin)
1457981c001SMika Westerberg {
1467981c001SMika Westerberg 	const struct intel_community *community;
147919eb475SMika Westerberg 	const struct intel_padgroup *padgrp;
14804035f7fSAndy Shevchenko 	unsigned int gpp, offset, gpp_offset;
1497981c001SMika Westerberg 	void __iomem *padown;
1507981c001SMika Westerberg 
1517981c001SMika Westerberg 	community = intel_get_community(pctrl, pin);
1527981c001SMika Westerberg 	if (!community)
1537981c001SMika Westerberg 		return false;
1547981c001SMika Westerberg 	if (!community->padown_offset)
1557981c001SMika Westerberg 		return true;
1567981c001SMika Westerberg 
157919eb475SMika Westerberg 	padgrp = intel_community_get_padgroup(community, pin);
158919eb475SMika Westerberg 	if (!padgrp)
159919eb475SMika Westerberg 		return false;
160919eb475SMika Westerberg 
161919eb475SMika Westerberg 	gpp_offset = padgroup_offset(padgrp, pin);
162919eb475SMika Westerberg 	gpp = PADOWN_GPP(gpp_offset);
163919eb475SMika Westerberg 	offset = community->padown_offset + padgrp->padown_num * 4 + gpp * 4;
1647981c001SMika Westerberg 	padown = community->regs + offset;
1657981c001SMika Westerberg 
166919eb475SMika Westerberg 	return !(readl(padown) & PADOWN_MASK(gpp_offset));
1677981c001SMika Westerberg }
1687981c001SMika Westerberg 
16904035f7fSAndy Shevchenko static bool intel_pad_acpi_mode(struct intel_pinctrl *pctrl, unsigned int pin)
1707981c001SMika Westerberg {
1717981c001SMika Westerberg 	const struct intel_community *community;
172919eb475SMika Westerberg 	const struct intel_padgroup *padgrp;
17304035f7fSAndy Shevchenko 	unsigned int offset, gpp_offset;
1747981c001SMika Westerberg 	void __iomem *hostown;
1757981c001SMika Westerberg 
1767981c001SMika Westerberg 	community = intel_get_community(pctrl, pin);
1777981c001SMika Westerberg 	if (!community)
1787981c001SMika Westerberg 		return true;
1797981c001SMika Westerberg 	if (!community->hostown_offset)
1807981c001SMika Westerberg 		return false;
1817981c001SMika Westerberg 
182919eb475SMika Westerberg 	padgrp = intel_community_get_padgroup(community, pin);
183919eb475SMika Westerberg 	if (!padgrp)
184919eb475SMika Westerberg 		return true;
185919eb475SMika Westerberg 
186919eb475SMika Westerberg 	gpp_offset = padgroup_offset(padgrp, pin);
187919eb475SMika Westerberg 	offset = community->hostown_offset + padgrp->reg_num * 4;
1887981c001SMika Westerberg 	hostown = community->regs + offset;
1897981c001SMika Westerberg 
190919eb475SMika Westerberg 	return !(readl(hostown) & BIT(gpp_offset));
1917981c001SMika Westerberg }
1927981c001SMika Westerberg 
1931bd23153SAndy Shevchenko /**
1941bd23153SAndy Shevchenko  * enum - Locking variants of the pad configuration
1951bd23153SAndy Shevchenko  *
1961bd23153SAndy Shevchenko  * @PAD_UNLOCKED:	pad is fully controlled by the configuration registers
1971bd23153SAndy Shevchenko  * @PAD_LOCKED:		pad configuration registers, except TX state, are locked
1981bd23153SAndy Shevchenko  * @PAD_LOCKED_TX:	pad configuration TX state is locked
1991bd23153SAndy Shevchenko  * @PAD_LOCKED_FULL:	pad configuration registers are locked completely
2001bd23153SAndy Shevchenko  *
2011bd23153SAndy Shevchenko  * Locking is considered as read-only mode for corresponding registers and
2021bd23153SAndy Shevchenko  * their respective fields. That said, TX state bit is locked separately from
2031bd23153SAndy Shevchenko  * the main locking scheme.
2041bd23153SAndy Shevchenko  */
2051bd23153SAndy Shevchenko enum {
2061bd23153SAndy Shevchenko 	PAD_UNLOCKED	= 0,
2071bd23153SAndy Shevchenko 	PAD_LOCKED	= 1,
2081bd23153SAndy Shevchenko 	PAD_LOCKED_TX	= 2,
2091bd23153SAndy Shevchenko 	PAD_LOCKED_FULL	= PAD_LOCKED | PAD_LOCKED_TX,
2101bd23153SAndy Shevchenko };
2111bd23153SAndy Shevchenko 
2121bd23153SAndy Shevchenko static int intel_pad_locked(struct intel_pinctrl *pctrl, unsigned int pin)
2137981c001SMika Westerberg {
2147981c001SMika Westerberg 	struct intel_community *community;
215919eb475SMika Westerberg 	const struct intel_padgroup *padgrp;
21604035f7fSAndy Shevchenko 	unsigned int offset, gpp_offset;
2177981c001SMika Westerberg 	u32 value;
2181bd23153SAndy Shevchenko 	int ret = PAD_UNLOCKED;
2197981c001SMika Westerberg 
2207981c001SMika Westerberg 	community = intel_get_community(pctrl, pin);
2217981c001SMika Westerberg 	if (!community)
2221bd23153SAndy Shevchenko 		return PAD_LOCKED_FULL;
2237981c001SMika Westerberg 	if (!community->padcfglock_offset)
2241bd23153SAndy Shevchenko 		return PAD_UNLOCKED;
2257981c001SMika Westerberg 
226919eb475SMika Westerberg 	padgrp = intel_community_get_padgroup(community, pin);
227919eb475SMika Westerberg 	if (!padgrp)
2281bd23153SAndy Shevchenko 		return PAD_LOCKED_FULL;
229919eb475SMika Westerberg 
230919eb475SMika Westerberg 	gpp_offset = padgroup_offset(padgrp, pin);
2317981c001SMika Westerberg 
2327981c001SMika Westerberg 	/*
2337981c001SMika Westerberg 	 * If PADCFGLOCK and PADCFGLOCKTX bits are both clear for this pad,
2347981c001SMika Westerberg 	 * the pad is considered unlocked. Any other case means that it is
2351bd23153SAndy Shevchenko 	 * either fully or partially locked.
2367981c001SMika Westerberg 	 */
2371bd23153SAndy Shevchenko 	offset = community->padcfglock_offset + 0 + padgrp->reg_num * 8;
2387981c001SMika Westerberg 	value = readl(community->regs + offset);
239919eb475SMika Westerberg 	if (value & BIT(gpp_offset))
2401bd23153SAndy Shevchenko 		ret |= PAD_LOCKED;
2417981c001SMika Westerberg 
242919eb475SMika Westerberg 	offset = community->padcfglock_offset + 4 + padgrp->reg_num * 8;
2437981c001SMika Westerberg 	value = readl(community->regs + offset);
244919eb475SMika Westerberg 	if (value & BIT(gpp_offset))
2451bd23153SAndy Shevchenko 		ret |= PAD_LOCKED_TX;
2467981c001SMika Westerberg 
2471bd23153SAndy Shevchenko 	return ret;
2481bd23153SAndy Shevchenko }
2491bd23153SAndy Shevchenko 
2501bd23153SAndy Shevchenko static bool intel_pad_is_unlocked(struct intel_pinctrl *pctrl, unsigned int pin)
2511bd23153SAndy Shevchenko {
2521bd23153SAndy Shevchenko 	return (intel_pad_locked(pctrl, pin) & PAD_LOCKED) == PAD_UNLOCKED;
2537981c001SMika Westerberg }
2547981c001SMika Westerberg 
25504035f7fSAndy Shevchenko static bool intel_pad_usable(struct intel_pinctrl *pctrl, unsigned int pin)
2567981c001SMika Westerberg {
2571bd23153SAndy Shevchenko 	return intel_pad_owned_by_host(pctrl, pin) && intel_pad_is_unlocked(pctrl, pin);
2587981c001SMika Westerberg }
2597981c001SMika Westerberg 
2607981c001SMika Westerberg static int intel_get_groups_count(struct pinctrl_dev *pctldev)
2617981c001SMika Westerberg {
2627981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
2637981c001SMika Westerberg 
2647981c001SMika Westerberg 	return pctrl->soc->ngroups;
2657981c001SMika Westerberg }
2667981c001SMika Westerberg 
2677981c001SMika Westerberg static const char *intel_get_group_name(struct pinctrl_dev *pctldev,
26804035f7fSAndy Shevchenko 				      unsigned int group)
2697981c001SMika Westerberg {
2707981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
2717981c001SMika Westerberg 
2727981c001SMika Westerberg 	return pctrl->soc->groups[group].name;
2737981c001SMika Westerberg }
2747981c001SMika Westerberg 
27504035f7fSAndy Shevchenko static int intel_get_group_pins(struct pinctrl_dev *pctldev, unsigned int group,
27604035f7fSAndy Shevchenko 			      const unsigned int **pins, unsigned int *npins)
2777981c001SMika Westerberg {
2787981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
2797981c001SMika Westerberg 
2807981c001SMika Westerberg 	*pins = pctrl->soc->groups[group].pins;
2817981c001SMika Westerberg 	*npins = pctrl->soc->groups[group].npins;
2827981c001SMika Westerberg 	return 0;
2837981c001SMika Westerberg }
2847981c001SMika Westerberg 
2857981c001SMika Westerberg static void intel_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
28604035f7fSAndy Shevchenko 			       unsigned int pin)
2877981c001SMika Westerberg {
2887981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
289e57725eaSMika Westerberg 	void __iomem *padcfg;
2907981c001SMika Westerberg 	u32 cfg0, cfg1, mode;
2911bd23153SAndy Shevchenko 	int locked;
2921bd23153SAndy Shevchenko 	bool acpi;
2937981c001SMika Westerberg 
2947981c001SMika Westerberg 	if (!intel_pad_owned_by_host(pctrl, pin)) {
2957981c001SMika Westerberg 		seq_puts(s, "not available");
2967981c001SMika Westerberg 		return;
2977981c001SMika Westerberg 	}
2987981c001SMika Westerberg 
2997981c001SMika Westerberg 	cfg0 = readl(intel_get_padcfg(pctrl, pin, PADCFG0));
3007981c001SMika Westerberg 	cfg1 = readl(intel_get_padcfg(pctrl, pin, PADCFG1));
3017981c001SMika Westerberg 
3027981c001SMika Westerberg 	mode = (cfg0 & PADCFG0_PMODE_MASK) >> PADCFG0_PMODE_SHIFT;
3034973ddc8SAndy Shevchenko 	if (mode == PADCFG0_PMODE_GPIO)
3047981c001SMika Westerberg 		seq_puts(s, "GPIO ");
3057981c001SMika Westerberg 	else
3067981c001SMika Westerberg 		seq_printf(s, "mode %d ", mode);
3077981c001SMika Westerberg 
3087981c001SMika Westerberg 	seq_printf(s, "0x%08x 0x%08x", cfg0, cfg1);
3097981c001SMika Westerberg 
310e57725eaSMika Westerberg 	/* Dump the additional PADCFG registers if available */
311e57725eaSMika Westerberg 	padcfg = intel_get_padcfg(pctrl, pin, PADCFG2);
312e57725eaSMika Westerberg 	if (padcfg)
313e57725eaSMika Westerberg 		seq_printf(s, " 0x%08x", readl(padcfg));
314e57725eaSMika Westerberg 
3157981c001SMika Westerberg 	locked = intel_pad_locked(pctrl, pin);
3164341e8a5SMika Westerberg 	acpi = intel_pad_acpi_mode(pctrl, pin);
3177981c001SMika Westerberg 
3187981c001SMika Westerberg 	if (locked || acpi) {
3197981c001SMika Westerberg 		seq_puts(s, " [");
3201bd23153SAndy Shevchenko 		if (locked)
3217981c001SMika Westerberg 			seq_puts(s, "LOCKED");
3221bd23153SAndy Shevchenko 		if ((locked & PAD_LOCKED_FULL) == PAD_LOCKED_TX)
3231bd23153SAndy Shevchenko 			seq_puts(s, " tx");
3241bd23153SAndy Shevchenko 		else if ((locked & PAD_LOCKED_FULL) == PAD_LOCKED_FULL)
3251bd23153SAndy Shevchenko 			seq_puts(s, " full");
3261bd23153SAndy Shevchenko 
3271bd23153SAndy Shevchenko 		if (locked && acpi)
3287981c001SMika Westerberg 			seq_puts(s, ", ");
3291bd23153SAndy Shevchenko 
3307981c001SMika Westerberg 		if (acpi)
3317981c001SMika Westerberg 			seq_puts(s, "ACPI");
3327981c001SMika Westerberg 		seq_puts(s, "]");
3337981c001SMika Westerberg 	}
3347981c001SMika Westerberg }
3357981c001SMika Westerberg 
3367981c001SMika Westerberg static const struct pinctrl_ops intel_pinctrl_ops = {
3377981c001SMika Westerberg 	.get_groups_count = intel_get_groups_count,
3387981c001SMika Westerberg 	.get_group_name = intel_get_group_name,
3397981c001SMika Westerberg 	.get_group_pins = intel_get_group_pins,
3407981c001SMika Westerberg 	.pin_dbg_show = intel_pin_dbg_show,
3417981c001SMika Westerberg };
3427981c001SMika Westerberg 
3437981c001SMika Westerberg static int intel_get_functions_count(struct pinctrl_dev *pctldev)
3447981c001SMika Westerberg {
3457981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
3467981c001SMika Westerberg 
3477981c001SMika Westerberg 	return pctrl->soc->nfunctions;
3487981c001SMika Westerberg }
3497981c001SMika Westerberg 
3507981c001SMika Westerberg static const char *intel_get_function_name(struct pinctrl_dev *pctldev,
35104035f7fSAndy Shevchenko 					   unsigned int function)
3527981c001SMika Westerberg {
3537981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
3547981c001SMika Westerberg 
3557981c001SMika Westerberg 	return pctrl->soc->functions[function].name;
3567981c001SMika Westerberg }
3577981c001SMika Westerberg 
3587981c001SMika Westerberg static int intel_get_function_groups(struct pinctrl_dev *pctldev,
35904035f7fSAndy Shevchenko 				     unsigned int function,
3607981c001SMika Westerberg 				     const char * const **groups,
36104035f7fSAndy Shevchenko 				     unsigned int * const ngroups)
3627981c001SMika Westerberg {
3637981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
3647981c001SMika Westerberg 
3657981c001SMika Westerberg 	*groups = pctrl->soc->functions[function].groups;
3667981c001SMika Westerberg 	*ngroups = pctrl->soc->functions[function].ngroups;
3677981c001SMika Westerberg 	return 0;
3687981c001SMika Westerberg }
3697981c001SMika Westerberg 
37004035f7fSAndy Shevchenko static int intel_pinmux_set_mux(struct pinctrl_dev *pctldev,
37104035f7fSAndy Shevchenko 				unsigned int function, unsigned int group)
3727981c001SMika Westerberg {
3737981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
3747981c001SMika Westerberg 	const struct intel_pingroup *grp = &pctrl->soc->groups[group];
3757981c001SMika Westerberg 	unsigned long flags;
3767981c001SMika Westerberg 	int i;
3777981c001SMika Westerberg 
37827d9098cSMika Westerberg 	raw_spin_lock_irqsave(&pctrl->lock, flags);
3797981c001SMika Westerberg 
3807981c001SMika Westerberg 	/*
3817981c001SMika Westerberg 	 * All pins in the groups needs to be accessible and writable
3827981c001SMika Westerberg 	 * before we can enable the mux for this group.
3837981c001SMika Westerberg 	 */
3847981c001SMika Westerberg 	for (i = 0; i < grp->npins; i++) {
3857981c001SMika Westerberg 		if (!intel_pad_usable(pctrl, grp->pins[i])) {
38627d9098cSMika Westerberg 			raw_spin_unlock_irqrestore(&pctrl->lock, flags);
3877981c001SMika Westerberg 			return -EBUSY;
3887981c001SMika Westerberg 		}
3897981c001SMika Westerberg 	}
3907981c001SMika Westerberg 
3917981c001SMika Westerberg 	/* Now enable the mux setting for each pin in the group */
3927981c001SMika Westerberg 	for (i = 0; i < grp->npins; i++) {
3937981c001SMika Westerberg 		void __iomem *padcfg0;
3947981c001SMika Westerberg 		u32 value;
3957981c001SMika Westerberg 
3967981c001SMika Westerberg 		padcfg0 = intel_get_padcfg(pctrl, grp->pins[i], PADCFG0);
3977981c001SMika Westerberg 		value = readl(padcfg0);
3987981c001SMika Westerberg 
3997981c001SMika Westerberg 		value &= ~PADCFG0_PMODE_MASK;
4001f6b419bSMika Westerberg 
4011f6b419bSMika Westerberg 		if (grp->modes)
4021f6b419bSMika Westerberg 			value |= grp->modes[i] << PADCFG0_PMODE_SHIFT;
4031f6b419bSMika Westerberg 		else
4047981c001SMika Westerberg 			value |= grp->mode << PADCFG0_PMODE_SHIFT;
4057981c001SMika Westerberg 
4067981c001SMika Westerberg 		writel(value, padcfg0);
4077981c001SMika Westerberg 	}
4087981c001SMika Westerberg 
40927d9098cSMika Westerberg 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
4107981c001SMika Westerberg 
4117981c001SMika Westerberg 	return 0;
4127981c001SMika Westerberg }
4137981c001SMika Westerberg 
41417fab473SAndy Shevchenko static void __intel_gpio_set_direction(void __iomem *padcfg0, bool input)
41517fab473SAndy Shevchenko {
41617fab473SAndy Shevchenko 	u32 value;
41717fab473SAndy Shevchenko 
41817fab473SAndy Shevchenko 	value = readl(padcfg0);
41917fab473SAndy Shevchenko 	if (input) {
42017fab473SAndy Shevchenko 		value &= ~PADCFG0_GPIORXDIS;
42117fab473SAndy Shevchenko 		value |= PADCFG0_GPIOTXDIS;
42217fab473SAndy Shevchenko 	} else {
42317fab473SAndy Shevchenko 		value &= ~PADCFG0_GPIOTXDIS;
42417fab473SAndy Shevchenko 		value |= PADCFG0_GPIORXDIS;
42517fab473SAndy Shevchenko 	}
42617fab473SAndy Shevchenko 	writel(value, padcfg0);
42717fab473SAndy Shevchenko }
42817fab473SAndy Shevchenko 
4294973ddc8SAndy Shevchenko static int intel_gpio_get_gpio_mode(void __iomem *padcfg0)
4304973ddc8SAndy Shevchenko {
4314973ddc8SAndy Shevchenko 	return (readl(padcfg0) & PADCFG0_PMODE_MASK) >> PADCFG0_PMODE_SHIFT;
4324973ddc8SAndy Shevchenko }
4334973ddc8SAndy Shevchenko 
434f5a26acfSMika Westerberg static void intel_gpio_set_gpio_mode(void __iomem *padcfg0)
435f5a26acfSMika Westerberg {
436f5a26acfSMika Westerberg 	u32 value;
437f5a26acfSMika Westerberg 
438af7e3eebSAndy Shevchenko 	value = readl(padcfg0);
439af7e3eebSAndy Shevchenko 
440f5a26acfSMika Westerberg 	/* Put the pad into GPIO mode */
441af7e3eebSAndy Shevchenko 	value &= ~PADCFG0_PMODE_MASK;
442af7e3eebSAndy Shevchenko 	value |= PADCFG0_PMODE_GPIO;
443af7e3eebSAndy Shevchenko 
444af7e3eebSAndy Shevchenko 	/* Disable input and output buffers */
445af7e3eebSAndy Shevchenko 	value &= ~PADCFG0_GPIORXDIS;
446af7e3eebSAndy Shevchenko 	value &= ~PADCFG0_GPIOTXDIS;
447af7e3eebSAndy Shevchenko 
448f5a26acfSMika Westerberg 	/* Disable SCI/SMI/NMI generation */
449f5a26acfSMika Westerberg 	value &= ~(PADCFG0_GPIROUTIOXAPIC | PADCFG0_GPIROUTSCI);
450f5a26acfSMika Westerberg 	value &= ~(PADCFG0_GPIROUTSMI | PADCFG0_GPIROUTNMI);
451af7e3eebSAndy Shevchenko 
452f5a26acfSMika Westerberg 	writel(value, padcfg0);
453f5a26acfSMika Westerberg }
454f5a26acfSMika Westerberg 
4557981c001SMika Westerberg static int intel_gpio_request_enable(struct pinctrl_dev *pctldev,
4567981c001SMika Westerberg 				     struct pinctrl_gpio_range *range,
45704035f7fSAndy Shevchenko 				     unsigned int pin)
4587981c001SMika Westerberg {
4597981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
4607981c001SMika Westerberg 	void __iomem *padcfg0;
4617981c001SMika Westerberg 	unsigned long flags;
4627981c001SMika Westerberg 
463f62cdde5SAndy Shevchenko 	padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
464f62cdde5SAndy Shevchenko 
46527d9098cSMika Westerberg 	raw_spin_lock_irqsave(&pctrl->lock, flags);
4667981c001SMika Westerberg 
4671bd23153SAndy Shevchenko 	if (!intel_pad_owned_by_host(pctrl, pin)) {
46827d9098cSMika Westerberg 		raw_spin_unlock_irqrestore(&pctrl->lock, flags);
4697981c001SMika Westerberg 		return -EBUSY;
4707981c001SMika Westerberg 	}
4717981c001SMika Westerberg 
4721bd23153SAndy Shevchenko 	if (!intel_pad_is_unlocked(pctrl, pin)) {
4731bd23153SAndy Shevchenko 		raw_spin_unlock_irqrestore(&pctrl->lock, flags);
4741bd23153SAndy Shevchenko 		return 0;
4751bd23153SAndy Shevchenko 	}
4761bd23153SAndy Shevchenko 
4774973ddc8SAndy Shevchenko 	/*
4784973ddc8SAndy Shevchenko 	 * If pin is already configured in GPIO mode, we assume that
4794973ddc8SAndy Shevchenko 	 * firmware provides correct settings. In such case we avoid
4804973ddc8SAndy Shevchenko 	 * potential glitches on the pin. Otherwise, for the pin in
4814973ddc8SAndy Shevchenko 	 * alternative mode, consumer has to supply respective flags.
4824973ddc8SAndy Shevchenko 	 */
4834973ddc8SAndy Shevchenko 	if (intel_gpio_get_gpio_mode(padcfg0) == PADCFG0_PMODE_GPIO) {
4844973ddc8SAndy Shevchenko 		raw_spin_unlock_irqrestore(&pctrl->lock, flags);
4854973ddc8SAndy Shevchenko 		return 0;
4864973ddc8SAndy Shevchenko 	}
4874973ddc8SAndy Shevchenko 
488f5a26acfSMika Westerberg 	intel_gpio_set_gpio_mode(padcfg0);
4894973ddc8SAndy Shevchenko 
49017fab473SAndy Shevchenko 	/* Disable TX buffer and enable RX (this will be input) */
49117fab473SAndy Shevchenko 	__intel_gpio_set_direction(padcfg0, true);
49217fab473SAndy Shevchenko 
49327d9098cSMika Westerberg 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
4947981c001SMika Westerberg 
4957981c001SMika Westerberg 	return 0;
4967981c001SMika Westerberg }
4977981c001SMika Westerberg 
4987981c001SMika Westerberg static int intel_gpio_set_direction(struct pinctrl_dev *pctldev,
4997981c001SMika Westerberg 				    struct pinctrl_gpio_range *range,
50004035f7fSAndy Shevchenko 				    unsigned int pin, bool input)
5017981c001SMika Westerberg {
5027981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
5037981c001SMika Westerberg 	void __iomem *padcfg0;
5047981c001SMika Westerberg 	unsigned long flags;
5057981c001SMika Westerberg 
5067981c001SMika Westerberg 	padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
5077981c001SMika Westerberg 
508f62cdde5SAndy Shevchenko 	raw_spin_lock_irqsave(&pctrl->lock, flags);
509f62cdde5SAndy Shevchenko 	__intel_gpio_set_direction(padcfg0, input);
51027d9098cSMika Westerberg 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
5117981c001SMika Westerberg 
5127981c001SMika Westerberg 	return 0;
5137981c001SMika Westerberg }
5147981c001SMika Westerberg 
5157981c001SMika Westerberg static const struct pinmux_ops intel_pinmux_ops = {
5167981c001SMika Westerberg 	.get_functions_count = intel_get_functions_count,
5177981c001SMika Westerberg 	.get_function_name = intel_get_function_name,
5187981c001SMika Westerberg 	.get_function_groups = intel_get_function_groups,
5197981c001SMika Westerberg 	.set_mux = intel_pinmux_set_mux,
5207981c001SMika Westerberg 	.gpio_request_enable = intel_gpio_request_enable,
5217981c001SMika Westerberg 	.gpio_set_direction = intel_gpio_set_direction,
5227981c001SMika Westerberg };
5237981c001SMika Westerberg 
52404035f7fSAndy Shevchenko static int intel_config_get(struct pinctrl_dev *pctldev, unsigned int pin,
5257981c001SMika Westerberg 			    unsigned long *config)
5267981c001SMika Westerberg {
5277981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
5287981c001SMika Westerberg 	enum pin_config_param param = pinconf_to_config_param(*config);
52904cc058fSMika Westerberg 	const struct intel_community *community;
5307981c001SMika Westerberg 	u32 value, term;
531e57725eaSMika Westerberg 	u32 arg = 0;
5327981c001SMika Westerberg 
5337981c001SMika Westerberg 	if (!intel_pad_owned_by_host(pctrl, pin))
5347981c001SMika Westerberg 		return -ENOTSUPP;
5357981c001SMika Westerberg 
53604cc058fSMika Westerberg 	community = intel_get_community(pctrl, pin);
5377981c001SMika Westerberg 	value = readl(intel_get_padcfg(pctrl, pin, PADCFG1));
5387981c001SMika Westerberg 	term = (value & PADCFG1_TERM_MASK) >> PADCFG1_TERM_SHIFT;
5397981c001SMika Westerberg 
5407981c001SMika Westerberg 	switch (param) {
5417981c001SMika Westerberg 	case PIN_CONFIG_BIAS_DISABLE:
5427981c001SMika Westerberg 		if (term)
5437981c001SMika Westerberg 			return -EINVAL;
5447981c001SMika Westerberg 		break;
5457981c001SMika Westerberg 
5467981c001SMika Westerberg 	case PIN_CONFIG_BIAS_PULL_UP:
5477981c001SMika Westerberg 		if (!term || !(value & PADCFG1_TERM_UP))
5487981c001SMika Westerberg 			return -EINVAL;
5497981c001SMika Westerberg 
5507981c001SMika Westerberg 		switch (term) {
5517981c001SMika Westerberg 		case PADCFG1_TERM_1K:
5527981c001SMika Westerberg 			arg = 1000;
5537981c001SMika Westerberg 			break;
5547981c001SMika Westerberg 		case PADCFG1_TERM_2K:
5557981c001SMika Westerberg 			arg = 2000;
5567981c001SMika Westerberg 			break;
5577981c001SMika Westerberg 		case PADCFG1_TERM_5K:
5587981c001SMika Westerberg 			arg = 5000;
5597981c001SMika Westerberg 			break;
5607981c001SMika Westerberg 		case PADCFG1_TERM_20K:
5617981c001SMika Westerberg 			arg = 20000;
5627981c001SMika Westerberg 			break;
5637981c001SMika Westerberg 		}
5647981c001SMika Westerberg 
5657981c001SMika Westerberg 		break;
5667981c001SMika Westerberg 
5677981c001SMika Westerberg 	case PIN_CONFIG_BIAS_PULL_DOWN:
5687981c001SMika Westerberg 		if (!term || value & PADCFG1_TERM_UP)
5697981c001SMika Westerberg 			return -EINVAL;
5707981c001SMika Westerberg 
5717981c001SMika Westerberg 		switch (term) {
57204cc058fSMika Westerberg 		case PADCFG1_TERM_1K:
57304cc058fSMika Westerberg 			if (!(community->features & PINCTRL_FEATURE_1K_PD))
57404cc058fSMika Westerberg 				return -EINVAL;
57504cc058fSMika Westerberg 			arg = 1000;
57604cc058fSMika Westerberg 			break;
5777981c001SMika Westerberg 		case PADCFG1_TERM_5K:
5787981c001SMika Westerberg 			arg = 5000;
5797981c001SMika Westerberg 			break;
5807981c001SMika Westerberg 		case PADCFG1_TERM_20K:
5817981c001SMika Westerberg 			arg = 20000;
5827981c001SMika Westerberg 			break;
5837981c001SMika Westerberg 		}
5847981c001SMika Westerberg 
5857981c001SMika Westerberg 		break;
5867981c001SMika Westerberg 
587e57725eaSMika Westerberg 	case PIN_CONFIG_INPUT_DEBOUNCE: {
588e57725eaSMika Westerberg 		void __iomem *padcfg2;
589e57725eaSMika Westerberg 		u32 v;
590e57725eaSMika Westerberg 
591e57725eaSMika Westerberg 		padcfg2 = intel_get_padcfg(pctrl, pin, PADCFG2);
592e57725eaSMika Westerberg 		if (!padcfg2)
593e57725eaSMika Westerberg 			return -ENOTSUPP;
594e57725eaSMika Westerberg 
595e57725eaSMika Westerberg 		v = readl(padcfg2);
596e57725eaSMika Westerberg 		if (!(v & PADCFG2_DEBEN))
597e57725eaSMika Westerberg 			return -EINVAL;
598e57725eaSMika Westerberg 
599e57725eaSMika Westerberg 		v = (v & PADCFG2_DEBOUNCE_MASK) >> PADCFG2_DEBOUNCE_SHIFT;
6006a33a1d6SAndy Shevchenko 		arg = BIT(v) * DEBOUNCE_PERIOD_NSEC / NSEC_PER_USEC;
601e57725eaSMika Westerberg 
602e57725eaSMika Westerberg 		break;
603e57725eaSMika Westerberg 	}
604e57725eaSMika Westerberg 
6057981c001SMika Westerberg 	default:
6067981c001SMika Westerberg 		return -ENOTSUPP;
6077981c001SMika Westerberg 	}
6087981c001SMika Westerberg 
6097981c001SMika Westerberg 	*config = pinconf_to_config_packed(param, arg);
6107981c001SMika Westerberg 	return 0;
6117981c001SMika Westerberg }
6127981c001SMika Westerberg 
61304035f7fSAndy Shevchenko static int intel_config_set_pull(struct intel_pinctrl *pctrl, unsigned int pin,
6147981c001SMika Westerberg 				 unsigned long config)
6157981c001SMika Westerberg {
61604035f7fSAndy Shevchenko 	unsigned int param = pinconf_to_config_param(config);
61704035f7fSAndy Shevchenko 	unsigned int arg = pinconf_to_config_argument(config);
61804cc058fSMika Westerberg 	const struct intel_community *community;
6197981c001SMika Westerberg 	void __iomem *padcfg1;
6207981c001SMika Westerberg 	unsigned long flags;
6217981c001SMika Westerberg 	int ret = 0;
6227981c001SMika Westerberg 	u32 value;
6237981c001SMika Westerberg 
62404cc058fSMika Westerberg 	community = intel_get_community(pctrl, pin);
6257981c001SMika Westerberg 	padcfg1 = intel_get_padcfg(pctrl, pin, PADCFG1);
626f62cdde5SAndy Shevchenko 
627f62cdde5SAndy Shevchenko 	raw_spin_lock_irqsave(&pctrl->lock, flags);
628f62cdde5SAndy Shevchenko 
6297981c001SMika Westerberg 	value = readl(padcfg1);
6307981c001SMika Westerberg 
6317981c001SMika Westerberg 	switch (param) {
6327981c001SMika Westerberg 	case PIN_CONFIG_BIAS_DISABLE:
6337981c001SMika Westerberg 		value &= ~(PADCFG1_TERM_MASK | PADCFG1_TERM_UP);
6347981c001SMika Westerberg 		break;
6357981c001SMika Westerberg 
6367981c001SMika Westerberg 	case PIN_CONFIG_BIAS_PULL_UP:
6377981c001SMika Westerberg 		value &= ~PADCFG1_TERM_MASK;
6387981c001SMika Westerberg 
6397981c001SMika Westerberg 		value |= PADCFG1_TERM_UP;
6407981c001SMika Westerberg 
6417981c001SMika Westerberg 		switch (arg) {
6427981c001SMika Westerberg 		case 20000:
6437981c001SMika Westerberg 			value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT;
6447981c001SMika Westerberg 			break;
6457981c001SMika Westerberg 		case 5000:
6467981c001SMika Westerberg 			value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT;
6477981c001SMika Westerberg 			break;
6487981c001SMika Westerberg 		case 2000:
6497981c001SMika Westerberg 			value |= PADCFG1_TERM_2K << PADCFG1_TERM_SHIFT;
6507981c001SMika Westerberg 			break;
6517981c001SMika Westerberg 		case 1000:
6527981c001SMika Westerberg 			value |= PADCFG1_TERM_1K << PADCFG1_TERM_SHIFT;
6537981c001SMika Westerberg 			break;
6547981c001SMika Westerberg 		default:
6557981c001SMika Westerberg 			ret = -EINVAL;
6567981c001SMika Westerberg 		}
6577981c001SMika Westerberg 
6587981c001SMika Westerberg 		break;
6597981c001SMika Westerberg 
6607981c001SMika Westerberg 	case PIN_CONFIG_BIAS_PULL_DOWN:
6617981c001SMika Westerberg 		value &= ~(PADCFG1_TERM_UP | PADCFG1_TERM_MASK);
6627981c001SMika Westerberg 
6637981c001SMika Westerberg 		switch (arg) {
6647981c001SMika Westerberg 		case 20000:
6657981c001SMika Westerberg 			value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT;
6667981c001SMika Westerberg 			break;
6677981c001SMika Westerberg 		case 5000:
6687981c001SMika Westerberg 			value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT;
6697981c001SMika Westerberg 			break;
67004cc058fSMika Westerberg 		case 1000:
671aa1dd80fSDan Carpenter 			if (!(community->features & PINCTRL_FEATURE_1K_PD)) {
672aa1dd80fSDan Carpenter 				ret = -EINVAL;
673aa1dd80fSDan Carpenter 				break;
674aa1dd80fSDan Carpenter 			}
67504cc058fSMika Westerberg 			value |= PADCFG1_TERM_1K << PADCFG1_TERM_SHIFT;
67604cc058fSMika Westerberg 			break;
6777981c001SMika Westerberg 		default:
6787981c001SMika Westerberg 			ret = -EINVAL;
6797981c001SMika Westerberg 		}
6807981c001SMika Westerberg 
6817981c001SMika Westerberg 		break;
6827981c001SMika Westerberg 	}
6837981c001SMika Westerberg 
6847981c001SMika Westerberg 	if (!ret)
6857981c001SMika Westerberg 		writel(value, padcfg1);
6867981c001SMika Westerberg 
68727d9098cSMika Westerberg 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
6887981c001SMika Westerberg 
6897981c001SMika Westerberg 	return ret;
6907981c001SMika Westerberg }
6917981c001SMika Westerberg 
69204035f7fSAndy Shevchenko static int intel_config_set_debounce(struct intel_pinctrl *pctrl,
69304035f7fSAndy Shevchenko 				     unsigned int pin, unsigned int debounce)
694e57725eaSMika Westerberg {
695e57725eaSMika Westerberg 	void __iomem *padcfg0, *padcfg2;
696e57725eaSMika Westerberg 	unsigned long flags;
697e57725eaSMika Westerberg 	u32 value0, value2;
698e57725eaSMika Westerberg 
699e57725eaSMika Westerberg 	padcfg2 = intel_get_padcfg(pctrl, pin, PADCFG2);
700e57725eaSMika Westerberg 	if (!padcfg2)
701e57725eaSMika Westerberg 		return -ENOTSUPP;
702e57725eaSMika Westerberg 
703e57725eaSMika Westerberg 	padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
704e57725eaSMika Westerberg 
705e57725eaSMika Westerberg 	raw_spin_lock_irqsave(&pctrl->lock, flags);
706e57725eaSMika Westerberg 
707e57725eaSMika Westerberg 	value0 = readl(padcfg0);
708e57725eaSMika Westerberg 	value2 = readl(padcfg2);
709e57725eaSMika Westerberg 
710e57725eaSMika Westerberg 	/* Disable glitch filter and debouncer */
711e57725eaSMika Westerberg 	value0 &= ~PADCFG0_PREGFRXSEL;
712e57725eaSMika Westerberg 	value2 &= ~(PADCFG2_DEBEN | PADCFG2_DEBOUNCE_MASK);
713e57725eaSMika Westerberg 
714e57725eaSMika Westerberg 	if (debounce) {
715e57725eaSMika Westerberg 		unsigned long v;
716e57725eaSMika Westerberg 
7176a33a1d6SAndy Shevchenko 		v = order_base_2(debounce * NSEC_PER_USEC / DEBOUNCE_PERIOD_NSEC);
718e57725eaSMika Westerberg 		if (v < 3 || v > 15) {
719*8fff0427SAndy Shevchenko 			raw_spin_unlock_irqrestore(&pctrl->lock, flags);
720*8fff0427SAndy Shevchenko 			return -EINVAL;
721bb2f43d4SAndy Shevchenko 		}
722bb2f43d4SAndy Shevchenko 
723e57725eaSMika Westerberg 		/* Enable glitch filter and debouncer */
724e57725eaSMika Westerberg 		value0 |= PADCFG0_PREGFRXSEL;
725e57725eaSMika Westerberg 		value2 |= v << PADCFG2_DEBOUNCE_SHIFT;
726e57725eaSMika Westerberg 		value2 |= PADCFG2_DEBEN;
727e57725eaSMika Westerberg 	}
728e57725eaSMika Westerberg 
729e57725eaSMika Westerberg 	writel(value0, padcfg0);
730e57725eaSMika Westerberg 	writel(value2, padcfg2);
731e57725eaSMika Westerberg 
732e57725eaSMika Westerberg 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
733e57725eaSMika Westerberg 
734*8fff0427SAndy Shevchenko 	return 0;
735e57725eaSMika Westerberg }
736e57725eaSMika Westerberg 
73704035f7fSAndy Shevchenko static int intel_config_set(struct pinctrl_dev *pctldev, unsigned int pin,
73804035f7fSAndy Shevchenko 			  unsigned long *configs, unsigned int nconfigs)
7397981c001SMika Westerberg {
7407981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
7417981c001SMika Westerberg 	int i, ret;
7427981c001SMika Westerberg 
7437981c001SMika Westerberg 	if (!intel_pad_usable(pctrl, pin))
7447981c001SMika Westerberg 		return -ENOTSUPP;
7457981c001SMika Westerberg 
7467981c001SMika Westerberg 	for (i = 0; i < nconfigs; i++) {
7477981c001SMika Westerberg 		switch (pinconf_to_config_param(configs[i])) {
7487981c001SMika Westerberg 		case PIN_CONFIG_BIAS_DISABLE:
7497981c001SMika Westerberg 		case PIN_CONFIG_BIAS_PULL_UP:
7507981c001SMika Westerberg 		case PIN_CONFIG_BIAS_PULL_DOWN:
7517981c001SMika Westerberg 			ret = intel_config_set_pull(pctrl, pin, configs[i]);
7527981c001SMika Westerberg 			if (ret)
7537981c001SMika Westerberg 				return ret;
7547981c001SMika Westerberg 			break;
7557981c001SMika Westerberg 
756e57725eaSMika Westerberg 		case PIN_CONFIG_INPUT_DEBOUNCE:
757e57725eaSMika Westerberg 			ret = intel_config_set_debounce(pctrl, pin,
758e57725eaSMika Westerberg 				pinconf_to_config_argument(configs[i]));
759e57725eaSMika Westerberg 			if (ret)
760e57725eaSMika Westerberg 				return ret;
761e57725eaSMika Westerberg 			break;
762e57725eaSMika Westerberg 
7637981c001SMika Westerberg 		default:
7647981c001SMika Westerberg 			return -ENOTSUPP;
7657981c001SMika Westerberg 		}
7667981c001SMika Westerberg 	}
7677981c001SMika Westerberg 
7687981c001SMika Westerberg 	return 0;
7697981c001SMika Westerberg }
7707981c001SMika Westerberg 
7717981c001SMika Westerberg static const struct pinconf_ops intel_pinconf_ops = {
7727981c001SMika Westerberg 	.is_generic = true,
7737981c001SMika Westerberg 	.pin_config_get = intel_config_get,
7747981c001SMika Westerberg 	.pin_config_set = intel_config_set,
7757981c001SMika Westerberg };
7767981c001SMika Westerberg 
7777981c001SMika Westerberg static const struct pinctrl_desc intel_pinctrl_desc = {
7787981c001SMika Westerberg 	.pctlops = &intel_pinctrl_ops,
7797981c001SMika Westerberg 	.pmxops = &intel_pinmux_ops,
7807981c001SMika Westerberg 	.confops = &intel_pinconf_ops,
7817981c001SMika Westerberg 	.owner = THIS_MODULE,
7827981c001SMika Westerberg };
7837981c001SMika Westerberg 
784a60eac32SMika Westerberg /**
785a60eac32SMika Westerberg  * intel_gpio_to_pin() - Translate from GPIO offset to pin number
786a60eac32SMika Westerberg  * @pctrl: Pinctrl structure
787a60eac32SMika Westerberg  * @offset: GPIO offset from gpiolib
788946ffefcSAndy Shevchenko  * @community: Community is filled here if not %NULL
789a60eac32SMika Westerberg  * @padgrp: Pad group is filled here if not %NULL
790a60eac32SMika Westerberg  *
791a60eac32SMika Westerberg  * When coming through gpiolib irqchip, the GPIO offset is not
792a60eac32SMika Westerberg  * automatically translated to pinctrl pin number. This function can be
793a60eac32SMika Westerberg  * used to find out the corresponding pinctrl pin.
794a60eac32SMika Westerberg  */
79504035f7fSAndy Shevchenko static int intel_gpio_to_pin(struct intel_pinctrl *pctrl, unsigned int offset,
796a60eac32SMika Westerberg 			     const struct intel_community **community,
797a60eac32SMika Westerberg 			     const struct intel_padgroup **padgrp)
798a60eac32SMika Westerberg {
799a60eac32SMika Westerberg 	int i;
800a60eac32SMika Westerberg 
801a60eac32SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
802a60eac32SMika Westerberg 		const struct intel_community *comm = &pctrl->communities[i];
803a60eac32SMika Westerberg 		int j;
804a60eac32SMika Westerberg 
805a60eac32SMika Westerberg 		for (j = 0; j < comm->ngpps; j++) {
806a60eac32SMika Westerberg 			const struct intel_padgroup *pgrp = &comm->gpps[j];
807a60eac32SMika Westerberg 
808e5a4ab6aSAndy Shevchenko 			if (pgrp->gpio_base == INTEL_GPIO_BASE_NOMAP)
809a60eac32SMika Westerberg 				continue;
810a60eac32SMika Westerberg 
811a60eac32SMika Westerberg 			if (offset >= pgrp->gpio_base &&
812a60eac32SMika Westerberg 			    offset < pgrp->gpio_base + pgrp->size) {
813a60eac32SMika Westerberg 				int pin;
814a60eac32SMika Westerberg 
815a60eac32SMika Westerberg 				pin = pgrp->base + offset - pgrp->gpio_base;
816a60eac32SMika Westerberg 				if (community)
817a60eac32SMika Westerberg 					*community = comm;
818a60eac32SMika Westerberg 				if (padgrp)
819a60eac32SMika Westerberg 					*padgrp = pgrp;
820a60eac32SMika Westerberg 
821a60eac32SMika Westerberg 				return pin;
822a60eac32SMika Westerberg 			}
823a60eac32SMika Westerberg 		}
824a60eac32SMika Westerberg 	}
825a60eac32SMika Westerberg 
826a60eac32SMika Westerberg 	return -EINVAL;
827a60eac32SMika Westerberg }
828a60eac32SMika Westerberg 
8296cb0880fSChris Chiu /**
8306cb0880fSChris Chiu  * intel_pin_to_gpio() - Translate from pin number to GPIO offset
8316cb0880fSChris Chiu  * @pctrl: Pinctrl structure
8326cb0880fSChris Chiu  * @pin: pin number
8336cb0880fSChris Chiu  *
8346cb0880fSChris Chiu  * Translate the pin number of pinctrl to GPIO offset
8356cb0880fSChris Chiu  */
83655dac437SArnd Bergmann static __maybe_unused int intel_pin_to_gpio(struct intel_pinctrl *pctrl, int pin)
8376cb0880fSChris Chiu {
8386cb0880fSChris Chiu 	const struct intel_community *community;
8396cb0880fSChris Chiu 	const struct intel_padgroup *padgrp;
8406cb0880fSChris Chiu 
8416cb0880fSChris Chiu 	community = intel_get_community(pctrl, pin);
8426cb0880fSChris Chiu 	if (!community)
8436cb0880fSChris Chiu 		return -EINVAL;
8446cb0880fSChris Chiu 
8456cb0880fSChris Chiu 	padgrp = intel_community_get_padgroup(community, pin);
8466cb0880fSChris Chiu 	if (!padgrp)
8476cb0880fSChris Chiu 		return -EINVAL;
8486cb0880fSChris Chiu 
8496cb0880fSChris Chiu 	return pin - padgrp->base + padgrp->gpio_base;
8506cb0880fSChris Chiu }
8516cb0880fSChris Chiu 
85204035f7fSAndy Shevchenko static int intel_gpio_get(struct gpio_chip *chip, unsigned int offset)
85355aedef5SAndy Shevchenko {
85496147db1SMika Westerberg 	struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
85596147db1SMika Westerberg 	void __iomem *reg;
85696147db1SMika Westerberg 	u32 padcfg0;
85755aedef5SAndy Shevchenko 	int pin;
85855aedef5SAndy Shevchenko 
85996147db1SMika Westerberg 	pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL);
86096147db1SMika Westerberg 	if (pin < 0)
86196147db1SMika Westerberg 		return -EINVAL;
86296147db1SMika Westerberg 
86396147db1SMika Westerberg 	reg = intel_get_padcfg(pctrl, pin, PADCFG0);
86496147db1SMika Westerberg 	if (!reg)
86596147db1SMika Westerberg 		return -EINVAL;
86696147db1SMika Westerberg 
86796147db1SMika Westerberg 	padcfg0 = readl(reg);
86896147db1SMika Westerberg 	if (!(padcfg0 & PADCFG0_GPIOTXDIS))
86996147db1SMika Westerberg 		return !!(padcfg0 & PADCFG0_GPIOTXSTATE);
87096147db1SMika Westerberg 
87196147db1SMika Westerberg 	return !!(padcfg0 & PADCFG0_GPIORXSTATE);
87255aedef5SAndy Shevchenko }
87355aedef5SAndy Shevchenko 
87404035f7fSAndy Shevchenko static void intel_gpio_set(struct gpio_chip *chip, unsigned int offset,
87504035f7fSAndy Shevchenko 			   int value)
87696147db1SMika Westerberg {
87796147db1SMika Westerberg 	struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
87896147db1SMika Westerberg 	unsigned long flags;
87996147db1SMika Westerberg 	void __iomem *reg;
88096147db1SMika Westerberg 	u32 padcfg0;
88196147db1SMika Westerberg 	int pin;
88296147db1SMika Westerberg 
88396147db1SMika Westerberg 	pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL);
88496147db1SMika Westerberg 	if (pin < 0)
88596147db1SMika Westerberg 		return;
88696147db1SMika Westerberg 
88796147db1SMika Westerberg 	reg = intel_get_padcfg(pctrl, pin, PADCFG0);
88896147db1SMika Westerberg 	if (!reg)
88996147db1SMika Westerberg 		return;
89096147db1SMika Westerberg 
89196147db1SMika Westerberg 	raw_spin_lock_irqsave(&pctrl->lock, flags);
89296147db1SMika Westerberg 	padcfg0 = readl(reg);
89396147db1SMika Westerberg 	if (value)
89496147db1SMika Westerberg 		padcfg0 |= PADCFG0_GPIOTXSTATE;
89596147db1SMika Westerberg 	else
89696147db1SMika Westerberg 		padcfg0 &= ~PADCFG0_GPIOTXSTATE;
89796147db1SMika Westerberg 	writel(padcfg0, reg);
89896147db1SMika Westerberg 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
89996147db1SMika Westerberg }
90096147db1SMika Westerberg 
90196147db1SMika Westerberg static int intel_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
90296147db1SMika Westerberg {
90396147db1SMika Westerberg 	struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
90496147db1SMika Westerberg 	void __iomem *reg;
90596147db1SMika Westerberg 	u32 padcfg0;
90696147db1SMika Westerberg 	int pin;
90796147db1SMika Westerberg 
90896147db1SMika Westerberg 	pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL);
90996147db1SMika Westerberg 	if (pin < 0)
91096147db1SMika Westerberg 		return -EINVAL;
91196147db1SMika Westerberg 
91296147db1SMika Westerberg 	reg = intel_get_padcfg(pctrl, pin, PADCFG0);
91396147db1SMika Westerberg 	if (!reg)
91496147db1SMika Westerberg 		return -EINVAL;
91596147db1SMika Westerberg 
91696147db1SMika Westerberg 	padcfg0 = readl(reg);
91796147db1SMika Westerberg 
91896147db1SMika Westerberg 	if (padcfg0 & PADCFG0_PMODE_MASK)
91996147db1SMika Westerberg 		return -EINVAL;
92096147db1SMika Westerberg 
9216a304752SMatti Vaittinen 	if (padcfg0 & PADCFG0_GPIOTXDIS)
9226a304752SMatti Vaittinen 		return GPIO_LINE_DIRECTION_IN;
9236a304752SMatti Vaittinen 
9246a304752SMatti Vaittinen 	return GPIO_LINE_DIRECTION_OUT;
92596147db1SMika Westerberg }
92696147db1SMika Westerberg 
92704035f7fSAndy Shevchenko static int intel_gpio_direction_input(struct gpio_chip *chip, unsigned int offset)
92896147db1SMika Westerberg {
92996147db1SMika Westerberg 	return pinctrl_gpio_direction_input(chip->base + offset);
93096147db1SMika Westerberg }
93196147db1SMika Westerberg 
93204035f7fSAndy Shevchenko static int intel_gpio_direction_output(struct gpio_chip *chip, unsigned int offset,
93396147db1SMika Westerberg 				       int value)
93496147db1SMika Westerberg {
93596147db1SMika Westerberg 	intel_gpio_set(chip, offset, value);
93696147db1SMika Westerberg 	return pinctrl_gpio_direction_output(chip->base + offset);
93796147db1SMika Westerberg }
93896147db1SMika Westerberg 
93996147db1SMika Westerberg static const struct gpio_chip intel_gpio_chip = {
94096147db1SMika Westerberg 	.owner = THIS_MODULE,
94196147db1SMika Westerberg 	.request = gpiochip_generic_request,
94296147db1SMika Westerberg 	.free = gpiochip_generic_free,
94396147db1SMika Westerberg 	.get_direction = intel_gpio_get_direction,
94496147db1SMika Westerberg 	.direction_input = intel_gpio_direction_input,
94596147db1SMika Westerberg 	.direction_output = intel_gpio_direction_output,
94696147db1SMika Westerberg 	.get = intel_gpio_get,
94796147db1SMika Westerberg 	.set = intel_gpio_set,
94896147db1SMika Westerberg 	.set_config = gpiochip_generic_config,
94996147db1SMika Westerberg };
95096147db1SMika Westerberg 
9517981c001SMika Westerberg static void intel_gpio_irq_ack(struct irq_data *d)
9527981c001SMika Westerberg {
9537981c001SMika Westerberg 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
954acfd4c63SLinus Walleij 	struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
9557981c001SMika Westerberg 	const struct intel_community *community;
956919eb475SMika Westerberg 	const struct intel_padgroup *padgrp;
957a60eac32SMika Westerberg 	int pin;
9587981c001SMika Westerberg 
959a60eac32SMika Westerberg 	pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), &community, &padgrp);
960a60eac32SMika Westerberg 	if (pin >= 0) {
96104035f7fSAndy Shevchenko 		unsigned int gpp, gpp_offset, is_offset;
962919eb475SMika Westerberg 
963919eb475SMika Westerberg 		gpp = padgrp->reg_num;
964919eb475SMika Westerberg 		gpp_offset = padgroup_offset(padgrp, pin);
965cf769bd8SMika Westerberg 		is_offset = community->is_offset + gpp * 4;
966919eb475SMika Westerberg 
967919eb475SMika Westerberg 		raw_spin_lock(&pctrl->lock);
968cf769bd8SMika Westerberg 		writel(BIT(gpp_offset), community->regs + is_offset);
96927d9098cSMika Westerberg 		raw_spin_unlock(&pctrl->lock);
9707981c001SMika Westerberg 	}
971919eb475SMika Westerberg }
9727981c001SMika Westerberg 
9737981c001SMika Westerberg static void intel_gpio_irq_mask_unmask(struct irq_data *d, bool mask)
9747981c001SMika Westerberg {
9757981c001SMika Westerberg 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
976acfd4c63SLinus Walleij 	struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
9777981c001SMika Westerberg 	const struct intel_community *community;
978919eb475SMika Westerberg 	const struct intel_padgroup *padgrp;
979a60eac32SMika Westerberg 	int pin;
980a60eac32SMika Westerberg 
981a60eac32SMika Westerberg 	pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), &community, &padgrp);
982a60eac32SMika Westerberg 	if (pin >= 0) {
98304035f7fSAndy Shevchenko 		unsigned int gpp, gpp_offset;
984919eb475SMika Westerberg 		unsigned long flags;
985670784fbSKai-Heng Feng 		void __iomem *reg, *is;
9867981c001SMika Westerberg 		u32 value;
9877981c001SMika Westerberg 
988919eb475SMika Westerberg 		gpp = padgrp->reg_num;
989919eb475SMika Westerberg 		gpp_offset = padgroup_offset(padgrp, pin);
990919eb475SMika Westerberg 
9917981c001SMika Westerberg 		reg = community->regs + community->ie_offset + gpp * 4;
992670784fbSKai-Heng Feng 		is = community->regs + community->is_offset + gpp * 4;
993919eb475SMika Westerberg 
994919eb475SMika Westerberg 		raw_spin_lock_irqsave(&pctrl->lock, flags);
995670784fbSKai-Heng Feng 
996670784fbSKai-Heng Feng 		/* Clear interrupt status first to avoid unexpected interrupt */
997670784fbSKai-Heng Feng 		writel(BIT(gpp_offset), is);
998670784fbSKai-Heng Feng 
9997981c001SMika Westerberg 		value = readl(reg);
10007981c001SMika Westerberg 		if (mask)
10017981c001SMika Westerberg 			value &= ~BIT(gpp_offset);
10027981c001SMika Westerberg 		else
10037981c001SMika Westerberg 			value |= BIT(gpp_offset);
10047981c001SMika Westerberg 		writel(value, reg);
100527d9098cSMika Westerberg 		raw_spin_unlock_irqrestore(&pctrl->lock, flags);
10067981c001SMika Westerberg 	}
1007919eb475SMika Westerberg }
10087981c001SMika Westerberg 
10097981c001SMika Westerberg static void intel_gpio_irq_mask(struct irq_data *d)
10107981c001SMika Westerberg {
10117981c001SMika Westerberg 	intel_gpio_irq_mask_unmask(d, true);
10127981c001SMika Westerberg }
10137981c001SMika Westerberg 
10147981c001SMika Westerberg static void intel_gpio_irq_unmask(struct irq_data *d)
10157981c001SMika Westerberg {
10167981c001SMika Westerberg 	intel_gpio_irq_mask_unmask(d, false);
10177981c001SMika Westerberg }
10187981c001SMika Westerberg 
101904035f7fSAndy Shevchenko static int intel_gpio_irq_type(struct irq_data *d, unsigned int type)
10207981c001SMika Westerberg {
10217981c001SMika Westerberg 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1022acfd4c63SLinus Walleij 	struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
102304035f7fSAndy Shevchenko 	unsigned int pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), NULL, NULL);
10247981c001SMika Westerberg 	unsigned long flags;
10257981c001SMika Westerberg 	void __iomem *reg;
10267981c001SMika Westerberg 	u32 value;
10277981c001SMika Westerberg 
10287981c001SMika Westerberg 	reg = intel_get_padcfg(pctrl, pin, PADCFG0);
10297981c001SMika Westerberg 	if (!reg)
10307981c001SMika Westerberg 		return -EINVAL;
10317981c001SMika Westerberg 
10324341e8a5SMika Westerberg 	/*
10334341e8a5SMika Westerberg 	 * If the pin is in ACPI mode it is still usable as a GPIO but it
10344341e8a5SMika Westerberg 	 * cannot be used as IRQ because GPI_IS status bit will not be
10354341e8a5SMika Westerberg 	 * updated by the host controller hardware.
10364341e8a5SMika Westerberg 	 */
10374341e8a5SMika Westerberg 	if (intel_pad_acpi_mode(pctrl, pin)) {
10384341e8a5SMika Westerberg 		dev_warn(pctrl->dev, "pin %u cannot be used as IRQ\n", pin);
10394341e8a5SMika Westerberg 		return -EPERM;
10404341e8a5SMika Westerberg 	}
10414341e8a5SMika Westerberg 
104227d9098cSMika Westerberg 	raw_spin_lock_irqsave(&pctrl->lock, flags);
10437981c001SMika Westerberg 
1044f5a26acfSMika Westerberg 	intel_gpio_set_gpio_mode(reg);
1045f5a26acfSMika Westerberg 
1046af7e3eebSAndy Shevchenko 	/* Disable TX buffer and enable RX (this will be input) */
1047af7e3eebSAndy Shevchenko 	__intel_gpio_set_direction(reg, true);
1048af7e3eebSAndy Shevchenko 
10497981c001SMika Westerberg 	value = readl(reg);
10507981c001SMika Westerberg 
10517981c001SMika Westerberg 	value &= ~(PADCFG0_RXEVCFG_MASK | PADCFG0_RXINV);
10527981c001SMika Westerberg 
10537981c001SMika Westerberg 	if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
10547981c001SMika Westerberg 		value |= PADCFG0_RXEVCFG_EDGE_BOTH << PADCFG0_RXEVCFG_SHIFT;
10557981c001SMika Westerberg 	} else if (type & IRQ_TYPE_EDGE_FALLING) {
10567981c001SMika Westerberg 		value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT;
10577981c001SMika Westerberg 		value |= PADCFG0_RXINV;
10587981c001SMika Westerberg 	} else if (type & IRQ_TYPE_EDGE_RISING) {
10597981c001SMika Westerberg 		value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT;
1060bf380cfaSQipeng Zha 	} else if (type & IRQ_TYPE_LEVEL_MASK) {
1061bf380cfaSQipeng Zha 		if (type & IRQ_TYPE_LEVEL_LOW)
10627981c001SMika Westerberg 			value |= PADCFG0_RXINV;
10637981c001SMika Westerberg 	} else {
10647981c001SMika Westerberg 		value |= PADCFG0_RXEVCFG_DISABLED << PADCFG0_RXEVCFG_SHIFT;
10657981c001SMika Westerberg 	}
10667981c001SMika Westerberg 
10677981c001SMika Westerberg 	writel(value, reg);
10687981c001SMika Westerberg 
10697981c001SMika Westerberg 	if (type & IRQ_TYPE_EDGE_BOTH)
1070fc756bcdSThomas Gleixner 		irq_set_handler_locked(d, handle_edge_irq);
10717981c001SMika Westerberg 	else if (type & IRQ_TYPE_LEVEL_MASK)
1072fc756bcdSThomas Gleixner 		irq_set_handler_locked(d, handle_level_irq);
10737981c001SMika Westerberg 
107427d9098cSMika Westerberg 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
10757981c001SMika Westerberg 
10767981c001SMika Westerberg 	return 0;
10777981c001SMika Westerberg }
10787981c001SMika Westerberg 
10797981c001SMika Westerberg static int intel_gpio_irq_wake(struct irq_data *d, unsigned int on)
10807981c001SMika Westerberg {
10817981c001SMika Westerberg 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1082acfd4c63SLinus Walleij 	struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
108304035f7fSAndy Shevchenko 	unsigned int pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), NULL, NULL);
10847981c001SMika Westerberg 
10857981c001SMika Westerberg 	if (on)
108601dabe91SNilesh Bacchewar 		enable_irq_wake(pctrl->irq);
10877981c001SMika Westerberg 	else
108801dabe91SNilesh Bacchewar 		disable_irq_wake(pctrl->irq);
10899a520fd9SAndy Shevchenko 
10907981c001SMika Westerberg 	dev_dbg(pctrl->dev, "%sable wake for pin %u\n", on ? "en" : "dis", pin);
10917981c001SMika Westerberg 	return 0;
10927981c001SMika Westerberg }
10937981c001SMika Westerberg 
109486851bbcSAndy Shevchenko static int intel_gpio_community_irq_handler(struct intel_pinctrl *pctrl,
10957981c001SMika Westerberg 					    const struct intel_community *community)
10967981c001SMika Westerberg {
1097193b40c8SMika Westerberg 	struct gpio_chip *gc = &pctrl->chip;
109886851bbcSAndy Shevchenko 	unsigned int gpp;
109986851bbcSAndy Shevchenko 	int ret = 0;
11007981c001SMika Westerberg 
11017981c001SMika Westerberg 	for (gpp = 0; gpp < community->ngpps; gpp++) {
1102919eb475SMika Westerberg 		const struct intel_padgroup *padgrp = &community->gpps[gpp];
11037981c001SMika Westerberg 		unsigned long pending, enabled, gpp_offset;
11047981c001SMika Westerberg 
1105cf769bd8SMika Westerberg 		pending = readl(community->regs + community->is_offset +
1106cf769bd8SMika Westerberg 				padgrp->reg_num * 4);
11077981c001SMika Westerberg 		enabled = readl(community->regs + community->ie_offset +
1108919eb475SMika Westerberg 				padgrp->reg_num * 4);
11097981c001SMika Westerberg 
11107981c001SMika Westerberg 		/* Only interrupts that are enabled */
11117981c001SMika Westerberg 		pending &= enabled;
11127981c001SMika Westerberg 
1113919eb475SMika Westerberg 		for_each_set_bit(gpp_offset, &pending, padgrp->size) {
111411b389ccSAndy Shevchenko 			unsigned int irq;
11157981c001SMika Westerberg 
1116f0fbe7bcSThierry Reding 			irq = irq_find_mapping(gc->irq.domain,
1117a60eac32SMika Westerberg 					       padgrp->gpio_base + gpp_offset);
11187981c001SMika Westerberg 			generic_handle_irq(irq);
11197981c001SMika Westerberg 		}
112086851bbcSAndy Shevchenko 
112186851bbcSAndy Shevchenko 		ret += pending ? 1 : 0;
11227981c001SMika Westerberg 	}
11237981c001SMika Westerberg 
1124193b40c8SMika Westerberg 	return ret;
1125193b40c8SMika Westerberg }
1126193b40c8SMika Westerberg 
1127193b40c8SMika Westerberg static irqreturn_t intel_gpio_irq(int irq, void *data)
11287981c001SMika Westerberg {
1129193b40c8SMika Westerberg 	const struct intel_community *community;
1130193b40c8SMika Westerberg 	struct intel_pinctrl *pctrl = data;
113186851bbcSAndy Shevchenko 	unsigned int i;
113286851bbcSAndy Shevchenko 	int ret = 0;
11337981c001SMika Westerberg 
11347981c001SMika Westerberg 	/* Need to check all communities for pending interrupts */
1135193b40c8SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
1136193b40c8SMika Westerberg 		community = &pctrl->communities[i];
113786851bbcSAndy Shevchenko 		ret += intel_gpio_community_irq_handler(pctrl, community);
1138193b40c8SMika Westerberg 	}
11397981c001SMika Westerberg 
114086851bbcSAndy Shevchenko 	return IRQ_RETVAL(ret);
11417981c001SMika Westerberg }
11427981c001SMika Westerberg 
11436d416b9bSLinus Walleij static int intel_gpio_add_community_ranges(struct intel_pinctrl *pctrl,
1144a60eac32SMika Westerberg 				const struct intel_community *community)
1145a60eac32SMika Westerberg {
114633b6cb58SColin Ian King 	int ret = 0, i;
1147a60eac32SMika Westerberg 
1148a60eac32SMika Westerberg 	for (i = 0; i < community->ngpps; i++) {
1149a60eac32SMika Westerberg 		const struct intel_padgroup *gpp = &community->gpps[i];
1150a60eac32SMika Westerberg 
1151e5a4ab6aSAndy Shevchenko 		if (gpp->gpio_base == INTEL_GPIO_BASE_NOMAP)
1152a60eac32SMika Westerberg 			continue;
1153a60eac32SMika Westerberg 
1154a60eac32SMika Westerberg 		ret = gpiochip_add_pin_range(&pctrl->chip, dev_name(pctrl->dev),
1155a60eac32SMika Westerberg 					     gpp->gpio_base, gpp->base,
1156a60eac32SMika Westerberg 					     gpp->size);
1157a60eac32SMika Westerberg 		if (ret)
1158a60eac32SMika Westerberg 			return ret;
1159a60eac32SMika Westerberg 	}
1160a60eac32SMika Westerberg 
1161a60eac32SMika Westerberg 	return ret;
1162a60eac32SMika Westerberg }
1163a60eac32SMika Westerberg 
11646d416b9bSLinus Walleij static int intel_gpio_add_pin_ranges(struct gpio_chip *gc)
11656d416b9bSLinus Walleij {
11666d416b9bSLinus Walleij 	struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
11676d416b9bSLinus Walleij 	int ret, i;
11686d416b9bSLinus Walleij 
11696d416b9bSLinus Walleij 	for (i = 0; i < pctrl->ncommunities; i++) {
11706d416b9bSLinus Walleij 		struct intel_community *community = &pctrl->communities[i];
11716d416b9bSLinus Walleij 
11726d416b9bSLinus Walleij 		ret = intel_gpio_add_community_ranges(pctrl, community);
11736d416b9bSLinus Walleij 		if (ret) {
11746d416b9bSLinus Walleij 			dev_err(pctrl->dev, "failed to add GPIO pin range\n");
11756d416b9bSLinus Walleij 			return ret;
11766d416b9bSLinus Walleij 		}
11776d416b9bSLinus Walleij 	}
11786d416b9bSLinus Walleij 
11796d416b9bSLinus Walleij 	return 0;
11806d416b9bSLinus Walleij }
11816d416b9bSLinus Walleij 
118211b389ccSAndy Shevchenko static unsigned int intel_gpio_ngpio(const struct intel_pinctrl *pctrl)
1183a60eac32SMika Westerberg {
1184a60eac32SMika Westerberg 	const struct intel_community *community;
118504035f7fSAndy Shevchenko 	unsigned int ngpio = 0;
1186a60eac32SMika Westerberg 	int i, j;
1187a60eac32SMika Westerberg 
1188a60eac32SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
1189a60eac32SMika Westerberg 		community = &pctrl->communities[i];
1190a60eac32SMika Westerberg 		for (j = 0; j < community->ngpps; j++) {
1191a60eac32SMika Westerberg 			const struct intel_padgroup *gpp = &community->gpps[j];
1192a60eac32SMika Westerberg 
1193e5a4ab6aSAndy Shevchenko 			if (gpp->gpio_base == INTEL_GPIO_BASE_NOMAP)
1194a60eac32SMika Westerberg 				continue;
1195a60eac32SMika Westerberg 
1196a60eac32SMika Westerberg 			if (gpp->gpio_base + gpp->size > ngpio)
1197a60eac32SMika Westerberg 				ngpio = gpp->gpio_base + gpp->size;
1198a60eac32SMika Westerberg 		}
1199a60eac32SMika Westerberg 	}
1200a60eac32SMika Westerberg 
1201a60eac32SMika Westerberg 	return ngpio;
1202a60eac32SMika Westerberg }
1203a60eac32SMika Westerberg 
12047981c001SMika Westerberg static int intel_gpio_probe(struct intel_pinctrl *pctrl, int irq)
12057981c001SMika Westerberg {
12066d416b9bSLinus Walleij 	int ret;
1207af0c5330SLinus Walleij 	struct gpio_irq_chip *girq;
12087981c001SMika Westerberg 
12097981c001SMika Westerberg 	pctrl->chip = intel_gpio_chip;
12107981c001SMika Westerberg 
121157ff2df1SAndy Shevchenko 	/* Setup GPIO chip */
1212a60eac32SMika Westerberg 	pctrl->chip.ngpio = intel_gpio_ngpio(pctrl);
12137981c001SMika Westerberg 	pctrl->chip.label = dev_name(pctrl->dev);
121458383c78SLinus Walleij 	pctrl->chip.parent = pctrl->dev;
12157981c001SMika Westerberg 	pctrl->chip.base = -1;
12166d416b9bSLinus Walleij 	pctrl->chip.add_pin_ranges = intel_gpio_add_pin_ranges;
121701dabe91SNilesh Bacchewar 	pctrl->irq = irq;
12187981c001SMika Westerberg 
121957ff2df1SAndy Shevchenko 	/* Setup IRQ chip */
122057ff2df1SAndy Shevchenko 	pctrl->irqchip.name = dev_name(pctrl->dev);
122157ff2df1SAndy Shevchenko 	pctrl->irqchip.irq_ack = intel_gpio_irq_ack;
122257ff2df1SAndy Shevchenko 	pctrl->irqchip.irq_mask = intel_gpio_irq_mask;
122357ff2df1SAndy Shevchenko 	pctrl->irqchip.irq_unmask = intel_gpio_irq_unmask;
122457ff2df1SAndy Shevchenko 	pctrl->irqchip.irq_set_type = intel_gpio_irq_type;
122557ff2df1SAndy Shevchenko 	pctrl->irqchip.irq_set_wake = intel_gpio_irq_wake;
122657ff2df1SAndy Shevchenko 	pctrl->irqchip.flags = IRQCHIP_MASK_ON_SUSPEND;
122757ff2df1SAndy Shevchenko 
1228193b40c8SMika Westerberg 	/*
1229af0c5330SLinus Walleij 	 * On some platforms several GPIO controllers share the same interrupt
1230af0c5330SLinus Walleij 	 * line.
1231193b40c8SMika Westerberg 	 */
12321a7d1cb8SMika Westerberg 	ret = devm_request_irq(pctrl->dev, irq, intel_gpio_irq,
12331a7d1cb8SMika Westerberg 			       IRQF_SHARED | IRQF_NO_THREAD,
1234193b40c8SMika Westerberg 			       dev_name(pctrl->dev), pctrl);
1235193b40c8SMika Westerberg 	if (ret) {
1236193b40c8SMika Westerberg 		dev_err(pctrl->dev, "failed to request interrupt\n");
1237f25c3aa9SMika Westerberg 		return ret;
12387981c001SMika Westerberg 	}
12397981c001SMika Westerberg 
1240af0c5330SLinus Walleij 	girq = &pctrl->chip.irq;
1241af0c5330SLinus Walleij 	girq->chip = &pctrl->irqchip;
1242af0c5330SLinus Walleij 	/* This will let us handle the IRQ in the driver */
1243af0c5330SLinus Walleij 	girq->parent_handler = NULL;
1244af0c5330SLinus Walleij 	girq->num_parents = 0;
1245af0c5330SLinus Walleij 	girq->default_type = IRQ_TYPE_NONE;
1246af0c5330SLinus Walleij 	girq->handler = handle_bad_irq;
1247af0c5330SLinus Walleij 
1248af0c5330SLinus Walleij 	ret = devm_gpiochip_add_data(pctrl->dev, &pctrl->chip, pctrl);
12497981c001SMika Westerberg 	if (ret) {
1250af0c5330SLinus Walleij 		dev_err(pctrl->dev, "failed to register gpiochip\n");
1251f25c3aa9SMika Westerberg 		return ret;
12527981c001SMika Westerberg 	}
12537981c001SMika Westerberg 
12547981c001SMika Westerberg 	return 0;
12557981c001SMika Westerberg }
12567981c001SMika Westerberg 
1257919eb475SMika Westerberg static int intel_pinctrl_add_padgroups(struct intel_pinctrl *pctrl,
1258919eb475SMika Westerberg 				       struct intel_community *community)
1259919eb475SMika Westerberg {
1260919eb475SMika Westerberg 	struct intel_padgroup *gpps;
126104035f7fSAndy Shevchenko 	unsigned int npins = community->npins;
126204035f7fSAndy Shevchenko 	unsigned int padown_num = 0;
1263919eb475SMika Westerberg 	size_t ngpps, i;
1264919eb475SMika Westerberg 
1265919eb475SMika Westerberg 	if (community->gpps)
1266919eb475SMika Westerberg 		ngpps = community->ngpps;
1267919eb475SMika Westerberg 	else
1268919eb475SMika Westerberg 		ngpps = DIV_ROUND_UP(community->npins, community->gpp_size);
1269919eb475SMika Westerberg 
1270919eb475SMika Westerberg 	gpps = devm_kcalloc(pctrl->dev, ngpps, sizeof(*gpps), GFP_KERNEL);
1271919eb475SMika Westerberg 	if (!gpps)
1272919eb475SMika Westerberg 		return -ENOMEM;
1273919eb475SMika Westerberg 
1274919eb475SMika Westerberg 	for (i = 0; i < ngpps; i++) {
1275919eb475SMika Westerberg 		if (community->gpps) {
1276919eb475SMika Westerberg 			gpps[i] = community->gpps[i];
1277919eb475SMika Westerberg 		} else {
127804035f7fSAndy Shevchenko 			unsigned int gpp_size = community->gpp_size;
1279919eb475SMika Westerberg 
1280919eb475SMika Westerberg 			gpps[i].reg_num = i;
1281919eb475SMika Westerberg 			gpps[i].base = community->pin_base + i * gpp_size;
1282919eb475SMika Westerberg 			gpps[i].size = min(gpp_size, npins);
1283919eb475SMika Westerberg 			npins -= gpps[i].size;
1284919eb475SMika Westerberg 		}
1285919eb475SMika Westerberg 
1286919eb475SMika Westerberg 		if (gpps[i].size > 32)
1287919eb475SMika Westerberg 			return -EINVAL;
1288919eb475SMika Westerberg 
1289e5a4ab6aSAndy Shevchenko 		/* Special treatment for GPIO base */
1290e5a4ab6aSAndy Shevchenko 		switch (gpps[i].gpio_base) {
1291e5a4ab6aSAndy Shevchenko 			case INTEL_GPIO_BASE_MATCH:
1292a60eac32SMika Westerberg 				gpps[i].gpio_base = gpps[i].base;
1293e5a4ab6aSAndy Shevchenko 				break;
12949bd59157SAndy Shevchenko 			case INTEL_GPIO_BASE_ZERO:
12959bd59157SAndy Shevchenko 				gpps[i].gpio_base = 0;
12969bd59157SAndy Shevchenko 				break;
1297e5a4ab6aSAndy Shevchenko 			case INTEL_GPIO_BASE_NOMAP:
1298e5a4ab6aSAndy Shevchenko 			default:
1299e5a4ab6aSAndy Shevchenko 				break;
1300e5a4ab6aSAndy Shevchenko 		}
1301a60eac32SMika Westerberg 
1302919eb475SMika Westerberg 		gpps[i].padown_num = padown_num;
1303919eb475SMika Westerberg 
1304919eb475SMika Westerberg 		/*
1305919eb475SMika Westerberg 		 * In older hardware the number of padown registers per
1306919eb475SMika Westerberg 		 * group is fixed regardless of the group size.
1307919eb475SMika Westerberg 		 */
1308919eb475SMika Westerberg 		if (community->gpp_num_padown_regs)
1309919eb475SMika Westerberg 			padown_num += community->gpp_num_padown_regs;
1310919eb475SMika Westerberg 		else
1311919eb475SMika Westerberg 			padown_num += DIV_ROUND_UP(gpps[i].size * 4, 32);
1312919eb475SMika Westerberg 	}
1313919eb475SMika Westerberg 
1314919eb475SMika Westerberg 	community->ngpps = ngpps;
1315919eb475SMika Westerberg 	community->gpps = gpps;
1316919eb475SMika Westerberg 
1317919eb475SMika Westerberg 	return 0;
1318919eb475SMika Westerberg }
1319919eb475SMika Westerberg 
13207981c001SMika Westerberg static int intel_pinctrl_pm_init(struct intel_pinctrl *pctrl)
13217981c001SMika Westerberg {
13227981c001SMika Westerberg #ifdef CONFIG_PM_SLEEP
13237981c001SMika Westerberg 	const struct intel_pinctrl_soc_data *soc = pctrl->soc;
13247981c001SMika Westerberg 	struct intel_community_context *communities;
13257981c001SMika Westerberg 	struct intel_pad_context *pads;
13267981c001SMika Westerberg 	int i;
13277981c001SMika Westerberg 
13287981c001SMika Westerberg 	pads = devm_kcalloc(pctrl->dev, soc->npins, sizeof(*pads), GFP_KERNEL);
13297981c001SMika Westerberg 	if (!pads)
13307981c001SMika Westerberg 		return -ENOMEM;
13317981c001SMika Westerberg 
13327981c001SMika Westerberg 	communities = devm_kcalloc(pctrl->dev, pctrl->ncommunities,
13337981c001SMika Westerberg 				   sizeof(*communities), GFP_KERNEL);
13347981c001SMika Westerberg 	if (!communities)
13357981c001SMika Westerberg 		return -ENOMEM;
13367981c001SMika Westerberg 
13377981c001SMika Westerberg 
13387981c001SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
13397981c001SMika Westerberg 		struct intel_community *community = &pctrl->communities[i];
1340a0a5f766SChris Chiu 		u32 *intmask, *hostown;
13417981c001SMika Westerberg 
13427981c001SMika Westerberg 		intmask = devm_kcalloc(pctrl->dev, community->ngpps,
13437981c001SMika Westerberg 				       sizeof(*intmask), GFP_KERNEL);
13447981c001SMika Westerberg 		if (!intmask)
13457981c001SMika Westerberg 			return -ENOMEM;
13467981c001SMika Westerberg 
13477981c001SMika Westerberg 		communities[i].intmask = intmask;
1348a0a5f766SChris Chiu 
1349a0a5f766SChris Chiu 		hostown = devm_kcalloc(pctrl->dev, community->ngpps,
1350a0a5f766SChris Chiu 				       sizeof(*hostown), GFP_KERNEL);
1351a0a5f766SChris Chiu 		if (!hostown)
1352a0a5f766SChris Chiu 			return -ENOMEM;
1353a0a5f766SChris Chiu 
1354a0a5f766SChris Chiu 		communities[i].hostown = hostown;
13557981c001SMika Westerberg 	}
13567981c001SMika Westerberg 
13577981c001SMika Westerberg 	pctrl->context.pads = pads;
13587981c001SMika Westerberg 	pctrl->context.communities = communities;
13597981c001SMika Westerberg #endif
13607981c001SMika Westerberg 
13617981c001SMika Westerberg 	return 0;
13627981c001SMika Westerberg }
13637981c001SMika Westerberg 
13640dd519e3SAndy Shevchenko static int intel_pinctrl_probe(struct platform_device *pdev,
13657981c001SMika Westerberg 			       const struct intel_pinctrl_soc_data *soc_data)
13667981c001SMika Westerberg {
13677981c001SMika Westerberg 	struct intel_pinctrl *pctrl;
13687981c001SMika Westerberg 	int i, ret, irq;
13697981c001SMika Westerberg 
13707981c001SMika Westerberg 	if (!soc_data)
13717981c001SMika Westerberg 		return -EINVAL;
13727981c001SMika Westerberg 
13737981c001SMika Westerberg 	pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL);
13747981c001SMika Westerberg 	if (!pctrl)
13757981c001SMika Westerberg 		return -ENOMEM;
13767981c001SMika Westerberg 
13777981c001SMika Westerberg 	pctrl->dev = &pdev->dev;
13787981c001SMika Westerberg 	pctrl->soc = soc_data;
137927d9098cSMika Westerberg 	raw_spin_lock_init(&pctrl->lock);
13807981c001SMika Westerberg 
13817981c001SMika Westerberg 	/*
13827981c001SMika Westerberg 	 * Make a copy of the communities which we can use to hold pointers
13837981c001SMika Westerberg 	 * to the registers.
13847981c001SMika Westerberg 	 */
13857981c001SMika Westerberg 	pctrl->ncommunities = pctrl->soc->ncommunities;
13867981c001SMika Westerberg 	pctrl->communities = devm_kcalloc(&pdev->dev, pctrl->ncommunities,
13877981c001SMika Westerberg 				  sizeof(*pctrl->communities), GFP_KERNEL);
13887981c001SMika Westerberg 	if (!pctrl->communities)
13897981c001SMika Westerberg 		return -ENOMEM;
13907981c001SMika Westerberg 
13917981c001SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
13927981c001SMika Westerberg 		struct intel_community *community = &pctrl->communities[i];
13937981c001SMika Westerberg 		void __iomem *regs;
13947981c001SMika Westerberg 		u32 padbar;
13957981c001SMika Westerberg 
13967981c001SMika Westerberg 		*community = pctrl->soc->communities[i];
13977981c001SMika Westerberg 
13989d5b6a95SAndy Shevchenko 		regs = devm_platform_ioremap_resource(pdev, community->barno);
13997981c001SMika Westerberg 		if (IS_ERR(regs))
14007981c001SMika Westerberg 			return PTR_ERR(regs);
14017981c001SMika Westerberg 
1402e57725eaSMika Westerberg 		/*
1403e57725eaSMika Westerberg 		 * Determine community features based on the revision if
1404e57725eaSMika Westerberg 		 * not specified already.
1405e57725eaSMika Westerberg 		 */
1406e57725eaSMika Westerberg 		if (!community->features) {
1407e57725eaSMika Westerberg 			u32 rev;
1408e57725eaSMika Westerberg 
1409e57725eaSMika Westerberg 			rev = (readl(regs + REVID) & REVID_MASK) >> REVID_SHIFT;
141004cc058fSMika Westerberg 			if (rev >= 0x94) {
1411e57725eaSMika Westerberg 				community->features |= PINCTRL_FEATURE_DEBOUNCE;
141204cc058fSMika Westerberg 				community->features |= PINCTRL_FEATURE_1K_PD;
141304cc058fSMika Westerberg 			}
1414e57725eaSMika Westerberg 		}
1415e57725eaSMika Westerberg 
14167981c001SMika Westerberg 		/* Read offset of the pad configuration registers */
14177981c001SMika Westerberg 		padbar = readl(regs + PADBAR);
14187981c001SMika Westerberg 
14197981c001SMika Westerberg 		community->regs = regs;
14207981c001SMika Westerberg 		community->pad_regs = regs + padbar;
1421919eb475SMika Westerberg 
1422919eb475SMika Westerberg 		ret = intel_pinctrl_add_padgroups(pctrl, community);
1423919eb475SMika Westerberg 		if (ret)
1424919eb475SMika Westerberg 			return ret;
14257981c001SMika Westerberg 	}
14267981c001SMika Westerberg 
14277981c001SMika Westerberg 	irq = platform_get_irq(pdev, 0);
14284e73d02fSStephen Boyd 	if (irq < 0)
14297981c001SMika Westerberg 		return irq;
14307981c001SMika Westerberg 
14317981c001SMika Westerberg 	ret = intel_pinctrl_pm_init(pctrl);
14327981c001SMika Westerberg 	if (ret)
14337981c001SMika Westerberg 		return ret;
14347981c001SMika Westerberg 
14357981c001SMika Westerberg 	pctrl->pctldesc = intel_pinctrl_desc;
14367981c001SMika Westerberg 	pctrl->pctldesc.name = dev_name(&pdev->dev);
14377981c001SMika Westerberg 	pctrl->pctldesc.pins = pctrl->soc->pins;
14387981c001SMika Westerberg 	pctrl->pctldesc.npins = pctrl->soc->npins;
14397981c001SMika Westerberg 
144054d46cd7SLaxman Dewangan 	pctrl->pctldev = devm_pinctrl_register(&pdev->dev, &pctrl->pctldesc,
144154d46cd7SLaxman Dewangan 					       pctrl);
1442323de9efSMasahiro Yamada 	if (IS_ERR(pctrl->pctldev)) {
14437981c001SMika Westerberg 		dev_err(&pdev->dev, "failed to register pinctrl driver\n");
1444323de9efSMasahiro Yamada 		return PTR_ERR(pctrl->pctldev);
14457981c001SMika Westerberg 	}
14467981c001SMika Westerberg 
14477981c001SMika Westerberg 	ret = intel_gpio_probe(pctrl, irq);
144854d46cd7SLaxman Dewangan 	if (ret)
14497981c001SMika Westerberg 		return ret;
14507981c001SMika Westerberg 
14517981c001SMika Westerberg 	platform_set_drvdata(pdev, pctrl);
14527981c001SMika Westerberg 
14537981c001SMika Westerberg 	return 0;
14547981c001SMika Westerberg }
14557981c001SMika Westerberg 
145670c263c4SAndy Shevchenko int intel_pinctrl_probe_by_hid(struct platform_device *pdev)
145770c263c4SAndy Shevchenko {
145870c263c4SAndy Shevchenko 	const struct intel_pinctrl_soc_data *data;
145970c263c4SAndy Shevchenko 
146070c263c4SAndy Shevchenko 	data = device_get_match_data(&pdev->dev);
146170c263c4SAndy Shevchenko 	return intel_pinctrl_probe(pdev, data);
146270c263c4SAndy Shevchenko }
146370c263c4SAndy Shevchenko EXPORT_SYMBOL_GPL(intel_pinctrl_probe_by_hid);
146470c263c4SAndy Shevchenko 
1465924cf800SAndy Shevchenko int intel_pinctrl_probe_by_uid(struct platform_device *pdev)
1466924cf800SAndy Shevchenko {
1467924cf800SAndy Shevchenko 	const struct intel_pinctrl_soc_data *data = NULL;
1468924cf800SAndy Shevchenko 	const struct intel_pinctrl_soc_data **table;
1469924cf800SAndy Shevchenko 	struct acpi_device *adev;
1470924cf800SAndy Shevchenko 	unsigned int i;
1471924cf800SAndy Shevchenko 
1472924cf800SAndy Shevchenko 	adev = ACPI_COMPANION(&pdev->dev);
1473924cf800SAndy Shevchenko 	if (adev) {
1474924cf800SAndy Shevchenko 		const void *match = device_get_match_data(&pdev->dev);
1475924cf800SAndy Shevchenko 
1476924cf800SAndy Shevchenko 		table = (const struct intel_pinctrl_soc_data **)match;
1477924cf800SAndy Shevchenko 		for (i = 0; table[i]; i++) {
1478924cf800SAndy Shevchenko 			if (!strcmp(adev->pnp.unique_id, table[i]->uid)) {
1479924cf800SAndy Shevchenko 				data = table[i];
1480924cf800SAndy Shevchenko 				break;
1481924cf800SAndy Shevchenko 			}
1482924cf800SAndy Shevchenko 		}
1483924cf800SAndy Shevchenko 	} else {
1484924cf800SAndy Shevchenko 		const struct platform_device_id *id;
1485924cf800SAndy Shevchenko 
1486924cf800SAndy Shevchenko 		id = platform_get_device_id(pdev);
1487924cf800SAndy Shevchenko 		if (!id)
1488924cf800SAndy Shevchenko 			return -ENODEV;
1489924cf800SAndy Shevchenko 
1490924cf800SAndy Shevchenko 		table = (const struct intel_pinctrl_soc_data **)id->driver_data;
1491924cf800SAndy Shevchenko 		data = table[pdev->id];
1492924cf800SAndy Shevchenko 	}
1493924cf800SAndy Shevchenko 
1494924cf800SAndy Shevchenko 	return intel_pinctrl_probe(pdev, data);
1495924cf800SAndy Shevchenko }
1496924cf800SAndy Shevchenko EXPORT_SYMBOL_GPL(intel_pinctrl_probe_by_uid);
1497924cf800SAndy Shevchenko 
14987981c001SMika Westerberg #ifdef CONFIG_PM_SLEEP
149904035f7fSAndy Shevchenko static bool intel_pinctrl_should_save(struct intel_pinctrl *pctrl, unsigned int pin)
1500c538b943SMika Westerberg {
1501c538b943SMika Westerberg 	const struct pin_desc *pd = pin_desc_get(pctrl->pctldev, pin);
1502c538b943SMika Westerberg 
1503c538b943SMika Westerberg 	if (!pd || !intel_pad_usable(pctrl, pin))
1504c538b943SMika Westerberg 		return false;
1505c538b943SMika Westerberg 
1506c538b943SMika Westerberg 	/*
1507c538b943SMika Westerberg 	 * Only restore the pin if it is actually in use by the kernel (or
1508c538b943SMika Westerberg 	 * by userspace). It is possible that some pins are used by the
1509c538b943SMika Westerberg 	 * BIOS during resume and those are not always locked down so leave
1510c538b943SMika Westerberg 	 * them alone.
1511c538b943SMika Westerberg 	 */
1512c538b943SMika Westerberg 	if (pd->mux_owner || pd->gpio_owner ||
15136cb0880fSChris Chiu 	    gpiochip_line_is_irq(&pctrl->chip, intel_pin_to_gpio(pctrl, pin)))
1514c538b943SMika Westerberg 		return true;
1515c538b943SMika Westerberg 
1516c538b943SMika Westerberg 	return false;
1517c538b943SMika Westerberg }
1518c538b943SMika Westerberg 
15192fef3276SBinbin Wu int intel_pinctrl_suspend_noirq(struct device *dev)
15207981c001SMika Westerberg {
1521cb035d74SWolfram Sang 	struct intel_pinctrl *pctrl = dev_get_drvdata(dev);
15227981c001SMika Westerberg 	struct intel_community_context *communities;
15237981c001SMika Westerberg 	struct intel_pad_context *pads;
15247981c001SMika Westerberg 	int i;
15257981c001SMika Westerberg 
15267981c001SMika Westerberg 	pads = pctrl->context.pads;
15277981c001SMika Westerberg 	for (i = 0; i < pctrl->soc->npins; i++) {
15287981c001SMika Westerberg 		const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
1529e57725eaSMika Westerberg 		void __iomem *padcfg;
15307981c001SMika Westerberg 		u32 val;
15317981c001SMika Westerberg 
1532c538b943SMika Westerberg 		if (!intel_pinctrl_should_save(pctrl, desc->number))
15337981c001SMika Westerberg 			continue;
15347981c001SMika Westerberg 
15357981c001SMika Westerberg 		val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG0));
15367981c001SMika Westerberg 		pads[i].padcfg0 = val & ~PADCFG0_GPIORXSTATE;
15377981c001SMika Westerberg 		val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG1));
15387981c001SMika Westerberg 		pads[i].padcfg1 = val;
1539e57725eaSMika Westerberg 
1540e57725eaSMika Westerberg 		padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG2);
1541e57725eaSMika Westerberg 		if (padcfg)
1542e57725eaSMika Westerberg 			pads[i].padcfg2 = readl(padcfg);
15437981c001SMika Westerberg 	}
15447981c001SMika Westerberg 
15457981c001SMika Westerberg 	communities = pctrl->context.communities;
15467981c001SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
15477981c001SMika Westerberg 		struct intel_community *community = &pctrl->communities[i];
15487981c001SMika Westerberg 		void __iomem *base;
154904035f7fSAndy Shevchenko 		unsigned int gpp;
15507981c001SMika Westerberg 
15517981c001SMika Westerberg 		base = community->regs + community->ie_offset;
15527981c001SMika Westerberg 		for (gpp = 0; gpp < community->ngpps; gpp++)
15537981c001SMika Westerberg 			communities[i].intmask[gpp] = readl(base + gpp * 4);
1554a0a5f766SChris Chiu 
1555a0a5f766SChris Chiu 		base = community->regs + community->hostown_offset;
1556a0a5f766SChris Chiu 		for (gpp = 0; gpp < community->ngpps; gpp++)
1557a0a5f766SChris Chiu 			communities[i].hostown[gpp] = readl(base + gpp * 4);
15587981c001SMika Westerberg 	}
15597981c001SMika Westerberg 
15607981c001SMika Westerberg 	return 0;
15617981c001SMika Westerberg }
15622fef3276SBinbin Wu EXPORT_SYMBOL_GPL(intel_pinctrl_suspend_noirq);
15637981c001SMika Westerberg 
1564f487bbf3SMika Westerberg static void intel_gpio_irq_init(struct intel_pinctrl *pctrl)
1565f487bbf3SMika Westerberg {
1566f487bbf3SMika Westerberg 	size_t i;
1567f487bbf3SMika Westerberg 
1568f487bbf3SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
1569f487bbf3SMika Westerberg 		const struct intel_community *community;
1570f487bbf3SMika Westerberg 		void __iomem *base;
157104035f7fSAndy Shevchenko 		unsigned int gpp;
1572f487bbf3SMika Westerberg 
1573f487bbf3SMika Westerberg 		community = &pctrl->communities[i];
1574f487bbf3SMika Westerberg 		base = community->regs;
1575f487bbf3SMika Westerberg 
1576f487bbf3SMika Westerberg 		for (gpp = 0; gpp < community->ngpps; gpp++) {
1577f487bbf3SMika Westerberg 			/* Mask and clear all interrupts */
1578f487bbf3SMika Westerberg 			writel(0, base + community->ie_offset + gpp * 4);
1579cf769bd8SMika Westerberg 			writel(0xffff, base + community->is_offset + gpp * 4);
1580f487bbf3SMika Westerberg 		}
1581f487bbf3SMika Westerberg 	}
1582f487bbf3SMika Westerberg }
1583f487bbf3SMika Westerberg 
1584a0a5f766SChris Chiu static u32
1585a0a5f766SChris Chiu intel_gpio_is_requested(struct gpio_chip *chip, int base, unsigned int size)
1586a0a5f766SChris Chiu {
1587a0a5f766SChris Chiu 	u32 requested = 0;
1588a0a5f766SChris Chiu 	unsigned int i;
1589a0a5f766SChris Chiu 
1590a0a5f766SChris Chiu 	for (i = 0; i < size; i++)
1591a0a5f766SChris Chiu 		if (gpiochip_is_requested(chip, base + i))
1592a0a5f766SChris Chiu 			requested |= BIT(i);
1593a0a5f766SChris Chiu 
1594a0a5f766SChris Chiu 	return requested;
1595a0a5f766SChris Chiu }
1596a0a5f766SChris Chiu 
1597942c5ea4SAndy Shevchenko static bool intel_gpio_update_reg(void __iomem *reg, u32 mask, u32 value)
1598a0a5f766SChris Chiu {
15995f61d951SAndy Shevchenko 	u32 curr, updated;
1600a0a5f766SChris Chiu 
1601942c5ea4SAndy Shevchenko 	curr = readl(reg);
16025f61d951SAndy Shevchenko 
1603942c5ea4SAndy Shevchenko 	updated = (curr & ~mask) | (value & mask);
1604942c5ea4SAndy Shevchenko 	if (curr == updated)
1605942c5ea4SAndy Shevchenko 		return false;
1606942c5ea4SAndy Shevchenko 
1607942c5ea4SAndy Shevchenko 	writel(updated, reg);
1608942c5ea4SAndy Shevchenko 	return true;
1609a0a5f766SChris Chiu }
1610a0a5f766SChris Chiu 
16117101e022SAndy Shevchenko static void intel_restore_hostown(struct intel_pinctrl *pctrl, unsigned int c,
16127101e022SAndy Shevchenko 				  void __iomem *base, unsigned int gpp, u32 saved)
16137101e022SAndy Shevchenko {
16147101e022SAndy Shevchenko 	const struct intel_community *community = &pctrl->communities[c];
16157101e022SAndy Shevchenko 	const struct intel_padgroup *padgrp = &community->gpps[gpp];
16167101e022SAndy Shevchenko 	struct device *dev = pctrl->dev;
1617942c5ea4SAndy Shevchenko 	u32 requested;
16187101e022SAndy Shevchenko 
1619e5a4ab6aSAndy Shevchenko 	if (padgrp->gpio_base == INTEL_GPIO_BASE_NOMAP)
16207101e022SAndy Shevchenko 		return;
16217101e022SAndy Shevchenko 
16227101e022SAndy Shevchenko 	requested = intel_gpio_is_requested(&pctrl->chip, padgrp->gpio_base, padgrp->size);
1623942c5ea4SAndy Shevchenko 	if (!intel_gpio_update_reg(base + gpp * 4, requested, saved))
16247101e022SAndy Shevchenko 		return;
16257101e022SAndy Shevchenko 
1626764cfe33SAndy Shevchenko 	dev_dbg(dev, "restored hostown %u/%u %#08x\n", c, gpp, readl(base + gpp * 4));
16277101e022SAndy Shevchenko }
16287101e022SAndy Shevchenko 
1629471dd9a9SAndy Shevchenko static void intel_restore_intmask(struct intel_pinctrl *pctrl, unsigned int c,
1630471dd9a9SAndy Shevchenko 				  void __iomem *base, unsigned int gpp, u32 saved)
1631471dd9a9SAndy Shevchenko {
1632471dd9a9SAndy Shevchenko 	struct device *dev = pctrl->dev;
1633471dd9a9SAndy Shevchenko 
1634942c5ea4SAndy Shevchenko 	if (!intel_gpio_update_reg(base + gpp * 4, ~0U, saved))
1635942c5ea4SAndy Shevchenko 		return;
1636942c5ea4SAndy Shevchenko 
1637471dd9a9SAndy Shevchenko 	dev_dbg(dev, "restored mask %u/%u %#08x\n", c, gpp, readl(base + gpp * 4));
1638471dd9a9SAndy Shevchenko }
1639471dd9a9SAndy Shevchenko 
1640f78f152aSAndy Shevchenko static void intel_restore_padcfg(struct intel_pinctrl *pctrl, unsigned int pin,
1641f78f152aSAndy Shevchenko 				 unsigned int reg, u32 saved)
1642f78f152aSAndy Shevchenko {
1643f78f152aSAndy Shevchenko 	u32 mask = (reg == PADCFG0) ? PADCFG0_GPIORXSTATE : 0;
1644f78f152aSAndy Shevchenko 	unsigned int n = reg / sizeof(u32);
1645f78f152aSAndy Shevchenko 	struct device *dev = pctrl->dev;
1646f78f152aSAndy Shevchenko 	void __iomem *padcfg;
1647f78f152aSAndy Shevchenko 
1648f78f152aSAndy Shevchenko 	padcfg = intel_get_padcfg(pctrl, pin, reg);
1649f78f152aSAndy Shevchenko 	if (!padcfg)
1650f78f152aSAndy Shevchenko 		return;
1651f78f152aSAndy Shevchenko 
1652942c5ea4SAndy Shevchenko 	if (!intel_gpio_update_reg(padcfg, ~mask, saved))
1653f78f152aSAndy Shevchenko 		return;
1654f78f152aSAndy Shevchenko 
1655f78f152aSAndy Shevchenko 	dev_dbg(dev, "restored pin %u padcfg%u %#08x\n", pin, n, readl(padcfg));
1656f78f152aSAndy Shevchenko }
1657f78f152aSAndy Shevchenko 
16582fef3276SBinbin Wu int intel_pinctrl_resume_noirq(struct device *dev)
16597981c001SMika Westerberg {
1660cb035d74SWolfram Sang 	struct intel_pinctrl *pctrl = dev_get_drvdata(dev);
16617981c001SMika Westerberg 	const struct intel_community_context *communities;
16627981c001SMika Westerberg 	const struct intel_pad_context *pads;
16637981c001SMika Westerberg 	int i;
16647981c001SMika Westerberg 
16657981c001SMika Westerberg 	/* Mask all interrupts */
16667981c001SMika Westerberg 	intel_gpio_irq_init(pctrl);
16677981c001SMika Westerberg 
16687981c001SMika Westerberg 	pads = pctrl->context.pads;
16697981c001SMika Westerberg 	for (i = 0; i < pctrl->soc->npins; i++) {
16707981c001SMika Westerberg 		const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
16717981c001SMika Westerberg 
1672c538b943SMika Westerberg 		if (!intel_pinctrl_should_save(pctrl, desc->number))
16737981c001SMika Westerberg 			continue;
16747981c001SMika Westerberg 
1675f78f152aSAndy Shevchenko 		intel_restore_padcfg(pctrl, desc->number, PADCFG0, pads[i].padcfg0);
1676f78f152aSAndy Shevchenko 		intel_restore_padcfg(pctrl, desc->number, PADCFG1, pads[i].padcfg1);
1677f78f152aSAndy Shevchenko 		intel_restore_padcfg(pctrl, desc->number, PADCFG2, pads[i].padcfg2);
16787981c001SMika Westerberg 	}
16797981c001SMika Westerberg 
16807981c001SMika Westerberg 	communities = pctrl->context.communities;
16817981c001SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
16827981c001SMika Westerberg 		struct intel_community *community = &pctrl->communities[i];
16837981c001SMika Westerberg 		void __iomem *base;
168404035f7fSAndy Shevchenko 		unsigned int gpp;
16857981c001SMika Westerberg 
16867981c001SMika Westerberg 		base = community->regs + community->ie_offset;
1687471dd9a9SAndy Shevchenko 		for (gpp = 0; gpp < community->ngpps; gpp++)
1688471dd9a9SAndy Shevchenko 			intel_restore_intmask(pctrl, i, base, gpp, communities[i].intmask[gpp]);
1689a0a5f766SChris Chiu 
1690a0a5f766SChris Chiu 		base = community->regs + community->hostown_offset;
16917101e022SAndy Shevchenko 		for (gpp = 0; gpp < community->ngpps; gpp++)
16927101e022SAndy Shevchenko 			intel_restore_hostown(pctrl, i, base, gpp, communities[i].hostown[gpp]);
16937981c001SMika Westerberg 	}
16947981c001SMika Westerberg 
16957981c001SMika Westerberg 	return 0;
16967981c001SMika Westerberg }
16972fef3276SBinbin Wu EXPORT_SYMBOL_GPL(intel_pinctrl_resume_noirq);
16987981c001SMika Westerberg #endif
16997981c001SMika Westerberg 
17007981c001SMika Westerberg MODULE_AUTHOR("Mathias Nyman <mathias.nyman@linux.intel.com>");
17017981c001SMika Westerberg MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
17027981c001SMika Westerberg MODULE_DESCRIPTION("Intel pinctrl/GPIO core driver");
17037981c001SMika Westerberg MODULE_LICENSE("GPL v2");
1704