]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/gpio/twl4030-gpio.c
47dc6e2510407d6ab9a004feb09a3e85c41ef56f
[linux-2.6-omap-h63xx.git] / drivers / gpio / twl4030-gpio.c
1 /*
2  * twl4030_gpio.c -- access to GPIOs on TWL4030/TPS659x0 chips
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 #include <linux/module.h>
29 #include <linux/kernel_stat.h>
30 #include <linux/init.h>
31 #include <linux/time.h>
32 #include <linux/interrupt.h>
33 #include <linux/device.h>
34 #include <linux/kthread.h>
35 #include <linux/irq.h>
36 #include <linux/gpio.h>
37 #include <linux/platform_device.h>
38 #include <linux/slab.h>
39
40 #include <linux/i2c/twl4030.h>
41 #include <linux/i2c/twl4030-gpio.h>
42
43
44 static inline void activate_irq(int irq)
45 {
46 #ifdef CONFIG_ARM
47         /* ARM requires an extra step to clear IRQ_NOREQUEST, which it
48          * sets on behalf of every irq_chip.  Also sets IRQ_NOPROBE.
49          */
50         set_irq_flags(irq, IRQF_VALID);
51 #else
52         /* same effect on other architectures */
53         set_irq_noprobe(irq);
54 #endif
55 }
56
57 static struct gpio_chip twl_gpiochip;
58 static int twl4030_gpio_irq_base;
59 static int twl4030_gpio_irq_end;
60
61 /* genirq interfaces are not available to modules */
62 #ifdef MODULE
63 #define is_module()     true
64 #else
65 #define is_module()     false
66 #endif
67
68 /* BitField Definitions */
69
70 /* Data banks : 3 banks for 8 gpios each */
71 #define DATA_BANK_MAX                           8
72 #define GET_GPIO_DATA_BANK(x)                   ((x)/DATA_BANK_MAX)
73 #define GET_GPIO_DATA_OFF(x)                    ((x)%DATA_BANK_MAX)
74
75 /* GPIODATADIR Fields each block 0-7 */
76 #define BIT_GPIODATADIR_GPIOxDIR(x)             (x)
77 #define MASK_GPIODATADIR_GPIOxDIR(x)            (0x01 << (x))
78
79 /* GPIODATAIN Fields each block 0-7 */
80 #define BIT_GPIODATAIN_GPIOxIN(x)               (x)
81 #define MASK_GPIODATAIN_GPIOxIN(x)              (0x01 << (x))
82
83 /* GPIODATAOUT Fields each block 0-7 */
84 #define BIT_GPIODATAOUT_GPIOxOUT(x)             (x)
85 #define MASK_GPIODATAOUT_GPIOxOUT(x)            (0x01 << (x))
86
87 /* CLEARGPIODATAOUT Fields */
88 #define BIT_CLEARGPIODATAOUT_GPIOxOUT(x)        (x)
89 #define MASK_CLEARGPIODATAOUT_GPIOxOUT(x)       (0x01 << (x))
90
91 /* SETGPIODATAOUT Fields */
92 #define BIT_SETGPIODATAOUT_GPIOxOUT(x)          (x)
93 #define MASK_SETGPIODATAOUT_GPIOxOUT(x)         (0x01 << (x))
94
95 /* GPIO_DEBEN Fields */
96 #define BIT_GPIO_DEBEN_GPIOxDEB(x)              (x)
97 #define MASK_GPIO_DEBEN_GPIOxDEB(x)             (0x01 << (x))
98
99 /* GPIO_ISR1A Fields */
100 #define BIT_GPIO_ISR_GPIOxISR(x)                (x)
101 #define MASK_GPIO_ISR_GPIOxISR(x)               (0x01 << (x))
102
103 /* GPIO_IMR1A Fields */
104 #define BIT_GPIO_IMR1A_GPIOxIMR(x)              (x)
105 #define MASK_GPIO_IMR1A_GPIOxIMR(x)             (0x01 << (x))
106
107 /* GPIO_SIR1 Fields */
108 #define BIT_GPIO_SIR1_GPIOxSIR(x)               (x)
109 #define MASK_GPIO_SIR1_GPIO0SIR                 (0x01 << (x))
110
111
112 /* Control banks : 5 banks for 4 gpios each */
113 #define DATA_CTL_MAX                    4
114 #define GET_GPIO_CTL_BANK(x)            ((x)/DATA_CTL_MAX)
115 #define GET_GPIO_CTL_OFF(x)             ((x)%DATA_CTL_MAX)
116 #define GPIO_BANK_MAX                   GET_GPIO_CTL_BANK(TWL4030_GPIO_MAX)
117
118 /* GPIOPUPDCTRx Fields 5 banks of 4 gpios each */
119 #define BIT_GPIOPUPDCTR1_GPIOxPD(x)     (2 * (x))
120 #define MASK_GPIOPUPDCTR1_GPIOxPD(x)    (0x01 << (2 * (x)))
121 #define BIT_GPIOPUPDCTR1_GPIOxPU(x)     ((x) + 1)
122 #define MASK_GPIOPUPDCTR1_GPIOxPU(x)    (0x01 << (((2 * (x)) + 1)))
123
124 /* GPIO_EDR1 Fields */
125 #define BIT_GPIO_EDR1_GPIOxFALLING(x)   (2 * (x))
126 #define MASK_GPIO_EDR1_GPIOxFALLING(x)  (0x01 << (2 * (x)))
127 #define BIT_GPIO_EDR1_GPIOxRISING(x)    ((x) + 1)
128 #define MASK_GPIO_EDR1_GPIOxRISING(x)   (0x01 << (((2 * (x)) + 1)))
129
130 /* GPIO_SIH_CTRL Fields */
131 #define BIT_GPIO_SIH_CTRL_EXCLEN        (0x000)
132 #define MASK_GPIO_SIH_CTRL_EXCLEN       (0x00000001)
133 #define BIT_GPIO_SIH_CTRL_PENDDIS       (0x001)
134 #define MASK_GPIO_SIH_CTRL_PENDDIS      (0x00000002)
135 #define BIT_GPIO_SIH_CTRL_COR           (0x002)
136 #define MASK_GPIO_SIH_CTRL_COR          (0x00000004)
137
138 /* GPIO_CTRL Fields */
139 #define BIT_GPIO_CTRL_GPIO0CD1          (0x000)
140 #define MASK_GPIO_CTRL_GPIO0CD1         (0x00000001)
141 #define BIT_GPIO_CTRL_GPIO1CD2          (0x001)
142 #define MASK_GPIO_CTRL_GPIO1CD2         (0x00000002)
143 #define BIT_GPIO_CTRL_GPIO_ON           (0x002)
144 #define MASK_GPIO_CTRL_GPIO_ON          (0x00000004)
145
146 /* Mask for GPIO registers when aggregated into a 32-bit integer */
147 #define GPIO_32_MASK                    0x0003ffff
148
149 /* Data structures */
150 static DEFINE_MUTEX(gpio_lock);
151
152 /* store usage of each GPIO. - each bit represents one GPIO */
153 static unsigned int gpio_usage_count;
154
155 /* shadow the imr register */
156 static unsigned int gpio_imr_shadow;
157
158 /* bitmask of pending requests to unmask gpio interrupts */
159 static unsigned int gpio_pending_unmask;
160
161 /* bitmask of requests to set gpio irq trigger type */
162 static unsigned int gpio_pending_trigger;
163
164 /* pointer to gpio unmask thread struct */
165 static struct task_struct *gpio_unmask_thread;
166
167 /*
168  * Helper functions to read and write the GPIO ISR and IMR registers as
169  * 32-bit integers. Functions return 0 on success, non-zero otherwise.
170  * The caller must hold gpio_lock.
171  */
172
173 static int gpio_read_isr(unsigned int *isr)
174 {
175         int ret;
176
177         *isr = 0;
178         ret = twl4030_i2c_read(TWL4030_MODULE_GPIO, (u8 *) isr,
179                         REG_GPIO_ISR1A, 3);
180         le32_to_cpup(isr);
181         *isr &= GPIO_32_MASK;
182
183         return ret;
184 }
185
186 static int gpio_write_isr(unsigned int isr)
187 {
188         isr &= GPIO_32_MASK;
189         /*
190          * The buffer passed to the twl4030_i2c_write() routine must have an
191          * extra byte at the beginning reserved for its internal use.
192          */
193         isr <<= 8;
194         isr = cpu_to_le32(isr);
195         return twl4030_i2c_write(TWL4030_MODULE_GPIO, (u8 *) &isr,
196                                 REG_GPIO_ISR1A, 3);
197 }
198
199 static int gpio_write_imr(unsigned int imr)
200 {
201         imr &= GPIO_32_MASK;
202         /*
203          * The buffer passed to the twl4030_i2c_write() routine must have an
204          * extra byte at the beginning reserved for its internal use.
205          */
206         imr <<= 8;
207         imr = cpu_to_le32(imr);
208         return twl4030_i2c_write(TWL4030_MODULE_GPIO, (u8 *) &imr,
209                                 REG_GPIO_IMR1A, 3);
210 }
211
212 /*
213  * These routines are analagous to the irqchip methods, but they are designed
214  * to be called from thread context with cpu interrupts enabled and with no
215  * locked spinlocks.  We call these routines from our custom IRQ handler
216  * instead of the usual irqchip methods.
217  */
218 static void twl4030_gpio_mask_and_ack(unsigned int irq)
219 {
220         int gpio = irq - twl4030_gpio_irq_base;
221
222         mutex_lock(&gpio_lock);
223         /* mask */
224         gpio_imr_shadow |= (1 << gpio);
225         gpio_write_imr(gpio_imr_shadow);
226         /* ack */
227         gpio_write_isr(1 << gpio);
228         mutex_unlock(&gpio_lock);
229 }
230
231 static void twl4030_gpio_unmask(unsigned int irq)
232 {
233         int gpio = irq - twl4030_gpio_irq_base;
234
235         mutex_lock(&gpio_lock);
236         gpio_imr_shadow &= ~(1 << gpio);
237         gpio_write_imr(gpio_imr_shadow);
238         mutex_unlock(&gpio_lock);
239 }
240
241 /*
242  * These are the irqchip methods for the TWL4030 GPIO interrupts.
243  * Our IRQ handle method doesn't call these, but they will be called by
244  * other routines such as setup_irq() and enable_irq().  They are called
245  * with cpu interrupts disabled and with a lock on the irq_controller_lock
246  * spinlock.  This complicates matters, because accessing the TWL4030 GPIO
247  * interrupt controller requires I2C bus transactions that can't be initiated
248  * in this context.  Our solution is to defer accessing the interrupt
249  * controller to a kernel thread.  We only need to support the unmask method.
250  */
251
252 static void twl4030_gpio_irq_mask_and_ack(unsigned int irq)
253 {
254 }
255
256 static void twl4030_gpio_irq_mask(unsigned int irq)
257 {
258 }
259
260 static void twl4030_gpio_irq_unmask(unsigned int irq)
261 {
262         int gpio = irq - twl4030_gpio_irq_base;
263
264         gpio_pending_unmask |= (1 << gpio);
265         if (gpio_unmask_thread && gpio_unmask_thread->state != TASK_RUNNING)
266                 wake_up_process(gpio_unmask_thread);
267 }
268
269 static int twl4030_gpio_irq_set_type(unsigned int irq, unsigned trigger)
270 {
271         struct irq_desc *desc = irq_desc + irq;
272         int gpio = irq - twl4030_gpio_irq_base;
273
274         trigger &= IRQ_TYPE_SENSE_MASK;
275         if (trigger & ~IRQ_TYPE_EDGE_BOTH)
276                 return -EINVAL;
277         if ((desc->status & IRQ_TYPE_SENSE_MASK) == trigger)
278                 return 0;
279
280         desc->status &= ~IRQ_TYPE_SENSE_MASK;
281         desc->status |= trigger;
282
283         /* REVISIT This makes the "unmask" thread do double duty,
284          * updating IRQ trigger modes too.  Rename appropriately...
285          */
286         gpio_pending_trigger |= (1 << gpio);
287         if (gpio_unmask_thread && gpio_unmask_thread->state != TASK_RUNNING)
288                 wake_up_process(gpio_unmask_thread);
289
290         return 0;
291 }
292
293 static struct irq_chip twl4030_gpio_irq_chip = {
294         .name           = "twl4030",
295         .ack            = twl4030_gpio_irq_mask_and_ack,
296         .mask           = twl4030_gpio_irq_mask,
297         .unmask         = twl4030_gpio_irq_unmask,
298         .set_type       = twl4030_gpio_irq_set_type,
299 };
300
301 /*
302  * These are the irqchip methods for the TWL4030 PIH GPIO module interrupt.
303  * The PIH module doesn't have interrupt masking capability, so these
304  * methods are NULL.
305  */
306 static void twl4030_gpio_module_ack(unsigned int irq) {}
307 static void twl4030_gpio_module_mask(unsigned int irq) {}
308 static void twl4030_gpio_module_unmask(unsigned int irq) {}
309 static struct irq_chip twl4030_gpio_module_irq_chip = {
310         .ack    = twl4030_gpio_module_ack,
311         .mask   = twl4030_gpio_module_mask,
312         .unmask = twl4030_gpio_module_unmask,
313 };
314
315 /*
316  * To configure TWL4030 GPIO module registers
317  */
318 static inline int gpio_twl4030_write(u8 address, u8 data)
319 {
320         int ret = 0;
321
322         ret = twl4030_i2c_write_u8(TWL4030_MODULE_GPIO, data, address);
323         return ret;
324 }
325
326 /*
327  * To read a TWL4030 GPIO module register
328  */
329 static inline int gpio_twl4030_read(u8 address)
330 {
331         u8 data;
332         int ret = 0;
333
334         ret = twl4030_i2c_read_u8(TWL4030_MODULE_GPIO, &data, address);
335         if (ret >= 0)
336                 ret = data;
337         return ret;
338 }
339
340 /*
341  * twl4030 GPIO request function
342  */
343 int twl4030_request_gpio(int gpio)
344 {
345         int ret = 0;
346
347         if (unlikely(gpio >= TWL4030_GPIO_MAX))
348                 return -EPERM;
349
350         ret = gpio_request(twl_gpiochip.base + gpio, NULL);
351         if (ret < 0)
352                 return ret;
353
354         mutex_lock(&gpio_lock);
355         if (gpio_usage_count & (0x1 << gpio)) {
356                 ret = -EBUSY;
357         } else {
358                 /* First time usage? - switch on GPIO module */
359                 if (!gpio_usage_count) {
360                         ret = gpio_twl4030_write(REG_GPIO_CTRL,
361                                         MASK_GPIO_CTRL_GPIO_ON);
362                         ret = gpio_twl4030_write(REG_GPIO_SIH_CTRL, 0x00);
363                 }
364                 if (!ret)
365                         gpio_usage_count |= (0x1 << gpio);
366                 else
367                         gpio_free(twl_gpiochip.base + gpio);
368         }
369         mutex_unlock(&gpio_lock);
370         return ret;
371 }
372 EXPORT_SYMBOL(twl4030_request_gpio);
373
374 /*
375  * TWL4030 GPIO free module
376  */
377 int twl4030_free_gpio(int gpio)
378 {
379         int ret = 0;
380
381         if (unlikely(gpio >= TWL4030_GPIO_MAX))
382                 return -EPERM;
383
384         mutex_lock(&gpio_lock);
385
386         if ((gpio_usage_count & (0x1 << gpio)) == 0) {
387                 ret = -EPERM;
388         } else {
389                 gpio_usage_count &= ~(0x1 << gpio);
390                 gpio_free(twl_gpiochip.base + gpio);
391         }
392
393         /* Last time usage? - switch off GPIO module */
394         if (ret == 0 && !gpio_usage_count)
395                 ret = gpio_twl4030_write(REG_GPIO_CTRL, 0x0);
396
397         mutex_unlock(&gpio_lock);
398         return ret;
399 }
400 EXPORT_SYMBOL(twl4030_free_gpio);
401
402 /*
403  * Set direction for TWL4030 GPIO
404  */
405 static int twl4030_set_gpio_direction(int gpio, int is_input)
406 {
407         u8 d_bnk = GET_GPIO_DATA_BANK(gpio);
408         u8 d_msk = MASK_GPIODATADIR_GPIOxDIR(GET_GPIO_DATA_OFF(gpio));
409         u8 reg = 0;
410         u8 base = REG_GPIODATADIR1 + d_bnk;
411         int ret = 0;
412
413         if (unlikely(!(gpio_usage_count & (0x1 << gpio))))
414                 return -EPERM;
415
416         mutex_lock(&gpio_lock);
417         ret = gpio_twl4030_read(base);
418         if (ret >= 0) {
419                 if (is_input)
420                         reg = (u8) ((ret) & ~(d_msk));
421                 else
422                         reg = (u8) ((ret) | (d_msk));
423
424                 ret = gpio_twl4030_write(base, reg);
425         }
426         mutex_unlock(&gpio_lock);
427         return ret;
428 }
429
430 /*
431  * To enable/disable GPIO pin on TWL4030
432  */
433 static int twl4030_set_gpio_dataout(int gpio, int enable)
434 {
435         u8 d_bnk = GET_GPIO_DATA_BANK(gpio);
436         u8 d_msk = MASK_GPIODATAOUT_GPIOxOUT(GET_GPIO_DATA_OFF(gpio));
437         u8 base = 0;
438         int ret = 0;
439
440         if (unlikely(!(gpio_usage_count & (0x1 << gpio))))
441                 return -EPERM;
442
443         if (enable)
444                 base = REG_SETGPIODATAOUT1 + d_bnk;
445         else
446                 base = REG_CLEARGPIODATAOUT1 + d_bnk;
447
448         mutex_lock(&gpio_lock);
449         ret = gpio_twl4030_write(base, d_msk);
450         mutex_unlock(&gpio_lock);
451         return ret;
452 }
453
454 /*
455  * To get the status of a GPIO pin on TWL4030
456  */
457 int twl4030_get_gpio_datain(int gpio)
458 {
459         u8 d_bnk = GET_GPIO_DATA_BANK(gpio);
460         u8 d_off = BIT_GPIODATAIN_GPIOxIN(GET_GPIO_DATA_OFF(gpio));
461         u8 base = 0;
462         int ret = 0;
463
464         if (unlikely((gpio >= TWL4030_GPIO_MAX)
465                 || !(gpio_usage_count & (0x1 << gpio))))
466                 return -EPERM;
467
468         base = REG_GPIODATAIN1 + d_bnk;
469         mutex_lock(&gpio_lock);
470         ret = gpio_twl4030_read(base);
471         mutex_unlock(&gpio_lock);
472         if (ret > 0)
473                 ret = (ret >> d_off) & 0x1;
474
475         return ret;
476 }
477 EXPORT_SYMBOL(twl4030_get_gpio_datain);
478
479 static int twl4030_set_gpio_edge_ctrl(int gpio, int edge)
480 {
481         u8 c_bnk = GET_GPIO_CTL_BANK(gpio);
482         u8 c_off = GET_GPIO_CTL_OFF(gpio);
483         u8 c_msk = 0;
484         u8 reg = 0;
485         u8 base = 0;
486         int ret = 0;
487
488         base = REG_GPIO_EDR1 + c_bnk;
489
490         if (edge & IRQ_TYPE_EDGE_RISING)
491                 c_msk |= MASK_GPIO_EDR1_GPIOxRISING(c_off);
492         if (edge & IRQ_TYPE_EDGE_FALLING)
493                 c_msk |= MASK_GPIO_EDR1_GPIOxFALLING(c_off);
494
495         mutex_lock(&gpio_lock);
496         ret = gpio_twl4030_read(base);
497         if (ret >= 0) {
498                 /* clear the previous rising/falling values */
499                 reg = (u8) ret;
500                 reg &= ~(MASK_GPIO_EDR1_GPIOxFALLING(c_off)
501                         | MASK_GPIO_EDR1_GPIOxRISING(c_off));
502                 reg |= c_msk;
503                 ret = gpio_twl4030_write(base, reg);
504         }
505         mutex_unlock(&gpio_lock);
506         return ret;
507 }
508
509 /*
510  * Configure debounce timing value for a GPIO pin on TWL4030
511  */
512 int twl4030_set_gpio_debounce(int gpio, int enable)
513 {
514         u8 d_bnk = GET_GPIO_DATA_BANK(gpio);
515         u8 d_msk = MASK_GPIO_DEBEN_GPIOxDEB(GET_GPIO_DATA_OFF(gpio));
516         u8 reg = 0;
517         u8 base = 0;
518         int ret = 0;
519
520         if (unlikely((gpio >= TWL4030_GPIO_MAX)
521                 || !(gpio_usage_count & (0x1 << gpio))))
522                 return -EPERM;
523
524         base = REG_GPIO_DEBEN1 + d_bnk;
525         mutex_lock(&gpio_lock);
526         ret = gpio_twl4030_read(base);
527         if (ret >= 0) {
528                 if (enable)
529                         reg = (u8) ((ret) | (d_msk));
530                 else
531                         reg = (u8) ((ret) & ~(d_msk));
532
533                 ret = gpio_twl4030_write(base, reg);
534         }
535         mutex_unlock(&gpio_lock);
536         return ret;
537 }
538 EXPORT_SYMBOL(twl4030_set_gpio_debounce);
539
540 #if 0
541 /*
542  * Configure Card detect for GPIO pin on TWL4030
543  *
544  * This means:  VMMC1 or VMMC2 is enabled or disabled based
545  * on the status of GPIO-0 or GPIO-1 pins (respectively).
546  */
547 int twl4030_set_gpio_card_detect(int gpio, int enable)
548 {
549         u8 reg = 0;
550         u8 msk = (1 << gpio);
551         int ret = 0;
552
553         /* Only GPIO 0 or 1 can be used for CD feature.. */
554         if (unlikely((gpio >= TWL4030_GPIO_MAX)
555                 || !(gpio_usage_count & (0x1 << gpio))
556                 || (gpio >= TWL4030_GPIO_MAX_CD))) {
557                 return -EPERM;
558         }
559
560         mutex_lock(&gpio_lock);
561         ret = gpio_twl4030_read(REG_GPIO_CTRL);
562         if (ret >= 0) {
563                 if (enable)
564                         reg = (u8) (ret | msk);
565                 else
566                         reg = (u8) (ret & ~msk);
567
568                 ret = gpio_twl4030_write(REG_GPIO_CTRL, reg);
569         }
570         mutex_unlock(&gpio_lock);
571         return ret;
572 }
573 #endif
574
575 /* MODULE FUNCTIONS */
576
577 /*
578  * gpio_unmask_thread() runs as a kernel thread.  It is awakened by the unmask
579  * method for the GPIO interrupts.  It unmasks all of the GPIO interrupts
580  * specified in the gpio_pending_unmask bitmask.  We have to do the unmasking
581  * in a kernel thread rather than directly in the unmask method because of the
582  * need to access the TWL4030 via the I2C bus.  Note that we don't need to be
583  * concerned about race conditions where the request to unmask a GPIO interrupt
584  * has already been cancelled before this thread does the unmasking.  If a GPIO
585  * interrupt is improperly unmasked, then the IRQ handler for it will mask it
586  * when an interrupt occurs.
587  */
588 static int twl4030_gpio_unmask_thread(void *data)
589 {
590         current->flags |= PF_NOFREEZE;
591
592         while (!kthread_should_stop()) {
593                 int irq;
594                 unsigned int gpio_unmask;
595                 unsigned int gpio_trigger;
596
597                 local_irq_disable();
598                 gpio_unmask = gpio_pending_unmask;
599                 gpio_pending_unmask = 0;
600
601                 gpio_trigger = gpio_pending_trigger;
602                 gpio_pending_trigger = 0;
603                 local_irq_enable();
604
605                 for (irq = twl4030_gpio_irq_base; 0 != gpio_unmask;
606                                 gpio_unmask >>= 1, irq++) {
607                         if (gpio_unmask & 0x1)
608                                 twl4030_gpio_unmask(irq);
609                 }
610
611                 for (irq = twl4030_gpio_irq_base;
612                                 gpio_trigger;
613                                 gpio_trigger >>= 1, irq++) {
614                         struct irq_desc *desc;
615                         unsigned type;
616
617                         if (!(gpio_trigger & 0x1))
618                                 continue;
619
620                         desc = irq_desc + irq;
621                         spin_lock_irq(&desc->lock);
622                         type = desc->status & IRQ_TYPE_SENSE_MASK;
623                         spin_unlock_irq(&desc->lock);
624
625                         twl4030_set_gpio_edge_ctrl(irq - twl4030_gpio_irq_base,
626                                         type);
627                 }
628
629                 local_irq_disable();
630                 if (!gpio_pending_unmask && !gpio_pending_trigger)
631                         set_current_state(TASK_INTERRUPTIBLE);
632                 local_irq_enable();
633
634                 schedule();
635         }
636         set_current_state(TASK_RUNNING);
637         return 0;
638 }
639
640 /*
641  * do_twl4030_gpio_irq() is the desc->handle method for each of the twl4030
642  * gpio interrupts.  It executes in kernel thread context.
643  * On entry, cpu interrupts are enabled.
644  */
645 static void do_twl4030_gpio_irq(unsigned int irq, irq_desc_t *desc)
646 {
647         struct irqaction *action;
648         const unsigned int cpu = smp_processor_id();
649
650         desc->status |= IRQ_LEVEL;
651
652         /*
653          * Acknowledge, clear _AND_ disable the interrupt.
654          */
655         twl4030_gpio_mask_and_ack(irq);
656
657         if (!desc->depth) {
658                 kstat_cpu(cpu).irqs[irq]++;
659
660                 action = desc->action;
661                 if (action) {
662                         int ret;
663                         int status = 0;
664                         int retval = 0;
665                         do {
666                                 /* Call the ISR with cpu interrupts enabled. */
667                                 ret = action->handler(irq, action->dev_id);
668                                 if (ret == IRQ_HANDLED)
669                                         status |= action->flags;
670                                 retval |= ret;
671                                 action = action->next;
672                         } while (action);
673
674                         if (retval != IRQ_HANDLED)
675                                 printk(KERN_ERR "ISR for TWL4030 GPIO"
676                                         " irq %d can't handle interrupt\n",
677                                         irq);
678
679                         if (!desc->depth)
680                                 twl4030_gpio_unmask(irq);
681                 }
682         }
683 }
684
685 /*
686  * do_twl4030_gpio_module_irq() is the desc->handle method for the twl4030 gpio
687  * module interrupt.  It executes in kernel thread context.
688  * This is a chained interrupt, so there is no desc->action method for it.
689  * We query the gpio module interrupt controller in the twl4030 to determine
690  * which gpio lines are generating interrupt requests, and then call the
691  * desc->handle method for each gpio that needs service.
692  * On entry, cpu interrupts are disabled.
693  */
694 static void do_twl4030_gpio_module_irq(unsigned int irq, irq_desc_t *desc)
695 {
696         const unsigned int cpu = smp_processor_id();
697
698         desc->status |= IRQ_LEVEL;
699         /*
700         * The desc->handle method would normally call the desc->chip->ack
701         * method here, but we won't bother since our ack method is NULL.
702         */
703         if (!desc->depth) {
704                 int gpio_irq;
705                 unsigned int gpio_isr;
706
707                 kstat_cpu(cpu).irqs[irq]++;
708                 local_irq_enable();
709
710                 mutex_lock(&gpio_lock);
711                 if (gpio_read_isr(&gpio_isr))
712                         gpio_isr = 0;
713                 mutex_unlock(&gpio_lock);
714
715                 for (gpio_irq = twl4030_gpio_irq_base; 0 != gpio_isr;
716                         gpio_isr >>= 1, gpio_irq++) {
717                         if (gpio_isr & 0x1) {
718                                 irq_desc_t *d = irq_desc + gpio_irq;
719                                 d->handle_irq(gpio_irq, d);
720                         }
721                 }
722
723                 local_irq_disable();
724                 /*
725                  * Here is where we should call the unmask method, but again we
726                  * won't bother since it is NULL.
727                  */
728         }
729 }
730
731 /*----------------------------------------------------------------------*/
732
733 static int twl_direction_in(struct gpio_chip *chip, unsigned offset)
734 {
735         return twl4030_set_gpio_direction(offset, 1);
736 }
737
738 static int twl_get(struct gpio_chip *chip, unsigned offset)
739 {
740         int status = twl4030_get_gpio_datain(offset);
741
742         return (status < 0) ? 0 : status;
743 }
744
745 static int twl_direction_out(struct gpio_chip *chip, unsigned offset, int value)
746 {
747         twl4030_set_gpio_dataout(offset, value);
748         return twl4030_set_gpio_direction(offset, 0);
749 }
750
751 static void twl_set(struct gpio_chip *chip, unsigned offset, int value)
752 {
753         twl4030_set_gpio_dataout(offset, value);
754 }
755
756 static struct gpio_chip twl_gpiochip = {
757         .label                  = "twl4030",
758         .owner                  = THIS_MODULE,
759         .direction_input        = twl_direction_in,
760         .get                    = twl_get,
761         .direction_output       = twl_direction_out,
762         .set                    = twl_set,
763         .can_sleep              = 1,
764 };
765
766 /*----------------------------------------------------------------------*/
767
768 static int __devinit gpio_twl4030_pulls(u32 ups, u32 downs)
769 {
770         u8              message[6];
771         unsigned        i, gpio_bit;
772
773         /* For most pins, a pulldown was enabled by default.
774          * We should have data that's specific to this board.
775          */
776         for (gpio_bit = 1, i = 1; i < 6; i++) {
777                 u8              bit_mask;
778                 unsigned        j;
779
780                 for (bit_mask = 0, j = 0; j < 8; j += 2, gpio_bit <<= 1) {
781                         if (ups & gpio_bit)
782                                 bit_mask |= 1 << (j + 1);
783                         else if (downs & gpio_bit)
784                                 bit_mask |= 1 << (j + 0);
785                 }
786                 message[i] = bit_mask;
787         }
788
789         return twl4030_i2c_write(TWL4030_MODULE_GPIO, message,
790                                 REG_GPIOPUPDCTR1, 5);
791 }
792
793 static int gpio_twl4030_remove(struct platform_device *pdev);
794
795 static int __devinit gpio_twl4030_probe(struct platform_device *pdev)
796 {
797         struct twl4030_gpio_platform_data *pdata = pdev->dev.platform_data;
798         int ret;
799         int irq = 0;
800
801         /* All GPIO interrupts are initially masked */
802         gpio_pending_unmask = 0;
803         gpio_imr_shadow = GPIO_32_MASK;
804         ret = gpio_write_imr(gpio_imr_shadow);
805
806         twl4030_gpio_irq_base = pdata->irq_base;
807         twl4030_gpio_irq_end = pdata->irq_end;
808
809         if ((twl4030_gpio_irq_end - twl4030_gpio_irq_base) > 0) {
810                 if (is_module()) {
811                         dev_err(&pdev->dev,
812                                 "can't dispatch IRQs from modules\n");
813                         goto no_irqs;
814                 }
815                 if (twl4030_gpio_irq_end > NR_IRQS) {
816                         dev_err(&pdev->dev,
817                                 "last IRQ is too large: %d\n",
818                                 twl4030_gpio_irq_end);
819                         return -EINVAL;
820                 }
821         } else {
822                 dev_notice(&pdev->dev,
823                         "no IRQs being dispatched\n");
824                 goto no_irqs;
825         }
826
827         if (!ret) {
828                 /*
829                  * Create a kernel thread to handle deferred unmasking of gpio
830                  * interrupts.
831                  */
832                 gpio_unmask_thread = kthread_create(twl4030_gpio_unmask_thread,
833                         NULL, "twl4030 gpio");
834                 if (!gpio_unmask_thread) {
835                         dev_err(&pdev->dev,
836                                 "could not create twl4030 gpio unmask"
837                                 " thread!\n");
838                         ret = -ENOMEM;
839                 }
840         }
841
842         if (!ret) {
843                 /* install an irq handler for each of the gpio interrupts */
844                 for (irq = twl4030_gpio_irq_base; irq < twl4030_gpio_irq_end;
845                                 irq++) {
846                         set_irq_chip_and_handler(irq, &twl4030_gpio_irq_chip,
847                                         do_twl4030_gpio_irq);
848                         activate_irq(irq);
849                 }
850
851                 /* gpio module IRQ */
852                 irq = platform_get_irq(pdev, 0);
853
854                 /*
855                  * Install an irq handler to demultiplex the gpio module
856                  * interrupt.
857                  */
858                 set_irq_chip(irq, &twl4030_gpio_module_irq_chip);
859                 set_irq_chained_handler(irq, do_twl4030_gpio_module_irq);
860                 wake_up_process(gpio_unmask_thread);
861
862                 dev_info(&pdev->dev, "IRQ %d chains IRQs %d..%d\n", irq,
863                         twl4030_gpio_irq_base, twl4030_gpio_irq_end - 1);
864         }
865
866 no_irqs:
867         if (!ret) {
868                 /*
869                  * NOTE:  boards may waste power if they don't set pullups
870                  * and pulldowns correctly ... default for non-ULPI pins is
871                  * pulldown, and some other pins may have external pullups
872                  * or pulldowns.  Careful!
873                  */
874                 ret = gpio_twl4030_pulls(pdata->pullups, pdata->pulldowns);
875                 if (ret)
876                         dev_dbg(&pdev->dev, "pullups %.05x %.05x --> %d\n",
877                                         pdata->pullups, pdata->pulldowns,
878                                         ret);
879
880                 twl_gpiochip.base = pdata->gpio_base;
881                 twl_gpiochip.ngpio = TWL4030_GPIO_MAX;
882                 twl_gpiochip.dev = &pdev->dev;
883
884                 ret = gpiochip_add(&twl_gpiochip);
885                 if (ret < 0) {
886                         dev_err(&pdev->dev,
887                                         "could not register gpiochip, %d\n",
888                                         ret);
889                         twl_gpiochip.ngpio = 0;
890                         gpio_twl4030_remove(pdev);
891                 } else if (pdata->setup) {
892                         int status;
893
894                         status = pdata->setup(&pdev->dev,
895                                         pdata->gpio_base, TWL4030_GPIO_MAX);
896                         if (status)
897                                 dev_dbg(&pdev->dev, "setup --> %d\n", status);
898                 }
899         }
900
901         return ret;
902 }
903
904 static int __devexit gpio_twl4030_remove(struct platform_device *pdev)
905 {
906         struct twl4030_gpio_platform_data *pdata = pdev->dev.platform_data;
907         int status;
908         int irq;
909
910         if (pdata->teardown) {
911                 status = pdata->teardown(&pdev->dev,
912                                 pdata->gpio_base, TWL4030_GPIO_MAX);
913                 if (status) {
914                         dev_dbg(&pdev->dev, "teardown --> %d\n", status);
915                         return status;
916                 }
917         }
918
919         status = gpiochip_remove(&twl_gpiochip);
920         if (status < 0)
921                 return status;
922
923         if (is_module() || (twl4030_gpio_irq_end - twl4030_gpio_irq_base) <= 0)
924                 return 0;
925
926         /* uninstall the gpio demultiplexing interrupt handler */
927         irq = platform_get_irq(pdev, 0);
928         set_irq_handler(irq, NULL);
929
930         /* uninstall the irq handler for each of the gpio interrupts */
931         for (irq = twl4030_gpio_irq_base; irq < twl4030_gpio_irq_end; irq++)
932                 set_irq_handler(irq, NULL);
933
934         /* stop the gpio unmask kernel thread */
935         if (gpio_unmask_thread) {
936                 kthread_stop(gpio_unmask_thread);
937                 gpio_unmask_thread = NULL;
938         }
939
940         return 0;
941 }
942
943 /* Note:  this hardware lives inside an I2C-based multi-function device. */
944 MODULE_ALIAS("platform:twl4030_gpio");
945
946 static struct platform_driver gpio_twl4030_driver = {
947         .driver.name    = "twl4030_gpio",
948         .driver.owner   = THIS_MODULE,
949         .probe          = gpio_twl4030_probe,
950         .remove         = __devexit_p(gpio_twl4030_remove),
951 };
952
953 static int __init gpio_twl4030_init(void)
954 {
955         return platform_driver_register(&gpio_twl4030_driver);
956 }
957 subsys_initcall(gpio_twl4030_init);
958
959 static void __exit gpio_twl4030_exit(void)
960 {
961         platform_driver_unregister(&gpio_twl4030_driver);
962 }
963 module_exit(gpio_twl4030_exit);
964
965 MODULE_AUTHOR("Texas Instruments, Inc.");
966 MODULE_DESCRIPTION("GPIO interface for TWL4030");
967 MODULE_LICENSE("GPL");