1875a92b3SAndy Shevchenko // SPDX-License-Identifier: GPL-2.0
27981c001SMika Westerberg /*
37981c001SMika Westerberg * Intel pinctrl/GPIO core driver.
47981c001SMika Westerberg *
57981c001SMika Westerberg * Copyright (C) 2015, Intel Corporation
67981c001SMika Westerberg * Authors: Mathias Nyman <mathias.nyman@linux.intel.com>
77981c001SMika Westerberg * Mika Westerberg <mika.westerberg@linux.intel.com>
87981c001SMika Westerberg */
97981c001SMika Westerberg
10924cf800SAndy Shevchenko #include <linux/acpi.h>
117981c001SMika Westerberg #include <linux/gpio/driver.h>
1266c812d2SAndy Shevchenko #include <linux/interrupt.h>
13e57725eaSMika Westerberg #include <linux/log2.h>
146a33a1d6SAndy Shevchenko #include <linux/module.h>
157981c001SMika Westerberg #include <linux/platform_device.h>
16924cf800SAndy Shevchenko #include <linux/property.h>
17de23ccb1SAndy Shevchenko #include <linux/seq_file.h>
1898e63c11SAndy Shevchenko #include <linux/string_helpers.h>
196a33a1d6SAndy Shevchenko #include <linux/time.h>
20924cf800SAndy Shevchenko
21de23ccb1SAndy Shevchenko #include <linux/pinctrl/consumer.h>
227981c001SMika Westerberg #include <linux/pinctrl/pinconf.h>
237981c001SMika Westerberg #include <linux/pinctrl/pinconf-generic.h>
24de23ccb1SAndy Shevchenko #include <linux/pinctrl/pinctrl.h>
25de23ccb1SAndy Shevchenko #include <linux/pinctrl/pinmux.h>
267981c001SMika Westerberg
27eb78d360SAndy Shevchenko #include <linux/platform_data/x86/pwm-lpss.h>
287981c001SMika Westerberg
29c538b943SMika Westerberg #include "../core.h"
307981c001SMika Westerberg #include "pinctrl-intel.h"
317981c001SMika Westerberg
327981c001SMika Westerberg /* Offset from regs */
33e57725eaSMika Westerberg #define REVID 0x000
34e57725eaSMika Westerberg #define REVID_SHIFT 16
35e57725eaSMika Westerberg #define REVID_MASK GENMASK(31, 16)
36e57725eaSMika Westerberg
3791d898e5SAndy Shevchenko #define CAPLIST 0x004
3891d898e5SAndy Shevchenko #define CAPLIST_ID_SHIFT 16
3991d898e5SAndy Shevchenko #define CAPLIST_ID_MASK GENMASK(23, 16)
4091d898e5SAndy Shevchenko #define CAPLIST_ID_GPIO_HW_INFO 1
4191d898e5SAndy Shevchenko #define CAPLIST_ID_PWM 2
4291d898e5SAndy Shevchenko #define CAPLIST_ID_BLINK 3
4391d898e5SAndy Shevchenko #define CAPLIST_ID_EXP 4
4491d898e5SAndy Shevchenko #define CAPLIST_NEXT_SHIFT 0
4591d898e5SAndy Shevchenko #define CAPLIST_NEXT_MASK GENMASK(15, 0)
4691d898e5SAndy Shevchenko
477981c001SMika Westerberg #define PADBAR 0x00c
487981c001SMika Westerberg
497981c001SMika Westerberg #define PADOWN_BITS 4
507981c001SMika Westerberg #define PADOWN_SHIFT(p) ((p) % 8 * PADOWN_BITS)
51e58926e7SAndy Shevchenko #define PADOWN_MASK(p) (GENMASK(3, 0) << PADOWN_SHIFT(p))
5299a735b3SQipeng Zha #define PADOWN_GPP(p) ((p) / 8)
537981c001SMika Westerberg
54eb78d360SAndy Shevchenko #define PWMC 0x204
55eb78d360SAndy Shevchenko
567981c001SMika Westerberg /* Offset from pad_regs */
577981c001SMika Westerberg #define PADCFG0 0x000
58e58926e7SAndy Shevchenko #define PADCFG0_RXEVCFG_MASK GENMASK(26, 25)
59d1bfdf86SRaag Jadav #define PADCFG0_RXEVCFG_LEVEL (0 << 25)
60d1bfdf86SRaag Jadav #define PADCFG0_RXEVCFG_EDGE (1 << 25)
61d1bfdf86SRaag Jadav #define PADCFG0_RXEVCFG_DISABLED (2 << 25)
62d1bfdf86SRaag Jadav #define PADCFG0_RXEVCFG_EDGE_BOTH (3 << 25)
63e57725eaSMika Westerberg #define PADCFG0_PREGFRXSEL BIT(24)
647981c001SMika Westerberg #define PADCFG0_RXINV BIT(23)
657981c001SMika Westerberg #define PADCFG0_GPIROUTIOXAPIC BIT(20)
667981c001SMika Westerberg #define PADCFG0_GPIROUTSCI BIT(19)
677981c001SMika Westerberg #define PADCFG0_GPIROUTSMI BIT(18)
687981c001SMika Westerberg #define PADCFG0_GPIROUTNMI BIT(17)
697981c001SMika Westerberg #define PADCFG0_PMODE_SHIFT 10
70e58926e7SAndy Shevchenko #define PADCFG0_PMODE_MASK GENMASK(13, 10)
714973ddc8SAndy Shevchenko #define PADCFG0_PMODE_GPIO 0
727981c001SMika Westerberg #define PADCFG0_GPIORXDIS BIT(9)
737981c001SMika Westerberg #define PADCFG0_GPIOTXDIS BIT(8)
747981c001SMika Westerberg #define PADCFG0_GPIORXSTATE BIT(1)
757981c001SMika Westerberg #define PADCFG0_GPIOTXSTATE BIT(0)
767981c001SMika Westerberg
777981c001SMika Westerberg #define PADCFG1 0x004
787981c001SMika Westerberg #define PADCFG1_TERM_UP BIT(13)
797981c001SMika Westerberg #define PADCFG1_TERM_SHIFT 10
80e58926e7SAndy Shevchenko #define PADCFG1_TERM_MASK GENMASK(12, 10)
81dd26209bSAndy Shevchenko #define PADCFG1_TERM_20K BIT(2)
82dd26209bSAndy Shevchenko #define PADCFG1_TERM_5K BIT(1)
83a63dd601SAndy Shevchenko #define PADCFG1_TERM_4K (BIT(2) | BIT(1))
84dd26209bSAndy Shevchenko #define PADCFG1_TERM_1K BIT(0)
85a63dd601SAndy Shevchenko #define PADCFG1_TERM_952 (BIT(2) | BIT(0))
86dd26209bSAndy Shevchenko #define PADCFG1_TERM_833 (BIT(1) | BIT(0))
87a63dd601SAndy Shevchenko #define PADCFG1_TERM_800 (BIT(2) | BIT(1) | BIT(0))
887981c001SMika Westerberg
89e57725eaSMika Westerberg #define PADCFG2 0x008
90e57725eaSMika Westerberg #define PADCFG2_DEBOUNCE_SHIFT 1
91e57725eaSMika Westerberg #define PADCFG2_DEBOUNCE_MASK GENMASK(4, 1)
92203a1c3eSAndy Shevchenko #define PADCFG2_DEBEN BIT(0)
93e57725eaSMika Westerberg
946a33a1d6SAndy Shevchenko #define DEBOUNCE_PERIOD_NSEC 31250
95e57725eaSMika Westerberg
967981c001SMika Westerberg struct intel_pad_context {
977981c001SMika Westerberg u32 padcfg0;
987981c001SMika Westerberg u32 padcfg1;
99e57725eaSMika Westerberg u32 padcfg2;
1007981c001SMika Westerberg };
1017981c001SMika Westerberg
1027981c001SMika Westerberg struct intel_community_context {
1037981c001SMika Westerberg u32 *intmask;
104a0a5f766SChris Chiu u32 *hostown;
1057981c001SMika Westerberg };
1067981c001SMika Westerberg
1077981c001SMika Westerberg #define pin_to_padno(c, p) ((p) - (c)->pin_base)
108919eb475SMika Westerberg #define padgroup_offset(g, p) ((p) - (g)->base)
1097981c001SMika Westerberg
intel_get_community(struct intel_pinctrl * pctrl,unsigned int pin)11025018aceSRaag Jadav struct intel_community *intel_get_community(struct intel_pinctrl *pctrl, unsigned int pin)
1117981c001SMika Westerberg {
1127981c001SMika Westerberg struct intel_community *community;
1137981c001SMika Westerberg int i;
1147981c001SMika Westerberg
1157981c001SMika Westerberg for (i = 0; i < pctrl->ncommunities; i++) {
1167981c001SMika Westerberg community = &pctrl->communities[i];
1177981c001SMika Westerberg if (pin >= community->pin_base &&
1187981c001SMika Westerberg pin < community->pin_base + community->npins)
1197981c001SMika Westerberg return community;
1207981c001SMika Westerberg }
1217981c001SMika Westerberg
1227981c001SMika Westerberg dev_warn(pctrl->dev, "failed to find community for pin %u\n", pin);
1237981c001SMika Westerberg return NULL;
1247981c001SMika Westerberg }
12525018aceSRaag Jadav EXPORT_SYMBOL_NS_GPL(intel_get_community, PINCTRL_INTEL);
1267981c001SMika Westerberg
127919eb475SMika Westerberg static const struct intel_padgroup *
intel_community_get_padgroup(const struct intel_community * community,unsigned int pin)128919eb475SMika Westerberg intel_community_get_padgroup(const struct intel_community *community,
12904035f7fSAndy Shevchenko unsigned int pin)
130919eb475SMika Westerberg {
131919eb475SMika Westerberg int i;
132919eb475SMika Westerberg
133919eb475SMika Westerberg for (i = 0; i < community->ngpps; i++) {
134919eb475SMika Westerberg const struct intel_padgroup *padgrp = &community->gpps[i];
135919eb475SMika Westerberg
136919eb475SMika Westerberg if (pin >= padgrp->base && pin < padgrp->base + padgrp->size)
137919eb475SMika Westerberg return padgrp;
138919eb475SMika Westerberg }
139919eb475SMika Westerberg
140919eb475SMika Westerberg return NULL;
141919eb475SMika Westerberg }
142919eb475SMika Westerberg
intel_get_padcfg(struct intel_pinctrl * pctrl,unsigned int pin,unsigned int reg)14304035f7fSAndy Shevchenko static void __iomem *intel_get_padcfg(struct intel_pinctrl *pctrl,
14404035f7fSAndy Shevchenko unsigned int pin, unsigned int reg)
1457981c001SMika Westerberg {
1467981c001SMika Westerberg const struct intel_community *community;
14704035f7fSAndy Shevchenko unsigned int padno;
148e57725eaSMika Westerberg size_t nregs;
1497981c001SMika Westerberg
1507981c001SMika Westerberg community = intel_get_community(pctrl, pin);
1517981c001SMika Westerberg if (!community)
1527981c001SMika Westerberg return NULL;
1537981c001SMika Westerberg
1547981c001SMika Westerberg padno = pin_to_padno(community, pin);
155e57725eaSMika Westerberg nregs = (community->features & PINCTRL_FEATURE_DEBOUNCE) ? 4 : 2;
156e57725eaSMika Westerberg
1577eb7ecddSAndy Shevchenko if (reg >= nregs * 4)
158e57725eaSMika Westerberg return NULL;
159e57725eaSMika Westerberg
160e57725eaSMika Westerberg return community->pad_regs + reg + padno * nregs * 4;
1617981c001SMika Westerberg }
1627981c001SMika Westerberg
intel_pad_owned_by_host(struct intel_pinctrl * pctrl,unsigned int pin)16304035f7fSAndy Shevchenko static bool intel_pad_owned_by_host(struct intel_pinctrl *pctrl, unsigned int pin)
1647981c001SMika Westerberg {
1657981c001SMika Westerberg const struct intel_community *community;
166919eb475SMika Westerberg const struct intel_padgroup *padgrp;
16704035f7fSAndy Shevchenko unsigned int gpp, offset, gpp_offset;
1687981c001SMika Westerberg void __iomem *padown;
1697981c001SMika Westerberg
1707981c001SMika Westerberg community = intel_get_community(pctrl, pin);
1717981c001SMika Westerberg if (!community)
1727981c001SMika Westerberg return false;
1737981c001SMika Westerberg if (!community->padown_offset)
1747981c001SMika Westerberg return true;
1757981c001SMika Westerberg
176919eb475SMika Westerberg padgrp = intel_community_get_padgroup(community, pin);
177919eb475SMika Westerberg if (!padgrp)
178919eb475SMika Westerberg return false;
179919eb475SMika Westerberg
180919eb475SMika Westerberg gpp_offset = padgroup_offset(padgrp, pin);
181919eb475SMika Westerberg gpp = PADOWN_GPP(gpp_offset);
182919eb475SMika Westerberg offset = community->padown_offset + padgrp->padown_num * 4 + gpp * 4;
1837981c001SMika Westerberg padown = community->regs + offset;
1847981c001SMika Westerberg
185919eb475SMika Westerberg return !(readl(padown) & PADOWN_MASK(gpp_offset));
1867981c001SMika Westerberg }
1877981c001SMika Westerberg
intel_pad_acpi_mode(struct intel_pinctrl * pctrl,unsigned int pin)18804035f7fSAndy Shevchenko static bool intel_pad_acpi_mode(struct intel_pinctrl *pctrl, unsigned int pin)
1897981c001SMika Westerberg {
1907981c001SMika Westerberg const struct intel_community *community;
191919eb475SMika Westerberg const struct intel_padgroup *padgrp;
19204035f7fSAndy Shevchenko unsigned int offset, gpp_offset;
1937981c001SMika Westerberg void __iomem *hostown;
1947981c001SMika Westerberg
1957981c001SMika Westerberg community = intel_get_community(pctrl, pin);
1967981c001SMika Westerberg if (!community)
1977981c001SMika Westerberg return true;
1987981c001SMika Westerberg if (!community->hostown_offset)
1997981c001SMika Westerberg return false;
2007981c001SMika Westerberg
201919eb475SMika Westerberg padgrp = intel_community_get_padgroup(community, pin);
202919eb475SMika Westerberg if (!padgrp)
203919eb475SMika Westerberg return true;
204919eb475SMika Westerberg
205919eb475SMika Westerberg gpp_offset = padgroup_offset(padgrp, pin);
206919eb475SMika Westerberg offset = community->hostown_offset + padgrp->reg_num * 4;
2077981c001SMika Westerberg hostown = community->regs + offset;
2087981c001SMika Westerberg
209919eb475SMika Westerberg return !(readl(hostown) & BIT(gpp_offset));
2107981c001SMika Westerberg }
2117981c001SMika Westerberg
2121bd23153SAndy Shevchenko /**
2131bd23153SAndy Shevchenko * enum - Locking variants of the pad configuration
2141bd23153SAndy Shevchenko *
2151bd23153SAndy Shevchenko * @PAD_UNLOCKED: pad is fully controlled by the configuration registers
2161bd23153SAndy Shevchenko * @PAD_LOCKED: pad configuration registers, except TX state, are locked
2171bd23153SAndy Shevchenko * @PAD_LOCKED_TX: pad configuration TX state is locked
2181bd23153SAndy Shevchenko * @PAD_LOCKED_FULL: pad configuration registers are locked completely
2191bd23153SAndy Shevchenko *
2201bd23153SAndy Shevchenko * Locking is considered as read-only mode for corresponding registers and
2211bd23153SAndy Shevchenko * their respective fields. That said, TX state bit is locked separately from
2221bd23153SAndy Shevchenko * the main locking scheme.
2231bd23153SAndy Shevchenko */
2241bd23153SAndy Shevchenko enum {
2251bd23153SAndy Shevchenko PAD_UNLOCKED = 0,
2261bd23153SAndy Shevchenko PAD_LOCKED = 1,
2271bd23153SAndy Shevchenko PAD_LOCKED_TX = 2,
2281bd23153SAndy Shevchenko PAD_LOCKED_FULL = PAD_LOCKED | PAD_LOCKED_TX,
2291bd23153SAndy Shevchenko };
2301bd23153SAndy Shevchenko
intel_pad_locked(struct intel_pinctrl * pctrl,unsigned int pin)2311bd23153SAndy Shevchenko static int intel_pad_locked(struct intel_pinctrl *pctrl, unsigned int pin)
2327981c001SMika Westerberg {
2337981c001SMika Westerberg struct intel_community *community;
234919eb475SMika Westerberg const struct intel_padgroup *padgrp;
23504035f7fSAndy Shevchenko unsigned int offset, gpp_offset;
2367981c001SMika Westerberg u32 value;
2371bd23153SAndy Shevchenko int ret = PAD_UNLOCKED;
2387981c001SMika Westerberg
2397981c001SMika Westerberg community = intel_get_community(pctrl, pin);
2407981c001SMika Westerberg if (!community)
2411bd23153SAndy Shevchenko return PAD_LOCKED_FULL;
2427981c001SMika Westerberg if (!community->padcfglock_offset)
2431bd23153SAndy Shevchenko return PAD_UNLOCKED;
2447981c001SMika Westerberg
245919eb475SMika Westerberg padgrp = intel_community_get_padgroup(community, pin);
246919eb475SMika Westerberg if (!padgrp)
2471bd23153SAndy Shevchenko return PAD_LOCKED_FULL;
248919eb475SMika Westerberg
249919eb475SMika Westerberg gpp_offset = padgroup_offset(padgrp, pin);
2507981c001SMika Westerberg
2517981c001SMika Westerberg /*
2527981c001SMika Westerberg * If PADCFGLOCK and PADCFGLOCKTX bits are both clear for this pad,
2537981c001SMika Westerberg * the pad is considered unlocked. Any other case means that it is
2541bd23153SAndy Shevchenko * either fully or partially locked.
2557981c001SMika Westerberg */
2561bd23153SAndy Shevchenko offset = community->padcfglock_offset + 0 + padgrp->reg_num * 8;
2577981c001SMika Westerberg value = readl(community->regs + offset);
258919eb475SMika Westerberg if (value & BIT(gpp_offset))
2591bd23153SAndy Shevchenko ret |= PAD_LOCKED;
2607981c001SMika Westerberg
261919eb475SMika Westerberg offset = community->padcfglock_offset + 4 + padgrp->reg_num * 8;
2627981c001SMika Westerberg value = readl(community->regs + offset);
263919eb475SMika Westerberg if (value & BIT(gpp_offset))
2641bd23153SAndy Shevchenko ret |= PAD_LOCKED_TX;
2657981c001SMika Westerberg
2661bd23153SAndy Shevchenko return ret;
2671bd23153SAndy Shevchenko }
2681bd23153SAndy Shevchenko
intel_pad_is_unlocked(struct intel_pinctrl * pctrl,unsigned int pin)2691bd23153SAndy Shevchenko static bool intel_pad_is_unlocked(struct intel_pinctrl *pctrl, unsigned int pin)
2701bd23153SAndy Shevchenko {
2711bd23153SAndy Shevchenko return (intel_pad_locked(pctrl, pin) & PAD_LOCKED) == PAD_UNLOCKED;
2727981c001SMika Westerberg }
2737981c001SMika Westerberg
intel_pad_usable(struct intel_pinctrl * pctrl,unsigned int pin)27404035f7fSAndy Shevchenko static bool intel_pad_usable(struct intel_pinctrl *pctrl, unsigned int pin)
2757981c001SMika Westerberg {
2761bd23153SAndy Shevchenko return intel_pad_owned_by_host(pctrl, pin) && intel_pad_is_unlocked(pctrl, pin);
2777981c001SMika Westerberg }
2787981c001SMika Westerberg
intel_get_groups_count(struct pinctrl_dev * pctldev)27925018aceSRaag Jadav int intel_get_groups_count(struct pinctrl_dev *pctldev)
2807981c001SMika Westerberg {
2817981c001SMika Westerberg struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
2827981c001SMika Westerberg
2837981c001SMika Westerberg return pctrl->soc->ngroups;
2847981c001SMika Westerberg }
28525018aceSRaag Jadav EXPORT_SYMBOL_NS_GPL(intel_get_groups_count, PINCTRL_INTEL);
2867981c001SMika Westerberg
intel_get_group_name(struct pinctrl_dev * pctldev,unsigned int group)28725018aceSRaag Jadav const char *intel_get_group_name(struct pinctrl_dev *pctldev, unsigned int group)
2887981c001SMika Westerberg {
2897981c001SMika Westerberg struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
2907981c001SMika Westerberg
2914426be36SAndy Shevchenko return pctrl->soc->groups[group].grp.name;
2927981c001SMika Westerberg }
29325018aceSRaag Jadav EXPORT_SYMBOL_NS_GPL(intel_get_group_name, PINCTRL_INTEL);
2947981c001SMika Westerberg
intel_get_group_pins(struct pinctrl_dev * pctldev,unsigned int group,const unsigned int ** pins,unsigned int * npins)29525018aceSRaag Jadav int intel_get_group_pins(struct pinctrl_dev *pctldev, unsigned int group,
29604035f7fSAndy Shevchenko const unsigned int **pins, unsigned int *npins)
2977981c001SMika Westerberg {
2987981c001SMika Westerberg struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
2997981c001SMika Westerberg
3004426be36SAndy Shevchenko *pins = pctrl->soc->groups[group].grp.pins;
3014426be36SAndy Shevchenko *npins = pctrl->soc->groups[group].grp.npins;
3027981c001SMika Westerberg return 0;
3037981c001SMika Westerberg }
30425018aceSRaag Jadav EXPORT_SYMBOL_NS_GPL(intel_get_group_pins, PINCTRL_INTEL);
3057981c001SMika Westerberg
intel_pin_dbg_show(struct pinctrl_dev * pctldev,struct seq_file * s,unsigned int pin)3067981c001SMika Westerberg static void intel_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
30704035f7fSAndy Shevchenko unsigned int pin)
3087981c001SMika Westerberg {
3097981c001SMika Westerberg struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
310e57725eaSMika Westerberg void __iomem *padcfg;
3117981c001SMika Westerberg u32 cfg0, cfg1, mode;
3121bd23153SAndy Shevchenko int locked;
3131bd23153SAndy Shevchenko bool acpi;
3147981c001SMika Westerberg
3157981c001SMika Westerberg if (!intel_pad_owned_by_host(pctrl, pin)) {
3167981c001SMika Westerberg seq_puts(s, "not available");
3177981c001SMika Westerberg return;
3187981c001SMika Westerberg }
3197981c001SMika Westerberg
3207981c001SMika Westerberg cfg0 = readl(intel_get_padcfg(pctrl, pin, PADCFG0));
3217981c001SMika Westerberg cfg1 = readl(intel_get_padcfg(pctrl, pin, PADCFG1));
3227981c001SMika Westerberg
3237981c001SMika Westerberg mode = (cfg0 & PADCFG0_PMODE_MASK) >> PADCFG0_PMODE_SHIFT;
3244973ddc8SAndy Shevchenko if (mode == PADCFG0_PMODE_GPIO)
3257981c001SMika Westerberg seq_puts(s, "GPIO ");
3267981c001SMika Westerberg else
3277981c001SMika Westerberg seq_printf(s, "mode %d ", mode);
3287981c001SMika Westerberg
3297981c001SMika Westerberg seq_printf(s, "0x%08x 0x%08x", cfg0, cfg1);
3307981c001SMika Westerberg
331e57725eaSMika Westerberg /* Dump the additional PADCFG registers if available */
332e57725eaSMika Westerberg padcfg = intel_get_padcfg(pctrl, pin, PADCFG2);
333e57725eaSMika Westerberg if (padcfg)
334e57725eaSMika Westerberg seq_printf(s, " 0x%08x", readl(padcfg));
335e57725eaSMika Westerberg
3367981c001SMika Westerberg locked = intel_pad_locked(pctrl, pin);
3374341e8a5SMika Westerberg acpi = intel_pad_acpi_mode(pctrl, pin);
3387981c001SMika Westerberg
3397981c001SMika Westerberg if (locked || acpi) {
3407981c001SMika Westerberg seq_puts(s, " [");
3411bd23153SAndy Shevchenko if (locked)
3427981c001SMika Westerberg seq_puts(s, "LOCKED");
3431bd23153SAndy Shevchenko if ((locked & PAD_LOCKED_FULL) == PAD_LOCKED_TX)
3441bd23153SAndy Shevchenko seq_puts(s, " tx");
3451bd23153SAndy Shevchenko else if ((locked & PAD_LOCKED_FULL) == PAD_LOCKED_FULL)
3461bd23153SAndy Shevchenko seq_puts(s, " full");
3471bd23153SAndy Shevchenko
3481bd23153SAndy Shevchenko if (locked && acpi)
3497981c001SMika Westerberg seq_puts(s, ", ");
3501bd23153SAndy Shevchenko
3517981c001SMika Westerberg if (acpi)
3527981c001SMika Westerberg seq_puts(s, "ACPI");
3537981c001SMika Westerberg seq_puts(s, "]");
3547981c001SMika Westerberg }
3557981c001SMika Westerberg }
3567981c001SMika Westerberg
3577981c001SMika Westerberg static const struct pinctrl_ops intel_pinctrl_ops = {
3587981c001SMika Westerberg .get_groups_count = intel_get_groups_count,
3597981c001SMika Westerberg .get_group_name = intel_get_group_name,
3607981c001SMika Westerberg .get_group_pins = intel_get_group_pins,
3617981c001SMika Westerberg .pin_dbg_show = intel_pin_dbg_show,
3627981c001SMika Westerberg };
3637981c001SMika Westerberg
intel_get_functions_count(struct pinctrl_dev * pctldev)36425018aceSRaag Jadav int intel_get_functions_count(struct pinctrl_dev *pctldev)
3657981c001SMika Westerberg {
3667981c001SMika Westerberg struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
3677981c001SMika Westerberg
3687981c001SMika Westerberg return pctrl->soc->nfunctions;
3697981c001SMika Westerberg }
37025018aceSRaag Jadav EXPORT_SYMBOL_NS_GPL(intel_get_functions_count, PINCTRL_INTEL);
3717981c001SMika Westerberg
intel_get_function_name(struct pinctrl_dev * pctldev,unsigned int function)37225018aceSRaag Jadav const char *intel_get_function_name(struct pinctrl_dev *pctldev, unsigned int function)
3737981c001SMika Westerberg {
3747981c001SMika Westerberg struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
3757981c001SMika Westerberg
376999b85bfSAndy Shevchenko return pctrl->soc->functions[function].func.name;
3777981c001SMika Westerberg }
37825018aceSRaag Jadav EXPORT_SYMBOL_NS_GPL(intel_get_function_name, PINCTRL_INTEL);
3797981c001SMika Westerberg
intel_get_function_groups(struct pinctrl_dev * pctldev,unsigned int function,const char * const ** groups,unsigned int * const ngroups)38025018aceSRaag Jadav int intel_get_function_groups(struct pinctrl_dev *pctldev, unsigned int function,
38125018aceSRaag Jadav const char * const **groups, unsigned int * const ngroups)
3827981c001SMika Westerberg {
3837981c001SMika Westerberg struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
3847981c001SMika Westerberg
385999b85bfSAndy Shevchenko *groups = pctrl->soc->functions[function].func.groups;
386999b85bfSAndy Shevchenko *ngroups = pctrl->soc->functions[function].func.ngroups;
3877981c001SMika Westerberg return 0;
3887981c001SMika Westerberg }
38925018aceSRaag Jadav EXPORT_SYMBOL_NS_GPL(intel_get_function_groups, PINCTRL_INTEL);
3907981c001SMika Westerberg
intel_pinmux_set_mux(struct pinctrl_dev * pctldev,unsigned int function,unsigned int group)39104035f7fSAndy Shevchenko static int intel_pinmux_set_mux(struct pinctrl_dev *pctldev,
39204035f7fSAndy Shevchenko unsigned int function, unsigned int group)
3937981c001SMika Westerberg {
3947981c001SMika Westerberg struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
3957981c001SMika Westerberg const struct intel_pingroup *grp = &pctrl->soc->groups[group];
3967981c001SMika Westerberg unsigned long flags;
3977981c001SMika Westerberg int i;
3987981c001SMika Westerberg
39927d9098cSMika Westerberg raw_spin_lock_irqsave(&pctrl->lock, flags);
4007981c001SMika Westerberg
4017981c001SMika Westerberg /*
4027981c001SMika Westerberg * All pins in the groups needs to be accessible and writable
4037981c001SMika Westerberg * before we can enable the mux for this group.
4047981c001SMika Westerberg */
4054426be36SAndy Shevchenko for (i = 0; i < grp->grp.npins; i++) {
4064426be36SAndy Shevchenko if (!intel_pad_usable(pctrl, grp->grp.pins[i])) {
40727d9098cSMika Westerberg raw_spin_unlock_irqrestore(&pctrl->lock, flags);
4087981c001SMika Westerberg return -EBUSY;
4097981c001SMika Westerberg }
4107981c001SMika Westerberg }
4117981c001SMika Westerberg
4127981c001SMika Westerberg /* Now enable the mux setting for each pin in the group */
4134426be36SAndy Shevchenko for (i = 0; i < grp->grp.npins; i++) {
4147981c001SMika Westerberg void __iomem *padcfg0;
415e95433c3SRaag Jadav u32 value, pmode;
4167981c001SMika Westerberg
4174426be36SAndy Shevchenko padcfg0 = intel_get_padcfg(pctrl, grp->grp.pins[i], PADCFG0);
4187981c001SMika Westerberg
419e95433c3SRaag Jadav value = readl(padcfg0);
4207981c001SMika Westerberg value &= ~PADCFG0_PMODE_MASK;
4211f6b419bSMika Westerberg
4221f6b419bSMika Westerberg if (grp->modes)
423e95433c3SRaag Jadav pmode = grp->modes[i];
4241f6b419bSMika Westerberg else
425e95433c3SRaag Jadav pmode = grp->mode;
4267981c001SMika Westerberg
427e95433c3SRaag Jadav value |= pmode << PADCFG0_PMODE_SHIFT;
4287981c001SMika Westerberg writel(value, padcfg0);
4297981c001SMika Westerberg }
4307981c001SMika Westerberg
43127d9098cSMika Westerberg raw_spin_unlock_irqrestore(&pctrl->lock, flags);
4327981c001SMika Westerberg
4337981c001SMika Westerberg return 0;
4347981c001SMika Westerberg }
4357981c001SMika Westerberg
__intel_gpio_set_direction(void __iomem * padcfg0,bool input)43617fab473SAndy Shevchenko static void __intel_gpio_set_direction(void __iomem *padcfg0, bool input)
43717fab473SAndy Shevchenko {
43817fab473SAndy Shevchenko u32 value;
43917fab473SAndy Shevchenko
44017fab473SAndy Shevchenko value = readl(padcfg0);
44117fab473SAndy Shevchenko if (input) {
44217fab473SAndy Shevchenko value &= ~PADCFG0_GPIORXDIS;
44317fab473SAndy Shevchenko value |= PADCFG0_GPIOTXDIS;
44417fab473SAndy Shevchenko } else {
44517fab473SAndy Shevchenko value &= ~PADCFG0_GPIOTXDIS;
44617fab473SAndy Shevchenko value |= PADCFG0_GPIORXDIS;
44717fab473SAndy Shevchenko }
44817fab473SAndy Shevchenko writel(value, padcfg0);
44917fab473SAndy Shevchenko }
45017fab473SAndy Shevchenko
__intel_gpio_get_gpio_mode(u32 value)4516989ea48SAndy Shevchenko static int __intel_gpio_get_gpio_mode(u32 value)
4526989ea48SAndy Shevchenko {
4536989ea48SAndy Shevchenko return (value & PADCFG0_PMODE_MASK) >> PADCFG0_PMODE_SHIFT;
4546989ea48SAndy Shevchenko }
4556989ea48SAndy Shevchenko
intel_gpio_get_gpio_mode(void __iomem * padcfg0)4564973ddc8SAndy Shevchenko static int intel_gpio_get_gpio_mode(void __iomem *padcfg0)
4574973ddc8SAndy Shevchenko {
4586989ea48SAndy Shevchenko return __intel_gpio_get_gpio_mode(readl(padcfg0));
4594973ddc8SAndy Shevchenko }
4604973ddc8SAndy Shevchenko
intel_gpio_set_gpio_mode(void __iomem * padcfg0)461f5a26acfSMika Westerberg static void intel_gpio_set_gpio_mode(void __iomem *padcfg0)
462f5a26acfSMika Westerberg {
463f5a26acfSMika Westerberg u32 value;
464f5a26acfSMika Westerberg
465af7e3eebSAndy Shevchenko value = readl(padcfg0);
466af7e3eebSAndy Shevchenko
467f5a26acfSMika Westerberg /* Put the pad into GPIO mode */
468af7e3eebSAndy Shevchenko value &= ~PADCFG0_PMODE_MASK;
469af7e3eebSAndy Shevchenko value |= PADCFG0_PMODE_GPIO;
470af7e3eebSAndy Shevchenko
471e12963c4SAndy Shevchenko /* Disable TX buffer and enable RX (this will be input) */
472e12963c4SAndy Shevchenko value &= ~PADCFG0_GPIORXDIS;
473e8873c0aSAndy Shevchenko value |= PADCFG0_GPIOTXDIS;
474af7e3eebSAndy Shevchenko
475f5a26acfSMika Westerberg /* Disable SCI/SMI/NMI generation */
476f5a26acfSMika Westerberg value &= ~(PADCFG0_GPIROUTIOXAPIC | PADCFG0_GPIROUTSCI);
477f5a26acfSMika Westerberg value &= ~(PADCFG0_GPIROUTSMI | PADCFG0_GPIROUTNMI);
478af7e3eebSAndy Shevchenko
479f5a26acfSMika Westerberg writel(value, padcfg0);
480f5a26acfSMika Westerberg }
481f5a26acfSMika Westerberg
intel_gpio_request_enable(struct pinctrl_dev * pctldev,struct pinctrl_gpio_range * range,unsigned int pin)4827981c001SMika Westerberg static int intel_gpio_request_enable(struct pinctrl_dev *pctldev,
4837981c001SMika Westerberg struct pinctrl_gpio_range *range,
48404035f7fSAndy Shevchenko unsigned int pin)
4857981c001SMika Westerberg {
4867981c001SMika Westerberg struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
4877981c001SMika Westerberg void __iomem *padcfg0;
4887981c001SMika Westerberg unsigned long flags;
4897981c001SMika Westerberg
490f62cdde5SAndy Shevchenko padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
491f62cdde5SAndy Shevchenko
49227d9098cSMika Westerberg raw_spin_lock_irqsave(&pctrl->lock, flags);
4937981c001SMika Westerberg
4941bd23153SAndy Shevchenko if (!intel_pad_owned_by_host(pctrl, pin)) {
49527d9098cSMika Westerberg raw_spin_unlock_irqrestore(&pctrl->lock, flags);
4967981c001SMika Westerberg return -EBUSY;
4977981c001SMika Westerberg }
4987981c001SMika Westerberg
4991bd23153SAndy Shevchenko if (!intel_pad_is_unlocked(pctrl, pin)) {
5001bd23153SAndy Shevchenko raw_spin_unlock_irqrestore(&pctrl->lock, flags);
5011bd23153SAndy Shevchenko return 0;
5021bd23153SAndy Shevchenko }
5031bd23153SAndy Shevchenko
5044973ddc8SAndy Shevchenko /*
5054973ddc8SAndy Shevchenko * If pin is already configured in GPIO mode, we assume that
5064973ddc8SAndy Shevchenko * firmware provides correct settings. In such case we avoid
5074973ddc8SAndy Shevchenko * potential glitches on the pin. Otherwise, for the pin in
5084973ddc8SAndy Shevchenko * alternative mode, consumer has to supply respective flags.
5094973ddc8SAndy Shevchenko */
5104973ddc8SAndy Shevchenko if (intel_gpio_get_gpio_mode(padcfg0) == PADCFG0_PMODE_GPIO) {
5114973ddc8SAndy Shevchenko raw_spin_unlock_irqrestore(&pctrl->lock, flags);
5124973ddc8SAndy Shevchenko return 0;
5134973ddc8SAndy Shevchenko }
5144973ddc8SAndy Shevchenko
515f5a26acfSMika Westerberg intel_gpio_set_gpio_mode(padcfg0);
5164973ddc8SAndy Shevchenko
51727d9098cSMika Westerberg raw_spin_unlock_irqrestore(&pctrl->lock, flags);
5187981c001SMika Westerberg
5197981c001SMika Westerberg return 0;
5207981c001SMika Westerberg }
5217981c001SMika Westerberg
intel_gpio_set_direction(struct pinctrl_dev * pctldev,struct pinctrl_gpio_range * range,unsigned int pin,bool input)5227981c001SMika Westerberg static int intel_gpio_set_direction(struct pinctrl_dev *pctldev,
5237981c001SMika Westerberg struct pinctrl_gpio_range *range,
52404035f7fSAndy Shevchenko unsigned int pin, bool input)
5257981c001SMika Westerberg {
5267981c001SMika Westerberg struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
5277981c001SMika Westerberg void __iomem *padcfg0;
5287981c001SMika Westerberg unsigned long flags;
5297981c001SMika Westerberg
5307981c001SMika Westerberg padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
5317981c001SMika Westerberg
532f62cdde5SAndy Shevchenko raw_spin_lock_irqsave(&pctrl->lock, flags);
533f62cdde5SAndy Shevchenko __intel_gpio_set_direction(padcfg0, input);
53427d9098cSMika Westerberg raw_spin_unlock_irqrestore(&pctrl->lock, flags);
5357981c001SMika Westerberg
5367981c001SMika Westerberg return 0;
5377981c001SMika Westerberg }
5387981c001SMika Westerberg
5397981c001SMika Westerberg static const struct pinmux_ops intel_pinmux_ops = {
5407981c001SMika Westerberg .get_functions_count = intel_get_functions_count,
5417981c001SMika Westerberg .get_function_name = intel_get_function_name,
5427981c001SMika Westerberg .get_function_groups = intel_get_function_groups,
5437981c001SMika Westerberg .set_mux = intel_pinmux_set_mux,
5447981c001SMika Westerberg .gpio_request_enable = intel_gpio_request_enable,
5457981c001SMika Westerberg .gpio_set_direction = intel_gpio_set_direction,
5467981c001SMika Westerberg };
5477981c001SMika Westerberg
intel_config_get_pull(struct intel_pinctrl * pctrl,unsigned int pin,enum pin_config_param param,u32 * arg)54881ab5542SAndy Shevchenko static int intel_config_get_pull(struct intel_pinctrl *pctrl, unsigned int pin,
54981ab5542SAndy Shevchenko enum pin_config_param param, u32 *arg)
5507981c001SMika Westerberg {
55104cc058fSMika Westerberg const struct intel_community *community;
55281ab5542SAndy Shevchenko void __iomem *padcfg1;
553e64fbfa5SAndy Shevchenko unsigned long flags;
5547981c001SMika Westerberg u32 value, term;
5557981c001SMika Westerberg
55604cc058fSMika Westerberg community = intel_get_community(pctrl, pin);
55781ab5542SAndy Shevchenko padcfg1 = intel_get_padcfg(pctrl, pin, PADCFG1);
558e64fbfa5SAndy Shevchenko
559e64fbfa5SAndy Shevchenko raw_spin_lock_irqsave(&pctrl->lock, flags);
56081ab5542SAndy Shevchenko value = readl(padcfg1);
561e64fbfa5SAndy Shevchenko raw_spin_unlock_irqrestore(&pctrl->lock, flags);
56281ab5542SAndy Shevchenko
5637981c001SMika Westerberg term = (value & PADCFG1_TERM_MASK) >> PADCFG1_TERM_SHIFT;
5647981c001SMika Westerberg
5657981c001SMika Westerberg switch (param) {
5667981c001SMika Westerberg case PIN_CONFIG_BIAS_DISABLE:
5677981c001SMika Westerberg if (term)
5687981c001SMika Westerberg return -EINVAL;
5697981c001SMika Westerberg break;
5707981c001SMika Westerberg
5717981c001SMika Westerberg case PIN_CONFIG_BIAS_PULL_UP:
5727981c001SMika Westerberg if (!term || !(value & PADCFG1_TERM_UP))
5737981c001SMika Westerberg return -EINVAL;
5747981c001SMika Westerberg
5757981c001SMika Westerberg switch (term) {
576dd26209bSAndy Shevchenko case PADCFG1_TERM_833:
577dd26209bSAndy Shevchenko *arg = 833;
578dd26209bSAndy Shevchenko break;
5797981c001SMika Westerberg case PADCFG1_TERM_1K:
58081ab5542SAndy Shevchenko *arg = 1000;
5817981c001SMika Westerberg break;
582346c8364SAndy Shevchenko case PADCFG1_TERM_4K:
583346c8364SAndy Shevchenko *arg = 4000;
584346c8364SAndy Shevchenko break;
5857981c001SMika Westerberg case PADCFG1_TERM_5K:
58681ab5542SAndy Shevchenko *arg = 5000;
5877981c001SMika Westerberg break;
5887981c001SMika Westerberg case PADCFG1_TERM_20K:
58981ab5542SAndy Shevchenko *arg = 20000;
5907981c001SMika Westerberg break;
5917981c001SMika Westerberg }
5927981c001SMika Westerberg
5937981c001SMika Westerberg break;
5947981c001SMika Westerberg
5957981c001SMika Westerberg case PIN_CONFIG_BIAS_PULL_DOWN:
5967981c001SMika Westerberg if (!term || value & PADCFG1_TERM_UP)
5977981c001SMika Westerberg return -EINVAL;
5987981c001SMika Westerberg
5997981c001SMika Westerberg switch (term) {
600dd26209bSAndy Shevchenko case PADCFG1_TERM_833:
601dd26209bSAndy Shevchenko if (!(community->features & PINCTRL_FEATURE_1K_PD))
602dd26209bSAndy Shevchenko return -EINVAL;
603dd26209bSAndy Shevchenko *arg = 833;
604dd26209bSAndy Shevchenko break;
60504cc058fSMika Westerberg case PADCFG1_TERM_1K:
60604cc058fSMika Westerberg if (!(community->features & PINCTRL_FEATURE_1K_PD))
60704cc058fSMika Westerberg return -EINVAL;
60881ab5542SAndy Shevchenko *arg = 1000;
60904cc058fSMika Westerberg break;
610346c8364SAndy Shevchenko case PADCFG1_TERM_4K:
611346c8364SAndy Shevchenko *arg = 4000;
612346c8364SAndy Shevchenko break;
6137981c001SMika Westerberg case PADCFG1_TERM_5K:
61481ab5542SAndy Shevchenko *arg = 5000;
6157981c001SMika Westerberg break;
6167981c001SMika Westerberg case PADCFG1_TERM_20K:
61781ab5542SAndy Shevchenko *arg = 20000;
6187981c001SMika Westerberg break;
6197981c001SMika Westerberg }
6207981c001SMika Westerberg
6217981c001SMika Westerberg break;
6227981c001SMika Westerberg
62381ab5542SAndy Shevchenko default:
62481ab5542SAndy Shevchenko return -EINVAL;
62581ab5542SAndy Shevchenko }
62681ab5542SAndy Shevchenko
62781ab5542SAndy Shevchenko return 0;
62881ab5542SAndy Shevchenko }
62981ab5542SAndy Shevchenko
intel_config_get_debounce(struct intel_pinctrl * pctrl,unsigned int pin,enum pin_config_param param,u32 * arg)63081ab5542SAndy Shevchenko static int intel_config_get_debounce(struct intel_pinctrl *pctrl, unsigned int pin,
63181ab5542SAndy Shevchenko enum pin_config_param param, u32 *arg)
63281ab5542SAndy Shevchenko {
633e57725eaSMika Westerberg void __iomem *padcfg2;
634e64fbfa5SAndy Shevchenko unsigned long flags;
63581ab5542SAndy Shevchenko unsigned long v;
63681ab5542SAndy Shevchenko u32 value2;
637e57725eaSMika Westerberg
638e57725eaSMika Westerberg padcfg2 = intel_get_padcfg(pctrl, pin, PADCFG2);
639e57725eaSMika Westerberg if (!padcfg2)
640e57725eaSMika Westerberg return -ENOTSUPP;
641e57725eaSMika Westerberg
642e64fbfa5SAndy Shevchenko raw_spin_lock_irqsave(&pctrl->lock, flags);
64381ab5542SAndy Shevchenko value2 = readl(padcfg2);
644e64fbfa5SAndy Shevchenko raw_spin_unlock_irqrestore(&pctrl->lock, flags);
64581ab5542SAndy Shevchenko if (!(value2 & PADCFG2_DEBEN))
646e57725eaSMika Westerberg return -EINVAL;
647e57725eaSMika Westerberg
64881ab5542SAndy Shevchenko v = (value2 & PADCFG2_DEBOUNCE_MASK) >> PADCFG2_DEBOUNCE_SHIFT;
64981ab5542SAndy Shevchenko *arg = BIT(v) * DEBOUNCE_PERIOD_NSEC / NSEC_PER_USEC;
650e57725eaSMika Westerberg
65181ab5542SAndy Shevchenko return 0;
652e57725eaSMika Westerberg }
653e57725eaSMika Westerberg
intel_config_get(struct pinctrl_dev * pctldev,unsigned int pin,unsigned long * config)65481ab5542SAndy Shevchenko static int intel_config_get(struct pinctrl_dev *pctldev, unsigned int pin,
65581ab5542SAndy Shevchenko unsigned long *config)
65681ab5542SAndy Shevchenko {
65781ab5542SAndy Shevchenko struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
65881ab5542SAndy Shevchenko enum pin_config_param param = pinconf_to_config_param(*config);
65981ab5542SAndy Shevchenko u32 arg = 0;
66081ab5542SAndy Shevchenko int ret;
66181ab5542SAndy Shevchenko
66281ab5542SAndy Shevchenko if (!intel_pad_owned_by_host(pctrl, pin))
66381ab5542SAndy Shevchenko return -ENOTSUPP;
66481ab5542SAndy Shevchenko
66581ab5542SAndy Shevchenko switch (param) {
66681ab5542SAndy Shevchenko case PIN_CONFIG_BIAS_DISABLE:
66781ab5542SAndy Shevchenko case PIN_CONFIG_BIAS_PULL_UP:
66881ab5542SAndy Shevchenko case PIN_CONFIG_BIAS_PULL_DOWN:
66981ab5542SAndy Shevchenko ret = intel_config_get_pull(pctrl, pin, param, &arg);
67081ab5542SAndy Shevchenko if (ret)
67181ab5542SAndy Shevchenko return ret;
67281ab5542SAndy Shevchenko break;
67381ab5542SAndy Shevchenko
67481ab5542SAndy Shevchenko case PIN_CONFIG_INPUT_DEBOUNCE:
67581ab5542SAndy Shevchenko ret = intel_config_get_debounce(pctrl, pin, param, &arg);
67681ab5542SAndy Shevchenko if (ret)
67781ab5542SAndy Shevchenko return ret;
67881ab5542SAndy Shevchenko break;
67981ab5542SAndy Shevchenko
6807981c001SMika Westerberg default:
6817981c001SMika Westerberg return -ENOTSUPP;
6827981c001SMika Westerberg }
6837981c001SMika Westerberg
6847981c001SMika Westerberg *config = pinconf_to_config_packed(param, arg);
6857981c001SMika Westerberg return 0;
6867981c001SMika Westerberg }
6877981c001SMika Westerberg
intel_config_set_pull(struct intel_pinctrl * pctrl,unsigned int pin,unsigned long config)68804035f7fSAndy Shevchenko static int intel_config_set_pull(struct intel_pinctrl *pctrl, unsigned int pin,
6897981c001SMika Westerberg unsigned long config)
6907981c001SMika Westerberg {
69104035f7fSAndy Shevchenko unsigned int param = pinconf_to_config_param(config);
69204035f7fSAndy Shevchenko unsigned int arg = pinconf_to_config_argument(config);
69304cc058fSMika Westerberg const struct intel_community *community;
6947981c001SMika Westerberg void __iomem *padcfg1;
6957981c001SMika Westerberg unsigned long flags;
6967981c001SMika Westerberg int ret = 0;
6977981c001SMika Westerberg u32 value;
6987981c001SMika Westerberg
69904cc058fSMika Westerberg community = intel_get_community(pctrl, pin);
7007981c001SMika Westerberg padcfg1 = intel_get_padcfg(pctrl, pin, PADCFG1);
701f62cdde5SAndy Shevchenko
702f62cdde5SAndy Shevchenko raw_spin_lock_irqsave(&pctrl->lock, flags);
703f62cdde5SAndy Shevchenko
7047981c001SMika Westerberg value = readl(padcfg1);
705cd535346SAndy Shevchenko value &= ~(PADCFG1_TERM_MASK | PADCFG1_TERM_UP);
706cd535346SAndy Shevchenko
707cd535346SAndy Shevchenko /* Set default strength value in case none is given */
708cd535346SAndy Shevchenko if (arg == 1)
709cd535346SAndy Shevchenko arg = 5000;
7107981c001SMika Westerberg
7117981c001SMika Westerberg switch (param) {
7127981c001SMika Westerberg case PIN_CONFIG_BIAS_DISABLE:
7137981c001SMika Westerberg break;
7147981c001SMika Westerberg
7157981c001SMika Westerberg case PIN_CONFIG_BIAS_PULL_UP:
7167981c001SMika Westerberg switch (arg) {
7177981c001SMika Westerberg case 20000:
7187981c001SMika Westerberg value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT;
7197981c001SMika Westerberg break;
7207981c001SMika Westerberg case 5000:
7217981c001SMika Westerberg value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT;
7227981c001SMika Westerberg break;
723346c8364SAndy Shevchenko case 4000:
724346c8364SAndy Shevchenko value |= PADCFG1_TERM_4K << PADCFG1_TERM_SHIFT;
725346c8364SAndy Shevchenko break;
7267981c001SMika Westerberg case 1000:
7277981c001SMika Westerberg value |= PADCFG1_TERM_1K << PADCFG1_TERM_SHIFT;
7287981c001SMika Westerberg break;
729dd26209bSAndy Shevchenko case 833:
730dd26209bSAndy Shevchenko value |= PADCFG1_TERM_833 << PADCFG1_TERM_SHIFT;
731dd26209bSAndy Shevchenko break;
7327981c001SMika Westerberg default:
7337981c001SMika Westerberg ret = -EINVAL;
734cd535346SAndy Shevchenko break;
7357981c001SMika Westerberg }
7367981c001SMika Westerberg
737cd535346SAndy Shevchenko value |= PADCFG1_TERM_UP;
7387981c001SMika Westerberg break;
7397981c001SMika Westerberg
7407981c001SMika Westerberg case PIN_CONFIG_BIAS_PULL_DOWN:
7417981c001SMika Westerberg switch (arg) {
7427981c001SMika Westerberg case 20000:
7437981c001SMika Westerberg value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT;
7447981c001SMika Westerberg break;
7457981c001SMika Westerberg case 5000:
7467981c001SMika Westerberg value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT;
7477981c001SMika Westerberg break;
748346c8364SAndy Shevchenko case 4000:
749346c8364SAndy Shevchenko value |= PADCFG1_TERM_4K << PADCFG1_TERM_SHIFT;
750346c8364SAndy Shevchenko break;
75104cc058fSMika Westerberg case 1000:
752aa1dd80fSDan Carpenter if (!(community->features & PINCTRL_FEATURE_1K_PD)) {
753aa1dd80fSDan Carpenter ret = -EINVAL;
754aa1dd80fSDan Carpenter break;
755aa1dd80fSDan Carpenter }
75604cc058fSMika Westerberg value |= PADCFG1_TERM_1K << PADCFG1_TERM_SHIFT;
75704cc058fSMika Westerberg break;
758dd26209bSAndy Shevchenko case 833:
759dd26209bSAndy Shevchenko if (!(community->features & PINCTRL_FEATURE_1K_PD)) {
760dd26209bSAndy Shevchenko ret = -EINVAL;
761dd26209bSAndy Shevchenko break;
762dd26209bSAndy Shevchenko }
763dd26209bSAndy Shevchenko value |= PADCFG1_TERM_833 << PADCFG1_TERM_SHIFT;
764dd26209bSAndy Shevchenko break;
7657981c001SMika Westerberg default:
7667981c001SMika Westerberg ret = -EINVAL;
767cd535346SAndy Shevchenko break;
7687981c001SMika Westerberg }
7697981c001SMika Westerberg
7707981c001SMika Westerberg break;
77161ef0e49SAndy Shevchenko
77261ef0e49SAndy Shevchenko default:
77361ef0e49SAndy Shevchenko ret = -EINVAL;
77461ef0e49SAndy Shevchenko break;
7757981c001SMika Westerberg }
7767981c001SMika Westerberg
7777981c001SMika Westerberg if (!ret)
7787981c001SMika Westerberg writel(value, padcfg1);
7797981c001SMika Westerberg
78027d9098cSMika Westerberg raw_spin_unlock_irqrestore(&pctrl->lock, flags);
7817981c001SMika Westerberg
7827981c001SMika Westerberg return ret;
7837981c001SMika Westerberg }
7847981c001SMika Westerberg
intel_config_set_debounce(struct intel_pinctrl * pctrl,unsigned int pin,unsigned int debounce)78504035f7fSAndy Shevchenko static int intel_config_set_debounce(struct intel_pinctrl *pctrl,
78604035f7fSAndy Shevchenko unsigned int pin, unsigned int debounce)
787e57725eaSMika Westerberg {
788e57725eaSMika Westerberg void __iomem *padcfg0, *padcfg2;
789e57725eaSMika Westerberg unsigned long flags;
790e57725eaSMika Westerberg u32 value0, value2;
791e57725eaSMika Westerberg
792e57725eaSMika Westerberg padcfg2 = intel_get_padcfg(pctrl, pin, PADCFG2);
793e57725eaSMika Westerberg if (!padcfg2)
794e57725eaSMika Westerberg return -ENOTSUPP;
795e57725eaSMika Westerberg
796e57725eaSMika Westerberg padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
797e57725eaSMika Westerberg
798e57725eaSMika Westerberg raw_spin_lock_irqsave(&pctrl->lock, flags);
799e57725eaSMika Westerberg
800e57725eaSMika Westerberg value0 = readl(padcfg0);
801e57725eaSMika Westerberg value2 = readl(padcfg2);
802e57725eaSMika Westerberg
803e57725eaSMika Westerberg /* Disable glitch filter and debouncer */
804e57725eaSMika Westerberg value0 &= ~PADCFG0_PREGFRXSEL;
805e57725eaSMika Westerberg value2 &= ~(PADCFG2_DEBEN | PADCFG2_DEBOUNCE_MASK);
806e57725eaSMika Westerberg
807e57725eaSMika Westerberg if (debounce) {
808e57725eaSMika Westerberg unsigned long v;
809e57725eaSMika Westerberg
8106a33a1d6SAndy Shevchenko v = order_base_2(debounce * NSEC_PER_USEC / DEBOUNCE_PERIOD_NSEC);
811e57725eaSMika Westerberg if (v < 3 || v > 15) {
8128fff0427SAndy Shevchenko raw_spin_unlock_irqrestore(&pctrl->lock, flags);
8138fff0427SAndy Shevchenko return -EINVAL;
814bb2f43d4SAndy Shevchenko }
815bb2f43d4SAndy Shevchenko
816e57725eaSMika Westerberg /* Enable glitch filter and debouncer */
817e57725eaSMika Westerberg value0 |= PADCFG0_PREGFRXSEL;
818e57725eaSMika Westerberg value2 |= v << PADCFG2_DEBOUNCE_SHIFT;
819e57725eaSMika Westerberg value2 |= PADCFG2_DEBEN;
820e57725eaSMika Westerberg }
821e57725eaSMika Westerberg
822e57725eaSMika Westerberg writel(value0, padcfg0);
823e57725eaSMika Westerberg writel(value2, padcfg2);
824e57725eaSMika Westerberg
825e57725eaSMika Westerberg raw_spin_unlock_irqrestore(&pctrl->lock, flags);
826e57725eaSMika Westerberg
8278fff0427SAndy Shevchenko return 0;
828e57725eaSMika Westerberg }
829e57725eaSMika Westerberg
intel_config_set(struct pinctrl_dev * pctldev,unsigned int pin,unsigned long * configs,unsigned int nconfigs)83004035f7fSAndy Shevchenko static int intel_config_set(struct pinctrl_dev *pctldev, unsigned int pin,
83104035f7fSAndy Shevchenko unsigned long *configs, unsigned int nconfigs)
8327981c001SMika Westerberg {
8337981c001SMika Westerberg struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
8347981c001SMika Westerberg int i, ret;
8357981c001SMika Westerberg
8367981c001SMika Westerberg if (!intel_pad_usable(pctrl, pin))
8377981c001SMika Westerberg return -ENOTSUPP;
8387981c001SMika Westerberg
8397981c001SMika Westerberg for (i = 0; i < nconfigs; i++) {
8407981c001SMika Westerberg switch (pinconf_to_config_param(configs[i])) {
8417981c001SMika Westerberg case PIN_CONFIG_BIAS_DISABLE:
8427981c001SMika Westerberg case PIN_CONFIG_BIAS_PULL_UP:
8437981c001SMika Westerberg case PIN_CONFIG_BIAS_PULL_DOWN:
8447981c001SMika Westerberg ret = intel_config_set_pull(pctrl, pin, configs[i]);
8457981c001SMika Westerberg if (ret)
8467981c001SMika Westerberg return ret;
8477981c001SMika Westerberg break;
8487981c001SMika Westerberg
849e57725eaSMika Westerberg case PIN_CONFIG_INPUT_DEBOUNCE:
850e57725eaSMika Westerberg ret = intel_config_set_debounce(pctrl, pin,
851e57725eaSMika Westerberg pinconf_to_config_argument(configs[i]));
852e57725eaSMika Westerberg if (ret)
853e57725eaSMika Westerberg return ret;
854e57725eaSMika Westerberg break;
855e57725eaSMika Westerberg
8567981c001SMika Westerberg default:
8577981c001SMika Westerberg return -ENOTSUPP;
8587981c001SMika Westerberg }
8597981c001SMika Westerberg }
8607981c001SMika Westerberg
8617981c001SMika Westerberg return 0;
8627981c001SMika Westerberg }
8637981c001SMika Westerberg
8647981c001SMika Westerberg static const struct pinconf_ops intel_pinconf_ops = {
8657981c001SMika Westerberg .is_generic = true,
8667981c001SMika Westerberg .pin_config_get = intel_config_get,
8677981c001SMika Westerberg .pin_config_set = intel_config_set,
8687981c001SMika Westerberg };
8697981c001SMika Westerberg
8707981c001SMika Westerberg static const struct pinctrl_desc intel_pinctrl_desc = {
8717981c001SMika Westerberg .pctlops = &intel_pinctrl_ops,
8727981c001SMika Westerberg .pmxops = &intel_pinmux_ops,
8737981c001SMika Westerberg .confops = &intel_pinconf_ops,
8747981c001SMika Westerberg .owner = THIS_MODULE,
8757981c001SMika Westerberg };
8767981c001SMika Westerberg
877a60eac32SMika Westerberg /**
878a60eac32SMika Westerberg * intel_gpio_to_pin() - Translate from GPIO offset to pin number
879a60eac32SMika Westerberg * @pctrl: Pinctrl structure
880a60eac32SMika Westerberg * @offset: GPIO offset from gpiolib
881946ffefcSAndy Shevchenko * @community: Community is filled here if not %NULL
882a60eac32SMika Westerberg * @padgrp: Pad group is filled here if not %NULL
883a60eac32SMika Westerberg *
884a60eac32SMika Westerberg * When coming through gpiolib irqchip, the GPIO offset is not
885a60eac32SMika Westerberg * automatically translated to pinctrl pin number. This function can be
886a60eac32SMika Westerberg * used to find out the corresponding pinctrl pin.
8877b923e67SAndy Shevchenko *
8887b923e67SAndy Shevchenko * Return: a pin number and pointers to the community and pad group, which
8897b923e67SAndy Shevchenko * the pin belongs to, or negative error code if translation can't be done.
890a60eac32SMika Westerberg */
intel_gpio_to_pin(struct intel_pinctrl * pctrl,unsigned int offset,const struct intel_community ** community,const struct intel_padgroup ** padgrp)89104035f7fSAndy Shevchenko static int intel_gpio_to_pin(struct intel_pinctrl *pctrl, unsigned int offset,
892a60eac32SMika Westerberg const struct intel_community **community,
893a60eac32SMika Westerberg const struct intel_padgroup **padgrp)
894a60eac32SMika Westerberg {
895a60eac32SMika Westerberg int i;
896a60eac32SMika Westerberg
897a60eac32SMika Westerberg for (i = 0; i < pctrl->ncommunities; i++) {
898a60eac32SMika Westerberg const struct intel_community *comm = &pctrl->communities[i];
899a60eac32SMika Westerberg int j;
900a60eac32SMika Westerberg
901a60eac32SMika Westerberg for (j = 0; j < comm->ngpps; j++) {
902a60eac32SMika Westerberg const struct intel_padgroup *pgrp = &comm->gpps[j];
903a60eac32SMika Westerberg
904e5a4ab6aSAndy Shevchenko if (pgrp->gpio_base == INTEL_GPIO_BASE_NOMAP)
905a60eac32SMika Westerberg continue;
906a60eac32SMika Westerberg
907a60eac32SMika Westerberg if (offset >= pgrp->gpio_base &&
908a60eac32SMika Westerberg offset < pgrp->gpio_base + pgrp->size) {
909a60eac32SMika Westerberg int pin;
910a60eac32SMika Westerberg
911a60eac32SMika Westerberg pin = pgrp->base + offset - pgrp->gpio_base;
912a60eac32SMika Westerberg if (community)
913a60eac32SMika Westerberg *community = comm;
914a60eac32SMika Westerberg if (padgrp)
915a60eac32SMika Westerberg *padgrp = pgrp;
916a60eac32SMika Westerberg
917a60eac32SMika Westerberg return pin;
918a60eac32SMika Westerberg }
919a60eac32SMika Westerberg }
920a60eac32SMika Westerberg }
921a60eac32SMika Westerberg
922a60eac32SMika Westerberg return -EINVAL;
923a60eac32SMika Westerberg }
924a60eac32SMika Westerberg
9256cb0880fSChris Chiu /**
9266cb0880fSChris Chiu * intel_pin_to_gpio() - Translate from pin number to GPIO offset
9276cb0880fSChris Chiu * @pctrl: Pinctrl structure
9286cb0880fSChris Chiu * @pin: pin number
9296cb0880fSChris Chiu *
9306cb0880fSChris Chiu * Translate the pin number of pinctrl to GPIO offset
9317b923e67SAndy Shevchenko *
9327b923e67SAndy Shevchenko * Return: a GPIO offset, or negative error code if translation can't be done.
9336cb0880fSChris Chiu */
intel_pin_to_gpio(struct intel_pinctrl * pctrl,int pin)93455dac437SArnd Bergmann static __maybe_unused int intel_pin_to_gpio(struct intel_pinctrl *pctrl, int pin)
9356cb0880fSChris Chiu {
9366cb0880fSChris Chiu const struct intel_community *community;
9376cb0880fSChris Chiu const struct intel_padgroup *padgrp;
9386cb0880fSChris Chiu
9396cb0880fSChris Chiu community = intel_get_community(pctrl, pin);
9406cb0880fSChris Chiu if (!community)
9416cb0880fSChris Chiu return -EINVAL;
9426cb0880fSChris Chiu
9436cb0880fSChris Chiu padgrp = intel_community_get_padgroup(community, pin);
9446cb0880fSChris Chiu if (!padgrp)
9456cb0880fSChris Chiu return -EINVAL;
9466cb0880fSChris Chiu
9476cb0880fSChris Chiu return pin - padgrp->base + padgrp->gpio_base;
9486cb0880fSChris Chiu }
9496cb0880fSChris Chiu
intel_gpio_get(struct gpio_chip * chip,unsigned int offset)95004035f7fSAndy Shevchenko static int intel_gpio_get(struct gpio_chip *chip, unsigned int offset)
95155aedef5SAndy Shevchenko {
95296147db1SMika Westerberg struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
95396147db1SMika Westerberg void __iomem *reg;
95496147db1SMika Westerberg u32 padcfg0;
95555aedef5SAndy Shevchenko int pin;
95655aedef5SAndy Shevchenko
95796147db1SMika Westerberg pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL);
95896147db1SMika Westerberg if (pin < 0)
95996147db1SMika Westerberg return -EINVAL;
96096147db1SMika Westerberg
96196147db1SMika Westerberg reg = intel_get_padcfg(pctrl, pin, PADCFG0);
96296147db1SMika Westerberg if (!reg)
96396147db1SMika Westerberg return -EINVAL;
96496147db1SMika Westerberg
96596147db1SMika Westerberg padcfg0 = readl(reg);
96696147db1SMika Westerberg if (!(padcfg0 & PADCFG0_GPIOTXDIS))
96796147db1SMika Westerberg return !!(padcfg0 & PADCFG0_GPIOTXSTATE);
96896147db1SMika Westerberg
96996147db1SMika Westerberg return !!(padcfg0 & PADCFG0_GPIORXSTATE);
97055aedef5SAndy Shevchenko }
97155aedef5SAndy Shevchenko
intel_gpio_set(struct gpio_chip * chip,unsigned int offset,int value)97204035f7fSAndy Shevchenko static void intel_gpio_set(struct gpio_chip *chip, unsigned int offset,
97304035f7fSAndy Shevchenko int value)
97496147db1SMika Westerberg {
97596147db1SMika Westerberg struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
97696147db1SMika Westerberg unsigned long flags;
97796147db1SMika Westerberg void __iomem *reg;
97896147db1SMika Westerberg u32 padcfg0;
97996147db1SMika Westerberg int pin;
98096147db1SMika Westerberg
98196147db1SMika Westerberg pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL);
98296147db1SMika Westerberg if (pin < 0)
98396147db1SMika Westerberg return;
98496147db1SMika Westerberg
98596147db1SMika Westerberg reg = intel_get_padcfg(pctrl, pin, PADCFG0);
98696147db1SMika Westerberg if (!reg)
98796147db1SMika Westerberg return;
98896147db1SMika Westerberg
98996147db1SMika Westerberg raw_spin_lock_irqsave(&pctrl->lock, flags);
99096147db1SMika Westerberg padcfg0 = readl(reg);
99196147db1SMika Westerberg if (value)
99296147db1SMika Westerberg padcfg0 |= PADCFG0_GPIOTXSTATE;
99396147db1SMika Westerberg else
99496147db1SMika Westerberg padcfg0 &= ~PADCFG0_GPIOTXSTATE;
99596147db1SMika Westerberg writel(padcfg0, reg);
99696147db1SMika Westerberg raw_spin_unlock_irqrestore(&pctrl->lock, flags);
99796147db1SMika Westerberg }
99896147db1SMika Westerberg
intel_gpio_get_direction(struct gpio_chip * chip,unsigned int offset)99996147db1SMika Westerberg static int intel_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
100096147db1SMika Westerberg {
100196147db1SMika Westerberg struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
1002e64fbfa5SAndy Shevchenko unsigned long flags;
100396147db1SMika Westerberg void __iomem *reg;
100496147db1SMika Westerberg u32 padcfg0;
100596147db1SMika Westerberg int pin;
100696147db1SMika Westerberg
100796147db1SMika Westerberg pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL);
100896147db1SMika Westerberg if (pin < 0)
100996147db1SMika Westerberg return -EINVAL;
101096147db1SMika Westerberg
101196147db1SMika Westerberg reg = intel_get_padcfg(pctrl, pin, PADCFG0);
101296147db1SMika Westerberg if (!reg)
101396147db1SMika Westerberg return -EINVAL;
101496147db1SMika Westerberg
1015e64fbfa5SAndy Shevchenko raw_spin_lock_irqsave(&pctrl->lock, flags);
101696147db1SMika Westerberg padcfg0 = readl(reg);
1017e64fbfa5SAndy Shevchenko raw_spin_unlock_irqrestore(&pctrl->lock, flags);
101896147db1SMika Westerberg if (padcfg0 & PADCFG0_PMODE_MASK)
101996147db1SMika Westerberg return -EINVAL;
102096147db1SMika Westerberg
10216a304752SMatti Vaittinen if (padcfg0 & PADCFG0_GPIOTXDIS)
10226a304752SMatti Vaittinen return GPIO_LINE_DIRECTION_IN;
10236a304752SMatti Vaittinen
10246a304752SMatti Vaittinen return GPIO_LINE_DIRECTION_OUT;
102596147db1SMika Westerberg }
102696147db1SMika Westerberg
intel_gpio_direction_input(struct gpio_chip * chip,unsigned int offset)102704035f7fSAndy Shevchenko static int intel_gpio_direction_input(struct gpio_chip *chip, unsigned int offset)
102896147db1SMika Westerberg {
102996147db1SMika Westerberg return pinctrl_gpio_direction_input(chip->base + offset);
103096147db1SMika Westerberg }
103196147db1SMika Westerberg
intel_gpio_direction_output(struct gpio_chip * chip,unsigned int offset,int value)103204035f7fSAndy Shevchenko static int intel_gpio_direction_output(struct gpio_chip *chip, unsigned int offset,
103396147db1SMika Westerberg int value)
103496147db1SMika Westerberg {
103596147db1SMika Westerberg intel_gpio_set(chip, offset, value);
103696147db1SMika Westerberg return pinctrl_gpio_direction_output(chip->base + offset);
103796147db1SMika Westerberg }
103896147db1SMika Westerberg
103996147db1SMika Westerberg static const struct gpio_chip intel_gpio_chip = {
104096147db1SMika Westerberg .owner = THIS_MODULE,
104196147db1SMika Westerberg .request = gpiochip_generic_request,
104296147db1SMika Westerberg .free = gpiochip_generic_free,
104396147db1SMika Westerberg .get_direction = intel_gpio_get_direction,
104496147db1SMika Westerberg .direction_input = intel_gpio_direction_input,
104596147db1SMika Westerberg .direction_output = intel_gpio_direction_output,
104696147db1SMika Westerberg .get = intel_gpio_get,
104796147db1SMika Westerberg .set = intel_gpio_set,
104896147db1SMika Westerberg .set_config = gpiochip_generic_config,
104996147db1SMika Westerberg };
105096147db1SMika Westerberg
intel_gpio_irq_ack(struct irq_data * d)10517981c001SMika Westerberg static void intel_gpio_irq_ack(struct irq_data *d)
10527981c001SMika Westerberg {
10537981c001SMika Westerberg struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1054acfd4c63SLinus Walleij struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
10557981c001SMika Westerberg const struct intel_community *community;
1056919eb475SMika Westerberg const struct intel_padgroup *padgrp;
1057a60eac32SMika Westerberg int pin;
10587981c001SMika Westerberg
1059a60eac32SMika Westerberg pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), &community, &padgrp);
1060a60eac32SMika Westerberg if (pin >= 0) {
106104035f7fSAndy Shevchenko unsigned int gpp, gpp_offset, is_offset;
1062919eb475SMika Westerberg
1063919eb475SMika Westerberg gpp = padgrp->reg_num;
1064919eb475SMika Westerberg gpp_offset = padgroup_offset(padgrp, pin);
1065cf769bd8SMika Westerberg is_offset = community->is_offset + gpp * 4;
1066919eb475SMika Westerberg
1067919eb475SMika Westerberg raw_spin_lock(&pctrl->lock);
1068cf769bd8SMika Westerberg writel(BIT(gpp_offset), community->regs + is_offset);
106927d9098cSMika Westerberg raw_spin_unlock(&pctrl->lock);
10707981c001SMika Westerberg }
1071919eb475SMika Westerberg }
10727981c001SMika Westerberg
intel_gpio_irq_mask_unmask(struct gpio_chip * gc,irq_hw_number_t hwirq,bool mask)10736fb6f8bfSAndy Shevchenko static void intel_gpio_irq_mask_unmask(struct gpio_chip *gc, irq_hw_number_t hwirq, bool mask)
10747981c001SMika Westerberg {
1075acfd4c63SLinus Walleij struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
10767981c001SMika Westerberg const struct intel_community *community;
1077919eb475SMika Westerberg const struct intel_padgroup *padgrp;
1078a60eac32SMika Westerberg int pin;
1079a60eac32SMika Westerberg
10806fb6f8bfSAndy Shevchenko pin = intel_gpio_to_pin(pctrl, hwirq, &community, &padgrp);
1081a60eac32SMika Westerberg if (pin >= 0) {
108204035f7fSAndy Shevchenko unsigned int gpp, gpp_offset;
1083919eb475SMika Westerberg unsigned long flags;
1084670784fbSKai-Heng Feng void __iomem *reg, *is;
10857981c001SMika Westerberg u32 value;
10867981c001SMika Westerberg
1087919eb475SMika Westerberg gpp = padgrp->reg_num;
1088919eb475SMika Westerberg gpp_offset = padgroup_offset(padgrp, pin);
1089919eb475SMika Westerberg
10907981c001SMika Westerberg reg = community->regs + community->ie_offset + gpp * 4;
1091670784fbSKai-Heng Feng is = community->regs + community->is_offset + gpp * 4;
1092919eb475SMika Westerberg
1093919eb475SMika Westerberg raw_spin_lock_irqsave(&pctrl->lock, flags);
1094670784fbSKai-Heng Feng
1095670784fbSKai-Heng Feng /* Clear interrupt status first to avoid unexpected interrupt */
1096670784fbSKai-Heng Feng writel(BIT(gpp_offset), is);
1097670784fbSKai-Heng Feng
10987981c001SMika Westerberg value = readl(reg);
10997981c001SMika Westerberg if (mask)
11007981c001SMika Westerberg value &= ~BIT(gpp_offset);
11017981c001SMika Westerberg else
11027981c001SMika Westerberg value |= BIT(gpp_offset);
11037981c001SMika Westerberg writel(value, reg);
110427d9098cSMika Westerberg raw_spin_unlock_irqrestore(&pctrl->lock, flags);
11057981c001SMika Westerberg }
1106919eb475SMika Westerberg }
11077981c001SMika Westerberg
intel_gpio_irq_mask(struct irq_data * d)11087981c001SMika Westerberg static void intel_gpio_irq_mask(struct irq_data *d)
11097981c001SMika Westerberg {
11106fb6f8bfSAndy Shevchenko struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
11116fb6f8bfSAndy Shevchenko irq_hw_number_t hwirq = irqd_to_hwirq(d);
11126fb6f8bfSAndy Shevchenko
11136fb6f8bfSAndy Shevchenko intel_gpio_irq_mask_unmask(gc, hwirq, true);
11146fb6f8bfSAndy Shevchenko gpiochip_disable_irq(gc, hwirq);
11157981c001SMika Westerberg }
11167981c001SMika Westerberg
intel_gpio_irq_unmask(struct irq_data * d)11177981c001SMika Westerberg static void intel_gpio_irq_unmask(struct irq_data *d)
11187981c001SMika Westerberg {
11196fb6f8bfSAndy Shevchenko struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
11206fb6f8bfSAndy Shevchenko irq_hw_number_t hwirq = irqd_to_hwirq(d);
11216fb6f8bfSAndy Shevchenko
11226fb6f8bfSAndy Shevchenko gpiochip_enable_irq(gc, hwirq);
11236fb6f8bfSAndy Shevchenko intel_gpio_irq_mask_unmask(gc, hwirq, false);
11247981c001SMika Westerberg }
11257981c001SMika Westerberg
intel_gpio_irq_type(struct irq_data * d,unsigned int type)112604035f7fSAndy Shevchenko static int intel_gpio_irq_type(struct irq_data *d, unsigned int type)
11277981c001SMika Westerberg {
11287981c001SMika Westerberg struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1129acfd4c63SLinus Walleij struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
113004035f7fSAndy Shevchenko unsigned int pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), NULL, NULL);
1131d1bfdf86SRaag Jadav u32 rxevcfg, rxinv, value;
11327981c001SMika Westerberg unsigned long flags;
11337981c001SMika Westerberg void __iomem *reg;
11347981c001SMika Westerberg
11357981c001SMika Westerberg reg = intel_get_padcfg(pctrl, pin, PADCFG0);
11367981c001SMika Westerberg if (!reg)
11377981c001SMika Westerberg return -EINVAL;
11387981c001SMika Westerberg
11394341e8a5SMika Westerberg /*
11404341e8a5SMika Westerberg * If the pin is in ACPI mode it is still usable as a GPIO but it
11414341e8a5SMika Westerberg * cannot be used as IRQ because GPI_IS status bit will not be
11424341e8a5SMika Westerberg * updated by the host controller hardware.
11434341e8a5SMika Westerberg */
11444341e8a5SMika Westerberg if (intel_pad_acpi_mode(pctrl, pin)) {
11454341e8a5SMika Westerberg dev_warn(pctrl->dev, "pin %u cannot be used as IRQ\n", pin);
11464341e8a5SMika Westerberg return -EPERM;
11474341e8a5SMika Westerberg }
11484341e8a5SMika Westerberg
1149d1bfdf86SRaag Jadav if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
1150d1bfdf86SRaag Jadav rxevcfg = PADCFG0_RXEVCFG_EDGE_BOTH;
1151d1bfdf86SRaag Jadav } else if (type & IRQ_TYPE_EDGE_FALLING) {
1152d1bfdf86SRaag Jadav rxevcfg = PADCFG0_RXEVCFG_EDGE;
1153d1bfdf86SRaag Jadav } else if (type & IRQ_TYPE_EDGE_RISING) {
1154d1bfdf86SRaag Jadav rxevcfg = PADCFG0_RXEVCFG_EDGE;
1155d1bfdf86SRaag Jadav } else if (type & IRQ_TYPE_LEVEL_MASK) {
1156d1bfdf86SRaag Jadav rxevcfg = PADCFG0_RXEVCFG_LEVEL;
1157d1bfdf86SRaag Jadav } else {
1158d1bfdf86SRaag Jadav rxevcfg = PADCFG0_RXEVCFG_DISABLED;
1159d1bfdf86SRaag Jadav }
1160d1bfdf86SRaag Jadav
1161d1bfdf86SRaag Jadav if (type == IRQ_TYPE_EDGE_FALLING || type == IRQ_TYPE_LEVEL_LOW)
1162d1bfdf86SRaag Jadav rxinv = PADCFG0_RXINV;
1163d1bfdf86SRaag Jadav else
1164d1bfdf86SRaag Jadav rxinv = 0;
1165d1bfdf86SRaag Jadav
116627d9098cSMika Westerberg raw_spin_lock_irqsave(&pctrl->lock, flags);
11677981c001SMika Westerberg
1168f5a26acfSMika Westerberg intel_gpio_set_gpio_mode(reg);
1169f5a26acfSMika Westerberg
11707981c001SMika Westerberg value = readl(reg);
11717981c001SMika Westerberg
1172d1bfdf86SRaag Jadav value = (value & ~PADCFG0_RXEVCFG_MASK) | rxevcfg;
1173d1bfdf86SRaag Jadav value = (value & ~PADCFG0_RXINV) | rxinv;
11747981c001SMika Westerberg
11757981c001SMika Westerberg writel(value, reg);
11767981c001SMika Westerberg
11777981c001SMika Westerberg if (type & IRQ_TYPE_EDGE_BOTH)
1178fc756bcdSThomas Gleixner irq_set_handler_locked(d, handle_edge_irq);
11797981c001SMika Westerberg else if (type & IRQ_TYPE_LEVEL_MASK)
1180fc756bcdSThomas Gleixner irq_set_handler_locked(d, handle_level_irq);
11817981c001SMika Westerberg
118227d9098cSMika Westerberg raw_spin_unlock_irqrestore(&pctrl->lock, flags);
11837981c001SMika Westerberg
11847981c001SMika Westerberg return 0;
11857981c001SMika Westerberg }
11867981c001SMika Westerberg
intel_gpio_irq_wake(struct irq_data * d,unsigned int on)11877981c001SMika Westerberg static int intel_gpio_irq_wake(struct irq_data *d, unsigned int on)
11887981c001SMika Westerberg {
11897981c001SMika Westerberg struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1190acfd4c63SLinus Walleij struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
119104035f7fSAndy Shevchenko unsigned int pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), NULL, NULL);
11927981c001SMika Westerberg
11937981c001SMika Westerberg if (on)
119401dabe91SNilesh Bacchewar enable_irq_wake(pctrl->irq);
11957981c001SMika Westerberg else
119601dabe91SNilesh Bacchewar disable_irq_wake(pctrl->irq);
11979a520fd9SAndy Shevchenko
119898e63c11SAndy Shevchenko dev_dbg(pctrl->dev, "%s wake for pin %u\n", str_enable_disable(on), pin);
11997981c001SMika Westerberg return 0;
12007981c001SMika Westerberg }
12017981c001SMika Westerberg
12026fb6f8bfSAndy Shevchenko static const struct irq_chip intel_gpio_irq_chip = {
12036fb6f8bfSAndy Shevchenko .name = "intel-gpio",
12046fb6f8bfSAndy Shevchenko .irq_ack = intel_gpio_irq_ack,
12056fb6f8bfSAndy Shevchenko .irq_mask = intel_gpio_irq_mask,
12066fb6f8bfSAndy Shevchenko .irq_unmask = intel_gpio_irq_unmask,
12076fb6f8bfSAndy Shevchenko .irq_set_type = intel_gpio_irq_type,
12086fb6f8bfSAndy Shevchenko .irq_set_wake = intel_gpio_irq_wake,
12096fb6f8bfSAndy Shevchenko .flags = IRQCHIP_MASK_ON_SUSPEND | IRQCHIP_IMMUTABLE,
12106fb6f8bfSAndy Shevchenko GPIOCHIP_IRQ_RESOURCE_HELPERS,
12116fb6f8bfSAndy Shevchenko };
12126fb6f8bfSAndy Shevchenko
intel_gpio_community_irq_handler(struct intel_pinctrl * pctrl,const struct intel_community * community)121386851bbcSAndy Shevchenko static int intel_gpio_community_irq_handler(struct intel_pinctrl *pctrl,
12147981c001SMika Westerberg const struct intel_community *community)
12157981c001SMika Westerberg {
1216193b40c8SMika Westerberg struct gpio_chip *gc = &pctrl->chip;
121786851bbcSAndy Shevchenko unsigned int gpp;
121886851bbcSAndy Shevchenko int ret = 0;
12197981c001SMika Westerberg
12207981c001SMika Westerberg for (gpp = 0; gpp < community->ngpps; gpp++) {
1221919eb475SMika Westerberg const struct intel_padgroup *padgrp = &community->gpps[gpp];
12227981c001SMika Westerberg unsigned long pending, enabled, gpp_offset;
1223e64fbfa5SAndy Shevchenko
12245b613df3SAndy Shevchenko raw_spin_lock(&pctrl->lock);
12257981c001SMika Westerberg
1226cf769bd8SMika Westerberg pending = readl(community->regs + community->is_offset +
1227cf769bd8SMika Westerberg padgrp->reg_num * 4);
12287981c001SMika Westerberg enabled = readl(community->regs + community->ie_offset +
1229919eb475SMika Westerberg padgrp->reg_num * 4);
12307981c001SMika Westerberg
12315b613df3SAndy Shevchenko raw_spin_unlock(&pctrl->lock);
1232e64fbfa5SAndy Shevchenko
12337981c001SMika Westerberg /* Only interrupts that are enabled */
12347981c001SMika Westerberg pending &= enabled;
12357981c001SMika Westerberg
12364019bd6dSAndy Shevchenko for_each_set_bit(gpp_offset, &pending, padgrp->size)
12374019bd6dSAndy Shevchenko generic_handle_domain_irq(gc->irq.domain, padgrp->gpio_base + gpp_offset);
123886851bbcSAndy Shevchenko
123986851bbcSAndy Shevchenko ret += pending ? 1 : 0;
12407981c001SMika Westerberg }
12417981c001SMika Westerberg
1242193b40c8SMika Westerberg return ret;
1243193b40c8SMika Westerberg }
1244193b40c8SMika Westerberg
intel_gpio_irq(int irq,void * data)1245193b40c8SMika Westerberg static irqreturn_t intel_gpio_irq(int irq, void *data)
12467981c001SMika Westerberg {
1247193b40c8SMika Westerberg const struct intel_community *community;
1248193b40c8SMika Westerberg struct intel_pinctrl *pctrl = data;
124986851bbcSAndy Shevchenko unsigned int i;
125086851bbcSAndy Shevchenko int ret = 0;
12517981c001SMika Westerberg
12527981c001SMika Westerberg /* Need to check all communities for pending interrupts */
1253193b40c8SMika Westerberg for (i = 0; i < pctrl->ncommunities; i++) {
1254193b40c8SMika Westerberg community = &pctrl->communities[i];
125586851bbcSAndy Shevchenko ret += intel_gpio_community_irq_handler(pctrl, community);
1256193b40c8SMika Westerberg }
12577981c001SMika Westerberg
125886851bbcSAndy Shevchenko return IRQ_RETVAL(ret);
12597981c001SMika Westerberg }
12607981c001SMika Westerberg
intel_gpio_irq_init(struct intel_pinctrl * pctrl)1261e986f0e6SŁukasz Bartosik static void intel_gpio_irq_init(struct intel_pinctrl *pctrl)
1262e986f0e6SŁukasz Bartosik {
1263e986f0e6SŁukasz Bartosik int i;
1264e986f0e6SŁukasz Bartosik
1265e986f0e6SŁukasz Bartosik for (i = 0; i < pctrl->ncommunities; i++) {
1266e986f0e6SŁukasz Bartosik const struct intel_community *community;
1267e986f0e6SŁukasz Bartosik void __iomem *base;
1268e986f0e6SŁukasz Bartosik unsigned int gpp;
1269e986f0e6SŁukasz Bartosik
1270e986f0e6SŁukasz Bartosik community = &pctrl->communities[i];
1271e986f0e6SŁukasz Bartosik base = community->regs;
1272e986f0e6SŁukasz Bartosik
1273e986f0e6SŁukasz Bartosik for (gpp = 0; gpp < community->ngpps; gpp++) {
1274e986f0e6SŁukasz Bartosik /* Mask and clear all interrupts */
1275e986f0e6SŁukasz Bartosik writel(0, base + community->ie_offset + gpp * 4);
1276e986f0e6SŁukasz Bartosik writel(0xffff, base + community->is_offset + gpp * 4);
1277e986f0e6SŁukasz Bartosik }
1278e986f0e6SŁukasz Bartosik }
1279e986f0e6SŁukasz Bartosik }
1280e986f0e6SŁukasz Bartosik
intel_gpio_irq_init_hw(struct gpio_chip * gc)1281e986f0e6SŁukasz Bartosik static int intel_gpio_irq_init_hw(struct gpio_chip *gc)
1282e986f0e6SŁukasz Bartosik {
1283e986f0e6SŁukasz Bartosik struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
1284e986f0e6SŁukasz Bartosik
1285e986f0e6SŁukasz Bartosik /*
1286e986f0e6SŁukasz Bartosik * Make sure the interrupt lines are in a proper state before
1287e986f0e6SŁukasz Bartosik * further configuration.
1288e986f0e6SŁukasz Bartosik */
1289e986f0e6SŁukasz Bartosik intel_gpio_irq_init(pctrl);
1290e986f0e6SŁukasz Bartosik
1291e986f0e6SŁukasz Bartosik return 0;
1292e986f0e6SŁukasz Bartosik }
1293e986f0e6SŁukasz Bartosik
intel_gpio_add_community_ranges(struct intel_pinctrl * pctrl,const struct intel_community * community)12946d416b9bSLinus Walleij static int intel_gpio_add_community_ranges(struct intel_pinctrl *pctrl,
1295a60eac32SMika Westerberg const struct intel_community *community)
1296a60eac32SMika Westerberg {
129733b6cb58SColin Ian King int ret = 0, i;
1298a60eac32SMika Westerberg
1299a60eac32SMika Westerberg for (i = 0; i < community->ngpps; i++) {
1300a60eac32SMika Westerberg const struct intel_padgroup *gpp = &community->gpps[i];
1301a60eac32SMika Westerberg
1302e5a4ab6aSAndy Shevchenko if (gpp->gpio_base == INTEL_GPIO_BASE_NOMAP)
1303a60eac32SMika Westerberg continue;
1304a60eac32SMika Westerberg
1305a60eac32SMika Westerberg ret = gpiochip_add_pin_range(&pctrl->chip, dev_name(pctrl->dev),
1306a60eac32SMika Westerberg gpp->gpio_base, gpp->base,
1307a60eac32SMika Westerberg gpp->size);
1308a60eac32SMika Westerberg if (ret)
1309a60eac32SMika Westerberg return ret;
1310a60eac32SMika Westerberg }
1311a60eac32SMika Westerberg
1312a60eac32SMika Westerberg return ret;
1313a60eac32SMika Westerberg }
1314a60eac32SMika Westerberg
intel_gpio_add_pin_ranges(struct gpio_chip * gc)13156d416b9bSLinus Walleij static int intel_gpio_add_pin_ranges(struct gpio_chip *gc)
13166d416b9bSLinus Walleij {
13176d416b9bSLinus Walleij struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
13186d416b9bSLinus Walleij int ret, i;
13196d416b9bSLinus Walleij
13206d416b9bSLinus Walleij for (i = 0; i < pctrl->ncommunities; i++) {
13216d416b9bSLinus Walleij struct intel_community *community = &pctrl->communities[i];
13226d416b9bSLinus Walleij
13236d416b9bSLinus Walleij ret = intel_gpio_add_community_ranges(pctrl, community);
13246d416b9bSLinus Walleij if (ret) {
13256d416b9bSLinus Walleij dev_err(pctrl->dev, "failed to add GPIO pin range\n");
13266d416b9bSLinus Walleij return ret;
13276d416b9bSLinus Walleij }
13286d416b9bSLinus Walleij }
13296d416b9bSLinus Walleij
13306d416b9bSLinus Walleij return 0;
13316d416b9bSLinus Walleij }
13326d416b9bSLinus Walleij
intel_gpio_ngpio(const struct intel_pinctrl * pctrl)133311b389ccSAndy Shevchenko static unsigned int intel_gpio_ngpio(const struct intel_pinctrl *pctrl)
1334a60eac32SMika Westerberg {
1335a60eac32SMika Westerberg const struct intel_community *community;
133604035f7fSAndy Shevchenko unsigned int ngpio = 0;
1337a60eac32SMika Westerberg int i, j;
1338a60eac32SMika Westerberg
1339a60eac32SMika Westerberg for (i = 0; i < pctrl->ncommunities; i++) {
1340a60eac32SMika Westerberg community = &pctrl->communities[i];
1341a60eac32SMika Westerberg for (j = 0; j < community->ngpps; j++) {
1342a60eac32SMika Westerberg const struct intel_padgroup *gpp = &community->gpps[j];
1343a60eac32SMika Westerberg
1344e5a4ab6aSAndy Shevchenko if (gpp->gpio_base == INTEL_GPIO_BASE_NOMAP)
1345a60eac32SMika Westerberg continue;
1346a60eac32SMika Westerberg
1347a60eac32SMika Westerberg if (gpp->gpio_base + gpp->size > ngpio)
1348a60eac32SMika Westerberg ngpio = gpp->gpio_base + gpp->size;
1349a60eac32SMika Westerberg }
1350a60eac32SMika Westerberg }
1351a60eac32SMika Westerberg
1352a60eac32SMika Westerberg return ngpio;
1353a60eac32SMika Westerberg }
1354a60eac32SMika Westerberg
intel_gpio_probe(struct intel_pinctrl * pctrl,int irq)13557981c001SMika Westerberg static int intel_gpio_probe(struct intel_pinctrl *pctrl, int irq)
13567981c001SMika Westerberg {
13576d416b9bSLinus Walleij int ret;
1358af0c5330SLinus Walleij struct gpio_irq_chip *girq;
13597981c001SMika Westerberg
13607981c001SMika Westerberg pctrl->chip = intel_gpio_chip;
13617981c001SMika Westerberg
136257ff2df1SAndy Shevchenko /* Setup GPIO chip */
1363a60eac32SMika Westerberg pctrl->chip.ngpio = intel_gpio_ngpio(pctrl);
13647981c001SMika Westerberg pctrl->chip.label = dev_name(pctrl->dev);
136558383c78SLinus Walleij pctrl->chip.parent = pctrl->dev;
13667981c001SMika Westerberg pctrl->chip.base = -1;
13676d416b9bSLinus Walleij pctrl->chip.add_pin_ranges = intel_gpio_add_pin_ranges;
136801dabe91SNilesh Bacchewar pctrl->irq = irq;
13697981c001SMika Westerberg
1370193b40c8SMika Westerberg /*
1371af0c5330SLinus Walleij * On some platforms several GPIO controllers share the same interrupt
1372af0c5330SLinus Walleij * line.
1373193b40c8SMika Westerberg */
13741a7d1cb8SMika Westerberg ret = devm_request_irq(pctrl->dev, irq, intel_gpio_irq,
13751a7d1cb8SMika Westerberg IRQF_SHARED | IRQF_NO_THREAD,
1376193b40c8SMika Westerberg dev_name(pctrl->dev), pctrl);
1377193b40c8SMika Westerberg if (ret) {
1378193b40c8SMika Westerberg dev_err(pctrl->dev, "failed to request interrupt\n");
1379f25c3aa9SMika Westerberg return ret;
13807981c001SMika Westerberg }
13817981c001SMika Westerberg
13826fb6f8bfSAndy Shevchenko /* Setup IRQ chip */
1383af0c5330SLinus Walleij girq = &pctrl->chip.irq;
13846fb6f8bfSAndy Shevchenko gpio_irq_chip_set_chip(girq, &intel_gpio_irq_chip);
1385af0c5330SLinus Walleij /* This will let us handle the IRQ in the driver */
1386af0c5330SLinus Walleij girq->parent_handler = NULL;
1387af0c5330SLinus Walleij girq->num_parents = 0;
1388af0c5330SLinus Walleij girq->default_type = IRQ_TYPE_NONE;
1389af0c5330SLinus Walleij girq->handler = handle_bad_irq;
1390e986f0e6SŁukasz Bartosik girq->init_hw = intel_gpio_irq_init_hw;
1391af0c5330SLinus Walleij
1392af0c5330SLinus Walleij ret = devm_gpiochip_add_data(pctrl->dev, &pctrl->chip, pctrl);
13937981c001SMika Westerberg if (ret) {
1394af0c5330SLinus Walleij dev_err(pctrl->dev, "failed to register gpiochip\n");
1395f25c3aa9SMika Westerberg return ret;
13967981c001SMika Westerberg }
13977981c001SMika Westerberg
13987981c001SMika Westerberg return 0;
13997981c001SMika Westerberg }
14007981c001SMika Westerberg
intel_pinctrl_add_padgroups_by_gpps(struct intel_pinctrl * pctrl,struct intel_community * community)1401036e126cSAndy Shevchenko static int intel_pinctrl_add_padgroups_by_gpps(struct intel_pinctrl *pctrl,
1402919eb475SMika Westerberg struct intel_community *community)
1403919eb475SMika Westerberg {
1404919eb475SMika Westerberg struct intel_padgroup *gpps;
140504035f7fSAndy Shevchenko unsigned int padown_num = 0;
1406036e126cSAndy Shevchenko size_t i, ngpps = community->ngpps;
1407919eb475SMika Westerberg
1408919eb475SMika Westerberg gpps = devm_kcalloc(pctrl->dev, ngpps, sizeof(*gpps), GFP_KERNEL);
1409919eb475SMika Westerberg if (!gpps)
1410919eb475SMika Westerberg return -ENOMEM;
1411919eb475SMika Westerberg
1412919eb475SMika Westerberg for (i = 0; i < ngpps; i++) {
1413919eb475SMika Westerberg gpps[i] = community->gpps[i];
1414919eb475SMika Westerberg
1415ed153b07SAndy Shevchenko if (gpps[i].size > INTEL_PINCTRL_MAX_GPP_SIZE)
1416919eb475SMika Westerberg return -EINVAL;
1417919eb475SMika Westerberg
1418e5a4ab6aSAndy Shevchenko /* Special treatment for GPIO base */
1419e5a4ab6aSAndy Shevchenko switch (gpps[i].gpio_base) {
1420e5a4ab6aSAndy Shevchenko case INTEL_GPIO_BASE_MATCH:
1421a60eac32SMika Westerberg gpps[i].gpio_base = gpps[i].base;
1422e5a4ab6aSAndy Shevchenko break;
14239bd59157SAndy Shevchenko case INTEL_GPIO_BASE_ZERO:
14249bd59157SAndy Shevchenko gpps[i].gpio_base = 0;
14259bd59157SAndy Shevchenko break;
1426e5a4ab6aSAndy Shevchenko case INTEL_GPIO_BASE_NOMAP:
142777e14126SAndy Shevchenko break;
1428e5a4ab6aSAndy Shevchenko default:
1429e5a4ab6aSAndy Shevchenko break;
1430e5a4ab6aSAndy Shevchenko }
1431a60eac32SMika Westerberg
1432919eb475SMika Westerberg gpps[i].padown_num = padown_num;
1433ed153b07SAndy Shevchenko padown_num += DIV_ROUND_UP(gpps[i].size * 4, INTEL_PINCTRL_MAX_GPP_SIZE);
1434036e126cSAndy Shevchenko }
1435036e126cSAndy Shevchenko
1436036e126cSAndy Shevchenko community->gpps = gpps;
1437036e126cSAndy Shevchenko
1438036e126cSAndy Shevchenko return 0;
1439036e126cSAndy Shevchenko }
1440036e126cSAndy Shevchenko
intel_pinctrl_add_padgroups_by_size(struct intel_pinctrl * pctrl,struct intel_community * community)1441036e126cSAndy Shevchenko static int intel_pinctrl_add_padgroups_by_size(struct intel_pinctrl *pctrl,
1442036e126cSAndy Shevchenko struct intel_community *community)
1443036e126cSAndy Shevchenko {
1444036e126cSAndy Shevchenko struct intel_padgroup *gpps;
1445036e126cSAndy Shevchenko unsigned int npins = community->npins;
1446036e126cSAndy Shevchenko unsigned int padown_num = 0;
1447036e126cSAndy Shevchenko size_t i, ngpps = DIV_ROUND_UP(npins, community->gpp_size);
1448036e126cSAndy Shevchenko
1449ed153b07SAndy Shevchenko if (community->gpp_size > INTEL_PINCTRL_MAX_GPP_SIZE)
1450036e126cSAndy Shevchenko return -EINVAL;
1451036e126cSAndy Shevchenko
1452036e126cSAndy Shevchenko gpps = devm_kcalloc(pctrl->dev, ngpps, sizeof(*gpps), GFP_KERNEL);
1453036e126cSAndy Shevchenko if (!gpps)
1454036e126cSAndy Shevchenko return -ENOMEM;
1455036e126cSAndy Shevchenko
1456036e126cSAndy Shevchenko for (i = 0; i < ngpps; i++) {
1457036e126cSAndy Shevchenko unsigned int gpp_size = community->gpp_size;
1458036e126cSAndy Shevchenko
1459036e126cSAndy Shevchenko gpps[i].reg_num = i;
1460036e126cSAndy Shevchenko gpps[i].base = community->pin_base + i * gpp_size;
1461036e126cSAndy Shevchenko gpps[i].size = min(gpp_size, npins);
1462036e126cSAndy Shevchenko npins -= gpps[i].size;
1463036e126cSAndy Shevchenko
146477e14126SAndy Shevchenko gpps[i].gpio_base = gpps[i].base;
1465036e126cSAndy Shevchenko gpps[i].padown_num = padown_num;
1466919eb475SMika Westerberg
1467919eb475SMika Westerberg padown_num += community->gpp_num_padown_regs;
1468919eb475SMika Westerberg }
1469919eb475SMika Westerberg
1470919eb475SMika Westerberg community->ngpps = ngpps;
1471919eb475SMika Westerberg community->gpps = gpps;
1472919eb475SMika Westerberg
1473919eb475SMika Westerberg return 0;
1474919eb475SMika Westerberg }
1475919eb475SMika Westerberg
intel_pinctrl_pm_init(struct intel_pinctrl * pctrl)14767981c001SMika Westerberg static int intel_pinctrl_pm_init(struct intel_pinctrl *pctrl)
14777981c001SMika Westerberg {
14787981c001SMika Westerberg #ifdef CONFIG_PM_SLEEP
14797981c001SMika Westerberg const struct intel_pinctrl_soc_data *soc = pctrl->soc;
14807981c001SMika Westerberg struct intel_community_context *communities;
14817981c001SMika Westerberg struct intel_pad_context *pads;
14827981c001SMika Westerberg int i;
14837981c001SMika Westerberg
14847981c001SMika Westerberg pads = devm_kcalloc(pctrl->dev, soc->npins, sizeof(*pads), GFP_KERNEL);
14857981c001SMika Westerberg if (!pads)
14867981c001SMika Westerberg return -ENOMEM;
14877981c001SMika Westerberg
14887981c001SMika Westerberg communities = devm_kcalloc(pctrl->dev, pctrl->ncommunities,
14897981c001SMika Westerberg sizeof(*communities), GFP_KERNEL);
14907981c001SMika Westerberg if (!communities)
14917981c001SMika Westerberg return -ENOMEM;
14927981c001SMika Westerberg
14937981c001SMika Westerberg
14947981c001SMika Westerberg for (i = 0; i < pctrl->ncommunities; i++) {
14957981c001SMika Westerberg struct intel_community *community = &pctrl->communities[i];
1496a0a5f766SChris Chiu u32 *intmask, *hostown;
14977981c001SMika Westerberg
14987981c001SMika Westerberg intmask = devm_kcalloc(pctrl->dev, community->ngpps,
14997981c001SMika Westerberg sizeof(*intmask), GFP_KERNEL);
15007981c001SMika Westerberg if (!intmask)
15017981c001SMika Westerberg return -ENOMEM;
15027981c001SMika Westerberg
15037981c001SMika Westerberg communities[i].intmask = intmask;
1504a0a5f766SChris Chiu
1505a0a5f766SChris Chiu hostown = devm_kcalloc(pctrl->dev, community->ngpps,
1506a0a5f766SChris Chiu sizeof(*hostown), GFP_KERNEL);
1507a0a5f766SChris Chiu if (!hostown)
1508a0a5f766SChris Chiu return -ENOMEM;
1509a0a5f766SChris Chiu
1510a0a5f766SChris Chiu communities[i].hostown = hostown;
15117981c001SMika Westerberg }
15127981c001SMika Westerberg
15137981c001SMika Westerberg pctrl->context.pads = pads;
15147981c001SMika Westerberg pctrl->context.communities = communities;
15157981c001SMika Westerberg #endif
15167981c001SMika Westerberg
15177981c001SMika Westerberg return 0;
15187981c001SMika Westerberg }
15197981c001SMika Westerberg
intel_pinctrl_probe_pwm(struct intel_pinctrl * pctrl,struct intel_community * community)1520eb78d360SAndy Shevchenko static int intel_pinctrl_probe_pwm(struct intel_pinctrl *pctrl,
1521eb78d360SAndy Shevchenko struct intel_community *community)
1522eb78d360SAndy Shevchenko {
1523eb78d360SAndy Shevchenko static const struct pwm_lpss_boardinfo info = {
1524eb78d360SAndy Shevchenko .clk_rate = 19200000,
1525eb78d360SAndy Shevchenko .npwm = 1,
1526eb78d360SAndy Shevchenko .base_unit_bits = 22,
1527eb78d360SAndy Shevchenko .bypass = true,
1528eb78d360SAndy Shevchenko };
1529eb78d360SAndy Shevchenko struct pwm_lpss_chip *pwm;
1530eb78d360SAndy Shevchenko
1531eb78d360SAndy Shevchenko if (!(community->features & PINCTRL_FEATURE_PWM))
1532eb78d360SAndy Shevchenko return 0;
1533eb78d360SAndy Shevchenko
1534eb78d360SAndy Shevchenko if (!IS_REACHABLE(CONFIG_PWM_LPSS))
1535eb78d360SAndy Shevchenko return 0;
1536eb78d360SAndy Shevchenko
1537eb78d360SAndy Shevchenko pwm = devm_pwm_lpss_probe(pctrl->dev, community->regs + PWMC, &info);
1538eb78d360SAndy Shevchenko return PTR_ERR_OR_ZERO(pwm);
1539eb78d360SAndy Shevchenko }
1540eb78d360SAndy Shevchenko
intel_pinctrl_probe(struct platform_device * pdev,const struct intel_pinctrl_soc_data * soc_data)15410dd519e3SAndy Shevchenko static int intel_pinctrl_probe(struct platform_device *pdev,
15427981c001SMika Westerberg const struct intel_pinctrl_soc_data *soc_data)
15437981c001SMika Westerberg {
154412b44105SAndy Shevchenko struct device *dev = &pdev->dev;
15457981c001SMika Westerberg struct intel_pinctrl *pctrl;
15467981c001SMika Westerberg int i, ret, irq;
15477981c001SMika Westerberg
154812b44105SAndy Shevchenko pctrl = devm_kzalloc(dev, sizeof(*pctrl), GFP_KERNEL);
15497981c001SMika Westerberg if (!pctrl)
15507981c001SMika Westerberg return -ENOMEM;
15517981c001SMika Westerberg
155212b44105SAndy Shevchenko pctrl->dev = dev;
15537981c001SMika Westerberg pctrl->soc = soc_data;
155427d9098cSMika Westerberg raw_spin_lock_init(&pctrl->lock);
15557981c001SMika Westerberg
15567981c001SMika Westerberg /*
15577981c001SMika Westerberg * Make a copy of the communities which we can use to hold pointers
15587981c001SMika Westerberg * to the registers.
15597981c001SMika Westerberg */
15607981c001SMika Westerberg pctrl->ncommunities = pctrl->soc->ncommunities;
156112b44105SAndy Shevchenko pctrl->communities = devm_kcalloc(dev, pctrl->ncommunities,
15627981c001SMika Westerberg sizeof(*pctrl->communities), GFP_KERNEL);
15637981c001SMika Westerberg if (!pctrl->communities)
15647981c001SMika Westerberg return -ENOMEM;
15657981c001SMika Westerberg
15667981c001SMika Westerberg for (i = 0; i < pctrl->ncommunities; i++) {
15677981c001SMika Westerberg struct intel_community *community = &pctrl->communities[i];
15687981c001SMika Westerberg void __iomem *regs;
156991d898e5SAndy Shevchenko u32 offset;
1570998c49e8SAndy Shevchenko u32 value;
15717981c001SMika Westerberg
15727981c001SMika Westerberg *community = pctrl->soc->communities[i];
15737981c001SMika Westerberg
15749d5b6a95SAndy Shevchenko regs = devm_platform_ioremap_resource(pdev, community->barno);
15757981c001SMika Westerberg if (IS_ERR(regs))
15767981c001SMika Westerberg return PTR_ERR(regs);
15777981c001SMika Westerberg
157839c1f1bdSRoger Pau Monne /*
157939c1f1bdSRoger Pau Monne * Determine community features based on the revision.
158039c1f1bdSRoger Pau Monne * A value of all ones means the device is not present.
158139c1f1bdSRoger Pau Monne */
1582998c49e8SAndy Shevchenko value = readl(regs + REVID);
158339c1f1bdSRoger Pau Monne if (value == ~0u)
158439c1f1bdSRoger Pau Monne return -ENODEV;
1585998c49e8SAndy Shevchenko if (((value & REVID_MASK) >> REVID_SHIFT) >= 0x94) {
1586e57725eaSMika Westerberg community->features |= PINCTRL_FEATURE_DEBOUNCE;
158704cc058fSMika Westerberg community->features |= PINCTRL_FEATURE_1K_PD;
158804cc058fSMika Westerberg }
1589e57725eaSMika Westerberg
159091d898e5SAndy Shevchenko /* Determine community features based on the capabilities */
159191d898e5SAndy Shevchenko offset = CAPLIST;
159291d898e5SAndy Shevchenko do {
159391d898e5SAndy Shevchenko value = readl(regs + offset);
159491d898e5SAndy Shevchenko switch ((value & CAPLIST_ID_MASK) >> CAPLIST_ID_SHIFT) {
159591d898e5SAndy Shevchenko case CAPLIST_ID_GPIO_HW_INFO:
159691d898e5SAndy Shevchenko community->features |= PINCTRL_FEATURE_GPIO_HW_INFO;
159791d898e5SAndy Shevchenko break;
159891d898e5SAndy Shevchenko case CAPLIST_ID_PWM:
159991d898e5SAndy Shevchenko community->features |= PINCTRL_FEATURE_PWM;
160091d898e5SAndy Shevchenko break;
160191d898e5SAndy Shevchenko case CAPLIST_ID_BLINK:
160291d898e5SAndy Shevchenko community->features |= PINCTRL_FEATURE_BLINK;
160391d898e5SAndy Shevchenko break;
160491d898e5SAndy Shevchenko case CAPLIST_ID_EXP:
160591d898e5SAndy Shevchenko community->features |= PINCTRL_FEATURE_EXP;
160691d898e5SAndy Shevchenko break;
160791d898e5SAndy Shevchenko default:
160891d898e5SAndy Shevchenko break;
160991d898e5SAndy Shevchenko }
161091d898e5SAndy Shevchenko offset = (value & CAPLIST_NEXT_MASK) >> CAPLIST_NEXT_SHIFT;
161191d898e5SAndy Shevchenko } while (offset);
161291d898e5SAndy Shevchenko
161312b44105SAndy Shevchenko dev_dbg(dev, "Community%d features: %#08x\n", i, community->features);
161491d898e5SAndy Shevchenko
16157981c001SMika Westerberg /* Read offset of the pad configuration registers */
161691d898e5SAndy Shevchenko offset = readl(regs + PADBAR);
16177981c001SMika Westerberg
16187981c001SMika Westerberg community->regs = regs;
161991d898e5SAndy Shevchenko community->pad_regs = regs + offset;
1620919eb475SMika Westerberg
1621036e126cSAndy Shevchenko if (community->gpps)
1622036e126cSAndy Shevchenko ret = intel_pinctrl_add_padgroups_by_gpps(pctrl, community);
1623036e126cSAndy Shevchenko else
1624036e126cSAndy Shevchenko ret = intel_pinctrl_add_padgroups_by_size(pctrl, community);
1625919eb475SMika Westerberg if (ret)
1626919eb475SMika Westerberg return ret;
1627eb78d360SAndy Shevchenko
1628eb78d360SAndy Shevchenko ret = intel_pinctrl_probe_pwm(pctrl, community);
1629eb78d360SAndy Shevchenko if (ret)
1630eb78d360SAndy Shevchenko return ret;
16317981c001SMika Westerberg }
16327981c001SMika Westerberg
16337981c001SMika Westerberg irq = platform_get_irq(pdev, 0);
16344e73d02fSStephen Boyd if (irq < 0)
16357981c001SMika Westerberg return irq;
16367981c001SMika Westerberg
16377981c001SMika Westerberg ret = intel_pinctrl_pm_init(pctrl);
16387981c001SMika Westerberg if (ret)
16397981c001SMika Westerberg return ret;
16407981c001SMika Westerberg
16417981c001SMika Westerberg pctrl->pctldesc = intel_pinctrl_desc;
164212b44105SAndy Shevchenko pctrl->pctldesc.name = dev_name(dev);
16437981c001SMika Westerberg pctrl->pctldesc.pins = pctrl->soc->pins;
16447981c001SMika Westerberg pctrl->pctldesc.npins = pctrl->soc->npins;
16457981c001SMika Westerberg
164612b44105SAndy Shevchenko pctrl->pctldev = devm_pinctrl_register(dev, &pctrl->pctldesc, pctrl);
1647323de9efSMasahiro Yamada if (IS_ERR(pctrl->pctldev)) {
164812b44105SAndy Shevchenko dev_err(dev, "failed to register pinctrl driver\n");
1649323de9efSMasahiro Yamada return PTR_ERR(pctrl->pctldev);
16507981c001SMika Westerberg }
16517981c001SMika Westerberg
16527981c001SMika Westerberg ret = intel_gpio_probe(pctrl, irq);
165354d46cd7SLaxman Dewangan if (ret)
16547981c001SMika Westerberg return ret;
16557981c001SMika Westerberg
16567981c001SMika Westerberg platform_set_drvdata(pdev, pctrl);
16577981c001SMika Westerberg
16587981c001SMika Westerberg return 0;
16597981c001SMika Westerberg }
16607981c001SMika Westerberg
intel_pinctrl_probe_by_hid(struct platform_device * pdev)166170c263c4SAndy Shevchenko int intel_pinctrl_probe_by_hid(struct platform_device *pdev)
166270c263c4SAndy Shevchenko {
166370c263c4SAndy Shevchenko const struct intel_pinctrl_soc_data *data;
166470c263c4SAndy Shevchenko
166570c263c4SAndy Shevchenko data = device_get_match_data(&pdev->dev);
1666ff360d62SAndy Shevchenko if (!data)
1667ff360d62SAndy Shevchenko return -ENODATA;
1668ff360d62SAndy Shevchenko
166970c263c4SAndy Shevchenko return intel_pinctrl_probe(pdev, data);
167070c263c4SAndy Shevchenko }
1671*34393c36SAndy Shevchenko EXPORT_SYMBOL_NS_GPL(intel_pinctrl_probe_by_hid, PINCTRL_INTEL);
167270c263c4SAndy Shevchenko
intel_pinctrl_probe_by_uid(struct platform_device * pdev)1673924cf800SAndy Shevchenko int intel_pinctrl_probe_by_uid(struct platform_device *pdev)
1674924cf800SAndy Shevchenko {
1675ff360d62SAndy Shevchenko const struct intel_pinctrl_soc_data *data;
1676ff360d62SAndy Shevchenko
1677ff360d62SAndy Shevchenko data = intel_pinctrl_get_soc_data(pdev);
1678ff360d62SAndy Shevchenko if (IS_ERR(data))
1679ff360d62SAndy Shevchenko return PTR_ERR(data);
1680ff360d62SAndy Shevchenko
1681ff360d62SAndy Shevchenko return intel_pinctrl_probe(pdev, data);
1682ff360d62SAndy Shevchenko }
1683*34393c36SAndy Shevchenko EXPORT_SYMBOL_NS_GPL(intel_pinctrl_probe_by_uid, PINCTRL_INTEL);
1684ff360d62SAndy Shevchenko
intel_pinctrl_get_soc_data(struct platform_device * pdev)1685ff360d62SAndy Shevchenko const struct intel_pinctrl_soc_data *intel_pinctrl_get_soc_data(struct platform_device *pdev)
1686ff360d62SAndy Shevchenko {
1687c551bd81SAndy Shevchenko const struct intel_pinctrl_soc_data * const *table;
1688924cf800SAndy Shevchenko const struct intel_pinctrl_soc_data *data = NULL;
168912b44105SAndy Shevchenko struct device *dev = &pdev->dev;
1690c551bd81SAndy Shevchenko
169112b44105SAndy Shevchenko table = device_get_match_data(dev);
1692c551bd81SAndy Shevchenko if (table) {
169312b44105SAndy Shevchenko struct acpi_device *adev = ACPI_COMPANION(dev);
1694924cf800SAndy Shevchenko unsigned int i;
1695924cf800SAndy Shevchenko
1696924cf800SAndy Shevchenko for (i = 0; table[i]; i++) {
1697924cf800SAndy Shevchenko if (!strcmp(adev->pnp.unique_id, table[i]->uid)) {
1698924cf800SAndy Shevchenko data = table[i];
1699924cf800SAndy Shevchenko break;
1700924cf800SAndy Shevchenko }
1701924cf800SAndy Shevchenko }
1702924cf800SAndy Shevchenko } else {
1703924cf800SAndy Shevchenko const struct platform_device_id *id;
1704924cf800SAndy Shevchenko
1705924cf800SAndy Shevchenko id = platform_get_device_id(pdev);
1706924cf800SAndy Shevchenko if (!id)
1707ff360d62SAndy Shevchenko return ERR_PTR(-ENODEV);
1708924cf800SAndy Shevchenko
1709c551bd81SAndy Shevchenko table = (const struct intel_pinctrl_soc_data * const *)id->driver_data;
1710924cf800SAndy Shevchenko data = table[pdev->id];
1711924cf800SAndy Shevchenko }
1712924cf800SAndy Shevchenko
1713ff360d62SAndy Shevchenko return data ?: ERR_PTR(-ENODATA);
1714924cf800SAndy Shevchenko }
1715*34393c36SAndy Shevchenko EXPORT_SYMBOL_NS_GPL(intel_pinctrl_get_soc_data, PINCTRL_INTEL);
1716924cf800SAndy Shevchenko
17177981c001SMika Westerberg #ifdef CONFIG_PM_SLEEP
__intel_gpio_is_direct_irq(u32 value)1718a8520be3SAndy Shevchenko static bool __intel_gpio_is_direct_irq(u32 value)
1719a8520be3SAndy Shevchenko {
1720a8520be3SAndy Shevchenko return (value & PADCFG0_GPIROUTIOXAPIC) && (value & PADCFG0_GPIOTXDIS) &&
1721a8520be3SAndy Shevchenko (__intel_gpio_get_gpio_mode(value) == PADCFG0_PMODE_GPIO);
1722a8520be3SAndy Shevchenko }
1723a8520be3SAndy Shevchenko
intel_pinctrl_should_save(struct intel_pinctrl * pctrl,unsigned int pin)172404035f7fSAndy Shevchenko static bool intel_pinctrl_should_save(struct intel_pinctrl *pctrl, unsigned int pin)
1725c538b943SMika Westerberg {
1726c538b943SMika Westerberg const struct pin_desc *pd = pin_desc_get(pctrl->pctldev, pin);
17276989ea48SAndy Shevchenko u32 value;
1728c538b943SMika Westerberg
1729c538b943SMika Westerberg if (!pd || !intel_pad_usable(pctrl, pin))
1730c538b943SMika Westerberg return false;
1731c538b943SMika Westerberg
1732c538b943SMika Westerberg /*
1733c538b943SMika Westerberg * Only restore the pin if it is actually in use by the kernel (or
1734c538b943SMika Westerberg * by userspace). It is possible that some pins are used by the
1735c538b943SMika Westerberg * BIOS during resume and those are not always locked down so leave
1736c538b943SMika Westerberg * them alone.
1737c538b943SMika Westerberg */
1738c538b943SMika Westerberg if (pd->mux_owner || pd->gpio_owner ||
17396cb0880fSChris Chiu gpiochip_line_is_irq(&pctrl->chip, intel_pin_to_gpio(pctrl, pin)))
1740c538b943SMika Westerberg return true;
1741c538b943SMika Westerberg
17426989ea48SAndy Shevchenko /*
17436989ea48SAndy Shevchenko * The firmware on some systems may configure GPIO pins to be
17446989ea48SAndy Shevchenko * an interrupt source in so called "direct IRQ" mode. In such
17456989ea48SAndy Shevchenko * cases the GPIO controller driver has no idea if those pins
17466989ea48SAndy Shevchenko * are being used or not. At the same time, there is a known bug
17476989ea48SAndy Shevchenko * in the firmwares that don't restore the pin settings correctly
17486989ea48SAndy Shevchenko * after suspend, i.e. by an unknown reason the Rx value becomes
17496989ea48SAndy Shevchenko * inverted.
17506989ea48SAndy Shevchenko *
17516989ea48SAndy Shevchenko * Hence, let's save and restore the pins that are configured
17526989ea48SAndy Shevchenko * as GPIOs in the input mode with GPIROUTIOXAPIC bit set.
17536989ea48SAndy Shevchenko *
17546989ea48SAndy Shevchenko * See https://bugzilla.kernel.org/show_bug.cgi?id=214749.
17556989ea48SAndy Shevchenko */
17566989ea48SAndy Shevchenko value = readl(intel_get_padcfg(pctrl, pin, PADCFG0));
1757a8520be3SAndy Shevchenko if (__intel_gpio_is_direct_irq(value))
17586989ea48SAndy Shevchenko return true;
17596989ea48SAndy Shevchenko
1760c538b943SMika Westerberg return false;
1761c538b943SMika Westerberg }
1762c538b943SMika Westerberg
intel_pinctrl_suspend_noirq(struct device * dev)17632fef3276SBinbin Wu int intel_pinctrl_suspend_noirq(struct device *dev)
17647981c001SMika Westerberg {
1765cb035d74SWolfram Sang struct intel_pinctrl *pctrl = dev_get_drvdata(dev);
17667981c001SMika Westerberg struct intel_community_context *communities;
17677981c001SMika Westerberg struct intel_pad_context *pads;
17687981c001SMika Westerberg int i;
17697981c001SMika Westerberg
17707981c001SMika Westerberg pads = pctrl->context.pads;
17717981c001SMika Westerberg for (i = 0; i < pctrl->soc->npins; i++) {
17727981c001SMika Westerberg const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
1773e57725eaSMika Westerberg void __iomem *padcfg;
17747981c001SMika Westerberg u32 val;
17757981c001SMika Westerberg
1776c538b943SMika Westerberg if (!intel_pinctrl_should_save(pctrl, desc->number))
17777981c001SMika Westerberg continue;
17787981c001SMika Westerberg
17797981c001SMika Westerberg val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG0));
17807981c001SMika Westerberg pads[i].padcfg0 = val & ~PADCFG0_GPIORXSTATE;
17817981c001SMika Westerberg val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG1));
17827981c001SMika Westerberg pads[i].padcfg1 = val;
1783e57725eaSMika Westerberg
1784e57725eaSMika Westerberg padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG2);
1785e57725eaSMika Westerberg if (padcfg)
1786e57725eaSMika Westerberg pads[i].padcfg2 = readl(padcfg);
17877981c001SMika Westerberg }
17887981c001SMika Westerberg
17897981c001SMika Westerberg communities = pctrl->context.communities;
17907981c001SMika Westerberg for (i = 0; i < pctrl->ncommunities; i++) {
17917981c001SMika Westerberg struct intel_community *community = &pctrl->communities[i];
17927981c001SMika Westerberg void __iomem *base;
179304035f7fSAndy Shevchenko unsigned int gpp;
17947981c001SMika Westerberg
17957981c001SMika Westerberg base = community->regs + community->ie_offset;
17967981c001SMika Westerberg for (gpp = 0; gpp < community->ngpps; gpp++)
17977981c001SMika Westerberg communities[i].intmask[gpp] = readl(base + gpp * 4);
1798a0a5f766SChris Chiu
1799a0a5f766SChris Chiu base = community->regs + community->hostown_offset;
1800a0a5f766SChris Chiu for (gpp = 0; gpp < community->ngpps; gpp++)
1801a0a5f766SChris Chiu communities[i].hostown[gpp] = readl(base + gpp * 4);
18027981c001SMika Westerberg }
18037981c001SMika Westerberg
18047981c001SMika Westerberg return 0;
18057981c001SMika Westerberg }
18062fef3276SBinbin Wu EXPORT_SYMBOL_GPL(intel_pinctrl_suspend_noirq);
18077981c001SMika Westerberg
intel_gpio_update_reg(void __iomem * reg,u32 mask,u32 value)1808942c5ea4SAndy Shevchenko static bool intel_gpio_update_reg(void __iomem *reg, u32 mask, u32 value)
1809a0a5f766SChris Chiu {
18105f61d951SAndy Shevchenko u32 curr, updated;
1811a0a5f766SChris Chiu
1812942c5ea4SAndy Shevchenko curr = readl(reg);
18135f61d951SAndy Shevchenko
1814942c5ea4SAndy Shevchenko updated = (curr & ~mask) | (value & mask);
1815942c5ea4SAndy Shevchenko if (curr == updated)
1816942c5ea4SAndy Shevchenko return false;
1817942c5ea4SAndy Shevchenko
1818942c5ea4SAndy Shevchenko writel(updated, reg);
1819942c5ea4SAndy Shevchenko return true;
1820a0a5f766SChris Chiu }
1821a0a5f766SChris Chiu
intel_restore_hostown(struct intel_pinctrl * pctrl,unsigned int c,void __iomem * base,unsigned int gpp,u32 saved)18227101e022SAndy Shevchenko static void intel_restore_hostown(struct intel_pinctrl *pctrl, unsigned int c,
18237101e022SAndy Shevchenko void __iomem *base, unsigned int gpp, u32 saved)
18247101e022SAndy Shevchenko {
18257101e022SAndy Shevchenko const struct intel_community *community = &pctrl->communities[c];
18267101e022SAndy Shevchenko const struct intel_padgroup *padgrp = &community->gpps[gpp];
18277101e022SAndy Shevchenko struct device *dev = pctrl->dev;
1828d1bfd022SAndy Shevchenko const char *dummy;
1829d1bfd022SAndy Shevchenko u32 requested = 0;
1830d1bfd022SAndy Shevchenko unsigned int i;
18317101e022SAndy Shevchenko
1832e5a4ab6aSAndy Shevchenko if (padgrp->gpio_base == INTEL_GPIO_BASE_NOMAP)
18337101e022SAndy Shevchenko return;
18347101e022SAndy Shevchenko
1835d1bfd022SAndy Shevchenko for_each_requested_gpio_in_range(&pctrl->chip, i, padgrp->gpio_base, padgrp->size, dummy)
1836d1bfd022SAndy Shevchenko requested |= BIT(i);
1837d1bfd022SAndy Shevchenko
1838942c5ea4SAndy Shevchenko if (!intel_gpio_update_reg(base + gpp * 4, requested, saved))
18397101e022SAndy Shevchenko return;
18407101e022SAndy Shevchenko
1841764cfe33SAndy Shevchenko dev_dbg(dev, "restored hostown %u/%u %#08x\n", c, gpp, readl(base + gpp * 4));
18427101e022SAndy Shevchenko }
18437101e022SAndy Shevchenko
intel_restore_intmask(struct intel_pinctrl * pctrl,unsigned int c,void __iomem * base,unsigned int gpp,u32 saved)1844471dd9a9SAndy Shevchenko static void intel_restore_intmask(struct intel_pinctrl *pctrl, unsigned int c,
1845471dd9a9SAndy Shevchenko void __iomem *base, unsigned int gpp, u32 saved)
1846471dd9a9SAndy Shevchenko {
1847471dd9a9SAndy Shevchenko struct device *dev = pctrl->dev;
1848471dd9a9SAndy Shevchenko
1849942c5ea4SAndy Shevchenko if (!intel_gpio_update_reg(base + gpp * 4, ~0U, saved))
1850942c5ea4SAndy Shevchenko return;
1851942c5ea4SAndy Shevchenko
1852471dd9a9SAndy Shevchenko dev_dbg(dev, "restored mask %u/%u %#08x\n", c, gpp, readl(base + gpp * 4));
1853471dd9a9SAndy Shevchenko }
1854471dd9a9SAndy Shevchenko
intel_restore_padcfg(struct intel_pinctrl * pctrl,unsigned int pin,unsigned int reg,u32 saved)1855f78f152aSAndy Shevchenko static void intel_restore_padcfg(struct intel_pinctrl *pctrl, unsigned int pin,
1856f78f152aSAndy Shevchenko unsigned int reg, u32 saved)
1857f78f152aSAndy Shevchenko {
1858f78f152aSAndy Shevchenko u32 mask = (reg == PADCFG0) ? PADCFG0_GPIORXSTATE : 0;
1859f78f152aSAndy Shevchenko unsigned int n = reg / sizeof(u32);
1860f78f152aSAndy Shevchenko struct device *dev = pctrl->dev;
1861f78f152aSAndy Shevchenko void __iomem *padcfg;
1862f78f152aSAndy Shevchenko
1863f78f152aSAndy Shevchenko padcfg = intel_get_padcfg(pctrl, pin, reg);
1864f78f152aSAndy Shevchenko if (!padcfg)
1865f78f152aSAndy Shevchenko return;
1866f78f152aSAndy Shevchenko
1867942c5ea4SAndy Shevchenko if (!intel_gpio_update_reg(padcfg, ~mask, saved))
1868f78f152aSAndy Shevchenko return;
1869f78f152aSAndy Shevchenko
1870f78f152aSAndy Shevchenko dev_dbg(dev, "restored pin %u padcfg%u %#08x\n", pin, n, readl(padcfg));
1871f78f152aSAndy Shevchenko }
1872f78f152aSAndy Shevchenko
intel_pinctrl_resume_noirq(struct device * dev)18732fef3276SBinbin Wu int intel_pinctrl_resume_noirq(struct device *dev)
18747981c001SMika Westerberg {
1875cb035d74SWolfram Sang struct intel_pinctrl *pctrl = dev_get_drvdata(dev);
18767981c001SMika Westerberg const struct intel_community_context *communities;
18777981c001SMika Westerberg const struct intel_pad_context *pads;
18787981c001SMika Westerberg int i;
18797981c001SMika Westerberg
18807981c001SMika Westerberg /* Mask all interrupts */
18817981c001SMika Westerberg intel_gpio_irq_init(pctrl);
18827981c001SMika Westerberg
18837981c001SMika Westerberg pads = pctrl->context.pads;
18847981c001SMika Westerberg for (i = 0; i < pctrl->soc->npins; i++) {
18857981c001SMika Westerberg const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
18867981c001SMika Westerberg
1887a8520be3SAndy Shevchenko if (!(intel_pinctrl_should_save(pctrl, desc->number) ||
1888a8520be3SAndy Shevchenko /*
1889a8520be3SAndy Shevchenko * If the firmware mangled the register contents too much,
1890a8520be3SAndy Shevchenko * check the saved value for the Direct IRQ mode.
1891a8520be3SAndy Shevchenko */
1892a8520be3SAndy Shevchenko __intel_gpio_is_direct_irq(pads[i].padcfg0)))
18937981c001SMika Westerberg continue;
18947981c001SMika Westerberg
1895f78f152aSAndy Shevchenko intel_restore_padcfg(pctrl, desc->number, PADCFG0, pads[i].padcfg0);
1896f78f152aSAndy Shevchenko intel_restore_padcfg(pctrl, desc->number, PADCFG1, pads[i].padcfg1);
1897f78f152aSAndy Shevchenko intel_restore_padcfg(pctrl, desc->number, PADCFG2, pads[i].padcfg2);
18987981c001SMika Westerberg }
18997981c001SMika Westerberg
19007981c001SMika Westerberg communities = pctrl->context.communities;
19017981c001SMika Westerberg for (i = 0; i < pctrl->ncommunities; i++) {
19027981c001SMika Westerberg struct intel_community *community = &pctrl->communities[i];
19037981c001SMika Westerberg void __iomem *base;
190404035f7fSAndy Shevchenko unsigned int gpp;
19057981c001SMika Westerberg
19067981c001SMika Westerberg base = community->regs + community->ie_offset;
1907471dd9a9SAndy Shevchenko for (gpp = 0; gpp < community->ngpps; gpp++)
1908471dd9a9SAndy Shevchenko intel_restore_intmask(pctrl, i, base, gpp, communities[i].intmask[gpp]);
1909a0a5f766SChris Chiu
1910a0a5f766SChris Chiu base = community->regs + community->hostown_offset;
19117101e022SAndy Shevchenko for (gpp = 0; gpp < community->ngpps; gpp++)
19127101e022SAndy Shevchenko intel_restore_hostown(pctrl, i, base, gpp, communities[i].hostown[gpp]);
19137981c001SMika Westerberg }
19147981c001SMika Westerberg
19157981c001SMika Westerberg return 0;
19167981c001SMika Westerberg }
19172fef3276SBinbin Wu EXPORT_SYMBOL_GPL(intel_pinctrl_resume_noirq);
19187981c001SMika Westerberg #endif
19197981c001SMika Westerberg
19207981c001SMika Westerberg MODULE_AUTHOR("Mathias Nyman <mathias.nyman@linux.intel.com>");
19217981c001SMika Westerberg MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
19227981c001SMika Westerberg MODULE_DESCRIPTION("Intel pinctrl/GPIO core driver");
19237981c001SMika Westerberg MODULE_LICENSE("GPL v2");
1924