xref: /openbmc/linux/drivers/pinctrl/intel/pinctrl-intel.c (revision 6a33a1d6d66a46157951388ce4c5f2674b251982)
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>
14*6a33a1d6SAndy Shevchenko #include <linux/module.h>
157981c001SMika Westerberg #include <linux/platform_device.h>
16924cf800SAndy Shevchenko #include <linux/property.h>
17*6a33a1d6SAndy 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 
74*6a33a1d6SAndy 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
997981c001SMika Westerberg  * @soc: SoC/PCH specific pin configuration data
1007981c001SMika Westerberg  * @communities: All communities in this pin controller
1017981c001SMika Westerberg  * @ncommunities: Number of communities in this pin controller
1027981c001SMika Westerberg  * @context: Configuration saved over system sleep
10301dabe91SNilesh Bacchewar  * @irq: pinctrl/GPIO chip irq number
1047981c001SMika Westerberg  */
1057981c001SMika Westerberg struct intel_pinctrl {
1067981c001SMika Westerberg 	struct device *dev;
10727d9098cSMika Westerberg 	raw_spinlock_t lock;
1087981c001SMika Westerberg 	struct pinctrl_desc pctldesc;
1097981c001SMika Westerberg 	struct pinctrl_dev *pctldev;
1107981c001SMika Westerberg 	struct gpio_chip chip;
1117981c001SMika Westerberg 	const struct intel_pinctrl_soc_data *soc;
1127981c001SMika Westerberg 	struct intel_community *communities;
1137981c001SMika Westerberg 	size_t ncommunities;
1147981c001SMika Westerberg 	struct intel_pinctrl_context context;
11501dabe91SNilesh Bacchewar 	int irq;
1167981c001SMika Westerberg };
1177981c001SMika Westerberg 
1187981c001SMika Westerberg #define pin_to_padno(c, p)	((p) - (c)->pin_base)
119919eb475SMika Westerberg #define padgroup_offset(g, p)	((p) - (g)->base)
1207981c001SMika Westerberg 
1217981c001SMika Westerberg static struct intel_community *intel_get_community(struct intel_pinctrl *pctrl,
12204035f7fSAndy Shevchenko 						   unsigned int pin)
1237981c001SMika Westerberg {
1247981c001SMika Westerberg 	struct intel_community *community;
1257981c001SMika Westerberg 	int i;
1267981c001SMika Westerberg 
1277981c001SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
1287981c001SMika Westerberg 		community = &pctrl->communities[i];
1297981c001SMika Westerberg 		if (pin >= community->pin_base &&
1307981c001SMika Westerberg 		    pin < community->pin_base + community->npins)
1317981c001SMika Westerberg 			return community;
1327981c001SMika Westerberg 	}
1337981c001SMika Westerberg 
1347981c001SMika Westerberg 	dev_warn(pctrl->dev, "failed to find community for pin %u\n", pin);
1357981c001SMika Westerberg 	return NULL;
1367981c001SMika Westerberg }
1377981c001SMika Westerberg 
138919eb475SMika Westerberg static const struct intel_padgroup *
139919eb475SMika Westerberg intel_community_get_padgroup(const struct intel_community *community,
14004035f7fSAndy Shevchenko 			     unsigned int pin)
141919eb475SMika Westerberg {
142919eb475SMika Westerberg 	int i;
143919eb475SMika Westerberg 
144919eb475SMika Westerberg 	for (i = 0; i < community->ngpps; i++) {
145919eb475SMika Westerberg 		const struct intel_padgroup *padgrp = &community->gpps[i];
146919eb475SMika Westerberg 
147919eb475SMika Westerberg 		if (pin >= padgrp->base && pin < padgrp->base + padgrp->size)
148919eb475SMika Westerberg 			return padgrp;
149919eb475SMika Westerberg 	}
150919eb475SMika Westerberg 
151919eb475SMika Westerberg 	return NULL;
152919eb475SMika Westerberg }
153919eb475SMika Westerberg 
15404035f7fSAndy Shevchenko static void __iomem *intel_get_padcfg(struct intel_pinctrl *pctrl,
15504035f7fSAndy Shevchenko 				      unsigned int pin, unsigned int reg)
1567981c001SMika Westerberg {
1577981c001SMika Westerberg 	const struct intel_community *community;
15804035f7fSAndy Shevchenko 	unsigned int padno;
159e57725eaSMika Westerberg 	size_t nregs;
1607981c001SMika Westerberg 
1617981c001SMika Westerberg 	community = intel_get_community(pctrl, pin);
1627981c001SMika Westerberg 	if (!community)
1637981c001SMika Westerberg 		return NULL;
1647981c001SMika Westerberg 
1657981c001SMika Westerberg 	padno = pin_to_padno(community, pin);
166e57725eaSMika Westerberg 	nregs = (community->features & PINCTRL_FEATURE_DEBOUNCE) ? 4 : 2;
167e57725eaSMika Westerberg 
1687eb7ecddSAndy Shevchenko 	if (reg >= nregs * 4)
169e57725eaSMika Westerberg 		return NULL;
170e57725eaSMika Westerberg 
171e57725eaSMika Westerberg 	return community->pad_regs + reg + padno * nregs * 4;
1727981c001SMika Westerberg }
1737981c001SMika Westerberg 
17404035f7fSAndy Shevchenko static bool intel_pad_owned_by_host(struct intel_pinctrl *pctrl, unsigned int pin)
1757981c001SMika Westerberg {
1767981c001SMika Westerberg 	const struct intel_community *community;
177919eb475SMika Westerberg 	const struct intel_padgroup *padgrp;
17804035f7fSAndy Shevchenko 	unsigned int gpp, offset, gpp_offset;
1797981c001SMika Westerberg 	void __iomem *padown;
1807981c001SMika Westerberg 
1817981c001SMika Westerberg 	community = intel_get_community(pctrl, pin);
1827981c001SMika Westerberg 	if (!community)
1837981c001SMika Westerberg 		return false;
1847981c001SMika Westerberg 	if (!community->padown_offset)
1857981c001SMika Westerberg 		return true;
1867981c001SMika Westerberg 
187919eb475SMika Westerberg 	padgrp = intel_community_get_padgroup(community, pin);
188919eb475SMika Westerberg 	if (!padgrp)
189919eb475SMika Westerberg 		return false;
190919eb475SMika Westerberg 
191919eb475SMika Westerberg 	gpp_offset = padgroup_offset(padgrp, pin);
192919eb475SMika Westerberg 	gpp = PADOWN_GPP(gpp_offset);
193919eb475SMika Westerberg 	offset = community->padown_offset + padgrp->padown_num * 4 + gpp * 4;
1947981c001SMika Westerberg 	padown = community->regs + offset;
1957981c001SMika Westerberg 
196919eb475SMika Westerberg 	return !(readl(padown) & PADOWN_MASK(gpp_offset));
1977981c001SMika Westerberg }
1987981c001SMika Westerberg 
19904035f7fSAndy Shevchenko static bool intel_pad_acpi_mode(struct intel_pinctrl *pctrl, unsigned int pin)
2007981c001SMika Westerberg {
2017981c001SMika Westerberg 	const struct intel_community *community;
202919eb475SMika Westerberg 	const struct intel_padgroup *padgrp;
20304035f7fSAndy Shevchenko 	unsigned int offset, gpp_offset;
2047981c001SMika Westerberg 	void __iomem *hostown;
2057981c001SMika Westerberg 
2067981c001SMika Westerberg 	community = intel_get_community(pctrl, pin);
2077981c001SMika Westerberg 	if (!community)
2087981c001SMika Westerberg 		return true;
2097981c001SMika Westerberg 	if (!community->hostown_offset)
2107981c001SMika Westerberg 		return false;
2117981c001SMika Westerberg 
212919eb475SMika Westerberg 	padgrp = intel_community_get_padgroup(community, pin);
213919eb475SMika Westerberg 	if (!padgrp)
214919eb475SMika Westerberg 		return true;
215919eb475SMika Westerberg 
216919eb475SMika Westerberg 	gpp_offset = padgroup_offset(padgrp, pin);
217919eb475SMika Westerberg 	offset = community->hostown_offset + padgrp->reg_num * 4;
2187981c001SMika Westerberg 	hostown = community->regs + offset;
2197981c001SMika Westerberg 
220919eb475SMika Westerberg 	return !(readl(hostown) & BIT(gpp_offset));
2217981c001SMika Westerberg }
2227981c001SMika Westerberg 
22304035f7fSAndy Shevchenko static bool intel_pad_locked(struct intel_pinctrl *pctrl, unsigned int pin)
2247981c001SMika Westerberg {
2257981c001SMika Westerberg 	struct intel_community *community;
226919eb475SMika Westerberg 	const struct intel_padgroup *padgrp;
22704035f7fSAndy Shevchenko 	unsigned int offset, gpp_offset;
2287981c001SMika Westerberg 	u32 value;
2297981c001SMika Westerberg 
2307981c001SMika Westerberg 	community = intel_get_community(pctrl, pin);
2317981c001SMika Westerberg 	if (!community)
2327981c001SMika Westerberg 		return true;
2337981c001SMika Westerberg 	if (!community->padcfglock_offset)
2347981c001SMika Westerberg 		return false;
2357981c001SMika Westerberg 
236919eb475SMika Westerberg 	padgrp = intel_community_get_padgroup(community, pin);
237919eb475SMika Westerberg 	if (!padgrp)
238919eb475SMika Westerberg 		return true;
239919eb475SMika Westerberg 
240919eb475SMika Westerberg 	gpp_offset = padgroup_offset(padgrp, pin);
2417981c001SMika Westerberg 
2427981c001SMika Westerberg 	/*
2437981c001SMika Westerberg 	 * If PADCFGLOCK and PADCFGLOCKTX bits are both clear for this pad,
2447981c001SMika Westerberg 	 * the pad is considered unlocked. Any other case means that it is
2457981c001SMika Westerberg 	 * either fully or partially locked and we don't touch it.
2467981c001SMika Westerberg 	 */
247919eb475SMika Westerberg 	offset = community->padcfglock_offset + padgrp->reg_num * 8;
2487981c001SMika Westerberg 	value = readl(community->regs + offset);
249919eb475SMika Westerberg 	if (value & BIT(gpp_offset))
2507981c001SMika Westerberg 		return true;
2517981c001SMika Westerberg 
252919eb475SMika Westerberg 	offset = community->padcfglock_offset + 4 + padgrp->reg_num * 8;
2537981c001SMika Westerberg 	value = readl(community->regs + offset);
254919eb475SMika Westerberg 	if (value & BIT(gpp_offset))
2557981c001SMika Westerberg 		return true;
2567981c001SMika Westerberg 
2577981c001SMika Westerberg 	return false;
2587981c001SMika Westerberg }
2597981c001SMika Westerberg 
26004035f7fSAndy Shevchenko static bool intel_pad_usable(struct intel_pinctrl *pctrl, unsigned int pin)
2617981c001SMika Westerberg {
2627981c001SMika Westerberg 	return intel_pad_owned_by_host(pctrl, pin) &&
2637981c001SMika Westerberg 		!intel_pad_locked(pctrl, pin);
2647981c001SMika Westerberg }
2657981c001SMika Westerberg 
2667981c001SMika Westerberg static int intel_get_groups_count(struct pinctrl_dev *pctldev)
2677981c001SMika Westerberg {
2687981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
2697981c001SMika Westerberg 
2707981c001SMika Westerberg 	return pctrl->soc->ngroups;
2717981c001SMika Westerberg }
2727981c001SMika Westerberg 
2737981c001SMika Westerberg static const char *intel_get_group_name(struct pinctrl_dev *pctldev,
27404035f7fSAndy Shevchenko 				      unsigned int group)
2757981c001SMika Westerberg {
2767981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
2777981c001SMika Westerberg 
2787981c001SMika Westerberg 	return pctrl->soc->groups[group].name;
2797981c001SMika Westerberg }
2807981c001SMika Westerberg 
28104035f7fSAndy Shevchenko static int intel_get_group_pins(struct pinctrl_dev *pctldev, unsigned int group,
28204035f7fSAndy Shevchenko 			      const unsigned int **pins, unsigned int *npins)
2837981c001SMika Westerberg {
2847981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
2857981c001SMika Westerberg 
2867981c001SMika Westerberg 	*pins = pctrl->soc->groups[group].pins;
2877981c001SMika Westerberg 	*npins = pctrl->soc->groups[group].npins;
2887981c001SMika Westerberg 	return 0;
2897981c001SMika Westerberg }
2907981c001SMika Westerberg 
2917981c001SMika Westerberg static void intel_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
29204035f7fSAndy Shevchenko 			       unsigned int pin)
2937981c001SMika Westerberg {
2947981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
295e57725eaSMika Westerberg 	void __iomem *padcfg;
2967981c001SMika Westerberg 	u32 cfg0, cfg1, mode;
2977981c001SMika Westerberg 	bool locked, acpi;
2987981c001SMika Westerberg 
2997981c001SMika Westerberg 	if (!intel_pad_owned_by_host(pctrl, pin)) {
3007981c001SMika Westerberg 		seq_puts(s, "not available");
3017981c001SMika Westerberg 		return;
3027981c001SMika Westerberg 	}
3037981c001SMika Westerberg 
3047981c001SMika Westerberg 	cfg0 = readl(intel_get_padcfg(pctrl, pin, PADCFG0));
3057981c001SMika Westerberg 	cfg1 = readl(intel_get_padcfg(pctrl, pin, PADCFG1));
3067981c001SMika Westerberg 
3077981c001SMika Westerberg 	mode = (cfg0 & PADCFG0_PMODE_MASK) >> PADCFG0_PMODE_SHIFT;
3087981c001SMika Westerberg 	if (!mode)
3097981c001SMika Westerberg 		seq_puts(s, "GPIO ");
3107981c001SMika Westerberg 	else
3117981c001SMika Westerberg 		seq_printf(s, "mode %d ", mode);
3127981c001SMika Westerberg 
3137981c001SMika Westerberg 	seq_printf(s, "0x%08x 0x%08x", cfg0, cfg1);
3147981c001SMika Westerberg 
315e57725eaSMika Westerberg 	/* Dump the additional PADCFG registers if available */
316e57725eaSMika Westerberg 	padcfg = intel_get_padcfg(pctrl, pin, PADCFG2);
317e57725eaSMika Westerberg 	if (padcfg)
318e57725eaSMika Westerberg 		seq_printf(s, " 0x%08x", readl(padcfg));
319e57725eaSMika Westerberg 
3207981c001SMika Westerberg 	locked = intel_pad_locked(pctrl, pin);
3214341e8a5SMika Westerberg 	acpi = intel_pad_acpi_mode(pctrl, pin);
3227981c001SMika Westerberg 
3237981c001SMika Westerberg 	if (locked || acpi) {
3247981c001SMika Westerberg 		seq_puts(s, " [");
3257981c001SMika Westerberg 		if (locked) {
3267981c001SMika Westerberg 			seq_puts(s, "LOCKED");
3277981c001SMika Westerberg 			if (acpi)
3287981c001SMika Westerberg 				seq_puts(s, ", ");
3297981c001SMika Westerberg 		}
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 
429f5a26acfSMika Westerberg static void intel_gpio_set_gpio_mode(void __iomem *padcfg0)
430f5a26acfSMika Westerberg {
431f5a26acfSMika Westerberg 	u32 value;
432f5a26acfSMika Westerberg 
433f5a26acfSMika Westerberg 	/* Put the pad into GPIO mode */
434f5a26acfSMika Westerberg 	value = readl(padcfg0) & ~PADCFG0_PMODE_MASK;
435f5a26acfSMika Westerberg 	/* Disable SCI/SMI/NMI generation */
436f5a26acfSMika Westerberg 	value &= ~(PADCFG0_GPIROUTIOXAPIC | PADCFG0_GPIROUTSCI);
437f5a26acfSMika Westerberg 	value &= ~(PADCFG0_GPIROUTSMI | PADCFG0_GPIROUTNMI);
438f5a26acfSMika Westerberg 	writel(value, padcfg0);
439f5a26acfSMika Westerberg }
440f5a26acfSMika Westerberg 
4417981c001SMika Westerberg static int intel_gpio_request_enable(struct pinctrl_dev *pctldev,
4427981c001SMika Westerberg 				     struct pinctrl_gpio_range *range,
44304035f7fSAndy Shevchenko 				     unsigned int pin)
4447981c001SMika Westerberg {
4457981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
4467981c001SMika Westerberg 	void __iomem *padcfg0;
4477981c001SMika Westerberg 	unsigned long flags;
4487981c001SMika Westerberg 
44927d9098cSMika Westerberg 	raw_spin_lock_irqsave(&pctrl->lock, flags);
4507981c001SMika Westerberg 
4517981c001SMika Westerberg 	if (!intel_pad_usable(pctrl, pin)) {
45227d9098cSMika Westerberg 		raw_spin_unlock_irqrestore(&pctrl->lock, flags);
4537981c001SMika Westerberg 		return -EBUSY;
4547981c001SMika Westerberg 	}
4557981c001SMika Westerberg 
4567981c001SMika Westerberg 	padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
457f5a26acfSMika Westerberg 	intel_gpio_set_gpio_mode(padcfg0);
45817fab473SAndy Shevchenko 	/* Disable TX buffer and enable RX (this will be input) */
45917fab473SAndy Shevchenko 	__intel_gpio_set_direction(padcfg0, true);
46017fab473SAndy Shevchenko 
46127d9098cSMika Westerberg 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
4627981c001SMika Westerberg 
4637981c001SMika Westerberg 	return 0;
4647981c001SMika Westerberg }
4657981c001SMika Westerberg 
4667981c001SMika Westerberg static int intel_gpio_set_direction(struct pinctrl_dev *pctldev,
4677981c001SMika Westerberg 				    struct pinctrl_gpio_range *range,
46804035f7fSAndy Shevchenko 				    unsigned int pin, bool input)
4697981c001SMika Westerberg {
4707981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
4717981c001SMika Westerberg 	void __iomem *padcfg0;
4727981c001SMika Westerberg 	unsigned long flags;
4737981c001SMika Westerberg 
47427d9098cSMika Westerberg 	raw_spin_lock_irqsave(&pctrl->lock, flags);
4757981c001SMika Westerberg 
4767981c001SMika Westerberg 	padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
47717fab473SAndy Shevchenko 	__intel_gpio_set_direction(padcfg0, input);
4787981c001SMika Westerberg 
47927d9098cSMika Westerberg 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
4807981c001SMika Westerberg 
4817981c001SMika Westerberg 	return 0;
4827981c001SMika Westerberg }
4837981c001SMika Westerberg 
4847981c001SMika Westerberg static const struct pinmux_ops intel_pinmux_ops = {
4857981c001SMika Westerberg 	.get_functions_count = intel_get_functions_count,
4867981c001SMika Westerberg 	.get_function_name = intel_get_function_name,
4877981c001SMika Westerberg 	.get_function_groups = intel_get_function_groups,
4887981c001SMika Westerberg 	.set_mux = intel_pinmux_set_mux,
4897981c001SMika Westerberg 	.gpio_request_enable = intel_gpio_request_enable,
4907981c001SMika Westerberg 	.gpio_set_direction = intel_gpio_set_direction,
4917981c001SMika Westerberg };
4927981c001SMika Westerberg 
49304035f7fSAndy Shevchenko static int intel_config_get(struct pinctrl_dev *pctldev, unsigned int pin,
4947981c001SMika Westerberg 			    unsigned long *config)
4957981c001SMika Westerberg {
4967981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
4977981c001SMika Westerberg 	enum pin_config_param param = pinconf_to_config_param(*config);
49804cc058fSMika Westerberg 	const struct intel_community *community;
4997981c001SMika Westerberg 	u32 value, term;
500e57725eaSMika Westerberg 	u32 arg = 0;
5017981c001SMika Westerberg 
5027981c001SMika Westerberg 	if (!intel_pad_owned_by_host(pctrl, pin))
5037981c001SMika Westerberg 		return -ENOTSUPP;
5047981c001SMika Westerberg 
50504cc058fSMika Westerberg 	community = intel_get_community(pctrl, pin);
5067981c001SMika Westerberg 	value = readl(intel_get_padcfg(pctrl, pin, PADCFG1));
5077981c001SMika Westerberg 	term = (value & PADCFG1_TERM_MASK) >> PADCFG1_TERM_SHIFT;
5087981c001SMika Westerberg 
5097981c001SMika Westerberg 	switch (param) {
5107981c001SMika Westerberg 	case PIN_CONFIG_BIAS_DISABLE:
5117981c001SMika Westerberg 		if (term)
5127981c001SMika Westerberg 			return -EINVAL;
5137981c001SMika Westerberg 		break;
5147981c001SMika Westerberg 
5157981c001SMika Westerberg 	case PIN_CONFIG_BIAS_PULL_UP:
5167981c001SMika Westerberg 		if (!term || !(value & PADCFG1_TERM_UP))
5177981c001SMika Westerberg 			return -EINVAL;
5187981c001SMika Westerberg 
5197981c001SMika Westerberg 		switch (term) {
5207981c001SMika Westerberg 		case PADCFG1_TERM_1K:
5217981c001SMika Westerberg 			arg = 1000;
5227981c001SMika Westerberg 			break;
5237981c001SMika Westerberg 		case PADCFG1_TERM_2K:
5247981c001SMika Westerberg 			arg = 2000;
5257981c001SMika Westerberg 			break;
5267981c001SMika Westerberg 		case PADCFG1_TERM_5K:
5277981c001SMika Westerberg 			arg = 5000;
5287981c001SMika Westerberg 			break;
5297981c001SMika Westerberg 		case PADCFG1_TERM_20K:
5307981c001SMika Westerberg 			arg = 20000;
5317981c001SMika Westerberg 			break;
5327981c001SMika Westerberg 		}
5337981c001SMika Westerberg 
5347981c001SMika Westerberg 		break;
5357981c001SMika Westerberg 
5367981c001SMika Westerberg 	case PIN_CONFIG_BIAS_PULL_DOWN:
5377981c001SMika Westerberg 		if (!term || value & PADCFG1_TERM_UP)
5387981c001SMika Westerberg 			return -EINVAL;
5397981c001SMika Westerberg 
5407981c001SMika Westerberg 		switch (term) {
54104cc058fSMika Westerberg 		case PADCFG1_TERM_1K:
54204cc058fSMika Westerberg 			if (!(community->features & PINCTRL_FEATURE_1K_PD))
54304cc058fSMika Westerberg 				return -EINVAL;
54404cc058fSMika Westerberg 			arg = 1000;
54504cc058fSMika Westerberg 			break;
5467981c001SMika Westerberg 		case PADCFG1_TERM_5K:
5477981c001SMika Westerberg 			arg = 5000;
5487981c001SMika Westerberg 			break;
5497981c001SMika Westerberg 		case PADCFG1_TERM_20K:
5507981c001SMika Westerberg 			arg = 20000;
5517981c001SMika Westerberg 			break;
5527981c001SMika Westerberg 		}
5537981c001SMika Westerberg 
5547981c001SMika Westerberg 		break;
5557981c001SMika Westerberg 
556e57725eaSMika Westerberg 	case PIN_CONFIG_INPUT_DEBOUNCE: {
557e57725eaSMika Westerberg 		void __iomem *padcfg2;
558e57725eaSMika Westerberg 		u32 v;
559e57725eaSMika Westerberg 
560e57725eaSMika Westerberg 		padcfg2 = intel_get_padcfg(pctrl, pin, PADCFG2);
561e57725eaSMika Westerberg 		if (!padcfg2)
562e57725eaSMika Westerberg 			return -ENOTSUPP;
563e57725eaSMika Westerberg 
564e57725eaSMika Westerberg 		v = readl(padcfg2);
565e57725eaSMika Westerberg 		if (!(v & PADCFG2_DEBEN))
566e57725eaSMika Westerberg 			return -EINVAL;
567e57725eaSMika Westerberg 
568e57725eaSMika Westerberg 		v = (v & PADCFG2_DEBOUNCE_MASK) >> PADCFG2_DEBOUNCE_SHIFT;
569*6a33a1d6SAndy Shevchenko 		arg = BIT(v) * DEBOUNCE_PERIOD_NSEC / NSEC_PER_USEC;
570e57725eaSMika Westerberg 
571e57725eaSMika Westerberg 		break;
572e57725eaSMika Westerberg 	}
573e57725eaSMika Westerberg 
5747981c001SMika Westerberg 	default:
5757981c001SMika Westerberg 		return -ENOTSUPP;
5767981c001SMika Westerberg 	}
5777981c001SMika Westerberg 
5787981c001SMika Westerberg 	*config = pinconf_to_config_packed(param, arg);
5797981c001SMika Westerberg 	return 0;
5807981c001SMika Westerberg }
5817981c001SMika Westerberg 
58204035f7fSAndy Shevchenko static int intel_config_set_pull(struct intel_pinctrl *pctrl, unsigned int pin,
5837981c001SMika Westerberg 				 unsigned long config)
5847981c001SMika Westerberg {
58504035f7fSAndy Shevchenko 	unsigned int param = pinconf_to_config_param(config);
58604035f7fSAndy Shevchenko 	unsigned int arg = pinconf_to_config_argument(config);
58704cc058fSMika Westerberg 	const struct intel_community *community;
5887981c001SMika Westerberg 	void __iomem *padcfg1;
5897981c001SMika Westerberg 	unsigned long flags;
5907981c001SMika Westerberg 	int ret = 0;
5917981c001SMika Westerberg 	u32 value;
5927981c001SMika Westerberg 
59327d9098cSMika Westerberg 	raw_spin_lock_irqsave(&pctrl->lock, flags);
5947981c001SMika Westerberg 
59504cc058fSMika Westerberg 	community = intel_get_community(pctrl, pin);
5967981c001SMika Westerberg 	padcfg1 = intel_get_padcfg(pctrl, pin, PADCFG1);
5977981c001SMika Westerberg 	value = readl(padcfg1);
5987981c001SMika Westerberg 
5997981c001SMika Westerberg 	switch (param) {
6007981c001SMika Westerberg 	case PIN_CONFIG_BIAS_DISABLE:
6017981c001SMika Westerberg 		value &= ~(PADCFG1_TERM_MASK | PADCFG1_TERM_UP);
6027981c001SMika Westerberg 		break;
6037981c001SMika Westerberg 
6047981c001SMika Westerberg 	case PIN_CONFIG_BIAS_PULL_UP:
6057981c001SMika Westerberg 		value &= ~PADCFG1_TERM_MASK;
6067981c001SMika Westerberg 
6077981c001SMika Westerberg 		value |= PADCFG1_TERM_UP;
6087981c001SMika Westerberg 
6097981c001SMika Westerberg 		switch (arg) {
6107981c001SMika Westerberg 		case 20000:
6117981c001SMika Westerberg 			value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT;
6127981c001SMika Westerberg 			break;
6137981c001SMika Westerberg 		case 5000:
6147981c001SMika Westerberg 			value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT;
6157981c001SMika Westerberg 			break;
6167981c001SMika Westerberg 		case 2000:
6177981c001SMika Westerberg 			value |= PADCFG1_TERM_2K << PADCFG1_TERM_SHIFT;
6187981c001SMika Westerberg 			break;
6197981c001SMika Westerberg 		case 1000:
6207981c001SMika Westerberg 			value |= PADCFG1_TERM_1K << PADCFG1_TERM_SHIFT;
6217981c001SMika Westerberg 			break;
6227981c001SMika Westerberg 		default:
6237981c001SMika Westerberg 			ret = -EINVAL;
6247981c001SMika Westerberg 		}
6257981c001SMika Westerberg 
6267981c001SMika Westerberg 		break;
6277981c001SMika Westerberg 
6287981c001SMika Westerberg 	case PIN_CONFIG_BIAS_PULL_DOWN:
6297981c001SMika Westerberg 		value &= ~(PADCFG1_TERM_UP | PADCFG1_TERM_MASK);
6307981c001SMika Westerberg 
6317981c001SMika Westerberg 		switch (arg) {
6327981c001SMika Westerberg 		case 20000:
6337981c001SMika Westerberg 			value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT;
6347981c001SMika Westerberg 			break;
6357981c001SMika Westerberg 		case 5000:
6367981c001SMika Westerberg 			value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT;
6377981c001SMika Westerberg 			break;
63804cc058fSMika Westerberg 		case 1000:
639aa1dd80fSDan Carpenter 			if (!(community->features & PINCTRL_FEATURE_1K_PD)) {
640aa1dd80fSDan Carpenter 				ret = -EINVAL;
641aa1dd80fSDan Carpenter 				break;
642aa1dd80fSDan Carpenter 			}
64304cc058fSMika Westerberg 			value |= PADCFG1_TERM_1K << PADCFG1_TERM_SHIFT;
64404cc058fSMika Westerberg 			break;
6457981c001SMika Westerberg 		default:
6467981c001SMika Westerberg 			ret = -EINVAL;
6477981c001SMika Westerberg 		}
6487981c001SMika Westerberg 
6497981c001SMika Westerberg 		break;
6507981c001SMika Westerberg 	}
6517981c001SMika Westerberg 
6527981c001SMika Westerberg 	if (!ret)
6537981c001SMika Westerberg 		writel(value, padcfg1);
6547981c001SMika Westerberg 
65527d9098cSMika Westerberg 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
6567981c001SMika Westerberg 
6577981c001SMika Westerberg 	return ret;
6587981c001SMika Westerberg }
6597981c001SMika Westerberg 
66004035f7fSAndy Shevchenko static int intel_config_set_debounce(struct intel_pinctrl *pctrl,
66104035f7fSAndy Shevchenko 				     unsigned int pin, unsigned int debounce)
662e57725eaSMika Westerberg {
663e57725eaSMika Westerberg 	void __iomem *padcfg0, *padcfg2;
664e57725eaSMika Westerberg 	unsigned long flags;
665e57725eaSMika Westerberg 	u32 value0, value2;
666e57725eaSMika Westerberg 	int ret = 0;
667e57725eaSMika Westerberg 
668e57725eaSMika Westerberg 	padcfg2 = intel_get_padcfg(pctrl, pin, PADCFG2);
669e57725eaSMika Westerberg 	if (!padcfg2)
670e57725eaSMika Westerberg 		return -ENOTSUPP;
671e57725eaSMika Westerberg 
672e57725eaSMika Westerberg 	padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
673e57725eaSMika Westerberg 
674e57725eaSMika Westerberg 	raw_spin_lock_irqsave(&pctrl->lock, flags);
675e57725eaSMika Westerberg 
676e57725eaSMika Westerberg 	value0 = readl(padcfg0);
677e57725eaSMika Westerberg 	value2 = readl(padcfg2);
678e57725eaSMika Westerberg 
679e57725eaSMika Westerberg 	/* Disable glitch filter and debouncer */
680e57725eaSMika Westerberg 	value0 &= ~PADCFG0_PREGFRXSEL;
681e57725eaSMika Westerberg 	value2 &= ~(PADCFG2_DEBEN | PADCFG2_DEBOUNCE_MASK);
682e57725eaSMika Westerberg 
683e57725eaSMika Westerberg 	if (debounce) {
684e57725eaSMika Westerberg 		unsigned long v;
685e57725eaSMika Westerberg 
686*6a33a1d6SAndy Shevchenko 		v = order_base_2(debounce * NSEC_PER_USEC / DEBOUNCE_PERIOD_NSEC);
687e57725eaSMika Westerberg 		if (v < 3 || v > 15) {
688e57725eaSMika Westerberg 			ret = -EINVAL;
689e57725eaSMika Westerberg 			goto exit_unlock;
690e57725eaSMika Westerberg 		} else {
691e57725eaSMika Westerberg 			/* Enable glitch filter and debouncer */
692e57725eaSMika Westerberg 			value0 |= PADCFG0_PREGFRXSEL;
693e57725eaSMika Westerberg 			value2 |= v << PADCFG2_DEBOUNCE_SHIFT;
694e57725eaSMika Westerberg 			value2 |= PADCFG2_DEBEN;
695e57725eaSMika Westerberg 		}
696e57725eaSMika Westerberg 	}
697e57725eaSMika Westerberg 
698e57725eaSMika Westerberg 	writel(value0, padcfg0);
699e57725eaSMika Westerberg 	writel(value2, padcfg2);
700e57725eaSMika Westerberg 
701e57725eaSMika Westerberg exit_unlock:
702e57725eaSMika Westerberg 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
703e57725eaSMika Westerberg 
704e57725eaSMika Westerberg 	return ret;
705e57725eaSMika Westerberg }
706e57725eaSMika Westerberg 
70704035f7fSAndy Shevchenko static int intel_config_set(struct pinctrl_dev *pctldev, unsigned int pin,
70804035f7fSAndy Shevchenko 			  unsigned long *configs, unsigned int nconfigs)
7097981c001SMika Westerberg {
7107981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
7117981c001SMika Westerberg 	int i, ret;
7127981c001SMika Westerberg 
7137981c001SMika Westerberg 	if (!intel_pad_usable(pctrl, pin))
7147981c001SMika Westerberg 		return -ENOTSUPP;
7157981c001SMika Westerberg 
7167981c001SMika Westerberg 	for (i = 0; i < nconfigs; i++) {
7177981c001SMika Westerberg 		switch (pinconf_to_config_param(configs[i])) {
7187981c001SMika Westerberg 		case PIN_CONFIG_BIAS_DISABLE:
7197981c001SMika Westerberg 		case PIN_CONFIG_BIAS_PULL_UP:
7207981c001SMika Westerberg 		case PIN_CONFIG_BIAS_PULL_DOWN:
7217981c001SMika Westerberg 			ret = intel_config_set_pull(pctrl, pin, configs[i]);
7227981c001SMika Westerberg 			if (ret)
7237981c001SMika Westerberg 				return ret;
7247981c001SMika Westerberg 			break;
7257981c001SMika Westerberg 
726e57725eaSMika Westerberg 		case PIN_CONFIG_INPUT_DEBOUNCE:
727e57725eaSMika Westerberg 			ret = intel_config_set_debounce(pctrl, pin,
728e57725eaSMika Westerberg 				pinconf_to_config_argument(configs[i]));
729e57725eaSMika Westerberg 			if (ret)
730e57725eaSMika Westerberg 				return ret;
731e57725eaSMika Westerberg 			break;
732e57725eaSMika Westerberg 
7337981c001SMika Westerberg 		default:
7347981c001SMika Westerberg 			return -ENOTSUPP;
7357981c001SMika Westerberg 		}
7367981c001SMika Westerberg 	}
7377981c001SMika Westerberg 
7387981c001SMika Westerberg 	return 0;
7397981c001SMika Westerberg }
7407981c001SMika Westerberg 
7417981c001SMika Westerberg static const struct pinconf_ops intel_pinconf_ops = {
7427981c001SMika Westerberg 	.is_generic = true,
7437981c001SMika Westerberg 	.pin_config_get = intel_config_get,
7447981c001SMika Westerberg 	.pin_config_set = intel_config_set,
7457981c001SMika Westerberg };
7467981c001SMika Westerberg 
7477981c001SMika Westerberg static const struct pinctrl_desc intel_pinctrl_desc = {
7487981c001SMika Westerberg 	.pctlops = &intel_pinctrl_ops,
7497981c001SMika Westerberg 	.pmxops = &intel_pinmux_ops,
7507981c001SMika Westerberg 	.confops = &intel_pinconf_ops,
7517981c001SMika Westerberg 	.owner = THIS_MODULE,
7527981c001SMika Westerberg };
7537981c001SMika Westerberg 
754a60eac32SMika Westerberg /**
755a60eac32SMika Westerberg  * intel_gpio_to_pin() - Translate from GPIO offset to pin number
756a60eac32SMika Westerberg  * @pctrl: Pinctrl structure
757a60eac32SMika Westerberg  * @offset: GPIO offset from gpiolib
758946ffefcSAndy Shevchenko  * @community: Community is filled here if not %NULL
759a60eac32SMika Westerberg  * @padgrp: Pad group is filled here if not %NULL
760a60eac32SMika Westerberg  *
761a60eac32SMika Westerberg  * When coming through gpiolib irqchip, the GPIO offset is not
762a60eac32SMika Westerberg  * automatically translated to pinctrl pin number. This function can be
763a60eac32SMika Westerberg  * used to find out the corresponding pinctrl pin.
764a60eac32SMika Westerberg  */
76504035f7fSAndy Shevchenko static int intel_gpio_to_pin(struct intel_pinctrl *pctrl, unsigned int offset,
766a60eac32SMika Westerberg 			     const struct intel_community **community,
767a60eac32SMika Westerberg 			     const struct intel_padgroup **padgrp)
768a60eac32SMika Westerberg {
769a60eac32SMika Westerberg 	int i;
770a60eac32SMika Westerberg 
771a60eac32SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
772a60eac32SMika Westerberg 		const struct intel_community *comm = &pctrl->communities[i];
773a60eac32SMika Westerberg 		int j;
774a60eac32SMika Westerberg 
775a60eac32SMika Westerberg 		for (j = 0; j < comm->ngpps; j++) {
776a60eac32SMika Westerberg 			const struct intel_padgroup *pgrp = &comm->gpps[j];
777a60eac32SMika Westerberg 
778a60eac32SMika Westerberg 			if (pgrp->gpio_base < 0)
779a60eac32SMika Westerberg 				continue;
780a60eac32SMika Westerberg 
781a60eac32SMika Westerberg 			if (offset >= pgrp->gpio_base &&
782a60eac32SMika Westerberg 			    offset < pgrp->gpio_base + pgrp->size) {
783a60eac32SMika Westerberg 				int pin;
784a60eac32SMika Westerberg 
785a60eac32SMika Westerberg 				pin = pgrp->base + offset - pgrp->gpio_base;
786a60eac32SMika Westerberg 				if (community)
787a60eac32SMika Westerberg 					*community = comm;
788a60eac32SMika Westerberg 				if (padgrp)
789a60eac32SMika Westerberg 					*padgrp = pgrp;
790a60eac32SMika Westerberg 
791a60eac32SMika Westerberg 				return pin;
792a60eac32SMika Westerberg 			}
793a60eac32SMika Westerberg 		}
794a60eac32SMika Westerberg 	}
795a60eac32SMika Westerberg 
796a60eac32SMika Westerberg 	return -EINVAL;
797a60eac32SMika Westerberg }
798a60eac32SMika Westerberg 
79904035f7fSAndy Shevchenko static int intel_gpio_get(struct gpio_chip *chip, unsigned int offset)
80055aedef5SAndy Shevchenko {
80196147db1SMika Westerberg 	struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
80296147db1SMika Westerberg 	void __iomem *reg;
80396147db1SMika Westerberg 	u32 padcfg0;
80455aedef5SAndy Shevchenko 	int pin;
80555aedef5SAndy Shevchenko 
80696147db1SMika Westerberg 	pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL);
80796147db1SMika Westerberg 	if (pin < 0)
80896147db1SMika Westerberg 		return -EINVAL;
80996147db1SMika Westerberg 
81096147db1SMika Westerberg 	reg = intel_get_padcfg(pctrl, pin, PADCFG0);
81196147db1SMika Westerberg 	if (!reg)
81296147db1SMika Westerberg 		return -EINVAL;
81396147db1SMika Westerberg 
81496147db1SMika Westerberg 	padcfg0 = readl(reg);
81596147db1SMika Westerberg 	if (!(padcfg0 & PADCFG0_GPIOTXDIS))
81696147db1SMika Westerberg 		return !!(padcfg0 & PADCFG0_GPIOTXSTATE);
81796147db1SMika Westerberg 
81896147db1SMika Westerberg 	return !!(padcfg0 & PADCFG0_GPIORXSTATE);
81955aedef5SAndy Shevchenko }
82055aedef5SAndy Shevchenko 
82104035f7fSAndy Shevchenko static void intel_gpio_set(struct gpio_chip *chip, unsigned int offset,
82204035f7fSAndy Shevchenko 			   int value)
82396147db1SMika Westerberg {
82496147db1SMika Westerberg 	struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
82596147db1SMika Westerberg 	unsigned long flags;
82696147db1SMika Westerberg 	void __iomem *reg;
82796147db1SMika Westerberg 	u32 padcfg0;
82896147db1SMika Westerberg 	int pin;
82996147db1SMika Westerberg 
83096147db1SMika Westerberg 	pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL);
83196147db1SMika Westerberg 	if (pin < 0)
83296147db1SMika Westerberg 		return;
83396147db1SMika Westerberg 
83496147db1SMika Westerberg 	reg = intel_get_padcfg(pctrl, pin, PADCFG0);
83596147db1SMika Westerberg 	if (!reg)
83696147db1SMika Westerberg 		return;
83796147db1SMika Westerberg 
83896147db1SMika Westerberg 	raw_spin_lock_irqsave(&pctrl->lock, flags);
83996147db1SMika Westerberg 	padcfg0 = readl(reg);
84096147db1SMika Westerberg 	if (value)
84196147db1SMika Westerberg 		padcfg0 |= PADCFG0_GPIOTXSTATE;
84296147db1SMika Westerberg 	else
84396147db1SMika Westerberg 		padcfg0 &= ~PADCFG0_GPIOTXSTATE;
84496147db1SMika Westerberg 	writel(padcfg0, reg);
84596147db1SMika Westerberg 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
84696147db1SMika Westerberg }
84796147db1SMika Westerberg 
84896147db1SMika Westerberg static int intel_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
84996147db1SMika Westerberg {
85096147db1SMika Westerberg 	struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
85196147db1SMika Westerberg 	void __iomem *reg;
85296147db1SMika Westerberg 	u32 padcfg0;
85396147db1SMika Westerberg 	int pin;
85496147db1SMika Westerberg 
85596147db1SMika Westerberg 	pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL);
85696147db1SMika Westerberg 	if (pin < 0)
85796147db1SMika Westerberg 		return -EINVAL;
85896147db1SMika Westerberg 
85996147db1SMika Westerberg 	reg = intel_get_padcfg(pctrl, pin, PADCFG0);
86096147db1SMika Westerberg 	if (!reg)
86196147db1SMika Westerberg 		return -EINVAL;
86296147db1SMika Westerberg 
86396147db1SMika Westerberg 	padcfg0 = readl(reg);
86496147db1SMika Westerberg 
86596147db1SMika Westerberg 	if (padcfg0 & PADCFG0_PMODE_MASK)
86696147db1SMika Westerberg 		return -EINVAL;
86796147db1SMika Westerberg 
86896147db1SMika Westerberg 	return !!(padcfg0 & PADCFG0_GPIOTXDIS);
86996147db1SMika Westerberg }
87096147db1SMika Westerberg 
87104035f7fSAndy Shevchenko static int intel_gpio_direction_input(struct gpio_chip *chip, unsigned int offset)
87296147db1SMika Westerberg {
87396147db1SMika Westerberg 	return pinctrl_gpio_direction_input(chip->base + offset);
87496147db1SMika Westerberg }
87596147db1SMika Westerberg 
87604035f7fSAndy Shevchenko static int intel_gpio_direction_output(struct gpio_chip *chip, unsigned int offset,
87796147db1SMika Westerberg 				       int value)
87896147db1SMika Westerberg {
87996147db1SMika Westerberg 	intel_gpio_set(chip, offset, value);
88096147db1SMika Westerberg 	return pinctrl_gpio_direction_output(chip->base + offset);
88196147db1SMika Westerberg }
88296147db1SMika Westerberg 
88396147db1SMika Westerberg static const struct gpio_chip intel_gpio_chip = {
88496147db1SMika Westerberg 	.owner = THIS_MODULE,
88596147db1SMika Westerberg 	.request = gpiochip_generic_request,
88696147db1SMika Westerberg 	.free = gpiochip_generic_free,
88796147db1SMika Westerberg 	.get_direction = intel_gpio_get_direction,
88896147db1SMika Westerberg 	.direction_input = intel_gpio_direction_input,
88996147db1SMika Westerberg 	.direction_output = intel_gpio_direction_output,
89096147db1SMika Westerberg 	.get = intel_gpio_get,
89196147db1SMika Westerberg 	.set = intel_gpio_set,
89296147db1SMika Westerberg 	.set_config = gpiochip_generic_config,
89396147db1SMika Westerberg };
89496147db1SMika Westerberg 
8957981c001SMika Westerberg static void intel_gpio_irq_ack(struct irq_data *d)
8967981c001SMika Westerberg {
8977981c001SMika Westerberg 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
898acfd4c63SLinus Walleij 	struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
8997981c001SMika Westerberg 	const struct intel_community *community;
900919eb475SMika Westerberg 	const struct intel_padgroup *padgrp;
901a60eac32SMika Westerberg 	int pin;
9027981c001SMika Westerberg 
903a60eac32SMika Westerberg 	pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), &community, &padgrp);
904a60eac32SMika Westerberg 	if (pin >= 0) {
90504035f7fSAndy Shevchenko 		unsigned int gpp, gpp_offset, is_offset;
906919eb475SMika Westerberg 
907919eb475SMika Westerberg 		gpp = padgrp->reg_num;
908919eb475SMika Westerberg 		gpp_offset = padgroup_offset(padgrp, pin);
909cf769bd8SMika Westerberg 		is_offset = community->is_offset + gpp * 4;
910919eb475SMika Westerberg 
911919eb475SMika Westerberg 		raw_spin_lock(&pctrl->lock);
912cf769bd8SMika Westerberg 		writel(BIT(gpp_offset), community->regs + is_offset);
91327d9098cSMika Westerberg 		raw_spin_unlock(&pctrl->lock);
9147981c001SMika Westerberg 	}
915919eb475SMika Westerberg }
9167981c001SMika Westerberg 
9177981c001SMika Westerberg static void intel_gpio_irq_mask_unmask(struct irq_data *d, bool mask)
9187981c001SMika Westerberg {
9197981c001SMika Westerberg 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
920acfd4c63SLinus Walleij 	struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
9217981c001SMika Westerberg 	const struct intel_community *community;
922919eb475SMika Westerberg 	const struct intel_padgroup *padgrp;
923a60eac32SMika Westerberg 	int pin;
924a60eac32SMika Westerberg 
925a60eac32SMika Westerberg 	pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), &community, &padgrp);
926a60eac32SMika Westerberg 	if (pin >= 0) {
92704035f7fSAndy Shevchenko 		unsigned int gpp, gpp_offset;
928919eb475SMika Westerberg 		unsigned long flags;
929670784fbSKai-Heng Feng 		void __iomem *reg, *is;
9307981c001SMika Westerberg 		u32 value;
9317981c001SMika Westerberg 
932919eb475SMika Westerberg 		gpp = padgrp->reg_num;
933919eb475SMika Westerberg 		gpp_offset = padgroup_offset(padgrp, pin);
934919eb475SMika Westerberg 
9357981c001SMika Westerberg 		reg = community->regs + community->ie_offset + gpp * 4;
936670784fbSKai-Heng Feng 		is = community->regs + community->is_offset + gpp * 4;
937919eb475SMika Westerberg 
938919eb475SMika Westerberg 		raw_spin_lock_irqsave(&pctrl->lock, flags);
939670784fbSKai-Heng Feng 
940670784fbSKai-Heng Feng 		/* Clear interrupt status first to avoid unexpected interrupt */
941670784fbSKai-Heng Feng 		writel(BIT(gpp_offset), is);
942670784fbSKai-Heng Feng 
9437981c001SMika Westerberg 		value = readl(reg);
9447981c001SMika Westerberg 		if (mask)
9457981c001SMika Westerberg 			value &= ~BIT(gpp_offset);
9467981c001SMika Westerberg 		else
9477981c001SMika Westerberg 			value |= BIT(gpp_offset);
9487981c001SMika Westerberg 		writel(value, reg);
94927d9098cSMika Westerberg 		raw_spin_unlock_irqrestore(&pctrl->lock, flags);
9507981c001SMika Westerberg 	}
951919eb475SMika Westerberg }
9527981c001SMika Westerberg 
9537981c001SMika Westerberg static void intel_gpio_irq_mask(struct irq_data *d)
9547981c001SMika Westerberg {
9557981c001SMika Westerberg 	intel_gpio_irq_mask_unmask(d, true);
9567981c001SMika Westerberg }
9577981c001SMika Westerberg 
9587981c001SMika Westerberg static void intel_gpio_irq_unmask(struct irq_data *d)
9597981c001SMika Westerberg {
9607981c001SMika Westerberg 	intel_gpio_irq_mask_unmask(d, false);
9617981c001SMika Westerberg }
9627981c001SMika Westerberg 
96304035f7fSAndy Shevchenko static int intel_gpio_irq_type(struct irq_data *d, unsigned int type)
9647981c001SMika Westerberg {
9657981c001SMika Westerberg 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
966acfd4c63SLinus Walleij 	struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
96704035f7fSAndy Shevchenko 	unsigned int pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), NULL, NULL);
9687981c001SMika Westerberg 	unsigned long flags;
9697981c001SMika Westerberg 	void __iomem *reg;
9707981c001SMika Westerberg 	u32 value;
9717981c001SMika Westerberg 
9727981c001SMika Westerberg 	reg = intel_get_padcfg(pctrl, pin, PADCFG0);
9737981c001SMika Westerberg 	if (!reg)
9747981c001SMika Westerberg 		return -EINVAL;
9757981c001SMika Westerberg 
9764341e8a5SMika Westerberg 	/*
9774341e8a5SMika Westerberg 	 * If the pin is in ACPI mode it is still usable as a GPIO but it
9784341e8a5SMika Westerberg 	 * cannot be used as IRQ because GPI_IS status bit will not be
9794341e8a5SMika Westerberg 	 * updated by the host controller hardware.
9804341e8a5SMika Westerberg 	 */
9814341e8a5SMika Westerberg 	if (intel_pad_acpi_mode(pctrl, pin)) {
9824341e8a5SMika Westerberg 		dev_warn(pctrl->dev, "pin %u cannot be used as IRQ\n", pin);
9834341e8a5SMika Westerberg 		return -EPERM;
9844341e8a5SMika Westerberg 	}
9854341e8a5SMika Westerberg 
98627d9098cSMika Westerberg 	raw_spin_lock_irqsave(&pctrl->lock, flags);
9877981c001SMika Westerberg 
988f5a26acfSMika Westerberg 	intel_gpio_set_gpio_mode(reg);
989f5a26acfSMika Westerberg 
9907981c001SMika Westerberg 	value = readl(reg);
9917981c001SMika Westerberg 
9927981c001SMika Westerberg 	value &= ~(PADCFG0_RXEVCFG_MASK | PADCFG0_RXINV);
9937981c001SMika Westerberg 
9947981c001SMika Westerberg 	if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
9957981c001SMika Westerberg 		value |= PADCFG0_RXEVCFG_EDGE_BOTH << PADCFG0_RXEVCFG_SHIFT;
9967981c001SMika Westerberg 	} else if (type & IRQ_TYPE_EDGE_FALLING) {
9977981c001SMika Westerberg 		value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT;
9987981c001SMika Westerberg 		value |= PADCFG0_RXINV;
9997981c001SMika Westerberg 	} else if (type & IRQ_TYPE_EDGE_RISING) {
10007981c001SMika Westerberg 		value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT;
1001bf380cfaSQipeng Zha 	} else if (type & IRQ_TYPE_LEVEL_MASK) {
1002bf380cfaSQipeng Zha 		if (type & IRQ_TYPE_LEVEL_LOW)
10037981c001SMika Westerberg 			value |= PADCFG0_RXINV;
10047981c001SMika Westerberg 	} else {
10057981c001SMika Westerberg 		value |= PADCFG0_RXEVCFG_DISABLED << PADCFG0_RXEVCFG_SHIFT;
10067981c001SMika Westerberg 	}
10077981c001SMika Westerberg 
10087981c001SMika Westerberg 	writel(value, reg);
10097981c001SMika Westerberg 
10107981c001SMika Westerberg 	if (type & IRQ_TYPE_EDGE_BOTH)
1011fc756bcdSThomas Gleixner 		irq_set_handler_locked(d, handle_edge_irq);
10127981c001SMika Westerberg 	else if (type & IRQ_TYPE_LEVEL_MASK)
1013fc756bcdSThomas Gleixner 		irq_set_handler_locked(d, handle_level_irq);
10147981c001SMika Westerberg 
101527d9098cSMika Westerberg 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
10167981c001SMika Westerberg 
10177981c001SMika Westerberg 	return 0;
10187981c001SMika Westerberg }
10197981c001SMika Westerberg 
10207981c001SMika Westerberg static int intel_gpio_irq_wake(struct irq_data *d, unsigned int on)
10217981c001SMika Westerberg {
10227981c001SMika Westerberg 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1023acfd4c63SLinus Walleij 	struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
102404035f7fSAndy Shevchenko 	unsigned int pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), NULL, NULL);
10257981c001SMika Westerberg 
10267981c001SMika Westerberg 	if (on)
102701dabe91SNilesh Bacchewar 		enable_irq_wake(pctrl->irq);
10287981c001SMika Westerberg 	else
102901dabe91SNilesh Bacchewar 		disable_irq_wake(pctrl->irq);
10309a520fd9SAndy Shevchenko 
10317981c001SMika Westerberg 	dev_dbg(pctrl->dev, "%sable wake for pin %u\n", on ? "en" : "dis", pin);
10327981c001SMika Westerberg 	return 0;
10337981c001SMika Westerberg }
10347981c001SMika Westerberg 
1035193b40c8SMika Westerberg static irqreturn_t intel_gpio_community_irq_handler(struct intel_pinctrl *pctrl,
10367981c001SMika Westerberg 	const struct intel_community *community)
10377981c001SMika Westerberg {
1038193b40c8SMika Westerberg 	struct gpio_chip *gc = &pctrl->chip;
1039193b40c8SMika Westerberg 	irqreturn_t ret = IRQ_NONE;
10407981c001SMika Westerberg 	int gpp;
10417981c001SMika Westerberg 
10427981c001SMika Westerberg 	for (gpp = 0; gpp < community->ngpps; gpp++) {
1043919eb475SMika Westerberg 		const struct intel_padgroup *padgrp = &community->gpps[gpp];
10447981c001SMika Westerberg 		unsigned long pending, enabled, gpp_offset;
10457981c001SMika Westerberg 
1046cf769bd8SMika Westerberg 		pending = readl(community->regs + community->is_offset +
1047cf769bd8SMika Westerberg 				padgrp->reg_num * 4);
10487981c001SMika Westerberg 		enabled = readl(community->regs + community->ie_offset +
1049919eb475SMika Westerberg 				padgrp->reg_num * 4);
10507981c001SMika Westerberg 
10517981c001SMika Westerberg 		/* Only interrupts that are enabled */
10527981c001SMika Westerberg 		pending &= enabled;
10537981c001SMika Westerberg 
1054919eb475SMika Westerberg 		for_each_set_bit(gpp_offset, &pending, padgrp->size) {
1055a60eac32SMika Westerberg 			unsigned irq;
10567981c001SMika Westerberg 
1057f0fbe7bcSThierry Reding 			irq = irq_find_mapping(gc->irq.domain,
1058a60eac32SMika Westerberg 					       padgrp->gpio_base + gpp_offset);
10597981c001SMika Westerberg 			generic_handle_irq(irq);
1060193b40c8SMika Westerberg 
1061193b40c8SMika Westerberg 			ret |= IRQ_HANDLED;
10627981c001SMika Westerberg 		}
10637981c001SMika Westerberg 	}
10647981c001SMika Westerberg 
1065193b40c8SMika Westerberg 	return ret;
1066193b40c8SMika Westerberg }
1067193b40c8SMika Westerberg 
1068193b40c8SMika Westerberg static irqreturn_t intel_gpio_irq(int irq, void *data)
10697981c001SMika Westerberg {
1070193b40c8SMika Westerberg 	const struct intel_community *community;
1071193b40c8SMika Westerberg 	struct intel_pinctrl *pctrl = data;
1072193b40c8SMika Westerberg 	irqreturn_t ret = IRQ_NONE;
10737981c001SMika Westerberg 	int i;
10747981c001SMika Westerberg 
10757981c001SMika Westerberg 	/* Need to check all communities for pending interrupts */
1076193b40c8SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
1077193b40c8SMika Westerberg 		community = &pctrl->communities[i];
1078193b40c8SMika Westerberg 		ret |= intel_gpio_community_irq_handler(pctrl, community);
1079193b40c8SMika Westerberg 	}
10807981c001SMika Westerberg 
1081193b40c8SMika Westerberg 	return ret;
10827981c001SMika Westerberg }
10837981c001SMika Westerberg 
10847981c001SMika Westerberg static struct irq_chip intel_gpio_irqchip = {
10857981c001SMika Westerberg 	.name = "intel-gpio",
10867981c001SMika Westerberg 	.irq_ack = intel_gpio_irq_ack,
10877981c001SMika Westerberg 	.irq_mask = intel_gpio_irq_mask,
10887981c001SMika Westerberg 	.irq_unmask = intel_gpio_irq_unmask,
10897981c001SMika Westerberg 	.irq_set_type = intel_gpio_irq_type,
10907981c001SMika Westerberg 	.irq_set_wake = intel_gpio_irq_wake,
10915ff56b01SRushikesh S Kadam 	.flags = IRQCHIP_MASK_ON_SUSPEND,
10927981c001SMika Westerberg };
10937981c001SMika Westerberg 
1094a60eac32SMika Westerberg static int intel_gpio_add_pin_ranges(struct intel_pinctrl *pctrl,
1095a60eac32SMika Westerberg 				     const struct intel_community *community)
1096a60eac32SMika Westerberg {
109733b6cb58SColin Ian King 	int ret = 0, i;
1098a60eac32SMika Westerberg 
1099a60eac32SMika Westerberg 	for (i = 0; i < community->ngpps; i++) {
1100a60eac32SMika Westerberg 		const struct intel_padgroup *gpp = &community->gpps[i];
1101a60eac32SMika Westerberg 
1102a60eac32SMika Westerberg 		if (gpp->gpio_base < 0)
1103a60eac32SMika Westerberg 			continue;
1104a60eac32SMika Westerberg 
1105a60eac32SMika Westerberg 		ret = gpiochip_add_pin_range(&pctrl->chip, dev_name(pctrl->dev),
1106a60eac32SMika Westerberg 					     gpp->gpio_base, gpp->base,
1107a60eac32SMika Westerberg 					     gpp->size);
1108a60eac32SMika Westerberg 		if (ret)
1109a60eac32SMika Westerberg 			return ret;
1110a60eac32SMika Westerberg 	}
1111a60eac32SMika Westerberg 
1112a60eac32SMika Westerberg 	return ret;
1113a60eac32SMika Westerberg }
1114a60eac32SMika Westerberg 
1115a60eac32SMika Westerberg static unsigned intel_gpio_ngpio(const struct intel_pinctrl *pctrl)
1116a60eac32SMika Westerberg {
1117a60eac32SMika Westerberg 	const struct intel_community *community;
111804035f7fSAndy Shevchenko 	unsigned int ngpio = 0;
1119a60eac32SMika Westerberg 	int i, j;
1120a60eac32SMika Westerberg 
1121a60eac32SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
1122a60eac32SMika Westerberg 		community = &pctrl->communities[i];
1123a60eac32SMika Westerberg 		for (j = 0; j < community->ngpps; j++) {
1124a60eac32SMika Westerberg 			const struct intel_padgroup *gpp = &community->gpps[j];
1125a60eac32SMika Westerberg 
1126a60eac32SMika Westerberg 			if (gpp->gpio_base < 0)
1127a60eac32SMika Westerberg 				continue;
1128a60eac32SMika Westerberg 
1129a60eac32SMika Westerberg 			if (gpp->gpio_base + gpp->size > ngpio)
1130a60eac32SMika Westerberg 				ngpio = gpp->gpio_base + gpp->size;
1131a60eac32SMika Westerberg 		}
1132a60eac32SMika Westerberg 	}
1133a60eac32SMika Westerberg 
1134a60eac32SMika Westerberg 	return ngpio;
1135a60eac32SMika Westerberg }
1136a60eac32SMika Westerberg 
11377981c001SMika Westerberg static int intel_gpio_probe(struct intel_pinctrl *pctrl, int irq)
11387981c001SMika Westerberg {
1139a60eac32SMika Westerberg 	int ret, i;
11407981c001SMika Westerberg 
11417981c001SMika Westerberg 	pctrl->chip = intel_gpio_chip;
11427981c001SMika Westerberg 
1143a60eac32SMika Westerberg 	pctrl->chip.ngpio = intel_gpio_ngpio(pctrl);
11447981c001SMika Westerberg 	pctrl->chip.label = dev_name(pctrl->dev);
114558383c78SLinus Walleij 	pctrl->chip.parent = pctrl->dev;
11467981c001SMika Westerberg 	pctrl->chip.base = -1;
114701dabe91SNilesh Bacchewar 	pctrl->irq = irq;
11487981c001SMika Westerberg 
1149f25c3aa9SMika Westerberg 	ret = devm_gpiochip_add_data(pctrl->dev, &pctrl->chip, pctrl);
11507981c001SMika Westerberg 	if (ret) {
11517981c001SMika Westerberg 		dev_err(pctrl->dev, "failed to register gpiochip\n");
11527981c001SMika Westerberg 		return ret;
11537981c001SMika Westerberg 	}
11547981c001SMika Westerberg 
1155a60eac32SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
1156a60eac32SMika Westerberg 		struct intel_community *community = &pctrl->communities[i];
1157a60eac32SMika Westerberg 
1158a60eac32SMika Westerberg 		ret = intel_gpio_add_pin_ranges(pctrl, community);
11597981c001SMika Westerberg 		if (ret) {
11607981c001SMika Westerberg 			dev_err(pctrl->dev, "failed to add GPIO pin range\n");
1161f25c3aa9SMika Westerberg 			return ret;
1162193b40c8SMika Westerberg 		}
1163a60eac32SMika Westerberg 	}
1164193b40c8SMika Westerberg 
1165193b40c8SMika Westerberg 	/*
1166193b40c8SMika Westerberg 	 * We need to request the interrupt here (instead of providing chip
1167193b40c8SMika Westerberg 	 * to the irq directly) because on some platforms several GPIO
1168193b40c8SMika Westerberg 	 * controllers share the same interrupt line.
1169193b40c8SMika Westerberg 	 */
11701a7d1cb8SMika Westerberg 	ret = devm_request_irq(pctrl->dev, irq, intel_gpio_irq,
11711a7d1cb8SMika Westerberg 			       IRQF_SHARED | IRQF_NO_THREAD,
1172193b40c8SMika Westerberg 			       dev_name(pctrl->dev), pctrl);
1173193b40c8SMika Westerberg 	if (ret) {
1174193b40c8SMika Westerberg 		dev_err(pctrl->dev, "failed to request interrupt\n");
1175f25c3aa9SMika Westerberg 		return ret;
11767981c001SMika Westerberg 	}
11777981c001SMika Westerberg 
11787981c001SMika Westerberg 	ret = gpiochip_irqchip_add(&pctrl->chip, &intel_gpio_irqchip, 0,
11793ae02c14SAndy Shevchenko 				   handle_bad_irq, IRQ_TYPE_NONE);
11807981c001SMika Westerberg 	if (ret) {
11817981c001SMika Westerberg 		dev_err(pctrl->dev, "failed to add irqchip\n");
1182f25c3aa9SMika Westerberg 		return ret;
11837981c001SMika Westerberg 	}
11847981c001SMika Westerberg 
11857981c001SMika Westerberg 	gpiochip_set_chained_irqchip(&pctrl->chip, &intel_gpio_irqchip, irq,
1186193b40c8SMika Westerberg 				     NULL);
11877981c001SMika Westerberg 	return 0;
11887981c001SMika Westerberg }
11897981c001SMika Westerberg 
1190919eb475SMika Westerberg static int intel_pinctrl_add_padgroups(struct intel_pinctrl *pctrl,
1191919eb475SMika Westerberg 				       struct intel_community *community)
1192919eb475SMika Westerberg {
1193919eb475SMika Westerberg 	struct intel_padgroup *gpps;
119404035f7fSAndy Shevchenko 	unsigned int npins = community->npins;
119504035f7fSAndy Shevchenko 	unsigned int padown_num = 0;
1196919eb475SMika Westerberg 	size_t ngpps, i;
1197919eb475SMika Westerberg 
1198919eb475SMika Westerberg 	if (community->gpps)
1199919eb475SMika Westerberg 		ngpps = community->ngpps;
1200919eb475SMika Westerberg 	else
1201919eb475SMika Westerberg 		ngpps = DIV_ROUND_UP(community->npins, community->gpp_size);
1202919eb475SMika Westerberg 
1203919eb475SMika Westerberg 	gpps = devm_kcalloc(pctrl->dev, ngpps, sizeof(*gpps), GFP_KERNEL);
1204919eb475SMika Westerberg 	if (!gpps)
1205919eb475SMika Westerberg 		return -ENOMEM;
1206919eb475SMika Westerberg 
1207919eb475SMika Westerberg 	for (i = 0; i < ngpps; i++) {
1208919eb475SMika Westerberg 		if (community->gpps) {
1209919eb475SMika Westerberg 			gpps[i] = community->gpps[i];
1210919eb475SMika Westerberg 		} else {
121104035f7fSAndy Shevchenko 			unsigned int gpp_size = community->gpp_size;
1212919eb475SMika Westerberg 
1213919eb475SMika Westerberg 			gpps[i].reg_num = i;
1214919eb475SMika Westerberg 			gpps[i].base = community->pin_base + i * gpp_size;
1215919eb475SMika Westerberg 			gpps[i].size = min(gpp_size, npins);
1216919eb475SMika Westerberg 			npins -= gpps[i].size;
1217919eb475SMika Westerberg 		}
1218919eb475SMika Westerberg 
1219919eb475SMika Westerberg 		if (gpps[i].size > 32)
1220919eb475SMika Westerberg 			return -EINVAL;
1221919eb475SMika Westerberg 
1222a60eac32SMika Westerberg 		if (!gpps[i].gpio_base)
1223a60eac32SMika Westerberg 			gpps[i].gpio_base = gpps[i].base;
1224a60eac32SMika Westerberg 
1225919eb475SMika Westerberg 		gpps[i].padown_num = padown_num;
1226919eb475SMika Westerberg 
1227919eb475SMika Westerberg 		/*
1228919eb475SMika Westerberg 		 * In older hardware the number of padown registers per
1229919eb475SMika Westerberg 		 * group is fixed regardless of the group size.
1230919eb475SMika Westerberg 		 */
1231919eb475SMika Westerberg 		if (community->gpp_num_padown_regs)
1232919eb475SMika Westerberg 			padown_num += community->gpp_num_padown_regs;
1233919eb475SMika Westerberg 		else
1234919eb475SMika Westerberg 			padown_num += DIV_ROUND_UP(gpps[i].size * 4, 32);
1235919eb475SMika Westerberg 	}
1236919eb475SMika Westerberg 
1237919eb475SMika Westerberg 	community->ngpps = ngpps;
1238919eb475SMika Westerberg 	community->gpps = gpps;
1239919eb475SMika Westerberg 
1240919eb475SMika Westerberg 	return 0;
1241919eb475SMika Westerberg }
1242919eb475SMika Westerberg 
12437981c001SMika Westerberg static int intel_pinctrl_pm_init(struct intel_pinctrl *pctrl)
12447981c001SMika Westerberg {
12457981c001SMika Westerberg #ifdef CONFIG_PM_SLEEP
12467981c001SMika Westerberg 	const struct intel_pinctrl_soc_data *soc = pctrl->soc;
12477981c001SMika Westerberg 	struct intel_community_context *communities;
12487981c001SMika Westerberg 	struct intel_pad_context *pads;
12497981c001SMika Westerberg 	int i;
12507981c001SMika Westerberg 
12517981c001SMika Westerberg 	pads = devm_kcalloc(pctrl->dev, soc->npins, sizeof(*pads), GFP_KERNEL);
12527981c001SMika Westerberg 	if (!pads)
12537981c001SMika Westerberg 		return -ENOMEM;
12547981c001SMika Westerberg 
12557981c001SMika Westerberg 	communities = devm_kcalloc(pctrl->dev, pctrl->ncommunities,
12567981c001SMika Westerberg 				   sizeof(*communities), GFP_KERNEL);
12577981c001SMika Westerberg 	if (!communities)
12587981c001SMika Westerberg 		return -ENOMEM;
12597981c001SMika Westerberg 
12607981c001SMika Westerberg 
12617981c001SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
12627981c001SMika Westerberg 		struct intel_community *community = &pctrl->communities[i];
1263a0a5f766SChris Chiu 		u32 *intmask, *hostown;
12647981c001SMika Westerberg 
12657981c001SMika Westerberg 		intmask = devm_kcalloc(pctrl->dev, community->ngpps,
12667981c001SMika Westerberg 				       sizeof(*intmask), GFP_KERNEL);
12677981c001SMika Westerberg 		if (!intmask)
12687981c001SMika Westerberg 			return -ENOMEM;
12697981c001SMika Westerberg 
12707981c001SMika Westerberg 		communities[i].intmask = intmask;
1271a0a5f766SChris Chiu 
1272a0a5f766SChris Chiu 		hostown = devm_kcalloc(pctrl->dev, community->ngpps,
1273a0a5f766SChris Chiu 				       sizeof(*hostown), GFP_KERNEL);
1274a0a5f766SChris Chiu 		if (!hostown)
1275a0a5f766SChris Chiu 			return -ENOMEM;
1276a0a5f766SChris Chiu 
1277a0a5f766SChris Chiu 		communities[i].hostown = hostown;
12787981c001SMika Westerberg 	}
12797981c001SMika Westerberg 
12807981c001SMika Westerberg 	pctrl->context.pads = pads;
12817981c001SMika Westerberg 	pctrl->context.communities = communities;
12827981c001SMika Westerberg #endif
12837981c001SMika Westerberg 
12847981c001SMika Westerberg 	return 0;
12857981c001SMika Westerberg }
12867981c001SMika Westerberg 
12870dd519e3SAndy Shevchenko static int intel_pinctrl_probe(struct platform_device *pdev,
12887981c001SMika Westerberg 			       const struct intel_pinctrl_soc_data *soc_data)
12897981c001SMika Westerberg {
12907981c001SMika Westerberg 	struct intel_pinctrl *pctrl;
12917981c001SMika Westerberg 	int i, ret, irq;
12927981c001SMika Westerberg 
12937981c001SMika Westerberg 	if (!soc_data)
12947981c001SMika Westerberg 		return -EINVAL;
12957981c001SMika Westerberg 
12967981c001SMika Westerberg 	pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL);
12977981c001SMika Westerberg 	if (!pctrl)
12987981c001SMika Westerberg 		return -ENOMEM;
12997981c001SMika Westerberg 
13007981c001SMika Westerberg 	pctrl->dev = &pdev->dev;
13017981c001SMika Westerberg 	pctrl->soc = soc_data;
130227d9098cSMika Westerberg 	raw_spin_lock_init(&pctrl->lock);
13037981c001SMika Westerberg 
13047981c001SMika Westerberg 	/*
13057981c001SMika Westerberg 	 * Make a copy of the communities which we can use to hold pointers
13067981c001SMika Westerberg 	 * to the registers.
13077981c001SMika Westerberg 	 */
13087981c001SMika Westerberg 	pctrl->ncommunities = pctrl->soc->ncommunities;
13097981c001SMika Westerberg 	pctrl->communities = devm_kcalloc(&pdev->dev, pctrl->ncommunities,
13107981c001SMika Westerberg 				  sizeof(*pctrl->communities), GFP_KERNEL);
13117981c001SMika Westerberg 	if (!pctrl->communities)
13127981c001SMika Westerberg 		return -ENOMEM;
13137981c001SMika Westerberg 
13147981c001SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
13157981c001SMika Westerberg 		struct intel_community *community = &pctrl->communities[i];
13167981c001SMika Westerberg 		void __iomem *regs;
13177981c001SMika Westerberg 		u32 padbar;
13187981c001SMika Westerberg 
13197981c001SMika Westerberg 		*community = pctrl->soc->communities[i];
13207981c001SMika Westerberg 
13219d5b6a95SAndy Shevchenko 		regs = devm_platform_ioremap_resource(pdev, community->barno);
13227981c001SMika Westerberg 		if (IS_ERR(regs))
13237981c001SMika Westerberg 			return PTR_ERR(regs);
13247981c001SMika Westerberg 
1325e57725eaSMika Westerberg 		/*
1326e57725eaSMika Westerberg 		 * Determine community features based on the revision if
1327e57725eaSMika Westerberg 		 * not specified already.
1328e57725eaSMika Westerberg 		 */
1329e57725eaSMika Westerberg 		if (!community->features) {
1330e57725eaSMika Westerberg 			u32 rev;
1331e57725eaSMika Westerberg 
1332e57725eaSMika Westerberg 			rev = (readl(regs + REVID) & REVID_MASK) >> REVID_SHIFT;
133304cc058fSMika Westerberg 			if (rev >= 0x94) {
1334e57725eaSMika Westerberg 				community->features |= PINCTRL_FEATURE_DEBOUNCE;
133504cc058fSMika Westerberg 				community->features |= PINCTRL_FEATURE_1K_PD;
133604cc058fSMika Westerberg 			}
1337e57725eaSMika Westerberg 		}
1338e57725eaSMika Westerberg 
13397981c001SMika Westerberg 		/* Read offset of the pad configuration registers */
13407981c001SMika Westerberg 		padbar = readl(regs + PADBAR);
13417981c001SMika Westerberg 
13427981c001SMika Westerberg 		community->regs = regs;
13437981c001SMika Westerberg 		community->pad_regs = regs + padbar;
1344919eb475SMika Westerberg 
1345919eb475SMika Westerberg 		ret = intel_pinctrl_add_padgroups(pctrl, community);
1346919eb475SMika Westerberg 		if (ret)
1347919eb475SMika Westerberg 			return ret;
13487981c001SMika Westerberg 	}
13497981c001SMika Westerberg 
13507981c001SMika Westerberg 	irq = platform_get_irq(pdev, 0);
13517981c001SMika Westerberg 	if (irq < 0) {
13527981c001SMika Westerberg 		dev_err(&pdev->dev, "failed to get interrupt number\n");
13537981c001SMika Westerberg 		return irq;
13547981c001SMika Westerberg 	}
13557981c001SMika Westerberg 
13567981c001SMika Westerberg 	ret = intel_pinctrl_pm_init(pctrl);
13577981c001SMika Westerberg 	if (ret)
13587981c001SMika Westerberg 		return ret;
13597981c001SMika Westerberg 
13607981c001SMika Westerberg 	pctrl->pctldesc = intel_pinctrl_desc;
13617981c001SMika Westerberg 	pctrl->pctldesc.name = dev_name(&pdev->dev);
13627981c001SMika Westerberg 	pctrl->pctldesc.pins = pctrl->soc->pins;
13637981c001SMika Westerberg 	pctrl->pctldesc.npins = pctrl->soc->npins;
13647981c001SMika Westerberg 
136554d46cd7SLaxman Dewangan 	pctrl->pctldev = devm_pinctrl_register(&pdev->dev, &pctrl->pctldesc,
136654d46cd7SLaxman Dewangan 					       pctrl);
1367323de9efSMasahiro Yamada 	if (IS_ERR(pctrl->pctldev)) {
13687981c001SMika Westerberg 		dev_err(&pdev->dev, "failed to register pinctrl driver\n");
1369323de9efSMasahiro Yamada 		return PTR_ERR(pctrl->pctldev);
13707981c001SMika Westerberg 	}
13717981c001SMika Westerberg 
13727981c001SMika Westerberg 	ret = intel_gpio_probe(pctrl, irq);
137354d46cd7SLaxman Dewangan 	if (ret)
13747981c001SMika Westerberg 		return ret;
13757981c001SMika Westerberg 
13767981c001SMika Westerberg 	platform_set_drvdata(pdev, pctrl);
13777981c001SMika Westerberg 
13787981c001SMika Westerberg 	return 0;
13797981c001SMika Westerberg }
13807981c001SMika Westerberg 
138170c263c4SAndy Shevchenko int intel_pinctrl_probe_by_hid(struct platform_device *pdev)
138270c263c4SAndy Shevchenko {
138370c263c4SAndy Shevchenko 	const struct intel_pinctrl_soc_data *data;
138470c263c4SAndy Shevchenko 
138570c263c4SAndy Shevchenko 	data = device_get_match_data(&pdev->dev);
138670c263c4SAndy Shevchenko 	return intel_pinctrl_probe(pdev, data);
138770c263c4SAndy Shevchenko }
138870c263c4SAndy Shevchenko EXPORT_SYMBOL_GPL(intel_pinctrl_probe_by_hid);
138970c263c4SAndy Shevchenko 
1390924cf800SAndy Shevchenko int intel_pinctrl_probe_by_uid(struct platform_device *pdev)
1391924cf800SAndy Shevchenko {
1392924cf800SAndy Shevchenko 	const struct intel_pinctrl_soc_data *data = NULL;
1393924cf800SAndy Shevchenko 	const struct intel_pinctrl_soc_data **table;
1394924cf800SAndy Shevchenko 	struct acpi_device *adev;
1395924cf800SAndy Shevchenko 	unsigned int i;
1396924cf800SAndy Shevchenko 
1397924cf800SAndy Shevchenko 	adev = ACPI_COMPANION(&pdev->dev);
1398924cf800SAndy Shevchenko 	if (adev) {
1399924cf800SAndy Shevchenko 		const void *match = device_get_match_data(&pdev->dev);
1400924cf800SAndy Shevchenko 
1401924cf800SAndy Shevchenko 		table = (const struct intel_pinctrl_soc_data **)match;
1402924cf800SAndy Shevchenko 		for (i = 0; table[i]; i++) {
1403924cf800SAndy Shevchenko 			if (!strcmp(adev->pnp.unique_id, table[i]->uid)) {
1404924cf800SAndy Shevchenko 				data = table[i];
1405924cf800SAndy Shevchenko 				break;
1406924cf800SAndy Shevchenko 			}
1407924cf800SAndy Shevchenko 		}
1408924cf800SAndy Shevchenko 	} else {
1409924cf800SAndy Shevchenko 		const struct platform_device_id *id;
1410924cf800SAndy Shevchenko 
1411924cf800SAndy Shevchenko 		id = platform_get_device_id(pdev);
1412924cf800SAndy Shevchenko 		if (!id)
1413924cf800SAndy Shevchenko 			return -ENODEV;
1414924cf800SAndy Shevchenko 
1415924cf800SAndy Shevchenko 		table = (const struct intel_pinctrl_soc_data **)id->driver_data;
1416924cf800SAndy Shevchenko 		data = table[pdev->id];
1417924cf800SAndy Shevchenko 	}
1418924cf800SAndy Shevchenko 
1419924cf800SAndy Shevchenko 	return intel_pinctrl_probe(pdev, data);
1420924cf800SAndy Shevchenko }
1421924cf800SAndy Shevchenko EXPORT_SYMBOL_GPL(intel_pinctrl_probe_by_uid);
1422924cf800SAndy Shevchenko 
14237981c001SMika Westerberg #ifdef CONFIG_PM_SLEEP
142404035f7fSAndy Shevchenko static bool intel_pinctrl_should_save(struct intel_pinctrl *pctrl, unsigned int pin)
1425c538b943SMika Westerberg {
1426c538b943SMika Westerberg 	const struct pin_desc *pd = pin_desc_get(pctrl->pctldev, pin);
1427c538b943SMika Westerberg 
1428c538b943SMika Westerberg 	if (!pd || !intel_pad_usable(pctrl, pin))
1429c538b943SMika Westerberg 		return false;
1430c538b943SMika Westerberg 
1431c538b943SMika Westerberg 	/*
1432c538b943SMika Westerberg 	 * Only restore the pin if it is actually in use by the kernel (or
1433c538b943SMika Westerberg 	 * by userspace). It is possible that some pins are used by the
1434c538b943SMika Westerberg 	 * BIOS during resume and those are not always locked down so leave
1435c538b943SMika Westerberg 	 * them alone.
1436c538b943SMika Westerberg 	 */
1437c538b943SMika Westerberg 	if (pd->mux_owner || pd->gpio_owner ||
1438c538b943SMika Westerberg 	    gpiochip_line_is_irq(&pctrl->chip, pin))
1439c538b943SMika Westerberg 		return true;
1440c538b943SMika Westerberg 
1441c538b943SMika Westerberg 	return false;
1442c538b943SMika Westerberg }
1443c538b943SMika Westerberg 
14442fef3276SBinbin Wu int intel_pinctrl_suspend_noirq(struct device *dev)
14457981c001SMika Westerberg {
1446cb035d74SWolfram Sang 	struct intel_pinctrl *pctrl = dev_get_drvdata(dev);
14477981c001SMika Westerberg 	struct intel_community_context *communities;
14487981c001SMika Westerberg 	struct intel_pad_context *pads;
14497981c001SMika Westerberg 	int i;
14507981c001SMika Westerberg 
14517981c001SMika Westerberg 	pads = pctrl->context.pads;
14527981c001SMika Westerberg 	for (i = 0; i < pctrl->soc->npins; i++) {
14537981c001SMika Westerberg 		const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
1454e57725eaSMika Westerberg 		void __iomem *padcfg;
14557981c001SMika Westerberg 		u32 val;
14567981c001SMika Westerberg 
1457c538b943SMika Westerberg 		if (!intel_pinctrl_should_save(pctrl, desc->number))
14587981c001SMika Westerberg 			continue;
14597981c001SMika Westerberg 
14607981c001SMika Westerberg 		val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG0));
14617981c001SMika Westerberg 		pads[i].padcfg0 = val & ~PADCFG0_GPIORXSTATE;
14627981c001SMika Westerberg 		val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG1));
14637981c001SMika Westerberg 		pads[i].padcfg1 = val;
1464e57725eaSMika Westerberg 
1465e57725eaSMika Westerberg 		padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG2);
1466e57725eaSMika Westerberg 		if (padcfg)
1467e57725eaSMika Westerberg 			pads[i].padcfg2 = readl(padcfg);
14687981c001SMika Westerberg 	}
14697981c001SMika Westerberg 
14707981c001SMika Westerberg 	communities = pctrl->context.communities;
14717981c001SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
14727981c001SMika Westerberg 		struct intel_community *community = &pctrl->communities[i];
14737981c001SMika Westerberg 		void __iomem *base;
147404035f7fSAndy Shevchenko 		unsigned int gpp;
14757981c001SMika Westerberg 
14767981c001SMika Westerberg 		base = community->regs + community->ie_offset;
14777981c001SMika Westerberg 		for (gpp = 0; gpp < community->ngpps; gpp++)
14787981c001SMika Westerberg 			communities[i].intmask[gpp] = readl(base + gpp * 4);
1479a0a5f766SChris Chiu 
1480a0a5f766SChris Chiu 		base = community->regs + community->hostown_offset;
1481a0a5f766SChris Chiu 		for (gpp = 0; gpp < community->ngpps; gpp++)
1482a0a5f766SChris Chiu 			communities[i].hostown[gpp] = readl(base + gpp * 4);
14837981c001SMika Westerberg 	}
14847981c001SMika Westerberg 
14857981c001SMika Westerberg 	return 0;
14867981c001SMika Westerberg }
14872fef3276SBinbin Wu EXPORT_SYMBOL_GPL(intel_pinctrl_suspend_noirq);
14887981c001SMika Westerberg 
1489f487bbf3SMika Westerberg static void intel_gpio_irq_init(struct intel_pinctrl *pctrl)
1490f487bbf3SMika Westerberg {
1491f487bbf3SMika Westerberg 	size_t i;
1492f487bbf3SMika Westerberg 
1493f487bbf3SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
1494f487bbf3SMika Westerberg 		const struct intel_community *community;
1495f487bbf3SMika Westerberg 		void __iomem *base;
149604035f7fSAndy Shevchenko 		unsigned int gpp;
1497f487bbf3SMika Westerberg 
1498f487bbf3SMika Westerberg 		community = &pctrl->communities[i];
1499f487bbf3SMika Westerberg 		base = community->regs;
1500f487bbf3SMika Westerberg 
1501f487bbf3SMika Westerberg 		for (gpp = 0; gpp < community->ngpps; gpp++) {
1502f487bbf3SMika Westerberg 			/* Mask and clear all interrupts */
1503f487bbf3SMika Westerberg 			writel(0, base + community->ie_offset + gpp * 4);
1504cf769bd8SMika Westerberg 			writel(0xffff, base + community->is_offset + gpp * 4);
1505f487bbf3SMika Westerberg 		}
1506f487bbf3SMika Westerberg 	}
1507f487bbf3SMika Westerberg }
1508f487bbf3SMika Westerberg 
1509a0a5f766SChris Chiu static u32
1510a0a5f766SChris Chiu intel_gpio_is_requested(struct gpio_chip *chip, int base, unsigned int size)
1511a0a5f766SChris Chiu {
1512a0a5f766SChris Chiu 	u32 requested = 0;
1513a0a5f766SChris Chiu 	unsigned int i;
1514a0a5f766SChris Chiu 
1515a0a5f766SChris Chiu 	for (i = 0; i < size; i++)
1516a0a5f766SChris Chiu 		if (gpiochip_is_requested(chip, base + i))
1517a0a5f766SChris Chiu 			requested |= BIT(i);
1518a0a5f766SChris Chiu 
1519a0a5f766SChris Chiu 	return requested;
1520a0a5f766SChris Chiu }
1521a0a5f766SChris Chiu 
1522a0a5f766SChris Chiu static u32
1523a0a5f766SChris Chiu intel_gpio_update_pad_mode(void __iomem *hostown, u32 mask, u32 value)
1524a0a5f766SChris Chiu {
15255f61d951SAndy Shevchenko 	u32 curr, updated;
1526a0a5f766SChris Chiu 
15275f61d951SAndy Shevchenko 	curr = readl(hostown);
15285f61d951SAndy Shevchenko 	updated = (curr & ~mask) | (value & mask);
1529a0a5f766SChris Chiu 	writel(updated, hostown);
15305f61d951SAndy Shevchenko 
1531a0a5f766SChris Chiu 	return curr;
1532a0a5f766SChris Chiu }
1533a0a5f766SChris Chiu 
15342fef3276SBinbin Wu int intel_pinctrl_resume_noirq(struct device *dev)
15357981c001SMika Westerberg {
1536cb035d74SWolfram Sang 	struct intel_pinctrl *pctrl = dev_get_drvdata(dev);
15377981c001SMika Westerberg 	const struct intel_community_context *communities;
15387981c001SMika Westerberg 	const struct intel_pad_context *pads;
15397981c001SMika Westerberg 	int i;
15407981c001SMika Westerberg 
15417981c001SMika Westerberg 	/* Mask all interrupts */
15427981c001SMika Westerberg 	intel_gpio_irq_init(pctrl);
15437981c001SMika Westerberg 
15447981c001SMika Westerberg 	pads = pctrl->context.pads;
15457981c001SMika Westerberg 	for (i = 0; i < pctrl->soc->npins; i++) {
15467981c001SMika Westerberg 		const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
15477981c001SMika Westerberg 		void __iomem *padcfg;
15487981c001SMika Westerberg 		u32 val;
15497981c001SMika Westerberg 
1550c538b943SMika Westerberg 		if (!intel_pinctrl_should_save(pctrl, desc->number))
15517981c001SMika Westerberg 			continue;
15527981c001SMika Westerberg 
15537981c001SMika Westerberg 		padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG0);
15547981c001SMika Westerberg 		val = readl(padcfg) & ~PADCFG0_GPIORXSTATE;
15557981c001SMika Westerberg 		if (val != pads[i].padcfg0) {
15567981c001SMika Westerberg 			writel(pads[i].padcfg0, padcfg);
15577981c001SMika Westerberg 			dev_dbg(dev, "restored pin %u padcfg0 %#08x\n",
15587981c001SMika Westerberg 				desc->number, readl(padcfg));
15597981c001SMika Westerberg 		}
15607981c001SMika Westerberg 
15617981c001SMika Westerberg 		padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG1);
15627981c001SMika Westerberg 		val = readl(padcfg);
15637981c001SMika Westerberg 		if (val != pads[i].padcfg1) {
15647981c001SMika Westerberg 			writel(pads[i].padcfg1, padcfg);
15657981c001SMika Westerberg 			dev_dbg(dev, "restored pin %u padcfg1 %#08x\n",
15667981c001SMika Westerberg 				desc->number, readl(padcfg));
15677981c001SMika Westerberg 		}
1568e57725eaSMika Westerberg 
1569e57725eaSMika Westerberg 		padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG2);
1570e57725eaSMika Westerberg 		if (padcfg) {
1571e57725eaSMika Westerberg 			val = readl(padcfg);
1572e57725eaSMika Westerberg 			if (val != pads[i].padcfg2) {
1573e57725eaSMika Westerberg 				writel(pads[i].padcfg2, padcfg);
1574e57725eaSMika Westerberg 				dev_dbg(dev, "restored pin %u padcfg2 %#08x\n",
1575e57725eaSMika Westerberg 					desc->number, readl(padcfg));
1576e57725eaSMika Westerberg 			}
1577e57725eaSMika Westerberg 		}
15787981c001SMika Westerberg 	}
15797981c001SMika Westerberg 
15807981c001SMika Westerberg 	communities = pctrl->context.communities;
15817981c001SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
15827981c001SMika Westerberg 		struct intel_community *community = &pctrl->communities[i];
15837981c001SMika Westerberg 		void __iomem *base;
158404035f7fSAndy Shevchenko 		unsigned int gpp;
15857981c001SMika Westerberg 
15867981c001SMika Westerberg 		base = community->regs + community->ie_offset;
15877981c001SMika Westerberg 		for (gpp = 0; gpp < community->ngpps; gpp++) {
15887981c001SMika Westerberg 			writel(communities[i].intmask[gpp], base + gpp * 4);
15897981c001SMika Westerberg 			dev_dbg(dev, "restored mask %d/%u %#08x\n", i, gpp,
15907981c001SMika Westerberg 				readl(base + gpp * 4));
15917981c001SMika Westerberg 		}
1592a0a5f766SChris Chiu 
1593a0a5f766SChris Chiu 		base = community->regs + community->hostown_offset;
1594a0a5f766SChris Chiu 		for (gpp = 0; gpp < community->ngpps; gpp++) {
1595a0a5f766SChris Chiu 			const struct intel_padgroup *padgrp = &community->gpps[gpp];
1596a0a5f766SChris Chiu 			u32 requested = 0, value = 0;
1597a0a5f766SChris Chiu 			u32 saved = communities[i].hostown[gpp];
1598a0a5f766SChris Chiu 
1599a0a5f766SChris Chiu 			if (padgrp->gpio_base < 0)
1600a0a5f766SChris Chiu 				continue;
1601a0a5f766SChris Chiu 
1602a0a5f766SChris Chiu 			requested = intel_gpio_is_requested(&pctrl->chip,
1603a0a5f766SChris Chiu 					padgrp->gpio_base, padgrp->size);
1604a0a5f766SChris Chiu 			value = intel_gpio_update_pad_mode(base + gpp * 4,
1605a0a5f766SChris Chiu 					requested, saved);
1606a0a5f766SChris Chiu 			if ((value ^ saved) & requested) {
1607a0a5f766SChris Chiu 				dev_warn(dev, "restore hostown %d/%u %#8x->%#8x\n",
1608a0a5f766SChris Chiu 					i, gpp, value, saved);
1609a0a5f766SChris Chiu 			}
1610a0a5f766SChris Chiu 		}
16117981c001SMika Westerberg 	}
16127981c001SMika Westerberg 
16137981c001SMika Westerberg 	return 0;
16147981c001SMika Westerberg }
16152fef3276SBinbin Wu EXPORT_SYMBOL_GPL(intel_pinctrl_resume_noirq);
16167981c001SMika Westerberg #endif
16177981c001SMika Westerberg 
16187981c001SMika Westerberg MODULE_AUTHOR("Mathias Nyman <mathias.nyman@linux.intel.com>");
16197981c001SMika Westerberg MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
16207981c001SMika Westerberg MODULE_DESCRIPTION("Intel pinctrl/GPIO core driver");
16217981c001SMika Westerberg MODULE_LICENSE("GPL v2");
1622