4 * Copyright (c) 2014,2015 AMD Corporation.
5 * Authors: Ken Xue <Ken.Xue@amd.com>
6 * Wu, Jeff <Jeff.Wu@amd.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
12 * Contact Information: Nehal Shah <Nehal-bakulchandra.Shah@amd.com>
13 * Shyam Sundar S K <Shyam-sundar.S-k@amd.com>
17 #include <linux/err.h>
18 #include <linux/bug.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/spinlock.h>
22 #include <linux/compiler.h>
23 #include <linux/types.h>
24 #include <linux/errno.h>
25 #include <linux/log2.h>
27 #include <linux/gpio.h>
28 #include <linux/slab.h>
29 #include <linux/platform_device.h>
30 #include <linux/mutex.h>
31 #include <linux/acpi.h>
32 #include <linux/seq_file.h>
33 #include <linux/interrupt.h>
34 #include <linux/list.h>
35 #include <linux/bitops.h>
36 #include <linux/pinctrl/pinconf.h>
37 #include <linux/pinctrl/pinconf-generic.h>
39 #include "pinctrl-utils.h"
40 #include "pinctrl-amd.h"
42 static int amd_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
46 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
48 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
49 pin_reg = readl(gpio_dev->base + offset * 4);
50 pin_reg &= ~BIT(OUTPUT_ENABLE_OFF);
51 writel(pin_reg, gpio_dev->base + offset * 4);
52 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
57 static int amd_gpio_direction_output(struct gpio_chip *gc, unsigned offset,
62 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
64 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
65 pin_reg = readl(gpio_dev->base + offset * 4);
66 pin_reg |= BIT(OUTPUT_ENABLE_OFF);
68 pin_reg |= BIT(OUTPUT_VALUE_OFF);
70 pin_reg &= ~BIT(OUTPUT_VALUE_OFF);
71 writel(pin_reg, gpio_dev->base + offset * 4);
72 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
77 static int amd_gpio_get_value(struct gpio_chip *gc, unsigned offset)
81 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
83 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
84 pin_reg = readl(gpio_dev->base + offset * 4);
85 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
87 return !!(pin_reg & BIT(PIN_STS_OFF));
90 static void amd_gpio_set_value(struct gpio_chip *gc, unsigned offset, int value)
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);
99 pin_reg |= BIT(OUTPUT_VALUE_OFF);
101 pin_reg &= ~BIT(OUTPUT_VALUE_OFF);
102 writel(pin_reg, gpio_dev->base + offset * 4);
103 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
106 static int amd_gpio_set_debounce(struct gpio_chip *gc, unsigned offset,
113 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
115 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
116 pin_reg = readl(gpio_dev->base + offset * 4);
119 pin_reg |= DB_TYPE_REMOVE_GLITCH << DB_CNTRL_OFF;
120 pin_reg &= ~DB_TMR_OUT_MASK;
122 Debounce Debounce Timer Max
123 TmrLarge TmrOutUnit Unit Debounce
125 0 0 61 usec (2 RtcClk) 976 usec
126 0 1 244 usec (8 RtcClk) 3.9 msec
127 1 0 15.6 msec (512 RtcClk) 250 msec
128 1 1 62.5 msec (2048 RtcClk) 1 sec
133 pin_reg &= ~BIT(DB_TMR_OUT_UNIT_OFF);
134 pin_reg &= ~BIT(DB_TMR_LARGE_OFF);
135 } else if (debounce < 976) {
136 time = debounce / 61;
137 pin_reg |= time & DB_TMR_OUT_MASK;
138 pin_reg &= ~BIT(DB_TMR_OUT_UNIT_OFF);
139 pin_reg &= ~BIT(DB_TMR_LARGE_OFF);
140 } else if (debounce < 3900) {
141 time = debounce / 244;
142 pin_reg |= time & DB_TMR_OUT_MASK;
143 pin_reg |= BIT(DB_TMR_OUT_UNIT_OFF);
144 pin_reg &= ~BIT(DB_TMR_LARGE_OFF);
145 } else if (debounce < 250000) {
146 time = debounce / 15600;
147 pin_reg |= time & DB_TMR_OUT_MASK;
148 pin_reg &= ~BIT(DB_TMR_OUT_UNIT_OFF);
149 pin_reg |= BIT(DB_TMR_LARGE_OFF);
150 } else if (debounce < 1000000) {
151 time = debounce / 62500;
152 pin_reg |= time & DB_TMR_OUT_MASK;
153 pin_reg |= BIT(DB_TMR_OUT_UNIT_OFF);
154 pin_reg |= BIT(DB_TMR_LARGE_OFF);
156 pin_reg &= ~DB_CNTRl_MASK;
160 pin_reg &= ~BIT(DB_TMR_OUT_UNIT_OFF);
161 pin_reg &= ~BIT(DB_TMR_LARGE_OFF);
162 pin_reg &= ~DB_TMR_OUT_MASK;
163 pin_reg &= ~DB_CNTRl_MASK;
165 writel(pin_reg, gpio_dev->base + offset * 4);
166 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
171 static int amd_gpio_set_config(struct gpio_chip *gc, unsigned offset,
172 unsigned long config)
176 if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE)
179 debounce = pinconf_to_config_argument(config);
180 return amd_gpio_set_debounce(gc, offset, debounce);
183 #ifdef CONFIG_DEBUG_FS
184 static void amd_gpio_dbg_show(struct seq_file *s, struct gpio_chip *gc)
188 unsigned int bank, i, pin_num;
189 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
193 char *interrupt_enable;
194 char *interrupt_mask;
200 char *pull_up_enable;
201 char *pull_down_enable;
205 for (bank = 0; bank < gpio_dev->hwbank_num; bank++) {
206 seq_printf(s, "GPIO bank%d\t", bank);
211 pin_num = AMD_GPIO_PINS_BANK0;
215 pin_num = AMD_GPIO_PINS_BANK1 + i;
219 pin_num = AMD_GPIO_PINS_BANK2 + i;
223 pin_num = AMD_GPIO_PINS_BANK3 + i;
226 /* Illegal bank number, ignore */
229 for (; i < pin_num; i++) {
230 seq_printf(s, "pin%d\t", i);
231 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
232 pin_reg = readl(gpio_dev->base + i * 4);
233 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
235 if (pin_reg & BIT(INTERRUPT_ENABLE_OFF)) {
236 interrupt_enable = "interrupt is enabled|";
238 if (!(pin_reg & BIT(ACTIVE_LEVEL_OFF)) &&
239 !(pin_reg & BIT(ACTIVE_LEVEL_OFF + 1)))
240 active_level = "Active low|";
241 else if (pin_reg & BIT(ACTIVE_LEVEL_OFF) &&
242 !(pin_reg & BIT(ACTIVE_LEVEL_OFF + 1)))
243 active_level = "Active high|";
244 else if (!(pin_reg & BIT(ACTIVE_LEVEL_OFF)) &&
245 pin_reg & BIT(ACTIVE_LEVEL_OFF + 1))
246 active_level = "Active on both|";
248 active_level = "Unknown Active level|";
250 if (pin_reg & BIT(LEVEL_TRIG_OFF))
251 level_trig = "Level trigger|";
253 level_trig = "Edge trigger|";
257 "interrupt is disabled|";
262 if (pin_reg & BIT(INTERRUPT_MASK_OFF))
264 "interrupt is unmasked|";
267 "interrupt is masked|";
269 if (pin_reg & BIT(WAKE_CNTRL_OFF_S0I3))
270 wake_cntrl0 = "enable wakeup in S0i3 state|";
272 wake_cntrl0 = "disable wakeup in S0i3 state|";
274 if (pin_reg & BIT(WAKE_CNTRL_OFF_S3))
275 wake_cntrl1 = "enable wakeup in S3 state|";
277 wake_cntrl1 = "disable wakeup in S3 state|";
279 if (pin_reg & BIT(WAKE_CNTRL_OFF_S4))
280 wake_cntrl2 = "enable wakeup in S4/S5 state|";
282 wake_cntrl2 = "disable wakeup in S4/S5 state|";
284 if (pin_reg & BIT(PULL_UP_ENABLE_OFF)) {
285 pull_up_enable = "pull-up is enabled|";
286 if (pin_reg & BIT(PULL_UP_SEL_OFF))
287 pull_up_sel = "8k pull-up|";
289 pull_up_sel = "4k pull-up|";
291 pull_up_enable = "pull-up is disabled|";
295 if (pin_reg & BIT(PULL_DOWN_ENABLE_OFF))
296 pull_down_enable = "pull-down is enabled|";
298 pull_down_enable = "Pull-down is disabled|";
300 if (pin_reg & BIT(OUTPUT_ENABLE_OFF)) {
302 output_enable = "output is enabled|";
303 if (pin_reg & BIT(OUTPUT_VALUE_OFF))
304 output_value = "output is high|";
306 output_value = "output is low|";
308 output_enable = "output is disabled|";
311 if (pin_reg & BIT(PIN_STS_OFF))
312 pin_sts = "input is high|";
314 pin_sts = "input is low|";
317 seq_printf(s, "%s %s %s %s %s %s\n"
318 " %s %s %s %s %s %s %s 0x%x\n",
319 level_trig, active_level, interrupt_enable,
320 interrupt_mask, wake_cntrl0, wake_cntrl1,
321 wake_cntrl2, pin_sts, pull_up_sel,
322 pull_up_enable, pull_down_enable,
323 output_value, output_enable, pin_reg);
328 #define amd_gpio_dbg_show NULL
331 static void amd_gpio_irq_enable(struct irq_data *d)
335 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
336 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
338 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
339 pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
340 pin_reg |= BIT(INTERRUPT_ENABLE_OFF);
341 pin_reg |= BIT(INTERRUPT_MASK_OFF);
342 writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
343 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
346 static void amd_gpio_irq_disable(struct irq_data *d)
350 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
351 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
353 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
354 pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
355 pin_reg &= ~BIT(INTERRUPT_ENABLE_OFF);
356 pin_reg &= ~BIT(INTERRUPT_MASK_OFF);
357 writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
358 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
361 static void amd_gpio_irq_mask(struct irq_data *d)
365 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
366 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
368 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
369 pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
370 pin_reg &= ~BIT(INTERRUPT_MASK_OFF);
371 writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
372 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
375 static void amd_gpio_irq_unmask(struct irq_data *d)
379 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
380 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
382 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
383 pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
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_eoi(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 reg = readl(gpio_dev->base + WAKE_INT_MASTER_REG);
399 writel(reg, gpio_dev->base + WAKE_INT_MASTER_REG);
400 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
403 static int amd_gpio_irq_set_type(struct irq_data *d, unsigned int type)
407 unsigned long flags, irq_flags;
408 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
409 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
411 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
412 pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
414 /* Ignore the settings coming from the client and
415 * read the values from the ACPI tables
416 * while setting the trigger type
419 irq_flags = irq_get_trigger_type(d->irq);
420 if (irq_flags != IRQ_TYPE_NONE)
423 switch (type & IRQ_TYPE_SENSE_MASK) {
424 case IRQ_TYPE_EDGE_RISING:
425 pin_reg &= ~BIT(LEVEL_TRIG_OFF);
426 pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
427 pin_reg |= ACTIVE_HIGH << ACTIVE_LEVEL_OFF;
428 pin_reg |= DB_TYPE_REMOVE_GLITCH << DB_CNTRL_OFF;
429 irq_set_handler_locked(d, handle_edge_irq);
432 case IRQ_TYPE_EDGE_FALLING:
433 pin_reg &= ~BIT(LEVEL_TRIG_OFF);
434 pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
435 pin_reg |= ACTIVE_LOW << ACTIVE_LEVEL_OFF;
436 pin_reg |= DB_TYPE_REMOVE_GLITCH << DB_CNTRL_OFF;
437 irq_set_handler_locked(d, handle_edge_irq);
440 case IRQ_TYPE_EDGE_BOTH:
441 pin_reg &= ~BIT(LEVEL_TRIG_OFF);
442 pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
443 pin_reg |= BOTH_EADGE << ACTIVE_LEVEL_OFF;
444 pin_reg |= DB_TYPE_REMOVE_GLITCH << DB_CNTRL_OFF;
445 irq_set_handler_locked(d, handle_edge_irq);
448 case IRQ_TYPE_LEVEL_HIGH:
449 pin_reg |= LEVEL_TRIGGER << LEVEL_TRIG_OFF;
450 pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
451 pin_reg |= ACTIVE_HIGH << ACTIVE_LEVEL_OFF;
452 pin_reg &= ~(DB_CNTRl_MASK << DB_CNTRL_OFF);
453 pin_reg |= DB_TYPE_PRESERVE_LOW_GLITCH << DB_CNTRL_OFF;
454 irq_set_handler_locked(d, handle_level_irq);
457 case IRQ_TYPE_LEVEL_LOW:
458 pin_reg |= LEVEL_TRIGGER << LEVEL_TRIG_OFF;
459 pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
460 pin_reg |= ACTIVE_LOW << ACTIVE_LEVEL_OFF;
461 pin_reg &= ~(DB_CNTRl_MASK << DB_CNTRL_OFF);
462 pin_reg |= DB_TYPE_PRESERVE_HIGH_GLITCH << DB_CNTRL_OFF;
463 irq_set_handler_locked(d, handle_level_irq);
470 dev_err(&gpio_dev->pdev->dev, "Invalid type value\n");
474 pin_reg |= CLR_INTR_STAT << INTERRUPT_STS_OFF;
475 writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
476 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
481 static void amd_irq_ack(struct irq_data *d)
484 * based on HW design,there is no need to ack HW
485 * before handle current irq. But this routine is
486 * necessary for handle_edge_irq
490 static struct irq_chip amd_gpio_irqchip = {
492 .irq_ack = amd_irq_ack,
493 .irq_enable = amd_gpio_irq_enable,
494 .irq_disable = amd_gpio_irq_disable,
495 .irq_mask = amd_gpio_irq_mask,
496 .irq_unmask = amd_gpio_irq_unmask,
497 .irq_eoi = amd_gpio_irq_eoi,
498 .irq_set_type = amd_gpio_irq_set_type,
499 .flags = IRQCHIP_SKIP_SET_WAKE,
502 #define PIN_IRQ_PENDING (BIT(INTERRUPT_STS_OFF) | BIT(WAKE_STS_OFF))
504 static irqreturn_t amd_gpio_irq_handler(int irq, void *dev_id)
506 struct amd_gpio *gpio_dev = dev_id;
507 struct gpio_chip *gc = &gpio_dev->gc;
508 irqreturn_t ret = IRQ_NONE;
509 unsigned int i, irqnr;
514 /* Read the wake status */
515 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
516 status = readl(gpio_dev->base + WAKE_INT_STATUS_REG1);
518 status |= readl(gpio_dev->base + WAKE_INT_STATUS_REG0);
519 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
521 /* Bit 0-45 contain the relevant status bits */
522 status &= (1ULL << 46) - 1;
523 regs = gpio_dev->base;
524 for (mask = 1, irqnr = 0; status; mask <<= 1, regs += 4, irqnr += 4) {
525 if (!(status & mask))
529 /* Each status bit covers four pins */
530 for (i = 0; i < 4; i++) {
531 regval = readl(regs + i);
532 if (!(regval & PIN_IRQ_PENDING))
534 irq = irq_find_mapping(gc->irqdomain, irqnr + i);
535 generic_handle_irq(irq);
536 /* Clear interrupt */
537 writel(regval, regs + i);
542 /* Signal EOI to the GPIO unit */
543 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
544 regval = readl(gpio_dev->base + WAKE_INT_MASTER_REG);
546 writel(regval, gpio_dev->base + WAKE_INT_MASTER_REG);
547 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
552 static int amd_get_groups_count(struct pinctrl_dev *pctldev)
554 struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
556 return gpio_dev->ngroups;
559 static const char *amd_get_group_name(struct pinctrl_dev *pctldev,
562 struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
564 return gpio_dev->groups[group].name;
567 static int amd_get_group_pins(struct pinctrl_dev *pctldev,
569 const unsigned **pins,
572 struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
574 *pins = gpio_dev->groups[group].pins;
575 *num_pins = gpio_dev->groups[group].npins;
579 static const struct pinctrl_ops amd_pinctrl_ops = {
580 .get_groups_count = amd_get_groups_count,
581 .get_group_name = amd_get_group_name,
582 .get_group_pins = amd_get_group_pins,
584 .dt_node_to_map = pinconf_generic_dt_node_to_map_group,
585 .dt_free_map = pinctrl_utils_free_map,
589 static int amd_pinconf_get(struct pinctrl_dev *pctldev,
591 unsigned long *config)
596 struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
597 enum pin_config_param param = pinconf_to_config_param(*config);
599 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
600 pin_reg = readl(gpio_dev->base + pin*4);
601 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
603 case PIN_CONFIG_INPUT_DEBOUNCE:
604 arg = pin_reg & DB_TMR_OUT_MASK;
607 case PIN_CONFIG_BIAS_PULL_DOWN:
608 arg = (pin_reg >> PULL_DOWN_ENABLE_OFF) & BIT(0);
611 case PIN_CONFIG_BIAS_PULL_UP:
612 arg = (pin_reg >> PULL_UP_SEL_OFF) & (BIT(0) | BIT(1));
615 case PIN_CONFIG_DRIVE_STRENGTH:
616 arg = (pin_reg >> DRV_STRENGTH_SEL_OFF) & DRV_STRENGTH_SEL_MASK;
620 dev_err(&gpio_dev->pdev->dev, "Invalid config param %04x\n",
625 *config = pinconf_to_config_packed(param, arg);
630 static int amd_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
631 unsigned long *configs, unsigned num_configs)
638 enum pin_config_param param;
639 struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
641 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
642 for (i = 0; i < num_configs; i++) {
643 param = pinconf_to_config_param(configs[i]);
644 arg = pinconf_to_config_argument(configs[i]);
645 pin_reg = readl(gpio_dev->base + pin*4);
648 case PIN_CONFIG_INPUT_DEBOUNCE:
649 pin_reg &= ~DB_TMR_OUT_MASK;
650 pin_reg |= arg & DB_TMR_OUT_MASK;
653 case PIN_CONFIG_BIAS_PULL_DOWN:
654 pin_reg &= ~BIT(PULL_DOWN_ENABLE_OFF);
655 pin_reg |= (arg & BIT(0)) << PULL_DOWN_ENABLE_OFF;
658 case PIN_CONFIG_BIAS_PULL_UP:
659 pin_reg &= ~BIT(PULL_UP_SEL_OFF);
660 pin_reg |= (arg & BIT(0)) << PULL_UP_SEL_OFF;
661 pin_reg &= ~BIT(PULL_UP_ENABLE_OFF);
662 pin_reg |= ((arg>>1) & BIT(0)) << PULL_UP_ENABLE_OFF;
665 case PIN_CONFIG_DRIVE_STRENGTH:
666 pin_reg &= ~(DRV_STRENGTH_SEL_MASK
667 << DRV_STRENGTH_SEL_OFF);
668 pin_reg |= (arg & DRV_STRENGTH_SEL_MASK)
669 << DRV_STRENGTH_SEL_OFF;
673 dev_err(&gpio_dev->pdev->dev,
674 "Invalid config param %04x\n", param);
678 writel(pin_reg, gpio_dev->base + pin*4);
680 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
685 static int amd_pinconf_group_get(struct pinctrl_dev *pctldev,
687 unsigned long *config)
689 const unsigned *pins;
693 ret = amd_get_group_pins(pctldev, group, &pins, &npins);
697 if (amd_pinconf_get(pctldev, pins[0], config))
703 static int amd_pinconf_group_set(struct pinctrl_dev *pctldev,
704 unsigned group, unsigned long *configs,
705 unsigned num_configs)
707 const unsigned *pins;
711 ret = amd_get_group_pins(pctldev, group, &pins, &npins);
714 for (i = 0; i < npins; i++) {
715 if (amd_pinconf_set(pctldev, pins[i], configs, num_configs))
721 static const struct pinconf_ops amd_pinconf_ops = {
722 .pin_config_get = amd_pinconf_get,
723 .pin_config_set = amd_pinconf_set,
724 .pin_config_group_get = amd_pinconf_group_get,
725 .pin_config_group_set = amd_pinconf_group_set,
728 static struct pinctrl_desc amd_pinctrl_desc = {
730 .npins = ARRAY_SIZE(kerncz_pins),
731 .pctlops = &amd_pinctrl_ops,
732 .confops = &amd_pinconf_ops,
733 .owner = THIS_MODULE,
736 static int amd_gpio_probe(struct platform_device *pdev)
740 struct resource *res;
741 struct amd_gpio *gpio_dev;
743 gpio_dev = devm_kzalloc(&pdev->dev,
744 sizeof(struct amd_gpio), GFP_KERNEL);
748 raw_spin_lock_init(&gpio_dev->lock);
750 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
752 dev_err(&pdev->dev, "Failed to get gpio io resource.\n");
756 gpio_dev->base = devm_ioremap_nocache(&pdev->dev, res->start,
761 irq_base = platform_get_irq(pdev, 0);
763 dev_err(&pdev->dev, "Failed to get gpio IRQ: %d\n", irq_base);
767 gpio_dev->pdev = pdev;
768 gpio_dev->gc.direction_input = amd_gpio_direction_input;
769 gpio_dev->gc.direction_output = amd_gpio_direction_output;
770 gpio_dev->gc.get = amd_gpio_get_value;
771 gpio_dev->gc.set = amd_gpio_set_value;
772 gpio_dev->gc.set_config = amd_gpio_set_config;
773 gpio_dev->gc.dbg_show = amd_gpio_dbg_show;
775 gpio_dev->gc.base = -1;
776 gpio_dev->gc.label = pdev->name;
777 gpio_dev->gc.owner = THIS_MODULE;
778 gpio_dev->gc.parent = &pdev->dev;
779 gpio_dev->gc.ngpio = resource_size(res) / 4;
780 #if defined(CONFIG_OF_GPIO)
781 gpio_dev->gc.of_node = pdev->dev.of_node;
784 gpio_dev->hwbank_num = gpio_dev->gc.ngpio / 64;
785 gpio_dev->groups = kerncz_groups;
786 gpio_dev->ngroups = ARRAY_SIZE(kerncz_groups);
788 amd_pinctrl_desc.name = dev_name(&pdev->dev);
789 gpio_dev->pctrl = devm_pinctrl_register(&pdev->dev, &amd_pinctrl_desc,
791 if (IS_ERR(gpio_dev->pctrl)) {
792 dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
793 return PTR_ERR(gpio_dev->pctrl);
796 ret = gpiochip_add_data(&gpio_dev->gc, gpio_dev);
800 ret = gpiochip_add_pin_range(&gpio_dev->gc, dev_name(&pdev->dev),
801 0, 0, gpio_dev->gc.ngpio);
803 dev_err(&pdev->dev, "Failed to add pin range\n");
807 ret = gpiochip_irqchip_add(&gpio_dev->gc,
813 dev_err(&pdev->dev, "could not add irqchip\n");
818 ret = devm_request_irq(&pdev->dev, irq_base, amd_gpio_irq_handler, 0,
819 KBUILD_MODNAME, gpio_dev);
823 platform_set_drvdata(pdev, gpio_dev);
825 dev_dbg(&pdev->dev, "amd gpio driver loaded\n");
829 gpiochip_remove(&gpio_dev->gc);
834 static int amd_gpio_remove(struct platform_device *pdev)
836 struct amd_gpio *gpio_dev;
838 gpio_dev = platform_get_drvdata(pdev);
840 gpiochip_remove(&gpio_dev->gc);
845 static const struct acpi_device_id amd_gpio_acpi_match[] = {
850 MODULE_DEVICE_TABLE(acpi, amd_gpio_acpi_match);
852 static struct platform_driver amd_gpio_driver = {
855 .acpi_match_table = ACPI_PTR(amd_gpio_acpi_match),
857 .probe = amd_gpio_probe,
858 .remove = amd_gpio_remove,
861 module_platform_driver(amd_gpio_driver);
863 MODULE_LICENSE("GPL v2");
864 MODULE_AUTHOR("Ken Xue <Ken.Xue@amd.com>, Jeff Wu <Jeff.Wu@amd.com>");
865 MODULE_DESCRIPTION("AMD GPIO pinctrl driver");