Merge branches 'acpi-scan', 'acpi-resource', 'acpi-apei', 'acpi-extlog' and 'acpi...
[sfrench/cifs-2.6.git] / drivers / pinctrl / pinctrl-at91.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * at91 pinctrl driver based on at91 pinmux core
4  *
5  * Copyright (C) 2011-2012 Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
6  */
7
8 #include <linux/clk.h>
9 #include <linux/err.h>
10 #include <linux/init.h>
11 #include <linux/of.h>
12 #include <linux/of_device.h>
13 #include <linux/of_address.h>
14 #include <linux/of_irq.h>
15 #include <linux/slab.h>
16 #include <linux/interrupt.h>
17 #include <linux/io.h>
18 #include <linux/gpio/driver.h>
19 #include <linux/pinctrl/machine.h>
20 #include <linux/pinctrl/pinconf.h>
21 #include <linux/pinctrl/pinctrl.h>
22 #include <linux/pinctrl/pinmux.h>
23 /* Since we request GPIOs from ourself */
24 #include <linux/pinctrl/consumer.h>
25 #include <linux/pm.h>
26
27 #include "pinctrl-at91.h"
28 #include "core.h"
29
30 #define MAX_GPIO_BANKS          5
31 #define MAX_NB_GPIO_PER_BANK    32
32
33 struct at91_pinctrl_mux_ops;
34
35 /**
36  * struct at91_gpio_chip: at91 gpio chip
37  * @chip: gpio chip
38  * @range: gpio range
39  * @next: bank sharing same clock
40  * @pioc_hwirq: PIO bank interrupt identifier on AIC
41  * @pioc_virq: PIO bank Linux virtual interrupt
42  * @pioc_idx: PIO bank index
43  * @regbase: PIO bank virtual address
44  * @clock: associated clock
45  * @ops: at91 pinctrl mux ops
46  * @wakeups: wakeup interrupts
47  * @backups: interrupts disabled in suspend
48  * @id: gpio chip identifier
49  */
50 struct at91_gpio_chip {
51         struct gpio_chip        chip;
52         struct pinctrl_gpio_range range;
53         struct at91_gpio_chip   *next;
54         int                     pioc_hwirq;
55         int                     pioc_virq;
56         int                     pioc_idx;
57         void __iomem            *regbase;
58         struct clk              *clock;
59         const struct at91_pinctrl_mux_ops *ops;
60         u32                     wakeups;
61         u32                     backups;
62         u32                     id;
63 };
64
65 static struct at91_gpio_chip *gpio_chips[MAX_GPIO_BANKS];
66
67 static int gpio_banks;
68
69 #define PULL_UP         (1 << 0)
70 #define MULTI_DRIVE     (1 << 1)
71 #define DEGLITCH        (1 << 2)
72 #define PULL_DOWN       (1 << 3)
73 #define DIS_SCHMIT      (1 << 4)
74 #define DRIVE_STRENGTH_SHIFT    5
75 #define DRIVE_STRENGTH_MASK             0x3
76 #define DRIVE_STRENGTH   (DRIVE_STRENGTH_MASK << DRIVE_STRENGTH_SHIFT)
77 #define OUTPUT          (1 << 7)
78 #define OUTPUT_VAL_SHIFT        8
79 #define OUTPUT_VAL      (0x1 << OUTPUT_VAL_SHIFT)
80 #define SLEWRATE_SHIFT  9
81 #define SLEWRATE_MASK   0x1
82 #define SLEWRATE        (SLEWRATE_MASK << SLEWRATE_SHIFT)
83 #define DEBOUNCE        (1 << 16)
84 #define DEBOUNCE_VAL_SHIFT      17
85 #define DEBOUNCE_VAL    (0x3fff << DEBOUNCE_VAL_SHIFT)
86
87 /*
88  * These defines will translated the dt binding settings to our internal
89  * settings. They are not necessarily the same value as the register setting.
90  * The actual drive strength current of low, medium and high must be looked up
91  * from the corresponding device datasheet. This value is different for pins
92  * that are even in the same banks. It is also dependent on VCC.
93  * DRIVE_STRENGTH_DEFAULT is just a placeholder to avoid changing the drive
94  * strength when there is no dt config for it.
95  */
96 enum drive_strength_bit {
97         DRIVE_STRENGTH_BIT_DEF,
98         DRIVE_STRENGTH_BIT_LOW,
99         DRIVE_STRENGTH_BIT_MED,
100         DRIVE_STRENGTH_BIT_HI,
101 };
102
103 #define DRIVE_STRENGTH_BIT_MSK(name)    (DRIVE_STRENGTH_BIT_##name << \
104                                          DRIVE_STRENGTH_SHIFT)
105
106 enum slewrate_bit {
107         SLEWRATE_BIT_ENA,
108         SLEWRATE_BIT_DIS,
109 };
110
111 #define SLEWRATE_BIT_MSK(name)          (SLEWRATE_BIT_##name << SLEWRATE_SHIFT)
112
113 /**
114  * struct at91_pmx_func - describes AT91 pinmux functions
115  * @name: the name of this specific function
116  * @groups: corresponding pin groups
117  * @ngroups: the number of groups
118  */
119 struct at91_pmx_func {
120         const char      *name;
121         const char      **groups;
122         unsigned        ngroups;
123 };
124
125 enum at91_mux {
126         AT91_MUX_GPIO = 0,
127         AT91_MUX_PERIPH_A = 1,
128         AT91_MUX_PERIPH_B = 2,
129         AT91_MUX_PERIPH_C = 3,
130         AT91_MUX_PERIPH_D = 4,
131 };
132
133 /**
134  * struct at91_pmx_pin - describes an At91 pin mux
135  * @bank: the bank of the pin
136  * @pin: the pin number in the @bank
137  * @mux: the mux mode : gpio or periph_x of the pin i.e. alternate function.
138  * @conf: the configuration of the pin: PULL_UP, MULTIDRIVE etc...
139  */
140 struct at91_pmx_pin {
141         uint32_t        bank;
142         uint32_t        pin;
143         enum at91_mux   mux;
144         unsigned long   conf;
145 };
146
147 /**
148  * struct at91_pin_group - describes an At91 pin group
149  * @name: the name of this specific pin group
150  * @pins_conf: the mux mode for each pin in this group. The size of this
151  *      array is the same as pins.
152  * @pins: an array of discrete physical pins used in this group, taken
153  *      from the driver-local pin enumeration space
154  * @npins: the number of pins in this group array, i.e. the number of
155  *      elements in .pins so we can iterate over that array
156  */
157 struct at91_pin_group {
158         const char              *name;
159         struct at91_pmx_pin     *pins_conf;
160         unsigned int            *pins;
161         unsigned                npins;
162 };
163
164 /**
165  * struct at91_pinctrl_mux_ops - describes an AT91 mux ops group
166  * on new IP with support for periph C and D the way to mux in
167  * periph A and B has changed
168  * So provide the right call back
169  * if not present means the IP does not support it
170  * @get_periph: return the periph mode configured
171  * @mux_A_periph: mux as periph A
172  * @mux_B_periph: mux as periph B
173  * @mux_C_periph: mux as periph C
174  * @mux_D_periph: mux as periph D
175  * @get_deglitch: get deglitch status
176  * @set_deglitch: enable/disable deglitch
177  * @get_debounce: get debounce status
178  * @set_debounce: enable/disable debounce
179  * @get_pulldown: get pulldown status
180  * @set_pulldown: enable/disable pulldown
181  * @get_schmitt_trig: get schmitt trigger status
182  * @disable_schmitt_trig: disable schmitt trigger
183  * @get_drivestrength: get driver strength
184  * @set_drivestrength: set driver strength
185  * @get_slewrate: get slew rate
186  * @set_slewrate: set slew rate
187  * @irq_type: return irq type
188  */
189 struct at91_pinctrl_mux_ops {
190         enum at91_mux (*get_periph)(void __iomem *pio, unsigned mask);
191         void (*mux_A_periph)(void __iomem *pio, unsigned mask);
192         void (*mux_B_periph)(void __iomem *pio, unsigned mask);
193         void (*mux_C_periph)(void __iomem *pio, unsigned mask);
194         void (*mux_D_periph)(void __iomem *pio, unsigned mask);
195         bool (*get_deglitch)(void __iomem *pio, unsigned pin);
196         void (*set_deglitch)(void __iomem *pio, unsigned mask, bool is_on);
197         bool (*get_debounce)(void __iomem *pio, unsigned pin, u32 *div);
198         void (*set_debounce)(void __iomem *pio, unsigned mask, bool is_on, u32 div);
199         bool (*get_pulldown)(void __iomem *pio, unsigned pin);
200         void (*set_pulldown)(void __iomem *pio, unsigned mask, bool is_on);
201         bool (*get_schmitt_trig)(void __iomem *pio, unsigned pin);
202         void (*disable_schmitt_trig)(void __iomem *pio, unsigned mask);
203         unsigned (*get_drivestrength)(void __iomem *pio, unsigned pin);
204         void (*set_drivestrength)(void __iomem *pio, unsigned pin,
205                                         u32 strength);
206         unsigned (*get_slewrate)(void __iomem *pio, unsigned pin);
207         void (*set_slewrate)(void __iomem *pio, unsigned pin, u32 slewrate);
208         /* irq */
209         int (*irq_type)(struct irq_data *d, unsigned type);
210 };
211
212 static int gpio_irq_type(struct irq_data *d, unsigned type);
213 static int alt_gpio_irq_type(struct irq_data *d, unsigned type);
214
215 struct at91_pinctrl {
216         struct device           *dev;
217         struct pinctrl_dev      *pctl;
218
219         int                     nactive_banks;
220
221         uint32_t                *mux_mask;
222         int                     nmux;
223
224         struct at91_pmx_func    *functions;
225         int                     nfunctions;
226
227         struct at91_pin_group   *groups;
228         int                     ngroups;
229
230         const struct at91_pinctrl_mux_ops *ops;
231 };
232
233 static inline const struct at91_pin_group *at91_pinctrl_find_group_by_name(
234                                 const struct at91_pinctrl *info,
235                                 const char *name)
236 {
237         const struct at91_pin_group *grp = NULL;
238         int i;
239
240         for (i = 0; i < info->ngroups; i++) {
241                 if (strcmp(info->groups[i].name, name))
242                         continue;
243
244                 grp = &info->groups[i];
245                 dev_dbg(info->dev, "%s: %d 0:%d\n", name, grp->npins, grp->pins[0]);
246                 break;
247         }
248
249         return grp;
250 }
251
252 static int at91_get_groups_count(struct pinctrl_dev *pctldev)
253 {
254         struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
255
256         return info->ngroups;
257 }
258
259 static const char *at91_get_group_name(struct pinctrl_dev *pctldev,
260                                        unsigned selector)
261 {
262         struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
263
264         return info->groups[selector].name;
265 }
266
267 static int at91_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
268                                const unsigned **pins,
269                                unsigned *npins)
270 {
271         struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
272
273         if (selector >= info->ngroups)
274                 return -EINVAL;
275
276         *pins = info->groups[selector].pins;
277         *npins = info->groups[selector].npins;
278
279         return 0;
280 }
281
282 static void at91_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
283                    unsigned offset)
284 {
285         seq_printf(s, "%s", dev_name(pctldev->dev));
286 }
287
288 static int at91_dt_node_to_map(struct pinctrl_dev *pctldev,
289                         struct device_node *np,
290                         struct pinctrl_map **map, unsigned *num_maps)
291 {
292         struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
293         const struct at91_pin_group *grp;
294         struct pinctrl_map *new_map;
295         struct device_node *parent;
296         int map_num = 1;
297         int i;
298
299         /*
300          * first find the group of this node and check if we need to create
301          * config maps for pins
302          */
303         grp = at91_pinctrl_find_group_by_name(info, np->name);
304         if (!grp) {
305                 dev_err(info->dev, "unable to find group for node %pOFn\n",
306                         np);
307                 return -EINVAL;
308         }
309
310         map_num += grp->npins;
311         new_map = devm_kcalloc(pctldev->dev, map_num, sizeof(*new_map),
312                                GFP_KERNEL);
313         if (!new_map)
314                 return -ENOMEM;
315
316         *map = new_map;
317         *num_maps = map_num;
318
319         /* create mux map */
320         parent = of_get_parent(np);
321         if (!parent) {
322                 devm_kfree(pctldev->dev, new_map);
323                 return -EINVAL;
324         }
325         new_map[0].type = PIN_MAP_TYPE_MUX_GROUP;
326         new_map[0].data.mux.function = parent->name;
327         new_map[0].data.mux.group = np->name;
328         of_node_put(parent);
329
330         /* create config map */
331         new_map++;
332         for (i = 0; i < grp->npins; i++) {
333                 new_map[i].type = PIN_MAP_TYPE_CONFIGS_PIN;
334                 new_map[i].data.configs.group_or_pin =
335                                 pin_get_name(pctldev, grp->pins[i]);
336                 new_map[i].data.configs.configs = &grp->pins_conf[i].conf;
337                 new_map[i].data.configs.num_configs = 1;
338         }
339
340         dev_dbg(pctldev->dev, "maps: function %s group %s num %d\n",
341                 (*map)->data.mux.function, (*map)->data.mux.group, map_num);
342
343         return 0;
344 }
345
346 static void at91_dt_free_map(struct pinctrl_dev *pctldev,
347                                 struct pinctrl_map *map, unsigned num_maps)
348 {
349 }
350
351 static const struct pinctrl_ops at91_pctrl_ops = {
352         .get_groups_count       = at91_get_groups_count,
353         .get_group_name         = at91_get_group_name,
354         .get_group_pins         = at91_get_group_pins,
355         .pin_dbg_show           = at91_pin_dbg_show,
356         .dt_node_to_map         = at91_dt_node_to_map,
357         .dt_free_map            = at91_dt_free_map,
358 };
359
360 static void __iomem *pin_to_controller(struct at91_pinctrl *info,
361                                  unsigned int bank)
362 {
363         if (!gpio_chips[bank])
364                 return NULL;
365
366         return gpio_chips[bank]->regbase;
367 }
368
369 static inline int pin_to_bank(unsigned pin)
370 {
371         return pin /= MAX_NB_GPIO_PER_BANK;
372 }
373
374 static unsigned pin_to_mask(unsigned int pin)
375 {
376         return 1 << pin;
377 }
378
379 static unsigned two_bit_pin_value_shift_amount(unsigned int pin)
380 {
381         /* return the shift value for a pin for "two bit" per pin registers,
382          * i.e. drive strength */
383         return 2*((pin >= MAX_NB_GPIO_PER_BANK/2)
384                         ? pin - MAX_NB_GPIO_PER_BANK/2 : pin);
385 }
386
387 static unsigned sama5d3_get_drive_register(unsigned int pin)
388 {
389         /* drive strength is split between two registers
390          * with two bits per pin */
391         return (pin >= MAX_NB_GPIO_PER_BANK/2)
392                         ? SAMA5D3_PIO_DRIVER2 : SAMA5D3_PIO_DRIVER1;
393 }
394
395 static unsigned at91sam9x5_get_drive_register(unsigned int pin)
396 {
397         /* drive strength is split between two registers
398          * with two bits per pin */
399         return (pin >= MAX_NB_GPIO_PER_BANK/2)
400                         ? AT91SAM9X5_PIO_DRIVER2 : AT91SAM9X5_PIO_DRIVER1;
401 }
402
403 static void at91_mux_disable_interrupt(void __iomem *pio, unsigned mask)
404 {
405         writel_relaxed(mask, pio + PIO_IDR);
406 }
407
408 static unsigned at91_mux_get_pullup(void __iomem *pio, unsigned pin)
409 {
410         return !((readl_relaxed(pio + PIO_PUSR) >> pin) & 0x1);
411 }
412
413 static void at91_mux_set_pullup(void __iomem *pio, unsigned mask, bool on)
414 {
415         if (on)
416                 writel_relaxed(mask, pio + PIO_PPDDR);
417
418         writel_relaxed(mask, pio + (on ? PIO_PUER : PIO_PUDR));
419 }
420
421 static bool at91_mux_get_output(void __iomem *pio, unsigned int pin, bool *val)
422 {
423         *val = (readl_relaxed(pio + PIO_ODSR) >> pin) & 0x1;
424         return (readl_relaxed(pio + PIO_OSR) >> pin) & 0x1;
425 }
426
427 static void at91_mux_set_output(void __iomem *pio, unsigned int mask,
428                                 bool is_on, bool val)
429 {
430         writel_relaxed(mask, pio + (val ? PIO_SODR : PIO_CODR));
431         writel_relaxed(mask, pio + (is_on ? PIO_OER : PIO_ODR));
432 }
433
434 static unsigned at91_mux_get_multidrive(void __iomem *pio, unsigned pin)
435 {
436         return (readl_relaxed(pio + PIO_MDSR) >> pin) & 0x1;
437 }
438
439 static void at91_mux_set_multidrive(void __iomem *pio, unsigned mask, bool on)
440 {
441         writel_relaxed(mask, pio + (on ? PIO_MDER : PIO_MDDR));
442 }
443
444 static void at91_mux_set_A_periph(void __iomem *pio, unsigned mask)
445 {
446         writel_relaxed(mask, pio + PIO_ASR);
447 }
448
449 static void at91_mux_set_B_periph(void __iomem *pio, unsigned mask)
450 {
451         writel_relaxed(mask, pio + PIO_BSR);
452 }
453
454 static void at91_mux_pio3_set_A_periph(void __iomem *pio, unsigned mask)
455 {
456
457         writel_relaxed(readl_relaxed(pio + PIO_ABCDSR1) & ~mask,
458                                                 pio + PIO_ABCDSR1);
459         writel_relaxed(readl_relaxed(pio + PIO_ABCDSR2) & ~mask,
460                                                 pio + PIO_ABCDSR2);
461 }
462
463 static void at91_mux_pio3_set_B_periph(void __iomem *pio, unsigned mask)
464 {
465         writel_relaxed(readl_relaxed(pio + PIO_ABCDSR1) | mask,
466                                                 pio + PIO_ABCDSR1);
467         writel_relaxed(readl_relaxed(pio + PIO_ABCDSR2) & ~mask,
468                                                 pio + PIO_ABCDSR2);
469 }
470
471 static void at91_mux_pio3_set_C_periph(void __iomem *pio, unsigned mask)
472 {
473         writel_relaxed(readl_relaxed(pio + PIO_ABCDSR1) & ~mask, pio + PIO_ABCDSR1);
474         writel_relaxed(readl_relaxed(pio + PIO_ABCDSR2) | mask, pio + PIO_ABCDSR2);
475 }
476
477 static void at91_mux_pio3_set_D_periph(void __iomem *pio, unsigned mask)
478 {
479         writel_relaxed(readl_relaxed(pio + PIO_ABCDSR1) | mask, pio + PIO_ABCDSR1);
480         writel_relaxed(readl_relaxed(pio + PIO_ABCDSR2) | mask, pio + PIO_ABCDSR2);
481 }
482
483 static enum at91_mux at91_mux_pio3_get_periph(void __iomem *pio, unsigned mask)
484 {
485         unsigned select;
486
487         if (readl_relaxed(pio + PIO_PSR) & mask)
488                 return AT91_MUX_GPIO;
489
490         select = !!(readl_relaxed(pio + PIO_ABCDSR1) & mask);
491         select |= (!!(readl_relaxed(pio + PIO_ABCDSR2) & mask) << 1);
492
493         return select + 1;
494 }
495
496 static enum at91_mux at91_mux_get_periph(void __iomem *pio, unsigned mask)
497 {
498         unsigned select;
499
500         if (readl_relaxed(pio + PIO_PSR) & mask)
501                 return AT91_MUX_GPIO;
502
503         select = readl_relaxed(pio + PIO_ABSR) & mask;
504
505         return select + 1;
506 }
507
508 static bool at91_mux_get_deglitch(void __iomem *pio, unsigned pin)
509 {
510         return (readl_relaxed(pio + PIO_IFSR) >> pin) & 0x1;
511 }
512
513 static void at91_mux_set_deglitch(void __iomem *pio, unsigned mask, bool is_on)
514 {
515         writel_relaxed(mask, pio + (is_on ? PIO_IFER : PIO_IFDR));
516 }
517
518 static bool at91_mux_pio3_get_deglitch(void __iomem *pio, unsigned pin)
519 {
520         if ((readl_relaxed(pio + PIO_IFSR) >> pin) & 0x1)
521                 return !((readl_relaxed(pio + PIO_IFSCSR) >> pin) & 0x1);
522
523         return false;
524 }
525
526 static void at91_mux_pio3_set_deglitch(void __iomem *pio, unsigned mask, bool is_on)
527 {
528         if (is_on)
529                 writel_relaxed(mask, pio + PIO_IFSCDR);
530         at91_mux_set_deglitch(pio, mask, is_on);
531 }
532
533 static bool at91_mux_pio3_get_debounce(void __iomem *pio, unsigned pin, u32 *div)
534 {
535         *div = readl_relaxed(pio + PIO_SCDR);
536
537         return ((readl_relaxed(pio + PIO_IFSR) >> pin) & 0x1) &&
538                ((readl_relaxed(pio + PIO_IFSCSR) >> pin) & 0x1);
539 }
540
541 static void at91_mux_pio3_set_debounce(void __iomem *pio, unsigned mask,
542                                 bool is_on, u32 div)
543 {
544         if (is_on) {
545                 writel_relaxed(mask, pio + PIO_IFSCER);
546                 writel_relaxed(div & PIO_SCDR_DIV, pio + PIO_SCDR);
547                 writel_relaxed(mask, pio + PIO_IFER);
548         } else
549                 writel_relaxed(mask, pio + PIO_IFSCDR);
550 }
551
552 static bool at91_mux_pio3_get_pulldown(void __iomem *pio, unsigned pin)
553 {
554         return !((readl_relaxed(pio + PIO_PPDSR) >> pin) & 0x1);
555 }
556
557 static void at91_mux_pio3_set_pulldown(void __iomem *pio, unsigned mask, bool is_on)
558 {
559         if (is_on)
560                 writel_relaxed(mask, pio + PIO_PUDR);
561
562         writel_relaxed(mask, pio + (is_on ? PIO_PPDER : PIO_PPDDR));
563 }
564
565 static void at91_mux_pio3_disable_schmitt_trig(void __iomem *pio, unsigned mask)
566 {
567         writel_relaxed(readl_relaxed(pio + PIO_SCHMITT) | mask, pio + PIO_SCHMITT);
568 }
569
570 static bool at91_mux_pio3_get_schmitt_trig(void __iomem *pio, unsigned pin)
571 {
572         return (readl_relaxed(pio + PIO_SCHMITT) >> pin) & 0x1;
573 }
574
575 static inline u32 read_drive_strength(void __iomem *reg, unsigned pin)
576 {
577         unsigned tmp = readl_relaxed(reg);
578
579         tmp = tmp >> two_bit_pin_value_shift_amount(pin);
580
581         return tmp & DRIVE_STRENGTH_MASK;
582 }
583
584 static unsigned at91_mux_sama5d3_get_drivestrength(void __iomem *pio,
585                                                         unsigned pin)
586 {
587         unsigned tmp = read_drive_strength(pio +
588                                         sama5d3_get_drive_register(pin), pin);
589
590         /* SAMA5 strength is 1:1 with our defines,
591          * except 0 is equivalent to low per datasheet */
592         if (!tmp)
593                 tmp = DRIVE_STRENGTH_BIT_MSK(LOW);
594
595         return tmp;
596 }
597
598 static unsigned at91_mux_sam9x5_get_drivestrength(void __iomem *pio,
599                                                         unsigned pin)
600 {
601         unsigned tmp = read_drive_strength(pio +
602                                 at91sam9x5_get_drive_register(pin), pin);
603
604         /* strength is inverse in SAM9x5s hardware with the pinctrl defines
605          * hardware: 0 = hi, 1 = med, 2 = low, 3 = rsvd */
606         tmp = DRIVE_STRENGTH_BIT_MSK(HI) - tmp;
607
608         return tmp;
609 }
610
611 static unsigned at91_mux_sam9x60_get_drivestrength(void __iomem *pio,
612                                                    unsigned pin)
613 {
614         unsigned tmp = readl_relaxed(pio + SAM9X60_PIO_DRIVER1);
615
616         if (tmp & BIT(pin))
617                 return DRIVE_STRENGTH_BIT_HI;
618
619         return DRIVE_STRENGTH_BIT_LOW;
620 }
621
622 static unsigned at91_mux_sam9x60_get_slewrate(void __iomem *pio, unsigned pin)
623 {
624         unsigned tmp = readl_relaxed(pio + SAM9X60_PIO_SLEWR);
625
626         if ((tmp & BIT(pin)))
627                 return SLEWRATE_BIT_ENA;
628
629         return SLEWRATE_BIT_DIS;
630 }
631
632 static void set_drive_strength(void __iomem *reg, unsigned pin, u32 strength)
633 {
634         unsigned tmp = readl_relaxed(reg);
635         unsigned shift = two_bit_pin_value_shift_amount(pin);
636
637         tmp &= ~(DRIVE_STRENGTH_MASK  <<  shift);
638         tmp |= strength << shift;
639
640         writel_relaxed(tmp, reg);
641 }
642
643 static void at91_mux_sama5d3_set_drivestrength(void __iomem *pio, unsigned pin,
644                                                 u32 setting)
645 {
646         /* do nothing if setting is zero */
647         if (!setting)
648                 return;
649
650         /* strength is 1 to 1 with setting for SAMA5 */
651         set_drive_strength(pio + sama5d3_get_drive_register(pin), pin, setting);
652 }
653
654 static void at91_mux_sam9x5_set_drivestrength(void __iomem *pio, unsigned pin,
655                                                 u32 setting)
656 {
657         /* do nothing if setting is zero */
658         if (!setting)
659                 return;
660
661         /* strength is inverse on SAM9x5s with our defines
662          * 0 = hi, 1 = med, 2 = low, 3 = rsvd */
663         setting = DRIVE_STRENGTH_BIT_MSK(HI) - setting;
664
665         set_drive_strength(pio + at91sam9x5_get_drive_register(pin), pin,
666                                 setting);
667 }
668
669 static void at91_mux_sam9x60_set_drivestrength(void __iomem *pio, unsigned pin,
670                                                u32 setting)
671 {
672         unsigned int tmp;
673
674         if (setting <= DRIVE_STRENGTH_BIT_DEF ||
675             setting == DRIVE_STRENGTH_BIT_MED ||
676             setting > DRIVE_STRENGTH_BIT_HI)
677                 return;
678
679         tmp = readl_relaxed(pio + SAM9X60_PIO_DRIVER1);
680
681         /* Strength is 0: low, 1: hi */
682         if (setting == DRIVE_STRENGTH_BIT_LOW)
683                 tmp &= ~BIT(pin);
684         else
685                 tmp |= BIT(pin);
686
687         writel_relaxed(tmp, pio + SAM9X60_PIO_DRIVER1);
688 }
689
690 static void at91_mux_sam9x60_set_slewrate(void __iomem *pio, unsigned pin,
691                                           u32 setting)
692 {
693         unsigned int tmp;
694
695         if (setting < SLEWRATE_BIT_ENA || setting > SLEWRATE_BIT_DIS)
696                 return;
697
698         tmp = readl_relaxed(pio + SAM9X60_PIO_SLEWR);
699
700         if (setting == SLEWRATE_BIT_DIS)
701                 tmp &= ~BIT(pin);
702         else
703                 tmp |= BIT(pin);
704
705         writel_relaxed(tmp, pio + SAM9X60_PIO_SLEWR);
706 }
707
708 static const struct at91_pinctrl_mux_ops at91rm9200_ops = {
709         .get_periph     = at91_mux_get_periph,
710         .mux_A_periph   = at91_mux_set_A_periph,
711         .mux_B_periph   = at91_mux_set_B_periph,
712         .get_deglitch   = at91_mux_get_deglitch,
713         .set_deglitch   = at91_mux_set_deglitch,
714         .irq_type       = gpio_irq_type,
715 };
716
717 static const struct at91_pinctrl_mux_ops at91sam9x5_ops = {
718         .get_periph     = at91_mux_pio3_get_periph,
719         .mux_A_periph   = at91_mux_pio3_set_A_periph,
720         .mux_B_periph   = at91_mux_pio3_set_B_periph,
721         .mux_C_periph   = at91_mux_pio3_set_C_periph,
722         .mux_D_periph   = at91_mux_pio3_set_D_periph,
723         .get_deglitch   = at91_mux_pio3_get_deglitch,
724         .set_deglitch   = at91_mux_pio3_set_deglitch,
725         .get_debounce   = at91_mux_pio3_get_debounce,
726         .set_debounce   = at91_mux_pio3_set_debounce,
727         .get_pulldown   = at91_mux_pio3_get_pulldown,
728         .set_pulldown   = at91_mux_pio3_set_pulldown,
729         .get_schmitt_trig = at91_mux_pio3_get_schmitt_trig,
730         .disable_schmitt_trig = at91_mux_pio3_disable_schmitt_trig,
731         .get_drivestrength = at91_mux_sam9x5_get_drivestrength,
732         .set_drivestrength = at91_mux_sam9x5_set_drivestrength,
733         .irq_type       = alt_gpio_irq_type,
734 };
735
736 static const struct at91_pinctrl_mux_ops sam9x60_ops = {
737         .get_periph     = at91_mux_pio3_get_periph,
738         .mux_A_periph   = at91_mux_pio3_set_A_periph,
739         .mux_B_periph   = at91_mux_pio3_set_B_periph,
740         .mux_C_periph   = at91_mux_pio3_set_C_periph,
741         .mux_D_periph   = at91_mux_pio3_set_D_periph,
742         .get_deglitch   = at91_mux_pio3_get_deglitch,
743         .set_deglitch   = at91_mux_pio3_set_deglitch,
744         .get_debounce   = at91_mux_pio3_get_debounce,
745         .set_debounce   = at91_mux_pio3_set_debounce,
746         .get_pulldown   = at91_mux_pio3_get_pulldown,
747         .set_pulldown   = at91_mux_pio3_set_pulldown,
748         .get_schmitt_trig = at91_mux_pio3_get_schmitt_trig,
749         .disable_schmitt_trig = at91_mux_pio3_disable_schmitt_trig,
750         .get_drivestrength = at91_mux_sam9x60_get_drivestrength,
751         .set_drivestrength = at91_mux_sam9x60_set_drivestrength,
752         .get_slewrate   = at91_mux_sam9x60_get_slewrate,
753         .set_slewrate   = at91_mux_sam9x60_set_slewrate,
754         .irq_type       = alt_gpio_irq_type,
755 };
756
757 static const struct at91_pinctrl_mux_ops sama5d3_ops = {
758         .get_periph     = at91_mux_pio3_get_periph,
759         .mux_A_periph   = at91_mux_pio3_set_A_periph,
760         .mux_B_periph   = at91_mux_pio3_set_B_periph,
761         .mux_C_periph   = at91_mux_pio3_set_C_periph,
762         .mux_D_periph   = at91_mux_pio3_set_D_periph,
763         .get_deglitch   = at91_mux_pio3_get_deglitch,
764         .set_deglitch   = at91_mux_pio3_set_deglitch,
765         .get_debounce   = at91_mux_pio3_get_debounce,
766         .set_debounce   = at91_mux_pio3_set_debounce,
767         .get_pulldown   = at91_mux_pio3_get_pulldown,
768         .set_pulldown   = at91_mux_pio3_set_pulldown,
769         .get_schmitt_trig = at91_mux_pio3_get_schmitt_trig,
770         .disable_schmitt_trig = at91_mux_pio3_disable_schmitt_trig,
771         .get_drivestrength = at91_mux_sama5d3_get_drivestrength,
772         .set_drivestrength = at91_mux_sama5d3_set_drivestrength,
773         .irq_type       = alt_gpio_irq_type,
774 };
775
776 static void at91_pin_dbg(const struct device *dev, const struct at91_pmx_pin *pin)
777 {
778         if (pin->mux) {
779                 dev_dbg(dev, "pio%c%d configured as periph%c with conf = 0x%lx\n",
780                         pin->bank + 'A', pin->pin, pin->mux - 1 + 'A', pin->conf);
781         } else {
782                 dev_dbg(dev, "pio%c%d configured as gpio with conf = 0x%lx\n",
783                         pin->bank + 'A', pin->pin, pin->conf);
784         }
785 }
786
787 static int pin_check_config(struct at91_pinctrl *info, const char *name,
788                             int index, const struct at91_pmx_pin *pin)
789 {
790         int mux;
791
792         /* check if it's a valid config */
793         if (pin->bank >= gpio_banks) {
794                 dev_err(info->dev, "%s: pin conf %d bank_id %d >= nbanks %d\n",
795                         name, index, pin->bank, gpio_banks);
796                 return -EINVAL;
797         }
798
799         if (!gpio_chips[pin->bank]) {
800                 dev_err(info->dev, "%s: pin conf %d bank_id %d not enabled\n",
801                         name, index, pin->bank);
802                 return -ENXIO;
803         }
804
805         if (pin->pin >= MAX_NB_GPIO_PER_BANK) {
806                 dev_err(info->dev, "%s: pin conf %d pin_bank_id %d >= %d\n",
807                         name, index, pin->pin, MAX_NB_GPIO_PER_BANK);
808                 return -EINVAL;
809         }
810
811         if (!pin->mux)
812                 return 0;
813
814         mux = pin->mux - 1;
815
816         if (mux >= info->nmux) {
817                 dev_err(info->dev, "%s: pin conf %d mux_id %d >= nmux %d\n",
818                         name, index, mux, info->nmux);
819                 return -EINVAL;
820         }
821
822         if (!(info->mux_mask[pin->bank * info->nmux + mux] & 1 << pin->pin)) {
823                 dev_err(info->dev, "%s: pin conf %d mux_id %d not supported for pio%c%d\n",
824                         name, index, mux, pin->bank + 'A', pin->pin);
825                 return -EINVAL;
826         }
827
828         return 0;
829 }
830
831 static void at91_mux_gpio_disable(void __iomem *pio, unsigned mask)
832 {
833         writel_relaxed(mask, pio + PIO_PDR);
834 }
835
836 static void at91_mux_gpio_enable(void __iomem *pio, unsigned mask, bool input)
837 {
838         writel_relaxed(mask, pio + PIO_PER);
839         writel_relaxed(mask, pio + (input ? PIO_ODR : PIO_OER));
840 }
841
842 static int at91_pmx_set(struct pinctrl_dev *pctldev, unsigned selector,
843                         unsigned group)
844 {
845         struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
846         const struct at91_pmx_pin *pins_conf = info->groups[group].pins_conf;
847         const struct at91_pmx_pin *pin;
848         uint32_t npins = info->groups[group].npins;
849         int i, ret;
850         unsigned mask;
851         void __iomem *pio;
852
853         dev_dbg(info->dev, "enable function %s group %s\n",
854                 info->functions[selector].name, info->groups[group].name);
855
856         /* first check that all the pins of the group are valid with a valid
857          * parameter */
858         for (i = 0; i < npins; i++) {
859                 pin = &pins_conf[i];
860                 ret = pin_check_config(info, info->groups[group].name, i, pin);
861                 if (ret)
862                         return ret;
863         }
864
865         for (i = 0; i < npins; i++) {
866                 pin = &pins_conf[i];
867                 at91_pin_dbg(info->dev, pin);
868                 pio = pin_to_controller(info, pin->bank);
869
870                 if (!pio)
871                         continue;
872
873                 mask = pin_to_mask(pin->pin);
874                 at91_mux_disable_interrupt(pio, mask);
875                 switch (pin->mux) {
876                 case AT91_MUX_GPIO:
877                         at91_mux_gpio_enable(pio, mask, 1);
878                         break;
879                 case AT91_MUX_PERIPH_A:
880                         info->ops->mux_A_periph(pio, mask);
881                         break;
882                 case AT91_MUX_PERIPH_B:
883                         info->ops->mux_B_periph(pio, mask);
884                         break;
885                 case AT91_MUX_PERIPH_C:
886                         if (!info->ops->mux_C_periph)
887                                 return -EINVAL;
888                         info->ops->mux_C_periph(pio, mask);
889                         break;
890                 case AT91_MUX_PERIPH_D:
891                         if (!info->ops->mux_D_periph)
892                                 return -EINVAL;
893                         info->ops->mux_D_periph(pio, mask);
894                         break;
895                 }
896                 if (pin->mux)
897                         at91_mux_gpio_disable(pio, mask);
898         }
899
900         return 0;
901 }
902
903 static int at91_pmx_get_funcs_count(struct pinctrl_dev *pctldev)
904 {
905         struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
906
907         return info->nfunctions;
908 }
909
910 static const char *at91_pmx_get_func_name(struct pinctrl_dev *pctldev,
911                                           unsigned selector)
912 {
913         struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
914
915         return info->functions[selector].name;
916 }
917
918 static int at91_pmx_get_groups(struct pinctrl_dev *pctldev, unsigned selector,
919                                const char * const **groups,
920                                unsigned * const num_groups)
921 {
922         struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
923
924         *groups = info->functions[selector].groups;
925         *num_groups = info->functions[selector].ngroups;
926
927         return 0;
928 }
929
930 static int at91_gpio_request_enable(struct pinctrl_dev *pctldev,
931                                     struct pinctrl_gpio_range *range,
932                                     unsigned offset)
933 {
934         struct at91_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
935         struct at91_gpio_chip *at91_chip;
936         struct gpio_chip *chip;
937         unsigned mask;
938
939         if (!range) {
940                 dev_err(npct->dev, "invalid range\n");
941                 return -EINVAL;
942         }
943         if (!range->gc) {
944                 dev_err(npct->dev, "missing GPIO chip in range\n");
945                 return -EINVAL;
946         }
947         chip = range->gc;
948         at91_chip = gpiochip_get_data(chip);
949
950         dev_dbg(npct->dev, "enable pin %u as GPIO\n", offset);
951
952         mask = 1 << (offset - chip->base);
953
954         dev_dbg(npct->dev, "enable pin %u as PIO%c%d 0x%x\n",
955                 offset, 'A' + range->id, offset - chip->base, mask);
956
957         writel_relaxed(mask, at91_chip->regbase + PIO_PER);
958
959         return 0;
960 }
961
962 static void at91_gpio_disable_free(struct pinctrl_dev *pctldev,
963                                    struct pinctrl_gpio_range *range,
964                                    unsigned offset)
965 {
966         struct at91_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
967
968         dev_dbg(npct->dev, "disable pin %u as GPIO\n", offset);
969         /* Set the pin to some default state, GPIO is usually default */
970 }
971
972 static const struct pinmux_ops at91_pmx_ops = {
973         .get_functions_count    = at91_pmx_get_funcs_count,
974         .get_function_name      = at91_pmx_get_func_name,
975         .get_function_groups    = at91_pmx_get_groups,
976         .set_mux                = at91_pmx_set,
977         .gpio_request_enable    = at91_gpio_request_enable,
978         .gpio_disable_free      = at91_gpio_disable_free,
979 };
980
981 static int at91_pinconf_get(struct pinctrl_dev *pctldev,
982                              unsigned pin_id, unsigned long *config)
983 {
984         struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
985         void __iomem *pio;
986         unsigned pin;
987         int div;
988         bool out;
989
990         *config = 0;
991         dev_dbg(info->dev, "%s:%d, pin_id=%d", __func__, __LINE__, pin_id);
992         pio = pin_to_controller(info, pin_to_bank(pin_id));
993
994         if (!pio)
995                 return -EINVAL;
996
997         pin = pin_id % MAX_NB_GPIO_PER_BANK;
998
999         if (at91_mux_get_multidrive(pio, pin))
1000                 *config |= MULTI_DRIVE;
1001
1002         if (at91_mux_get_pullup(pio, pin))
1003                 *config |= PULL_UP;
1004
1005         if (info->ops->get_deglitch && info->ops->get_deglitch(pio, pin))
1006                 *config |= DEGLITCH;
1007         if (info->ops->get_debounce && info->ops->get_debounce(pio, pin, &div))
1008                 *config |= DEBOUNCE | (div << DEBOUNCE_VAL_SHIFT);
1009         if (info->ops->get_pulldown && info->ops->get_pulldown(pio, pin))
1010                 *config |= PULL_DOWN;
1011         if (info->ops->get_schmitt_trig && info->ops->get_schmitt_trig(pio, pin))
1012                 *config |= DIS_SCHMIT;
1013         if (info->ops->get_drivestrength)
1014                 *config |= (info->ops->get_drivestrength(pio, pin)
1015                                 << DRIVE_STRENGTH_SHIFT);
1016         if (info->ops->get_slewrate)
1017                 *config |= (info->ops->get_slewrate(pio, pin) << SLEWRATE_SHIFT);
1018         if (at91_mux_get_output(pio, pin, &out))
1019                 *config |= OUTPUT | (out << OUTPUT_VAL_SHIFT);
1020
1021         return 0;
1022 }
1023
1024 static int at91_pinconf_set(struct pinctrl_dev *pctldev,
1025                              unsigned pin_id, unsigned long *configs,
1026                              unsigned num_configs)
1027 {
1028         struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
1029         unsigned mask;
1030         void __iomem *pio;
1031         int i;
1032         unsigned long config;
1033         unsigned pin;
1034
1035         for (i = 0; i < num_configs; i++) {
1036                 config = configs[i];
1037
1038                 dev_dbg(info->dev,
1039                         "%s:%d, pin_id=%d, config=0x%lx",
1040                         __func__, __LINE__, pin_id, config);
1041                 pio = pin_to_controller(info, pin_to_bank(pin_id));
1042
1043                 if (!pio)
1044                         return -EINVAL;
1045
1046                 pin = pin_id % MAX_NB_GPIO_PER_BANK;
1047                 mask = pin_to_mask(pin);
1048
1049                 if (config & PULL_UP && config & PULL_DOWN)
1050                         return -EINVAL;
1051
1052                 at91_mux_set_output(pio, mask, config & OUTPUT,
1053                                     (config & OUTPUT_VAL) >> OUTPUT_VAL_SHIFT);
1054                 at91_mux_set_pullup(pio, mask, config & PULL_UP);
1055                 at91_mux_set_multidrive(pio, mask, config & MULTI_DRIVE);
1056                 if (info->ops->set_deglitch)
1057                         info->ops->set_deglitch(pio, mask, config & DEGLITCH);
1058                 if (info->ops->set_debounce)
1059                         info->ops->set_debounce(pio, mask, config & DEBOUNCE,
1060                                 (config & DEBOUNCE_VAL) >> DEBOUNCE_VAL_SHIFT);
1061                 if (info->ops->set_pulldown)
1062                         info->ops->set_pulldown(pio, mask, config & PULL_DOWN);
1063                 if (info->ops->disable_schmitt_trig && config & DIS_SCHMIT)
1064                         info->ops->disable_schmitt_trig(pio, mask);
1065                 if (info->ops->set_drivestrength)
1066                         info->ops->set_drivestrength(pio, pin,
1067                                 (config & DRIVE_STRENGTH)
1068                                         >> DRIVE_STRENGTH_SHIFT);
1069                 if (info->ops->set_slewrate)
1070                         info->ops->set_slewrate(pio, pin,
1071                                 (config & SLEWRATE) >> SLEWRATE_SHIFT);
1072
1073         } /* for each config */
1074
1075         return 0;
1076 }
1077
1078 #define DBG_SHOW_FLAG(flag) do {                \
1079         if (config & flag) {                    \
1080                 if (num_conf)                   \
1081                         seq_puts(s, "|");       \
1082                 seq_puts(s, #flag);             \
1083                 num_conf++;                     \
1084         }                                       \
1085 } while (0)
1086
1087 #define DBG_SHOW_FLAG_MASKED(mask, flag, name) do { \
1088         if ((config & mask) == flag) {          \
1089                 if (num_conf)                   \
1090                         seq_puts(s, "|");       \
1091                 seq_puts(s, #name);             \
1092                 num_conf++;                     \
1093         }                                       \
1094 } while (0)
1095
1096 static void at91_pinconf_dbg_show(struct pinctrl_dev *pctldev,
1097                                    struct seq_file *s, unsigned pin_id)
1098 {
1099         unsigned long config;
1100         int val, num_conf = 0;
1101
1102         at91_pinconf_get(pctldev, pin_id, &config);
1103
1104         DBG_SHOW_FLAG(MULTI_DRIVE);
1105         DBG_SHOW_FLAG(PULL_UP);
1106         DBG_SHOW_FLAG(PULL_DOWN);
1107         DBG_SHOW_FLAG(DIS_SCHMIT);
1108         DBG_SHOW_FLAG(DEGLITCH);
1109         DBG_SHOW_FLAG_MASKED(DRIVE_STRENGTH, DRIVE_STRENGTH_BIT_MSK(LOW),
1110                              DRIVE_STRENGTH_LOW);
1111         DBG_SHOW_FLAG_MASKED(DRIVE_STRENGTH, DRIVE_STRENGTH_BIT_MSK(MED),
1112                              DRIVE_STRENGTH_MED);
1113         DBG_SHOW_FLAG_MASKED(DRIVE_STRENGTH, DRIVE_STRENGTH_BIT_MSK(HI),
1114                              DRIVE_STRENGTH_HI);
1115         DBG_SHOW_FLAG(SLEWRATE);
1116         DBG_SHOW_FLAG(DEBOUNCE);
1117         if (config & DEBOUNCE) {
1118                 val = config >> DEBOUNCE_VAL_SHIFT;
1119                 seq_printf(s, "(%d)", val);
1120         }
1121
1122         return;
1123 }
1124
1125 static void at91_pinconf_group_dbg_show(struct pinctrl_dev *pctldev,
1126                                          struct seq_file *s, unsigned group)
1127 {
1128 }
1129
1130 static const struct pinconf_ops at91_pinconf_ops = {
1131         .pin_config_get                 = at91_pinconf_get,
1132         .pin_config_set                 = at91_pinconf_set,
1133         .pin_config_dbg_show            = at91_pinconf_dbg_show,
1134         .pin_config_group_dbg_show      = at91_pinconf_group_dbg_show,
1135 };
1136
1137 static struct pinctrl_desc at91_pinctrl_desc = {
1138         .pctlops        = &at91_pctrl_ops,
1139         .pmxops         = &at91_pmx_ops,
1140         .confops        = &at91_pinconf_ops,
1141         .owner          = THIS_MODULE,
1142 };
1143
1144 static const char *gpio_compat = "atmel,at91rm9200-gpio";
1145
1146 static void at91_pinctrl_child_count(struct at91_pinctrl *info,
1147                                      struct device_node *np)
1148 {
1149         struct device_node *child;
1150
1151         for_each_child_of_node(np, child) {
1152                 if (of_device_is_compatible(child, gpio_compat)) {
1153                         if (of_device_is_available(child))
1154                                 info->nactive_banks++;
1155                 } else {
1156                         info->nfunctions++;
1157                         info->ngroups += of_get_child_count(child);
1158                 }
1159         }
1160 }
1161
1162 static int at91_pinctrl_mux_mask(struct at91_pinctrl *info,
1163                                  struct device_node *np)
1164 {
1165         int ret = 0;
1166         int size;
1167         const __be32 *list;
1168
1169         list = of_get_property(np, "atmel,mux-mask", &size);
1170         if (!list) {
1171                 dev_err(info->dev, "can not read the mux-mask of %d\n", size);
1172                 return -EINVAL;
1173         }
1174
1175         size /= sizeof(*list);
1176         if (!size || size % gpio_banks) {
1177                 dev_err(info->dev, "wrong mux mask array should be by %d\n", gpio_banks);
1178                 return -EINVAL;
1179         }
1180         info->nmux = size / gpio_banks;
1181
1182         info->mux_mask = devm_kcalloc(info->dev, size, sizeof(u32),
1183                                       GFP_KERNEL);
1184         if (!info->mux_mask)
1185                 return -ENOMEM;
1186
1187         ret = of_property_read_u32_array(np, "atmel,mux-mask",
1188                                           info->mux_mask, size);
1189         if (ret)
1190                 dev_err(info->dev, "can not read the mux-mask of %d\n", size);
1191         return ret;
1192 }
1193
1194 static int at91_pinctrl_parse_groups(struct device_node *np,
1195                                      struct at91_pin_group *grp,
1196                                      struct at91_pinctrl *info, u32 index)
1197 {
1198         struct at91_pmx_pin *pin;
1199         int size;
1200         const __be32 *list;
1201         int i, j;
1202
1203         dev_dbg(info->dev, "group(%d): %pOFn\n", index, np);
1204
1205         /* Initialise group */
1206         grp->name = np->name;
1207
1208         /*
1209          * the binding format is atmel,pins = <bank pin mux CONFIG ...>,
1210          * do sanity check and calculate pins number
1211          */
1212         list = of_get_property(np, "atmel,pins", &size);
1213         /* we do not check return since it's safe node passed down */
1214         size /= sizeof(*list);
1215         if (!size || size % 4) {
1216                 dev_err(info->dev, "wrong pins number or pins and configs should be by 4\n");
1217                 return -EINVAL;
1218         }
1219
1220         grp->npins = size / 4;
1221         pin = grp->pins_conf = devm_kcalloc(info->dev,
1222                                             grp->npins,
1223                                             sizeof(struct at91_pmx_pin),
1224                                             GFP_KERNEL);
1225         grp->pins = devm_kcalloc(info->dev, grp->npins, sizeof(unsigned int),
1226                                  GFP_KERNEL);
1227         if (!grp->pins_conf || !grp->pins)
1228                 return -ENOMEM;
1229
1230         for (i = 0, j = 0; i < size; i += 4, j++) {
1231                 pin->bank = be32_to_cpu(*list++);
1232                 pin->pin = be32_to_cpu(*list++);
1233                 grp->pins[j] = pin->bank * MAX_NB_GPIO_PER_BANK + pin->pin;
1234                 pin->mux = be32_to_cpu(*list++);
1235                 pin->conf = be32_to_cpu(*list++);
1236
1237                 at91_pin_dbg(info->dev, pin);
1238                 pin++;
1239         }
1240
1241         return 0;
1242 }
1243
1244 static int at91_pinctrl_parse_functions(struct device_node *np,
1245                                         struct at91_pinctrl *info, u32 index)
1246 {
1247         struct device_node *child;
1248         struct at91_pmx_func *func;
1249         struct at91_pin_group *grp;
1250         int ret;
1251         static u32 grp_index;
1252         u32 i = 0;
1253
1254         dev_dbg(info->dev, "parse function(%d): %pOFn\n", index, np);
1255
1256         func = &info->functions[index];
1257
1258         /* Initialise function */
1259         func->name = np->name;
1260         func->ngroups = of_get_child_count(np);
1261         if (func->ngroups == 0) {
1262                 dev_err(info->dev, "no groups defined\n");
1263                 return -EINVAL;
1264         }
1265         func->groups = devm_kcalloc(info->dev,
1266                         func->ngroups, sizeof(char *), GFP_KERNEL);
1267         if (!func->groups)
1268                 return -ENOMEM;
1269
1270         for_each_child_of_node(np, child) {
1271                 func->groups[i] = child->name;
1272                 grp = &info->groups[grp_index++];
1273                 ret = at91_pinctrl_parse_groups(child, grp, info, i++);
1274                 if (ret) {
1275                         of_node_put(child);
1276                         return ret;
1277                 }
1278         }
1279
1280         return 0;
1281 }
1282
1283 static const struct of_device_id at91_pinctrl_of_match[] = {
1284         { .compatible = "atmel,sama5d3-pinctrl", .data = &sama5d3_ops },
1285         { .compatible = "atmel,at91sam9x5-pinctrl", .data = &at91sam9x5_ops },
1286         { .compatible = "atmel,at91rm9200-pinctrl", .data = &at91rm9200_ops },
1287         { .compatible = "microchip,sam9x60-pinctrl", .data = &sam9x60_ops },
1288         { /* sentinel */ }
1289 };
1290
1291 static int at91_pinctrl_probe_dt(struct platform_device *pdev,
1292                                  struct at91_pinctrl *info)
1293 {
1294         int ret = 0;
1295         int i, j;
1296         uint32_t *tmp;
1297         struct device_node *np = pdev->dev.of_node;
1298         struct device_node *child;
1299
1300         if (!np)
1301                 return -ENODEV;
1302
1303         info->dev = &pdev->dev;
1304         info->ops = (const struct at91_pinctrl_mux_ops *)
1305                 of_match_device(at91_pinctrl_of_match, &pdev->dev)->data;
1306         at91_pinctrl_child_count(info, np);
1307
1308         if (gpio_banks < 1) {
1309                 dev_err(&pdev->dev, "you need to specify at least one gpio-controller\n");
1310                 return -EINVAL;
1311         }
1312
1313         ret = at91_pinctrl_mux_mask(info, np);
1314         if (ret)
1315                 return ret;
1316
1317         dev_dbg(&pdev->dev, "nmux = %d\n", info->nmux);
1318
1319         dev_dbg(&pdev->dev, "mux-mask\n");
1320         tmp = info->mux_mask;
1321         for (i = 0; i < gpio_banks; i++) {
1322                 for (j = 0; j < info->nmux; j++, tmp++) {
1323                         dev_dbg(&pdev->dev, "%d:%d\t0x%x\n", i, j, tmp[0]);
1324                 }
1325         }
1326
1327         dev_dbg(&pdev->dev, "nfunctions = %d\n", info->nfunctions);
1328         dev_dbg(&pdev->dev, "ngroups = %d\n", info->ngroups);
1329         info->functions = devm_kcalloc(&pdev->dev,
1330                                         info->nfunctions,
1331                                         sizeof(struct at91_pmx_func),
1332                                         GFP_KERNEL);
1333         if (!info->functions)
1334                 return -ENOMEM;
1335
1336         info->groups = devm_kcalloc(&pdev->dev,
1337                                         info->ngroups,
1338                                         sizeof(struct at91_pin_group),
1339                                         GFP_KERNEL);
1340         if (!info->groups)
1341                 return -ENOMEM;
1342
1343         dev_dbg(&pdev->dev, "nbanks = %d\n", gpio_banks);
1344         dev_dbg(&pdev->dev, "nfunctions = %d\n", info->nfunctions);
1345         dev_dbg(&pdev->dev, "ngroups = %d\n", info->ngroups);
1346
1347         i = 0;
1348
1349         for_each_child_of_node(np, child) {
1350                 if (of_device_is_compatible(child, gpio_compat))
1351                         continue;
1352                 ret = at91_pinctrl_parse_functions(child, info, i++);
1353                 if (ret) {
1354                         dev_err(&pdev->dev, "failed to parse function\n");
1355                         of_node_put(child);
1356                         return ret;
1357                 }
1358         }
1359
1360         return 0;
1361 }
1362
1363 static int at91_pinctrl_probe(struct platform_device *pdev)
1364 {
1365         struct at91_pinctrl *info;
1366         struct pinctrl_pin_desc *pdesc;
1367         int ret, i, j, k, ngpio_chips_enabled = 0;
1368
1369         info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
1370         if (!info)
1371                 return -ENOMEM;
1372
1373         ret = at91_pinctrl_probe_dt(pdev, info);
1374         if (ret)
1375                 return ret;
1376
1377         /*
1378          * We need all the GPIO drivers to probe FIRST, or we will not be able
1379          * to obtain references to the struct gpio_chip * for them, and we
1380          * need this to proceed.
1381          */
1382         for (i = 0; i < gpio_banks; i++)
1383                 if (gpio_chips[i])
1384                         ngpio_chips_enabled++;
1385
1386         if (ngpio_chips_enabled < info->nactive_banks) {
1387                 dev_warn(&pdev->dev,
1388                          "All GPIO chips are not registered yet (%d/%d)\n",
1389                          ngpio_chips_enabled, info->nactive_banks);
1390                 devm_kfree(&pdev->dev, info);
1391                 return -EPROBE_DEFER;
1392         }
1393
1394         at91_pinctrl_desc.name = dev_name(&pdev->dev);
1395         at91_pinctrl_desc.npins = gpio_banks * MAX_NB_GPIO_PER_BANK;
1396         at91_pinctrl_desc.pins = pdesc =
1397                 devm_kcalloc(&pdev->dev,
1398                              at91_pinctrl_desc.npins, sizeof(*pdesc),
1399                              GFP_KERNEL);
1400
1401         if (!at91_pinctrl_desc.pins)
1402                 return -ENOMEM;
1403
1404         for (i = 0, k = 0; i < gpio_banks; i++) {
1405                 for (j = 0; j < MAX_NB_GPIO_PER_BANK; j++, k++) {
1406                         pdesc->number = k;
1407                         pdesc->name = kasprintf(GFP_KERNEL, "pio%c%d", i + 'A', j);
1408                         pdesc++;
1409                 }
1410         }
1411
1412         platform_set_drvdata(pdev, info);
1413         info->pctl = devm_pinctrl_register(&pdev->dev, &at91_pinctrl_desc,
1414                                            info);
1415
1416         if (IS_ERR(info->pctl)) {
1417                 dev_err(&pdev->dev, "could not register AT91 pinctrl driver\n");
1418                 return PTR_ERR(info->pctl);
1419         }
1420
1421         /* We will handle a range of GPIO pins */
1422         for (i = 0; i < gpio_banks; i++)
1423                 if (gpio_chips[i])
1424                         pinctrl_add_gpio_range(info->pctl, &gpio_chips[i]->range);
1425
1426         dev_info(&pdev->dev, "initialized AT91 pinctrl driver\n");
1427
1428         return 0;
1429 }
1430
1431 static int at91_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
1432 {
1433         struct at91_gpio_chip *at91_gpio = gpiochip_get_data(chip);
1434         void __iomem *pio = at91_gpio->regbase;
1435         unsigned mask = 1 << offset;
1436         u32 osr;
1437
1438         osr = readl_relaxed(pio + PIO_OSR);
1439         if (osr & mask)
1440                 return GPIO_LINE_DIRECTION_OUT;
1441
1442         return GPIO_LINE_DIRECTION_IN;
1443 }
1444
1445 static int at91_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
1446 {
1447         struct at91_gpio_chip *at91_gpio = gpiochip_get_data(chip);
1448         void __iomem *pio = at91_gpio->regbase;
1449         unsigned mask = 1 << offset;
1450
1451         writel_relaxed(mask, pio + PIO_ODR);
1452         return 0;
1453 }
1454
1455 static int at91_gpio_get(struct gpio_chip *chip, unsigned offset)
1456 {
1457         struct at91_gpio_chip *at91_gpio = gpiochip_get_data(chip);
1458         void __iomem *pio = at91_gpio->regbase;
1459         unsigned mask = 1 << offset;
1460         u32 pdsr;
1461
1462         pdsr = readl_relaxed(pio + PIO_PDSR);
1463         return (pdsr & mask) != 0;
1464 }
1465
1466 static void at91_gpio_set(struct gpio_chip *chip, unsigned offset,
1467                                 int val)
1468 {
1469         struct at91_gpio_chip *at91_gpio = gpiochip_get_data(chip);
1470         void __iomem *pio = at91_gpio->regbase;
1471         unsigned mask = 1 << offset;
1472
1473         writel_relaxed(mask, pio + (val ? PIO_SODR : PIO_CODR));
1474 }
1475
1476 static void at91_gpio_set_multiple(struct gpio_chip *chip,
1477                                       unsigned long *mask, unsigned long *bits)
1478 {
1479         struct at91_gpio_chip *at91_gpio = gpiochip_get_data(chip);
1480         void __iomem *pio = at91_gpio->regbase;
1481
1482 #define BITS_MASK(bits) (((bits) == 32) ? ~0U : (BIT(bits) - 1))
1483         /* Mask additionally to ngpio as not all GPIO controllers have 32 pins */
1484         uint32_t set_mask = (*mask & *bits) & BITS_MASK(chip->ngpio);
1485         uint32_t clear_mask = (*mask & ~(*bits)) & BITS_MASK(chip->ngpio);
1486
1487         writel_relaxed(set_mask, pio + PIO_SODR);
1488         writel_relaxed(clear_mask, pio + PIO_CODR);
1489 }
1490
1491 static int at91_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
1492                                 int val)
1493 {
1494         struct at91_gpio_chip *at91_gpio = gpiochip_get_data(chip);
1495         void __iomem *pio = at91_gpio->regbase;
1496         unsigned mask = 1 << offset;
1497
1498         writel_relaxed(mask, pio + (val ? PIO_SODR : PIO_CODR));
1499         writel_relaxed(mask, pio + PIO_OER);
1500
1501         return 0;
1502 }
1503
1504 #ifdef CONFIG_DEBUG_FS
1505 static void at91_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1506 {
1507         enum at91_mux mode;
1508         int i;
1509         struct at91_gpio_chip *at91_gpio = gpiochip_get_data(chip);
1510         void __iomem *pio = at91_gpio->regbase;
1511         const char *gpio_label;
1512
1513         for_each_requested_gpio(chip, i, gpio_label) {
1514                 unsigned mask = pin_to_mask(i);
1515
1516                 mode = at91_gpio->ops->get_periph(pio, mask);
1517                 seq_printf(s, "[%s] GPIO%s%d: ",
1518                            gpio_label, chip->label, i);
1519                 if (mode == AT91_MUX_GPIO) {
1520                         seq_printf(s, "[gpio] ");
1521                         seq_printf(s, "%s ",
1522                                       readl_relaxed(pio + PIO_OSR) & mask ?
1523                                       "output" : "input");
1524                         seq_printf(s, "%s\n",
1525                                       readl_relaxed(pio + PIO_PDSR) & mask ?
1526                                       "set" : "clear");
1527                 } else {
1528                         seq_printf(s, "[periph %c]\n",
1529                                    mode + 'A' - 1);
1530                 }
1531         }
1532 }
1533 #else
1534 #define at91_gpio_dbg_show      NULL
1535 #endif
1536
1537 /* Several AIC controller irqs are dispatched through this GPIO handler.
1538  * To use any AT91_PIN_* as an externally triggered IRQ, first call
1539  * at91_set_gpio_input() then maybe enable its glitch filter.
1540  * Then just request_irq() with the pin ID; it works like any ARM IRQ
1541  * handler.
1542  * First implementation always triggers on rising and falling edges
1543  * whereas the newer PIO3 can be additionally configured to trigger on
1544  * level, edge with any polarity.
1545  *
1546  * Alternatively, certain pins may be used directly as IRQ0..IRQ6 after
1547  * configuring them with at91_set_a_periph() or at91_set_b_periph().
1548  * IRQ0..IRQ6 should be configurable, e.g. level vs edge triggering.
1549  */
1550
1551 static void gpio_irq_mask(struct irq_data *d)
1552 {
1553         struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
1554         void __iomem    *pio = at91_gpio->regbase;
1555         unsigned        mask = 1 << d->hwirq;
1556
1557         if (pio)
1558                 writel_relaxed(mask, pio + PIO_IDR);
1559 }
1560
1561 static void gpio_irq_unmask(struct irq_data *d)
1562 {
1563         struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
1564         void __iomem    *pio = at91_gpio->regbase;
1565         unsigned        mask = 1 << d->hwirq;
1566
1567         if (pio)
1568                 writel_relaxed(mask, pio + PIO_IER);
1569 }
1570
1571 static int gpio_irq_type(struct irq_data *d, unsigned type)
1572 {
1573         switch (type) {
1574         case IRQ_TYPE_NONE:
1575         case IRQ_TYPE_EDGE_BOTH:
1576                 return 0;
1577         default:
1578                 return -EINVAL;
1579         }
1580 }
1581
1582 /* Alternate irq type for PIO3 support */
1583 static int alt_gpio_irq_type(struct irq_data *d, unsigned type)
1584 {
1585         struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
1586         void __iomem    *pio = at91_gpio->regbase;
1587         unsigned        mask = 1 << d->hwirq;
1588
1589         switch (type) {
1590         case IRQ_TYPE_EDGE_RISING:
1591                 irq_set_handler_locked(d, handle_simple_irq);
1592                 writel_relaxed(mask, pio + PIO_ESR);
1593                 writel_relaxed(mask, pio + PIO_REHLSR);
1594                 break;
1595         case IRQ_TYPE_EDGE_FALLING:
1596                 irq_set_handler_locked(d, handle_simple_irq);
1597                 writel_relaxed(mask, pio + PIO_ESR);
1598                 writel_relaxed(mask, pio + PIO_FELLSR);
1599                 break;
1600         case IRQ_TYPE_LEVEL_LOW:
1601                 irq_set_handler_locked(d, handle_level_irq);
1602                 writel_relaxed(mask, pio + PIO_LSR);
1603                 writel_relaxed(mask, pio + PIO_FELLSR);
1604                 break;
1605         case IRQ_TYPE_LEVEL_HIGH:
1606                 irq_set_handler_locked(d, handle_level_irq);
1607                 writel_relaxed(mask, pio + PIO_LSR);
1608                 writel_relaxed(mask, pio + PIO_REHLSR);
1609                 break;
1610         case IRQ_TYPE_EDGE_BOTH:
1611                 /*
1612                  * disable additional interrupt modes:
1613                  * fall back to default behavior
1614                  */
1615                 irq_set_handler_locked(d, handle_simple_irq);
1616                 writel_relaxed(mask, pio + PIO_AIMDR);
1617                 return 0;
1618         case IRQ_TYPE_NONE:
1619         default:
1620                 pr_warn("AT91: No type for GPIO irq offset %d\n", d->irq);
1621                 return -EINVAL;
1622         }
1623
1624         /* enable additional interrupt modes */
1625         writel_relaxed(mask, pio + PIO_AIMER);
1626
1627         return 0;
1628 }
1629
1630 static void gpio_irq_ack(struct irq_data *d)
1631 {
1632         /* the interrupt is already cleared before by reading ISR */
1633 }
1634
1635 static int gpio_irq_set_wake(struct irq_data *d, unsigned state)
1636 {
1637         struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
1638         unsigned mask = 1 << d->hwirq;
1639
1640         if (state)
1641                 at91_gpio->wakeups |= mask;
1642         else
1643                 at91_gpio->wakeups &= ~mask;
1644
1645         irq_set_irq_wake(at91_gpio->pioc_virq, state);
1646
1647         return 0;
1648 }
1649
1650 static int at91_gpio_suspend(struct device *dev)
1651 {
1652         struct at91_gpio_chip *at91_chip = dev_get_drvdata(dev);
1653         void __iomem *pio = at91_chip->regbase;
1654
1655         at91_chip->backups = readl_relaxed(pio + PIO_IMR);
1656         writel_relaxed(at91_chip->backups, pio + PIO_IDR);
1657         writel_relaxed(at91_chip->wakeups, pio + PIO_IER);
1658
1659         if (!at91_chip->wakeups)
1660                 clk_disable_unprepare(at91_chip->clock);
1661         else
1662                 dev_dbg(dev, "GPIO-%c may wake for %08x\n",
1663                         'A' + at91_chip->id, at91_chip->wakeups);
1664
1665         return 0;
1666 }
1667
1668 static int at91_gpio_resume(struct device *dev)
1669 {
1670         struct at91_gpio_chip *at91_chip = dev_get_drvdata(dev);
1671         void __iomem *pio = at91_chip->regbase;
1672
1673         if (!at91_chip->wakeups)
1674                 clk_prepare_enable(at91_chip->clock);
1675
1676         writel_relaxed(at91_chip->wakeups, pio + PIO_IDR);
1677         writel_relaxed(at91_chip->backups, pio + PIO_IER);
1678
1679         return 0;
1680 }
1681
1682 static void gpio_irq_handler(struct irq_desc *desc)
1683 {
1684         struct irq_chip *chip = irq_desc_get_chip(desc);
1685         struct gpio_chip *gpio_chip = irq_desc_get_handler_data(desc);
1686         struct at91_gpio_chip *at91_gpio = gpiochip_get_data(gpio_chip);
1687         void __iomem    *pio = at91_gpio->regbase;
1688         unsigned long   isr;
1689         int             n;
1690
1691         chained_irq_enter(chip, desc);
1692         for (;;) {
1693                 /* Reading ISR acks pending (edge triggered) GPIO interrupts.
1694                  * When there are none pending, we're finished unless we need
1695                  * to process multiple banks (like ID_PIOCDE on sam9263).
1696                  */
1697                 isr = readl_relaxed(pio + PIO_ISR) & readl_relaxed(pio + PIO_IMR);
1698                 if (!isr) {
1699                         if (!at91_gpio->next)
1700                                 break;
1701                         at91_gpio = at91_gpio->next;
1702                         pio = at91_gpio->regbase;
1703                         gpio_chip = &at91_gpio->chip;
1704                         continue;
1705                 }
1706
1707                 for_each_set_bit(n, &isr, BITS_PER_LONG)
1708                         generic_handle_domain_irq(gpio_chip->irq.domain, n);
1709         }
1710         chained_irq_exit(chip, desc);
1711         /* now it may re-trigger */
1712 }
1713
1714 static int at91_gpio_of_irq_setup(struct platform_device *pdev,
1715                                   struct at91_gpio_chip *at91_gpio)
1716 {
1717         struct gpio_chip        *gpiochip_prev = NULL;
1718         struct at91_gpio_chip   *prev = NULL;
1719         struct irq_data         *d = irq_get_irq_data(at91_gpio->pioc_virq);
1720         struct irq_chip         *gpio_irqchip;
1721         struct gpio_irq_chip    *girq;
1722         int i;
1723
1724         gpio_irqchip = devm_kzalloc(&pdev->dev, sizeof(*gpio_irqchip),
1725                                     GFP_KERNEL);
1726         if (!gpio_irqchip)
1727                 return -ENOMEM;
1728
1729         at91_gpio->pioc_hwirq = irqd_to_hwirq(d);
1730
1731         gpio_irqchip->name = "GPIO";
1732         gpio_irqchip->irq_ack = gpio_irq_ack;
1733         gpio_irqchip->irq_disable = gpio_irq_mask;
1734         gpio_irqchip->irq_mask = gpio_irq_mask;
1735         gpio_irqchip->irq_unmask = gpio_irq_unmask;
1736         gpio_irqchip->irq_set_wake = pm_ptr(gpio_irq_set_wake);
1737         gpio_irqchip->irq_set_type = at91_gpio->ops->irq_type;
1738
1739         /* Disable irqs of this PIO controller */
1740         writel_relaxed(~0, at91_gpio->regbase + PIO_IDR);
1741
1742         /*
1743          * Let the generic code handle this edge IRQ, the chained
1744          * handler will perform the actual work of handling the parent
1745          * interrupt.
1746          */
1747         girq = &at91_gpio->chip.irq;
1748         girq->chip = gpio_irqchip;
1749         girq->default_type = IRQ_TYPE_NONE;
1750         girq->handler = handle_edge_irq;
1751
1752         /*
1753          * The top level handler handles one bank of GPIOs, except
1754          * on some SoC it can handle up to three...
1755          * We only set up the handler for the first of the list.
1756          */
1757         gpiochip_prev = irq_get_handler_data(at91_gpio->pioc_virq);
1758         if (!gpiochip_prev) {
1759                 girq->parent_handler = gpio_irq_handler;
1760                 girq->num_parents = 1;
1761                 girq->parents = devm_kcalloc(&pdev->dev, 1,
1762                                              sizeof(*girq->parents),
1763                                              GFP_KERNEL);
1764                 if (!girq->parents)
1765                         return -ENOMEM;
1766                 girq->parents[0] = at91_gpio->pioc_virq;
1767                 return 0;
1768         }
1769
1770         prev = gpiochip_get_data(gpiochip_prev);
1771         /* we can only have 2 banks before */
1772         for (i = 0; i < 2; i++) {
1773                 if (prev->next) {
1774                         prev = prev->next;
1775                 } else {
1776                         prev->next = at91_gpio;
1777                         return 0;
1778                 }
1779         }
1780
1781         return -EINVAL;
1782 }
1783
1784 /* This structure is replicated for each GPIO block allocated at probe time */
1785 static const struct gpio_chip at91_gpio_template = {
1786         .request                = gpiochip_generic_request,
1787         .free                   = gpiochip_generic_free,
1788         .get_direction          = at91_gpio_get_direction,
1789         .direction_input        = at91_gpio_direction_input,
1790         .get                    = at91_gpio_get,
1791         .direction_output       = at91_gpio_direction_output,
1792         .set                    = at91_gpio_set,
1793         .set_multiple           = at91_gpio_set_multiple,
1794         .dbg_show               = at91_gpio_dbg_show,
1795         .can_sleep              = false,
1796         .ngpio                  = MAX_NB_GPIO_PER_BANK,
1797 };
1798
1799 static const struct of_device_id at91_gpio_of_match[] = {
1800         { .compatible = "atmel,at91sam9x5-gpio", .data = &at91sam9x5_ops, },
1801         { .compatible = "atmel,at91rm9200-gpio", .data = &at91rm9200_ops },
1802         { .compatible = "microchip,sam9x60-gpio", .data = &sam9x60_ops },
1803         { /* sentinel */ }
1804 };
1805
1806 static int at91_gpio_probe(struct platform_device *pdev)
1807 {
1808         struct device_node *np = pdev->dev.of_node;
1809         struct at91_gpio_chip *at91_chip = NULL;
1810         struct gpio_chip *chip;
1811         struct pinctrl_gpio_range *range;
1812         int ret = 0;
1813         int irq, i;
1814         int alias_idx = of_alias_get_id(np, "gpio");
1815         uint32_t ngpio;
1816         char **names;
1817
1818         BUG_ON(alias_idx >= ARRAY_SIZE(gpio_chips));
1819         if (gpio_chips[alias_idx]) {
1820                 ret = -EBUSY;
1821                 goto err;
1822         }
1823
1824         irq = platform_get_irq(pdev, 0);
1825         if (irq < 0) {
1826                 ret = irq;
1827                 goto err;
1828         }
1829
1830         at91_chip = devm_kzalloc(&pdev->dev, sizeof(*at91_chip), GFP_KERNEL);
1831         if (!at91_chip) {
1832                 ret = -ENOMEM;
1833                 goto err;
1834         }
1835
1836         at91_chip->regbase = devm_platform_ioremap_resource(pdev, 0);
1837         if (IS_ERR(at91_chip->regbase)) {
1838                 ret = PTR_ERR(at91_chip->regbase);
1839                 goto err;
1840         }
1841
1842         at91_chip->ops = (const struct at91_pinctrl_mux_ops *)
1843                 of_match_device(at91_gpio_of_match, &pdev->dev)->data;
1844         at91_chip->pioc_virq = irq;
1845         at91_chip->pioc_idx = alias_idx;
1846
1847         at91_chip->clock = devm_clk_get(&pdev->dev, NULL);
1848         if (IS_ERR(at91_chip->clock)) {
1849                 dev_err(&pdev->dev, "failed to get clock, ignoring.\n");
1850                 ret = PTR_ERR(at91_chip->clock);
1851                 goto err;
1852         }
1853
1854         ret = clk_prepare_enable(at91_chip->clock);
1855         if (ret) {
1856                 dev_err(&pdev->dev, "failed to prepare and enable clock, ignoring.\n");
1857                 goto clk_enable_err;
1858         }
1859
1860         at91_chip->chip = at91_gpio_template;
1861         at91_chip->id = alias_idx;
1862
1863         chip = &at91_chip->chip;
1864         chip->label = dev_name(&pdev->dev);
1865         chip->parent = &pdev->dev;
1866         chip->owner = THIS_MODULE;
1867         chip->base = alias_idx * MAX_NB_GPIO_PER_BANK;
1868
1869         if (!of_property_read_u32(np, "#gpio-lines", &ngpio)) {
1870                 if (ngpio >= MAX_NB_GPIO_PER_BANK)
1871                         pr_err("at91_gpio.%d, gpio-nb >= %d failback to %d\n",
1872                                alias_idx, MAX_NB_GPIO_PER_BANK, MAX_NB_GPIO_PER_BANK);
1873                 else
1874                         chip->ngpio = ngpio;
1875         }
1876
1877         names = devm_kcalloc(&pdev->dev, chip->ngpio, sizeof(char *),
1878                              GFP_KERNEL);
1879
1880         if (!names) {
1881                 ret = -ENOMEM;
1882                 goto clk_enable_err;
1883         }
1884
1885         for (i = 0; i < chip->ngpio; i++)
1886                 names[i] = kasprintf(GFP_KERNEL, "pio%c%d", alias_idx + 'A', i);
1887
1888         chip->names = (const char *const *)names;
1889
1890         range = &at91_chip->range;
1891         range->name = chip->label;
1892         range->id = alias_idx;
1893         range->pin_base = range->base = range->id * MAX_NB_GPIO_PER_BANK;
1894
1895         range->npins = chip->ngpio;
1896         range->gc = chip;
1897
1898         ret = at91_gpio_of_irq_setup(pdev, at91_chip);
1899         if (ret)
1900                 goto gpiochip_add_err;
1901
1902         ret = gpiochip_add_data(chip, at91_chip);
1903         if (ret)
1904                 goto gpiochip_add_err;
1905
1906         gpio_chips[alias_idx] = at91_chip;
1907         platform_set_drvdata(pdev, at91_chip);
1908         gpio_banks = max(gpio_banks, alias_idx + 1);
1909
1910         dev_info(&pdev->dev, "at address %p\n", at91_chip->regbase);
1911
1912         return 0;
1913
1914 gpiochip_add_err:
1915 clk_enable_err:
1916         clk_disable_unprepare(at91_chip->clock);
1917 err:
1918         dev_err(&pdev->dev, "Failure %i for GPIO %i\n", ret, alias_idx);
1919
1920         return ret;
1921 }
1922
1923 static const struct dev_pm_ops at91_gpio_pm_ops = {
1924         SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(at91_gpio_suspend, at91_gpio_resume)
1925 };
1926
1927 static struct platform_driver at91_gpio_driver = {
1928         .driver = {
1929                 .name = "gpio-at91",
1930                 .of_match_table = at91_gpio_of_match,
1931                 .pm = pm_ptr(&at91_gpio_pm_ops),
1932         },
1933         .probe = at91_gpio_probe,
1934 };
1935
1936 static struct platform_driver at91_pinctrl_driver = {
1937         .driver = {
1938                 .name = "pinctrl-at91",
1939                 .of_match_table = at91_pinctrl_of_match,
1940         },
1941         .probe = at91_pinctrl_probe,
1942 };
1943
1944 static struct platform_driver * const drivers[] = {
1945         &at91_gpio_driver,
1946         &at91_pinctrl_driver,
1947 };
1948
1949 static int __init at91_pinctrl_init(void)
1950 {
1951         return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
1952 }
1953 arch_initcall(at91_pinctrl_init);