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