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>
9 * Contact Information: Nehal Shah <Nehal-bakulchandra.Shah@amd.com>
10 * Shyam Sundar S K <Shyam-sundar.S-k@amd.com>
13 #include <linux/err.h>
14 #include <linux/bug.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/spinlock.h>
18 #include <linux/compiler.h>
19 #include <linux/types.h>
20 #include <linux/errno.h>
21 #include <linux/log2.h>
23 #include <linux/gpio/driver.h>
24 #include <linux/slab.h>
25 #include <linux/platform_device.h>
26 #include <linux/mutex.h>
27 #include <linux/acpi.h>
28 #include <linux/seq_file.h>
29 #include <linux/interrupt.h>
30 #include <linux/list.h>
31 #include <linux/bitops.h>
32 #include <linux/pinctrl/pinconf.h>
33 #include <linux/pinctrl/pinconf-generic.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 gpio_chip *gc, unsigned offset,
126 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
128 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
129 pin_reg = readl(gpio_dev->base + offset * 4);
132 pin_reg |= DB_TYPE_REMOVE_GLITCH << DB_CNTRL_OFF;
133 pin_reg &= ~DB_TMR_OUT_MASK;
135 Debounce Debounce Timer Max
136 TmrLarge TmrOutUnit Unit Debounce
138 0 0 61 usec (2 RtcClk) 976 usec
139 0 1 244 usec (8 RtcClk) 3.9 msec
140 1 0 15.6 msec (512 RtcClk) 250 msec
141 1 1 62.5 msec (2048 RtcClk) 1 sec
146 pin_reg &= ~BIT(DB_TMR_OUT_UNIT_OFF);
147 pin_reg &= ~BIT(DB_TMR_LARGE_OFF);
148 } else if (debounce < 976) {
149 time = debounce / 61;
150 pin_reg |= time & DB_TMR_OUT_MASK;
151 pin_reg &= ~BIT(DB_TMR_OUT_UNIT_OFF);
152 pin_reg &= ~BIT(DB_TMR_LARGE_OFF);
153 } else if (debounce < 3900) {
154 time = debounce / 244;
155 pin_reg |= time & DB_TMR_OUT_MASK;
156 pin_reg |= BIT(DB_TMR_OUT_UNIT_OFF);
157 pin_reg &= ~BIT(DB_TMR_LARGE_OFF);
158 } else if (debounce < 250000) {
159 time = debounce / 15625;
160 pin_reg |= time & DB_TMR_OUT_MASK;
161 pin_reg &= ~BIT(DB_TMR_OUT_UNIT_OFF);
162 pin_reg |= BIT(DB_TMR_LARGE_OFF);
163 } else if (debounce < 1000000) {
164 time = debounce / 62500;
165 pin_reg |= time & DB_TMR_OUT_MASK;
166 pin_reg |= BIT(DB_TMR_OUT_UNIT_OFF);
167 pin_reg |= BIT(DB_TMR_LARGE_OFF);
169 pin_reg &= ~(DB_CNTRl_MASK << DB_CNTRL_OFF);
173 pin_reg &= ~BIT(DB_TMR_OUT_UNIT_OFF);
174 pin_reg &= ~BIT(DB_TMR_LARGE_OFF);
175 pin_reg &= ~DB_TMR_OUT_MASK;
176 pin_reg &= ~(DB_CNTRl_MASK << DB_CNTRL_OFF);
178 writel(pin_reg, gpio_dev->base + offset * 4);
179 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
184 static int amd_gpio_set_config(struct gpio_chip *gc, unsigned offset,
185 unsigned long config)
189 if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE)
192 debounce = pinconf_to_config_argument(config);
193 return amd_gpio_set_debounce(gc, offset, debounce);
196 #ifdef CONFIG_DEBUG_FS
197 static void amd_gpio_dbg_show(struct seq_file *s, struct gpio_chip *gc)
202 unsigned int bank, i, pin_num;
203 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
212 char *interrupt_enable;
213 char *interrupt_mask;
219 char *pull_up_enable;
220 char *pull_down_enable;
223 char debounce_value[40];
224 char *debounce_enable;
226 for (bank = 0; bank < gpio_dev->hwbank_num; bank++) {
227 seq_printf(s, "GPIO bank%d\t", bank);
232 pin_num = AMD_GPIO_PINS_BANK0;
236 pin_num = AMD_GPIO_PINS_BANK1 + i;
240 pin_num = AMD_GPIO_PINS_BANK2 + i;
244 pin_num = AMD_GPIO_PINS_BANK3 + i;
247 /* Illegal bank number, ignore */
250 for (; i < pin_num; i++) {
251 seq_printf(s, "pin%d\t", i);
252 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
253 pin_reg = readl(gpio_dev->base + i * 4);
254 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
256 if (pin_reg & BIT(INTERRUPT_ENABLE_OFF)) {
257 u8 level = (pin_reg >> ACTIVE_LEVEL_OFF) &
259 interrupt_enable = "interrupt is enabled|";
261 if (level == ACTIVE_LEVEL_HIGH)
262 active_level = "Active high|";
263 else if (level == ACTIVE_LEVEL_LOW)
264 active_level = "Active low|";
265 else if (!(pin_reg & BIT(LEVEL_TRIG_OFF)) &&
266 level == ACTIVE_LEVEL_BOTH)
267 active_level = "Active on both|";
269 active_level = "Unknown Active level|";
271 if (pin_reg & BIT(LEVEL_TRIG_OFF))
272 level_trig = "Level trigger|";
274 level_trig = "Edge trigger|";
278 "interrupt is disabled|";
283 if (pin_reg & BIT(INTERRUPT_MASK_OFF))
285 "interrupt is unmasked|";
288 "interrupt is masked|";
290 if (pin_reg & BIT(WAKE_CNTRL_OFF_S0I3))
291 wake_cntrl0 = "enable wakeup in S0i3 state|";
293 wake_cntrl0 = "disable wakeup in S0i3 state|";
295 if (pin_reg & BIT(WAKE_CNTRL_OFF_S3))
296 wake_cntrl1 = "enable wakeup in S3 state|";
298 wake_cntrl1 = "disable wakeup in S3 state|";
300 if (pin_reg & BIT(WAKE_CNTRL_OFF_S4))
301 wake_cntrl2 = "enable wakeup in S4/S5 state|";
303 wake_cntrl2 = "disable wakeup in S4/S5 state|";
305 if (pin_reg & BIT(PULL_UP_ENABLE_OFF)) {
306 pull_up_enable = "pull-up is enabled|";
307 if (pin_reg & BIT(PULL_UP_SEL_OFF))
308 pull_up_sel = "8k pull-up|";
310 pull_up_sel = "4k pull-up|";
312 pull_up_enable = "pull-up is disabled|";
316 if (pin_reg & BIT(PULL_DOWN_ENABLE_OFF))
317 pull_down_enable = "pull-down is enabled|";
319 pull_down_enable = "Pull-down is disabled|";
321 if (pin_reg & BIT(OUTPUT_ENABLE_OFF)) {
323 output_enable = "output is enabled|";
324 if (pin_reg & BIT(OUTPUT_VALUE_OFF))
325 output_value = "output is high|";
327 output_value = "output is low|";
329 output_enable = "output is disabled|";
332 if (pin_reg & BIT(PIN_STS_OFF))
333 pin_sts = "input is high|";
335 pin_sts = "input is low|";
338 db_cntrl = (DB_CNTRl_MASK << DB_CNTRL_OFF) & pin_reg;
340 tmr_out_unit = pin_reg & BIT(DB_TMR_OUT_UNIT_OFF);
341 tmr_large = pin_reg & BIT(DB_TMR_LARGE_OFF);
342 time = pin_reg & DB_TMR_OUT_MASK;
354 if ((DB_TYPE_REMOVE_GLITCH << DB_CNTRL_OFF) == db_cntrl)
355 debounce_enable = "debouncing filter (high and low) enabled|";
356 else if ((DB_TYPE_PRESERVE_LOW_GLITCH << DB_CNTRL_OFF) == db_cntrl)
357 debounce_enable = "debouncing filter (low) enabled|";
359 debounce_enable = "debouncing filter (high) enabled|";
361 snprintf(debounce_value, sizeof(debounce_value),
362 "debouncing timeout is %u (us)|", time * unit);
364 debounce_enable = "debouncing filter disabled|";
365 snprintf(debounce_value, sizeof(debounce_value), " ");
368 seq_printf(s, "%s %s %s %s %s %s\n"
369 " %s %s %s %s %s %s %s %s %s 0x%x\n",
370 level_trig, active_level, interrupt_enable,
371 interrupt_mask, wake_cntrl0, wake_cntrl1,
372 wake_cntrl2, pin_sts, pull_up_sel,
373 pull_up_enable, pull_down_enable,
374 output_value, output_enable,
375 debounce_enable, debounce_value, pin_reg);
380 #define amd_gpio_dbg_show NULL
383 static void amd_gpio_irq_enable(struct irq_data *d)
387 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
388 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
390 gpiochip_enable_irq(gc, d->hwirq);
392 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
393 pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
394 pin_reg |= BIT(INTERRUPT_ENABLE_OFF);
395 pin_reg |= BIT(INTERRUPT_MASK_OFF);
396 writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
397 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
400 static void amd_gpio_irq_disable(struct irq_data *d)
404 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
405 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
407 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
408 pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
409 pin_reg &= ~BIT(INTERRUPT_ENABLE_OFF);
410 pin_reg &= ~BIT(INTERRUPT_MASK_OFF);
411 writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
412 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
414 gpiochip_disable_irq(gc, d->hwirq);
417 static void amd_gpio_irq_mask(struct irq_data *d)
421 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
422 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
424 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
425 pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
426 pin_reg &= ~BIT(INTERRUPT_MASK_OFF);
427 writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
428 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
431 static void amd_gpio_irq_unmask(struct irq_data *d)
435 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
436 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
438 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
439 pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
440 pin_reg |= BIT(INTERRUPT_MASK_OFF);
441 writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
442 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
445 static int amd_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
449 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
450 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
451 u32 wake_mask = BIT(WAKE_CNTRL_OFF_S0I3) | BIT(WAKE_CNTRL_OFF_S3);
454 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
455 pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
458 pin_reg |= wake_mask;
460 pin_reg &= ~wake_mask;
462 writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
463 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
466 err = enable_irq_wake(gpio_dev->irq);
468 err = disable_irq_wake(gpio_dev->irq);
471 dev_err(&gpio_dev->pdev->dev, "failed to %s wake-up interrupt\n",
472 on ? "enable" : "disable");
477 static void amd_gpio_irq_eoi(struct irq_data *d)
481 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
482 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
484 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
485 reg = readl(gpio_dev->base + WAKE_INT_MASTER_REG);
487 writel(reg, gpio_dev->base + WAKE_INT_MASTER_REG);
488 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
491 static int amd_gpio_irq_set_type(struct irq_data *d, unsigned int type)
494 u32 pin_reg, pin_reg_irq_en, mask;
496 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
497 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
499 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
500 pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
502 switch (type & IRQ_TYPE_SENSE_MASK) {
503 case IRQ_TYPE_EDGE_RISING:
504 pin_reg &= ~BIT(LEVEL_TRIG_OFF);
505 pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
506 pin_reg |= ACTIVE_HIGH << ACTIVE_LEVEL_OFF;
507 irq_set_handler_locked(d, handle_edge_irq);
510 case IRQ_TYPE_EDGE_FALLING:
511 pin_reg &= ~BIT(LEVEL_TRIG_OFF);
512 pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
513 pin_reg |= ACTIVE_LOW << ACTIVE_LEVEL_OFF;
514 irq_set_handler_locked(d, handle_edge_irq);
517 case IRQ_TYPE_EDGE_BOTH:
518 pin_reg &= ~BIT(LEVEL_TRIG_OFF);
519 pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
520 pin_reg |= BOTH_EADGE << ACTIVE_LEVEL_OFF;
521 irq_set_handler_locked(d, handle_edge_irq);
524 case IRQ_TYPE_LEVEL_HIGH:
525 pin_reg |= LEVEL_TRIGGER << LEVEL_TRIG_OFF;
526 pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
527 pin_reg |= ACTIVE_HIGH << ACTIVE_LEVEL_OFF;
528 irq_set_handler_locked(d, handle_level_irq);
531 case IRQ_TYPE_LEVEL_LOW:
532 pin_reg |= LEVEL_TRIGGER << LEVEL_TRIG_OFF;
533 pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
534 pin_reg |= ACTIVE_LOW << ACTIVE_LEVEL_OFF;
535 irq_set_handler_locked(d, handle_level_irq);
542 dev_err(&gpio_dev->pdev->dev, "Invalid type value\n");
546 pin_reg |= CLR_INTR_STAT << INTERRUPT_STS_OFF;
548 * If WAKE_INT_MASTER_REG.MaskStsEn is set, a software write to the
549 * debounce registers of any GPIO will block wake/interrupt status
550 * generation for *all* GPIOs for a length of time that depends on
551 * WAKE_INT_MASTER_REG.MaskStsLength[11:0]. During this period the
552 * INTERRUPT_ENABLE bit will read as 0.
554 * We temporarily enable irq for the GPIO whose configuration is
555 * changing, and then wait for it to read back as 1 to know when
556 * debounce has settled and then disable the irq again.
557 * We do this polling with the spinlock held to ensure other GPIO
558 * access routines do not read an incorrect value for the irq enable
559 * bit of other GPIOs. We keep the GPIO masked while polling to avoid
560 * spurious irqs, and disable the irq again after polling.
562 mask = BIT(INTERRUPT_ENABLE_OFF);
563 pin_reg_irq_en = pin_reg;
564 pin_reg_irq_en |= mask;
565 pin_reg_irq_en &= ~BIT(INTERRUPT_MASK_OFF);
566 writel(pin_reg_irq_en, gpio_dev->base + (d->hwirq)*4);
567 while ((readl(gpio_dev->base + (d->hwirq)*4) & mask) != mask)
569 writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
570 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
575 static void amd_irq_ack(struct irq_data *d)
578 * based on HW design,there is no need to ack HW
579 * before handle current irq. But this routine is
580 * necessary for handle_edge_irq
584 static const struct irq_chip amd_gpio_irqchip = {
586 .irq_ack = amd_irq_ack,
587 .irq_enable = amd_gpio_irq_enable,
588 .irq_disable = amd_gpio_irq_disable,
589 .irq_mask = amd_gpio_irq_mask,
590 .irq_unmask = amd_gpio_irq_unmask,
591 .irq_set_wake = amd_gpio_irq_set_wake,
592 .irq_eoi = amd_gpio_irq_eoi,
593 .irq_set_type = amd_gpio_irq_set_type,
595 * We need to set IRQCHIP_ENABLE_WAKEUP_ON_SUSPEND so that a wake event
596 * also generates an IRQ. We need the IRQ so the irq_handler can clear
597 * the wake event. Otherwise the wake event will never clear and
598 * prevent the system from suspending.
600 .flags = IRQCHIP_ENABLE_WAKEUP_ON_SUSPEND | IRQCHIP_IMMUTABLE,
601 GPIOCHIP_IRQ_RESOURCE_HELPERS,
604 #define PIN_IRQ_PENDING (BIT(INTERRUPT_STS_OFF) | BIT(WAKE_STS_OFF))
606 static bool do_amd_gpio_irq_handler(int irq, void *dev_id)
608 struct amd_gpio *gpio_dev = dev_id;
609 struct gpio_chip *gc = &gpio_dev->gc;
610 unsigned int i, irqnr;
617 /* Read the wake status */
618 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
619 status = readl(gpio_dev->base + WAKE_INT_STATUS_REG1);
621 status |= readl(gpio_dev->base + WAKE_INT_STATUS_REG0);
622 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
624 /* Bit 0-45 contain the relevant status bits */
625 status &= (1ULL << 46) - 1;
626 regs = gpio_dev->base;
627 for (mask = 1, irqnr = 0; status; mask <<= 1, regs += 4, irqnr += 4) {
628 if (!(status & mask))
632 /* Each status bit covers four pins */
633 for (i = 0; i < 4; i++) {
634 regval = readl(regs + i);
635 /* caused wake on resume context for shared IRQ */
636 if (irq < 0 && (regval & BIT(WAKE_STS_OFF))) {
637 dev_dbg(&gpio_dev->pdev->dev,
638 "Waking due to GPIO %d: 0x%x",
643 if (!(regval & PIN_IRQ_PENDING) ||
644 !(regval & BIT(INTERRUPT_MASK_OFF)))
646 generic_handle_domain_irq(gc->irq.domain, irqnr + i);
649 * We must read the pin register again, in case the
650 * value was changed while executing
651 * generic_handle_domain_irq() above.
652 * If we didn't find a mapping for the interrupt,
653 * disable it in order to avoid a system hang caused
654 * by an interrupt storm.
656 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
657 regval = readl(regs + i);
659 regval &= ~BIT(INTERRUPT_ENABLE_OFF);
660 dev_dbg(&gpio_dev->pdev->dev,
661 "Disabling spurious GPIO IRQ %d\n",
664 writel(regval, regs + i);
665 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
669 /* did not cause wake on resume context for shared IRQ */
673 /* Signal EOI to the GPIO unit */
674 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
675 regval = readl(gpio_dev->base + WAKE_INT_MASTER_REG);
677 writel(regval, gpio_dev->base + WAKE_INT_MASTER_REG);
678 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
683 static irqreturn_t amd_gpio_irq_handler(int irq, void *dev_id)
685 return IRQ_RETVAL(do_amd_gpio_irq_handler(irq, dev_id));
688 static bool __maybe_unused amd_gpio_check_wake(void *dev_id)
690 return do_amd_gpio_irq_handler(-1, dev_id);
693 static int amd_get_groups_count(struct pinctrl_dev *pctldev)
695 struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
697 return gpio_dev->ngroups;
700 static const char *amd_get_group_name(struct pinctrl_dev *pctldev,
703 struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
705 return gpio_dev->groups[group].name;
708 static int amd_get_group_pins(struct pinctrl_dev *pctldev,
710 const unsigned **pins,
713 struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
715 *pins = gpio_dev->groups[group].pins;
716 *num_pins = gpio_dev->groups[group].npins;
720 static const struct pinctrl_ops amd_pinctrl_ops = {
721 .get_groups_count = amd_get_groups_count,
722 .get_group_name = amd_get_group_name,
723 .get_group_pins = amd_get_group_pins,
725 .dt_node_to_map = pinconf_generic_dt_node_to_map_group,
726 .dt_free_map = pinctrl_utils_free_map,
730 static int amd_pinconf_get(struct pinctrl_dev *pctldev,
732 unsigned long *config)
737 struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
738 enum pin_config_param param = pinconf_to_config_param(*config);
740 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
741 pin_reg = readl(gpio_dev->base + pin*4);
742 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
744 case PIN_CONFIG_INPUT_DEBOUNCE:
745 arg = pin_reg & DB_TMR_OUT_MASK;
748 case PIN_CONFIG_BIAS_PULL_DOWN:
749 arg = (pin_reg >> PULL_DOWN_ENABLE_OFF) & BIT(0);
752 case PIN_CONFIG_BIAS_PULL_UP:
753 arg = (pin_reg >> PULL_UP_SEL_OFF) & (BIT(0) | BIT(1));
756 case PIN_CONFIG_DRIVE_STRENGTH:
757 arg = (pin_reg >> DRV_STRENGTH_SEL_OFF) & DRV_STRENGTH_SEL_MASK;
761 dev_err(&gpio_dev->pdev->dev, "Invalid config param %04x\n",
766 *config = pinconf_to_config_packed(param, arg);
771 static int amd_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
772 unsigned long *configs, unsigned num_configs)
779 enum pin_config_param param;
780 struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
782 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
783 for (i = 0; i < num_configs; i++) {
784 param = pinconf_to_config_param(configs[i]);
785 arg = pinconf_to_config_argument(configs[i]);
786 pin_reg = readl(gpio_dev->base + pin*4);
789 case PIN_CONFIG_INPUT_DEBOUNCE:
790 pin_reg &= ~DB_TMR_OUT_MASK;
791 pin_reg |= arg & DB_TMR_OUT_MASK;
794 case PIN_CONFIG_BIAS_PULL_DOWN:
795 pin_reg &= ~BIT(PULL_DOWN_ENABLE_OFF);
796 pin_reg |= (arg & BIT(0)) << PULL_DOWN_ENABLE_OFF;
799 case PIN_CONFIG_BIAS_PULL_UP:
800 pin_reg &= ~BIT(PULL_UP_SEL_OFF);
801 pin_reg |= (arg & BIT(0)) << PULL_UP_SEL_OFF;
802 pin_reg &= ~BIT(PULL_UP_ENABLE_OFF);
803 pin_reg |= ((arg>>1) & BIT(0)) << PULL_UP_ENABLE_OFF;
806 case PIN_CONFIG_DRIVE_STRENGTH:
807 pin_reg &= ~(DRV_STRENGTH_SEL_MASK
808 << DRV_STRENGTH_SEL_OFF);
809 pin_reg |= (arg & DRV_STRENGTH_SEL_MASK)
810 << DRV_STRENGTH_SEL_OFF;
814 dev_err(&gpio_dev->pdev->dev,
815 "Invalid config param %04x\n", param);
819 writel(pin_reg, gpio_dev->base + pin*4);
821 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
826 static int amd_pinconf_group_get(struct pinctrl_dev *pctldev,
828 unsigned long *config)
830 const unsigned *pins;
834 ret = amd_get_group_pins(pctldev, group, &pins, &npins);
838 if (amd_pinconf_get(pctldev, pins[0], config))
844 static int amd_pinconf_group_set(struct pinctrl_dev *pctldev,
845 unsigned group, unsigned long *configs,
846 unsigned num_configs)
848 const unsigned *pins;
852 ret = amd_get_group_pins(pctldev, group, &pins, &npins);
855 for (i = 0; i < npins; i++) {
856 if (amd_pinconf_set(pctldev, pins[i], configs, num_configs))
862 static const struct pinconf_ops amd_pinconf_ops = {
863 .pin_config_get = amd_pinconf_get,
864 .pin_config_set = amd_pinconf_set,
865 .pin_config_group_get = amd_pinconf_group_get,
866 .pin_config_group_set = amd_pinconf_group_set,
869 static void amd_gpio_irq_init(struct amd_gpio *gpio_dev)
871 struct pinctrl_desc *desc = gpio_dev->pctrl->desc;
876 mask = BIT(WAKE_CNTRL_OFF_S0I3) | BIT(WAKE_CNTRL_OFF_S3) |
877 BIT(INTERRUPT_MASK_OFF) | BIT(INTERRUPT_ENABLE_OFF) |
878 BIT(WAKE_CNTRL_OFF_S4);
880 for (i = 0; i < desc->npins; i++) {
881 int pin = desc->pins[i].number;
882 const struct pin_desc *pd = pin_desc_get(gpio_dev->pctrl, pin);
887 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
889 pin_reg = readl(gpio_dev->base + i * 4);
891 writel(pin_reg, gpio_dev->base + i * 4);
893 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
897 #ifdef CONFIG_PM_SLEEP
898 static bool amd_gpio_should_save(struct amd_gpio *gpio_dev, unsigned int pin)
900 const struct pin_desc *pd = pin_desc_get(gpio_dev->pctrl, pin);
906 * Only restore the pin if it is actually in use by the kernel (or
909 if (pd->mux_owner || pd->gpio_owner ||
910 gpiochip_line_is_irq(&gpio_dev->gc, pin))
916 static int amd_gpio_suspend(struct device *dev)
918 struct amd_gpio *gpio_dev = dev_get_drvdata(dev);
919 struct pinctrl_desc *desc = gpio_dev->pctrl->desc;
922 for (i = 0; i < desc->npins; i++) {
923 int pin = desc->pins[i].number;
925 if (!amd_gpio_should_save(gpio_dev, pin))
928 gpio_dev->saved_regs[i] = readl(gpio_dev->base + pin*4);
934 static int amd_gpio_resume(struct device *dev)
936 struct amd_gpio *gpio_dev = dev_get_drvdata(dev);
937 struct pinctrl_desc *desc = gpio_dev->pctrl->desc;
940 for (i = 0; i < desc->npins; i++) {
941 int pin = desc->pins[i].number;
943 if (!amd_gpio_should_save(gpio_dev, pin))
946 writel(gpio_dev->saved_regs[i], gpio_dev->base + pin*4);
952 static const struct dev_pm_ops amd_gpio_pm_ops = {
953 SET_LATE_SYSTEM_SLEEP_PM_OPS(amd_gpio_suspend,
958 static struct pinctrl_desc amd_pinctrl_desc = {
960 .npins = ARRAY_SIZE(kerncz_pins),
961 .pctlops = &amd_pinctrl_ops,
962 .confops = &amd_pinconf_ops,
963 .owner = THIS_MODULE,
966 static int amd_gpio_probe(struct platform_device *pdev)
969 struct resource *res;
970 struct amd_gpio *gpio_dev;
971 struct gpio_irq_chip *girq;
973 gpio_dev = devm_kzalloc(&pdev->dev,
974 sizeof(struct amd_gpio), GFP_KERNEL);
978 raw_spin_lock_init(&gpio_dev->lock);
980 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
982 dev_err(&pdev->dev, "Failed to get gpio io resource.\n");
986 gpio_dev->base = devm_ioremap(&pdev->dev, res->start,
991 gpio_dev->irq = platform_get_irq(pdev, 0);
992 if (gpio_dev->irq < 0)
993 return gpio_dev->irq;
995 #ifdef CONFIG_PM_SLEEP
996 gpio_dev->saved_regs = devm_kcalloc(&pdev->dev, amd_pinctrl_desc.npins,
997 sizeof(*gpio_dev->saved_regs),
999 if (!gpio_dev->saved_regs)
1003 gpio_dev->pdev = pdev;
1004 gpio_dev->gc.get_direction = amd_gpio_get_direction;
1005 gpio_dev->gc.direction_input = amd_gpio_direction_input;
1006 gpio_dev->gc.direction_output = amd_gpio_direction_output;
1007 gpio_dev->gc.get = amd_gpio_get_value;
1008 gpio_dev->gc.set = amd_gpio_set_value;
1009 gpio_dev->gc.set_config = amd_gpio_set_config;
1010 gpio_dev->gc.dbg_show = amd_gpio_dbg_show;
1012 gpio_dev->gc.base = -1;
1013 gpio_dev->gc.label = pdev->name;
1014 gpio_dev->gc.owner = THIS_MODULE;
1015 gpio_dev->gc.parent = &pdev->dev;
1016 gpio_dev->gc.ngpio = resource_size(res) / 4;
1018 gpio_dev->hwbank_num = gpio_dev->gc.ngpio / 64;
1019 gpio_dev->groups = kerncz_groups;
1020 gpio_dev->ngroups = ARRAY_SIZE(kerncz_groups);
1022 amd_pinctrl_desc.name = dev_name(&pdev->dev);
1023 gpio_dev->pctrl = devm_pinctrl_register(&pdev->dev, &amd_pinctrl_desc,
1025 if (IS_ERR(gpio_dev->pctrl)) {
1026 dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
1027 return PTR_ERR(gpio_dev->pctrl);
1030 /* Disable and mask interrupts */
1031 amd_gpio_irq_init(gpio_dev);
1033 girq = &gpio_dev->gc.irq;
1034 gpio_irq_chip_set_chip(girq, &amd_gpio_irqchip);
1035 /* This will let us handle the parent IRQ in the driver */
1036 girq->parent_handler = NULL;
1037 girq->num_parents = 0;
1038 girq->parents = NULL;
1039 girq->default_type = IRQ_TYPE_NONE;
1040 girq->handler = handle_simple_irq;
1042 ret = gpiochip_add_data(&gpio_dev->gc, gpio_dev);
1046 ret = gpiochip_add_pin_range(&gpio_dev->gc, dev_name(&pdev->dev),
1047 0, 0, gpio_dev->gc.ngpio);
1049 dev_err(&pdev->dev, "Failed to add pin range\n");
1053 ret = devm_request_irq(&pdev->dev, gpio_dev->irq, amd_gpio_irq_handler,
1054 IRQF_SHARED, KBUILD_MODNAME, gpio_dev);
1058 platform_set_drvdata(pdev, gpio_dev);
1059 acpi_register_wakeup_handler(gpio_dev->irq, amd_gpio_check_wake, gpio_dev);
1061 dev_dbg(&pdev->dev, "amd gpio driver loaded\n");
1065 gpiochip_remove(&gpio_dev->gc);
1070 static int amd_gpio_remove(struct platform_device *pdev)
1072 struct amd_gpio *gpio_dev;
1074 gpio_dev = platform_get_drvdata(pdev);
1076 gpiochip_remove(&gpio_dev->gc);
1077 acpi_unregister_wakeup_handler(amd_gpio_check_wake, gpio_dev);
1083 static const struct acpi_device_id amd_gpio_acpi_match[] = {
1089 MODULE_DEVICE_TABLE(acpi, amd_gpio_acpi_match);
1092 static struct platform_driver amd_gpio_driver = {
1095 .acpi_match_table = ACPI_PTR(amd_gpio_acpi_match),
1096 #ifdef CONFIG_PM_SLEEP
1097 .pm = &amd_gpio_pm_ops,
1100 .probe = amd_gpio_probe,
1101 .remove = amd_gpio_remove,
1104 module_platform_driver(amd_gpio_driver);
1106 MODULE_LICENSE("GPL v2");
1107 MODULE_AUTHOR("Ken Xue <Ken.Xue@amd.com>, Jeff Wu <Jeff.Wu@amd.com>");
1108 MODULE_DESCRIPTION("AMD GPIO pinctrl driver");