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