xref: /openbmc/linux/drivers/pinctrl/intel/pinctrl-intel.c (revision 1f6b419b24285409a9365461bf7367a220eff1db)
17981c001SMika Westerberg /*
27981c001SMika Westerberg  * Intel pinctrl/GPIO core driver.
37981c001SMika Westerberg  *
47981c001SMika Westerberg  * Copyright (C) 2015, Intel Corporation
57981c001SMika Westerberg  * Authors: Mathias Nyman <mathias.nyman@linux.intel.com>
67981c001SMika Westerberg  *          Mika Westerberg <mika.westerberg@linux.intel.com>
77981c001SMika Westerberg  *
87981c001SMika Westerberg  * This program is free software; you can redistribute it and/or modify
97981c001SMika Westerberg  * it under the terms of the GNU General Public License version 2 as
107981c001SMika Westerberg  * published by the Free Software Foundation.
117981c001SMika Westerberg  */
127981c001SMika Westerberg 
137981c001SMika Westerberg #include <linux/module.h>
14193b40c8SMika Westerberg #include <linux/interrupt.h>
157981c001SMika Westerberg #include <linux/gpio/driver.h>
16e57725eaSMika Westerberg #include <linux/log2.h>
177981c001SMika Westerberg #include <linux/platform_device.h>
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 #define GPI_GPE_STS			0x140
347981c001SMika Westerberg #define GPI_GPE_EN			0x160
357981c001SMika Westerberg 
367981c001SMika Westerberg #define PADOWN_BITS			4
377981c001SMika Westerberg #define PADOWN_SHIFT(p)			((p) % 8 * PADOWN_BITS)
387981c001SMika Westerberg #define PADOWN_MASK(p)			(0xf << PADOWN_SHIFT(p))
3999a735b3SQipeng Zha #define PADOWN_GPP(p)			((p) / 8)
407981c001SMika Westerberg 
417981c001SMika Westerberg /* Offset from pad_regs */
427981c001SMika Westerberg #define PADCFG0				0x000
437981c001SMika Westerberg #define PADCFG0_RXEVCFG_SHIFT		25
447981c001SMika Westerberg #define PADCFG0_RXEVCFG_MASK		(3 << PADCFG0_RXEVCFG_SHIFT)
457981c001SMika Westerberg #define PADCFG0_RXEVCFG_LEVEL		0
467981c001SMika Westerberg #define PADCFG0_RXEVCFG_EDGE		1
477981c001SMika Westerberg #define PADCFG0_RXEVCFG_DISABLED	2
487981c001SMika Westerberg #define PADCFG0_RXEVCFG_EDGE_BOTH	3
49e57725eaSMika Westerberg #define PADCFG0_PREGFRXSEL		BIT(24)
507981c001SMika Westerberg #define PADCFG0_RXINV			BIT(23)
517981c001SMika Westerberg #define PADCFG0_GPIROUTIOXAPIC		BIT(20)
527981c001SMika Westerberg #define PADCFG0_GPIROUTSCI		BIT(19)
537981c001SMika Westerberg #define PADCFG0_GPIROUTSMI		BIT(18)
547981c001SMika Westerberg #define PADCFG0_GPIROUTNMI		BIT(17)
557981c001SMika Westerberg #define PADCFG0_PMODE_SHIFT		10
567981c001SMika Westerberg #define PADCFG0_PMODE_MASK		(0xf << PADCFG0_PMODE_SHIFT)
577981c001SMika Westerberg #define PADCFG0_GPIORXDIS		BIT(9)
587981c001SMika Westerberg #define PADCFG0_GPIOTXDIS		BIT(8)
597981c001SMika Westerberg #define PADCFG0_GPIORXSTATE		BIT(1)
607981c001SMika Westerberg #define PADCFG0_GPIOTXSTATE		BIT(0)
617981c001SMika Westerberg 
627981c001SMika Westerberg #define PADCFG1				0x004
637981c001SMika Westerberg #define PADCFG1_TERM_UP			BIT(13)
647981c001SMika Westerberg #define PADCFG1_TERM_SHIFT		10
657981c001SMika Westerberg #define PADCFG1_TERM_MASK		(7 << PADCFG1_TERM_SHIFT)
667981c001SMika Westerberg #define PADCFG1_TERM_20K		4
677981c001SMika Westerberg #define PADCFG1_TERM_2K			3
687981c001SMika Westerberg #define PADCFG1_TERM_5K			2
697981c001SMika Westerberg #define PADCFG1_TERM_1K			1
707981c001SMika Westerberg 
71e57725eaSMika Westerberg #define PADCFG2				0x008
72e57725eaSMika Westerberg #define PADCFG2_DEBEN			BIT(0)
73e57725eaSMika Westerberg #define PADCFG2_DEBOUNCE_SHIFT		1
74e57725eaSMika Westerberg #define PADCFG2_DEBOUNCE_MASK		GENMASK(4, 1)
75e57725eaSMika Westerberg 
76e57725eaSMika Westerberg #define DEBOUNCE_PERIOD			31250 /* ns */
77e57725eaSMika Westerberg 
787981c001SMika Westerberg struct intel_pad_context {
797981c001SMika Westerberg 	u32 padcfg0;
807981c001SMika Westerberg 	u32 padcfg1;
81e57725eaSMika Westerberg 	u32 padcfg2;
827981c001SMika Westerberg };
837981c001SMika Westerberg 
847981c001SMika Westerberg struct intel_community_context {
857981c001SMika Westerberg 	u32 *intmask;
867981c001SMika Westerberg };
877981c001SMika Westerberg 
887981c001SMika Westerberg struct intel_pinctrl_context {
897981c001SMika Westerberg 	struct intel_pad_context *pads;
907981c001SMika Westerberg 	struct intel_community_context *communities;
917981c001SMika Westerberg };
927981c001SMika Westerberg 
937981c001SMika Westerberg /**
947981c001SMika Westerberg  * struct intel_pinctrl - Intel pinctrl private structure
957981c001SMika Westerberg  * @dev: Pointer to the device structure
967981c001SMika Westerberg  * @lock: Lock to serialize register access
977981c001SMika Westerberg  * @pctldesc: Pin controller description
987981c001SMika Westerberg  * @pctldev: Pointer to the pin controller device
997981c001SMika Westerberg  * @chip: GPIO chip in this pin controller
1007981c001SMika Westerberg  * @soc: SoC/PCH specific pin configuration data
1017981c001SMika Westerberg  * @communities: All communities in this pin controller
1027981c001SMika Westerberg  * @ncommunities: Number of communities in this pin controller
1037981c001SMika Westerberg  * @context: Configuration saved over system sleep
10401dabe91SNilesh Bacchewar  * @irq: pinctrl/GPIO chip irq number
1057981c001SMika Westerberg  */
1067981c001SMika Westerberg struct intel_pinctrl {
1077981c001SMika Westerberg 	struct device *dev;
10827d9098cSMika Westerberg 	raw_spinlock_t lock;
1097981c001SMika Westerberg 	struct pinctrl_desc pctldesc;
1107981c001SMika Westerberg 	struct pinctrl_dev *pctldev;
1117981c001SMika Westerberg 	struct gpio_chip chip;
1127981c001SMika Westerberg 	const struct intel_pinctrl_soc_data *soc;
1137981c001SMika Westerberg 	struct intel_community *communities;
1147981c001SMika Westerberg 	size_t ncommunities;
1157981c001SMika Westerberg 	struct intel_pinctrl_context context;
11601dabe91SNilesh Bacchewar 	int irq;
1177981c001SMika Westerberg };
1187981c001SMika Westerberg 
1197981c001SMika Westerberg #define pin_to_padno(c, p)	((p) - (c)->pin_base)
120919eb475SMika Westerberg #define padgroup_offset(g, p)	((p) - (g)->base)
1217981c001SMika Westerberg 
1227981c001SMika Westerberg static struct intel_community *intel_get_community(struct intel_pinctrl *pctrl,
1237981c001SMika Westerberg 						   unsigned pin)
1247981c001SMika Westerberg {
1257981c001SMika Westerberg 	struct intel_community *community;
1267981c001SMika Westerberg 	int i;
1277981c001SMika Westerberg 
1287981c001SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
1297981c001SMika Westerberg 		community = &pctrl->communities[i];
1307981c001SMika Westerberg 		if (pin >= community->pin_base &&
1317981c001SMika Westerberg 		    pin < community->pin_base + community->npins)
1327981c001SMika Westerberg 			return community;
1337981c001SMika Westerberg 	}
1347981c001SMika Westerberg 
1357981c001SMika Westerberg 	dev_warn(pctrl->dev, "failed to find community for pin %u\n", pin);
1367981c001SMika Westerberg 	return NULL;
1377981c001SMika Westerberg }
1387981c001SMika Westerberg 
139919eb475SMika Westerberg static const struct intel_padgroup *
140919eb475SMika Westerberg intel_community_get_padgroup(const struct intel_community *community,
141919eb475SMika Westerberg 			     unsigned pin)
142919eb475SMika Westerberg {
143919eb475SMika Westerberg 	int i;
144919eb475SMika Westerberg 
145919eb475SMika Westerberg 	for (i = 0; i < community->ngpps; i++) {
146919eb475SMika Westerberg 		const struct intel_padgroup *padgrp = &community->gpps[i];
147919eb475SMika Westerberg 
148919eb475SMika Westerberg 		if (pin >= padgrp->base && pin < padgrp->base + padgrp->size)
149919eb475SMika Westerberg 			return padgrp;
150919eb475SMika Westerberg 	}
151919eb475SMika Westerberg 
152919eb475SMika Westerberg 	return NULL;
153919eb475SMika Westerberg }
154919eb475SMika Westerberg 
1557981c001SMika Westerberg static void __iomem *intel_get_padcfg(struct intel_pinctrl *pctrl, unsigned pin,
1567981c001SMika Westerberg 				      unsigned reg)
1577981c001SMika Westerberg {
1587981c001SMika Westerberg 	const struct intel_community *community;
1597981c001SMika Westerberg 	unsigned padno;
160e57725eaSMika Westerberg 	size_t nregs;
1617981c001SMika Westerberg 
1627981c001SMika Westerberg 	community = intel_get_community(pctrl, pin);
1637981c001SMika Westerberg 	if (!community)
1647981c001SMika Westerberg 		return NULL;
1657981c001SMika Westerberg 
1667981c001SMika Westerberg 	padno = pin_to_padno(community, pin);
167e57725eaSMika Westerberg 	nregs = (community->features & PINCTRL_FEATURE_DEBOUNCE) ? 4 : 2;
168e57725eaSMika Westerberg 
169e57725eaSMika Westerberg 	if (reg == PADCFG2 && !(community->features & PINCTRL_FEATURE_DEBOUNCE))
170e57725eaSMika Westerberg 		return NULL;
171e57725eaSMika Westerberg 
172e57725eaSMika Westerberg 	return community->pad_regs + reg + padno * nregs * 4;
1737981c001SMika Westerberg }
1747981c001SMika Westerberg 
1757981c001SMika Westerberg static bool intel_pad_owned_by_host(struct intel_pinctrl *pctrl, unsigned pin)
1767981c001SMika Westerberg {
1777981c001SMika Westerberg 	const struct intel_community *community;
178919eb475SMika Westerberg 	const struct intel_padgroup *padgrp;
179919eb475SMika Westerberg 	unsigned gpp, offset, gpp_offset;
1807981c001SMika Westerberg 	void __iomem *padown;
1817981c001SMika Westerberg 
1827981c001SMika Westerberg 	community = intel_get_community(pctrl, pin);
1837981c001SMika Westerberg 	if (!community)
1847981c001SMika Westerberg 		return false;
1857981c001SMika Westerberg 	if (!community->padown_offset)
1867981c001SMika Westerberg 		return true;
1877981c001SMika Westerberg 
188919eb475SMika Westerberg 	padgrp = intel_community_get_padgroup(community, pin);
189919eb475SMika Westerberg 	if (!padgrp)
190919eb475SMika Westerberg 		return false;
191919eb475SMika Westerberg 
192919eb475SMika Westerberg 	gpp_offset = padgroup_offset(padgrp, pin);
193919eb475SMika Westerberg 	gpp = PADOWN_GPP(gpp_offset);
194919eb475SMika Westerberg 	offset = community->padown_offset + padgrp->padown_num * 4 + gpp * 4;
1957981c001SMika Westerberg 	padown = community->regs + offset;
1967981c001SMika Westerberg 
197919eb475SMika Westerberg 	return !(readl(padown) & PADOWN_MASK(gpp_offset));
1987981c001SMika Westerberg }
1997981c001SMika Westerberg 
2004341e8a5SMika Westerberg static bool intel_pad_acpi_mode(struct intel_pinctrl *pctrl, unsigned pin)
2017981c001SMika Westerberg {
2027981c001SMika Westerberg 	const struct intel_community *community;
203919eb475SMika Westerberg 	const struct intel_padgroup *padgrp;
204919eb475SMika Westerberg 	unsigned offset, gpp_offset;
2057981c001SMika Westerberg 	void __iomem *hostown;
2067981c001SMika Westerberg 
2077981c001SMika Westerberg 	community = intel_get_community(pctrl, pin);
2087981c001SMika Westerberg 	if (!community)
2097981c001SMika Westerberg 		return true;
2107981c001SMika Westerberg 	if (!community->hostown_offset)
2117981c001SMika Westerberg 		return false;
2127981c001SMika Westerberg 
213919eb475SMika Westerberg 	padgrp = intel_community_get_padgroup(community, pin);
214919eb475SMika Westerberg 	if (!padgrp)
215919eb475SMika Westerberg 		return true;
216919eb475SMika Westerberg 
217919eb475SMika Westerberg 	gpp_offset = padgroup_offset(padgrp, pin);
218919eb475SMika Westerberg 	offset = community->hostown_offset + padgrp->reg_num * 4;
2197981c001SMika Westerberg 	hostown = community->regs + offset;
2207981c001SMika Westerberg 
221919eb475SMika Westerberg 	return !(readl(hostown) & BIT(gpp_offset));
2227981c001SMika Westerberg }
2237981c001SMika Westerberg 
2247981c001SMika Westerberg static bool intel_pad_locked(struct intel_pinctrl *pctrl, unsigned pin)
2257981c001SMika Westerberg {
2267981c001SMika Westerberg 	struct intel_community *community;
227919eb475SMika Westerberg 	const struct intel_padgroup *padgrp;
228919eb475SMika Westerberg 	unsigned offset, gpp_offset;
2297981c001SMika Westerberg 	u32 value;
2307981c001SMika Westerberg 
2317981c001SMika Westerberg 	community = intel_get_community(pctrl, pin);
2327981c001SMika Westerberg 	if (!community)
2337981c001SMika Westerberg 		return true;
2347981c001SMika Westerberg 	if (!community->padcfglock_offset)
2357981c001SMika Westerberg 		return false;
2367981c001SMika Westerberg 
237919eb475SMika Westerberg 	padgrp = intel_community_get_padgroup(community, pin);
238919eb475SMika Westerberg 	if (!padgrp)
239919eb475SMika Westerberg 		return true;
240919eb475SMika Westerberg 
241919eb475SMika Westerberg 	gpp_offset = padgroup_offset(padgrp, pin);
2427981c001SMika Westerberg 
2437981c001SMika Westerberg 	/*
2447981c001SMika Westerberg 	 * If PADCFGLOCK and PADCFGLOCKTX bits are both clear for this pad,
2457981c001SMika Westerberg 	 * the pad is considered unlocked. Any other case means that it is
2467981c001SMika Westerberg 	 * either fully or partially locked and we don't touch it.
2477981c001SMika Westerberg 	 */
248919eb475SMika Westerberg 	offset = community->padcfglock_offset + padgrp->reg_num * 8;
2497981c001SMika Westerberg 	value = readl(community->regs + offset);
250919eb475SMika Westerberg 	if (value & BIT(gpp_offset))
2517981c001SMika Westerberg 		return true;
2527981c001SMika Westerberg 
253919eb475SMika Westerberg 	offset = community->padcfglock_offset + 4 + padgrp->reg_num * 8;
2547981c001SMika Westerberg 	value = readl(community->regs + offset);
255919eb475SMika Westerberg 	if (value & BIT(gpp_offset))
2567981c001SMika Westerberg 		return true;
2577981c001SMika Westerberg 
2587981c001SMika Westerberg 	return false;
2597981c001SMika Westerberg }
2607981c001SMika Westerberg 
2617981c001SMika Westerberg static bool intel_pad_usable(struct intel_pinctrl *pctrl, unsigned pin)
2627981c001SMika Westerberg {
2637981c001SMika Westerberg 	return intel_pad_owned_by_host(pctrl, pin) &&
2647981c001SMika Westerberg 		!intel_pad_locked(pctrl, pin);
2657981c001SMika Westerberg }
2667981c001SMika Westerberg 
2677981c001SMika Westerberg static int intel_get_groups_count(struct pinctrl_dev *pctldev)
2687981c001SMika Westerberg {
2697981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
2707981c001SMika Westerberg 
2717981c001SMika Westerberg 	return pctrl->soc->ngroups;
2727981c001SMika Westerberg }
2737981c001SMika Westerberg 
2747981c001SMika Westerberg static const char *intel_get_group_name(struct pinctrl_dev *pctldev,
2757981c001SMika Westerberg 				      unsigned group)
2767981c001SMika Westerberg {
2777981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
2787981c001SMika Westerberg 
2797981c001SMika Westerberg 	return pctrl->soc->groups[group].name;
2807981c001SMika Westerberg }
2817981c001SMika Westerberg 
2827981c001SMika Westerberg static int intel_get_group_pins(struct pinctrl_dev *pctldev, unsigned group,
2837981c001SMika Westerberg 			      const unsigned **pins, unsigned *npins)
2847981c001SMika Westerberg {
2857981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
2867981c001SMika Westerberg 
2877981c001SMika Westerberg 	*pins = pctrl->soc->groups[group].pins;
2887981c001SMika Westerberg 	*npins = pctrl->soc->groups[group].npins;
2897981c001SMika Westerberg 	return 0;
2907981c001SMika Westerberg }
2917981c001SMika Westerberg 
2927981c001SMika Westerberg static void intel_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
2937981c001SMika Westerberg 			       unsigned pin)
2947981c001SMika Westerberg {
2957981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
296e57725eaSMika Westerberg 	void __iomem *padcfg;
2977981c001SMika Westerberg 	u32 cfg0, cfg1, mode;
2987981c001SMika Westerberg 	bool locked, acpi;
2997981c001SMika Westerberg 
3007981c001SMika Westerberg 	if (!intel_pad_owned_by_host(pctrl, pin)) {
3017981c001SMika Westerberg 		seq_puts(s, "not available");
3027981c001SMika Westerberg 		return;
3037981c001SMika Westerberg 	}
3047981c001SMika Westerberg 
3057981c001SMika Westerberg 	cfg0 = readl(intel_get_padcfg(pctrl, pin, PADCFG0));
3067981c001SMika Westerberg 	cfg1 = readl(intel_get_padcfg(pctrl, pin, PADCFG1));
3077981c001SMika Westerberg 
3087981c001SMika Westerberg 	mode = (cfg0 & PADCFG0_PMODE_MASK) >> PADCFG0_PMODE_SHIFT;
3097981c001SMika Westerberg 	if (!mode)
3107981c001SMika Westerberg 		seq_puts(s, "GPIO ");
3117981c001SMika Westerberg 	else
3127981c001SMika Westerberg 		seq_printf(s, "mode %d ", mode);
3137981c001SMika Westerberg 
3147981c001SMika Westerberg 	seq_printf(s, "0x%08x 0x%08x", cfg0, cfg1);
3157981c001SMika Westerberg 
316e57725eaSMika Westerberg 	/* Dump the additional PADCFG registers if available */
317e57725eaSMika Westerberg 	padcfg = intel_get_padcfg(pctrl, pin, PADCFG2);
318e57725eaSMika Westerberg 	if (padcfg)
319e57725eaSMika Westerberg 		seq_printf(s, " 0x%08x", readl(padcfg));
320e57725eaSMika Westerberg 
3217981c001SMika Westerberg 	locked = intel_pad_locked(pctrl, pin);
3224341e8a5SMika Westerberg 	acpi = intel_pad_acpi_mode(pctrl, pin);
3237981c001SMika Westerberg 
3247981c001SMika Westerberg 	if (locked || acpi) {
3257981c001SMika Westerberg 		seq_puts(s, " [");
3267981c001SMika Westerberg 		if (locked) {
3277981c001SMika Westerberg 			seq_puts(s, "LOCKED");
3287981c001SMika Westerberg 			if (acpi)
3297981c001SMika Westerberg 				seq_puts(s, ", ");
3307981c001SMika Westerberg 		}
3317981c001SMika Westerberg 		if (acpi)
3327981c001SMika Westerberg 			seq_puts(s, "ACPI");
3337981c001SMika Westerberg 		seq_puts(s, "]");
3347981c001SMika Westerberg 	}
3357981c001SMika Westerberg }
3367981c001SMika Westerberg 
3377981c001SMika Westerberg static const struct pinctrl_ops intel_pinctrl_ops = {
3387981c001SMika Westerberg 	.get_groups_count = intel_get_groups_count,
3397981c001SMika Westerberg 	.get_group_name = intel_get_group_name,
3407981c001SMika Westerberg 	.get_group_pins = intel_get_group_pins,
3417981c001SMika Westerberg 	.pin_dbg_show = intel_pin_dbg_show,
3427981c001SMika Westerberg };
3437981c001SMika Westerberg 
3447981c001SMika Westerberg static int intel_get_functions_count(struct pinctrl_dev *pctldev)
3457981c001SMika Westerberg {
3467981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
3477981c001SMika Westerberg 
3487981c001SMika Westerberg 	return pctrl->soc->nfunctions;
3497981c001SMika Westerberg }
3507981c001SMika Westerberg 
3517981c001SMika Westerberg static const char *intel_get_function_name(struct pinctrl_dev *pctldev,
3527981c001SMika Westerberg 					   unsigned function)
3537981c001SMika Westerberg {
3547981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
3557981c001SMika Westerberg 
3567981c001SMika Westerberg 	return pctrl->soc->functions[function].name;
3577981c001SMika Westerberg }
3587981c001SMika Westerberg 
3597981c001SMika Westerberg static int intel_get_function_groups(struct pinctrl_dev *pctldev,
3607981c001SMika Westerberg 				     unsigned function,
3617981c001SMika Westerberg 				     const char * const **groups,
3627981c001SMika Westerberg 				     unsigned * const ngroups)
3637981c001SMika Westerberg {
3647981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
3657981c001SMika Westerberg 
3667981c001SMika Westerberg 	*groups = pctrl->soc->functions[function].groups;
3677981c001SMika Westerberg 	*ngroups = pctrl->soc->functions[function].ngroups;
3687981c001SMika Westerberg 	return 0;
3697981c001SMika Westerberg }
3707981c001SMika Westerberg 
3717981c001SMika Westerberg static int intel_pinmux_set_mux(struct pinctrl_dev *pctldev, unsigned function,
3727981c001SMika Westerberg 				unsigned group)
3737981c001SMika Westerberg {
3747981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
3757981c001SMika Westerberg 	const struct intel_pingroup *grp = &pctrl->soc->groups[group];
3767981c001SMika Westerberg 	unsigned long flags;
3777981c001SMika Westerberg 	int i;
3787981c001SMika Westerberg 
37927d9098cSMika Westerberg 	raw_spin_lock_irqsave(&pctrl->lock, flags);
3807981c001SMika Westerberg 
3817981c001SMika Westerberg 	/*
3827981c001SMika Westerberg 	 * All pins in the groups needs to be accessible and writable
3837981c001SMika Westerberg 	 * before we can enable the mux for this group.
3847981c001SMika Westerberg 	 */
3857981c001SMika Westerberg 	for (i = 0; i < grp->npins; i++) {
3867981c001SMika Westerberg 		if (!intel_pad_usable(pctrl, grp->pins[i])) {
38727d9098cSMika Westerberg 			raw_spin_unlock_irqrestore(&pctrl->lock, flags);
3887981c001SMika Westerberg 			return -EBUSY;
3897981c001SMika Westerberg 		}
3907981c001SMika Westerberg 	}
3917981c001SMika Westerberg 
3927981c001SMika Westerberg 	/* Now enable the mux setting for each pin in the group */
3937981c001SMika Westerberg 	for (i = 0; i < grp->npins; i++) {
3947981c001SMika Westerberg 		void __iomem *padcfg0;
3957981c001SMika Westerberg 		u32 value;
3967981c001SMika Westerberg 
3977981c001SMika Westerberg 		padcfg0 = intel_get_padcfg(pctrl, grp->pins[i], PADCFG0);
3987981c001SMika Westerberg 		value = readl(padcfg0);
3997981c001SMika Westerberg 
4007981c001SMika Westerberg 		value &= ~PADCFG0_PMODE_MASK;
401*1f6b419bSMika Westerberg 
402*1f6b419bSMika Westerberg 		if (grp->modes)
403*1f6b419bSMika Westerberg 			value |= grp->modes[i] << PADCFG0_PMODE_SHIFT;
404*1f6b419bSMika Westerberg 		else
4057981c001SMika Westerberg 			value |= grp->mode << PADCFG0_PMODE_SHIFT;
4067981c001SMika Westerberg 
4077981c001SMika Westerberg 		writel(value, padcfg0);
4087981c001SMika Westerberg 	}
4097981c001SMika Westerberg 
41027d9098cSMika Westerberg 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
4117981c001SMika Westerberg 
4127981c001SMika Westerberg 	return 0;
4137981c001SMika Westerberg }
4147981c001SMika Westerberg 
41517fab473SAndy Shevchenko static void __intel_gpio_set_direction(void __iomem *padcfg0, bool input)
41617fab473SAndy Shevchenko {
41717fab473SAndy Shevchenko 	u32 value;
41817fab473SAndy Shevchenko 
41917fab473SAndy Shevchenko 	value = readl(padcfg0);
42017fab473SAndy Shevchenko 	if (input) {
42117fab473SAndy Shevchenko 		value &= ~PADCFG0_GPIORXDIS;
42217fab473SAndy Shevchenko 		value |= PADCFG0_GPIOTXDIS;
42317fab473SAndy Shevchenko 	} else {
42417fab473SAndy Shevchenko 		value &= ~PADCFG0_GPIOTXDIS;
42517fab473SAndy Shevchenko 		value |= PADCFG0_GPIORXDIS;
42617fab473SAndy Shevchenko 	}
42717fab473SAndy Shevchenko 	writel(value, padcfg0);
42817fab473SAndy Shevchenko }
42917fab473SAndy Shevchenko 
4307981c001SMika Westerberg static int intel_gpio_request_enable(struct pinctrl_dev *pctldev,
4317981c001SMika Westerberg 				     struct pinctrl_gpio_range *range,
4327981c001SMika Westerberg 				     unsigned pin)
4337981c001SMika Westerberg {
4347981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
4357981c001SMika Westerberg 	void __iomem *padcfg0;
4367981c001SMika Westerberg 	unsigned long flags;
4377981c001SMika Westerberg 	u32 value;
4387981c001SMika Westerberg 
43927d9098cSMika Westerberg 	raw_spin_lock_irqsave(&pctrl->lock, flags);
4407981c001SMika Westerberg 
4417981c001SMika Westerberg 	if (!intel_pad_usable(pctrl, pin)) {
44227d9098cSMika Westerberg 		raw_spin_unlock_irqrestore(&pctrl->lock, flags);
4437981c001SMika Westerberg 		return -EBUSY;
4447981c001SMika Westerberg 	}
4457981c001SMika Westerberg 
4467981c001SMika Westerberg 	padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
4477981c001SMika Westerberg 	/* Put the pad into GPIO mode */
4487981c001SMika Westerberg 	value = readl(padcfg0) & ~PADCFG0_PMODE_MASK;
4497981c001SMika Westerberg 	/* Disable SCI/SMI/NMI generation */
4507981c001SMika Westerberg 	value &= ~(PADCFG0_GPIROUTIOXAPIC | PADCFG0_GPIROUTSCI);
4517981c001SMika Westerberg 	value &= ~(PADCFG0_GPIROUTSMI | PADCFG0_GPIROUTNMI);
4527981c001SMika Westerberg 	writel(value, padcfg0);
4537981c001SMika Westerberg 
45417fab473SAndy Shevchenko 	/* Disable TX buffer and enable RX (this will be input) */
45517fab473SAndy Shevchenko 	__intel_gpio_set_direction(padcfg0, true);
45617fab473SAndy Shevchenko 
45727d9098cSMika Westerberg 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
4587981c001SMika Westerberg 
4597981c001SMika Westerberg 	return 0;
4607981c001SMika Westerberg }
4617981c001SMika Westerberg 
4627981c001SMika Westerberg static int intel_gpio_set_direction(struct pinctrl_dev *pctldev,
4637981c001SMika Westerberg 				    struct pinctrl_gpio_range *range,
4647981c001SMika Westerberg 				    unsigned pin, bool input)
4657981c001SMika Westerberg {
4667981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
4677981c001SMika Westerberg 	void __iomem *padcfg0;
4687981c001SMika Westerberg 	unsigned long flags;
4697981c001SMika Westerberg 
47027d9098cSMika Westerberg 	raw_spin_lock_irqsave(&pctrl->lock, flags);
4717981c001SMika Westerberg 
4727981c001SMika Westerberg 	padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
47317fab473SAndy Shevchenko 	__intel_gpio_set_direction(padcfg0, input);
4747981c001SMika Westerberg 
47527d9098cSMika Westerberg 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
4767981c001SMika Westerberg 
4777981c001SMika Westerberg 	return 0;
4787981c001SMika Westerberg }
4797981c001SMika Westerberg 
4807981c001SMika Westerberg static const struct pinmux_ops intel_pinmux_ops = {
4817981c001SMika Westerberg 	.get_functions_count = intel_get_functions_count,
4827981c001SMika Westerberg 	.get_function_name = intel_get_function_name,
4837981c001SMika Westerberg 	.get_function_groups = intel_get_function_groups,
4847981c001SMika Westerberg 	.set_mux = intel_pinmux_set_mux,
4857981c001SMika Westerberg 	.gpio_request_enable = intel_gpio_request_enable,
4867981c001SMika Westerberg 	.gpio_set_direction = intel_gpio_set_direction,
4877981c001SMika Westerberg };
4887981c001SMika Westerberg 
4897981c001SMika Westerberg static int intel_config_get(struct pinctrl_dev *pctldev, unsigned pin,
4907981c001SMika Westerberg 			    unsigned long *config)
4917981c001SMika Westerberg {
4927981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
4937981c001SMika Westerberg 	enum pin_config_param param = pinconf_to_config_param(*config);
49404cc058fSMika Westerberg 	const struct intel_community *community;
4957981c001SMika Westerberg 	u32 value, term;
496e57725eaSMika Westerberg 	u32 arg = 0;
4977981c001SMika Westerberg 
4987981c001SMika Westerberg 	if (!intel_pad_owned_by_host(pctrl, pin))
4997981c001SMika Westerberg 		return -ENOTSUPP;
5007981c001SMika Westerberg 
50104cc058fSMika Westerberg 	community = intel_get_community(pctrl, pin);
5027981c001SMika Westerberg 	value = readl(intel_get_padcfg(pctrl, pin, PADCFG1));
5037981c001SMika Westerberg 	term = (value & PADCFG1_TERM_MASK) >> PADCFG1_TERM_SHIFT;
5047981c001SMika Westerberg 
5057981c001SMika Westerberg 	switch (param) {
5067981c001SMika Westerberg 	case PIN_CONFIG_BIAS_DISABLE:
5077981c001SMika Westerberg 		if (term)
5087981c001SMika Westerberg 			return -EINVAL;
5097981c001SMika Westerberg 		break;
5107981c001SMika Westerberg 
5117981c001SMika Westerberg 	case PIN_CONFIG_BIAS_PULL_UP:
5127981c001SMika Westerberg 		if (!term || !(value & PADCFG1_TERM_UP))
5137981c001SMika Westerberg 			return -EINVAL;
5147981c001SMika Westerberg 
5157981c001SMika Westerberg 		switch (term) {
5167981c001SMika Westerberg 		case PADCFG1_TERM_1K:
5177981c001SMika Westerberg 			arg = 1000;
5187981c001SMika Westerberg 			break;
5197981c001SMika Westerberg 		case PADCFG1_TERM_2K:
5207981c001SMika Westerberg 			arg = 2000;
5217981c001SMika Westerberg 			break;
5227981c001SMika Westerberg 		case PADCFG1_TERM_5K:
5237981c001SMika Westerberg 			arg = 5000;
5247981c001SMika Westerberg 			break;
5257981c001SMika Westerberg 		case PADCFG1_TERM_20K:
5267981c001SMika Westerberg 			arg = 20000;
5277981c001SMika Westerberg 			break;
5287981c001SMika Westerberg 		}
5297981c001SMika Westerberg 
5307981c001SMika Westerberg 		break;
5317981c001SMika Westerberg 
5327981c001SMika Westerberg 	case PIN_CONFIG_BIAS_PULL_DOWN:
5337981c001SMika Westerberg 		if (!term || value & PADCFG1_TERM_UP)
5347981c001SMika Westerberg 			return -EINVAL;
5357981c001SMika Westerberg 
5367981c001SMika Westerberg 		switch (term) {
53704cc058fSMika Westerberg 		case PADCFG1_TERM_1K:
53804cc058fSMika Westerberg 			if (!(community->features & PINCTRL_FEATURE_1K_PD))
53904cc058fSMika Westerberg 				return -EINVAL;
54004cc058fSMika Westerberg 			arg = 1000;
54104cc058fSMika Westerberg 			break;
5427981c001SMika Westerberg 		case PADCFG1_TERM_5K:
5437981c001SMika Westerberg 			arg = 5000;
5447981c001SMika Westerberg 			break;
5457981c001SMika Westerberg 		case PADCFG1_TERM_20K:
5467981c001SMika Westerberg 			arg = 20000;
5477981c001SMika Westerberg 			break;
5487981c001SMika Westerberg 		}
5497981c001SMika Westerberg 
5507981c001SMika Westerberg 		break;
5517981c001SMika Westerberg 
552e57725eaSMika Westerberg 	case PIN_CONFIG_INPUT_DEBOUNCE: {
553e57725eaSMika Westerberg 		void __iomem *padcfg2;
554e57725eaSMika Westerberg 		u32 v;
555e57725eaSMika Westerberg 
556e57725eaSMika Westerberg 		padcfg2 = intel_get_padcfg(pctrl, pin, PADCFG2);
557e57725eaSMika Westerberg 		if (!padcfg2)
558e57725eaSMika Westerberg 			return -ENOTSUPP;
559e57725eaSMika Westerberg 
560e57725eaSMika Westerberg 		v = readl(padcfg2);
561e57725eaSMika Westerberg 		if (!(v & PADCFG2_DEBEN))
562e57725eaSMika Westerberg 			return -EINVAL;
563e57725eaSMika Westerberg 
564e57725eaSMika Westerberg 		v = (v & PADCFG2_DEBOUNCE_MASK) >> PADCFG2_DEBOUNCE_SHIFT;
565e57725eaSMika Westerberg 		arg = BIT(v) * DEBOUNCE_PERIOD / 1000;
566e57725eaSMika Westerberg 
567e57725eaSMika Westerberg 		break;
568e57725eaSMika Westerberg 	}
569e57725eaSMika Westerberg 
5707981c001SMika Westerberg 	default:
5717981c001SMika Westerberg 		return -ENOTSUPP;
5727981c001SMika Westerberg 	}
5737981c001SMika Westerberg 
5747981c001SMika Westerberg 	*config = pinconf_to_config_packed(param, arg);
5757981c001SMika Westerberg 	return 0;
5767981c001SMika Westerberg }
5777981c001SMika Westerberg 
5787981c001SMika Westerberg static int intel_config_set_pull(struct intel_pinctrl *pctrl, unsigned pin,
5797981c001SMika Westerberg 				 unsigned long config)
5807981c001SMika Westerberg {
5817981c001SMika Westerberg 	unsigned param = pinconf_to_config_param(config);
5827981c001SMika Westerberg 	unsigned arg = pinconf_to_config_argument(config);
58304cc058fSMika Westerberg 	const struct intel_community *community;
5847981c001SMika Westerberg 	void __iomem *padcfg1;
5857981c001SMika Westerberg 	unsigned long flags;
5867981c001SMika Westerberg 	int ret = 0;
5877981c001SMika Westerberg 	u32 value;
5887981c001SMika Westerberg 
58927d9098cSMika Westerberg 	raw_spin_lock_irqsave(&pctrl->lock, flags);
5907981c001SMika Westerberg 
59104cc058fSMika Westerberg 	community = intel_get_community(pctrl, pin);
5927981c001SMika Westerberg 	padcfg1 = intel_get_padcfg(pctrl, pin, PADCFG1);
5937981c001SMika Westerberg 	value = readl(padcfg1);
5947981c001SMika Westerberg 
5957981c001SMika Westerberg 	switch (param) {
5967981c001SMika Westerberg 	case PIN_CONFIG_BIAS_DISABLE:
5977981c001SMika Westerberg 		value &= ~(PADCFG1_TERM_MASK | PADCFG1_TERM_UP);
5987981c001SMika Westerberg 		break;
5997981c001SMika Westerberg 
6007981c001SMika Westerberg 	case PIN_CONFIG_BIAS_PULL_UP:
6017981c001SMika Westerberg 		value &= ~PADCFG1_TERM_MASK;
6027981c001SMika Westerberg 
6037981c001SMika Westerberg 		value |= PADCFG1_TERM_UP;
6047981c001SMika Westerberg 
6057981c001SMika Westerberg 		switch (arg) {
6067981c001SMika Westerberg 		case 20000:
6077981c001SMika Westerberg 			value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT;
6087981c001SMika Westerberg 			break;
6097981c001SMika Westerberg 		case 5000:
6107981c001SMika Westerberg 			value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT;
6117981c001SMika Westerberg 			break;
6127981c001SMika Westerberg 		case 2000:
6137981c001SMika Westerberg 			value |= PADCFG1_TERM_2K << PADCFG1_TERM_SHIFT;
6147981c001SMika Westerberg 			break;
6157981c001SMika Westerberg 		case 1000:
6167981c001SMika Westerberg 			value |= PADCFG1_TERM_1K << PADCFG1_TERM_SHIFT;
6177981c001SMika Westerberg 			break;
6187981c001SMika Westerberg 		default:
6197981c001SMika Westerberg 			ret = -EINVAL;
6207981c001SMika Westerberg 		}
6217981c001SMika Westerberg 
6227981c001SMika Westerberg 		break;
6237981c001SMika Westerberg 
6247981c001SMika Westerberg 	case PIN_CONFIG_BIAS_PULL_DOWN:
6257981c001SMika Westerberg 		value &= ~(PADCFG1_TERM_UP | PADCFG1_TERM_MASK);
6267981c001SMika Westerberg 
6277981c001SMika Westerberg 		switch (arg) {
6287981c001SMika Westerberg 		case 20000:
6297981c001SMika Westerberg 			value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT;
6307981c001SMika Westerberg 			break;
6317981c001SMika Westerberg 		case 5000:
6327981c001SMika Westerberg 			value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT;
6337981c001SMika Westerberg 			break;
63404cc058fSMika Westerberg 		case 1000:
635aa1dd80fSDan Carpenter 			if (!(community->features & PINCTRL_FEATURE_1K_PD)) {
636aa1dd80fSDan Carpenter 				ret = -EINVAL;
637aa1dd80fSDan Carpenter 				break;
638aa1dd80fSDan Carpenter 			}
63904cc058fSMika Westerberg 			value |= PADCFG1_TERM_1K << PADCFG1_TERM_SHIFT;
64004cc058fSMika Westerberg 			break;
6417981c001SMika Westerberg 		default:
6427981c001SMika Westerberg 			ret = -EINVAL;
6437981c001SMika Westerberg 		}
6447981c001SMika Westerberg 
6457981c001SMika Westerberg 		break;
6467981c001SMika Westerberg 	}
6477981c001SMika Westerberg 
6487981c001SMika Westerberg 	if (!ret)
6497981c001SMika Westerberg 		writel(value, padcfg1);
6507981c001SMika Westerberg 
65127d9098cSMika Westerberg 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
6527981c001SMika Westerberg 
6537981c001SMika Westerberg 	return ret;
6547981c001SMika Westerberg }
6557981c001SMika Westerberg 
656e57725eaSMika Westerberg static int intel_config_set_debounce(struct intel_pinctrl *pctrl, unsigned pin,
657e57725eaSMika Westerberg 				     unsigned debounce)
658e57725eaSMika Westerberg {
659e57725eaSMika Westerberg 	void __iomem *padcfg0, *padcfg2;
660e57725eaSMika Westerberg 	unsigned long flags;
661e57725eaSMika Westerberg 	u32 value0, value2;
662e57725eaSMika Westerberg 	int ret = 0;
663e57725eaSMika Westerberg 
664e57725eaSMika Westerberg 	padcfg2 = intel_get_padcfg(pctrl, pin, PADCFG2);
665e57725eaSMika Westerberg 	if (!padcfg2)
666e57725eaSMika Westerberg 		return -ENOTSUPP;
667e57725eaSMika Westerberg 
668e57725eaSMika Westerberg 	padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
669e57725eaSMika Westerberg 
670e57725eaSMika Westerberg 	raw_spin_lock_irqsave(&pctrl->lock, flags);
671e57725eaSMika Westerberg 
672e57725eaSMika Westerberg 	value0 = readl(padcfg0);
673e57725eaSMika Westerberg 	value2 = readl(padcfg2);
674e57725eaSMika Westerberg 
675e57725eaSMika Westerberg 	/* Disable glitch filter and debouncer */
676e57725eaSMika Westerberg 	value0 &= ~PADCFG0_PREGFRXSEL;
677e57725eaSMika Westerberg 	value2 &= ~(PADCFG2_DEBEN | PADCFG2_DEBOUNCE_MASK);
678e57725eaSMika Westerberg 
679e57725eaSMika Westerberg 	if (debounce) {
680e57725eaSMika Westerberg 		unsigned long v;
681e57725eaSMika Westerberg 
682e57725eaSMika Westerberg 		v = order_base_2(debounce * 1000 / DEBOUNCE_PERIOD);
683e57725eaSMika Westerberg 		if (v < 3 || v > 15) {
684e57725eaSMika Westerberg 			ret = -EINVAL;
685e57725eaSMika Westerberg 			goto exit_unlock;
686e57725eaSMika Westerberg 		} else {
687e57725eaSMika Westerberg 			/* Enable glitch filter and debouncer */
688e57725eaSMika Westerberg 			value0 |= PADCFG0_PREGFRXSEL;
689e57725eaSMika Westerberg 			value2 |= v << PADCFG2_DEBOUNCE_SHIFT;
690e57725eaSMika Westerberg 			value2 |= PADCFG2_DEBEN;
691e57725eaSMika Westerberg 		}
692e57725eaSMika Westerberg 	}
693e57725eaSMika Westerberg 
694e57725eaSMika Westerberg 	writel(value0, padcfg0);
695e57725eaSMika Westerberg 	writel(value2, padcfg2);
696e57725eaSMika Westerberg 
697e57725eaSMika Westerberg exit_unlock:
698e57725eaSMika Westerberg 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
699e57725eaSMika Westerberg 
700e57725eaSMika Westerberg 	return ret;
701e57725eaSMika Westerberg }
702e57725eaSMika Westerberg 
7037981c001SMika Westerberg static int intel_config_set(struct pinctrl_dev *pctldev, unsigned pin,
7047981c001SMika Westerberg 			  unsigned long *configs, unsigned nconfigs)
7057981c001SMika Westerberg {
7067981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
7077981c001SMika Westerberg 	int i, ret;
7087981c001SMika Westerberg 
7097981c001SMika Westerberg 	if (!intel_pad_usable(pctrl, pin))
7107981c001SMika Westerberg 		return -ENOTSUPP;
7117981c001SMika Westerberg 
7127981c001SMika Westerberg 	for (i = 0; i < nconfigs; i++) {
7137981c001SMika Westerberg 		switch (pinconf_to_config_param(configs[i])) {
7147981c001SMika Westerberg 		case PIN_CONFIG_BIAS_DISABLE:
7157981c001SMika Westerberg 		case PIN_CONFIG_BIAS_PULL_UP:
7167981c001SMika Westerberg 		case PIN_CONFIG_BIAS_PULL_DOWN:
7177981c001SMika Westerberg 			ret = intel_config_set_pull(pctrl, pin, configs[i]);
7187981c001SMika Westerberg 			if (ret)
7197981c001SMika Westerberg 				return ret;
7207981c001SMika Westerberg 			break;
7217981c001SMika Westerberg 
722e57725eaSMika Westerberg 		case PIN_CONFIG_INPUT_DEBOUNCE:
723e57725eaSMika Westerberg 			ret = intel_config_set_debounce(pctrl, pin,
724e57725eaSMika Westerberg 				pinconf_to_config_argument(configs[i]));
725e57725eaSMika Westerberg 			if (ret)
726e57725eaSMika Westerberg 				return ret;
727e57725eaSMika Westerberg 			break;
728e57725eaSMika Westerberg 
7297981c001SMika Westerberg 		default:
7307981c001SMika Westerberg 			return -ENOTSUPP;
7317981c001SMika Westerberg 		}
7327981c001SMika Westerberg 	}
7337981c001SMika Westerberg 
7347981c001SMika Westerberg 	return 0;
7357981c001SMika Westerberg }
7367981c001SMika Westerberg 
7377981c001SMika Westerberg static const struct pinconf_ops intel_pinconf_ops = {
7387981c001SMika Westerberg 	.is_generic = true,
7397981c001SMika Westerberg 	.pin_config_get = intel_config_get,
7407981c001SMika Westerberg 	.pin_config_set = intel_config_set,
7417981c001SMika Westerberg };
7427981c001SMika Westerberg 
7437981c001SMika Westerberg static const struct pinctrl_desc intel_pinctrl_desc = {
7447981c001SMika Westerberg 	.pctlops = &intel_pinctrl_ops,
7457981c001SMika Westerberg 	.pmxops = &intel_pinmux_ops,
7467981c001SMika Westerberg 	.confops = &intel_pinconf_ops,
7477981c001SMika Westerberg 	.owner = THIS_MODULE,
7487981c001SMika Westerberg };
7497981c001SMika Westerberg 
7507981c001SMika Westerberg static int intel_gpio_get(struct gpio_chip *chip, unsigned offset)
7517981c001SMika Westerberg {
752acfd4c63SLinus Walleij 	struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
7537981c001SMika Westerberg 	void __iomem *reg;
7547981c001SMika Westerberg 
7557981c001SMika Westerberg 	reg = intel_get_padcfg(pctrl, offset, PADCFG0);
7567981c001SMika Westerberg 	if (!reg)
7577981c001SMika Westerberg 		return -EINVAL;
7587981c001SMika Westerberg 
7597981c001SMika Westerberg 	return !!(readl(reg) & PADCFG0_GPIORXSTATE);
7607981c001SMika Westerberg }
7617981c001SMika Westerberg 
7627981c001SMika Westerberg static void intel_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
7637981c001SMika Westerberg {
764acfd4c63SLinus Walleij 	struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
7657981c001SMika Westerberg 	void __iomem *reg;
7667981c001SMika Westerberg 
7677981c001SMika Westerberg 	reg = intel_get_padcfg(pctrl, offset, PADCFG0);
7687981c001SMika Westerberg 	if (reg) {
7697981c001SMika Westerberg 		unsigned long flags;
7707981c001SMika Westerberg 		u32 padcfg0;
7717981c001SMika Westerberg 
77227d9098cSMika Westerberg 		raw_spin_lock_irqsave(&pctrl->lock, flags);
7737981c001SMika Westerberg 		padcfg0 = readl(reg);
7747981c001SMika Westerberg 		if (value)
7757981c001SMika Westerberg 			padcfg0 |= PADCFG0_GPIOTXSTATE;
7767981c001SMika Westerberg 		else
7777981c001SMika Westerberg 			padcfg0 &= ~PADCFG0_GPIOTXSTATE;
7787981c001SMika Westerberg 		writel(padcfg0, reg);
77927d9098cSMika Westerberg 		raw_spin_unlock_irqrestore(&pctrl->lock, flags);
7807981c001SMika Westerberg 	}
7817981c001SMika Westerberg }
7827981c001SMika Westerberg 
7837981c001SMika Westerberg static int intel_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
7847981c001SMika Westerberg {
7857981c001SMika Westerberg 	return pinctrl_gpio_direction_input(chip->base + offset);
7867981c001SMika Westerberg }
7877981c001SMika Westerberg 
7887981c001SMika Westerberg static int intel_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
7897981c001SMika Westerberg 				       int value)
7907981c001SMika Westerberg {
7917981c001SMika Westerberg 	intel_gpio_set(chip, offset, value);
7927981c001SMika Westerberg 	return pinctrl_gpio_direction_output(chip->base + offset);
7937981c001SMika Westerberg }
7947981c001SMika Westerberg 
7957981c001SMika Westerberg static const struct gpio_chip intel_gpio_chip = {
7967981c001SMika Westerberg 	.owner = THIS_MODULE,
79798c85d58SJonas Gorski 	.request = gpiochip_generic_request,
79898c85d58SJonas Gorski 	.free = gpiochip_generic_free,
7997981c001SMika Westerberg 	.direction_input = intel_gpio_direction_input,
8007981c001SMika Westerberg 	.direction_output = intel_gpio_direction_output,
8017981c001SMika Westerberg 	.get = intel_gpio_get,
8027981c001SMika Westerberg 	.set = intel_gpio_set,
803e57725eaSMika Westerberg 	.set_config = gpiochip_generic_config,
8047981c001SMika Westerberg };
8057981c001SMika Westerberg 
8067981c001SMika Westerberg static void intel_gpio_irq_ack(struct irq_data *d)
8077981c001SMika Westerberg {
8087981c001SMika Westerberg 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
809acfd4c63SLinus Walleij 	struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
8107981c001SMika Westerberg 	const struct intel_community *community;
8117981c001SMika Westerberg 	unsigned pin = irqd_to_hwirq(d);
8127981c001SMika Westerberg 
8137981c001SMika Westerberg 	community = intel_get_community(pctrl, pin);
8147981c001SMika Westerberg 	if (community) {
815919eb475SMika Westerberg 		const struct intel_padgroup *padgrp;
816919eb475SMika Westerberg 		unsigned gpp, gpp_offset;
8177981c001SMika Westerberg 
818919eb475SMika Westerberg 		padgrp = intel_community_get_padgroup(community, pin);
819919eb475SMika Westerberg 		if (!padgrp)
820919eb475SMika Westerberg 			return;
821919eb475SMika Westerberg 
822919eb475SMika Westerberg 		gpp = padgrp->reg_num;
823919eb475SMika Westerberg 		gpp_offset = padgroup_offset(padgrp, pin);
824919eb475SMika Westerberg 
825919eb475SMika Westerberg 		raw_spin_lock(&pctrl->lock);
8267981c001SMika Westerberg 		writel(BIT(gpp_offset), community->regs + GPI_IS + gpp * 4);
82727d9098cSMika Westerberg 		raw_spin_unlock(&pctrl->lock);
8287981c001SMika Westerberg 	}
829919eb475SMika Westerberg }
8307981c001SMika Westerberg 
831a939bb57SQi Zheng static void intel_gpio_irq_enable(struct irq_data *d)
832a939bb57SQi Zheng {
833a939bb57SQi Zheng 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
834a939bb57SQi Zheng 	struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
835a939bb57SQi Zheng 	const struct intel_community *community;
836a939bb57SQi Zheng 	unsigned pin = irqd_to_hwirq(d);
837a939bb57SQi Zheng 
838a939bb57SQi Zheng 	community = intel_get_community(pctrl, pin);
839a939bb57SQi Zheng 	if (community) {
840919eb475SMika Westerberg 		const struct intel_padgroup *padgrp;
841919eb475SMika Westerberg 		unsigned gpp, gpp_offset;
842919eb475SMika Westerberg 		unsigned long flags;
843a939bb57SQi Zheng 		u32 value;
844a939bb57SQi Zheng 
845919eb475SMika Westerberg 		padgrp = intel_community_get_padgroup(community, pin);
846919eb475SMika Westerberg 		if (!padgrp)
847919eb475SMika Westerberg 			return;
848919eb475SMika Westerberg 
849919eb475SMika Westerberg 		gpp = padgrp->reg_num;
850919eb475SMika Westerberg 		gpp_offset = padgroup_offset(padgrp, pin);
851919eb475SMika Westerberg 
852919eb475SMika Westerberg 		raw_spin_lock_irqsave(&pctrl->lock, flags);
853a939bb57SQi Zheng 		/* Clear interrupt status first to avoid unexpected interrupt */
854a939bb57SQi Zheng 		writel(BIT(gpp_offset), community->regs + GPI_IS + gpp * 4);
855a939bb57SQi Zheng 
856a939bb57SQi Zheng 		value = readl(community->regs + community->ie_offset + gpp * 4);
857a939bb57SQi Zheng 		value |= BIT(gpp_offset);
858a939bb57SQi Zheng 		writel(value, community->regs + community->ie_offset + gpp * 4);
85927d9098cSMika Westerberg 		raw_spin_unlock_irqrestore(&pctrl->lock, flags);
860a939bb57SQi Zheng 	}
861919eb475SMika Westerberg }
862a939bb57SQi Zheng 
8637981c001SMika Westerberg static void intel_gpio_irq_mask_unmask(struct irq_data *d, bool mask)
8647981c001SMika Westerberg {
8657981c001SMika Westerberg 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
866acfd4c63SLinus Walleij 	struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
8677981c001SMika Westerberg 	const struct intel_community *community;
8687981c001SMika Westerberg 	unsigned pin = irqd_to_hwirq(d);
8697981c001SMika Westerberg 
8707981c001SMika Westerberg 	community = intel_get_community(pctrl, pin);
8717981c001SMika Westerberg 	if (community) {
872919eb475SMika Westerberg 		const struct intel_padgroup *padgrp;
873919eb475SMika Westerberg 		unsigned gpp, gpp_offset;
874919eb475SMika Westerberg 		unsigned long flags;
8757981c001SMika Westerberg 		void __iomem *reg;
8767981c001SMika Westerberg 		u32 value;
8777981c001SMika Westerberg 
878919eb475SMika Westerberg 		padgrp = intel_community_get_padgroup(community, pin);
879919eb475SMika Westerberg 		if (!padgrp)
880919eb475SMika Westerberg 			return;
881919eb475SMika Westerberg 
882919eb475SMika Westerberg 		gpp = padgrp->reg_num;
883919eb475SMika Westerberg 		gpp_offset = padgroup_offset(padgrp, pin);
884919eb475SMika Westerberg 
8857981c001SMika Westerberg 		reg = community->regs + community->ie_offset + gpp * 4;
886919eb475SMika Westerberg 
887919eb475SMika Westerberg 		raw_spin_lock_irqsave(&pctrl->lock, flags);
8887981c001SMika Westerberg 		value = readl(reg);
8897981c001SMika Westerberg 		if (mask)
8907981c001SMika Westerberg 			value &= ~BIT(gpp_offset);
8917981c001SMika Westerberg 		else
8927981c001SMika Westerberg 			value |= BIT(gpp_offset);
8937981c001SMika Westerberg 		writel(value, reg);
89427d9098cSMika Westerberg 		raw_spin_unlock_irqrestore(&pctrl->lock, flags);
8957981c001SMika Westerberg 	}
896919eb475SMika Westerberg }
8977981c001SMika Westerberg 
8987981c001SMika Westerberg static void intel_gpio_irq_mask(struct irq_data *d)
8997981c001SMika Westerberg {
9007981c001SMika Westerberg 	intel_gpio_irq_mask_unmask(d, true);
9017981c001SMika Westerberg }
9027981c001SMika Westerberg 
9037981c001SMika Westerberg static void intel_gpio_irq_unmask(struct irq_data *d)
9047981c001SMika Westerberg {
9057981c001SMika Westerberg 	intel_gpio_irq_mask_unmask(d, false);
9067981c001SMika Westerberg }
9077981c001SMika Westerberg 
9087981c001SMika Westerberg static int intel_gpio_irq_type(struct irq_data *d, unsigned type)
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 	unsigned pin = irqd_to_hwirq(d);
9137981c001SMika Westerberg 	unsigned long flags;
9147981c001SMika Westerberg 	void __iomem *reg;
9157981c001SMika Westerberg 	u32 value;
9167981c001SMika Westerberg 
9177981c001SMika Westerberg 	reg = intel_get_padcfg(pctrl, pin, PADCFG0);
9187981c001SMika Westerberg 	if (!reg)
9197981c001SMika Westerberg 		return -EINVAL;
9207981c001SMika Westerberg 
9214341e8a5SMika Westerberg 	/*
9224341e8a5SMika Westerberg 	 * If the pin is in ACPI mode it is still usable as a GPIO but it
9234341e8a5SMika Westerberg 	 * cannot be used as IRQ because GPI_IS status bit will not be
9244341e8a5SMika Westerberg 	 * updated by the host controller hardware.
9254341e8a5SMika Westerberg 	 */
9264341e8a5SMika Westerberg 	if (intel_pad_acpi_mode(pctrl, pin)) {
9274341e8a5SMika Westerberg 		dev_warn(pctrl->dev, "pin %u cannot be used as IRQ\n", pin);
9284341e8a5SMika Westerberg 		return -EPERM;
9294341e8a5SMika Westerberg 	}
9304341e8a5SMika Westerberg 
93127d9098cSMika Westerberg 	raw_spin_lock_irqsave(&pctrl->lock, flags);
9327981c001SMika Westerberg 
9337981c001SMika Westerberg 	value = readl(reg);
9347981c001SMika Westerberg 
9357981c001SMika Westerberg 	value &= ~(PADCFG0_RXEVCFG_MASK | PADCFG0_RXINV);
9367981c001SMika Westerberg 
9377981c001SMika Westerberg 	if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
9387981c001SMika Westerberg 		value |= PADCFG0_RXEVCFG_EDGE_BOTH << PADCFG0_RXEVCFG_SHIFT;
9397981c001SMika Westerberg 	} else if (type & IRQ_TYPE_EDGE_FALLING) {
9407981c001SMika Westerberg 		value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT;
9417981c001SMika Westerberg 		value |= PADCFG0_RXINV;
9427981c001SMika Westerberg 	} else if (type & IRQ_TYPE_EDGE_RISING) {
9437981c001SMika Westerberg 		value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT;
944bf380cfaSQipeng Zha 	} else if (type & IRQ_TYPE_LEVEL_MASK) {
945bf380cfaSQipeng Zha 		if (type & IRQ_TYPE_LEVEL_LOW)
9467981c001SMika Westerberg 			value |= PADCFG0_RXINV;
9477981c001SMika Westerberg 	} else {
9487981c001SMika Westerberg 		value |= PADCFG0_RXEVCFG_DISABLED << PADCFG0_RXEVCFG_SHIFT;
9497981c001SMika Westerberg 	}
9507981c001SMika Westerberg 
9517981c001SMika Westerberg 	writel(value, reg);
9527981c001SMika Westerberg 
9537981c001SMika Westerberg 	if (type & IRQ_TYPE_EDGE_BOTH)
954fc756bcdSThomas Gleixner 		irq_set_handler_locked(d, handle_edge_irq);
9557981c001SMika Westerberg 	else if (type & IRQ_TYPE_LEVEL_MASK)
956fc756bcdSThomas Gleixner 		irq_set_handler_locked(d, handle_level_irq);
9577981c001SMika Westerberg 
95827d9098cSMika Westerberg 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
9597981c001SMika Westerberg 
9607981c001SMika Westerberg 	return 0;
9617981c001SMika Westerberg }
9627981c001SMika Westerberg 
9637981c001SMika Westerberg static int intel_gpio_irq_wake(struct irq_data *d, unsigned int on)
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);
9677981c001SMika Westerberg 	unsigned pin = irqd_to_hwirq(d);
9687981c001SMika Westerberg 
9697981c001SMika Westerberg 	if (on)
97001dabe91SNilesh Bacchewar 		enable_irq_wake(pctrl->irq);
9717981c001SMika Westerberg 	else
97201dabe91SNilesh Bacchewar 		disable_irq_wake(pctrl->irq);
9739a520fd9SAndy Shevchenko 
9747981c001SMika Westerberg 	dev_dbg(pctrl->dev, "%sable wake for pin %u\n", on ? "en" : "dis", pin);
9757981c001SMika Westerberg 	return 0;
9767981c001SMika Westerberg }
9777981c001SMika Westerberg 
978193b40c8SMika Westerberg static irqreturn_t intel_gpio_community_irq_handler(struct intel_pinctrl *pctrl,
9797981c001SMika Westerberg 	const struct intel_community *community)
9807981c001SMika Westerberg {
981193b40c8SMika Westerberg 	struct gpio_chip *gc = &pctrl->chip;
982193b40c8SMika Westerberg 	irqreturn_t ret = IRQ_NONE;
9837981c001SMika Westerberg 	int gpp;
9847981c001SMika Westerberg 
9857981c001SMika Westerberg 	for (gpp = 0; gpp < community->ngpps; gpp++) {
986919eb475SMika Westerberg 		const struct intel_padgroup *padgrp = &community->gpps[gpp];
9877981c001SMika Westerberg 		unsigned long pending, enabled, gpp_offset;
9887981c001SMika Westerberg 
989919eb475SMika Westerberg 		pending = readl(community->regs + GPI_IS + padgrp->reg_num * 4);
9907981c001SMika Westerberg 		enabled = readl(community->regs + community->ie_offset +
991919eb475SMika Westerberg 				padgrp->reg_num * 4);
9927981c001SMika Westerberg 
9937981c001SMika Westerberg 		/* Only interrupts that are enabled */
9947981c001SMika Westerberg 		pending &= enabled;
9957981c001SMika Westerberg 
996919eb475SMika Westerberg 		for_each_set_bit(gpp_offset, &pending, padgrp->size) {
9977981c001SMika Westerberg 			unsigned padno, irq;
9987981c001SMika Westerberg 
999919eb475SMika Westerberg 			padno = padgrp->base - community->pin_base + gpp_offset;
10007981c001SMika Westerberg 			if (padno >= community->npins)
10017981c001SMika Westerberg 				break;
10027981c001SMika Westerberg 
10037981c001SMika Westerberg 			irq = irq_find_mapping(gc->irqdomain,
10047981c001SMika Westerberg 					       community->pin_base + padno);
10057981c001SMika Westerberg 			generic_handle_irq(irq);
1006193b40c8SMika Westerberg 
1007193b40c8SMika Westerberg 			ret |= IRQ_HANDLED;
10087981c001SMika Westerberg 		}
10097981c001SMika Westerberg 	}
10107981c001SMika Westerberg 
1011193b40c8SMika Westerberg 	return ret;
1012193b40c8SMika Westerberg }
1013193b40c8SMika Westerberg 
1014193b40c8SMika Westerberg static irqreturn_t intel_gpio_irq(int irq, void *data)
10157981c001SMika Westerberg {
1016193b40c8SMika Westerberg 	const struct intel_community *community;
1017193b40c8SMika Westerberg 	struct intel_pinctrl *pctrl = data;
1018193b40c8SMika Westerberg 	irqreturn_t ret = IRQ_NONE;
10197981c001SMika Westerberg 	int i;
10207981c001SMika Westerberg 
10217981c001SMika Westerberg 	/* Need to check all communities for pending interrupts */
1022193b40c8SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
1023193b40c8SMika Westerberg 		community = &pctrl->communities[i];
1024193b40c8SMika Westerberg 		ret |= intel_gpio_community_irq_handler(pctrl, community);
1025193b40c8SMika Westerberg 	}
10267981c001SMika Westerberg 
1027193b40c8SMika Westerberg 	return ret;
10287981c001SMika Westerberg }
10297981c001SMika Westerberg 
10307981c001SMika Westerberg static struct irq_chip intel_gpio_irqchip = {
10317981c001SMika Westerberg 	.name = "intel-gpio",
1032a939bb57SQi Zheng 	.irq_enable = intel_gpio_irq_enable,
10337981c001SMika Westerberg 	.irq_ack = intel_gpio_irq_ack,
10347981c001SMika Westerberg 	.irq_mask = intel_gpio_irq_mask,
10357981c001SMika Westerberg 	.irq_unmask = intel_gpio_irq_unmask,
10367981c001SMika Westerberg 	.irq_set_type = intel_gpio_irq_type,
10377981c001SMika Westerberg 	.irq_set_wake = intel_gpio_irq_wake,
10387981c001SMika Westerberg };
10397981c001SMika Westerberg 
10407981c001SMika Westerberg static int intel_gpio_probe(struct intel_pinctrl *pctrl, int irq)
10417981c001SMika Westerberg {
10427981c001SMika Westerberg 	int ret;
10437981c001SMika Westerberg 
10447981c001SMika Westerberg 	pctrl->chip = intel_gpio_chip;
10457981c001SMika Westerberg 
10467981c001SMika Westerberg 	pctrl->chip.ngpio = pctrl->soc->npins;
10477981c001SMika Westerberg 	pctrl->chip.label = dev_name(pctrl->dev);
104858383c78SLinus Walleij 	pctrl->chip.parent = pctrl->dev;
10497981c001SMika Westerberg 	pctrl->chip.base = -1;
105001dabe91SNilesh Bacchewar 	pctrl->irq = irq;
10517981c001SMika Westerberg 
1052f25c3aa9SMika Westerberg 	ret = devm_gpiochip_add_data(pctrl->dev, &pctrl->chip, pctrl);
10537981c001SMika Westerberg 	if (ret) {
10547981c001SMika Westerberg 		dev_err(pctrl->dev, "failed to register gpiochip\n");
10557981c001SMika Westerberg 		return ret;
10567981c001SMika Westerberg 	}
10577981c001SMika Westerberg 
10587981c001SMika Westerberg 	ret = gpiochip_add_pin_range(&pctrl->chip, dev_name(pctrl->dev),
10597981c001SMika Westerberg 				     0, 0, pctrl->soc->npins);
10607981c001SMika Westerberg 	if (ret) {
10617981c001SMika Westerberg 		dev_err(pctrl->dev, "failed to add GPIO pin range\n");
1062f25c3aa9SMika Westerberg 		return ret;
1063193b40c8SMika Westerberg 	}
1064193b40c8SMika Westerberg 
1065193b40c8SMika Westerberg 	/*
1066193b40c8SMika Westerberg 	 * We need to request the interrupt here (instead of providing chip
1067193b40c8SMika Westerberg 	 * to the irq directly) because on some platforms several GPIO
1068193b40c8SMika Westerberg 	 * controllers share the same interrupt line.
1069193b40c8SMika Westerberg 	 */
10701a7d1cb8SMika Westerberg 	ret = devm_request_irq(pctrl->dev, irq, intel_gpio_irq,
10711a7d1cb8SMika Westerberg 			       IRQF_SHARED | IRQF_NO_THREAD,
1072193b40c8SMika Westerberg 			       dev_name(pctrl->dev), pctrl);
1073193b40c8SMika Westerberg 	if (ret) {
1074193b40c8SMika Westerberg 		dev_err(pctrl->dev, "failed to request interrupt\n");
1075f25c3aa9SMika Westerberg 		return ret;
10767981c001SMika Westerberg 	}
10777981c001SMika Westerberg 
10787981c001SMika Westerberg 	ret = gpiochip_irqchip_add(&pctrl->chip, &intel_gpio_irqchip, 0,
10793ae02c14SAndy Shevchenko 				   handle_bad_irq, IRQ_TYPE_NONE);
10807981c001SMika Westerberg 	if (ret) {
10817981c001SMika Westerberg 		dev_err(pctrl->dev, "failed to add irqchip\n");
1082f25c3aa9SMika Westerberg 		return ret;
10837981c001SMika Westerberg 	}
10847981c001SMika Westerberg 
10857981c001SMika Westerberg 	gpiochip_set_chained_irqchip(&pctrl->chip, &intel_gpio_irqchip, irq,
1086193b40c8SMika Westerberg 				     NULL);
10877981c001SMika Westerberg 	return 0;
10887981c001SMika Westerberg }
10897981c001SMika Westerberg 
1090919eb475SMika Westerberg static int intel_pinctrl_add_padgroups(struct intel_pinctrl *pctrl,
1091919eb475SMika Westerberg 				       struct intel_community *community)
1092919eb475SMika Westerberg {
1093919eb475SMika Westerberg 	struct intel_padgroup *gpps;
1094919eb475SMika Westerberg 	unsigned npins = community->npins;
1095919eb475SMika Westerberg 	unsigned padown_num = 0;
1096919eb475SMika Westerberg 	size_t ngpps, i;
1097919eb475SMika Westerberg 
1098919eb475SMika Westerberg 	if (community->gpps)
1099919eb475SMika Westerberg 		ngpps = community->ngpps;
1100919eb475SMika Westerberg 	else
1101919eb475SMika Westerberg 		ngpps = DIV_ROUND_UP(community->npins, community->gpp_size);
1102919eb475SMika Westerberg 
1103919eb475SMika Westerberg 	gpps = devm_kcalloc(pctrl->dev, ngpps, sizeof(*gpps), GFP_KERNEL);
1104919eb475SMika Westerberg 	if (!gpps)
1105919eb475SMika Westerberg 		return -ENOMEM;
1106919eb475SMika Westerberg 
1107919eb475SMika Westerberg 	for (i = 0; i < ngpps; i++) {
1108919eb475SMika Westerberg 		if (community->gpps) {
1109919eb475SMika Westerberg 			gpps[i] = community->gpps[i];
1110919eb475SMika Westerberg 		} else {
1111919eb475SMika Westerberg 			unsigned gpp_size = community->gpp_size;
1112919eb475SMika Westerberg 
1113919eb475SMika Westerberg 			gpps[i].reg_num = i;
1114919eb475SMika Westerberg 			gpps[i].base = community->pin_base + i * gpp_size;
1115919eb475SMika Westerberg 			gpps[i].size = min(gpp_size, npins);
1116919eb475SMika Westerberg 			npins -= gpps[i].size;
1117919eb475SMika Westerberg 		}
1118919eb475SMika Westerberg 
1119919eb475SMika Westerberg 		if (gpps[i].size > 32)
1120919eb475SMika Westerberg 			return -EINVAL;
1121919eb475SMika Westerberg 
1122919eb475SMika Westerberg 		gpps[i].padown_num = padown_num;
1123919eb475SMika Westerberg 
1124919eb475SMika Westerberg 		/*
1125919eb475SMika Westerberg 		 * In older hardware the number of padown registers per
1126919eb475SMika Westerberg 		 * group is fixed regardless of the group size.
1127919eb475SMika Westerberg 		 */
1128919eb475SMika Westerberg 		if (community->gpp_num_padown_regs)
1129919eb475SMika Westerberg 			padown_num += community->gpp_num_padown_regs;
1130919eb475SMika Westerberg 		else
1131919eb475SMika Westerberg 			padown_num += DIV_ROUND_UP(gpps[i].size * 4, 32);
1132919eb475SMika Westerberg 	}
1133919eb475SMika Westerberg 
1134919eb475SMika Westerberg 	community->ngpps = ngpps;
1135919eb475SMika Westerberg 	community->gpps = gpps;
1136919eb475SMika Westerberg 
1137919eb475SMika Westerberg 	return 0;
1138919eb475SMika Westerberg }
1139919eb475SMika Westerberg 
11407981c001SMika Westerberg static int intel_pinctrl_pm_init(struct intel_pinctrl *pctrl)
11417981c001SMika Westerberg {
11427981c001SMika Westerberg #ifdef CONFIG_PM_SLEEP
11437981c001SMika Westerberg 	const struct intel_pinctrl_soc_data *soc = pctrl->soc;
11447981c001SMika Westerberg 	struct intel_community_context *communities;
11457981c001SMika Westerberg 	struct intel_pad_context *pads;
11467981c001SMika Westerberg 	int i;
11477981c001SMika Westerberg 
11487981c001SMika Westerberg 	pads = devm_kcalloc(pctrl->dev, soc->npins, sizeof(*pads), GFP_KERNEL);
11497981c001SMika Westerberg 	if (!pads)
11507981c001SMika Westerberg 		return -ENOMEM;
11517981c001SMika Westerberg 
11527981c001SMika Westerberg 	communities = devm_kcalloc(pctrl->dev, pctrl->ncommunities,
11537981c001SMika Westerberg 				   sizeof(*communities), GFP_KERNEL);
11547981c001SMika Westerberg 	if (!communities)
11557981c001SMika Westerberg 		return -ENOMEM;
11567981c001SMika Westerberg 
11577981c001SMika Westerberg 
11587981c001SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
11597981c001SMika Westerberg 		struct intel_community *community = &pctrl->communities[i];
11607981c001SMika Westerberg 		u32 *intmask;
11617981c001SMika Westerberg 
11627981c001SMika Westerberg 		intmask = devm_kcalloc(pctrl->dev, community->ngpps,
11637981c001SMika Westerberg 				       sizeof(*intmask), GFP_KERNEL);
11647981c001SMika Westerberg 		if (!intmask)
11657981c001SMika Westerberg 			return -ENOMEM;
11667981c001SMika Westerberg 
11677981c001SMika Westerberg 		communities[i].intmask = intmask;
11687981c001SMika Westerberg 	}
11697981c001SMika Westerberg 
11707981c001SMika Westerberg 	pctrl->context.pads = pads;
11717981c001SMika Westerberg 	pctrl->context.communities = communities;
11727981c001SMika Westerberg #endif
11737981c001SMika Westerberg 
11747981c001SMika Westerberg 	return 0;
11757981c001SMika Westerberg }
11767981c001SMika Westerberg 
11777981c001SMika Westerberg int intel_pinctrl_probe(struct platform_device *pdev,
11787981c001SMika Westerberg 			const struct intel_pinctrl_soc_data *soc_data)
11797981c001SMika Westerberg {
11807981c001SMika Westerberg 	struct intel_pinctrl *pctrl;
11817981c001SMika Westerberg 	int i, ret, irq;
11827981c001SMika Westerberg 
11837981c001SMika Westerberg 	if (!soc_data)
11847981c001SMika Westerberg 		return -EINVAL;
11857981c001SMika Westerberg 
11867981c001SMika Westerberg 	pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL);
11877981c001SMika Westerberg 	if (!pctrl)
11887981c001SMika Westerberg 		return -ENOMEM;
11897981c001SMika Westerberg 
11907981c001SMika Westerberg 	pctrl->dev = &pdev->dev;
11917981c001SMika Westerberg 	pctrl->soc = soc_data;
119227d9098cSMika Westerberg 	raw_spin_lock_init(&pctrl->lock);
11937981c001SMika Westerberg 
11947981c001SMika Westerberg 	/*
11957981c001SMika Westerberg 	 * Make a copy of the communities which we can use to hold pointers
11967981c001SMika Westerberg 	 * to the registers.
11977981c001SMika Westerberg 	 */
11987981c001SMika Westerberg 	pctrl->ncommunities = pctrl->soc->ncommunities;
11997981c001SMika Westerberg 	pctrl->communities = devm_kcalloc(&pdev->dev, pctrl->ncommunities,
12007981c001SMika Westerberg 				  sizeof(*pctrl->communities), GFP_KERNEL);
12017981c001SMika Westerberg 	if (!pctrl->communities)
12027981c001SMika Westerberg 		return -ENOMEM;
12037981c001SMika Westerberg 
12047981c001SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
12057981c001SMika Westerberg 		struct intel_community *community = &pctrl->communities[i];
12067981c001SMika Westerberg 		struct resource *res;
12077981c001SMika Westerberg 		void __iomem *regs;
12087981c001SMika Westerberg 		u32 padbar;
12097981c001SMika Westerberg 
12107981c001SMika Westerberg 		*community = pctrl->soc->communities[i];
12117981c001SMika Westerberg 
12127981c001SMika Westerberg 		res = platform_get_resource(pdev, IORESOURCE_MEM,
12137981c001SMika Westerberg 					    community->barno);
12147981c001SMika Westerberg 		regs = devm_ioremap_resource(&pdev->dev, res);
12157981c001SMika Westerberg 		if (IS_ERR(regs))
12167981c001SMika Westerberg 			return PTR_ERR(regs);
12177981c001SMika Westerberg 
1218e57725eaSMika Westerberg 		/*
1219e57725eaSMika Westerberg 		 * Determine community features based on the revision if
1220e57725eaSMika Westerberg 		 * not specified already.
1221e57725eaSMika Westerberg 		 */
1222e57725eaSMika Westerberg 		if (!community->features) {
1223e57725eaSMika Westerberg 			u32 rev;
1224e57725eaSMika Westerberg 
1225e57725eaSMika Westerberg 			rev = (readl(regs + REVID) & REVID_MASK) >> REVID_SHIFT;
122604cc058fSMika Westerberg 			if (rev >= 0x94) {
1227e57725eaSMika Westerberg 				community->features |= PINCTRL_FEATURE_DEBOUNCE;
122804cc058fSMika Westerberg 				community->features |= PINCTRL_FEATURE_1K_PD;
122904cc058fSMika Westerberg 			}
1230e57725eaSMika Westerberg 		}
1231e57725eaSMika Westerberg 
12327981c001SMika Westerberg 		/* Read offset of the pad configuration registers */
12337981c001SMika Westerberg 		padbar = readl(regs + PADBAR);
12347981c001SMika Westerberg 
12357981c001SMika Westerberg 		community->regs = regs;
12367981c001SMika Westerberg 		community->pad_regs = regs + padbar;
1237919eb475SMika Westerberg 
1238919eb475SMika Westerberg 		ret = intel_pinctrl_add_padgroups(pctrl, community);
1239919eb475SMika Westerberg 		if (ret)
1240919eb475SMika Westerberg 			return ret;
12417981c001SMika Westerberg 	}
12427981c001SMika Westerberg 
12437981c001SMika Westerberg 	irq = platform_get_irq(pdev, 0);
12447981c001SMika Westerberg 	if (irq < 0) {
12457981c001SMika Westerberg 		dev_err(&pdev->dev, "failed to get interrupt number\n");
12467981c001SMika Westerberg 		return irq;
12477981c001SMika Westerberg 	}
12487981c001SMika Westerberg 
12497981c001SMika Westerberg 	ret = intel_pinctrl_pm_init(pctrl);
12507981c001SMika Westerberg 	if (ret)
12517981c001SMika Westerberg 		return ret;
12527981c001SMika Westerberg 
12537981c001SMika Westerberg 	pctrl->pctldesc = intel_pinctrl_desc;
12547981c001SMika Westerberg 	pctrl->pctldesc.name = dev_name(&pdev->dev);
12557981c001SMika Westerberg 	pctrl->pctldesc.pins = pctrl->soc->pins;
12567981c001SMika Westerberg 	pctrl->pctldesc.npins = pctrl->soc->npins;
12577981c001SMika Westerberg 
125854d46cd7SLaxman Dewangan 	pctrl->pctldev = devm_pinctrl_register(&pdev->dev, &pctrl->pctldesc,
125954d46cd7SLaxman Dewangan 					       pctrl);
1260323de9efSMasahiro Yamada 	if (IS_ERR(pctrl->pctldev)) {
12617981c001SMika Westerberg 		dev_err(&pdev->dev, "failed to register pinctrl driver\n");
1262323de9efSMasahiro Yamada 		return PTR_ERR(pctrl->pctldev);
12637981c001SMika Westerberg 	}
12647981c001SMika Westerberg 
12657981c001SMika Westerberg 	ret = intel_gpio_probe(pctrl, irq);
126654d46cd7SLaxman Dewangan 	if (ret)
12677981c001SMika Westerberg 		return ret;
12687981c001SMika Westerberg 
12697981c001SMika Westerberg 	platform_set_drvdata(pdev, pctrl);
12707981c001SMika Westerberg 
12717981c001SMika Westerberg 	return 0;
12727981c001SMika Westerberg }
12737981c001SMika Westerberg EXPORT_SYMBOL_GPL(intel_pinctrl_probe);
12747981c001SMika Westerberg 
12757981c001SMika Westerberg #ifdef CONFIG_PM_SLEEP
1276c538b943SMika Westerberg static bool intel_pinctrl_should_save(struct intel_pinctrl *pctrl, unsigned pin)
1277c538b943SMika Westerberg {
1278c538b943SMika Westerberg 	const struct pin_desc *pd = pin_desc_get(pctrl->pctldev, pin);
1279c538b943SMika Westerberg 
1280c538b943SMika Westerberg 	if (!pd || !intel_pad_usable(pctrl, pin))
1281c538b943SMika Westerberg 		return false;
1282c538b943SMika Westerberg 
1283c538b943SMika Westerberg 	/*
1284c538b943SMika Westerberg 	 * Only restore the pin if it is actually in use by the kernel (or
1285c538b943SMika Westerberg 	 * by userspace). It is possible that some pins are used by the
1286c538b943SMika Westerberg 	 * BIOS during resume and those are not always locked down so leave
1287c538b943SMika Westerberg 	 * them alone.
1288c538b943SMika Westerberg 	 */
1289c538b943SMika Westerberg 	if (pd->mux_owner || pd->gpio_owner ||
1290c538b943SMika Westerberg 	    gpiochip_line_is_irq(&pctrl->chip, pin))
1291c538b943SMika Westerberg 		return true;
1292c538b943SMika Westerberg 
1293c538b943SMika Westerberg 	return false;
1294c538b943SMika Westerberg }
1295c538b943SMika Westerberg 
12967981c001SMika Westerberg int intel_pinctrl_suspend(struct device *dev)
12977981c001SMika Westerberg {
12987981c001SMika Westerberg 	struct platform_device *pdev = to_platform_device(dev);
12997981c001SMika Westerberg 	struct intel_pinctrl *pctrl = platform_get_drvdata(pdev);
13007981c001SMika Westerberg 	struct intel_community_context *communities;
13017981c001SMika Westerberg 	struct intel_pad_context *pads;
13027981c001SMika Westerberg 	int i;
13037981c001SMika Westerberg 
13047981c001SMika Westerberg 	pads = pctrl->context.pads;
13057981c001SMika Westerberg 	for (i = 0; i < pctrl->soc->npins; i++) {
13067981c001SMika Westerberg 		const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
1307e57725eaSMika Westerberg 		void __iomem *padcfg;
13087981c001SMika Westerberg 		u32 val;
13097981c001SMika Westerberg 
1310c538b943SMika Westerberg 		if (!intel_pinctrl_should_save(pctrl, desc->number))
13117981c001SMika Westerberg 			continue;
13127981c001SMika Westerberg 
13137981c001SMika Westerberg 		val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG0));
13147981c001SMika Westerberg 		pads[i].padcfg0 = val & ~PADCFG0_GPIORXSTATE;
13157981c001SMika Westerberg 		val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG1));
13167981c001SMika Westerberg 		pads[i].padcfg1 = val;
1317e57725eaSMika Westerberg 
1318e57725eaSMika Westerberg 		padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG2);
1319e57725eaSMika Westerberg 		if (padcfg)
1320e57725eaSMika Westerberg 			pads[i].padcfg2 = readl(padcfg);
13217981c001SMika Westerberg 	}
13227981c001SMika Westerberg 
13237981c001SMika Westerberg 	communities = pctrl->context.communities;
13247981c001SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
13257981c001SMika Westerberg 		struct intel_community *community = &pctrl->communities[i];
13267981c001SMika Westerberg 		void __iomem *base;
13277981c001SMika Westerberg 		unsigned gpp;
13287981c001SMika Westerberg 
13297981c001SMika Westerberg 		base = community->regs + community->ie_offset;
13307981c001SMika Westerberg 		for (gpp = 0; gpp < community->ngpps; gpp++)
13317981c001SMika Westerberg 			communities[i].intmask[gpp] = readl(base + gpp * 4);
13327981c001SMika Westerberg 	}
13337981c001SMika Westerberg 
13347981c001SMika Westerberg 	return 0;
13357981c001SMika Westerberg }
13367981c001SMika Westerberg EXPORT_SYMBOL_GPL(intel_pinctrl_suspend);
13377981c001SMika Westerberg 
1338f487bbf3SMika Westerberg static void intel_gpio_irq_init(struct intel_pinctrl *pctrl)
1339f487bbf3SMika Westerberg {
1340f487bbf3SMika Westerberg 	size_t i;
1341f487bbf3SMika Westerberg 
1342f487bbf3SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
1343f487bbf3SMika Westerberg 		const struct intel_community *community;
1344f487bbf3SMika Westerberg 		void __iomem *base;
1345f487bbf3SMika Westerberg 		unsigned gpp;
1346f487bbf3SMika Westerberg 
1347f487bbf3SMika Westerberg 		community = &pctrl->communities[i];
1348f487bbf3SMika Westerberg 		base = community->regs;
1349f487bbf3SMika Westerberg 
1350f487bbf3SMika Westerberg 		for (gpp = 0; gpp < community->ngpps; gpp++) {
1351f487bbf3SMika Westerberg 			/* Mask and clear all interrupts */
1352f487bbf3SMika Westerberg 			writel(0, base + community->ie_offset + gpp * 4);
1353f487bbf3SMika Westerberg 			writel(0xffff, base + GPI_IS + gpp * 4);
1354f487bbf3SMika Westerberg 		}
1355f487bbf3SMika Westerberg 	}
1356f487bbf3SMika Westerberg }
1357f487bbf3SMika Westerberg 
13587981c001SMika Westerberg int intel_pinctrl_resume(struct device *dev)
13597981c001SMika Westerberg {
13607981c001SMika Westerberg 	struct platform_device *pdev = to_platform_device(dev);
13617981c001SMika Westerberg 	struct intel_pinctrl *pctrl = platform_get_drvdata(pdev);
13627981c001SMika Westerberg 	const struct intel_community_context *communities;
13637981c001SMika Westerberg 	const struct intel_pad_context *pads;
13647981c001SMika Westerberg 	int i;
13657981c001SMika Westerberg 
13667981c001SMika Westerberg 	/* Mask all interrupts */
13677981c001SMika Westerberg 	intel_gpio_irq_init(pctrl);
13687981c001SMika Westerberg 
13697981c001SMika Westerberg 	pads = pctrl->context.pads;
13707981c001SMika Westerberg 	for (i = 0; i < pctrl->soc->npins; i++) {
13717981c001SMika Westerberg 		const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
13727981c001SMika Westerberg 		void __iomem *padcfg;
13737981c001SMika Westerberg 		u32 val;
13747981c001SMika Westerberg 
1375c538b943SMika Westerberg 		if (!intel_pinctrl_should_save(pctrl, desc->number))
13767981c001SMika Westerberg 			continue;
13777981c001SMika Westerberg 
13787981c001SMika Westerberg 		padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG0);
13797981c001SMika Westerberg 		val = readl(padcfg) & ~PADCFG0_GPIORXSTATE;
13807981c001SMika Westerberg 		if (val != pads[i].padcfg0) {
13817981c001SMika Westerberg 			writel(pads[i].padcfg0, padcfg);
13827981c001SMika Westerberg 			dev_dbg(dev, "restored pin %u padcfg0 %#08x\n",
13837981c001SMika Westerberg 				desc->number, readl(padcfg));
13847981c001SMika Westerberg 		}
13857981c001SMika Westerberg 
13867981c001SMika Westerberg 		padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG1);
13877981c001SMika Westerberg 		val = readl(padcfg);
13887981c001SMika Westerberg 		if (val != pads[i].padcfg1) {
13897981c001SMika Westerberg 			writel(pads[i].padcfg1, padcfg);
13907981c001SMika Westerberg 			dev_dbg(dev, "restored pin %u padcfg1 %#08x\n",
13917981c001SMika Westerberg 				desc->number, readl(padcfg));
13927981c001SMika Westerberg 		}
1393e57725eaSMika Westerberg 
1394e57725eaSMika Westerberg 		padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG2);
1395e57725eaSMika Westerberg 		if (padcfg) {
1396e57725eaSMika Westerberg 			val = readl(padcfg);
1397e57725eaSMika Westerberg 			if (val != pads[i].padcfg2) {
1398e57725eaSMika Westerberg 				writel(pads[i].padcfg2, padcfg);
1399e57725eaSMika Westerberg 				dev_dbg(dev, "restored pin %u padcfg2 %#08x\n",
1400e57725eaSMika Westerberg 					desc->number, readl(padcfg));
1401e57725eaSMika Westerberg 			}
1402e57725eaSMika Westerberg 		}
14037981c001SMika Westerberg 	}
14047981c001SMika Westerberg 
14057981c001SMika Westerberg 	communities = pctrl->context.communities;
14067981c001SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
14077981c001SMika Westerberg 		struct intel_community *community = &pctrl->communities[i];
14087981c001SMika Westerberg 		void __iomem *base;
14097981c001SMika Westerberg 		unsigned gpp;
14107981c001SMika Westerberg 
14117981c001SMika Westerberg 		base = community->regs + community->ie_offset;
14127981c001SMika Westerberg 		for (gpp = 0; gpp < community->ngpps; gpp++) {
14137981c001SMika Westerberg 			writel(communities[i].intmask[gpp], base + gpp * 4);
14147981c001SMika Westerberg 			dev_dbg(dev, "restored mask %d/%u %#08x\n", i, gpp,
14157981c001SMika Westerberg 				readl(base + gpp * 4));
14167981c001SMika Westerberg 		}
14177981c001SMika Westerberg 	}
14187981c001SMika Westerberg 
14197981c001SMika Westerberg 	return 0;
14207981c001SMika Westerberg }
14217981c001SMika Westerberg EXPORT_SYMBOL_GPL(intel_pinctrl_resume);
14227981c001SMika Westerberg #endif
14237981c001SMika Westerberg 
14247981c001SMika Westerberg MODULE_AUTHOR("Mathias Nyman <mathias.nyman@linux.intel.com>");
14257981c001SMika Westerberg MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
14267981c001SMika Westerberg MODULE_DESCRIPTION("Intel pinctrl/GPIO core driver");
14277981c001SMika Westerberg MODULE_LICENSE("GPL v2");
1428