GNU Linux-libre 4.19.286-gnu1
[releases.git] / drivers / pinctrl / pinctrl-amd.c
1 /*
2  * GPIO driver for AMD
3  *
4  * Copyright (c) 2014,2015 AMD Corporation.
5  * Authors: Ken Xue <Ken.Xue@amd.com>
6  *      Wu, Jeff <Jeff.Wu@amd.com>
7  *
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.
11  *
12  * Contact Information: Nehal Shah <Nehal-bakulchandra.Shah@amd.com>
13  *                      Shyam Sundar S K <Shyam-sundar.S-k@amd.com>
14  *
15  */
16
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>
26 #include <linux/io.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>
38
39 #include "core.h"
40 #include "pinctrl-utils.h"
41 #include "pinctrl-amd.h"
42
43 static int amd_gpio_get_direction(struct gpio_chip *gc, unsigned offset)
44 {
45         unsigned long flags;
46         u32 pin_reg;
47         struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
48
49         raw_spin_lock_irqsave(&gpio_dev->lock, flags);
50         pin_reg = readl(gpio_dev->base + offset * 4);
51         raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
52
53         return !(pin_reg & BIT(OUTPUT_ENABLE_OFF));
54 }
55
56 static int amd_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
57 {
58         unsigned long flags;
59         u32 pin_reg;
60         struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
61
62         raw_spin_lock_irqsave(&gpio_dev->lock, flags);
63         pin_reg = readl(gpio_dev->base + offset * 4);
64         pin_reg &= ~BIT(OUTPUT_ENABLE_OFF);
65         writel(pin_reg, gpio_dev->base + offset * 4);
66         raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
67
68         return 0;
69 }
70
71 static int amd_gpio_direction_output(struct gpio_chip *gc, unsigned offset,
72                 int value)
73 {
74         u32 pin_reg;
75         unsigned long flags;
76         struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
77
78         raw_spin_lock_irqsave(&gpio_dev->lock, flags);
79         pin_reg = readl(gpio_dev->base + offset * 4);
80         pin_reg |= BIT(OUTPUT_ENABLE_OFF);
81         if (value)
82                 pin_reg |= BIT(OUTPUT_VALUE_OFF);
83         else
84                 pin_reg &= ~BIT(OUTPUT_VALUE_OFF);
85         writel(pin_reg, gpio_dev->base + offset * 4);
86         raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
87
88         return 0;
89 }
90
91 static int amd_gpio_get_value(struct gpio_chip *gc, unsigned offset)
92 {
93         u32 pin_reg;
94         unsigned long flags;
95         struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
96
97         raw_spin_lock_irqsave(&gpio_dev->lock, flags);
98         pin_reg = readl(gpio_dev->base + offset * 4);
99         raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
100
101         return !!(pin_reg & BIT(PIN_STS_OFF));
102 }
103
104 static void amd_gpio_set_value(struct gpio_chip *gc, unsigned offset, int value)
105 {
106         u32 pin_reg;
107         unsigned long flags;
108         struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
109
110         raw_spin_lock_irqsave(&gpio_dev->lock, flags);
111         pin_reg = readl(gpio_dev->base + offset * 4);
112         if (value)
113                 pin_reg |= BIT(OUTPUT_VALUE_OFF);
114         else
115                 pin_reg &= ~BIT(OUTPUT_VALUE_OFF);
116         writel(pin_reg, gpio_dev->base + offset * 4);
117         raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
118 }
119
120 static int amd_gpio_set_debounce(struct gpio_chip *gc, unsigned offset,
121                 unsigned debounce)
122 {
123         u32 time;
124         u32 pin_reg;
125         int ret = 0;
126         unsigned long flags;
127         struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
128
129         raw_spin_lock_irqsave(&gpio_dev->lock, flags);
130         pin_reg = readl(gpio_dev->base + offset * 4);
131
132         if (debounce) {
133                 pin_reg |= DB_TYPE_REMOVE_GLITCH << DB_CNTRL_OFF;
134                 pin_reg &= ~DB_TMR_OUT_MASK;
135                 /*
136                 Debounce        Debounce        Timer   Max
137                 TmrLarge        TmrOutUnit      Unit    Debounce
138                                                         Time
139                 0       0       61 usec (2 RtcClk)      976 usec
140                 0       1       244 usec (8 RtcClk)     3.9 msec
141                 1       0       15.6 msec (512 RtcClk)  250 msec
142                 1       1       62.5 msec (2048 RtcClk) 1 sec
143                 */
144
145                 if (debounce < 61) {
146                         pin_reg |= 1;
147                         pin_reg &= ~BIT(DB_TMR_OUT_UNIT_OFF);
148                         pin_reg &= ~BIT(DB_TMR_LARGE_OFF);
149                 } else if (debounce < 976) {
150                         time = debounce / 61;
151                         pin_reg |= time & DB_TMR_OUT_MASK;
152                         pin_reg &= ~BIT(DB_TMR_OUT_UNIT_OFF);
153                         pin_reg &= ~BIT(DB_TMR_LARGE_OFF);
154                 } else if (debounce < 3900) {
155                         time = debounce / 244;
156                         pin_reg |= time & DB_TMR_OUT_MASK;
157                         pin_reg |= BIT(DB_TMR_OUT_UNIT_OFF);
158                         pin_reg &= ~BIT(DB_TMR_LARGE_OFF);
159                 } else if (debounce < 250000) {
160                         time = debounce / 15625;
161                         pin_reg |= time & DB_TMR_OUT_MASK;
162                         pin_reg &= ~BIT(DB_TMR_OUT_UNIT_OFF);
163                         pin_reg |= BIT(DB_TMR_LARGE_OFF);
164                 } else if (debounce < 1000000) {
165                         time = debounce / 62500;
166                         pin_reg |= time & DB_TMR_OUT_MASK;
167                         pin_reg |= BIT(DB_TMR_OUT_UNIT_OFF);
168                         pin_reg |= BIT(DB_TMR_LARGE_OFF);
169                 } else {
170                         pin_reg &= ~(DB_CNTRl_MASK << DB_CNTRL_OFF);
171                         ret = -EINVAL;
172                 }
173         } else {
174                 pin_reg &= ~BIT(DB_TMR_OUT_UNIT_OFF);
175                 pin_reg &= ~BIT(DB_TMR_LARGE_OFF);
176                 pin_reg &= ~DB_TMR_OUT_MASK;
177                 pin_reg &= ~(DB_CNTRl_MASK << DB_CNTRL_OFF);
178         }
179         writel(pin_reg, gpio_dev->base + offset * 4);
180         raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
181
182         return ret;
183 }
184
185 static int amd_gpio_set_config(struct gpio_chip *gc, unsigned offset,
186                                unsigned long config)
187 {
188         u32 debounce;
189
190         if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE)
191                 return -ENOTSUPP;
192
193         debounce = pinconf_to_config_argument(config);
194         return amd_gpio_set_debounce(gc, offset, debounce);
195 }
196
197 #ifdef CONFIG_DEBUG_FS
198 static void amd_gpio_dbg_show(struct seq_file *s, struct gpio_chip *gc)
199 {
200         u32 pin_reg;
201         unsigned long flags;
202         unsigned int bank, i, pin_num;
203         struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
204
205         char *level_trig;
206         char *active_level;
207         char *interrupt_enable;
208         char *interrupt_mask;
209         char *wake_cntrl0;
210         char *wake_cntrl1;
211         char *wake_cntrl2;
212         char *pin_sts;
213         char *pull_up_sel;
214         char *pull_up_enable;
215         char *pull_down_enable;
216         char *output_value;
217         char *output_enable;
218
219         for (bank = 0; bank < gpio_dev->hwbank_num; bank++) {
220                 seq_printf(s, "GPIO bank%d\t", bank);
221
222                 switch (bank) {
223                 case 0:
224                         i = 0;
225                         pin_num = AMD_GPIO_PINS_BANK0;
226                         break;
227                 case 1:
228                         i = 64;
229                         pin_num = AMD_GPIO_PINS_BANK1 + i;
230                         break;
231                 case 2:
232                         i = 128;
233                         pin_num = AMD_GPIO_PINS_BANK2 + i;
234                         break;
235                 case 3:
236                         i = 192;
237                         pin_num = AMD_GPIO_PINS_BANK3 + i;
238                         break;
239                 default:
240                         /* Illegal bank number, ignore */
241                         continue;
242                 }
243                 for (; i < pin_num; i++) {
244                         seq_printf(s, "pin%d\t", i);
245                         raw_spin_lock_irqsave(&gpio_dev->lock, flags);
246                         pin_reg = readl(gpio_dev->base + i * 4);
247                         raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
248
249                         if (pin_reg & BIT(INTERRUPT_ENABLE_OFF)) {
250                                 u8 level = (pin_reg >> ACTIVE_LEVEL_OFF) &
251                                                 ACTIVE_LEVEL_MASK;
252                                 interrupt_enable = "interrupt is enabled|";
253
254                                 if (level == ACTIVE_LEVEL_HIGH)
255                                         active_level = "Active high|";
256                                 else if (level == ACTIVE_LEVEL_LOW)
257                                         active_level = "Active low|";
258                                 else if (!(pin_reg & BIT(LEVEL_TRIG_OFF)) &&
259                                          level == ACTIVE_LEVEL_BOTH)
260                                         active_level = "Active on both|";
261                                 else
262                                         active_level = "Unknown Active level|";
263
264                                 if (pin_reg & BIT(LEVEL_TRIG_OFF))
265                                         level_trig = "Level trigger|";
266                                 else
267                                         level_trig = "Edge trigger|";
268
269                         } else {
270                                 interrupt_enable =
271                                         "interrupt is disabled|";
272                                 active_level = " ";
273                                 level_trig = " ";
274                         }
275
276                         if (pin_reg & BIT(INTERRUPT_MASK_OFF))
277                                 interrupt_mask =
278                                         "interrupt is unmasked|";
279                         else
280                                 interrupt_mask =
281                                         "interrupt is masked|";
282
283                         if (pin_reg & BIT(WAKE_CNTRL_OFF_S0I3))
284                                 wake_cntrl0 = "enable wakeup in S0i3 state|";
285                         else
286                                 wake_cntrl0 = "disable wakeup in S0i3 state|";
287
288                         if (pin_reg & BIT(WAKE_CNTRL_OFF_S3))
289                                 wake_cntrl1 = "enable wakeup in S3 state|";
290                         else
291                                 wake_cntrl1 = "disable wakeup in S3 state|";
292
293                         if (pin_reg & BIT(WAKE_CNTRL_OFF_S4))
294                                 wake_cntrl2 = "enable wakeup in S4/S5 state|";
295                         else
296                                 wake_cntrl2 = "disable wakeup in S4/S5 state|";
297
298                         if (pin_reg & BIT(PULL_UP_ENABLE_OFF)) {
299                                 pull_up_enable = "pull-up is enabled|";
300                                 if (pin_reg & BIT(PULL_UP_SEL_OFF))
301                                         pull_up_sel = "8k pull-up|";
302                                 else
303                                         pull_up_sel = "4k pull-up|";
304                         } else {
305                                 pull_up_enable = "pull-up is disabled|";
306                                 pull_up_sel = " ";
307                         }
308
309                         if (pin_reg & BIT(PULL_DOWN_ENABLE_OFF))
310                                 pull_down_enable = "pull-down is enabled|";
311                         else
312                                 pull_down_enable = "Pull-down is disabled|";
313
314                         if (pin_reg & BIT(OUTPUT_ENABLE_OFF)) {
315                                 pin_sts = " ";
316                                 output_enable = "output is enabled|";
317                                 if (pin_reg & BIT(OUTPUT_VALUE_OFF))
318                                         output_value = "output is high|";
319                                 else
320                                         output_value = "output is low|";
321                         } else {
322                                 output_enable = "output is disabled|";
323                                 output_value = " ";
324
325                                 if (pin_reg & BIT(PIN_STS_OFF))
326                                         pin_sts = "input is high|";
327                                 else
328                                         pin_sts = "input is low|";
329                         }
330
331                         seq_printf(s, "%s %s %s %s %s %s\n"
332                                 " %s %s %s %s %s %s %s 0x%x\n",
333                                 level_trig, active_level, interrupt_enable,
334                                 interrupt_mask, wake_cntrl0, wake_cntrl1,
335                                 wake_cntrl2, pin_sts, pull_up_sel,
336                                 pull_up_enable, pull_down_enable,
337                                 output_value, output_enable, pin_reg);
338                 }
339         }
340 }
341 #else
342 #define amd_gpio_dbg_show NULL
343 #endif
344
345 static void amd_gpio_irq_enable(struct irq_data *d)
346 {
347         u32 pin_reg;
348         unsigned long flags;
349         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
350         struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
351
352         raw_spin_lock_irqsave(&gpio_dev->lock, flags);
353         pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
354         pin_reg |= BIT(INTERRUPT_ENABLE_OFF);
355         pin_reg |= BIT(INTERRUPT_MASK_OFF);
356         writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
357         raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
358 }
359
360 static void amd_gpio_irq_disable(struct irq_data *d)
361 {
362         u32 pin_reg;
363         unsigned long flags;
364         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
365         struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
366
367         raw_spin_lock_irqsave(&gpio_dev->lock, flags);
368         pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
369         pin_reg &= ~BIT(INTERRUPT_ENABLE_OFF);
370         pin_reg &= ~BIT(INTERRUPT_MASK_OFF);
371         writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
372         raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
373 }
374
375 static void amd_gpio_irq_mask(struct irq_data *d)
376 {
377         u32 pin_reg;
378         unsigned long flags;
379         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
380         struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
381
382         raw_spin_lock_irqsave(&gpio_dev->lock, flags);
383         pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
384         pin_reg &= ~BIT(INTERRUPT_MASK_OFF);
385         writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
386         raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
387 }
388
389 static void amd_gpio_irq_unmask(struct irq_data *d)
390 {
391         u32 pin_reg;
392         unsigned long flags;
393         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
394         struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
395
396         raw_spin_lock_irqsave(&gpio_dev->lock, flags);
397         pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
398         pin_reg |= BIT(INTERRUPT_MASK_OFF);
399         writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
400         raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
401 }
402
403 static void amd_gpio_irq_eoi(struct irq_data *d)
404 {
405         u32 reg;
406         unsigned long flags;
407         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
408         struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
409
410         raw_spin_lock_irqsave(&gpio_dev->lock, flags);
411         reg = readl(gpio_dev->base + WAKE_INT_MASTER_REG);
412         reg |= EOI_MASK;
413         writel(reg, gpio_dev->base + WAKE_INT_MASTER_REG);
414         raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
415 }
416
417 static int amd_gpio_irq_set_type(struct irq_data *d, unsigned int type)
418 {
419         int ret = 0;
420         u32 pin_reg, pin_reg_irq_en, mask;
421         unsigned long flags, irq_flags;
422         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
423         struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
424
425         raw_spin_lock_irqsave(&gpio_dev->lock, flags);
426         pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
427
428         /* Ignore the settings coming from the client and
429          * read the values from the ACPI tables
430          * while setting the trigger type
431          */
432
433         irq_flags = irq_get_trigger_type(d->irq);
434         if (irq_flags != IRQ_TYPE_NONE)
435                 type = irq_flags;
436
437         switch (type & IRQ_TYPE_SENSE_MASK) {
438         case IRQ_TYPE_EDGE_RISING:
439                 pin_reg &= ~BIT(LEVEL_TRIG_OFF);
440                 pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
441                 pin_reg |= ACTIVE_HIGH << ACTIVE_LEVEL_OFF;
442                 irq_set_handler_locked(d, handle_edge_irq);
443                 break;
444
445         case IRQ_TYPE_EDGE_FALLING:
446                 pin_reg &= ~BIT(LEVEL_TRIG_OFF);
447                 pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
448                 pin_reg |= ACTIVE_LOW << ACTIVE_LEVEL_OFF;
449                 irq_set_handler_locked(d, handle_edge_irq);
450                 break;
451
452         case IRQ_TYPE_EDGE_BOTH:
453                 pin_reg &= ~BIT(LEVEL_TRIG_OFF);
454                 pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
455                 pin_reg |= BOTH_EADGE << ACTIVE_LEVEL_OFF;
456                 irq_set_handler_locked(d, handle_edge_irq);
457                 break;
458
459         case IRQ_TYPE_LEVEL_HIGH:
460                 pin_reg |= LEVEL_TRIGGER << LEVEL_TRIG_OFF;
461                 pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
462                 pin_reg |= ACTIVE_HIGH << ACTIVE_LEVEL_OFF;
463                 irq_set_handler_locked(d, handle_level_irq);
464                 break;
465
466         case IRQ_TYPE_LEVEL_LOW:
467                 pin_reg |= LEVEL_TRIGGER << LEVEL_TRIG_OFF;
468                 pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
469                 pin_reg |= ACTIVE_LOW << ACTIVE_LEVEL_OFF;
470                 irq_set_handler_locked(d, handle_level_irq);
471                 break;
472
473         case IRQ_TYPE_NONE:
474                 break;
475
476         default:
477                 dev_err(&gpio_dev->pdev->dev, "Invalid type value\n");
478                 ret = -EINVAL;
479         }
480
481         pin_reg |= CLR_INTR_STAT << INTERRUPT_STS_OFF;
482         /*
483          * If WAKE_INT_MASTER_REG.MaskStsEn is set, a software write to the
484          * debounce registers of any GPIO will block wake/interrupt status
485          * generation for *all* GPIOs for a lenght of time that depends on
486          * WAKE_INT_MASTER_REG.MaskStsLength[11:0].  During this period the
487          * INTERRUPT_ENABLE bit will read as 0.
488          *
489          * We temporarily enable irq for the GPIO whose configuration is
490          * changing, and then wait for it to read back as 1 to know when
491          * debounce has settled and then disable the irq again.
492          * We do this polling with the spinlock held to ensure other GPIO
493          * access routines do not read an incorrect value for the irq enable
494          * bit of other GPIOs.  We keep the GPIO masked while polling to avoid
495          * spurious irqs, and disable the irq again after polling.
496          */
497         mask = BIT(INTERRUPT_ENABLE_OFF);
498         pin_reg_irq_en = pin_reg;
499         pin_reg_irq_en |= mask;
500         pin_reg_irq_en &= ~BIT(INTERRUPT_MASK_OFF);
501         writel(pin_reg_irq_en, gpio_dev->base + (d->hwirq)*4);
502         while ((readl(gpio_dev->base + (d->hwirq)*4) & mask) != mask)
503                 continue;
504         writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
505         raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
506
507         return ret;
508 }
509
510 static void amd_irq_ack(struct irq_data *d)
511 {
512         /*
513          * based on HW design,there is no need to ack HW
514          * before handle current irq. But this routine is
515          * necessary for handle_edge_irq
516         */
517 }
518
519 static struct irq_chip amd_gpio_irqchip = {
520         .name         = "amd_gpio",
521         .irq_ack      = amd_irq_ack,
522         .irq_enable   = amd_gpio_irq_enable,
523         .irq_disable  = amd_gpio_irq_disable,
524         .irq_mask     = amd_gpio_irq_mask,
525         .irq_unmask   = amd_gpio_irq_unmask,
526         .irq_eoi      = amd_gpio_irq_eoi,
527         .irq_set_type = amd_gpio_irq_set_type,
528         .flags        = IRQCHIP_SKIP_SET_WAKE,
529 };
530
531 #define PIN_IRQ_PENDING (BIT(INTERRUPT_STS_OFF) | BIT(WAKE_STS_OFF))
532
533 static irqreturn_t amd_gpio_irq_handler(int irq, void *dev_id)
534 {
535         struct amd_gpio *gpio_dev = dev_id;
536         struct gpio_chip *gc = &gpio_dev->gc;
537         irqreturn_t ret = IRQ_NONE;
538         unsigned int i, irqnr;
539         unsigned long flags;
540         u32 __iomem *regs;
541         u32  regval;
542         u64 status, mask;
543
544         /* Read the wake status */
545         raw_spin_lock_irqsave(&gpio_dev->lock, flags);
546         status = readl(gpio_dev->base + WAKE_INT_STATUS_REG1);
547         status <<= 32;
548         status |= readl(gpio_dev->base + WAKE_INT_STATUS_REG0);
549         raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
550
551         /* Bit 0-45 contain the relevant status bits */
552         status &= (1ULL << 46) - 1;
553         regs = gpio_dev->base;
554         for (mask = 1, irqnr = 0; status; mask <<= 1, regs += 4, irqnr += 4) {
555                 if (!(status & mask))
556                         continue;
557                 status &= ~mask;
558
559                 /* Each status bit covers four pins */
560                 for (i = 0; i < 4; i++) {
561                         regval = readl(regs + i);
562                         if (!(regval & PIN_IRQ_PENDING) ||
563                             !(regval & BIT(INTERRUPT_MASK_OFF)))
564                                 continue;
565                         irq = irq_find_mapping(gc->irq.domain, irqnr + i);
566                         if (irq != 0)
567                                 generic_handle_irq(irq);
568
569                         /* Clear interrupt.
570                          * We must read the pin register again, in case the
571                          * value was changed while executing
572                          * generic_handle_irq() above.
573                          * If we didn't find a mapping for the interrupt,
574                          * disable it in order to avoid a system hang caused
575                          * by an interrupt storm.
576                          */
577                         raw_spin_lock_irqsave(&gpio_dev->lock, flags);
578                         regval = readl(regs + i);
579                         if (irq == 0) {
580                                 regval &= ~BIT(INTERRUPT_ENABLE_OFF);
581                                 dev_dbg(&gpio_dev->pdev->dev,
582                                         "Disabling spurious GPIO IRQ %d\n",
583                                         irqnr + i);
584                         }
585                         writel(regval, regs + i);
586                         raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
587                         ret = IRQ_HANDLED;
588                 }
589         }
590
591         /* Signal EOI to the GPIO unit */
592         raw_spin_lock_irqsave(&gpio_dev->lock, flags);
593         regval = readl(gpio_dev->base + WAKE_INT_MASTER_REG);
594         regval |= EOI_MASK;
595         writel(regval, gpio_dev->base + WAKE_INT_MASTER_REG);
596         raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
597
598         return ret;
599 }
600
601 static int amd_get_groups_count(struct pinctrl_dev *pctldev)
602 {
603         struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
604
605         return gpio_dev->ngroups;
606 }
607
608 static const char *amd_get_group_name(struct pinctrl_dev *pctldev,
609                                       unsigned group)
610 {
611         struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
612
613         return gpio_dev->groups[group].name;
614 }
615
616 static int amd_get_group_pins(struct pinctrl_dev *pctldev,
617                               unsigned group,
618                               const unsigned **pins,
619                               unsigned *num_pins)
620 {
621         struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
622
623         *pins = gpio_dev->groups[group].pins;
624         *num_pins = gpio_dev->groups[group].npins;
625         return 0;
626 }
627
628 static const struct pinctrl_ops amd_pinctrl_ops = {
629         .get_groups_count       = amd_get_groups_count,
630         .get_group_name         = amd_get_group_name,
631         .get_group_pins         = amd_get_group_pins,
632 #ifdef CONFIG_OF
633         .dt_node_to_map         = pinconf_generic_dt_node_to_map_group,
634         .dt_free_map            = pinctrl_utils_free_map,
635 #endif
636 };
637
638 static int amd_pinconf_get(struct pinctrl_dev *pctldev,
639                           unsigned int pin,
640                           unsigned long *config)
641 {
642         u32 pin_reg;
643         unsigned arg;
644         unsigned long flags;
645         struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
646         enum pin_config_param param = pinconf_to_config_param(*config);
647
648         raw_spin_lock_irqsave(&gpio_dev->lock, flags);
649         pin_reg = readl(gpio_dev->base + pin*4);
650         raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
651         switch (param) {
652         case PIN_CONFIG_INPUT_DEBOUNCE:
653                 arg = pin_reg & DB_TMR_OUT_MASK;
654                 break;
655
656         case PIN_CONFIG_BIAS_PULL_DOWN:
657                 arg = (pin_reg >> PULL_DOWN_ENABLE_OFF) & BIT(0);
658                 break;
659
660         case PIN_CONFIG_BIAS_PULL_UP:
661                 arg = (pin_reg >> PULL_UP_SEL_OFF) & (BIT(0) | BIT(1));
662                 break;
663
664         case PIN_CONFIG_DRIVE_STRENGTH:
665                 arg = (pin_reg >> DRV_STRENGTH_SEL_OFF) & DRV_STRENGTH_SEL_MASK;
666                 break;
667
668         default:
669                 dev_err(&gpio_dev->pdev->dev, "Invalid config param %04x\n",
670                         param);
671                 return -ENOTSUPP;
672         }
673
674         *config = pinconf_to_config_packed(param, arg);
675
676         return 0;
677 }
678
679 static int amd_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
680                                 unsigned long *configs, unsigned num_configs)
681 {
682         int i;
683         u32 arg;
684         int ret = 0;
685         u32 pin_reg;
686         unsigned long flags;
687         enum pin_config_param param;
688         struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
689
690         raw_spin_lock_irqsave(&gpio_dev->lock, flags);
691         for (i = 0; i < num_configs; i++) {
692                 param = pinconf_to_config_param(configs[i]);
693                 arg = pinconf_to_config_argument(configs[i]);
694                 pin_reg = readl(gpio_dev->base + pin*4);
695
696                 switch (param) {
697                 case PIN_CONFIG_INPUT_DEBOUNCE:
698                         pin_reg &= ~DB_TMR_OUT_MASK;
699                         pin_reg |= arg & DB_TMR_OUT_MASK;
700                         break;
701
702                 case PIN_CONFIG_BIAS_PULL_DOWN:
703                         pin_reg &= ~BIT(PULL_DOWN_ENABLE_OFF);
704                         pin_reg |= (arg & BIT(0)) << PULL_DOWN_ENABLE_OFF;
705                         break;
706
707                 case PIN_CONFIG_BIAS_PULL_UP:
708                         pin_reg &= ~BIT(PULL_UP_SEL_OFF);
709                         pin_reg |= (arg & BIT(0)) << PULL_UP_SEL_OFF;
710                         pin_reg &= ~BIT(PULL_UP_ENABLE_OFF);
711                         pin_reg |= ((arg>>1) & BIT(0)) << PULL_UP_ENABLE_OFF;
712                         break;
713
714                 case PIN_CONFIG_DRIVE_STRENGTH:
715                         pin_reg &= ~(DRV_STRENGTH_SEL_MASK
716                                         << DRV_STRENGTH_SEL_OFF);
717                         pin_reg |= (arg & DRV_STRENGTH_SEL_MASK)
718                                         << DRV_STRENGTH_SEL_OFF;
719                         break;
720
721                 default:
722                         dev_err(&gpio_dev->pdev->dev,
723                                 "Invalid config param %04x\n", param);
724                         ret = -ENOTSUPP;
725                 }
726
727                 writel(pin_reg, gpio_dev->base + pin*4);
728         }
729         raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
730
731         return ret;
732 }
733
734 static int amd_pinconf_group_get(struct pinctrl_dev *pctldev,
735                                 unsigned int group,
736                                 unsigned long *config)
737 {
738         const unsigned *pins;
739         unsigned npins;
740         int ret;
741
742         ret = amd_get_group_pins(pctldev, group, &pins, &npins);
743         if (ret)
744                 return ret;
745
746         if (amd_pinconf_get(pctldev, pins[0], config))
747                         return -ENOTSUPP;
748
749         return 0;
750 }
751
752 static int amd_pinconf_group_set(struct pinctrl_dev *pctldev,
753                                 unsigned group, unsigned long *configs,
754                                 unsigned num_configs)
755 {
756         const unsigned *pins;
757         unsigned npins;
758         int i, ret;
759
760         ret = amd_get_group_pins(pctldev, group, &pins, &npins);
761         if (ret)
762                 return ret;
763         for (i = 0; i < npins; i++) {
764                 if (amd_pinconf_set(pctldev, pins[i], configs, num_configs))
765                         return -ENOTSUPP;
766         }
767         return 0;
768 }
769
770 static const struct pinconf_ops amd_pinconf_ops = {
771         .pin_config_get         = amd_pinconf_get,
772         .pin_config_set         = amd_pinconf_set,
773         .pin_config_group_get = amd_pinconf_group_get,
774         .pin_config_group_set = amd_pinconf_group_set,
775 };
776
777 static void amd_gpio_irq_init(struct amd_gpio *gpio_dev)
778 {
779         struct pinctrl_desc *desc = gpio_dev->pctrl->desc;
780         unsigned long flags;
781         u32 pin_reg, mask;
782         int i;
783
784         mask = BIT(WAKE_CNTRL_OFF_S0I3) | BIT(WAKE_CNTRL_OFF_S3) |
785                 BIT(INTERRUPT_MASK_OFF) | BIT(INTERRUPT_ENABLE_OFF) |
786                 BIT(WAKE_CNTRL_OFF_S4);
787
788         for (i = 0; i < desc->npins; i++) {
789                 int pin = desc->pins[i].number;
790                 const struct pin_desc *pd = pin_desc_get(gpio_dev->pctrl, pin);
791
792                 if (!pd)
793                         continue;
794
795                 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
796
797                 pin_reg = readl(gpio_dev->base + i * 4);
798                 pin_reg &= ~mask;
799                 writel(pin_reg, gpio_dev->base + i * 4);
800
801                 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
802         }
803 }
804
805 #ifdef CONFIG_PM_SLEEP
806 static bool amd_gpio_should_save(struct amd_gpio *gpio_dev, unsigned int pin)
807 {
808         const struct pin_desc *pd = pin_desc_get(gpio_dev->pctrl, pin);
809
810         if (!pd)
811                 return false;
812
813         /*
814          * Only restore the pin if it is actually in use by the kernel (or
815          * by userspace).
816          */
817         if (pd->mux_owner || pd->gpio_owner ||
818             gpiochip_line_is_irq(&gpio_dev->gc, pin))
819                 return true;
820
821         return false;
822 }
823
824 static int amd_gpio_suspend(struct device *dev)
825 {
826         struct platform_device *pdev = to_platform_device(dev);
827         struct amd_gpio *gpio_dev = platform_get_drvdata(pdev);
828         struct pinctrl_desc *desc = gpio_dev->pctrl->desc;
829         unsigned long flags;
830         int i;
831
832         for (i = 0; i < desc->npins; i++) {
833                 int pin = desc->pins[i].number;
834
835                 if (!amd_gpio_should_save(gpio_dev, pin))
836                         continue;
837
838                 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
839                 gpio_dev->saved_regs[i] = readl(gpio_dev->base + pin * 4) & ~PIN_IRQ_PENDING;
840                 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
841         }
842
843         return 0;
844 }
845
846 static int amd_gpio_resume(struct device *dev)
847 {
848         struct platform_device *pdev = to_platform_device(dev);
849         struct amd_gpio *gpio_dev = platform_get_drvdata(pdev);
850         struct pinctrl_desc *desc = gpio_dev->pctrl->desc;
851         unsigned long flags;
852         int i;
853
854         for (i = 0; i < desc->npins; i++) {
855                 int pin = desc->pins[i].number;
856
857                 if (!amd_gpio_should_save(gpio_dev, pin))
858                         continue;
859
860                 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
861                 gpio_dev->saved_regs[i] |= readl(gpio_dev->base + pin * 4) & PIN_IRQ_PENDING;
862                 writel(gpio_dev->saved_regs[i], gpio_dev->base + pin * 4);
863                 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
864         }
865
866         return 0;
867 }
868
869 static const struct dev_pm_ops amd_gpio_pm_ops = {
870         SET_LATE_SYSTEM_SLEEP_PM_OPS(amd_gpio_suspend,
871                                      amd_gpio_resume)
872 };
873 #endif
874
875 static struct pinctrl_desc amd_pinctrl_desc = {
876         .pins   = kerncz_pins,
877         .npins = ARRAY_SIZE(kerncz_pins),
878         .pctlops = &amd_pinctrl_ops,
879         .confops = &amd_pinconf_ops,
880         .owner = THIS_MODULE,
881 };
882
883 static int amd_gpio_probe(struct platform_device *pdev)
884 {
885         int ret = 0;
886         int irq_base;
887         struct resource *res;
888         struct amd_gpio *gpio_dev;
889         struct gpio_irq_chip *girq;
890
891         gpio_dev = devm_kzalloc(&pdev->dev,
892                                 sizeof(struct amd_gpio), GFP_KERNEL);
893         if (!gpio_dev)
894                 return -ENOMEM;
895
896         raw_spin_lock_init(&gpio_dev->lock);
897
898         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
899         if (!res) {
900                 dev_err(&pdev->dev, "Failed to get gpio io resource.\n");
901                 return -EINVAL;
902         }
903
904         gpio_dev->base = devm_ioremap_nocache(&pdev->dev, res->start,
905                                                 resource_size(res));
906         if (!gpio_dev->base)
907                 return -ENOMEM;
908
909         irq_base = platform_get_irq(pdev, 0);
910         if (irq_base < 0) {
911                 dev_err(&pdev->dev, "Failed to get gpio IRQ: %d\n", irq_base);
912                 return irq_base;
913         }
914
915 #ifdef CONFIG_PM_SLEEP
916         gpio_dev->saved_regs = devm_kcalloc(&pdev->dev, amd_pinctrl_desc.npins,
917                                             sizeof(*gpio_dev->saved_regs),
918                                             GFP_KERNEL);
919         if (!gpio_dev->saved_regs)
920                 return -ENOMEM;
921 #endif
922
923         gpio_dev->pdev = pdev;
924         gpio_dev->gc.get_direction      = amd_gpio_get_direction;
925         gpio_dev->gc.direction_input    = amd_gpio_direction_input;
926         gpio_dev->gc.direction_output   = amd_gpio_direction_output;
927         gpio_dev->gc.get                        = amd_gpio_get_value;
928         gpio_dev->gc.set                        = amd_gpio_set_value;
929         gpio_dev->gc.set_config         = amd_gpio_set_config;
930         gpio_dev->gc.dbg_show           = amd_gpio_dbg_show;
931
932         gpio_dev->gc.base               = -1;
933         gpio_dev->gc.label                      = pdev->name;
934         gpio_dev->gc.owner                      = THIS_MODULE;
935         gpio_dev->gc.parent                     = &pdev->dev;
936         gpio_dev->gc.ngpio                      = resource_size(res) / 4;
937 #if defined(CONFIG_OF_GPIO)
938         gpio_dev->gc.of_node                    = pdev->dev.of_node;
939 #endif
940
941         gpio_dev->hwbank_num = gpio_dev->gc.ngpio / 64;
942         gpio_dev->groups = kerncz_groups;
943         gpio_dev->ngroups = ARRAY_SIZE(kerncz_groups);
944
945         amd_pinctrl_desc.name = dev_name(&pdev->dev);
946         gpio_dev->pctrl = devm_pinctrl_register(&pdev->dev, &amd_pinctrl_desc,
947                                                 gpio_dev);
948         if (IS_ERR(gpio_dev->pctrl)) {
949                 dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
950                 return PTR_ERR(gpio_dev->pctrl);
951         }
952
953         /* Disable and mask interrupts */
954         amd_gpio_irq_init(gpio_dev);
955
956         girq = &gpio_dev->gc.irq;
957         girq->chip = &amd_gpio_irqchip;
958         /* This will let us handle the parent IRQ in the driver */
959         girq->parent_handler = NULL;
960         girq->num_parents = 0;
961         girq->parents = NULL;
962         girq->default_type = IRQ_TYPE_NONE;
963         girq->handler = handle_simple_irq;
964
965         ret = gpiochip_add_data(&gpio_dev->gc, gpio_dev);
966         if (ret)
967                 return ret;
968
969         ret = gpiochip_add_pin_range(&gpio_dev->gc, dev_name(&pdev->dev),
970                                 0, 0, gpio_dev->gc.ngpio);
971         if (ret) {
972                 dev_err(&pdev->dev, "Failed to add pin range\n");
973                 goto out2;
974         }
975
976         ret = devm_request_irq(&pdev->dev, irq_base, amd_gpio_irq_handler,
977                                IRQF_SHARED, KBUILD_MODNAME, gpio_dev);
978         if (ret)
979                 goto out2;
980
981         platform_set_drvdata(pdev, gpio_dev);
982
983         dev_dbg(&pdev->dev, "amd gpio driver loaded\n");
984         return ret;
985
986 out2:
987         gpiochip_remove(&gpio_dev->gc);
988
989         return ret;
990 }
991
992 static int amd_gpio_remove(struct platform_device *pdev)
993 {
994         struct amd_gpio *gpio_dev;
995
996         gpio_dev = platform_get_drvdata(pdev);
997
998         gpiochip_remove(&gpio_dev->gc);
999
1000         return 0;
1001 }
1002
1003 static const struct acpi_device_id amd_gpio_acpi_match[] = {
1004         { "AMD0030", 0 },
1005         { "AMDI0030", 0},
1006         { "AMDI0031", 0},
1007         { },
1008 };
1009 MODULE_DEVICE_TABLE(acpi, amd_gpio_acpi_match);
1010
1011 static struct platform_driver amd_gpio_driver = {
1012         .driver         = {
1013                 .name   = "amd_gpio",
1014                 .acpi_match_table = ACPI_PTR(amd_gpio_acpi_match),
1015 #ifdef CONFIG_PM_SLEEP
1016                 .pm     = &amd_gpio_pm_ops,
1017 #endif
1018         },
1019         .probe          = amd_gpio_probe,
1020         .remove         = amd_gpio_remove,
1021 };
1022
1023 module_platform_driver(amd_gpio_driver);
1024
1025 MODULE_LICENSE("GPL v2");
1026 MODULE_AUTHOR("Ken Xue <Ken.Xue@amd.com>, Jeff Wu <Jeff.Wu@amd.com>");
1027 MODULE_DESCRIPTION("AMD GPIO pinctrl driver");