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