Andy Shevchenko | 875a92b | 2018-06-29 15:36:34 +0300 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 2 | /* |
| 3 | * Intel pinctrl/GPIO core driver. |
| 4 | * |
| 5 | * Copyright (C) 2015, Intel Corporation |
| 6 | * Authors: Mathias Nyman <mathias.nyman@linux.intel.com> |
| 7 | * Mika Westerberg <mika.westerberg@linux.intel.com> |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 8 | */ |
| 9 | |
Andy Shevchenko | 924cf80 | 2018-08-30 19:27:36 +0300 | [diff] [blame] | 10 | #include <linux/acpi.h> |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 11 | #include <linux/module.h> |
Mika Westerberg | 193b40c | 2015-10-21 13:08:43 +0300 | [diff] [blame] | 12 | #include <linux/interrupt.h> |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 13 | #include <linux/gpio/driver.h> |
Mika Westerberg | e57725e | 2017-01-27 13:07:14 +0300 | [diff] [blame] | 14 | #include <linux/log2.h> |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 15 | #include <linux/platform_device.h> |
Andy Shevchenko | 924cf80 | 2018-08-30 19:27:36 +0300 | [diff] [blame] | 16 | #include <linux/property.h> |
| 17 | |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 18 | #include <linux/pinctrl/pinctrl.h> |
| 19 | #include <linux/pinctrl/pinmux.h> |
| 20 | #include <linux/pinctrl/pinconf.h> |
| 21 | #include <linux/pinctrl/pinconf-generic.h> |
| 22 | |
Mika Westerberg | c538b94 | 2016-10-10 16:39:31 +0300 | [diff] [blame] | 23 | #include "../core.h" |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 24 | #include "pinctrl-intel.h" |
| 25 | |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 26 | /* Offset from regs */ |
Mika Westerberg | e57725e | 2017-01-27 13:07:14 +0300 | [diff] [blame] | 27 | #define REVID 0x000 |
| 28 | #define REVID_SHIFT 16 |
| 29 | #define REVID_MASK GENMASK(31, 16) |
| 30 | |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 31 | #define PADBAR 0x00c |
| 32 | #define GPI_IS 0x100 |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 33 | |
| 34 | #define PADOWN_BITS 4 |
| 35 | #define PADOWN_SHIFT(p) ((p) % 8 * PADOWN_BITS) |
| 36 | #define PADOWN_MASK(p) (0xf << PADOWN_SHIFT(p)) |
Qipeng Zha | 99a735b | 2015-11-30 19:20:16 +0800 | [diff] [blame] | 37 | #define PADOWN_GPP(p) ((p) / 8) |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 38 | |
| 39 | /* Offset from pad_regs */ |
| 40 | #define PADCFG0 0x000 |
| 41 | #define PADCFG0_RXEVCFG_SHIFT 25 |
| 42 | #define PADCFG0_RXEVCFG_MASK (3 << PADCFG0_RXEVCFG_SHIFT) |
| 43 | #define PADCFG0_RXEVCFG_LEVEL 0 |
| 44 | #define PADCFG0_RXEVCFG_EDGE 1 |
| 45 | #define PADCFG0_RXEVCFG_DISABLED 2 |
| 46 | #define PADCFG0_RXEVCFG_EDGE_BOTH 3 |
Mika Westerberg | e57725e | 2017-01-27 13:07:14 +0300 | [diff] [blame] | 47 | #define PADCFG0_PREGFRXSEL BIT(24) |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 48 | #define PADCFG0_RXINV BIT(23) |
| 49 | #define PADCFG0_GPIROUTIOXAPIC BIT(20) |
| 50 | #define PADCFG0_GPIROUTSCI BIT(19) |
| 51 | #define PADCFG0_GPIROUTSMI BIT(18) |
| 52 | #define PADCFG0_GPIROUTNMI BIT(17) |
| 53 | #define PADCFG0_PMODE_SHIFT 10 |
| 54 | #define PADCFG0_PMODE_MASK (0xf << PADCFG0_PMODE_SHIFT) |
| 55 | #define PADCFG0_GPIORXDIS BIT(9) |
| 56 | #define PADCFG0_GPIOTXDIS BIT(8) |
| 57 | #define PADCFG0_GPIORXSTATE BIT(1) |
| 58 | #define PADCFG0_GPIOTXSTATE BIT(0) |
| 59 | |
| 60 | #define PADCFG1 0x004 |
| 61 | #define PADCFG1_TERM_UP BIT(13) |
| 62 | #define PADCFG1_TERM_SHIFT 10 |
| 63 | #define PADCFG1_TERM_MASK (7 << PADCFG1_TERM_SHIFT) |
| 64 | #define PADCFG1_TERM_20K 4 |
| 65 | #define PADCFG1_TERM_2K 3 |
| 66 | #define PADCFG1_TERM_5K 2 |
| 67 | #define PADCFG1_TERM_1K 1 |
| 68 | |
Mika Westerberg | e57725e | 2017-01-27 13:07:14 +0300 | [diff] [blame] | 69 | #define PADCFG2 0x008 |
| 70 | #define PADCFG2_DEBEN BIT(0) |
| 71 | #define PADCFG2_DEBOUNCE_SHIFT 1 |
| 72 | #define PADCFG2_DEBOUNCE_MASK GENMASK(4, 1) |
| 73 | |
| 74 | #define DEBOUNCE_PERIOD 31250 /* ns */ |
| 75 | |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 76 | struct intel_pad_context { |
| 77 | u32 padcfg0; |
| 78 | u32 padcfg1; |
Mika Westerberg | e57725e | 2017-01-27 13:07:14 +0300 | [diff] [blame] | 79 | u32 padcfg2; |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | struct intel_community_context { |
| 83 | u32 *intmask; |
Chris Chiu | a0a5f76 | 2019-04-15 13:53:58 +0800 | [diff] [blame] | 84 | u32 *hostown; |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | struct intel_pinctrl_context { |
| 88 | struct intel_pad_context *pads; |
| 89 | struct intel_community_context *communities; |
| 90 | }; |
| 91 | |
| 92 | /** |
| 93 | * struct intel_pinctrl - Intel pinctrl private structure |
| 94 | * @dev: Pointer to the device structure |
| 95 | * @lock: Lock to serialize register access |
| 96 | * @pctldesc: Pin controller description |
| 97 | * @pctldev: Pointer to the pin controller device |
| 98 | * @chip: GPIO chip in this pin controller |
| 99 | * @soc: SoC/PCH specific pin configuration data |
| 100 | * @communities: All communities in this pin controller |
| 101 | * @ncommunities: Number of communities in this pin controller |
| 102 | * @context: Configuration saved over system sleep |
Nilesh Bacchewar | 01dabe9 | 2016-09-21 16:35:23 -0700 | [diff] [blame] | 103 | * @irq: pinctrl/GPIO chip irq number |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 104 | */ |
| 105 | struct intel_pinctrl { |
| 106 | struct device *dev; |
Mika Westerberg | 27d9098 | 2016-06-16 11:25:36 +0300 | [diff] [blame] | 107 | raw_spinlock_t lock; |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 108 | struct pinctrl_desc pctldesc; |
| 109 | struct pinctrl_dev *pctldev; |
| 110 | struct gpio_chip chip; |
| 111 | const struct intel_pinctrl_soc_data *soc; |
| 112 | struct intel_community *communities; |
| 113 | size_t ncommunities; |
| 114 | struct intel_pinctrl_context context; |
Nilesh Bacchewar | 01dabe9 | 2016-09-21 16:35:23 -0700 | [diff] [blame] | 115 | int irq; |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 116 | }; |
| 117 | |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 118 | #define pin_to_padno(c, p) ((p) - (c)->pin_base) |
Mika Westerberg | 919eb47 | 2017-06-06 16:18:17 +0300 | [diff] [blame] | 119 | #define padgroup_offset(g, p) ((p) - (g)->base) |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 120 | |
| 121 | static struct intel_community *intel_get_community(struct intel_pinctrl *pctrl, |
Andy Shevchenko | 04035f7 | 2018-09-26 17:50:26 +0300 | [diff] [blame] | 122 | unsigned int pin) |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 123 | { |
| 124 | struct intel_community *community; |
| 125 | int i; |
| 126 | |
| 127 | for (i = 0; i < pctrl->ncommunities; i++) { |
| 128 | community = &pctrl->communities[i]; |
| 129 | if (pin >= community->pin_base && |
| 130 | pin < community->pin_base + community->npins) |
| 131 | return community; |
| 132 | } |
| 133 | |
| 134 | dev_warn(pctrl->dev, "failed to find community for pin %u\n", pin); |
| 135 | return NULL; |
| 136 | } |
| 137 | |
Mika Westerberg | 919eb47 | 2017-06-06 16:18:17 +0300 | [diff] [blame] | 138 | static const struct intel_padgroup * |
| 139 | intel_community_get_padgroup(const struct intel_community *community, |
Andy Shevchenko | 04035f7 | 2018-09-26 17:50:26 +0300 | [diff] [blame] | 140 | unsigned int pin) |
Mika Westerberg | 919eb47 | 2017-06-06 16:18:17 +0300 | [diff] [blame] | 141 | { |
| 142 | int i; |
| 143 | |
| 144 | for (i = 0; i < community->ngpps; i++) { |
| 145 | const struct intel_padgroup *padgrp = &community->gpps[i]; |
| 146 | |
| 147 | if (pin >= padgrp->base && pin < padgrp->base + padgrp->size) |
| 148 | return padgrp; |
| 149 | } |
| 150 | |
| 151 | return NULL; |
| 152 | } |
| 153 | |
Andy Shevchenko | 04035f7 | 2018-09-26 17:50:26 +0300 | [diff] [blame] | 154 | static void __iomem *intel_get_padcfg(struct intel_pinctrl *pctrl, |
| 155 | unsigned int pin, unsigned int reg) |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 156 | { |
| 157 | const struct intel_community *community; |
Andy Shevchenko | 04035f7 | 2018-09-26 17:50:26 +0300 | [diff] [blame] | 158 | unsigned int padno; |
Mika Westerberg | e57725e | 2017-01-27 13:07:14 +0300 | [diff] [blame] | 159 | size_t nregs; |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 160 | |
| 161 | community = intel_get_community(pctrl, pin); |
| 162 | if (!community) |
| 163 | return NULL; |
| 164 | |
| 165 | padno = pin_to_padno(community, pin); |
Mika Westerberg | e57725e | 2017-01-27 13:07:14 +0300 | [diff] [blame] | 166 | nregs = (community->features & PINCTRL_FEATURE_DEBOUNCE) ? 4 : 2; |
| 167 | |
| 168 | if (reg == PADCFG2 && !(community->features & PINCTRL_FEATURE_DEBOUNCE)) |
| 169 | return NULL; |
| 170 | |
| 171 | return community->pad_regs + reg + padno * nregs * 4; |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 172 | } |
| 173 | |
Andy Shevchenko | 04035f7 | 2018-09-26 17:50:26 +0300 | [diff] [blame] | 174 | static bool intel_pad_owned_by_host(struct intel_pinctrl *pctrl, unsigned int pin) |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 175 | { |
| 176 | const struct intel_community *community; |
Mika Westerberg | 919eb47 | 2017-06-06 16:18:17 +0300 | [diff] [blame] | 177 | const struct intel_padgroup *padgrp; |
Andy Shevchenko | 04035f7 | 2018-09-26 17:50:26 +0300 | [diff] [blame] | 178 | unsigned int gpp, offset, gpp_offset; |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 179 | void __iomem *padown; |
| 180 | |
| 181 | community = intel_get_community(pctrl, pin); |
| 182 | if (!community) |
| 183 | return false; |
| 184 | if (!community->padown_offset) |
| 185 | return true; |
| 186 | |
Mika Westerberg | 919eb47 | 2017-06-06 16:18:17 +0300 | [diff] [blame] | 187 | padgrp = intel_community_get_padgroup(community, pin); |
| 188 | if (!padgrp) |
| 189 | return false; |
| 190 | |
| 191 | gpp_offset = padgroup_offset(padgrp, pin); |
| 192 | gpp = PADOWN_GPP(gpp_offset); |
| 193 | offset = community->padown_offset + padgrp->padown_num * 4 + gpp * 4; |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 194 | padown = community->regs + offset; |
| 195 | |
Mika Westerberg | 919eb47 | 2017-06-06 16:18:17 +0300 | [diff] [blame] | 196 | return !(readl(padown) & PADOWN_MASK(gpp_offset)); |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 197 | } |
| 198 | |
Andy Shevchenko | 04035f7 | 2018-09-26 17:50:26 +0300 | [diff] [blame] | 199 | static bool intel_pad_acpi_mode(struct intel_pinctrl *pctrl, unsigned int pin) |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 200 | { |
| 201 | const struct intel_community *community; |
Mika Westerberg | 919eb47 | 2017-06-06 16:18:17 +0300 | [diff] [blame] | 202 | const struct intel_padgroup *padgrp; |
Andy Shevchenko | 04035f7 | 2018-09-26 17:50:26 +0300 | [diff] [blame] | 203 | unsigned int offset, gpp_offset; |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 204 | void __iomem *hostown; |
| 205 | |
| 206 | community = intel_get_community(pctrl, pin); |
| 207 | if (!community) |
| 208 | return true; |
| 209 | if (!community->hostown_offset) |
| 210 | return false; |
| 211 | |
Mika Westerberg | 919eb47 | 2017-06-06 16:18:17 +0300 | [diff] [blame] | 212 | padgrp = intel_community_get_padgroup(community, pin); |
| 213 | if (!padgrp) |
| 214 | return true; |
| 215 | |
| 216 | gpp_offset = padgroup_offset(padgrp, pin); |
| 217 | offset = community->hostown_offset + padgrp->reg_num * 4; |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 218 | hostown = community->regs + offset; |
| 219 | |
Mika Westerberg | 919eb47 | 2017-06-06 16:18:17 +0300 | [diff] [blame] | 220 | return !(readl(hostown) & BIT(gpp_offset)); |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 221 | } |
| 222 | |
Andy Shevchenko | 04035f7 | 2018-09-26 17:50:26 +0300 | [diff] [blame] | 223 | static bool intel_pad_locked(struct intel_pinctrl *pctrl, unsigned int pin) |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 224 | { |
| 225 | struct intel_community *community; |
Mika Westerberg | 919eb47 | 2017-06-06 16:18:17 +0300 | [diff] [blame] | 226 | const struct intel_padgroup *padgrp; |
Andy Shevchenko | 04035f7 | 2018-09-26 17:50:26 +0300 | [diff] [blame] | 227 | unsigned int offset, gpp_offset; |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 228 | u32 value; |
| 229 | |
| 230 | community = intel_get_community(pctrl, pin); |
| 231 | if (!community) |
| 232 | return true; |
| 233 | if (!community->padcfglock_offset) |
| 234 | return false; |
| 235 | |
Mika Westerberg | 919eb47 | 2017-06-06 16:18:17 +0300 | [diff] [blame] | 236 | padgrp = intel_community_get_padgroup(community, pin); |
| 237 | if (!padgrp) |
| 238 | return true; |
| 239 | |
| 240 | gpp_offset = padgroup_offset(padgrp, pin); |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 241 | |
| 242 | /* |
| 243 | * If PADCFGLOCK and PADCFGLOCKTX bits are both clear for this pad, |
| 244 | * the pad is considered unlocked. Any other case means that it is |
| 245 | * either fully or partially locked and we don't touch it. |
| 246 | */ |
Mika Westerberg | 919eb47 | 2017-06-06 16:18:17 +0300 | [diff] [blame] | 247 | offset = community->padcfglock_offset + padgrp->reg_num * 8; |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 248 | value = readl(community->regs + offset); |
Mika Westerberg | 919eb47 | 2017-06-06 16:18:17 +0300 | [diff] [blame] | 249 | if (value & BIT(gpp_offset)) |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 250 | return true; |
| 251 | |
Mika Westerberg | 919eb47 | 2017-06-06 16:18:17 +0300 | [diff] [blame] | 252 | offset = community->padcfglock_offset + 4 + padgrp->reg_num * 8; |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 253 | value = readl(community->regs + offset); |
Mika Westerberg | 919eb47 | 2017-06-06 16:18:17 +0300 | [diff] [blame] | 254 | if (value & BIT(gpp_offset)) |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 255 | return true; |
| 256 | |
| 257 | return false; |
| 258 | } |
| 259 | |
Andy Shevchenko | 04035f7 | 2018-09-26 17:50:26 +0300 | [diff] [blame] | 260 | static bool intel_pad_usable(struct intel_pinctrl *pctrl, unsigned int pin) |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 261 | { |
| 262 | return intel_pad_owned_by_host(pctrl, pin) && |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 263 | !intel_pad_locked(pctrl, pin); |
| 264 | } |
| 265 | |
| 266 | static int intel_get_groups_count(struct pinctrl_dev *pctldev) |
| 267 | { |
| 268 | struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); |
| 269 | |
| 270 | return pctrl->soc->ngroups; |
| 271 | } |
| 272 | |
| 273 | static const char *intel_get_group_name(struct pinctrl_dev *pctldev, |
Andy Shevchenko | 04035f7 | 2018-09-26 17:50:26 +0300 | [diff] [blame] | 274 | unsigned int group) |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 275 | { |
| 276 | struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); |
| 277 | |
| 278 | return pctrl->soc->groups[group].name; |
| 279 | } |
| 280 | |
Andy Shevchenko | 04035f7 | 2018-09-26 17:50:26 +0300 | [diff] [blame] | 281 | static int intel_get_group_pins(struct pinctrl_dev *pctldev, unsigned int group, |
| 282 | const unsigned int **pins, unsigned int *npins) |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 283 | { |
| 284 | struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); |
| 285 | |
| 286 | *pins = pctrl->soc->groups[group].pins; |
| 287 | *npins = pctrl->soc->groups[group].npins; |
| 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | static void intel_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s, |
Andy Shevchenko | 04035f7 | 2018-09-26 17:50:26 +0300 | [diff] [blame] | 292 | unsigned int pin) |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 293 | { |
| 294 | struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); |
Mika Westerberg | e57725e | 2017-01-27 13:07:14 +0300 | [diff] [blame] | 295 | void __iomem *padcfg; |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 296 | u32 cfg0, cfg1, mode; |
| 297 | bool locked, acpi; |
| 298 | |
| 299 | if (!intel_pad_owned_by_host(pctrl, pin)) { |
| 300 | seq_puts(s, "not available"); |
| 301 | return; |
| 302 | } |
| 303 | |
| 304 | cfg0 = readl(intel_get_padcfg(pctrl, pin, PADCFG0)); |
| 305 | cfg1 = readl(intel_get_padcfg(pctrl, pin, PADCFG1)); |
| 306 | |
| 307 | mode = (cfg0 & PADCFG0_PMODE_MASK) >> PADCFG0_PMODE_SHIFT; |
| 308 | if (!mode) |
| 309 | seq_puts(s, "GPIO "); |
| 310 | else |
| 311 | seq_printf(s, "mode %d ", mode); |
| 312 | |
| 313 | seq_printf(s, "0x%08x 0x%08x", cfg0, cfg1); |
| 314 | |
Mika Westerberg | e57725e | 2017-01-27 13:07:14 +0300 | [diff] [blame] | 315 | /* Dump the additional PADCFG registers if available */ |
| 316 | padcfg = intel_get_padcfg(pctrl, pin, PADCFG2); |
| 317 | if (padcfg) |
| 318 | seq_printf(s, " 0x%08x", readl(padcfg)); |
| 319 | |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 320 | locked = intel_pad_locked(pctrl, pin); |
Mika Westerberg | 4341e8a | 2015-10-21 13:08:44 +0300 | [diff] [blame] | 321 | acpi = intel_pad_acpi_mode(pctrl, pin); |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 322 | |
| 323 | if (locked || acpi) { |
| 324 | seq_puts(s, " ["); |
| 325 | if (locked) { |
| 326 | seq_puts(s, "LOCKED"); |
| 327 | if (acpi) |
| 328 | seq_puts(s, ", "); |
| 329 | } |
| 330 | if (acpi) |
| 331 | seq_puts(s, "ACPI"); |
| 332 | seq_puts(s, "]"); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | static const struct pinctrl_ops intel_pinctrl_ops = { |
| 337 | .get_groups_count = intel_get_groups_count, |
| 338 | .get_group_name = intel_get_group_name, |
| 339 | .get_group_pins = intel_get_group_pins, |
| 340 | .pin_dbg_show = intel_pin_dbg_show, |
| 341 | }; |
| 342 | |
| 343 | static int intel_get_functions_count(struct pinctrl_dev *pctldev) |
| 344 | { |
| 345 | struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); |
| 346 | |
| 347 | return pctrl->soc->nfunctions; |
| 348 | } |
| 349 | |
| 350 | static const char *intel_get_function_name(struct pinctrl_dev *pctldev, |
Andy Shevchenko | 04035f7 | 2018-09-26 17:50:26 +0300 | [diff] [blame] | 351 | unsigned int function) |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 352 | { |
| 353 | struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); |
| 354 | |
| 355 | return pctrl->soc->functions[function].name; |
| 356 | } |
| 357 | |
| 358 | static int intel_get_function_groups(struct pinctrl_dev *pctldev, |
Andy Shevchenko | 04035f7 | 2018-09-26 17:50:26 +0300 | [diff] [blame] | 359 | unsigned int function, |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 360 | const char * const **groups, |
Andy Shevchenko | 04035f7 | 2018-09-26 17:50:26 +0300 | [diff] [blame] | 361 | unsigned int * const ngroups) |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 362 | { |
| 363 | struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); |
| 364 | |
| 365 | *groups = pctrl->soc->functions[function].groups; |
| 366 | *ngroups = pctrl->soc->functions[function].ngroups; |
| 367 | return 0; |
| 368 | } |
| 369 | |
Andy Shevchenko | 04035f7 | 2018-09-26 17:50:26 +0300 | [diff] [blame] | 370 | static int intel_pinmux_set_mux(struct pinctrl_dev *pctldev, |
| 371 | unsigned int function, unsigned int group) |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 372 | { |
| 373 | struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); |
| 374 | const struct intel_pingroup *grp = &pctrl->soc->groups[group]; |
| 375 | unsigned long flags; |
| 376 | int i; |
| 377 | |
Mika Westerberg | 27d9098 | 2016-06-16 11:25:36 +0300 | [diff] [blame] | 378 | raw_spin_lock_irqsave(&pctrl->lock, flags); |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 379 | |
| 380 | /* |
| 381 | * All pins in the groups needs to be accessible and writable |
| 382 | * before we can enable the mux for this group. |
| 383 | */ |
| 384 | for (i = 0; i < grp->npins; i++) { |
| 385 | if (!intel_pad_usable(pctrl, grp->pins[i])) { |
Mika Westerberg | 27d9098 | 2016-06-16 11:25:36 +0300 | [diff] [blame] | 386 | raw_spin_unlock_irqrestore(&pctrl->lock, flags); |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 387 | return -EBUSY; |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | /* Now enable the mux setting for each pin in the group */ |
| 392 | for (i = 0; i < grp->npins; i++) { |
| 393 | void __iomem *padcfg0; |
| 394 | u32 value; |
| 395 | |
| 396 | padcfg0 = intel_get_padcfg(pctrl, grp->pins[i], PADCFG0); |
| 397 | value = readl(padcfg0); |
| 398 | |
| 399 | value &= ~PADCFG0_PMODE_MASK; |
Mika Westerberg | 1f6b419 | 2017-06-06 16:18:18 +0300 | [diff] [blame] | 400 | |
| 401 | if (grp->modes) |
| 402 | value |= grp->modes[i] << PADCFG0_PMODE_SHIFT; |
| 403 | else |
| 404 | value |= grp->mode << PADCFG0_PMODE_SHIFT; |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 405 | |
| 406 | writel(value, padcfg0); |
| 407 | } |
| 408 | |
Mika Westerberg | 27d9098 | 2016-06-16 11:25:36 +0300 | [diff] [blame] | 409 | raw_spin_unlock_irqrestore(&pctrl->lock, flags); |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 410 | |
| 411 | return 0; |
| 412 | } |
| 413 | |
Andy Shevchenko | 17fab47 | 2017-01-02 14:07:22 +0200 | [diff] [blame] | 414 | static void __intel_gpio_set_direction(void __iomem *padcfg0, bool input) |
| 415 | { |
| 416 | u32 value; |
| 417 | |
| 418 | value = readl(padcfg0); |
| 419 | if (input) { |
| 420 | value &= ~PADCFG0_GPIORXDIS; |
| 421 | value |= PADCFG0_GPIOTXDIS; |
| 422 | } else { |
| 423 | value &= ~PADCFG0_GPIOTXDIS; |
| 424 | value |= PADCFG0_GPIORXDIS; |
| 425 | } |
| 426 | writel(value, padcfg0); |
| 427 | } |
| 428 | |
Mika Westerberg | f5a26ac | 2017-11-29 16:25:44 +0300 | [diff] [blame] | 429 | static void intel_gpio_set_gpio_mode(void __iomem *padcfg0) |
| 430 | { |
| 431 | u32 value; |
| 432 | |
| 433 | /* Put the pad into GPIO mode */ |
| 434 | value = readl(padcfg0) & ~PADCFG0_PMODE_MASK; |
| 435 | /* Disable SCI/SMI/NMI generation */ |
| 436 | value &= ~(PADCFG0_GPIROUTIOXAPIC | PADCFG0_GPIROUTSCI); |
| 437 | value &= ~(PADCFG0_GPIROUTSMI | PADCFG0_GPIROUTNMI); |
| 438 | writel(value, padcfg0); |
| 439 | } |
| 440 | |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 441 | static int intel_gpio_request_enable(struct pinctrl_dev *pctldev, |
| 442 | struct pinctrl_gpio_range *range, |
Andy Shevchenko | 04035f7 | 2018-09-26 17:50:26 +0300 | [diff] [blame] | 443 | unsigned int pin) |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 444 | { |
| 445 | struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); |
| 446 | void __iomem *padcfg0; |
| 447 | unsigned long flags; |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 448 | |
Mika Westerberg | 27d9098 | 2016-06-16 11:25:36 +0300 | [diff] [blame] | 449 | raw_spin_lock_irqsave(&pctrl->lock, flags); |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 450 | |
| 451 | if (!intel_pad_usable(pctrl, pin)) { |
Mika Westerberg | 27d9098 | 2016-06-16 11:25:36 +0300 | [diff] [blame] | 452 | raw_spin_unlock_irqrestore(&pctrl->lock, flags); |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 453 | return -EBUSY; |
| 454 | } |
| 455 | |
| 456 | padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0); |
Mika Westerberg | f5a26ac | 2017-11-29 16:25:44 +0300 | [diff] [blame] | 457 | intel_gpio_set_gpio_mode(padcfg0); |
Andy Shevchenko | 17fab47 | 2017-01-02 14:07:22 +0200 | [diff] [blame] | 458 | /* Disable TX buffer and enable RX (this will be input) */ |
| 459 | __intel_gpio_set_direction(padcfg0, true); |
| 460 | |
Mika Westerberg | 27d9098 | 2016-06-16 11:25:36 +0300 | [diff] [blame] | 461 | raw_spin_unlock_irqrestore(&pctrl->lock, flags); |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 462 | |
| 463 | return 0; |
| 464 | } |
| 465 | |
| 466 | static int intel_gpio_set_direction(struct pinctrl_dev *pctldev, |
| 467 | struct pinctrl_gpio_range *range, |
Andy Shevchenko | 04035f7 | 2018-09-26 17:50:26 +0300 | [diff] [blame] | 468 | unsigned int pin, bool input) |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 469 | { |
| 470 | struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); |
| 471 | void __iomem *padcfg0; |
| 472 | unsigned long flags; |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 473 | |
Mika Westerberg | 27d9098 | 2016-06-16 11:25:36 +0300 | [diff] [blame] | 474 | raw_spin_lock_irqsave(&pctrl->lock, flags); |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 475 | |
| 476 | padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0); |
Andy Shevchenko | 17fab47 | 2017-01-02 14:07:22 +0200 | [diff] [blame] | 477 | __intel_gpio_set_direction(padcfg0, input); |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 478 | |
Mika Westerberg | 27d9098 | 2016-06-16 11:25:36 +0300 | [diff] [blame] | 479 | raw_spin_unlock_irqrestore(&pctrl->lock, flags); |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 480 | |
| 481 | return 0; |
| 482 | } |
| 483 | |
| 484 | static const struct pinmux_ops intel_pinmux_ops = { |
| 485 | .get_functions_count = intel_get_functions_count, |
| 486 | .get_function_name = intel_get_function_name, |
| 487 | .get_function_groups = intel_get_function_groups, |
| 488 | .set_mux = intel_pinmux_set_mux, |
| 489 | .gpio_request_enable = intel_gpio_request_enable, |
| 490 | .gpio_set_direction = intel_gpio_set_direction, |
| 491 | }; |
| 492 | |
Andy Shevchenko | 04035f7 | 2018-09-26 17:50:26 +0300 | [diff] [blame] | 493 | static int intel_config_get(struct pinctrl_dev *pctldev, unsigned int pin, |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 494 | unsigned long *config) |
| 495 | { |
| 496 | struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); |
| 497 | enum pin_config_param param = pinconf_to_config_param(*config); |
Mika Westerberg | 04cc058 | 2017-01-27 13:07:15 +0300 | [diff] [blame] | 498 | const struct intel_community *community; |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 499 | u32 value, term; |
Mika Westerberg | e57725e | 2017-01-27 13:07:14 +0300 | [diff] [blame] | 500 | u32 arg = 0; |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 501 | |
| 502 | if (!intel_pad_owned_by_host(pctrl, pin)) |
| 503 | return -ENOTSUPP; |
| 504 | |
Mika Westerberg | 04cc058 | 2017-01-27 13:07:15 +0300 | [diff] [blame] | 505 | community = intel_get_community(pctrl, pin); |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 506 | value = readl(intel_get_padcfg(pctrl, pin, PADCFG1)); |
| 507 | term = (value & PADCFG1_TERM_MASK) >> PADCFG1_TERM_SHIFT; |
| 508 | |
| 509 | switch (param) { |
| 510 | case PIN_CONFIG_BIAS_DISABLE: |
| 511 | if (term) |
| 512 | return -EINVAL; |
| 513 | break; |
| 514 | |
| 515 | case PIN_CONFIG_BIAS_PULL_UP: |
| 516 | if (!term || !(value & PADCFG1_TERM_UP)) |
| 517 | return -EINVAL; |
| 518 | |
| 519 | switch (term) { |
| 520 | case PADCFG1_TERM_1K: |
| 521 | arg = 1000; |
| 522 | break; |
| 523 | case PADCFG1_TERM_2K: |
| 524 | arg = 2000; |
| 525 | break; |
| 526 | case PADCFG1_TERM_5K: |
| 527 | arg = 5000; |
| 528 | break; |
| 529 | case PADCFG1_TERM_20K: |
| 530 | arg = 20000; |
| 531 | break; |
| 532 | } |
| 533 | |
| 534 | break; |
| 535 | |
| 536 | case PIN_CONFIG_BIAS_PULL_DOWN: |
| 537 | if (!term || value & PADCFG1_TERM_UP) |
| 538 | return -EINVAL; |
| 539 | |
| 540 | switch (term) { |
Mika Westerberg | 04cc058 | 2017-01-27 13:07:15 +0300 | [diff] [blame] | 541 | case PADCFG1_TERM_1K: |
| 542 | if (!(community->features & PINCTRL_FEATURE_1K_PD)) |
| 543 | return -EINVAL; |
| 544 | arg = 1000; |
| 545 | break; |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 546 | case PADCFG1_TERM_5K: |
| 547 | arg = 5000; |
| 548 | break; |
| 549 | case PADCFG1_TERM_20K: |
| 550 | arg = 20000; |
| 551 | break; |
| 552 | } |
| 553 | |
| 554 | break; |
| 555 | |
Mika Westerberg | e57725e | 2017-01-27 13:07:14 +0300 | [diff] [blame] | 556 | case PIN_CONFIG_INPUT_DEBOUNCE: { |
| 557 | void __iomem *padcfg2; |
| 558 | u32 v; |
| 559 | |
| 560 | padcfg2 = intel_get_padcfg(pctrl, pin, PADCFG2); |
| 561 | if (!padcfg2) |
| 562 | return -ENOTSUPP; |
| 563 | |
| 564 | v = readl(padcfg2); |
| 565 | if (!(v & PADCFG2_DEBEN)) |
| 566 | return -EINVAL; |
| 567 | |
| 568 | v = (v & PADCFG2_DEBOUNCE_MASK) >> PADCFG2_DEBOUNCE_SHIFT; |
| 569 | arg = BIT(v) * DEBOUNCE_PERIOD / 1000; |
| 570 | |
| 571 | break; |
| 572 | } |
| 573 | |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 574 | default: |
| 575 | return -ENOTSUPP; |
| 576 | } |
| 577 | |
| 578 | *config = pinconf_to_config_packed(param, arg); |
| 579 | return 0; |
| 580 | } |
| 581 | |
Andy Shevchenko | 04035f7 | 2018-09-26 17:50:26 +0300 | [diff] [blame] | 582 | static int intel_config_set_pull(struct intel_pinctrl *pctrl, unsigned int pin, |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 583 | unsigned long config) |
| 584 | { |
Andy Shevchenko | 04035f7 | 2018-09-26 17:50:26 +0300 | [diff] [blame] | 585 | unsigned int param = pinconf_to_config_param(config); |
| 586 | unsigned int arg = pinconf_to_config_argument(config); |
Mika Westerberg | 04cc058 | 2017-01-27 13:07:15 +0300 | [diff] [blame] | 587 | const struct intel_community *community; |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 588 | void __iomem *padcfg1; |
| 589 | unsigned long flags; |
| 590 | int ret = 0; |
| 591 | u32 value; |
| 592 | |
Mika Westerberg | 27d9098 | 2016-06-16 11:25:36 +0300 | [diff] [blame] | 593 | raw_spin_lock_irqsave(&pctrl->lock, flags); |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 594 | |
Mika Westerberg | 04cc058 | 2017-01-27 13:07:15 +0300 | [diff] [blame] | 595 | community = intel_get_community(pctrl, pin); |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 596 | padcfg1 = intel_get_padcfg(pctrl, pin, PADCFG1); |
| 597 | value = readl(padcfg1); |
| 598 | |
| 599 | switch (param) { |
| 600 | case PIN_CONFIG_BIAS_DISABLE: |
| 601 | value &= ~(PADCFG1_TERM_MASK | PADCFG1_TERM_UP); |
| 602 | break; |
| 603 | |
| 604 | case PIN_CONFIG_BIAS_PULL_UP: |
| 605 | value &= ~PADCFG1_TERM_MASK; |
| 606 | |
| 607 | value |= PADCFG1_TERM_UP; |
| 608 | |
| 609 | switch (arg) { |
| 610 | case 20000: |
| 611 | value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT; |
| 612 | break; |
| 613 | case 5000: |
| 614 | value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT; |
| 615 | break; |
| 616 | case 2000: |
| 617 | value |= PADCFG1_TERM_2K << PADCFG1_TERM_SHIFT; |
| 618 | break; |
| 619 | case 1000: |
| 620 | value |= PADCFG1_TERM_1K << PADCFG1_TERM_SHIFT; |
| 621 | break; |
| 622 | default: |
| 623 | ret = -EINVAL; |
| 624 | } |
| 625 | |
| 626 | break; |
| 627 | |
| 628 | case PIN_CONFIG_BIAS_PULL_DOWN: |
| 629 | value &= ~(PADCFG1_TERM_UP | PADCFG1_TERM_MASK); |
| 630 | |
| 631 | switch (arg) { |
| 632 | case 20000: |
| 633 | value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT; |
| 634 | break; |
| 635 | case 5000: |
| 636 | value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT; |
| 637 | break; |
Mika Westerberg | 04cc058 | 2017-01-27 13:07:15 +0300 | [diff] [blame] | 638 | case 1000: |
Dan Carpenter | aa1dd80 | 2017-02-07 16:20:08 +0300 | [diff] [blame] | 639 | if (!(community->features & PINCTRL_FEATURE_1K_PD)) { |
| 640 | ret = -EINVAL; |
| 641 | break; |
| 642 | } |
Mika Westerberg | 04cc058 | 2017-01-27 13:07:15 +0300 | [diff] [blame] | 643 | value |= PADCFG1_TERM_1K << PADCFG1_TERM_SHIFT; |
| 644 | break; |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 645 | default: |
| 646 | ret = -EINVAL; |
| 647 | } |
| 648 | |
| 649 | break; |
| 650 | } |
| 651 | |
| 652 | if (!ret) |
| 653 | writel(value, padcfg1); |
| 654 | |
Mika Westerberg | 27d9098 | 2016-06-16 11:25:36 +0300 | [diff] [blame] | 655 | raw_spin_unlock_irqrestore(&pctrl->lock, flags); |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 656 | |
| 657 | return ret; |
| 658 | } |
| 659 | |
Andy Shevchenko | 04035f7 | 2018-09-26 17:50:26 +0300 | [diff] [blame] | 660 | static int intel_config_set_debounce(struct intel_pinctrl *pctrl, |
| 661 | unsigned int pin, unsigned int debounce) |
Mika Westerberg | e57725e | 2017-01-27 13:07:14 +0300 | [diff] [blame] | 662 | { |
| 663 | void __iomem *padcfg0, *padcfg2; |
| 664 | unsigned long flags; |
| 665 | u32 value0, value2; |
| 666 | int ret = 0; |
| 667 | |
| 668 | padcfg2 = intel_get_padcfg(pctrl, pin, PADCFG2); |
| 669 | if (!padcfg2) |
| 670 | return -ENOTSUPP; |
| 671 | |
| 672 | padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0); |
| 673 | |
| 674 | raw_spin_lock_irqsave(&pctrl->lock, flags); |
| 675 | |
| 676 | value0 = readl(padcfg0); |
| 677 | value2 = readl(padcfg2); |
| 678 | |
| 679 | /* Disable glitch filter and debouncer */ |
| 680 | value0 &= ~PADCFG0_PREGFRXSEL; |
| 681 | value2 &= ~(PADCFG2_DEBEN | PADCFG2_DEBOUNCE_MASK); |
| 682 | |
| 683 | if (debounce) { |
| 684 | unsigned long v; |
| 685 | |
| 686 | v = order_base_2(debounce * 1000 / DEBOUNCE_PERIOD); |
| 687 | if (v < 3 || v > 15) { |
| 688 | ret = -EINVAL; |
| 689 | goto exit_unlock; |
| 690 | } else { |
| 691 | /* Enable glitch filter and debouncer */ |
| 692 | value0 |= PADCFG0_PREGFRXSEL; |
| 693 | value2 |= v << PADCFG2_DEBOUNCE_SHIFT; |
| 694 | value2 |= PADCFG2_DEBEN; |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | writel(value0, padcfg0); |
| 699 | writel(value2, padcfg2); |
| 700 | |
| 701 | exit_unlock: |
| 702 | raw_spin_unlock_irqrestore(&pctrl->lock, flags); |
| 703 | |
| 704 | return ret; |
| 705 | } |
| 706 | |
Andy Shevchenko | 04035f7 | 2018-09-26 17:50:26 +0300 | [diff] [blame] | 707 | static int intel_config_set(struct pinctrl_dev *pctldev, unsigned int pin, |
| 708 | unsigned long *configs, unsigned int nconfigs) |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 709 | { |
| 710 | struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); |
| 711 | int i, ret; |
| 712 | |
| 713 | if (!intel_pad_usable(pctrl, pin)) |
| 714 | return -ENOTSUPP; |
| 715 | |
| 716 | for (i = 0; i < nconfigs; i++) { |
| 717 | switch (pinconf_to_config_param(configs[i])) { |
| 718 | case PIN_CONFIG_BIAS_DISABLE: |
| 719 | case PIN_CONFIG_BIAS_PULL_UP: |
| 720 | case PIN_CONFIG_BIAS_PULL_DOWN: |
| 721 | ret = intel_config_set_pull(pctrl, pin, configs[i]); |
| 722 | if (ret) |
| 723 | return ret; |
| 724 | break; |
| 725 | |
Mika Westerberg | e57725e | 2017-01-27 13:07:14 +0300 | [diff] [blame] | 726 | case PIN_CONFIG_INPUT_DEBOUNCE: |
| 727 | ret = intel_config_set_debounce(pctrl, pin, |
| 728 | pinconf_to_config_argument(configs[i])); |
| 729 | if (ret) |
| 730 | return ret; |
| 731 | break; |
| 732 | |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 733 | default: |
| 734 | return -ENOTSUPP; |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | return 0; |
| 739 | } |
| 740 | |
| 741 | static const struct pinconf_ops intel_pinconf_ops = { |
| 742 | .is_generic = true, |
| 743 | .pin_config_get = intel_config_get, |
| 744 | .pin_config_set = intel_config_set, |
| 745 | }; |
| 746 | |
| 747 | static const struct pinctrl_desc intel_pinctrl_desc = { |
| 748 | .pctlops = &intel_pinctrl_ops, |
| 749 | .pmxops = &intel_pinmux_ops, |
| 750 | .confops = &intel_pinconf_ops, |
| 751 | .owner = THIS_MODULE, |
| 752 | }; |
| 753 | |
Mika Westerberg | a60eac3 | 2017-11-27 16:54:43 +0300 | [diff] [blame] | 754 | /** |
| 755 | * intel_gpio_to_pin() - Translate from GPIO offset to pin number |
| 756 | * @pctrl: Pinctrl structure |
| 757 | * @offset: GPIO offset from gpiolib |
Andy Shevchenko | 946ffef | 2018-09-26 17:43:17 +0300 | [diff] [blame] | 758 | * @community: Community is filled here if not %NULL |
Mika Westerberg | a60eac3 | 2017-11-27 16:54:43 +0300 | [diff] [blame] | 759 | * @padgrp: Pad group is filled here if not %NULL |
| 760 | * |
| 761 | * When coming through gpiolib irqchip, the GPIO offset is not |
| 762 | * automatically translated to pinctrl pin number. This function can be |
| 763 | * used to find out the corresponding pinctrl pin. |
| 764 | */ |
Andy Shevchenko | 04035f7 | 2018-09-26 17:50:26 +0300 | [diff] [blame] | 765 | static int intel_gpio_to_pin(struct intel_pinctrl *pctrl, unsigned int offset, |
Mika Westerberg | a60eac3 | 2017-11-27 16:54:43 +0300 | [diff] [blame] | 766 | const struct intel_community **community, |
| 767 | const struct intel_padgroup **padgrp) |
| 768 | { |
| 769 | int i; |
| 770 | |
| 771 | for (i = 0; i < pctrl->ncommunities; i++) { |
| 772 | const struct intel_community *comm = &pctrl->communities[i]; |
| 773 | int j; |
| 774 | |
| 775 | for (j = 0; j < comm->ngpps; j++) { |
| 776 | const struct intel_padgroup *pgrp = &comm->gpps[j]; |
| 777 | |
| 778 | if (pgrp->gpio_base < 0) |
| 779 | continue; |
| 780 | |
| 781 | if (offset >= pgrp->gpio_base && |
| 782 | offset < pgrp->gpio_base + pgrp->size) { |
| 783 | int pin; |
| 784 | |
| 785 | pin = pgrp->base + offset - pgrp->gpio_base; |
| 786 | if (community) |
| 787 | *community = comm; |
| 788 | if (padgrp) |
| 789 | *padgrp = pgrp; |
| 790 | |
| 791 | return pin; |
| 792 | } |
| 793 | } |
| 794 | } |
| 795 | |
| 796 | return -EINVAL; |
| 797 | } |
| 798 | |
Andy Shevchenko | 04035f7 | 2018-09-26 17:50:26 +0300 | [diff] [blame] | 799 | static int intel_gpio_get(struct gpio_chip *chip, unsigned int offset) |
Andy Shevchenko | 55aedef5 | 2018-07-25 15:42:08 +0300 | [diff] [blame] | 800 | { |
Mika Westerberg | 96147db | 2018-09-18 18:36:21 +0300 | [diff] [blame] | 801 | struct intel_pinctrl *pctrl = gpiochip_get_data(chip); |
| 802 | void __iomem *reg; |
| 803 | u32 padcfg0; |
Andy Shevchenko | 55aedef5 | 2018-07-25 15:42:08 +0300 | [diff] [blame] | 804 | int pin; |
| 805 | |
Mika Westerberg | 96147db | 2018-09-18 18:36:21 +0300 | [diff] [blame] | 806 | pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL); |
| 807 | if (pin < 0) |
| 808 | return -EINVAL; |
| 809 | |
| 810 | reg = intel_get_padcfg(pctrl, pin, PADCFG0); |
| 811 | if (!reg) |
| 812 | return -EINVAL; |
| 813 | |
| 814 | padcfg0 = readl(reg); |
| 815 | if (!(padcfg0 & PADCFG0_GPIOTXDIS)) |
| 816 | return !!(padcfg0 & PADCFG0_GPIOTXSTATE); |
| 817 | |
| 818 | return !!(padcfg0 & PADCFG0_GPIORXSTATE); |
Andy Shevchenko | 55aedef5 | 2018-07-25 15:42:08 +0300 | [diff] [blame] | 819 | } |
| 820 | |
Andy Shevchenko | 04035f7 | 2018-09-26 17:50:26 +0300 | [diff] [blame] | 821 | static void intel_gpio_set(struct gpio_chip *chip, unsigned int offset, |
| 822 | int value) |
Mika Westerberg | 96147db | 2018-09-18 18:36:21 +0300 | [diff] [blame] | 823 | { |
| 824 | struct intel_pinctrl *pctrl = gpiochip_get_data(chip); |
| 825 | unsigned long flags; |
| 826 | void __iomem *reg; |
| 827 | u32 padcfg0; |
| 828 | int pin; |
| 829 | |
| 830 | pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL); |
| 831 | if (pin < 0) |
| 832 | return; |
| 833 | |
| 834 | reg = intel_get_padcfg(pctrl, pin, PADCFG0); |
| 835 | if (!reg) |
| 836 | return; |
| 837 | |
| 838 | raw_spin_lock_irqsave(&pctrl->lock, flags); |
| 839 | padcfg0 = readl(reg); |
| 840 | if (value) |
| 841 | padcfg0 |= PADCFG0_GPIOTXSTATE; |
| 842 | else |
| 843 | padcfg0 &= ~PADCFG0_GPIOTXSTATE; |
| 844 | writel(padcfg0, reg); |
| 845 | raw_spin_unlock_irqrestore(&pctrl->lock, flags); |
| 846 | } |
| 847 | |
| 848 | static int intel_gpio_get_direction(struct gpio_chip *chip, unsigned int offset) |
| 849 | { |
| 850 | struct intel_pinctrl *pctrl = gpiochip_get_data(chip); |
| 851 | void __iomem *reg; |
| 852 | u32 padcfg0; |
| 853 | int pin; |
| 854 | |
| 855 | pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL); |
| 856 | if (pin < 0) |
| 857 | return -EINVAL; |
| 858 | |
| 859 | reg = intel_get_padcfg(pctrl, pin, PADCFG0); |
| 860 | if (!reg) |
| 861 | return -EINVAL; |
| 862 | |
| 863 | padcfg0 = readl(reg); |
| 864 | |
| 865 | if (padcfg0 & PADCFG0_PMODE_MASK) |
| 866 | return -EINVAL; |
| 867 | |
| 868 | return !!(padcfg0 & PADCFG0_GPIOTXDIS); |
| 869 | } |
| 870 | |
Andy Shevchenko | 04035f7 | 2018-09-26 17:50:26 +0300 | [diff] [blame] | 871 | static int intel_gpio_direction_input(struct gpio_chip *chip, unsigned int offset) |
Mika Westerberg | 96147db | 2018-09-18 18:36:21 +0300 | [diff] [blame] | 872 | { |
| 873 | return pinctrl_gpio_direction_input(chip->base + offset); |
| 874 | } |
| 875 | |
Andy Shevchenko | 04035f7 | 2018-09-26 17:50:26 +0300 | [diff] [blame] | 876 | static int intel_gpio_direction_output(struct gpio_chip *chip, unsigned int offset, |
Mika Westerberg | 96147db | 2018-09-18 18:36:21 +0300 | [diff] [blame] | 877 | int value) |
| 878 | { |
| 879 | intel_gpio_set(chip, offset, value); |
| 880 | return pinctrl_gpio_direction_output(chip->base + offset); |
| 881 | } |
| 882 | |
| 883 | static const struct gpio_chip intel_gpio_chip = { |
| 884 | .owner = THIS_MODULE, |
| 885 | .request = gpiochip_generic_request, |
| 886 | .free = gpiochip_generic_free, |
| 887 | .get_direction = intel_gpio_get_direction, |
| 888 | .direction_input = intel_gpio_direction_input, |
| 889 | .direction_output = intel_gpio_direction_output, |
| 890 | .get = intel_gpio_get, |
| 891 | .set = intel_gpio_set, |
| 892 | .set_config = gpiochip_generic_config, |
| 893 | }; |
| 894 | |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 895 | static void intel_gpio_irq_ack(struct irq_data *d) |
| 896 | { |
| 897 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
Linus Walleij | acfd4c6 | 2015-12-08 00:18:59 +0100 | [diff] [blame] | 898 | struct intel_pinctrl *pctrl = gpiochip_get_data(gc); |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 899 | const struct intel_community *community; |
Mika Westerberg | a60eac3 | 2017-11-27 16:54:43 +0300 | [diff] [blame] | 900 | const struct intel_padgroup *padgrp; |
| 901 | int pin; |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 902 | |
Mika Westerberg | a60eac3 | 2017-11-27 16:54:43 +0300 | [diff] [blame] | 903 | pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), &community, &padgrp); |
| 904 | if (pin >= 0) { |
Andy Shevchenko | 04035f7 | 2018-09-26 17:50:26 +0300 | [diff] [blame] | 905 | unsigned int gpp, gpp_offset, is_offset; |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 906 | |
Mika Westerberg | 919eb47 | 2017-06-06 16:18:17 +0300 | [diff] [blame] | 907 | gpp = padgrp->reg_num; |
| 908 | gpp_offset = padgroup_offset(padgrp, pin); |
Mika Westerberg | cf769bd | 2017-10-23 15:40:25 +0300 | [diff] [blame] | 909 | is_offset = community->is_offset + gpp * 4; |
Mika Westerberg | 919eb47 | 2017-06-06 16:18:17 +0300 | [diff] [blame] | 910 | |
| 911 | raw_spin_lock(&pctrl->lock); |
Mika Westerberg | cf769bd | 2017-10-23 15:40:25 +0300 | [diff] [blame] | 912 | writel(BIT(gpp_offset), community->regs + is_offset); |
Mika Westerberg | 919eb47 | 2017-06-06 16:18:17 +0300 | [diff] [blame] | 913 | raw_spin_unlock(&pctrl->lock); |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 914 | } |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 915 | } |
| 916 | |
Qi Zheng | a939bb5 | 2016-03-17 02:15:26 +0800 | [diff] [blame] | 917 | static void intel_gpio_irq_enable(struct irq_data *d) |
| 918 | { |
| 919 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
| 920 | struct intel_pinctrl *pctrl = gpiochip_get_data(gc); |
| 921 | const struct intel_community *community; |
Mika Westerberg | a60eac3 | 2017-11-27 16:54:43 +0300 | [diff] [blame] | 922 | const struct intel_padgroup *padgrp; |
| 923 | int pin; |
Qi Zheng | a939bb5 | 2016-03-17 02:15:26 +0800 | [diff] [blame] | 924 | |
Mika Westerberg | a60eac3 | 2017-11-27 16:54:43 +0300 | [diff] [blame] | 925 | pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), &community, &padgrp); |
| 926 | if (pin >= 0) { |
Andy Shevchenko | 04035f7 | 2018-09-26 17:50:26 +0300 | [diff] [blame] | 927 | unsigned int gpp, gpp_offset, is_offset; |
Mika Westerberg | 919eb47 | 2017-06-06 16:18:17 +0300 | [diff] [blame] | 928 | unsigned long flags; |
Qi Zheng | a939bb5 | 2016-03-17 02:15:26 +0800 | [diff] [blame] | 929 | u32 value; |
| 930 | |
Mika Westerberg | 919eb47 | 2017-06-06 16:18:17 +0300 | [diff] [blame] | 931 | gpp = padgrp->reg_num; |
| 932 | gpp_offset = padgroup_offset(padgrp, pin); |
Mika Westerberg | cf769bd | 2017-10-23 15:40:25 +0300 | [diff] [blame] | 933 | is_offset = community->is_offset + gpp * 4; |
Mika Westerberg | 919eb47 | 2017-06-06 16:18:17 +0300 | [diff] [blame] | 934 | |
| 935 | raw_spin_lock_irqsave(&pctrl->lock, flags); |
Qi Zheng | a939bb5 | 2016-03-17 02:15:26 +0800 | [diff] [blame] | 936 | /* Clear interrupt status first to avoid unexpected interrupt */ |
Mika Westerberg | cf769bd | 2017-10-23 15:40:25 +0300 | [diff] [blame] | 937 | writel(BIT(gpp_offset), community->regs + is_offset); |
Qi Zheng | a939bb5 | 2016-03-17 02:15:26 +0800 | [diff] [blame] | 938 | |
| 939 | value = readl(community->regs + community->ie_offset + gpp * 4); |
| 940 | value |= BIT(gpp_offset); |
| 941 | writel(value, community->regs + community->ie_offset + gpp * 4); |
Mika Westerberg | 919eb47 | 2017-06-06 16:18:17 +0300 | [diff] [blame] | 942 | raw_spin_unlock_irqrestore(&pctrl->lock, flags); |
Qi Zheng | a939bb5 | 2016-03-17 02:15:26 +0800 | [diff] [blame] | 943 | } |
Qi Zheng | a939bb5 | 2016-03-17 02:15:26 +0800 | [diff] [blame] | 944 | } |
| 945 | |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 946 | static void intel_gpio_irq_mask_unmask(struct irq_data *d, bool mask) |
| 947 | { |
| 948 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
Linus Walleij | acfd4c6 | 2015-12-08 00:18:59 +0100 | [diff] [blame] | 949 | struct intel_pinctrl *pctrl = gpiochip_get_data(gc); |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 950 | const struct intel_community *community; |
Mika Westerberg | a60eac3 | 2017-11-27 16:54:43 +0300 | [diff] [blame] | 951 | const struct intel_padgroup *padgrp; |
| 952 | int pin; |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 953 | |
Mika Westerberg | a60eac3 | 2017-11-27 16:54:43 +0300 | [diff] [blame] | 954 | pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), &community, &padgrp); |
| 955 | if (pin >= 0) { |
Andy Shevchenko | 04035f7 | 2018-09-26 17:50:26 +0300 | [diff] [blame] | 956 | unsigned int gpp, gpp_offset; |
Mika Westerberg | 919eb47 | 2017-06-06 16:18:17 +0300 | [diff] [blame] | 957 | unsigned long flags; |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 958 | void __iomem *reg; |
| 959 | u32 value; |
| 960 | |
Mika Westerberg | 919eb47 | 2017-06-06 16:18:17 +0300 | [diff] [blame] | 961 | gpp = padgrp->reg_num; |
| 962 | gpp_offset = padgroup_offset(padgrp, pin); |
| 963 | |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 964 | reg = community->regs + community->ie_offset + gpp * 4; |
Mika Westerberg | 919eb47 | 2017-06-06 16:18:17 +0300 | [diff] [blame] | 965 | |
| 966 | raw_spin_lock_irqsave(&pctrl->lock, flags); |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 967 | value = readl(reg); |
| 968 | if (mask) |
| 969 | value &= ~BIT(gpp_offset); |
| 970 | else |
| 971 | value |= BIT(gpp_offset); |
| 972 | writel(value, reg); |
Mika Westerberg | 919eb47 | 2017-06-06 16:18:17 +0300 | [diff] [blame] | 973 | raw_spin_unlock_irqrestore(&pctrl->lock, flags); |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 974 | } |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | static void intel_gpio_irq_mask(struct irq_data *d) |
| 978 | { |
| 979 | intel_gpio_irq_mask_unmask(d, true); |
| 980 | } |
| 981 | |
| 982 | static void intel_gpio_irq_unmask(struct irq_data *d) |
| 983 | { |
| 984 | intel_gpio_irq_mask_unmask(d, false); |
| 985 | } |
| 986 | |
Andy Shevchenko | 04035f7 | 2018-09-26 17:50:26 +0300 | [diff] [blame] | 987 | static int intel_gpio_irq_type(struct irq_data *d, unsigned int type) |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 988 | { |
| 989 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
Linus Walleij | acfd4c6 | 2015-12-08 00:18:59 +0100 | [diff] [blame] | 990 | struct intel_pinctrl *pctrl = gpiochip_get_data(gc); |
Andy Shevchenko | 04035f7 | 2018-09-26 17:50:26 +0300 | [diff] [blame] | 991 | unsigned int pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), NULL, NULL); |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 992 | unsigned long flags; |
| 993 | void __iomem *reg; |
| 994 | u32 value; |
| 995 | |
| 996 | reg = intel_get_padcfg(pctrl, pin, PADCFG0); |
| 997 | if (!reg) |
| 998 | return -EINVAL; |
| 999 | |
Mika Westerberg | 4341e8a | 2015-10-21 13:08:44 +0300 | [diff] [blame] | 1000 | /* |
| 1001 | * If the pin is in ACPI mode it is still usable as a GPIO but it |
| 1002 | * cannot be used as IRQ because GPI_IS status bit will not be |
| 1003 | * updated by the host controller hardware. |
| 1004 | */ |
| 1005 | if (intel_pad_acpi_mode(pctrl, pin)) { |
| 1006 | dev_warn(pctrl->dev, "pin %u cannot be used as IRQ\n", pin); |
| 1007 | return -EPERM; |
| 1008 | } |
| 1009 | |
Mika Westerberg | 27d9098 | 2016-06-16 11:25:36 +0300 | [diff] [blame] | 1010 | raw_spin_lock_irqsave(&pctrl->lock, flags); |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1011 | |
Mika Westerberg | f5a26ac | 2017-11-29 16:25:44 +0300 | [diff] [blame] | 1012 | intel_gpio_set_gpio_mode(reg); |
| 1013 | |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1014 | value = readl(reg); |
| 1015 | |
| 1016 | value &= ~(PADCFG0_RXEVCFG_MASK | PADCFG0_RXINV); |
| 1017 | |
| 1018 | if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) { |
| 1019 | value |= PADCFG0_RXEVCFG_EDGE_BOTH << PADCFG0_RXEVCFG_SHIFT; |
| 1020 | } else if (type & IRQ_TYPE_EDGE_FALLING) { |
| 1021 | value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT; |
| 1022 | value |= PADCFG0_RXINV; |
| 1023 | } else if (type & IRQ_TYPE_EDGE_RISING) { |
| 1024 | value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT; |
Qipeng Zha | bf380cf | 2016-03-17 02:15:25 +0800 | [diff] [blame] | 1025 | } else if (type & IRQ_TYPE_LEVEL_MASK) { |
| 1026 | if (type & IRQ_TYPE_LEVEL_LOW) |
| 1027 | value |= PADCFG0_RXINV; |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1028 | } else { |
| 1029 | value |= PADCFG0_RXEVCFG_DISABLED << PADCFG0_RXEVCFG_SHIFT; |
| 1030 | } |
| 1031 | |
| 1032 | writel(value, reg); |
| 1033 | |
| 1034 | if (type & IRQ_TYPE_EDGE_BOTH) |
Thomas Gleixner | fc756bc | 2015-06-23 15:52:45 +0200 | [diff] [blame] | 1035 | irq_set_handler_locked(d, handle_edge_irq); |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1036 | else if (type & IRQ_TYPE_LEVEL_MASK) |
Thomas Gleixner | fc756bc | 2015-06-23 15:52:45 +0200 | [diff] [blame] | 1037 | irq_set_handler_locked(d, handle_level_irq); |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1038 | |
Mika Westerberg | 27d9098 | 2016-06-16 11:25:36 +0300 | [diff] [blame] | 1039 | raw_spin_unlock_irqrestore(&pctrl->lock, flags); |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1040 | |
| 1041 | return 0; |
| 1042 | } |
| 1043 | |
| 1044 | static int intel_gpio_irq_wake(struct irq_data *d, unsigned int on) |
| 1045 | { |
| 1046 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
Linus Walleij | acfd4c6 | 2015-12-08 00:18:59 +0100 | [diff] [blame] | 1047 | struct intel_pinctrl *pctrl = gpiochip_get_data(gc); |
Andy Shevchenko | 04035f7 | 2018-09-26 17:50:26 +0300 | [diff] [blame] | 1048 | unsigned int pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), NULL, NULL); |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1049 | |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1050 | if (on) |
Nilesh Bacchewar | 01dabe9 | 2016-09-21 16:35:23 -0700 | [diff] [blame] | 1051 | enable_irq_wake(pctrl->irq); |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1052 | else |
Nilesh Bacchewar | 01dabe9 | 2016-09-21 16:35:23 -0700 | [diff] [blame] | 1053 | disable_irq_wake(pctrl->irq); |
Andy Shevchenko | 9a520fd | 2016-07-08 14:30:46 +0300 | [diff] [blame] | 1054 | |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1055 | dev_dbg(pctrl->dev, "%sable wake for pin %u\n", on ? "en" : "dis", pin); |
| 1056 | return 0; |
| 1057 | } |
| 1058 | |
Mika Westerberg | 193b40c | 2015-10-21 13:08:43 +0300 | [diff] [blame] | 1059 | static irqreturn_t intel_gpio_community_irq_handler(struct intel_pinctrl *pctrl, |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1060 | const struct intel_community *community) |
| 1061 | { |
Mika Westerberg | 193b40c | 2015-10-21 13:08:43 +0300 | [diff] [blame] | 1062 | struct gpio_chip *gc = &pctrl->chip; |
| 1063 | irqreturn_t ret = IRQ_NONE; |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1064 | int gpp; |
| 1065 | |
| 1066 | for (gpp = 0; gpp < community->ngpps; gpp++) { |
Mika Westerberg | 919eb47 | 2017-06-06 16:18:17 +0300 | [diff] [blame] | 1067 | const struct intel_padgroup *padgrp = &community->gpps[gpp]; |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1068 | unsigned long pending, enabled, gpp_offset; |
| 1069 | |
Mika Westerberg | cf769bd | 2017-10-23 15:40:25 +0300 | [diff] [blame] | 1070 | pending = readl(community->regs + community->is_offset + |
| 1071 | padgrp->reg_num * 4); |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1072 | enabled = readl(community->regs + community->ie_offset + |
Mika Westerberg | 919eb47 | 2017-06-06 16:18:17 +0300 | [diff] [blame] | 1073 | padgrp->reg_num * 4); |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1074 | |
| 1075 | /* Only interrupts that are enabled */ |
| 1076 | pending &= enabled; |
| 1077 | |
Mika Westerberg | 919eb47 | 2017-06-06 16:18:17 +0300 | [diff] [blame] | 1078 | for_each_set_bit(gpp_offset, &pending, padgrp->size) { |
Mika Westerberg | a60eac3 | 2017-11-27 16:54:43 +0300 | [diff] [blame] | 1079 | unsigned irq; |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1080 | |
Thierry Reding | f0fbe7b | 2017-11-07 19:15:47 +0100 | [diff] [blame] | 1081 | irq = irq_find_mapping(gc->irq.domain, |
Mika Westerberg | a60eac3 | 2017-11-27 16:54:43 +0300 | [diff] [blame] | 1082 | padgrp->gpio_base + gpp_offset); |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1083 | generic_handle_irq(irq); |
Mika Westerberg | 193b40c | 2015-10-21 13:08:43 +0300 | [diff] [blame] | 1084 | |
| 1085 | ret |= IRQ_HANDLED; |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1086 | } |
| 1087 | } |
Mika Westerberg | 193b40c | 2015-10-21 13:08:43 +0300 | [diff] [blame] | 1088 | |
| 1089 | return ret; |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1090 | } |
| 1091 | |
Mika Westerberg | 193b40c | 2015-10-21 13:08:43 +0300 | [diff] [blame] | 1092 | static irqreturn_t intel_gpio_irq(int irq, void *data) |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1093 | { |
Mika Westerberg | 193b40c | 2015-10-21 13:08:43 +0300 | [diff] [blame] | 1094 | const struct intel_community *community; |
| 1095 | struct intel_pinctrl *pctrl = data; |
| 1096 | irqreturn_t ret = IRQ_NONE; |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1097 | int i; |
| 1098 | |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1099 | /* Need to check all communities for pending interrupts */ |
Mika Westerberg | 193b40c | 2015-10-21 13:08:43 +0300 | [diff] [blame] | 1100 | for (i = 0; i < pctrl->ncommunities; i++) { |
| 1101 | community = &pctrl->communities[i]; |
| 1102 | ret |= intel_gpio_community_irq_handler(pctrl, community); |
| 1103 | } |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1104 | |
Mika Westerberg | 193b40c | 2015-10-21 13:08:43 +0300 | [diff] [blame] | 1105 | return ret; |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1106 | } |
| 1107 | |
| 1108 | static struct irq_chip intel_gpio_irqchip = { |
| 1109 | .name = "intel-gpio", |
Qi Zheng | a939bb5 | 2016-03-17 02:15:26 +0800 | [diff] [blame] | 1110 | .irq_enable = intel_gpio_irq_enable, |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1111 | .irq_ack = intel_gpio_irq_ack, |
| 1112 | .irq_mask = intel_gpio_irq_mask, |
| 1113 | .irq_unmask = intel_gpio_irq_unmask, |
| 1114 | .irq_set_type = intel_gpio_irq_type, |
| 1115 | .irq_set_wake = intel_gpio_irq_wake, |
Rushikesh S Kadam | 5ff56b0 | 2017-08-11 13:53:44 +0530 | [diff] [blame] | 1116 | .flags = IRQCHIP_MASK_ON_SUSPEND, |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1117 | }; |
| 1118 | |
Mika Westerberg | a60eac3 | 2017-11-27 16:54:43 +0300 | [diff] [blame] | 1119 | static int intel_gpio_add_pin_ranges(struct intel_pinctrl *pctrl, |
| 1120 | const struct intel_community *community) |
| 1121 | { |
Colin Ian King | 33b6cb5 | 2017-12-04 17:08:15 +0000 | [diff] [blame] | 1122 | int ret = 0, i; |
Mika Westerberg | a60eac3 | 2017-11-27 16:54:43 +0300 | [diff] [blame] | 1123 | |
| 1124 | for (i = 0; i < community->ngpps; i++) { |
| 1125 | const struct intel_padgroup *gpp = &community->gpps[i]; |
| 1126 | |
| 1127 | if (gpp->gpio_base < 0) |
| 1128 | continue; |
| 1129 | |
| 1130 | ret = gpiochip_add_pin_range(&pctrl->chip, dev_name(pctrl->dev), |
| 1131 | gpp->gpio_base, gpp->base, |
| 1132 | gpp->size); |
| 1133 | if (ret) |
| 1134 | return ret; |
| 1135 | } |
| 1136 | |
| 1137 | return ret; |
| 1138 | } |
| 1139 | |
| 1140 | static unsigned intel_gpio_ngpio(const struct intel_pinctrl *pctrl) |
| 1141 | { |
| 1142 | const struct intel_community *community; |
Andy Shevchenko | 04035f7 | 2018-09-26 17:50:26 +0300 | [diff] [blame] | 1143 | unsigned int ngpio = 0; |
Mika Westerberg | a60eac3 | 2017-11-27 16:54:43 +0300 | [diff] [blame] | 1144 | int i, j; |
| 1145 | |
| 1146 | for (i = 0; i < pctrl->ncommunities; i++) { |
| 1147 | community = &pctrl->communities[i]; |
| 1148 | for (j = 0; j < community->ngpps; j++) { |
| 1149 | const struct intel_padgroup *gpp = &community->gpps[j]; |
| 1150 | |
| 1151 | if (gpp->gpio_base < 0) |
| 1152 | continue; |
| 1153 | |
| 1154 | if (gpp->gpio_base + gpp->size > ngpio) |
| 1155 | ngpio = gpp->gpio_base + gpp->size; |
| 1156 | } |
| 1157 | } |
| 1158 | |
| 1159 | return ngpio; |
| 1160 | } |
| 1161 | |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1162 | static int intel_gpio_probe(struct intel_pinctrl *pctrl, int irq) |
| 1163 | { |
Mika Westerberg | a60eac3 | 2017-11-27 16:54:43 +0300 | [diff] [blame] | 1164 | int ret, i; |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1165 | |
| 1166 | pctrl->chip = intel_gpio_chip; |
| 1167 | |
Mika Westerberg | a60eac3 | 2017-11-27 16:54:43 +0300 | [diff] [blame] | 1168 | pctrl->chip.ngpio = intel_gpio_ngpio(pctrl); |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1169 | pctrl->chip.label = dev_name(pctrl->dev); |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 1170 | pctrl->chip.parent = pctrl->dev; |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1171 | pctrl->chip.base = -1; |
Nilesh Bacchewar | 01dabe9 | 2016-09-21 16:35:23 -0700 | [diff] [blame] | 1172 | pctrl->irq = irq; |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1173 | |
Mika Westerberg | f25c3aa | 2017-01-10 17:31:57 +0300 | [diff] [blame] | 1174 | ret = devm_gpiochip_add_data(pctrl->dev, &pctrl->chip, pctrl); |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1175 | if (ret) { |
| 1176 | dev_err(pctrl->dev, "failed to register gpiochip\n"); |
| 1177 | return ret; |
| 1178 | } |
| 1179 | |
Mika Westerberg | a60eac3 | 2017-11-27 16:54:43 +0300 | [diff] [blame] | 1180 | for (i = 0; i < pctrl->ncommunities; i++) { |
| 1181 | struct intel_community *community = &pctrl->communities[i]; |
| 1182 | |
| 1183 | ret = intel_gpio_add_pin_ranges(pctrl, community); |
| 1184 | if (ret) { |
| 1185 | dev_err(pctrl->dev, "failed to add GPIO pin range\n"); |
| 1186 | return ret; |
| 1187 | } |
Mika Westerberg | 193b40c | 2015-10-21 13:08:43 +0300 | [diff] [blame] | 1188 | } |
| 1189 | |
| 1190 | /* |
| 1191 | * We need to request the interrupt here (instead of providing chip |
| 1192 | * to the irq directly) because on some platforms several GPIO |
| 1193 | * controllers share the same interrupt line. |
| 1194 | */ |
Mika Westerberg | 1a7d1cb | 2016-06-16 11:25:37 +0300 | [diff] [blame] | 1195 | ret = devm_request_irq(pctrl->dev, irq, intel_gpio_irq, |
| 1196 | IRQF_SHARED | IRQF_NO_THREAD, |
Mika Westerberg | 193b40c | 2015-10-21 13:08:43 +0300 | [diff] [blame] | 1197 | dev_name(pctrl->dev), pctrl); |
| 1198 | if (ret) { |
| 1199 | dev_err(pctrl->dev, "failed to request interrupt\n"); |
Mika Westerberg | f25c3aa | 2017-01-10 17:31:57 +0300 | [diff] [blame] | 1200 | return ret; |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1201 | } |
| 1202 | |
| 1203 | ret = gpiochip_irqchip_add(&pctrl->chip, &intel_gpio_irqchip, 0, |
Andy Shevchenko | 3ae02c1 | 2016-11-25 13:31:16 +0200 | [diff] [blame] | 1204 | handle_bad_irq, IRQ_TYPE_NONE); |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1205 | if (ret) { |
| 1206 | dev_err(pctrl->dev, "failed to add irqchip\n"); |
Mika Westerberg | f25c3aa | 2017-01-10 17:31:57 +0300 | [diff] [blame] | 1207 | return ret; |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1208 | } |
| 1209 | |
| 1210 | gpiochip_set_chained_irqchip(&pctrl->chip, &intel_gpio_irqchip, irq, |
Mika Westerberg | 193b40c | 2015-10-21 13:08:43 +0300 | [diff] [blame] | 1211 | NULL); |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1212 | return 0; |
| 1213 | } |
| 1214 | |
Mika Westerberg | 919eb47 | 2017-06-06 16:18:17 +0300 | [diff] [blame] | 1215 | static int intel_pinctrl_add_padgroups(struct intel_pinctrl *pctrl, |
| 1216 | struct intel_community *community) |
| 1217 | { |
| 1218 | struct intel_padgroup *gpps; |
Andy Shevchenko | 04035f7 | 2018-09-26 17:50:26 +0300 | [diff] [blame] | 1219 | unsigned int npins = community->npins; |
| 1220 | unsigned int padown_num = 0; |
Mika Westerberg | 919eb47 | 2017-06-06 16:18:17 +0300 | [diff] [blame] | 1221 | size_t ngpps, i; |
| 1222 | |
| 1223 | if (community->gpps) |
| 1224 | ngpps = community->ngpps; |
| 1225 | else |
| 1226 | ngpps = DIV_ROUND_UP(community->npins, community->gpp_size); |
| 1227 | |
| 1228 | gpps = devm_kcalloc(pctrl->dev, ngpps, sizeof(*gpps), GFP_KERNEL); |
| 1229 | if (!gpps) |
| 1230 | return -ENOMEM; |
| 1231 | |
| 1232 | for (i = 0; i < ngpps; i++) { |
| 1233 | if (community->gpps) { |
| 1234 | gpps[i] = community->gpps[i]; |
| 1235 | } else { |
Andy Shevchenko | 04035f7 | 2018-09-26 17:50:26 +0300 | [diff] [blame] | 1236 | unsigned int gpp_size = community->gpp_size; |
Mika Westerberg | 919eb47 | 2017-06-06 16:18:17 +0300 | [diff] [blame] | 1237 | |
| 1238 | gpps[i].reg_num = i; |
| 1239 | gpps[i].base = community->pin_base + i * gpp_size; |
| 1240 | gpps[i].size = min(gpp_size, npins); |
| 1241 | npins -= gpps[i].size; |
| 1242 | } |
| 1243 | |
| 1244 | if (gpps[i].size > 32) |
| 1245 | return -EINVAL; |
| 1246 | |
Mika Westerberg | a60eac3 | 2017-11-27 16:54:43 +0300 | [diff] [blame] | 1247 | if (!gpps[i].gpio_base) |
| 1248 | gpps[i].gpio_base = gpps[i].base; |
| 1249 | |
Mika Westerberg | 919eb47 | 2017-06-06 16:18:17 +0300 | [diff] [blame] | 1250 | gpps[i].padown_num = padown_num; |
| 1251 | |
| 1252 | /* |
| 1253 | * In older hardware the number of padown registers per |
| 1254 | * group is fixed regardless of the group size. |
| 1255 | */ |
| 1256 | if (community->gpp_num_padown_regs) |
| 1257 | padown_num += community->gpp_num_padown_regs; |
| 1258 | else |
| 1259 | padown_num += DIV_ROUND_UP(gpps[i].size * 4, 32); |
| 1260 | } |
| 1261 | |
| 1262 | community->ngpps = ngpps; |
| 1263 | community->gpps = gpps; |
| 1264 | |
| 1265 | return 0; |
| 1266 | } |
| 1267 | |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1268 | static int intel_pinctrl_pm_init(struct intel_pinctrl *pctrl) |
| 1269 | { |
| 1270 | #ifdef CONFIG_PM_SLEEP |
| 1271 | const struct intel_pinctrl_soc_data *soc = pctrl->soc; |
| 1272 | struct intel_community_context *communities; |
| 1273 | struct intel_pad_context *pads; |
| 1274 | int i; |
| 1275 | |
| 1276 | pads = devm_kcalloc(pctrl->dev, soc->npins, sizeof(*pads), GFP_KERNEL); |
| 1277 | if (!pads) |
| 1278 | return -ENOMEM; |
| 1279 | |
| 1280 | communities = devm_kcalloc(pctrl->dev, pctrl->ncommunities, |
| 1281 | sizeof(*communities), GFP_KERNEL); |
| 1282 | if (!communities) |
| 1283 | return -ENOMEM; |
| 1284 | |
| 1285 | |
| 1286 | for (i = 0; i < pctrl->ncommunities; i++) { |
| 1287 | struct intel_community *community = &pctrl->communities[i]; |
Chris Chiu | a0a5f76 | 2019-04-15 13:53:58 +0800 | [diff] [blame] | 1288 | u32 *intmask, *hostown; |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1289 | |
| 1290 | intmask = devm_kcalloc(pctrl->dev, community->ngpps, |
| 1291 | sizeof(*intmask), GFP_KERNEL); |
| 1292 | if (!intmask) |
| 1293 | return -ENOMEM; |
| 1294 | |
| 1295 | communities[i].intmask = intmask; |
Chris Chiu | a0a5f76 | 2019-04-15 13:53:58 +0800 | [diff] [blame] | 1296 | |
| 1297 | hostown = devm_kcalloc(pctrl->dev, community->ngpps, |
| 1298 | sizeof(*hostown), GFP_KERNEL); |
| 1299 | if (!hostown) |
| 1300 | return -ENOMEM; |
| 1301 | |
| 1302 | communities[i].hostown = hostown; |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1303 | } |
| 1304 | |
| 1305 | pctrl->context.pads = pads; |
| 1306 | pctrl->context.communities = communities; |
| 1307 | #endif |
| 1308 | |
| 1309 | return 0; |
| 1310 | } |
| 1311 | |
Andy Shevchenko | 0dd519e | 2018-10-17 19:10:27 +0300 | [diff] [blame] | 1312 | static int intel_pinctrl_probe(struct platform_device *pdev, |
| 1313 | const struct intel_pinctrl_soc_data *soc_data) |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1314 | { |
| 1315 | struct intel_pinctrl *pctrl; |
| 1316 | int i, ret, irq; |
| 1317 | |
| 1318 | if (!soc_data) |
| 1319 | return -EINVAL; |
| 1320 | |
| 1321 | pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL); |
| 1322 | if (!pctrl) |
| 1323 | return -ENOMEM; |
| 1324 | |
| 1325 | pctrl->dev = &pdev->dev; |
| 1326 | pctrl->soc = soc_data; |
Mika Westerberg | 27d9098 | 2016-06-16 11:25:36 +0300 | [diff] [blame] | 1327 | raw_spin_lock_init(&pctrl->lock); |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1328 | |
| 1329 | /* |
| 1330 | * Make a copy of the communities which we can use to hold pointers |
| 1331 | * to the registers. |
| 1332 | */ |
| 1333 | pctrl->ncommunities = pctrl->soc->ncommunities; |
| 1334 | pctrl->communities = devm_kcalloc(&pdev->dev, pctrl->ncommunities, |
| 1335 | sizeof(*pctrl->communities), GFP_KERNEL); |
| 1336 | if (!pctrl->communities) |
| 1337 | return -ENOMEM; |
| 1338 | |
| 1339 | for (i = 0; i < pctrl->ncommunities; i++) { |
| 1340 | struct intel_community *community = &pctrl->communities[i]; |
| 1341 | struct resource *res; |
| 1342 | void __iomem *regs; |
| 1343 | u32 padbar; |
| 1344 | |
| 1345 | *community = pctrl->soc->communities[i]; |
| 1346 | |
| 1347 | res = platform_get_resource(pdev, IORESOURCE_MEM, |
| 1348 | community->barno); |
| 1349 | regs = devm_ioremap_resource(&pdev->dev, res); |
| 1350 | if (IS_ERR(regs)) |
| 1351 | return PTR_ERR(regs); |
| 1352 | |
Mika Westerberg | e57725e | 2017-01-27 13:07:14 +0300 | [diff] [blame] | 1353 | /* |
| 1354 | * Determine community features based on the revision if |
| 1355 | * not specified already. |
| 1356 | */ |
| 1357 | if (!community->features) { |
| 1358 | u32 rev; |
| 1359 | |
| 1360 | rev = (readl(regs + REVID) & REVID_MASK) >> REVID_SHIFT; |
Mika Westerberg | 04cc058 | 2017-01-27 13:07:15 +0300 | [diff] [blame] | 1361 | if (rev >= 0x94) { |
Mika Westerberg | e57725e | 2017-01-27 13:07:14 +0300 | [diff] [blame] | 1362 | community->features |= PINCTRL_FEATURE_DEBOUNCE; |
Mika Westerberg | 04cc058 | 2017-01-27 13:07:15 +0300 | [diff] [blame] | 1363 | community->features |= PINCTRL_FEATURE_1K_PD; |
| 1364 | } |
Mika Westerberg | e57725e | 2017-01-27 13:07:14 +0300 | [diff] [blame] | 1365 | } |
| 1366 | |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1367 | /* Read offset of the pad configuration registers */ |
| 1368 | padbar = readl(regs + PADBAR); |
| 1369 | |
| 1370 | community->regs = regs; |
| 1371 | community->pad_regs = regs + padbar; |
Mika Westerberg | 919eb47 | 2017-06-06 16:18:17 +0300 | [diff] [blame] | 1372 | |
Mika Westerberg | cf769bd | 2017-10-23 15:40:25 +0300 | [diff] [blame] | 1373 | if (!community->is_offset) |
| 1374 | community->is_offset = GPI_IS; |
| 1375 | |
Mika Westerberg | 919eb47 | 2017-06-06 16:18:17 +0300 | [diff] [blame] | 1376 | ret = intel_pinctrl_add_padgroups(pctrl, community); |
| 1377 | if (ret) |
| 1378 | return ret; |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1379 | } |
| 1380 | |
| 1381 | irq = platform_get_irq(pdev, 0); |
| 1382 | if (irq < 0) { |
| 1383 | dev_err(&pdev->dev, "failed to get interrupt number\n"); |
| 1384 | return irq; |
| 1385 | } |
| 1386 | |
| 1387 | ret = intel_pinctrl_pm_init(pctrl); |
| 1388 | if (ret) |
| 1389 | return ret; |
| 1390 | |
| 1391 | pctrl->pctldesc = intel_pinctrl_desc; |
| 1392 | pctrl->pctldesc.name = dev_name(&pdev->dev); |
| 1393 | pctrl->pctldesc.pins = pctrl->soc->pins; |
| 1394 | pctrl->pctldesc.npins = pctrl->soc->npins; |
| 1395 | |
Laxman Dewangan | 54d46cd | 2016-02-28 14:42:47 +0530 | [diff] [blame] | 1396 | pctrl->pctldev = devm_pinctrl_register(&pdev->dev, &pctrl->pctldesc, |
| 1397 | pctrl); |
Masahiro Yamada | 323de9e | 2015-06-09 13:01:16 +0900 | [diff] [blame] | 1398 | if (IS_ERR(pctrl->pctldev)) { |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1399 | dev_err(&pdev->dev, "failed to register pinctrl driver\n"); |
Masahiro Yamada | 323de9e | 2015-06-09 13:01:16 +0900 | [diff] [blame] | 1400 | return PTR_ERR(pctrl->pctldev); |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1401 | } |
| 1402 | |
| 1403 | ret = intel_gpio_probe(pctrl, irq); |
Laxman Dewangan | 54d46cd | 2016-02-28 14:42:47 +0530 | [diff] [blame] | 1404 | if (ret) |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1405 | return ret; |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1406 | |
| 1407 | platform_set_drvdata(pdev, pctrl); |
| 1408 | |
| 1409 | return 0; |
| 1410 | } |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1411 | |
Andy Shevchenko | 70c263c | 2018-08-30 19:27:40 +0300 | [diff] [blame] | 1412 | int intel_pinctrl_probe_by_hid(struct platform_device *pdev) |
| 1413 | { |
| 1414 | const struct intel_pinctrl_soc_data *data; |
| 1415 | |
| 1416 | data = device_get_match_data(&pdev->dev); |
| 1417 | return intel_pinctrl_probe(pdev, data); |
| 1418 | } |
| 1419 | EXPORT_SYMBOL_GPL(intel_pinctrl_probe_by_hid); |
| 1420 | |
Andy Shevchenko | 924cf80 | 2018-08-30 19:27:36 +0300 | [diff] [blame] | 1421 | int intel_pinctrl_probe_by_uid(struct platform_device *pdev) |
| 1422 | { |
| 1423 | const struct intel_pinctrl_soc_data *data = NULL; |
| 1424 | const struct intel_pinctrl_soc_data **table; |
| 1425 | struct acpi_device *adev; |
| 1426 | unsigned int i; |
| 1427 | |
| 1428 | adev = ACPI_COMPANION(&pdev->dev); |
| 1429 | if (adev) { |
| 1430 | const void *match = device_get_match_data(&pdev->dev); |
| 1431 | |
| 1432 | table = (const struct intel_pinctrl_soc_data **)match; |
| 1433 | for (i = 0; table[i]; i++) { |
| 1434 | if (!strcmp(adev->pnp.unique_id, table[i]->uid)) { |
| 1435 | data = table[i]; |
| 1436 | break; |
| 1437 | } |
| 1438 | } |
| 1439 | } else { |
| 1440 | const struct platform_device_id *id; |
| 1441 | |
| 1442 | id = platform_get_device_id(pdev); |
| 1443 | if (!id) |
| 1444 | return -ENODEV; |
| 1445 | |
| 1446 | table = (const struct intel_pinctrl_soc_data **)id->driver_data; |
| 1447 | data = table[pdev->id]; |
| 1448 | } |
| 1449 | if (!data) |
| 1450 | return -ENODEV; |
| 1451 | |
| 1452 | return intel_pinctrl_probe(pdev, data); |
| 1453 | } |
| 1454 | EXPORT_SYMBOL_GPL(intel_pinctrl_probe_by_uid); |
| 1455 | |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1456 | #ifdef CONFIG_PM_SLEEP |
Andy Shevchenko | 04035f7 | 2018-09-26 17:50:26 +0300 | [diff] [blame] | 1457 | static bool intel_pinctrl_should_save(struct intel_pinctrl *pctrl, unsigned int pin) |
Mika Westerberg | c538b94 | 2016-10-10 16:39:31 +0300 | [diff] [blame] | 1458 | { |
| 1459 | const struct pin_desc *pd = pin_desc_get(pctrl->pctldev, pin); |
| 1460 | |
| 1461 | if (!pd || !intel_pad_usable(pctrl, pin)) |
| 1462 | return false; |
| 1463 | |
| 1464 | /* |
| 1465 | * Only restore the pin if it is actually in use by the kernel (or |
| 1466 | * by userspace). It is possible that some pins are used by the |
| 1467 | * BIOS during resume and those are not always locked down so leave |
| 1468 | * them alone. |
| 1469 | */ |
| 1470 | if (pd->mux_owner || pd->gpio_owner || |
| 1471 | gpiochip_line_is_irq(&pctrl->chip, pin)) |
| 1472 | return true; |
| 1473 | |
| 1474 | return false; |
| 1475 | } |
| 1476 | |
Binbin Wu | 2fef327 | 2019-04-08 18:49:26 +0800 | [diff] [blame] | 1477 | int intel_pinctrl_suspend_noirq(struct device *dev) |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1478 | { |
Wolfram Sang | cb035d7 | 2018-10-21 22:00:29 +0200 | [diff] [blame] | 1479 | struct intel_pinctrl *pctrl = dev_get_drvdata(dev); |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1480 | struct intel_community_context *communities; |
| 1481 | struct intel_pad_context *pads; |
| 1482 | int i; |
| 1483 | |
| 1484 | pads = pctrl->context.pads; |
| 1485 | for (i = 0; i < pctrl->soc->npins; i++) { |
| 1486 | const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i]; |
Mika Westerberg | e57725e | 2017-01-27 13:07:14 +0300 | [diff] [blame] | 1487 | void __iomem *padcfg; |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1488 | u32 val; |
| 1489 | |
Mika Westerberg | c538b94 | 2016-10-10 16:39:31 +0300 | [diff] [blame] | 1490 | if (!intel_pinctrl_should_save(pctrl, desc->number)) |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1491 | continue; |
| 1492 | |
| 1493 | val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG0)); |
| 1494 | pads[i].padcfg0 = val & ~PADCFG0_GPIORXSTATE; |
| 1495 | val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG1)); |
| 1496 | pads[i].padcfg1 = val; |
Mika Westerberg | e57725e | 2017-01-27 13:07:14 +0300 | [diff] [blame] | 1497 | |
| 1498 | padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG2); |
| 1499 | if (padcfg) |
| 1500 | pads[i].padcfg2 = readl(padcfg); |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1501 | } |
| 1502 | |
| 1503 | communities = pctrl->context.communities; |
| 1504 | for (i = 0; i < pctrl->ncommunities; i++) { |
| 1505 | struct intel_community *community = &pctrl->communities[i]; |
| 1506 | void __iomem *base; |
Andy Shevchenko | 04035f7 | 2018-09-26 17:50:26 +0300 | [diff] [blame] | 1507 | unsigned int gpp; |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1508 | |
| 1509 | base = community->regs + community->ie_offset; |
| 1510 | for (gpp = 0; gpp < community->ngpps; gpp++) |
| 1511 | communities[i].intmask[gpp] = readl(base + gpp * 4); |
Chris Chiu | a0a5f76 | 2019-04-15 13:53:58 +0800 | [diff] [blame] | 1512 | |
| 1513 | base = community->regs + community->hostown_offset; |
| 1514 | for (gpp = 0; gpp < community->ngpps; gpp++) |
| 1515 | communities[i].hostown[gpp] = readl(base + gpp * 4); |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1516 | } |
| 1517 | |
| 1518 | return 0; |
| 1519 | } |
Binbin Wu | 2fef327 | 2019-04-08 18:49:26 +0800 | [diff] [blame] | 1520 | EXPORT_SYMBOL_GPL(intel_pinctrl_suspend_noirq); |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1521 | |
Mika Westerberg | f487bbf | 2015-10-13 17:51:25 +0300 | [diff] [blame] | 1522 | static void intel_gpio_irq_init(struct intel_pinctrl *pctrl) |
| 1523 | { |
| 1524 | size_t i; |
| 1525 | |
| 1526 | for (i = 0; i < pctrl->ncommunities; i++) { |
| 1527 | const struct intel_community *community; |
| 1528 | void __iomem *base; |
Andy Shevchenko | 04035f7 | 2018-09-26 17:50:26 +0300 | [diff] [blame] | 1529 | unsigned int gpp; |
Mika Westerberg | f487bbf | 2015-10-13 17:51:25 +0300 | [diff] [blame] | 1530 | |
| 1531 | community = &pctrl->communities[i]; |
| 1532 | base = community->regs; |
| 1533 | |
| 1534 | for (gpp = 0; gpp < community->ngpps; gpp++) { |
| 1535 | /* Mask and clear all interrupts */ |
| 1536 | writel(0, base + community->ie_offset + gpp * 4); |
Mika Westerberg | cf769bd | 2017-10-23 15:40:25 +0300 | [diff] [blame] | 1537 | writel(0xffff, base + community->is_offset + gpp * 4); |
Mika Westerberg | f487bbf | 2015-10-13 17:51:25 +0300 | [diff] [blame] | 1538 | } |
| 1539 | } |
| 1540 | } |
| 1541 | |
Chris Chiu | a0a5f76 | 2019-04-15 13:53:58 +0800 | [diff] [blame] | 1542 | static u32 |
| 1543 | intel_gpio_is_requested(struct gpio_chip *chip, int base, unsigned int size) |
| 1544 | { |
| 1545 | u32 requested = 0; |
| 1546 | unsigned int i; |
| 1547 | |
| 1548 | for (i = 0; i < size; i++) |
| 1549 | if (gpiochip_is_requested(chip, base + i)) |
| 1550 | requested |= BIT(i); |
| 1551 | |
| 1552 | return requested; |
| 1553 | } |
| 1554 | |
| 1555 | static u32 |
| 1556 | intel_gpio_update_pad_mode(void __iomem *hostown, u32 mask, u32 value) |
| 1557 | { |
Andy Shevchenko | 5f61d95 | 2019-04-28 20:19:06 +0300 | [diff] [blame^] | 1558 | u32 curr, updated; |
Chris Chiu | a0a5f76 | 2019-04-15 13:53:58 +0800 | [diff] [blame] | 1559 | |
Andy Shevchenko | 5f61d95 | 2019-04-28 20:19:06 +0300 | [diff] [blame^] | 1560 | curr = readl(hostown); |
| 1561 | updated = (curr & ~mask) | (value & mask); |
Chris Chiu | a0a5f76 | 2019-04-15 13:53:58 +0800 | [diff] [blame] | 1562 | writel(updated, hostown); |
Andy Shevchenko | 5f61d95 | 2019-04-28 20:19:06 +0300 | [diff] [blame^] | 1563 | |
Chris Chiu | a0a5f76 | 2019-04-15 13:53:58 +0800 | [diff] [blame] | 1564 | return curr; |
| 1565 | } |
| 1566 | |
Binbin Wu | 2fef327 | 2019-04-08 18:49:26 +0800 | [diff] [blame] | 1567 | int intel_pinctrl_resume_noirq(struct device *dev) |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1568 | { |
Wolfram Sang | cb035d7 | 2018-10-21 22:00:29 +0200 | [diff] [blame] | 1569 | struct intel_pinctrl *pctrl = dev_get_drvdata(dev); |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1570 | const struct intel_community_context *communities; |
| 1571 | const struct intel_pad_context *pads; |
| 1572 | int i; |
| 1573 | |
| 1574 | /* Mask all interrupts */ |
| 1575 | intel_gpio_irq_init(pctrl); |
| 1576 | |
| 1577 | pads = pctrl->context.pads; |
| 1578 | for (i = 0; i < pctrl->soc->npins; i++) { |
| 1579 | const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i]; |
| 1580 | void __iomem *padcfg; |
| 1581 | u32 val; |
| 1582 | |
Mika Westerberg | c538b94 | 2016-10-10 16:39:31 +0300 | [diff] [blame] | 1583 | if (!intel_pinctrl_should_save(pctrl, desc->number)) |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1584 | continue; |
| 1585 | |
| 1586 | padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG0); |
| 1587 | val = readl(padcfg) & ~PADCFG0_GPIORXSTATE; |
| 1588 | if (val != pads[i].padcfg0) { |
| 1589 | writel(pads[i].padcfg0, padcfg); |
| 1590 | dev_dbg(dev, "restored pin %u padcfg0 %#08x\n", |
| 1591 | desc->number, readl(padcfg)); |
| 1592 | } |
| 1593 | |
| 1594 | padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG1); |
| 1595 | val = readl(padcfg); |
| 1596 | if (val != pads[i].padcfg1) { |
| 1597 | writel(pads[i].padcfg1, padcfg); |
| 1598 | dev_dbg(dev, "restored pin %u padcfg1 %#08x\n", |
| 1599 | desc->number, readl(padcfg)); |
| 1600 | } |
Mika Westerberg | e57725e | 2017-01-27 13:07:14 +0300 | [diff] [blame] | 1601 | |
| 1602 | padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG2); |
| 1603 | if (padcfg) { |
| 1604 | val = readl(padcfg); |
| 1605 | if (val != pads[i].padcfg2) { |
| 1606 | writel(pads[i].padcfg2, padcfg); |
| 1607 | dev_dbg(dev, "restored pin %u padcfg2 %#08x\n", |
| 1608 | desc->number, readl(padcfg)); |
| 1609 | } |
| 1610 | } |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1611 | } |
| 1612 | |
| 1613 | communities = pctrl->context.communities; |
| 1614 | for (i = 0; i < pctrl->ncommunities; i++) { |
| 1615 | struct intel_community *community = &pctrl->communities[i]; |
| 1616 | void __iomem *base; |
Andy Shevchenko | 04035f7 | 2018-09-26 17:50:26 +0300 | [diff] [blame] | 1617 | unsigned int gpp; |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1618 | |
| 1619 | base = community->regs + community->ie_offset; |
| 1620 | for (gpp = 0; gpp < community->ngpps; gpp++) { |
| 1621 | writel(communities[i].intmask[gpp], base + gpp * 4); |
| 1622 | dev_dbg(dev, "restored mask %d/%u %#08x\n", i, gpp, |
| 1623 | readl(base + gpp * 4)); |
| 1624 | } |
Chris Chiu | a0a5f76 | 2019-04-15 13:53:58 +0800 | [diff] [blame] | 1625 | |
| 1626 | base = community->regs + community->hostown_offset; |
| 1627 | for (gpp = 0; gpp < community->ngpps; gpp++) { |
| 1628 | const struct intel_padgroup *padgrp = &community->gpps[gpp]; |
| 1629 | u32 requested = 0, value = 0; |
| 1630 | u32 saved = communities[i].hostown[gpp]; |
| 1631 | |
| 1632 | if (padgrp->gpio_base < 0) |
| 1633 | continue; |
| 1634 | |
| 1635 | requested = intel_gpio_is_requested(&pctrl->chip, |
| 1636 | padgrp->gpio_base, padgrp->size); |
| 1637 | value = intel_gpio_update_pad_mode(base + gpp * 4, |
| 1638 | requested, saved); |
| 1639 | if ((value ^ saved) & requested) { |
| 1640 | dev_warn(dev, "restore hostown %d/%u %#8x->%#8x\n", |
| 1641 | i, gpp, value, saved); |
| 1642 | } |
| 1643 | } |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1644 | } |
| 1645 | |
| 1646 | return 0; |
| 1647 | } |
Binbin Wu | 2fef327 | 2019-04-08 18:49:26 +0800 | [diff] [blame] | 1648 | EXPORT_SYMBOL_GPL(intel_pinctrl_resume_noirq); |
Mika Westerberg | 7981c001 | 2015-03-30 17:31:49 +0300 | [diff] [blame] | 1649 | #endif |
| 1650 | |
| 1651 | MODULE_AUTHOR("Mathias Nyman <mathias.nyman@linux.intel.com>"); |
| 1652 | MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>"); |
| 1653 | MODULE_DESCRIPTION("Intel pinctrl/GPIO core driver"); |
| 1654 | MODULE_LICENSE("GPL v2"); |