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);
51 pin_reg
&= ~BIT(OUTPUT_ENABLE_OFF
);
52 writel(pin_reg
, gpio_dev
->base
+ offset
* 4);
53 spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
58 static int amd_gpio_direction_output(struct gpio_chip
*gc
, unsigned offset
,
63 struct amd_gpio
*gpio_dev
= to_amd_gpio(gc
);
65 spin_lock_irqsave(&gpio_dev
->lock
, flags
);
66 pin_reg
= readl(gpio_dev
->base
+ offset
* 4);
67 pin_reg
|= BIT(OUTPUT_ENABLE_OFF
);
69 pin_reg
|= BIT(OUTPUT_VALUE_OFF
);
71 pin_reg
&= ~BIT(OUTPUT_VALUE_OFF
);
72 writel(pin_reg
, gpio_dev
->base
+ offset
* 4);
73 spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
78 static int amd_gpio_get_value(struct gpio_chip
*gc
, unsigned offset
)
82 struct amd_gpio
*gpio_dev
= to_amd_gpio(gc
);
84 spin_lock_irqsave(&gpio_dev
->lock
, flags
);
85 pin_reg
= readl(gpio_dev
->base
+ offset
* 4);
86 spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
88 return !!(pin_reg
& BIT(PIN_STS_OFF
));
91 static void amd_gpio_set_value(struct gpio_chip
*gc
, unsigned offset
, int value
)
95 struct amd_gpio
*gpio_dev
= to_amd_gpio(gc
);
97 spin_lock_irqsave(&gpio_dev
->lock
, flags
);
98 pin_reg
= readl(gpio_dev
->base
+ offset
* 4);
100 pin_reg
|= BIT(OUTPUT_VALUE_OFF
);
102 pin_reg
&= ~BIT(OUTPUT_VALUE_OFF
);
103 writel(pin_reg
, gpio_dev
->base
+ offset
* 4);
104 spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
107 static int amd_gpio_set_debounce(struct gpio_chip
*gc
, unsigned offset
,
114 struct amd_gpio
*gpio_dev
= to_amd_gpio(gc
);
116 spin_lock_irqsave(&gpio_dev
->lock
, flags
);
117 pin_reg
= readl(gpio_dev
->base
+ offset
* 4);
120 pin_reg
|= DB_TYPE_REMOVE_GLITCH
<< DB_CNTRL_OFF
;
121 pin_reg
&= ~DB_TMR_OUT_MASK
;
123 Debounce Debounce Timer Max
124 TmrLarge TmrOutUnit Unit Debounce
126 0 0 61 usec (2 RtcClk) 976 usec
127 0 1 244 usec (8 RtcClk) 3.9 msec
128 1 0 15.6 msec (512 RtcClk) 250 msec
129 1 1 62.5 msec (2048 RtcClk) 1 sec
134 pin_reg
&= ~BIT(DB_TMR_OUT_UNIT_OFF
);
135 pin_reg
&= ~BIT(DB_TMR_LARGE_OFF
);
136 } else if (debounce
< 976) {
137 time
= debounce
/ 61;
138 pin_reg
|= time
& DB_TMR_OUT_MASK
;
139 pin_reg
&= ~BIT(DB_TMR_OUT_UNIT_OFF
);
140 pin_reg
&= ~BIT(DB_TMR_LARGE_OFF
);
141 } else if (debounce
< 3900) {
142 time
= debounce
/ 244;
143 pin_reg
|= time
& DB_TMR_OUT_MASK
;
144 pin_reg
|= BIT(DB_TMR_OUT_UNIT_OFF
);
145 pin_reg
&= ~BIT(DB_TMR_LARGE_OFF
);
146 } else if (debounce
< 250000) {
147 time
= debounce
/ 15600;
148 pin_reg
|= time
& DB_TMR_OUT_MASK
;
149 pin_reg
&= ~BIT(DB_TMR_OUT_UNIT_OFF
);
150 pin_reg
|= BIT(DB_TMR_LARGE_OFF
);
151 } else if (debounce
< 1000000) {
152 time
= debounce
/ 62500;
153 pin_reg
|= time
& DB_TMR_OUT_MASK
;
154 pin_reg
|= BIT(DB_TMR_OUT_UNIT_OFF
);
155 pin_reg
|= BIT(DB_TMR_LARGE_OFF
);
157 pin_reg
&= ~DB_CNTRl_MASK
;
161 pin_reg
&= ~BIT(DB_TMR_OUT_UNIT_OFF
);
162 pin_reg
&= ~BIT(DB_TMR_LARGE_OFF
);
163 pin_reg
&= ~DB_TMR_OUT_MASK
;
164 pin_reg
&= ~DB_CNTRl_MASK
;
166 writel(pin_reg
, gpio_dev
->base
+ offset
* 4);
167 spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
172 #ifdef CONFIG_DEBUG_FS
173 static void amd_gpio_dbg_show(struct seq_file
*s
, struct gpio_chip
*gc
)
177 unsigned int bank
, i
, pin_num
;
178 struct amd_gpio
*gpio_dev
= to_amd_gpio(gc
);
182 char *interrupt_enable
;
183 char *interrupt_mask
;
189 char *pull_up_enable
;
190 char *pull_down_enable
;
194 for (bank
= 0; bank
< AMD_GPIO_TOTAL_BANKS
; bank
++) {
195 seq_printf(s
, "GPIO bank%d\t", bank
);
200 pin_num
= AMD_GPIO_PINS_BANK0
;
204 pin_num
= AMD_GPIO_PINS_BANK1
+ i
;
208 pin_num
= AMD_GPIO_PINS_BANK2
+ i
;
212 for (; i
< pin_num
; i
++) {
213 seq_printf(s
, "pin%d\t", i
);
214 spin_lock_irqsave(&gpio_dev
->lock
, flags
);
215 pin_reg
= readl(gpio_dev
->base
+ i
* 4);
216 spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
218 if (pin_reg
& BIT(INTERRUPT_ENABLE_OFF
)) {
219 interrupt_enable
= "interrupt is enabled|";
221 if (!(pin_reg
& BIT(ACTIVE_LEVEL_OFF
))
222 && !(pin_reg
& BIT(ACTIVE_LEVEL_OFF
+1)))
223 active_level
= "Active low|";
224 else if (pin_reg
& BIT(ACTIVE_LEVEL_OFF
)
225 && !(pin_reg
& BIT(ACTIVE_LEVEL_OFF
+1)))
226 active_level
= "Active high|";
227 else if (!(pin_reg
& BIT(ACTIVE_LEVEL_OFF
))
228 && pin_reg
& BIT(ACTIVE_LEVEL_OFF
+1))
229 active_level
= "Active on both|";
231 active_level
= "Unknow Active level|";
233 if (pin_reg
& BIT(LEVEL_TRIG_OFF
))
234 level_trig
= "Level trigger|";
236 level_trig
= "Edge trigger|";
240 "interrupt is disabled|";
245 if (pin_reg
& BIT(INTERRUPT_MASK_OFF
))
247 "interrupt is unmasked|";
250 "interrupt is masked|";
252 if (pin_reg
& BIT(WAKE_CNTRL_OFF
))
253 wake_cntrl0
= "enable wakeup in S0i3 state|";
255 wake_cntrl0
= "disable wakeup in S0i3 state|";
257 if (pin_reg
& BIT(WAKE_CNTRL_OFF
))
258 wake_cntrl1
= "enable wakeup in S3 state|";
260 wake_cntrl1
= "disable wakeup in S3 state|";
262 if (pin_reg
& BIT(WAKE_CNTRL_OFF
))
263 wake_cntrl2
= "enable wakeup in S4/S5 state|";
265 wake_cntrl2
= "disable wakeup in S4/S5 state|";
267 if (pin_reg
& BIT(PULL_UP_ENABLE_OFF
)) {
268 pull_up_enable
= "pull-up is enabled|";
269 if (pin_reg
& BIT(PULL_UP_SEL_OFF
))
270 pull_up_sel
= "8k pull-up|";
272 pull_up_sel
= "4k pull-up|";
274 pull_up_enable
= "pull-up is disabled|";
278 if (pin_reg
& BIT(PULL_DOWN_ENABLE_OFF
))
279 pull_down_enable
= "pull-down is enabled|";
281 pull_down_enable
= "Pull-down is disabled|";
283 if (pin_reg
& BIT(OUTPUT_ENABLE_OFF
)) {
285 output_enable
= "output is enabled|";
286 if (pin_reg
& BIT(OUTPUT_VALUE_OFF
))
287 output_value
= "output is high|";
289 output_value
= "output is low|";
291 output_enable
= "output is disabled|";
294 if (pin_reg
& BIT(PIN_STS_OFF
))
295 pin_sts
= "input is high|";
297 pin_sts
= "input is low|";
300 seq_printf(s
, "%s %s %s %s %s %s\n"
301 " %s %s %s %s %s %s %s 0x%x\n",
302 level_trig
, active_level
, interrupt_enable
,
303 interrupt_mask
, wake_cntrl0
, wake_cntrl1
,
304 wake_cntrl2
, pin_sts
, pull_up_sel
,
305 pull_up_enable
, pull_down_enable
,
306 output_value
, output_enable
, pin_reg
);
311 #define amd_gpio_dbg_show NULL
314 static void amd_gpio_irq_enable(struct irq_data
*d
)
318 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
319 struct amd_gpio
*gpio_dev
= to_amd_gpio(gc
);
321 spin_lock_irqsave(&gpio_dev
->lock
, flags
);
322 pin_reg
= readl(gpio_dev
->base
+ (d
->hwirq
)*4);
323 pin_reg
|= BIT(INTERRUPT_ENABLE_OFF
);
324 pin_reg
|= BIT(INTERRUPT_MASK_OFF
);
325 writel(pin_reg
, gpio_dev
->base
+ (d
->hwirq
)*4);
326 spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
329 static void amd_gpio_irq_disable(struct irq_data
*d
)
333 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
334 struct amd_gpio
*gpio_dev
= to_amd_gpio(gc
);
336 spin_lock_irqsave(&gpio_dev
->lock
, flags
);
337 pin_reg
= readl(gpio_dev
->base
+ (d
->hwirq
)*4);
338 pin_reg
&= ~BIT(INTERRUPT_ENABLE_OFF
);
339 pin_reg
&= ~BIT(INTERRUPT_MASK_OFF
);
340 writel(pin_reg
, gpio_dev
->base
+ (d
->hwirq
)*4);
341 spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
344 static void amd_gpio_irq_mask(struct irq_data
*d
)
348 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
349 struct amd_gpio
*gpio_dev
= to_amd_gpio(gc
);
351 spin_lock_irqsave(&gpio_dev
->lock
, flags
);
352 pin_reg
= readl(gpio_dev
->base
+ (d
->hwirq
)*4);
353 pin_reg
&= ~BIT(INTERRUPT_MASK_OFF
);
354 writel(pin_reg
, gpio_dev
->base
+ (d
->hwirq
)*4);
355 spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
358 static void amd_gpio_irq_unmask(struct irq_data
*d
)
362 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
363 struct amd_gpio
*gpio_dev
= to_amd_gpio(gc
);
365 spin_lock_irqsave(&gpio_dev
->lock
, flags
);
366 pin_reg
= readl(gpio_dev
->base
+ (d
->hwirq
)*4);
367 pin_reg
|= BIT(INTERRUPT_MASK_OFF
);
368 writel(pin_reg
, gpio_dev
->base
+ (d
->hwirq
)*4);
369 spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
372 static void amd_gpio_irq_eoi(struct irq_data
*d
)
376 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
377 struct amd_gpio
*gpio_dev
= to_amd_gpio(gc
);
379 spin_lock_irqsave(&gpio_dev
->lock
, flags
);
380 reg
= readl(gpio_dev
->base
+ WAKE_INT_MASTER_REG
);
382 writel(reg
, gpio_dev
->base
+ WAKE_INT_MASTER_REG
);
383 spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
386 static int amd_gpio_irq_set_type(struct irq_data
*d
, unsigned int type
)
391 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
392 struct amd_gpio
*gpio_dev
= to_amd_gpio(gc
);
394 spin_lock_irqsave(&gpio_dev
->lock
, flags
);
395 pin_reg
= readl(gpio_dev
->base
+ (d
->hwirq
)*4);
397 switch (type
& IRQ_TYPE_SENSE_MASK
) {
398 case IRQ_TYPE_EDGE_RISING
:
399 pin_reg
&= ~BIT(LEVEL_TRIG_OFF
);
400 pin_reg
&= ~(ACTIVE_LEVEL_MASK
<< ACTIVE_LEVEL_OFF
);
401 pin_reg
|= ACTIVE_HIGH
<< ACTIVE_LEVEL_OFF
;
402 pin_reg
|= DB_TYPE_REMOVE_GLITCH
<< DB_CNTRL_OFF
;
403 irq_set_handler_locked(d
, handle_edge_irq
);
406 case IRQ_TYPE_EDGE_FALLING
:
407 pin_reg
&= ~BIT(LEVEL_TRIG_OFF
);
408 pin_reg
&= ~(ACTIVE_LEVEL_MASK
<< ACTIVE_LEVEL_OFF
);
409 pin_reg
|= ACTIVE_LOW
<< ACTIVE_LEVEL_OFF
;
410 pin_reg
|= DB_TYPE_REMOVE_GLITCH
<< DB_CNTRL_OFF
;
411 irq_set_handler_locked(d
, handle_edge_irq
);
414 case IRQ_TYPE_EDGE_BOTH
:
415 pin_reg
&= ~BIT(LEVEL_TRIG_OFF
);
416 pin_reg
&= ~(ACTIVE_LEVEL_MASK
<< ACTIVE_LEVEL_OFF
);
417 pin_reg
|= BOTH_EADGE
<< ACTIVE_LEVEL_OFF
;
418 pin_reg
|= DB_TYPE_REMOVE_GLITCH
<< DB_CNTRL_OFF
;
419 irq_set_handler_locked(d
, handle_edge_irq
);
422 case IRQ_TYPE_LEVEL_HIGH
:
423 pin_reg
|= LEVEL_TRIGGER
<< LEVEL_TRIG_OFF
;
424 pin_reg
&= ~(ACTIVE_LEVEL_MASK
<< ACTIVE_LEVEL_OFF
);
425 pin_reg
|= ACTIVE_HIGH
<< ACTIVE_LEVEL_OFF
;
426 pin_reg
&= ~(DB_CNTRl_MASK
<< DB_CNTRL_OFF
);
427 pin_reg
|= DB_TYPE_PRESERVE_LOW_GLITCH
<< DB_CNTRL_OFF
;
428 irq_set_handler_locked(d
, handle_level_irq
);
431 case IRQ_TYPE_LEVEL_LOW
:
432 pin_reg
|= LEVEL_TRIGGER
<< LEVEL_TRIG_OFF
;
433 pin_reg
&= ~(ACTIVE_LEVEL_MASK
<< ACTIVE_LEVEL_OFF
);
434 pin_reg
|= ACTIVE_LOW
<< ACTIVE_LEVEL_OFF
;
435 pin_reg
&= ~(DB_CNTRl_MASK
<< DB_CNTRL_OFF
);
436 pin_reg
|= DB_TYPE_PRESERVE_HIGH_GLITCH
<< DB_CNTRL_OFF
;
437 irq_set_handler_locked(d
, handle_level_irq
);
444 dev_err(&gpio_dev
->pdev
->dev
, "Invalid type value\n");
448 pin_reg
|= CLR_INTR_STAT
<< INTERRUPT_STS_OFF
;
449 writel(pin_reg
, gpio_dev
->base
+ (d
->hwirq
)*4);
450 spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
455 static void amd_irq_ack(struct irq_data
*d
)
458 * based on HW design,there is no need to ack HW
459 * before handle current irq. But this routine is
460 * necessary for handle_edge_irq
464 static struct irq_chip amd_gpio_irqchip
= {
466 .irq_ack
= amd_irq_ack
,
467 .irq_enable
= amd_gpio_irq_enable
,
468 .irq_disable
= amd_gpio_irq_disable
,
469 .irq_mask
= amd_gpio_irq_mask
,
470 .irq_unmask
= amd_gpio_irq_unmask
,
471 .irq_eoi
= amd_gpio_irq_eoi
,
472 .irq_set_type
= amd_gpio_irq_set_type
,
475 static void amd_gpio_irq_handler(struct irq_desc
*desc
)
485 struct irq_chip
*chip
= irq_desc_get_chip(desc
);
486 struct gpio_chip
*gc
= irq_desc_get_handler_data(desc
);
487 struct amd_gpio
*gpio_dev
= to_amd_gpio(gc
);
489 chained_irq_enter(chip
, desc
);
490 /*enable GPIO interrupt again*/
491 spin_lock_irqsave(&gpio_dev
->lock
, flags
);
492 reg
= readl(gpio_dev
->base
+ WAKE_INT_STATUS_REG1
);
496 reg
= readl(gpio_dev
->base
+ WAKE_INT_STATUS_REG0
);
498 spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
501 * first 46 bits indicates interrupt status.
502 * one bit represents four interrupt sources.
504 for (off
= 0; off
< 46 ; off
++) {
505 if (reg64
& BIT(off
)) {
506 for (i
= 0; i
< 4; i
++) {
507 pin_reg
= readl(gpio_dev
->base
+
509 if ((pin_reg
& BIT(INTERRUPT_STS_OFF
)) ||
510 (pin_reg
& BIT(WAKE_STS_OFF
))) {
511 irq
= irq_find_mapping(gc
->irqdomain
,
513 generic_handle_irq(irq
);
516 + (off
* 4 + i
) * 4);
524 handle_bad_irq(desc
);
526 spin_lock_irqsave(&gpio_dev
->lock
, flags
);
527 reg
= readl(gpio_dev
->base
+ WAKE_INT_MASTER_REG
);
529 writel(reg
, gpio_dev
->base
+ WAKE_INT_MASTER_REG
);
530 spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
532 chained_irq_exit(chip
, desc
);
535 static int amd_get_groups_count(struct pinctrl_dev
*pctldev
)
537 struct amd_gpio
*gpio_dev
= pinctrl_dev_get_drvdata(pctldev
);
539 return gpio_dev
->ngroups
;
542 static const char *amd_get_group_name(struct pinctrl_dev
*pctldev
,
545 struct amd_gpio
*gpio_dev
= pinctrl_dev_get_drvdata(pctldev
);
547 return gpio_dev
->groups
[group
].name
;
550 static int amd_get_group_pins(struct pinctrl_dev
*pctldev
,
552 const unsigned **pins
,
555 struct amd_gpio
*gpio_dev
= pinctrl_dev_get_drvdata(pctldev
);
557 *pins
= gpio_dev
->groups
[group
].pins
;
558 *num_pins
= gpio_dev
->groups
[group
].npins
;
562 static const struct pinctrl_ops amd_pinctrl_ops
= {
563 .get_groups_count
= amd_get_groups_count
,
564 .get_group_name
= amd_get_group_name
,
565 .get_group_pins
= amd_get_group_pins
,
567 .dt_node_to_map
= pinconf_generic_dt_node_to_map_group
,
568 .dt_free_map
= pinctrl_utils_dt_free_map
,
572 static int amd_pinconf_get(struct pinctrl_dev
*pctldev
,
574 unsigned long *config
)
579 struct amd_gpio
*gpio_dev
= pinctrl_dev_get_drvdata(pctldev
);
580 enum pin_config_param param
= pinconf_to_config_param(*config
);
582 spin_lock_irqsave(&gpio_dev
->lock
, flags
);
583 pin_reg
= readl(gpio_dev
->base
+ pin
*4);
584 spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
586 case PIN_CONFIG_INPUT_DEBOUNCE
:
587 arg
= pin_reg
& DB_TMR_OUT_MASK
;
590 case PIN_CONFIG_BIAS_PULL_DOWN
:
591 arg
= (pin_reg
>> PULL_DOWN_ENABLE_OFF
) & BIT(0);
594 case PIN_CONFIG_BIAS_PULL_UP
:
595 arg
= (pin_reg
>> PULL_UP_SEL_OFF
) & (BIT(0) | BIT(1));
598 case PIN_CONFIG_DRIVE_STRENGTH
:
599 arg
= (pin_reg
>> DRV_STRENGTH_SEL_OFF
) & DRV_STRENGTH_SEL_MASK
;
603 dev_err(&gpio_dev
->pdev
->dev
, "Invalid config param %04x\n",
608 *config
= pinconf_to_config_packed(param
, arg
);
613 static int amd_pinconf_set(struct pinctrl_dev
*pctldev
, unsigned int pin
,
614 unsigned long *configs
, unsigned num_configs
)
621 enum pin_config_param param
;
622 struct amd_gpio
*gpio_dev
= pinctrl_dev_get_drvdata(pctldev
);
624 spin_lock_irqsave(&gpio_dev
->lock
, flags
);
625 for (i
= 0; i
< num_configs
; i
++) {
626 param
= pinconf_to_config_param(configs
[i
]);
627 arg
= pinconf_to_config_argument(configs
[i
]);
628 pin_reg
= readl(gpio_dev
->base
+ pin
*4);
631 case PIN_CONFIG_INPUT_DEBOUNCE
:
632 pin_reg
&= ~DB_TMR_OUT_MASK
;
633 pin_reg
|= arg
& DB_TMR_OUT_MASK
;
636 case PIN_CONFIG_BIAS_PULL_DOWN
:
637 pin_reg
&= ~BIT(PULL_DOWN_ENABLE_OFF
);
638 pin_reg
|= (arg
& BIT(0)) << PULL_DOWN_ENABLE_OFF
;
641 case PIN_CONFIG_BIAS_PULL_UP
:
642 pin_reg
&= ~BIT(PULL_UP_SEL_OFF
);
643 pin_reg
|= (arg
& BIT(0)) << PULL_UP_SEL_OFF
;
644 pin_reg
&= ~BIT(PULL_UP_ENABLE_OFF
);
645 pin_reg
|= ((arg
>>1) & BIT(0)) << PULL_UP_ENABLE_OFF
;
648 case PIN_CONFIG_DRIVE_STRENGTH
:
649 pin_reg
&= ~(DRV_STRENGTH_SEL_MASK
650 << DRV_STRENGTH_SEL_OFF
);
651 pin_reg
|= (arg
& DRV_STRENGTH_SEL_MASK
)
652 << DRV_STRENGTH_SEL_OFF
;
656 dev_err(&gpio_dev
->pdev
->dev
,
657 "Invalid config param %04x\n", param
);
661 writel(pin_reg
, gpio_dev
->base
+ pin
*4);
663 spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
668 static int amd_pinconf_group_get(struct pinctrl_dev
*pctldev
,
670 unsigned long *config
)
672 const unsigned *pins
;
676 ret
= amd_get_group_pins(pctldev
, group
, &pins
, &npins
);
680 if (amd_pinconf_get(pctldev
, pins
[0], config
))
686 static int amd_pinconf_group_set(struct pinctrl_dev
*pctldev
,
687 unsigned group
, unsigned long *configs
,
688 unsigned num_configs
)
690 const unsigned *pins
;
694 ret
= amd_get_group_pins(pctldev
, group
, &pins
, &npins
);
697 for (i
= 0; i
< npins
; i
++) {
698 if (amd_pinconf_set(pctldev
, pins
[i
], configs
, num_configs
))
704 static const struct pinconf_ops amd_pinconf_ops
= {
705 .pin_config_get
= amd_pinconf_get
,
706 .pin_config_set
= amd_pinconf_set
,
707 .pin_config_group_get
= amd_pinconf_group_get
,
708 .pin_config_group_set
= amd_pinconf_group_set
,
711 static struct pinctrl_desc amd_pinctrl_desc
= {
713 .npins
= ARRAY_SIZE(kerncz_pins
),
714 .pctlops
= &amd_pinctrl_ops
,
715 .confops
= &amd_pinconf_ops
,
716 .owner
= THIS_MODULE
,
719 static int amd_gpio_probe(struct platform_device
*pdev
)
723 struct resource
*res
;
724 struct amd_gpio
*gpio_dev
;
726 gpio_dev
= devm_kzalloc(&pdev
->dev
,
727 sizeof(struct amd_gpio
), GFP_KERNEL
);
731 spin_lock_init(&gpio_dev
->lock
);
733 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
735 dev_err(&pdev
->dev
, "Failed to get gpio io resource.\n");
739 gpio_dev
->base
= devm_ioremap_nocache(&pdev
->dev
, res
->start
,
741 if (IS_ERR(gpio_dev
->base
))
742 return PTR_ERR(gpio_dev
->base
);
744 irq_base
= platform_get_irq(pdev
, 0);
746 dev_err(&pdev
->dev
, "Failed to get gpio IRQ.\n");
750 gpio_dev
->pdev
= pdev
;
751 gpio_dev
->gc
.direction_input
= amd_gpio_direction_input
;
752 gpio_dev
->gc
.direction_output
= amd_gpio_direction_output
;
753 gpio_dev
->gc
.get
= amd_gpio_get_value
;
754 gpio_dev
->gc
.set
= amd_gpio_set_value
;
755 gpio_dev
->gc
.set_debounce
= amd_gpio_set_debounce
;
756 gpio_dev
->gc
.dbg_show
= amd_gpio_dbg_show
;
758 gpio_dev
->gc
.base
= 0;
759 gpio_dev
->gc
.label
= pdev
->name
;
760 gpio_dev
->gc
.owner
= THIS_MODULE
;
761 gpio_dev
->gc
.dev
= &pdev
->dev
;
762 gpio_dev
->gc
.ngpio
= TOTAL_NUMBER_OF_PINS
;
763 #if defined(CONFIG_OF_GPIO)
764 gpio_dev
->gc
.of_node
= pdev
->dev
.of_node
;
767 gpio_dev
->groups
= kerncz_groups
;
768 gpio_dev
->ngroups
= ARRAY_SIZE(kerncz_groups
);
770 amd_pinctrl_desc
.name
= dev_name(&pdev
->dev
);
771 gpio_dev
->pctrl
= pinctrl_register(&amd_pinctrl_desc
,
772 &pdev
->dev
, gpio_dev
);
773 if (IS_ERR(gpio_dev
->pctrl
)) {
774 dev_err(&pdev
->dev
, "Couldn't register pinctrl driver\n");
775 return PTR_ERR(gpio_dev
->pctrl
);
778 ret
= gpiochip_add(&gpio_dev
->gc
);
782 ret
= gpiochip_add_pin_range(&gpio_dev
->gc
, dev_name(&pdev
->dev
),
783 0, 0, TOTAL_NUMBER_OF_PINS
);
785 dev_err(&pdev
->dev
, "Failed to add pin range\n");
789 ret
= gpiochip_irqchip_add(&gpio_dev
->gc
,
795 dev_err(&pdev
->dev
, "could not add irqchip\n");
800 gpiochip_set_chained_irqchip(&gpio_dev
->gc
,
803 amd_gpio_irq_handler
);
805 platform_set_drvdata(pdev
, gpio_dev
);
807 dev_dbg(&pdev
->dev
, "amd gpio driver loaded\n");
811 gpiochip_remove(&gpio_dev
->gc
);
814 pinctrl_unregister(gpio_dev
->pctrl
);
818 static int amd_gpio_remove(struct platform_device
*pdev
)
820 struct amd_gpio
*gpio_dev
;
822 gpio_dev
= platform_get_drvdata(pdev
);
824 gpiochip_remove(&gpio_dev
->gc
);
825 pinctrl_unregister(gpio_dev
->pctrl
);
830 static const struct acpi_device_id amd_gpio_acpi_match
[] = {
834 MODULE_DEVICE_TABLE(acpi
, amd_gpio_acpi_match
);
836 static struct platform_driver amd_gpio_driver
= {
839 .acpi_match_table
= ACPI_PTR(amd_gpio_acpi_match
),
841 .probe
= amd_gpio_probe
,
842 .remove
= amd_gpio_remove
,
845 module_platform_driver(amd_gpio_driver
);
847 MODULE_LICENSE("GPL v2");
848 MODULE_AUTHOR("Ken Xue <Ken.Xue@amd.com>, Jeff Wu <Jeff.Wu@amd.com>");
849 MODULE_DESCRIPTION("AMD GPIO pinctrl driver");