]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/pinctrl/pinctrl-amd.c
pinctrl/amd: Update contact information for AMD pinctrl/amd
[karo-tx-linux.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 "pinctrl-utils.h"
40 #include "pinctrl-amd.h"
41
42 static int amd_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
43 {
44         unsigned long flags;
45         u32 pin_reg;
46         struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
47
48         raw_spin_lock_irqsave(&gpio_dev->lock, flags);
49         pin_reg = readl(gpio_dev->base + offset * 4);
50         pin_reg &= ~BIT(OUTPUT_ENABLE_OFF);
51         writel(pin_reg, gpio_dev->base + offset * 4);
52         raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
53
54         return 0;
55 }
56
57 static int amd_gpio_direction_output(struct gpio_chip *gc, unsigned offset,
58                 int value)
59 {
60         u32 pin_reg;
61         unsigned long flags;
62         struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
63
64         raw_spin_lock_irqsave(&gpio_dev->lock, flags);
65         pin_reg = readl(gpio_dev->base + offset * 4);
66         pin_reg |= BIT(OUTPUT_ENABLE_OFF);
67         if (value)
68                 pin_reg |= BIT(OUTPUT_VALUE_OFF);
69         else
70                 pin_reg &= ~BIT(OUTPUT_VALUE_OFF);
71         writel(pin_reg, gpio_dev->base + offset * 4);
72         raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
73
74         return 0;
75 }
76
77 static int amd_gpio_get_value(struct gpio_chip *gc, unsigned offset)
78 {
79         u32 pin_reg;
80         unsigned long flags;
81         struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
82
83         raw_spin_lock_irqsave(&gpio_dev->lock, flags);
84         pin_reg = readl(gpio_dev->base + offset * 4);
85         raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
86
87         return !!(pin_reg & BIT(PIN_STS_OFF));
88 }
89
90 static void amd_gpio_set_value(struct gpio_chip *gc, unsigned offset, int value)
91 {
92         u32 pin_reg;
93         unsigned long flags;
94         struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
95
96         raw_spin_lock_irqsave(&gpio_dev->lock, flags);
97         pin_reg = readl(gpio_dev->base + offset * 4);
98         if (value)
99                 pin_reg |= BIT(OUTPUT_VALUE_OFF);
100         else
101                 pin_reg &= ~BIT(OUTPUT_VALUE_OFF);
102         writel(pin_reg, gpio_dev->base + offset * 4);
103         raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
104 }
105
106 static int amd_gpio_set_debounce(struct gpio_chip *gc, unsigned offset,
107                 unsigned debounce)
108 {
109         u32 time;
110         u32 pin_reg;
111         int ret = 0;
112         unsigned long flags;
113         struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
114
115         raw_spin_lock_irqsave(&gpio_dev->lock, flags);
116         pin_reg = readl(gpio_dev->base + offset * 4);
117
118         if (debounce) {
119                 pin_reg |= DB_TYPE_REMOVE_GLITCH << DB_CNTRL_OFF;
120                 pin_reg &= ~DB_TMR_OUT_MASK;
121                 /*
122                 Debounce        Debounce        Timer   Max
123                 TmrLarge        TmrOutUnit      Unit    Debounce
124                                                         Time
125                 0       0       61 usec (2 RtcClk)      976 usec
126                 0       1       244 usec (8 RtcClk)     3.9 msec
127                 1       0       15.6 msec (512 RtcClk)  250 msec
128                 1       1       62.5 msec (2048 RtcClk) 1 sec
129                 */
130
131                 if (debounce < 61) {
132                         pin_reg |= 1;
133                         pin_reg &= ~BIT(DB_TMR_OUT_UNIT_OFF);
134                         pin_reg &= ~BIT(DB_TMR_LARGE_OFF);
135                 } else if (debounce < 976) {
136                         time = debounce / 61;
137                         pin_reg |= time & DB_TMR_OUT_MASK;
138                         pin_reg &= ~BIT(DB_TMR_OUT_UNIT_OFF);
139                         pin_reg &= ~BIT(DB_TMR_LARGE_OFF);
140                 } else if (debounce < 3900) {
141                         time = debounce / 244;
142                         pin_reg |= time & DB_TMR_OUT_MASK;
143                         pin_reg |= BIT(DB_TMR_OUT_UNIT_OFF);
144                         pin_reg &= ~BIT(DB_TMR_LARGE_OFF);
145                 } else if (debounce < 250000) {
146                         time = debounce / 15600;
147                         pin_reg |= time & DB_TMR_OUT_MASK;
148                         pin_reg &= ~BIT(DB_TMR_OUT_UNIT_OFF);
149                         pin_reg |= BIT(DB_TMR_LARGE_OFF);
150                 } else if (debounce < 1000000) {
151                         time = debounce / 62500;
152                         pin_reg |= time & DB_TMR_OUT_MASK;
153                         pin_reg |= BIT(DB_TMR_OUT_UNIT_OFF);
154                         pin_reg |= BIT(DB_TMR_LARGE_OFF);
155                 } else {
156                         pin_reg &= ~DB_CNTRl_MASK;
157                         ret = -EINVAL;
158                 }
159         } else {
160                 pin_reg &= ~BIT(DB_TMR_OUT_UNIT_OFF);
161                 pin_reg &= ~BIT(DB_TMR_LARGE_OFF);
162                 pin_reg &= ~DB_TMR_OUT_MASK;
163                 pin_reg &= ~DB_CNTRl_MASK;
164         }
165         writel(pin_reg, gpio_dev->base + offset * 4);
166         raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
167
168         return ret;
169 }
170
171 static int amd_gpio_set_config(struct gpio_chip *gc, unsigned offset,
172                                unsigned long config)
173 {
174         u32 debounce;
175
176         if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE)
177                 return -ENOTSUPP;
178
179         debounce = pinconf_to_config_argument(config);
180         return amd_gpio_set_debounce(gc, offset, debounce);
181 }
182
183 #ifdef CONFIG_DEBUG_FS
184 static void amd_gpio_dbg_show(struct seq_file *s, struct gpio_chip *gc)
185 {
186         u32 pin_reg;
187         unsigned long flags;
188         unsigned int bank, i, pin_num;
189         struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
190
191         char *level_trig;
192         char *active_level;
193         char *interrupt_enable;
194         char *interrupt_mask;
195         char *wake_cntrl0;
196         char *wake_cntrl1;
197         char *wake_cntrl2;
198         char *pin_sts;
199         char *pull_up_sel;
200         char *pull_up_enable;
201         char *pull_down_enable;
202         char *output_value;
203         char *output_enable;
204
205         for (bank = 0; bank < gpio_dev->hwbank_num; bank++) {
206                 seq_printf(s, "GPIO bank%d\t", bank);
207
208                 switch (bank) {
209                 case 0:
210                         i = 0;
211                         pin_num = AMD_GPIO_PINS_BANK0;
212                         break;
213                 case 1:
214                         i = 64;
215                         pin_num = AMD_GPIO_PINS_BANK1 + i;
216                         break;
217                 case 2:
218                         i = 128;
219                         pin_num = AMD_GPIO_PINS_BANK2 + i;
220                         break;
221                 case 3:
222                         i = 192;
223                         pin_num = AMD_GPIO_PINS_BANK3 + i;
224                         break;
225                 default:
226                         /* Illegal bank number, ignore */
227                         continue;
228                 }
229                 for (; i < pin_num; i++) {
230                         seq_printf(s, "pin%d\t", i);
231                         raw_spin_lock_irqsave(&gpio_dev->lock, flags);
232                         pin_reg = readl(gpio_dev->base + i * 4);
233                         raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
234
235                         if (pin_reg & BIT(INTERRUPT_ENABLE_OFF)) {
236                                 interrupt_enable = "interrupt is enabled|";
237
238                                 if (!(pin_reg & BIT(ACTIVE_LEVEL_OFF)) &&
239                                     !(pin_reg & BIT(ACTIVE_LEVEL_OFF + 1)))
240                                         active_level = "Active low|";
241                                 else if (pin_reg & BIT(ACTIVE_LEVEL_OFF) &&
242                                          !(pin_reg & BIT(ACTIVE_LEVEL_OFF + 1)))
243                                         active_level = "Active high|";
244                                 else if (!(pin_reg & BIT(ACTIVE_LEVEL_OFF)) &&
245                                          pin_reg & BIT(ACTIVE_LEVEL_OFF + 1))
246                                         active_level = "Active on both|";
247                                 else
248                                         active_level = "Unknown Active level|";
249
250                                 if (pin_reg & BIT(LEVEL_TRIG_OFF))
251                                         level_trig = "Level trigger|";
252                                 else
253                                         level_trig = "Edge trigger|";
254
255                         } else {
256                                 interrupt_enable =
257                                         "interrupt is disabled|";
258                                 active_level = " ";
259                                 level_trig = " ";
260                         }
261
262                         if (pin_reg & BIT(INTERRUPT_MASK_OFF))
263                                 interrupt_mask =
264                                         "interrupt is unmasked|";
265                         else
266                                 interrupt_mask =
267                                         "interrupt is masked|";
268
269                         if (pin_reg & BIT(WAKE_CNTRL_OFF_S0I3))
270                                 wake_cntrl0 = "enable wakeup in S0i3 state|";
271                         else
272                                 wake_cntrl0 = "disable wakeup in S0i3 state|";
273
274                         if (pin_reg & BIT(WAKE_CNTRL_OFF_S3))
275                                 wake_cntrl1 = "enable wakeup in S3 state|";
276                         else
277                                 wake_cntrl1 = "disable wakeup in S3 state|";
278
279                         if (pin_reg & BIT(WAKE_CNTRL_OFF_S4))
280                                 wake_cntrl2 = "enable wakeup in S4/S5 state|";
281                         else
282                                 wake_cntrl2 = "disable wakeup in S4/S5 state|";
283
284                         if (pin_reg & BIT(PULL_UP_ENABLE_OFF)) {
285                                 pull_up_enable = "pull-up is enabled|";
286                                 if (pin_reg & BIT(PULL_UP_SEL_OFF))
287                                         pull_up_sel = "8k pull-up|";
288                                 else
289                                         pull_up_sel = "4k pull-up|";
290                         } else {
291                                 pull_up_enable = "pull-up is disabled|";
292                                 pull_up_sel = " ";
293                         }
294
295                         if (pin_reg & BIT(PULL_DOWN_ENABLE_OFF))
296                                 pull_down_enable = "pull-down is enabled|";
297                         else
298                                 pull_down_enable = "Pull-down is disabled|";
299
300                         if (pin_reg & BIT(OUTPUT_ENABLE_OFF)) {
301                                 pin_sts = " ";
302                                 output_enable = "output is enabled|";
303                                 if (pin_reg & BIT(OUTPUT_VALUE_OFF))
304                                         output_value = "output is high|";
305                                 else
306                                         output_value = "output is low|";
307                         } else {
308                                 output_enable = "output is disabled|";
309                                 output_value = " ";
310
311                                 if (pin_reg & BIT(PIN_STS_OFF))
312                                         pin_sts = "input is high|";
313                                 else
314                                         pin_sts = "input is low|";
315                         }
316
317                         seq_printf(s, "%s %s %s %s %s %s\n"
318                                 " %s %s %s %s %s %s %s 0x%x\n",
319                                 level_trig, active_level, interrupt_enable,
320                                 interrupt_mask, wake_cntrl0, wake_cntrl1,
321                                 wake_cntrl2, pin_sts, pull_up_sel,
322                                 pull_up_enable, pull_down_enable,
323                                 output_value, output_enable, pin_reg);
324                 }
325         }
326 }
327 #else
328 #define amd_gpio_dbg_show NULL
329 #endif
330
331 static void amd_gpio_irq_enable(struct irq_data *d)
332 {
333         u32 pin_reg;
334         unsigned long flags;
335         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
336         struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
337
338         raw_spin_lock_irqsave(&gpio_dev->lock, flags);
339         pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
340         pin_reg |= BIT(INTERRUPT_ENABLE_OFF);
341         pin_reg |= BIT(INTERRUPT_MASK_OFF);
342         writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
343         raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
344 }
345
346 static void amd_gpio_irq_disable(struct irq_data *d)
347 {
348         u32 pin_reg;
349         unsigned long flags;
350         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
351         struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
352
353         raw_spin_lock_irqsave(&gpio_dev->lock, flags);
354         pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
355         pin_reg &= ~BIT(INTERRUPT_ENABLE_OFF);
356         pin_reg &= ~BIT(INTERRUPT_MASK_OFF);
357         writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
358         raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
359 }
360
361 static void amd_gpio_irq_mask(struct irq_data *d)
362 {
363         u32 pin_reg;
364         unsigned long flags;
365         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
366         struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
367
368         raw_spin_lock_irqsave(&gpio_dev->lock, flags);
369         pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
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_unmask(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_eoi(struct irq_data *d)
390 {
391         u32 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         reg = readl(gpio_dev->base + WAKE_INT_MASTER_REG);
398         reg |= EOI_MASK;
399         writel(reg, gpio_dev->base + WAKE_INT_MASTER_REG);
400         raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
401 }
402
403 static int amd_gpio_irq_set_type(struct irq_data *d, unsigned int type)
404 {
405         int ret = 0;
406         u32 pin_reg;
407         unsigned long flags, irq_flags;
408         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
409         struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
410
411         raw_spin_lock_irqsave(&gpio_dev->lock, flags);
412         pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
413
414         /* Ignore the settings coming from the client and
415          * read the values from the ACPI tables
416          * while setting the trigger type
417          */
418
419         irq_flags = irq_get_trigger_type(d->irq);
420         if (irq_flags != IRQ_TYPE_NONE)
421                 type = irq_flags;
422
423         switch (type & IRQ_TYPE_SENSE_MASK) {
424         case IRQ_TYPE_EDGE_RISING:
425                 pin_reg &= ~BIT(LEVEL_TRIG_OFF);
426                 pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
427                 pin_reg |= ACTIVE_HIGH << ACTIVE_LEVEL_OFF;
428                 pin_reg |= DB_TYPE_REMOVE_GLITCH << DB_CNTRL_OFF;
429                 irq_set_handler_locked(d, handle_edge_irq);
430                 break;
431
432         case IRQ_TYPE_EDGE_FALLING:
433                 pin_reg &= ~BIT(LEVEL_TRIG_OFF);
434                 pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
435                 pin_reg |= ACTIVE_LOW << ACTIVE_LEVEL_OFF;
436                 pin_reg |= DB_TYPE_REMOVE_GLITCH << DB_CNTRL_OFF;
437                 irq_set_handler_locked(d, handle_edge_irq);
438                 break;
439
440         case IRQ_TYPE_EDGE_BOTH:
441                 pin_reg &= ~BIT(LEVEL_TRIG_OFF);
442                 pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
443                 pin_reg |= BOTH_EADGE << ACTIVE_LEVEL_OFF;
444                 pin_reg |= DB_TYPE_REMOVE_GLITCH << DB_CNTRL_OFF;
445                 irq_set_handler_locked(d, handle_edge_irq);
446                 break;
447
448         case IRQ_TYPE_LEVEL_HIGH:
449                 pin_reg |= LEVEL_TRIGGER << LEVEL_TRIG_OFF;
450                 pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
451                 pin_reg |= ACTIVE_HIGH << ACTIVE_LEVEL_OFF;
452                 pin_reg &= ~(DB_CNTRl_MASK << DB_CNTRL_OFF);
453                 pin_reg |= DB_TYPE_PRESERVE_LOW_GLITCH << DB_CNTRL_OFF;
454                 irq_set_handler_locked(d, handle_level_irq);
455                 break;
456
457         case IRQ_TYPE_LEVEL_LOW:
458                 pin_reg |= LEVEL_TRIGGER << LEVEL_TRIG_OFF;
459                 pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
460                 pin_reg |= ACTIVE_LOW << ACTIVE_LEVEL_OFF;
461                 pin_reg &= ~(DB_CNTRl_MASK << DB_CNTRL_OFF);
462                 pin_reg |= DB_TYPE_PRESERVE_HIGH_GLITCH << DB_CNTRL_OFF;
463                 irq_set_handler_locked(d, handle_level_irq);
464                 break;
465
466         case IRQ_TYPE_NONE:
467                 break;
468
469         default:
470                 dev_err(&gpio_dev->pdev->dev, "Invalid type value\n");
471                 ret = -EINVAL;
472         }
473
474         pin_reg |= CLR_INTR_STAT << INTERRUPT_STS_OFF;
475         writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
476         raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
477
478         return ret;
479 }
480
481 static void amd_irq_ack(struct irq_data *d)
482 {
483         /*
484          * based on HW design,there is no need to ack HW
485          * before handle current irq. But this routine is
486          * necessary for handle_edge_irq
487         */
488 }
489
490 static struct irq_chip amd_gpio_irqchip = {
491         .name         = "amd_gpio",
492         .irq_ack      = amd_irq_ack,
493         .irq_enable   = amd_gpio_irq_enable,
494         .irq_disable  = amd_gpio_irq_disable,
495         .irq_mask     = amd_gpio_irq_mask,
496         .irq_unmask   = amd_gpio_irq_unmask,
497         .irq_eoi      = amd_gpio_irq_eoi,
498         .irq_set_type = amd_gpio_irq_set_type,
499         .flags        = IRQCHIP_SKIP_SET_WAKE,
500 };
501
502 static void amd_gpio_irq_handler(struct irq_desc *desc)
503 {
504         u32 i;
505         u32 off;
506         u32 reg;
507         u32 pin_reg;
508         u64 reg64;
509         int handled = 0;
510         unsigned int irq;
511         unsigned long flags;
512         struct irq_chip *chip = irq_desc_get_chip(desc);
513         struct gpio_chip *gc = irq_desc_get_handler_data(desc);
514         struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
515
516         chained_irq_enter(chip, desc);
517         /*enable GPIO interrupt again*/
518         raw_spin_lock_irqsave(&gpio_dev->lock, flags);
519         reg = readl(gpio_dev->base + WAKE_INT_STATUS_REG1);
520         reg64 = reg;
521         reg64 = reg64 << 32;
522
523         reg = readl(gpio_dev->base + WAKE_INT_STATUS_REG0);
524         reg64 |= reg;
525         raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
526
527         /*
528          * first 46 bits indicates interrupt status.
529          * one bit represents four interrupt sources.
530         */
531         for (off = 0; off < 46 ; off++) {
532                 if (reg64 & BIT(off)) {
533                         for (i = 0; i < 4; i++) {
534                                 pin_reg = readl(gpio_dev->base +
535                                                 (off * 4 + i) * 4);
536                                 if ((pin_reg & BIT(INTERRUPT_STS_OFF)) ||
537                                         (pin_reg & BIT(WAKE_STS_OFF))) {
538                                         irq = irq_find_mapping(gc->irqdomain,
539                                                                 off * 4 + i);
540                                         generic_handle_irq(irq);
541                                         writel(pin_reg,
542                                                 gpio_dev->base
543                                                 + (off * 4 + i) * 4);
544                                         handled++;
545                                 }
546                         }
547                 }
548         }
549
550         if (handled == 0)
551                 handle_bad_irq(desc);
552
553         raw_spin_lock_irqsave(&gpio_dev->lock, flags);
554         reg = readl(gpio_dev->base + WAKE_INT_MASTER_REG);
555         reg |= EOI_MASK;
556         writel(reg, gpio_dev->base + WAKE_INT_MASTER_REG);
557         raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
558
559         chained_irq_exit(chip, desc);
560 }
561
562 static int amd_get_groups_count(struct pinctrl_dev *pctldev)
563 {
564         struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
565
566         return gpio_dev->ngroups;
567 }
568
569 static const char *amd_get_group_name(struct pinctrl_dev *pctldev,
570                                       unsigned group)
571 {
572         struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
573
574         return gpio_dev->groups[group].name;
575 }
576
577 static int amd_get_group_pins(struct pinctrl_dev *pctldev,
578                               unsigned group,
579                               const unsigned **pins,
580                               unsigned *num_pins)
581 {
582         struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
583
584         *pins = gpio_dev->groups[group].pins;
585         *num_pins = gpio_dev->groups[group].npins;
586         return 0;
587 }
588
589 static const struct pinctrl_ops amd_pinctrl_ops = {
590         .get_groups_count       = amd_get_groups_count,
591         .get_group_name         = amd_get_group_name,
592         .get_group_pins         = amd_get_group_pins,
593 #ifdef CONFIG_OF
594         .dt_node_to_map         = pinconf_generic_dt_node_to_map_group,
595         .dt_free_map            = pinctrl_utils_free_map,
596 #endif
597 };
598
599 static int amd_pinconf_get(struct pinctrl_dev *pctldev,
600                           unsigned int pin,
601                           unsigned long *config)
602 {
603         u32 pin_reg;
604         unsigned arg;
605         unsigned long flags;
606         struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
607         enum pin_config_param param = pinconf_to_config_param(*config);
608
609         raw_spin_lock_irqsave(&gpio_dev->lock, flags);
610         pin_reg = readl(gpio_dev->base + pin*4);
611         raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
612         switch (param) {
613         case PIN_CONFIG_INPUT_DEBOUNCE:
614                 arg = pin_reg & DB_TMR_OUT_MASK;
615                 break;
616
617         case PIN_CONFIG_BIAS_PULL_DOWN:
618                 arg = (pin_reg >> PULL_DOWN_ENABLE_OFF) & BIT(0);
619                 break;
620
621         case PIN_CONFIG_BIAS_PULL_UP:
622                 arg = (pin_reg >> PULL_UP_SEL_OFF) & (BIT(0) | BIT(1));
623                 break;
624
625         case PIN_CONFIG_DRIVE_STRENGTH:
626                 arg = (pin_reg >> DRV_STRENGTH_SEL_OFF) & DRV_STRENGTH_SEL_MASK;
627                 break;
628
629         default:
630                 dev_err(&gpio_dev->pdev->dev, "Invalid config param %04x\n",
631                         param);
632                 return -ENOTSUPP;
633         }
634
635         *config = pinconf_to_config_packed(param, arg);
636
637         return 0;
638 }
639
640 static int amd_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
641                                 unsigned long *configs, unsigned num_configs)
642 {
643         int i;
644         u32 arg;
645         int ret = 0;
646         u32 pin_reg;
647         unsigned long flags;
648         enum pin_config_param param;
649         struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
650
651         raw_spin_lock_irqsave(&gpio_dev->lock, flags);
652         for (i = 0; i < num_configs; i++) {
653                 param = pinconf_to_config_param(configs[i]);
654                 arg = pinconf_to_config_argument(configs[i]);
655                 pin_reg = readl(gpio_dev->base + pin*4);
656
657                 switch (param) {
658                 case PIN_CONFIG_INPUT_DEBOUNCE:
659                         pin_reg &= ~DB_TMR_OUT_MASK;
660                         pin_reg |= arg & DB_TMR_OUT_MASK;
661                         break;
662
663                 case PIN_CONFIG_BIAS_PULL_DOWN:
664                         pin_reg &= ~BIT(PULL_DOWN_ENABLE_OFF);
665                         pin_reg |= (arg & BIT(0)) << PULL_DOWN_ENABLE_OFF;
666                         break;
667
668                 case PIN_CONFIG_BIAS_PULL_UP:
669                         pin_reg &= ~BIT(PULL_UP_SEL_OFF);
670                         pin_reg |= (arg & BIT(0)) << PULL_UP_SEL_OFF;
671                         pin_reg &= ~BIT(PULL_UP_ENABLE_OFF);
672                         pin_reg |= ((arg>>1) & BIT(0)) << PULL_UP_ENABLE_OFF;
673                         break;
674
675                 case PIN_CONFIG_DRIVE_STRENGTH:
676                         pin_reg &= ~(DRV_STRENGTH_SEL_MASK
677                                         << DRV_STRENGTH_SEL_OFF);
678                         pin_reg |= (arg & DRV_STRENGTH_SEL_MASK)
679                                         << DRV_STRENGTH_SEL_OFF;
680                         break;
681
682                 default:
683                         dev_err(&gpio_dev->pdev->dev,
684                                 "Invalid config param %04x\n", param);
685                         ret = -ENOTSUPP;
686                 }
687
688                 writel(pin_reg, gpio_dev->base + pin*4);
689         }
690         raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
691
692         return ret;
693 }
694
695 static int amd_pinconf_group_get(struct pinctrl_dev *pctldev,
696                                 unsigned int group,
697                                 unsigned long *config)
698 {
699         const unsigned *pins;
700         unsigned npins;
701         int ret;
702
703         ret = amd_get_group_pins(pctldev, group, &pins, &npins);
704         if (ret)
705                 return ret;
706
707         if (amd_pinconf_get(pctldev, pins[0], config))
708                         return -ENOTSUPP;
709
710         return 0;
711 }
712
713 static int amd_pinconf_group_set(struct pinctrl_dev *pctldev,
714                                 unsigned group, unsigned long *configs,
715                                 unsigned num_configs)
716 {
717         const unsigned *pins;
718         unsigned npins;
719         int i, ret;
720
721         ret = amd_get_group_pins(pctldev, group, &pins, &npins);
722         if (ret)
723                 return ret;
724         for (i = 0; i < npins; i++) {
725                 if (amd_pinconf_set(pctldev, pins[i], configs, num_configs))
726                         return -ENOTSUPP;
727         }
728         return 0;
729 }
730
731 static const struct pinconf_ops amd_pinconf_ops = {
732         .pin_config_get         = amd_pinconf_get,
733         .pin_config_set         = amd_pinconf_set,
734         .pin_config_group_get = amd_pinconf_group_get,
735         .pin_config_group_set = amd_pinconf_group_set,
736 };
737
738 static struct pinctrl_desc amd_pinctrl_desc = {
739         .pins   = kerncz_pins,
740         .npins = ARRAY_SIZE(kerncz_pins),
741         .pctlops = &amd_pinctrl_ops,
742         .confops = &amd_pinconf_ops,
743         .owner = THIS_MODULE,
744 };
745
746 static int amd_gpio_probe(struct platform_device *pdev)
747 {
748         int ret = 0;
749         int irq_base;
750         struct resource *res;
751         struct amd_gpio *gpio_dev;
752
753         gpio_dev = devm_kzalloc(&pdev->dev,
754                                 sizeof(struct amd_gpio), GFP_KERNEL);
755         if (!gpio_dev)
756                 return -ENOMEM;
757
758         raw_spin_lock_init(&gpio_dev->lock);
759
760         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
761         if (!res) {
762                 dev_err(&pdev->dev, "Failed to get gpio io resource.\n");
763                 return -EINVAL;
764         }
765
766         gpio_dev->base = devm_ioremap_nocache(&pdev->dev, res->start,
767                                                 resource_size(res));
768         if (!gpio_dev->base)
769                 return -ENOMEM;
770
771         irq_base = platform_get_irq(pdev, 0);
772         if (irq_base < 0) {
773                 dev_err(&pdev->dev, "Failed to get gpio IRQ.\n");
774                 return -EINVAL;
775         }
776
777         gpio_dev->pdev = pdev;
778         gpio_dev->gc.direction_input    = amd_gpio_direction_input;
779         gpio_dev->gc.direction_output   = amd_gpio_direction_output;
780         gpio_dev->gc.get                        = amd_gpio_get_value;
781         gpio_dev->gc.set                        = amd_gpio_set_value;
782         gpio_dev->gc.set_config         = amd_gpio_set_config;
783         gpio_dev->gc.dbg_show           = amd_gpio_dbg_show;
784
785         gpio_dev->gc.base               = -1;
786         gpio_dev->gc.label                      = pdev->name;
787         gpio_dev->gc.owner                      = THIS_MODULE;
788         gpio_dev->gc.parent                     = &pdev->dev;
789         gpio_dev->gc.ngpio                      = resource_size(res) / 4;
790 #if defined(CONFIG_OF_GPIO)
791         gpio_dev->gc.of_node                    = pdev->dev.of_node;
792 #endif
793
794         gpio_dev->hwbank_num = gpio_dev->gc.ngpio / 64;
795         gpio_dev->groups = kerncz_groups;
796         gpio_dev->ngroups = ARRAY_SIZE(kerncz_groups);
797
798         amd_pinctrl_desc.name = dev_name(&pdev->dev);
799         gpio_dev->pctrl = devm_pinctrl_register(&pdev->dev, &amd_pinctrl_desc,
800                                                 gpio_dev);
801         if (IS_ERR(gpio_dev->pctrl)) {
802                 dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
803                 return PTR_ERR(gpio_dev->pctrl);
804         }
805
806         ret = gpiochip_add_data(&gpio_dev->gc, gpio_dev);
807         if (ret)
808                 return ret;
809
810         ret = gpiochip_add_pin_range(&gpio_dev->gc, dev_name(&pdev->dev),
811                                 0, 0, gpio_dev->gc.ngpio);
812         if (ret) {
813                 dev_err(&pdev->dev, "Failed to add pin range\n");
814                 goto out2;
815         }
816
817         ret = gpiochip_irqchip_add(&gpio_dev->gc,
818                                 &amd_gpio_irqchip,
819                                 0,
820                                 handle_simple_irq,
821                                 IRQ_TYPE_NONE);
822         if (ret) {
823                 dev_err(&pdev->dev, "could not add irqchip\n");
824                 ret = -ENODEV;
825                 goto out2;
826         }
827
828         gpiochip_set_chained_irqchip(&gpio_dev->gc,
829                                  &amd_gpio_irqchip,
830                                  irq_base,
831                                  amd_gpio_irq_handler);
832         platform_set_drvdata(pdev, gpio_dev);
833
834         dev_dbg(&pdev->dev, "amd gpio driver loaded\n");
835         return ret;
836
837 out2:
838         gpiochip_remove(&gpio_dev->gc);
839
840         return ret;
841 }
842
843 static int amd_gpio_remove(struct platform_device *pdev)
844 {
845         struct amd_gpio *gpio_dev;
846
847         gpio_dev = platform_get_drvdata(pdev);
848
849         gpiochip_remove(&gpio_dev->gc);
850
851         return 0;
852 }
853
854 static const struct acpi_device_id amd_gpio_acpi_match[] = {
855         { "AMD0030", 0 },
856         { "AMDI0030", 0},
857         { },
858 };
859 MODULE_DEVICE_TABLE(acpi, amd_gpio_acpi_match);
860
861 static struct platform_driver amd_gpio_driver = {
862         .driver         = {
863                 .name   = "amd_gpio",
864                 .acpi_match_table = ACPI_PTR(amd_gpio_acpi_match),
865         },
866         .probe          = amd_gpio_probe,
867         .remove         = amd_gpio_remove,
868 };
869
870 module_platform_driver(amd_gpio_driver);
871
872 MODULE_LICENSE("GPL v2");
873 MODULE_AUTHOR("Ken Xue <Ken.Xue@amd.com>, Jeff Wu <Jeff.Wu@amd.com>");
874 MODULE_DESCRIPTION("AMD GPIO pinctrl driver");