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.
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.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>
35 #include "pinctrl-utils.h"
36 #include "pinctrl-amd.h"
38 static inline struct amd_gpio
*to_amd_gpio(struct gpio_chip
*gc
)
40 return container_of(gc
, struct amd_gpio
, gc
);
43 static int amd_gpio_direction_input(struct gpio_chip
*gc
, unsigned offset
)
47 struct amd_gpio
*gpio_dev
= to_amd_gpio(gc
);
49 spin_lock_irqsave(&gpio_dev
->lock
, flags
);
50 pin_reg
= readl(gpio_dev
->base
+ offset
* 4);
52 * Suppose BIOS or Bootloader sets specific debounce for the
53 * GPIO. if not, set debounce to be 2.75ms and remove glitch.
55 if ((pin_reg
& DB_TMR_OUT_MASK
) == 0) {
57 pin_reg
|= BIT(DB_TMR_OUT_UNIT_OFF
);
58 pin_reg
|= DB_TYPE_REMOVE_GLITCH
<< DB_CNTRL_OFF
;
59 pin_reg
&= ~BIT(DB_TMR_LARGE_OFF
);
62 pin_reg
&= ~BIT(OUTPUT_ENABLE_OFF
);
63 writel(pin_reg
, gpio_dev
->base
+ offset
* 4);
64 spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
69 static int amd_gpio_direction_output(struct gpio_chip
*gc
, unsigned offset
,
74 struct amd_gpio
*gpio_dev
= to_amd_gpio(gc
);
76 spin_lock_irqsave(&gpio_dev
->lock
, flags
);
77 pin_reg
= readl(gpio_dev
->base
+ offset
* 4);
78 pin_reg
|= BIT(OUTPUT_ENABLE_OFF
);
80 pin_reg
|= BIT(OUTPUT_VALUE_OFF
);
82 pin_reg
&= ~BIT(OUTPUT_VALUE_OFF
);
83 writel(pin_reg
, gpio_dev
->base
+ offset
* 4);
84 spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
89 static int amd_gpio_get_value(struct gpio_chip
*gc
, unsigned offset
)
93 struct amd_gpio
*gpio_dev
= to_amd_gpio(gc
);
95 spin_lock_irqsave(&gpio_dev
->lock
, flags
);
96 pin_reg
= readl(gpio_dev
->base
+ offset
* 4);
97 spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
99 return !!(pin_reg
& BIT(PIN_STS_OFF
));
102 static void amd_gpio_set_value(struct gpio_chip
*gc
, unsigned offset
, int value
)
106 struct amd_gpio
*gpio_dev
= to_amd_gpio(gc
);
108 spin_lock_irqsave(&gpio_dev
->lock
, flags
);
109 pin_reg
= readl(gpio_dev
->base
+ offset
* 4);
111 pin_reg
|= BIT(OUTPUT_VALUE_OFF
);
113 pin_reg
&= ~BIT(OUTPUT_VALUE_OFF
);
114 writel(pin_reg
, gpio_dev
->base
+ offset
* 4);
115 spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
118 static int amd_gpio_set_debounce(struct gpio_chip
*gc
, unsigned offset
,
125 struct amd_gpio
*gpio_dev
= to_amd_gpio(gc
);
127 spin_lock_irqsave(&gpio_dev
->lock
, flags
);
128 pin_reg
= readl(gpio_dev
->base
+ offset
* 4);
131 pin_reg
|= DB_TYPE_REMOVE_GLITCH
<< DB_CNTRL_OFF
;
132 pin_reg
&= ~DB_TMR_OUT_MASK
;
134 Debounce Debounce Timer Max
135 TmrLarge TmrOutUnit Unit Debounce
137 0 0 61 usec (2 RtcClk) 976 usec
138 0 1 244 usec (8 RtcClk) 3.9 msec
139 1 0 15.6 msec (512 RtcClk) 250 msec
140 1 1 62.5 msec (2048 RtcClk) 1 sec
145 pin_reg
&= ~BIT(DB_TMR_OUT_UNIT_OFF
);
146 pin_reg
&= ~BIT(DB_TMR_LARGE_OFF
);
147 } else if (debounce
< 976) {
148 time
= debounce
/ 61;
149 pin_reg
|= time
& DB_TMR_OUT_MASK
;
150 pin_reg
&= ~BIT(DB_TMR_OUT_UNIT_OFF
);
151 pin_reg
&= ~BIT(DB_TMR_LARGE_OFF
);
152 } else if (debounce
< 3900) {
153 time
= debounce
/ 244;
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
< 250000) {
158 time
= debounce
/ 15600;
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
< 1000000) {
163 time
= debounce
/ 62500;
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
);
168 pin_reg
&= ~DB_CNTRl_MASK
;
172 pin_reg
&= ~BIT(DB_TMR_OUT_UNIT_OFF
);
173 pin_reg
&= ~BIT(DB_TMR_LARGE_OFF
);
174 pin_reg
&= ~DB_TMR_OUT_MASK
;
175 pin_reg
&= ~DB_CNTRl_MASK
;
177 writel(pin_reg
, gpio_dev
->base
+ offset
* 4);
178 spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
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
= to_amd_gpio(gc
);
193 char *interrupt_enable
;
194 char *interrupt_mask
;
200 char *pull_up_enable
;
201 char *pull_down_enable
;
205 for (bank
= 0; bank
< AMD_GPIO_TOTAL_BANKS
; 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 for (; i
< pin_num
; i
++) {
224 seq_printf(s
, "pin%d\t", i
);
225 spin_lock_irqsave(&gpio_dev
->lock
, flags
);
226 pin_reg
= readl(gpio_dev
->base
+ i
* 4);
227 spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
229 if (pin_reg
& BIT(INTERRUPT_ENABLE_OFF
)) {
230 interrupt_enable
= "interrupt is enabled|";
232 if (!(pin_reg
& BIT(ACTIVE_LEVEL_OFF
))
233 && !(pin_reg
& BIT(ACTIVE_LEVEL_OFF
+1)))
234 active_level
= "Active low|";
235 else if (pin_reg
& BIT(ACTIVE_LEVEL_OFF
)
236 && !(pin_reg
& BIT(ACTIVE_LEVEL_OFF
+1)))
237 active_level
= "Active high|";
238 else if (!(pin_reg
& BIT(ACTIVE_LEVEL_OFF
))
239 && pin_reg
& BIT(ACTIVE_LEVEL_OFF
+1))
240 active_level
= "Active on both|";
242 active_level
= "Unknow Active level|";
244 if (pin_reg
& BIT(LEVEL_TRIG_OFF
))
245 level_trig
= "Level trigger|";
247 level_trig
= "Edge trigger|";
251 "interrupt is disabled|";
256 if (pin_reg
& BIT(INTERRUPT_MASK_OFF
))
258 "interrupt is unmasked|";
261 "interrupt is masked|";
263 if (pin_reg
& BIT(WAKE_CNTRL_OFF
))
264 wake_cntrl0
= "enable wakeup in S0i3 state|";
266 wake_cntrl0
= "disable wakeup in S0i3 state|";
268 if (pin_reg
& BIT(WAKE_CNTRL_OFF
))
269 wake_cntrl1
= "enable wakeup in S3 state|";
271 wake_cntrl1
= "disable wakeup in S3 state|";
273 if (pin_reg
& BIT(WAKE_CNTRL_OFF
))
274 wake_cntrl2
= "enable wakeup in S4/S5 state|";
276 wake_cntrl2
= "disable wakeup in S4/S5 state|";
278 if (pin_reg
& BIT(PULL_UP_ENABLE_OFF
)) {
279 pull_up_enable
= "pull-up is enabled|";
280 if (pin_reg
& BIT(PULL_UP_SEL_OFF
))
281 pull_up_sel
= "8k pull-up|";
283 pull_up_sel
= "4k pull-up|";
285 pull_up_enable
= "pull-up is disabled|";
289 if (pin_reg
& BIT(PULL_DOWN_ENABLE_OFF
))
290 pull_down_enable
= "pull-down is enabled|";
292 pull_down_enable
= "Pull-down is disabled|";
294 if (pin_reg
& BIT(OUTPUT_ENABLE_OFF
)) {
296 output_enable
= "output is enabled|";
297 if (pin_reg
& BIT(OUTPUT_VALUE_OFF
))
298 output_value
= "output is high|";
300 output_value
= "output is low|";
302 output_enable
= "output is disabled|";
305 if (pin_reg
& BIT(PIN_STS_OFF
))
306 pin_sts
= "input is high|";
308 pin_sts
= "input is low|";
311 seq_printf(s
, "%s %s %s %s %s %s\n"
312 " %s %s %s %s %s %s %s 0x%x\n",
313 level_trig
, active_level
, interrupt_enable
,
314 interrupt_mask
, wake_cntrl0
, wake_cntrl1
,
315 wake_cntrl2
, pin_sts
, pull_up_sel
,
316 pull_up_enable
, pull_down_enable
,
317 output_value
, output_enable
, pin_reg
);
322 #define amd_gpio_dbg_show NULL
325 static void amd_gpio_irq_enable(struct irq_data
*d
)
329 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
330 struct amd_gpio
*gpio_dev
= to_amd_gpio(gc
);
332 spin_lock_irqsave(&gpio_dev
->lock
, flags
);
333 pin_reg
= readl(gpio_dev
->base
+ (d
->hwirq
)*4);
335 Suppose BIOS or Bootloader sets specific debounce for the
336 GPIO. if not, set debounce to be 2.75ms.
338 if ((pin_reg
& DB_TMR_OUT_MASK
) == 0) {
340 pin_reg
|= BIT(DB_TMR_OUT_UNIT_OFF
);
341 pin_reg
&= ~BIT(DB_TMR_LARGE_OFF
);
343 pin_reg
|= BIT(INTERRUPT_ENABLE_OFF
);
344 pin_reg
|= BIT(INTERRUPT_MASK_OFF
);
345 writel(pin_reg
, gpio_dev
->base
+ (d
->hwirq
)*4);
346 spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
349 static void amd_gpio_irq_disable(struct irq_data
*d
)
353 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
354 struct amd_gpio
*gpio_dev
= to_amd_gpio(gc
);
356 spin_lock_irqsave(&gpio_dev
->lock
, flags
);
357 pin_reg
= readl(gpio_dev
->base
+ (d
->hwirq
)*4);
358 pin_reg
&= ~BIT(INTERRUPT_ENABLE_OFF
);
359 pin_reg
&= ~BIT(INTERRUPT_MASK_OFF
);
360 writel(pin_reg
, gpio_dev
->base
+ (d
->hwirq
)*4);
361 spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
364 static void amd_gpio_irq_mask(struct irq_data
*d
)
368 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
369 struct amd_gpio
*gpio_dev
= to_amd_gpio(gc
);
371 spin_lock_irqsave(&gpio_dev
->lock
, flags
);
372 pin_reg
= readl(gpio_dev
->base
+ (d
->hwirq
)*4);
373 pin_reg
&= ~BIT(INTERRUPT_MASK_OFF
);
374 writel(pin_reg
, gpio_dev
->base
+ (d
->hwirq
)*4);
375 spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
378 static void amd_gpio_irq_unmask(struct irq_data
*d
)
382 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
383 struct amd_gpio
*gpio_dev
= to_amd_gpio(gc
);
385 spin_lock_irqsave(&gpio_dev
->lock
, flags
);
386 pin_reg
= readl(gpio_dev
->base
+ (d
->hwirq
)*4);
387 pin_reg
|= BIT(INTERRUPT_MASK_OFF
);
388 writel(pin_reg
, gpio_dev
->base
+ (d
->hwirq
)*4);
389 spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
392 static void amd_gpio_irq_eoi(struct irq_data
*d
)
396 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
397 struct amd_gpio
*gpio_dev
= to_amd_gpio(gc
);
399 spin_lock_irqsave(&gpio_dev
->lock
, flags
);
400 reg
= readl(gpio_dev
->base
+ WAKE_INT_MASTER_REG
);
402 writel(reg
, gpio_dev
->base
+ WAKE_INT_MASTER_REG
);
403 spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
406 static int amd_gpio_irq_set_type(struct irq_data
*d
, unsigned int type
)
411 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
412 struct amd_gpio
*gpio_dev
= to_amd_gpio(gc
);
414 spin_lock_irqsave(&gpio_dev
->lock
, flags
);
415 pin_reg
= readl(gpio_dev
->base
+ (d
->hwirq
)*4);
417 switch (type
& IRQ_TYPE_SENSE_MASK
) {
418 case IRQ_TYPE_EDGE_RISING
:
419 pin_reg
&= ~BIT(LEVEL_TRIG_OFF
);
420 pin_reg
&= ~(ACTIVE_LEVEL_MASK
<< ACTIVE_LEVEL_OFF
);
421 pin_reg
|= ACTIVE_HIGH
<< ACTIVE_LEVEL_OFF
;
422 pin_reg
|= DB_TYPE_REMOVE_GLITCH
<< DB_CNTRL_OFF
;
423 irq_set_handler_locked(d
, handle_edge_irq
);
426 case IRQ_TYPE_EDGE_FALLING
:
427 pin_reg
&= ~BIT(LEVEL_TRIG_OFF
);
428 pin_reg
&= ~(ACTIVE_LEVEL_MASK
<< ACTIVE_LEVEL_OFF
);
429 pin_reg
|= ACTIVE_LOW
<< ACTIVE_LEVEL_OFF
;
430 pin_reg
|= DB_TYPE_REMOVE_GLITCH
<< DB_CNTRL_OFF
;
431 irq_set_handler_locked(d
, handle_edge_irq
);
434 case IRQ_TYPE_EDGE_BOTH
:
435 pin_reg
&= ~BIT(LEVEL_TRIG_OFF
);
436 pin_reg
&= ~(ACTIVE_LEVEL_MASK
<< ACTIVE_LEVEL_OFF
);
437 pin_reg
|= BOTH_EADGE
<< ACTIVE_LEVEL_OFF
;
438 pin_reg
|= DB_TYPE_REMOVE_GLITCH
<< DB_CNTRL_OFF
;
439 irq_set_handler_locked(d
, handle_edge_irq
);
442 case IRQ_TYPE_LEVEL_HIGH
:
443 pin_reg
|= LEVEL_TRIGGER
<< LEVEL_TRIG_OFF
;
444 pin_reg
&= ~(ACTIVE_LEVEL_MASK
<< ACTIVE_LEVEL_OFF
);
445 pin_reg
|= ACTIVE_HIGH
<< ACTIVE_LEVEL_OFF
;
446 pin_reg
&= ~(DB_CNTRl_MASK
<< DB_CNTRL_OFF
);
447 pin_reg
|= DB_TYPE_PRESERVE_LOW_GLITCH
<< DB_CNTRL_OFF
;
448 irq_set_handler_locked(d
, handle_level_irq
);
451 case IRQ_TYPE_LEVEL_LOW
:
452 pin_reg
|= LEVEL_TRIGGER
<< LEVEL_TRIG_OFF
;
453 pin_reg
&= ~(ACTIVE_LEVEL_MASK
<< ACTIVE_LEVEL_OFF
);
454 pin_reg
|= ACTIVE_LOW
<< ACTIVE_LEVEL_OFF
;
455 pin_reg
&= ~(DB_CNTRl_MASK
<< DB_CNTRL_OFF
);
456 pin_reg
|= DB_TYPE_PRESERVE_HIGH_GLITCH
<< DB_CNTRL_OFF
;
457 irq_set_handler_locked(d
, handle_level_irq
);
464 dev_err(&gpio_dev
->pdev
->dev
, "Invalid type value\n");
468 pin_reg
|= CLR_INTR_STAT
<< INTERRUPT_STS_OFF
;
469 writel(pin_reg
, gpio_dev
->base
+ (d
->hwirq
)*4);
470 spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
475 static void amd_irq_ack(struct irq_data
*d
)
478 * based on HW design,there is no need to ack HW
479 * before handle current irq. But this routine is
480 * necessary for handle_edge_irq
484 static struct irq_chip amd_gpio_irqchip
= {
486 .irq_ack
= amd_irq_ack
,
487 .irq_enable
= amd_gpio_irq_enable
,
488 .irq_disable
= amd_gpio_irq_disable
,
489 .irq_mask
= amd_gpio_irq_mask
,
490 .irq_unmask
= amd_gpio_irq_unmask
,
491 .irq_eoi
= amd_gpio_irq_eoi
,
492 .irq_set_type
= amd_gpio_irq_set_type
,
495 static void amd_gpio_irq_handler(struct irq_desc
*desc
)
505 struct irq_chip
*chip
= irq_desc_get_chip(desc
);
506 struct gpio_chip
*gc
= irq_desc_get_handler_data(desc
);
507 struct amd_gpio
*gpio_dev
= to_amd_gpio(gc
);
509 chained_irq_enter(chip
, desc
);
510 /*enable GPIO interrupt again*/
511 spin_lock_irqsave(&gpio_dev
->lock
, flags
);
512 reg
= readl(gpio_dev
->base
+ WAKE_INT_STATUS_REG1
);
516 reg
= readl(gpio_dev
->base
+ WAKE_INT_STATUS_REG0
);
518 spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
521 * first 46 bits indicates interrupt status.
522 * one bit represents four interrupt sources.
524 for (off
= 0; off
< 46 ; off
++) {
525 if (reg64
& BIT(off
)) {
526 for (i
= 0; i
< 4; i
++) {
527 pin_reg
= readl(gpio_dev
->base
+
529 if ((pin_reg
& BIT(INTERRUPT_STS_OFF
)) ||
530 (pin_reg
& BIT(WAKE_STS_OFF
))) {
531 irq
= irq_find_mapping(gc
->irqdomain
,
533 generic_handle_irq(irq
);
536 + (off
* 4 + i
) * 4);
544 handle_bad_irq(desc
);
546 spin_lock_irqsave(&gpio_dev
->lock
, flags
);
547 reg
= readl(gpio_dev
->base
+ WAKE_INT_MASTER_REG
);
549 writel(reg
, gpio_dev
->base
+ WAKE_INT_MASTER_REG
);
550 spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
552 chained_irq_exit(chip
, desc
);
555 static int amd_get_groups_count(struct pinctrl_dev
*pctldev
)
557 struct amd_gpio
*gpio_dev
= pinctrl_dev_get_drvdata(pctldev
);
559 return gpio_dev
->ngroups
;
562 static const char *amd_get_group_name(struct pinctrl_dev
*pctldev
,
565 struct amd_gpio
*gpio_dev
= pinctrl_dev_get_drvdata(pctldev
);
567 return gpio_dev
->groups
[group
].name
;
570 static int amd_get_group_pins(struct pinctrl_dev
*pctldev
,
572 const unsigned **pins
,
575 struct amd_gpio
*gpio_dev
= pinctrl_dev_get_drvdata(pctldev
);
577 *pins
= gpio_dev
->groups
[group
].pins
;
578 *num_pins
= gpio_dev
->groups
[group
].npins
;
582 static const struct pinctrl_ops amd_pinctrl_ops
= {
583 .get_groups_count
= amd_get_groups_count
,
584 .get_group_name
= amd_get_group_name
,
585 .get_group_pins
= amd_get_group_pins
,
587 .dt_node_to_map
= pinconf_generic_dt_node_to_map_group
,
588 .dt_free_map
= pinctrl_utils_dt_free_map
,
592 static int amd_pinconf_get(struct pinctrl_dev
*pctldev
,
594 unsigned long *config
)
599 struct amd_gpio
*gpio_dev
= pinctrl_dev_get_drvdata(pctldev
);
600 enum pin_config_param param
= pinconf_to_config_param(*config
);
602 spin_lock_irqsave(&gpio_dev
->lock
, flags
);
603 pin_reg
= readl(gpio_dev
->base
+ pin
*4);
604 spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
606 case PIN_CONFIG_INPUT_DEBOUNCE
:
607 arg
= pin_reg
& DB_TMR_OUT_MASK
;
610 case PIN_CONFIG_BIAS_PULL_DOWN
:
611 arg
= (pin_reg
>> PULL_DOWN_ENABLE_OFF
) & BIT(0);
614 case PIN_CONFIG_BIAS_PULL_UP
:
615 arg
= (pin_reg
>> PULL_UP_SEL_OFF
) & (BIT(0) | BIT(1));
618 case PIN_CONFIG_DRIVE_STRENGTH
:
619 arg
= (pin_reg
>> DRV_STRENGTH_SEL_OFF
) & DRV_STRENGTH_SEL_MASK
;
623 dev_err(&gpio_dev
->pdev
->dev
, "Invalid config param %04x\n",
628 *config
= pinconf_to_config_packed(param
, arg
);
633 static int amd_pinconf_set(struct pinctrl_dev
*pctldev
, unsigned int pin
,
634 unsigned long *configs
, unsigned num_configs
)
641 enum pin_config_param param
;
642 struct amd_gpio
*gpio_dev
= pinctrl_dev_get_drvdata(pctldev
);
644 spin_lock_irqsave(&gpio_dev
->lock
, flags
);
645 for (i
= 0; i
< num_configs
; i
++) {
646 param
= pinconf_to_config_param(configs
[i
]);
647 arg
= pinconf_to_config_argument(configs
[i
]);
648 pin_reg
= readl(gpio_dev
->base
+ pin
*4);
651 case PIN_CONFIG_INPUT_DEBOUNCE
:
652 pin_reg
&= ~DB_TMR_OUT_MASK
;
653 pin_reg
|= arg
& DB_TMR_OUT_MASK
;
656 case PIN_CONFIG_BIAS_PULL_DOWN
:
657 pin_reg
&= ~BIT(PULL_DOWN_ENABLE_OFF
);
658 pin_reg
|= (arg
& BIT(0)) << PULL_DOWN_ENABLE_OFF
;
661 case PIN_CONFIG_BIAS_PULL_UP
:
662 pin_reg
&= ~BIT(PULL_UP_SEL_OFF
);
663 pin_reg
|= (arg
& BIT(0)) << PULL_UP_SEL_OFF
;
664 pin_reg
&= ~BIT(PULL_UP_ENABLE_OFF
);
665 pin_reg
|= ((arg
>>1) & BIT(0)) << PULL_UP_ENABLE_OFF
;
668 case PIN_CONFIG_DRIVE_STRENGTH
:
669 pin_reg
&= ~(DRV_STRENGTH_SEL_MASK
670 << DRV_STRENGTH_SEL_OFF
);
671 pin_reg
|= (arg
& DRV_STRENGTH_SEL_MASK
)
672 << DRV_STRENGTH_SEL_OFF
;
676 dev_err(&gpio_dev
->pdev
->dev
,
677 "Invalid config param %04x\n", param
);
681 writel(pin_reg
, gpio_dev
->base
+ pin
*4);
683 spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
688 static int amd_pinconf_group_get(struct pinctrl_dev
*pctldev
,
690 unsigned long *config
)
692 const unsigned *pins
;
696 ret
= amd_get_group_pins(pctldev
, group
, &pins
, &npins
);
700 if (amd_pinconf_get(pctldev
, pins
[0], config
))
706 static int amd_pinconf_group_set(struct pinctrl_dev
*pctldev
,
707 unsigned group
, unsigned long *configs
,
708 unsigned num_configs
)
710 const unsigned *pins
;
714 ret
= amd_get_group_pins(pctldev
, group
, &pins
, &npins
);
717 for (i
= 0; i
< npins
; i
++) {
718 if (amd_pinconf_set(pctldev
, pins
[i
], configs
, num_configs
))
724 static const struct pinconf_ops amd_pinconf_ops
= {
725 .pin_config_get
= amd_pinconf_get
,
726 .pin_config_set
= amd_pinconf_set
,
727 .pin_config_group_get
= amd_pinconf_group_get
,
728 .pin_config_group_set
= amd_pinconf_group_set
,
731 static struct pinctrl_desc amd_pinctrl_desc
= {
733 .npins
= ARRAY_SIZE(kerncz_pins
),
734 .pctlops
= &amd_pinctrl_ops
,
735 .confops
= &amd_pinconf_ops
,
736 .owner
= THIS_MODULE
,
739 static int amd_gpio_probe(struct platform_device
*pdev
)
743 struct resource
*res
;
744 struct amd_gpio
*gpio_dev
;
746 gpio_dev
= devm_kzalloc(&pdev
->dev
,
747 sizeof(struct amd_gpio
), GFP_KERNEL
);
751 spin_lock_init(&gpio_dev
->lock
);
753 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
755 dev_err(&pdev
->dev
, "Failed to get gpio io resource.\n");
759 gpio_dev
->base
= devm_ioremap_nocache(&pdev
->dev
, res
->start
,
761 if (IS_ERR(gpio_dev
->base
))
762 return PTR_ERR(gpio_dev
->base
);
764 irq_base
= platform_get_irq(pdev
, 0);
766 dev_err(&pdev
->dev
, "Failed to get gpio IRQ.\n");
770 gpio_dev
->pdev
= pdev
;
771 gpio_dev
->gc
.direction_input
= amd_gpio_direction_input
;
772 gpio_dev
->gc
.direction_output
= amd_gpio_direction_output
;
773 gpio_dev
->gc
.get
= amd_gpio_get_value
;
774 gpio_dev
->gc
.set
= amd_gpio_set_value
;
775 gpio_dev
->gc
.set_debounce
= amd_gpio_set_debounce
;
776 gpio_dev
->gc
.dbg_show
= amd_gpio_dbg_show
;
778 gpio_dev
->gc
.base
= 0;
779 gpio_dev
->gc
.label
= pdev
->name
;
780 gpio_dev
->gc
.owner
= THIS_MODULE
;
781 gpio_dev
->gc
.dev
= &pdev
->dev
;
782 gpio_dev
->gc
.ngpio
= TOTAL_NUMBER_OF_PINS
;
783 #if defined(CONFIG_OF_GPIO)
784 gpio_dev
->gc
.of_node
= pdev
->dev
.of_node
;
787 gpio_dev
->groups
= kerncz_groups
;
788 gpio_dev
->ngroups
= ARRAY_SIZE(kerncz_groups
);
790 amd_pinctrl_desc
.name
= dev_name(&pdev
->dev
);
791 gpio_dev
->pctrl
= pinctrl_register(&amd_pinctrl_desc
,
792 &pdev
->dev
, gpio_dev
);
793 if (IS_ERR(gpio_dev
->pctrl
)) {
794 dev_err(&pdev
->dev
, "Couldn't register pinctrl driver\n");
795 return PTR_ERR(gpio_dev
->pctrl
);
798 ret
= gpiochip_add(&gpio_dev
->gc
);
802 ret
= gpiochip_add_pin_range(&gpio_dev
->gc
, dev_name(&pdev
->dev
),
803 0, 0, TOTAL_NUMBER_OF_PINS
);
805 dev_err(&pdev
->dev
, "Failed to add pin range\n");
809 ret
= gpiochip_irqchip_add(&gpio_dev
->gc
,
815 dev_err(&pdev
->dev
, "could not add irqchip\n");
820 gpiochip_set_chained_irqchip(&gpio_dev
->gc
,
823 amd_gpio_irq_handler
);
825 platform_set_drvdata(pdev
, gpio_dev
);
827 dev_dbg(&pdev
->dev
, "amd gpio driver loaded\n");
831 gpiochip_remove(&gpio_dev
->gc
);
834 pinctrl_unregister(gpio_dev
->pctrl
);
838 static int amd_gpio_remove(struct platform_device
*pdev
)
840 struct amd_gpio
*gpio_dev
;
842 gpio_dev
= platform_get_drvdata(pdev
);
844 gpiochip_remove(&gpio_dev
->gc
);
845 pinctrl_unregister(gpio_dev
->pctrl
);
850 static const struct acpi_device_id amd_gpio_acpi_match
[] = {
854 MODULE_DEVICE_TABLE(acpi
, amd_gpio_acpi_match
);
856 static struct platform_driver amd_gpio_driver
= {
859 .acpi_match_table
= ACPI_PTR(amd_gpio_acpi_match
),
861 .probe
= amd_gpio_probe
,
862 .remove
= amd_gpio_remove
,
865 module_platform_driver(amd_gpio_driver
);
867 MODULE_LICENSE("GPL v2");
868 MODULE_AUTHOR("Ken Xue <Ken.Xue@amd.com>, Jeff Wu <Jeff.Wu@amd.com>");
869 MODULE_DESCRIPTION("AMD GPIO pinctrl driver");