17981c001SMika Westerberg /* 27981c001SMika Westerberg * Intel pinctrl/GPIO core driver. 37981c001SMika Westerberg * 47981c001SMika Westerberg * Copyright (C) 2015, Intel Corporation 57981c001SMika Westerberg * Authors: Mathias Nyman <mathias.nyman@linux.intel.com> 67981c001SMika Westerberg * Mika Westerberg <mika.westerberg@linux.intel.com> 77981c001SMika Westerberg * 87981c001SMika Westerberg * This program is free software; you can redistribute it and/or modify 97981c001SMika Westerberg * it under the terms of the GNU General Public License version 2 as 107981c001SMika Westerberg * published by the Free Software Foundation. 117981c001SMika Westerberg */ 127981c001SMika Westerberg 137981c001SMika Westerberg #include <linux/module.h> 147981c001SMika Westerberg #include <linux/init.h> 15193b40c8SMika Westerberg #include <linux/interrupt.h> 167981c001SMika Westerberg #include <linux/acpi.h> 177981c001SMika Westerberg #include <linux/gpio.h> 187981c001SMika Westerberg #include <linux/gpio/driver.h> 197981c001SMika Westerberg #include <linux/platform_device.h> 207981c001SMika Westerberg #include <linux/pm.h> 217981c001SMika Westerberg #include <linux/pinctrl/pinctrl.h> 227981c001SMika Westerberg #include <linux/pinctrl/pinmux.h> 237981c001SMika Westerberg #include <linux/pinctrl/pinconf.h> 247981c001SMika Westerberg #include <linux/pinctrl/pinconf-generic.h> 257981c001SMika Westerberg 267981c001SMika Westerberg #include "pinctrl-intel.h" 277981c001SMika Westerberg 287981c001SMika Westerberg /* Offset from regs */ 297981c001SMika Westerberg #define PADBAR 0x00c 307981c001SMika Westerberg #define GPI_IS 0x100 317981c001SMika Westerberg #define GPI_GPE_STS 0x140 327981c001SMika Westerberg #define GPI_GPE_EN 0x160 337981c001SMika Westerberg 347981c001SMika Westerberg #define PADOWN_BITS 4 357981c001SMika Westerberg #define PADOWN_SHIFT(p) ((p) % 8 * PADOWN_BITS) 367981c001SMika Westerberg #define PADOWN_MASK(p) (0xf << PADOWN_SHIFT(p)) 3799a735b3SQipeng Zha #define PADOWN_GPP(p) ((p) / 8) 387981c001SMika Westerberg 397981c001SMika Westerberg /* Offset from pad_regs */ 407981c001SMika Westerberg #define PADCFG0 0x000 417981c001SMika Westerberg #define PADCFG0_RXEVCFG_SHIFT 25 427981c001SMika Westerberg #define PADCFG0_RXEVCFG_MASK (3 << PADCFG0_RXEVCFG_SHIFT) 437981c001SMika Westerberg #define PADCFG0_RXEVCFG_LEVEL 0 447981c001SMika Westerberg #define PADCFG0_RXEVCFG_EDGE 1 457981c001SMika Westerberg #define PADCFG0_RXEVCFG_DISABLED 2 467981c001SMika Westerberg #define PADCFG0_RXEVCFG_EDGE_BOTH 3 477981c001SMika Westerberg #define PADCFG0_RXINV BIT(23) 487981c001SMika Westerberg #define PADCFG0_GPIROUTIOXAPIC BIT(20) 497981c001SMika Westerberg #define PADCFG0_GPIROUTSCI BIT(19) 507981c001SMika Westerberg #define PADCFG0_GPIROUTSMI BIT(18) 517981c001SMika Westerberg #define PADCFG0_GPIROUTNMI BIT(17) 527981c001SMika Westerberg #define PADCFG0_PMODE_SHIFT 10 537981c001SMika Westerberg #define PADCFG0_PMODE_MASK (0xf << PADCFG0_PMODE_SHIFT) 547981c001SMika Westerberg #define PADCFG0_GPIORXDIS BIT(9) 557981c001SMika Westerberg #define PADCFG0_GPIOTXDIS BIT(8) 567981c001SMika Westerberg #define PADCFG0_GPIORXSTATE BIT(1) 577981c001SMika Westerberg #define PADCFG0_GPIOTXSTATE BIT(0) 587981c001SMika Westerberg 597981c001SMika Westerberg #define PADCFG1 0x004 607981c001SMika Westerberg #define PADCFG1_TERM_UP BIT(13) 617981c001SMika Westerberg #define PADCFG1_TERM_SHIFT 10 627981c001SMika Westerberg #define PADCFG1_TERM_MASK (7 << PADCFG1_TERM_SHIFT) 637981c001SMika Westerberg #define PADCFG1_TERM_20K 4 647981c001SMika Westerberg #define PADCFG1_TERM_2K 3 657981c001SMika Westerberg #define PADCFG1_TERM_5K 2 667981c001SMika Westerberg #define PADCFG1_TERM_1K 1 677981c001SMika Westerberg 687981c001SMika Westerberg struct intel_pad_context { 697981c001SMika Westerberg u32 padcfg0; 707981c001SMika Westerberg u32 padcfg1; 717981c001SMika Westerberg }; 727981c001SMika Westerberg 737981c001SMika Westerberg struct intel_community_context { 747981c001SMika Westerberg u32 *intmask; 757981c001SMika Westerberg }; 767981c001SMika Westerberg 777981c001SMika Westerberg struct intel_pinctrl_context { 787981c001SMika Westerberg struct intel_pad_context *pads; 797981c001SMika Westerberg struct intel_community_context *communities; 807981c001SMika Westerberg }; 817981c001SMika Westerberg 827981c001SMika Westerberg /** 837981c001SMika Westerberg * struct intel_pinctrl - Intel pinctrl private structure 847981c001SMika Westerberg * @dev: Pointer to the device structure 857981c001SMika Westerberg * @lock: Lock to serialize register access 867981c001SMika Westerberg * @pctldesc: Pin controller description 877981c001SMika Westerberg * @pctldev: Pointer to the pin controller device 887981c001SMika Westerberg * @chip: GPIO chip in this pin controller 897981c001SMika Westerberg * @soc: SoC/PCH specific pin configuration data 907981c001SMika Westerberg * @communities: All communities in this pin controller 917981c001SMika Westerberg * @ncommunities: Number of communities in this pin controller 927981c001SMika Westerberg * @context: Configuration saved over system sleep 937981c001SMika Westerberg */ 947981c001SMika Westerberg struct intel_pinctrl { 957981c001SMika Westerberg struct device *dev; 967981c001SMika Westerberg spinlock_t lock; 977981c001SMika Westerberg struct pinctrl_desc pctldesc; 987981c001SMika Westerberg struct pinctrl_dev *pctldev; 997981c001SMika Westerberg struct gpio_chip chip; 1007981c001SMika Westerberg const struct intel_pinctrl_soc_data *soc; 1017981c001SMika Westerberg struct intel_community *communities; 1027981c001SMika Westerberg size_t ncommunities; 1037981c001SMika Westerberg struct intel_pinctrl_context context; 1047981c001SMika Westerberg }; 1057981c001SMika Westerberg 1067981c001SMika Westerberg #define pin_to_padno(c, p) ((p) - (c)->pin_base) 1077981c001SMika Westerberg 1087981c001SMika Westerberg static struct intel_community *intel_get_community(struct intel_pinctrl *pctrl, 1097981c001SMika Westerberg unsigned pin) 1107981c001SMika Westerberg { 1117981c001SMika Westerberg struct intel_community *community; 1127981c001SMika Westerberg int i; 1137981c001SMika Westerberg 1147981c001SMika Westerberg for (i = 0; i < pctrl->ncommunities; i++) { 1157981c001SMika Westerberg community = &pctrl->communities[i]; 1167981c001SMika Westerberg if (pin >= community->pin_base && 1177981c001SMika Westerberg pin < community->pin_base + community->npins) 1187981c001SMika Westerberg return community; 1197981c001SMika Westerberg } 1207981c001SMika Westerberg 1217981c001SMika Westerberg dev_warn(pctrl->dev, "failed to find community for pin %u\n", pin); 1227981c001SMika Westerberg return NULL; 1237981c001SMika Westerberg } 1247981c001SMika Westerberg 1257981c001SMika Westerberg static void __iomem *intel_get_padcfg(struct intel_pinctrl *pctrl, unsigned pin, 1267981c001SMika Westerberg unsigned reg) 1277981c001SMika Westerberg { 1287981c001SMika Westerberg const struct intel_community *community; 1297981c001SMika Westerberg unsigned padno; 1307981c001SMika Westerberg 1317981c001SMika Westerberg community = intel_get_community(pctrl, pin); 1327981c001SMika Westerberg if (!community) 1337981c001SMika Westerberg return NULL; 1347981c001SMika Westerberg 1357981c001SMika Westerberg padno = pin_to_padno(community, pin); 1367981c001SMika Westerberg return community->pad_regs + reg + padno * 8; 1377981c001SMika Westerberg } 1387981c001SMika Westerberg 1397981c001SMika Westerberg static bool intel_pad_owned_by_host(struct intel_pinctrl *pctrl, unsigned pin) 1407981c001SMika Westerberg { 1417981c001SMika Westerberg const struct intel_community *community; 14299a735b3SQipeng Zha unsigned padno, gpp, offset, group; 1437981c001SMika Westerberg void __iomem *padown; 1447981c001SMika Westerberg 1457981c001SMika Westerberg community = intel_get_community(pctrl, pin); 1467981c001SMika Westerberg if (!community) 1477981c001SMika Westerberg return false; 1487981c001SMika Westerberg if (!community->padown_offset) 1497981c001SMika Westerberg return true; 1507981c001SMika Westerberg 1517981c001SMika Westerberg padno = pin_to_padno(community, pin); 15299a735b3SQipeng Zha group = padno / community->gpp_size; 15399a735b3SQipeng Zha gpp = PADOWN_GPP(padno % community->gpp_size); 15499a735b3SQipeng Zha offset = community->padown_offset + 0x10 * group + gpp * 4; 1557981c001SMika Westerberg padown = community->regs + offset; 1567981c001SMika Westerberg 1577981c001SMika Westerberg return !(readl(padown) & PADOWN_MASK(padno)); 1587981c001SMika Westerberg } 1597981c001SMika Westerberg 1604341e8a5SMika Westerberg static bool intel_pad_acpi_mode(struct intel_pinctrl *pctrl, unsigned pin) 1617981c001SMika Westerberg { 1627981c001SMika Westerberg const struct intel_community *community; 1637981c001SMika Westerberg unsigned padno, gpp, offset; 1647981c001SMika Westerberg void __iomem *hostown; 1657981c001SMika Westerberg 1667981c001SMika Westerberg community = intel_get_community(pctrl, pin); 1677981c001SMika Westerberg if (!community) 1687981c001SMika Westerberg return true; 1697981c001SMika Westerberg if (!community->hostown_offset) 1707981c001SMika Westerberg return false; 1717981c001SMika Westerberg 1727981c001SMika Westerberg padno = pin_to_padno(community, pin); 173618a919bSQipeng Zha gpp = padno / community->gpp_size; 1747981c001SMika Westerberg offset = community->hostown_offset + gpp * 4; 1757981c001SMika Westerberg hostown = community->regs + offset; 1767981c001SMika Westerberg 177618a919bSQipeng Zha return !(readl(hostown) & BIT(padno % community->gpp_size)); 1787981c001SMika Westerberg } 1797981c001SMika Westerberg 1807981c001SMika Westerberg static bool intel_pad_locked(struct intel_pinctrl *pctrl, unsigned pin) 1817981c001SMika Westerberg { 1827981c001SMika Westerberg struct intel_community *community; 1837981c001SMika Westerberg unsigned padno, gpp, offset; 1847981c001SMika Westerberg u32 value; 1857981c001SMika Westerberg 1867981c001SMika Westerberg community = intel_get_community(pctrl, pin); 1877981c001SMika Westerberg if (!community) 1887981c001SMika Westerberg return true; 1897981c001SMika Westerberg if (!community->padcfglock_offset) 1907981c001SMika Westerberg return false; 1917981c001SMika Westerberg 1927981c001SMika Westerberg padno = pin_to_padno(community, pin); 193618a919bSQipeng Zha gpp = padno / community->gpp_size; 1947981c001SMika Westerberg 1957981c001SMika Westerberg /* 1967981c001SMika Westerberg * If PADCFGLOCK and PADCFGLOCKTX bits are both clear for this pad, 1977981c001SMika Westerberg * the pad is considered unlocked. Any other case means that it is 1987981c001SMika Westerberg * either fully or partially locked and we don't touch it. 1997981c001SMika Westerberg */ 2007981c001SMika Westerberg offset = community->padcfglock_offset + gpp * 8; 2017981c001SMika Westerberg value = readl(community->regs + offset); 202618a919bSQipeng Zha if (value & BIT(pin % community->gpp_size)) 2037981c001SMika Westerberg return true; 2047981c001SMika Westerberg 2057981c001SMika Westerberg offset = community->padcfglock_offset + 4 + gpp * 8; 2067981c001SMika Westerberg value = readl(community->regs + offset); 207618a919bSQipeng Zha if (value & BIT(pin % community->gpp_size)) 2087981c001SMika Westerberg return true; 2097981c001SMika Westerberg 2107981c001SMika Westerberg return false; 2117981c001SMika Westerberg } 2127981c001SMika Westerberg 2137981c001SMika Westerberg static bool intel_pad_usable(struct intel_pinctrl *pctrl, unsigned pin) 2147981c001SMika Westerberg { 2157981c001SMika Westerberg return intel_pad_owned_by_host(pctrl, pin) && 2167981c001SMika Westerberg !intel_pad_locked(pctrl, pin); 2177981c001SMika Westerberg } 2187981c001SMika Westerberg 2197981c001SMika Westerberg static int intel_get_groups_count(struct pinctrl_dev *pctldev) 2207981c001SMika Westerberg { 2217981c001SMika Westerberg struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); 2227981c001SMika Westerberg 2237981c001SMika Westerberg return pctrl->soc->ngroups; 2247981c001SMika Westerberg } 2257981c001SMika Westerberg 2267981c001SMika Westerberg static const char *intel_get_group_name(struct pinctrl_dev *pctldev, 2277981c001SMika Westerberg unsigned group) 2287981c001SMika Westerberg { 2297981c001SMika Westerberg struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); 2307981c001SMika Westerberg 2317981c001SMika Westerberg return pctrl->soc->groups[group].name; 2327981c001SMika Westerberg } 2337981c001SMika Westerberg 2347981c001SMika Westerberg static int intel_get_group_pins(struct pinctrl_dev *pctldev, unsigned group, 2357981c001SMika Westerberg const unsigned **pins, unsigned *npins) 2367981c001SMika Westerberg { 2377981c001SMika Westerberg struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); 2387981c001SMika Westerberg 2397981c001SMika Westerberg *pins = pctrl->soc->groups[group].pins; 2407981c001SMika Westerberg *npins = pctrl->soc->groups[group].npins; 2417981c001SMika Westerberg return 0; 2427981c001SMika Westerberg } 2437981c001SMika Westerberg 2447981c001SMika Westerberg static void intel_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s, 2457981c001SMika Westerberg unsigned pin) 2467981c001SMika Westerberg { 2477981c001SMika Westerberg struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); 2487981c001SMika Westerberg u32 cfg0, cfg1, mode; 2497981c001SMika Westerberg bool locked, acpi; 2507981c001SMika Westerberg 2517981c001SMika Westerberg if (!intel_pad_owned_by_host(pctrl, pin)) { 2527981c001SMika Westerberg seq_puts(s, "not available"); 2537981c001SMika Westerberg return; 2547981c001SMika Westerberg } 2557981c001SMika Westerberg 2567981c001SMika Westerberg cfg0 = readl(intel_get_padcfg(pctrl, pin, PADCFG0)); 2577981c001SMika Westerberg cfg1 = readl(intel_get_padcfg(pctrl, pin, PADCFG1)); 2587981c001SMika Westerberg 2597981c001SMika Westerberg mode = (cfg0 & PADCFG0_PMODE_MASK) >> PADCFG0_PMODE_SHIFT; 2607981c001SMika Westerberg if (!mode) 2617981c001SMika Westerberg seq_puts(s, "GPIO "); 2627981c001SMika Westerberg else 2637981c001SMika Westerberg seq_printf(s, "mode %d ", mode); 2647981c001SMika Westerberg 2657981c001SMika Westerberg seq_printf(s, "0x%08x 0x%08x", cfg0, cfg1); 2667981c001SMika Westerberg 2677981c001SMika Westerberg locked = intel_pad_locked(pctrl, pin); 2684341e8a5SMika Westerberg acpi = intel_pad_acpi_mode(pctrl, pin); 2697981c001SMika Westerberg 2707981c001SMika Westerberg if (locked || acpi) { 2717981c001SMika Westerberg seq_puts(s, " ["); 2727981c001SMika Westerberg if (locked) { 2737981c001SMika Westerberg seq_puts(s, "LOCKED"); 2747981c001SMika Westerberg if (acpi) 2757981c001SMika Westerberg seq_puts(s, ", "); 2767981c001SMika Westerberg } 2777981c001SMika Westerberg if (acpi) 2787981c001SMika Westerberg seq_puts(s, "ACPI"); 2797981c001SMika Westerberg seq_puts(s, "]"); 2807981c001SMika Westerberg } 2817981c001SMika Westerberg } 2827981c001SMika Westerberg 2837981c001SMika Westerberg static const struct pinctrl_ops intel_pinctrl_ops = { 2847981c001SMika Westerberg .get_groups_count = intel_get_groups_count, 2857981c001SMika Westerberg .get_group_name = intel_get_group_name, 2867981c001SMika Westerberg .get_group_pins = intel_get_group_pins, 2877981c001SMika Westerberg .pin_dbg_show = intel_pin_dbg_show, 2887981c001SMika Westerberg }; 2897981c001SMika Westerberg 2907981c001SMika Westerberg static int intel_get_functions_count(struct pinctrl_dev *pctldev) 2917981c001SMika Westerberg { 2927981c001SMika Westerberg struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); 2937981c001SMika Westerberg 2947981c001SMika Westerberg return pctrl->soc->nfunctions; 2957981c001SMika Westerberg } 2967981c001SMika Westerberg 2977981c001SMika Westerberg static const char *intel_get_function_name(struct pinctrl_dev *pctldev, 2987981c001SMika Westerberg unsigned function) 2997981c001SMika Westerberg { 3007981c001SMika Westerberg struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); 3017981c001SMika Westerberg 3027981c001SMika Westerberg return pctrl->soc->functions[function].name; 3037981c001SMika Westerberg } 3047981c001SMika Westerberg 3057981c001SMika Westerberg static int intel_get_function_groups(struct pinctrl_dev *pctldev, 3067981c001SMika Westerberg unsigned function, 3077981c001SMika Westerberg const char * const **groups, 3087981c001SMika Westerberg unsigned * const ngroups) 3097981c001SMika Westerberg { 3107981c001SMika Westerberg struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); 3117981c001SMika Westerberg 3127981c001SMika Westerberg *groups = pctrl->soc->functions[function].groups; 3137981c001SMika Westerberg *ngroups = pctrl->soc->functions[function].ngroups; 3147981c001SMika Westerberg return 0; 3157981c001SMika Westerberg } 3167981c001SMika Westerberg 3177981c001SMika Westerberg static int intel_pinmux_set_mux(struct pinctrl_dev *pctldev, unsigned function, 3187981c001SMika Westerberg unsigned group) 3197981c001SMika Westerberg { 3207981c001SMika Westerberg struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); 3217981c001SMika Westerberg const struct intel_pingroup *grp = &pctrl->soc->groups[group]; 3227981c001SMika Westerberg unsigned long flags; 3237981c001SMika Westerberg int i; 3247981c001SMika Westerberg 3257981c001SMika Westerberg spin_lock_irqsave(&pctrl->lock, flags); 3267981c001SMika Westerberg 3277981c001SMika Westerberg /* 3287981c001SMika Westerberg * All pins in the groups needs to be accessible and writable 3297981c001SMika Westerberg * before we can enable the mux for this group. 3307981c001SMika Westerberg */ 3317981c001SMika Westerberg for (i = 0; i < grp->npins; i++) { 3327981c001SMika Westerberg if (!intel_pad_usable(pctrl, grp->pins[i])) { 3337981c001SMika Westerberg spin_unlock_irqrestore(&pctrl->lock, flags); 3347981c001SMika Westerberg return -EBUSY; 3357981c001SMika Westerberg } 3367981c001SMika Westerberg } 3377981c001SMika Westerberg 3387981c001SMika Westerberg /* Now enable the mux setting for each pin in the group */ 3397981c001SMika Westerberg for (i = 0; i < grp->npins; i++) { 3407981c001SMika Westerberg void __iomem *padcfg0; 3417981c001SMika Westerberg u32 value; 3427981c001SMika Westerberg 3437981c001SMika Westerberg padcfg0 = intel_get_padcfg(pctrl, grp->pins[i], PADCFG0); 3447981c001SMika Westerberg value = readl(padcfg0); 3457981c001SMika Westerberg 3467981c001SMika Westerberg value &= ~PADCFG0_PMODE_MASK; 3477981c001SMika Westerberg value |= grp->mode << PADCFG0_PMODE_SHIFT; 3487981c001SMika Westerberg 3497981c001SMika Westerberg writel(value, padcfg0); 3507981c001SMika Westerberg } 3517981c001SMika Westerberg 3527981c001SMika Westerberg spin_unlock_irqrestore(&pctrl->lock, flags); 3537981c001SMika Westerberg 3547981c001SMika Westerberg return 0; 3557981c001SMika Westerberg } 3567981c001SMika Westerberg 3577981c001SMika Westerberg static int intel_gpio_request_enable(struct pinctrl_dev *pctldev, 3587981c001SMika Westerberg struct pinctrl_gpio_range *range, 3597981c001SMika Westerberg unsigned pin) 3607981c001SMika Westerberg { 3617981c001SMika Westerberg struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); 3627981c001SMika Westerberg void __iomem *padcfg0; 3637981c001SMika Westerberg unsigned long flags; 3647981c001SMika Westerberg u32 value; 3657981c001SMika Westerberg 3667981c001SMika Westerberg spin_lock_irqsave(&pctrl->lock, flags); 3677981c001SMika Westerberg 3687981c001SMika Westerberg if (!intel_pad_usable(pctrl, pin)) { 3697981c001SMika Westerberg spin_unlock_irqrestore(&pctrl->lock, flags); 3707981c001SMika Westerberg return -EBUSY; 3717981c001SMika Westerberg } 3727981c001SMika Westerberg 3737981c001SMika Westerberg padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0); 3747981c001SMika Westerberg /* Put the pad into GPIO mode */ 3757981c001SMika Westerberg value = readl(padcfg0) & ~PADCFG0_PMODE_MASK; 3767981c001SMika Westerberg /* Disable SCI/SMI/NMI generation */ 3777981c001SMika Westerberg value &= ~(PADCFG0_GPIROUTIOXAPIC | PADCFG0_GPIROUTSCI); 3787981c001SMika Westerberg value &= ~(PADCFG0_GPIROUTSMI | PADCFG0_GPIROUTNMI); 3797981c001SMika Westerberg /* Disable TX buffer and enable RX (this will be input) */ 3807981c001SMika Westerberg value &= ~PADCFG0_GPIORXDIS; 3817981c001SMika Westerberg value |= PADCFG0_GPIOTXDIS; 3827981c001SMika Westerberg writel(value, padcfg0); 3837981c001SMika Westerberg 3847981c001SMika Westerberg spin_unlock_irqrestore(&pctrl->lock, flags); 3857981c001SMika Westerberg 3867981c001SMika Westerberg return 0; 3877981c001SMika Westerberg } 3887981c001SMika Westerberg 3897981c001SMika Westerberg static int intel_gpio_set_direction(struct pinctrl_dev *pctldev, 3907981c001SMika Westerberg struct pinctrl_gpio_range *range, 3917981c001SMika Westerberg unsigned pin, bool input) 3927981c001SMika Westerberg { 3937981c001SMika Westerberg struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); 3947981c001SMika Westerberg void __iomem *padcfg0; 3957981c001SMika Westerberg unsigned long flags; 3967981c001SMika Westerberg u32 value; 3977981c001SMika Westerberg 3987981c001SMika Westerberg spin_lock_irqsave(&pctrl->lock, flags); 3997981c001SMika Westerberg 4007981c001SMika Westerberg padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0); 4017981c001SMika Westerberg 4027981c001SMika Westerberg value = readl(padcfg0); 4037981c001SMika Westerberg if (input) 4047981c001SMika Westerberg value |= PADCFG0_GPIOTXDIS; 4057981c001SMika Westerberg else 4067981c001SMika Westerberg value &= ~PADCFG0_GPIOTXDIS; 4077981c001SMika Westerberg writel(value, padcfg0); 4087981c001SMika Westerberg 4097981c001SMika Westerberg spin_unlock_irqrestore(&pctrl->lock, flags); 4107981c001SMika Westerberg 4117981c001SMika Westerberg return 0; 4127981c001SMika Westerberg } 4137981c001SMika Westerberg 4147981c001SMika Westerberg static const struct pinmux_ops intel_pinmux_ops = { 4157981c001SMika Westerberg .get_functions_count = intel_get_functions_count, 4167981c001SMika Westerberg .get_function_name = intel_get_function_name, 4177981c001SMika Westerberg .get_function_groups = intel_get_function_groups, 4187981c001SMika Westerberg .set_mux = intel_pinmux_set_mux, 4197981c001SMika Westerberg .gpio_request_enable = intel_gpio_request_enable, 4207981c001SMika Westerberg .gpio_set_direction = intel_gpio_set_direction, 4217981c001SMika Westerberg }; 4227981c001SMika Westerberg 4237981c001SMika Westerberg static int intel_config_get(struct pinctrl_dev *pctldev, unsigned pin, 4247981c001SMika Westerberg unsigned long *config) 4257981c001SMika Westerberg { 4267981c001SMika Westerberg struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); 4277981c001SMika Westerberg enum pin_config_param param = pinconf_to_config_param(*config); 4287981c001SMika Westerberg u32 value, term; 4297981c001SMika Westerberg u16 arg = 0; 4307981c001SMika Westerberg 4317981c001SMika Westerberg if (!intel_pad_owned_by_host(pctrl, pin)) 4327981c001SMika Westerberg return -ENOTSUPP; 4337981c001SMika Westerberg 4347981c001SMika Westerberg value = readl(intel_get_padcfg(pctrl, pin, PADCFG1)); 4357981c001SMika Westerberg term = (value & PADCFG1_TERM_MASK) >> PADCFG1_TERM_SHIFT; 4367981c001SMika Westerberg 4377981c001SMika Westerberg switch (param) { 4387981c001SMika Westerberg case PIN_CONFIG_BIAS_DISABLE: 4397981c001SMika Westerberg if (term) 4407981c001SMika Westerberg return -EINVAL; 4417981c001SMika Westerberg break; 4427981c001SMika Westerberg 4437981c001SMika Westerberg case PIN_CONFIG_BIAS_PULL_UP: 4447981c001SMika Westerberg if (!term || !(value & PADCFG1_TERM_UP)) 4457981c001SMika Westerberg return -EINVAL; 4467981c001SMika Westerberg 4477981c001SMika Westerberg switch (term) { 4487981c001SMika Westerberg case PADCFG1_TERM_1K: 4497981c001SMika Westerberg arg = 1000; 4507981c001SMika Westerberg break; 4517981c001SMika Westerberg case PADCFG1_TERM_2K: 4527981c001SMika Westerberg arg = 2000; 4537981c001SMika Westerberg break; 4547981c001SMika Westerberg case PADCFG1_TERM_5K: 4557981c001SMika Westerberg arg = 5000; 4567981c001SMika Westerberg break; 4577981c001SMika Westerberg case PADCFG1_TERM_20K: 4587981c001SMika Westerberg arg = 20000; 4597981c001SMika Westerberg break; 4607981c001SMika Westerberg } 4617981c001SMika Westerberg 4627981c001SMika Westerberg break; 4637981c001SMika Westerberg 4647981c001SMika Westerberg case PIN_CONFIG_BIAS_PULL_DOWN: 4657981c001SMika Westerberg if (!term || value & PADCFG1_TERM_UP) 4667981c001SMika Westerberg return -EINVAL; 4677981c001SMika Westerberg 4687981c001SMika Westerberg switch (term) { 4697981c001SMika Westerberg case PADCFG1_TERM_5K: 4707981c001SMika Westerberg arg = 5000; 4717981c001SMika Westerberg break; 4727981c001SMika Westerberg case PADCFG1_TERM_20K: 4737981c001SMika Westerberg arg = 20000; 4747981c001SMika Westerberg break; 4757981c001SMika Westerberg } 4767981c001SMika Westerberg 4777981c001SMika Westerberg break; 4787981c001SMika Westerberg 4797981c001SMika Westerberg default: 4807981c001SMika Westerberg return -ENOTSUPP; 4817981c001SMika Westerberg } 4827981c001SMika Westerberg 4837981c001SMika Westerberg *config = pinconf_to_config_packed(param, arg); 4847981c001SMika Westerberg return 0; 4857981c001SMika Westerberg } 4867981c001SMika Westerberg 4877981c001SMika Westerberg static int intel_config_set_pull(struct intel_pinctrl *pctrl, unsigned pin, 4887981c001SMika Westerberg unsigned long config) 4897981c001SMika Westerberg { 4907981c001SMika Westerberg unsigned param = pinconf_to_config_param(config); 4917981c001SMika Westerberg unsigned arg = pinconf_to_config_argument(config); 4927981c001SMika Westerberg void __iomem *padcfg1; 4937981c001SMika Westerberg unsigned long flags; 4947981c001SMika Westerberg int ret = 0; 4957981c001SMika Westerberg u32 value; 4967981c001SMika Westerberg 4977981c001SMika Westerberg spin_lock_irqsave(&pctrl->lock, flags); 4987981c001SMika Westerberg 4997981c001SMika Westerberg padcfg1 = intel_get_padcfg(pctrl, pin, PADCFG1); 5007981c001SMika Westerberg value = readl(padcfg1); 5017981c001SMika Westerberg 5027981c001SMika Westerberg switch (param) { 5037981c001SMika Westerberg case PIN_CONFIG_BIAS_DISABLE: 5047981c001SMika Westerberg value &= ~(PADCFG1_TERM_MASK | PADCFG1_TERM_UP); 5057981c001SMika Westerberg break; 5067981c001SMika Westerberg 5077981c001SMika Westerberg case PIN_CONFIG_BIAS_PULL_UP: 5087981c001SMika Westerberg value &= ~PADCFG1_TERM_MASK; 5097981c001SMika Westerberg 5107981c001SMika Westerberg value |= PADCFG1_TERM_UP; 5117981c001SMika Westerberg 5127981c001SMika Westerberg switch (arg) { 5137981c001SMika Westerberg case 20000: 5147981c001SMika Westerberg value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT; 5157981c001SMika Westerberg break; 5167981c001SMika Westerberg case 5000: 5177981c001SMika Westerberg value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT; 5187981c001SMika Westerberg break; 5197981c001SMika Westerberg case 2000: 5207981c001SMika Westerberg value |= PADCFG1_TERM_2K << PADCFG1_TERM_SHIFT; 5217981c001SMika Westerberg break; 5227981c001SMika Westerberg case 1000: 5237981c001SMika Westerberg value |= PADCFG1_TERM_1K << PADCFG1_TERM_SHIFT; 5247981c001SMika Westerberg break; 5257981c001SMika Westerberg default: 5267981c001SMika Westerberg ret = -EINVAL; 5277981c001SMika Westerberg } 5287981c001SMika Westerberg 5297981c001SMika Westerberg break; 5307981c001SMika Westerberg 5317981c001SMika Westerberg case PIN_CONFIG_BIAS_PULL_DOWN: 5327981c001SMika Westerberg value &= ~(PADCFG1_TERM_UP | PADCFG1_TERM_MASK); 5337981c001SMika Westerberg 5347981c001SMika Westerberg switch (arg) { 5357981c001SMika Westerberg case 20000: 5367981c001SMika Westerberg value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT; 5377981c001SMika Westerberg break; 5387981c001SMika Westerberg case 5000: 5397981c001SMika Westerberg value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT; 5407981c001SMika Westerberg break; 5417981c001SMika Westerberg default: 5427981c001SMika Westerberg ret = -EINVAL; 5437981c001SMika Westerberg } 5447981c001SMika Westerberg 5457981c001SMika Westerberg break; 5467981c001SMika Westerberg } 5477981c001SMika Westerberg 5487981c001SMika Westerberg if (!ret) 5497981c001SMika Westerberg writel(value, padcfg1); 5507981c001SMika Westerberg 5517981c001SMika Westerberg spin_unlock_irqrestore(&pctrl->lock, flags); 5527981c001SMika Westerberg 5537981c001SMika Westerberg return ret; 5547981c001SMika Westerberg } 5557981c001SMika Westerberg 5567981c001SMika Westerberg static int intel_config_set(struct pinctrl_dev *pctldev, unsigned pin, 5577981c001SMika Westerberg unsigned long *configs, unsigned nconfigs) 5587981c001SMika Westerberg { 5597981c001SMika Westerberg struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); 5607981c001SMika Westerberg int i, ret; 5617981c001SMika Westerberg 5627981c001SMika Westerberg if (!intel_pad_usable(pctrl, pin)) 5637981c001SMika Westerberg return -ENOTSUPP; 5647981c001SMika Westerberg 5657981c001SMika Westerberg for (i = 0; i < nconfigs; i++) { 5667981c001SMika Westerberg switch (pinconf_to_config_param(configs[i])) { 5677981c001SMika Westerberg case PIN_CONFIG_BIAS_DISABLE: 5687981c001SMika Westerberg case PIN_CONFIG_BIAS_PULL_UP: 5697981c001SMika Westerberg case PIN_CONFIG_BIAS_PULL_DOWN: 5707981c001SMika Westerberg ret = intel_config_set_pull(pctrl, pin, configs[i]); 5717981c001SMika Westerberg if (ret) 5727981c001SMika Westerberg return ret; 5737981c001SMika Westerberg break; 5747981c001SMika Westerberg 5757981c001SMika Westerberg default: 5767981c001SMika Westerberg return -ENOTSUPP; 5777981c001SMika Westerberg } 5787981c001SMika Westerberg } 5797981c001SMika Westerberg 5807981c001SMika Westerberg return 0; 5817981c001SMika Westerberg } 5827981c001SMika Westerberg 5837981c001SMika Westerberg static const struct pinconf_ops intel_pinconf_ops = { 5847981c001SMika Westerberg .is_generic = true, 5857981c001SMika Westerberg .pin_config_get = intel_config_get, 5867981c001SMika Westerberg .pin_config_set = intel_config_set, 5877981c001SMika Westerberg }; 5887981c001SMika Westerberg 5897981c001SMika Westerberg static const struct pinctrl_desc intel_pinctrl_desc = { 5907981c001SMika Westerberg .pctlops = &intel_pinctrl_ops, 5917981c001SMika Westerberg .pmxops = &intel_pinmux_ops, 5927981c001SMika Westerberg .confops = &intel_pinconf_ops, 5937981c001SMika Westerberg .owner = THIS_MODULE, 5947981c001SMika Westerberg }; 5957981c001SMika Westerberg 5967981c001SMika Westerberg static int intel_gpio_get(struct gpio_chip *chip, unsigned offset) 5977981c001SMika Westerberg { 598*acfd4c63SLinus Walleij struct intel_pinctrl *pctrl = gpiochip_get_data(chip); 5997981c001SMika Westerberg void __iomem *reg; 6007981c001SMika Westerberg 6017981c001SMika Westerberg reg = intel_get_padcfg(pctrl, offset, PADCFG0); 6027981c001SMika Westerberg if (!reg) 6037981c001SMika Westerberg return -EINVAL; 6047981c001SMika Westerberg 6057981c001SMika Westerberg return !!(readl(reg) & PADCFG0_GPIORXSTATE); 6067981c001SMika Westerberg } 6077981c001SMika Westerberg 6087981c001SMika Westerberg static void intel_gpio_set(struct gpio_chip *chip, unsigned offset, int value) 6097981c001SMika Westerberg { 610*acfd4c63SLinus Walleij struct intel_pinctrl *pctrl = gpiochip_get_data(chip); 6117981c001SMika Westerberg void __iomem *reg; 6127981c001SMika Westerberg 6137981c001SMika Westerberg reg = intel_get_padcfg(pctrl, offset, PADCFG0); 6147981c001SMika Westerberg if (reg) { 6157981c001SMika Westerberg unsigned long flags; 6167981c001SMika Westerberg u32 padcfg0; 6177981c001SMika Westerberg 6187981c001SMika Westerberg spin_lock_irqsave(&pctrl->lock, flags); 6197981c001SMika Westerberg padcfg0 = readl(reg); 6207981c001SMika Westerberg if (value) 6217981c001SMika Westerberg padcfg0 |= PADCFG0_GPIOTXSTATE; 6227981c001SMika Westerberg else 6237981c001SMika Westerberg padcfg0 &= ~PADCFG0_GPIOTXSTATE; 6247981c001SMika Westerberg writel(padcfg0, reg); 6257981c001SMika Westerberg spin_unlock_irqrestore(&pctrl->lock, flags); 6267981c001SMika Westerberg } 6277981c001SMika Westerberg } 6287981c001SMika Westerberg 6297981c001SMika Westerberg static int intel_gpio_direction_input(struct gpio_chip *chip, unsigned offset) 6307981c001SMika Westerberg { 6317981c001SMika Westerberg return pinctrl_gpio_direction_input(chip->base + offset); 6327981c001SMika Westerberg } 6337981c001SMika Westerberg 6347981c001SMika Westerberg static int intel_gpio_direction_output(struct gpio_chip *chip, unsigned offset, 6357981c001SMika Westerberg int value) 6367981c001SMika Westerberg { 6377981c001SMika Westerberg intel_gpio_set(chip, offset, value); 6387981c001SMika Westerberg return pinctrl_gpio_direction_output(chip->base + offset); 6397981c001SMika Westerberg } 6407981c001SMika Westerberg 6417981c001SMika Westerberg static const struct gpio_chip intel_gpio_chip = { 6427981c001SMika Westerberg .owner = THIS_MODULE, 64398c85d58SJonas Gorski .request = gpiochip_generic_request, 64498c85d58SJonas Gorski .free = gpiochip_generic_free, 6457981c001SMika Westerberg .direction_input = intel_gpio_direction_input, 6467981c001SMika Westerberg .direction_output = intel_gpio_direction_output, 6477981c001SMika Westerberg .get = intel_gpio_get, 6487981c001SMika Westerberg .set = intel_gpio_set, 6497981c001SMika Westerberg }; 6507981c001SMika Westerberg 6517981c001SMika Westerberg static void intel_gpio_irq_ack(struct irq_data *d) 6527981c001SMika Westerberg { 6537981c001SMika Westerberg struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 654*acfd4c63SLinus Walleij struct intel_pinctrl *pctrl = gpiochip_get_data(gc); 6557981c001SMika Westerberg const struct intel_community *community; 6567981c001SMika Westerberg unsigned pin = irqd_to_hwirq(d); 6577981c001SMika Westerberg 6587981c001SMika Westerberg spin_lock(&pctrl->lock); 6597981c001SMika Westerberg 6607981c001SMika Westerberg community = intel_get_community(pctrl, pin); 6617981c001SMika Westerberg if (community) { 6627981c001SMika Westerberg unsigned padno = pin_to_padno(community, pin); 663618a919bSQipeng Zha unsigned gpp_offset = padno % community->gpp_size; 664618a919bSQipeng Zha unsigned gpp = padno / community->gpp_size; 6657981c001SMika Westerberg 6667981c001SMika Westerberg writel(BIT(gpp_offset), community->regs + GPI_IS + gpp * 4); 6677981c001SMika Westerberg } 6687981c001SMika Westerberg 6697981c001SMika Westerberg spin_unlock(&pctrl->lock); 6707981c001SMika Westerberg } 6717981c001SMika Westerberg 6727981c001SMika Westerberg static void intel_gpio_irq_mask_unmask(struct irq_data *d, bool mask) 6737981c001SMika Westerberg { 6747981c001SMika Westerberg struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 675*acfd4c63SLinus Walleij struct intel_pinctrl *pctrl = gpiochip_get_data(gc); 6767981c001SMika Westerberg const struct intel_community *community; 6777981c001SMika Westerberg unsigned pin = irqd_to_hwirq(d); 6787981c001SMika Westerberg unsigned long flags; 6797981c001SMika Westerberg 6807981c001SMika Westerberg spin_lock_irqsave(&pctrl->lock, flags); 6817981c001SMika Westerberg 6827981c001SMika Westerberg community = intel_get_community(pctrl, pin); 6837981c001SMika Westerberg if (community) { 6847981c001SMika Westerberg unsigned padno = pin_to_padno(community, pin); 685618a919bSQipeng Zha unsigned gpp_offset = padno % community->gpp_size; 686618a919bSQipeng Zha unsigned gpp = padno / community->gpp_size; 6877981c001SMika Westerberg void __iomem *reg; 6887981c001SMika Westerberg u32 value; 6897981c001SMika Westerberg 6907981c001SMika Westerberg reg = community->regs + community->ie_offset + gpp * 4; 6917981c001SMika Westerberg value = readl(reg); 6927981c001SMika Westerberg if (mask) 6937981c001SMika Westerberg value &= ~BIT(gpp_offset); 6947981c001SMika Westerberg else 6957981c001SMika Westerberg value |= BIT(gpp_offset); 6967981c001SMika Westerberg writel(value, reg); 6977981c001SMika Westerberg } 6987981c001SMika Westerberg 6997981c001SMika Westerberg spin_unlock_irqrestore(&pctrl->lock, flags); 7007981c001SMika Westerberg } 7017981c001SMika Westerberg 7027981c001SMika Westerberg static void intel_gpio_irq_mask(struct irq_data *d) 7037981c001SMika Westerberg { 7047981c001SMika Westerberg intel_gpio_irq_mask_unmask(d, true); 7057981c001SMika Westerberg } 7067981c001SMika Westerberg 7077981c001SMika Westerberg static void intel_gpio_irq_unmask(struct irq_data *d) 7087981c001SMika Westerberg { 7097981c001SMika Westerberg intel_gpio_irq_mask_unmask(d, false); 7107981c001SMika Westerberg } 7117981c001SMika Westerberg 7127981c001SMika Westerberg static int intel_gpio_irq_type(struct irq_data *d, unsigned type) 7137981c001SMika Westerberg { 7147981c001SMika Westerberg struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 715*acfd4c63SLinus Walleij struct intel_pinctrl *pctrl = gpiochip_get_data(gc); 7167981c001SMika Westerberg unsigned pin = irqd_to_hwirq(d); 7177981c001SMika Westerberg unsigned long flags; 7187981c001SMika Westerberg void __iomem *reg; 7197981c001SMika Westerberg u32 value; 7207981c001SMika Westerberg 7217981c001SMika Westerberg reg = intel_get_padcfg(pctrl, pin, PADCFG0); 7227981c001SMika Westerberg if (!reg) 7237981c001SMika Westerberg return -EINVAL; 7247981c001SMika Westerberg 7254341e8a5SMika Westerberg /* 7264341e8a5SMika Westerberg * If the pin is in ACPI mode it is still usable as a GPIO but it 7274341e8a5SMika Westerberg * cannot be used as IRQ because GPI_IS status bit will not be 7284341e8a5SMika Westerberg * updated by the host controller hardware. 7294341e8a5SMika Westerberg */ 7304341e8a5SMika Westerberg if (intel_pad_acpi_mode(pctrl, pin)) { 7314341e8a5SMika Westerberg dev_warn(pctrl->dev, "pin %u cannot be used as IRQ\n", pin); 7324341e8a5SMika Westerberg return -EPERM; 7334341e8a5SMika Westerberg } 7344341e8a5SMika Westerberg 7357981c001SMika Westerberg spin_lock_irqsave(&pctrl->lock, flags); 7367981c001SMika Westerberg 7377981c001SMika Westerberg value = readl(reg); 7387981c001SMika Westerberg 7397981c001SMika Westerberg value &= ~(PADCFG0_RXEVCFG_MASK | PADCFG0_RXINV); 7407981c001SMika Westerberg 7417981c001SMika Westerberg if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) { 7427981c001SMika Westerberg value |= PADCFG0_RXEVCFG_EDGE_BOTH << PADCFG0_RXEVCFG_SHIFT; 7437981c001SMika Westerberg } else if (type & IRQ_TYPE_EDGE_FALLING) { 7447981c001SMika Westerberg value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT; 7457981c001SMika Westerberg value |= PADCFG0_RXINV; 7467981c001SMika Westerberg } else if (type & IRQ_TYPE_EDGE_RISING) { 7477981c001SMika Westerberg value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT; 7487981c001SMika Westerberg } else if (type & IRQ_TYPE_LEVEL_LOW) { 7497981c001SMika Westerberg value |= PADCFG0_RXINV; 7507981c001SMika Westerberg } else { 7517981c001SMika Westerberg value |= PADCFG0_RXEVCFG_DISABLED << PADCFG0_RXEVCFG_SHIFT; 7527981c001SMika Westerberg } 7537981c001SMika Westerberg 7547981c001SMika Westerberg writel(value, reg); 7557981c001SMika Westerberg 7567981c001SMika Westerberg if (type & IRQ_TYPE_EDGE_BOTH) 757fc756bcdSThomas Gleixner irq_set_handler_locked(d, handle_edge_irq); 7587981c001SMika Westerberg else if (type & IRQ_TYPE_LEVEL_MASK) 759fc756bcdSThomas Gleixner irq_set_handler_locked(d, handle_level_irq); 7607981c001SMika Westerberg 7617981c001SMika Westerberg spin_unlock_irqrestore(&pctrl->lock, flags); 7627981c001SMika Westerberg 7637981c001SMika Westerberg return 0; 7647981c001SMika Westerberg } 7657981c001SMika Westerberg 7667981c001SMika Westerberg static int intel_gpio_irq_wake(struct irq_data *d, unsigned int on) 7677981c001SMika Westerberg { 7687981c001SMika Westerberg struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 769*acfd4c63SLinus Walleij struct intel_pinctrl *pctrl = gpiochip_get_data(gc); 7707981c001SMika Westerberg const struct intel_community *community; 7717981c001SMika Westerberg unsigned pin = irqd_to_hwirq(d); 7727981c001SMika Westerberg unsigned padno, gpp, gpp_offset; 7737981c001SMika Westerberg u32 gpe_en; 7747981c001SMika Westerberg 7757981c001SMika Westerberg community = intel_get_community(pctrl, pin); 7767981c001SMika Westerberg if (!community) 7777981c001SMika Westerberg return -EINVAL; 7787981c001SMika Westerberg 7797981c001SMika Westerberg padno = pin_to_padno(community, pin); 780618a919bSQipeng Zha gpp = padno / community->gpp_size; 781618a919bSQipeng Zha gpp_offset = padno % community->gpp_size; 7827981c001SMika Westerberg 7837981c001SMika Westerberg /* Clear the existing wake status */ 7847981c001SMika Westerberg writel(BIT(gpp_offset), community->regs + GPI_GPE_STS + gpp * 4); 7857981c001SMika Westerberg 7867981c001SMika Westerberg /* 7877981c001SMika Westerberg * The controller will generate wake when GPE of the corresponding 7887981c001SMika Westerberg * pad is enabled and it is not routed to SCI (GPIROUTSCI is not 7897981c001SMika Westerberg * set). 7907981c001SMika Westerberg */ 7917981c001SMika Westerberg gpe_en = readl(community->regs + GPI_GPE_EN + gpp * 4); 7927981c001SMika Westerberg if (on) 7937981c001SMika Westerberg gpe_en |= BIT(gpp_offset); 7947981c001SMika Westerberg else 7957981c001SMika Westerberg gpe_en &= ~BIT(gpp_offset); 7967981c001SMika Westerberg writel(gpe_en, community->regs + GPI_GPE_EN + gpp * 4); 7977981c001SMika Westerberg 7987981c001SMika Westerberg dev_dbg(pctrl->dev, "%sable wake for pin %u\n", on ? "en" : "dis", pin); 7997981c001SMika Westerberg return 0; 8007981c001SMika Westerberg } 8017981c001SMika Westerberg 802193b40c8SMika Westerberg static irqreturn_t intel_gpio_community_irq_handler(struct intel_pinctrl *pctrl, 8037981c001SMika Westerberg const struct intel_community *community) 8047981c001SMika Westerberg { 805193b40c8SMika Westerberg struct gpio_chip *gc = &pctrl->chip; 806193b40c8SMika Westerberg irqreturn_t ret = IRQ_NONE; 8077981c001SMika Westerberg int gpp; 8087981c001SMika Westerberg 8097981c001SMika Westerberg for (gpp = 0; gpp < community->ngpps; gpp++) { 8107981c001SMika Westerberg unsigned long pending, enabled, gpp_offset; 8117981c001SMika Westerberg 8127981c001SMika Westerberg pending = readl(community->regs + GPI_IS + gpp * 4); 8137981c001SMika Westerberg enabled = readl(community->regs + community->ie_offset + 8147981c001SMika Westerberg gpp * 4); 8157981c001SMika Westerberg 8167981c001SMika Westerberg /* Only interrupts that are enabled */ 8177981c001SMika Westerberg pending &= enabled; 8187981c001SMika Westerberg 819618a919bSQipeng Zha for_each_set_bit(gpp_offset, &pending, community->gpp_size) { 8207981c001SMika Westerberg unsigned padno, irq; 8217981c001SMika Westerberg 8227981c001SMika Westerberg /* 8237981c001SMika Westerberg * The last group in community can have less pins 8247981c001SMika Westerberg * than NPADS_IN_GPP. 8257981c001SMika Westerberg */ 826618a919bSQipeng Zha padno = gpp_offset + gpp * community->gpp_size; 8277981c001SMika Westerberg if (padno >= community->npins) 8287981c001SMika Westerberg break; 8297981c001SMika Westerberg 8307981c001SMika Westerberg irq = irq_find_mapping(gc->irqdomain, 8317981c001SMika Westerberg community->pin_base + padno); 8327981c001SMika Westerberg generic_handle_irq(irq); 833193b40c8SMika Westerberg 834193b40c8SMika Westerberg ret |= IRQ_HANDLED; 8357981c001SMika Westerberg } 8367981c001SMika Westerberg } 8377981c001SMika Westerberg 838193b40c8SMika Westerberg return ret; 839193b40c8SMika Westerberg } 840193b40c8SMika Westerberg 841193b40c8SMika Westerberg static irqreturn_t intel_gpio_irq(int irq, void *data) 8427981c001SMika Westerberg { 843193b40c8SMika Westerberg const struct intel_community *community; 844193b40c8SMika Westerberg struct intel_pinctrl *pctrl = data; 845193b40c8SMika Westerberg irqreturn_t ret = IRQ_NONE; 8467981c001SMika Westerberg int i; 8477981c001SMika Westerberg 8487981c001SMika Westerberg /* Need to check all communities for pending interrupts */ 849193b40c8SMika Westerberg for (i = 0; i < pctrl->ncommunities; i++) { 850193b40c8SMika Westerberg community = &pctrl->communities[i]; 851193b40c8SMika Westerberg ret |= intel_gpio_community_irq_handler(pctrl, community); 852193b40c8SMika Westerberg } 8537981c001SMika Westerberg 854193b40c8SMika Westerberg return ret; 8557981c001SMika Westerberg } 8567981c001SMika Westerberg 8577981c001SMika Westerberg static struct irq_chip intel_gpio_irqchip = { 8587981c001SMika Westerberg .name = "intel-gpio", 8597981c001SMika Westerberg .irq_ack = intel_gpio_irq_ack, 8607981c001SMika Westerberg .irq_mask = intel_gpio_irq_mask, 8617981c001SMika Westerberg .irq_unmask = intel_gpio_irq_unmask, 8627981c001SMika Westerberg .irq_set_type = intel_gpio_irq_type, 8637981c001SMika Westerberg .irq_set_wake = intel_gpio_irq_wake, 8647981c001SMika Westerberg }; 8657981c001SMika Westerberg 8667981c001SMika Westerberg static int intel_gpio_probe(struct intel_pinctrl *pctrl, int irq) 8677981c001SMika Westerberg { 8687981c001SMika Westerberg int ret; 8697981c001SMika Westerberg 8707981c001SMika Westerberg pctrl->chip = intel_gpio_chip; 8717981c001SMika Westerberg 8727981c001SMika Westerberg pctrl->chip.ngpio = pctrl->soc->npins; 8737981c001SMika Westerberg pctrl->chip.label = dev_name(pctrl->dev); 87458383c78SLinus Walleij pctrl->chip.parent = pctrl->dev; 8757981c001SMika Westerberg pctrl->chip.base = -1; 8767981c001SMika Westerberg 877*acfd4c63SLinus Walleij ret = gpiochip_add_data(&pctrl->chip, pctrl); 8787981c001SMika Westerberg if (ret) { 8797981c001SMika Westerberg dev_err(pctrl->dev, "failed to register gpiochip\n"); 8807981c001SMika Westerberg return ret; 8817981c001SMika Westerberg } 8827981c001SMika Westerberg 8837981c001SMika Westerberg ret = gpiochip_add_pin_range(&pctrl->chip, dev_name(pctrl->dev), 8847981c001SMika Westerberg 0, 0, pctrl->soc->npins); 8857981c001SMika Westerberg if (ret) { 8867981c001SMika Westerberg dev_err(pctrl->dev, "failed to add GPIO pin range\n"); 887193b40c8SMika Westerberg goto fail; 888193b40c8SMika Westerberg } 889193b40c8SMika Westerberg 890193b40c8SMika Westerberg /* 891193b40c8SMika Westerberg * We need to request the interrupt here (instead of providing chip 892193b40c8SMika Westerberg * to the irq directly) because on some platforms several GPIO 893193b40c8SMika Westerberg * controllers share the same interrupt line. 894193b40c8SMika Westerberg */ 895193b40c8SMika Westerberg ret = devm_request_irq(pctrl->dev, irq, intel_gpio_irq, IRQF_SHARED, 896193b40c8SMika Westerberg dev_name(pctrl->dev), pctrl); 897193b40c8SMika Westerberg if (ret) { 898193b40c8SMika Westerberg dev_err(pctrl->dev, "failed to request interrupt\n"); 899193b40c8SMika Westerberg goto fail; 9007981c001SMika Westerberg } 9017981c001SMika Westerberg 9027981c001SMika Westerberg ret = gpiochip_irqchip_add(&pctrl->chip, &intel_gpio_irqchip, 0, 9037981c001SMika Westerberg handle_simple_irq, IRQ_TYPE_NONE); 9047981c001SMika Westerberg if (ret) { 9057981c001SMika Westerberg dev_err(pctrl->dev, "failed to add irqchip\n"); 906193b40c8SMika Westerberg goto fail; 9077981c001SMika Westerberg } 9087981c001SMika Westerberg 9097981c001SMika Westerberg gpiochip_set_chained_irqchip(&pctrl->chip, &intel_gpio_irqchip, irq, 910193b40c8SMika Westerberg NULL); 9117981c001SMika Westerberg return 0; 912193b40c8SMika Westerberg 913193b40c8SMika Westerberg fail: 914193b40c8SMika Westerberg gpiochip_remove(&pctrl->chip); 915193b40c8SMika Westerberg 916193b40c8SMika Westerberg return ret; 9177981c001SMika Westerberg } 9187981c001SMika Westerberg 9197981c001SMika Westerberg static int intel_pinctrl_pm_init(struct intel_pinctrl *pctrl) 9207981c001SMika Westerberg { 9217981c001SMika Westerberg #ifdef CONFIG_PM_SLEEP 9227981c001SMika Westerberg const struct intel_pinctrl_soc_data *soc = pctrl->soc; 9237981c001SMika Westerberg struct intel_community_context *communities; 9247981c001SMika Westerberg struct intel_pad_context *pads; 9257981c001SMika Westerberg int i; 9267981c001SMika Westerberg 9277981c001SMika Westerberg pads = devm_kcalloc(pctrl->dev, soc->npins, sizeof(*pads), GFP_KERNEL); 9287981c001SMika Westerberg if (!pads) 9297981c001SMika Westerberg return -ENOMEM; 9307981c001SMika Westerberg 9317981c001SMika Westerberg communities = devm_kcalloc(pctrl->dev, pctrl->ncommunities, 9327981c001SMika Westerberg sizeof(*communities), GFP_KERNEL); 9337981c001SMika Westerberg if (!communities) 9347981c001SMika Westerberg return -ENOMEM; 9357981c001SMika Westerberg 9367981c001SMika Westerberg 9377981c001SMika Westerberg for (i = 0; i < pctrl->ncommunities; i++) { 9387981c001SMika Westerberg struct intel_community *community = &pctrl->communities[i]; 9397981c001SMika Westerberg u32 *intmask; 9407981c001SMika Westerberg 9417981c001SMika Westerberg intmask = devm_kcalloc(pctrl->dev, community->ngpps, 9427981c001SMika Westerberg sizeof(*intmask), GFP_KERNEL); 9437981c001SMika Westerberg if (!intmask) 9447981c001SMika Westerberg return -ENOMEM; 9457981c001SMika Westerberg 9467981c001SMika Westerberg communities[i].intmask = intmask; 9477981c001SMika Westerberg } 9487981c001SMika Westerberg 9497981c001SMika Westerberg pctrl->context.pads = pads; 9507981c001SMika Westerberg pctrl->context.communities = communities; 9517981c001SMika Westerberg #endif 9527981c001SMika Westerberg 9537981c001SMika Westerberg return 0; 9547981c001SMika Westerberg } 9557981c001SMika Westerberg 9567981c001SMika Westerberg int intel_pinctrl_probe(struct platform_device *pdev, 9577981c001SMika Westerberg const struct intel_pinctrl_soc_data *soc_data) 9587981c001SMika Westerberg { 9597981c001SMika Westerberg struct intel_pinctrl *pctrl; 9607981c001SMika Westerberg int i, ret, irq; 9617981c001SMika Westerberg 9627981c001SMika Westerberg if (!soc_data) 9637981c001SMika Westerberg return -EINVAL; 9647981c001SMika Westerberg 9657981c001SMika Westerberg pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL); 9667981c001SMika Westerberg if (!pctrl) 9677981c001SMika Westerberg return -ENOMEM; 9687981c001SMika Westerberg 9697981c001SMika Westerberg pctrl->dev = &pdev->dev; 9707981c001SMika Westerberg pctrl->soc = soc_data; 9717981c001SMika Westerberg spin_lock_init(&pctrl->lock); 9727981c001SMika Westerberg 9737981c001SMika Westerberg /* 9747981c001SMika Westerberg * Make a copy of the communities which we can use to hold pointers 9757981c001SMika Westerberg * to the registers. 9767981c001SMika Westerberg */ 9777981c001SMika Westerberg pctrl->ncommunities = pctrl->soc->ncommunities; 9787981c001SMika Westerberg pctrl->communities = devm_kcalloc(&pdev->dev, pctrl->ncommunities, 9797981c001SMika Westerberg sizeof(*pctrl->communities), GFP_KERNEL); 9807981c001SMika Westerberg if (!pctrl->communities) 9817981c001SMika Westerberg return -ENOMEM; 9827981c001SMika Westerberg 9837981c001SMika Westerberg for (i = 0; i < pctrl->ncommunities; i++) { 9847981c001SMika Westerberg struct intel_community *community = &pctrl->communities[i]; 9857981c001SMika Westerberg struct resource *res; 9867981c001SMika Westerberg void __iomem *regs; 9877981c001SMika Westerberg u32 padbar; 9887981c001SMika Westerberg 9897981c001SMika Westerberg *community = pctrl->soc->communities[i]; 9907981c001SMika Westerberg 9917981c001SMika Westerberg res = platform_get_resource(pdev, IORESOURCE_MEM, 9927981c001SMika Westerberg community->barno); 9937981c001SMika Westerberg regs = devm_ioremap_resource(&pdev->dev, res); 9947981c001SMika Westerberg if (IS_ERR(regs)) 9957981c001SMika Westerberg return PTR_ERR(regs); 9967981c001SMika Westerberg 9977981c001SMika Westerberg /* Read offset of the pad configuration registers */ 9987981c001SMika Westerberg padbar = readl(regs + PADBAR); 9997981c001SMika Westerberg 10007981c001SMika Westerberg community->regs = regs; 10017981c001SMika Westerberg community->pad_regs = regs + padbar; 1002618a919bSQipeng Zha community->ngpps = DIV_ROUND_UP(community->npins, 1003618a919bSQipeng Zha community->gpp_size); 10047981c001SMika Westerberg } 10057981c001SMika Westerberg 10067981c001SMika Westerberg irq = platform_get_irq(pdev, 0); 10077981c001SMika Westerberg if (irq < 0) { 10087981c001SMika Westerberg dev_err(&pdev->dev, "failed to get interrupt number\n"); 10097981c001SMika Westerberg return irq; 10107981c001SMika Westerberg } 10117981c001SMika Westerberg 10127981c001SMika Westerberg ret = intel_pinctrl_pm_init(pctrl); 10137981c001SMika Westerberg if (ret) 10147981c001SMika Westerberg return ret; 10157981c001SMika Westerberg 10167981c001SMika Westerberg pctrl->pctldesc = intel_pinctrl_desc; 10177981c001SMika Westerberg pctrl->pctldesc.name = dev_name(&pdev->dev); 10187981c001SMika Westerberg pctrl->pctldesc.pins = pctrl->soc->pins; 10197981c001SMika Westerberg pctrl->pctldesc.npins = pctrl->soc->npins; 10207981c001SMika Westerberg 10217981c001SMika Westerberg pctrl->pctldev = pinctrl_register(&pctrl->pctldesc, &pdev->dev, pctrl); 1022323de9efSMasahiro Yamada if (IS_ERR(pctrl->pctldev)) { 10237981c001SMika Westerberg dev_err(&pdev->dev, "failed to register pinctrl driver\n"); 1024323de9efSMasahiro Yamada return PTR_ERR(pctrl->pctldev); 10257981c001SMika Westerberg } 10267981c001SMika Westerberg 10277981c001SMika Westerberg ret = intel_gpio_probe(pctrl, irq); 10287981c001SMika Westerberg if (ret) { 10297981c001SMika Westerberg pinctrl_unregister(pctrl->pctldev); 10307981c001SMika Westerberg return ret; 10317981c001SMika Westerberg } 10327981c001SMika Westerberg 10337981c001SMika Westerberg platform_set_drvdata(pdev, pctrl); 10347981c001SMika Westerberg 10357981c001SMika Westerberg return 0; 10367981c001SMika Westerberg } 10377981c001SMika Westerberg EXPORT_SYMBOL_GPL(intel_pinctrl_probe); 10387981c001SMika Westerberg 10397981c001SMika Westerberg int intel_pinctrl_remove(struct platform_device *pdev) 10407981c001SMika Westerberg { 10417981c001SMika Westerberg struct intel_pinctrl *pctrl = platform_get_drvdata(pdev); 10427981c001SMika Westerberg 10437981c001SMika Westerberg gpiochip_remove(&pctrl->chip); 10447981c001SMika Westerberg pinctrl_unregister(pctrl->pctldev); 10457981c001SMika Westerberg 10467981c001SMika Westerberg return 0; 10477981c001SMika Westerberg } 10487981c001SMika Westerberg EXPORT_SYMBOL_GPL(intel_pinctrl_remove); 10497981c001SMika Westerberg 10507981c001SMika Westerberg #ifdef CONFIG_PM_SLEEP 10517981c001SMika Westerberg int intel_pinctrl_suspend(struct device *dev) 10527981c001SMika Westerberg { 10537981c001SMika Westerberg struct platform_device *pdev = to_platform_device(dev); 10547981c001SMika Westerberg struct intel_pinctrl *pctrl = platform_get_drvdata(pdev); 10557981c001SMika Westerberg struct intel_community_context *communities; 10567981c001SMika Westerberg struct intel_pad_context *pads; 10577981c001SMika Westerberg int i; 10587981c001SMika Westerberg 10597981c001SMika Westerberg pads = pctrl->context.pads; 10607981c001SMika Westerberg for (i = 0; i < pctrl->soc->npins; i++) { 10617981c001SMika Westerberg const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i]; 10627981c001SMika Westerberg u32 val; 10637981c001SMika Westerberg 10647981c001SMika Westerberg if (!intel_pad_usable(pctrl, desc->number)) 10657981c001SMika Westerberg continue; 10667981c001SMika Westerberg 10677981c001SMika Westerberg val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG0)); 10687981c001SMika Westerberg pads[i].padcfg0 = val & ~PADCFG0_GPIORXSTATE; 10697981c001SMika Westerberg val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG1)); 10707981c001SMika Westerberg pads[i].padcfg1 = val; 10717981c001SMika Westerberg } 10727981c001SMika Westerberg 10737981c001SMika Westerberg communities = pctrl->context.communities; 10747981c001SMika Westerberg for (i = 0; i < pctrl->ncommunities; i++) { 10757981c001SMika Westerberg struct intel_community *community = &pctrl->communities[i]; 10767981c001SMika Westerberg void __iomem *base; 10777981c001SMika Westerberg unsigned gpp; 10787981c001SMika Westerberg 10797981c001SMika Westerberg base = community->regs + community->ie_offset; 10807981c001SMika Westerberg for (gpp = 0; gpp < community->ngpps; gpp++) 10817981c001SMika Westerberg communities[i].intmask[gpp] = readl(base + gpp * 4); 10827981c001SMika Westerberg } 10837981c001SMika Westerberg 10847981c001SMika Westerberg return 0; 10857981c001SMika Westerberg } 10867981c001SMika Westerberg EXPORT_SYMBOL_GPL(intel_pinctrl_suspend); 10877981c001SMika Westerberg 1088f487bbf3SMika Westerberg static void intel_gpio_irq_init(struct intel_pinctrl *pctrl) 1089f487bbf3SMika Westerberg { 1090f487bbf3SMika Westerberg size_t i; 1091f487bbf3SMika Westerberg 1092f487bbf3SMika Westerberg for (i = 0; i < pctrl->ncommunities; i++) { 1093f487bbf3SMika Westerberg const struct intel_community *community; 1094f487bbf3SMika Westerberg void __iomem *base; 1095f487bbf3SMika Westerberg unsigned gpp; 1096f487bbf3SMika Westerberg 1097f487bbf3SMika Westerberg community = &pctrl->communities[i]; 1098f487bbf3SMika Westerberg base = community->regs; 1099f487bbf3SMika Westerberg 1100f487bbf3SMika Westerberg for (gpp = 0; gpp < community->ngpps; gpp++) { 1101f487bbf3SMika Westerberg /* Mask and clear all interrupts */ 1102f487bbf3SMika Westerberg writel(0, base + community->ie_offset + gpp * 4); 1103f487bbf3SMika Westerberg writel(0xffff, base + GPI_IS + gpp * 4); 1104f487bbf3SMika Westerberg } 1105f487bbf3SMika Westerberg } 1106f487bbf3SMika Westerberg } 1107f487bbf3SMika Westerberg 11087981c001SMika Westerberg int intel_pinctrl_resume(struct device *dev) 11097981c001SMika Westerberg { 11107981c001SMika Westerberg struct platform_device *pdev = to_platform_device(dev); 11117981c001SMika Westerberg struct intel_pinctrl *pctrl = platform_get_drvdata(pdev); 11127981c001SMika Westerberg const struct intel_community_context *communities; 11137981c001SMika Westerberg const struct intel_pad_context *pads; 11147981c001SMika Westerberg int i; 11157981c001SMika Westerberg 11167981c001SMika Westerberg /* Mask all interrupts */ 11177981c001SMika Westerberg intel_gpio_irq_init(pctrl); 11187981c001SMika Westerberg 11197981c001SMika Westerberg pads = pctrl->context.pads; 11207981c001SMika Westerberg for (i = 0; i < pctrl->soc->npins; i++) { 11217981c001SMika Westerberg const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i]; 11227981c001SMika Westerberg void __iomem *padcfg; 11237981c001SMika Westerberg u32 val; 11247981c001SMika Westerberg 11257981c001SMika Westerberg if (!intel_pad_usable(pctrl, desc->number)) 11267981c001SMika Westerberg continue; 11277981c001SMika Westerberg 11287981c001SMika Westerberg padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG0); 11297981c001SMika Westerberg val = readl(padcfg) & ~PADCFG0_GPIORXSTATE; 11307981c001SMika Westerberg if (val != pads[i].padcfg0) { 11317981c001SMika Westerberg writel(pads[i].padcfg0, padcfg); 11327981c001SMika Westerberg dev_dbg(dev, "restored pin %u padcfg0 %#08x\n", 11337981c001SMika Westerberg desc->number, readl(padcfg)); 11347981c001SMika Westerberg } 11357981c001SMika Westerberg 11367981c001SMika Westerberg padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG1); 11377981c001SMika Westerberg val = readl(padcfg); 11387981c001SMika Westerberg if (val != pads[i].padcfg1) { 11397981c001SMika Westerberg writel(pads[i].padcfg1, padcfg); 11407981c001SMika Westerberg dev_dbg(dev, "restored pin %u padcfg1 %#08x\n", 11417981c001SMika Westerberg desc->number, readl(padcfg)); 11427981c001SMika Westerberg } 11437981c001SMika Westerberg } 11447981c001SMika Westerberg 11457981c001SMika Westerberg communities = pctrl->context.communities; 11467981c001SMika Westerberg for (i = 0; i < pctrl->ncommunities; i++) { 11477981c001SMika Westerberg struct intel_community *community = &pctrl->communities[i]; 11487981c001SMika Westerberg void __iomem *base; 11497981c001SMika Westerberg unsigned gpp; 11507981c001SMika Westerberg 11517981c001SMika Westerberg base = community->regs + community->ie_offset; 11527981c001SMika Westerberg for (gpp = 0; gpp < community->ngpps; gpp++) { 11537981c001SMika Westerberg writel(communities[i].intmask[gpp], base + gpp * 4); 11547981c001SMika Westerberg dev_dbg(dev, "restored mask %d/%u %#08x\n", i, gpp, 11557981c001SMika Westerberg readl(base + gpp * 4)); 11567981c001SMika Westerberg } 11577981c001SMika Westerberg } 11587981c001SMika Westerberg 11597981c001SMika Westerberg return 0; 11607981c001SMika Westerberg } 11617981c001SMika Westerberg EXPORT_SYMBOL_GPL(intel_pinctrl_resume); 11627981c001SMika Westerberg #endif 11637981c001SMika Westerberg 11647981c001SMika Westerberg MODULE_AUTHOR("Mathias Nyman <mathias.nyman@linux.intel.com>"); 11657981c001SMika Westerberg MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>"); 11667981c001SMika Westerberg MODULE_DESCRIPTION("Intel pinctrl/GPIO core driver"); 11677981c001SMika Westerberg MODULE_LICENSE("GPL v2"); 1168