1 // SPDX-License-Identifier: GPL-2.0
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>
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>
25 #include "pinctrl-intel.h"
27 /* Offset from regs */
29 #define REVID_SHIFT 16
30 #define REVID_MASK GENMASK(31, 16)
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 */
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)
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 4
66 #define PADCFG1_TERM_2K 3
67 #define PADCFG1_TERM_5K 2
68 #define PADCFG1_TERM_1K 1
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
{
83 struct intel_community_context
{
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
,
94 struct intel_community
*community
;
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
)
104 dev_warn(pctrl
->dev
, "failed to find community for pin %u\n", pin
);
108 static const struct intel_padgroup
*
109 intel_community_get_padgroup(const struct intel_community
*community
,
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
)
124 static void __iomem
*intel_get_padcfg(struct intel_pinctrl
*pctrl
,
125 unsigned int pin
, unsigned int reg
)
127 const struct intel_community
*community
;
131 community
= intel_get_community(pctrl
, pin
);
135 padno
= pin_to_padno(community
, pin
);
136 nregs
= (community
->features
& PINCTRL_FEATURE_DEBOUNCE
) ? 4 : 2;
138 if (reg
>= nregs
* 4)
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
);
154 if (!community
->padown_offset
)
157 padgrp
= intel_community_get_padgroup(community
, pin
);
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
);
179 if (!community
->hostown_offset
)
182 padgrp
= intel_community_get_padgroup(community
, pin
);
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.
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
;
218 int ret
= PAD_UNLOCKED
;
220 community
= intel_get_community(pctrl
, pin
);
222 return PAD_LOCKED_FULL
;
223 if (!community
->padcfglock_offset
)
226 padgrp
= intel_community_get_padgroup(community
, pin
);
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
))
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
;
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
,
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
;
285 static void intel_pin_dbg_show(struct pinctrl_dev
*pctldev
, struct seq_file
*s
,
288 struct intel_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
289 void __iomem
*padcfg
;
290 u32 cfg0
, cfg1
, mode
;
294 if (!intel_pad_owned_by_host(pctrl
, pin
)) {
295 seq_puts(s
, "not available");
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 ");
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
);
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
) {
321 seq_puts(s
, "LOCKED");
322 if ((locked
& PAD_LOCKED_FULL
) == PAD_LOCKED_TX
)
324 else if ((locked
& PAD_LOCKED_FULL
) == PAD_LOCKED_FULL
)
325 seq_puts(s
, " full");
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
;
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
];
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
);
391 /* Now enable the mux setting for each pin in the group */
392 for (i
= 0; i
< grp
->npins
; i
++) {
393 void __iomem
*padcfg0
;
396 padcfg0
= intel_get_padcfg(pctrl
, grp
->pins
[i
], PADCFG0
);
397 value
= readl(padcfg0
);
399 value
&= ~PADCFG0_PMODE_MASK
;
402 value
|= grp
->modes
[i
] << PADCFG0_PMODE_SHIFT
;
404 value
|= grp
->mode
<< PADCFG0_PMODE_SHIFT
;
406 writel(value
, padcfg0
);
409 raw_spin_unlock_irqrestore(&pctrl
->lock
, flags
);
414 static void __intel_gpio_set_direction(void __iomem
*padcfg0
, bool input
)
418 value
= readl(padcfg0
);
420 value
&= ~PADCFG0_GPIORXDIS
;
421 value
|= PADCFG0_GPIOTXDIS
;
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
)
438 /* Put the pad into GPIO mode */
439 value
= readl(padcfg0
) & ~PADCFG0_PMODE_MASK
;
440 /* Disable SCI/SMI/NMI generation */
441 value
&= ~(PADCFG0_GPIROUTIOXAPIC
| PADCFG0_GPIROUTSCI
);
442 value
&= ~(PADCFG0_GPIROUTSMI
| PADCFG0_GPIROUTNMI
);
443 writel(value
, padcfg0
);
446 static int intel_gpio_request_enable(struct pinctrl_dev
*pctldev
,
447 struct pinctrl_gpio_range
*range
,
450 struct intel_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
451 void __iomem
*padcfg0
;
454 raw_spin_lock_irqsave(&pctrl
->lock
, flags
);
456 if (!intel_pad_owned_by_host(pctrl
, pin
)) {
457 raw_spin_unlock_irqrestore(&pctrl
->lock
, flags
);
461 if (!intel_pad_is_unlocked(pctrl
, pin
)) {
462 raw_spin_unlock_irqrestore(&pctrl
->lock
, flags
);
466 padcfg0
= intel_get_padcfg(pctrl
, pin
, PADCFG0
);
469 * If pin is already configured in GPIO mode, we assume that
470 * firmware provides correct settings. In such case we avoid
471 * potential glitches on the pin. Otherwise, for the pin in
472 * alternative mode, consumer has to supply respective flags.
474 if (intel_gpio_get_gpio_mode(padcfg0
) == PADCFG0_PMODE_GPIO
) {
475 raw_spin_unlock_irqrestore(&pctrl
->lock
, flags
);
479 intel_gpio_set_gpio_mode(padcfg0
);
481 /* Disable TX buffer and enable RX (this will be input) */
482 __intel_gpio_set_direction(padcfg0
, true);
484 raw_spin_unlock_irqrestore(&pctrl
->lock
, flags
);
489 static int intel_gpio_set_direction(struct pinctrl_dev
*pctldev
,
490 struct pinctrl_gpio_range
*range
,
491 unsigned int pin
, bool input
)
493 struct intel_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
494 void __iomem
*padcfg0
;
497 raw_spin_lock_irqsave(&pctrl
->lock
, flags
);
499 padcfg0
= intel_get_padcfg(pctrl
, pin
, PADCFG0
);
500 __intel_gpio_set_direction(padcfg0
, input
);
502 raw_spin_unlock_irqrestore(&pctrl
->lock
, flags
);
507 static const struct pinmux_ops intel_pinmux_ops
= {
508 .get_functions_count
= intel_get_functions_count
,
509 .get_function_name
= intel_get_function_name
,
510 .get_function_groups
= intel_get_function_groups
,
511 .set_mux
= intel_pinmux_set_mux
,
512 .gpio_request_enable
= intel_gpio_request_enable
,
513 .gpio_set_direction
= intel_gpio_set_direction
,
516 static int intel_config_get(struct pinctrl_dev
*pctldev
, unsigned int pin
,
517 unsigned long *config
)
519 struct intel_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
520 enum pin_config_param param
= pinconf_to_config_param(*config
);
521 const struct intel_community
*community
;
525 if (!intel_pad_owned_by_host(pctrl
, pin
))
528 community
= intel_get_community(pctrl
, pin
);
529 value
= readl(intel_get_padcfg(pctrl
, pin
, PADCFG1
));
530 term
= (value
& PADCFG1_TERM_MASK
) >> PADCFG1_TERM_SHIFT
;
533 case PIN_CONFIG_BIAS_DISABLE
:
538 case PIN_CONFIG_BIAS_PULL_UP
:
539 if (!term
|| !(value
& PADCFG1_TERM_UP
))
543 case PADCFG1_TERM_1K
:
546 case PADCFG1_TERM_2K
:
549 case PADCFG1_TERM_5K
:
552 case PADCFG1_TERM_20K
:
559 case PIN_CONFIG_BIAS_PULL_DOWN
:
560 if (!term
|| value
& PADCFG1_TERM_UP
)
564 case PADCFG1_TERM_1K
:
565 if (!(community
->features
& PINCTRL_FEATURE_1K_PD
))
569 case PADCFG1_TERM_5K
:
572 case PADCFG1_TERM_20K
:
579 case PIN_CONFIG_INPUT_DEBOUNCE
: {
580 void __iomem
*padcfg2
;
583 padcfg2
= intel_get_padcfg(pctrl
, pin
, PADCFG2
);
588 if (!(v
& PADCFG2_DEBEN
))
591 v
= (v
& PADCFG2_DEBOUNCE_MASK
) >> PADCFG2_DEBOUNCE_SHIFT
;
592 arg
= BIT(v
) * DEBOUNCE_PERIOD_NSEC
/ NSEC_PER_USEC
;
601 *config
= pinconf_to_config_packed(param
, arg
);
605 static int intel_config_set_pull(struct intel_pinctrl
*pctrl
, unsigned int pin
,
606 unsigned long config
)
608 unsigned int param
= pinconf_to_config_param(config
);
609 unsigned int arg
= pinconf_to_config_argument(config
);
610 const struct intel_community
*community
;
611 void __iomem
*padcfg1
;
616 raw_spin_lock_irqsave(&pctrl
->lock
, flags
);
618 community
= intel_get_community(pctrl
, pin
);
619 padcfg1
= intel_get_padcfg(pctrl
, pin
, PADCFG1
);
620 value
= readl(padcfg1
);
623 case PIN_CONFIG_BIAS_DISABLE
:
624 value
&= ~(PADCFG1_TERM_MASK
| PADCFG1_TERM_UP
);
627 case PIN_CONFIG_BIAS_PULL_UP
:
628 value
&= ~PADCFG1_TERM_MASK
;
630 value
|= PADCFG1_TERM_UP
;
634 value
|= PADCFG1_TERM_20K
<< PADCFG1_TERM_SHIFT
;
637 value
|= PADCFG1_TERM_5K
<< PADCFG1_TERM_SHIFT
;
640 value
|= PADCFG1_TERM_2K
<< PADCFG1_TERM_SHIFT
;
643 value
|= PADCFG1_TERM_1K
<< PADCFG1_TERM_SHIFT
;
651 case PIN_CONFIG_BIAS_PULL_DOWN
:
652 value
&= ~(PADCFG1_TERM_UP
| PADCFG1_TERM_MASK
);
656 value
|= PADCFG1_TERM_20K
<< PADCFG1_TERM_SHIFT
;
659 value
|= PADCFG1_TERM_5K
<< PADCFG1_TERM_SHIFT
;
662 if (!(community
->features
& PINCTRL_FEATURE_1K_PD
)) {
666 value
|= PADCFG1_TERM_1K
<< PADCFG1_TERM_SHIFT
;
676 writel(value
, padcfg1
);
678 raw_spin_unlock_irqrestore(&pctrl
->lock
, flags
);
683 static int intel_config_set_debounce(struct intel_pinctrl
*pctrl
,
684 unsigned int pin
, unsigned int debounce
)
686 void __iomem
*padcfg0
, *padcfg2
;
691 padcfg2
= intel_get_padcfg(pctrl
, pin
, PADCFG2
);
695 padcfg0
= intel_get_padcfg(pctrl
, pin
, PADCFG0
);
697 raw_spin_lock_irqsave(&pctrl
->lock
, flags
);
699 value0
= readl(padcfg0
);
700 value2
= readl(padcfg2
);
702 /* Disable glitch filter and debouncer */
703 value0
&= ~PADCFG0_PREGFRXSEL
;
704 value2
&= ~(PADCFG2_DEBEN
| PADCFG2_DEBOUNCE_MASK
);
709 v
= order_base_2(debounce
* NSEC_PER_USEC
/ DEBOUNCE_PERIOD_NSEC
);
710 if (v
< 3 || v
> 15) {
714 /* Enable glitch filter and debouncer */
715 value0
|= PADCFG0_PREGFRXSEL
;
716 value2
|= v
<< PADCFG2_DEBOUNCE_SHIFT
;
717 value2
|= PADCFG2_DEBEN
;
721 writel(value0
, padcfg0
);
722 writel(value2
, padcfg2
);
725 raw_spin_unlock_irqrestore(&pctrl
->lock
, flags
);
730 static int intel_config_set(struct pinctrl_dev
*pctldev
, unsigned int pin
,
731 unsigned long *configs
, unsigned int nconfigs
)
733 struct intel_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
736 if (!intel_pad_usable(pctrl
, pin
))
739 for (i
= 0; i
< nconfigs
; i
++) {
740 switch (pinconf_to_config_param(configs
[i
])) {
741 case PIN_CONFIG_BIAS_DISABLE
:
742 case PIN_CONFIG_BIAS_PULL_UP
:
743 case PIN_CONFIG_BIAS_PULL_DOWN
:
744 ret
= intel_config_set_pull(pctrl
, pin
, configs
[i
]);
749 case PIN_CONFIG_INPUT_DEBOUNCE
:
750 ret
= intel_config_set_debounce(pctrl
, pin
,
751 pinconf_to_config_argument(configs
[i
]));
764 static const struct pinconf_ops intel_pinconf_ops
= {
766 .pin_config_get
= intel_config_get
,
767 .pin_config_set
= intel_config_set
,
770 static const struct pinctrl_desc intel_pinctrl_desc
= {
771 .pctlops
= &intel_pinctrl_ops
,
772 .pmxops
= &intel_pinmux_ops
,
773 .confops
= &intel_pinconf_ops
,
774 .owner
= THIS_MODULE
,
778 * intel_gpio_to_pin() - Translate from GPIO offset to pin number
779 * @pctrl: Pinctrl structure
780 * @offset: GPIO offset from gpiolib
781 * @community: Community is filled here if not %NULL
782 * @padgrp: Pad group is filled here if not %NULL
784 * When coming through gpiolib irqchip, the GPIO offset is not
785 * automatically translated to pinctrl pin number. This function can be
786 * used to find out the corresponding pinctrl pin.
788 static int intel_gpio_to_pin(struct intel_pinctrl
*pctrl
, unsigned int offset
,
789 const struct intel_community
**community
,
790 const struct intel_padgroup
**padgrp
)
794 for (i
= 0; i
< pctrl
->ncommunities
; i
++) {
795 const struct intel_community
*comm
= &pctrl
->communities
[i
];
798 for (j
= 0; j
< comm
->ngpps
; j
++) {
799 const struct intel_padgroup
*pgrp
= &comm
->gpps
[j
];
801 if (pgrp
->gpio_base
< 0)
804 if (offset
>= pgrp
->gpio_base
&&
805 offset
< pgrp
->gpio_base
+ pgrp
->size
) {
808 pin
= pgrp
->base
+ offset
- pgrp
->gpio_base
;
823 * intel_pin_to_gpio() - Translate from pin number to GPIO offset
824 * @pctrl: Pinctrl structure
827 * Translate the pin number of pinctrl to GPIO offset
829 static __maybe_unused
int intel_pin_to_gpio(struct intel_pinctrl
*pctrl
, int pin
)
831 const struct intel_community
*community
;
832 const struct intel_padgroup
*padgrp
;
834 community
= intel_get_community(pctrl
, pin
);
838 padgrp
= intel_community_get_padgroup(community
, pin
);
842 return pin
- padgrp
->base
+ padgrp
->gpio_base
;
845 static int intel_gpio_get(struct gpio_chip
*chip
, unsigned int offset
)
847 struct intel_pinctrl
*pctrl
= gpiochip_get_data(chip
);
852 pin
= intel_gpio_to_pin(pctrl
, offset
, NULL
, NULL
);
856 reg
= intel_get_padcfg(pctrl
, pin
, PADCFG0
);
860 padcfg0
= readl(reg
);
861 if (!(padcfg0
& PADCFG0_GPIOTXDIS
))
862 return !!(padcfg0
& PADCFG0_GPIOTXSTATE
);
864 return !!(padcfg0
& PADCFG0_GPIORXSTATE
);
867 static void intel_gpio_set(struct gpio_chip
*chip
, unsigned int offset
,
870 struct intel_pinctrl
*pctrl
= gpiochip_get_data(chip
);
876 pin
= intel_gpio_to_pin(pctrl
, offset
, NULL
, NULL
);
880 reg
= intel_get_padcfg(pctrl
, pin
, PADCFG0
);
884 raw_spin_lock_irqsave(&pctrl
->lock
, flags
);
885 padcfg0
= readl(reg
);
887 padcfg0
|= PADCFG0_GPIOTXSTATE
;
889 padcfg0
&= ~PADCFG0_GPIOTXSTATE
;
890 writel(padcfg0
, reg
);
891 raw_spin_unlock_irqrestore(&pctrl
->lock
, flags
);
894 static int intel_gpio_get_direction(struct gpio_chip
*chip
, unsigned int offset
)
896 struct intel_pinctrl
*pctrl
= gpiochip_get_data(chip
);
901 pin
= intel_gpio_to_pin(pctrl
, offset
, NULL
, NULL
);
905 reg
= intel_get_padcfg(pctrl
, pin
, PADCFG0
);
909 padcfg0
= readl(reg
);
911 if (padcfg0
& PADCFG0_PMODE_MASK
)
914 if (padcfg0
& PADCFG0_GPIOTXDIS
)
915 return GPIO_LINE_DIRECTION_IN
;
917 return GPIO_LINE_DIRECTION_OUT
;
920 static int intel_gpio_direction_input(struct gpio_chip
*chip
, unsigned int offset
)
922 return pinctrl_gpio_direction_input(chip
->base
+ offset
);
925 static int intel_gpio_direction_output(struct gpio_chip
*chip
, unsigned int offset
,
928 intel_gpio_set(chip
, offset
, value
);
929 return pinctrl_gpio_direction_output(chip
->base
+ offset
);
932 static const struct gpio_chip intel_gpio_chip
= {
933 .owner
= THIS_MODULE
,
934 .request
= gpiochip_generic_request
,
935 .free
= gpiochip_generic_free
,
936 .get_direction
= intel_gpio_get_direction
,
937 .direction_input
= intel_gpio_direction_input
,
938 .direction_output
= intel_gpio_direction_output
,
939 .get
= intel_gpio_get
,
940 .set
= intel_gpio_set
,
941 .set_config
= gpiochip_generic_config
,
944 static void intel_gpio_irq_ack(struct irq_data
*d
)
946 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
947 struct intel_pinctrl
*pctrl
= gpiochip_get_data(gc
);
948 const struct intel_community
*community
;
949 const struct intel_padgroup
*padgrp
;
952 pin
= intel_gpio_to_pin(pctrl
, irqd_to_hwirq(d
), &community
, &padgrp
);
954 unsigned int gpp
, gpp_offset
, is_offset
;
956 gpp
= padgrp
->reg_num
;
957 gpp_offset
= padgroup_offset(padgrp
, pin
);
958 is_offset
= community
->is_offset
+ gpp
* 4;
960 raw_spin_lock(&pctrl
->lock
);
961 writel(BIT(gpp_offset
), community
->regs
+ is_offset
);
962 raw_spin_unlock(&pctrl
->lock
);
966 static void intel_gpio_irq_mask_unmask(struct irq_data
*d
, bool mask
)
968 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
969 struct intel_pinctrl
*pctrl
= gpiochip_get_data(gc
);
970 const struct intel_community
*community
;
971 const struct intel_padgroup
*padgrp
;
974 pin
= intel_gpio_to_pin(pctrl
, irqd_to_hwirq(d
), &community
, &padgrp
);
976 unsigned int gpp
, gpp_offset
;
978 void __iomem
*reg
, *is
;
981 gpp
= padgrp
->reg_num
;
982 gpp_offset
= padgroup_offset(padgrp
, pin
);
984 reg
= community
->regs
+ community
->ie_offset
+ gpp
* 4;
985 is
= community
->regs
+ community
->is_offset
+ gpp
* 4;
987 raw_spin_lock_irqsave(&pctrl
->lock
, flags
);
989 /* Clear interrupt status first to avoid unexpected interrupt */
990 writel(BIT(gpp_offset
), is
);
994 value
&= ~BIT(gpp_offset
);
996 value
|= BIT(gpp_offset
);
998 raw_spin_unlock_irqrestore(&pctrl
->lock
, flags
);
1002 static void intel_gpio_irq_mask(struct irq_data
*d
)
1004 intel_gpio_irq_mask_unmask(d
, true);
1007 static void intel_gpio_irq_unmask(struct irq_data
*d
)
1009 intel_gpio_irq_mask_unmask(d
, false);
1012 static int intel_gpio_irq_type(struct irq_data
*d
, unsigned int type
)
1014 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
1015 struct intel_pinctrl
*pctrl
= gpiochip_get_data(gc
);
1016 unsigned int pin
= intel_gpio_to_pin(pctrl
, irqd_to_hwirq(d
), NULL
, NULL
);
1017 unsigned long flags
;
1021 reg
= intel_get_padcfg(pctrl
, pin
, PADCFG0
);
1026 * If the pin is in ACPI mode it is still usable as a GPIO but it
1027 * cannot be used as IRQ because GPI_IS status bit will not be
1028 * updated by the host controller hardware.
1030 if (intel_pad_acpi_mode(pctrl
, pin
)) {
1031 dev_warn(pctrl
->dev
, "pin %u cannot be used as IRQ\n", pin
);
1035 raw_spin_lock_irqsave(&pctrl
->lock
, flags
);
1037 intel_gpio_set_gpio_mode(reg
);
1041 value
&= ~(PADCFG0_RXEVCFG_MASK
| PADCFG0_RXINV
);
1043 if ((type
& IRQ_TYPE_EDGE_BOTH
) == IRQ_TYPE_EDGE_BOTH
) {
1044 value
|= PADCFG0_RXEVCFG_EDGE_BOTH
<< PADCFG0_RXEVCFG_SHIFT
;
1045 } else if (type
& IRQ_TYPE_EDGE_FALLING
) {
1046 value
|= PADCFG0_RXEVCFG_EDGE
<< PADCFG0_RXEVCFG_SHIFT
;
1047 value
|= PADCFG0_RXINV
;
1048 } else if (type
& IRQ_TYPE_EDGE_RISING
) {
1049 value
|= PADCFG0_RXEVCFG_EDGE
<< PADCFG0_RXEVCFG_SHIFT
;
1050 } else if (type
& IRQ_TYPE_LEVEL_MASK
) {
1051 if (type
& IRQ_TYPE_LEVEL_LOW
)
1052 value
|= PADCFG0_RXINV
;
1054 value
|= PADCFG0_RXEVCFG_DISABLED
<< PADCFG0_RXEVCFG_SHIFT
;
1059 if (type
& IRQ_TYPE_EDGE_BOTH
)
1060 irq_set_handler_locked(d
, handle_edge_irq
);
1061 else if (type
& IRQ_TYPE_LEVEL_MASK
)
1062 irq_set_handler_locked(d
, handle_level_irq
);
1064 raw_spin_unlock_irqrestore(&pctrl
->lock
, flags
);
1069 static int intel_gpio_irq_wake(struct irq_data
*d
, unsigned int on
)
1071 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
1072 struct intel_pinctrl
*pctrl
= gpiochip_get_data(gc
);
1073 unsigned int pin
= intel_gpio_to_pin(pctrl
, irqd_to_hwirq(d
), NULL
, NULL
);
1076 enable_irq_wake(pctrl
->irq
);
1078 disable_irq_wake(pctrl
->irq
);
1080 dev_dbg(pctrl
->dev
, "%sable wake for pin %u\n", on
? "en" : "dis", pin
);
1084 static irqreturn_t
intel_gpio_community_irq_handler(struct intel_pinctrl
*pctrl
,
1085 const struct intel_community
*community
)
1087 struct gpio_chip
*gc
= &pctrl
->chip
;
1088 irqreturn_t ret
= IRQ_NONE
;
1091 for (gpp
= 0; gpp
< community
->ngpps
; gpp
++) {
1092 const struct intel_padgroup
*padgrp
= &community
->gpps
[gpp
];
1093 unsigned long pending
, enabled
, gpp_offset
;
1095 pending
= readl(community
->regs
+ community
->is_offset
+
1096 padgrp
->reg_num
* 4);
1097 enabled
= readl(community
->regs
+ community
->ie_offset
+
1098 padgrp
->reg_num
* 4);
1100 /* Only interrupts that are enabled */
1103 for_each_set_bit(gpp_offset
, &pending
, padgrp
->size
) {
1106 irq
= irq_find_mapping(gc
->irq
.domain
,
1107 padgrp
->gpio_base
+ gpp_offset
);
1108 generic_handle_irq(irq
);
1117 static irqreturn_t
intel_gpio_irq(int irq
, void *data
)
1119 const struct intel_community
*community
;
1120 struct intel_pinctrl
*pctrl
= data
;
1121 irqreturn_t ret
= IRQ_NONE
;
1124 /* Need to check all communities for pending interrupts */
1125 for (i
= 0; i
< pctrl
->ncommunities
; i
++) {
1126 community
= &pctrl
->communities
[i
];
1127 ret
|= intel_gpio_community_irq_handler(pctrl
, community
);
1133 static int intel_gpio_add_community_ranges(struct intel_pinctrl
*pctrl
,
1134 const struct intel_community
*community
)
1138 for (i
= 0; i
< community
->ngpps
; i
++) {
1139 const struct intel_padgroup
*gpp
= &community
->gpps
[i
];
1141 if (gpp
->gpio_base
< 0)
1144 ret
= gpiochip_add_pin_range(&pctrl
->chip
, dev_name(pctrl
->dev
),
1145 gpp
->gpio_base
, gpp
->base
,
1154 static int intel_gpio_add_pin_ranges(struct gpio_chip
*gc
)
1156 struct intel_pinctrl
*pctrl
= gpiochip_get_data(gc
);
1159 for (i
= 0; i
< pctrl
->ncommunities
; i
++) {
1160 struct intel_community
*community
= &pctrl
->communities
[i
];
1162 ret
= intel_gpio_add_community_ranges(pctrl
, community
);
1164 dev_err(pctrl
->dev
, "failed to add GPIO pin range\n");
1172 static unsigned int intel_gpio_ngpio(const struct intel_pinctrl
*pctrl
)
1174 const struct intel_community
*community
;
1175 unsigned int ngpio
= 0;
1178 for (i
= 0; i
< pctrl
->ncommunities
; i
++) {
1179 community
= &pctrl
->communities
[i
];
1180 for (j
= 0; j
< community
->ngpps
; j
++) {
1181 const struct intel_padgroup
*gpp
= &community
->gpps
[j
];
1183 if (gpp
->gpio_base
< 0)
1186 if (gpp
->gpio_base
+ gpp
->size
> ngpio
)
1187 ngpio
= gpp
->gpio_base
+ gpp
->size
;
1194 static int intel_gpio_probe(struct intel_pinctrl
*pctrl
, int irq
)
1197 struct gpio_irq_chip
*girq
;
1199 pctrl
->chip
= intel_gpio_chip
;
1201 /* Setup GPIO chip */
1202 pctrl
->chip
.ngpio
= intel_gpio_ngpio(pctrl
);
1203 pctrl
->chip
.label
= dev_name(pctrl
->dev
);
1204 pctrl
->chip
.parent
= pctrl
->dev
;
1205 pctrl
->chip
.base
= -1;
1206 pctrl
->chip
.add_pin_ranges
= intel_gpio_add_pin_ranges
;
1209 /* Setup IRQ chip */
1210 pctrl
->irqchip
.name
= dev_name(pctrl
->dev
);
1211 pctrl
->irqchip
.irq_ack
= intel_gpio_irq_ack
;
1212 pctrl
->irqchip
.irq_mask
= intel_gpio_irq_mask
;
1213 pctrl
->irqchip
.irq_unmask
= intel_gpio_irq_unmask
;
1214 pctrl
->irqchip
.irq_set_type
= intel_gpio_irq_type
;
1215 pctrl
->irqchip
.irq_set_wake
= intel_gpio_irq_wake
;
1216 pctrl
->irqchip
.flags
= IRQCHIP_MASK_ON_SUSPEND
;
1219 * On some platforms several GPIO controllers share the same interrupt
1222 ret
= devm_request_irq(pctrl
->dev
, irq
, intel_gpio_irq
,
1223 IRQF_SHARED
| IRQF_NO_THREAD
,
1224 dev_name(pctrl
->dev
), pctrl
);
1226 dev_err(pctrl
->dev
, "failed to request interrupt\n");
1230 girq
= &pctrl
->chip
.irq
;
1231 girq
->chip
= &pctrl
->irqchip
;
1232 /* This will let us handle the IRQ in the driver */
1233 girq
->parent_handler
= NULL
;
1234 girq
->num_parents
= 0;
1235 girq
->default_type
= IRQ_TYPE_NONE
;
1236 girq
->handler
= handle_bad_irq
;
1238 ret
= devm_gpiochip_add_data(pctrl
->dev
, &pctrl
->chip
, pctrl
);
1240 dev_err(pctrl
->dev
, "failed to register gpiochip\n");
1247 static int intel_pinctrl_add_padgroups(struct intel_pinctrl
*pctrl
,
1248 struct intel_community
*community
)
1250 struct intel_padgroup
*gpps
;
1251 unsigned int npins
= community
->npins
;
1252 unsigned int padown_num
= 0;
1255 if (community
->gpps
)
1256 ngpps
= community
->ngpps
;
1258 ngpps
= DIV_ROUND_UP(community
->npins
, community
->gpp_size
);
1260 gpps
= devm_kcalloc(pctrl
->dev
, ngpps
, sizeof(*gpps
), GFP_KERNEL
);
1264 for (i
= 0; i
< ngpps
; i
++) {
1265 if (community
->gpps
) {
1266 gpps
[i
] = community
->gpps
[i
];
1268 unsigned int gpp_size
= community
->gpp_size
;
1270 gpps
[i
].reg_num
= i
;
1271 gpps
[i
].base
= community
->pin_base
+ i
* gpp_size
;
1272 gpps
[i
].size
= min(gpp_size
, npins
);
1273 npins
-= gpps
[i
].size
;
1276 if (gpps
[i
].size
> 32)
1279 if (!gpps
[i
].gpio_base
)
1280 gpps
[i
].gpio_base
= gpps
[i
].base
;
1282 gpps
[i
].padown_num
= padown_num
;
1285 * In older hardware the number of padown registers per
1286 * group is fixed regardless of the group size.
1288 if (community
->gpp_num_padown_regs
)
1289 padown_num
+= community
->gpp_num_padown_regs
;
1291 padown_num
+= DIV_ROUND_UP(gpps
[i
].size
* 4, 32);
1294 community
->ngpps
= ngpps
;
1295 community
->gpps
= gpps
;
1300 static int intel_pinctrl_pm_init(struct intel_pinctrl
*pctrl
)
1302 #ifdef CONFIG_PM_SLEEP
1303 const struct intel_pinctrl_soc_data
*soc
= pctrl
->soc
;
1304 struct intel_community_context
*communities
;
1305 struct intel_pad_context
*pads
;
1308 pads
= devm_kcalloc(pctrl
->dev
, soc
->npins
, sizeof(*pads
), GFP_KERNEL
);
1312 communities
= devm_kcalloc(pctrl
->dev
, pctrl
->ncommunities
,
1313 sizeof(*communities
), GFP_KERNEL
);
1318 for (i
= 0; i
< pctrl
->ncommunities
; i
++) {
1319 struct intel_community
*community
= &pctrl
->communities
[i
];
1320 u32
*intmask
, *hostown
;
1322 intmask
= devm_kcalloc(pctrl
->dev
, community
->ngpps
,
1323 sizeof(*intmask
), GFP_KERNEL
);
1327 communities
[i
].intmask
= intmask
;
1329 hostown
= devm_kcalloc(pctrl
->dev
, community
->ngpps
,
1330 sizeof(*hostown
), GFP_KERNEL
);
1334 communities
[i
].hostown
= hostown
;
1337 pctrl
->context
.pads
= pads
;
1338 pctrl
->context
.communities
= communities
;
1344 static int intel_pinctrl_probe(struct platform_device
*pdev
,
1345 const struct intel_pinctrl_soc_data
*soc_data
)
1347 struct intel_pinctrl
*pctrl
;
1353 pctrl
= devm_kzalloc(&pdev
->dev
, sizeof(*pctrl
), GFP_KERNEL
);
1357 pctrl
->dev
= &pdev
->dev
;
1358 pctrl
->soc
= soc_data
;
1359 raw_spin_lock_init(&pctrl
->lock
);
1362 * Make a copy of the communities which we can use to hold pointers
1365 pctrl
->ncommunities
= pctrl
->soc
->ncommunities
;
1366 pctrl
->communities
= devm_kcalloc(&pdev
->dev
, pctrl
->ncommunities
,
1367 sizeof(*pctrl
->communities
), GFP_KERNEL
);
1368 if (!pctrl
->communities
)
1371 for (i
= 0; i
< pctrl
->ncommunities
; i
++) {
1372 struct intel_community
*community
= &pctrl
->communities
[i
];
1376 *community
= pctrl
->soc
->communities
[i
];
1378 regs
= devm_platform_ioremap_resource(pdev
, community
->barno
);
1380 return PTR_ERR(regs
);
1383 * Determine community features based on the revision if
1384 * not specified already.
1386 if (!community
->features
) {
1389 rev
= (readl(regs
+ REVID
) & REVID_MASK
) >> REVID_SHIFT
;
1391 community
->features
|= PINCTRL_FEATURE_DEBOUNCE
;
1392 community
->features
|= PINCTRL_FEATURE_1K_PD
;
1396 /* Read offset of the pad configuration registers */
1397 padbar
= readl(regs
+ PADBAR
);
1399 community
->regs
= regs
;
1400 community
->pad_regs
= regs
+ padbar
;
1402 ret
= intel_pinctrl_add_padgroups(pctrl
, community
);
1407 irq
= platform_get_irq(pdev
, 0);
1411 ret
= intel_pinctrl_pm_init(pctrl
);
1415 pctrl
->pctldesc
= intel_pinctrl_desc
;
1416 pctrl
->pctldesc
.name
= dev_name(&pdev
->dev
);
1417 pctrl
->pctldesc
.pins
= pctrl
->soc
->pins
;
1418 pctrl
->pctldesc
.npins
= pctrl
->soc
->npins
;
1420 pctrl
->pctldev
= devm_pinctrl_register(&pdev
->dev
, &pctrl
->pctldesc
,
1422 if (IS_ERR(pctrl
->pctldev
)) {
1423 dev_err(&pdev
->dev
, "failed to register pinctrl driver\n");
1424 return PTR_ERR(pctrl
->pctldev
);
1427 ret
= intel_gpio_probe(pctrl
, irq
);
1431 platform_set_drvdata(pdev
, pctrl
);
1436 int intel_pinctrl_probe_by_hid(struct platform_device
*pdev
)
1438 const struct intel_pinctrl_soc_data
*data
;
1440 data
= device_get_match_data(&pdev
->dev
);
1441 return intel_pinctrl_probe(pdev
, data
);
1443 EXPORT_SYMBOL_GPL(intel_pinctrl_probe_by_hid
);
1445 int intel_pinctrl_probe_by_uid(struct platform_device
*pdev
)
1447 const struct intel_pinctrl_soc_data
*data
= NULL
;
1448 const struct intel_pinctrl_soc_data
**table
;
1449 struct acpi_device
*adev
;
1452 adev
= ACPI_COMPANION(&pdev
->dev
);
1454 const void *match
= device_get_match_data(&pdev
->dev
);
1456 table
= (const struct intel_pinctrl_soc_data
**)match
;
1457 for (i
= 0; table
[i
]; i
++) {
1458 if (!strcmp(adev
->pnp
.unique_id
, table
[i
]->uid
)) {
1464 const struct platform_device_id
*id
;
1466 id
= platform_get_device_id(pdev
);
1470 table
= (const struct intel_pinctrl_soc_data
**)id
->driver_data
;
1471 data
= table
[pdev
->id
];
1474 return intel_pinctrl_probe(pdev
, data
);
1476 EXPORT_SYMBOL_GPL(intel_pinctrl_probe_by_uid
);
1478 #ifdef CONFIG_PM_SLEEP
1479 static bool intel_pinctrl_should_save(struct intel_pinctrl
*pctrl
, unsigned int pin
)
1481 const struct pin_desc
*pd
= pin_desc_get(pctrl
->pctldev
, pin
);
1483 if (!pd
|| !intel_pad_usable(pctrl
, pin
))
1487 * Only restore the pin if it is actually in use by the kernel (or
1488 * by userspace). It is possible that some pins are used by the
1489 * BIOS during resume and those are not always locked down so leave
1492 if (pd
->mux_owner
|| pd
->gpio_owner
||
1493 gpiochip_line_is_irq(&pctrl
->chip
, intel_pin_to_gpio(pctrl
, pin
)))
1499 int intel_pinctrl_suspend_noirq(struct device
*dev
)
1501 struct intel_pinctrl
*pctrl
= dev_get_drvdata(dev
);
1502 struct intel_community_context
*communities
;
1503 struct intel_pad_context
*pads
;
1506 pads
= pctrl
->context
.pads
;
1507 for (i
= 0; i
< pctrl
->soc
->npins
; i
++) {
1508 const struct pinctrl_pin_desc
*desc
= &pctrl
->soc
->pins
[i
];
1509 void __iomem
*padcfg
;
1512 if (!intel_pinctrl_should_save(pctrl
, desc
->number
))
1515 val
= readl(intel_get_padcfg(pctrl
, desc
->number
, PADCFG0
));
1516 pads
[i
].padcfg0
= val
& ~PADCFG0_GPIORXSTATE
;
1517 val
= readl(intel_get_padcfg(pctrl
, desc
->number
, PADCFG1
));
1518 pads
[i
].padcfg1
= val
;
1520 padcfg
= intel_get_padcfg(pctrl
, desc
->number
, PADCFG2
);
1522 pads
[i
].padcfg2
= readl(padcfg
);
1525 communities
= pctrl
->context
.communities
;
1526 for (i
= 0; i
< pctrl
->ncommunities
; i
++) {
1527 struct intel_community
*community
= &pctrl
->communities
[i
];
1531 base
= community
->regs
+ community
->ie_offset
;
1532 for (gpp
= 0; gpp
< community
->ngpps
; gpp
++)
1533 communities
[i
].intmask
[gpp
] = readl(base
+ gpp
* 4);
1535 base
= community
->regs
+ community
->hostown_offset
;
1536 for (gpp
= 0; gpp
< community
->ngpps
; gpp
++)
1537 communities
[i
].hostown
[gpp
] = readl(base
+ gpp
* 4);
1542 EXPORT_SYMBOL_GPL(intel_pinctrl_suspend_noirq
);
1544 static void intel_gpio_irq_init(struct intel_pinctrl
*pctrl
)
1548 for (i
= 0; i
< pctrl
->ncommunities
; i
++) {
1549 const struct intel_community
*community
;
1553 community
= &pctrl
->communities
[i
];
1554 base
= community
->regs
;
1556 for (gpp
= 0; gpp
< community
->ngpps
; gpp
++) {
1557 /* Mask and clear all interrupts */
1558 writel(0, base
+ community
->ie_offset
+ gpp
* 4);
1559 writel(0xffff, base
+ community
->is_offset
+ gpp
* 4);
1565 intel_gpio_is_requested(struct gpio_chip
*chip
, int base
, unsigned int size
)
1570 for (i
= 0; i
< size
; i
++)
1571 if (gpiochip_is_requested(chip
, base
+ i
))
1572 requested
|= BIT(i
);
1577 static bool intel_gpio_update_reg(void __iomem
*reg
, u32 mask
, u32 value
)
1583 updated
= (curr
& ~mask
) | (value
& mask
);
1584 if (curr
== updated
)
1587 writel(updated
, reg
);
1591 static void intel_restore_hostown(struct intel_pinctrl
*pctrl
, unsigned int c
,
1592 void __iomem
*base
, unsigned int gpp
, u32 saved
)
1594 const struct intel_community
*community
= &pctrl
->communities
[c
];
1595 const struct intel_padgroup
*padgrp
= &community
->gpps
[gpp
];
1596 struct device
*dev
= pctrl
->dev
;
1599 if (padgrp
->gpio_base
< 0)
1602 requested
= intel_gpio_is_requested(&pctrl
->chip
, padgrp
->gpio_base
, padgrp
->size
);
1603 if (!intel_gpio_update_reg(base
+ gpp
* 4, requested
, saved
))
1606 dev_dbg(dev
, "restored hostown %u/%u %#08x\n", c
, gpp
, readl(base
+ gpp
* 4));
1609 static void intel_restore_intmask(struct intel_pinctrl
*pctrl
, unsigned int c
,
1610 void __iomem
*base
, unsigned int gpp
, u32 saved
)
1612 struct device
*dev
= pctrl
->dev
;
1614 if (!intel_gpio_update_reg(base
+ gpp
* 4, ~0U, saved
))
1617 dev_dbg(dev
, "restored mask %u/%u %#08x\n", c
, gpp
, readl(base
+ gpp
* 4));
1620 static void intel_restore_padcfg(struct intel_pinctrl
*pctrl
, unsigned int pin
,
1621 unsigned int reg
, u32 saved
)
1623 u32 mask
= (reg
== PADCFG0
) ? PADCFG0_GPIORXSTATE
: 0;
1624 unsigned int n
= reg
/ sizeof(u32
);
1625 struct device
*dev
= pctrl
->dev
;
1626 void __iomem
*padcfg
;
1628 padcfg
= intel_get_padcfg(pctrl
, pin
, reg
);
1632 if (!intel_gpio_update_reg(padcfg
, ~mask
, saved
))
1635 dev_dbg(dev
, "restored pin %u padcfg%u %#08x\n", pin
, n
, readl(padcfg
));
1638 int intel_pinctrl_resume_noirq(struct device
*dev
)
1640 struct intel_pinctrl
*pctrl
= dev_get_drvdata(dev
);
1641 const struct intel_community_context
*communities
;
1642 const struct intel_pad_context
*pads
;
1645 /* Mask all interrupts */
1646 intel_gpio_irq_init(pctrl
);
1648 pads
= pctrl
->context
.pads
;
1649 for (i
= 0; i
< pctrl
->soc
->npins
; i
++) {
1650 const struct pinctrl_pin_desc
*desc
= &pctrl
->soc
->pins
[i
];
1652 if (!intel_pinctrl_should_save(pctrl
, desc
->number
))
1655 intel_restore_padcfg(pctrl
, desc
->number
, PADCFG0
, pads
[i
].padcfg0
);
1656 intel_restore_padcfg(pctrl
, desc
->number
, PADCFG1
, pads
[i
].padcfg1
);
1657 intel_restore_padcfg(pctrl
, desc
->number
, PADCFG2
, pads
[i
].padcfg2
);
1660 communities
= pctrl
->context
.communities
;
1661 for (i
= 0; i
< pctrl
->ncommunities
; i
++) {
1662 struct intel_community
*community
= &pctrl
->communities
[i
];
1666 base
= community
->regs
+ community
->ie_offset
;
1667 for (gpp
= 0; gpp
< community
->ngpps
; gpp
++)
1668 intel_restore_intmask(pctrl
, i
, base
, gpp
, communities
[i
].intmask
[gpp
]);
1670 base
= community
->regs
+ community
->hostown_offset
;
1671 for (gpp
= 0; gpp
< community
->ngpps
; gpp
++)
1672 intel_restore_hostown(pctrl
, i
, base
, gpp
, communities
[i
].hostown
[gpp
]);
1677 EXPORT_SYMBOL_GPL(intel_pinctrl_resume_noirq
);
1680 MODULE_AUTHOR("Mathias Nyman <mathias.nyman@linux.intel.com>");
1681 MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
1682 MODULE_DESCRIPTION("Intel pinctrl/GPIO core driver");
1683 MODULE_LICENSE("GPL v2");