Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / pinctrl / intel / pinctrl-intel.c
blobb6ef1911c1dd16a3060a1e95d08e21f41d3fe8d3
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Intel pinctrl/GPIO core driver.
5 * Copyright (C) 2015, Intel Corporation
6 * Authors: Mathias Nyman <mathias.nyman@linux.intel.com>
7 * Mika Westerberg <mika.westerberg@linux.intel.com>
8 */
10 #include <linux/acpi.h>
11 #include <linux/gpio/driver.h>
12 #include <linux/interrupt.h>
13 #include <linux/log2.h>
14 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/property.h>
17 #include <linux/time.h>
19 #include <linux/pinctrl/pinctrl.h>
20 #include <linux/pinctrl/pinmux.h>
21 #include <linux/pinctrl/pinconf.h>
22 #include <linux/pinctrl/pinconf-generic.h>
24 #include "../core.h"
25 #include "pinctrl-intel.h"
27 /* Offset from regs */
28 #define REVID 0x000
29 #define REVID_SHIFT 16
30 #define REVID_MASK GENMASK(31, 16)
32 #define PADBAR 0x00c
34 #define PADOWN_BITS 4
35 #define PADOWN_SHIFT(p) ((p) % 8 * PADOWN_BITS)
36 #define PADOWN_MASK(p) (GENMASK(3, 0) << PADOWN_SHIFT(p))
37 #define PADOWN_GPP(p) ((p) / 8)
39 /* Offset from pad_regs */
40 #define PADCFG0 0x000
41 #define PADCFG0_RXEVCFG_SHIFT 25
42 #define PADCFG0_RXEVCFG_MASK GENMASK(26, 25)
43 #define PADCFG0_RXEVCFG_LEVEL 0
44 #define PADCFG0_RXEVCFG_EDGE 1
45 #define PADCFG0_RXEVCFG_DISABLED 2
46 #define PADCFG0_RXEVCFG_EDGE_BOTH 3
47 #define PADCFG0_PREGFRXSEL BIT(24)
48 #define PADCFG0_RXINV BIT(23)
49 #define PADCFG0_GPIROUTIOXAPIC BIT(20)
50 #define PADCFG0_GPIROUTSCI BIT(19)
51 #define PADCFG0_GPIROUTSMI BIT(18)
52 #define PADCFG0_GPIROUTNMI BIT(17)
53 #define PADCFG0_PMODE_SHIFT 10
54 #define PADCFG0_PMODE_MASK GENMASK(13, 10)
55 #define PADCFG0_PMODE_GPIO 0
56 #define PADCFG0_GPIORXDIS BIT(9)
57 #define PADCFG0_GPIOTXDIS BIT(8)
58 #define PADCFG0_GPIORXSTATE BIT(1)
59 #define PADCFG0_GPIOTXSTATE BIT(0)
61 #define PADCFG1 0x004
62 #define PADCFG1_TERM_UP BIT(13)
63 #define PADCFG1_TERM_SHIFT 10
64 #define PADCFG1_TERM_MASK GENMASK(12, 10)
65 #define PADCFG1_TERM_20K BIT(2)
66 #define PADCFG1_TERM_5K BIT(1)
67 #define PADCFG1_TERM_1K BIT(0)
68 #define PADCFG1_TERM_833 (BIT(1) | BIT(0))
70 #define PADCFG2 0x008
71 #define PADCFG2_DEBEN BIT(0)
72 #define PADCFG2_DEBOUNCE_SHIFT 1
73 #define PADCFG2_DEBOUNCE_MASK GENMASK(4, 1)
75 #define DEBOUNCE_PERIOD_NSEC 31250
77 struct intel_pad_context {
78 u32 padcfg0;
79 u32 padcfg1;
80 u32 padcfg2;
83 struct intel_community_context {
84 u32 *intmask;
85 u32 *hostown;
88 #define pin_to_padno(c, p) ((p) - (c)->pin_base)
89 #define padgroup_offset(g, p) ((p) - (g)->base)
91 static struct intel_community *intel_get_community(struct intel_pinctrl *pctrl,
92 unsigned int pin)
94 struct intel_community *community;
95 int i;
97 for (i = 0; i < pctrl->ncommunities; i++) {
98 community = &pctrl->communities[i];
99 if (pin >= community->pin_base &&
100 pin < community->pin_base + community->npins)
101 return community;
104 dev_warn(pctrl->dev, "failed to find community for pin %u\n", pin);
105 return NULL;
108 static const struct intel_padgroup *
109 intel_community_get_padgroup(const struct intel_community *community,
110 unsigned int pin)
112 int i;
114 for (i = 0; i < community->ngpps; i++) {
115 const struct intel_padgroup *padgrp = &community->gpps[i];
117 if (pin >= padgrp->base && pin < padgrp->base + padgrp->size)
118 return padgrp;
121 return NULL;
124 static void __iomem *intel_get_padcfg(struct intel_pinctrl *pctrl,
125 unsigned int pin, unsigned int reg)
127 const struct intel_community *community;
128 unsigned int padno;
129 size_t nregs;
131 community = intel_get_community(pctrl, pin);
132 if (!community)
133 return NULL;
135 padno = pin_to_padno(community, pin);
136 nregs = (community->features & PINCTRL_FEATURE_DEBOUNCE) ? 4 : 2;
138 if (reg >= nregs * 4)
139 return NULL;
141 return community->pad_regs + reg + padno * nregs * 4;
144 static bool intel_pad_owned_by_host(struct intel_pinctrl *pctrl, unsigned int pin)
146 const struct intel_community *community;
147 const struct intel_padgroup *padgrp;
148 unsigned int gpp, offset, gpp_offset;
149 void __iomem *padown;
151 community = intel_get_community(pctrl, pin);
152 if (!community)
153 return false;
154 if (!community->padown_offset)
155 return true;
157 padgrp = intel_community_get_padgroup(community, pin);
158 if (!padgrp)
159 return false;
161 gpp_offset = padgroup_offset(padgrp, pin);
162 gpp = PADOWN_GPP(gpp_offset);
163 offset = community->padown_offset + padgrp->padown_num * 4 + gpp * 4;
164 padown = community->regs + offset;
166 return !(readl(padown) & PADOWN_MASK(gpp_offset));
169 static bool intel_pad_acpi_mode(struct intel_pinctrl *pctrl, unsigned int pin)
171 const struct intel_community *community;
172 const struct intel_padgroup *padgrp;
173 unsigned int offset, gpp_offset;
174 void __iomem *hostown;
176 community = intel_get_community(pctrl, pin);
177 if (!community)
178 return true;
179 if (!community->hostown_offset)
180 return false;
182 padgrp = intel_community_get_padgroup(community, pin);
183 if (!padgrp)
184 return true;
186 gpp_offset = padgroup_offset(padgrp, pin);
187 offset = community->hostown_offset + padgrp->reg_num * 4;
188 hostown = community->regs + offset;
190 return !(readl(hostown) & BIT(gpp_offset));
194 * enum - Locking variants of the pad configuration
196 * @PAD_UNLOCKED: pad is fully controlled by the configuration registers
197 * @PAD_LOCKED: pad configuration registers, except TX state, are locked
198 * @PAD_LOCKED_TX: pad configuration TX state is locked
199 * @PAD_LOCKED_FULL: pad configuration registers are locked completely
201 * Locking is considered as read-only mode for corresponding registers and
202 * their respective fields. That said, TX state bit is locked separately from
203 * the main locking scheme.
205 enum {
206 PAD_UNLOCKED = 0,
207 PAD_LOCKED = 1,
208 PAD_LOCKED_TX = 2,
209 PAD_LOCKED_FULL = PAD_LOCKED | PAD_LOCKED_TX,
212 static int intel_pad_locked(struct intel_pinctrl *pctrl, unsigned int pin)
214 struct intel_community *community;
215 const struct intel_padgroup *padgrp;
216 unsigned int offset, gpp_offset;
217 u32 value;
218 int ret = PAD_UNLOCKED;
220 community = intel_get_community(pctrl, pin);
221 if (!community)
222 return PAD_LOCKED_FULL;
223 if (!community->padcfglock_offset)
224 return PAD_UNLOCKED;
226 padgrp = intel_community_get_padgroup(community, pin);
227 if (!padgrp)
228 return PAD_LOCKED_FULL;
230 gpp_offset = padgroup_offset(padgrp, pin);
233 * If PADCFGLOCK and PADCFGLOCKTX bits are both clear for this pad,
234 * the pad is considered unlocked. Any other case means that it is
235 * either fully or partially locked.
237 offset = community->padcfglock_offset + 0 + padgrp->reg_num * 8;
238 value = readl(community->regs + offset);
239 if (value & BIT(gpp_offset))
240 ret |= PAD_LOCKED;
242 offset = community->padcfglock_offset + 4 + padgrp->reg_num * 8;
243 value = readl(community->regs + offset);
244 if (value & BIT(gpp_offset))
245 ret |= PAD_LOCKED_TX;
247 return ret;
250 static bool intel_pad_is_unlocked(struct intel_pinctrl *pctrl, unsigned int pin)
252 return (intel_pad_locked(pctrl, pin) & PAD_LOCKED) == PAD_UNLOCKED;
255 static bool intel_pad_usable(struct intel_pinctrl *pctrl, unsigned int pin)
257 return intel_pad_owned_by_host(pctrl, pin) && intel_pad_is_unlocked(pctrl, pin);
260 static int intel_get_groups_count(struct pinctrl_dev *pctldev)
262 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
264 return pctrl->soc->ngroups;
267 static const char *intel_get_group_name(struct pinctrl_dev *pctldev,
268 unsigned int group)
270 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
272 return pctrl->soc->groups[group].name;
275 static int intel_get_group_pins(struct pinctrl_dev *pctldev, unsigned int group,
276 const unsigned int **pins, unsigned int *npins)
278 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
280 *pins = pctrl->soc->groups[group].pins;
281 *npins = pctrl->soc->groups[group].npins;
282 return 0;
285 static void intel_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
286 unsigned int pin)
288 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
289 void __iomem *padcfg;
290 u32 cfg0, cfg1, mode;
291 int locked;
292 bool acpi;
294 if (!intel_pad_owned_by_host(pctrl, pin)) {
295 seq_puts(s, "not available");
296 return;
299 cfg0 = readl(intel_get_padcfg(pctrl, pin, PADCFG0));
300 cfg1 = readl(intel_get_padcfg(pctrl, pin, PADCFG1));
302 mode = (cfg0 & PADCFG0_PMODE_MASK) >> PADCFG0_PMODE_SHIFT;
303 if (mode == PADCFG0_PMODE_GPIO)
304 seq_puts(s, "GPIO ");
305 else
306 seq_printf(s, "mode %d ", mode);
308 seq_printf(s, "0x%08x 0x%08x", cfg0, cfg1);
310 /* Dump the additional PADCFG registers if available */
311 padcfg = intel_get_padcfg(pctrl, pin, PADCFG2);
312 if (padcfg)
313 seq_printf(s, " 0x%08x", readl(padcfg));
315 locked = intel_pad_locked(pctrl, pin);
316 acpi = intel_pad_acpi_mode(pctrl, pin);
318 if (locked || acpi) {
319 seq_puts(s, " [");
320 if (locked)
321 seq_puts(s, "LOCKED");
322 if ((locked & PAD_LOCKED_FULL) == PAD_LOCKED_TX)
323 seq_puts(s, " tx");
324 else if ((locked & PAD_LOCKED_FULL) == PAD_LOCKED_FULL)
325 seq_puts(s, " full");
327 if (locked && acpi)
328 seq_puts(s, ", ");
330 if (acpi)
331 seq_puts(s, "ACPI");
332 seq_puts(s, "]");
336 static const struct pinctrl_ops intel_pinctrl_ops = {
337 .get_groups_count = intel_get_groups_count,
338 .get_group_name = intel_get_group_name,
339 .get_group_pins = intel_get_group_pins,
340 .pin_dbg_show = intel_pin_dbg_show,
343 static int intel_get_functions_count(struct pinctrl_dev *pctldev)
345 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
347 return pctrl->soc->nfunctions;
350 static const char *intel_get_function_name(struct pinctrl_dev *pctldev,
351 unsigned int function)
353 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
355 return pctrl->soc->functions[function].name;
358 static int intel_get_function_groups(struct pinctrl_dev *pctldev,
359 unsigned int function,
360 const char * const **groups,
361 unsigned int * const ngroups)
363 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
365 *groups = pctrl->soc->functions[function].groups;
366 *ngroups = pctrl->soc->functions[function].ngroups;
367 return 0;
370 static int intel_pinmux_set_mux(struct pinctrl_dev *pctldev,
371 unsigned int function, unsigned int group)
373 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
374 const struct intel_pingroup *grp = &pctrl->soc->groups[group];
375 unsigned long flags;
376 int i;
378 raw_spin_lock_irqsave(&pctrl->lock, flags);
381 * All pins in the groups needs to be accessible and writable
382 * before we can enable the mux for this group.
384 for (i = 0; i < grp->npins; i++) {
385 if (!intel_pad_usable(pctrl, grp->pins[i])) {
386 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
387 return -EBUSY;
391 /* Now enable the mux setting for each pin in the group */
392 for (i = 0; i < grp->npins; i++) {
393 void __iomem *padcfg0;
394 u32 value;
396 padcfg0 = intel_get_padcfg(pctrl, grp->pins[i], PADCFG0);
397 value = readl(padcfg0);
399 value &= ~PADCFG0_PMODE_MASK;
401 if (grp->modes)
402 value |= grp->modes[i] << PADCFG0_PMODE_SHIFT;
403 else
404 value |= grp->mode << PADCFG0_PMODE_SHIFT;
406 writel(value, padcfg0);
409 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
411 return 0;
414 static void __intel_gpio_set_direction(void __iomem *padcfg0, bool input)
416 u32 value;
418 value = readl(padcfg0);
419 if (input) {
420 value &= ~PADCFG0_GPIORXDIS;
421 value |= PADCFG0_GPIOTXDIS;
422 } else {
423 value &= ~PADCFG0_GPIOTXDIS;
424 value |= PADCFG0_GPIORXDIS;
426 writel(value, padcfg0);
429 static int intel_gpio_get_gpio_mode(void __iomem *padcfg0)
431 return (readl(padcfg0) & PADCFG0_PMODE_MASK) >> PADCFG0_PMODE_SHIFT;
434 static void intel_gpio_set_gpio_mode(void __iomem *padcfg0)
436 u32 value;
438 value = readl(padcfg0);
440 /* Put the pad into GPIO mode */
441 value &= ~PADCFG0_PMODE_MASK;
442 value |= PADCFG0_PMODE_GPIO;
444 /* Disable input and output buffers */
445 value |= PADCFG0_GPIORXDIS;
446 value |= PADCFG0_GPIOTXDIS;
448 /* Disable SCI/SMI/NMI generation */
449 value &= ~(PADCFG0_GPIROUTIOXAPIC | PADCFG0_GPIROUTSCI);
450 value &= ~(PADCFG0_GPIROUTSMI | PADCFG0_GPIROUTNMI);
452 writel(value, padcfg0);
455 static int intel_gpio_request_enable(struct pinctrl_dev *pctldev,
456 struct pinctrl_gpio_range *range,
457 unsigned int pin)
459 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
460 void __iomem *padcfg0;
461 unsigned long flags;
463 padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
465 raw_spin_lock_irqsave(&pctrl->lock, flags);
467 if (!intel_pad_owned_by_host(pctrl, pin)) {
468 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
469 return -EBUSY;
472 if (!intel_pad_is_unlocked(pctrl, pin)) {
473 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
474 return 0;
478 * If pin is already configured in GPIO mode, we assume that
479 * firmware provides correct settings. In such case we avoid
480 * potential glitches on the pin. Otherwise, for the pin in
481 * alternative mode, consumer has to supply respective flags.
483 if (intel_gpio_get_gpio_mode(padcfg0) == PADCFG0_PMODE_GPIO) {
484 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
485 return 0;
488 intel_gpio_set_gpio_mode(padcfg0);
490 /* Disable TX buffer and enable RX (this will be input) */
491 __intel_gpio_set_direction(padcfg0, true);
493 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
495 return 0;
498 static int intel_gpio_set_direction(struct pinctrl_dev *pctldev,
499 struct pinctrl_gpio_range *range,
500 unsigned int pin, bool input)
502 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
503 void __iomem *padcfg0;
504 unsigned long flags;
506 padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
508 raw_spin_lock_irqsave(&pctrl->lock, flags);
509 __intel_gpio_set_direction(padcfg0, input);
510 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
512 return 0;
515 static const struct pinmux_ops intel_pinmux_ops = {
516 .get_functions_count = intel_get_functions_count,
517 .get_function_name = intel_get_function_name,
518 .get_function_groups = intel_get_function_groups,
519 .set_mux = intel_pinmux_set_mux,
520 .gpio_request_enable = intel_gpio_request_enable,
521 .gpio_set_direction = intel_gpio_set_direction,
524 static int intel_config_get_pull(struct intel_pinctrl *pctrl, unsigned int pin,
525 enum pin_config_param param, u32 *arg)
527 const struct intel_community *community;
528 void __iomem *padcfg1;
529 unsigned long flags;
530 u32 value, term;
532 community = intel_get_community(pctrl, pin);
533 padcfg1 = intel_get_padcfg(pctrl, pin, PADCFG1);
535 raw_spin_lock_irqsave(&pctrl->lock, flags);
536 value = readl(padcfg1);
537 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
539 term = (value & PADCFG1_TERM_MASK) >> PADCFG1_TERM_SHIFT;
541 switch (param) {
542 case PIN_CONFIG_BIAS_DISABLE:
543 if (term)
544 return -EINVAL;
545 break;
547 case PIN_CONFIG_BIAS_PULL_UP:
548 if (!term || !(value & PADCFG1_TERM_UP))
549 return -EINVAL;
551 switch (term) {
552 case PADCFG1_TERM_833:
553 *arg = 833;
554 break;
555 case PADCFG1_TERM_1K:
556 *arg = 1000;
557 break;
558 case PADCFG1_TERM_5K:
559 *arg = 5000;
560 break;
561 case PADCFG1_TERM_20K:
562 *arg = 20000;
563 break;
566 break;
568 case PIN_CONFIG_BIAS_PULL_DOWN:
569 if (!term || value & PADCFG1_TERM_UP)
570 return -EINVAL;
572 switch (term) {
573 case PADCFG1_TERM_833:
574 if (!(community->features & PINCTRL_FEATURE_1K_PD))
575 return -EINVAL;
576 *arg = 833;
577 break;
578 case PADCFG1_TERM_1K:
579 if (!(community->features & PINCTRL_FEATURE_1K_PD))
580 return -EINVAL;
581 *arg = 1000;
582 break;
583 case PADCFG1_TERM_5K:
584 *arg = 5000;
585 break;
586 case PADCFG1_TERM_20K:
587 *arg = 20000;
588 break;
591 break;
593 default:
594 return -EINVAL;
597 return 0;
600 static int intel_config_get_debounce(struct intel_pinctrl *pctrl, unsigned int pin,
601 enum pin_config_param param, u32 *arg)
603 void __iomem *padcfg2;
604 unsigned long flags;
605 unsigned long v;
606 u32 value2;
608 padcfg2 = intel_get_padcfg(pctrl, pin, PADCFG2);
609 if (!padcfg2)
610 return -ENOTSUPP;
612 raw_spin_lock_irqsave(&pctrl->lock, flags);
613 value2 = readl(padcfg2);
614 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
615 if (!(value2 & PADCFG2_DEBEN))
616 return -EINVAL;
618 v = (value2 & PADCFG2_DEBOUNCE_MASK) >> PADCFG2_DEBOUNCE_SHIFT;
619 *arg = BIT(v) * DEBOUNCE_PERIOD_NSEC / NSEC_PER_USEC;
621 return 0;
624 static int intel_config_get(struct pinctrl_dev *pctldev, unsigned int pin,
625 unsigned long *config)
627 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
628 enum pin_config_param param = pinconf_to_config_param(*config);
629 u32 arg = 0;
630 int ret;
632 if (!intel_pad_owned_by_host(pctrl, pin))
633 return -ENOTSUPP;
635 switch (param) {
636 case PIN_CONFIG_BIAS_DISABLE:
637 case PIN_CONFIG_BIAS_PULL_UP:
638 case PIN_CONFIG_BIAS_PULL_DOWN:
639 ret = intel_config_get_pull(pctrl, pin, param, &arg);
640 if (ret)
641 return ret;
642 break;
644 case PIN_CONFIG_INPUT_DEBOUNCE:
645 ret = intel_config_get_debounce(pctrl, pin, param, &arg);
646 if (ret)
647 return ret;
648 break;
650 default:
651 return -ENOTSUPP;
654 *config = pinconf_to_config_packed(param, arg);
655 return 0;
658 static int intel_config_set_pull(struct intel_pinctrl *pctrl, unsigned int pin,
659 unsigned long config)
661 unsigned int param = pinconf_to_config_param(config);
662 unsigned int arg = pinconf_to_config_argument(config);
663 const struct intel_community *community;
664 void __iomem *padcfg1;
665 unsigned long flags;
666 int ret = 0;
667 u32 value;
669 community = intel_get_community(pctrl, pin);
670 padcfg1 = intel_get_padcfg(pctrl, pin, PADCFG1);
672 raw_spin_lock_irqsave(&pctrl->lock, flags);
674 value = readl(padcfg1);
676 switch (param) {
677 case PIN_CONFIG_BIAS_DISABLE:
678 value &= ~(PADCFG1_TERM_MASK | PADCFG1_TERM_UP);
679 break;
681 case PIN_CONFIG_BIAS_PULL_UP:
682 value &= ~PADCFG1_TERM_MASK;
684 value |= PADCFG1_TERM_UP;
686 /* Set default strength value in case none is given */
687 if (arg == 1)
688 arg = 5000;
690 switch (arg) {
691 case 20000:
692 value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT;
693 break;
694 case 5000:
695 value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT;
696 break;
697 case 1000:
698 value |= PADCFG1_TERM_1K << PADCFG1_TERM_SHIFT;
699 break;
700 case 833:
701 value |= PADCFG1_TERM_833 << PADCFG1_TERM_SHIFT;
702 break;
703 default:
704 ret = -EINVAL;
707 break;
709 case PIN_CONFIG_BIAS_PULL_DOWN:
710 value &= ~(PADCFG1_TERM_UP | PADCFG1_TERM_MASK);
712 /* Set default strength value in case none is given */
713 if (arg == 1)
714 arg = 5000;
716 switch (arg) {
717 case 20000:
718 value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT;
719 break;
720 case 5000:
721 value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT;
722 break;
723 case 1000:
724 if (!(community->features & PINCTRL_FEATURE_1K_PD)) {
725 ret = -EINVAL;
726 break;
728 value |= PADCFG1_TERM_1K << PADCFG1_TERM_SHIFT;
729 break;
730 case 833:
731 if (!(community->features & PINCTRL_FEATURE_1K_PD)) {
732 ret = -EINVAL;
733 break;
735 value |= PADCFG1_TERM_833 << PADCFG1_TERM_SHIFT;
736 break;
737 default:
738 ret = -EINVAL;
741 break;
744 if (!ret)
745 writel(value, padcfg1);
747 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
749 return ret;
752 static int intel_config_set_debounce(struct intel_pinctrl *pctrl,
753 unsigned int pin, unsigned int debounce)
755 void __iomem *padcfg0, *padcfg2;
756 unsigned long flags;
757 u32 value0, value2;
759 padcfg2 = intel_get_padcfg(pctrl, pin, PADCFG2);
760 if (!padcfg2)
761 return -ENOTSUPP;
763 padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
765 raw_spin_lock_irqsave(&pctrl->lock, flags);
767 value0 = readl(padcfg0);
768 value2 = readl(padcfg2);
770 /* Disable glitch filter and debouncer */
771 value0 &= ~PADCFG0_PREGFRXSEL;
772 value2 &= ~(PADCFG2_DEBEN | PADCFG2_DEBOUNCE_MASK);
774 if (debounce) {
775 unsigned long v;
777 v = order_base_2(debounce * NSEC_PER_USEC / DEBOUNCE_PERIOD_NSEC);
778 if (v < 3 || v > 15) {
779 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
780 return -EINVAL;
783 /* Enable glitch filter and debouncer */
784 value0 |= PADCFG0_PREGFRXSEL;
785 value2 |= v << PADCFG2_DEBOUNCE_SHIFT;
786 value2 |= PADCFG2_DEBEN;
789 writel(value0, padcfg0);
790 writel(value2, padcfg2);
792 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
794 return 0;
797 static int intel_config_set(struct pinctrl_dev *pctldev, unsigned int pin,
798 unsigned long *configs, unsigned int nconfigs)
800 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
801 int i, ret;
803 if (!intel_pad_usable(pctrl, pin))
804 return -ENOTSUPP;
806 for (i = 0; i < nconfigs; i++) {
807 switch (pinconf_to_config_param(configs[i])) {
808 case PIN_CONFIG_BIAS_DISABLE:
809 case PIN_CONFIG_BIAS_PULL_UP:
810 case PIN_CONFIG_BIAS_PULL_DOWN:
811 ret = intel_config_set_pull(pctrl, pin, configs[i]);
812 if (ret)
813 return ret;
814 break;
816 case PIN_CONFIG_INPUT_DEBOUNCE:
817 ret = intel_config_set_debounce(pctrl, pin,
818 pinconf_to_config_argument(configs[i]));
819 if (ret)
820 return ret;
821 break;
823 default:
824 return -ENOTSUPP;
828 return 0;
831 static const struct pinconf_ops intel_pinconf_ops = {
832 .is_generic = true,
833 .pin_config_get = intel_config_get,
834 .pin_config_set = intel_config_set,
837 static const struct pinctrl_desc intel_pinctrl_desc = {
838 .pctlops = &intel_pinctrl_ops,
839 .pmxops = &intel_pinmux_ops,
840 .confops = &intel_pinconf_ops,
841 .owner = THIS_MODULE,
845 * intel_gpio_to_pin() - Translate from GPIO offset to pin number
846 * @pctrl: Pinctrl structure
847 * @offset: GPIO offset from gpiolib
848 * @community: Community is filled here if not %NULL
849 * @padgrp: Pad group is filled here if not %NULL
851 * When coming through gpiolib irqchip, the GPIO offset is not
852 * automatically translated to pinctrl pin number. This function can be
853 * used to find out the corresponding pinctrl pin.
855 static int intel_gpio_to_pin(struct intel_pinctrl *pctrl, unsigned int offset,
856 const struct intel_community **community,
857 const struct intel_padgroup **padgrp)
859 int i;
861 for (i = 0; i < pctrl->ncommunities; i++) {
862 const struct intel_community *comm = &pctrl->communities[i];
863 int j;
865 for (j = 0; j < comm->ngpps; j++) {
866 const struct intel_padgroup *pgrp = &comm->gpps[j];
868 if (pgrp->gpio_base == INTEL_GPIO_BASE_NOMAP)
869 continue;
871 if (offset >= pgrp->gpio_base &&
872 offset < pgrp->gpio_base + pgrp->size) {
873 int pin;
875 pin = pgrp->base + offset - pgrp->gpio_base;
876 if (community)
877 *community = comm;
878 if (padgrp)
879 *padgrp = pgrp;
881 return pin;
886 return -EINVAL;
890 * intel_pin_to_gpio() - Translate from pin number to GPIO offset
891 * @pctrl: Pinctrl structure
892 * @pin: pin number
894 * Translate the pin number of pinctrl to GPIO offset
896 static __maybe_unused int intel_pin_to_gpio(struct intel_pinctrl *pctrl, int pin)
898 const struct intel_community *community;
899 const struct intel_padgroup *padgrp;
901 community = intel_get_community(pctrl, pin);
902 if (!community)
903 return -EINVAL;
905 padgrp = intel_community_get_padgroup(community, pin);
906 if (!padgrp)
907 return -EINVAL;
909 return pin - padgrp->base + padgrp->gpio_base;
912 static int intel_gpio_get(struct gpio_chip *chip, unsigned int offset)
914 struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
915 void __iomem *reg;
916 u32 padcfg0;
917 int pin;
919 pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL);
920 if (pin < 0)
921 return -EINVAL;
923 reg = intel_get_padcfg(pctrl, pin, PADCFG0);
924 if (!reg)
925 return -EINVAL;
927 padcfg0 = readl(reg);
928 if (!(padcfg0 & PADCFG0_GPIOTXDIS))
929 return !!(padcfg0 & PADCFG0_GPIOTXSTATE);
931 return !!(padcfg0 & PADCFG0_GPIORXSTATE);
934 static void intel_gpio_set(struct gpio_chip *chip, unsigned int offset,
935 int value)
937 struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
938 unsigned long flags;
939 void __iomem *reg;
940 u32 padcfg0;
941 int pin;
943 pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL);
944 if (pin < 0)
945 return;
947 reg = intel_get_padcfg(pctrl, pin, PADCFG0);
948 if (!reg)
949 return;
951 raw_spin_lock_irqsave(&pctrl->lock, flags);
952 padcfg0 = readl(reg);
953 if (value)
954 padcfg0 |= PADCFG0_GPIOTXSTATE;
955 else
956 padcfg0 &= ~PADCFG0_GPIOTXSTATE;
957 writel(padcfg0, reg);
958 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
961 static int intel_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
963 struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
964 unsigned long flags;
965 void __iomem *reg;
966 u32 padcfg0;
967 int pin;
969 pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL);
970 if (pin < 0)
971 return -EINVAL;
973 reg = intel_get_padcfg(pctrl, pin, PADCFG0);
974 if (!reg)
975 return -EINVAL;
977 raw_spin_lock_irqsave(&pctrl->lock, flags);
978 padcfg0 = readl(reg);
979 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
980 if (padcfg0 & PADCFG0_PMODE_MASK)
981 return -EINVAL;
983 if (padcfg0 & PADCFG0_GPIOTXDIS)
984 return GPIO_LINE_DIRECTION_IN;
986 return GPIO_LINE_DIRECTION_OUT;
989 static int intel_gpio_direction_input(struct gpio_chip *chip, unsigned int offset)
991 return pinctrl_gpio_direction_input(chip->base + offset);
994 static int intel_gpio_direction_output(struct gpio_chip *chip, unsigned int offset,
995 int value)
997 intel_gpio_set(chip, offset, value);
998 return pinctrl_gpio_direction_output(chip->base + offset);
1001 static const struct gpio_chip intel_gpio_chip = {
1002 .owner = THIS_MODULE,
1003 .request = gpiochip_generic_request,
1004 .free = gpiochip_generic_free,
1005 .get_direction = intel_gpio_get_direction,
1006 .direction_input = intel_gpio_direction_input,
1007 .direction_output = intel_gpio_direction_output,
1008 .get = intel_gpio_get,
1009 .set = intel_gpio_set,
1010 .set_config = gpiochip_generic_config,
1013 static void intel_gpio_irq_ack(struct irq_data *d)
1015 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1016 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
1017 const struct intel_community *community;
1018 const struct intel_padgroup *padgrp;
1019 int pin;
1021 pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), &community, &padgrp);
1022 if (pin >= 0) {
1023 unsigned int gpp, gpp_offset, is_offset;
1025 gpp = padgrp->reg_num;
1026 gpp_offset = padgroup_offset(padgrp, pin);
1027 is_offset = community->is_offset + gpp * 4;
1029 raw_spin_lock(&pctrl->lock);
1030 writel(BIT(gpp_offset), community->regs + is_offset);
1031 raw_spin_unlock(&pctrl->lock);
1035 static void intel_gpio_irq_mask_unmask(struct irq_data *d, bool mask)
1037 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1038 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
1039 const struct intel_community *community;
1040 const struct intel_padgroup *padgrp;
1041 int pin;
1043 pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), &community, &padgrp);
1044 if (pin >= 0) {
1045 unsigned int gpp, gpp_offset;
1046 unsigned long flags;
1047 void __iomem *reg, *is;
1048 u32 value;
1050 gpp = padgrp->reg_num;
1051 gpp_offset = padgroup_offset(padgrp, pin);
1053 reg = community->regs + community->ie_offset + gpp * 4;
1054 is = community->regs + community->is_offset + gpp * 4;
1056 raw_spin_lock_irqsave(&pctrl->lock, flags);
1058 /* Clear interrupt status first to avoid unexpected interrupt */
1059 writel(BIT(gpp_offset), is);
1061 value = readl(reg);
1062 if (mask)
1063 value &= ~BIT(gpp_offset);
1064 else
1065 value |= BIT(gpp_offset);
1066 writel(value, reg);
1067 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
1071 static void intel_gpio_irq_mask(struct irq_data *d)
1073 intel_gpio_irq_mask_unmask(d, true);
1076 static void intel_gpio_irq_unmask(struct irq_data *d)
1078 intel_gpio_irq_mask_unmask(d, false);
1081 static int intel_gpio_irq_type(struct irq_data *d, unsigned int type)
1083 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1084 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
1085 unsigned int pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), NULL, NULL);
1086 unsigned long flags;
1087 void __iomem *reg;
1088 u32 value;
1090 reg = intel_get_padcfg(pctrl, pin, PADCFG0);
1091 if (!reg)
1092 return -EINVAL;
1095 * If the pin is in ACPI mode it is still usable as a GPIO but it
1096 * cannot be used as IRQ because GPI_IS status bit will not be
1097 * updated by the host controller hardware.
1099 if (intel_pad_acpi_mode(pctrl, pin)) {
1100 dev_warn(pctrl->dev, "pin %u cannot be used as IRQ\n", pin);
1101 return -EPERM;
1104 raw_spin_lock_irqsave(&pctrl->lock, flags);
1106 intel_gpio_set_gpio_mode(reg);
1108 /* Disable TX buffer and enable RX (this will be input) */
1109 __intel_gpio_set_direction(reg, true);
1111 value = readl(reg);
1113 value &= ~(PADCFG0_RXEVCFG_MASK | PADCFG0_RXINV);
1115 if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
1116 value |= PADCFG0_RXEVCFG_EDGE_BOTH << PADCFG0_RXEVCFG_SHIFT;
1117 } else if (type & IRQ_TYPE_EDGE_FALLING) {
1118 value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT;
1119 value |= PADCFG0_RXINV;
1120 } else if (type & IRQ_TYPE_EDGE_RISING) {
1121 value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT;
1122 } else if (type & IRQ_TYPE_LEVEL_MASK) {
1123 if (type & IRQ_TYPE_LEVEL_LOW)
1124 value |= PADCFG0_RXINV;
1125 } else {
1126 value |= PADCFG0_RXEVCFG_DISABLED << PADCFG0_RXEVCFG_SHIFT;
1129 writel(value, reg);
1131 if (type & IRQ_TYPE_EDGE_BOTH)
1132 irq_set_handler_locked(d, handle_edge_irq);
1133 else if (type & IRQ_TYPE_LEVEL_MASK)
1134 irq_set_handler_locked(d, handle_level_irq);
1136 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
1138 return 0;
1141 static int intel_gpio_irq_wake(struct irq_data *d, unsigned int on)
1143 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1144 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
1145 unsigned int pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), NULL, NULL);
1147 if (on)
1148 enable_irq_wake(pctrl->irq);
1149 else
1150 disable_irq_wake(pctrl->irq);
1152 dev_dbg(pctrl->dev, "%sable wake for pin %u\n", on ? "en" : "dis", pin);
1153 return 0;
1156 static int intel_gpio_community_irq_handler(struct intel_pinctrl *pctrl,
1157 const struct intel_community *community)
1159 struct gpio_chip *gc = &pctrl->chip;
1160 unsigned int gpp;
1161 int ret = 0;
1163 for (gpp = 0; gpp < community->ngpps; gpp++) {
1164 const struct intel_padgroup *padgrp = &community->gpps[gpp];
1165 unsigned long pending, enabled, gpp_offset;
1166 unsigned long flags;
1168 raw_spin_lock_irqsave(&pctrl->lock, flags);
1170 pending = readl(community->regs + community->is_offset +
1171 padgrp->reg_num * 4);
1172 enabled = readl(community->regs + community->ie_offset +
1173 padgrp->reg_num * 4);
1175 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
1177 /* Only interrupts that are enabled */
1178 pending &= enabled;
1180 for_each_set_bit(gpp_offset, &pending, padgrp->size) {
1181 unsigned int irq;
1183 irq = irq_find_mapping(gc->irq.domain,
1184 padgrp->gpio_base + gpp_offset);
1185 generic_handle_irq(irq);
1188 ret += pending ? 1 : 0;
1191 return ret;
1194 static irqreturn_t intel_gpio_irq(int irq, void *data)
1196 const struct intel_community *community;
1197 struct intel_pinctrl *pctrl = data;
1198 unsigned int i;
1199 int ret = 0;
1201 /* Need to check all communities for pending interrupts */
1202 for (i = 0; i < pctrl->ncommunities; i++) {
1203 community = &pctrl->communities[i];
1204 ret += intel_gpio_community_irq_handler(pctrl, community);
1207 return IRQ_RETVAL(ret);
1210 static int intel_gpio_add_community_ranges(struct intel_pinctrl *pctrl,
1211 const struct intel_community *community)
1213 int ret = 0, i;
1215 for (i = 0; i < community->ngpps; i++) {
1216 const struct intel_padgroup *gpp = &community->gpps[i];
1218 if (gpp->gpio_base == INTEL_GPIO_BASE_NOMAP)
1219 continue;
1221 ret = gpiochip_add_pin_range(&pctrl->chip, dev_name(pctrl->dev),
1222 gpp->gpio_base, gpp->base,
1223 gpp->size);
1224 if (ret)
1225 return ret;
1228 return ret;
1231 static int intel_gpio_add_pin_ranges(struct gpio_chip *gc)
1233 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
1234 int ret, i;
1236 for (i = 0; i < pctrl->ncommunities; i++) {
1237 struct intel_community *community = &pctrl->communities[i];
1239 ret = intel_gpio_add_community_ranges(pctrl, community);
1240 if (ret) {
1241 dev_err(pctrl->dev, "failed to add GPIO pin range\n");
1242 return ret;
1246 return 0;
1249 static unsigned int intel_gpio_ngpio(const struct intel_pinctrl *pctrl)
1251 const struct intel_community *community;
1252 unsigned int ngpio = 0;
1253 int i, j;
1255 for (i = 0; i < pctrl->ncommunities; i++) {
1256 community = &pctrl->communities[i];
1257 for (j = 0; j < community->ngpps; j++) {
1258 const struct intel_padgroup *gpp = &community->gpps[j];
1260 if (gpp->gpio_base == INTEL_GPIO_BASE_NOMAP)
1261 continue;
1263 if (gpp->gpio_base + gpp->size > ngpio)
1264 ngpio = gpp->gpio_base + gpp->size;
1268 return ngpio;
1271 static int intel_gpio_probe(struct intel_pinctrl *pctrl, int irq)
1273 int ret;
1274 struct gpio_irq_chip *girq;
1276 pctrl->chip = intel_gpio_chip;
1278 /* Setup GPIO chip */
1279 pctrl->chip.ngpio = intel_gpio_ngpio(pctrl);
1280 pctrl->chip.label = dev_name(pctrl->dev);
1281 pctrl->chip.parent = pctrl->dev;
1282 pctrl->chip.base = -1;
1283 pctrl->chip.add_pin_ranges = intel_gpio_add_pin_ranges;
1284 pctrl->irq = irq;
1286 /* Setup IRQ chip */
1287 pctrl->irqchip.name = dev_name(pctrl->dev);
1288 pctrl->irqchip.irq_ack = intel_gpio_irq_ack;
1289 pctrl->irqchip.irq_mask = intel_gpio_irq_mask;
1290 pctrl->irqchip.irq_unmask = intel_gpio_irq_unmask;
1291 pctrl->irqchip.irq_set_type = intel_gpio_irq_type;
1292 pctrl->irqchip.irq_set_wake = intel_gpio_irq_wake;
1293 pctrl->irqchip.flags = IRQCHIP_MASK_ON_SUSPEND;
1296 * On some platforms several GPIO controllers share the same interrupt
1297 * line.
1299 ret = devm_request_irq(pctrl->dev, irq, intel_gpio_irq,
1300 IRQF_SHARED | IRQF_NO_THREAD,
1301 dev_name(pctrl->dev), pctrl);
1302 if (ret) {
1303 dev_err(pctrl->dev, "failed to request interrupt\n");
1304 return ret;
1307 girq = &pctrl->chip.irq;
1308 girq->chip = &pctrl->irqchip;
1309 /* This will let us handle the IRQ in the driver */
1310 girq->parent_handler = NULL;
1311 girq->num_parents = 0;
1312 girq->default_type = IRQ_TYPE_NONE;
1313 girq->handler = handle_bad_irq;
1315 ret = devm_gpiochip_add_data(pctrl->dev, &pctrl->chip, pctrl);
1316 if (ret) {
1317 dev_err(pctrl->dev, "failed to register gpiochip\n");
1318 return ret;
1321 return 0;
1324 static int intel_pinctrl_add_padgroups(struct intel_pinctrl *pctrl,
1325 struct intel_community *community)
1327 struct intel_padgroup *gpps;
1328 unsigned int npins = community->npins;
1329 unsigned int padown_num = 0;
1330 size_t ngpps, i;
1332 if (community->gpps)
1333 ngpps = community->ngpps;
1334 else
1335 ngpps = DIV_ROUND_UP(community->npins, community->gpp_size);
1337 gpps = devm_kcalloc(pctrl->dev, ngpps, sizeof(*gpps), GFP_KERNEL);
1338 if (!gpps)
1339 return -ENOMEM;
1341 for (i = 0; i < ngpps; i++) {
1342 if (community->gpps) {
1343 gpps[i] = community->gpps[i];
1344 } else {
1345 unsigned int gpp_size = community->gpp_size;
1347 gpps[i].reg_num = i;
1348 gpps[i].base = community->pin_base + i * gpp_size;
1349 gpps[i].size = min(gpp_size, npins);
1350 npins -= gpps[i].size;
1353 if (gpps[i].size > 32)
1354 return -EINVAL;
1356 /* Special treatment for GPIO base */
1357 switch (gpps[i].gpio_base) {
1358 case INTEL_GPIO_BASE_MATCH:
1359 gpps[i].gpio_base = gpps[i].base;
1360 break;
1361 case INTEL_GPIO_BASE_ZERO:
1362 gpps[i].gpio_base = 0;
1363 break;
1364 case INTEL_GPIO_BASE_NOMAP:
1365 default:
1366 break;
1369 gpps[i].padown_num = padown_num;
1372 * In older hardware the number of padown registers per
1373 * group is fixed regardless of the group size.
1375 if (community->gpp_num_padown_regs)
1376 padown_num += community->gpp_num_padown_regs;
1377 else
1378 padown_num += DIV_ROUND_UP(gpps[i].size * 4, 32);
1381 community->ngpps = ngpps;
1382 community->gpps = gpps;
1384 return 0;
1387 static int intel_pinctrl_pm_init(struct intel_pinctrl *pctrl)
1389 #ifdef CONFIG_PM_SLEEP
1390 const struct intel_pinctrl_soc_data *soc = pctrl->soc;
1391 struct intel_community_context *communities;
1392 struct intel_pad_context *pads;
1393 int i;
1395 pads = devm_kcalloc(pctrl->dev, soc->npins, sizeof(*pads), GFP_KERNEL);
1396 if (!pads)
1397 return -ENOMEM;
1399 communities = devm_kcalloc(pctrl->dev, pctrl->ncommunities,
1400 sizeof(*communities), GFP_KERNEL);
1401 if (!communities)
1402 return -ENOMEM;
1405 for (i = 0; i < pctrl->ncommunities; i++) {
1406 struct intel_community *community = &pctrl->communities[i];
1407 u32 *intmask, *hostown;
1409 intmask = devm_kcalloc(pctrl->dev, community->ngpps,
1410 sizeof(*intmask), GFP_KERNEL);
1411 if (!intmask)
1412 return -ENOMEM;
1414 communities[i].intmask = intmask;
1416 hostown = devm_kcalloc(pctrl->dev, community->ngpps,
1417 sizeof(*hostown), GFP_KERNEL);
1418 if (!hostown)
1419 return -ENOMEM;
1421 communities[i].hostown = hostown;
1424 pctrl->context.pads = pads;
1425 pctrl->context.communities = communities;
1426 #endif
1428 return 0;
1431 static int intel_pinctrl_probe(struct platform_device *pdev,
1432 const struct intel_pinctrl_soc_data *soc_data)
1434 struct intel_pinctrl *pctrl;
1435 int i, ret, irq;
1437 pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL);
1438 if (!pctrl)
1439 return -ENOMEM;
1441 pctrl->dev = &pdev->dev;
1442 pctrl->soc = soc_data;
1443 raw_spin_lock_init(&pctrl->lock);
1446 * Make a copy of the communities which we can use to hold pointers
1447 * to the registers.
1449 pctrl->ncommunities = pctrl->soc->ncommunities;
1450 pctrl->communities = devm_kcalloc(&pdev->dev, pctrl->ncommunities,
1451 sizeof(*pctrl->communities), GFP_KERNEL);
1452 if (!pctrl->communities)
1453 return -ENOMEM;
1455 for (i = 0; i < pctrl->ncommunities; i++) {
1456 struct intel_community *community = &pctrl->communities[i];
1457 void __iomem *regs;
1458 u32 padbar;
1460 *community = pctrl->soc->communities[i];
1462 regs = devm_platform_ioremap_resource(pdev, community->barno);
1463 if (IS_ERR(regs))
1464 return PTR_ERR(regs);
1467 * Determine community features based on the revision if
1468 * not specified already.
1470 if (!community->features) {
1471 u32 rev;
1473 rev = (readl(regs + REVID) & REVID_MASK) >> REVID_SHIFT;
1474 if (rev >= 0x94) {
1475 community->features |= PINCTRL_FEATURE_DEBOUNCE;
1476 community->features |= PINCTRL_FEATURE_1K_PD;
1480 /* Read offset of the pad configuration registers */
1481 padbar = readl(regs + PADBAR);
1483 community->regs = regs;
1484 community->pad_regs = regs + padbar;
1486 ret = intel_pinctrl_add_padgroups(pctrl, community);
1487 if (ret)
1488 return ret;
1491 irq = platform_get_irq(pdev, 0);
1492 if (irq < 0)
1493 return irq;
1495 ret = intel_pinctrl_pm_init(pctrl);
1496 if (ret)
1497 return ret;
1499 pctrl->pctldesc = intel_pinctrl_desc;
1500 pctrl->pctldesc.name = dev_name(&pdev->dev);
1501 pctrl->pctldesc.pins = pctrl->soc->pins;
1502 pctrl->pctldesc.npins = pctrl->soc->npins;
1504 pctrl->pctldev = devm_pinctrl_register(&pdev->dev, &pctrl->pctldesc,
1505 pctrl);
1506 if (IS_ERR(pctrl->pctldev)) {
1507 dev_err(&pdev->dev, "failed to register pinctrl driver\n");
1508 return PTR_ERR(pctrl->pctldev);
1511 ret = intel_gpio_probe(pctrl, irq);
1512 if (ret)
1513 return ret;
1515 platform_set_drvdata(pdev, pctrl);
1517 return 0;
1520 int intel_pinctrl_probe_by_hid(struct platform_device *pdev)
1522 const struct intel_pinctrl_soc_data *data;
1524 data = device_get_match_data(&pdev->dev);
1525 if (!data)
1526 return -ENODATA;
1528 return intel_pinctrl_probe(pdev, data);
1530 EXPORT_SYMBOL_GPL(intel_pinctrl_probe_by_hid);
1532 int intel_pinctrl_probe_by_uid(struct platform_device *pdev)
1534 const struct intel_pinctrl_soc_data *data;
1536 data = intel_pinctrl_get_soc_data(pdev);
1537 if (IS_ERR(data))
1538 return PTR_ERR(data);
1540 return intel_pinctrl_probe(pdev, data);
1542 EXPORT_SYMBOL_GPL(intel_pinctrl_probe_by_uid);
1544 const struct intel_pinctrl_soc_data *intel_pinctrl_get_soc_data(struct platform_device *pdev)
1546 const struct intel_pinctrl_soc_data *data = NULL;
1547 const struct intel_pinctrl_soc_data **table;
1548 struct acpi_device *adev;
1549 unsigned int i;
1551 adev = ACPI_COMPANION(&pdev->dev);
1552 if (adev) {
1553 const void *match = device_get_match_data(&pdev->dev);
1555 table = (const struct intel_pinctrl_soc_data **)match;
1556 for (i = 0; table[i]; i++) {
1557 if (!strcmp(adev->pnp.unique_id, table[i]->uid)) {
1558 data = table[i];
1559 break;
1562 } else {
1563 const struct platform_device_id *id;
1565 id = platform_get_device_id(pdev);
1566 if (!id)
1567 return ERR_PTR(-ENODEV);
1569 table = (const struct intel_pinctrl_soc_data **)id->driver_data;
1570 data = table[pdev->id];
1573 return data ?: ERR_PTR(-ENODATA);
1575 EXPORT_SYMBOL_GPL(intel_pinctrl_get_soc_data);
1577 #ifdef CONFIG_PM_SLEEP
1578 static bool intel_pinctrl_should_save(struct intel_pinctrl *pctrl, unsigned int pin)
1580 const struct pin_desc *pd = pin_desc_get(pctrl->pctldev, pin);
1582 if (!pd || !intel_pad_usable(pctrl, pin))
1583 return false;
1586 * Only restore the pin if it is actually in use by the kernel (or
1587 * by userspace). It is possible that some pins are used by the
1588 * BIOS during resume and those are not always locked down so leave
1589 * them alone.
1591 if (pd->mux_owner || pd->gpio_owner ||
1592 gpiochip_line_is_irq(&pctrl->chip, intel_pin_to_gpio(pctrl, pin)))
1593 return true;
1595 return false;
1598 int intel_pinctrl_suspend_noirq(struct device *dev)
1600 struct intel_pinctrl *pctrl = dev_get_drvdata(dev);
1601 struct intel_community_context *communities;
1602 struct intel_pad_context *pads;
1603 int i;
1605 pads = pctrl->context.pads;
1606 for (i = 0; i < pctrl->soc->npins; i++) {
1607 const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
1608 void __iomem *padcfg;
1609 u32 val;
1611 if (!intel_pinctrl_should_save(pctrl, desc->number))
1612 continue;
1614 val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG0));
1615 pads[i].padcfg0 = val & ~PADCFG0_GPIORXSTATE;
1616 val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG1));
1617 pads[i].padcfg1 = val;
1619 padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG2);
1620 if (padcfg)
1621 pads[i].padcfg2 = readl(padcfg);
1624 communities = pctrl->context.communities;
1625 for (i = 0; i < pctrl->ncommunities; i++) {
1626 struct intel_community *community = &pctrl->communities[i];
1627 void __iomem *base;
1628 unsigned int gpp;
1630 base = community->regs + community->ie_offset;
1631 for (gpp = 0; gpp < community->ngpps; gpp++)
1632 communities[i].intmask[gpp] = readl(base + gpp * 4);
1634 base = community->regs + community->hostown_offset;
1635 for (gpp = 0; gpp < community->ngpps; gpp++)
1636 communities[i].hostown[gpp] = readl(base + gpp * 4);
1639 return 0;
1641 EXPORT_SYMBOL_GPL(intel_pinctrl_suspend_noirq);
1643 static void intel_gpio_irq_init(struct intel_pinctrl *pctrl)
1645 size_t i;
1647 for (i = 0; i < pctrl->ncommunities; i++) {
1648 const struct intel_community *community;
1649 void __iomem *base;
1650 unsigned int gpp;
1652 community = &pctrl->communities[i];
1653 base = community->regs;
1655 for (gpp = 0; gpp < community->ngpps; gpp++) {
1656 /* Mask and clear all interrupts */
1657 writel(0, base + community->ie_offset + gpp * 4);
1658 writel(0xffff, base + community->is_offset + gpp * 4);
1663 static bool intel_gpio_update_reg(void __iomem *reg, u32 mask, u32 value)
1665 u32 curr, updated;
1667 curr = readl(reg);
1669 updated = (curr & ~mask) | (value & mask);
1670 if (curr == updated)
1671 return false;
1673 writel(updated, reg);
1674 return true;
1677 static void intel_restore_hostown(struct intel_pinctrl *pctrl, unsigned int c,
1678 void __iomem *base, unsigned int gpp, u32 saved)
1680 const struct intel_community *community = &pctrl->communities[c];
1681 const struct intel_padgroup *padgrp = &community->gpps[gpp];
1682 struct device *dev = pctrl->dev;
1683 const char *dummy;
1684 u32 requested = 0;
1685 unsigned int i;
1687 if (padgrp->gpio_base == INTEL_GPIO_BASE_NOMAP)
1688 return;
1690 for_each_requested_gpio_in_range(&pctrl->chip, i, padgrp->gpio_base, padgrp->size, dummy)
1691 requested |= BIT(i);
1693 if (!intel_gpio_update_reg(base + gpp * 4, requested, saved))
1694 return;
1696 dev_dbg(dev, "restored hostown %u/%u %#08x\n", c, gpp, readl(base + gpp * 4));
1699 static void intel_restore_intmask(struct intel_pinctrl *pctrl, unsigned int c,
1700 void __iomem *base, unsigned int gpp, u32 saved)
1702 struct device *dev = pctrl->dev;
1704 if (!intel_gpio_update_reg(base + gpp * 4, ~0U, saved))
1705 return;
1707 dev_dbg(dev, "restored mask %u/%u %#08x\n", c, gpp, readl(base + gpp * 4));
1710 static void intel_restore_padcfg(struct intel_pinctrl *pctrl, unsigned int pin,
1711 unsigned int reg, u32 saved)
1713 u32 mask = (reg == PADCFG0) ? PADCFG0_GPIORXSTATE : 0;
1714 unsigned int n = reg / sizeof(u32);
1715 struct device *dev = pctrl->dev;
1716 void __iomem *padcfg;
1718 padcfg = intel_get_padcfg(pctrl, pin, reg);
1719 if (!padcfg)
1720 return;
1722 if (!intel_gpio_update_reg(padcfg, ~mask, saved))
1723 return;
1725 dev_dbg(dev, "restored pin %u padcfg%u %#08x\n", pin, n, readl(padcfg));
1728 int intel_pinctrl_resume_noirq(struct device *dev)
1730 struct intel_pinctrl *pctrl = dev_get_drvdata(dev);
1731 const struct intel_community_context *communities;
1732 const struct intel_pad_context *pads;
1733 int i;
1735 /* Mask all interrupts */
1736 intel_gpio_irq_init(pctrl);
1738 pads = pctrl->context.pads;
1739 for (i = 0; i < pctrl->soc->npins; i++) {
1740 const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
1742 if (!intel_pinctrl_should_save(pctrl, desc->number))
1743 continue;
1745 intel_restore_padcfg(pctrl, desc->number, PADCFG0, pads[i].padcfg0);
1746 intel_restore_padcfg(pctrl, desc->number, PADCFG1, pads[i].padcfg1);
1747 intel_restore_padcfg(pctrl, desc->number, PADCFG2, pads[i].padcfg2);
1750 communities = pctrl->context.communities;
1751 for (i = 0; i < pctrl->ncommunities; i++) {
1752 struct intel_community *community = &pctrl->communities[i];
1753 void __iomem *base;
1754 unsigned int gpp;
1756 base = community->regs + community->ie_offset;
1757 for (gpp = 0; gpp < community->ngpps; gpp++)
1758 intel_restore_intmask(pctrl, i, base, gpp, communities[i].intmask[gpp]);
1760 base = community->regs + community->hostown_offset;
1761 for (gpp = 0; gpp < community->ngpps; gpp++)
1762 intel_restore_hostown(pctrl, i, base, gpp, communities[i].hostown[gpp]);
1765 return 0;
1767 EXPORT_SYMBOL_GPL(intel_pinctrl_resume_noirq);
1768 #endif
1770 MODULE_AUTHOR("Mathias Nyman <mathias.nyman@linux.intel.com>");
1771 MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
1772 MODULE_DESCRIPTION("Intel pinctrl/GPIO core driver");
1773 MODULE_LICENSE("GPL v2");