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>
40 #include "pinctrl-utils.h"
41 #include "pinctrl-amd.h"
43 static int amd_gpio_direction_input(struct gpio_chip
*gc
, unsigned offset
)
47 struct amd_gpio
*gpio_dev
= gpiochip_get_data(gc
);
49 raw_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 raw_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
= gpiochip_get_data(gc
);
65 raw_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 raw_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
= gpiochip_get_data(gc
);
84 raw_spin_lock_irqsave(&gpio_dev
->lock
, flags
);
85 pin_reg
= readl(gpio_dev
->base
+ offset
* 4);
86 raw_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
= gpiochip_get_data(gc
);
97 raw_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 raw_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
= gpiochip_get_data(gc
);
116 raw_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 raw_spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
172 static int amd_gpio_set_config(struct gpio_chip
*gc
, unsigned offset
,
173 unsigned long config
)
177 if (pinconf_to_config_param(config
) != PIN_CONFIG_INPUT_DEBOUNCE
)
180 debounce
= pinconf_to_config_argument(config
);
181 return amd_gpio_set_debounce(gc
, offset
, debounce
);
184 #ifdef CONFIG_DEBUG_FS
185 static void amd_gpio_dbg_show(struct seq_file
*s
, struct gpio_chip
*gc
)
189 unsigned int bank
, i
, pin_num
;
190 struct amd_gpio
*gpio_dev
= gpiochip_get_data(gc
);
194 char *interrupt_enable
;
195 char *interrupt_mask
;
201 char *pull_up_enable
;
202 char *pull_down_enable
;
206 for (bank
= 0; bank
< gpio_dev
->hwbank_num
; bank
++) {
207 seq_printf(s
, "GPIO bank%d\t", bank
);
212 pin_num
= AMD_GPIO_PINS_BANK0
;
216 pin_num
= AMD_GPIO_PINS_BANK1
+ i
;
220 pin_num
= AMD_GPIO_PINS_BANK2
+ i
;
224 pin_num
= AMD_GPIO_PINS_BANK3
+ i
;
227 /* Illegal bank number, ignore */
230 for (; i
< pin_num
; i
++) {
231 seq_printf(s
, "pin%d\t", i
);
232 raw_spin_lock_irqsave(&gpio_dev
->lock
, flags
);
233 pin_reg
= readl(gpio_dev
->base
+ i
* 4);
234 raw_spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
236 if (pin_reg
& BIT(INTERRUPT_ENABLE_OFF
)) {
237 interrupt_enable
= "interrupt is enabled|";
239 if (!(pin_reg
& BIT(ACTIVE_LEVEL_OFF
)) &&
240 !(pin_reg
& BIT(ACTIVE_LEVEL_OFF
+ 1)))
241 active_level
= "Active low|";
242 else if (pin_reg
& BIT(ACTIVE_LEVEL_OFF
) &&
243 !(pin_reg
& BIT(ACTIVE_LEVEL_OFF
+ 1)))
244 active_level
= "Active high|";
245 else if (!(pin_reg
& BIT(ACTIVE_LEVEL_OFF
)) &&
246 pin_reg
& BIT(ACTIVE_LEVEL_OFF
+ 1))
247 active_level
= "Active on both|";
249 active_level
= "Unknown Active level|";
251 if (pin_reg
& BIT(LEVEL_TRIG_OFF
))
252 level_trig
= "Level trigger|";
254 level_trig
= "Edge trigger|";
258 "interrupt is disabled|";
263 if (pin_reg
& BIT(INTERRUPT_MASK_OFF
))
265 "interrupt is unmasked|";
268 "interrupt is masked|";
270 if (pin_reg
& BIT(WAKE_CNTRL_OFF_S0I3
))
271 wake_cntrl0
= "enable wakeup in S0i3 state|";
273 wake_cntrl0
= "disable wakeup in S0i3 state|";
275 if (pin_reg
& BIT(WAKE_CNTRL_OFF_S3
))
276 wake_cntrl1
= "enable wakeup in S3 state|";
278 wake_cntrl1
= "disable wakeup in S3 state|";
280 if (pin_reg
& BIT(WAKE_CNTRL_OFF_S4
))
281 wake_cntrl2
= "enable wakeup in S4/S5 state|";
283 wake_cntrl2
= "disable wakeup in S4/S5 state|";
285 if (pin_reg
& BIT(PULL_UP_ENABLE_OFF
)) {
286 pull_up_enable
= "pull-up is enabled|";
287 if (pin_reg
& BIT(PULL_UP_SEL_OFF
))
288 pull_up_sel
= "8k pull-up|";
290 pull_up_sel
= "4k pull-up|";
292 pull_up_enable
= "pull-up is disabled|";
296 if (pin_reg
& BIT(PULL_DOWN_ENABLE_OFF
))
297 pull_down_enable
= "pull-down is enabled|";
299 pull_down_enable
= "Pull-down is disabled|";
301 if (pin_reg
& BIT(OUTPUT_ENABLE_OFF
)) {
303 output_enable
= "output is enabled|";
304 if (pin_reg
& BIT(OUTPUT_VALUE_OFF
))
305 output_value
= "output is high|";
307 output_value
= "output is low|";
309 output_enable
= "output is disabled|";
312 if (pin_reg
& BIT(PIN_STS_OFF
))
313 pin_sts
= "input is high|";
315 pin_sts
= "input is low|";
318 seq_printf(s
, "%s %s %s %s %s %s\n"
319 " %s %s %s %s %s %s %s 0x%x\n",
320 level_trig
, active_level
, interrupt_enable
,
321 interrupt_mask
, wake_cntrl0
, wake_cntrl1
,
322 wake_cntrl2
, pin_sts
, pull_up_sel
,
323 pull_up_enable
, pull_down_enable
,
324 output_value
, output_enable
, pin_reg
);
329 #define amd_gpio_dbg_show NULL
332 static void amd_gpio_irq_enable(struct irq_data
*d
)
336 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
337 struct amd_gpio
*gpio_dev
= gpiochip_get_data(gc
);
339 raw_spin_lock_irqsave(&gpio_dev
->lock
, flags
);
340 pin_reg
= readl(gpio_dev
->base
+ (d
->hwirq
)*4);
341 pin_reg
|= BIT(INTERRUPT_ENABLE_OFF
);
342 pin_reg
|= BIT(INTERRUPT_MASK_OFF
);
343 writel(pin_reg
, gpio_dev
->base
+ (d
->hwirq
)*4);
344 raw_spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
347 static void amd_gpio_irq_disable(struct irq_data
*d
)
351 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
352 struct amd_gpio
*gpio_dev
= gpiochip_get_data(gc
);
354 raw_spin_lock_irqsave(&gpio_dev
->lock
, flags
);
355 pin_reg
= readl(gpio_dev
->base
+ (d
->hwirq
)*4);
356 pin_reg
&= ~BIT(INTERRUPT_ENABLE_OFF
);
357 pin_reg
&= ~BIT(INTERRUPT_MASK_OFF
);
358 writel(pin_reg
, gpio_dev
->base
+ (d
->hwirq
)*4);
359 raw_spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
362 static void amd_gpio_irq_mask(struct irq_data
*d
)
366 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
367 struct amd_gpio
*gpio_dev
= gpiochip_get_data(gc
);
369 raw_spin_lock_irqsave(&gpio_dev
->lock
, flags
);
370 pin_reg
= readl(gpio_dev
->base
+ (d
->hwirq
)*4);
371 pin_reg
&= ~BIT(INTERRUPT_MASK_OFF
);
372 writel(pin_reg
, gpio_dev
->base
+ (d
->hwirq
)*4);
373 raw_spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
376 static void amd_gpio_irq_unmask(struct irq_data
*d
)
380 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
381 struct amd_gpio
*gpio_dev
= gpiochip_get_data(gc
);
383 raw_spin_lock_irqsave(&gpio_dev
->lock
, flags
);
384 pin_reg
= readl(gpio_dev
->base
+ (d
->hwirq
)*4);
385 pin_reg
|= BIT(INTERRUPT_MASK_OFF
);
386 writel(pin_reg
, gpio_dev
->base
+ (d
->hwirq
)*4);
387 raw_spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
390 static void amd_gpio_irq_eoi(struct irq_data
*d
)
394 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
395 struct amd_gpio
*gpio_dev
= gpiochip_get_data(gc
);
397 raw_spin_lock_irqsave(&gpio_dev
->lock
, flags
);
398 reg
= readl(gpio_dev
->base
+ WAKE_INT_MASTER_REG
);
400 writel(reg
, gpio_dev
->base
+ WAKE_INT_MASTER_REG
);
401 raw_spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
404 static int amd_gpio_irq_set_type(struct irq_data
*d
, unsigned int type
)
408 unsigned long flags
, irq_flags
;
409 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
410 struct amd_gpio
*gpio_dev
= gpiochip_get_data(gc
);
412 raw_spin_lock_irqsave(&gpio_dev
->lock
, flags
);
413 pin_reg
= readl(gpio_dev
->base
+ (d
->hwirq
)*4);
415 /* Ignore the settings coming from the client and
416 * read the values from the ACPI tables
417 * while setting the trigger type
420 irq_flags
= irq_get_trigger_type(d
->irq
);
421 if (irq_flags
!= IRQ_TYPE_NONE
)
424 switch (type
& IRQ_TYPE_SENSE_MASK
) {
425 case IRQ_TYPE_EDGE_RISING
:
426 pin_reg
&= ~BIT(LEVEL_TRIG_OFF
);
427 pin_reg
&= ~(ACTIVE_LEVEL_MASK
<< ACTIVE_LEVEL_OFF
);
428 pin_reg
|= ACTIVE_HIGH
<< ACTIVE_LEVEL_OFF
;
429 pin_reg
|= DB_TYPE_REMOVE_GLITCH
<< DB_CNTRL_OFF
;
430 irq_set_handler_locked(d
, handle_edge_irq
);
433 case IRQ_TYPE_EDGE_FALLING
:
434 pin_reg
&= ~BIT(LEVEL_TRIG_OFF
);
435 pin_reg
&= ~(ACTIVE_LEVEL_MASK
<< ACTIVE_LEVEL_OFF
);
436 pin_reg
|= ACTIVE_LOW
<< ACTIVE_LEVEL_OFF
;
437 pin_reg
|= DB_TYPE_REMOVE_GLITCH
<< DB_CNTRL_OFF
;
438 irq_set_handler_locked(d
, handle_edge_irq
);
441 case IRQ_TYPE_EDGE_BOTH
:
442 pin_reg
&= ~BIT(LEVEL_TRIG_OFF
);
443 pin_reg
&= ~(ACTIVE_LEVEL_MASK
<< ACTIVE_LEVEL_OFF
);
444 pin_reg
|= BOTH_EADGE
<< ACTIVE_LEVEL_OFF
;
445 pin_reg
|= DB_TYPE_REMOVE_GLITCH
<< DB_CNTRL_OFF
;
446 irq_set_handler_locked(d
, handle_edge_irq
);
449 case IRQ_TYPE_LEVEL_HIGH
:
450 pin_reg
|= LEVEL_TRIGGER
<< LEVEL_TRIG_OFF
;
451 pin_reg
&= ~(ACTIVE_LEVEL_MASK
<< ACTIVE_LEVEL_OFF
);
452 pin_reg
|= ACTIVE_HIGH
<< ACTIVE_LEVEL_OFF
;
453 pin_reg
&= ~(DB_CNTRl_MASK
<< DB_CNTRL_OFF
);
454 pin_reg
|= DB_TYPE_PRESERVE_LOW_GLITCH
<< DB_CNTRL_OFF
;
455 irq_set_handler_locked(d
, handle_level_irq
);
458 case IRQ_TYPE_LEVEL_LOW
:
459 pin_reg
|= LEVEL_TRIGGER
<< LEVEL_TRIG_OFF
;
460 pin_reg
&= ~(ACTIVE_LEVEL_MASK
<< ACTIVE_LEVEL_OFF
);
461 pin_reg
|= ACTIVE_LOW
<< ACTIVE_LEVEL_OFF
;
462 pin_reg
&= ~(DB_CNTRl_MASK
<< DB_CNTRL_OFF
);
463 pin_reg
|= DB_TYPE_PRESERVE_HIGH_GLITCH
<< DB_CNTRL_OFF
;
464 irq_set_handler_locked(d
, handle_level_irq
);
471 dev_err(&gpio_dev
->pdev
->dev
, "Invalid type value\n");
475 pin_reg
|= CLR_INTR_STAT
<< INTERRUPT_STS_OFF
;
476 writel(pin_reg
, gpio_dev
->base
+ (d
->hwirq
)*4);
477 raw_spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
482 static void amd_irq_ack(struct irq_data
*d
)
485 * based on HW design,there is no need to ack HW
486 * before handle current irq. But this routine is
487 * necessary for handle_edge_irq
491 static struct irq_chip amd_gpio_irqchip
= {
493 .irq_ack
= amd_irq_ack
,
494 .irq_enable
= amd_gpio_irq_enable
,
495 .irq_disable
= amd_gpio_irq_disable
,
496 .irq_mask
= amd_gpio_irq_mask
,
497 .irq_unmask
= amd_gpio_irq_unmask
,
498 .irq_eoi
= amd_gpio_irq_eoi
,
499 .irq_set_type
= amd_gpio_irq_set_type
,
500 .flags
= IRQCHIP_SKIP_SET_WAKE
,
503 #define PIN_IRQ_PENDING (BIT(INTERRUPT_STS_OFF) | BIT(WAKE_STS_OFF))
505 static irqreturn_t
amd_gpio_irq_handler(int irq
, void *dev_id
)
507 struct amd_gpio
*gpio_dev
= dev_id
;
508 struct gpio_chip
*gc
= &gpio_dev
->gc
;
509 irqreturn_t ret
= IRQ_NONE
;
510 unsigned int i
, irqnr
;
515 /* Read the wake status */
516 raw_spin_lock_irqsave(&gpio_dev
->lock
, flags
);
517 status
= readl(gpio_dev
->base
+ WAKE_INT_STATUS_REG1
);
519 status
|= readl(gpio_dev
->base
+ WAKE_INT_STATUS_REG0
);
520 raw_spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
522 /* Bit 0-45 contain the relevant status bits */
523 status
&= (1ULL << 46) - 1;
524 regs
= gpio_dev
->base
;
525 for (mask
= 1, irqnr
= 0; status
; mask
<<= 1, regs
+= 4, irqnr
+= 4) {
526 if (!(status
& mask
))
530 /* Each status bit covers four pins */
531 for (i
= 0; i
< 4; i
++) {
532 regval
= readl(regs
+ i
);
533 if (!(regval
& PIN_IRQ_PENDING
))
535 irq
= irq_find_mapping(gc
->irq
.domain
, irqnr
+ i
);
536 generic_handle_irq(irq
);
539 * We must read the pin register again, in case the
540 * value was changed while executing
541 * generic_handle_irq() above.
543 raw_spin_lock_irqsave(&gpio_dev
->lock
, flags
);
544 regval
= readl(regs
+ i
);
545 writel(regval
, regs
+ i
);
546 raw_spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
551 /* Signal EOI to the GPIO unit */
552 raw_spin_lock_irqsave(&gpio_dev
->lock
, flags
);
553 regval
= readl(gpio_dev
->base
+ WAKE_INT_MASTER_REG
);
555 writel(regval
, gpio_dev
->base
+ WAKE_INT_MASTER_REG
);
556 raw_spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
561 static int amd_get_groups_count(struct pinctrl_dev
*pctldev
)
563 struct amd_gpio
*gpio_dev
= pinctrl_dev_get_drvdata(pctldev
);
565 return gpio_dev
->ngroups
;
568 static const char *amd_get_group_name(struct pinctrl_dev
*pctldev
,
571 struct amd_gpio
*gpio_dev
= pinctrl_dev_get_drvdata(pctldev
);
573 return gpio_dev
->groups
[group
].name
;
576 static int amd_get_group_pins(struct pinctrl_dev
*pctldev
,
578 const unsigned **pins
,
581 struct amd_gpio
*gpio_dev
= pinctrl_dev_get_drvdata(pctldev
);
583 *pins
= gpio_dev
->groups
[group
].pins
;
584 *num_pins
= gpio_dev
->groups
[group
].npins
;
588 static const struct pinctrl_ops amd_pinctrl_ops
= {
589 .get_groups_count
= amd_get_groups_count
,
590 .get_group_name
= amd_get_group_name
,
591 .get_group_pins
= amd_get_group_pins
,
593 .dt_node_to_map
= pinconf_generic_dt_node_to_map_group
,
594 .dt_free_map
= pinctrl_utils_free_map
,
598 static int amd_pinconf_get(struct pinctrl_dev
*pctldev
,
600 unsigned long *config
)
605 struct amd_gpio
*gpio_dev
= pinctrl_dev_get_drvdata(pctldev
);
606 enum pin_config_param param
= pinconf_to_config_param(*config
);
608 raw_spin_lock_irqsave(&gpio_dev
->lock
, flags
);
609 pin_reg
= readl(gpio_dev
->base
+ pin
*4);
610 raw_spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
612 case PIN_CONFIG_INPUT_DEBOUNCE
:
613 arg
= pin_reg
& DB_TMR_OUT_MASK
;
616 case PIN_CONFIG_BIAS_PULL_DOWN
:
617 arg
= (pin_reg
>> PULL_DOWN_ENABLE_OFF
) & BIT(0);
620 case PIN_CONFIG_BIAS_PULL_UP
:
621 arg
= (pin_reg
>> PULL_UP_SEL_OFF
) & (BIT(0) | BIT(1));
624 case PIN_CONFIG_DRIVE_STRENGTH
:
625 arg
= (pin_reg
>> DRV_STRENGTH_SEL_OFF
) & DRV_STRENGTH_SEL_MASK
;
629 dev_err(&gpio_dev
->pdev
->dev
, "Invalid config param %04x\n",
634 *config
= pinconf_to_config_packed(param
, arg
);
639 static int amd_pinconf_set(struct pinctrl_dev
*pctldev
, unsigned int pin
,
640 unsigned long *configs
, unsigned num_configs
)
647 enum pin_config_param param
;
648 struct amd_gpio
*gpio_dev
= pinctrl_dev_get_drvdata(pctldev
);
650 raw_spin_lock_irqsave(&gpio_dev
->lock
, flags
);
651 for (i
= 0; i
< num_configs
; i
++) {
652 param
= pinconf_to_config_param(configs
[i
]);
653 arg
= pinconf_to_config_argument(configs
[i
]);
654 pin_reg
= readl(gpio_dev
->base
+ pin
*4);
657 case PIN_CONFIG_INPUT_DEBOUNCE
:
658 pin_reg
&= ~DB_TMR_OUT_MASK
;
659 pin_reg
|= arg
& DB_TMR_OUT_MASK
;
662 case PIN_CONFIG_BIAS_PULL_DOWN
:
663 pin_reg
&= ~BIT(PULL_DOWN_ENABLE_OFF
);
664 pin_reg
|= (arg
& BIT(0)) << PULL_DOWN_ENABLE_OFF
;
667 case PIN_CONFIG_BIAS_PULL_UP
:
668 pin_reg
&= ~BIT(PULL_UP_SEL_OFF
);
669 pin_reg
|= (arg
& BIT(0)) << PULL_UP_SEL_OFF
;
670 pin_reg
&= ~BIT(PULL_UP_ENABLE_OFF
);
671 pin_reg
|= ((arg
>>1) & BIT(0)) << PULL_UP_ENABLE_OFF
;
674 case PIN_CONFIG_DRIVE_STRENGTH
:
675 pin_reg
&= ~(DRV_STRENGTH_SEL_MASK
676 << DRV_STRENGTH_SEL_OFF
);
677 pin_reg
|= (arg
& DRV_STRENGTH_SEL_MASK
)
678 << DRV_STRENGTH_SEL_OFF
;
682 dev_err(&gpio_dev
->pdev
->dev
,
683 "Invalid config param %04x\n", param
);
687 writel(pin_reg
, gpio_dev
->base
+ pin
*4);
689 raw_spin_unlock_irqrestore(&gpio_dev
->lock
, flags
);
694 static int amd_pinconf_group_get(struct pinctrl_dev
*pctldev
,
696 unsigned long *config
)
698 const unsigned *pins
;
702 ret
= amd_get_group_pins(pctldev
, group
, &pins
, &npins
);
706 if (amd_pinconf_get(pctldev
, pins
[0], config
))
712 static int amd_pinconf_group_set(struct pinctrl_dev
*pctldev
,
713 unsigned group
, unsigned long *configs
,
714 unsigned num_configs
)
716 const unsigned *pins
;
720 ret
= amd_get_group_pins(pctldev
, group
, &pins
, &npins
);
723 for (i
= 0; i
< npins
; i
++) {
724 if (amd_pinconf_set(pctldev
, pins
[i
], configs
, num_configs
))
730 static const struct pinconf_ops amd_pinconf_ops
= {
731 .pin_config_get
= amd_pinconf_get
,
732 .pin_config_set
= amd_pinconf_set
,
733 .pin_config_group_get
= amd_pinconf_group_get
,
734 .pin_config_group_set
= amd_pinconf_group_set
,
737 #ifdef CONFIG_PM_SLEEP
738 static bool amd_gpio_should_save(struct amd_gpio
*gpio_dev
, unsigned int pin
)
740 const struct pin_desc
*pd
= pin_desc_get(gpio_dev
->pctrl
, pin
);
746 * Only restore the pin if it is actually in use by the kernel (or
749 if (pd
->mux_owner
|| pd
->gpio_owner
||
750 gpiochip_line_is_irq(&gpio_dev
->gc
, pin
))
756 static int amd_gpio_suspend(struct device
*dev
)
758 struct platform_device
*pdev
= to_platform_device(dev
);
759 struct amd_gpio
*gpio_dev
= platform_get_drvdata(pdev
);
760 struct pinctrl_desc
*desc
= gpio_dev
->pctrl
->desc
;
763 for (i
= 0; i
< desc
->npins
; i
++) {
764 int pin
= desc
->pins
[i
].number
;
766 if (!amd_gpio_should_save(gpio_dev
, pin
))
769 gpio_dev
->saved_regs
[i
] = readl(gpio_dev
->base
+ pin
*4);
775 static int amd_gpio_resume(struct device
*dev
)
777 struct platform_device
*pdev
= to_platform_device(dev
);
778 struct amd_gpio
*gpio_dev
= platform_get_drvdata(pdev
);
779 struct pinctrl_desc
*desc
= gpio_dev
->pctrl
->desc
;
782 for (i
= 0; i
< desc
->npins
; i
++) {
783 int pin
= desc
->pins
[i
].number
;
785 if (!amd_gpio_should_save(gpio_dev
, pin
))
788 writel(gpio_dev
->saved_regs
[i
], gpio_dev
->base
+ pin
*4);
794 static const struct dev_pm_ops amd_gpio_pm_ops
= {
795 SET_LATE_SYSTEM_SLEEP_PM_OPS(amd_gpio_suspend
,
800 static struct pinctrl_desc amd_pinctrl_desc
= {
802 .npins
= ARRAY_SIZE(kerncz_pins
),
803 .pctlops
= &amd_pinctrl_ops
,
804 .confops
= &amd_pinconf_ops
,
805 .owner
= THIS_MODULE
,
808 static int amd_gpio_probe(struct platform_device
*pdev
)
812 struct resource
*res
;
813 struct amd_gpio
*gpio_dev
;
815 gpio_dev
= devm_kzalloc(&pdev
->dev
,
816 sizeof(struct amd_gpio
), GFP_KERNEL
);
820 raw_spin_lock_init(&gpio_dev
->lock
);
822 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
824 dev_err(&pdev
->dev
, "Failed to get gpio io resource.\n");
828 gpio_dev
->base
= devm_ioremap_nocache(&pdev
->dev
, res
->start
,
833 irq_base
= platform_get_irq(pdev
, 0);
835 dev_err(&pdev
->dev
, "Failed to get gpio IRQ: %d\n", irq_base
);
839 #ifdef CONFIG_PM_SLEEP
840 gpio_dev
->saved_regs
= devm_kcalloc(&pdev
->dev
, amd_pinctrl_desc
.npins
,
841 sizeof(*gpio_dev
->saved_regs
),
843 if (!gpio_dev
->saved_regs
)
847 gpio_dev
->pdev
= pdev
;
848 gpio_dev
->gc
.direction_input
= amd_gpio_direction_input
;
849 gpio_dev
->gc
.direction_output
= amd_gpio_direction_output
;
850 gpio_dev
->gc
.get
= amd_gpio_get_value
;
851 gpio_dev
->gc
.set
= amd_gpio_set_value
;
852 gpio_dev
->gc
.set_config
= amd_gpio_set_config
;
853 gpio_dev
->gc
.dbg_show
= amd_gpio_dbg_show
;
855 gpio_dev
->gc
.base
= -1;
856 gpio_dev
->gc
.label
= pdev
->name
;
857 gpio_dev
->gc
.owner
= THIS_MODULE
;
858 gpio_dev
->gc
.parent
= &pdev
->dev
;
859 gpio_dev
->gc
.ngpio
= resource_size(res
) / 4;
860 #if defined(CONFIG_OF_GPIO)
861 gpio_dev
->gc
.of_node
= pdev
->dev
.of_node
;
864 gpio_dev
->hwbank_num
= gpio_dev
->gc
.ngpio
/ 64;
865 gpio_dev
->groups
= kerncz_groups
;
866 gpio_dev
->ngroups
= ARRAY_SIZE(kerncz_groups
);
868 amd_pinctrl_desc
.name
= dev_name(&pdev
->dev
);
869 gpio_dev
->pctrl
= devm_pinctrl_register(&pdev
->dev
, &amd_pinctrl_desc
,
871 if (IS_ERR(gpio_dev
->pctrl
)) {
872 dev_err(&pdev
->dev
, "Couldn't register pinctrl driver\n");
873 return PTR_ERR(gpio_dev
->pctrl
);
876 ret
= gpiochip_add_data(&gpio_dev
->gc
, gpio_dev
);
880 ret
= gpiochip_add_pin_range(&gpio_dev
->gc
, dev_name(&pdev
->dev
),
881 0, 0, gpio_dev
->gc
.ngpio
);
883 dev_err(&pdev
->dev
, "Failed to add pin range\n");
887 ret
= gpiochip_irqchip_add(&gpio_dev
->gc
,
893 dev_err(&pdev
->dev
, "could not add irqchip\n");
898 ret
= devm_request_irq(&pdev
->dev
, irq_base
, amd_gpio_irq_handler
, 0,
899 KBUILD_MODNAME
, gpio_dev
);
903 platform_set_drvdata(pdev
, gpio_dev
);
905 dev_dbg(&pdev
->dev
, "amd gpio driver loaded\n");
909 gpiochip_remove(&gpio_dev
->gc
);
914 static int amd_gpio_remove(struct platform_device
*pdev
)
916 struct amd_gpio
*gpio_dev
;
918 gpio_dev
= platform_get_drvdata(pdev
);
920 gpiochip_remove(&gpio_dev
->gc
);
925 static const struct acpi_device_id amd_gpio_acpi_match
[] = {
930 MODULE_DEVICE_TABLE(acpi
, amd_gpio_acpi_match
);
932 static struct platform_driver amd_gpio_driver
= {
935 .acpi_match_table
= ACPI_PTR(amd_gpio_acpi_match
),
936 #ifdef CONFIG_PM_SLEEP
937 .pm
= &amd_gpio_pm_ops
,
940 .probe
= amd_gpio_probe
,
941 .remove
= amd_gpio_remove
,
944 module_platform_driver(amd_gpio_driver
);
946 MODULE_LICENSE("GPL v2");
947 MODULE_AUTHOR("Ken Xue <Ken.Xue@amd.com>, Jeff Wu <Jeff.Wu@amd.com>");
948 MODULE_DESCRIPTION("AMD GPIO pinctrl driver");