xref: /openbmc/linux/drivers/pinctrl/intel/pinctrl-intel.c (revision 3ae02c14d964499da76b5ef4d8912710902e64f8)
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>
167981c001SMika Westerberg #include <linux/platform_device.h>
177981c001SMika Westerberg #include <linux/pinctrl/pinctrl.h>
187981c001SMika Westerberg #include <linux/pinctrl/pinmux.h>
197981c001SMika Westerberg #include <linux/pinctrl/pinconf.h>
207981c001SMika Westerberg #include <linux/pinctrl/pinconf-generic.h>
217981c001SMika Westerberg 
22c538b943SMika Westerberg #include "../core.h"
237981c001SMika Westerberg #include "pinctrl-intel.h"
247981c001SMika Westerberg 
257981c001SMika Westerberg /* Offset from regs */
267981c001SMika Westerberg #define PADBAR				0x00c
277981c001SMika Westerberg #define GPI_IS				0x100
287981c001SMika Westerberg #define GPI_GPE_STS			0x140
297981c001SMika Westerberg #define GPI_GPE_EN			0x160
307981c001SMika Westerberg 
317981c001SMika Westerberg #define PADOWN_BITS			4
327981c001SMika Westerberg #define PADOWN_SHIFT(p)			((p) % 8 * PADOWN_BITS)
337981c001SMika Westerberg #define PADOWN_MASK(p)			(0xf << PADOWN_SHIFT(p))
3499a735b3SQipeng Zha #define PADOWN_GPP(p)			((p) / 8)
357981c001SMika Westerberg 
367981c001SMika Westerberg /* Offset from pad_regs */
377981c001SMika Westerberg #define PADCFG0				0x000
387981c001SMika Westerberg #define PADCFG0_RXEVCFG_SHIFT		25
397981c001SMika Westerberg #define PADCFG0_RXEVCFG_MASK		(3 << PADCFG0_RXEVCFG_SHIFT)
407981c001SMika Westerberg #define PADCFG0_RXEVCFG_LEVEL		0
417981c001SMika Westerberg #define PADCFG0_RXEVCFG_EDGE		1
427981c001SMika Westerberg #define PADCFG0_RXEVCFG_DISABLED	2
437981c001SMika Westerberg #define PADCFG0_RXEVCFG_EDGE_BOTH	3
447981c001SMika Westerberg #define PADCFG0_RXINV			BIT(23)
457981c001SMika Westerberg #define PADCFG0_GPIROUTIOXAPIC		BIT(20)
467981c001SMika Westerberg #define PADCFG0_GPIROUTSCI		BIT(19)
477981c001SMika Westerberg #define PADCFG0_GPIROUTSMI		BIT(18)
487981c001SMika Westerberg #define PADCFG0_GPIROUTNMI		BIT(17)
497981c001SMika Westerberg #define PADCFG0_PMODE_SHIFT		10
507981c001SMika Westerberg #define PADCFG0_PMODE_MASK		(0xf << PADCFG0_PMODE_SHIFT)
517981c001SMika Westerberg #define PADCFG0_GPIORXDIS		BIT(9)
527981c001SMika Westerberg #define PADCFG0_GPIOTXDIS		BIT(8)
537981c001SMika Westerberg #define PADCFG0_GPIORXSTATE		BIT(1)
547981c001SMika Westerberg #define PADCFG0_GPIOTXSTATE		BIT(0)
557981c001SMika Westerberg 
567981c001SMika Westerberg #define PADCFG1				0x004
577981c001SMika Westerberg #define PADCFG1_TERM_UP			BIT(13)
587981c001SMika Westerberg #define PADCFG1_TERM_SHIFT		10
597981c001SMika Westerberg #define PADCFG1_TERM_MASK		(7 << PADCFG1_TERM_SHIFT)
607981c001SMika Westerberg #define PADCFG1_TERM_20K		4
617981c001SMika Westerberg #define PADCFG1_TERM_2K			3
627981c001SMika Westerberg #define PADCFG1_TERM_5K			2
637981c001SMika Westerberg #define PADCFG1_TERM_1K			1
647981c001SMika Westerberg 
657981c001SMika Westerberg struct intel_pad_context {
667981c001SMika Westerberg 	u32 padcfg0;
677981c001SMika Westerberg 	u32 padcfg1;
687981c001SMika Westerberg };
697981c001SMika Westerberg 
707981c001SMika Westerberg struct intel_community_context {
717981c001SMika Westerberg 	u32 *intmask;
727981c001SMika Westerberg };
737981c001SMika Westerberg 
747981c001SMika Westerberg struct intel_pinctrl_context {
757981c001SMika Westerberg 	struct intel_pad_context *pads;
767981c001SMika Westerberg 	struct intel_community_context *communities;
777981c001SMika Westerberg };
787981c001SMika Westerberg 
797981c001SMika Westerberg /**
807981c001SMika Westerberg  * struct intel_pinctrl - Intel pinctrl private structure
817981c001SMika Westerberg  * @dev: Pointer to the device structure
827981c001SMika Westerberg  * @lock: Lock to serialize register access
837981c001SMika Westerberg  * @pctldesc: Pin controller description
847981c001SMika Westerberg  * @pctldev: Pointer to the pin controller device
857981c001SMika Westerberg  * @chip: GPIO chip in this pin controller
867981c001SMika Westerberg  * @soc: SoC/PCH specific pin configuration data
877981c001SMika Westerberg  * @communities: All communities in this pin controller
887981c001SMika Westerberg  * @ncommunities: Number of communities in this pin controller
897981c001SMika Westerberg  * @context: Configuration saved over system sleep
9001dabe91SNilesh Bacchewar  * @irq: pinctrl/GPIO chip irq number
917981c001SMika Westerberg  */
927981c001SMika Westerberg struct intel_pinctrl {
937981c001SMika Westerberg 	struct device *dev;
9427d9098cSMika Westerberg 	raw_spinlock_t lock;
957981c001SMika Westerberg 	struct pinctrl_desc pctldesc;
967981c001SMika Westerberg 	struct pinctrl_dev *pctldev;
977981c001SMika Westerberg 	struct gpio_chip chip;
987981c001SMika Westerberg 	const struct intel_pinctrl_soc_data *soc;
997981c001SMika Westerberg 	struct intel_community *communities;
1007981c001SMika Westerberg 	size_t ncommunities;
1017981c001SMika Westerberg 	struct intel_pinctrl_context context;
10201dabe91SNilesh Bacchewar 	int irq;
1037981c001SMika Westerberg };
1047981c001SMika Westerberg 
1057981c001SMika Westerberg #define pin_to_padno(c, p)	((p) - (c)->pin_base)
1067981c001SMika Westerberg 
1077981c001SMika Westerberg static struct intel_community *intel_get_community(struct intel_pinctrl *pctrl,
1087981c001SMika Westerberg 						   unsigned pin)
1097981c001SMika Westerberg {
1107981c001SMika Westerberg 	struct intel_community *community;
1117981c001SMika Westerberg 	int i;
1127981c001SMika Westerberg 
1137981c001SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
1147981c001SMika Westerberg 		community = &pctrl->communities[i];
1157981c001SMika Westerberg 		if (pin >= community->pin_base &&
1167981c001SMika Westerberg 		    pin < community->pin_base + community->npins)
1177981c001SMika Westerberg 			return community;
1187981c001SMika Westerberg 	}
1197981c001SMika Westerberg 
1207981c001SMika Westerberg 	dev_warn(pctrl->dev, "failed to find community for pin %u\n", pin);
1217981c001SMika Westerberg 	return NULL;
1227981c001SMika Westerberg }
1237981c001SMika Westerberg 
1247981c001SMika Westerberg static void __iomem *intel_get_padcfg(struct intel_pinctrl *pctrl, unsigned pin,
1257981c001SMika Westerberg 				      unsigned reg)
1267981c001SMika Westerberg {
1277981c001SMika Westerberg 	const struct intel_community *community;
1287981c001SMika Westerberg 	unsigned padno;
1297981c001SMika Westerberg 
1307981c001SMika Westerberg 	community = intel_get_community(pctrl, pin);
1317981c001SMika Westerberg 	if (!community)
1327981c001SMika Westerberg 		return NULL;
1337981c001SMika Westerberg 
1347981c001SMika Westerberg 	padno = pin_to_padno(community, pin);
1357981c001SMika Westerberg 	return community->pad_regs + reg + padno * 8;
1367981c001SMika Westerberg }
1377981c001SMika Westerberg 
1387981c001SMika Westerberg static bool intel_pad_owned_by_host(struct intel_pinctrl *pctrl, unsigned pin)
1397981c001SMika Westerberg {
1407981c001SMika Westerberg 	const struct intel_community *community;
14199a735b3SQipeng Zha 	unsigned padno, gpp, offset, group;
1427981c001SMika Westerberg 	void __iomem *padown;
1437981c001SMika Westerberg 
1447981c001SMika Westerberg 	community = intel_get_community(pctrl, pin);
1457981c001SMika Westerberg 	if (!community)
1467981c001SMika Westerberg 		return false;
1477981c001SMika Westerberg 	if (!community->padown_offset)
1487981c001SMika Westerberg 		return true;
1497981c001SMika Westerberg 
1507981c001SMika Westerberg 	padno = pin_to_padno(community, pin);
15199a735b3SQipeng Zha 	group = padno / community->gpp_size;
15299a735b3SQipeng Zha 	gpp = PADOWN_GPP(padno % community->gpp_size);
15399a735b3SQipeng Zha 	offset = community->padown_offset + 0x10 * group + gpp * 4;
1547981c001SMika Westerberg 	padown = community->regs + offset;
1557981c001SMika Westerberg 
1567981c001SMika Westerberg 	return !(readl(padown) & PADOWN_MASK(padno));
1577981c001SMika Westerberg }
1587981c001SMika Westerberg 
1594341e8a5SMika Westerberg static bool intel_pad_acpi_mode(struct intel_pinctrl *pctrl, unsigned pin)
1607981c001SMika Westerberg {
1617981c001SMika Westerberg 	const struct intel_community *community;
1627981c001SMika Westerberg 	unsigned padno, gpp, offset;
1637981c001SMika Westerberg 	void __iomem *hostown;
1647981c001SMika Westerberg 
1657981c001SMika Westerberg 	community = intel_get_community(pctrl, pin);
1667981c001SMika Westerberg 	if (!community)
1677981c001SMika Westerberg 		return true;
1687981c001SMika Westerberg 	if (!community->hostown_offset)
1697981c001SMika Westerberg 		return false;
1707981c001SMika Westerberg 
1717981c001SMika Westerberg 	padno = pin_to_padno(community, pin);
172618a919bSQipeng Zha 	gpp = padno / community->gpp_size;
1737981c001SMika Westerberg 	offset = community->hostown_offset + gpp * 4;
1747981c001SMika Westerberg 	hostown = community->regs + offset;
1757981c001SMika Westerberg 
176618a919bSQipeng Zha 	return !(readl(hostown) & BIT(padno % community->gpp_size));
1777981c001SMika Westerberg }
1787981c001SMika Westerberg 
1797981c001SMika Westerberg static bool intel_pad_locked(struct intel_pinctrl *pctrl, unsigned pin)
1807981c001SMika Westerberg {
1817981c001SMika Westerberg 	struct intel_community *community;
1827981c001SMika Westerberg 	unsigned padno, gpp, offset;
1837981c001SMika Westerberg 	u32 value;
1847981c001SMika Westerberg 
1857981c001SMika Westerberg 	community = intel_get_community(pctrl, pin);
1867981c001SMika Westerberg 	if (!community)
1877981c001SMika Westerberg 		return true;
1887981c001SMika Westerberg 	if (!community->padcfglock_offset)
1897981c001SMika Westerberg 		return false;
1907981c001SMika Westerberg 
1917981c001SMika Westerberg 	padno = pin_to_padno(community, pin);
192618a919bSQipeng Zha 	gpp = padno / community->gpp_size;
1937981c001SMika Westerberg 
1947981c001SMika Westerberg 	/*
1957981c001SMika Westerberg 	 * If PADCFGLOCK and PADCFGLOCKTX bits are both clear for this pad,
1967981c001SMika Westerberg 	 * the pad is considered unlocked. Any other case means that it is
1977981c001SMika Westerberg 	 * either fully or partially locked and we don't touch it.
1987981c001SMika Westerberg 	 */
1997981c001SMika Westerberg 	offset = community->padcfglock_offset + gpp * 8;
2007981c001SMika Westerberg 	value = readl(community->regs + offset);
201618a919bSQipeng Zha 	if (value & BIT(pin % community->gpp_size))
2027981c001SMika Westerberg 		return true;
2037981c001SMika Westerberg 
2047981c001SMika Westerberg 	offset = community->padcfglock_offset + 4 + gpp * 8;
2057981c001SMika Westerberg 	value = readl(community->regs + offset);
206618a919bSQipeng Zha 	if (value & BIT(pin % community->gpp_size))
2077981c001SMika Westerberg 		return true;
2087981c001SMika Westerberg 
2097981c001SMika Westerberg 	return false;
2107981c001SMika Westerberg }
2117981c001SMika Westerberg 
2127981c001SMika Westerberg static bool intel_pad_usable(struct intel_pinctrl *pctrl, unsigned pin)
2137981c001SMika Westerberg {
2147981c001SMika Westerberg 	return intel_pad_owned_by_host(pctrl, pin) &&
2157981c001SMika Westerberg 		!intel_pad_locked(pctrl, pin);
2167981c001SMika Westerberg }
2177981c001SMika Westerberg 
2187981c001SMika Westerberg static int intel_get_groups_count(struct pinctrl_dev *pctldev)
2197981c001SMika Westerberg {
2207981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
2217981c001SMika Westerberg 
2227981c001SMika Westerberg 	return pctrl->soc->ngroups;
2237981c001SMika Westerberg }
2247981c001SMika Westerberg 
2257981c001SMika Westerberg static const char *intel_get_group_name(struct pinctrl_dev *pctldev,
2267981c001SMika Westerberg 				      unsigned group)
2277981c001SMika Westerberg {
2287981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
2297981c001SMika Westerberg 
2307981c001SMika Westerberg 	return pctrl->soc->groups[group].name;
2317981c001SMika Westerberg }
2327981c001SMika Westerberg 
2337981c001SMika Westerberg static int intel_get_group_pins(struct pinctrl_dev *pctldev, unsigned group,
2347981c001SMika Westerberg 			      const unsigned **pins, unsigned *npins)
2357981c001SMika Westerberg {
2367981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
2377981c001SMika Westerberg 
2387981c001SMika Westerberg 	*pins = pctrl->soc->groups[group].pins;
2397981c001SMika Westerberg 	*npins = pctrl->soc->groups[group].npins;
2407981c001SMika Westerberg 	return 0;
2417981c001SMika Westerberg }
2427981c001SMika Westerberg 
2437981c001SMika Westerberg static void intel_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
2447981c001SMika Westerberg 			       unsigned pin)
2457981c001SMika Westerberg {
2467981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
2477981c001SMika Westerberg 	u32 cfg0, cfg1, mode;
2487981c001SMika Westerberg 	bool locked, acpi;
2497981c001SMika Westerberg 
2507981c001SMika Westerberg 	if (!intel_pad_owned_by_host(pctrl, pin)) {
2517981c001SMika Westerberg 		seq_puts(s, "not available");
2527981c001SMika Westerberg 		return;
2537981c001SMika Westerberg 	}
2547981c001SMika Westerberg 
2557981c001SMika Westerberg 	cfg0 = readl(intel_get_padcfg(pctrl, pin, PADCFG0));
2567981c001SMika Westerberg 	cfg1 = readl(intel_get_padcfg(pctrl, pin, PADCFG1));
2577981c001SMika Westerberg 
2587981c001SMika Westerberg 	mode = (cfg0 & PADCFG0_PMODE_MASK) >> PADCFG0_PMODE_SHIFT;
2597981c001SMika Westerberg 	if (!mode)
2607981c001SMika Westerberg 		seq_puts(s, "GPIO ");
2617981c001SMika Westerberg 	else
2627981c001SMika Westerberg 		seq_printf(s, "mode %d ", mode);
2637981c001SMika Westerberg 
2647981c001SMika Westerberg 	seq_printf(s, "0x%08x 0x%08x", cfg0, cfg1);
2657981c001SMika Westerberg 
2667981c001SMika Westerberg 	locked = intel_pad_locked(pctrl, pin);
2674341e8a5SMika Westerberg 	acpi = intel_pad_acpi_mode(pctrl, pin);
2687981c001SMika Westerberg 
2697981c001SMika Westerberg 	if (locked || acpi) {
2707981c001SMika Westerberg 		seq_puts(s, " [");
2717981c001SMika Westerberg 		if (locked) {
2727981c001SMika Westerberg 			seq_puts(s, "LOCKED");
2737981c001SMika Westerberg 			if (acpi)
2747981c001SMika Westerberg 				seq_puts(s, ", ");
2757981c001SMika Westerberg 		}
2767981c001SMika Westerberg 		if (acpi)
2777981c001SMika Westerberg 			seq_puts(s, "ACPI");
2787981c001SMika Westerberg 		seq_puts(s, "]");
2797981c001SMika Westerberg 	}
2807981c001SMika Westerberg }
2817981c001SMika Westerberg 
2827981c001SMika Westerberg static const struct pinctrl_ops intel_pinctrl_ops = {
2837981c001SMika Westerberg 	.get_groups_count = intel_get_groups_count,
2847981c001SMika Westerberg 	.get_group_name = intel_get_group_name,
2857981c001SMika Westerberg 	.get_group_pins = intel_get_group_pins,
2867981c001SMika Westerberg 	.pin_dbg_show = intel_pin_dbg_show,
2877981c001SMika Westerberg };
2887981c001SMika Westerberg 
2897981c001SMika Westerberg static int intel_get_functions_count(struct pinctrl_dev *pctldev)
2907981c001SMika Westerberg {
2917981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
2927981c001SMika Westerberg 
2937981c001SMika Westerberg 	return pctrl->soc->nfunctions;
2947981c001SMika Westerberg }
2957981c001SMika Westerberg 
2967981c001SMika Westerberg static const char *intel_get_function_name(struct pinctrl_dev *pctldev,
2977981c001SMika Westerberg 					   unsigned function)
2987981c001SMika Westerberg {
2997981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
3007981c001SMika Westerberg 
3017981c001SMika Westerberg 	return pctrl->soc->functions[function].name;
3027981c001SMika Westerberg }
3037981c001SMika Westerberg 
3047981c001SMika Westerberg static int intel_get_function_groups(struct pinctrl_dev *pctldev,
3057981c001SMika Westerberg 				     unsigned function,
3067981c001SMika Westerberg 				     const char * const **groups,
3077981c001SMika Westerberg 				     unsigned * const ngroups)
3087981c001SMika Westerberg {
3097981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
3107981c001SMika Westerberg 
3117981c001SMika Westerberg 	*groups = pctrl->soc->functions[function].groups;
3127981c001SMika Westerberg 	*ngroups = pctrl->soc->functions[function].ngroups;
3137981c001SMika Westerberg 	return 0;
3147981c001SMika Westerberg }
3157981c001SMika Westerberg 
3167981c001SMika Westerberg static int intel_pinmux_set_mux(struct pinctrl_dev *pctldev, unsigned function,
3177981c001SMika Westerberg 				unsigned group)
3187981c001SMika Westerberg {
3197981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
3207981c001SMika Westerberg 	const struct intel_pingroup *grp = &pctrl->soc->groups[group];
3217981c001SMika Westerberg 	unsigned long flags;
3227981c001SMika Westerberg 	int i;
3237981c001SMika Westerberg 
32427d9098cSMika Westerberg 	raw_spin_lock_irqsave(&pctrl->lock, flags);
3257981c001SMika Westerberg 
3267981c001SMika Westerberg 	/*
3277981c001SMika Westerberg 	 * All pins in the groups needs to be accessible and writable
3287981c001SMika Westerberg 	 * before we can enable the mux for this group.
3297981c001SMika Westerberg 	 */
3307981c001SMika Westerberg 	for (i = 0; i < grp->npins; i++) {
3317981c001SMika Westerberg 		if (!intel_pad_usable(pctrl, grp->pins[i])) {
33227d9098cSMika Westerberg 			raw_spin_unlock_irqrestore(&pctrl->lock, flags);
3337981c001SMika Westerberg 			return -EBUSY;
3347981c001SMika Westerberg 		}
3357981c001SMika Westerberg 	}
3367981c001SMika Westerberg 
3377981c001SMika Westerberg 	/* Now enable the mux setting for each pin in the group */
3387981c001SMika Westerberg 	for (i = 0; i < grp->npins; i++) {
3397981c001SMika Westerberg 		void __iomem *padcfg0;
3407981c001SMika Westerberg 		u32 value;
3417981c001SMika Westerberg 
3427981c001SMika Westerberg 		padcfg0 = intel_get_padcfg(pctrl, grp->pins[i], PADCFG0);
3437981c001SMika Westerberg 		value = readl(padcfg0);
3447981c001SMika Westerberg 
3457981c001SMika Westerberg 		value &= ~PADCFG0_PMODE_MASK;
3467981c001SMika Westerberg 		value |= grp->mode << PADCFG0_PMODE_SHIFT;
3477981c001SMika Westerberg 
3487981c001SMika Westerberg 		writel(value, padcfg0);
3497981c001SMika Westerberg 	}
3507981c001SMika Westerberg 
35127d9098cSMika Westerberg 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
3527981c001SMika Westerberg 
3537981c001SMika Westerberg 	return 0;
3547981c001SMika Westerberg }
3557981c001SMika Westerberg 
3567981c001SMika Westerberg static int intel_gpio_request_enable(struct pinctrl_dev *pctldev,
3577981c001SMika Westerberg 				     struct pinctrl_gpio_range *range,
3587981c001SMika Westerberg 				     unsigned pin)
3597981c001SMika Westerberg {
3607981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
3617981c001SMika Westerberg 	void __iomem *padcfg0;
3627981c001SMika Westerberg 	unsigned long flags;
3637981c001SMika Westerberg 	u32 value;
3647981c001SMika Westerberg 
36527d9098cSMika Westerberg 	raw_spin_lock_irqsave(&pctrl->lock, flags);
3667981c001SMika Westerberg 
3677981c001SMika Westerberg 	if (!intel_pad_usable(pctrl, pin)) {
36827d9098cSMika Westerberg 		raw_spin_unlock_irqrestore(&pctrl->lock, flags);
3697981c001SMika Westerberg 		return -EBUSY;
3707981c001SMika Westerberg 	}
3717981c001SMika Westerberg 
3727981c001SMika Westerberg 	padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
3737981c001SMika Westerberg 	/* Put the pad into GPIO mode */
3747981c001SMika Westerberg 	value = readl(padcfg0) & ~PADCFG0_PMODE_MASK;
3757981c001SMika Westerberg 	/* Disable SCI/SMI/NMI generation */
3767981c001SMika Westerberg 	value &= ~(PADCFG0_GPIROUTIOXAPIC | PADCFG0_GPIROUTSCI);
3777981c001SMika Westerberg 	value &= ~(PADCFG0_GPIROUTSMI | PADCFG0_GPIROUTNMI);
3787981c001SMika Westerberg 	/* Disable TX buffer and enable RX (this will be input) */
3797981c001SMika Westerberg 	value &= ~PADCFG0_GPIORXDIS;
3807981c001SMika Westerberg 	value |= PADCFG0_GPIOTXDIS;
3817981c001SMika Westerberg 	writel(value, padcfg0);
3827981c001SMika Westerberg 
38327d9098cSMika Westerberg 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
3847981c001SMika Westerberg 
3857981c001SMika Westerberg 	return 0;
3867981c001SMika Westerberg }
3877981c001SMika Westerberg 
3887981c001SMika Westerberg static int intel_gpio_set_direction(struct pinctrl_dev *pctldev,
3897981c001SMika Westerberg 				    struct pinctrl_gpio_range *range,
3907981c001SMika Westerberg 				    unsigned pin, bool input)
3917981c001SMika Westerberg {
3927981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
3937981c001SMika Westerberg 	void __iomem *padcfg0;
3947981c001SMika Westerberg 	unsigned long flags;
3957981c001SMika Westerberg 	u32 value;
3967981c001SMika Westerberg 
39727d9098cSMika Westerberg 	raw_spin_lock_irqsave(&pctrl->lock, flags);
3987981c001SMika Westerberg 
3997981c001SMika Westerberg 	padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
4007981c001SMika Westerberg 
4017981c001SMika Westerberg 	value = readl(padcfg0);
4027981c001SMika Westerberg 	if (input)
4037981c001SMika Westerberg 		value |= PADCFG0_GPIOTXDIS;
4047981c001SMika Westerberg 	else
4057981c001SMika Westerberg 		value &= ~PADCFG0_GPIOTXDIS;
4067981c001SMika Westerberg 	writel(value, padcfg0);
4077981c001SMika Westerberg 
40827d9098cSMika Westerberg 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
4097981c001SMika Westerberg 
4107981c001SMika Westerberg 	return 0;
4117981c001SMika Westerberg }
4127981c001SMika Westerberg 
4137981c001SMika Westerberg static const struct pinmux_ops intel_pinmux_ops = {
4147981c001SMika Westerberg 	.get_functions_count = intel_get_functions_count,
4157981c001SMika Westerberg 	.get_function_name = intel_get_function_name,
4167981c001SMika Westerberg 	.get_function_groups = intel_get_function_groups,
4177981c001SMika Westerberg 	.set_mux = intel_pinmux_set_mux,
4187981c001SMika Westerberg 	.gpio_request_enable = intel_gpio_request_enable,
4197981c001SMika Westerberg 	.gpio_set_direction = intel_gpio_set_direction,
4207981c001SMika Westerberg };
4217981c001SMika Westerberg 
4227981c001SMika Westerberg static int intel_config_get(struct pinctrl_dev *pctldev, unsigned pin,
4237981c001SMika Westerberg 			    unsigned long *config)
4247981c001SMika Westerberg {
4257981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
4267981c001SMika Westerberg 	enum pin_config_param param = pinconf_to_config_param(*config);
4277981c001SMika Westerberg 	u32 value, term;
4287981c001SMika Westerberg 	u16 arg = 0;
4297981c001SMika Westerberg 
4307981c001SMika Westerberg 	if (!intel_pad_owned_by_host(pctrl, pin))
4317981c001SMika Westerberg 		return -ENOTSUPP;
4327981c001SMika Westerberg 
4337981c001SMika Westerberg 	value = readl(intel_get_padcfg(pctrl, pin, PADCFG1));
4347981c001SMika Westerberg 	term = (value & PADCFG1_TERM_MASK) >> PADCFG1_TERM_SHIFT;
4357981c001SMika Westerberg 
4367981c001SMika Westerberg 	switch (param) {
4377981c001SMika Westerberg 	case PIN_CONFIG_BIAS_DISABLE:
4387981c001SMika Westerberg 		if (term)
4397981c001SMika Westerberg 			return -EINVAL;
4407981c001SMika Westerberg 		break;
4417981c001SMika Westerberg 
4427981c001SMika Westerberg 	case PIN_CONFIG_BIAS_PULL_UP:
4437981c001SMika Westerberg 		if (!term || !(value & PADCFG1_TERM_UP))
4447981c001SMika Westerberg 			return -EINVAL;
4457981c001SMika Westerberg 
4467981c001SMika Westerberg 		switch (term) {
4477981c001SMika Westerberg 		case PADCFG1_TERM_1K:
4487981c001SMika Westerberg 			arg = 1000;
4497981c001SMika Westerberg 			break;
4507981c001SMika Westerberg 		case PADCFG1_TERM_2K:
4517981c001SMika Westerberg 			arg = 2000;
4527981c001SMika Westerberg 			break;
4537981c001SMika Westerberg 		case PADCFG1_TERM_5K:
4547981c001SMika Westerberg 			arg = 5000;
4557981c001SMika Westerberg 			break;
4567981c001SMika Westerberg 		case PADCFG1_TERM_20K:
4577981c001SMika Westerberg 			arg = 20000;
4587981c001SMika Westerberg 			break;
4597981c001SMika Westerberg 		}
4607981c001SMika Westerberg 
4617981c001SMika Westerberg 		break;
4627981c001SMika Westerberg 
4637981c001SMika Westerberg 	case PIN_CONFIG_BIAS_PULL_DOWN:
4647981c001SMika Westerberg 		if (!term || value & PADCFG1_TERM_UP)
4657981c001SMika Westerberg 			return -EINVAL;
4667981c001SMika Westerberg 
4677981c001SMika Westerberg 		switch (term) {
4687981c001SMika Westerberg 		case PADCFG1_TERM_5K:
4697981c001SMika Westerberg 			arg = 5000;
4707981c001SMika Westerberg 			break;
4717981c001SMika Westerberg 		case PADCFG1_TERM_20K:
4727981c001SMika Westerberg 			arg = 20000;
4737981c001SMika Westerberg 			break;
4747981c001SMika Westerberg 		}
4757981c001SMika Westerberg 
4767981c001SMika Westerberg 		break;
4777981c001SMika Westerberg 
4787981c001SMika Westerberg 	default:
4797981c001SMika Westerberg 		return -ENOTSUPP;
4807981c001SMika Westerberg 	}
4817981c001SMika Westerberg 
4827981c001SMika Westerberg 	*config = pinconf_to_config_packed(param, arg);
4837981c001SMika Westerberg 	return 0;
4847981c001SMika Westerberg }
4857981c001SMika Westerberg 
4867981c001SMika Westerberg static int intel_config_set_pull(struct intel_pinctrl *pctrl, unsigned pin,
4877981c001SMika Westerberg 				 unsigned long config)
4887981c001SMika Westerberg {
4897981c001SMika Westerberg 	unsigned param = pinconf_to_config_param(config);
4907981c001SMika Westerberg 	unsigned arg = pinconf_to_config_argument(config);
4917981c001SMika Westerberg 	void __iomem *padcfg1;
4927981c001SMika Westerberg 	unsigned long flags;
4937981c001SMika Westerberg 	int ret = 0;
4947981c001SMika Westerberg 	u32 value;
4957981c001SMika Westerberg 
49627d9098cSMika Westerberg 	raw_spin_lock_irqsave(&pctrl->lock, flags);
4977981c001SMika Westerberg 
4987981c001SMika Westerberg 	padcfg1 = intel_get_padcfg(pctrl, pin, PADCFG1);
4997981c001SMika Westerberg 	value = readl(padcfg1);
5007981c001SMika Westerberg 
5017981c001SMika Westerberg 	switch (param) {
5027981c001SMika Westerberg 	case PIN_CONFIG_BIAS_DISABLE:
5037981c001SMika Westerberg 		value &= ~(PADCFG1_TERM_MASK | PADCFG1_TERM_UP);
5047981c001SMika Westerberg 		break;
5057981c001SMika Westerberg 
5067981c001SMika Westerberg 	case PIN_CONFIG_BIAS_PULL_UP:
5077981c001SMika Westerberg 		value &= ~PADCFG1_TERM_MASK;
5087981c001SMika Westerberg 
5097981c001SMika Westerberg 		value |= PADCFG1_TERM_UP;
5107981c001SMika Westerberg 
5117981c001SMika Westerberg 		switch (arg) {
5127981c001SMika Westerberg 		case 20000:
5137981c001SMika Westerberg 			value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT;
5147981c001SMika Westerberg 			break;
5157981c001SMika Westerberg 		case 5000:
5167981c001SMika Westerberg 			value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT;
5177981c001SMika Westerberg 			break;
5187981c001SMika Westerberg 		case 2000:
5197981c001SMika Westerberg 			value |= PADCFG1_TERM_2K << PADCFG1_TERM_SHIFT;
5207981c001SMika Westerberg 			break;
5217981c001SMika Westerberg 		case 1000:
5227981c001SMika Westerberg 			value |= PADCFG1_TERM_1K << PADCFG1_TERM_SHIFT;
5237981c001SMika Westerberg 			break;
5247981c001SMika Westerberg 		default:
5257981c001SMika Westerberg 			ret = -EINVAL;
5267981c001SMika Westerberg 		}
5277981c001SMika Westerberg 
5287981c001SMika Westerberg 		break;
5297981c001SMika Westerberg 
5307981c001SMika Westerberg 	case PIN_CONFIG_BIAS_PULL_DOWN:
5317981c001SMika Westerberg 		value &= ~(PADCFG1_TERM_UP | PADCFG1_TERM_MASK);
5327981c001SMika Westerberg 
5337981c001SMika Westerberg 		switch (arg) {
5347981c001SMika Westerberg 		case 20000:
5357981c001SMika Westerberg 			value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT;
5367981c001SMika Westerberg 			break;
5377981c001SMika Westerberg 		case 5000:
5387981c001SMika Westerberg 			value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT;
5397981c001SMika Westerberg 			break;
5407981c001SMika Westerberg 		default:
5417981c001SMika Westerberg 			ret = -EINVAL;
5427981c001SMika Westerberg 		}
5437981c001SMika Westerberg 
5447981c001SMika Westerberg 		break;
5457981c001SMika Westerberg 	}
5467981c001SMika Westerberg 
5477981c001SMika Westerberg 	if (!ret)
5487981c001SMika Westerberg 		writel(value, padcfg1);
5497981c001SMika Westerberg 
55027d9098cSMika Westerberg 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
5517981c001SMika Westerberg 
5527981c001SMika Westerberg 	return ret;
5537981c001SMika Westerberg }
5547981c001SMika Westerberg 
5557981c001SMika Westerberg static int intel_config_set(struct pinctrl_dev *pctldev, unsigned pin,
5567981c001SMika Westerberg 			  unsigned long *configs, unsigned nconfigs)
5577981c001SMika Westerberg {
5587981c001SMika Westerberg 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
5597981c001SMika Westerberg 	int i, ret;
5607981c001SMika Westerberg 
5617981c001SMika Westerberg 	if (!intel_pad_usable(pctrl, pin))
5627981c001SMika Westerberg 		return -ENOTSUPP;
5637981c001SMika Westerberg 
5647981c001SMika Westerberg 	for (i = 0; i < nconfigs; i++) {
5657981c001SMika Westerberg 		switch (pinconf_to_config_param(configs[i])) {
5667981c001SMika Westerberg 		case PIN_CONFIG_BIAS_DISABLE:
5677981c001SMika Westerberg 		case PIN_CONFIG_BIAS_PULL_UP:
5687981c001SMika Westerberg 		case PIN_CONFIG_BIAS_PULL_DOWN:
5697981c001SMika Westerberg 			ret = intel_config_set_pull(pctrl, pin, configs[i]);
5707981c001SMika Westerberg 			if (ret)
5717981c001SMika Westerberg 				return ret;
5727981c001SMika Westerberg 			break;
5737981c001SMika Westerberg 
5747981c001SMika Westerberg 		default:
5757981c001SMika Westerberg 			return -ENOTSUPP;
5767981c001SMika Westerberg 		}
5777981c001SMika Westerberg 	}
5787981c001SMika Westerberg 
5797981c001SMika Westerberg 	return 0;
5807981c001SMika Westerberg }
5817981c001SMika Westerberg 
5827981c001SMika Westerberg static const struct pinconf_ops intel_pinconf_ops = {
5837981c001SMika Westerberg 	.is_generic = true,
5847981c001SMika Westerberg 	.pin_config_get = intel_config_get,
5857981c001SMika Westerberg 	.pin_config_set = intel_config_set,
5867981c001SMika Westerberg };
5877981c001SMika Westerberg 
5887981c001SMika Westerberg static const struct pinctrl_desc intel_pinctrl_desc = {
5897981c001SMika Westerberg 	.pctlops = &intel_pinctrl_ops,
5907981c001SMika Westerberg 	.pmxops = &intel_pinmux_ops,
5917981c001SMika Westerberg 	.confops = &intel_pinconf_ops,
5927981c001SMika Westerberg 	.owner = THIS_MODULE,
5937981c001SMika Westerberg };
5947981c001SMika Westerberg 
5957981c001SMika Westerberg static int intel_gpio_get(struct gpio_chip *chip, unsigned offset)
5967981c001SMika Westerberg {
597acfd4c63SLinus Walleij 	struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
5987981c001SMika Westerberg 	void __iomem *reg;
5997981c001SMika Westerberg 
6007981c001SMika Westerberg 	reg = intel_get_padcfg(pctrl, offset, PADCFG0);
6017981c001SMika Westerberg 	if (!reg)
6027981c001SMika Westerberg 		return -EINVAL;
6037981c001SMika Westerberg 
6047981c001SMika Westerberg 	return !!(readl(reg) & PADCFG0_GPIORXSTATE);
6057981c001SMika Westerberg }
6067981c001SMika Westerberg 
6077981c001SMika Westerberg static void intel_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
6087981c001SMika Westerberg {
609acfd4c63SLinus Walleij 	struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
6107981c001SMika Westerberg 	void __iomem *reg;
6117981c001SMika Westerberg 
6127981c001SMika Westerberg 	reg = intel_get_padcfg(pctrl, offset, PADCFG0);
6137981c001SMika Westerberg 	if (reg) {
6147981c001SMika Westerberg 		unsigned long flags;
6157981c001SMika Westerberg 		u32 padcfg0;
6167981c001SMika Westerberg 
61727d9098cSMika Westerberg 		raw_spin_lock_irqsave(&pctrl->lock, flags);
6187981c001SMika Westerberg 		padcfg0 = readl(reg);
6197981c001SMika Westerberg 		if (value)
6207981c001SMika Westerberg 			padcfg0 |= PADCFG0_GPIOTXSTATE;
6217981c001SMika Westerberg 		else
6227981c001SMika Westerberg 			padcfg0 &= ~PADCFG0_GPIOTXSTATE;
6237981c001SMika Westerberg 		writel(padcfg0, reg);
62427d9098cSMika Westerberg 		raw_spin_unlock_irqrestore(&pctrl->lock, flags);
6257981c001SMika Westerberg 	}
6267981c001SMika Westerberg }
6277981c001SMika Westerberg 
6287981c001SMika Westerberg static int intel_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
6297981c001SMika Westerberg {
6307981c001SMika Westerberg 	return pinctrl_gpio_direction_input(chip->base + offset);
6317981c001SMika Westerberg }
6327981c001SMika Westerberg 
6337981c001SMika Westerberg static int intel_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
6347981c001SMika Westerberg 				       int value)
6357981c001SMika Westerberg {
6367981c001SMika Westerberg 	intel_gpio_set(chip, offset, value);
6377981c001SMika Westerberg 	return pinctrl_gpio_direction_output(chip->base + offset);
6387981c001SMika Westerberg }
6397981c001SMika Westerberg 
6407981c001SMika Westerberg static const struct gpio_chip intel_gpio_chip = {
6417981c001SMika Westerberg 	.owner = THIS_MODULE,
64298c85d58SJonas Gorski 	.request = gpiochip_generic_request,
64398c85d58SJonas Gorski 	.free = gpiochip_generic_free,
6447981c001SMika Westerberg 	.direction_input = intel_gpio_direction_input,
6457981c001SMika Westerberg 	.direction_output = intel_gpio_direction_output,
6467981c001SMika Westerberg 	.get = intel_gpio_get,
6477981c001SMika Westerberg 	.set = intel_gpio_set,
6487981c001SMika Westerberg };
6497981c001SMika Westerberg 
6507981c001SMika Westerberg static void intel_gpio_irq_ack(struct irq_data *d)
6517981c001SMika Westerberg {
6527981c001SMika Westerberg 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
653acfd4c63SLinus Walleij 	struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
6547981c001SMika Westerberg 	const struct intel_community *community;
6557981c001SMika Westerberg 	unsigned pin = irqd_to_hwirq(d);
6567981c001SMika Westerberg 
65727d9098cSMika Westerberg 	raw_spin_lock(&pctrl->lock);
6587981c001SMika Westerberg 
6597981c001SMika Westerberg 	community = intel_get_community(pctrl, pin);
6607981c001SMika Westerberg 	if (community) {
6617981c001SMika Westerberg 		unsigned padno = pin_to_padno(community, pin);
662618a919bSQipeng Zha 		unsigned gpp_offset = padno % community->gpp_size;
663618a919bSQipeng Zha 		unsigned gpp = padno / community->gpp_size;
6647981c001SMika Westerberg 
6657981c001SMika Westerberg 		writel(BIT(gpp_offset), community->regs + GPI_IS + gpp * 4);
6667981c001SMika Westerberg 	}
6677981c001SMika Westerberg 
66827d9098cSMika Westerberg 	raw_spin_unlock(&pctrl->lock);
6697981c001SMika Westerberg }
6707981c001SMika Westerberg 
671a939bb57SQi Zheng static void intel_gpio_irq_enable(struct irq_data *d)
672a939bb57SQi Zheng {
673a939bb57SQi Zheng 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
674a939bb57SQi Zheng 	struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
675a939bb57SQi Zheng 	const struct intel_community *community;
676a939bb57SQi Zheng 	unsigned pin = irqd_to_hwirq(d);
677a939bb57SQi Zheng 	unsigned long flags;
678a939bb57SQi Zheng 
67927d9098cSMika Westerberg 	raw_spin_lock_irqsave(&pctrl->lock, flags);
680a939bb57SQi Zheng 
681a939bb57SQi Zheng 	community = intel_get_community(pctrl, pin);
682a939bb57SQi Zheng 	if (community) {
683a939bb57SQi Zheng 		unsigned padno = pin_to_padno(community, pin);
684a939bb57SQi Zheng 		unsigned gpp_size = community->gpp_size;
685a939bb57SQi Zheng 		unsigned gpp_offset = padno % gpp_size;
686a939bb57SQi Zheng 		unsigned gpp = padno / gpp_size;
687a939bb57SQi Zheng 		u32 value;
688a939bb57SQi Zheng 
689a939bb57SQi Zheng 		/* Clear interrupt status first to avoid unexpected interrupt */
690a939bb57SQi Zheng 		writel(BIT(gpp_offset), community->regs + GPI_IS + gpp * 4);
691a939bb57SQi Zheng 
692a939bb57SQi Zheng 		value = readl(community->regs + community->ie_offset + gpp * 4);
693a939bb57SQi Zheng 		value |= BIT(gpp_offset);
694a939bb57SQi Zheng 		writel(value, community->regs + community->ie_offset + gpp * 4);
695a939bb57SQi Zheng 	}
696a939bb57SQi Zheng 
69727d9098cSMika Westerberg 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
698a939bb57SQi Zheng }
699a939bb57SQi Zheng 
7007981c001SMika Westerberg static void intel_gpio_irq_mask_unmask(struct irq_data *d, bool mask)
7017981c001SMika Westerberg {
7027981c001SMika Westerberg 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
703acfd4c63SLinus Walleij 	struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
7047981c001SMika Westerberg 	const struct intel_community *community;
7057981c001SMika Westerberg 	unsigned pin = irqd_to_hwirq(d);
7067981c001SMika Westerberg 	unsigned long flags;
7077981c001SMika Westerberg 
70827d9098cSMika Westerberg 	raw_spin_lock_irqsave(&pctrl->lock, flags);
7097981c001SMika Westerberg 
7107981c001SMika Westerberg 	community = intel_get_community(pctrl, pin);
7117981c001SMika Westerberg 	if (community) {
7127981c001SMika Westerberg 		unsigned padno = pin_to_padno(community, pin);
713618a919bSQipeng Zha 		unsigned gpp_offset = padno % community->gpp_size;
714618a919bSQipeng Zha 		unsigned gpp = padno / community->gpp_size;
7157981c001SMika Westerberg 		void __iomem *reg;
7167981c001SMika Westerberg 		u32 value;
7177981c001SMika Westerberg 
7187981c001SMika Westerberg 		reg = community->regs + community->ie_offset + gpp * 4;
7197981c001SMika Westerberg 		value = readl(reg);
7207981c001SMika Westerberg 		if (mask)
7217981c001SMika Westerberg 			value &= ~BIT(gpp_offset);
7227981c001SMika Westerberg 		else
7237981c001SMika Westerberg 			value |= BIT(gpp_offset);
7247981c001SMika Westerberg 		writel(value, reg);
7257981c001SMika Westerberg 	}
7267981c001SMika Westerberg 
72727d9098cSMika Westerberg 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
7287981c001SMika Westerberg }
7297981c001SMika Westerberg 
7307981c001SMika Westerberg static void intel_gpio_irq_mask(struct irq_data *d)
7317981c001SMika Westerberg {
7327981c001SMika Westerberg 	intel_gpio_irq_mask_unmask(d, true);
7337981c001SMika Westerberg }
7347981c001SMika Westerberg 
7357981c001SMika Westerberg static void intel_gpio_irq_unmask(struct irq_data *d)
7367981c001SMika Westerberg {
7377981c001SMika Westerberg 	intel_gpio_irq_mask_unmask(d, false);
7387981c001SMika Westerberg }
7397981c001SMika Westerberg 
7407981c001SMika Westerberg static int intel_gpio_irq_type(struct irq_data *d, unsigned type)
7417981c001SMika Westerberg {
7427981c001SMika Westerberg 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
743acfd4c63SLinus Walleij 	struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
7447981c001SMika Westerberg 	unsigned pin = irqd_to_hwirq(d);
7457981c001SMika Westerberg 	unsigned long flags;
7467981c001SMika Westerberg 	void __iomem *reg;
7477981c001SMika Westerberg 	u32 value;
7487981c001SMika Westerberg 
7497981c001SMika Westerberg 	reg = intel_get_padcfg(pctrl, pin, PADCFG0);
7507981c001SMika Westerberg 	if (!reg)
7517981c001SMika Westerberg 		return -EINVAL;
7527981c001SMika Westerberg 
7534341e8a5SMika Westerberg 	/*
7544341e8a5SMika Westerberg 	 * If the pin is in ACPI mode it is still usable as a GPIO but it
7554341e8a5SMika Westerberg 	 * cannot be used as IRQ because GPI_IS status bit will not be
7564341e8a5SMika Westerberg 	 * updated by the host controller hardware.
7574341e8a5SMika Westerberg 	 */
7584341e8a5SMika Westerberg 	if (intel_pad_acpi_mode(pctrl, pin)) {
7594341e8a5SMika Westerberg 		dev_warn(pctrl->dev, "pin %u cannot be used as IRQ\n", pin);
7604341e8a5SMika Westerberg 		return -EPERM;
7614341e8a5SMika Westerberg 	}
7624341e8a5SMika Westerberg 
76327d9098cSMika Westerberg 	raw_spin_lock_irqsave(&pctrl->lock, flags);
7647981c001SMika Westerberg 
7657981c001SMika Westerberg 	value = readl(reg);
7667981c001SMika Westerberg 
7677981c001SMika Westerberg 	value &= ~(PADCFG0_RXEVCFG_MASK | PADCFG0_RXINV);
7687981c001SMika Westerberg 
7697981c001SMika Westerberg 	if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
7707981c001SMika Westerberg 		value |= PADCFG0_RXEVCFG_EDGE_BOTH << PADCFG0_RXEVCFG_SHIFT;
7717981c001SMika Westerberg 	} else if (type & IRQ_TYPE_EDGE_FALLING) {
7727981c001SMika Westerberg 		value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT;
7737981c001SMika Westerberg 		value |= PADCFG0_RXINV;
7747981c001SMika Westerberg 	} else if (type & IRQ_TYPE_EDGE_RISING) {
7757981c001SMika Westerberg 		value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT;
776bf380cfaSQipeng Zha 	} else if (type & IRQ_TYPE_LEVEL_MASK) {
777bf380cfaSQipeng Zha 		if (type & IRQ_TYPE_LEVEL_LOW)
7787981c001SMika Westerberg 			value |= PADCFG0_RXINV;
7797981c001SMika Westerberg 	} else {
7807981c001SMika Westerberg 		value |= PADCFG0_RXEVCFG_DISABLED << PADCFG0_RXEVCFG_SHIFT;
7817981c001SMika Westerberg 	}
7827981c001SMika Westerberg 
7837981c001SMika Westerberg 	writel(value, reg);
7847981c001SMika Westerberg 
7857981c001SMika Westerberg 	if (type & IRQ_TYPE_EDGE_BOTH)
786fc756bcdSThomas Gleixner 		irq_set_handler_locked(d, handle_edge_irq);
7877981c001SMika Westerberg 	else if (type & IRQ_TYPE_LEVEL_MASK)
788fc756bcdSThomas Gleixner 		irq_set_handler_locked(d, handle_level_irq);
7897981c001SMika Westerberg 
79027d9098cSMika Westerberg 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
7917981c001SMika Westerberg 
7927981c001SMika Westerberg 	return 0;
7937981c001SMika Westerberg }
7947981c001SMika Westerberg 
7957981c001SMika Westerberg static int intel_gpio_irq_wake(struct irq_data *d, unsigned int on)
7967981c001SMika Westerberg {
7977981c001SMika Westerberg 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
798acfd4c63SLinus Walleij 	struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
7997981c001SMika Westerberg 	unsigned pin = irqd_to_hwirq(d);
8007981c001SMika Westerberg 
8017981c001SMika Westerberg 	if (on)
80201dabe91SNilesh Bacchewar 		enable_irq_wake(pctrl->irq);
8037981c001SMika Westerberg 	else
80401dabe91SNilesh Bacchewar 		disable_irq_wake(pctrl->irq);
8059a520fd9SAndy Shevchenko 
8067981c001SMika Westerberg 	dev_dbg(pctrl->dev, "%sable wake for pin %u\n", on ? "en" : "dis", pin);
8077981c001SMika Westerberg 	return 0;
8087981c001SMika Westerberg }
8097981c001SMika Westerberg 
810193b40c8SMika Westerberg static irqreturn_t intel_gpio_community_irq_handler(struct intel_pinctrl *pctrl,
8117981c001SMika Westerberg 	const struct intel_community *community)
8127981c001SMika Westerberg {
813193b40c8SMika Westerberg 	struct gpio_chip *gc = &pctrl->chip;
814193b40c8SMika Westerberg 	irqreturn_t ret = IRQ_NONE;
8157981c001SMika Westerberg 	int gpp;
8167981c001SMika Westerberg 
8177981c001SMika Westerberg 	for (gpp = 0; gpp < community->ngpps; gpp++) {
8187981c001SMika Westerberg 		unsigned long pending, enabled, gpp_offset;
8197981c001SMika Westerberg 
8207981c001SMika Westerberg 		pending = readl(community->regs + GPI_IS + gpp * 4);
8217981c001SMika Westerberg 		enabled = readl(community->regs + community->ie_offset +
8227981c001SMika Westerberg 				gpp * 4);
8237981c001SMika Westerberg 
8247981c001SMika Westerberg 		/* Only interrupts that are enabled */
8257981c001SMika Westerberg 		pending &= enabled;
8267981c001SMika Westerberg 
827618a919bSQipeng Zha 		for_each_set_bit(gpp_offset, &pending, community->gpp_size) {
8287981c001SMika Westerberg 			unsigned padno, irq;
8297981c001SMika Westerberg 
8307981c001SMika Westerberg 			/*
8317981c001SMika Westerberg 			 * The last group in community can have less pins
8327981c001SMika Westerberg 			 * than NPADS_IN_GPP.
8337981c001SMika Westerberg 			 */
834618a919bSQipeng Zha 			padno = gpp_offset + gpp * community->gpp_size;
8357981c001SMika Westerberg 			if (padno >= community->npins)
8367981c001SMika Westerberg 				break;
8377981c001SMika Westerberg 
8387981c001SMika Westerberg 			irq = irq_find_mapping(gc->irqdomain,
8397981c001SMika Westerberg 					       community->pin_base + padno);
8407981c001SMika Westerberg 			generic_handle_irq(irq);
841193b40c8SMika Westerberg 
842193b40c8SMika Westerberg 			ret |= IRQ_HANDLED;
8437981c001SMika Westerberg 		}
8447981c001SMika Westerberg 	}
8457981c001SMika Westerberg 
846193b40c8SMika Westerberg 	return ret;
847193b40c8SMika Westerberg }
848193b40c8SMika Westerberg 
849193b40c8SMika Westerberg static irqreturn_t intel_gpio_irq(int irq, void *data)
8507981c001SMika Westerberg {
851193b40c8SMika Westerberg 	const struct intel_community *community;
852193b40c8SMika Westerberg 	struct intel_pinctrl *pctrl = data;
853193b40c8SMika Westerberg 	irqreturn_t ret = IRQ_NONE;
8547981c001SMika Westerberg 	int i;
8557981c001SMika Westerberg 
8567981c001SMika Westerberg 	/* Need to check all communities for pending interrupts */
857193b40c8SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
858193b40c8SMika Westerberg 		community = &pctrl->communities[i];
859193b40c8SMika Westerberg 		ret |= intel_gpio_community_irq_handler(pctrl, community);
860193b40c8SMika Westerberg 	}
8617981c001SMika Westerberg 
862193b40c8SMika Westerberg 	return ret;
8637981c001SMika Westerberg }
8647981c001SMika Westerberg 
8657981c001SMika Westerberg static struct irq_chip intel_gpio_irqchip = {
8667981c001SMika Westerberg 	.name = "intel-gpio",
867a939bb57SQi Zheng 	.irq_enable = intel_gpio_irq_enable,
8687981c001SMika Westerberg 	.irq_ack = intel_gpio_irq_ack,
8697981c001SMika Westerberg 	.irq_mask = intel_gpio_irq_mask,
8707981c001SMika Westerberg 	.irq_unmask = intel_gpio_irq_unmask,
8717981c001SMika Westerberg 	.irq_set_type = intel_gpio_irq_type,
8727981c001SMika Westerberg 	.irq_set_wake = intel_gpio_irq_wake,
8737981c001SMika Westerberg };
8747981c001SMika Westerberg 
8757981c001SMika Westerberg static int intel_gpio_probe(struct intel_pinctrl *pctrl, int irq)
8767981c001SMika Westerberg {
8777981c001SMika Westerberg 	int ret;
8787981c001SMika Westerberg 
8797981c001SMika Westerberg 	pctrl->chip = intel_gpio_chip;
8807981c001SMika Westerberg 
8817981c001SMika Westerberg 	pctrl->chip.ngpio = pctrl->soc->npins;
8827981c001SMika Westerberg 	pctrl->chip.label = dev_name(pctrl->dev);
88358383c78SLinus Walleij 	pctrl->chip.parent = pctrl->dev;
8847981c001SMika Westerberg 	pctrl->chip.base = -1;
88501dabe91SNilesh Bacchewar 	pctrl->irq = irq;
8867981c001SMika Westerberg 
887acfd4c63SLinus Walleij 	ret = gpiochip_add_data(&pctrl->chip, pctrl);
8887981c001SMika Westerberg 	if (ret) {
8897981c001SMika Westerberg 		dev_err(pctrl->dev, "failed to register gpiochip\n");
8907981c001SMika Westerberg 		return ret;
8917981c001SMika Westerberg 	}
8927981c001SMika Westerberg 
8937981c001SMika Westerberg 	ret = gpiochip_add_pin_range(&pctrl->chip, dev_name(pctrl->dev),
8947981c001SMika Westerberg 				     0, 0, pctrl->soc->npins);
8957981c001SMika Westerberg 	if (ret) {
8967981c001SMika Westerberg 		dev_err(pctrl->dev, "failed to add GPIO pin range\n");
897193b40c8SMika Westerberg 		goto fail;
898193b40c8SMika Westerberg 	}
899193b40c8SMika Westerberg 
900193b40c8SMika Westerberg 	/*
901193b40c8SMika Westerberg 	 * We need to request the interrupt here (instead of providing chip
902193b40c8SMika Westerberg 	 * to the irq directly) because on some platforms several GPIO
903193b40c8SMika Westerberg 	 * controllers share the same interrupt line.
904193b40c8SMika Westerberg 	 */
9051a7d1cb8SMika Westerberg 	ret = devm_request_irq(pctrl->dev, irq, intel_gpio_irq,
9061a7d1cb8SMika Westerberg 			       IRQF_SHARED | IRQF_NO_THREAD,
907193b40c8SMika Westerberg 			       dev_name(pctrl->dev), pctrl);
908193b40c8SMika Westerberg 	if (ret) {
909193b40c8SMika Westerberg 		dev_err(pctrl->dev, "failed to request interrupt\n");
910193b40c8SMika Westerberg 		goto fail;
9117981c001SMika Westerberg 	}
9127981c001SMika Westerberg 
9137981c001SMika Westerberg 	ret = gpiochip_irqchip_add(&pctrl->chip, &intel_gpio_irqchip, 0,
914*3ae02c14SAndy Shevchenko 				   handle_bad_irq, IRQ_TYPE_NONE);
9157981c001SMika Westerberg 	if (ret) {
9167981c001SMika Westerberg 		dev_err(pctrl->dev, "failed to add irqchip\n");
917193b40c8SMika Westerberg 		goto fail;
9187981c001SMika Westerberg 	}
9197981c001SMika Westerberg 
9207981c001SMika Westerberg 	gpiochip_set_chained_irqchip(&pctrl->chip, &intel_gpio_irqchip, irq,
921193b40c8SMika Westerberg 				     NULL);
9227981c001SMika Westerberg 	return 0;
923193b40c8SMika Westerberg 
924193b40c8SMika Westerberg fail:
925193b40c8SMika Westerberg 	gpiochip_remove(&pctrl->chip);
926193b40c8SMika Westerberg 
927193b40c8SMika Westerberg 	return ret;
9287981c001SMika Westerberg }
9297981c001SMika Westerberg 
9307981c001SMika Westerberg static int intel_pinctrl_pm_init(struct intel_pinctrl *pctrl)
9317981c001SMika Westerberg {
9327981c001SMika Westerberg #ifdef CONFIG_PM_SLEEP
9337981c001SMika Westerberg 	const struct intel_pinctrl_soc_data *soc = pctrl->soc;
9347981c001SMika Westerberg 	struct intel_community_context *communities;
9357981c001SMika Westerberg 	struct intel_pad_context *pads;
9367981c001SMika Westerberg 	int i;
9377981c001SMika Westerberg 
9387981c001SMika Westerberg 	pads = devm_kcalloc(pctrl->dev, soc->npins, sizeof(*pads), GFP_KERNEL);
9397981c001SMika Westerberg 	if (!pads)
9407981c001SMika Westerberg 		return -ENOMEM;
9417981c001SMika Westerberg 
9427981c001SMika Westerberg 	communities = devm_kcalloc(pctrl->dev, pctrl->ncommunities,
9437981c001SMika Westerberg 				   sizeof(*communities), GFP_KERNEL);
9447981c001SMika Westerberg 	if (!communities)
9457981c001SMika Westerberg 		return -ENOMEM;
9467981c001SMika Westerberg 
9477981c001SMika Westerberg 
9487981c001SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
9497981c001SMika Westerberg 		struct intel_community *community = &pctrl->communities[i];
9507981c001SMika Westerberg 		u32 *intmask;
9517981c001SMika Westerberg 
9527981c001SMika Westerberg 		intmask = devm_kcalloc(pctrl->dev, community->ngpps,
9537981c001SMika Westerberg 				       sizeof(*intmask), GFP_KERNEL);
9547981c001SMika Westerberg 		if (!intmask)
9557981c001SMika Westerberg 			return -ENOMEM;
9567981c001SMika Westerberg 
9577981c001SMika Westerberg 		communities[i].intmask = intmask;
9587981c001SMika Westerberg 	}
9597981c001SMika Westerberg 
9607981c001SMika Westerberg 	pctrl->context.pads = pads;
9617981c001SMika Westerberg 	pctrl->context.communities = communities;
9627981c001SMika Westerberg #endif
9637981c001SMika Westerberg 
9647981c001SMika Westerberg 	return 0;
9657981c001SMika Westerberg }
9667981c001SMika Westerberg 
9677981c001SMika Westerberg int intel_pinctrl_probe(struct platform_device *pdev,
9687981c001SMika Westerberg 			const struct intel_pinctrl_soc_data *soc_data)
9697981c001SMika Westerberg {
9707981c001SMika Westerberg 	struct intel_pinctrl *pctrl;
9717981c001SMika Westerberg 	int i, ret, irq;
9727981c001SMika Westerberg 
9737981c001SMika Westerberg 	if (!soc_data)
9747981c001SMika Westerberg 		return -EINVAL;
9757981c001SMika Westerberg 
9767981c001SMika Westerberg 	pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL);
9777981c001SMika Westerberg 	if (!pctrl)
9787981c001SMika Westerberg 		return -ENOMEM;
9797981c001SMika Westerberg 
9807981c001SMika Westerberg 	pctrl->dev = &pdev->dev;
9817981c001SMika Westerberg 	pctrl->soc = soc_data;
98227d9098cSMika Westerberg 	raw_spin_lock_init(&pctrl->lock);
9837981c001SMika Westerberg 
9847981c001SMika Westerberg 	/*
9857981c001SMika Westerberg 	 * Make a copy of the communities which we can use to hold pointers
9867981c001SMika Westerberg 	 * to the registers.
9877981c001SMika Westerberg 	 */
9887981c001SMika Westerberg 	pctrl->ncommunities = pctrl->soc->ncommunities;
9897981c001SMika Westerberg 	pctrl->communities = devm_kcalloc(&pdev->dev, pctrl->ncommunities,
9907981c001SMika Westerberg 				  sizeof(*pctrl->communities), GFP_KERNEL);
9917981c001SMika Westerberg 	if (!pctrl->communities)
9927981c001SMika Westerberg 		return -ENOMEM;
9937981c001SMika Westerberg 
9947981c001SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
9957981c001SMika Westerberg 		struct intel_community *community = &pctrl->communities[i];
9967981c001SMika Westerberg 		struct resource *res;
9977981c001SMika Westerberg 		void __iomem *regs;
9987981c001SMika Westerberg 		u32 padbar;
9997981c001SMika Westerberg 
10007981c001SMika Westerberg 		*community = pctrl->soc->communities[i];
10017981c001SMika Westerberg 
10027981c001SMika Westerberg 		res = platform_get_resource(pdev, IORESOURCE_MEM,
10037981c001SMika Westerberg 					    community->barno);
10047981c001SMika Westerberg 		regs = devm_ioremap_resource(&pdev->dev, res);
10057981c001SMika Westerberg 		if (IS_ERR(regs))
10067981c001SMika Westerberg 			return PTR_ERR(regs);
10077981c001SMika Westerberg 
10087981c001SMika Westerberg 		/* Read offset of the pad configuration registers */
10097981c001SMika Westerberg 		padbar = readl(regs + PADBAR);
10107981c001SMika Westerberg 
10117981c001SMika Westerberg 		community->regs = regs;
10127981c001SMika Westerberg 		community->pad_regs = regs + padbar;
1013618a919bSQipeng Zha 		community->ngpps = DIV_ROUND_UP(community->npins,
1014618a919bSQipeng Zha 						community->gpp_size);
10157981c001SMika Westerberg 	}
10167981c001SMika Westerberg 
10177981c001SMika Westerberg 	irq = platform_get_irq(pdev, 0);
10187981c001SMika Westerberg 	if (irq < 0) {
10197981c001SMika Westerberg 		dev_err(&pdev->dev, "failed to get interrupt number\n");
10207981c001SMika Westerberg 		return irq;
10217981c001SMika Westerberg 	}
10227981c001SMika Westerberg 
10237981c001SMika Westerberg 	ret = intel_pinctrl_pm_init(pctrl);
10247981c001SMika Westerberg 	if (ret)
10257981c001SMika Westerberg 		return ret;
10267981c001SMika Westerberg 
10277981c001SMika Westerberg 	pctrl->pctldesc = intel_pinctrl_desc;
10287981c001SMika Westerberg 	pctrl->pctldesc.name = dev_name(&pdev->dev);
10297981c001SMika Westerberg 	pctrl->pctldesc.pins = pctrl->soc->pins;
10307981c001SMika Westerberg 	pctrl->pctldesc.npins = pctrl->soc->npins;
10317981c001SMika Westerberg 
103254d46cd7SLaxman Dewangan 	pctrl->pctldev = devm_pinctrl_register(&pdev->dev, &pctrl->pctldesc,
103354d46cd7SLaxman Dewangan 					       pctrl);
1034323de9efSMasahiro Yamada 	if (IS_ERR(pctrl->pctldev)) {
10357981c001SMika Westerberg 		dev_err(&pdev->dev, "failed to register pinctrl driver\n");
1036323de9efSMasahiro Yamada 		return PTR_ERR(pctrl->pctldev);
10377981c001SMika Westerberg 	}
10387981c001SMika Westerberg 
10397981c001SMika Westerberg 	ret = intel_gpio_probe(pctrl, irq);
104054d46cd7SLaxman Dewangan 	if (ret)
10417981c001SMika Westerberg 		return ret;
10427981c001SMika Westerberg 
10437981c001SMika Westerberg 	platform_set_drvdata(pdev, pctrl);
10447981c001SMika Westerberg 
10457981c001SMika Westerberg 	return 0;
10467981c001SMika Westerberg }
10477981c001SMika Westerberg EXPORT_SYMBOL_GPL(intel_pinctrl_probe);
10487981c001SMika Westerberg 
10497981c001SMika Westerberg int intel_pinctrl_remove(struct platform_device *pdev)
10507981c001SMika Westerberg {
10517981c001SMika Westerberg 	struct intel_pinctrl *pctrl = platform_get_drvdata(pdev);
10527981c001SMika Westerberg 
10537981c001SMika Westerberg 	gpiochip_remove(&pctrl->chip);
10547981c001SMika Westerberg 
10557981c001SMika Westerberg 	return 0;
10567981c001SMika Westerberg }
10577981c001SMika Westerberg EXPORT_SYMBOL_GPL(intel_pinctrl_remove);
10587981c001SMika Westerberg 
10597981c001SMika Westerberg #ifdef CONFIG_PM_SLEEP
1060c538b943SMika Westerberg static bool intel_pinctrl_should_save(struct intel_pinctrl *pctrl, unsigned pin)
1061c538b943SMika Westerberg {
1062c538b943SMika Westerberg 	const struct pin_desc *pd = pin_desc_get(pctrl->pctldev, pin);
1063c538b943SMika Westerberg 
1064c538b943SMika Westerberg 	if (!pd || !intel_pad_usable(pctrl, pin))
1065c538b943SMika Westerberg 		return false;
1066c538b943SMika Westerberg 
1067c538b943SMika Westerberg 	/*
1068c538b943SMika Westerberg 	 * Only restore the pin if it is actually in use by the kernel (or
1069c538b943SMika Westerberg 	 * by userspace). It is possible that some pins are used by the
1070c538b943SMika Westerberg 	 * BIOS during resume and those are not always locked down so leave
1071c538b943SMika Westerberg 	 * them alone.
1072c538b943SMika Westerberg 	 */
1073c538b943SMika Westerberg 	if (pd->mux_owner || pd->gpio_owner ||
1074c538b943SMika Westerberg 	    gpiochip_line_is_irq(&pctrl->chip, pin))
1075c538b943SMika Westerberg 		return true;
1076c538b943SMika Westerberg 
1077c538b943SMika Westerberg 	return false;
1078c538b943SMika Westerberg }
1079c538b943SMika Westerberg 
10807981c001SMika Westerberg int intel_pinctrl_suspend(struct device *dev)
10817981c001SMika Westerberg {
10827981c001SMika Westerberg 	struct platform_device *pdev = to_platform_device(dev);
10837981c001SMika Westerberg 	struct intel_pinctrl *pctrl = platform_get_drvdata(pdev);
10847981c001SMika Westerberg 	struct intel_community_context *communities;
10857981c001SMika Westerberg 	struct intel_pad_context *pads;
10867981c001SMika Westerberg 	int i;
10877981c001SMika Westerberg 
10887981c001SMika Westerberg 	pads = pctrl->context.pads;
10897981c001SMika Westerberg 	for (i = 0; i < pctrl->soc->npins; i++) {
10907981c001SMika Westerberg 		const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
10917981c001SMika Westerberg 		u32 val;
10927981c001SMika Westerberg 
1093c538b943SMika Westerberg 		if (!intel_pinctrl_should_save(pctrl, desc->number))
10947981c001SMika Westerberg 			continue;
10957981c001SMika Westerberg 
10967981c001SMika Westerberg 		val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG0));
10977981c001SMika Westerberg 		pads[i].padcfg0 = val & ~PADCFG0_GPIORXSTATE;
10987981c001SMika Westerberg 		val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG1));
10997981c001SMika Westerberg 		pads[i].padcfg1 = val;
11007981c001SMika Westerberg 	}
11017981c001SMika Westerberg 
11027981c001SMika Westerberg 	communities = pctrl->context.communities;
11037981c001SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
11047981c001SMika Westerberg 		struct intel_community *community = &pctrl->communities[i];
11057981c001SMika Westerberg 		void __iomem *base;
11067981c001SMika Westerberg 		unsigned gpp;
11077981c001SMika Westerberg 
11087981c001SMika Westerberg 		base = community->regs + community->ie_offset;
11097981c001SMika Westerberg 		for (gpp = 0; gpp < community->ngpps; gpp++)
11107981c001SMika Westerberg 			communities[i].intmask[gpp] = readl(base + gpp * 4);
11117981c001SMika Westerberg 	}
11127981c001SMika Westerberg 
11137981c001SMika Westerberg 	return 0;
11147981c001SMika Westerberg }
11157981c001SMika Westerberg EXPORT_SYMBOL_GPL(intel_pinctrl_suspend);
11167981c001SMika Westerberg 
1117f487bbf3SMika Westerberg static void intel_gpio_irq_init(struct intel_pinctrl *pctrl)
1118f487bbf3SMika Westerberg {
1119f487bbf3SMika Westerberg 	size_t i;
1120f487bbf3SMika Westerberg 
1121f487bbf3SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
1122f487bbf3SMika Westerberg 		const struct intel_community *community;
1123f487bbf3SMika Westerberg 		void __iomem *base;
1124f487bbf3SMika Westerberg 		unsigned gpp;
1125f487bbf3SMika Westerberg 
1126f487bbf3SMika Westerberg 		community = &pctrl->communities[i];
1127f487bbf3SMika Westerberg 		base = community->regs;
1128f487bbf3SMika Westerberg 
1129f487bbf3SMika Westerberg 		for (gpp = 0; gpp < community->ngpps; gpp++) {
1130f487bbf3SMika Westerberg 			/* Mask and clear all interrupts */
1131f487bbf3SMika Westerberg 			writel(0, base + community->ie_offset + gpp * 4);
1132f487bbf3SMika Westerberg 			writel(0xffff, base + GPI_IS + gpp * 4);
1133f487bbf3SMika Westerberg 		}
1134f487bbf3SMika Westerberg 	}
1135f487bbf3SMika Westerberg }
1136f487bbf3SMika Westerberg 
11377981c001SMika Westerberg int intel_pinctrl_resume(struct device *dev)
11387981c001SMika Westerberg {
11397981c001SMika Westerberg 	struct platform_device *pdev = to_platform_device(dev);
11407981c001SMika Westerberg 	struct intel_pinctrl *pctrl = platform_get_drvdata(pdev);
11417981c001SMika Westerberg 	const struct intel_community_context *communities;
11427981c001SMika Westerberg 	const struct intel_pad_context *pads;
11437981c001SMika Westerberg 	int i;
11447981c001SMika Westerberg 
11457981c001SMika Westerberg 	/* Mask all interrupts */
11467981c001SMika Westerberg 	intel_gpio_irq_init(pctrl);
11477981c001SMika Westerberg 
11487981c001SMika Westerberg 	pads = pctrl->context.pads;
11497981c001SMika Westerberg 	for (i = 0; i < pctrl->soc->npins; i++) {
11507981c001SMika Westerberg 		const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
11517981c001SMika Westerberg 		void __iomem *padcfg;
11527981c001SMika Westerberg 		u32 val;
11537981c001SMika Westerberg 
1154c538b943SMika Westerberg 		if (!intel_pinctrl_should_save(pctrl, desc->number))
11557981c001SMika Westerberg 			continue;
11567981c001SMika Westerberg 
11577981c001SMika Westerberg 		padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG0);
11587981c001SMika Westerberg 		val = readl(padcfg) & ~PADCFG0_GPIORXSTATE;
11597981c001SMika Westerberg 		if (val != pads[i].padcfg0) {
11607981c001SMika Westerberg 			writel(pads[i].padcfg0, padcfg);
11617981c001SMika Westerberg 			dev_dbg(dev, "restored pin %u padcfg0 %#08x\n",
11627981c001SMika Westerberg 				desc->number, readl(padcfg));
11637981c001SMika Westerberg 		}
11647981c001SMika Westerberg 
11657981c001SMika Westerberg 		padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG1);
11667981c001SMika Westerberg 		val = readl(padcfg);
11677981c001SMika Westerberg 		if (val != pads[i].padcfg1) {
11687981c001SMika Westerberg 			writel(pads[i].padcfg1, padcfg);
11697981c001SMika Westerberg 			dev_dbg(dev, "restored pin %u padcfg1 %#08x\n",
11707981c001SMika Westerberg 				desc->number, readl(padcfg));
11717981c001SMika Westerberg 		}
11727981c001SMika Westerberg 	}
11737981c001SMika Westerberg 
11747981c001SMika Westerberg 	communities = pctrl->context.communities;
11757981c001SMika Westerberg 	for (i = 0; i < pctrl->ncommunities; i++) {
11767981c001SMika Westerberg 		struct intel_community *community = &pctrl->communities[i];
11777981c001SMika Westerberg 		void __iomem *base;
11787981c001SMika Westerberg 		unsigned gpp;
11797981c001SMika Westerberg 
11807981c001SMika Westerberg 		base = community->regs + community->ie_offset;
11817981c001SMika Westerberg 		for (gpp = 0; gpp < community->ngpps; gpp++) {
11827981c001SMika Westerberg 			writel(communities[i].intmask[gpp], base + gpp * 4);
11837981c001SMika Westerberg 			dev_dbg(dev, "restored mask %d/%u %#08x\n", i, gpp,
11847981c001SMika Westerberg 				readl(base + gpp * 4));
11857981c001SMika Westerberg 		}
11867981c001SMika Westerberg 	}
11877981c001SMika Westerberg 
11887981c001SMika Westerberg 	return 0;
11897981c001SMika Westerberg }
11907981c001SMika Westerberg EXPORT_SYMBOL_GPL(intel_pinctrl_resume);
11917981c001SMika Westerberg #endif
11927981c001SMika Westerberg 
11937981c001SMika Westerberg MODULE_AUTHOR("Mathias Nyman <mathias.nyman@linux.intel.com>");
11947981c001SMika Westerberg MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
11957981c001SMika Westerberg MODULE_DESCRIPTION("Intel pinctrl/GPIO core driver");
11967981c001SMika Westerberg MODULE_LICENSE("GPL v2");
1197