oxnas: also reset GPIO B registers on boot
[openwrt.git] / target / linux / oxnas / files / drivers / pinctrl / pinctrl-oxnas.c
1 /*
2  * oxnas pinctrl driver based on at91 pinctrl driver
3  *
4  * Copyright (C) 2011-2012 Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
5  *
6  * Under GPLv2 only
7  */
8 #include <linux/clk.h>
9 #include <linux/err.h>
10 #include <linux/init.h>
11 #include <linux/module.h>
12 #include <linux/of.h>
13 #include <linux/of_device.h>
14 #include <linux/of_address.h>
15 #include <linux/of_irq.h>
16 #include <linux/slab.h>
17 #include <linux/interrupt.h>
18 #include <linux/irq.h>
19 #include <linux/irqdomain.h>
20 #include <linux/irqchip/chained_irq.h>
21 #include <linux/io.h>
22 #include <linux/gpio.h>
23 #include <linux/pinctrl/machine.h>
24 #include <linux/pinctrl/pinconf.h>
25 #include <linux/pinctrl/pinctrl.h>
26 #include <linux/pinctrl/pinmux.h>
27 /* Since we request GPIOs from ourself */
28 #include <linux/pinctrl/consumer.h>
29 #include <linux/version.h>
30
31 #include "core.h"
32
33 #include <mach/utils.h>
34
35 #define MAX_NB_GPIO_PER_BANK    32
36 #define MAX_GPIO_BANKS          2
37
38 struct oxnas_gpio_chip {
39         struct gpio_chip        chip;
40         struct pinctrl_gpio_range range;
41         void __iomem            *regbase;  /* GPIOA/B virtual address */
42         void __iomem            *ctrlbase; /* SYS/SEC_CTRL virtual address */
43         struct irq_domain       *domain;   /* associated irq domain */
44 };
45
46 #define to_oxnas_gpio_chip(c) container_of(c, struct oxnas_gpio_chip, chip)
47
48 static struct oxnas_gpio_chip *gpio_chips[MAX_GPIO_BANKS];
49
50 static int gpio_banks;
51
52 #define PULL_UP         (1 << 0)
53 #define PULL_DOWN       (1 << 1)
54 #define DEBOUNCE        (1 << 2)
55
56 /**
57  * struct oxnas_pmx_func - describes pinmux functions
58  * @name: the name of this specific function
59  * @groups: corresponding pin groups
60  * @ngroups: the number of groups
61  */
62 struct oxnas_pmx_func {
63         const char      *name;
64         const char      **groups;
65         unsigned        ngroups;
66 };
67
68 enum oxnas_mux {
69         OXNAS_PINMUX_GPIO,
70         OXNAS_PINMUX_FUNC2,
71         OXNAS_PINMUX_FUNC3,
72         OXNAS_PINMUX_FUNC4,
73         OXNAS_PINMUX_DEBUG,
74         OXNAS_PINMUX_ALT,
75 };
76
77 enum {
78         INPUT_VALUE = 0,
79         OUTPUT_ENABLE = 4,
80         IRQ_PENDING = 0xC,
81         OUTPUT_VALUE = 0x10,
82         OUTPUT_SET = 0x14,
83         OUTPUT_CLEAR = 0x18,
84         OUTPUT_EN_SET = 0x1C,
85         OUTPUT_EN_CLEAR = 0x20,
86         DEBOUNCE_ENABLE = 0x24,
87         RE_IRQ_ENABLE = 0x28, /* rising edge */
88         FE_IRQ_ENABLE = 0x2C, /* falling edge */
89         RE_IRQ_PENDING = 0x30, /* rising edge */
90         FE_IRQ_PENDING = 0x34, /* falling edge */
91         CLOCK_DIV = 0x48,
92         PULL_ENABLE = 0x50,
93         PULL_SENSE = 0x54, /* 1 up, 0 down */
94
95
96         DEBOUNCE_MASK = 0x3FFF0000,
97         /* put hw debounce and soft config at same bit position*/
98         DEBOUNCE_SHIFT = 16
99 };
100
101 enum {
102         PINMUX_SECONDARY_SEL = 0x14,
103         PINMUX_TERTIARY_SEL = 0x8c,
104         PINMUX_QUATERNARY_SEL = 0x94,
105         PINMUX_DEBUG_SEL = 0x9c,
106         PINMUX_ALTERNATIVE_SEL = 0xa4,
107         PINMUX_PULLUP_SEL = 0xac,
108 };
109
110 /**
111  * struct oxnas_pmx_pin - describes an pin mux
112  * @bank: the bank of the pin
113  * @pin: the pin number in the @bank
114  * @mux: the mux mode : gpio or periph_x of the pin i.e. alternate function.
115  * @conf: the configuration of the pin: PULL_UP, MULTIDRIVE etc...
116  */
117 struct oxnas_pmx_pin {
118         uint32_t        bank;
119         uint32_t        pin;
120         enum oxnas_mux  mux;
121         unsigned long   conf;
122 };
123
124 /**
125  * struct oxnas_pin_group - describes an pin group
126  * @name: the name of this specific pin group
127  * @pins_conf: the mux mode for each pin in this group. The size of this
128  *      array is the same as pins.
129  * @pins: an array of discrete physical pins used in this group, taken
130  *      from the driver-local pin enumeration space
131  * @npins: the number of pins in this group array, i.e. the number of
132  *      elements in .pins so we can iterate over that array
133  */
134 struct oxnas_pin_group {
135         const char              *name;
136         struct oxnas_pmx_pin    *pins_conf;
137         unsigned int            *pins;
138         unsigned                npins;
139 };
140
141 struct oxnas_pinctrl {
142         struct device           *dev;
143         struct pinctrl_dev      *pctl;
144
145         int                     nbanks;
146
147         uint32_t                *mux_mask;
148         int                     nmux;
149
150         struct oxnas_pmx_func   *functions;
151         int                     nfunctions;
152
153         struct oxnas_pin_group  *groups;
154         int                     ngroups;
155 };
156
157 static const inline struct oxnas_pin_group *oxnas_pinctrl_find_group_by_name(
158                                 const struct oxnas_pinctrl *info,
159                                 const char *name)
160 {
161         const struct oxnas_pin_group *grp = NULL;
162         int i;
163
164         for (i = 0; i < info->ngroups; i++) {
165                 if (strcmp(info->groups[i].name, name))
166                         continue;
167
168                 grp = &info->groups[i];
169                 dev_dbg(info->dev, "%s: %d 0:%d\n", name, grp->npins,
170                         grp->pins[0]);
171                 break;
172         }
173
174         return grp;
175 }
176
177 static int oxnas_get_groups_count(struct pinctrl_dev *pctldev)
178 {
179         struct oxnas_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
180
181         return info->ngroups;
182 }
183
184 static const char *oxnas_get_group_name(struct pinctrl_dev *pctldev,
185                                        unsigned selector)
186 {
187         struct oxnas_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
188
189         return info->groups[selector].name;
190 }
191
192 static int oxnas_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
193                                const unsigned **pins,
194                                unsigned *npins)
195 {
196         struct oxnas_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
197
198         if (selector >= info->ngroups)
199                 return -EINVAL;
200
201         *pins = info->groups[selector].pins;
202         *npins = info->groups[selector].npins;
203
204         return 0;
205 }
206
207 static void oxnas_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
208                    unsigned offset)
209 {
210         seq_printf(s, "%s", dev_name(pctldev->dev));
211 }
212
213 static int oxnas_dt_node_to_map(struct pinctrl_dev *pctldev,
214                         struct device_node *np,
215                         struct pinctrl_map **map, unsigned *num_maps)
216 {
217         struct oxnas_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
218         const struct oxnas_pin_group *grp;
219         struct pinctrl_map *new_map;
220         struct device_node *parent;
221         int map_num = 1;
222         int i;
223
224         /*
225          * first find the group of this node and check if we need create
226          * config maps for pins
227          */
228         grp = oxnas_pinctrl_find_group_by_name(info, np->name);
229         if (!grp) {
230                 dev_err(info->dev, "unable to find group for node %s\n",
231                         np->name);
232                 return -EINVAL;
233         }
234
235         map_num += grp->npins;
236         new_map = devm_kzalloc(pctldev->dev, sizeof(*new_map) * map_num,
237                                GFP_KERNEL);
238         if (!new_map)
239                 return -ENOMEM;
240
241         *map = new_map;
242         *num_maps = map_num;
243
244         /* create mux map */
245         parent = of_get_parent(np);
246         if (!parent) {
247                 devm_kfree(pctldev->dev, new_map);
248                 return -EINVAL;
249         }
250         new_map[0].type = PIN_MAP_TYPE_MUX_GROUP;
251         new_map[0].data.mux.function = parent->name;
252         new_map[0].data.mux.group = np->name;
253         of_node_put(parent);
254
255         /* create config map */
256         new_map++;
257         for (i = 0; i < grp->npins; i++) {
258                 new_map[i].type = PIN_MAP_TYPE_CONFIGS_PIN;
259                 new_map[i].data.configs.group_or_pin =
260                                 pin_get_name(pctldev, grp->pins[i]);
261                 new_map[i].data.configs.configs = &grp->pins_conf[i].conf;
262                 new_map[i].data.configs.num_configs = 1;
263         }
264
265         dev_dbg(pctldev->dev, "maps: function %s group %s num %d\n",
266                 (*map)->data.mux.function, (*map)->data.mux.group, map_num);
267
268         return 0;
269 }
270
271 static void oxnas_dt_free_map(struct pinctrl_dev *pctldev,
272                                 struct pinctrl_map *map, unsigned num_maps)
273 {
274 }
275
276 static const struct pinctrl_ops oxnas_pctrl_ops = {
277         .get_groups_count       = oxnas_get_groups_count,
278         .get_group_name         = oxnas_get_group_name,
279         .get_group_pins         = oxnas_get_group_pins,
280         .pin_dbg_show           = oxnas_pin_dbg_show,
281         .dt_node_to_map         = oxnas_dt_node_to_map,
282         .dt_free_map            = oxnas_dt_free_map,
283 };
284
285 static void __iomem *pin_to_gpioctrl(struct oxnas_pinctrl *info,
286                                  unsigned int bank)
287 {
288         return gpio_chips[bank]->regbase;
289 }
290
291 static void __iomem *pin_to_muxctrl(struct oxnas_pinctrl *info,
292                                  unsigned int bank)
293 {
294         return gpio_chips[bank]->ctrlbase;
295 }
296
297
298 static inline int pin_to_bank(unsigned pin)
299 {
300         return pin / MAX_NB_GPIO_PER_BANK;
301 }
302
303 static unsigned pin_to_mask(unsigned int pin)
304 {
305         return 1 << pin;
306 }
307
308 static void oxnas_mux_disable_interrupt(void __iomem *pio, unsigned mask)
309 {
310         oxnas_register_clear_mask(pio + RE_IRQ_ENABLE, mask);
311         oxnas_register_clear_mask(pio + FE_IRQ_ENABLE, mask);
312 }
313
314 static unsigned oxnas_mux_get_pullup(void __iomem *pio, unsigned pin)
315 {
316         return (readl_relaxed(pio + PULL_ENABLE) & BIT(pin)) &&
317                 (readl_relaxed(pio + PULL_SENSE) & BIT(pin));
318 }
319
320 static void oxnas_mux_set_pullup(void __iomem *pio, unsigned mask, bool on)
321 {
322         if (on) {
323                 oxnas_register_set_mask(pio + PULL_SENSE, mask);
324                 oxnas_register_set_mask(pio + PULL_ENABLE, mask);
325         } else {
326                 oxnas_register_clear_mask(pio + PULL_ENABLE, mask);
327         }
328 }
329
330 static bool oxnas_mux_get_pulldown(void __iomem *pio, unsigned pin)
331 {
332         return (readl_relaxed(pio + PULL_ENABLE) & BIT(pin)) &&
333                         (!(readl_relaxed(pio + PULL_SENSE) & BIT(pin)));
334 }
335
336 static void oxnas_mux_set_pulldown(void __iomem *pio, unsigned mask, bool on)
337 {
338         if (on) {
339                 oxnas_register_clear_mask(pio + PULL_SENSE, mask);
340                 oxnas_register_set_mask(pio + PULL_ENABLE, mask);
341         } else {
342                 oxnas_register_clear_mask(pio + PULL_ENABLE, mask);
343         };
344 }
345
346 /* unfortunately debounce control are shared */
347 static bool oxnas_mux_get_debounce(void __iomem *pio, unsigned pin, u32 *div)
348 {
349         *div = __raw_readl(pio + CLOCK_DIV) & DEBOUNCE_MASK;
350         return __raw_readl(pio + DEBOUNCE_ENABLE) & BIT(pin);
351 }
352
353 static void oxnas_mux_set_debounce(void __iomem *pio, unsigned mask,
354                                 bool is_on, u32 div)
355 {
356         if (is_on) {
357                 oxnas_register_value_mask(pio + CLOCK_DIV, DEBOUNCE_MASK, div);
358                 oxnas_register_set_mask(pio + DEBOUNCE_ENABLE, mask);
359         } else {
360                 oxnas_register_clear_mask(pio + DEBOUNCE_ENABLE, mask);
361         }
362 }
363
364
365 static void oxnas_mux_set_func2(void __iomem *cio, unsigned mask)
366 {
367 /* in fact, SECONDARY takes precedence, so clear others is not necessary */
368         oxnas_register_set_mask(cio + PINMUX_SECONDARY_SEL, mask);
369         oxnas_register_clear_mask(cio + PINMUX_TERTIARY_SEL, mask);
370         oxnas_register_clear_mask(cio + PINMUX_QUATERNARY_SEL, mask);
371         oxnas_register_clear_mask(cio + PINMUX_DEBUG_SEL, mask);
372         oxnas_register_clear_mask(cio + PINMUX_ALTERNATIVE_SEL, mask);
373 }
374
375 static void oxnas_mux_set_func3(void __iomem *cio, unsigned mask)
376 {
377         oxnas_register_clear_mask(cio + PINMUX_SECONDARY_SEL, mask);
378         oxnas_register_set_mask(cio + PINMUX_TERTIARY_SEL, mask);
379         oxnas_register_clear_mask(cio + PINMUX_QUATERNARY_SEL, mask);
380         oxnas_register_clear_mask(cio + PINMUX_DEBUG_SEL, mask);
381         oxnas_register_clear_mask(cio + PINMUX_ALTERNATIVE_SEL, mask);
382 }
383
384 static void oxnas_mux_set_func4(void __iomem *cio, unsigned mask)
385 {
386         oxnas_register_clear_mask(cio + PINMUX_SECONDARY_SEL, mask);
387         oxnas_register_clear_mask(cio + PINMUX_TERTIARY_SEL, mask);
388         oxnas_register_set_mask(cio + PINMUX_QUATERNARY_SEL, mask);
389         oxnas_register_clear_mask(cio + PINMUX_DEBUG_SEL, mask);
390         oxnas_register_clear_mask(cio + PINMUX_ALTERNATIVE_SEL, mask);
391 }
392
393 static void oxnas_mux_set_func_dbg(void __iomem *cio, unsigned mask)
394 {
395         oxnas_register_clear_mask(cio + PINMUX_SECONDARY_SEL, mask);
396         oxnas_register_clear_mask(cio + PINMUX_TERTIARY_SEL, mask);
397         oxnas_register_clear_mask(cio + PINMUX_QUATERNARY_SEL, mask);
398         oxnas_register_set_mask(cio + PINMUX_DEBUG_SEL, mask);
399         oxnas_register_clear_mask(cio + PINMUX_ALTERNATIVE_SEL, mask);
400 }
401
402 static void oxnas_mux_set_func_alt(void __iomem *cio, unsigned mask)
403 {
404         oxnas_register_clear_mask(cio + PINMUX_SECONDARY_SEL, mask);
405         oxnas_register_clear_mask(cio + PINMUX_TERTIARY_SEL, mask);
406         oxnas_register_clear_mask(cio + PINMUX_QUATERNARY_SEL, mask);
407         oxnas_register_clear_mask(cio + PINMUX_DEBUG_SEL, mask);
408         oxnas_register_set_mask(cio + PINMUX_ALTERNATIVE_SEL, mask);
409 }
410
411 static void oxnas_mux_set_gpio(void __iomem *cio, unsigned mask)
412 {
413         oxnas_register_clear_mask(cio + PINMUX_SECONDARY_SEL, mask);
414         oxnas_register_clear_mask(cio + PINMUX_TERTIARY_SEL, mask);
415         oxnas_register_clear_mask(cio + PINMUX_QUATERNARY_SEL, mask);
416         oxnas_register_clear_mask(cio + PINMUX_DEBUG_SEL, mask);
417         oxnas_register_clear_mask(cio + PINMUX_ALTERNATIVE_SEL, mask);
418 }
419
420 static enum oxnas_mux oxnas_mux_get_func(void __iomem *cio, unsigned mask)
421 {
422         if (readl_relaxed(cio + PINMUX_SECONDARY_SEL) & mask)
423                 return OXNAS_PINMUX_FUNC2;
424         if (readl_relaxed(cio + PINMUX_TERTIARY_SEL) & mask)
425                 return OXNAS_PINMUX_FUNC3;
426         if (readl_relaxed(cio + PINMUX_QUATERNARY_SEL) & mask)
427                 return OXNAS_PINMUX_FUNC4;
428         if (readl_relaxed(cio + PINMUX_DEBUG_SEL) & mask)
429                 return OXNAS_PINMUX_DEBUG;
430         if (readl_relaxed(cio + PINMUX_ALTERNATIVE_SEL) & mask)
431                 return OXNAS_PINMUX_ALT;
432         return OXNAS_PINMUX_GPIO;
433 }
434
435
436 static void oxnas_pin_dbg(const struct device *dev,
437                           const struct oxnas_pmx_pin *pin)
438 {
439         if (pin->mux) {
440                 dev_dbg(dev,
441                         "MF_%c%d configured as periph%c with conf = 0x%lu\n",
442                         pin->bank + 'A', pin->pin, pin->mux - 1 + 'A',
443                         pin->conf);
444         } else {
445                 dev_dbg(dev, "MF_%c%d configured as gpio with conf = 0x%lu\n",
446                         pin->bank + 'A', pin->pin, pin->conf);
447         }
448 }
449
450 static int pin_check_config(struct oxnas_pinctrl *info, const char *name,
451                             int index, const struct oxnas_pmx_pin *pin)
452 {
453         int mux;
454
455         /* check if it's a valid config */
456         if (pin->bank >= info->nbanks) {
457                 dev_err(info->dev, "%s: pin conf %d bank_id %d >= nbanks %d\n",
458                         name, index, pin->bank, info->nbanks);
459                 return -EINVAL;
460         }
461
462         if (pin->pin >= MAX_NB_GPIO_PER_BANK) {
463                 dev_err(info->dev, "%s: pin conf %d pin_bank_id %d >= %d\n",
464                         name, index, pin->pin, MAX_NB_GPIO_PER_BANK);
465                 return -EINVAL;
466         }
467         /* gpio always allowed */
468         if (!pin->mux)
469                 return 0;
470
471         mux = pin->mux - 1;
472
473         if (mux >= info->nmux) {
474                 dev_err(info->dev, "%s: pin conf %d mux_id %d >= nmux %d\n",
475                         name, index, mux, info->nmux);
476                 return -EINVAL;
477         }
478
479         if (!(info->mux_mask[pin->bank * info->nmux + mux] & 1 << pin->pin)) {
480                 dev_err(info->dev, "%s: pin conf %d mux_id %d not supported for MF_%c%d\n",
481                         name, index, mux, pin->bank + 'A', pin->pin);
482                 return -EINVAL;
483         }
484
485         return 0;
486 }
487
488 static void oxnas_mux_gpio_enable(void __iomem *cio, void __iomem *pio,
489                                   unsigned mask, bool input)
490 {
491         oxnas_mux_set_gpio(cio, mask);
492         if (input)
493                 writel_relaxed(mask, pio + OUTPUT_EN_CLEAR);
494         else
495                 writel_relaxed(mask, pio + OUTPUT_EN_SET);
496 }
497
498 static void oxnas_mux_gpio_disable(void __iomem *cio, void __iomem *pio,
499                                    unsigned mask)
500 {
501         /* when switch to other function,  gpio is disabled automatically */
502         return;
503 }
504
505 static int oxnas_pmx_set_mux(struct pinctrl_dev *pctldev, unsigned selector,
506                             unsigned group)
507 {
508         struct oxnas_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
509         const struct oxnas_pmx_pin *pins_conf = info->groups[group].pins_conf;
510         const struct oxnas_pmx_pin *pin;
511         uint32_t npins = info->groups[group].npins;
512         int i, ret;
513         unsigned mask;
514         void __iomem *pio;
515         void __iomem *cio;
516
517         dev_dbg(info->dev, "enable function %s group %s\n",
518                 info->functions[selector].name, info->groups[group].name);
519
520         /* first check that all the pins of the group are valid with a valid
521          * paramter */
522         for (i = 0; i < npins; i++) {
523                 pin = &pins_conf[i];
524                 ret = pin_check_config(info, info->groups[group].name, i, pin);
525                 if (ret)
526                         return ret;
527         }
528
529         for (i = 0; i < npins; i++) {
530                 pin = &pins_conf[i];
531                 oxnas_pin_dbg(info->dev, pin);
532
533                 pio = pin_to_gpioctrl(info, pin->bank);
534                 cio = pin_to_muxctrl(info, pin->bank);
535
536                 mask = pin_to_mask(pin->pin);
537                 oxnas_mux_disable_interrupt(pio, mask);
538
539                 switch (pin->mux) {
540                 case OXNAS_PINMUX_GPIO:
541                         oxnas_mux_gpio_enable(cio, pio, mask, 1);
542                         break;
543                 case OXNAS_PINMUX_FUNC2:
544                         oxnas_mux_set_func2(cio, mask);
545                         break;
546                 case OXNAS_PINMUX_FUNC3:
547                         oxnas_mux_set_func3(cio, mask);
548                         break;
549                 case OXNAS_PINMUX_FUNC4:
550                         oxnas_mux_set_func4(cio, mask);
551                         break;
552                 case OXNAS_PINMUX_DEBUG:
553                         oxnas_mux_set_func_dbg(cio, mask);
554                         break;
555                 case OXNAS_PINMUX_ALT:
556                         oxnas_mux_set_func_alt(cio, mask);
557                         break;
558                 }
559                 if (pin->mux)
560                         oxnas_mux_gpio_disable(cio, pio, mask);
561         }
562
563         return 0;
564 }
565
566 static int oxnas_pmx_get_funcs_count(struct pinctrl_dev *pctldev)
567 {
568         struct oxnas_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
569
570         return info->nfunctions;
571 }
572
573 static const char *oxnas_pmx_get_func_name(struct pinctrl_dev *pctldev,
574                                            unsigned selector)
575 {
576         struct oxnas_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
577
578         return info->functions[selector].name;
579 }
580
581 static int oxnas_pmx_get_groups(struct pinctrl_dev *pctldev, unsigned selector,
582                                 const char * const **groups,
583                                 unsigned * const num_groups)
584 {
585         struct oxnas_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
586
587         *groups = info->functions[selector].groups;
588         *num_groups = info->functions[selector].ngroups;
589
590         return 0;
591 }
592
593 static int oxnas_gpio_request_enable(struct pinctrl_dev *pctldev,
594                                      struct pinctrl_gpio_range *range,
595                                      unsigned offset)
596 {
597         struct oxnas_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
598         struct oxnas_gpio_chip *oxnas_chip;
599         struct gpio_chip *chip;
600         unsigned mask;
601
602         if (!range) {
603                 dev_err(npct->dev, "invalid range\n");
604                 return -EINVAL;
605         }
606         if (!range->gc) {
607                 dev_err(npct->dev, "missing GPIO chip in range\n");
608                 return -EINVAL;
609         }
610         chip = range->gc;
611         oxnas_chip = container_of(chip, struct oxnas_gpio_chip, chip);
612
613         dev_dbg(npct->dev, "enable pin %u as GPIO\n", offset);
614
615         mask = 1 << (offset - chip->base);
616
617         dev_dbg(npct->dev, "enable pin %u as MF_%c%d 0x%x\n",
618                 offset, 'A' + range->id, offset - chip->base, mask);
619
620         oxnas_mux_set_gpio(oxnas_chip->ctrlbase, mask);
621
622         return 0;
623 }
624
625 static void oxnas_gpio_disable_free(struct pinctrl_dev *pctldev,
626                                     struct pinctrl_gpio_range *range,
627                                     unsigned offset)
628 {
629         struct oxnas_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
630
631         dev_dbg(npct->dev, "disable pin %u as GPIO\n", offset);
632         /* Set the pin to some default state, GPIO is usually default */
633 }
634
635 static const struct pinmux_ops oxnas_pmx_ops = {
636         .get_functions_count    = oxnas_pmx_get_funcs_count,
637         .get_function_name      = oxnas_pmx_get_func_name,
638         .get_function_groups    = oxnas_pmx_get_groups,
639         .set_mux                = oxnas_pmx_set_mux,
640         .gpio_request_enable    = oxnas_gpio_request_enable,
641         .gpio_disable_free      = oxnas_gpio_disable_free,
642 };
643
644 static int oxnas_pinconf_get(struct pinctrl_dev *pctldev,
645                              unsigned pin_id, unsigned long *config)
646 {
647         struct oxnas_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
648         void __iomem *pio;
649         unsigned pin;
650         int div;
651
652         dev_dbg(info->dev, "%s:%d, pin_id=%d, config=0x%lx", __func__,
653                 __LINE__, pin_id, *config);
654         pio = pin_to_gpioctrl(info, pin_to_bank(pin_id));
655         pin = pin_id % MAX_NB_GPIO_PER_BANK;
656
657         if (oxnas_mux_get_pullup(pio, pin))
658                 *config |= PULL_UP;
659
660         if (oxnas_mux_get_pulldown(pio, pin))
661                 *config |= PULL_DOWN;
662
663         if (oxnas_mux_get_debounce(pio, pin, &div))
664                 *config |= DEBOUNCE | div;
665         return 0;
666 }
667
668 static int oxnas_pinconf_set(struct pinctrl_dev *pctldev,
669                              unsigned pin_id, unsigned long *configs,
670                              unsigned num_configs)
671 {
672         struct oxnas_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
673         unsigned mask;
674         void __iomem *pio;
675         int i;
676         unsigned long config;
677
678         pio = pin_to_gpioctrl(info, pin_to_bank(pin_id));
679         mask = pin_to_mask(pin_id % MAX_NB_GPIO_PER_BANK);
680
681         for (i = 0; i < num_configs; i++) {
682                 config = configs[i];
683
684                 dev_dbg(info->dev,
685                         "%s:%d, pin_id=%d, config=0x%lx",
686                         __func__, __LINE__, pin_id, config);
687
688                 if ((config & PULL_UP) && (config & PULL_DOWN))
689                         return -EINVAL;
690
691                 oxnas_mux_set_pullup(pio, mask, config & PULL_UP);
692                 oxnas_mux_set_pulldown(pio, mask, config & PULL_DOWN);
693                 oxnas_mux_set_debounce(pio, mask, config & DEBOUNCE,
694                                        config & DEBOUNCE_MASK);
695
696         } /* for each config */
697
698         return 0;
699 }
700
701 static void oxnas_pinconf_dbg_show(struct pinctrl_dev *pctldev,
702                                    struct seq_file *s, unsigned pin_id)
703 {
704
705 }
706
707 static void oxnas_pinconf_group_dbg_show(struct pinctrl_dev *pctldev,
708                                          struct seq_file *s, unsigned group)
709 {
710 }
711
712 static const struct pinconf_ops oxnas_pinconf_ops = {
713         .pin_config_get                 = oxnas_pinconf_get,
714         .pin_config_set                 = oxnas_pinconf_set,
715         .pin_config_dbg_show            = oxnas_pinconf_dbg_show,
716         .pin_config_group_dbg_show      = oxnas_pinconf_group_dbg_show,
717 };
718
719 static struct pinctrl_desc oxnas_pinctrl_desc = {
720         .pctlops        = &oxnas_pctrl_ops,
721         .pmxops         = &oxnas_pmx_ops,
722         .confops        = &oxnas_pinconf_ops,
723         .owner          = THIS_MODULE,
724 };
725
726 static const char *gpio_compat = "plxtech,nas782x-gpio";
727
728 static void oxnas_pinctrl_child_count(struct oxnas_pinctrl *info,
729                                       struct device_node *np)
730 {
731         struct device_node *child;
732
733         for_each_child_of_node(np, child) {
734                 if (of_device_is_compatible(child, gpio_compat)) {
735                         info->nbanks++;
736                 } else {
737                         info->nfunctions++;
738                         info->ngroups += of_get_child_count(child);
739                 }
740         }
741 }
742
743 static int oxnas_pinctrl_mux_mask(struct oxnas_pinctrl *info,
744                                   struct device_node *np)
745 {
746         int ret = 0;
747         int size;
748         const __be32 *list;
749
750         list = of_get_property(np, "plxtech,mux-mask", &size);
751         if (!list) {
752                 dev_err(info->dev, "can not read the mux-mask of %d\n", size);
753                 return -EINVAL;
754         }
755
756         size /= sizeof(*list);
757         if (!size || size % info->nbanks) {
758                 dev_err(info->dev, "wrong mux mask array should be by %d\n",
759                         info->nbanks);
760                 return -EINVAL;
761         }
762         info->nmux = size / info->nbanks;
763
764         info->mux_mask = devm_kzalloc(info->dev, sizeof(u32) * size, GFP_KERNEL);
765         if (!info->mux_mask) {
766                 dev_err(info->dev, "could not alloc mux_mask\n");
767                 return -ENOMEM;
768         }
769
770         ret = of_property_read_u32_array(np, "plxtech,mux-mask",
771                                           info->mux_mask, size);
772         if (ret)
773                 dev_err(info->dev, "can not read the mux-mask of %d\n", size);
774         return ret;
775 }
776
777 static int oxnas_pinctrl_parse_groups(struct device_node *np,
778                                       struct oxnas_pin_group *grp,
779                                       struct oxnas_pinctrl *info, u32 index)
780 {
781         struct oxnas_pmx_pin *pin;
782         int size;
783         const __be32 *list;
784         int i, j;
785
786         dev_dbg(info->dev, "group(%d): %s\n", index, np->name);
787
788         /* Initialise group */
789         grp->name = np->name;
790
791         /*
792          * the binding format is plxtech,pins = <bank pin mux CONFIG ...>,
793          * do sanity check and calculate pins number
794          */
795         list = of_get_property(np, "plxtech,pins", &size);
796         /* we do not check return since it's safe node passed down */
797         size /= sizeof(*list);
798         if (!size || size % 4) {
799                 dev_err(info->dev, "wrong pins number or pins and configs"
800                         " should be divisible by 4\n");
801                 return -EINVAL;
802         }
803
804         grp->npins = size / 4;
805         pin = grp->pins_conf = devm_kzalloc(info->dev,
806                                 grp->npins * sizeof(struct oxnas_pmx_pin),
807                                 GFP_KERNEL);
808         grp->pins = devm_kzalloc(info->dev, grp->npins * sizeof(unsigned int),
809                                 GFP_KERNEL);
810         if (!grp->pins_conf || !grp->pins)
811                 return -ENOMEM;
812
813         for (i = 0, j = 0; i < size; i += 4, j++) {
814                 pin->bank = be32_to_cpu(*list++);
815                 pin->pin = be32_to_cpu(*list++);
816                 grp->pins[j] = pin->bank * MAX_NB_GPIO_PER_BANK + pin->pin;
817                 pin->mux = be32_to_cpu(*list++);
818                 pin->conf = be32_to_cpu(*list++);
819
820                 oxnas_pin_dbg(info->dev, pin);
821                 pin++;
822         }
823
824         return 0;
825 }
826
827 static int oxnas_pinctrl_parse_functions(struct device_node *np,
828                                         struct oxnas_pinctrl *info, u32 index)
829 {
830         struct device_node *child;
831         struct oxnas_pmx_func *func;
832         struct oxnas_pin_group *grp;
833         int ret;
834         static u32 grp_index;
835         u32 i = 0;
836
837         dev_dbg(info->dev, "parse function(%d): %s\n", index, np->name);
838
839         func = &info->functions[index];
840
841         /* Initialise function */
842         func->name = np->name;
843         func->ngroups = of_get_child_count(np);
844         if (func->ngroups <= 0) {
845                 dev_err(info->dev, "no groups defined\n");
846                 return -EINVAL;
847         }
848         func->groups = devm_kzalloc(info->dev,
849                         func->ngroups * sizeof(char *), GFP_KERNEL);
850         if (!func->groups)
851                 return -ENOMEM;
852
853         for_each_child_of_node(np, child) {
854                 func->groups[i] = child->name;
855                 grp = &info->groups[grp_index++];
856                 ret = oxnas_pinctrl_parse_groups(child, grp, info, i++);
857                 if (ret)
858                         return ret;
859         }
860
861         return 0;
862 }
863
864 static struct of_device_id oxnas_pinctrl_of_match[] = {
865         { .compatible = "plxtech,nas782x-pinctrl"},
866         { /* sentinel */ }
867 };
868
869 static int oxnas_pinctrl_probe_dt(struct platform_device *pdev,
870                                  struct oxnas_pinctrl *info)
871 {
872         int ret = 0;
873         int i, j;
874         uint32_t *tmp;
875         struct device_node *np = pdev->dev.of_node;
876         struct device_node *child;
877
878         if (!np)
879                 return -ENODEV;
880
881         info->dev = &pdev->dev;
882
883         oxnas_pinctrl_child_count(info, np);
884
885         if (info->nbanks < 1) {
886                 dev_err(&pdev->dev, "you need to specify atleast one gpio-controller\n");
887                 return -EINVAL;
888         }
889
890         ret = oxnas_pinctrl_mux_mask(info, np);
891         if (ret)
892                 return ret;
893
894         dev_dbg(&pdev->dev, "nmux = %d\n", info->nmux);
895
896         dev_dbg(&pdev->dev, "mux-mask\n");
897         tmp = info->mux_mask;
898         for (i = 0; i < info->nbanks; i++)
899                 for (j = 0; j < info->nmux; j++, tmp++)
900                         dev_dbg(&pdev->dev, "%d:%d\t0x%x\n", i, j, tmp[0]);
901
902         dev_dbg(&pdev->dev, "nfunctions = %d\n", info->nfunctions);
903         dev_dbg(&pdev->dev, "ngroups = %d\n", info->ngroups);
904         info->functions = devm_kzalloc(&pdev->dev, info->nfunctions *
905                                                 sizeof(struct oxnas_pmx_func),
906                                         GFP_KERNEL);
907         if (!info->functions)
908                 return -ENOMEM;
909
910         info->groups = devm_kzalloc(&pdev->dev, info->ngroups *
911                                         sizeof(struct oxnas_pin_group),
912                                     GFP_KERNEL);
913         if (!info->groups)
914                 return -ENOMEM;
915
916         dev_dbg(&pdev->dev, "nbanks = %d\n", info->nbanks);
917         dev_dbg(&pdev->dev, "nfunctions = %d\n", info->nfunctions);
918         dev_dbg(&pdev->dev, "ngroups = %d\n", info->ngroups);
919
920         i = 0;
921
922         for_each_child_of_node(np, child) {
923                 if (of_device_is_compatible(child, gpio_compat))
924                         continue;
925                 ret = oxnas_pinctrl_parse_functions(child, info, i++);
926                 if (ret) {
927                         dev_err(&pdev->dev, "failed to parse function\n");
928                         return ret;
929                 }
930         }
931
932         return 0;
933 }
934
935 static int oxnas_pinctrl_probe(struct platform_device *pdev)
936 {
937         struct oxnas_pinctrl *info;
938         struct pinctrl_pin_desc *pdesc;
939         int ret, i, j, k;
940
941         info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
942         if (!info)
943                 return -ENOMEM;
944
945         ret = oxnas_pinctrl_probe_dt(pdev, info);
946         if (ret)
947                 return ret;
948
949         /*
950          * We need all the GPIO drivers to probe FIRST, or we will not be able
951          * to obtain references to the struct gpio_chip * for them, and we
952          * need this to proceed.
953          */
954         for (i = 0; i < info->nbanks; i++) {
955                 if (!gpio_chips[i]) {
956                         dev_warn(&pdev->dev,
957                                  "GPIO chip %d not registered yet\n", i);
958                         devm_kfree(&pdev->dev, info);
959                         return -EPROBE_DEFER;
960                 }
961         }
962
963         oxnas_pinctrl_desc.name = dev_name(&pdev->dev);
964         oxnas_pinctrl_desc.npins = info->nbanks * MAX_NB_GPIO_PER_BANK;
965         oxnas_pinctrl_desc.pins = pdesc =
966                 devm_kzalloc(&pdev->dev, sizeof(*pdesc) *
967                                 oxnas_pinctrl_desc.npins, GFP_KERNEL);
968
969         if (!oxnas_pinctrl_desc.pins)
970                 return -ENOMEM;
971
972         for (i = 0 , k = 0; i < info->nbanks; i++) {
973                 for (j = 0; j < MAX_NB_GPIO_PER_BANK; j++, k++) {
974                         pdesc->number = k;
975                         pdesc->name = kasprintf(GFP_KERNEL, "MF_%c%d", i + 'A',
976                                                 j);
977                         pdesc++;
978                 }
979         }
980
981         platform_set_drvdata(pdev, info);
982         info->pctl = pinctrl_register(&oxnas_pinctrl_desc, &pdev->dev, info);
983
984         if (!info->pctl) {
985                 dev_err(&pdev->dev, "could not register OX820 pinctrl driver\n");
986                 ret = -EINVAL;
987                 goto err;
988         }
989
990         /* We will handle a range of GPIO pins */
991         for (i = 0; i < info->nbanks; i++)
992                 pinctrl_add_gpio_range(info->pctl, &gpio_chips[i]->range);
993
994         dev_info(&pdev->dev, "initialized OX820 pinctrl driver\n");
995
996         return 0;
997
998 err:
999         return ret;
1000 }
1001
1002 static int oxnas_pinctrl_remove(struct platform_device *pdev)
1003 {
1004         struct oxnas_pinctrl *info = platform_get_drvdata(pdev);
1005
1006         pinctrl_unregister(info->pctl);
1007
1008         return 0;
1009 }
1010
1011 static int oxnas_gpio_request(struct gpio_chip *chip, unsigned offset)
1012 {
1013         /*
1014          * Map back to global GPIO space and request muxing, the direction
1015          * parameter does not matter for this controller.
1016          */
1017         int gpio = chip->base + offset;
1018         int bank = chip->base / chip->ngpio;
1019
1020         dev_dbg(chip->dev, "%s:%d MF_%c%d(%d)\n", __func__, __LINE__,
1021                 'A' + bank, offset, gpio);
1022
1023         return pinctrl_request_gpio(gpio);
1024 }
1025
1026 static void oxnas_gpio_free(struct gpio_chip *chip, unsigned offset)
1027 {
1028         int gpio = chip->base + offset;
1029
1030         pinctrl_free_gpio(gpio);
1031 }
1032
1033 static int oxnas_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
1034 {
1035         struct oxnas_gpio_chip *oxnas_gpio = to_oxnas_gpio_chip(chip);
1036         void __iomem *pio = oxnas_gpio->regbase;
1037
1038         writel_relaxed(BIT(offset), pio + OUTPUT_EN_CLEAR);
1039         return 0;
1040 }
1041
1042 static int oxnas_gpio_get(struct gpio_chip *chip, unsigned offset)
1043 {
1044         struct oxnas_gpio_chip *oxnas_gpio = to_oxnas_gpio_chip(chip);
1045         void __iomem *pio = oxnas_gpio->regbase;
1046         unsigned mask = 1 << offset;
1047         u32 pdsr;
1048
1049         pdsr = readl_relaxed(pio + INPUT_VALUE);
1050         return (pdsr & mask) != 0;
1051 }
1052
1053 static void oxnas_gpio_set(struct gpio_chip *chip, unsigned offset,
1054                                 int val)
1055 {
1056         struct oxnas_gpio_chip *oxnas_gpio = to_oxnas_gpio_chip(chip);
1057         void __iomem *pio = oxnas_gpio->regbase;
1058
1059         if (val)
1060                 writel_relaxed(BIT(offset), pio + OUTPUT_SET);
1061         else
1062                 writel_relaxed(BIT(offset), pio + OUTPUT_CLEAR);
1063
1064 }
1065
1066 static int oxnas_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
1067                                 int val)
1068 {
1069         struct oxnas_gpio_chip *oxnas_gpio = to_oxnas_gpio_chip(chip);
1070         void __iomem *pio = oxnas_gpio->regbase;
1071
1072         if (val)
1073                 writel_relaxed(BIT(offset), pio + OUTPUT_SET);
1074         else
1075                 writel_relaxed(BIT(offset), pio + OUTPUT_CLEAR);
1076
1077         writel_relaxed(BIT(offset), pio + OUTPUT_EN_SET);
1078
1079         return 0;
1080 }
1081
1082 static int oxnas_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
1083 {
1084         struct oxnas_gpio_chip *oxnas_gpio = to_oxnas_gpio_chip(chip);
1085         int virq;
1086
1087         if (offset < chip->ngpio)
1088                 virq = irq_create_mapping(oxnas_gpio->domain, offset);
1089         else
1090                 virq = -ENXIO;
1091
1092         dev_dbg(chip->dev, "%s: request IRQ for GPIO %d, return %d\n",
1093                                 chip->label, offset + chip->base, virq);
1094         return virq;
1095 }
1096
1097 #ifdef CONFIG_DEBUG_FS
1098 static void oxnas_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1099 {
1100         enum oxnas_mux mode;
1101         int i;
1102         struct oxnas_gpio_chip *oxnas_gpio = to_oxnas_gpio_chip(chip);
1103         void __iomem *pio = oxnas_gpio->regbase;
1104         void __iomem *cio = oxnas_gpio->ctrlbase;
1105
1106         for (i = 0; i < chip->ngpio; i++) {
1107                 unsigned pin = chip->base + i;
1108                 unsigned mask = pin_to_mask(pin);
1109                 const char *gpio_label;
1110                 u32 pdsr;
1111
1112                 gpio_label = gpiochip_is_requested(chip, i);
1113                 if (!gpio_label)
1114                         continue;
1115                 /* FIXME */
1116                 mode = oxnas_mux_get_func(cio, mask);
1117                 seq_printf(s, "[%s] GPIO%s%d: ",
1118                            gpio_label, chip->label, i);
1119                 if (mode == OXNAS_PINMUX_GPIO) {
1120                         pdsr = readl_relaxed(pio + INPUT_VALUE);
1121
1122                         seq_printf(s, "[gpio] %s\n",
1123                                    pdsr & mask ?
1124                                    "set" : "clear");
1125                 } else {
1126                         seq_printf(s, "[periph %c]\n",
1127                                    mode + 'A' - 1);
1128                 }
1129         }
1130 }
1131 #else
1132 #define oxnas_gpio_dbg_show     NULL
1133 #endif
1134
1135 /* Several AIC controller irqs are dispatched through this GPIO handler.
1136  * To use any AT91_PIN_* as an externally triggered IRQ, first call
1137  * oxnas_set_gpio_input() then maybe enable its glitch filter.
1138  * Then just request_irq() with the pin ID; it works like any ARM IRQ
1139  * handler.
1140  */
1141
1142 static void gpio_irq_mask(struct irq_data *d)
1143 {
1144         struct oxnas_gpio_chip *oxnas_gpio = irq_data_get_irq_chip_data(d);
1145         void __iomem    *pio = oxnas_gpio->regbase;
1146         unsigned        mask = 1 << d->hwirq;
1147         unsigned        type = irqd_get_trigger_type(d);
1148
1149         /* FIXME: need proper lock */
1150         if (type & IRQ_TYPE_EDGE_RISING)
1151                 oxnas_register_clear_mask(pio + RE_IRQ_ENABLE, mask);
1152         if (type & IRQ_TYPE_EDGE_FALLING)
1153                 oxnas_register_clear_mask(pio + FE_IRQ_ENABLE, mask);
1154 }
1155
1156 static void gpio_irq_unmask(struct irq_data *d)
1157 {
1158         struct oxnas_gpio_chip *oxnas_gpio = irq_data_get_irq_chip_data(d);
1159         void __iomem    *pio = oxnas_gpio->regbase;
1160         unsigned        mask = 1 << d->hwirq;
1161         unsigned        type = irqd_get_trigger_type(d);
1162
1163         /* FIXME: need proper lock */
1164         if (type & IRQ_TYPE_EDGE_RISING)
1165                 oxnas_register_set_mask(pio + RE_IRQ_ENABLE, mask);
1166         if (type & IRQ_TYPE_EDGE_FALLING)
1167                 oxnas_register_set_mask(pio + FE_IRQ_ENABLE, mask);
1168 }
1169
1170
1171 static int gpio_irq_type(struct irq_data *d, unsigned type)
1172 {
1173         if ((type & IRQ_TYPE_EDGE_BOTH) == 0) {
1174                 pr_warn("OX820: Unsupported type for irq %d\n",
1175                         gpio_to_irq(d->irq));
1176                 return -EINVAL;
1177         }
1178         /* seems no way to set trigger type without enable irq, so leave it to unmask time */
1179
1180         return 0;
1181 }
1182
1183 static struct irq_chip gpio_irqchip = {
1184         .name           = "GPIO",
1185         .irq_disable    = gpio_irq_mask,
1186         .irq_mask       = gpio_irq_mask,
1187         .irq_unmask     = gpio_irq_unmask,
1188         .irq_set_type   = gpio_irq_type,
1189 };
1190
1191 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,2,0)
1192 static void gpio_irq_handler(unsigned irq, struct irq_desc *desc)
1193 #else
1194 static void gpio_irq_handler(struct irq_desc *desc)
1195 #endif
1196 {
1197         struct irq_chip *chip = irq_desc_get_chip(desc);
1198         struct irq_data *idata = irq_desc_get_irq_data(desc);
1199         struct oxnas_gpio_chip *oxnas_gpio = irq_data_get_irq_chip_data(idata);
1200         void __iomem *pio = oxnas_gpio->regbase;
1201         unsigned long isr;
1202         int n;
1203
1204         chained_irq_enter(chip, desc);
1205         for (;;) {
1206                 /* TODO: see if it works */
1207                 isr = readl_relaxed(pio + IRQ_PENDING);
1208                 if (!isr)
1209                         break;
1210                 /* acks pending interrupts */
1211                 writel_relaxed(isr, pio + IRQ_PENDING);
1212
1213                 for_each_set_bit(n, &isr, BITS_PER_LONG) {
1214                         generic_handle_irq(irq_find_mapping(oxnas_gpio->domain,
1215                                                             n));
1216                 }
1217         }
1218         chained_irq_exit(chip, desc);
1219         /* now it may re-trigger */
1220 }
1221
1222 /*
1223  * This lock class tells lockdep that GPIO irqs are in a different
1224  * category than their parents, so it won't report false recursion.
1225  */
1226 static struct lock_class_key gpio_lock_class;
1227
1228 static int oxnas_gpio_irq_map(struct irq_domain *h, unsigned int virq,
1229                               irq_hw_number_t hw)
1230 {
1231         struct oxnas_gpio_chip *oxnas_gpio = h->host_data;
1232
1233         irq_set_lockdep_class(virq, &gpio_lock_class);
1234
1235         irq_set_chip_and_handler(virq, &gpio_irqchip, handle_edge_irq);
1236 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,2,0)
1237         set_irq_flags(virq, IRQF_VALID);
1238 #endif
1239         irq_set_chip_data(virq, oxnas_gpio);
1240
1241         return 0;
1242 }
1243
1244 static int oxnas_gpio_irq_domain_xlate(struct irq_domain *d,
1245                                        struct device_node *ctrlr,
1246                                        const u32 *intspec,
1247                                        unsigned int intsize,
1248                                        irq_hw_number_t *out_hwirq,
1249                                        unsigned int *out_type)
1250 {
1251         struct oxnas_gpio_chip *oxnas_gpio = d->host_data;
1252         int ret;
1253         int pin = oxnas_gpio->chip.base + intspec[0];
1254
1255         if (WARN_ON(intsize < 2))
1256                 return -EINVAL;
1257         *out_hwirq = intspec[0];
1258         *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
1259
1260         ret = gpio_request(pin, ctrlr->full_name);
1261         if (ret)
1262                 return ret;
1263
1264         ret = gpio_direction_input(pin);
1265         if (ret)
1266                 return ret;
1267
1268         return 0;
1269 }
1270
1271 static struct irq_domain_ops oxnas_gpio_ops = {
1272         .map    = oxnas_gpio_irq_map,
1273         .xlate  = oxnas_gpio_irq_domain_xlate,
1274 };
1275
1276 static int oxnas_gpio_of_irq_setup(struct device_node *node,
1277                                    struct oxnas_gpio_chip *oxnas_gpio,
1278                                    unsigned int irq)
1279 {
1280         /* Disable irqs of this controller */
1281         writel_relaxed(0, oxnas_gpio->regbase + RE_IRQ_ENABLE);
1282         writel_relaxed(0, oxnas_gpio->regbase + FE_IRQ_ENABLE);
1283
1284         /* Setup irq domain */
1285         oxnas_gpio->domain = irq_domain_add_linear(node, oxnas_gpio->chip.ngpio,
1286                                                    &oxnas_gpio_ops, oxnas_gpio);
1287         if (!oxnas_gpio->domain)
1288                 panic("oxnas_gpio: couldn't allocate irq domain (DT).\n");
1289
1290         irq_set_chip_data(irq, oxnas_gpio);
1291         irq_set_chained_handler(irq, gpio_irq_handler);
1292
1293         return 0;
1294 }
1295
1296 /* This structure is replicated for each GPIO block allocated at probe time */
1297 static struct gpio_chip oxnas_gpio_template = {
1298         .request                = oxnas_gpio_request,
1299         .free                   = oxnas_gpio_free,
1300         .direction_input        = oxnas_gpio_direction_input,
1301         .get                    = oxnas_gpio_get,
1302         .direction_output       = oxnas_gpio_direction_output,
1303         .set                    = oxnas_gpio_set,
1304         .to_irq                 = oxnas_gpio_to_irq,
1305         .dbg_show               = oxnas_gpio_dbg_show,
1306         .can_sleep              = 0,
1307         .ngpio                  = MAX_NB_GPIO_PER_BANK,
1308 };
1309
1310 static struct of_device_id oxnas_gpio_of_match[] = {
1311         { .compatible = "plxtech,nas782x-gpio"},
1312         { /* sentinel */ }
1313 };
1314
1315 static int oxnas_gpio_probe(struct platform_device *pdev)
1316 {
1317         struct device_node *np = pdev->dev.of_node;
1318         struct resource *res;
1319         struct oxnas_gpio_chip *oxnas_chip = NULL;
1320         struct gpio_chip *chip;
1321         struct pinctrl_gpio_range *range;
1322         int ret = 0;
1323         int irq, i;
1324         int alias_idx = of_alias_get_id(np, "gpio");
1325         uint32_t ngpio;
1326         char **names;
1327
1328         BUG_ON(alias_idx >= ARRAY_SIZE(gpio_chips));
1329         if (gpio_chips[alias_idx]) {
1330                 ret = -EBUSY;
1331                 goto err;
1332         }
1333
1334         irq = platform_get_irq(pdev, 0);
1335         if (irq < 0) {
1336                 ret = irq;
1337                 goto err;
1338         }
1339
1340         oxnas_chip = devm_kzalloc(&pdev->dev, sizeof(*oxnas_chip), GFP_KERNEL);
1341         if (!oxnas_chip) {
1342                 ret = -ENOMEM;
1343                 goto err;
1344         }
1345
1346         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1347         oxnas_chip->regbase = devm_ioremap_resource(&pdev->dev, res);
1348         if (IS_ERR(oxnas_chip->regbase)) {
1349                 ret = PTR_ERR(oxnas_chip->regbase);
1350                 goto err;
1351         }
1352
1353         res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1354         oxnas_chip->ctrlbase = devm_ioremap_resource(&pdev->dev, res);
1355         if (IS_ERR(oxnas_chip->ctrlbase)) {
1356                 ret = PTR_ERR(oxnas_chip->ctrlbase);
1357                 goto err;
1358         }
1359
1360         oxnas_chip->chip = oxnas_gpio_template;
1361
1362         chip = &oxnas_chip->chip;
1363         chip->of_node = np;
1364         chip->label = dev_name(&pdev->dev);
1365         chip->dev = &pdev->dev;
1366         chip->owner = THIS_MODULE;
1367         chip->base = alias_idx * MAX_NB_GPIO_PER_BANK;
1368
1369         if (!of_property_read_u32(np, "#gpio-lines", &ngpio)) {
1370                 if (ngpio > MAX_NB_GPIO_PER_BANK)
1371                         pr_err("oxnas_gpio.%d, gpio-nb >= %d failback to %d\n",
1372                                alias_idx, MAX_NB_GPIO_PER_BANK,
1373                                MAX_NB_GPIO_PER_BANK);
1374                 else
1375                         chip->ngpio = ngpio;
1376         }
1377
1378         names = devm_kzalloc(&pdev->dev, sizeof(char *) * chip->ngpio,
1379                              GFP_KERNEL);
1380
1381         if (!names) {
1382                 ret = -ENOMEM;
1383                 goto err;
1384         }
1385
1386         for (i = 0; i < chip->ngpio; i++)
1387                 names[i] = kasprintf(GFP_KERNEL, "MF_%c%d", alias_idx + 'A', i);
1388
1389         chip->names = (const char *const *)names;
1390
1391         range = &oxnas_chip->range;
1392         range->name = chip->label;
1393         range->id = alias_idx;
1394         range->pin_base = range->base = range->id * MAX_NB_GPIO_PER_BANK;
1395
1396         range->npins = chip->ngpio;
1397         range->gc = chip;
1398
1399         ret = gpiochip_add(chip);
1400         if (ret)
1401                 goto err;
1402
1403         gpio_chips[alias_idx] = oxnas_chip;
1404         gpio_banks = max(gpio_banks, alias_idx + 1);
1405
1406         oxnas_gpio_of_irq_setup(np, oxnas_chip, irq);
1407
1408         dev_info(&pdev->dev, "at address %p\n", oxnas_chip->regbase);
1409
1410         return 0;
1411 err:
1412         dev_err(&pdev->dev, "Failure %i for GPIO %i\n", ret, alias_idx);
1413
1414         return ret;
1415 }
1416
1417 static struct platform_driver oxnas_gpio_driver = {
1418         .driver = {
1419                 .name = "gpio-oxnas",
1420                 .owner = THIS_MODULE,
1421                 .of_match_table = of_match_ptr(oxnas_gpio_of_match),
1422         },
1423         .probe = oxnas_gpio_probe,
1424 };
1425
1426 static struct platform_driver oxnas_pinctrl_driver = {
1427         .driver = {
1428                 .name = "pinctrl-oxnas",
1429                 .owner = THIS_MODULE,
1430                 .of_match_table = of_match_ptr(oxnas_pinctrl_of_match),
1431         },
1432         .probe = oxnas_pinctrl_probe,
1433         .remove = oxnas_pinctrl_remove,
1434 };
1435
1436 static int __init oxnas_pinctrl_init(void)
1437 {
1438         int ret;
1439
1440         ret = platform_driver_register(&oxnas_gpio_driver);
1441         if (ret)
1442                 return ret;
1443         return platform_driver_register(&oxnas_pinctrl_driver);
1444 }
1445 arch_initcall(oxnas_pinctrl_init);
1446
1447 static void __exit oxnas_pinctrl_exit(void)
1448 {
1449         platform_driver_unregister(&oxnas_pinctrl_driver);
1450 }
1451
1452 module_exit(oxnas_pinctrl_exit);
1453 MODULE_AUTHOR("Ma Hajun <mahaijuns@gmail.com>");
1454 MODULE_DESCRIPTION("Plxtech Nas782x pinctrl driver");
1455 MODULE_LICENSE("GPL v2");