xref: /openbmc/linux/drivers/pinctrl/intel/pinctrl-intel.c (revision 924cf800574ffd53469d245637607a8b0768ad69)
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 
10*924cf800SAndy Shevchenko #include <linux/acpi.h>
117981c001SMika Westerberg #include <linux/module.h>
12193b40c8SMika Westerberg #include <linux/interrupt.h>
137981c001SMika Westerberg #include <linux/gpio/driver.h>
14e57725eaSMika Westerberg #include <linux/log2.h>
157981c001SMika Westerberg #include <linux/platform_device.h>
16*924cf800SAndy Shevchenko #include <linux/property.h>
17*924cf800SAndy Shevchenko 
187981c001SMika Westerberg #include <linux/pinctrl/pinctrl.h>
197981c001SMika Westerberg #include <linux/pinctrl/pinmux.h>
207981c001SMika Westerberg #include <linux/pinctrl/pinconf.h>
217981c001SMika Westerberg #include <linux/pinctrl/pinconf-generic.h>
227981c001SMika Westerberg 
23c538b943SMika Westerberg #include "../core.h"
247981c001SMika Westerberg #include "pinctrl-intel.h"
257981c001SMika Westerberg 
267981c001SMika Westerberg /* Offset from regs */
27e57725eaSMika Westerberg #define REVID				0x000
28e57725eaSMika Westerberg #define REVID_SHIFT			16
29e57725eaSMika Westerberg #define REVID_MASK			GENMASK(31, 16)
30e57725eaSMika Westerberg 
317981c001SMika Westerberg #define PADBAR				0x00c
327981c001SMika Westerberg #define GPI_IS				0x100
337981c001SMika Westerberg 
347981c001SMika Westerberg #define PADOWN_BITS			4
357981c001SMika Westerberg #define PADOWN_SHIFT(p)			((p) % 8 * PADOWN_BITS)
367981c001SMika Westerberg #define PADOWN_MASK(p)			(0xf << 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
427981c001SMika Westerberg #define PADCFG0_RXEVCFG_MASK		(3 << PADCFG0_RXEVCFG_SHIFT)
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
547981c001SMika Westerberg #define PADCFG0_PMODE_MASK		(0xf << PADCFG0_PMODE_SHIFT)
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
637981c001SMika Westerberg #define PADCFG1_TERM_MASK		(7 << PADCFG1_TERM_SHIFT)
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 
74e57725eaSMika Westerberg #define DEBOUNCE_PERIOD			31250 /* ns */
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;
847981c001SMika Westerberg };
857981c001SMika Westerberg 
867981c001SMika Westerberg struct intel_pinctrl_context {
877981c001SMika Westerberg 	struct intel_pad_context *pads;
887981c001SMika Westerberg 	struct intel_community_context *communities;
897981c001SMika Westerberg };
907981c001SMika Westerberg 
917981c001SMika Westerberg /**
927981c001SMika Westerberg  * struct intel_pinctrl - Intel pinctrl private structure
937981c001SMika Westerberg  * @dev: Pointer to the device structure
947981c001SMika Westerberg  * @lock: Lock to serialize register access
957981c001SMika Westerberg  * @pctldesc: Pin controller description
967981c001SMika Westerberg  * @pctldev: Pointer to the pin controller device
977981c001SMika Westerberg  * @chip: GPIO chip in this pin controller
987981c001SMika Westerberg  * @soc: SoC/PCH specific pin configuration data
997981c001SMika Westerberg  * @communities: All communities in this pin controller
1007981c001SMika Westerberg  * @ncommunities: Number of communities in this pin controller
1017981c001SMika Westerberg  * @context: Configuration saved over system sleep
10201dabe91SNilesh Bacchewar  * @irq: pinctrl/GPIO chip irq number
1037981c001SMika Westerberg  */
1047981c001SMika Westerberg struct intel_pinctrl {
1057981c001SMika Westerberg 	struct device *dev;
10627d9098cSMika Westerberg 	raw_spinlock_t lock;
1077981c001SMika Westerberg 	struct pinctrl_desc pctldesc;
1087981c001SMika Westerberg 	struct pinctrl_dev *pctldev;
1097981c001SMika Westerberg 	struct gpio_chip chip;
1107981c001SMika Westerberg 	const struct intel_pinctrl_soc_data *soc;
1117981c001SMika Westerberg 	struct intel_community *communities;
1127981c001SMika Westerberg 	size_t ncommunities;
1137981c001SMika Westerberg 	struct intel_pinctrl_context context;
11401dabe91SNilesh Bacchewar 	int irq;
1157981c001SMika Westerberg };
1167981c001SMika Westerberg 
1177981c001SMika Westerberg #define pin_to_padno(c, p)	((p) - (c)->pin_base)
118919eb475SMika Westerberg #define padgroup_offset(g, p)	((p) - (g)->base)
1197981c001SMika Westerberg 
1207981c001SMika Westerberg static struct intel_community *intel_get_community(struct intel_pinctrl *pctrl,
1217981c001SMika Westerberg 						   unsigned pin)
1227981c001SMika Westerberg {
1237981c001SMika Westerberg 	struct intel_community *community;
1247981c001SMika Westerberg 	int i;
1257981c001SMika Westerberg 
1267981c001SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
1277981c001SMika Westerberg 		community = &pctrl->communities[i];
1287981c001SMika Westerberg 		if (pin >= community->pin_base &&
1297981c001SMika Westerberg 		    pin < community->pin_base + community->npins)
1307981c001SMika Westerberg 			return community;
1317981c001SMika Westerberg 	}
1327981c001SMika Westerberg 
1337981c001SMika Westerberg 	dev_warn(pctrl->dev, "failed to find community for pin %u\n", pin);
1347981c001SMika Westerberg 	return NULL;
1357981c001SMika Westerberg }
1367981c001SMika Westerberg 
137919eb475SMika Westerberg static const struct intel_padgroup *
138919eb475SMika Westerberg intel_community_get_padgroup(const struct intel_community *community,
139919eb475SMika Westerberg 			     unsigned pin)
140919eb475SMika Westerberg {
141919eb475SMika Westerberg 	int i;
142919eb475SMika Westerberg 
143919eb475SMika Westerberg 	for (i = 0; i < community->ngpps; i++) {
144919eb475SMika Westerberg 		const struct intel_padgroup *padgrp = &community->gpps[i];
145919eb475SMika Westerberg 
146919eb475SMika Westerberg 		if (pin >= padgrp->base && pin < padgrp->base + padgrp->size)
147919eb475SMika Westerberg 			return padgrp;
148919eb475SMika Westerberg 	}
149919eb475SMika Westerberg 
150919eb475SMika Westerberg 	return NULL;
151919eb475SMika Westerberg }
152919eb475SMika Westerberg 
1537981c001SMika Westerberg static void __iomem *intel_get_padcfg(struct intel_pinctrl *pctrl, unsigned pin,
1547981c001SMika Westerberg 				      unsigned reg)
1557981c001SMika Westerberg {
1567981c001SMika Westerberg 	const struct intel_community *community;
1577981c001SMika Westerberg 	unsigned padno;
158e57725eaSMika Westerberg 	size_t nregs;
1597981c001SMika Westerberg 
1607981c001SMika Westerberg 	community = intel_get_community(pctrl, pin);
1617981c001SMika Westerberg 	if (!community)
1627981c001SMika Westerberg 		return NULL;
1637981c001SMika Westerberg 
1647981c001SMika Westerberg 	padno = pin_to_padno(community, pin);
165e57725eaSMika Westerberg 	nregs = (community->features & PINCTRL_FEATURE_DEBOUNCE) ? 4 : 2;
166e57725eaSMika Westerberg 
167e57725eaSMika Westerberg 	if (reg == PADCFG2 && !(community->features & PINCTRL_FEATURE_DEBOUNCE))
168e57725eaSMika Westerberg 		return NULL;
169e57725eaSMika Westerberg 
170e57725eaSMika Westerberg 	return community->pad_regs + reg + padno * nregs * 4;
1717981c001SMika Westerberg }
1727981c001SMika Westerberg 
1737981c001SMika Westerberg static bool intel_pad_owned_by_host(struct intel_pinctrl *pctrl, unsigned pin)
1747981c001SMika Westerberg {
1757981c001SMika Westerberg 	const struct intel_community *community;
176919eb475SMika Westerberg 	const struct intel_padgroup *padgrp;
177919eb475SMika Westerberg 	unsigned gpp, offset, gpp_offset;
1787981c001SMika Westerberg 	void __iomem *padown;
1797981c001SMika Westerberg 
1807981c001SMika Westerberg 	community = intel_get_community(pctrl, pin);
1817981c001SMika Westerberg 	if (!community)
1827981c001SMika Westerberg 		return false;
1837981c001SMika Westerberg 	if (!community->padown_offset)
1847981c001SMika Westerberg 		return true;
1857981c001SMika Westerberg 
186919eb475SMika Westerberg 	padgrp = intel_community_get_padgroup(community, pin);
187919eb475SMika Westerberg 	if (!padgrp)
188919eb475SMika Westerberg 		return false;
189919eb475SMika Westerberg 
190919eb475SMika Westerberg 	gpp_offset = padgroup_offset(padgrp, pin);
191919eb475SMika Westerberg 	gpp = PADOWN_GPP(gpp_offset);
192919eb475SMika Westerberg 	offset = community->padown_offset + padgrp->padown_num * 4 + gpp * 4;
1937981c001SMika Westerberg 	padown = community->regs + offset;
1947981c001SMika Westerberg 
195919eb475SMika Westerberg 	return !(readl(padown) & PADOWN_MASK(gpp_offset));
1967981c001SMika Westerberg }
1977981c001SMika Westerberg 
1984341e8a5SMika Westerberg static bool intel_pad_acpi_mode(struct intel_pinctrl *pctrl, unsigned pin)
1997981c001SMika Westerberg {
2007981c001SMika Westerberg 	const struct intel_community *community;
201919eb475SMika Westerberg 	const struct intel_padgroup *padgrp;
202919eb475SMika Westerberg 	unsigned offset, gpp_offset;
2037981c001SMika Westerberg 	void __iomem *hostown;
2047981c001SMika Westerberg 
2057981c001SMika Westerberg 	community = intel_get_community(pctrl, pin);
2067981c001SMika Westerberg 	if (!community)
2077981c001SMika Westerberg 		return true;
2087981c001SMika Westerberg 	if (!community->hostown_offset)
2097981c001SMika Westerberg 		return false;
2107981c001SMika Westerberg 
211919eb475SMika Westerberg 	padgrp = intel_community_get_padgroup(community, pin);
212919eb475SMika Westerberg 	if (!padgrp)
213919eb475SMika Westerberg 		return true;
214919eb475SMika Westerberg 
215919eb475SMika Westerberg 	gpp_offset = padgroup_offset(padgrp, pin);
216919eb475SMika Westerberg 	offset = community->hostown_offset + padgrp->reg_num * 4;
2177981c001SMika Westerberg 	hostown = community->regs + offset;
2187981c001SMika Westerberg 
219919eb475SMika Westerberg 	return !(readl(hostown) & BIT(gpp_offset));
2207981c001SMika Westerberg }
2217981c001SMika Westerberg 
2227981c001SMika Westerberg static bool intel_pad_locked(struct intel_pinctrl *pctrl, unsigned pin)
2237981c001SMika Westerberg {
2247981c001SMika Westerberg 	struct intel_community *community;
225919eb475SMika Westerberg 	const struct intel_padgroup *padgrp;
226919eb475SMika Westerberg 	unsigned offset, gpp_offset;
2277981c001SMika Westerberg 	u32 value;
2287981c001SMika Westerberg 
2297981c001SMika Westerberg 	community = intel_get_community(pctrl, pin);
2307981c001SMika Westerberg 	if (!community)
2317981c001SMika Westerberg 		return true;
2327981c001SMika Westerberg 	if (!community->padcfglock_offset)
2337981c001SMika Westerberg 		return false;
2347981c001SMika Westerberg 
235919eb475SMika Westerberg 	padgrp = intel_community_get_padgroup(community, pin);
236919eb475SMika Westerberg 	if (!padgrp)
237919eb475SMika Westerberg 		return true;
238919eb475SMika Westerberg 
239919eb475SMika Westerberg 	gpp_offset = padgroup_offset(padgrp, pin);
2407981c001SMika Westerberg 
2417981c001SMika Westerberg 	/*
2427981c001SMika Westerberg 	 * If PADCFGLOCK and PADCFGLOCKTX bits are both clear for this pad,
2437981c001SMika Westerberg 	 * the pad is considered unlocked. Any other case means that it is
2447981c001SMika Westerberg 	 * either fully or partially locked and we don't touch it.
2457981c001SMika Westerberg 	 */
246919eb475SMika Westerberg 	offset = community->padcfglock_offset + padgrp->reg_num * 8;
2477981c001SMika Westerberg 	value = readl(community->regs + offset);
248919eb475SMika Westerberg 	if (value & BIT(gpp_offset))
2497981c001SMika Westerberg 		return true;
2507981c001SMika Westerberg 
251919eb475SMika Westerberg 	offset = community->padcfglock_offset + 4 + padgrp->reg_num * 8;
2527981c001SMika Westerberg 	value = readl(community->regs + offset);
253919eb475SMika Westerberg 	if (value & BIT(gpp_offset))
2547981c001SMika Westerberg 		return true;
2557981c001SMika Westerberg 
2567981c001SMika Westerberg 	return false;
2577981c001SMika Westerberg }
2587981c001SMika Westerberg 
2597981c001SMika Westerberg static bool intel_pad_usable(struct intel_pinctrl *pctrl, unsigned pin)
2607981c001SMika Westerberg {
2617981c001SMika Westerberg 	return intel_pad_owned_by_host(pctrl, pin) &&
2627981c001SMika Westerberg 		!intel_pad_locked(pctrl, pin);
2637981c001SMika Westerberg }
2647981c001SMika Westerberg 
2657981c001SMika Westerberg static int intel_get_groups_count(struct pinctrl_dev *pctldev)
2667981c001SMika Westerberg {
2677981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
2687981c001SMika Westerberg 
2697981c001SMika Westerberg 	return pctrl->soc->ngroups;
2707981c001SMika Westerberg }
2717981c001SMika Westerberg 
2727981c001SMika Westerberg static const char *intel_get_group_name(struct pinctrl_dev *pctldev,
2737981c001SMika Westerberg 				      unsigned group)
2747981c001SMika Westerberg {
2757981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
2767981c001SMika Westerberg 
2777981c001SMika Westerberg 	return pctrl->soc->groups[group].name;
2787981c001SMika Westerberg }
2797981c001SMika Westerberg 
2807981c001SMika Westerberg static int intel_get_group_pins(struct pinctrl_dev *pctldev, unsigned group,
2817981c001SMika Westerberg 			      const unsigned **pins, unsigned *npins)
2827981c001SMika Westerberg {
2837981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
2847981c001SMika Westerberg 
2857981c001SMika Westerberg 	*pins = pctrl->soc->groups[group].pins;
2867981c001SMika Westerberg 	*npins = pctrl->soc->groups[group].npins;
2877981c001SMika Westerberg 	return 0;
2887981c001SMika Westerberg }
2897981c001SMika Westerberg 
2907981c001SMika Westerberg static void intel_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
2917981c001SMika Westerberg 			       unsigned pin)
2927981c001SMika Westerberg {
2937981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
294e57725eaSMika Westerberg 	void __iomem *padcfg;
2957981c001SMika Westerberg 	u32 cfg0, cfg1, mode;
2967981c001SMika Westerberg 	bool locked, acpi;
2977981c001SMika Westerberg 
2987981c001SMika Westerberg 	if (!intel_pad_owned_by_host(pctrl, pin)) {
2997981c001SMika Westerberg 		seq_puts(s, "not available");
3007981c001SMika Westerberg 		return;
3017981c001SMika Westerberg 	}
3027981c001SMika Westerberg 
3037981c001SMika Westerberg 	cfg0 = readl(intel_get_padcfg(pctrl, pin, PADCFG0));
3047981c001SMika Westerberg 	cfg1 = readl(intel_get_padcfg(pctrl, pin, PADCFG1));
3057981c001SMika Westerberg 
3067981c001SMika Westerberg 	mode = (cfg0 & PADCFG0_PMODE_MASK) >> PADCFG0_PMODE_SHIFT;
3077981c001SMika Westerberg 	if (!mode)
3087981c001SMika Westerberg 		seq_puts(s, "GPIO ");
3097981c001SMika Westerberg 	else
3107981c001SMika Westerberg 		seq_printf(s, "mode %d ", mode);
3117981c001SMika Westerberg 
3127981c001SMika Westerberg 	seq_printf(s, "0x%08x 0x%08x", cfg0, cfg1);
3137981c001SMika Westerberg 
314e57725eaSMika Westerberg 	/* Dump the additional PADCFG registers if available */
315e57725eaSMika Westerberg 	padcfg = intel_get_padcfg(pctrl, pin, PADCFG2);
316e57725eaSMika Westerberg 	if (padcfg)
317e57725eaSMika Westerberg 		seq_printf(s, " 0x%08x", readl(padcfg));
318e57725eaSMika Westerberg 
3197981c001SMika Westerberg 	locked = intel_pad_locked(pctrl, pin);
3204341e8a5SMika Westerberg 	acpi = intel_pad_acpi_mode(pctrl, pin);
3217981c001SMika Westerberg 
3227981c001SMika Westerberg 	if (locked || acpi) {
3237981c001SMika Westerberg 		seq_puts(s, " [");
3247981c001SMika Westerberg 		if (locked) {
3257981c001SMika Westerberg 			seq_puts(s, "LOCKED");
3267981c001SMika Westerberg 			if (acpi)
3277981c001SMika Westerberg 				seq_puts(s, ", ");
3287981c001SMika Westerberg 		}
3297981c001SMika Westerberg 		if (acpi)
3307981c001SMika Westerberg 			seq_puts(s, "ACPI");
3317981c001SMika Westerberg 		seq_puts(s, "]");
3327981c001SMika Westerberg 	}
3337981c001SMika Westerberg }
3347981c001SMika Westerberg 
3357981c001SMika Westerberg static const struct pinctrl_ops intel_pinctrl_ops = {
3367981c001SMika Westerberg 	.get_groups_count = intel_get_groups_count,
3377981c001SMika Westerberg 	.get_group_name = intel_get_group_name,
3387981c001SMika Westerberg 	.get_group_pins = intel_get_group_pins,
3397981c001SMika Westerberg 	.pin_dbg_show = intel_pin_dbg_show,
3407981c001SMika Westerberg };
3417981c001SMika Westerberg 
3427981c001SMika Westerberg static int intel_get_functions_count(struct pinctrl_dev *pctldev)
3437981c001SMika Westerberg {
3447981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
3457981c001SMika Westerberg 
3467981c001SMika Westerberg 	return pctrl->soc->nfunctions;
3477981c001SMika Westerberg }
3487981c001SMika Westerberg 
3497981c001SMika Westerberg static const char *intel_get_function_name(struct pinctrl_dev *pctldev,
3507981c001SMika Westerberg 					   unsigned function)
3517981c001SMika Westerberg {
3527981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
3537981c001SMika Westerberg 
3547981c001SMika Westerberg 	return pctrl->soc->functions[function].name;
3557981c001SMika Westerberg }
3567981c001SMika Westerberg 
3577981c001SMika Westerberg static int intel_get_function_groups(struct pinctrl_dev *pctldev,
3587981c001SMika Westerberg 				     unsigned function,
3597981c001SMika Westerberg 				     const char * const **groups,
3607981c001SMika Westerberg 				     unsigned * const ngroups)
3617981c001SMika Westerberg {
3627981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
3637981c001SMika Westerberg 
3647981c001SMika Westerberg 	*groups = pctrl->soc->functions[function].groups;
3657981c001SMika Westerberg 	*ngroups = pctrl->soc->functions[function].ngroups;
3667981c001SMika Westerberg 	return 0;
3677981c001SMika Westerberg }
3687981c001SMika Westerberg 
3697981c001SMika Westerberg static int intel_pinmux_set_mux(struct pinctrl_dev *pctldev, unsigned function,
3707981c001SMika Westerberg 				unsigned group)
3717981c001SMika Westerberg {
3727981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
3737981c001SMika Westerberg 	const struct intel_pingroup *grp = &pctrl->soc->groups[group];
3747981c001SMika Westerberg 	unsigned long flags;
3757981c001SMika Westerberg 	int i;
3767981c001SMika Westerberg 
37727d9098cSMika Westerberg 	raw_spin_lock_irqsave(&pctrl->lock, flags);
3787981c001SMika Westerberg 
3797981c001SMika Westerberg 	/*
3807981c001SMika Westerberg 	 * All pins in the groups needs to be accessible and writable
3817981c001SMika Westerberg 	 * before we can enable the mux for this group.
3827981c001SMika Westerberg 	 */
3837981c001SMika Westerberg 	for (i = 0; i < grp->npins; i++) {
3847981c001SMika Westerberg 		if (!intel_pad_usable(pctrl, grp->pins[i])) {
38527d9098cSMika Westerberg 			raw_spin_unlock_irqrestore(&pctrl->lock, flags);
3867981c001SMika Westerberg 			return -EBUSY;
3877981c001SMika Westerberg 		}
3887981c001SMika Westerberg 	}
3897981c001SMika Westerberg 
3907981c001SMika Westerberg 	/* Now enable the mux setting for each pin in the group */
3917981c001SMika Westerberg 	for (i = 0; i < grp->npins; i++) {
3927981c001SMika Westerberg 		void __iomem *padcfg0;
3937981c001SMika Westerberg 		u32 value;
3947981c001SMika Westerberg 
3957981c001SMika Westerberg 		padcfg0 = intel_get_padcfg(pctrl, grp->pins[i], PADCFG0);
3967981c001SMika Westerberg 		value = readl(padcfg0);
3977981c001SMika Westerberg 
3987981c001SMika Westerberg 		value &= ~PADCFG0_PMODE_MASK;
3991f6b419bSMika Westerberg 
4001f6b419bSMika Westerberg 		if (grp->modes)
4011f6b419bSMika Westerberg 			value |= grp->modes[i] << PADCFG0_PMODE_SHIFT;
4021f6b419bSMika Westerberg 		else
4037981c001SMika Westerberg 			value |= grp->mode << PADCFG0_PMODE_SHIFT;
4047981c001SMika Westerberg 
4057981c001SMika Westerberg 		writel(value, padcfg0);
4067981c001SMika Westerberg 	}
4077981c001SMika Westerberg 
40827d9098cSMika Westerberg 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
4097981c001SMika Westerberg 
4107981c001SMika Westerberg 	return 0;
4117981c001SMika Westerberg }
4127981c001SMika Westerberg 
41317fab473SAndy Shevchenko static void __intel_gpio_set_direction(void __iomem *padcfg0, bool input)
41417fab473SAndy Shevchenko {
41517fab473SAndy Shevchenko 	u32 value;
41617fab473SAndy Shevchenko 
41717fab473SAndy Shevchenko 	value = readl(padcfg0);
41817fab473SAndy Shevchenko 	if (input) {
41917fab473SAndy Shevchenko 		value &= ~PADCFG0_GPIORXDIS;
42017fab473SAndy Shevchenko 		value |= PADCFG0_GPIOTXDIS;
42117fab473SAndy Shevchenko 	} else {
42217fab473SAndy Shevchenko 		value &= ~PADCFG0_GPIOTXDIS;
42317fab473SAndy Shevchenko 		value |= PADCFG0_GPIORXDIS;
42417fab473SAndy Shevchenko 	}
42517fab473SAndy Shevchenko 	writel(value, padcfg0);
42617fab473SAndy Shevchenko }
42717fab473SAndy Shevchenko 
428f5a26acfSMika Westerberg static void intel_gpio_set_gpio_mode(void __iomem *padcfg0)
429f5a26acfSMika Westerberg {
430f5a26acfSMika Westerberg 	u32 value;
431f5a26acfSMika Westerberg 
432f5a26acfSMika Westerberg 	/* Put the pad into GPIO mode */
433f5a26acfSMika Westerberg 	value = readl(padcfg0) & ~PADCFG0_PMODE_MASK;
434f5a26acfSMika Westerberg 	/* Disable SCI/SMI/NMI generation */
435f5a26acfSMika Westerberg 	value &= ~(PADCFG0_GPIROUTIOXAPIC | PADCFG0_GPIROUTSCI);
436f5a26acfSMika Westerberg 	value &= ~(PADCFG0_GPIROUTSMI | PADCFG0_GPIROUTNMI);
437f5a26acfSMika Westerberg 	writel(value, padcfg0);
438f5a26acfSMika Westerberg }
439f5a26acfSMika Westerberg 
4407981c001SMika Westerberg static int intel_gpio_request_enable(struct pinctrl_dev *pctldev,
4417981c001SMika Westerberg 				     struct pinctrl_gpio_range *range,
4427981c001SMika Westerberg 				     unsigned pin)
4437981c001SMika Westerberg {
4447981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
4457981c001SMika Westerberg 	void __iomem *padcfg0;
4467981c001SMika Westerberg 	unsigned long flags;
4477981c001SMika Westerberg 
44827d9098cSMika Westerberg 	raw_spin_lock_irqsave(&pctrl->lock, flags);
4497981c001SMika Westerberg 
4507981c001SMika Westerberg 	if (!intel_pad_usable(pctrl, pin)) {
45127d9098cSMika Westerberg 		raw_spin_unlock_irqrestore(&pctrl->lock, flags);
4527981c001SMika Westerberg 		return -EBUSY;
4537981c001SMika Westerberg 	}
4547981c001SMika Westerberg 
4557981c001SMika Westerberg 	padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
456f5a26acfSMika Westerberg 	intel_gpio_set_gpio_mode(padcfg0);
45717fab473SAndy Shevchenko 	/* Disable TX buffer and enable RX (this will be input) */
45817fab473SAndy Shevchenko 	__intel_gpio_set_direction(padcfg0, true);
45917fab473SAndy Shevchenko 
46027d9098cSMika Westerberg 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
4617981c001SMika Westerberg 
4627981c001SMika Westerberg 	return 0;
4637981c001SMika Westerberg }
4647981c001SMika Westerberg 
4657981c001SMika Westerberg static int intel_gpio_set_direction(struct pinctrl_dev *pctldev,
4667981c001SMika Westerberg 				    struct pinctrl_gpio_range *range,
4677981c001SMika Westerberg 				    unsigned pin, bool input)
4687981c001SMika Westerberg {
4697981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
4707981c001SMika Westerberg 	void __iomem *padcfg0;
4717981c001SMika Westerberg 	unsigned long flags;
4727981c001SMika Westerberg 
47327d9098cSMika Westerberg 	raw_spin_lock_irqsave(&pctrl->lock, flags);
4747981c001SMika Westerberg 
4757981c001SMika Westerberg 	padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
47617fab473SAndy Shevchenko 	__intel_gpio_set_direction(padcfg0, input);
4777981c001SMika Westerberg 
47827d9098cSMika Westerberg 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
4797981c001SMika Westerberg 
4807981c001SMika Westerberg 	return 0;
4817981c001SMika Westerberg }
4827981c001SMika Westerberg 
4837981c001SMika Westerberg static const struct pinmux_ops intel_pinmux_ops = {
4847981c001SMika Westerberg 	.get_functions_count = intel_get_functions_count,
4857981c001SMika Westerberg 	.get_function_name = intel_get_function_name,
4867981c001SMika Westerberg 	.get_function_groups = intel_get_function_groups,
4877981c001SMika Westerberg 	.set_mux = intel_pinmux_set_mux,
4887981c001SMika Westerberg 	.gpio_request_enable = intel_gpio_request_enable,
4897981c001SMika Westerberg 	.gpio_set_direction = intel_gpio_set_direction,
4907981c001SMika Westerberg };
4917981c001SMika Westerberg 
4927981c001SMika Westerberg static int intel_config_get(struct pinctrl_dev *pctldev, unsigned pin,
4937981c001SMika Westerberg 			    unsigned long *config)
4947981c001SMika Westerberg {
4957981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
4967981c001SMika Westerberg 	enum pin_config_param param = pinconf_to_config_param(*config);
49704cc058fSMika Westerberg 	const struct intel_community *community;
4987981c001SMika Westerberg 	u32 value, term;
499e57725eaSMika Westerberg 	u32 arg = 0;
5007981c001SMika Westerberg 
5017981c001SMika Westerberg 	if (!intel_pad_owned_by_host(pctrl, pin))
5027981c001SMika Westerberg 		return -ENOTSUPP;
5037981c001SMika Westerberg 
50404cc058fSMika Westerberg 	community = intel_get_community(pctrl, pin);
5057981c001SMika Westerberg 	value = readl(intel_get_padcfg(pctrl, pin, PADCFG1));
5067981c001SMika Westerberg 	term = (value & PADCFG1_TERM_MASK) >> PADCFG1_TERM_SHIFT;
5077981c001SMika Westerberg 
5087981c001SMika Westerberg 	switch (param) {
5097981c001SMika Westerberg 	case PIN_CONFIG_BIAS_DISABLE:
5107981c001SMika Westerberg 		if (term)
5117981c001SMika Westerberg 			return -EINVAL;
5127981c001SMika Westerberg 		break;
5137981c001SMika Westerberg 
5147981c001SMika Westerberg 	case PIN_CONFIG_BIAS_PULL_UP:
5157981c001SMika Westerberg 		if (!term || !(value & PADCFG1_TERM_UP))
5167981c001SMika Westerberg 			return -EINVAL;
5177981c001SMika Westerberg 
5187981c001SMika Westerberg 		switch (term) {
5197981c001SMika Westerberg 		case PADCFG1_TERM_1K:
5207981c001SMika Westerberg 			arg = 1000;
5217981c001SMika Westerberg 			break;
5227981c001SMika Westerberg 		case PADCFG1_TERM_2K:
5237981c001SMika Westerberg 			arg = 2000;
5247981c001SMika Westerberg 			break;
5257981c001SMika Westerberg 		case PADCFG1_TERM_5K:
5267981c001SMika Westerberg 			arg = 5000;
5277981c001SMika Westerberg 			break;
5287981c001SMika Westerberg 		case PADCFG1_TERM_20K:
5297981c001SMika Westerberg 			arg = 20000;
5307981c001SMika Westerberg 			break;
5317981c001SMika Westerberg 		}
5327981c001SMika Westerberg 
5337981c001SMika Westerberg 		break;
5347981c001SMika Westerberg 
5357981c001SMika Westerberg 	case PIN_CONFIG_BIAS_PULL_DOWN:
5367981c001SMika Westerberg 		if (!term || value & PADCFG1_TERM_UP)
5377981c001SMika Westerberg 			return -EINVAL;
5387981c001SMika Westerberg 
5397981c001SMika Westerberg 		switch (term) {
54004cc058fSMika Westerberg 		case PADCFG1_TERM_1K:
54104cc058fSMika Westerberg 			if (!(community->features & PINCTRL_FEATURE_1K_PD))
54204cc058fSMika Westerberg 				return -EINVAL;
54304cc058fSMika Westerberg 			arg = 1000;
54404cc058fSMika Westerberg 			break;
5457981c001SMika Westerberg 		case PADCFG1_TERM_5K:
5467981c001SMika Westerberg 			arg = 5000;
5477981c001SMika Westerberg 			break;
5487981c001SMika Westerberg 		case PADCFG1_TERM_20K:
5497981c001SMika Westerberg 			arg = 20000;
5507981c001SMika Westerberg 			break;
5517981c001SMika Westerberg 		}
5527981c001SMika Westerberg 
5537981c001SMika Westerberg 		break;
5547981c001SMika Westerberg 
555e57725eaSMika Westerberg 	case PIN_CONFIG_INPUT_DEBOUNCE: {
556e57725eaSMika Westerberg 		void __iomem *padcfg2;
557e57725eaSMika Westerberg 		u32 v;
558e57725eaSMika Westerberg 
559e57725eaSMika Westerberg 		padcfg2 = intel_get_padcfg(pctrl, pin, PADCFG2);
560e57725eaSMika Westerberg 		if (!padcfg2)
561e57725eaSMika Westerberg 			return -ENOTSUPP;
562e57725eaSMika Westerberg 
563e57725eaSMika Westerberg 		v = readl(padcfg2);
564e57725eaSMika Westerberg 		if (!(v & PADCFG2_DEBEN))
565e57725eaSMika Westerberg 			return -EINVAL;
566e57725eaSMika Westerberg 
567e57725eaSMika Westerberg 		v = (v & PADCFG2_DEBOUNCE_MASK) >> PADCFG2_DEBOUNCE_SHIFT;
568e57725eaSMika Westerberg 		arg = BIT(v) * DEBOUNCE_PERIOD / 1000;
569e57725eaSMika Westerberg 
570e57725eaSMika Westerberg 		break;
571e57725eaSMika Westerberg 	}
572e57725eaSMika Westerberg 
5737981c001SMika Westerberg 	default:
5747981c001SMika Westerberg 		return -ENOTSUPP;
5757981c001SMika Westerberg 	}
5767981c001SMika Westerberg 
5777981c001SMika Westerberg 	*config = pinconf_to_config_packed(param, arg);
5787981c001SMika Westerberg 	return 0;
5797981c001SMika Westerberg }
5807981c001SMika Westerberg 
5817981c001SMika Westerberg static int intel_config_set_pull(struct intel_pinctrl *pctrl, unsigned pin,
5827981c001SMika Westerberg 				 unsigned long config)
5837981c001SMika Westerberg {
5847981c001SMika Westerberg 	unsigned param = pinconf_to_config_param(config);
5857981c001SMika Westerberg 	unsigned arg = pinconf_to_config_argument(config);
58604cc058fSMika Westerberg 	const struct intel_community *community;
5877981c001SMika Westerberg 	void __iomem *padcfg1;
5887981c001SMika Westerberg 	unsigned long flags;
5897981c001SMika Westerberg 	int ret = 0;
5907981c001SMika Westerberg 	u32 value;
5917981c001SMika Westerberg 
59227d9098cSMika Westerberg 	raw_spin_lock_irqsave(&pctrl->lock, flags);
5937981c001SMika Westerberg 
59404cc058fSMika Westerberg 	community = intel_get_community(pctrl, pin);
5957981c001SMika Westerberg 	padcfg1 = intel_get_padcfg(pctrl, pin, PADCFG1);
5967981c001SMika Westerberg 	value = readl(padcfg1);
5977981c001SMika Westerberg 
5987981c001SMika Westerberg 	switch (param) {
5997981c001SMika Westerberg 	case PIN_CONFIG_BIAS_DISABLE:
6007981c001SMika Westerberg 		value &= ~(PADCFG1_TERM_MASK | PADCFG1_TERM_UP);
6017981c001SMika Westerberg 		break;
6027981c001SMika Westerberg 
6037981c001SMika Westerberg 	case PIN_CONFIG_BIAS_PULL_UP:
6047981c001SMika Westerberg 		value &= ~PADCFG1_TERM_MASK;
6057981c001SMika Westerberg 
6067981c001SMika Westerberg 		value |= PADCFG1_TERM_UP;
6077981c001SMika Westerberg 
6087981c001SMika Westerberg 		switch (arg) {
6097981c001SMika Westerberg 		case 20000:
6107981c001SMika Westerberg 			value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT;
6117981c001SMika Westerberg 			break;
6127981c001SMika Westerberg 		case 5000:
6137981c001SMika Westerberg 			value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT;
6147981c001SMika Westerberg 			break;
6157981c001SMika Westerberg 		case 2000:
6167981c001SMika Westerberg 			value |= PADCFG1_TERM_2K << PADCFG1_TERM_SHIFT;
6177981c001SMika Westerberg 			break;
6187981c001SMika Westerberg 		case 1000:
6197981c001SMika Westerberg 			value |= PADCFG1_TERM_1K << PADCFG1_TERM_SHIFT;
6207981c001SMika Westerberg 			break;
6217981c001SMika Westerberg 		default:
6227981c001SMika Westerberg 			ret = -EINVAL;
6237981c001SMika Westerberg 		}
6247981c001SMika Westerberg 
6257981c001SMika Westerberg 		break;
6267981c001SMika Westerberg 
6277981c001SMika Westerberg 	case PIN_CONFIG_BIAS_PULL_DOWN:
6287981c001SMika Westerberg 		value &= ~(PADCFG1_TERM_UP | PADCFG1_TERM_MASK);
6297981c001SMika Westerberg 
6307981c001SMika Westerberg 		switch (arg) {
6317981c001SMika Westerberg 		case 20000:
6327981c001SMika Westerberg 			value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT;
6337981c001SMika Westerberg 			break;
6347981c001SMika Westerberg 		case 5000:
6357981c001SMika Westerberg 			value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT;
6367981c001SMika Westerberg 			break;
63704cc058fSMika Westerberg 		case 1000:
638aa1dd80fSDan Carpenter 			if (!(community->features & PINCTRL_FEATURE_1K_PD)) {
639aa1dd80fSDan Carpenter 				ret = -EINVAL;
640aa1dd80fSDan Carpenter 				break;
641aa1dd80fSDan Carpenter 			}
64204cc058fSMika Westerberg 			value |= PADCFG1_TERM_1K << PADCFG1_TERM_SHIFT;
64304cc058fSMika Westerberg 			break;
6447981c001SMika Westerberg 		default:
6457981c001SMika Westerberg 			ret = -EINVAL;
6467981c001SMika Westerberg 		}
6477981c001SMika Westerberg 
6487981c001SMika Westerberg 		break;
6497981c001SMika Westerberg 	}
6507981c001SMika Westerberg 
6517981c001SMika Westerberg 	if (!ret)
6527981c001SMika Westerberg 		writel(value, padcfg1);
6537981c001SMika Westerberg 
65427d9098cSMika Westerberg 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
6557981c001SMika Westerberg 
6567981c001SMika Westerberg 	return ret;
6577981c001SMika Westerberg }
6587981c001SMika Westerberg 
659e57725eaSMika Westerberg static int intel_config_set_debounce(struct intel_pinctrl *pctrl, unsigned pin,
660e57725eaSMika Westerberg 				     unsigned debounce)
661e57725eaSMika Westerberg {
662e57725eaSMika Westerberg 	void __iomem *padcfg0, *padcfg2;
663e57725eaSMika Westerberg 	unsigned long flags;
664e57725eaSMika Westerberg 	u32 value0, value2;
665e57725eaSMika Westerberg 	int ret = 0;
666e57725eaSMika Westerberg 
667e57725eaSMika Westerberg 	padcfg2 = intel_get_padcfg(pctrl, pin, PADCFG2);
668e57725eaSMika Westerberg 	if (!padcfg2)
669e57725eaSMika Westerberg 		return -ENOTSUPP;
670e57725eaSMika Westerberg 
671e57725eaSMika Westerberg 	padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
672e57725eaSMika Westerberg 
673e57725eaSMika Westerberg 	raw_spin_lock_irqsave(&pctrl->lock, flags);
674e57725eaSMika Westerberg 
675e57725eaSMika Westerberg 	value0 = readl(padcfg0);
676e57725eaSMika Westerberg 	value2 = readl(padcfg2);
677e57725eaSMika Westerberg 
678e57725eaSMika Westerberg 	/* Disable glitch filter and debouncer */
679e57725eaSMika Westerberg 	value0 &= ~PADCFG0_PREGFRXSEL;
680e57725eaSMika Westerberg 	value2 &= ~(PADCFG2_DEBEN | PADCFG2_DEBOUNCE_MASK);
681e57725eaSMika Westerberg 
682e57725eaSMika Westerberg 	if (debounce) {
683e57725eaSMika Westerberg 		unsigned long v;
684e57725eaSMika Westerberg 
685e57725eaSMika Westerberg 		v = order_base_2(debounce * 1000 / DEBOUNCE_PERIOD);
686e57725eaSMika Westerberg 		if (v < 3 || v > 15) {
687e57725eaSMika Westerberg 			ret = -EINVAL;
688e57725eaSMika Westerberg 			goto exit_unlock;
689e57725eaSMika Westerberg 		} else {
690e57725eaSMika Westerberg 			/* Enable glitch filter and debouncer */
691e57725eaSMika Westerberg 			value0 |= PADCFG0_PREGFRXSEL;
692e57725eaSMika Westerberg 			value2 |= v << PADCFG2_DEBOUNCE_SHIFT;
693e57725eaSMika Westerberg 			value2 |= PADCFG2_DEBEN;
694e57725eaSMika Westerberg 		}
695e57725eaSMika Westerberg 	}
696e57725eaSMika Westerberg 
697e57725eaSMika Westerberg 	writel(value0, padcfg0);
698e57725eaSMika Westerberg 	writel(value2, padcfg2);
699e57725eaSMika Westerberg 
700e57725eaSMika Westerberg exit_unlock:
701e57725eaSMika Westerberg 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
702e57725eaSMika Westerberg 
703e57725eaSMika Westerberg 	return ret;
704e57725eaSMika Westerberg }
705e57725eaSMika Westerberg 
7067981c001SMika Westerberg static int intel_config_set(struct pinctrl_dev *pctldev, unsigned pin,
7077981c001SMika Westerberg 			  unsigned long *configs, unsigned nconfigs)
7087981c001SMika Westerberg {
7097981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
7107981c001SMika Westerberg 	int i, ret;
7117981c001SMika Westerberg 
7127981c001SMika Westerberg 	if (!intel_pad_usable(pctrl, pin))
7137981c001SMika Westerberg 		return -ENOTSUPP;
7147981c001SMika Westerberg 
7157981c001SMika Westerberg 	for (i = 0; i < nconfigs; i++) {
7167981c001SMika Westerberg 		switch (pinconf_to_config_param(configs[i])) {
7177981c001SMika Westerberg 		case PIN_CONFIG_BIAS_DISABLE:
7187981c001SMika Westerberg 		case PIN_CONFIG_BIAS_PULL_UP:
7197981c001SMika Westerberg 		case PIN_CONFIG_BIAS_PULL_DOWN:
7207981c001SMika Westerberg 			ret = intel_config_set_pull(pctrl, pin, configs[i]);
7217981c001SMika Westerberg 			if (ret)
7227981c001SMika Westerberg 				return ret;
7237981c001SMika Westerberg 			break;
7247981c001SMika Westerberg 
725e57725eaSMika Westerberg 		case PIN_CONFIG_INPUT_DEBOUNCE:
726e57725eaSMika Westerberg 			ret = intel_config_set_debounce(pctrl, pin,
727e57725eaSMika Westerberg 				pinconf_to_config_argument(configs[i]));
728e57725eaSMika Westerberg 			if (ret)
729e57725eaSMika Westerberg 				return ret;
730e57725eaSMika Westerberg 			break;
731e57725eaSMika Westerberg 
7327981c001SMika Westerberg 		default:
7337981c001SMika Westerberg 			return -ENOTSUPP;
7347981c001SMika Westerberg 		}
7357981c001SMika Westerberg 	}
7367981c001SMika Westerberg 
7377981c001SMika Westerberg 	return 0;
7387981c001SMika Westerberg }
7397981c001SMika Westerberg 
7407981c001SMika Westerberg static const struct pinconf_ops intel_pinconf_ops = {
7417981c001SMika Westerberg 	.is_generic = true,
7427981c001SMika Westerberg 	.pin_config_get = intel_config_get,
7437981c001SMika Westerberg 	.pin_config_set = intel_config_set,
7447981c001SMika Westerberg };
7457981c001SMika Westerberg 
7467981c001SMika Westerberg static const struct pinctrl_desc intel_pinctrl_desc = {
7477981c001SMika Westerberg 	.pctlops = &intel_pinctrl_ops,
7487981c001SMika Westerberg 	.pmxops = &intel_pinmux_ops,
7497981c001SMika Westerberg 	.confops = &intel_pinconf_ops,
7507981c001SMika Westerberg 	.owner = THIS_MODULE,
7517981c001SMika Westerberg };
7527981c001SMika Westerberg 
7537981c001SMika Westerberg static int intel_gpio_get(struct gpio_chip *chip, unsigned offset)
7547981c001SMika Westerberg {
755acfd4c63SLinus Walleij 	struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
7567981c001SMika Westerberg 	void __iomem *reg;
757d68b42e3SAndy Shevchenko 	u32 padcfg0;
7587981c001SMika Westerberg 
7597981c001SMika Westerberg 	reg = intel_get_padcfg(pctrl, offset, PADCFG0);
7607981c001SMika Westerberg 	if (!reg)
7617981c001SMika Westerberg 		return -EINVAL;
7627981c001SMika Westerberg 
763d68b42e3SAndy Shevchenko 	padcfg0 = readl(reg);
764d68b42e3SAndy Shevchenko 	if (!(padcfg0 & PADCFG0_GPIOTXDIS))
765d68b42e3SAndy Shevchenko 		return !!(padcfg0 & PADCFG0_GPIOTXSTATE);
766d68b42e3SAndy Shevchenko 
767d68b42e3SAndy Shevchenko 	return !!(padcfg0 & PADCFG0_GPIORXSTATE);
7687981c001SMika Westerberg }
7697981c001SMika Westerberg 
7707981c001SMika Westerberg static void intel_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
7717981c001SMika Westerberg {
772acfd4c63SLinus Walleij 	struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
77385461377SAndy Shevchenko 	unsigned long flags;
7747981c001SMika Westerberg 	void __iomem *reg;
77585461377SAndy Shevchenko 	u32 padcfg0;
7767981c001SMika Westerberg 
7777981c001SMika Westerberg 	reg = intel_get_padcfg(pctrl, offset, PADCFG0);
77885461377SAndy Shevchenko 	if (!reg)
77985461377SAndy Shevchenko 		return;
7807981c001SMika Westerberg 
78127d9098cSMika Westerberg 	raw_spin_lock_irqsave(&pctrl->lock, flags);
7827981c001SMika Westerberg 	padcfg0 = readl(reg);
7837981c001SMika Westerberg 	if (value)
7847981c001SMika Westerberg 		padcfg0 |= PADCFG0_GPIOTXSTATE;
7857981c001SMika Westerberg 	else
7867981c001SMika Westerberg 		padcfg0 &= ~PADCFG0_GPIOTXSTATE;
7877981c001SMika Westerberg 	writel(padcfg0, reg);
78827d9098cSMika Westerberg 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
7897981c001SMika Westerberg }
7907981c001SMika Westerberg 
79167e6d3e8SJavier Arteaga static int intel_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
79267e6d3e8SJavier Arteaga {
79367e6d3e8SJavier Arteaga 	struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
79467e6d3e8SJavier Arteaga 	void __iomem *reg;
79567e6d3e8SJavier Arteaga 	u32 padcfg0;
79667e6d3e8SJavier Arteaga 
79767e6d3e8SJavier Arteaga 	reg = intel_get_padcfg(pctrl, offset, PADCFG0);
79867e6d3e8SJavier Arteaga 	if (!reg)
79967e6d3e8SJavier Arteaga 		return -EINVAL;
80067e6d3e8SJavier Arteaga 
80167e6d3e8SJavier Arteaga 	padcfg0 = readl(reg);
80267e6d3e8SJavier Arteaga 
80367e6d3e8SJavier Arteaga 	if (padcfg0 & PADCFG0_PMODE_MASK)
80467e6d3e8SJavier Arteaga 		return -EINVAL;
80567e6d3e8SJavier Arteaga 
80667e6d3e8SJavier Arteaga 	return !!(padcfg0 & PADCFG0_GPIOTXDIS);
80767e6d3e8SJavier Arteaga }
80867e6d3e8SJavier Arteaga 
8097981c001SMika Westerberg static int intel_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
8107981c001SMika Westerberg {
8117981c001SMika Westerberg 	return pinctrl_gpio_direction_input(chip->base + offset);
8127981c001SMika Westerberg }
8137981c001SMika Westerberg 
8147981c001SMika Westerberg static int intel_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
8157981c001SMika Westerberg 				       int value)
8167981c001SMika Westerberg {
8177981c001SMika Westerberg 	intel_gpio_set(chip, offset, value);
8187981c001SMika Westerberg 	return pinctrl_gpio_direction_output(chip->base + offset);
8197981c001SMika Westerberg }
8207981c001SMika Westerberg 
8217981c001SMika Westerberg static const struct gpio_chip intel_gpio_chip = {
8227981c001SMika Westerberg 	.owner = THIS_MODULE,
82398c85d58SJonas Gorski 	.request = gpiochip_generic_request,
82498c85d58SJonas Gorski 	.free = gpiochip_generic_free,
82567e6d3e8SJavier Arteaga 	.get_direction = intel_gpio_get_direction,
8267981c001SMika Westerberg 	.direction_input = intel_gpio_direction_input,
8277981c001SMika Westerberg 	.direction_output = intel_gpio_direction_output,
8287981c001SMika Westerberg 	.get = intel_gpio_get,
8297981c001SMika Westerberg 	.set = intel_gpio_set,
830e57725eaSMika Westerberg 	.set_config = gpiochip_generic_config,
8317981c001SMika Westerberg };
8327981c001SMika Westerberg 
833a60eac32SMika Westerberg /**
834a60eac32SMika Westerberg  * intel_gpio_to_pin() - Translate from GPIO offset to pin number
835a60eac32SMika Westerberg  * @pctrl: Pinctrl structure
836a60eac32SMika Westerberg  * @offset: GPIO offset from gpiolib
837803ceb29SAndy Shevchenko  * @community: Community is filled here if not %NULL
838a60eac32SMika Westerberg  * @padgrp: Pad group is filled here if not %NULL
839a60eac32SMika Westerberg  *
840a60eac32SMika Westerberg  * When coming through gpiolib irqchip, the GPIO offset is not
841a60eac32SMika Westerberg  * automatically translated to pinctrl pin number. This function can be
842a60eac32SMika Westerberg  * used to find out the corresponding pinctrl pin.
843a60eac32SMika Westerberg  */
844a60eac32SMika Westerberg static int intel_gpio_to_pin(struct intel_pinctrl *pctrl, unsigned offset,
845a60eac32SMika Westerberg 			     const struct intel_community **community,
846a60eac32SMika Westerberg 			     const struct intel_padgroup **padgrp)
847a60eac32SMika Westerberg {
848a60eac32SMika Westerberg 	int i;
849a60eac32SMika Westerberg 
850a60eac32SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
851a60eac32SMika Westerberg 		const struct intel_community *comm = &pctrl->communities[i];
852a60eac32SMika Westerberg 		int j;
853a60eac32SMika Westerberg 
854a60eac32SMika Westerberg 		for (j = 0; j < comm->ngpps; j++) {
855a60eac32SMika Westerberg 			const struct intel_padgroup *pgrp = &comm->gpps[j];
856a60eac32SMika Westerberg 
857a60eac32SMika Westerberg 			if (pgrp->gpio_base < 0)
858a60eac32SMika Westerberg 				continue;
859a60eac32SMika Westerberg 
860a60eac32SMika Westerberg 			if (offset >= pgrp->gpio_base &&
861a60eac32SMika Westerberg 			    offset < pgrp->gpio_base + pgrp->size) {
862a60eac32SMika Westerberg 				int pin;
863a60eac32SMika Westerberg 
864a60eac32SMika Westerberg 				pin = pgrp->base + offset - pgrp->gpio_base;
865a60eac32SMika Westerberg 				if (community)
866a60eac32SMika Westerberg 					*community = comm;
867a60eac32SMika Westerberg 				if (padgrp)
868a60eac32SMika Westerberg 					*padgrp = pgrp;
869a60eac32SMika Westerberg 
870a60eac32SMika Westerberg 				return pin;
871a60eac32SMika Westerberg 			}
872a60eac32SMika Westerberg 		}
873a60eac32SMika Westerberg 	}
874a60eac32SMika Westerberg 
875a60eac32SMika Westerberg 	return -EINVAL;
876a60eac32SMika Westerberg }
877a60eac32SMika Westerberg 
87855aedef5SAndy Shevchenko static int intel_gpio_irq_reqres(struct irq_data *d)
87955aedef5SAndy Shevchenko {
88055aedef5SAndy Shevchenko 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
88155aedef5SAndy Shevchenko 	struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
88255aedef5SAndy Shevchenko 	int pin;
883cb85d2b0SAndy Shevchenko 	int ret;
88455aedef5SAndy Shevchenko 
88555aedef5SAndy Shevchenko 	pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), NULL, NULL);
88655aedef5SAndy Shevchenko 	if (pin >= 0) {
887cb85d2b0SAndy Shevchenko 		ret = gpiochip_lock_as_irq(gc, pin);
888cb85d2b0SAndy Shevchenko 		if (ret) {
88955aedef5SAndy Shevchenko 			dev_err(pctrl->dev, "unable to lock HW IRQ %d for IRQ\n",
89055aedef5SAndy Shevchenko 				pin);
891cb85d2b0SAndy Shevchenko 			return ret;
89255aedef5SAndy Shevchenko 		}
89355aedef5SAndy Shevchenko 	}
89455aedef5SAndy Shevchenko 	return 0;
89555aedef5SAndy Shevchenko }
89655aedef5SAndy Shevchenko 
89755aedef5SAndy Shevchenko static void intel_gpio_irq_relres(struct irq_data *d)
89855aedef5SAndy Shevchenko {
89955aedef5SAndy Shevchenko 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
90055aedef5SAndy Shevchenko 	struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
90155aedef5SAndy Shevchenko 	int pin;
90255aedef5SAndy Shevchenko 
90355aedef5SAndy Shevchenko 	pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), NULL, NULL);
90455aedef5SAndy Shevchenko 	if (pin >= 0)
90555aedef5SAndy Shevchenko 		gpiochip_unlock_as_irq(gc, pin);
90655aedef5SAndy Shevchenko }
90755aedef5SAndy Shevchenko 
9087981c001SMika Westerberg static void intel_gpio_irq_ack(struct irq_data *d)
9097981c001SMika Westerberg {
9107981c001SMika Westerberg 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
911acfd4c63SLinus Walleij 	struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
9127981c001SMika Westerberg 	const struct intel_community *community;
913919eb475SMika Westerberg 	const struct intel_padgroup *padgrp;
914a60eac32SMika Westerberg 	int pin;
9157981c001SMika Westerberg 
916a60eac32SMika Westerberg 	pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), &community, &padgrp);
917a60eac32SMika Westerberg 	if (pin >= 0) {
918a60eac32SMika Westerberg 		unsigned gpp, gpp_offset, is_offset;
919919eb475SMika Westerberg 
920919eb475SMika Westerberg 		gpp = padgrp->reg_num;
921919eb475SMika Westerberg 		gpp_offset = padgroup_offset(padgrp, pin);
922cf769bd8SMika Westerberg 		is_offset = community->is_offset + gpp * 4;
923919eb475SMika Westerberg 
924919eb475SMika Westerberg 		raw_spin_lock(&pctrl->lock);
925cf769bd8SMika Westerberg 		writel(BIT(gpp_offset), community->regs + is_offset);
92627d9098cSMika Westerberg 		raw_spin_unlock(&pctrl->lock);
9277981c001SMika Westerberg 	}
928919eb475SMika Westerberg }
9297981c001SMika Westerberg 
930a939bb57SQi Zheng static void intel_gpio_irq_enable(struct irq_data *d)
931a939bb57SQi Zheng {
932a939bb57SQi Zheng 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
933a939bb57SQi Zheng 	struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
934a939bb57SQi Zheng 	const struct intel_community *community;
935919eb475SMika Westerberg 	const struct intel_padgroup *padgrp;
936a60eac32SMika Westerberg 	int pin;
937a60eac32SMika Westerberg 
938a60eac32SMika Westerberg 	pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), &community, &padgrp);
939a60eac32SMika Westerberg 	if (pin >= 0) {
940cf769bd8SMika Westerberg 		unsigned gpp, gpp_offset, is_offset;
941919eb475SMika Westerberg 		unsigned long flags;
942a939bb57SQi Zheng 		u32 value;
943a939bb57SQi Zheng 
944919eb475SMika Westerberg 		gpp = padgrp->reg_num;
945919eb475SMika Westerberg 		gpp_offset = padgroup_offset(padgrp, pin);
946cf769bd8SMika Westerberg 		is_offset = community->is_offset + gpp * 4;
947919eb475SMika Westerberg 
948919eb475SMika Westerberg 		raw_spin_lock_irqsave(&pctrl->lock, flags);
949a939bb57SQi Zheng 		/* Clear interrupt status first to avoid unexpected interrupt */
950cf769bd8SMika Westerberg 		writel(BIT(gpp_offset), community->regs + is_offset);
951a939bb57SQi Zheng 
952a939bb57SQi Zheng 		value = readl(community->regs + community->ie_offset + gpp * 4);
953a939bb57SQi Zheng 		value |= BIT(gpp_offset);
954a939bb57SQi Zheng 		writel(value, community->regs + community->ie_offset + gpp * 4);
95527d9098cSMika Westerberg 		raw_spin_unlock_irqrestore(&pctrl->lock, flags);
956a939bb57SQi Zheng 	}
957919eb475SMika Westerberg }
958a939bb57SQi Zheng 
9597981c001SMika Westerberg static void intel_gpio_irq_mask_unmask(struct irq_data *d, bool mask)
9607981c001SMika Westerberg {
9617981c001SMika Westerberg 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
962acfd4c63SLinus Walleij 	struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
9637981c001SMika Westerberg 	const struct intel_community *community;
964919eb475SMika Westerberg 	const struct intel_padgroup *padgrp;
965a60eac32SMika Westerberg 	int pin;
966a60eac32SMika Westerberg 
967a60eac32SMika Westerberg 	pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), &community, &padgrp);
968a60eac32SMika Westerberg 	if (pin >= 0) {
969919eb475SMika Westerberg 		unsigned gpp, gpp_offset;
970919eb475SMika Westerberg 		unsigned long flags;
9717981c001SMika Westerberg 		void __iomem *reg;
9727981c001SMika Westerberg 		u32 value;
9737981c001SMika Westerberg 
974919eb475SMika Westerberg 		gpp = padgrp->reg_num;
975919eb475SMika Westerberg 		gpp_offset = padgroup_offset(padgrp, pin);
976919eb475SMika Westerberg 
9777981c001SMika Westerberg 		reg = community->regs + community->ie_offset + gpp * 4;
978919eb475SMika Westerberg 
979919eb475SMika Westerberg 		raw_spin_lock_irqsave(&pctrl->lock, flags);
9807981c001SMika Westerberg 		value = readl(reg);
9817981c001SMika Westerberg 		if (mask)
9827981c001SMika Westerberg 			value &= ~BIT(gpp_offset);
9837981c001SMika Westerberg 		else
9847981c001SMika Westerberg 			value |= BIT(gpp_offset);
9857981c001SMika Westerberg 		writel(value, reg);
98627d9098cSMika Westerberg 		raw_spin_unlock_irqrestore(&pctrl->lock, flags);
9877981c001SMika Westerberg 	}
988919eb475SMika Westerberg }
9897981c001SMika Westerberg 
9907981c001SMika Westerberg static void intel_gpio_irq_mask(struct irq_data *d)
9917981c001SMika Westerberg {
9927981c001SMika Westerberg 	intel_gpio_irq_mask_unmask(d, true);
9937981c001SMika Westerberg }
9947981c001SMika Westerberg 
9957981c001SMika Westerberg static void intel_gpio_irq_unmask(struct irq_data *d)
9967981c001SMika Westerberg {
9977981c001SMika Westerberg 	intel_gpio_irq_mask_unmask(d, false);
9987981c001SMika Westerberg }
9997981c001SMika Westerberg 
10007981c001SMika Westerberg static int intel_gpio_irq_type(struct irq_data *d, unsigned type)
10017981c001SMika Westerberg {
10027981c001SMika Westerberg 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1003acfd4c63SLinus Walleij 	struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
1004a60eac32SMika Westerberg 	unsigned pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), NULL, NULL);
10057981c001SMika Westerberg 	unsigned long flags;
10067981c001SMika Westerberg 	void __iomem *reg;
10077981c001SMika Westerberg 	u32 value;
10087981c001SMika Westerberg 
10097981c001SMika Westerberg 	reg = intel_get_padcfg(pctrl, pin, PADCFG0);
10107981c001SMika Westerberg 	if (!reg)
10117981c001SMika Westerberg 		return -EINVAL;
10127981c001SMika Westerberg 
10134341e8a5SMika Westerberg 	/*
10144341e8a5SMika Westerberg 	 * If the pin is in ACPI mode it is still usable as a GPIO but it
10154341e8a5SMika Westerberg 	 * cannot be used as IRQ because GPI_IS status bit will not be
10164341e8a5SMika Westerberg 	 * updated by the host controller hardware.
10174341e8a5SMika Westerberg 	 */
10184341e8a5SMika Westerberg 	if (intel_pad_acpi_mode(pctrl, pin)) {
10194341e8a5SMika Westerberg 		dev_warn(pctrl->dev, "pin %u cannot be used as IRQ\n", pin);
10204341e8a5SMika Westerberg 		return -EPERM;
10214341e8a5SMika Westerberg 	}
10224341e8a5SMika Westerberg 
102327d9098cSMika Westerberg 	raw_spin_lock_irqsave(&pctrl->lock, flags);
10247981c001SMika Westerberg 
1025f5a26acfSMika Westerberg 	intel_gpio_set_gpio_mode(reg);
1026f5a26acfSMika Westerberg 
10277981c001SMika Westerberg 	value = readl(reg);
10287981c001SMika Westerberg 
10297981c001SMika Westerberg 	value &= ~(PADCFG0_RXEVCFG_MASK | PADCFG0_RXINV);
10307981c001SMika Westerberg 
10317981c001SMika Westerberg 	if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
10327981c001SMika Westerberg 		value |= PADCFG0_RXEVCFG_EDGE_BOTH << PADCFG0_RXEVCFG_SHIFT;
10337981c001SMika Westerberg 	} else if (type & IRQ_TYPE_EDGE_FALLING) {
10347981c001SMika Westerberg 		value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT;
10357981c001SMika Westerberg 		value |= PADCFG0_RXINV;
10367981c001SMika Westerberg 	} else if (type & IRQ_TYPE_EDGE_RISING) {
10377981c001SMika Westerberg 		value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT;
1038bf380cfaSQipeng Zha 	} else if (type & IRQ_TYPE_LEVEL_MASK) {
1039bf380cfaSQipeng Zha 		if (type & IRQ_TYPE_LEVEL_LOW)
10407981c001SMika Westerberg 			value |= PADCFG0_RXINV;
10417981c001SMika Westerberg 	} else {
10427981c001SMika Westerberg 		value |= PADCFG0_RXEVCFG_DISABLED << PADCFG0_RXEVCFG_SHIFT;
10437981c001SMika Westerberg 	}
10447981c001SMika Westerberg 
10457981c001SMika Westerberg 	writel(value, reg);
10467981c001SMika Westerberg 
10477981c001SMika Westerberg 	if (type & IRQ_TYPE_EDGE_BOTH)
1048fc756bcdSThomas Gleixner 		irq_set_handler_locked(d, handle_edge_irq);
10497981c001SMika Westerberg 	else if (type & IRQ_TYPE_LEVEL_MASK)
1050fc756bcdSThomas Gleixner 		irq_set_handler_locked(d, handle_level_irq);
10517981c001SMika Westerberg 
105227d9098cSMika Westerberg 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
10537981c001SMika Westerberg 
10547981c001SMika Westerberg 	return 0;
10557981c001SMika Westerberg }
10567981c001SMika Westerberg 
10577981c001SMika Westerberg static int intel_gpio_irq_wake(struct irq_data *d, unsigned int on)
10587981c001SMika Westerberg {
10597981c001SMika Westerberg 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1060acfd4c63SLinus Walleij 	struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
1061a60eac32SMika Westerberg 	unsigned pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), NULL, NULL);
10627981c001SMika Westerberg 
10637981c001SMika Westerberg 	if (on)
106401dabe91SNilesh Bacchewar 		enable_irq_wake(pctrl->irq);
10657981c001SMika Westerberg 	else
106601dabe91SNilesh Bacchewar 		disable_irq_wake(pctrl->irq);
10679a520fd9SAndy Shevchenko 
10687981c001SMika Westerberg 	dev_dbg(pctrl->dev, "%sable wake for pin %u\n", on ? "en" : "dis", pin);
10697981c001SMika Westerberg 	return 0;
10707981c001SMika Westerberg }
10717981c001SMika Westerberg 
1072193b40c8SMika Westerberg static irqreturn_t intel_gpio_community_irq_handler(struct intel_pinctrl *pctrl,
10737981c001SMika Westerberg 	const struct intel_community *community)
10747981c001SMika Westerberg {
1075193b40c8SMika Westerberg 	struct gpio_chip *gc = &pctrl->chip;
1076193b40c8SMika Westerberg 	irqreturn_t ret = IRQ_NONE;
10777981c001SMika Westerberg 	int gpp;
10787981c001SMika Westerberg 
10797981c001SMika Westerberg 	for (gpp = 0; gpp < community->ngpps; gpp++) {
1080919eb475SMika Westerberg 		const struct intel_padgroup *padgrp = &community->gpps[gpp];
10817981c001SMika Westerberg 		unsigned long pending, enabled, gpp_offset;
10827981c001SMika Westerberg 
1083cf769bd8SMika Westerberg 		pending = readl(community->regs + community->is_offset +
1084cf769bd8SMika Westerberg 				padgrp->reg_num * 4);
10857981c001SMika Westerberg 		enabled = readl(community->regs + community->ie_offset +
1086919eb475SMika Westerberg 				padgrp->reg_num * 4);
10877981c001SMika Westerberg 
10887981c001SMika Westerberg 		/* Only interrupts that are enabled */
10897981c001SMika Westerberg 		pending &= enabled;
10907981c001SMika Westerberg 
1091919eb475SMika Westerberg 		for_each_set_bit(gpp_offset, &pending, padgrp->size) {
1092a60eac32SMika Westerberg 			unsigned irq;
10937981c001SMika Westerberg 
1094f0fbe7bcSThierry Reding 			irq = irq_find_mapping(gc->irq.domain,
1095a60eac32SMika Westerberg 					       padgrp->gpio_base + gpp_offset);
10967981c001SMika Westerberg 			generic_handle_irq(irq);
1097193b40c8SMika Westerberg 
1098193b40c8SMika Westerberg 			ret |= IRQ_HANDLED;
10997981c001SMika Westerberg 		}
11007981c001SMika Westerberg 	}
11017981c001SMika Westerberg 
1102193b40c8SMika Westerberg 	return ret;
1103193b40c8SMika Westerberg }
1104193b40c8SMika Westerberg 
1105193b40c8SMika Westerberg static irqreturn_t intel_gpio_irq(int irq, void *data)
11067981c001SMika Westerberg {
1107193b40c8SMika Westerberg 	const struct intel_community *community;
1108193b40c8SMika Westerberg 	struct intel_pinctrl *pctrl = data;
1109193b40c8SMika Westerberg 	irqreturn_t ret = IRQ_NONE;
11107981c001SMika Westerberg 	int i;
11117981c001SMika Westerberg 
11127981c001SMika Westerberg 	/* Need to check all communities for pending interrupts */
1113193b40c8SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
1114193b40c8SMika Westerberg 		community = &pctrl->communities[i];
1115193b40c8SMika Westerberg 		ret |= intel_gpio_community_irq_handler(pctrl, community);
1116193b40c8SMika Westerberg 	}
11177981c001SMika Westerberg 
1118193b40c8SMika Westerberg 	return ret;
11197981c001SMika Westerberg }
11207981c001SMika Westerberg 
11217981c001SMika Westerberg static struct irq_chip intel_gpio_irqchip = {
11227981c001SMika Westerberg 	.name = "intel-gpio",
112355aedef5SAndy Shevchenko 	.irq_request_resources = intel_gpio_irq_reqres,
112455aedef5SAndy Shevchenko 	.irq_release_resources = intel_gpio_irq_relres,
1125a939bb57SQi Zheng 	.irq_enable = intel_gpio_irq_enable,
11267981c001SMika Westerberg 	.irq_ack = intel_gpio_irq_ack,
11277981c001SMika Westerberg 	.irq_mask = intel_gpio_irq_mask,
11287981c001SMika Westerberg 	.irq_unmask = intel_gpio_irq_unmask,
11297981c001SMika Westerberg 	.irq_set_type = intel_gpio_irq_type,
11307981c001SMika Westerberg 	.irq_set_wake = intel_gpio_irq_wake,
11315ff56b01SRushikesh S Kadam 	.flags = IRQCHIP_MASK_ON_SUSPEND,
11327981c001SMika Westerberg };
11337981c001SMika Westerberg 
1134a60eac32SMika Westerberg static int intel_gpio_add_pin_ranges(struct intel_pinctrl *pctrl,
1135a60eac32SMika Westerberg 				     const struct intel_community *community)
1136a60eac32SMika Westerberg {
113733b6cb58SColin Ian King 	int ret = 0, i;
1138a60eac32SMika Westerberg 
1139a60eac32SMika Westerberg 	for (i = 0; i < community->ngpps; i++) {
1140a60eac32SMika Westerberg 		const struct intel_padgroup *gpp = &community->gpps[i];
1141a60eac32SMika Westerberg 
1142a60eac32SMika Westerberg 		if (gpp->gpio_base < 0)
1143a60eac32SMika Westerberg 			continue;
1144a60eac32SMika Westerberg 
1145a60eac32SMika Westerberg 		ret = gpiochip_add_pin_range(&pctrl->chip, dev_name(pctrl->dev),
1146a60eac32SMika Westerberg 					     gpp->gpio_base, gpp->base,
1147a60eac32SMika Westerberg 					     gpp->size);
1148a60eac32SMika Westerberg 		if (ret)
1149a60eac32SMika Westerberg 			return ret;
1150a60eac32SMika Westerberg 	}
1151a60eac32SMika Westerberg 
1152a60eac32SMika Westerberg 	return ret;
1153a60eac32SMika Westerberg }
1154a60eac32SMika Westerberg 
1155a60eac32SMika Westerberg static unsigned intel_gpio_ngpio(const struct intel_pinctrl *pctrl)
1156a60eac32SMika Westerberg {
1157a60eac32SMika Westerberg 	const struct intel_community *community;
1158a60eac32SMika Westerberg 	unsigned ngpio = 0;
1159a60eac32SMika Westerberg 	int i, j;
1160a60eac32SMika Westerberg 
1161a60eac32SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
1162a60eac32SMika Westerberg 		community = &pctrl->communities[i];
1163a60eac32SMika Westerberg 		for (j = 0; j < community->ngpps; j++) {
1164a60eac32SMika Westerberg 			const struct intel_padgroup *gpp = &community->gpps[j];
1165a60eac32SMika Westerberg 
1166a60eac32SMika Westerberg 			if (gpp->gpio_base < 0)
1167a60eac32SMika Westerberg 				continue;
1168a60eac32SMika Westerberg 
1169a60eac32SMika Westerberg 			if (gpp->gpio_base + gpp->size > ngpio)
1170a60eac32SMika Westerberg 				ngpio = gpp->gpio_base + gpp->size;
1171a60eac32SMika Westerberg 		}
1172a60eac32SMika Westerberg 	}
1173a60eac32SMika Westerberg 
1174a60eac32SMika Westerberg 	return ngpio;
1175a60eac32SMika Westerberg }
1176a60eac32SMika Westerberg 
11777981c001SMika Westerberg static int intel_gpio_probe(struct intel_pinctrl *pctrl, int irq)
11787981c001SMika Westerberg {
1179a60eac32SMika Westerberg 	int ret, i;
11807981c001SMika Westerberg 
11817981c001SMika Westerberg 	pctrl->chip = intel_gpio_chip;
11827981c001SMika Westerberg 
1183a60eac32SMika Westerberg 	pctrl->chip.ngpio = intel_gpio_ngpio(pctrl);
11847981c001SMika Westerberg 	pctrl->chip.label = dev_name(pctrl->dev);
118558383c78SLinus Walleij 	pctrl->chip.parent = pctrl->dev;
11867981c001SMika Westerberg 	pctrl->chip.base = -1;
118701dabe91SNilesh Bacchewar 	pctrl->irq = irq;
11887981c001SMika Westerberg 
1189f25c3aa9SMika Westerberg 	ret = devm_gpiochip_add_data(pctrl->dev, &pctrl->chip, pctrl);
11907981c001SMika Westerberg 	if (ret) {
11917981c001SMika Westerberg 		dev_err(pctrl->dev, "failed to register gpiochip\n");
11927981c001SMika Westerberg 		return ret;
11937981c001SMika Westerberg 	}
11947981c001SMika Westerberg 
1195a60eac32SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
1196a60eac32SMika Westerberg 		struct intel_community *community = &pctrl->communities[i];
1197a60eac32SMika Westerberg 
1198a60eac32SMika Westerberg 		ret = intel_gpio_add_pin_ranges(pctrl, community);
11997981c001SMika Westerberg 		if (ret) {
12007981c001SMika Westerberg 			dev_err(pctrl->dev, "failed to add GPIO pin range\n");
1201f25c3aa9SMika Westerberg 			return ret;
1202193b40c8SMika Westerberg 		}
1203a60eac32SMika Westerberg 	}
1204193b40c8SMika Westerberg 
1205193b40c8SMika Westerberg 	/*
1206193b40c8SMika Westerberg 	 * We need to request the interrupt here (instead of providing chip
1207193b40c8SMika Westerberg 	 * to the irq directly) because on some platforms several GPIO
1208193b40c8SMika Westerberg 	 * controllers share the same interrupt line.
1209193b40c8SMika Westerberg 	 */
12101a7d1cb8SMika Westerberg 	ret = devm_request_irq(pctrl->dev, irq, intel_gpio_irq,
12111a7d1cb8SMika Westerberg 			       IRQF_SHARED | IRQF_NO_THREAD,
1212193b40c8SMika Westerberg 			       dev_name(pctrl->dev), pctrl);
1213193b40c8SMika Westerberg 	if (ret) {
1214193b40c8SMika Westerberg 		dev_err(pctrl->dev, "failed to request interrupt\n");
1215f25c3aa9SMika Westerberg 		return ret;
12167981c001SMika Westerberg 	}
12177981c001SMika Westerberg 
12187981c001SMika Westerberg 	ret = gpiochip_irqchip_add(&pctrl->chip, &intel_gpio_irqchip, 0,
12193ae02c14SAndy Shevchenko 				   handle_bad_irq, IRQ_TYPE_NONE);
12207981c001SMika Westerberg 	if (ret) {
12217981c001SMika Westerberg 		dev_err(pctrl->dev, "failed to add irqchip\n");
1222f25c3aa9SMika Westerberg 		return ret;
12237981c001SMika Westerberg 	}
12247981c001SMika Westerberg 
12257981c001SMika Westerberg 	gpiochip_set_chained_irqchip(&pctrl->chip, &intel_gpio_irqchip, irq,
1226193b40c8SMika Westerberg 				     NULL);
12277981c001SMika Westerberg 	return 0;
12287981c001SMika Westerberg }
12297981c001SMika Westerberg 
1230919eb475SMika Westerberg static int intel_pinctrl_add_padgroups(struct intel_pinctrl *pctrl,
1231919eb475SMika Westerberg 				       struct intel_community *community)
1232919eb475SMika Westerberg {
1233919eb475SMika Westerberg 	struct intel_padgroup *gpps;
1234919eb475SMika Westerberg 	unsigned npins = community->npins;
1235919eb475SMika Westerberg 	unsigned padown_num = 0;
1236919eb475SMika Westerberg 	size_t ngpps, i;
1237919eb475SMika Westerberg 
1238919eb475SMika Westerberg 	if (community->gpps)
1239919eb475SMika Westerberg 		ngpps = community->ngpps;
1240919eb475SMika Westerberg 	else
1241919eb475SMika Westerberg 		ngpps = DIV_ROUND_UP(community->npins, community->gpp_size);
1242919eb475SMika Westerberg 
1243919eb475SMika Westerberg 	gpps = devm_kcalloc(pctrl->dev, ngpps, sizeof(*gpps), GFP_KERNEL);
1244919eb475SMika Westerberg 	if (!gpps)
1245919eb475SMika Westerberg 		return -ENOMEM;
1246919eb475SMika Westerberg 
1247919eb475SMika Westerberg 	for (i = 0; i < ngpps; i++) {
1248919eb475SMika Westerberg 		if (community->gpps) {
1249919eb475SMika Westerberg 			gpps[i] = community->gpps[i];
1250919eb475SMika Westerberg 		} else {
1251919eb475SMika Westerberg 			unsigned gpp_size = community->gpp_size;
1252919eb475SMika Westerberg 
1253919eb475SMika Westerberg 			gpps[i].reg_num = i;
1254919eb475SMika Westerberg 			gpps[i].base = community->pin_base + i * gpp_size;
1255919eb475SMika Westerberg 			gpps[i].size = min(gpp_size, npins);
1256919eb475SMika Westerberg 			npins -= gpps[i].size;
1257919eb475SMika Westerberg 		}
1258919eb475SMika Westerberg 
1259919eb475SMika Westerberg 		if (gpps[i].size > 32)
1260919eb475SMika Westerberg 			return -EINVAL;
1261919eb475SMika Westerberg 
1262a60eac32SMika Westerberg 		if (!gpps[i].gpio_base)
1263a60eac32SMika Westerberg 			gpps[i].gpio_base = gpps[i].base;
1264a60eac32SMika Westerberg 
1265919eb475SMika Westerberg 		gpps[i].padown_num = padown_num;
1266919eb475SMika Westerberg 
1267919eb475SMika Westerberg 		/*
1268919eb475SMika Westerberg 		 * In older hardware the number of padown registers per
1269919eb475SMika Westerberg 		 * group is fixed regardless of the group size.
1270919eb475SMika Westerberg 		 */
1271919eb475SMika Westerberg 		if (community->gpp_num_padown_regs)
1272919eb475SMika Westerberg 			padown_num += community->gpp_num_padown_regs;
1273919eb475SMika Westerberg 		else
1274919eb475SMika Westerberg 			padown_num += DIV_ROUND_UP(gpps[i].size * 4, 32);
1275919eb475SMika Westerberg 	}
1276919eb475SMika Westerberg 
1277919eb475SMika Westerberg 	community->ngpps = ngpps;
1278919eb475SMika Westerberg 	community->gpps = gpps;
1279919eb475SMika Westerberg 
1280919eb475SMika Westerberg 	return 0;
1281919eb475SMika Westerberg }
1282919eb475SMika Westerberg 
12837981c001SMika Westerberg static int intel_pinctrl_pm_init(struct intel_pinctrl *pctrl)
12847981c001SMika Westerberg {
12857981c001SMika Westerberg #ifdef CONFIG_PM_SLEEP
12867981c001SMika Westerberg 	const struct intel_pinctrl_soc_data *soc = pctrl->soc;
12877981c001SMika Westerberg 	struct intel_community_context *communities;
12887981c001SMika Westerberg 	struct intel_pad_context *pads;
12897981c001SMika Westerberg 	int i;
12907981c001SMika Westerberg 
12917981c001SMika Westerberg 	pads = devm_kcalloc(pctrl->dev, soc->npins, sizeof(*pads), GFP_KERNEL);
12927981c001SMika Westerberg 	if (!pads)
12937981c001SMika Westerberg 		return -ENOMEM;
12947981c001SMika Westerberg 
12957981c001SMika Westerberg 	communities = devm_kcalloc(pctrl->dev, pctrl->ncommunities,
12967981c001SMika Westerberg 				   sizeof(*communities), GFP_KERNEL);
12977981c001SMika Westerberg 	if (!communities)
12987981c001SMika Westerberg 		return -ENOMEM;
12997981c001SMika Westerberg 
13007981c001SMika Westerberg 
13017981c001SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
13027981c001SMika Westerberg 		struct intel_community *community = &pctrl->communities[i];
13037981c001SMika Westerberg 		u32 *intmask;
13047981c001SMika Westerberg 
13057981c001SMika Westerberg 		intmask = devm_kcalloc(pctrl->dev, community->ngpps,
13067981c001SMika Westerberg 				       sizeof(*intmask), GFP_KERNEL);
13077981c001SMika Westerberg 		if (!intmask)
13087981c001SMika Westerberg 			return -ENOMEM;
13097981c001SMika Westerberg 
13107981c001SMika Westerberg 		communities[i].intmask = intmask;
13117981c001SMika Westerberg 	}
13127981c001SMika Westerberg 
13137981c001SMika Westerberg 	pctrl->context.pads = pads;
13147981c001SMika Westerberg 	pctrl->context.communities = communities;
13157981c001SMika Westerberg #endif
13167981c001SMika Westerberg 
13177981c001SMika Westerberg 	return 0;
13187981c001SMika Westerberg }
13197981c001SMika Westerberg 
13207981c001SMika Westerberg int intel_pinctrl_probe(struct platform_device *pdev,
13217981c001SMika Westerberg 			const struct intel_pinctrl_soc_data *soc_data)
13227981c001SMika Westerberg {
13237981c001SMika Westerberg 	struct intel_pinctrl *pctrl;
13247981c001SMika Westerberg 	int i, ret, irq;
13257981c001SMika Westerberg 
13267981c001SMika Westerberg 	if (!soc_data)
13277981c001SMika Westerberg 		return -EINVAL;
13287981c001SMika Westerberg 
13297981c001SMika Westerberg 	pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL);
13307981c001SMika Westerberg 	if (!pctrl)
13317981c001SMika Westerberg 		return -ENOMEM;
13327981c001SMika Westerberg 
13337981c001SMika Westerberg 	pctrl->dev = &pdev->dev;
13347981c001SMika Westerberg 	pctrl->soc = soc_data;
133527d9098cSMika Westerberg 	raw_spin_lock_init(&pctrl->lock);
13367981c001SMika Westerberg 
13377981c001SMika Westerberg 	/*
13387981c001SMika Westerberg 	 * Make a copy of the communities which we can use to hold pointers
13397981c001SMika Westerberg 	 * to the registers.
13407981c001SMika Westerberg 	 */
13417981c001SMika Westerberg 	pctrl->ncommunities = pctrl->soc->ncommunities;
13427981c001SMika Westerberg 	pctrl->communities = devm_kcalloc(&pdev->dev, pctrl->ncommunities,
13437981c001SMika Westerberg 				  sizeof(*pctrl->communities), GFP_KERNEL);
13447981c001SMika Westerberg 	if (!pctrl->communities)
13457981c001SMika Westerberg 		return -ENOMEM;
13467981c001SMika Westerberg 
13477981c001SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
13487981c001SMika Westerberg 		struct intel_community *community = &pctrl->communities[i];
13497981c001SMika Westerberg 		struct resource *res;
13507981c001SMika Westerberg 		void __iomem *regs;
13517981c001SMika Westerberg 		u32 padbar;
13527981c001SMika Westerberg 
13537981c001SMika Westerberg 		*community = pctrl->soc->communities[i];
13547981c001SMika Westerberg 
13557981c001SMika Westerberg 		res = platform_get_resource(pdev, IORESOURCE_MEM,
13567981c001SMika Westerberg 					    community->barno);
13577981c001SMika Westerberg 		regs = devm_ioremap_resource(&pdev->dev, res);
13587981c001SMika Westerberg 		if (IS_ERR(regs))
13597981c001SMika Westerberg 			return PTR_ERR(regs);
13607981c001SMika Westerberg 
1361e57725eaSMika Westerberg 		/*
1362e57725eaSMika Westerberg 		 * Determine community features based on the revision if
1363e57725eaSMika Westerberg 		 * not specified already.
1364e57725eaSMika Westerberg 		 */
1365e57725eaSMika Westerberg 		if (!community->features) {
1366e57725eaSMika Westerberg 			u32 rev;
1367e57725eaSMika Westerberg 
1368e57725eaSMika Westerberg 			rev = (readl(regs + REVID) & REVID_MASK) >> REVID_SHIFT;
136904cc058fSMika Westerberg 			if (rev >= 0x94) {
1370e57725eaSMika Westerberg 				community->features |= PINCTRL_FEATURE_DEBOUNCE;
137104cc058fSMika Westerberg 				community->features |= PINCTRL_FEATURE_1K_PD;
137204cc058fSMika Westerberg 			}
1373e57725eaSMika Westerberg 		}
1374e57725eaSMika Westerberg 
13757981c001SMika Westerberg 		/* Read offset of the pad configuration registers */
13767981c001SMika Westerberg 		padbar = readl(regs + PADBAR);
13777981c001SMika Westerberg 
13787981c001SMika Westerberg 		community->regs = regs;
13797981c001SMika Westerberg 		community->pad_regs = regs + padbar;
1380919eb475SMika Westerberg 
1381cf769bd8SMika Westerberg 		if (!community->is_offset)
1382cf769bd8SMika Westerberg 			community->is_offset = GPI_IS;
1383cf769bd8SMika Westerberg 
1384919eb475SMika Westerberg 		ret = intel_pinctrl_add_padgroups(pctrl, community);
1385919eb475SMika Westerberg 		if (ret)
1386919eb475SMika Westerberg 			return ret;
13877981c001SMika Westerberg 	}
13887981c001SMika Westerberg 
13897981c001SMika Westerberg 	irq = platform_get_irq(pdev, 0);
13907981c001SMika Westerberg 	if (irq < 0) {
13917981c001SMika Westerberg 		dev_err(&pdev->dev, "failed to get interrupt number\n");
13927981c001SMika Westerberg 		return irq;
13937981c001SMika Westerberg 	}
13947981c001SMika Westerberg 
13957981c001SMika Westerberg 	ret = intel_pinctrl_pm_init(pctrl);
13967981c001SMika Westerberg 	if (ret)
13977981c001SMika Westerberg 		return ret;
13987981c001SMika Westerberg 
13997981c001SMika Westerberg 	pctrl->pctldesc = intel_pinctrl_desc;
14007981c001SMika Westerberg 	pctrl->pctldesc.name = dev_name(&pdev->dev);
14017981c001SMika Westerberg 	pctrl->pctldesc.pins = pctrl->soc->pins;
14027981c001SMika Westerberg 	pctrl->pctldesc.npins = pctrl->soc->npins;
14037981c001SMika Westerberg 
140454d46cd7SLaxman Dewangan 	pctrl->pctldev = devm_pinctrl_register(&pdev->dev, &pctrl->pctldesc,
140554d46cd7SLaxman Dewangan 					       pctrl);
1406323de9efSMasahiro Yamada 	if (IS_ERR(pctrl->pctldev)) {
14077981c001SMika Westerberg 		dev_err(&pdev->dev, "failed to register pinctrl driver\n");
1408323de9efSMasahiro Yamada 		return PTR_ERR(pctrl->pctldev);
14097981c001SMika Westerberg 	}
14107981c001SMika Westerberg 
14117981c001SMika Westerberg 	ret = intel_gpio_probe(pctrl, irq);
141254d46cd7SLaxman Dewangan 	if (ret)
14137981c001SMika Westerberg 		return ret;
14147981c001SMika Westerberg 
14157981c001SMika Westerberg 	platform_set_drvdata(pdev, pctrl);
14167981c001SMika Westerberg 
14177981c001SMika Westerberg 	return 0;
14187981c001SMika Westerberg }
14197981c001SMika Westerberg EXPORT_SYMBOL_GPL(intel_pinctrl_probe);
14207981c001SMika Westerberg 
1421*924cf800SAndy Shevchenko int intel_pinctrl_probe_by_uid(struct platform_device *pdev)
1422*924cf800SAndy Shevchenko {
1423*924cf800SAndy Shevchenko 	const struct intel_pinctrl_soc_data *data = NULL;
1424*924cf800SAndy Shevchenko 	const struct intel_pinctrl_soc_data **table;
1425*924cf800SAndy Shevchenko 	struct acpi_device *adev;
1426*924cf800SAndy Shevchenko 	unsigned int i;
1427*924cf800SAndy Shevchenko 
1428*924cf800SAndy Shevchenko 	adev = ACPI_COMPANION(&pdev->dev);
1429*924cf800SAndy Shevchenko 	if (adev) {
1430*924cf800SAndy Shevchenko 		const void *match = device_get_match_data(&pdev->dev);
1431*924cf800SAndy Shevchenko 
1432*924cf800SAndy Shevchenko 		table = (const struct intel_pinctrl_soc_data **)match;
1433*924cf800SAndy Shevchenko 		for (i = 0; table[i]; i++) {
1434*924cf800SAndy Shevchenko 			if (!strcmp(adev->pnp.unique_id, table[i]->uid)) {
1435*924cf800SAndy Shevchenko 				data = table[i];
1436*924cf800SAndy Shevchenko 				break;
1437*924cf800SAndy Shevchenko 			}
1438*924cf800SAndy Shevchenko 		}
1439*924cf800SAndy Shevchenko 	} else {
1440*924cf800SAndy Shevchenko 		const struct platform_device_id *id;
1441*924cf800SAndy Shevchenko 
1442*924cf800SAndy Shevchenko 		id = platform_get_device_id(pdev);
1443*924cf800SAndy Shevchenko 		if (!id)
1444*924cf800SAndy Shevchenko 			return -ENODEV;
1445*924cf800SAndy Shevchenko 
1446*924cf800SAndy Shevchenko 		table = (const struct intel_pinctrl_soc_data **)id->driver_data;
1447*924cf800SAndy Shevchenko 		data = table[pdev->id];
1448*924cf800SAndy Shevchenko 	}
1449*924cf800SAndy Shevchenko 	if (!data)
1450*924cf800SAndy Shevchenko 		return -ENODEV;
1451*924cf800SAndy Shevchenko 
1452*924cf800SAndy Shevchenko 	return intel_pinctrl_probe(pdev, data);
1453*924cf800SAndy Shevchenko }
1454*924cf800SAndy Shevchenko EXPORT_SYMBOL_GPL(intel_pinctrl_probe_by_uid);
1455*924cf800SAndy Shevchenko 
14567981c001SMika Westerberg #ifdef CONFIG_PM_SLEEP
1457c538b943SMika Westerberg static bool intel_pinctrl_should_save(struct intel_pinctrl *pctrl, unsigned pin)
1458c538b943SMika Westerberg {
1459c538b943SMika Westerberg 	const struct pin_desc *pd = pin_desc_get(pctrl->pctldev, pin);
1460c538b943SMika Westerberg 
1461c538b943SMika Westerberg 	if (!pd || !intel_pad_usable(pctrl, pin))
1462c538b943SMika Westerberg 		return false;
1463c538b943SMika Westerberg 
1464c538b943SMika Westerberg 	/*
1465c538b943SMika Westerberg 	 * Only restore the pin if it is actually in use by the kernel (or
1466c538b943SMika Westerberg 	 * by userspace). It is possible that some pins are used by the
1467c538b943SMika Westerberg 	 * BIOS during resume and those are not always locked down so leave
1468c538b943SMika Westerberg 	 * them alone.
1469c538b943SMika Westerberg 	 */
1470c538b943SMika Westerberg 	if (pd->mux_owner || pd->gpio_owner ||
1471c538b943SMika Westerberg 	    gpiochip_line_is_irq(&pctrl->chip, pin))
1472c538b943SMika Westerberg 		return true;
1473c538b943SMika Westerberg 
1474c538b943SMika Westerberg 	return false;
1475c538b943SMika Westerberg }
1476c538b943SMika Westerberg 
14777981c001SMika Westerberg int intel_pinctrl_suspend(struct device *dev)
14787981c001SMika Westerberg {
14797981c001SMika Westerberg 	struct platform_device *pdev = to_platform_device(dev);
14807981c001SMika Westerberg 	struct intel_pinctrl *pctrl = platform_get_drvdata(pdev);
14817981c001SMika Westerberg 	struct intel_community_context *communities;
14827981c001SMika Westerberg 	struct intel_pad_context *pads;
14837981c001SMika Westerberg 	int i;
14847981c001SMika Westerberg 
14857981c001SMika Westerberg 	pads = pctrl->context.pads;
14867981c001SMika Westerberg 	for (i = 0; i < pctrl->soc->npins; i++) {
14877981c001SMika Westerberg 		const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
1488e57725eaSMika Westerberg 		void __iomem *padcfg;
14897981c001SMika Westerberg 		u32 val;
14907981c001SMika Westerberg 
1491c538b943SMika Westerberg 		if (!intel_pinctrl_should_save(pctrl, desc->number))
14927981c001SMika Westerberg 			continue;
14937981c001SMika Westerberg 
14947981c001SMika Westerberg 		val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG0));
14957981c001SMika Westerberg 		pads[i].padcfg0 = val & ~PADCFG0_GPIORXSTATE;
14967981c001SMika Westerberg 		val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG1));
14977981c001SMika Westerberg 		pads[i].padcfg1 = val;
1498e57725eaSMika Westerberg 
1499e57725eaSMika Westerberg 		padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG2);
1500e57725eaSMika Westerberg 		if (padcfg)
1501e57725eaSMika Westerberg 			pads[i].padcfg2 = readl(padcfg);
15027981c001SMika Westerberg 	}
15037981c001SMika Westerberg 
15047981c001SMika Westerberg 	communities = pctrl->context.communities;
15057981c001SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
15067981c001SMika Westerberg 		struct intel_community *community = &pctrl->communities[i];
15077981c001SMika Westerberg 		void __iomem *base;
15087981c001SMika Westerberg 		unsigned gpp;
15097981c001SMika Westerberg 
15107981c001SMika Westerberg 		base = community->regs + community->ie_offset;
15117981c001SMika Westerberg 		for (gpp = 0; gpp < community->ngpps; gpp++)
15127981c001SMika Westerberg 			communities[i].intmask[gpp] = readl(base + gpp * 4);
15137981c001SMika Westerberg 	}
15147981c001SMika Westerberg 
15157981c001SMika Westerberg 	return 0;
15167981c001SMika Westerberg }
15177981c001SMika Westerberg EXPORT_SYMBOL_GPL(intel_pinctrl_suspend);
15187981c001SMika Westerberg 
1519f487bbf3SMika Westerberg static void intel_gpio_irq_init(struct intel_pinctrl *pctrl)
1520f487bbf3SMika Westerberg {
1521f487bbf3SMika Westerberg 	size_t i;
1522f487bbf3SMika Westerberg 
1523f487bbf3SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
1524f487bbf3SMika Westerberg 		const struct intel_community *community;
1525f487bbf3SMika Westerberg 		void __iomem *base;
1526f487bbf3SMika Westerberg 		unsigned gpp;
1527f487bbf3SMika Westerberg 
1528f487bbf3SMika Westerberg 		community = &pctrl->communities[i];
1529f487bbf3SMika Westerberg 		base = community->regs;
1530f487bbf3SMika Westerberg 
1531f487bbf3SMika Westerberg 		for (gpp = 0; gpp < community->ngpps; gpp++) {
1532f487bbf3SMika Westerberg 			/* Mask and clear all interrupts */
1533f487bbf3SMika Westerberg 			writel(0, base + community->ie_offset + gpp * 4);
1534cf769bd8SMika Westerberg 			writel(0xffff, base + community->is_offset + gpp * 4);
1535f487bbf3SMika Westerberg 		}
1536f487bbf3SMika Westerberg 	}
1537f487bbf3SMika Westerberg }
1538f487bbf3SMika Westerberg 
15397981c001SMika Westerberg int intel_pinctrl_resume(struct device *dev)
15407981c001SMika Westerberg {
15417981c001SMika Westerberg 	struct platform_device *pdev = to_platform_device(dev);
15427981c001SMika Westerberg 	struct intel_pinctrl *pctrl = platform_get_drvdata(pdev);
15437981c001SMika Westerberg 	const struct intel_community_context *communities;
15447981c001SMika Westerberg 	const struct intel_pad_context *pads;
15457981c001SMika Westerberg 	int i;
15467981c001SMika Westerberg 
15477981c001SMika Westerberg 	/* Mask all interrupts */
15487981c001SMika Westerberg 	intel_gpio_irq_init(pctrl);
15497981c001SMika Westerberg 
15507981c001SMika Westerberg 	pads = pctrl->context.pads;
15517981c001SMika Westerberg 	for (i = 0; i < pctrl->soc->npins; i++) {
15527981c001SMika Westerberg 		const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
15537981c001SMika Westerberg 		void __iomem *padcfg;
15547981c001SMika Westerberg 		u32 val;
15557981c001SMika Westerberg 
1556c538b943SMika Westerberg 		if (!intel_pinctrl_should_save(pctrl, desc->number))
15577981c001SMika Westerberg 			continue;
15587981c001SMika Westerberg 
15597981c001SMika Westerberg 		padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG0);
15607981c001SMika Westerberg 		val = readl(padcfg) & ~PADCFG0_GPIORXSTATE;
15617981c001SMika Westerberg 		if (val != pads[i].padcfg0) {
15627981c001SMika Westerberg 			writel(pads[i].padcfg0, padcfg);
15637981c001SMika Westerberg 			dev_dbg(dev, "restored pin %u padcfg0 %#08x\n",
15647981c001SMika Westerberg 				desc->number, readl(padcfg));
15657981c001SMika Westerberg 		}
15667981c001SMika Westerberg 
15677981c001SMika Westerberg 		padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG1);
15687981c001SMika Westerberg 		val = readl(padcfg);
15697981c001SMika Westerberg 		if (val != pads[i].padcfg1) {
15707981c001SMika Westerberg 			writel(pads[i].padcfg1, padcfg);
15717981c001SMika Westerberg 			dev_dbg(dev, "restored pin %u padcfg1 %#08x\n",
15727981c001SMika Westerberg 				desc->number, readl(padcfg));
15737981c001SMika Westerberg 		}
1574e57725eaSMika Westerberg 
1575e57725eaSMika Westerberg 		padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG2);
1576e57725eaSMika Westerberg 		if (padcfg) {
1577e57725eaSMika Westerberg 			val = readl(padcfg);
1578e57725eaSMika Westerberg 			if (val != pads[i].padcfg2) {
1579e57725eaSMika Westerberg 				writel(pads[i].padcfg2, padcfg);
1580e57725eaSMika Westerberg 				dev_dbg(dev, "restored pin %u padcfg2 %#08x\n",
1581e57725eaSMika Westerberg 					desc->number, readl(padcfg));
1582e57725eaSMika Westerberg 			}
1583e57725eaSMika Westerberg 		}
15847981c001SMika Westerberg 	}
15857981c001SMika Westerberg 
15867981c001SMika Westerberg 	communities = pctrl->context.communities;
15877981c001SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
15887981c001SMika Westerberg 		struct intel_community *community = &pctrl->communities[i];
15897981c001SMika Westerberg 		void __iomem *base;
15907981c001SMika Westerberg 		unsigned gpp;
15917981c001SMika Westerberg 
15927981c001SMika Westerberg 		base = community->regs + community->ie_offset;
15937981c001SMika Westerberg 		for (gpp = 0; gpp < community->ngpps; gpp++) {
15947981c001SMika Westerberg 			writel(communities[i].intmask[gpp], base + gpp * 4);
15957981c001SMika Westerberg 			dev_dbg(dev, "restored mask %d/%u %#08x\n", i, gpp,
15967981c001SMika Westerberg 				readl(base + gpp * 4));
15977981c001SMika Westerberg 		}
15987981c001SMika Westerberg 	}
15997981c001SMika Westerberg 
16007981c001SMika Westerberg 	return 0;
16017981c001SMika Westerberg }
16027981c001SMika Westerberg EXPORT_SYMBOL_GPL(intel_pinctrl_resume);
16037981c001SMika Westerberg #endif
16047981c001SMika Westerberg 
16057981c001SMika Westerberg MODULE_AUTHOR("Mathias Nyman <mathias.nyman@linux.intel.com>");
16067981c001SMika Westerberg MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
16077981c001SMika Westerberg MODULE_DESCRIPTION("Intel pinctrl/GPIO core driver");
16087981c001SMika Westerberg MODULE_LICENSE("GPL v2");
1609