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