]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/i2c/chips/twl4030-gpio.c
twl4030-gpio: Remove default pullup enable/disable of GPIO
[linux-2.6-omap-h63xx.git] / drivers / i2c / chips / twl4030-gpio.c
1 /*
2  * linux/drivers/i2c/chips/twl4030_gpio.c
3  *
4  * Copyright (C) 2006-2007 Texas Instruments, Inc.
5  * Copyright (C) 2006 MontaVista Software, Inc.
6  *
7  * Code re-arranged and cleaned up by:
8  *      Syed Mohammed Khasim <x0khasim@ti.com>
9  *
10  * Initial Code:
11  *      Andy Lowe / Nishanth Menon
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
26  *
27  */
28
29 #include <linux/module.h>
30 #include <linux/kernel_stat.h>
31 #include <linux/init.h>
32 #include <linux/time.h>
33 #include <linux/interrupt.h>
34 #include <linux/random.h>
35 #include <linux/syscalls.h>
36 #include <linux/kthread.h>
37 #include <linux/irq.h>
38
39 #include <linux/i2c.h>
40 #include <linux/i2c/twl4030.h>
41 #include <linux/i2c/twl4030-gpio.h>
42 #include <linux/slab.h>
43
44 #include <mach/irqs.h>
45 #include <asm/mach/irq.h>
46 #include <mach/gpio.h>
47 #include <mach/mux.h>
48
49 #include <linux/device.h>
50
51 /* BitField Definitions */
52
53 /* Data banks : 3 banks for 8 gpios each */
54 #define DATA_BANK_MAX                           8
55 #define GET_GPIO_DATA_BANK(x)                   ((x)/DATA_BANK_MAX)
56 #define GET_GPIO_DATA_OFF(x)                    ((x)%DATA_BANK_MAX)
57
58 /* GPIODATADIR Fields each block 0-7 */
59 #define BIT_GPIODATADIR_GPIOxDIR(x)             (x)
60 #define MASK_GPIODATADIR_GPIOxDIR(x)            (0x01 << (x))
61
62 /* GPIODATAIN Fields each block 0-7 */
63 #define BIT_GPIODATAIN_GPIOxIN(x)               (x)
64 #define MASK_GPIODATAIN_GPIOxIN(x)              (0x01 << (x))
65
66 /* GPIODATAOUT Fields each block 0-7 */
67 #define BIT_GPIODATAOUT_GPIOxOUT(x)             (x)
68 #define MASK_GPIODATAOUT_GPIOxOUT(x)            (0x01 << (x))
69
70 /* CLEARGPIODATAOUT Fields */
71 #define BIT_CLEARGPIODATAOUT_GPIOxOUT(x)        (x)
72 #define MASK_CLEARGPIODATAOUT_GPIOxOUT(x)       (0x01 << (x))
73
74 /* SETGPIODATAOUT Fields */
75 #define BIT_SETGPIODATAOUT_GPIOxOUT(x)          (x)
76 #define MASK_SETGPIODATAOUT_GPIOxOUT(x)         (0x01 << (x))
77
78 /* GPIO_DEBEN Fields */
79 #define BIT_GPIO_DEBEN_GPIOxDEB(x)              (x)
80 #define MASK_GPIO_DEBEN_GPIOxDEB(x)             (0x01 << (x))
81
82 /* GPIO_ISR1A Fields */
83 #define BIT_GPIO_ISR_GPIOxISR(x)                (x)
84 #define MASK_GPIO_ISR_GPIOxISR(x)               (0x01 << (x))
85
86 /* GPIO_IMR1A Fields */
87 #define BIT_GPIO_IMR1A_GPIOxIMR(x)              (x)
88 #define MASK_GPIO_IMR1A_GPIOxIMR(x)             (0x01 << (x))
89
90 /* GPIO_SIR1 Fields */
91 #define BIT_GPIO_SIR1_GPIOxSIR(x)               (x)
92 #define MASK_GPIO_SIR1_GPIO0SIR                 (0x01 << (x))
93
94
95 /* Control banks : 5 banks for 4 gpios each */
96 #define DATA_CTL_MAX                    4
97 #define GET_GPIO_CTL_BANK(x)            ((x)/DATA_CTL_MAX)
98 #define GET_GPIO_CTL_OFF(x)             ((x)%DATA_CTL_MAX)
99 #define GPIO_BANK_MAX                   GET_GPIO_CTL_BANK(TWL4030_GPIO_MAX)
100
101 /* GPIOPUPDCTRx Fields 5 banks of 4 gpios each */
102 #define BIT_GPIOPUPDCTR1_GPIOxPD(x)     (2 * (x))
103 #define MASK_GPIOPUPDCTR1_GPIOxPD(x)    (0x01 << (2 * (x)))
104 #define BIT_GPIOPUPDCTR1_GPIOxPU(x)     ((x) + 1)
105 #define MASK_GPIOPUPDCTR1_GPIOxPU(x)    (0x01 << (((2 * (x)) + 1)))
106
107 /* GPIO_EDR1 Fields */
108 #define BIT_GPIO_EDR1_GPIOxFALLING(x)   (2 * (x))
109 #define MASK_GPIO_EDR1_GPIOxFALLING(x)  (0x01 << (2 * (x)))
110 #define BIT_GPIO_EDR1_GPIOxRISING(x)    ((x) + 1)
111 #define MASK_GPIO_EDR1_GPIOxRISING(x)   (0x01 << (((2 * (x)) + 1)))
112
113 /* GPIO_SIH_CTRL Fields */
114 #define BIT_GPIO_SIH_CTRL_EXCLEN        (0x000)
115 #define MASK_GPIO_SIH_CTRL_EXCLEN       (0x00000001)
116 #define BIT_GPIO_SIH_CTRL_PENDDIS       (0x001)
117 #define MASK_GPIO_SIH_CTRL_PENDDIS      (0x00000002)
118 #define BIT_GPIO_SIH_CTRL_COR           (0x002)
119 #define MASK_GPIO_SIH_CTRL_COR          (0x00000004)
120
121 /* GPIO_CTRL Fields */
122 #define BIT_GPIO_CTRL_GPIO0CD1          (0x000)
123 #define MASK_GPIO_CTRL_GPIO0CD1         (0x00000001)
124 #define BIT_GPIO_CTRL_GPIO1CD2          (0x001)
125 #define MASK_GPIO_CTRL_GPIO1CD2         (0x00000002)
126 #define BIT_GPIO_CTRL_GPIO_ON           (0x002)
127 #define MASK_GPIO_CTRL_GPIO_ON          (0x00000004)
128
129 /* Mask for GPIO registers when aggregated into a 32-bit integer */
130 #define GPIO_32_MASK                    0x0003ffff
131
132 /* Data structures */
133 static struct semaphore gpio_sem;
134
135 /* store usage of each GPIO. - each bit represents one GPIO */
136 static unsigned int gpio_usage_count;
137
138 /* shadow the imr register */
139 static unsigned int gpio_imr_shadow;
140
141 /* bitmask of pending requests to unmask gpio interrupts */
142 static unsigned int gpio_pending_unmask;
143
144 /* pointer to gpio unmask thread struct */
145 static struct task_struct *gpio_unmask_thread;
146
147 /*
148  * Helper functions to read and write the GPIO ISR and IMR registers as
149  * 32-bit integers. Functions return 0 on success, non-zero otherwise.
150  * The caller must hold a lock on gpio_sem.
151  */
152
153 static int gpio_read_isr(unsigned int *isr)
154 {
155         int ret;
156
157         *isr = 0;
158         ret = twl4030_i2c_read(TWL4030_MODULE_GPIO, (u8 *) isr,
159                         REG_GPIO_ISR1A, 3);
160         le32_to_cpup(isr);
161         *isr &= GPIO_32_MASK;
162
163         return ret;
164 }
165
166 static int gpio_write_isr(unsigned int isr)
167 {
168         isr &= GPIO_32_MASK;
169         /*
170          * The buffer passed to the twl4030_i2c_write() routine must have an
171          * extra byte at the beginning reserved for its internal use.
172          */
173         isr <<= 8;
174         isr = cpu_to_le32(isr);
175         return twl4030_i2c_write(TWL4030_MODULE_GPIO, (u8 *) &isr,
176                                 REG_GPIO_ISR1A, 3);
177 }
178
179 static int gpio_write_imr(unsigned int imr)
180 {
181         imr &= GPIO_32_MASK;
182         /*
183          * The buffer passed to the twl4030_i2c_write() routine must have an
184          * extra byte at the beginning reserved for its internal use.
185          */
186         imr <<= 8;
187         imr = cpu_to_le32(imr);
188         return twl4030_i2c_write(TWL4030_MODULE_GPIO, (u8 *) &imr,
189                                 REG_GPIO_IMR1A, 3);
190 }
191
192 /*
193  * These routines are analagous to the irqchip methods, but they are designed
194  * to be called from thread context with cpu interrupts enabled and with no
195  * locked spinlocks.  We call these routines from our custom IRQ handler
196  * instead of the usual irqchip methods.
197  */
198 static void twl4030_gpio_mask_and_ack(unsigned int irq)
199 {
200         int gpio = irq - TWL4030_GPIO_IRQ_BASE;
201
202         down(&gpio_sem);
203         /* mask */
204         gpio_imr_shadow |= (1 << gpio);
205         gpio_write_imr(gpio_imr_shadow);
206         /* ack */
207         gpio_write_isr(1 << gpio);
208         up(&gpio_sem);
209 }
210
211 static void twl4030_gpio_unmask(unsigned int irq)
212 {
213         int gpio = irq - TWL4030_GPIO_IRQ_BASE;
214
215         down(&gpio_sem);
216         gpio_imr_shadow &= ~(1 << gpio);
217         gpio_write_imr(gpio_imr_shadow);
218         up(&gpio_sem);
219 }
220
221 /*
222  * These are the irqchip methods for the TWL4030 GPIO interrupts.
223  * Our IRQ handle method doesn't call these, but they will be called by
224  * other routines such as setup_irq() and enable_irq().  They are called
225  * with cpu interrupts disabled and with a lock on the irq_controller_lock
226  * spinlock.  This complicates matters, because accessing the TWL4030 GPIO
227  * interrupt controller requires I2C bus transactions that can't be initiated
228  * in this context.  Our solution is to defer accessing the interrupt
229  * controller to a kernel thread.  We only need to support the unmask method.
230  */
231
232 static void twl4030_gpio_mask_and_ack_irqchip(unsigned int irq) {}
233 static void twl4030_gpio_mask_irqchip(unsigned int irq) {}
234
235 static void twl4030_gpio_unmask_irqchip(unsigned int irq)
236 {
237         int gpio = irq - TWL4030_GPIO_IRQ_BASE;
238
239         gpio_pending_unmask |= (1 << gpio);
240         if (gpio_unmask_thread && gpio_unmask_thread->state != TASK_RUNNING)
241                 wake_up_process(gpio_unmask_thread);
242 }
243
244 static struct irq_chip twl4030_gpio_irq_chip = {
245         .name   = "twl4030-gpio",
246         .ack    = twl4030_gpio_mask_and_ack_irqchip,
247         .mask   = twl4030_gpio_mask_irqchip,
248         .unmask = twl4030_gpio_unmask_irqchip,
249 };
250
251 /*
252  * These are the irqchip methods for the TWL4030 PIH GPIO module interrupt.
253  * The PIH module doesn't have interrupt masking capability, so these
254  * methods are NULL.
255  */
256 static void twl4030_gpio_module_ack(unsigned int irq) {}
257 static void twl4030_gpio_module_mask(unsigned int irq) {}
258 static void twl4030_gpio_module_unmask(unsigned int irq) {}
259 static struct irq_chip twl4030_gpio_module_irq_chip = {
260         .ack    = twl4030_gpio_module_ack,
261         .mask   = twl4030_gpio_module_mask,
262         .unmask = twl4030_gpio_module_unmask,
263 };
264
265 /*
266  * To configure TWL4030 GPIO module registers
267  */
268 static inline int gpio_twl4030_write(u8 address, u8 data)
269 {
270         int ret = 0;
271
272         ret = twl4030_i2c_write_u8(TWL4030_MODULE_GPIO, data, address);
273         return ret;
274 }
275
276 /*
277  * To read a TWL4030 GPIO module register
278  */
279 static inline int gpio_twl4030_read(u8 address)
280 {
281         u8 data;
282         int ret = 0;
283
284         ret = twl4030_i2c_read_u8(TWL4030_MODULE_GPIO, &data, address);
285         if (ret >= 0)
286                 ret = data;
287         return ret;
288 }
289
290 /*
291  * twl4030 GPIO request function
292  */
293 int twl4030_request_gpio(int gpio)
294 {
295         int ret = 0;
296
297         if (unlikely(gpio >= TWL4030_GPIO_MAX))
298                 return -EPERM;
299
300         down(&gpio_sem);
301         if (gpio_usage_count & (0x1 << gpio))
302                 ret = -EBUSY;
303         else {
304                 /* First time usage? - switch on GPIO module */
305                 if (!gpio_usage_count) {
306                         ret =
307                         gpio_twl4030_write(REG_GPIO_CTRL,
308                                         MASK_GPIO_CTRL_GPIO_ON);
309                         ret = gpio_twl4030_write(REG_GPIO_SIH_CTRL, 0x00);
310                 }
311                 if (!ret)
312                         gpio_usage_count |= (0x1 << gpio);
313         }
314         up(&gpio_sem);
315         return ret;
316 }
317 EXPORT_SYMBOL(twl4030_request_gpio);
318
319 /*
320  * TWL4030 GPIO free module
321  */
322 int twl4030_free_gpio(int gpio)
323 {
324         int ret = 0;
325
326         if (unlikely(gpio >= TWL4030_GPIO_MAX))
327                 return -EPERM;
328
329         down(&gpio_sem);
330
331         if ((gpio_usage_count & (0x1 << gpio)) == 0)
332                 ret = -EPERM;
333         else
334                 gpio_usage_count &= ~(0x1 << gpio);
335
336         /* Last time usage? - switch off GPIO module */
337         if (!gpio_usage_count)
338                 ret = gpio_twl4030_write(REG_GPIO_CTRL, 0x0);
339
340         up(&gpio_sem);
341         return ret;
342 }
343 EXPORT_SYMBOL(twl4030_free_gpio);
344
345 /*
346  * Set direction for TWL4030 GPIO
347  */
348 int twl4030_set_gpio_direction(int gpio, int is_input)
349 {
350         u8 d_bnk = GET_GPIO_DATA_BANK(gpio);
351         u8 d_msk = MASK_GPIODATADIR_GPIOxDIR(GET_GPIO_DATA_OFF(gpio));
352         u8 reg = 0;
353         u8 base = 0;
354         int ret = 0;
355
356         if (unlikely((gpio >= TWL4030_GPIO_MAX)
357                 || !(gpio_usage_count & (0x1 << gpio))))
358                 return -EPERM;
359
360         base = REG_GPIODATADIR1 + d_bnk;
361
362         down(&gpio_sem);
363         ret = gpio_twl4030_read(base);
364         if (ret >= 0) {
365                 if (is_input)
366                         reg = (u8) ((ret) & ~(d_msk));
367                 else
368                         reg = (u8) ((ret) | (d_msk));
369
370                 ret = gpio_twl4030_write(base, reg);
371         }
372         up(&gpio_sem);
373         return ret;
374 }
375 EXPORT_SYMBOL(twl4030_set_gpio_direction);
376
377 /*
378  * To enable/disable GPIO pin on TWL4030
379  */
380 int twl4030_set_gpio_dataout(int gpio, int enable)
381 {
382         u8 d_bnk = GET_GPIO_DATA_BANK(gpio);
383         u8 d_msk = MASK_GPIODATAOUT_GPIOxOUT(GET_GPIO_DATA_OFF(gpio));
384         u8 base = 0;
385         int ret = 0;
386
387         if (unlikely((gpio >= TWL4030_GPIO_MAX)
388                 || !(gpio_usage_count & (0x1 << gpio))))
389                 return -EPERM;
390
391         if (enable)
392                 base = REG_SETGPIODATAOUT1 + d_bnk;
393         else
394                 base = REG_CLEARGPIODATAOUT1 + d_bnk;
395
396         down(&gpio_sem);
397         ret = gpio_twl4030_write(base, d_msk);
398         up(&gpio_sem);
399         return ret;
400 }
401 EXPORT_SYMBOL(twl4030_set_gpio_dataout);
402
403 /*
404  * To get the status of a GPIO pin on TWL4030
405  */
406 int twl4030_get_gpio_datain(int gpio)
407 {
408         u8 d_bnk = GET_GPIO_DATA_BANK(gpio);
409         u8 d_off = BIT_GPIODATAIN_GPIOxIN(GET_GPIO_DATA_OFF(gpio));
410         u8 base = 0;
411         int ret = 0;
412
413         if (unlikely((gpio >= TWL4030_GPIO_MAX)
414                 || !(gpio_usage_count & (0x1 << gpio))))
415                 return -EPERM;
416
417         base = REG_GPIODATAIN1 + d_bnk;
418         down(&gpio_sem);
419         ret = gpio_twl4030_read(base);
420         up(&gpio_sem);
421         if (ret > 0)
422                 ret = (ret >> d_off) & 0x1;
423
424         return ret;
425 }
426 EXPORT_SYMBOL(twl4030_get_gpio_datain);
427
428 /*
429  * Configure PULL type for a GPIO pin on TWL4030
430  */
431 int twl4030_set_gpio_pull(int gpio, int pull_dircn)
432 {
433         u8 c_bnk = GET_GPIO_CTL_BANK(gpio);
434         u8 c_off = GET_GPIO_CTL_OFF(gpio);
435         u8 c_msk = 0;
436         u8 reg = 0;
437         u8 base = 0;
438         int ret = 0;
439
440         if (unlikely((gpio >= TWL4030_GPIO_MAX) ||
441                 !(gpio_usage_count & (0x1 << gpio))))
442                 return -EPERM;
443
444         base = REG_GPIOPUPDCTR1 + c_bnk;
445         if (pull_dircn == TWL4030_GPIO_PULL_DOWN)
446                 c_msk = MASK_GPIOPUPDCTR1_GPIOxPD(c_off);
447         else if (pull_dircn == TWL4030_GPIO_PULL_UP)
448                 c_msk = MASK_GPIOPUPDCTR1_GPIOxPU(c_off);
449
450         down(&gpio_sem);
451         ret = gpio_twl4030_read(base);
452         if (ret >= 0) {
453                 /* clear the previous up/down values */
454                 reg = (u8) (ret);
455                 reg &= ~(MASK_GPIOPUPDCTR1_GPIOxPU(c_off) |
456                         MASK_GPIOPUPDCTR1_GPIOxPD(c_off));
457                 reg |= c_msk;
458                 ret = gpio_twl4030_write(base, reg);
459         }
460         up(&gpio_sem);
461         return ret;
462 }
463 EXPORT_SYMBOL(twl4030_set_gpio_pull);
464
465 /*
466  * Configure Edge control for a GPIO pin on TWL4030
467  */
468 int twl4030_set_gpio_edge_ctrl(int gpio, int edge)
469 {
470         u8 c_bnk = GET_GPIO_CTL_BANK(gpio);
471         u8 c_off = GET_GPIO_CTL_OFF(gpio);
472         u8 c_msk = 0;
473         u8 reg = 0;
474         u8 base = 0;
475         int ret = 0;
476
477         if (unlikely((gpio >= TWL4030_GPIO_MAX)
478                 || !(gpio_usage_count & (0x1 << gpio))))
479                 return -EPERM;
480
481         base = REG_GPIO_EDR1 + c_bnk;
482
483         if (edge & TWL4030_GPIO_EDGE_RISING)
484                 c_msk |= MASK_GPIO_EDR1_GPIOxRISING(c_off);
485
486         if (edge & TWL4030_GPIO_EDGE_FALLING)
487                 c_msk |= MASK_GPIO_EDR1_GPIOxFALLING(c_off);
488
489         down(&gpio_sem);
490         ret = gpio_twl4030_read(base);
491         if (ret >= 0) {
492                 /* clear the previous rising/falling values */
493                 reg =
494                 (u8) (ret &
495                         ~(MASK_GPIO_EDR1_GPIOxFALLING(c_off) |
496                         MASK_GPIO_EDR1_GPIOxRISING(c_off)));
497                 reg |= c_msk;
498                 ret = gpio_twl4030_write(base, reg);
499         }
500         up(&gpio_sem);
501         return ret;
502 }
503 EXPORT_SYMBOL(twl4030_set_gpio_edge_ctrl);
504
505 /*
506  * Configure debounce timing value for a GPIO pin on TWL4030
507  */
508 int twl4030_set_gpio_debounce(int gpio, int enable)
509 {
510         u8 d_bnk = GET_GPIO_DATA_BANK(gpio);
511         u8 d_msk = MASK_GPIO_DEBEN_GPIOxDEB(GET_GPIO_DATA_OFF(gpio));
512         u8 reg = 0;
513         u8 base = 0;
514         int ret = 0;
515
516         if (unlikely((gpio >= TWL4030_GPIO_MAX)
517                 || !(gpio_usage_count & (0x1 << gpio))))
518                 return -EPERM;
519
520         base = REG_GPIO_DEBEN1 + d_bnk;
521         down(&gpio_sem);
522         ret = gpio_twl4030_read(base);
523         if (ret >= 0) {
524                 if (enable)
525                         reg = (u8) ((ret) | (d_msk));
526                 else
527                         reg = (u8) ((ret) & ~(d_msk));
528
529                 ret = gpio_twl4030_write(base, reg);
530         }
531         up(&gpio_sem);
532         return ret;
533 }
534 EXPORT_SYMBOL(twl4030_set_gpio_debounce);
535
536 /*
537  * Configure Card detect for GPIO pin on TWL4030
538  */
539 int twl4030_set_gpio_card_detect(int gpio, int enable)
540 {
541         u8 reg = 0;
542         u8 msk = (1 << gpio);
543         int ret = 0;
544
545         /* Only GPIO 0 or 1 can be used for CD feature.. */
546         if (unlikely((gpio >= TWL4030_GPIO_MAX)
547                 || !(gpio_usage_count & (0x1 << gpio))
548                 || (gpio >= TWL4030_GPIO_MAX_CD))) {
549                 return -EPERM;
550         }
551
552         down(&gpio_sem);
553         ret = gpio_twl4030_read(REG_GPIO_CTRL);
554         if (ret >= 0) {
555                 if (enable)
556                         reg = (u8) (ret | msk);
557                 else
558                         reg = (u8) (ret & ~msk);
559
560                 ret = gpio_twl4030_write(REG_GPIO_CTRL, reg);
561         }
562         up(&gpio_sem);
563         return (ret);
564 }
565 EXPORT_SYMBOL(twl4030_set_gpio_card_detect);
566
567 /* MODULE FUNCTIONS */
568
569 /*
570  * gpio_unmask_thread() runs as a kernel thread.  It is awakened by the unmask
571  * method for the GPIO interrupts.  It unmasks all of the GPIO interrupts
572  * specified in the gpio_pending_unmask bitmask.  We have to do the unmasking
573  * in a kernel thread rather than directly in the unmask method because of the
574  * need to access the TWL4030 via the I2C bus.  Note that we don't need to be
575  * concerned about race conditions where the request to unmask a GPIO interrupt
576  * has already been cancelled before this thread does the unmasking.  If a GPIO
577  * interrupt is improperly unmasked, then the IRQ handler for it will mask it
578  * when an interrupt occurs.
579  */
580 static int twl4030_gpio_unmask_thread(void *data)
581 {
582         current->flags |= PF_NOFREEZE;
583
584         while (!kthread_should_stop()) {
585                 int irq;
586                 unsigned int gpio_unmask;
587
588                 local_irq_disable();
589                 gpio_unmask = gpio_pending_unmask;
590                 gpio_pending_unmask = 0;
591                 local_irq_enable();
592
593                 for (irq = TWL4030_GPIO_IRQ_BASE; 0 != gpio_unmask;
594                                 gpio_unmask >>= 1, irq++) {
595                         if (gpio_unmask & 0x1)
596                                 twl4030_gpio_unmask(irq);
597                 }
598
599                 local_irq_disable();
600                 if (!gpio_pending_unmask)
601                         set_current_state(TASK_INTERRUPTIBLE);
602                 local_irq_enable();
603
604                 schedule();
605         }
606         set_current_state(TASK_RUNNING);
607         return 0;
608 }
609
610 /*
611  * do_twl4030_gpio_irq() is the desc->handle method for each of the twl4030
612  * gpio interrupts.  It executes in kernel thread context.
613  * On entry, cpu interrupts are enabled.
614  */
615 static void do_twl4030_gpio_irq(unsigned int irq, irq_desc_t *desc)
616 {
617         struct irqaction *action;
618         const unsigned int cpu = smp_processor_id();
619
620         desc->status |= IRQ_LEVEL;
621
622         /*
623          * Acknowledge, clear _AND_ disable the interrupt.
624          */
625         twl4030_gpio_mask_and_ack(irq);
626
627         if (!desc->depth) {
628                 kstat_cpu(cpu).irqs[irq]++;
629
630                 action = desc->action;
631                 if (action) {
632                         int ret;
633                         int status = 0;
634                         int retval = 0;
635                         do {
636                                 /* Call the ISR with cpu interrupts enabled. */
637                                 ret = action->handler(irq, action->dev_id);
638                                 if (ret == IRQ_HANDLED)
639                                         status |= action->flags;
640                                 retval |= ret;
641                                 action = action->next;
642                         } while (action);
643
644                         if (retval != IRQ_HANDLED)
645                                 printk(KERN_ERR "ISR for TWL4030 GPIO"
646                                         " irq %d can't handle interrupt\n",
647                                         irq);
648
649                         if (!desc->depth)
650                                 twl4030_gpio_unmask(irq);
651                 }
652         }
653 }
654
655 /*
656  * do_twl4030_gpio_module_irq() is the desc->handle method for the twl4030 gpio
657  * module interrupt.  It executes in kernel thread context.
658  * This is a chained interrupt, so there is no desc->action method for it.
659  * We query the gpio module interrupt controller in the twl4030 to determine
660  * which gpio lines are generating interrupt requests, and then call the
661  * desc->handle method for each gpio that needs service.
662  * On entry, cpu interrupts are disabled.
663  */
664 static void do_twl4030_gpio_module_irq(unsigned int irq, irq_desc_t *desc)
665 {
666         const unsigned int cpu = smp_processor_id();
667
668         desc->status |= IRQ_LEVEL;
669         /*
670         * The desc->handle method would normally call the desc->chip->ack
671         * method here, but we won't bother since our ack method is NULL.
672         */
673         if (!desc->depth) {
674                 int gpio_irq;
675                 unsigned int gpio_isr;
676
677                 kstat_cpu(cpu).irqs[irq]++;
678                 local_irq_enable();
679
680                 down(&gpio_sem);
681                 if (gpio_read_isr(&gpio_isr))
682                         gpio_isr = 0;
683                 up(&gpio_sem);
684
685                 for (gpio_irq = TWL4030_GPIO_IRQ_BASE; 0 != gpio_isr;
686                         gpio_isr >>= 1, gpio_irq++) {
687                         if (gpio_isr & 0x1) {
688                                 irq_desc_t *d = irq_desc + gpio_irq;
689                                 d->handle_irq(gpio_irq, d);
690                         }
691                 }
692
693                 local_irq_disable();
694                 /*
695                  * Here is where we should call the unmask method, but again we
696                  * won't bother since it is NULL.
697                  */
698         }
699 }
700
701 /* TWL4030 Initialization module */
702 static int __init gpio_twl4030_init(void)
703 {
704         int ret;
705         int irq = 0;
706
707         /* init the global locking sem */
708         sema_init(&gpio_sem, 1);
709
710         /* All GPIO interrupts are initially masked */
711         gpio_pending_unmask = 0;
712         gpio_imr_shadow = GPIO_32_MASK;
713         ret = gpio_write_imr(gpio_imr_shadow);
714         if (!ret) {
715                 /*
716                  * Create a kernel thread to handle deferred unmasking of gpio
717                  * interrupts.
718                  */
719                 gpio_unmask_thread = kthread_create(twl4030_gpio_unmask_thread,
720                         NULL, "twl4030 gpio");
721                 if (!gpio_unmask_thread) {
722                         printk(KERN_ERR
723                                 "%s: could not create twl4030 gpio unmask"
724                                 " thread!\n", __func__);
725                         ret = -ENOMEM;
726                 }
727         }
728
729         if (!ret) {
730                 /* install an irq handler for each of the gpio interrupts */
731                 for (irq = TWL4030_GPIO_IRQ_BASE; irq < TWL4030_GPIO_IRQ_END;
732                         irq++) {
733                         set_irq_chip(irq, &twl4030_gpio_irq_chip);
734                         set_irq_handler(irq, do_twl4030_gpio_irq);
735                         set_irq_flags(irq, IRQF_VALID);
736                 }
737
738                 /*
739                  * Install an irq handler to demultiplex the gpio module
740                  * interrupt.
741                  */
742                 set_irq_chip(TWL4030_MODIRQ_GPIO,
743                         &twl4030_gpio_module_irq_chip);
744                 set_irq_chained_handler(TWL4030_MODIRQ_GPIO,
745                         do_twl4030_gpio_module_irq);
746                 wake_up_process(gpio_unmask_thread);
747         }
748
749         printk(KERN_INFO "TWL4030 GPIO Demux: IRQ Range %d to %d,"
750                 " Initialization %s\n", TWL4030_GPIO_IRQ_BASE,
751                 TWL4030_GPIO_IRQ_END, (ret) ? "Failed" : "Success");
752         return ret;
753 }
754
755 /* TWL GPIO exit module */
756 static void __exit gpio_twl4030_exit(void)
757 {
758         int irq;
759
760         /* uninstall the gpio demultiplexing interrupt handler */
761         set_irq_handler(TWL4030_MODIRQ_GPIO, NULL);
762         set_irq_flags(TWL4030_MODIRQ_GPIO, 0);
763
764         /* uninstall the irq handler for each of the gpio interrupts */
765         for (irq = TWL4030_GPIO_IRQ_BASE; irq < TWL4030_GPIO_IRQ_END; irq++) {
766                 set_irq_handler(irq, NULL);
767                 set_irq_flags(irq, 0);
768         }
769
770         /* stop the gpio unmask kernel thread */
771         if (gpio_unmask_thread) {
772                 kthread_stop(gpio_unmask_thread);
773                 gpio_unmask_thread = NULL;
774         }
775 }
776
777 module_init(gpio_twl4030_init);
778 module_exit(gpio_twl4030_exit);
779
780 MODULE_ALIAS("i2c:twl4030-gpio");
781 MODULE_AUTHOR("Texas Instruments, Inc.");
782 MODULE_DESCRIPTION("GPIO interface for TWL4030");
783 MODULE_LICENSE("GPL");