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