]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/i2c/chips/twl4030-gpio.c
I2C: TWL4030: checkpatch.pl fixes for twl4030-gpio.c
[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/slab.h>
42
43 #include <asm/arch/irqs.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 EXPORT_SYMBOL(twl4030_request_gpio);
347
348 /*
349  * TWL4030 GPIO free module
350  */
351 int twl4030_free_gpio(int gpio)
352 {
353         int ret = 0;
354
355         if (unlikely(gpio >= TWL4030_GPIO_MAX))
356                 return -EPERM;
357
358         down(&gpio_sem);
359
360         if ((gpio_usage_count & (0x1 << gpio)) == 0)
361                 ret = -EPERM;
362         else
363                 gpio_usage_count &= ~(0x1 << gpio);
364
365         /* Last time usage? - switch off GPIO module */
366         if (!gpio_usage_count)
367                 ret = gpio_twl4030_write(REG_GPIO_CTRL, 0x0);
368
369         up(&gpio_sem);
370         return ret;
371 }
372 EXPORT_SYMBOL(twl4030_free_gpio);
373
374 /*
375  * Set direction for TWL4030 GPIO
376  */
377 int twl4030_set_gpio_direction(int gpio, int is_input)
378 {
379         u8 d_bnk = GET_GPIO_DATA_BANK(gpio);
380         u8 d_msk = MASK_GPIODATADIR_GPIOxDIR(GET_GPIO_DATA_OFF(gpio));
381         u8 reg = 0;
382         u8 base = 0;
383         int ret = 0;
384
385         if (unlikely((gpio >= TWL4030_GPIO_MAX)
386                 || !(gpio_usage_count & (0x1 << gpio))))
387                 return -EPERM;
388
389         base = REG_GPIODATADIR1 + d_bnk;
390
391         down(&gpio_sem);
392         ret = gpio_twl4030_read(base);
393         if (ret >= 0) {
394                 if (is_input)
395                         reg = (u8) ((ret) & ~(d_msk));
396                 else
397                         reg = (u8) ((ret) | (d_msk));
398
399                 ret = gpio_twl4030_write(base, reg);
400         }
401         up(&gpio_sem);
402         return ret;
403 }
404 EXPORT_SYMBOL(twl4030_set_gpio_direction);
405
406 /*
407  * To enable/disable GPIO pin on TWL4030
408  */
409 int twl4030_set_gpio_dataout(int gpio, int enable)
410 {
411         u8 d_bnk = GET_GPIO_DATA_BANK(gpio);
412         u8 d_msk = MASK_GPIODATAOUT_GPIOxOUT(GET_GPIO_DATA_OFF(gpio));
413         u8 base = 0;
414         int ret = 0;
415
416         if (unlikely((gpio >= TWL4030_GPIO_MAX)
417                 || !(gpio_usage_count & (0x1 << gpio))))
418                 return -EPERM;
419
420         if (enable)
421                 base = REG_SETGPIODATAOUT1 + d_bnk;
422         else
423                 base = REG_CLEARGPIODATAOUT1 + d_bnk;
424
425         down(&gpio_sem);
426         ret = gpio_twl4030_write(base, d_msk);
427         up(&gpio_sem);
428         return ret;
429 }
430 EXPORT_SYMBOL(twl4030_set_gpio_dataout);
431
432 /*
433  * To get the status of a GPIO pin on TWL4030
434  */
435 int twl4030_get_gpio_datain(int gpio)
436 {
437         u8 d_bnk = GET_GPIO_DATA_BANK(gpio);
438         u8 d_off = BIT_GPIODATAIN_GPIOxIN(GET_GPIO_DATA_OFF(gpio));
439         u8 base = 0;
440         int ret = 0;
441
442         if (unlikely((gpio >= TWL4030_GPIO_MAX)
443                 || !(gpio_usage_count & (0x1 << gpio))))
444                 return -EPERM;
445
446         base = REG_GPIODATAIN1 + d_bnk;
447         down(&gpio_sem);
448         ret = gpio_twl4030_read(base);
449         up(&gpio_sem);
450         if (ret > 0)
451                 ret = (ret >> d_off) & 0x1;
452
453         return ret;
454 }
455 EXPORT_SYMBOL(twl4030_get_gpio_datain);
456
457 /*
458  * Configure PULL type for a GPIO pin on TWL4030
459  */
460 int twl4030_set_gpio_pull(int gpio, int pull_dircn)
461 {
462         u8 c_bnk = GET_GPIO_CTL_BANK(gpio);
463         u8 c_off = GET_GPIO_CTL_OFF(gpio);
464         u8 c_msk = 0;
465         u8 reg = 0;
466         u8 base = 0;
467         int ret = 0;
468
469         if (unlikely((gpio >= TWL4030_GPIO_MAX) ||
470                 !(gpio_usage_count & (0x1 << gpio))))
471                 return -EPERM;
472
473         base = REG_GPIOPUPDCTR1 + c_bnk;
474         if (pull_dircn == TWL4030_GPIO_PULL_DOWN)
475                 c_msk = MASK_GPIOPUPDCTR1_GPIOxPD(c_off);
476         else if (pull_dircn == TWL4030_GPIO_PULL_UP)
477                 c_msk = MASK_GPIOPUPDCTR1_GPIOxPU(c_off);
478
479         down(&gpio_sem);
480         ret = gpio_twl4030_read(base);
481         if (ret >= 0) {
482                 /* clear the previous up/down values */
483                 reg = (u8) (ret);
484                 reg &= ~(MASK_GPIOPUPDCTR1_GPIOxPU(c_off) |
485                         MASK_GPIOPUPDCTR1_GPIOxPD(c_off));
486                 reg |= c_msk;
487                 ret = gpio_twl4030_write(base, reg);
488         }
489         up(&gpio_sem);
490         return ret;
491 }
492 EXPORT_SYMBOL(twl4030_set_gpio_pull);
493
494 /*
495  * Configure Edge control for a GPIO pin on TWL4030
496  */
497 int twl4030_set_gpio_edge_ctrl(int gpio, int edge)
498 {
499         u8 c_bnk = GET_GPIO_CTL_BANK(gpio);
500         u8 c_off = GET_GPIO_CTL_OFF(gpio);
501         u8 c_msk = 0;
502         u8 reg = 0;
503         u8 base = 0;
504         int ret = 0;
505
506         if (unlikely((gpio >= TWL4030_GPIO_MAX)
507                 || !(gpio_usage_count & (0x1 << gpio))))
508                 return -EPERM;
509
510         base = REG_GPIO_EDR1 + c_bnk;
511
512         if (edge & TWL4030_GPIO_EDGE_RISING)
513                 c_msk |= MASK_GPIO_EDR1_GPIOxRISING(c_off);
514
515         if (edge & TWL4030_GPIO_EDGE_FALLING)
516                 c_msk |= MASK_GPIO_EDR1_GPIOxFALLING(c_off);
517
518         down(&gpio_sem);
519         ret = gpio_twl4030_read(base);
520         if (ret >= 0) {
521                 /* clear the previous rising/falling values */
522                 reg =
523                 (u8) (ret &
524                         ~(MASK_GPIO_EDR1_GPIOxFALLING(c_off) |
525                         MASK_GPIO_EDR1_GPIOxRISING(c_off)));
526                 reg |= c_msk;
527                 ret = gpio_twl4030_write(base, reg);
528         }
529         up(&gpio_sem);
530         return ret;
531 }
532 EXPORT_SYMBOL(twl4030_set_gpio_edge_ctrl);
533
534 /*
535  * Configure debounce timing value for a GPIO pin on TWL4030
536  */
537 int twl4030_set_gpio_debounce(int gpio, int enable)
538 {
539         u8 d_bnk = GET_GPIO_DATA_BANK(gpio);
540         u8 d_msk = MASK_GPIO_DEBEN_GPIOxDEB(GET_GPIO_DATA_OFF(gpio));
541         u8 reg = 0;
542         u8 base = 0;
543         int ret = 0;
544
545         if (unlikely((gpio >= TWL4030_GPIO_MAX)
546                 || !(gpio_usage_count & (0x1 << gpio))))
547                 return -EPERM;
548
549         base = REG_GPIO_DEBEN1 + d_bnk;
550         down(&gpio_sem);
551         ret = gpio_twl4030_read(base);
552         if (ret >= 0) {
553                 if (enable)
554                         reg = (u8) ((ret) | (d_msk));
555                 else
556                         reg = (u8) ((ret) & ~(d_msk));
557
558                 ret = gpio_twl4030_write(base, reg);
559         }
560         up(&gpio_sem);
561         return ret;
562 }
563 EXPORT_SYMBOL(twl4030_set_gpio_debounce);
564
565 /*
566  * Configure Card detect for GPIO pin on TWL4030
567  */
568 int twl4030_set_gpio_card_detect(int gpio, int enable)
569 {
570         u8 reg = 0;
571         u8 msk = (1 << gpio);
572         int ret = 0;
573
574         /* Only GPIO 0 or 1 can be used for CD feature.. */
575         if (unlikely((gpio >= TWL4030_GPIO_MAX)
576                 || !(gpio_usage_count & (0x1 << gpio))
577                 || (gpio >= TWL4030_GPIO_MAX_CD))) {
578                 return -EPERM;
579         }
580
581         down(&gpio_sem);
582         ret = gpio_twl4030_read(REG_GPIO_CTRL);
583         if (ret >= 0) {
584                 if (enable)
585                         reg = (u8) (ret | msk);
586                 else
587                         reg = (u8) (ret & ~msk);
588
589                 ret = gpio_twl4030_write(REG_GPIO_CTRL, reg);
590         }
591         up(&gpio_sem);
592         return (ret);
593 }
594 EXPORT_SYMBOL(twl4030_set_gpio_card_detect);
595
596 /* MODULE FUNCTIONS */
597
598 /*
599  * To configure TWL4030 GPIO module registers
600  */
601 static inline int gpio_twl4030_write(u8 address, u8 data)
602 {
603         int ret = 0;
604
605         ret = twl4030_i2c_write_u8(TWL4030_MODULE_GPIO, data, address);
606         return ret;
607 }
608
609 /*
610  * To read a TWL4030 GPIO module register
611  */
612 static inline int gpio_twl4030_read(u8 address)
613 {
614         u8 data;
615         int ret = 0;
616
617         ret = twl4030_i2c_read_u8(TWL4030_MODULE_GPIO, &data, address);
618         if (ret >= 0)
619                 ret = data;
620         return ret;
621 }
622
623 /*
624  * gpio_unmask_thread() runs as a kernel thread.  It is awakened by the unmask
625  * method for the GPIO interrupts.  It unmasks all of the GPIO interrupts
626  * specified in the gpio_pending_unmask bitmask.  We have to do the unmasking
627  * in a kernel thread rather than directly in the unmask method because of the
628  * need to access the TWL4030 via the I2C bus.  Note that we don't need to be
629  * concerned about race conditions where the request to unmask a GPIO interrupt
630  * has already been cancelled before this thread does the unmasking.  If a GPIO
631  * interrupt is improperly unmasked, then the IRQ handler for it will mask it
632  * when an interrupt occurs.
633  */
634 static int twl4030_gpio_unmask_thread(void *data)
635 {
636         current->flags |= PF_NOFREEZE;
637
638         while (!kthread_should_stop()) {
639                 int irq;
640                 unsigned int gpio_unmask;
641
642                 local_irq_disable();
643                 gpio_unmask = gpio_pending_unmask;
644                 gpio_pending_unmask = 0;
645                 local_irq_enable();
646
647                 for (irq = IH_TWL4030_GPIO_BASE; 0 != gpio_unmask;
648                                 gpio_unmask >>= 1, irq++) {
649                         if (gpio_unmask & 0x1)
650                                 twl4030_gpio_unmask(irq);
651                 }
652
653                 local_irq_disable();
654                 if (!gpio_pending_unmask)
655                         set_current_state(TASK_INTERRUPTIBLE);
656                 local_irq_enable();
657
658                 schedule();
659         }
660         set_current_state(TASK_RUNNING);
661         return 0;
662 }
663
664 /*
665  * do_twl4030_gpio_irq() is the desc->handle method for each of the twl4030
666  * gpio interrupts.  It executes in kernel thread context.
667  * On entry, cpu interrupts are enabled.
668  */
669 static void do_twl4030_gpio_irq(unsigned int irq, irq_desc_t *desc)
670 {
671         struct irqaction *action;
672         const unsigned int cpu = smp_processor_id();
673
674         desc->status |= IRQ_LEVEL;
675
676         /*
677          * Acknowledge, clear _AND_ disable the interrupt.
678          */
679         twl4030_gpio_mask_and_ack(irq);
680
681         if (!desc->depth) {
682                 kstat_cpu(cpu).irqs[irq]++;
683
684                 action = desc->action;
685                 if (action) {
686                         int ret;
687                         int status = 0;
688                         int retval = 0;
689                         do {
690                                 /* Call the ISR with cpu interrupts enabled. */
691                                 ret = action->handler(irq, action->dev_id);
692                                 if (ret == IRQ_HANDLED)
693                                         status |= action->flags;
694                                 retval |= ret;
695                                 action = action->next;
696                         } while (action);
697
698                         if (retval != IRQ_HANDLED)
699                                 printk(KERN_ERR "ISR for TWL4030 GPIO"
700                                         " irq %d can't handle interrupt\n",
701                                         irq);
702
703                         if (!desc->depth)
704                                 twl4030_gpio_unmask(irq);
705                 }
706         }
707 }
708
709 /*
710  * do_twl4030_gpio_module_irq() is the desc->handle method for the twl4030 gpio
711  * module interrupt.  It executes in kernel thread context.
712  * This is a chained interrupt, so there is no desc->action method for it.
713  * We query the gpio module interrupt controller in the twl4030 to determine
714  * which gpio lines are generating interrupt requests, and then call the
715  * desc->handle method for each gpio that needs service.
716  * On entry, cpu interrupts are disabled.
717  */
718 static void do_twl4030_gpio_module_irq(unsigned int irq, irq_desc_t *desc)
719 {
720         const unsigned int cpu = smp_processor_id();
721
722         desc->status |= IRQ_LEVEL;
723         /*
724         * The desc->handle method would normally call the desc->chip->ack
725         * method here, but we won't bother since our ack method is NULL.
726         */
727         if (!desc->depth) {
728                 int gpio_irq;
729                 unsigned int gpio_isr;
730
731                 kstat_cpu(cpu).irqs[irq]++;
732                 local_irq_enable();
733
734                 down(&gpio_sem);
735                 if (gpio_read_isr(&gpio_isr))
736                         gpio_isr = 0;
737                 up(&gpio_sem);
738
739                 for (gpio_irq = IH_TWL4030_GPIO_BASE; 0 != gpio_isr;
740                         gpio_isr >>= 1, gpio_irq++) {
741                         if (gpio_isr & 0x1) {
742                                 irq_desc_t *d = irq_desc + gpio_irq;
743                                 d->handle_irq(gpio_irq, d);
744                         }
745                 }
746
747                 local_irq_disable();
748                 /*
749                  * Here is where we should call the unmask method, but again we
750                  * won't bother since it is NULL.
751                  */
752         }
753 }
754
755 /* TWL4030 Initialization module */
756 static int __init gpio_twl4030_init(void)
757 {
758         int ret;
759         int irq = 0;
760
761         /* init the global locking sem */
762         sema_init(&gpio_sem, 1);
763
764         /* All GPIO interrupts are initially masked */
765         gpio_pending_unmask = 0;
766         gpio_imr_shadow = GPIO_32_MASK;
767         ret = gpio_write_imr(gpio_imr_shadow);
768         if (!ret) {
769                 /*
770                  * Create a kernel thread to handle deferred unmasking of gpio
771                  * interrupts.
772                  */
773                 gpio_unmask_thread = kthread_create(twl4030_gpio_unmask_thread,
774                         NULL, "twl4030 gpio");
775                 if (!gpio_unmask_thread) {
776                         printk(KERN_ERR
777                                 "%s: could not create twl4030 gpio unmask"
778                                 " thread!\n", __func__);
779                         ret = -ENOMEM;
780                 }
781         }
782
783         if (!ret) {
784                 /* install an irq handler for each of the gpio interrupts */
785                 for (irq = IH_TWL4030_GPIO_BASE; irq < IH_TWL4030_GPIO_END;
786                         irq++) {
787                         set_irq_chip(irq, &twl4030_gpio_irq_chip);
788                         set_irq_handler(irq, do_twl4030_gpio_irq);
789                         set_irq_flags(irq, IRQF_VALID);
790                 }
791
792                 /*
793                  * Install an irq handler to demultiplex the gpio module
794                  * interrupt.
795                  */
796                 set_irq_chip(TWL4030_MODIRQ_GPIO,
797                         &twl4030_gpio_module_irq_chip);
798                 set_irq_chained_handler(TWL4030_MODIRQ_GPIO,
799                         do_twl4030_gpio_module_irq);
800         }
801
802         printk(KERN_INFO "TWL4030 GPIO Demux: IRQ Range %d to %d,"
803                 " Initialization %s\n", IH_TWL4030_GPIO_BASE,
804                 IH_TWL4030_GPIO_END, (ret) ? "Failed" : "Success");
805         return ret;
806 }
807
808 /* TWL GPIO exit module */
809 static void __exit gpio_twl4030_exit(void)
810 {
811         int irq;
812
813         /* uninstall the gpio demultiplexing interrupt handler */
814         set_irq_handler(TWL4030_MODIRQ_GPIO, NULL);
815         set_irq_flags(TWL4030_MODIRQ_GPIO, 0);
816
817         /* uninstall the irq handler for each of the gpio interrupts */
818         for (irq = IH_TWL4030_GPIO_BASE; irq < IH_TWL4030_GPIO_END; irq++) {
819                 set_irq_handler(irq, NULL);
820                 set_irq_flags(irq, 0);
821         }
822
823         /* stop the gpio unmask kernel thread */
824         if (gpio_unmask_thread) {
825                 kthread_stop(gpio_unmask_thread);
826                 gpio_unmask_thread = NULL;
827         }
828 }
829
830 module_init(gpio_twl4030_init);
831 module_exit(gpio_twl4030_exit);
832
833 MODULE_AUTHOR("Texas Instruments, Inc.");
834 MODULE_DESCRIPTION("GPIO interface for TWL4030");
835 MODULE_LICENSE("GPL");