gpio/nomadik: cache [rf]w?imsc
[firefly-linux-kernel-4.4.55.git] / drivers / gpio / gpio-nomadik.c
1 /*
2  * Generic GPIO driver for logic cells found in the Nomadik SoC
3  *
4  * Copyright (C) 2008,2009 STMicroelectronics
5  * Copyright (C) 2009 Alessandro Rubini <rubini@unipv.it>
6  *   Rewritten based on work by Prafulla WADASKAR <prafulla.wadaskar@st.com>
7  * Copyright (C) 2011 Linus Walleij <linus.walleij@linaro.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  */
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/device.h>
17 #include <linux/platform_device.h>
18 #include <linux/io.h>
19 #include <linux/clk.h>
20 #include <linux/err.h>
21 #include <linux/gpio.h>
22 #include <linux/spinlock.h>
23 #include <linux/interrupt.h>
24 #include <linux/irq.h>
25 #include <linux/slab.h>
26
27 #include <asm/mach/irq.h>
28
29 #include <plat/pincfg.h>
30 #include <plat/gpio-nomadik.h>
31 #include <mach/hardware.h>
32 #include <asm/gpio.h>
33
34 /*
35  * The GPIO module in the Nomadik family of Systems-on-Chip is an
36  * AMBA device, managing 32 pins and alternate functions.  The logic block
37  * is currently used in the Nomadik and ux500.
38  *
39  * Symbols in this file are called "nmk_gpio" for "nomadik gpio"
40  */
41
42 #define NMK_GPIO_PER_CHIP       32
43
44 struct nmk_gpio_chip {
45         struct gpio_chip chip;
46         void __iomem *addr;
47         struct clk *clk;
48         unsigned int bank;
49         unsigned int parent_irq;
50         int secondary_parent_irq;
51         u32 (*get_secondary_status)(unsigned int bank);
52         void (*set_ioforce)(bool enable);
53         spinlock_t lock;
54         bool sleepmode;
55         /* Keep track of configured edges */
56         u32 edge_rising;
57         u32 edge_falling;
58         u32 real_wake;
59         u32 rwimsc;
60         u32 fwimsc;
61         u32 rimsc;
62         u32 fimsc;
63         u32 pull_up;
64 };
65
66 static struct nmk_gpio_chip *
67 nmk_gpio_chips[DIV_ROUND_UP(ARCH_NR_GPIOS, NMK_GPIO_PER_CHIP)];
68
69 static DEFINE_SPINLOCK(nmk_gpio_slpm_lock);
70
71 #define NUM_BANKS ARRAY_SIZE(nmk_gpio_chips)
72
73 static void __nmk_gpio_set_mode(struct nmk_gpio_chip *nmk_chip,
74                                 unsigned offset, int gpio_mode)
75 {
76         u32 bit = 1 << offset;
77         u32 afunc, bfunc;
78
79         afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & ~bit;
80         bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & ~bit;
81         if (gpio_mode & NMK_GPIO_ALT_A)
82                 afunc |= bit;
83         if (gpio_mode & NMK_GPIO_ALT_B)
84                 bfunc |= bit;
85         writel(afunc, nmk_chip->addr + NMK_GPIO_AFSLA);
86         writel(bfunc, nmk_chip->addr + NMK_GPIO_AFSLB);
87 }
88
89 static void __nmk_gpio_set_slpm(struct nmk_gpio_chip *nmk_chip,
90                                 unsigned offset, enum nmk_gpio_slpm mode)
91 {
92         u32 bit = 1 << offset;
93         u32 slpm;
94
95         slpm = readl(nmk_chip->addr + NMK_GPIO_SLPC);
96         if (mode == NMK_GPIO_SLPM_NOCHANGE)
97                 slpm |= bit;
98         else
99                 slpm &= ~bit;
100         writel(slpm, nmk_chip->addr + NMK_GPIO_SLPC);
101 }
102
103 static void __nmk_gpio_set_pull(struct nmk_gpio_chip *nmk_chip,
104                                 unsigned offset, enum nmk_gpio_pull pull)
105 {
106         u32 bit = 1 << offset;
107         u32 pdis;
108
109         pdis = readl(nmk_chip->addr + NMK_GPIO_PDIS);
110         if (pull == NMK_GPIO_PULL_NONE) {
111                 pdis |= bit;
112                 nmk_chip->pull_up &= ~bit;
113         } else {
114                 pdis &= ~bit;
115         }
116
117         writel(pdis, nmk_chip->addr + NMK_GPIO_PDIS);
118
119         if (pull == NMK_GPIO_PULL_UP) {
120                 nmk_chip->pull_up |= bit;
121                 writel(bit, nmk_chip->addr + NMK_GPIO_DATS);
122         } else if (pull == NMK_GPIO_PULL_DOWN) {
123                 nmk_chip->pull_up &= ~bit;
124                 writel(bit, nmk_chip->addr + NMK_GPIO_DATC);
125         }
126 }
127
128 static void __nmk_gpio_make_input(struct nmk_gpio_chip *nmk_chip,
129                                   unsigned offset)
130 {
131         writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
132 }
133
134 static void __nmk_gpio_set_output(struct nmk_gpio_chip *nmk_chip,
135                                   unsigned offset, int val)
136 {
137         if (val)
138                 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATS);
139         else
140                 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATC);
141 }
142
143 static void __nmk_gpio_make_output(struct nmk_gpio_chip *nmk_chip,
144                                   unsigned offset, int val)
145 {
146         writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRS);
147         __nmk_gpio_set_output(nmk_chip, offset, val);
148 }
149
150 static void __nmk_gpio_set_mode_safe(struct nmk_gpio_chip *nmk_chip,
151                                      unsigned offset, int gpio_mode,
152                                      bool glitch)
153 {
154         u32 rwimsc = nmk_chip->rwimsc;
155         u32 fwimsc = nmk_chip->fwimsc;
156
157         if (glitch && nmk_chip->set_ioforce) {
158                 u32 bit = BIT(offset);
159
160                 /* Prevent spurious wakeups */
161                 writel(rwimsc & ~bit, nmk_chip->addr + NMK_GPIO_RWIMSC);
162                 writel(fwimsc & ~bit, nmk_chip->addr + NMK_GPIO_FWIMSC);
163
164                 nmk_chip->set_ioforce(true);
165         }
166
167         __nmk_gpio_set_mode(nmk_chip, offset, gpio_mode);
168
169         if (glitch && nmk_chip->set_ioforce) {
170                 nmk_chip->set_ioforce(false);
171
172                 writel(rwimsc, nmk_chip->addr + NMK_GPIO_RWIMSC);
173                 writel(fwimsc, nmk_chip->addr + NMK_GPIO_FWIMSC);
174         }
175 }
176
177 static void __nmk_config_pin(struct nmk_gpio_chip *nmk_chip, unsigned offset,
178                              pin_cfg_t cfg, bool sleep, unsigned int *slpmregs)
179 {
180         static const char *afnames[] = {
181                 [NMK_GPIO_ALT_GPIO]     = "GPIO",
182                 [NMK_GPIO_ALT_A]        = "A",
183                 [NMK_GPIO_ALT_B]        = "B",
184                 [NMK_GPIO_ALT_C]        = "C"
185         };
186         static const char *pullnames[] = {
187                 [NMK_GPIO_PULL_NONE]    = "none",
188                 [NMK_GPIO_PULL_UP]      = "up",
189                 [NMK_GPIO_PULL_DOWN]    = "down",
190                 [3] /* illegal */       = "??"
191         };
192         static const char *slpmnames[] = {
193                 [NMK_GPIO_SLPM_INPUT]           = "input/wakeup",
194                 [NMK_GPIO_SLPM_NOCHANGE]        = "no-change/no-wakeup",
195         };
196
197         int pin = PIN_NUM(cfg);
198         int pull = PIN_PULL(cfg);
199         int af = PIN_ALT(cfg);
200         int slpm = PIN_SLPM(cfg);
201         int output = PIN_DIR(cfg);
202         int val = PIN_VAL(cfg);
203         bool glitch = af == NMK_GPIO_ALT_C;
204
205         dev_dbg(nmk_chip->chip.dev, "pin %d [%#lx]: af %s, pull %s, slpm %s (%s%s)\n",
206                 pin, cfg, afnames[af], pullnames[pull], slpmnames[slpm],
207                 output ? "output " : "input",
208                 output ? (val ? "high" : "low") : "");
209
210         if (sleep) {
211                 int slpm_pull = PIN_SLPM_PULL(cfg);
212                 int slpm_output = PIN_SLPM_DIR(cfg);
213                 int slpm_val = PIN_SLPM_VAL(cfg);
214
215                 af = NMK_GPIO_ALT_GPIO;
216
217                 /*
218                  * The SLPM_* values are normal values + 1 to allow zero to
219                  * mean "same as normal".
220                  */
221                 if (slpm_pull)
222                         pull = slpm_pull - 1;
223                 if (slpm_output)
224                         output = slpm_output - 1;
225                 if (slpm_val)
226                         val = slpm_val - 1;
227
228                 dev_dbg(nmk_chip->chip.dev, "pin %d: sleep pull %s, dir %s, val %s\n",
229                         pin,
230                         slpm_pull ? pullnames[pull] : "same",
231                         slpm_output ? (output ? "output" : "input") : "same",
232                         slpm_val ? (val ? "high" : "low") : "same");
233         }
234
235         if (output)
236                 __nmk_gpio_make_output(nmk_chip, offset, val);
237         else {
238                 __nmk_gpio_make_input(nmk_chip, offset);
239                 __nmk_gpio_set_pull(nmk_chip, offset, pull);
240         }
241
242         /*
243          * If we've backed up the SLPM registers (glitch workaround), modify
244          * the backups since they will be restored.
245          */
246         if (slpmregs) {
247                 if (slpm == NMK_GPIO_SLPM_NOCHANGE)
248                         slpmregs[nmk_chip->bank] |= BIT(offset);
249                 else
250                         slpmregs[nmk_chip->bank] &= ~BIT(offset);
251         } else
252                 __nmk_gpio_set_slpm(nmk_chip, offset, slpm);
253
254         __nmk_gpio_set_mode_safe(nmk_chip, offset, af, glitch);
255 }
256
257 /*
258  * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
259  *  - Save SLPM registers
260  *  - Set SLPM=0 for the IOs you want to switch and others to 1
261  *  - Configure the GPIO registers for the IOs that are being switched
262  *  - Set IOFORCE=1
263  *  - Modify the AFLSA/B registers for the IOs that are being switched
264  *  - Set IOFORCE=0
265  *  - Restore SLPM registers
266  *  - Any spurious wake up event during switch sequence to be ignored and
267  *    cleared
268  */
269 static void nmk_gpio_glitch_slpm_init(unsigned int *slpm)
270 {
271         int i;
272
273         for (i = 0; i < NUM_BANKS; i++) {
274                 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
275                 unsigned int temp = slpm[i];
276
277                 if (!chip)
278                         break;
279
280                 clk_enable(chip->clk);
281
282                 slpm[i] = readl(chip->addr + NMK_GPIO_SLPC);
283                 writel(temp, chip->addr + NMK_GPIO_SLPC);
284         }
285 }
286
287 static void nmk_gpio_glitch_slpm_restore(unsigned int *slpm)
288 {
289         int i;
290
291         for (i = 0; i < NUM_BANKS; i++) {
292                 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
293
294                 if (!chip)
295                         break;
296
297                 writel(slpm[i], chip->addr + NMK_GPIO_SLPC);
298
299                 clk_disable(chip->clk);
300         }
301 }
302
303 static int __nmk_config_pins(pin_cfg_t *cfgs, int num, bool sleep)
304 {
305         static unsigned int slpm[NUM_BANKS];
306         unsigned long flags;
307         bool glitch = false;
308         int ret = 0;
309         int i;
310
311         for (i = 0; i < num; i++) {
312                 if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C) {
313                         glitch = true;
314                         break;
315                 }
316         }
317
318         spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
319
320         if (glitch) {
321                 memset(slpm, 0xff, sizeof(slpm));
322
323                 for (i = 0; i < num; i++) {
324                         int pin = PIN_NUM(cfgs[i]);
325                         int offset = pin % NMK_GPIO_PER_CHIP;
326
327                         if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C)
328                                 slpm[pin / NMK_GPIO_PER_CHIP] &= ~BIT(offset);
329                 }
330
331                 nmk_gpio_glitch_slpm_init(slpm);
332         }
333
334         for (i = 0; i < num; i++) {
335                 struct nmk_gpio_chip *nmk_chip;
336                 int pin = PIN_NUM(cfgs[i]);
337
338                 nmk_chip = irq_get_chip_data(NOMADIK_GPIO_TO_IRQ(pin));
339                 if (!nmk_chip) {
340                         ret = -EINVAL;
341                         break;
342                 }
343
344                 clk_enable(nmk_chip->clk);
345                 spin_lock(&nmk_chip->lock);
346                 __nmk_config_pin(nmk_chip, pin - nmk_chip->chip.base,
347                                  cfgs[i], sleep, glitch ? slpm : NULL);
348                 spin_unlock(&nmk_chip->lock);
349                 clk_disable(nmk_chip->clk);
350         }
351
352         if (glitch)
353                 nmk_gpio_glitch_slpm_restore(slpm);
354
355         spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
356
357         return ret;
358 }
359
360 /**
361  * nmk_config_pin - configure a pin's mux attributes
362  * @cfg: pin confguration
363  *
364  * Configures a pin's mode (alternate function or GPIO), its pull up status,
365  * and its sleep mode based on the specified configuration.  The @cfg is
366  * usually one of the SoC specific macros defined in mach/<soc>-pins.h.  These
367  * are constructed using, and can be further enhanced with, the macros in
368  * plat/pincfg.h.
369  *
370  * If a pin's mode is set to GPIO, it is configured as an input to avoid
371  * side-effects.  The gpio can be manipulated later using standard GPIO API
372  * calls.
373  */
374 int nmk_config_pin(pin_cfg_t cfg, bool sleep)
375 {
376         return __nmk_config_pins(&cfg, 1, sleep);
377 }
378 EXPORT_SYMBOL(nmk_config_pin);
379
380 /**
381  * nmk_config_pins - configure several pins at once
382  * @cfgs: array of pin configurations
383  * @num: number of elments in the array
384  *
385  * Configures several pins using nmk_config_pin().  Refer to that function for
386  * further information.
387  */
388 int nmk_config_pins(pin_cfg_t *cfgs, int num)
389 {
390         return __nmk_config_pins(cfgs, num, false);
391 }
392 EXPORT_SYMBOL(nmk_config_pins);
393
394 int nmk_config_pins_sleep(pin_cfg_t *cfgs, int num)
395 {
396         return __nmk_config_pins(cfgs, num, true);
397 }
398 EXPORT_SYMBOL(nmk_config_pins_sleep);
399
400 /**
401  * nmk_gpio_set_slpm() - configure the sleep mode of a pin
402  * @gpio: pin number
403  * @mode: NMK_GPIO_SLPM_INPUT or NMK_GPIO_SLPM_NOCHANGE,
404  *
405  * This register is actually in the pinmux layer, not the GPIO block itself.
406  * The GPIO1B_SLPM register defines the GPIO mode when SLEEP/DEEP-SLEEP
407  * mode is entered (i.e. when signal IOFORCE is HIGH by the platform code).
408  * Each GPIO can be configured to be forced into GPIO mode when IOFORCE is
409  * HIGH, overriding the normal setting defined by GPIO_AFSELx registers.
410  * When IOFORCE returns LOW (by software, after SLEEP/DEEP-SLEEP exit),
411  * the GPIOs return to the normal setting defined by GPIO_AFSELx registers.
412  *
413  * If @mode is NMK_GPIO_SLPM_INPUT, the corresponding GPIO is switched to GPIO
414  * mode when signal IOFORCE is HIGH (i.e. when SLEEP/DEEP-SLEEP mode is
415  * entered) regardless of the altfunction selected. Also wake-up detection is
416  * ENABLED.
417  *
418  * If @mode is NMK_GPIO_SLPM_NOCHANGE, the corresponding GPIO remains
419  * controlled by NMK_GPIO_DATC, NMK_GPIO_DATS, NMK_GPIO_DIR, NMK_GPIO_PDIS
420  * (for altfunction GPIO) or respective on-chip peripherals (for other
421  * altfuncs) when IOFORCE is HIGH. Also wake-up detection DISABLED.
422  *
423  * Note that enable_irq_wake() will automatically enable wakeup detection.
424  */
425 int nmk_gpio_set_slpm(int gpio, enum nmk_gpio_slpm mode)
426 {
427         struct nmk_gpio_chip *nmk_chip;
428         unsigned long flags;
429
430         nmk_chip = irq_get_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
431         if (!nmk_chip)
432                 return -EINVAL;
433
434         clk_enable(nmk_chip->clk);
435         spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
436         spin_lock(&nmk_chip->lock);
437
438         __nmk_gpio_set_slpm(nmk_chip, gpio - nmk_chip->chip.base, mode);
439
440         spin_unlock(&nmk_chip->lock);
441         spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
442         clk_disable(nmk_chip->clk);
443
444         return 0;
445 }
446
447 /**
448  * nmk_gpio_set_pull() - enable/disable pull up/down on a gpio
449  * @gpio: pin number
450  * @pull: one of NMK_GPIO_PULL_DOWN, NMK_GPIO_PULL_UP, and NMK_GPIO_PULL_NONE
451  *
452  * Enables/disables pull up/down on a specified pin.  This only takes effect if
453  * the pin is configured as an input (either explicitly or by the alternate
454  * function).
455  *
456  * NOTE: If enabling the pull up/down, the caller must ensure that the GPIO is
457  * configured as an input.  Otherwise, due to the way the controller registers
458  * work, this function will change the value output on the pin.
459  */
460 int nmk_gpio_set_pull(int gpio, enum nmk_gpio_pull pull)
461 {
462         struct nmk_gpio_chip *nmk_chip;
463         unsigned long flags;
464
465         nmk_chip = irq_get_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
466         if (!nmk_chip)
467                 return -EINVAL;
468
469         clk_enable(nmk_chip->clk);
470         spin_lock_irqsave(&nmk_chip->lock, flags);
471         __nmk_gpio_set_pull(nmk_chip, gpio - nmk_chip->chip.base, pull);
472         spin_unlock_irqrestore(&nmk_chip->lock, flags);
473         clk_disable(nmk_chip->clk);
474
475         return 0;
476 }
477
478 /* Mode functions */
479 /**
480  * nmk_gpio_set_mode() - set the mux mode of a gpio pin
481  * @gpio: pin number
482  * @gpio_mode: one of NMK_GPIO_ALT_GPIO, NMK_GPIO_ALT_A,
483  *             NMK_GPIO_ALT_B, and NMK_GPIO_ALT_C
484  *
485  * Sets the mode of the specified pin to one of the alternate functions or
486  * plain GPIO.
487  */
488 int nmk_gpio_set_mode(int gpio, int gpio_mode)
489 {
490         struct nmk_gpio_chip *nmk_chip;
491         unsigned long flags;
492
493         nmk_chip = irq_get_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
494         if (!nmk_chip)
495                 return -EINVAL;
496
497         clk_enable(nmk_chip->clk);
498         spin_lock_irqsave(&nmk_chip->lock, flags);
499         __nmk_gpio_set_mode(nmk_chip, gpio - nmk_chip->chip.base, gpio_mode);
500         spin_unlock_irqrestore(&nmk_chip->lock, flags);
501         clk_disable(nmk_chip->clk);
502
503         return 0;
504 }
505 EXPORT_SYMBOL(nmk_gpio_set_mode);
506
507 int nmk_gpio_get_mode(int gpio)
508 {
509         struct nmk_gpio_chip *nmk_chip;
510         u32 afunc, bfunc, bit;
511
512         nmk_chip = irq_get_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
513         if (!nmk_chip)
514                 return -EINVAL;
515
516         bit = 1 << (gpio - nmk_chip->chip.base);
517
518         clk_enable(nmk_chip->clk);
519
520         afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & bit;
521         bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & bit;
522
523         clk_disable(nmk_chip->clk);
524
525         return (afunc ? NMK_GPIO_ALT_A : 0) | (bfunc ? NMK_GPIO_ALT_B : 0);
526 }
527 EXPORT_SYMBOL(nmk_gpio_get_mode);
528
529
530 /* IRQ functions */
531 static inline int nmk_gpio_get_bitmask(int gpio)
532 {
533         return 1 << (gpio % 32);
534 }
535
536 static void nmk_gpio_irq_ack(struct irq_data *d)
537 {
538         int gpio;
539         struct nmk_gpio_chip *nmk_chip;
540
541         gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
542         nmk_chip = irq_data_get_irq_chip_data(d);
543         if (!nmk_chip)
544                 return;
545
546         clk_enable(nmk_chip->clk);
547         writel(nmk_gpio_get_bitmask(gpio), nmk_chip->addr + NMK_GPIO_IC);
548         clk_disable(nmk_chip->clk);
549 }
550
551 enum nmk_gpio_irq_type {
552         NORMAL,
553         WAKE,
554 };
555
556 static void __nmk_gpio_irq_modify(struct nmk_gpio_chip *nmk_chip,
557                                   int gpio, enum nmk_gpio_irq_type which,
558                                   bool enable)
559 {
560         u32 bitmask = nmk_gpio_get_bitmask(gpio);
561         u32 *rimscval;
562         u32 *fimscval;
563         u32 rimscreg;
564         u32 fimscreg;
565
566         if (which == NORMAL) {
567                 rimscreg = NMK_GPIO_RIMSC;
568                 fimscreg = NMK_GPIO_FIMSC;
569                 rimscval = &nmk_chip->rimsc;
570                 fimscval = &nmk_chip->fimsc;
571         } else  {
572                 rimscreg = NMK_GPIO_RWIMSC;
573                 fimscreg = NMK_GPIO_FWIMSC;
574                 rimscval = &nmk_chip->rwimsc;
575                 fimscval = &nmk_chip->fwimsc;
576         }
577
578         /* we must individually set/clear the two edges */
579         if (nmk_chip->edge_rising & bitmask) {
580                 if (enable)
581                         *rimscval |= bitmask;
582                 else
583                         *rimscval &= ~bitmask;
584                 writel(*rimscval, nmk_chip->addr + rimscreg);
585         }
586         if (nmk_chip->edge_falling & bitmask) {
587                 if (enable)
588                         *fimscval |= bitmask;
589                 else
590                         *fimscval &= ~bitmask;
591                 writel(*fimscval, nmk_chip->addr + fimscreg);
592         }
593 }
594
595 static void __nmk_gpio_set_wake(struct nmk_gpio_chip *nmk_chip,
596                                 int gpio, bool on)
597 {
598         /*
599          * Ensure WAKEUP_ENABLE is on.  No need to disable it if wakeup is
600          * disabled, since setting SLPM to 1 increases power consumption, and
601          * wakeup is anyhow controlled by the RIMSC and FIMSC registers.
602          */
603         if (nmk_chip->sleepmode && on) {
604                 __nmk_gpio_set_slpm(nmk_chip, gpio - nmk_chip->chip.base,
605                                     NMK_GPIO_SLPM_WAKEUP_ENABLE);
606         }
607
608         __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, on);
609 }
610
611 static int nmk_gpio_irq_maskunmask(struct irq_data *d, bool enable)
612 {
613         int gpio;
614         struct nmk_gpio_chip *nmk_chip;
615         unsigned long flags;
616         u32 bitmask;
617
618         gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
619         nmk_chip = irq_data_get_irq_chip_data(d);
620         bitmask = nmk_gpio_get_bitmask(gpio);
621         if (!nmk_chip)
622                 return -EINVAL;
623
624         clk_enable(nmk_chip->clk);
625         spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
626         spin_lock(&nmk_chip->lock);
627
628         __nmk_gpio_irq_modify(nmk_chip, gpio, NORMAL, enable);
629
630         if (!(nmk_chip->real_wake & bitmask))
631                 __nmk_gpio_set_wake(nmk_chip, gpio, enable);
632
633         spin_unlock(&nmk_chip->lock);
634         spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
635         clk_disable(nmk_chip->clk);
636
637         return 0;
638 }
639
640 static void nmk_gpio_irq_mask(struct irq_data *d)
641 {
642         nmk_gpio_irq_maskunmask(d, false);
643 }
644
645 static void nmk_gpio_irq_unmask(struct irq_data *d)
646 {
647         nmk_gpio_irq_maskunmask(d, true);
648 }
649
650 static int nmk_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
651 {
652         struct nmk_gpio_chip *nmk_chip;
653         unsigned long flags;
654         u32 bitmask;
655         int gpio;
656
657         gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
658         nmk_chip = irq_data_get_irq_chip_data(d);
659         if (!nmk_chip)
660                 return -EINVAL;
661         bitmask = nmk_gpio_get_bitmask(gpio);
662
663         clk_enable(nmk_chip->clk);
664         spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
665         spin_lock(&nmk_chip->lock);
666
667         if (irqd_irq_disabled(d))
668                 __nmk_gpio_set_wake(nmk_chip, gpio, on);
669
670         if (on)
671                 nmk_chip->real_wake |= bitmask;
672         else
673                 nmk_chip->real_wake &= ~bitmask;
674
675         spin_unlock(&nmk_chip->lock);
676         spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
677         clk_disable(nmk_chip->clk);
678
679         return 0;
680 }
681
682 static int nmk_gpio_irq_set_type(struct irq_data *d, unsigned int type)
683 {
684         bool enabled = !irqd_irq_disabled(d);
685         bool wake = irqd_is_wakeup_set(d);
686         int gpio;
687         struct nmk_gpio_chip *nmk_chip;
688         unsigned long flags;
689         u32 bitmask;
690
691         gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
692         nmk_chip = irq_data_get_irq_chip_data(d);
693         bitmask = nmk_gpio_get_bitmask(gpio);
694         if (!nmk_chip)
695                 return -EINVAL;
696
697         if (type & IRQ_TYPE_LEVEL_HIGH)
698                 return -EINVAL;
699         if (type & IRQ_TYPE_LEVEL_LOW)
700                 return -EINVAL;
701
702         clk_enable(nmk_chip->clk);
703         spin_lock_irqsave(&nmk_chip->lock, flags);
704
705         if (enabled)
706                 __nmk_gpio_irq_modify(nmk_chip, gpio, NORMAL, false);
707
708         if (enabled || wake)
709                 __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, false);
710
711         nmk_chip->edge_rising &= ~bitmask;
712         if (type & IRQ_TYPE_EDGE_RISING)
713                 nmk_chip->edge_rising |= bitmask;
714
715         nmk_chip->edge_falling &= ~bitmask;
716         if (type & IRQ_TYPE_EDGE_FALLING)
717                 nmk_chip->edge_falling |= bitmask;
718
719         if (enabled)
720                 __nmk_gpio_irq_modify(nmk_chip, gpio, NORMAL, true);
721
722         if (enabled || wake)
723                 __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, true);
724
725         spin_unlock_irqrestore(&nmk_chip->lock, flags);
726         clk_disable(nmk_chip->clk);
727
728         return 0;
729 }
730
731 static unsigned int nmk_gpio_irq_startup(struct irq_data *d)
732 {
733         struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
734
735         clk_enable(nmk_chip->clk);
736         nmk_gpio_irq_unmask(d);
737         return 0;
738 }
739
740 static void nmk_gpio_irq_shutdown(struct irq_data *d)
741 {
742         struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
743
744         nmk_gpio_irq_mask(d);
745         clk_disable(nmk_chip->clk);
746 }
747
748 static struct irq_chip nmk_gpio_irq_chip = {
749         .name           = "Nomadik-GPIO",
750         .irq_ack        = nmk_gpio_irq_ack,
751         .irq_mask       = nmk_gpio_irq_mask,
752         .irq_unmask     = nmk_gpio_irq_unmask,
753         .irq_set_type   = nmk_gpio_irq_set_type,
754         .irq_set_wake   = nmk_gpio_irq_set_wake,
755         .irq_startup    = nmk_gpio_irq_startup,
756         .irq_shutdown   = nmk_gpio_irq_shutdown,
757 };
758
759 static void __nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc,
760                                    u32 status)
761 {
762         struct nmk_gpio_chip *nmk_chip;
763         struct irq_chip *host_chip = irq_get_chip(irq);
764         unsigned int first_irq;
765
766         chained_irq_enter(host_chip, desc);
767
768         nmk_chip = irq_get_handler_data(irq);
769         first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base);
770         while (status) {
771                 int bit = __ffs(status);
772
773                 generic_handle_irq(first_irq + bit);
774                 status &= ~BIT(bit);
775         }
776
777         chained_irq_exit(host_chip, desc);
778 }
779
780 static void nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
781 {
782         struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
783         u32 status;
784
785         clk_enable(nmk_chip->clk);
786         status = readl(nmk_chip->addr + NMK_GPIO_IS);
787         clk_disable(nmk_chip->clk);
788
789         __nmk_gpio_irq_handler(irq, desc, status);
790 }
791
792 static void nmk_gpio_secondary_irq_handler(unsigned int irq,
793                                            struct irq_desc *desc)
794 {
795         struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
796         u32 status = nmk_chip->get_secondary_status(nmk_chip->bank);
797
798         __nmk_gpio_irq_handler(irq, desc, status);
799 }
800
801 static int nmk_gpio_init_irq(struct nmk_gpio_chip *nmk_chip)
802 {
803         unsigned int first_irq;
804         int i;
805
806         first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base);
807         for (i = first_irq; i < first_irq + nmk_chip->chip.ngpio; i++) {
808                 irq_set_chip_and_handler(i, &nmk_gpio_irq_chip,
809                                          handle_edge_irq);
810                 set_irq_flags(i, IRQF_VALID);
811                 irq_set_chip_data(i, nmk_chip);
812                 irq_set_irq_type(i, IRQ_TYPE_EDGE_FALLING);
813         }
814
815         irq_set_chained_handler(nmk_chip->parent_irq, nmk_gpio_irq_handler);
816         irq_set_handler_data(nmk_chip->parent_irq, nmk_chip);
817
818         if (nmk_chip->secondary_parent_irq >= 0) {
819                 irq_set_chained_handler(nmk_chip->secondary_parent_irq,
820                                         nmk_gpio_secondary_irq_handler);
821                 irq_set_handler_data(nmk_chip->secondary_parent_irq, nmk_chip);
822         }
823
824         return 0;
825 }
826
827 /* I/O Functions */
828 static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned offset)
829 {
830         struct nmk_gpio_chip *nmk_chip =
831                 container_of(chip, struct nmk_gpio_chip, chip);
832
833         clk_enable(nmk_chip->clk);
834
835         writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
836
837         clk_disable(nmk_chip->clk);
838
839         return 0;
840 }
841
842 static int nmk_gpio_get_input(struct gpio_chip *chip, unsigned offset)
843 {
844         struct nmk_gpio_chip *nmk_chip =
845                 container_of(chip, struct nmk_gpio_chip, chip);
846         u32 bit = 1 << offset;
847         int value;
848
849         clk_enable(nmk_chip->clk);
850
851         value = (readl(nmk_chip->addr + NMK_GPIO_DAT) & bit) != 0;
852
853         clk_disable(nmk_chip->clk);
854
855         return value;
856 }
857
858 static void nmk_gpio_set_output(struct gpio_chip *chip, unsigned offset,
859                                 int val)
860 {
861         struct nmk_gpio_chip *nmk_chip =
862                 container_of(chip, struct nmk_gpio_chip, chip);
863
864         clk_enable(nmk_chip->clk);
865
866         __nmk_gpio_set_output(nmk_chip, offset, val);
867
868         clk_disable(nmk_chip->clk);
869 }
870
871 static int nmk_gpio_make_output(struct gpio_chip *chip, unsigned offset,
872                                 int val)
873 {
874         struct nmk_gpio_chip *nmk_chip =
875                 container_of(chip, struct nmk_gpio_chip, chip);
876
877         clk_enable(nmk_chip->clk);
878
879         __nmk_gpio_make_output(nmk_chip, offset, val);
880
881         clk_disable(nmk_chip->clk);
882
883         return 0;
884 }
885
886 static int nmk_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
887 {
888         struct nmk_gpio_chip *nmk_chip =
889                 container_of(chip, struct nmk_gpio_chip, chip);
890
891         return NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base) + offset;
892 }
893
894 #ifdef CONFIG_DEBUG_FS
895
896 #include <linux/seq_file.h>
897
898 static void nmk_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
899 {
900         int mode;
901         unsigned                i;
902         unsigned                gpio = chip->base;
903         int                     is_out;
904         struct nmk_gpio_chip *nmk_chip =
905                 container_of(chip, struct nmk_gpio_chip, chip);
906         const char *modes[] = {
907                 [NMK_GPIO_ALT_GPIO]     = "gpio",
908                 [NMK_GPIO_ALT_A]        = "altA",
909                 [NMK_GPIO_ALT_B]        = "altB",
910                 [NMK_GPIO_ALT_C]        = "altC",
911         };
912
913         clk_enable(nmk_chip->clk);
914
915         for (i = 0; i < chip->ngpio; i++, gpio++) {
916                 const char *label = gpiochip_is_requested(chip, i);
917                 bool pull;
918                 u32 bit = 1 << i;
919
920                 is_out = readl(nmk_chip->addr + NMK_GPIO_DIR) & bit;
921                 pull = !(readl(nmk_chip->addr + NMK_GPIO_PDIS) & bit);
922                 mode = nmk_gpio_get_mode(gpio);
923                 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s %s",
924                         gpio, label ?: "(none)",
925                         is_out ? "out" : "in ",
926                         chip->get
927                                 ? (chip->get(chip, i) ? "hi" : "lo")
928                                 : "?  ",
929                         (mode < 0) ? "unknown" : modes[mode],
930                         pull ? "pull" : "none");
931
932                 if (label && !is_out) {
933                         int             irq = gpio_to_irq(gpio);
934                         struct irq_desc *desc = irq_to_desc(irq);
935
936                         /* This races with request_irq(), set_irq_type(),
937                          * and set_irq_wake() ... but those are "rare".
938                          */
939                         if (irq >= 0 && desc->action) {
940                                 char *trigger;
941                                 u32 bitmask = nmk_gpio_get_bitmask(gpio);
942
943                                 if (nmk_chip->edge_rising & bitmask)
944                                         trigger = "edge-rising";
945                                 else if (nmk_chip->edge_falling & bitmask)
946                                         trigger = "edge-falling";
947                                 else
948                                         trigger = "edge-undefined";
949
950                                 seq_printf(s, " irq-%d %s%s",
951                                         irq, trigger,
952                                         irqd_is_wakeup_set(&desc->irq_data)
953                                                 ? " wakeup" : "");
954                         }
955                 }
956
957                 seq_printf(s, "\n");
958         }
959
960         clk_disable(nmk_chip->clk);
961 }
962
963 #else
964 #define nmk_gpio_dbg_show       NULL
965 #endif
966
967 /* This structure is replicated for each GPIO block allocated at probe time */
968 static struct gpio_chip nmk_gpio_template = {
969         .direction_input        = nmk_gpio_make_input,
970         .get                    = nmk_gpio_get_input,
971         .direction_output       = nmk_gpio_make_output,
972         .set                    = nmk_gpio_set_output,
973         .to_irq                 = nmk_gpio_to_irq,
974         .dbg_show               = nmk_gpio_dbg_show,
975         .can_sleep              = 0,
976 };
977
978 void nmk_gpio_clocks_enable(void)
979 {
980         int i;
981
982         for (i = 0; i < NUM_BANKS; i++) {
983                 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
984
985                 if (!chip)
986                         continue;
987
988                 clk_enable(chip->clk);
989         }
990 }
991
992 void nmk_gpio_clocks_disable(void)
993 {
994         int i;
995
996         for (i = 0; i < NUM_BANKS; i++) {
997                 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
998
999                 if (!chip)
1000                         continue;
1001
1002                 clk_disable(chip->clk);
1003         }
1004 }
1005
1006 /*
1007  * Called from the suspend/resume path to only keep the real wakeup interrupts
1008  * (those that have had set_irq_wake() called on them) as wakeup interrupts,
1009  * and not the rest of the interrupts which we needed to have as wakeups for
1010  * cpuidle.
1011  *
1012  * PM ops are not used since this needs to be done at the end, after all the
1013  * other drivers are done with their suspend callbacks.
1014  */
1015 void nmk_gpio_wakeups_suspend(void)
1016 {
1017         int i;
1018
1019         for (i = 0; i < NUM_BANKS; i++) {
1020                 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1021
1022                 if (!chip)
1023                         break;
1024
1025                 clk_enable(chip->clk);
1026
1027                 writel(chip->rwimsc & chip->real_wake,
1028                        chip->addr + NMK_GPIO_RWIMSC);
1029                 writel(chip->fwimsc & chip->real_wake,
1030                        chip->addr + NMK_GPIO_FWIMSC);
1031
1032                 clk_disable(chip->clk);
1033         }
1034 }
1035
1036 void nmk_gpio_wakeups_resume(void)
1037 {
1038         int i;
1039
1040         for (i = 0; i < NUM_BANKS; i++) {
1041                 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1042
1043                 if (!chip)
1044                         break;
1045
1046                 clk_enable(chip->clk);
1047
1048                 writel(chip->rwimsc, chip->addr + NMK_GPIO_RWIMSC);
1049                 writel(chip->fwimsc, chip->addr + NMK_GPIO_FWIMSC);
1050
1051                 clk_disable(chip->clk);
1052         }
1053 }
1054
1055 /*
1056  * Read the pull up/pull down status.
1057  * A bit set in 'pull_up' means that pull up
1058  * is selected if pull is enabled in PDIS register.
1059  * Note: only pull up/down set via this driver can
1060  * be detected due to HW limitations.
1061  */
1062 void nmk_gpio_read_pull(int gpio_bank, u32 *pull_up)
1063 {
1064         if (gpio_bank < NUM_BANKS) {
1065                 struct nmk_gpio_chip *chip = nmk_gpio_chips[gpio_bank];
1066
1067                 if (!chip)
1068                         return;
1069
1070                 *pull_up = chip->pull_up;
1071         }
1072 }
1073
1074 static int __devinit nmk_gpio_probe(struct platform_device *dev)
1075 {
1076         struct nmk_gpio_platform_data *pdata = dev->dev.platform_data;
1077         struct nmk_gpio_chip *nmk_chip;
1078         struct gpio_chip *chip;
1079         struct resource *res;
1080         struct clk *clk;
1081         int secondary_irq;
1082         int irq;
1083         int ret;
1084
1085         if (!pdata)
1086                 return -ENODEV;
1087
1088         res = platform_get_resource(dev, IORESOURCE_MEM, 0);
1089         if (!res) {
1090                 ret = -ENOENT;
1091                 goto out;
1092         }
1093
1094         irq = platform_get_irq(dev, 0);
1095         if (irq < 0) {
1096                 ret = irq;
1097                 goto out;
1098         }
1099
1100         secondary_irq = platform_get_irq(dev, 1);
1101         if (secondary_irq >= 0 && !pdata->get_secondary_status) {
1102                 ret = -EINVAL;
1103                 goto out;
1104         }
1105
1106         if (request_mem_region(res->start, resource_size(res),
1107                                dev_name(&dev->dev)) == NULL) {
1108                 ret = -EBUSY;
1109                 goto out;
1110         }
1111
1112         clk = clk_get(&dev->dev, NULL);
1113         if (IS_ERR(clk)) {
1114                 ret = PTR_ERR(clk);
1115                 goto out_release;
1116         }
1117
1118         nmk_chip = kzalloc(sizeof(*nmk_chip), GFP_KERNEL);
1119         if (!nmk_chip) {
1120                 ret = -ENOMEM;
1121                 goto out_clk;
1122         }
1123         /*
1124          * The virt address in nmk_chip->addr is in the nomadik register space,
1125          * so we can simply convert the resource address, without remapping
1126          */
1127         nmk_chip->bank = dev->id;
1128         nmk_chip->clk = clk;
1129         nmk_chip->addr = io_p2v(res->start);
1130         nmk_chip->chip = nmk_gpio_template;
1131         nmk_chip->parent_irq = irq;
1132         nmk_chip->secondary_parent_irq = secondary_irq;
1133         nmk_chip->get_secondary_status = pdata->get_secondary_status;
1134         nmk_chip->set_ioforce = pdata->set_ioforce;
1135         nmk_chip->sleepmode = pdata->supports_sleepmode;
1136         spin_lock_init(&nmk_chip->lock);
1137
1138         chip = &nmk_chip->chip;
1139         chip->base = pdata->first_gpio;
1140         chip->ngpio = pdata->num_gpio;
1141         chip->label = pdata->name ?: dev_name(&dev->dev);
1142         chip->dev = &dev->dev;
1143         chip->owner = THIS_MODULE;
1144
1145         ret = gpiochip_add(&nmk_chip->chip);
1146         if (ret)
1147                 goto out_free;
1148
1149         BUG_ON(nmk_chip->bank >= ARRAY_SIZE(nmk_gpio_chips));
1150
1151         nmk_gpio_chips[nmk_chip->bank] = nmk_chip;
1152         platform_set_drvdata(dev, nmk_chip);
1153
1154         nmk_gpio_init_irq(nmk_chip);
1155
1156         dev_info(&dev->dev, "at address %p\n",
1157                  nmk_chip->addr);
1158         return 0;
1159
1160 out_free:
1161         kfree(nmk_chip);
1162 out_clk:
1163         clk_disable(clk);
1164         clk_put(clk);
1165 out_release:
1166         release_mem_region(res->start, resource_size(res));
1167 out:
1168         dev_err(&dev->dev, "Failure %i for GPIO %i-%i\n", ret,
1169                   pdata->first_gpio, pdata->first_gpio+31);
1170         return ret;
1171 }
1172
1173 static struct platform_driver nmk_gpio_driver = {
1174         .driver = {
1175                 .owner = THIS_MODULE,
1176                 .name = "gpio",
1177         },
1178         .probe = nmk_gpio_probe,
1179 };
1180
1181 static int __init nmk_gpio_init(void)
1182 {
1183         return platform_driver_register(&nmk_gpio_driver);
1184 }
1185
1186 core_initcall(nmk_gpio_init);
1187
1188 MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini");
1189 MODULE_DESCRIPTION("Nomadik GPIO Driver");
1190 MODULE_LICENSE("GPL");