1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (c) 2014,2015 AMD Corporation.
6 * Authors: Ken Xue <Ken.Xue@amd.com>
7 * Wu, Jeff <Jeff.Wu@amd.com>
11 #include <linux/err.h>
12 #include <linux/bug.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/spinlock.h>
16 #include <linux/compiler.h>
17 #include <linux/types.h>
18 #include <linux/errno.h>
19 #include <linux/log2.h>
21 #include <linux/gpio/driver.h>
22 #include <linux/slab.h>
23 #include <linux/platform_device.h>
24 #include <linux/mutex.h>
25 #include <linux/acpi.h>
26 #include <linux/seq_file.h>
27 #include <linux/interrupt.h>
28 #include <linux/list.h>
29 #include <linux/bitops.h>
30 #include <linux/pinctrl/pinconf.h>
31 #include <linux/pinctrl/pinconf-generic.h>
32 #include <linux/pinctrl/pinmux.h>
33 #include <linux/suspend.h>
36 #include "pinctrl-utils.h"
37 #include "pinctrl-amd.h"
39 static int amd_gpio_get_direction(struct gpio_chip
*gc
, unsigned offset
)
43 struct amd_gpio
*gpio_dev
= gpiochip_get_data(gc
);
45 raw_spin_lock_irqsave(&gpio_dev
->lock
, flags
);
46 pin_reg
= readl(gpio_dev
->base
+ offset
* 4);
47 raw_spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
49 if (pin_reg
& BIT(OUTPUT_ENABLE_OFF
))
50 return GPIO_LINE_DIRECTION_OUT
;
52 return GPIO_LINE_DIRECTION_IN
;
55 static int amd_gpio_direction_input(struct gpio_chip
*gc
, unsigned offset
)
59 struct amd_gpio
*gpio_dev
= gpiochip_get_data(gc
);
61 raw_spin_lock_irqsave(&gpio_dev
->lock
, flags
);
62 pin_reg
= readl(gpio_dev
->base
+ offset
* 4);
63 pin_reg
&= ~BIT(OUTPUT_ENABLE_OFF
);
64 writel(pin_reg
, gpio_dev
->base
+ offset
* 4);
65 raw_spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
70 static int amd_gpio_direction_output(struct gpio_chip
*gc
, unsigned offset
,
75 struct amd_gpio
*gpio_dev
= gpiochip_get_data(gc
);
77 raw_spin_lock_irqsave(&gpio_dev
->lock
, flags
);
78 pin_reg
= readl(gpio_dev
->base
+ offset
* 4);
79 pin_reg
|= BIT(OUTPUT_ENABLE_OFF
);
81 pin_reg
|= BIT(OUTPUT_VALUE_OFF
);
83 pin_reg
&= ~BIT(OUTPUT_VALUE_OFF
);
84 writel(pin_reg
, gpio_dev
->base
+ offset
* 4);
85 raw_spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
90 static int amd_gpio_get_value(struct gpio_chip
*gc
, unsigned offset
)
94 struct amd_gpio
*gpio_dev
= gpiochip_get_data(gc
);
96 raw_spin_lock_irqsave(&gpio_dev
->lock
, flags
);
97 pin_reg
= readl(gpio_dev
->base
+ offset
* 4);
98 raw_spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
100 return !!(pin_reg
& BIT(PIN_STS_OFF
));
103 static void amd_gpio_set_value(struct gpio_chip
*gc
, unsigned offset
, int value
)
107 struct amd_gpio
*gpio_dev
= gpiochip_get_data(gc
);
109 raw_spin_lock_irqsave(&gpio_dev
->lock
, flags
);
110 pin_reg
= readl(gpio_dev
->base
+ offset
* 4);
112 pin_reg
|= BIT(OUTPUT_VALUE_OFF
);
114 pin_reg
&= ~BIT(OUTPUT_VALUE_OFF
);
115 writel(pin_reg
, gpio_dev
->base
+ offset
* 4);
116 raw_spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
119 static int amd_gpio_set_debounce(struct amd_gpio
*gpio_dev
, unsigned int offset
,
120 unsigned int debounce
)
126 /* Use special handling for Pin0 debounce */
128 pin_reg
= readl(gpio_dev
->base
+ WAKE_INT_MASTER_REG
);
129 if (pin_reg
& INTERNAL_GPIO0_DEBOUNCE
)
133 pin_reg
= readl(gpio_dev
->base
+ offset
* 4);
136 pin_reg
|= DB_TYPE_REMOVE_GLITCH
<< DB_CNTRL_OFF
;
137 pin_reg
&= ~DB_TMR_OUT_MASK
;
139 Debounce Debounce Timer Max
140 TmrLarge TmrOutUnit Unit Debounce
142 0 0 61 usec (2 RtcClk) 976 usec
143 0 1 244 usec (8 RtcClk) 3.9 msec
144 1 0 15.6 msec (512 RtcClk) 250 msec
145 1 1 62.5 msec (2048 RtcClk) 1 sec
150 pin_reg
&= ~BIT(DB_TMR_OUT_UNIT_OFF
);
151 pin_reg
&= ~BIT(DB_TMR_LARGE_OFF
);
152 } else if (debounce
< 976) {
153 time
= debounce
/ 61;
154 pin_reg
|= time
& DB_TMR_OUT_MASK
;
155 pin_reg
&= ~BIT(DB_TMR_OUT_UNIT_OFF
);
156 pin_reg
&= ~BIT(DB_TMR_LARGE_OFF
);
157 } else if (debounce
< 3900) {
158 time
= debounce
/ 244;
159 pin_reg
|= time
& DB_TMR_OUT_MASK
;
160 pin_reg
|= BIT(DB_TMR_OUT_UNIT_OFF
);
161 pin_reg
&= ~BIT(DB_TMR_LARGE_OFF
);
162 } else if (debounce
< 250000) {
163 time
= debounce
/ 15625;
164 pin_reg
|= time
& DB_TMR_OUT_MASK
;
165 pin_reg
&= ~BIT(DB_TMR_OUT_UNIT_OFF
);
166 pin_reg
|= BIT(DB_TMR_LARGE_OFF
);
167 } else if (debounce
< 1000000) {
168 time
= debounce
/ 62500;
169 pin_reg
|= time
& DB_TMR_OUT_MASK
;
170 pin_reg
|= BIT(DB_TMR_OUT_UNIT_OFF
);
171 pin_reg
|= BIT(DB_TMR_LARGE_OFF
);
173 pin_reg
&= ~(DB_CNTRl_MASK
<< DB_CNTRL_OFF
);
177 pin_reg
&= ~BIT(DB_TMR_OUT_UNIT_OFF
);
178 pin_reg
&= ~BIT(DB_TMR_LARGE_OFF
);
179 pin_reg
&= ~DB_TMR_OUT_MASK
;
180 pin_reg
&= ~(DB_CNTRl_MASK
<< DB_CNTRL_OFF
);
182 writel(pin_reg
, gpio_dev
->base
+ offset
* 4);
187 #ifdef CONFIG_DEBUG_FS
188 static void amd_gpio_dbg_show(struct seq_file
*s
, struct gpio_chip
*gc
)
193 unsigned int bank
, i
, pin_num
;
194 struct amd_gpio
*gpio_dev
= gpiochip_get_data(gc
);
201 char *interrupt_mask
;
209 char debounce_value
[40];
210 char *debounce_enable
;
213 seq_printf(s
, "WAKE_INT_MASTER_REG: 0x%08x\n", readl(gpio_dev
->base
+ WAKE_INT_MASTER_REG
));
214 for (bank
= 0; bank
< gpio_dev
->hwbank_num
; bank
++) {
215 unsigned int time
= 0;
216 unsigned int unit
= 0;
221 pin_num
= AMD_GPIO_PINS_BANK0
;
225 pin_num
= AMD_GPIO_PINS_BANK1
+ i
;
229 pin_num
= AMD_GPIO_PINS_BANK2
+ i
;
233 pin_num
= AMD_GPIO_PINS_BANK3
+ i
;
236 /* Illegal bank number, ignore */
239 seq_printf(s
, "GPIO bank%d\n", bank
);
240 seq_puts(s
, "gpio\t int|active|trigger|S0i3| S3|S4/S5| Z|wake|pull| orient| debounce|reg\n");
241 for (; i
< pin_num
; i
++) {
242 seq_printf(s
, "#%d\t", i
);
243 raw_spin_lock_irqsave(&gpio_dev
->lock
, flags
);
244 pin_reg
= readl(gpio_dev
->base
+ i
* 4);
245 raw_spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
247 if (pin_reg
& BIT(INTERRUPT_ENABLE_OFF
)) {
248 u8 level
= (pin_reg
>> ACTIVE_LEVEL_OFF
) &
251 if (level
== ACTIVE_LEVEL_HIGH
)
253 else if (level
== ACTIVE_LEVEL_LOW
)
255 else if (!(pin_reg
& BIT(LEVEL_TRIG_OFF
)) &&
256 level
== ACTIVE_LEVEL_BOTH
)
261 if (pin_reg
& BIT(LEVEL_TRIG_OFF
))
262 level_trig
= "level";
264 level_trig
= " edge";
266 if (pin_reg
& BIT(INTERRUPT_MASK_OFF
))
267 interrupt_mask
= "😛";
269 interrupt_mask
= "😷";
271 if (pin_reg
& BIT(INTERRUPT_STS_OFF
))
276 seq_printf(s
, "%s %s| %s| %s|",
282 seq_puts(s
, " ∅| | |");
284 if (pin_reg
& BIT(WAKE_CNTRL_OFF_S0I3
))
288 seq_printf(s
, " %s| ", wake_cntrl0
);
290 if (pin_reg
& BIT(WAKE_CNTRL_OFF_S3
))
294 seq_printf(s
, "%s|", wake_cntrl1
);
296 if (pin_reg
& BIT(WAKE_CNTRL_OFF_S4
))
300 seq_printf(s
, " %s|", wake_cntrl2
);
302 if (pin_reg
& BIT(WAKECNTRL_Z_OFF
))
306 seq_printf(s
, "%s|", wake_cntrlz
);
308 if (pin_reg
& BIT(WAKE_STS_OFF
))
312 seq_printf(s
, " %s|", wake_sts
);
314 if (pin_reg
& BIT(PULL_UP_ENABLE_OFF
)) {
316 } else if (pin_reg
& BIT(PULL_DOWN_ENABLE_OFF
)) {
322 if (pin_reg
& BIT(OUTPUT_ENABLE_OFF
)) {
324 if (pin_reg
& BIT(OUTPUT_VALUE_OFF
))
330 if (pin_reg
& BIT(PIN_STS_OFF
))
335 seq_printf(s
, "%s %s|", pin_sts
, orientation
);
337 db_cntrl
= (DB_CNTRl_MASK
<< DB_CNTRL_OFF
) & pin_reg
;
339 tmr_out_unit
= pin_reg
& BIT(DB_TMR_OUT_UNIT_OFF
);
340 tmr_large
= pin_reg
& BIT(DB_TMR_LARGE_OFF
);
341 time
= pin_reg
& DB_TMR_OUT_MASK
;
353 if ((DB_TYPE_REMOVE_GLITCH
<< DB_CNTRL_OFF
) == db_cntrl
)
354 debounce_enable
= "b";
355 else if ((DB_TYPE_PRESERVE_LOW_GLITCH
<< DB_CNTRL_OFF
) == db_cntrl
)
356 debounce_enable
= "↓";
358 debounce_enable
= "↑";
359 snprintf(debounce_value
, sizeof(debounce_value
), "%06u", time
* unit
);
360 seq_printf(s
, "%s (🕑 %sus)|", debounce_enable
, debounce_value
);
364 seq_printf(s
, "0x%x\n", pin_reg
);
369 #define amd_gpio_dbg_show NULL
372 static void amd_gpio_irq_enable(struct irq_data
*d
)
376 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
377 struct amd_gpio
*gpio_dev
= gpiochip_get_data(gc
);
379 gpiochip_enable_irq(gc
, d
->hwirq
);
381 raw_spin_lock_irqsave(&gpio_dev
->lock
, flags
);
382 pin_reg
= readl(gpio_dev
->base
+ (d
->hwirq
)*4);
383 pin_reg
|= BIT(INTERRUPT_ENABLE_OFF
);
384 pin_reg
|= BIT(INTERRUPT_MASK_OFF
);
385 writel(pin_reg
, gpio_dev
->base
+ (d
->hwirq
)*4);
386 raw_spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
389 static void amd_gpio_irq_disable(struct irq_data
*d
)
393 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
394 struct amd_gpio
*gpio_dev
= gpiochip_get_data(gc
);
396 raw_spin_lock_irqsave(&gpio_dev
->lock
, flags
);
397 pin_reg
= readl(gpio_dev
->base
+ (d
->hwirq
)*4);
398 pin_reg
&= ~BIT(INTERRUPT_ENABLE_OFF
);
399 pin_reg
&= ~BIT(INTERRUPT_MASK_OFF
);
400 writel(pin_reg
, gpio_dev
->base
+ (d
->hwirq
)*4);
401 raw_spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
403 gpiochip_disable_irq(gc
, d
->hwirq
);
406 static void amd_gpio_irq_mask(struct irq_data
*d
)
410 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
411 struct amd_gpio
*gpio_dev
= gpiochip_get_data(gc
);
413 raw_spin_lock_irqsave(&gpio_dev
->lock
, flags
);
414 pin_reg
= readl(gpio_dev
->base
+ (d
->hwirq
)*4);
415 pin_reg
&= ~BIT(INTERRUPT_MASK_OFF
);
416 writel(pin_reg
, gpio_dev
->base
+ (d
->hwirq
)*4);
417 raw_spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
420 static void amd_gpio_irq_unmask(struct irq_data
*d
)
424 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
425 struct amd_gpio
*gpio_dev
= gpiochip_get_data(gc
);
427 raw_spin_lock_irqsave(&gpio_dev
->lock
, flags
);
428 pin_reg
= readl(gpio_dev
->base
+ (d
->hwirq
)*4);
429 pin_reg
|= BIT(INTERRUPT_MASK_OFF
);
430 writel(pin_reg
, gpio_dev
->base
+ (d
->hwirq
)*4);
431 raw_spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
434 static int amd_gpio_irq_set_wake(struct irq_data
*d
, unsigned int on
)
438 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
439 struct amd_gpio
*gpio_dev
= gpiochip_get_data(gc
);
440 u32 wake_mask
= BIT(WAKE_CNTRL_OFF_S0I3
) | BIT(WAKE_CNTRL_OFF_S3
);
443 raw_spin_lock_irqsave(&gpio_dev
->lock
, flags
);
444 pin_reg
= readl(gpio_dev
->base
+ (d
->hwirq
)*4);
447 pin_reg
|= wake_mask
;
449 pin_reg
&= ~wake_mask
;
451 writel(pin_reg
, gpio_dev
->base
+ (d
->hwirq
)*4);
452 raw_spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
455 err
= enable_irq_wake(gpio_dev
->irq
);
457 err
= disable_irq_wake(gpio_dev
->irq
);
460 dev_err(&gpio_dev
->pdev
->dev
, "failed to %s wake-up interrupt\n",
461 on
? "enable" : "disable");
466 static void amd_gpio_irq_eoi(struct irq_data
*d
)
470 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
471 struct amd_gpio
*gpio_dev
= gpiochip_get_data(gc
);
473 raw_spin_lock_irqsave(&gpio_dev
->lock
, flags
);
474 reg
= readl(gpio_dev
->base
+ WAKE_INT_MASTER_REG
);
476 writel(reg
, gpio_dev
->base
+ WAKE_INT_MASTER_REG
);
477 raw_spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
480 static int amd_gpio_irq_set_type(struct irq_data
*d
, unsigned int type
)
483 u32 pin_reg
, pin_reg_irq_en
, mask
;
485 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
486 struct amd_gpio
*gpio_dev
= gpiochip_get_data(gc
);
488 raw_spin_lock_irqsave(&gpio_dev
->lock
, flags
);
489 pin_reg
= readl(gpio_dev
->base
+ (d
->hwirq
)*4);
491 switch (type
& IRQ_TYPE_SENSE_MASK
) {
492 case IRQ_TYPE_EDGE_RISING
:
493 pin_reg
&= ~BIT(LEVEL_TRIG_OFF
);
494 pin_reg
&= ~(ACTIVE_LEVEL_MASK
<< ACTIVE_LEVEL_OFF
);
495 pin_reg
|= ACTIVE_HIGH
<< ACTIVE_LEVEL_OFF
;
496 irq_set_handler_locked(d
, handle_edge_irq
);
499 case IRQ_TYPE_EDGE_FALLING
:
500 pin_reg
&= ~BIT(LEVEL_TRIG_OFF
);
501 pin_reg
&= ~(ACTIVE_LEVEL_MASK
<< ACTIVE_LEVEL_OFF
);
502 pin_reg
|= ACTIVE_LOW
<< ACTIVE_LEVEL_OFF
;
503 irq_set_handler_locked(d
, handle_edge_irq
);
506 case IRQ_TYPE_EDGE_BOTH
:
507 pin_reg
&= ~BIT(LEVEL_TRIG_OFF
);
508 pin_reg
&= ~(ACTIVE_LEVEL_MASK
<< ACTIVE_LEVEL_OFF
);
509 pin_reg
|= BOTH_EDGES
<< ACTIVE_LEVEL_OFF
;
510 irq_set_handler_locked(d
, handle_edge_irq
);
513 case IRQ_TYPE_LEVEL_HIGH
:
514 pin_reg
|= LEVEL_TRIGGER
<< LEVEL_TRIG_OFF
;
515 pin_reg
&= ~(ACTIVE_LEVEL_MASK
<< ACTIVE_LEVEL_OFF
);
516 pin_reg
|= ACTIVE_HIGH
<< ACTIVE_LEVEL_OFF
;
517 irq_set_handler_locked(d
, handle_level_irq
);
520 case IRQ_TYPE_LEVEL_LOW
:
521 pin_reg
|= LEVEL_TRIGGER
<< LEVEL_TRIG_OFF
;
522 pin_reg
&= ~(ACTIVE_LEVEL_MASK
<< ACTIVE_LEVEL_OFF
);
523 pin_reg
|= ACTIVE_LOW
<< ACTIVE_LEVEL_OFF
;
524 irq_set_handler_locked(d
, handle_level_irq
);
531 dev_err(&gpio_dev
->pdev
->dev
, "Invalid type value\n");
535 pin_reg
|= CLR_INTR_STAT
<< INTERRUPT_STS_OFF
;
537 * If WAKE_INT_MASTER_REG.MaskStsEn is set, a software write to the
538 * debounce registers of any GPIO will block wake/interrupt status
539 * generation for *all* GPIOs for a length of time that depends on
540 * WAKE_INT_MASTER_REG.MaskStsLength[11:0]. During this period the
541 * INTERRUPT_ENABLE bit will read as 0.
543 * We temporarily enable irq for the GPIO whose configuration is
544 * changing, and then wait for it to read back as 1 to know when
545 * debounce has settled and then disable the irq again.
546 * We do this polling with the spinlock held to ensure other GPIO
547 * access routines do not read an incorrect value for the irq enable
548 * bit of other GPIOs. We keep the GPIO masked while polling to avoid
549 * spurious irqs, and disable the irq again after polling.
551 mask
= BIT(INTERRUPT_ENABLE_OFF
);
552 pin_reg_irq_en
= pin_reg
;
553 pin_reg_irq_en
|= mask
;
554 pin_reg_irq_en
&= ~BIT(INTERRUPT_MASK_OFF
);
555 writel(pin_reg_irq_en
, gpio_dev
->base
+ (d
->hwirq
)*4);
556 while ((readl(gpio_dev
->base
+ (d
->hwirq
)*4) & mask
) != mask
)
558 writel(pin_reg
, gpio_dev
->base
+ (d
->hwirq
)*4);
559 raw_spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
564 static void amd_irq_ack(struct irq_data
*d
)
567 * based on HW design,there is no need to ack HW
568 * before handle current irq. But this routine is
569 * necessary for handle_edge_irq
573 static const struct irq_chip amd_gpio_irqchip
= {
575 .irq_ack
= amd_irq_ack
,
576 .irq_enable
= amd_gpio_irq_enable
,
577 .irq_disable
= amd_gpio_irq_disable
,
578 .irq_mask
= amd_gpio_irq_mask
,
579 .irq_unmask
= amd_gpio_irq_unmask
,
580 .irq_set_wake
= amd_gpio_irq_set_wake
,
581 .irq_eoi
= amd_gpio_irq_eoi
,
582 .irq_set_type
= amd_gpio_irq_set_type
,
584 * We need to set IRQCHIP_ENABLE_WAKEUP_ON_SUSPEND so that a wake event
585 * also generates an IRQ. We need the IRQ so the irq_handler can clear
586 * the wake event. Otherwise the wake event will never clear and
587 * prevent the system from suspending.
589 .flags
= IRQCHIP_ENABLE_WAKEUP_ON_SUSPEND
| IRQCHIP_IMMUTABLE
,
590 GPIOCHIP_IRQ_RESOURCE_HELPERS
,
593 #define PIN_IRQ_PENDING (BIT(INTERRUPT_STS_OFF) | BIT(WAKE_STS_OFF))
595 static bool do_amd_gpio_irq_handler(int irq
, void *dev_id
)
597 struct amd_gpio
*gpio_dev
= dev_id
;
598 struct gpio_chip
*gc
= &gpio_dev
->gc
;
599 unsigned int i
, irqnr
;
606 /* Read the wake status */
607 raw_spin_lock_irqsave(&gpio_dev
->lock
, flags
);
608 status
= readl(gpio_dev
->base
+ WAKE_INT_STATUS_REG1
);
610 status
|= readl(gpio_dev
->base
+ WAKE_INT_STATUS_REG0
);
611 raw_spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
613 /* Bit 0-45 contain the relevant status bits */
614 status
&= (1ULL << 46) - 1;
615 regs
= gpio_dev
->base
;
616 for (mask
= 1, irqnr
= 0; status
; mask
<<= 1, regs
+= 4, irqnr
+= 4) {
617 if (!(status
& mask
))
621 /* Each status bit covers four pins */
622 for (i
= 0; i
< 4; i
++) {
623 regval
= readl(regs
+ i
);
625 if (regval
& PIN_IRQ_PENDING
)
626 pm_pr_dbg("GPIO %d is active: 0x%x",
629 /* caused wake on resume context for shared IRQ */
630 if (irq
< 0 && (regval
& BIT(WAKE_STS_OFF
)))
633 if (!(regval
& PIN_IRQ_PENDING
) ||
634 !(regval
& BIT(INTERRUPT_MASK_OFF
)))
636 generic_handle_domain_irq_safe(gc
->irq
.domain
, irqnr
+ i
);
639 * We must read the pin register again, in case the
640 * value was changed while executing
641 * generic_handle_domain_irq() above.
642 * If the line is not an irq, disable it in order to
643 * avoid a system hang caused by an interrupt storm.
645 raw_spin_lock_irqsave(&gpio_dev
->lock
, flags
);
646 regval
= readl(regs
+ i
);
647 if (!gpiochip_line_is_irq(gc
, irqnr
+ i
)) {
648 regval
&= ~BIT(INTERRUPT_MASK_OFF
);
649 dev_dbg(&gpio_dev
->pdev
->dev
,
650 "Disabling spurious GPIO IRQ %d\n",
655 writel(regval
, regs
+ i
);
656 raw_spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
659 /* did not cause wake on resume context for shared IRQ */
663 /* Signal EOI to the GPIO unit */
664 raw_spin_lock_irqsave(&gpio_dev
->lock
, flags
);
665 regval
= readl(gpio_dev
->base
+ WAKE_INT_MASTER_REG
);
667 writel(regval
, gpio_dev
->base
+ WAKE_INT_MASTER_REG
);
668 raw_spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
673 static irqreturn_t
amd_gpio_irq_handler(int irq
, void *dev_id
)
675 return IRQ_RETVAL(do_amd_gpio_irq_handler(irq
, dev_id
));
678 static bool __maybe_unused
amd_gpio_check_wake(void *dev_id
)
680 return do_amd_gpio_irq_handler(-1, dev_id
);
683 static int amd_get_groups_count(struct pinctrl_dev
*pctldev
)
685 struct amd_gpio
*gpio_dev
= pinctrl_dev_get_drvdata(pctldev
);
687 return gpio_dev
->ngroups
;
690 static const char *amd_get_group_name(struct pinctrl_dev
*pctldev
,
693 struct amd_gpio
*gpio_dev
= pinctrl_dev_get_drvdata(pctldev
);
695 return gpio_dev
->groups
[group
].name
;
698 static int amd_get_group_pins(struct pinctrl_dev
*pctldev
,
700 const unsigned **pins
,
703 struct amd_gpio
*gpio_dev
= pinctrl_dev_get_drvdata(pctldev
);
705 *pins
= gpio_dev
->groups
[group
].pins
;
706 *num_pins
= gpio_dev
->groups
[group
].npins
;
710 static const struct pinctrl_ops amd_pinctrl_ops
= {
711 .get_groups_count
= amd_get_groups_count
,
712 .get_group_name
= amd_get_group_name
,
713 .get_group_pins
= amd_get_group_pins
,
715 .dt_node_to_map
= pinconf_generic_dt_node_to_map_group
,
716 .dt_free_map
= pinctrl_utils_free_map
,
720 static int amd_pinconf_get(struct pinctrl_dev
*pctldev
,
722 unsigned long *config
)
727 struct amd_gpio
*gpio_dev
= pinctrl_dev_get_drvdata(pctldev
);
728 enum pin_config_param param
= pinconf_to_config_param(*config
);
730 raw_spin_lock_irqsave(&gpio_dev
->lock
, flags
);
731 pin_reg
= readl(gpio_dev
->base
+ pin
*4);
732 raw_spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
734 case PIN_CONFIG_INPUT_DEBOUNCE
:
735 arg
= pin_reg
& DB_TMR_OUT_MASK
;
738 case PIN_CONFIG_BIAS_PULL_DOWN
:
739 arg
= (pin_reg
>> PULL_DOWN_ENABLE_OFF
) & BIT(0);
742 case PIN_CONFIG_BIAS_PULL_UP
:
743 arg
= (pin_reg
>> PULL_UP_ENABLE_OFF
) & BIT(0);
746 case PIN_CONFIG_DRIVE_STRENGTH
:
747 arg
= (pin_reg
>> DRV_STRENGTH_SEL_OFF
) & DRV_STRENGTH_SEL_MASK
;
751 dev_dbg(&gpio_dev
->pdev
->dev
, "Invalid config param %04x\n",
756 *config
= pinconf_to_config_packed(param
, arg
);
761 static int amd_pinconf_set(struct pinctrl_dev
*pctldev
, unsigned int pin
,
762 unsigned long *configs
, unsigned int num_configs
)
769 enum pin_config_param param
;
770 struct amd_gpio
*gpio_dev
= pinctrl_dev_get_drvdata(pctldev
);
772 raw_spin_lock_irqsave(&gpio_dev
->lock
, flags
);
773 for (i
= 0; i
< num_configs
; i
++) {
774 param
= pinconf_to_config_param(configs
[i
]);
775 arg
= pinconf_to_config_argument(configs
[i
]);
776 pin_reg
= readl(gpio_dev
->base
+ pin
*4);
779 case PIN_CONFIG_INPUT_DEBOUNCE
:
780 ret
= amd_gpio_set_debounce(gpio_dev
, pin
, arg
);
783 case PIN_CONFIG_BIAS_PULL_DOWN
:
784 pin_reg
&= ~BIT(PULL_DOWN_ENABLE_OFF
);
785 pin_reg
|= (arg
& BIT(0)) << PULL_DOWN_ENABLE_OFF
;
788 case PIN_CONFIG_BIAS_PULL_UP
:
789 pin_reg
&= ~BIT(PULL_UP_ENABLE_OFF
);
790 pin_reg
|= (arg
& BIT(0)) << PULL_UP_ENABLE_OFF
;
793 case PIN_CONFIG_DRIVE_STRENGTH
:
794 pin_reg
&= ~(DRV_STRENGTH_SEL_MASK
795 << DRV_STRENGTH_SEL_OFF
);
796 pin_reg
|= (arg
& DRV_STRENGTH_SEL_MASK
)
797 << DRV_STRENGTH_SEL_OFF
;
801 dev_dbg(&gpio_dev
->pdev
->dev
,
802 "Invalid config param %04x\n", param
);
806 writel(pin_reg
, gpio_dev
->base
+ pin
*4);
809 raw_spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
814 static int amd_pinconf_group_get(struct pinctrl_dev
*pctldev
,
816 unsigned long *config
)
818 const unsigned *pins
;
822 ret
= amd_get_group_pins(pctldev
, group
, &pins
, &npins
);
826 if (amd_pinconf_get(pctldev
, pins
[0], config
))
832 static int amd_pinconf_group_set(struct pinctrl_dev
*pctldev
,
833 unsigned group
, unsigned long *configs
,
834 unsigned num_configs
)
836 const unsigned *pins
;
840 ret
= amd_get_group_pins(pctldev
, group
, &pins
, &npins
);
843 for (i
= 0; i
< npins
; i
++) {
844 if (amd_pinconf_set(pctldev
, pins
[i
], configs
, num_configs
))
850 static int amd_gpio_set_config(struct gpio_chip
*gc
, unsigned int pin
,
851 unsigned long config
)
853 struct amd_gpio
*gpio_dev
= gpiochip_get_data(gc
);
855 return amd_pinconf_set(gpio_dev
->pctrl
, pin
, &config
, 1);
858 static const struct pinconf_ops amd_pinconf_ops
= {
859 .pin_config_get
= amd_pinconf_get
,
860 .pin_config_set
= amd_pinconf_set
,
861 .pin_config_group_get
= amd_pinconf_group_get
,
862 .pin_config_group_set
= amd_pinconf_group_set
,
865 static void amd_gpio_irq_init(struct amd_gpio
*gpio_dev
)
867 struct pinctrl_desc
*desc
= gpio_dev
->pctrl
->desc
;
872 mask
= BIT(WAKE_CNTRL_OFF_S0I3
) | BIT(WAKE_CNTRL_OFF_S3
) |
873 BIT(WAKE_CNTRL_OFF_S4
);
875 for (i
= 0; i
< desc
->npins
; i
++) {
876 int pin
= desc
->pins
[i
].number
;
877 const struct pin_desc
*pd
= pin_desc_get(gpio_dev
->pctrl
, pin
);
882 raw_spin_lock_irqsave(&gpio_dev
->lock
, flags
);
884 pin_reg
= readl(gpio_dev
->base
+ pin
* 4);
886 writel(pin_reg
, gpio_dev
->base
+ pin
* 4);
888 raw_spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
892 #ifdef CONFIG_PM_SLEEP
893 static bool amd_gpio_should_save(struct amd_gpio
*gpio_dev
, unsigned int pin
)
895 const struct pin_desc
*pd
= pin_desc_get(gpio_dev
->pctrl
, pin
);
901 * Only restore the pin if it is actually in use by the kernel (or
904 if (pd
->mux_owner
|| pd
->gpio_owner
||
905 gpiochip_line_is_irq(&gpio_dev
->gc
, pin
))
911 static int amd_gpio_suspend(struct device
*dev
)
913 struct amd_gpio
*gpio_dev
= dev_get_drvdata(dev
);
914 struct pinctrl_desc
*desc
= gpio_dev
->pctrl
->desc
;
918 for (i
= 0; i
< desc
->npins
; i
++) {
919 int pin
= desc
->pins
[i
].number
;
921 if (!amd_gpio_should_save(gpio_dev
, pin
))
924 raw_spin_lock_irqsave(&gpio_dev
->lock
, flags
);
925 gpio_dev
->saved_regs
[i
] = readl(gpio_dev
->base
+ pin
* 4) & ~PIN_IRQ_PENDING
;
927 /* mask any interrupts not intended to be a wake source */
928 if (!(gpio_dev
->saved_regs
[i
] & WAKE_SOURCE
)) {
929 writel(gpio_dev
->saved_regs
[i
] & ~BIT(INTERRUPT_MASK_OFF
),
930 gpio_dev
->base
+ pin
* 4);
931 pm_pr_dbg("Disabling GPIO #%d interrupt for suspend.\n",
935 raw_spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
941 static int amd_gpio_resume(struct device
*dev
)
943 struct amd_gpio
*gpio_dev
= dev_get_drvdata(dev
);
944 struct pinctrl_desc
*desc
= gpio_dev
->pctrl
->desc
;
948 for (i
= 0; i
< desc
->npins
; i
++) {
949 int pin
= desc
->pins
[i
].number
;
951 if (!amd_gpio_should_save(gpio_dev
, pin
))
954 raw_spin_lock_irqsave(&gpio_dev
->lock
, flags
);
955 gpio_dev
->saved_regs
[i
] |= readl(gpio_dev
->base
+ pin
* 4) & PIN_IRQ_PENDING
;
956 writel(gpio_dev
->saved_regs
[i
], gpio_dev
->base
+ pin
* 4);
957 raw_spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
963 static const struct dev_pm_ops amd_gpio_pm_ops
= {
964 SET_LATE_SYSTEM_SLEEP_PM_OPS(amd_gpio_suspend
,
969 static int amd_get_functions_count(struct pinctrl_dev
*pctldev
)
971 return ARRAY_SIZE(pmx_functions
);
974 static const char *amd_get_fname(struct pinctrl_dev
*pctrldev
, unsigned int selector
)
976 return pmx_functions
[selector
].name
;
979 static int amd_get_groups(struct pinctrl_dev
*pctrldev
, unsigned int selector
,
980 const char * const **groups
,
981 unsigned int * const num_groups
)
983 struct amd_gpio
*gpio_dev
= pinctrl_dev_get_drvdata(pctrldev
);
985 if (!gpio_dev
->iomux_base
) {
986 dev_err(&gpio_dev
->pdev
->dev
, "iomux function %d group not supported\n", selector
);
990 *groups
= pmx_functions
[selector
].groups
;
991 *num_groups
= pmx_functions
[selector
].ngroups
;
995 static int amd_set_mux(struct pinctrl_dev
*pctrldev
, unsigned int function
, unsigned int group
)
997 struct amd_gpio
*gpio_dev
= pinctrl_dev_get_drvdata(pctrldev
);
998 struct device
*dev
= &gpio_dev
->pdev
->dev
;
1002 if (!gpio_dev
->iomux_base
)
1005 for (index
= 0; index
< NSELECTS
; index
++) {
1006 if (strcmp(gpio_dev
->groups
[group
].name
, pmx_functions
[function
].groups
[index
]))
1009 if (readb(gpio_dev
->iomux_base
+ pmx_functions
[function
].index
) ==
1011 dev_err(dev
, "IOMUX_GPIO 0x%x not present or supported\n",
1012 pmx_functions
[function
].index
);
1016 writeb(index
, gpio_dev
->iomux_base
+ pmx_functions
[function
].index
);
1018 if (index
!= (readb(gpio_dev
->iomux_base
+ pmx_functions
[function
].index
) &
1020 dev_err(dev
, "IOMUX_GPIO 0x%x not present or supported\n",
1021 pmx_functions
[function
].index
);
1025 for (ind
= 0; ind
< gpio_dev
->groups
[group
].npins
; ind
++) {
1026 if (strncmp(gpio_dev
->groups
[group
].name
, "IMX_F", strlen("IMX_F")))
1029 pd
= pin_desc_get(gpio_dev
->pctrl
, gpio_dev
->groups
[group
].pins
[ind
]);
1030 pd
->mux_owner
= gpio_dev
->groups
[group
].name
;
1038 static const struct pinmux_ops amd_pmxops
= {
1039 .get_functions_count
= amd_get_functions_count
,
1040 .get_function_name
= amd_get_fname
,
1041 .get_function_groups
= amd_get_groups
,
1042 .set_mux
= amd_set_mux
,
1045 static struct pinctrl_desc amd_pinctrl_desc
= {
1046 .pins
= kerncz_pins
,
1047 .npins
= ARRAY_SIZE(kerncz_pins
),
1048 .pctlops
= &amd_pinctrl_ops
,
1049 .pmxops
= &amd_pmxops
,
1050 .confops
= &amd_pinconf_ops
,
1051 .owner
= THIS_MODULE
,
1054 static void amd_get_iomux_res(struct amd_gpio
*gpio_dev
)
1056 struct pinctrl_desc
*desc
= &amd_pinctrl_desc
;
1057 struct device
*dev
= &gpio_dev
->pdev
->dev
;
1060 index
= device_property_match_string(dev
, "pinctrl-resource-names", "iomux");
1062 dev_dbg(dev
, "iomux not supported\n");
1066 gpio_dev
->iomux_base
= devm_platform_ioremap_resource(gpio_dev
->pdev
, index
);
1067 if (IS_ERR(gpio_dev
->iomux_base
)) {
1068 dev_dbg(dev
, "iomux not supported %d io resource\n", index
);
1075 desc
->pmxops
= NULL
;
1078 static int amd_gpio_probe(struct platform_device
*pdev
)
1081 struct resource
*res
;
1082 struct amd_gpio
*gpio_dev
;
1083 struct gpio_irq_chip
*girq
;
1085 gpio_dev
= devm_kzalloc(&pdev
->dev
,
1086 sizeof(struct amd_gpio
), GFP_KERNEL
);
1090 raw_spin_lock_init(&gpio_dev
->lock
);
1092 gpio_dev
->base
= devm_platform_get_and_ioremap_resource(pdev
, 0, &res
);
1093 if (IS_ERR(gpio_dev
->base
)) {
1094 dev_err(&pdev
->dev
, "Failed to get gpio io resource.\n");
1095 return PTR_ERR(gpio_dev
->base
);
1098 gpio_dev
->irq
= platform_get_irq(pdev
, 0);
1099 if (gpio_dev
->irq
< 0)
1100 return gpio_dev
->irq
;
1102 #ifdef CONFIG_PM_SLEEP
1103 gpio_dev
->saved_regs
= devm_kcalloc(&pdev
->dev
, amd_pinctrl_desc
.npins
,
1104 sizeof(*gpio_dev
->saved_regs
),
1106 if (!gpio_dev
->saved_regs
)
1110 gpio_dev
->pdev
= pdev
;
1111 gpio_dev
->gc
.get_direction
= amd_gpio_get_direction
;
1112 gpio_dev
->gc
.direction_input
= amd_gpio_direction_input
;
1113 gpio_dev
->gc
.direction_output
= amd_gpio_direction_output
;
1114 gpio_dev
->gc
.get
= amd_gpio_get_value
;
1115 gpio_dev
->gc
.set
= amd_gpio_set_value
;
1116 gpio_dev
->gc
.set_config
= amd_gpio_set_config
;
1117 gpio_dev
->gc
.dbg_show
= amd_gpio_dbg_show
;
1119 gpio_dev
->gc
.base
= -1;
1120 gpio_dev
->gc
.label
= pdev
->name
;
1121 gpio_dev
->gc
.owner
= THIS_MODULE
;
1122 gpio_dev
->gc
.parent
= &pdev
->dev
;
1123 gpio_dev
->gc
.ngpio
= resource_size(res
) / 4;
1125 gpio_dev
->hwbank_num
= gpio_dev
->gc
.ngpio
/ 64;
1126 gpio_dev
->groups
= kerncz_groups
;
1127 gpio_dev
->ngroups
= ARRAY_SIZE(kerncz_groups
);
1129 amd_pinctrl_desc
.name
= dev_name(&pdev
->dev
);
1130 amd_get_iomux_res(gpio_dev
);
1131 gpio_dev
->pctrl
= devm_pinctrl_register(&pdev
->dev
, &amd_pinctrl_desc
,
1133 if (IS_ERR(gpio_dev
->pctrl
)) {
1134 dev_err(&pdev
->dev
, "Couldn't register pinctrl driver\n");
1135 return PTR_ERR(gpio_dev
->pctrl
);
1138 /* Disable and mask interrupts */
1139 amd_gpio_irq_init(gpio_dev
);
1141 girq
= &gpio_dev
->gc
.irq
;
1142 gpio_irq_chip_set_chip(girq
, &amd_gpio_irqchip
);
1143 /* This will let us handle the parent IRQ in the driver */
1144 girq
->parent_handler
= NULL
;
1145 girq
->num_parents
= 0;
1146 girq
->parents
= NULL
;
1147 girq
->default_type
= IRQ_TYPE_NONE
;
1148 girq
->handler
= handle_simple_irq
;
1150 ret
= gpiochip_add_data(&gpio_dev
->gc
, gpio_dev
);
1154 ret
= gpiochip_add_pin_range(&gpio_dev
->gc
, dev_name(&pdev
->dev
),
1155 0, 0, gpio_dev
->gc
.ngpio
);
1157 dev_err(&pdev
->dev
, "Failed to add pin range\n");
1161 ret
= devm_request_irq(&pdev
->dev
, gpio_dev
->irq
, amd_gpio_irq_handler
,
1162 IRQF_SHARED
| IRQF_COND_ONESHOT
, KBUILD_MODNAME
, gpio_dev
);
1166 platform_set_drvdata(pdev
, gpio_dev
);
1167 acpi_register_wakeup_handler(gpio_dev
->irq
, amd_gpio_check_wake
, gpio_dev
);
1169 dev_dbg(&pdev
->dev
, "amd gpio driver loaded\n");
1173 gpiochip_remove(&gpio_dev
->gc
);
1178 static void amd_gpio_remove(struct platform_device
*pdev
)
1180 struct amd_gpio
*gpio_dev
;
1182 gpio_dev
= platform_get_drvdata(pdev
);
1184 gpiochip_remove(&gpio_dev
->gc
);
1185 acpi_unregister_wakeup_handler(amd_gpio_check_wake
, gpio_dev
);
1189 static const struct acpi_device_id amd_gpio_acpi_match
[] = {
1195 MODULE_DEVICE_TABLE(acpi
, amd_gpio_acpi_match
);
1198 static struct platform_driver amd_gpio_driver
= {
1201 .acpi_match_table
= ACPI_PTR(amd_gpio_acpi_match
),
1202 #ifdef CONFIG_PM_SLEEP
1203 .pm
= &amd_gpio_pm_ops
,
1206 .probe
= amd_gpio_probe
,
1207 .remove
= amd_gpio_remove
,
1210 module_platform_driver(amd_gpio_driver
);
1212 MODULE_AUTHOR("Ken Xue <Ken.Xue@amd.com>, Jeff Wu <Jeff.Wu@amd.com>");
1213 MODULE_DESCRIPTION("AMD GPIO pinctrl driver");