pinctrl: amd: Fix mistake in handling clearing pins at startup
[platform/kernel/linux-starfive.git] / drivers / pinctrl / pinctrl-amd.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * GPIO driver for AMD
4  *
5  * Copyright (c) 2014,2015 AMD Corporation.
6  * Authors: Ken Xue <Ken.Xue@amd.com>
7  *      Wu, Jeff <Jeff.Wu@amd.com>
8  *
9  */
10
11 #include <linux/err.h>
12 #include <linux/bug.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/spinlock.h>
16 #include <linux/compiler.h>
17 #include <linux/types.h>
18 #include <linux/errno.h>
19 #include <linux/log2.h>
20 #include <linux/io.h>
21 #include <linux/gpio/driver.h>
22 #include <linux/slab.h>
23 #include <linux/platform_device.h>
24 #include <linux/mutex.h>
25 #include <linux/acpi.h>
26 #include <linux/seq_file.h>
27 #include <linux/interrupt.h>
28 #include <linux/list.h>
29 #include <linux/bitops.h>
30 #include <linux/pinctrl/pinconf.h>
31 #include <linux/pinctrl/pinconf-generic.h>
32 #include <linux/pinctrl/pinmux.h>
33
34 #include "core.h"
35 #include "pinctrl-utils.h"
36 #include "pinctrl-amd.h"
37
38 static int amd_gpio_get_direction(struct gpio_chip *gc, unsigned offset)
39 {
40         unsigned long flags;
41         u32 pin_reg;
42         struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
43
44         raw_spin_lock_irqsave(&gpio_dev->lock, flags);
45         pin_reg = readl(gpio_dev->base + offset * 4);
46         raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
47
48         if (pin_reg & BIT(OUTPUT_ENABLE_OFF))
49                 return GPIO_LINE_DIRECTION_OUT;
50
51         return GPIO_LINE_DIRECTION_IN;
52 }
53
54 static int amd_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
55 {
56         unsigned long flags;
57         u32 pin_reg;
58         struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
59
60         raw_spin_lock_irqsave(&gpio_dev->lock, flags);
61         pin_reg = readl(gpio_dev->base + offset * 4);
62         pin_reg &= ~BIT(OUTPUT_ENABLE_OFF);
63         writel(pin_reg, gpio_dev->base + offset * 4);
64         raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
65
66         return 0;
67 }
68
69 static int amd_gpio_direction_output(struct gpio_chip *gc, unsigned offset,
70                 int value)
71 {
72         u32 pin_reg;
73         unsigned long flags;
74         struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
75
76         raw_spin_lock_irqsave(&gpio_dev->lock, flags);
77         pin_reg = readl(gpio_dev->base + offset * 4);
78         pin_reg |= BIT(OUTPUT_ENABLE_OFF);
79         if (value)
80                 pin_reg |= BIT(OUTPUT_VALUE_OFF);
81         else
82                 pin_reg &= ~BIT(OUTPUT_VALUE_OFF);
83         writel(pin_reg, gpio_dev->base + offset * 4);
84         raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
85
86         return 0;
87 }
88
89 static int amd_gpio_get_value(struct gpio_chip *gc, unsigned offset)
90 {
91         u32 pin_reg;
92         unsigned long flags;
93         struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
94
95         raw_spin_lock_irqsave(&gpio_dev->lock, flags);
96         pin_reg = readl(gpio_dev->base + offset * 4);
97         raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
98
99         return !!(pin_reg & BIT(PIN_STS_OFF));
100 }
101
102 static void amd_gpio_set_value(struct gpio_chip *gc, unsigned offset, int value)
103 {
104         u32 pin_reg;
105         unsigned long flags;
106         struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
107
108         raw_spin_lock_irqsave(&gpio_dev->lock, flags);
109         pin_reg = readl(gpio_dev->base + offset * 4);
110         if (value)
111                 pin_reg |= BIT(OUTPUT_VALUE_OFF);
112         else
113                 pin_reg &= ~BIT(OUTPUT_VALUE_OFF);
114         writel(pin_reg, gpio_dev->base + offset * 4);
115         raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
116 }
117
118 static int amd_gpio_set_debounce(struct gpio_chip *gc, unsigned offset,
119                 unsigned debounce)
120 {
121         u32 time;
122         u32 pin_reg;
123         int ret = 0;
124         unsigned long flags;
125         struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
126
127         raw_spin_lock_irqsave(&gpio_dev->lock, flags);
128
129         /* Use special handling for Pin0 debounce */
130         pin_reg = readl(gpio_dev->base + WAKE_INT_MASTER_REG);
131         if (pin_reg & INTERNAL_GPIO0_DEBOUNCE)
132                 debounce = 0;
133
134         pin_reg = readl(gpio_dev->base + offset * 4);
135
136         if (debounce) {
137                 pin_reg |= DB_TYPE_REMOVE_GLITCH << DB_CNTRL_OFF;
138                 pin_reg &= ~DB_TMR_OUT_MASK;
139                 /*
140                 Debounce        Debounce        Timer   Max
141                 TmrLarge        TmrOutUnit      Unit    Debounce
142                                                         Time
143                 0       0       61 usec (2 RtcClk)      976 usec
144                 0       1       244 usec (8 RtcClk)     3.9 msec
145                 1       0       15.6 msec (512 RtcClk)  250 msec
146                 1       1       62.5 msec (2048 RtcClk) 1 sec
147                 */
148
149                 if (debounce < 61) {
150                         pin_reg |= 1;
151                         pin_reg &= ~BIT(DB_TMR_OUT_UNIT_OFF);
152                         pin_reg &= ~BIT(DB_TMR_LARGE_OFF);
153                 } else if (debounce < 976) {
154                         time = debounce / 61;
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 < 3900) {
159                         time = debounce / 244;
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 < 250000) {
164                         time = debounce / 15625;
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);
168                 } else if (debounce < 1000000) {
169                         time = debounce / 62500;
170                         pin_reg |= time & DB_TMR_OUT_MASK;
171                         pin_reg |= BIT(DB_TMR_OUT_UNIT_OFF);
172                         pin_reg |= BIT(DB_TMR_LARGE_OFF);
173                 } else {
174                         pin_reg &= ~(DB_CNTRl_MASK << DB_CNTRL_OFF);
175                         ret = -EINVAL;
176                 }
177         } else {
178                 pin_reg &= ~BIT(DB_TMR_OUT_UNIT_OFF);
179                 pin_reg &= ~BIT(DB_TMR_LARGE_OFF);
180                 pin_reg &= ~DB_TMR_OUT_MASK;
181                 pin_reg &= ~(DB_CNTRl_MASK << DB_CNTRL_OFF);
182         }
183         writel(pin_reg, gpio_dev->base + offset * 4);
184         raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
185
186         return ret;
187 }
188
189 static int amd_gpio_set_config(struct gpio_chip *gc, unsigned offset,
190                                unsigned long config)
191 {
192         u32 debounce;
193
194         if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE)
195                 return -ENOTSUPP;
196
197         debounce = pinconf_to_config_argument(config);
198         return amd_gpio_set_debounce(gc, offset, debounce);
199 }
200
201 #ifdef CONFIG_DEBUG_FS
202 static void amd_gpio_dbg_show(struct seq_file *s, struct gpio_chip *gc)
203 {
204         u32 pin_reg;
205         u32 db_cntrl;
206         unsigned long flags;
207         unsigned int bank, i, pin_num;
208         struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
209
210         bool tmr_out_unit;
211         bool tmr_large;
212
213         char *level_trig;
214         char *active_level;
215         char *interrupt_mask;
216         char *wake_cntrl0;
217         char *wake_cntrl1;
218         char *wake_cntrl2;
219         char *pin_sts;
220         char *interrupt_sts;
221         char *wake_sts;
222         char *pull_up_sel;
223         char *orientation;
224         char debounce_value[40];
225         char *debounce_enable;
226         char *wake_cntrlz;
227
228         seq_printf(s, "WAKE_INT_MASTER_REG: 0x%08x\n", readl(gpio_dev->base + WAKE_INT_MASTER_REG));
229         for (bank = 0; bank < gpio_dev->hwbank_num; bank++) {
230                 unsigned int time = 0;
231                 unsigned int unit = 0;
232
233                 switch (bank) {
234                 case 0:
235                         i = 0;
236                         pin_num = AMD_GPIO_PINS_BANK0;
237                         break;
238                 case 1:
239                         i = 64;
240                         pin_num = AMD_GPIO_PINS_BANK1 + i;
241                         break;
242                 case 2:
243                         i = 128;
244                         pin_num = AMD_GPIO_PINS_BANK2 + i;
245                         break;
246                 case 3:
247                         i = 192;
248                         pin_num = AMD_GPIO_PINS_BANK3 + i;
249                         break;
250                 default:
251                         /* Illegal bank number, ignore */
252                         continue;
253                 }
254                 seq_printf(s, "GPIO bank%d\n", bank);
255                 seq_puts(s, "gpio\t  int|active|trigger|S0i3| S3|S4/S5| Z|wake|pull|  orient|       debounce|reg\n");
256                 for (; i < pin_num; i++) {
257                         seq_printf(s, "#%d\t", i);
258                         raw_spin_lock_irqsave(&gpio_dev->lock, flags);
259                         pin_reg = readl(gpio_dev->base + i * 4);
260                         raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
261
262                         if (pin_reg & BIT(INTERRUPT_ENABLE_OFF)) {
263                                 u8 level = (pin_reg >> ACTIVE_LEVEL_OFF) &
264                                                 ACTIVE_LEVEL_MASK;
265
266                                 if (level == ACTIVE_LEVEL_HIGH)
267                                         active_level = "↑";
268                                 else if (level == ACTIVE_LEVEL_LOW)
269                                         active_level = "↓";
270                                 else if (!(pin_reg & BIT(LEVEL_TRIG_OFF)) &&
271                                          level == ACTIVE_LEVEL_BOTH)
272                                         active_level = "b";
273                                 else
274                                         active_level = "?";
275
276                                 if (pin_reg & BIT(LEVEL_TRIG_OFF))
277                                         level_trig = "level";
278                                 else
279                                         level_trig = " edge";
280
281                                 if (pin_reg & BIT(INTERRUPT_MASK_OFF))
282                                         interrupt_mask = "😛";
283                                 else
284                                         interrupt_mask = "😷";
285
286                                 if (pin_reg & BIT(INTERRUPT_STS_OFF))
287                                         interrupt_sts = "🔥";
288                                 else
289                                         interrupt_sts = "  ";
290
291                                 seq_printf(s, "%s %s|     %s|  %s|",
292                                    interrupt_sts,
293                                    interrupt_mask,
294                                    active_level,
295                                    level_trig);
296                         } else
297                                 seq_puts(s, "    ∅|      |       |");
298
299                         if (pin_reg & BIT(WAKE_CNTRL_OFF_S0I3))
300                                 wake_cntrl0 = "⏰";
301                         else
302                                 wake_cntrl0 = "  ";
303                         seq_printf(s, "  %s| ", wake_cntrl0);
304
305                         if (pin_reg & BIT(WAKE_CNTRL_OFF_S3))
306                                 wake_cntrl1 = "⏰";
307                         else
308                                 wake_cntrl1 = "  ";
309                         seq_printf(s, "%s|", wake_cntrl1);
310
311                         if (pin_reg & BIT(WAKE_CNTRL_OFF_S4))
312                                 wake_cntrl2 = "⏰";
313                         else
314                                 wake_cntrl2 = "  ";
315                         seq_printf(s, "   %s|", wake_cntrl2);
316
317                         if (pin_reg & BIT(WAKECNTRL_Z_OFF))
318                                 wake_cntrlz = "⏰";
319                         else
320                                 wake_cntrlz = "  ";
321                         seq_printf(s, "%s|", wake_cntrlz);
322
323                         if (pin_reg & BIT(WAKE_STS_OFF))
324                                 wake_sts = "🔥";
325                         else
326                                 wake_sts = " ";
327                         seq_printf(s, "   %s|", wake_sts);
328
329                         if (pin_reg & BIT(PULL_UP_ENABLE_OFF)) {
330                                 if (pin_reg & BIT(PULL_UP_SEL_OFF))
331                                         pull_up_sel = "8k";
332                                 else
333                                         pull_up_sel = "4k";
334                                 seq_printf(s, "%s ↑|",
335                                            pull_up_sel);
336                         } else if (pin_reg & BIT(PULL_DOWN_ENABLE_OFF)) {
337                                 seq_puts(s, "   ↓|");
338                         } else  {
339                                 seq_puts(s, "    |");
340                         }
341
342                         if (pin_reg & BIT(OUTPUT_ENABLE_OFF)) {
343                                 pin_sts = "output";
344                                 if (pin_reg & BIT(OUTPUT_VALUE_OFF))
345                                         orientation = "↑";
346                                 else
347                                         orientation = "↓";
348                         } else {
349                                 pin_sts = "input ";
350                                 if (pin_reg & BIT(PIN_STS_OFF))
351                                         orientation = "↑";
352                                 else
353                                         orientation = "↓";
354                         }
355                         seq_printf(s, "%s %s|", pin_sts, orientation);
356
357                         db_cntrl = (DB_CNTRl_MASK << DB_CNTRL_OFF) & pin_reg;
358                         if (db_cntrl) {
359                                 tmr_out_unit = pin_reg & BIT(DB_TMR_OUT_UNIT_OFF);
360                                 tmr_large = pin_reg & BIT(DB_TMR_LARGE_OFF);
361                                 time = pin_reg & DB_TMR_OUT_MASK;
362                                 if (tmr_large) {
363                                         if (tmr_out_unit)
364                                                 unit = 62500;
365                                         else
366                                                 unit = 15625;
367                                 } else {
368                                         if (tmr_out_unit)
369                                                 unit = 244;
370                                         else
371                                                 unit = 61;
372                                 }
373                                 if ((DB_TYPE_REMOVE_GLITCH << DB_CNTRL_OFF) == db_cntrl)
374                                         debounce_enable = "b";
375                                 else if ((DB_TYPE_PRESERVE_LOW_GLITCH << DB_CNTRL_OFF) == db_cntrl)
376                                         debounce_enable = "↓";
377                                 else
378                                         debounce_enable = "↑";
379                                 snprintf(debounce_value, sizeof(debounce_value), "%06u", time * unit);
380                                 seq_printf(s, "%s (🕑 %sus)|", debounce_enable, debounce_value);
381                         } else {
382                                 seq_puts(s, "               |");
383                         }
384                         seq_printf(s, "0x%x\n", pin_reg);
385                 }
386         }
387 }
388 #else
389 #define amd_gpio_dbg_show NULL
390 #endif
391
392 static void amd_gpio_irq_enable(struct irq_data *d)
393 {
394         u32 pin_reg;
395         unsigned long flags;
396         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
397         struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
398
399         gpiochip_enable_irq(gc, d->hwirq);
400
401         raw_spin_lock_irqsave(&gpio_dev->lock, flags);
402         pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
403         pin_reg |= BIT(INTERRUPT_ENABLE_OFF);
404         pin_reg |= BIT(INTERRUPT_MASK_OFF);
405         writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
406         raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
407 }
408
409 static void amd_gpio_irq_disable(struct irq_data *d)
410 {
411         u32 pin_reg;
412         unsigned long flags;
413         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
414         struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
415
416         raw_spin_lock_irqsave(&gpio_dev->lock, flags);
417         pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
418         pin_reg &= ~BIT(INTERRUPT_ENABLE_OFF);
419         pin_reg &= ~BIT(INTERRUPT_MASK_OFF);
420         writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
421         raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
422
423         gpiochip_disable_irq(gc, d->hwirq);
424 }
425
426 static void amd_gpio_irq_mask(struct irq_data *d)
427 {
428         u32 pin_reg;
429         unsigned long flags;
430         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
431         struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
432
433         raw_spin_lock_irqsave(&gpio_dev->lock, flags);
434         pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
435         pin_reg &= ~BIT(INTERRUPT_MASK_OFF);
436         writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
437         raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
438 }
439
440 static void amd_gpio_irq_unmask(struct irq_data *d)
441 {
442         u32 pin_reg;
443         unsigned long flags;
444         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
445         struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
446
447         raw_spin_lock_irqsave(&gpio_dev->lock, flags);
448         pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
449         pin_reg |= BIT(INTERRUPT_MASK_OFF);
450         writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
451         raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
452 }
453
454 static int amd_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
455 {
456         u32 pin_reg;
457         unsigned long flags;
458         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
459         struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
460         u32 wake_mask = BIT(WAKE_CNTRL_OFF_S0I3) | BIT(WAKE_CNTRL_OFF_S3);
461         int err;
462
463         raw_spin_lock_irqsave(&gpio_dev->lock, flags);
464         pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
465
466         if (on)
467                 pin_reg |= wake_mask;
468         else
469                 pin_reg &= ~wake_mask;
470
471         writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
472         raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
473
474         if (on)
475                 err = enable_irq_wake(gpio_dev->irq);
476         else
477                 err = disable_irq_wake(gpio_dev->irq);
478
479         if (err)
480                 dev_err(&gpio_dev->pdev->dev, "failed to %s wake-up interrupt\n",
481                         on ? "enable" : "disable");
482
483         return 0;
484 }
485
486 static void amd_gpio_irq_eoi(struct irq_data *d)
487 {
488         u32 reg;
489         unsigned long flags;
490         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
491         struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
492
493         raw_spin_lock_irqsave(&gpio_dev->lock, flags);
494         reg = readl(gpio_dev->base + WAKE_INT_MASTER_REG);
495         reg |= EOI_MASK;
496         writel(reg, gpio_dev->base + WAKE_INT_MASTER_REG);
497         raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
498 }
499
500 static int amd_gpio_irq_set_type(struct irq_data *d, unsigned int type)
501 {
502         int ret = 0;
503         u32 pin_reg, pin_reg_irq_en, mask;
504         unsigned long flags;
505         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
506         struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
507
508         raw_spin_lock_irqsave(&gpio_dev->lock, flags);
509         pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
510
511         switch (type & IRQ_TYPE_SENSE_MASK) {
512         case IRQ_TYPE_EDGE_RISING:
513                 pin_reg &= ~BIT(LEVEL_TRIG_OFF);
514                 pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
515                 pin_reg |= ACTIVE_HIGH << ACTIVE_LEVEL_OFF;
516                 irq_set_handler_locked(d, handle_edge_irq);
517                 break;
518
519         case IRQ_TYPE_EDGE_FALLING:
520                 pin_reg &= ~BIT(LEVEL_TRIG_OFF);
521                 pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
522                 pin_reg |= ACTIVE_LOW << ACTIVE_LEVEL_OFF;
523                 irq_set_handler_locked(d, handle_edge_irq);
524                 break;
525
526         case IRQ_TYPE_EDGE_BOTH:
527                 pin_reg &= ~BIT(LEVEL_TRIG_OFF);
528                 pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
529                 pin_reg |= BOTH_EADGE << ACTIVE_LEVEL_OFF;
530                 irq_set_handler_locked(d, handle_edge_irq);
531                 break;
532
533         case IRQ_TYPE_LEVEL_HIGH:
534                 pin_reg |= LEVEL_TRIGGER << LEVEL_TRIG_OFF;
535                 pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
536                 pin_reg |= ACTIVE_HIGH << ACTIVE_LEVEL_OFF;
537                 irq_set_handler_locked(d, handle_level_irq);
538                 break;
539
540         case IRQ_TYPE_LEVEL_LOW:
541                 pin_reg |= LEVEL_TRIGGER << LEVEL_TRIG_OFF;
542                 pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
543                 pin_reg |= ACTIVE_LOW << ACTIVE_LEVEL_OFF;
544                 irq_set_handler_locked(d, handle_level_irq);
545                 break;
546
547         case IRQ_TYPE_NONE:
548                 break;
549
550         default:
551                 dev_err(&gpio_dev->pdev->dev, "Invalid type value\n");
552                 ret = -EINVAL;
553         }
554
555         pin_reg |= CLR_INTR_STAT << INTERRUPT_STS_OFF;
556         /*
557          * If WAKE_INT_MASTER_REG.MaskStsEn is set, a software write to the
558          * debounce registers of any GPIO will block wake/interrupt status
559          * generation for *all* GPIOs for a length of time that depends on
560          * WAKE_INT_MASTER_REG.MaskStsLength[11:0].  During this period the
561          * INTERRUPT_ENABLE bit will read as 0.
562          *
563          * We temporarily enable irq for the GPIO whose configuration is
564          * changing, and then wait for it to read back as 1 to know when
565          * debounce has settled and then disable the irq again.
566          * We do this polling with the spinlock held to ensure other GPIO
567          * access routines do not read an incorrect value for the irq enable
568          * bit of other GPIOs.  We keep the GPIO masked while polling to avoid
569          * spurious irqs, and disable the irq again after polling.
570          */
571         mask = BIT(INTERRUPT_ENABLE_OFF);
572         pin_reg_irq_en = pin_reg;
573         pin_reg_irq_en |= mask;
574         pin_reg_irq_en &= ~BIT(INTERRUPT_MASK_OFF);
575         writel(pin_reg_irq_en, gpio_dev->base + (d->hwirq)*4);
576         while ((readl(gpio_dev->base + (d->hwirq)*4) & mask) != mask)
577                 continue;
578         writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
579         raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
580
581         return ret;
582 }
583
584 static void amd_irq_ack(struct irq_data *d)
585 {
586         /*
587          * based on HW design,there is no need to ack HW
588          * before handle current irq. But this routine is
589          * necessary for handle_edge_irq
590         */
591 }
592
593 static const struct irq_chip amd_gpio_irqchip = {
594         .name         = "amd_gpio",
595         .irq_ack      = amd_irq_ack,
596         .irq_enable   = amd_gpio_irq_enable,
597         .irq_disable  = amd_gpio_irq_disable,
598         .irq_mask     = amd_gpio_irq_mask,
599         .irq_unmask   = amd_gpio_irq_unmask,
600         .irq_set_wake = amd_gpio_irq_set_wake,
601         .irq_eoi      = amd_gpio_irq_eoi,
602         .irq_set_type = amd_gpio_irq_set_type,
603         /*
604          * We need to set IRQCHIP_ENABLE_WAKEUP_ON_SUSPEND so that a wake event
605          * also generates an IRQ. We need the IRQ so the irq_handler can clear
606          * the wake event. Otherwise the wake event will never clear and
607          * prevent the system from suspending.
608          */
609         .flags        = IRQCHIP_ENABLE_WAKEUP_ON_SUSPEND | IRQCHIP_IMMUTABLE,
610         GPIOCHIP_IRQ_RESOURCE_HELPERS,
611 };
612
613 #define PIN_IRQ_PENDING (BIT(INTERRUPT_STS_OFF) | BIT(WAKE_STS_OFF))
614
615 static bool do_amd_gpio_irq_handler(int irq, void *dev_id)
616 {
617         struct amd_gpio *gpio_dev = dev_id;
618         struct gpio_chip *gc = &gpio_dev->gc;
619         unsigned int i, irqnr;
620         unsigned long flags;
621         u32 __iomem *regs;
622         bool ret = false;
623         u32  regval;
624         u64 status, mask;
625
626         /* Read the wake status */
627         raw_spin_lock_irqsave(&gpio_dev->lock, flags);
628         status = readl(gpio_dev->base + WAKE_INT_STATUS_REG1);
629         status <<= 32;
630         status |= readl(gpio_dev->base + WAKE_INT_STATUS_REG0);
631         raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
632
633         /* Bit 0-45 contain the relevant status bits */
634         status &= (1ULL << 46) - 1;
635         regs = gpio_dev->base;
636         for (mask = 1, irqnr = 0; status; mask <<= 1, regs += 4, irqnr += 4) {
637                 if (!(status & mask))
638                         continue;
639                 status &= ~mask;
640
641                 /* Each status bit covers four pins */
642                 for (i = 0; i < 4; i++) {
643                         regval = readl(regs + i);
644
645                         if (regval & PIN_IRQ_PENDING)
646                                 dev_dbg(&gpio_dev->pdev->dev,
647                                         "GPIO %d is active: 0x%x",
648                                         irqnr + i, regval);
649
650                         /* caused wake on resume context for shared IRQ */
651                         if (irq < 0 && (regval & BIT(WAKE_STS_OFF)))
652                                 return true;
653
654                         if (!(regval & PIN_IRQ_PENDING) ||
655                             !(regval & BIT(INTERRUPT_MASK_OFF)))
656                                 continue;
657                         generic_handle_domain_irq_safe(gc->irq.domain, irqnr + i);
658
659                         /* Clear interrupt.
660                          * We must read the pin register again, in case the
661                          * value was changed while executing
662                          * generic_handle_domain_irq() above.
663                          * If we didn't find a mapping for the interrupt,
664                          * disable it in order to avoid a system hang caused
665                          * by an interrupt storm.
666                          */
667                         raw_spin_lock_irqsave(&gpio_dev->lock, flags);
668                         regval = readl(regs + i);
669                         if (irq == 0) {
670                                 regval &= ~BIT(INTERRUPT_ENABLE_OFF);
671                                 dev_dbg(&gpio_dev->pdev->dev,
672                                         "Disabling spurious GPIO IRQ %d\n",
673                                         irqnr + i);
674                         }
675                         writel(regval, regs + i);
676                         raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
677                         ret = true;
678                 }
679         }
680         /* did not cause wake on resume context for shared IRQ */
681         if (irq < 0)
682                 return false;
683
684         /* Signal EOI to the GPIO unit */
685         raw_spin_lock_irqsave(&gpio_dev->lock, flags);
686         regval = readl(gpio_dev->base + WAKE_INT_MASTER_REG);
687         regval |= EOI_MASK;
688         writel(regval, gpio_dev->base + WAKE_INT_MASTER_REG);
689         raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
690
691         return ret;
692 }
693
694 static irqreturn_t amd_gpio_irq_handler(int irq, void *dev_id)
695 {
696         return IRQ_RETVAL(do_amd_gpio_irq_handler(irq, dev_id));
697 }
698
699 static bool __maybe_unused amd_gpio_check_wake(void *dev_id)
700 {
701         return do_amd_gpio_irq_handler(-1, dev_id);
702 }
703
704 static int amd_get_groups_count(struct pinctrl_dev *pctldev)
705 {
706         struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
707
708         return gpio_dev->ngroups;
709 }
710
711 static const char *amd_get_group_name(struct pinctrl_dev *pctldev,
712                                       unsigned group)
713 {
714         struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
715
716         return gpio_dev->groups[group].name;
717 }
718
719 static int amd_get_group_pins(struct pinctrl_dev *pctldev,
720                               unsigned group,
721                               const unsigned **pins,
722                               unsigned *num_pins)
723 {
724         struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
725
726         *pins = gpio_dev->groups[group].pins;
727         *num_pins = gpio_dev->groups[group].npins;
728         return 0;
729 }
730
731 static const struct pinctrl_ops amd_pinctrl_ops = {
732         .get_groups_count       = amd_get_groups_count,
733         .get_group_name         = amd_get_group_name,
734         .get_group_pins         = amd_get_group_pins,
735 #ifdef CONFIG_OF
736         .dt_node_to_map         = pinconf_generic_dt_node_to_map_group,
737         .dt_free_map            = pinctrl_utils_free_map,
738 #endif
739 };
740
741 static int amd_pinconf_get(struct pinctrl_dev *pctldev,
742                           unsigned int pin,
743                           unsigned long *config)
744 {
745         u32 pin_reg;
746         unsigned arg;
747         unsigned long flags;
748         struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
749         enum pin_config_param param = pinconf_to_config_param(*config);
750
751         raw_spin_lock_irqsave(&gpio_dev->lock, flags);
752         pin_reg = readl(gpio_dev->base + pin*4);
753         raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
754         switch (param) {
755         case PIN_CONFIG_INPUT_DEBOUNCE:
756                 arg = pin_reg & DB_TMR_OUT_MASK;
757                 break;
758
759         case PIN_CONFIG_BIAS_PULL_DOWN:
760                 arg = (pin_reg >> PULL_DOWN_ENABLE_OFF) & BIT(0);
761                 break;
762
763         case PIN_CONFIG_BIAS_PULL_UP:
764                 arg = (pin_reg >> PULL_UP_SEL_OFF) & (BIT(0) | BIT(1));
765                 break;
766
767         case PIN_CONFIG_DRIVE_STRENGTH:
768                 arg = (pin_reg >> DRV_STRENGTH_SEL_OFF) & DRV_STRENGTH_SEL_MASK;
769                 break;
770
771         default:
772                 dev_err(&gpio_dev->pdev->dev, "Invalid config param %04x\n",
773                         param);
774                 return -ENOTSUPP;
775         }
776
777         *config = pinconf_to_config_packed(param, arg);
778
779         return 0;
780 }
781
782 static int amd_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
783                                 unsigned long *configs, unsigned num_configs)
784 {
785         int i;
786         u32 arg;
787         int ret = 0;
788         u32 pin_reg;
789         unsigned long flags;
790         enum pin_config_param param;
791         struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
792
793         raw_spin_lock_irqsave(&gpio_dev->lock, flags);
794         for (i = 0; i < num_configs; i++) {
795                 param = pinconf_to_config_param(configs[i]);
796                 arg = pinconf_to_config_argument(configs[i]);
797                 pin_reg = readl(gpio_dev->base + pin*4);
798
799                 switch (param) {
800                 case PIN_CONFIG_INPUT_DEBOUNCE:
801                         pin_reg &= ~DB_TMR_OUT_MASK;
802                         pin_reg |= arg & DB_TMR_OUT_MASK;
803                         break;
804
805                 case PIN_CONFIG_BIAS_PULL_DOWN:
806                         pin_reg &= ~BIT(PULL_DOWN_ENABLE_OFF);
807                         pin_reg |= (arg & BIT(0)) << PULL_DOWN_ENABLE_OFF;
808                         break;
809
810                 case PIN_CONFIG_BIAS_PULL_UP:
811                         pin_reg &= ~BIT(PULL_UP_SEL_OFF);
812                         pin_reg |= (arg & BIT(0)) << PULL_UP_SEL_OFF;
813                         pin_reg &= ~BIT(PULL_UP_ENABLE_OFF);
814                         pin_reg |= ((arg>>1) & BIT(0)) << PULL_UP_ENABLE_OFF;
815                         break;
816
817                 case PIN_CONFIG_DRIVE_STRENGTH:
818                         pin_reg &= ~(DRV_STRENGTH_SEL_MASK
819                                         << DRV_STRENGTH_SEL_OFF);
820                         pin_reg |= (arg & DRV_STRENGTH_SEL_MASK)
821                                         << DRV_STRENGTH_SEL_OFF;
822                         break;
823
824                 default:
825                         dev_err(&gpio_dev->pdev->dev,
826                                 "Invalid config param %04x\n", param);
827                         ret = -ENOTSUPP;
828                 }
829
830                 writel(pin_reg, gpio_dev->base + pin*4);
831         }
832         raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
833
834         return ret;
835 }
836
837 static int amd_pinconf_group_get(struct pinctrl_dev *pctldev,
838                                 unsigned int group,
839                                 unsigned long *config)
840 {
841         const unsigned *pins;
842         unsigned npins;
843         int ret;
844
845         ret = amd_get_group_pins(pctldev, group, &pins, &npins);
846         if (ret)
847                 return ret;
848
849         if (amd_pinconf_get(pctldev, pins[0], config))
850                         return -ENOTSUPP;
851
852         return 0;
853 }
854
855 static int amd_pinconf_group_set(struct pinctrl_dev *pctldev,
856                                 unsigned group, unsigned long *configs,
857                                 unsigned num_configs)
858 {
859         const unsigned *pins;
860         unsigned npins;
861         int i, ret;
862
863         ret = amd_get_group_pins(pctldev, group, &pins, &npins);
864         if (ret)
865                 return ret;
866         for (i = 0; i < npins; i++) {
867                 if (amd_pinconf_set(pctldev, pins[i], configs, num_configs))
868                         return -ENOTSUPP;
869         }
870         return 0;
871 }
872
873 static const struct pinconf_ops amd_pinconf_ops = {
874         .pin_config_get         = amd_pinconf_get,
875         .pin_config_set         = amd_pinconf_set,
876         .pin_config_group_get = amd_pinconf_group_get,
877         .pin_config_group_set = amd_pinconf_group_set,
878 };
879
880 static void amd_gpio_irq_init(struct amd_gpio *gpio_dev)
881 {
882         struct pinctrl_desc *desc = gpio_dev->pctrl->desc;
883         unsigned long flags;
884         u32 pin_reg, mask;
885         int i;
886
887         mask = BIT(WAKE_CNTRL_OFF_S0I3) | BIT(WAKE_CNTRL_OFF_S3) |
888                 BIT(INTERRUPT_MASK_OFF) | BIT(INTERRUPT_ENABLE_OFF) |
889                 BIT(WAKE_CNTRL_OFF_S4);
890
891         for (i = 0; i < desc->npins; i++) {
892                 int pin = desc->pins[i].number;
893                 const struct pin_desc *pd = pin_desc_get(gpio_dev->pctrl, pin);
894
895                 if (!pd)
896                         continue;
897
898                 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
899
900                 pin_reg = readl(gpio_dev->base + pin * 4);
901                 pin_reg &= ~mask;
902                 writel(pin_reg, gpio_dev->base + pin * 4);
903
904                 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
905         }
906 }
907
908 #ifdef CONFIG_PM_SLEEP
909 static bool amd_gpio_should_save(struct amd_gpio *gpio_dev, unsigned int pin)
910 {
911         const struct pin_desc *pd = pin_desc_get(gpio_dev->pctrl, pin);
912
913         if (!pd)
914                 return false;
915
916         /*
917          * Only restore the pin if it is actually in use by the kernel (or
918          * by userspace).
919          */
920         if (pd->mux_owner || pd->gpio_owner ||
921             gpiochip_line_is_irq(&gpio_dev->gc, pin))
922                 return true;
923
924         return false;
925 }
926
927 static int amd_gpio_suspend(struct device *dev)
928 {
929         struct amd_gpio *gpio_dev = dev_get_drvdata(dev);
930         struct pinctrl_desc *desc = gpio_dev->pctrl->desc;
931         unsigned long flags;
932         int i;
933
934         for (i = 0; i < desc->npins; i++) {
935                 int pin = desc->pins[i].number;
936
937                 if (!amd_gpio_should_save(gpio_dev, pin))
938                         continue;
939
940                 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
941                 gpio_dev->saved_regs[i] = readl(gpio_dev->base + pin * 4) & ~PIN_IRQ_PENDING;
942                 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
943         }
944
945         return 0;
946 }
947
948 static int amd_gpio_resume(struct device *dev)
949 {
950         struct amd_gpio *gpio_dev = dev_get_drvdata(dev);
951         struct pinctrl_desc *desc = gpio_dev->pctrl->desc;
952         unsigned long flags;
953         int i;
954
955         for (i = 0; i < desc->npins; i++) {
956                 int pin = desc->pins[i].number;
957
958                 if (!amd_gpio_should_save(gpio_dev, pin))
959                         continue;
960
961                 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
962                 gpio_dev->saved_regs[i] |= readl(gpio_dev->base + pin * 4) & PIN_IRQ_PENDING;
963                 writel(gpio_dev->saved_regs[i], gpio_dev->base + pin * 4);
964                 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
965         }
966
967         return 0;
968 }
969
970 static const struct dev_pm_ops amd_gpio_pm_ops = {
971         SET_LATE_SYSTEM_SLEEP_PM_OPS(amd_gpio_suspend,
972                                      amd_gpio_resume)
973 };
974 #endif
975
976 static int amd_get_functions_count(struct pinctrl_dev *pctldev)
977 {
978         return ARRAY_SIZE(pmx_functions);
979 }
980
981 static const char *amd_get_fname(struct pinctrl_dev *pctrldev, unsigned int selector)
982 {
983         return pmx_functions[selector].name;
984 }
985
986 static int amd_get_groups(struct pinctrl_dev *pctrldev, unsigned int selector,
987                           const char * const **groups,
988                           unsigned int * const num_groups)
989 {
990         struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctrldev);
991
992         if (!gpio_dev->iomux_base) {
993                 dev_err(&gpio_dev->pdev->dev, "iomux function %d group not supported\n", selector);
994                 return -EINVAL;
995         }
996
997         *groups = pmx_functions[selector].groups;
998         *num_groups = pmx_functions[selector].ngroups;
999         return 0;
1000 }
1001
1002 static int amd_set_mux(struct pinctrl_dev *pctrldev, unsigned int function, unsigned int group)
1003 {
1004         struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctrldev);
1005         struct device *dev = &gpio_dev->pdev->dev;
1006         struct pin_desc *pd;
1007         int ind, index;
1008
1009         if (!gpio_dev->iomux_base)
1010                 return -EINVAL;
1011
1012         for (index = 0; index < NSELECTS; index++) {
1013                 if (strcmp(gpio_dev->groups[group].name,  pmx_functions[function].groups[index]))
1014                         continue;
1015
1016                 if (readb(gpio_dev->iomux_base + pmx_functions[function].index) ==
1017                                 FUNCTION_INVALID) {
1018                         dev_err(dev, "IOMUX_GPIO 0x%x not present or supported\n",
1019                                 pmx_functions[function].index);
1020                         return -EINVAL;
1021                 }
1022
1023                 writeb(index, gpio_dev->iomux_base + pmx_functions[function].index);
1024
1025                 if (index != (readb(gpio_dev->iomux_base + pmx_functions[function].index) &
1026                                         FUNCTION_MASK)) {
1027                         dev_err(dev, "IOMUX_GPIO 0x%x not present or supported\n",
1028                                 pmx_functions[function].index);
1029                         return -EINVAL;
1030                 }
1031
1032                 for (ind = 0; ind < gpio_dev->groups[group].npins; ind++) {
1033                         if (strncmp(gpio_dev->groups[group].name, "IMX_F", strlen("IMX_F")))
1034                                 continue;
1035
1036                         pd = pin_desc_get(gpio_dev->pctrl, gpio_dev->groups[group].pins[ind]);
1037                         pd->mux_owner = gpio_dev->groups[group].name;
1038                 }
1039                 break;
1040         }
1041
1042         return 0;
1043 }
1044
1045 static const struct pinmux_ops amd_pmxops = {
1046         .get_functions_count = amd_get_functions_count,
1047         .get_function_name = amd_get_fname,
1048         .get_function_groups = amd_get_groups,
1049         .set_mux = amd_set_mux,
1050 };
1051
1052 static struct pinctrl_desc amd_pinctrl_desc = {
1053         .pins   = kerncz_pins,
1054         .npins = ARRAY_SIZE(kerncz_pins),
1055         .pctlops = &amd_pinctrl_ops,
1056         .pmxops = &amd_pmxops,
1057         .confops = &amd_pinconf_ops,
1058         .owner = THIS_MODULE,
1059 };
1060
1061 static void amd_get_iomux_res(struct amd_gpio *gpio_dev)
1062 {
1063         struct pinctrl_desc *desc = &amd_pinctrl_desc;
1064         struct device *dev = &gpio_dev->pdev->dev;
1065         int index;
1066
1067         index = device_property_match_string(dev, "pinctrl-resource-names",  "iomux");
1068         if (index < 0) {
1069                 dev_dbg(dev, "iomux not supported\n");
1070                 goto out_no_pinmux;
1071         }
1072
1073         gpio_dev->iomux_base = devm_platform_ioremap_resource(gpio_dev->pdev, index);
1074         if (IS_ERR(gpio_dev->iomux_base)) {
1075                 dev_dbg(dev, "iomux not supported %d io resource\n", index);
1076                 goto out_no_pinmux;
1077         }
1078
1079         return;
1080
1081 out_no_pinmux:
1082         desc->pmxops = NULL;
1083 }
1084
1085 static int amd_gpio_probe(struct platform_device *pdev)
1086 {
1087         int ret = 0;
1088         struct resource *res;
1089         struct amd_gpio *gpio_dev;
1090         struct gpio_irq_chip *girq;
1091
1092         gpio_dev = devm_kzalloc(&pdev->dev,
1093                                 sizeof(struct amd_gpio), GFP_KERNEL);
1094         if (!gpio_dev)
1095                 return -ENOMEM;
1096
1097         raw_spin_lock_init(&gpio_dev->lock);
1098
1099         gpio_dev->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
1100         if (IS_ERR(gpio_dev->base)) {
1101                 dev_err(&pdev->dev, "Failed to get gpio io resource.\n");
1102                 return PTR_ERR(gpio_dev->base);
1103         }
1104
1105         gpio_dev->irq = platform_get_irq(pdev, 0);
1106         if (gpio_dev->irq < 0)
1107                 return gpio_dev->irq;
1108
1109 #ifdef CONFIG_PM_SLEEP
1110         gpio_dev->saved_regs = devm_kcalloc(&pdev->dev, amd_pinctrl_desc.npins,
1111                                             sizeof(*gpio_dev->saved_regs),
1112                                             GFP_KERNEL);
1113         if (!gpio_dev->saved_regs)
1114                 return -ENOMEM;
1115 #endif
1116
1117         gpio_dev->pdev = pdev;
1118         gpio_dev->gc.get_direction      = amd_gpio_get_direction;
1119         gpio_dev->gc.direction_input    = amd_gpio_direction_input;
1120         gpio_dev->gc.direction_output   = amd_gpio_direction_output;
1121         gpio_dev->gc.get                        = amd_gpio_get_value;
1122         gpio_dev->gc.set                        = amd_gpio_set_value;
1123         gpio_dev->gc.set_config         = amd_gpio_set_config;
1124         gpio_dev->gc.dbg_show           = amd_gpio_dbg_show;
1125
1126         gpio_dev->gc.base               = -1;
1127         gpio_dev->gc.label                      = pdev->name;
1128         gpio_dev->gc.owner                      = THIS_MODULE;
1129         gpio_dev->gc.parent                     = &pdev->dev;
1130         gpio_dev->gc.ngpio                      = resource_size(res) / 4;
1131
1132         gpio_dev->hwbank_num = gpio_dev->gc.ngpio / 64;
1133         gpio_dev->groups = kerncz_groups;
1134         gpio_dev->ngroups = ARRAY_SIZE(kerncz_groups);
1135
1136         amd_pinctrl_desc.name = dev_name(&pdev->dev);
1137         amd_get_iomux_res(gpio_dev);
1138         gpio_dev->pctrl = devm_pinctrl_register(&pdev->dev, &amd_pinctrl_desc,
1139                                                 gpio_dev);
1140         if (IS_ERR(gpio_dev->pctrl)) {
1141                 dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
1142                 return PTR_ERR(gpio_dev->pctrl);
1143         }
1144
1145         /* Disable and mask interrupts */
1146         amd_gpio_irq_init(gpio_dev);
1147
1148         girq = &gpio_dev->gc.irq;
1149         gpio_irq_chip_set_chip(girq, &amd_gpio_irqchip);
1150         /* This will let us handle the parent IRQ in the driver */
1151         girq->parent_handler = NULL;
1152         girq->num_parents = 0;
1153         girq->parents = NULL;
1154         girq->default_type = IRQ_TYPE_NONE;
1155         girq->handler = handle_simple_irq;
1156
1157         ret = gpiochip_add_data(&gpio_dev->gc, gpio_dev);
1158         if (ret)
1159                 return ret;
1160
1161         ret = gpiochip_add_pin_range(&gpio_dev->gc, dev_name(&pdev->dev),
1162                                 0, 0, gpio_dev->gc.ngpio);
1163         if (ret) {
1164                 dev_err(&pdev->dev, "Failed to add pin range\n");
1165                 goto out2;
1166         }
1167
1168         ret = devm_request_irq(&pdev->dev, gpio_dev->irq, amd_gpio_irq_handler,
1169                                IRQF_SHARED, KBUILD_MODNAME, gpio_dev);
1170         if (ret)
1171                 goto out2;
1172
1173         platform_set_drvdata(pdev, gpio_dev);
1174         acpi_register_wakeup_handler(gpio_dev->irq, amd_gpio_check_wake, gpio_dev);
1175
1176         dev_dbg(&pdev->dev, "amd gpio driver loaded\n");
1177         return ret;
1178
1179 out2:
1180         gpiochip_remove(&gpio_dev->gc);
1181
1182         return ret;
1183 }
1184
1185 static int amd_gpio_remove(struct platform_device *pdev)
1186 {
1187         struct amd_gpio *gpio_dev;
1188
1189         gpio_dev = platform_get_drvdata(pdev);
1190
1191         gpiochip_remove(&gpio_dev->gc);
1192         acpi_unregister_wakeup_handler(amd_gpio_check_wake, gpio_dev);
1193
1194         return 0;
1195 }
1196
1197 #ifdef CONFIG_ACPI
1198 static const struct acpi_device_id amd_gpio_acpi_match[] = {
1199         { "AMD0030", 0 },
1200         { "AMDI0030", 0},
1201         { "AMDI0031", 0},
1202         { },
1203 };
1204 MODULE_DEVICE_TABLE(acpi, amd_gpio_acpi_match);
1205 #endif
1206
1207 static struct platform_driver amd_gpio_driver = {
1208         .driver         = {
1209                 .name   = "amd_gpio",
1210                 .acpi_match_table = ACPI_PTR(amd_gpio_acpi_match),
1211 #ifdef CONFIG_PM_SLEEP
1212                 .pm     = &amd_gpio_pm_ops,
1213 #endif
1214         },
1215         .probe          = amd_gpio_probe,
1216         .remove         = amd_gpio_remove,
1217 };
1218
1219 module_platform_driver(amd_gpio_driver);
1220
1221 MODULE_LICENSE("GPL v2");
1222 MODULE_AUTHOR("Ken Xue <Ken.Xue@amd.com>, Jeff Wu <Jeff.Wu@amd.com>");
1223 MODULE_DESCRIPTION("AMD GPIO pinctrl driver");