6b017854ce61bc20466ca6c393fccec6efe18719
[linux-2.6-microblaze.git] / drivers / gpio / gpio-mvebu.c
1 /*
2  * GPIO driver for Marvell SoCs
3  *
4  * Copyright (C) 2012 Marvell
5  *
6  * Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
7  * Andrew Lunn <andrew@lunn.ch>
8  * Sebastian Hesselbarth <sebastian.hesselbarth@gmail.com>
9  *
10  * This file is licensed under the terms of the GNU General Public
11  * License version 2.  This program is licensed "as is" without any
12  * warranty of any kind, whether express or implied.
13  *
14  * This driver is a fairly straightforward GPIO driver for the
15  * complete family of Marvell EBU SoC platforms (Orion, Dove,
16  * Kirkwood, Discovery, Armada 370/XP). The only complexity of this
17  * driver is the different register layout that exists between the
18  * non-SMP platforms (Orion, Dove, Kirkwood, Armada 370) and the SMP
19  * platforms (MV78200 from the Discovery family and the Armada
20  * XP). Therefore, this driver handles three variants of the GPIO
21  * block:
22  * - the basic variant, called "orion-gpio", with the simplest
23  *   register set. Used on Orion, Dove, Kirkwoord, Armada 370 and
24  *   non-SMP Discovery systems
25  * - the mv78200 variant for MV78200 Discovery systems. This variant
26  *   turns the edge mask and level mask registers into CPU0 edge
27  *   mask/level mask registers, and adds CPU1 edge mask/level mask
28  *   registers.
29  * - the armadaxp variant for Armada XP systems. This variant keeps
30  *   the normal cause/edge mask/level mask registers when the global
31  *   interrupts are used, but adds per-CPU cause/edge mask/level mask
32  *   registers n a separate memory area for the per-CPU GPIO
33  *   interrupts.
34  */
35
36 #include <linux/bitops.h>
37 #include <linux/clk.h>
38 #include <linux/err.h>
39 #include <linux/gpio/driver.h>
40 #include <linux/gpio/consumer.h>
41 #include <linux/gpio/machine.h>
42 #include <linux/init.h>
43 #include <linux/io.h>
44 #include <linux/irq.h>
45 #include <linux/irqchip/chained_irq.h>
46 #include <linux/irqdomain.h>
47 #include <linux/mfd/syscon.h>
48 #include <linux/of_device.h>
49 #include <linux/pinctrl/consumer.h>
50 #include <linux/platform_device.h>
51 #include <linux/pwm.h>
52 #include <linux/regmap.h>
53 #include <linux/slab.h>
54
55 /*
56  * GPIO unit register offsets.
57  */
58 #define GPIO_OUT_OFF                    0x0000
59 #define GPIO_IO_CONF_OFF                0x0004
60 #define GPIO_BLINK_EN_OFF               0x0008
61 #define GPIO_IN_POL_OFF                 0x000c
62 #define GPIO_DATA_IN_OFF                0x0010
63 #define GPIO_EDGE_CAUSE_OFF             0x0014
64 #define GPIO_EDGE_MASK_OFF              0x0018
65 #define GPIO_LEVEL_MASK_OFF             0x001c
66 #define GPIO_BLINK_CNT_SELECT_OFF       0x0020
67
68 /*
69  * PWM register offsets.
70  */
71 #define PWM_BLINK_ON_DURATION_OFF       0x0
72 #define PWM_BLINK_OFF_DURATION_OFF      0x4
73
74
75 /* The MV78200 has per-CPU registers for edge mask and level mask */
76 #define GPIO_EDGE_MASK_MV78200_OFF(cpu)   ((cpu) ? 0x30 : 0x18)
77 #define GPIO_LEVEL_MASK_MV78200_OFF(cpu)  ((cpu) ? 0x34 : 0x1C)
78
79 /*
80  * The Armada XP has per-CPU registers for interrupt cause, interrupt
81  * mask and interrupt level mask. Those are in percpu_regs range.
82  */
83 #define GPIO_EDGE_CAUSE_ARMADAXP_OFF(cpu) ((cpu) * 0x4)
84 #define GPIO_EDGE_MASK_ARMADAXP_OFF(cpu)  (0x10 + (cpu) * 0x4)
85 #define GPIO_LEVEL_MASK_ARMADAXP_OFF(cpu) (0x20 + (cpu) * 0x4)
86
87 #define MVEBU_GPIO_SOC_VARIANT_ORION    0x1
88 #define MVEBU_GPIO_SOC_VARIANT_MV78200  0x2
89 #define MVEBU_GPIO_SOC_VARIANT_ARMADAXP 0x3
90 #define MVEBU_GPIO_SOC_VARIANT_A8K      0x4
91
92 #define MVEBU_MAX_GPIO_PER_BANK         32
93
94 struct mvebu_pwm {
95         struct regmap           *regs;
96         unsigned long            clk_rate;
97         struct gpio_desc        *gpiod;
98         struct pwm_chip          chip;
99         spinlock_t               lock;
100         struct mvebu_gpio_chip  *mvchip;
101
102         /* Used to preserve GPIO/PWM registers across suspend/resume */
103         u32                      blink_select;
104         u32                      blink_on_duration;
105         u32                      blink_off_duration;
106 };
107
108 struct mvebu_gpio_chip {
109         struct gpio_chip   chip;
110         struct regmap     *regs;
111         u32                offset;
112         struct regmap     *percpu_regs;
113         int                irqbase;
114         struct irq_domain *domain;
115         int                soc_variant;
116
117         /* Used for PWM support */
118         struct clk        *clk;
119         struct mvebu_pwm  *mvpwm;
120
121         /* Used to preserve GPIO registers across suspend/resume */
122         u32                out_reg;
123         u32                io_conf_reg;
124         u32                blink_en_reg;
125         u32                in_pol_reg;
126         u32                edge_mask_regs[4];
127         u32                level_mask_regs[4];
128 };
129
130 /*
131  * Functions returning addresses of individual registers for a given
132  * GPIO controller.
133  */
134
135 static void mvebu_gpioreg_edge_cause(struct mvebu_gpio_chip *mvchip,
136                          struct regmap **map, unsigned int *offset)
137 {
138         int cpu;
139
140         switch (mvchip->soc_variant) {
141         case MVEBU_GPIO_SOC_VARIANT_ORION:
142         case MVEBU_GPIO_SOC_VARIANT_MV78200:
143         case MVEBU_GPIO_SOC_VARIANT_A8K:
144                 *map = mvchip->regs;
145                 *offset = GPIO_EDGE_CAUSE_OFF + mvchip->offset;
146                 break;
147         case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
148                 cpu = smp_processor_id();
149                 *map = mvchip->percpu_regs;
150                 *offset = GPIO_EDGE_CAUSE_ARMADAXP_OFF(cpu);
151                 break;
152         default:
153                 BUG();
154         }
155 }
156
157 static u32
158 mvebu_gpio_read_edge_cause(struct mvebu_gpio_chip *mvchip)
159 {
160         struct regmap *map;
161         unsigned int offset;
162         u32 val;
163
164         mvebu_gpioreg_edge_cause(mvchip, &map, &offset);
165         regmap_read(map, offset, &val);
166
167         return val;
168 }
169
170 static void
171 mvebu_gpio_write_edge_cause(struct mvebu_gpio_chip *mvchip, u32 val)
172 {
173         struct regmap *map;
174         unsigned int offset;
175
176         mvebu_gpioreg_edge_cause(mvchip, &map, &offset);
177         regmap_write(map, offset, val);
178 }
179
180 static inline void
181 mvebu_gpioreg_edge_mask(struct mvebu_gpio_chip *mvchip,
182                         struct regmap **map, unsigned int *offset)
183 {
184         int cpu;
185
186         switch (mvchip->soc_variant) {
187         case MVEBU_GPIO_SOC_VARIANT_ORION:
188         case MVEBU_GPIO_SOC_VARIANT_A8K:
189                 *map = mvchip->regs;
190                 *offset = GPIO_EDGE_MASK_OFF + mvchip->offset;
191                 break;
192         case MVEBU_GPIO_SOC_VARIANT_MV78200:
193                 cpu = smp_processor_id();
194                 *map = mvchip->regs;
195                 *offset = GPIO_EDGE_MASK_MV78200_OFF(cpu);
196                 break;
197         case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
198                 cpu = smp_processor_id();
199                 *map = mvchip->percpu_regs;
200                 *offset = GPIO_EDGE_MASK_ARMADAXP_OFF(cpu);
201                 break;
202         default:
203                 BUG();
204         }
205 }
206
207 static u32
208 mvebu_gpio_read_edge_mask(struct mvebu_gpio_chip *mvchip)
209 {
210         struct regmap *map;
211         unsigned int offset;
212         u32 val;
213
214         mvebu_gpioreg_edge_mask(mvchip, &map, &offset);
215         regmap_read(map, offset, &val);
216
217         return val;
218 }
219
220 static void
221 mvebu_gpio_write_edge_mask(struct mvebu_gpio_chip *mvchip, u32 val)
222 {
223         struct regmap *map;
224         unsigned int offset;
225
226         mvebu_gpioreg_edge_mask(mvchip, &map, &offset);
227         regmap_write(map, offset, val);
228 }
229
230 static void
231 mvebu_gpioreg_level_mask(struct mvebu_gpio_chip *mvchip,
232                          struct regmap **map, unsigned int *offset)
233 {
234         int cpu;
235
236         switch (mvchip->soc_variant) {
237         case MVEBU_GPIO_SOC_VARIANT_ORION:
238         case MVEBU_GPIO_SOC_VARIANT_A8K:
239                 *map = mvchip->regs;
240                 *offset = GPIO_LEVEL_MASK_OFF + mvchip->offset;
241                 break;
242         case MVEBU_GPIO_SOC_VARIANT_MV78200:
243                 cpu = smp_processor_id();
244                 *map = mvchip->regs;
245                 *offset = GPIO_LEVEL_MASK_MV78200_OFF(cpu);
246                 break;
247         case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
248                 cpu = smp_processor_id();
249                 *map = mvchip->percpu_regs;
250                 *offset = GPIO_LEVEL_MASK_ARMADAXP_OFF(cpu);
251                 break;
252         default:
253                 BUG();
254         }
255 }
256
257 static u32
258 mvebu_gpio_read_level_mask(struct mvebu_gpio_chip *mvchip)
259 {
260         struct regmap *map;
261         unsigned int offset;
262         u32 val;
263
264         mvebu_gpioreg_level_mask(mvchip, &map, &offset);
265         regmap_read(map, offset, &val);
266
267         return val;
268 }
269
270 static void
271 mvebu_gpio_write_level_mask(struct mvebu_gpio_chip *mvchip, u32 val)
272 {
273         struct regmap *map;
274         unsigned int offset;
275
276         mvebu_gpioreg_level_mask(mvchip, &map, &offset);
277         regmap_write(map, offset, val);
278 }
279
280 /*
281  * Functions returning offsets of individual registers for a given
282  * PWM controller.
283  */
284 static unsigned int mvebu_pwmreg_blink_on_duration(struct mvebu_pwm *mvpwm)
285 {
286         return PWM_BLINK_ON_DURATION_OFF;
287 }
288
289 static unsigned int mvebu_pwmreg_blink_off_duration(struct mvebu_pwm *mvpwm)
290 {
291         return PWM_BLINK_OFF_DURATION_OFF;
292 }
293
294 /*
295  * Functions implementing the gpio_chip methods
296  */
297 static void mvebu_gpio_set(struct gpio_chip *chip, unsigned int pin, int value)
298 {
299         struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip);
300
301         regmap_update_bits(mvchip->regs, GPIO_OUT_OFF + mvchip->offset,
302                            BIT(pin), value ? BIT(pin) : 0);
303 }
304
305 static int mvebu_gpio_get(struct gpio_chip *chip, unsigned int pin)
306 {
307         struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip);
308         u32 u;
309
310         regmap_read(mvchip->regs, GPIO_IO_CONF_OFF + mvchip->offset, &u);
311
312         if (u & BIT(pin)) {
313                 u32 data_in, in_pol;
314
315                 regmap_read(mvchip->regs, GPIO_DATA_IN_OFF + mvchip->offset,
316                             &data_in);
317                 regmap_read(mvchip->regs, GPIO_IN_POL_OFF + mvchip->offset,
318                             &in_pol);
319                 u = data_in ^ in_pol;
320         } else {
321                 regmap_read(mvchip->regs, GPIO_OUT_OFF + mvchip->offset, &u);
322         }
323
324         return (u >> pin) & 1;
325 }
326
327 static void mvebu_gpio_blink(struct gpio_chip *chip, unsigned int pin,
328                              int value)
329 {
330         struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip);
331
332         regmap_update_bits(mvchip->regs, GPIO_BLINK_EN_OFF + mvchip->offset,
333                            BIT(pin), value ? BIT(pin) : 0);
334 }
335
336 static int mvebu_gpio_direction_input(struct gpio_chip *chip, unsigned int pin)
337 {
338         struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip);
339         int ret;
340
341         /*
342          * Check with the pinctrl driver whether this pin is usable as
343          * an input GPIO
344          */
345         ret = pinctrl_gpio_direction_input(chip->base + pin);
346         if (ret)
347                 return ret;
348
349         regmap_update_bits(mvchip->regs, GPIO_IO_CONF_OFF + mvchip->offset,
350                            BIT(pin), BIT(pin));
351
352         return 0;
353 }
354
355 static int mvebu_gpio_direction_output(struct gpio_chip *chip, unsigned int pin,
356                                        int value)
357 {
358         struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip);
359         int ret;
360
361         /*
362          * Check with the pinctrl driver whether this pin is usable as
363          * an output GPIO
364          */
365         ret = pinctrl_gpio_direction_output(chip->base + pin);
366         if (ret)
367                 return ret;
368
369         mvebu_gpio_blink(chip, pin, 0);
370         mvebu_gpio_set(chip, pin, value);
371
372         regmap_update_bits(mvchip->regs, GPIO_IO_CONF_OFF + mvchip->offset,
373                            BIT(pin), 0);
374
375         return 0;
376 }
377
378 static int mvebu_gpio_get_direction(struct gpio_chip *chip, unsigned int pin)
379 {
380         struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip);
381         u32 u;
382
383         regmap_read(mvchip->regs, GPIO_IO_CONF_OFF + mvchip->offset, &u);
384
385         if (u & BIT(pin))
386                 return GPIO_LINE_DIRECTION_IN;
387
388         return GPIO_LINE_DIRECTION_OUT;
389 }
390
391 static int mvebu_gpio_to_irq(struct gpio_chip *chip, unsigned int pin)
392 {
393         struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip);
394
395         return irq_create_mapping(mvchip->domain, pin);
396 }
397
398 /*
399  * Functions implementing the irq_chip methods
400  */
401 static void mvebu_gpio_irq_ack(struct irq_data *d)
402 {
403         struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
404         struct mvebu_gpio_chip *mvchip = gc->private;
405         u32 mask = d->mask;
406
407         irq_gc_lock(gc);
408         mvebu_gpio_write_edge_cause(mvchip, ~mask);
409         irq_gc_unlock(gc);
410 }
411
412 static void mvebu_gpio_edge_irq_mask(struct irq_data *d)
413 {
414         struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
415         struct mvebu_gpio_chip *mvchip = gc->private;
416         struct irq_chip_type *ct = irq_data_get_chip_type(d);
417         u32 mask = d->mask;
418
419         irq_gc_lock(gc);
420         ct->mask_cache_priv &= ~mask;
421         mvebu_gpio_write_edge_mask(mvchip, ct->mask_cache_priv);
422         irq_gc_unlock(gc);
423 }
424
425 static void mvebu_gpio_edge_irq_unmask(struct irq_data *d)
426 {
427         struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
428         struct mvebu_gpio_chip *mvchip = gc->private;
429         struct irq_chip_type *ct = irq_data_get_chip_type(d);
430         u32 mask = d->mask;
431
432         irq_gc_lock(gc);
433         mvebu_gpio_write_edge_cause(mvchip, ~mask);
434         ct->mask_cache_priv |= mask;
435         mvebu_gpio_write_edge_mask(mvchip, ct->mask_cache_priv);
436         irq_gc_unlock(gc);
437 }
438
439 static void mvebu_gpio_level_irq_mask(struct irq_data *d)
440 {
441         struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
442         struct mvebu_gpio_chip *mvchip = gc->private;
443         struct irq_chip_type *ct = irq_data_get_chip_type(d);
444         u32 mask = d->mask;
445
446         irq_gc_lock(gc);
447         ct->mask_cache_priv &= ~mask;
448         mvebu_gpio_write_level_mask(mvchip, ct->mask_cache_priv);
449         irq_gc_unlock(gc);
450 }
451
452 static void mvebu_gpio_level_irq_unmask(struct irq_data *d)
453 {
454         struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
455         struct mvebu_gpio_chip *mvchip = gc->private;
456         struct irq_chip_type *ct = irq_data_get_chip_type(d);
457         u32 mask = d->mask;
458
459         irq_gc_lock(gc);
460         ct->mask_cache_priv |= mask;
461         mvebu_gpio_write_level_mask(mvchip, ct->mask_cache_priv);
462         irq_gc_unlock(gc);
463 }
464
465 /*****************************************************************************
466  * MVEBU GPIO IRQ
467  *
468  * GPIO_IN_POL register controls whether GPIO_DATA_IN will hold the same
469  * value of the line or the opposite value.
470  *
471  * Level IRQ handlers: DATA_IN is used directly as cause register.
472  *                     Interrupt are masked by LEVEL_MASK registers.
473  * Edge IRQ handlers:  Change in DATA_IN are latched in EDGE_CAUSE.
474  *                     Interrupt are masked by EDGE_MASK registers.
475  * Both-edge handlers: Similar to regular Edge handlers, but also swaps
476  *                     the polarity to catch the next line transaction.
477  *                     This is a race condition that might not perfectly
478  *                     work on some use cases.
479  *
480  * Every eight GPIO lines are grouped (OR'ed) before going up to main
481  * cause register.
482  *
483  *                    EDGE  cause    mask
484  *        data-in   /--------| |-----| |----\
485  *     -----| |-----                         ---- to main cause reg
486  *           X      \----------------| |----/
487  *        polarity    LEVEL          mask
488  *
489  ****************************************************************************/
490
491 static int mvebu_gpio_irq_set_type(struct irq_data *d, unsigned int type)
492 {
493         struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
494         struct irq_chip_type *ct = irq_data_get_chip_type(d);
495         struct mvebu_gpio_chip *mvchip = gc->private;
496         int pin;
497         u32 u;
498
499         pin = d->hwirq;
500
501         regmap_read(mvchip->regs, GPIO_IO_CONF_OFF + mvchip->offset, &u);
502         if ((u & BIT(pin)) == 0)
503                 return -EINVAL;
504
505         type &= IRQ_TYPE_SENSE_MASK;
506         if (type == IRQ_TYPE_NONE)
507                 return -EINVAL;
508
509         /* Check if we need to change chip and handler */
510         if (!(ct->type & type))
511                 if (irq_setup_alt_chip(d, type))
512                         return -EINVAL;
513
514         /*
515          * Configure interrupt polarity.
516          */
517         switch (type) {
518         case IRQ_TYPE_EDGE_RISING:
519         case IRQ_TYPE_LEVEL_HIGH:
520                 regmap_update_bits(mvchip->regs,
521                                    GPIO_IN_POL_OFF + mvchip->offset,
522                                    BIT(pin), 0);
523                 break;
524         case IRQ_TYPE_EDGE_FALLING:
525         case IRQ_TYPE_LEVEL_LOW:
526                 regmap_update_bits(mvchip->regs,
527                                    GPIO_IN_POL_OFF + mvchip->offset,
528                                    BIT(pin), BIT(pin));
529                 break;
530         case IRQ_TYPE_EDGE_BOTH: {
531                 u32 data_in, in_pol, val;
532
533                 regmap_read(mvchip->regs,
534                             GPIO_IN_POL_OFF + mvchip->offset, &in_pol);
535                 regmap_read(mvchip->regs,
536                             GPIO_DATA_IN_OFF + mvchip->offset, &data_in);
537
538                 /*
539                  * set initial polarity based on current input level
540                  */
541                 if ((data_in ^ in_pol) & BIT(pin))
542                         val = BIT(pin); /* falling */
543                 else
544                         val = 0; /* raising */
545
546                 regmap_update_bits(mvchip->regs,
547                                    GPIO_IN_POL_OFF + mvchip->offset,
548                                    BIT(pin), val);
549                 break;
550         }
551         }
552         return 0;
553 }
554
555 static void mvebu_gpio_irq_handler(struct irq_desc *desc)
556 {
557         struct mvebu_gpio_chip *mvchip = irq_desc_get_handler_data(desc);
558         struct irq_chip *chip = irq_desc_get_chip(desc);
559         u32 cause, type, data_in, level_mask, edge_cause, edge_mask;
560         int i;
561
562         if (mvchip == NULL)
563                 return;
564
565         chained_irq_enter(chip, desc);
566
567         regmap_read(mvchip->regs, GPIO_DATA_IN_OFF + mvchip->offset, &data_in);
568         level_mask = mvebu_gpio_read_level_mask(mvchip);
569         edge_cause = mvebu_gpio_read_edge_cause(mvchip);
570         edge_mask  = mvebu_gpio_read_edge_mask(mvchip);
571
572         cause = (data_in & level_mask) | (edge_cause & edge_mask);
573
574         for (i = 0; i < mvchip->chip.ngpio; i++) {
575                 int irq;
576
577                 irq = irq_find_mapping(mvchip->domain, i);
578
579                 if (!(cause & BIT(i)))
580                         continue;
581
582                 type = irq_get_trigger_type(irq);
583                 if ((type & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH) {
584                         /* Swap polarity (race with GPIO line) */
585                         u32 polarity;
586
587                         regmap_read(mvchip->regs,
588                                     GPIO_IN_POL_OFF + mvchip->offset,
589                                     &polarity);
590                         polarity ^= BIT(i);
591                         regmap_write(mvchip->regs,
592                                      GPIO_IN_POL_OFF + mvchip->offset,
593                                      polarity);
594                 }
595
596                 generic_handle_irq(irq);
597         }
598
599         chained_irq_exit(chip, desc);
600 }
601
602 static const struct regmap_config mvebu_gpio_regmap_config = {
603         .reg_bits = 32,
604         .reg_stride = 4,
605         .val_bits = 32,
606         .fast_io = true,
607 };
608
609 /*
610  * Functions implementing the pwm_chip methods
611  */
612 static struct mvebu_pwm *to_mvebu_pwm(struct pwm_chip *chip)
613 {
614         return container_of(chip, struct mvebu_pwm, chip);
615 }
616
617 static int mvebu_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
618 {
619         struct mvebu_pwm *mvpwm = to_mvebu_pwm(chip);
620         struct mvebu_gpio_chip *mvchip = mvpwm->mvchip;
621         struct gpio_desc *desc;
622         unsigned long flags;
623         int ret = 0;
624
625         spin_lock_irqsave(&mvpwm->lock, flags);
626
627         if (mvpwm->gpiod) {
628                 ret = -EBUSY;
629         } else {
630                 desc = gpiochip_request_own_desc(&mvchip->chip,
631                                                  pwm->hwpwm, "mvebu-pwm",
632                                                  GPIO_ACTIVE_HIGH,
633                                                  GPIOD_OUT_LOW);
634                 if (IS_ERR(desc)) {
635                         ret = PTR_ERR(desc);
636                         goto out;
637                 }
638
639                 mvpwm->gpiod = desc;
640         }
641 out:
642         spin_unlock_irqrestore(&mvpwm->lock, flags);
643         return ret;
644 }
645
646 static void mvebu_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
647 {
648         struct mvebu_pwm *mvpwm = to_mvebu_pwm(chip);
649         unsigned long flags;
650
651         spin_lock_irqsave(&mvpwm->lock, flags);
652         gpiochip_free_own_desc(mvpwm->gpiod);
653         mvpwm->gpiod = NULL;
654         spin_unlock_irqrestore(&mvpwm->lock, flags);
655 }
656
657 static void mvebu_pwm_get_state(struct pwm_chip *chip,
658                                 struct pwm_device *pwm,
659                                 struct pwm_state *state) {
660
661         struct mvebu_pwm *mvpwm = to_mvebu_pwm(chip);
662         struct mvebu_gpio_chip *mvchip = mvpwm->mvchip;
663         unsigned long long val;
664         unsigned long flags;
665         u32 u;
666
667         spin_lock_irqsave(&mvpwm->lock, flags);
668
669         regmap_read(mvpwm->regs, mvebu_pwmreg_blink_on_duration(mvpwm), &u);
670         val = (unsigned long long) u * NSEC_PER_SEC;
671         val = DIV_ROUND_UP_ULL(val, mvpwm->clk_rate);
672         if (val)
673                 state->duty_cycle = val;
674         else
675                 state->duty_cycle = 1;
676
677         val = (unsigned long long) u; /* on duration */
678         regmap_read(mvpwm->regs, mvebu_pwmreg_blink_off_duration(mvpwm), &u);
679         val += (unsigned long long) u; /* period = on + off duration */
680         val *= NSEC_PER_SEC;
681         val = DIV_ROUND_UP_ULL(val, mvpwm->clk_rate);
682         if (val)
683                 state->period = val;
684         else
685                 state->period = 1;
686
687         regmap_read(mvchip->regs, GPIO_BLINK_EN_OFF + mvchip->offset, &u);
688         if (u)
689                 state->enabled = true;
690         else
691                 state->enabled = false;
692
693         spin_unlock_irqrestore(&mvpwm->lock, flags);
694 }
695
696 static int mvebu_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
697                            const struct pwm_state *state)
698 {
699         struct mvebu_pwm *mvpwm = to_mvebu_pwm(chip);
700         struct mvebu_gpio_chip *mvchip = mvpwm->mvchip;
701         unsigned long long val;
702         unsigned long flags;
703         unsigned int on, off;
704
705         val = (unsigned long long) mvpwm->clk_rate * state->duty_cycle;
706         do_div(val, NSEC_PER_SEC);
707         if (val > UINT_MAX)
708                 return -EINVAL;
709         if (val)
710                 on = val;
711         else
712                 on = 1;
713
714         val = (unsigned long long) mvpwm->clk_rate * state->period;
715         do_div(val, NSEC_PER_SEC);
716         val -= on;
717         if (val > UINT_MAX)
718                 return -EINVAL;
719         if (val)
720                 off = val;
721         else
722                 off = 1;
723
724         spin_lock_irqsave(&mvpwm->lock, flags);
725
726         regmap_write(mvpwm->regs, mvebu_pwmreg_blink_on_duration(mvpwm), on);
727         regmap_write(mvpwm->regs, mvebu_pwmreg_blink_off_duration(mvpwm), off);
728         if (state->enabled)
729                 mvebu_gpio_blink(&mvchip->chip, pwm->hwpwm, 1);
730         else
731                 mvebu_gpio_blink(&mvchip->chip, pwm->hwpwm, 0);
732
733         spin_unlock_irqrestore(&mvpwm->lock, flags);
734
735         return 0;
736 }
737
738 static const struct pwm_ops mvebu_pwm_ops = {
739         .request = mvebu_pwm_request,
740         .free = mvebu_pwm_free,
741         .get_state = mvebu_pwm_get_state,
742         .apply = mvebu_pwm_apply,
743         .owner = THIS_MODULE,
744 };
745
746 static void __maybe_unused mvebu_pwm_suspend(struct mvebu_gpio_chip *mvchip)
747 {
748         struct mvebu_pwm *mvpwm = mvchip->mvpwm;
749
750         regmap_read(mvchip->regs, GPIO_BLINK_CNT_SELECT_OFF + mvchip->offset,
751                     &mvpwm->blink_select);
752         regmap_read(mvpwm->regs, mvebu_pwmreg_blink_on_duration(mvpwm),
753                     &mvpwm->blink_on_duration);
754         regmap_read(mvpwm->regs, mvebu_pwmreg_blink_off_duration(mvpwm),
755                     &mvpwm->blink_off_duration);
756 }
757
758 static void __maybe_unused mvebu_pwm_resume(struct mvebu_gpio_chip *mvchip)
759 {
760         struct mvebu_pwm *mvpwm = mvchip->mvpwm;
761
762         regmap_write(mvchip->regs, GPIO_BLINK_CNT_SELECT_OFF + mvchip->offset,
763                      mvpwm->blink_select);
764         regmap_write(mvpwm->regs, mvebu_pwmreg_blink_on_duration(mvpwm),
765                      mvpwm->blink_on_duration);
766         regmap_write(mvpwm->regs, mvebu_pwmreg_blink_off_duration(mvpwm),
767                      mvpwm->blink_off_duration);
768 }
769
770 static int mvebu_pwm_probe(struct platform_device *pdev,
771                            struct mvebu_gpio_chip *mvchip,
772                            int id)
773 {
774         struct device *dev = &pdev->dev;
775         struct mvebu_pwm *mvpwm;
776         void __iomem *base;
777         u32 set;
778
779         if (!of_device_is_compatible(mvchip->chip.of_node,
780                                      "marvell,armada-370-gpio"))
781                 return 0;
782
783         /*
784          * There are only two sets of PWM configuration registers for
785          * all the GPIO lines on those SoCs which this driver reserves
786          * for the first two GPIO chips. So if the resource is missing
787          * we can't treat it as an error.
788          */
789         if (!platform_get_resource_byname(pdev, IORESOURCE_MEM, "pwm"))
790                 return 0;
791
792         if (IS_ERR(mvchip->clk))
793                 return PTR_ERR(mvchip->clk);
794
795         /*
796          * Use set A for lines of GPIO chip with id 0, B for GPIO chip
797          * with id 1. Don't allow further GPIO chips to be used for PWM.
798          */
799         if (id == 0)
800                 set = 0;
801         else if (id == 1)
802                 set = U32_MAX;
803         else
804                 return -EINVAL;
805         regmap_write(mvchip->regs,
806                      GPIO_BLINK_CNT_SELECT_OFF + mvchip->offset, set);
807
808         mvpwm = devm_kzalloc(dev, sizeof(struct mvebu_pwm), GFP_KERNEL);
809         if (!mvpwm)
810                 return -ENOMEM;
811         mvchip->mvpwm = mvpwm;
812         mvpwm->mvchip = mvchip;
813
814         base = devm_platform_ioremap_resource_byname(pdev, "pwm");
815         if (IS_ERR(base))
816                 return PTR_ERR(base);
817
818         mvpwm->regs = devm_regmap_init_mmio(&pdev->dev, base,
819                                             &mvebu_gpio_regmap_config);
820         if (IS_ERR(mvpwm->regs))
821                 return PTR_ERR(mvpwm->regs);
822
823         mvpwm->clk_rate = clk_get_rate(mvchip->clk);
824         if (!mvpwm->clk_rate) {
825                 dev_err(dev, "failed to get clock rate\n");
826                 return -EINVAL;
827         }
828
829         mvpwm->chip.dev = dev;
830         mvpwm->chip.ops = &mvebu_pwm_ops;
831         mvpwm->chip.npwm = mvchip->chip.ngpio;
832         /*
833          * There may already be some PWM allocated, so we can't force
834          * mvpwm->chip.base to a fixed point like mvchip->chip.base.
835          * So, we let pwmchip_add() do the numbering and take the next free
836          * region.
837          */
838         mvpwm->chip.base = -1;
839
840         spin_lock_init(&mvpwm->lock);
841
842         return pwmchip_add(&mvpwm->chip);
843 }
844
845 #ifdef CONFIG_DEBUG_FS
846 #include <linux/seq_file.h>
847
848 static void mvebu_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
849 {
850         struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip);
851         u32 out, io_conf, blink, in_pol, data_in, cause, edg_msk, lvl_msk;
852         const char *label;
853         int i;
854
855         regmap_read(mvchip->regs, GPIO_OUT_OFF + mvchip->offset, &out);
856         regmap_read(mvchip->regs, GPIO_IO_CONF_OFF + mvchip->offset, &io_conf);
857         regmap_read(mvchip->regs, GPIO_BLINK_EN_OFF + mvchip->offset, &blink);
858         regmap_read(mvchip->regs, GPIO_IN_POL_OFF + mvchip->offset, &in_pol);
859         regmap_read(mvchip->regs, GPIO_DATA_IN_OFF + mvchip->offset, &data_in);
860         cause   = mvebu_gpio_read_edge_cause(mvchip);
861         edg_msk = mvebu_gpio_read_edge_mask(mvchip);
862         lvl_msk = mvebu_gpio_read_level_mask(mvchip);
863
864         for_each_requested_gpio(chip, i, label) {
865                 u32 msk;
866                 bool is_out;
867
868                 msk = BIT(i);
869                 is_out = !(io_conf & msk);
870
871                 seq_printf(s, " gpio-%-3d (%-20.20s)", chip->base + i, label);
872
873                 if (is_out) {
874                         seq_printf(s, " out %s %s\n",
875                                    out & msk ? "hi" : "lo",
876                                    blink & msk ? "(blink )" : "");
877                         continue;
878                 }
879
880                 seq_printf(s, " in  %s (act %s) - IRQ",
881                            (data_in ^ in_pol) & msk  ? "hi" : "lo",
882                            in_pol & msk ? "lo" : "hi");
883                 if (!((edg_msk | lvl_msk) & msk)) {
884                         seq_puts(s, " disabled\n");
885                         continue;
886                 }
887                 if (edg_msk & msk)
888                         seq_puts(s, " edge ");
889                 if (lvl_msk & msk)
890                         seq_puts(s, " level");
891                 seq_printf(s, " (%s)\n", cause & msk ? "pending" : "clear  ");
892         }
893 }
894 #else
895 #define mvebu_gpio_dbg_show NULL
896 #endif
897
898 static const struct of_device_id mvebu_gpio_of_match[] = {
899         {
900                 .compatible = "marvell,orion-gpio",
901                 .data       = (void *) MVEBU_GPIO_SOC_VARIANT_ORION,
902         },
903         {
904                 .compatible = "marvell,mv78200-gpio",
905                 .data       = (void *) MVEBU_GPIO_SOC_VARIANT_MV78200,
906         },
907         {
908                 .compatible = "marvell,armadaxp-gpio",
909                 .data       = (void *) MVEBU_GPIO_SOC_VARIANT_ARMADAXP,
910         },
911         {
912                 .compatible = "marvell,armada-370-gpio",
913                 .data       = (void *) MVEBU_GPIO_SOC_VARIANT_ORION,
914         },
915         {
916                 .compatible = "marvell,armada-8k-gpio",
917                 .data       = (void *) MVEBU_GPIO_SOC_VARIANT_A8K,
918         },
919         {
920                 /* sentinel */
921         },
922 };
923
924 static int mvebu_gpio_suspend(struct platform_device *pdev, pm_message_t state)
925 {
926         struct mvebu_gpio_chip *mvchip = platform_get_drvdata(pdev);
927         int i;
928
929         regmap_read(mvchip->regs, GPIO_OUT_OFF + mvchip->offset,
930                     &mvchip->out_reg);
931         regmap_read(mvchip->regs, GPIO_IO_CONF_OFF + mvchip->offset,
932                     &mvchip->io_conf_reg);
933         regmap_read(mvchip->regs, GPIO_BLINK_EN_OFF + mvchip->offset,
934                     &mvchip->blink_en_reg);
935         regmap_read(mvchip->regs, GPIO_IN_POL_OFF + mvchip->offset,
936                     &mvchip->in_pol_reg);
937
938         switch (mvchip->soc_variant) {
939         case MVEBU_GPIO_SOC_VARIANT_ORION:
940         case MVEBU_GPIO_SOC_VARIANT_A8K:
941                 regmap_read(mvchip->regs, GPIO_EDGE_MASK_OFF + mvchip->offset,
942                             &mvchip->edge_mask_regs[0]);
943                 regmap_read(mvchip->regs, GPIO_LEVEL_MASK_OFF + mvchip->offset,
944                             &mvchip->level_mask_regs[0]);
945                 break;
946         case MVEBU_GPIO_SOC_VARIANT_MV78200:
947                 for (i = 0; i < 2; i++) {
948                         regmap_read(mvchip->regs,
949                                     GPIO_EDGE_MASK_MV78200_OFF(i),
950                                     &mvchip->edge_mask_regs[i]);
951                         regmap_read(mvchip->regs,
952                                     GPIO_LEVEL_MASK_MV78200_OFF(i),
953                                     &mvchip->level_mask_regs[i]);
954                 }
955                 break;
956         case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
957                 for (i = 0; i < 4; i++) {
958                         regmap_read(mvchip->regs,
959                                     GPIO_EDGE_MASK_ARMADAXP_OFF(i),
960                                     &mvchip->edge_mask_regs[i]);
961                         regmap_read(mvchip->regs,
962                                     GPIO_LEVEL_MASK_ARMADAXP_OFF(i),
963                                     &mvchip->level_mask_regs[i]);
964                 }
965                 break;
966         default:
967                 BUG();
968         }
969
970         if (IS_ENABLED(CONFIG_PWM))
971                 mvebu_pwm_suspend(mvchip);
972
973         return 0;
974 }
975
976 static int mvebu_gpio_resume(struct platform_device *pdev)
977 {
978         struct mvebu_gpio_chip *mvchip = platform_get_drvdata(pdev);
979         int i;
980
981         regmap_write(mvchip->regs, GPIO_OUT_OFF + mvchip->offset,
982                      mvchip->out_reg);
983         regmap_write(mvchip->regs, GPIO_IO_CONF_OFF + mvchip->offset,
984                      mvchip->io_conf_reg);
985         regmap_write(mvchip->regs, GPIO_BLINK_EN_OFF + mvchip->offset,
986                      mvchip->blink_en_reg);
987         regmap_write(mvchip->regs, GPIO_IN_POL_OFF + mvchip->offset,
988                      mvchip->in_pol_reg);
989
990         switch (mvchip->soc_variant) {
991         case MVEBU_GPIO_SOC_VARIANT_ORION:
992         case MVEBU_GPIO_SOC_VARIANT_A8K:
993                 regmap_write(mvchip->regs, GPIO_EDGE_MASK_OFF + mvchip->offset,
994                              mvchip->edge_mask_regs[0]);
995                 regmap_write(mvchip->regs, GPIO_LEVEL_MASK_OFF + mvchip->offset,
996                              mvchip->level_mask_regs[0]);
997                 break;
998         case MVEBU_GPIO_SOC_VARIANT_MV78200:
999                 for (i = 0; i < 2; i++) {
1000                         regmap_write(mvchip->regs,
1001                                      GPIO_EDGE_MASK_MV78200_OFF(i),
1002                                      mvchip->edge_mask_regs[i]);
1003                         regmap_write(mvchip->regs,
1004                                      GPIO_LEVEL_MASK_MV78200_OFF(i),
1005                                      mvchip->level_mask_regs[i]);
1006                 }
1007                 break;
1008         case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
1009                 for (i = 0; i < 4; i++) {
1010                         regmap_write(mvchip->regs,
1011                                      GPIO_EDGE_MASK_ARMADAXP_OFF(i),
1012                                      mvchip->edge_mask_regs[i]);
1013                         regmap_write(mvchip->regs,
1014                                      GPIO_LEVEL_MASK_ARMADAXP_OFF(i),
1015                                      mvchip->level_mask_regs[i]);
1016                 }
1017                 break;
1018         default:
1019                 BUG();
1020         }
1021
1022         if (IS_ENABLED(CONFIG_PWM))
1023                 mvebu_pwm_resume(mvchip);
1024
1025         return 0;
1026 }
1027
1028 static int mvebu_gpio_probe_raw(struct platform_device *pdev,
1029                                 struct mvebu_gpio_chip *mvchip)
1030 {
1031         void __iomem *base;
1032
1033         base = devm_platform_ioremap_resource(pdev, 0);
1034         if (IS_ERR(base))
1035                 return PTR_ERR(base);
1036
1037         mvchip->regs = devm_regmap_init_mmio(&pdev->dev, base,
1038                                              &mvebu_gpio_regmap_config);
1039         if (IS_ERR(mvchip->regs))
1040                 return PTR_ERR(mvchip->regs);
1041
1042         /*
1043          * For the legacy SoCs, the regmap directly maps to the GPIO
1044          * registers, so no offset is needed.
1045          */
1046         mvchip->offset = 0;
1047
1048         /*
1049          * The Armada XP has a second range of registers for the
1050          * per-CPU registers
1051          */
1052         if (mvchip->soc_variant == MVEBU_GPIO_SOC_VARIANT_ARMADAXP) {
1053                 base = devm_platform_ioremap_resource(pdev, 1);
1054                 if (IS_ERR(base))
1055                         return PTR_ERR(base);
1056
1057                 mvchip->percpu_regs =
1058                         devm_regmap_init_mmio(&pdev->dev, base,
1059                                               &mvebu_gpio_regmap_config);
1060                 if (IS_ERR(mvchip->percpu_regs))
1061                         return PTR_ERR(mvchip->percpu_regs);
1062         }
1063
1064         return 0;
1065 }
1066
1067 static int mvebu_gpio_probe_syscon(struct platform_device *pdev,
1068                                    struct mvebu_gpio_chip *mvchip)
1069 {
1070         mvchip->regs = syscon_node_to_regmap(pdev->dev.parent->of_node);
1071         if (IS_ERR(mvchip->regs))
1072                 return PTR_ERR(mvchip->regs);
1073
1074         if (of_property_read_u32(pdev->dev.of_node, "offset", &mvchip->offset))
1075                 return -EINVAL;
1076
1077         return 0;
1078 }
1079
1080 static int mvebu_gpio_probe(struct platform_device *pdev)
1081 {
1082         struct mvebu_gpio_chip *mvchip;
1083         const struct of_device_id *match;
1084         struct device_node *np = pdev->dev.of_node;
1085         struct irq_chip_generic *gc;
1086         struct irq_chip_type *ct;
1087         unsigned int ngpios;
1088         bool have_irqs;
1089         int soc_variant;
1090         int i, cpu, id;
1091         int err;
1092
1093         match = of_match_device(mvebu_gpio_of_match, &pdev->dev);
1094         if (match)
1095                 soc_variant = (unsigned long) match->data;
1096         else
1097                 soc_variant = MVEBU_GPIO_SOC_VARIANT_ORION;
1098
1099         /* Some gpio controllers do not provide irq support */
1100         err = platform_irq_count(pdev);
1101         if (err < 0)
1102                 return err;
1103
1104         have_irqs = err != 0;
1105
1106         mvchip = devm_kzalloc(&pdev->dev, sizeof(struct mvebu_gpio_chip),
1107                               GFP_KERNEL);
1108         if (!mvchip)
1109                 return -ENOMEM;
1110
1111         platform_set_drvdata(pdev, mvchip);
1112
1113         if (of_property_read_u32(pdev->dev.of_node, "ngpios", &ngpios)) {
1114                 dev_err(&pdev->dev, "Missing ngpios OF property\n");
1115                 return -ENODEV;
1116         }
1117
1118         id = of_alias_get_id(pdev->dev.of_node, "gpio");
1119         if (id < 0) {
1120                 dev_err(&pdev->dev, "Couldn't get OF id\n");
1121                 return id;
1122         }
1123
1124         mvchip->clk = devm_clk_get(&pdev->dev, NULL);
1125         /* Not all SoCs require a clock.*/
1126         if (!IS_ERR(mvchip->clk))
1127                 clk_prepare_enable(mvchip->clk);
1128
1129         mvchip->soc_variant = soc_variant;
1130         mvchip->chip.label = dev_name(&pdev->dev);
1131         mvchip->chip.parent = &pdev->dev;
1132         mvchip->chip.request = gpiochip_generic_request;
1133         mvchip->chip.free = gpiochip_generic_free;
1134         mvchip->chip.get_direction = mvebu_gpio_get_direction;
1135         mvchip->chip.direction_input = mvebu_gpio_direction_input;
1136         mvchip->chip.get = mvebu_gpio_get;
1137         mvchip->chip.direction_output = mvebu_gpio_direction_output;
1138         mvchip->chip.set = mvebu_gpio_set;
1139         if (have_irqs)
1140                 mvchip->chip.to_irq = mvebu_gpio_to_irq;
1141         mvchip->chip.base = id * MVEBU_MAX_GPIO_PER_BANK;
1142         mvchip->chip.ngpio = ngpios;
1143         mvchip->chip.can_sleep = false;
1144         mvchip->chip.of_node = np;
1145         mvchip->chip.dbg_show = mvebu_gpio_dbg_show;
1146
1147         if (soc_variant == MVEBU_GPIO_SOC_VARIANT_A8K)
1148                 err = mvebu_gpio_probe_syscon(pdev, mvchip);
1149         else
1150                 err = mvebu_gpio_probe_raw(pdev, mvchip);
1151
1152         if (err)
1153                 return err;
1154
1155         /*
1156          * Mask and clear GPIO interrupts.
1157          */
1158         switch (soc_variant) {
1159         case MVEBU_GPIO_SOC_VARIANT_ORION:
1160         case MVEBU_GPIO_SOC_VARIANT_A8K:
1161                 regmap_write(mvchip->regs,
1162                              GPIO_EDGE_CAUSE_OFF + mvchip->offset, 0);
1163                 regmap_write(mvchip->regs,
1164                              GPIO_EDGE_MASK_OFF + mvchip->offset, 0);
1165                 regmap_write(mvchip->regs,
1166                              GPIO_LEVEL_MASK_OFF + mvchip->offset, 0);
1167                 break;
1168         case MVEBU_GPIO_SOC_VARIANT_MV78200:
1169                 regmap_write(mvchip->regs, GPIO_EDGE_CAUSE_OFF, 0);
1170                 for (cpu = 0; cpu < 2; cpu++) {
1171                         regmap_write(mvchip->regs,
1172                                      GPIO_EDGE_MASK_MV78200_OFF(cpu), 0);
1173                         regmap_write(mvchip->regs,
1174                                      GPIO_LEVEL_MASK_MV78200_OFF(cpu), 0);
1175                 }
1176                 break;
1177         case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
1178                 regmap_write(mvchip->regs, GPIO_EDGE_CAUSE_OFF, 0);
1179                 regmap_write(mvchip->regs, GPIO_EDGE_MASK_OFF, 0);
1180                 regmap_write(mvchip->regs, GPIO_LEVEL_MASK_OFF, 0);
1181                 for (cpu = 0; cpu < 4; cpu++) {
1182                         regmap_write(mvchip->percpu_regs,
1183                                      GPIO_EDGE_CAUSE_ARMADAXP_OFF(cpu), 0);
1184                         regmap_write(mvchip->percpu_regs,
1185                                      GPIO_EDGE_MASK_ARMADAXP_OFF(cpu), 0);
1186                         regmap_write(mvchip->percpu_regs,
1187                                      GPIO_LEVEL_MASK_ARMADAXP_OFF(cpu), 0);
1188                 }
1189                 break;
1190         default:
1191                 BUG();
1192         }
1193
1194         devm_gpiochip_add_data(&pdev->dev, &mvchip->chip, mvchip);
1195
1196         /* Some MVEBU SoCs have simple PWM support for GPIO lines */
1197         if (IS_ENABLED(CONFIG_PWM)) {
1198                 err = mvebu_pwm_probe(pdev, mvchip, id);
1199                 if (err)
1200                         return err;
1201         }
1202
1203         /* Some gpio controllers do not provide irq support */
1204         if (!have_irqs)
1205                 return 0;
1206
1207         mvchip->domain =
1208             irq_domain_add_linear(np, ngpios, &irq_generic_chip_ops, NULL);
1209         if (!mvchip->domain) {
1210                 dev_err(&pdev->dev, "couldn't allocate irq domain %s (DT).\n",
1211                         mvchip->chip.label);
1212                 err = -ENODEV;
1213                 goto err_pwm;
1214         }
1215
1216         err = irq_alloc_domain_generic_chips(
1217             mvchip->domain, ngpios, 2, np->name, handle_level_irq,
1218             IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_LEVEL, 0, 0);
1219         if (err) {
1220                 dev_err(&pdev->dev, "couldn't allocate irq chips %s (DT).\n",
1221                         mvchip->chip.label);
1222                 goto err_domain;
1223         }
1224
1225         /*
1226          * NOTE: The common accessors cannot be used because of the percpu
1227          * access to the mask registers
1228          */
1229         gc = irq_get_domain_generic_chip(mvchip->domain, 0);
1230         gc->private = mvchip;
1231         ct = &gc->chip_types[0];
1232         ct->type = IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW;
1233         ct->chip.irq_mask = mvebu_gpio_level_irq_mask;
1234         ct->chip.irq_unmask = mvebu_gpio_level_irq_unmask;
1235         ct->chip.irq_set_type = mvebu_gpio_irq_set_type;
1236         ct->chip.name = mvchip->chip.label;
1237
1238         ct = &gc->chip_types[1];
1239         ct->type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
1240         ct->chip.irq_ack = mvebu_gpio_irq_ack;
1241         ct->chip.irq_mask = mvebu_gpio_edge_irq_mask;
1242         ct->chip.irq_unmask = mvebu_gpio_edge_irq_unmask;
1243         ct->chip.irq_set_type = mvebu_gpio_irq_set_type;
1244         ct->handler = handle_edge_irq;
1245         ct->chip.name = mvchip->chip.label;
1246
1247         /*
1248          * Setup the interrupt handlers. Each chip can have up to 4
1249          * interrupt handlers, with each handler dealing with 8 GPIO
1250          * pins.
1251          */
1252         for (i = 0; i < 4; i++) {
1253                 int irq = platform_get_irq_optional(pdev, i);
1254
1255                 if (irq < 0)
1256                         continue;
1257                 irq_set_chained_handler_and_data(irq, mvebu_gpio_irq_handler,
1258                                                  mvchip);
1259         }
1260
1261         return 0;
1262
1263 err_domain:
1264         irq_domain_remove(mvchip->domain);
1265 err_pwm:
1266         pwmchip_remove(&mvchip->mvpwm->chip);
1267
1268         return err;
1269 }
1270
1271 static struct platform_driver mvebu_gpio_driver = {
1272         .driver         = {
1273                 .name           = "mvebu-gpio",
1274                 .of_match_table = mvebu_gpio_of_match,
1275         },
1276         .probe          = mvebu_gpio_probe,
1277         .suspend        = mvebu_gpio_suspend,
1278         .resume         = mvebu_gpio_resume,
1279 };
1280 builtin_platform_driver(mvebu_gpio_driver);