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