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