]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/i2c/chips/twl4030-gpio.c
I2C: TWL4030: sparse fixes for twl4030 driver
[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 /*
194  * Helper functions to read and write the GPIO ISR and IMR registers as
195  * 32-bit integers. Functions return 0 on success, non-zero otherwise.
196  * The caller must hold a lock on gpio_sem.
197  */
198
199 static int gpio_read_isr(unsigned int *isr)
200 {
201         int ret;
202
203         *isr = 0;
204         ret = twl4030_i2c_read(TWL4030_MODULE_GPIO, (u8 *) isr,
205                         REG_GPIO_ISR1A, 3);
206         le32_to_cpup(isr);
207         *isr &= GPIO_32_MASK;
208
209         return ret;
210 }
211
212 static int gpio_write_isr(unsigned int isr)
213 {
214         isr &= GPIO_32_MASK;
215         /*
216          * The buffer passed to the twl4030_i2c_write() routine must have an
217          * extra byte at the beginning reserved for its internal use.
218          */
219         isr <<= 8;
220         isr = cpu_to_le32(isr);
221         return twl4030_i2c_write(TWL4030_MODULE_GPIO, (u8 *) &isr,
222                                 REG_GPIO_ISR1A, 3);
223 }
224
225 static int gpio_write_imr(unsigned int imr)
226 {
227         imr &= GPIO_32_MASK;
228         /*
229          * The buffer passed to the twl4030_i2c_write() routine must have an
230          * extra byte at the beginning reserved for its internal use.
231          */
232         imr <<= 8;
233         imr = cpu_to_le32(imr);
234         return twl4030_i2c_write(TWL4030_MODULE_GPIO, (u8 *) &imr,
235                                 REG_GPIO_IMR1A, 3);
236 }
237
238 /*
239  * These routines are analagous to the irqchip methods, but they are designed
240  * to be called from thread context with cpu interrupts enabled and with no
241  * locked spinlocks.  We call these routines from our custom IRQ handler
242  * instead of the usual irqchip methods.
243  */
244 static void twl4030_gpio_mask_and_ack(unsigned int irq)
245 {
246         int gpio = irq - IH_TWL4030_GPIO_BASE;
247
248         down(&gpio_sem);
249         /* mask */
250         gpio_imr_shadow |= (1 << gpio);
251         gpio_write_imr(gpio_imr_shadow);
252         /* ack */
253         gpio_write_isr(1 << gpio);
254         up(&gpio_sem);
255 }
256
257 static void twl4030_gpio_unmask(unsigned int irq)
258 {
259         int gpio = irq - IH_TWL4030_GPIO_BASE;
260
261         down(&gpio_sem);
262         gpio_imr_shadow &= ~(1 << gpio);
263         gpio_write_imr(gpio_imr_shadow);
264         up(&gpio_sem);
265 }
266
267 /*
268  * These are the irqchip methods for the TWL4030 GPIO interrupts.
269  * Our IRQ handle method doesn't call these, but they will be called by
270  * other routines such as setup_irq() and enable_irq().  They are called
271  * with cpu interrupts disabled and with a lock on the irq_controller_lock
272  * spinlock.  This complicates matters, because accessing the TWL4030 GPIO
273  * interrupt controller requires I2C bus transactions that can't be initiated
274  * in this context.  Our solution is to defer accessing the interrupt
275  * controller to a kernel thread.  We only need to support the unmask method.
276  */
277
278 static void twl4030_gpio_mask_and_ack_irqchip(unsigned int irq) {}
279 static void twl4030_gpio_mask_irqchip(unsigned int irq) {}
280
281 static void twl4030_gpio_unmask_irqchip(unsigned int irq)
282 {
283         int gpio = irq - IH_TWL4030_GPIO_BASE;
284
285         gpio_pending_unmask |= (1 << gpio);
286         if (gpio_unmask_thread && gpio_unmask_thread->state != TASK_RUNNING)
287                 wake_up_process(gpio_unmask_thread);
288 }
289
290 static struct irq_chip twl4030_gpio_irq_chip = {
291         .name   = "twl4030-gpio",
292         .ack    = twl4030_gpio_mask_and_ack_irqchip,
293         .mask   = twl4030_gpio_mask_irqchip,
294         .unmask = twl4030_gpio_unmask_irqchip,
295 };
296
297 /*
298  * These are the irqchip methods for the TWL4030 PIH GPIO module interrupt.
299  * The PIH module doesn't have interrupt masking capability, so these
300  * methods are NULL.
301  */
302 static void twl4030_gpio_module_ack(unsigned int irq) {}
303 static void twl4030_gpio_module_mask(unsigned int irq) {}
304 static void twl4030_gpio_module_unmask(unsigned int irq) {}
305 static struct irq_chip twl4030_gpio_module_irq_chip = {
306         .ack    = twl4030_gpio_module_ack,
307         .mask   = twl4030_gpio_module_mask,
308         .unmask = twl4030_gpio_module_unmask,
309 };
310
311 /*
312  * To configure TWL4030 GPIO module registers
313  */
314 static inline int gpio_twl4030_write(u8 address, u8 data)
315 {
316         int ret = 0;
317
318         ret = twl4030_i2c_write_u8(TWL4030_MODULE_GPIO, data, address);
319         return ret;
320 }
321
322 /*
323  * To read a TWL4030 GPIO module register
324  */
325 static inline int gpio_twl4030_read(u8 address)
326 {
327         u8 data;
328         int ret = 0;
329
330         ret = twl4030_i2c_read_u8(TWL4030_MODULE_GPIO, &data, address);
331         if (ret >= 0)
332                 ret = data;
333         return ret;
334 }
335
336 /*
337  * twl4030 GPIO request function
338  */
339 int twl4030_request_gpio(int gpio)
340 {
341         int ret = 0;
342
343         if (unlikely(gpio >= TWL4030_GPIO_MAX))
344                 return -EPERM;
345
346         down(&gpio_sem);
347         if (gpio_usage_count & (0x1 << gpio))
348                 ret = -EBUSY;
349         else {
350                 u8 clear_pull[6] = { 0, 0, 0, 0, 0, 0 };
351                 /* First time usage? - switch on GPIO module */
352                 if (!gpio_usage_count) {
353                         ret =
354                         gpio_twl4030_write(REG_GPIO_CTRL,
355                                         MASK_GPIO_CTRL_GPIO_ON);
356                         ret = gpio_twl4030_write(REG_GPIO_SIH_CTRL, 0x00);
357                 }
358                 if (!ret)
359                         gpio_usage_count |= (0x1 << gpio);
360
361                 ret =
362                 twl4030_i2c_write(TWL4030_MODULE_GPIO, clear_pull,
363                                 REG_GPIOPUPDCTR1, 5);
364         }
365         up(&gpio_sem);
366         return ret;
367 }
368 EXPORT_SYMBOL(twl4030_request_gpio);
369
370 /*
371  * TWL4030 GPIO free module
372  */
373 int twl4030_free_gpio(int gpio)
374 {
375         int ret = 0;
376
377         if (unlikely(gpio >= TWL4030_GPIO_MAX))
378                 return -EPERM;
379
380         down(&gpio_sem);
381
382         if ((gpio_usage_count & (0x1 << gpio)) == 0)
383                 ret = -EPERM;
384         else
385                 gpio_usage_count &= ~(0x1 << gpio);
386
387         /* Last time usage? - switch off GPIO module */
388         if (!gpio_usage_count)
389                 ret = gpio_twl4030_write(REG_GPIO_CTRL, 0x0);
390
391         up(&gpio_sem);
392         return ret;
393 }
394 EXPORT_SYMBOL(twl4030_free_gpio);
395
396 /*
397  * Set direction for TWL4030 GPIO
398  */
399 int twl4030_set_gpio_direction(int gpio, int is_input)
400 {
401         u8 d_bnk = GET_GPIO_DATA_BANK(gpio);
402         u8 d_msk = MASK_GPIODATADIR_GPIOxDIR(GET_GPIO_DATA_OFF(gpio));
403         u8 reg = 0;
404         u8 base = 0;
405         int ret = 0;
406
407         if (unlikely((gpio >= TWL4030_GPIO_MAX)
408                 || !(gpio_usage_count & (0x1 << gpio))))
409                 return -EPERM;
410
411         base = REG_GPIODATADIR1 + d_bnk;
412
413         down(&gpio_sem);
414         ret = gpio_twl4030_read(base);
415         if (ret >= 0) {
416                 if (is_input)
417                         reg = (u8) ((ret) & ~(d_msk));
418                 else
419                         reg = (u8) ((ret) | (d_msk));
420
421                 ret = gpio_twl4030_write(base, reg);
422         }
423         up(&gpio_sem);
424         return ret;
425 }
426 EXPORT_SYMBOL(twl4030_set_gpio_direction);
427
428 /*
429  * To enable/disable GPIO pin on TWL4030
430  */
431 int twl4030_set_gpio_dataout(int gpio, int enable)
432 {
433         u8 d_bnk = GET_GPIO_DATA_BANK(gpio);
434         u8 d_msk = MASK_GPIODATAOUT_GPIOxOUT(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         if (enable)
443                 base = REG_SETGPIODATAOUT1 + d_bnk;
444         else
445                 base = REG_CLEARGPIODATAOUT1 + d_bnk;
446
447         down(&gpio_sem);
448         ret = gpio_twl4030_write(base, d_msk);
449         up(&gpio_sem);
450         return ret;
451 }
452 EXPORT_SYMBOL(twl4030_set_gpio_dataout);
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         down(&gpio_sem);
470         ret = gpio_twl4030_read(base);
471         up(&gpio_sem);
472         if (ret > 0)
473                 ret = (ret >> d_off) & 0x1;
474
475         return ret;
476 }
477 EXPORT_SYMBOL(twl4030_get_gpio_datain);
478
479 /*
480  * Configure PULL type for a GPIO pin on TWL4030
481  */
482 int twl4030_set_gpio_pull(int gpio, int pull_dircn)
483 {
484         u8 c_bnk = GET_GPIO_CTL_BANK(gpio);
485         u8 c_off = GET_GPIO_CTL_OFF(gpio);
486         u8 c_msk = 0;
487         u8 reg = 0;
488         u8 base = 0;
489         int ret = 0;
490
491         if (unlikely((gpio >= TWL4030_GPIO_MAX) ||
492                 !(gpio_usage_count & (0x1 << gpio))))
493                 return -EPERM;
494
495         base = REG_GPIOPUPDCTR1 + c_bnk;
496         if (pull_dircn == TWL4030_GPIO_PULL_DOWN)
497                 c_msk = MASK_GPIOPUPDCTR1_GPIOxPD(c_off);
498         else if (pull_dircn == TWL4030_GPIO_PULL_UP)
499                 c_msk = MASK_GPIOPUPDCTR1_GPIOxPU(c_off);
500
501         down(&gpio_sem);
502         ret = gpio_twl4030_read(base);
503         if (ret >= 0) {
504                 /* clear the previous up/down values */
505                 reg = (u8) (ret);
506                 reg &= ~(MASK_GPIOPUPDCTR1_GPIOxPU(c_off) |
507                         MASK_GPIOPUPDCTR1_GPIOxPD(c_off));
508                 reg |= c_msk;
509                 ret = gpio_twl4030_write(base, reg);
510         }
511         up(&gpio_sem);
512         return ret;
513 }
514 EXPORT_SYMBOL(twl4030_set_gpio_pull);
515
516 /*
517  * Configure Edge control for a GPIO pin on TWL4030
518  */
519 int twl4030_set_gpio_edge_ctrl(int gpio, int edge)
520 {
521         u8 c_bnk = GET_GPIO_CTL_BANK(gpio);
522         u8 c_off = GET_GPIO_CTL_OFF(gpio);
523         u8 c_msk = 0;
524         u8 reg = 0;
525         u8 base = 0;
526         int ret = 0;
527
528         if (unlikely((gpio >= TWL4030_GPIO_MAX)
529                 || !(gpio_usage_count & (0x1 << gpio))))
530                 return -EPERM;
531
532         base = REG_GPIO_EDR1 + c_bnk;
533
534         if (edge & TWL4030_GPIO_EDGE_RISING)
535                 c_msk |= MASK_GPIO_EDR1_GPIOxRISING(c_off);
536
537         if (edge & TWL4030_GPIO_EDGE_FALLING)
538                 c_msk |= MASK_GPIO_EDR1_GPIOxFALLING(c_off);
539
540         down(&gpio_sem);
541         ret = gpio_twl4030_read(base);
542         if (ret >= 0) {
543                 /* clear the previous rising/falling values */
544                 reg =
545                 (u8) (ret &
546                         ~(MASK_GPIO_EDR1_GPIOxFALLING(c_off) |
547                         MASK_GPIO_EDR1_GPIOxRISING(c_off)));
548                 reg |= c_msk;
549                 ret = gpio_twl4030_write(base, reg);
550         }
551         up(&gpio_sem);
552         return ret;
553 }
554 EXPORT_SYMBOL(twl4030_set_gpio_edge_ctrl);
555
556 /*
557  * Configure debounce timing value for a GPIO pin on TWL4030
558  */
559 int twl4030_set_gpio_debounce(int gpio, int enable)
560 {
561         u8 d_bnk = GET_GPIO_DATA_BANK(gpio);
562         u8 d_msk = MASK_GPIO_DEBEN_GPIOxDEB(GET_GPIO_DATA_OFF(gpio));
563         u8 reg = 0;
564         u8 base = 0;
565         int ret = 0;
566
567         if (unlikely((gpio >= TWL4030_GPIO_MAX)
568                 || !(gpio_usage_count & (0x1 << gpio))))
569                 return -EPERM;
570
571         base = REG_GPIO_DEBEN1 + d_bnk;
572         down(&gpio_sem);
573         ret = gpio_twl4030_read(base);
574         if (ret >= 0) {
575                 if (enable)
576                         reg = (u8) ((ret) | (d_msk));
577                 else
578                         reg = (u8) ((ret) & ~(d_msk));
579
580                 ret = gpio_twl4030_write(base, reg);
581         }
582         up(&gpio_sem);
583         return ret;
584 }
585 EXPORT_SYMBOL(twl4030_set_gpio_debounce);
586
587 /*
588  * Configure Card detect for GPIO pin on TWL4030
589  */
590 int twl4030_set_gpio_card_detect(int gpio, int enable)
591 {
592         u8 reg = 0;
593         u8 msk = (1 << gpio);
594         int ret = 0;
595
596         /* Only GPIO 0 or 1 can be used for CD feature.. */
597         if (unlikely((gpio >= TWL4030_GPIO_MAX)
598                 || !(gpio_usage_count & (0x1 << gpio))
599                 || (gpio >= TWL4030_GPIO_MAX_CD))) {
600                 return -EPERM;
601         }
602
603         down(&gpio_sem);
604         ret = gpio_twl4030_read(REG_GPIO_CTRL);
605         if (ret >= 0) {
606                 if (enable)
607                         reg = (u8) (ret | msk);
608                 else
609                         reg = (u8) (ret & ~msk);
610
611                 ret = gpio_twl4030_write(REG_GPIO_CTRL, reg);
612         }
613         up(&gpio_sem);
614         return (ret);
615 }
616 EXPORT_SYMBOL(twl4030_set_gpio_card_detect);
617
618 /* MODULE FUNCTIONS */
619
620 /*
621  * gpio_unmask_thread() runs as a kernel thread.  It is awakened by the unmask
622  * method for the GPIO interrupts.  It unmasks all of the GPIO interrupts
623  * specified in the gpio_pending_unmask bitmask.  We have to do the unmasking
624  * in a kernel thread rather than directly in the unmask method because of the
625  * need to access the TWL4030 via the I2C bus.  Note that we don't need to be
626  * concerned about race conditions where the request to unmask a GPIO interrupt
627  * has already been cancelled before this thread does the unmasking.  If a GPIO
628  * interrupt is improperly unmasked, then the IRQ handler for it will mask it
629  * when an interrupt occurs.
630  */
631 static int twl4030_gpio_unmask_thread(void *data)
632 {
633         current->flags |= PF_NOFREEZE;
634
635         while (!kthread_should_stop()) {
636                 int irq;
637                 unsigned int gpio_unmask;
638
639                 local_irq_disable();
640                 gpio_unmask = gpio_pending_unmask;
641                 gpio_pending_unmask = 0;
642                 local_irq_enable();
643
644                 for (irq = IH_TWL4030_GPIO_BASE; 0 != gpio_unmask;
645                                 gpio_unmask >>= 1, irq++) {
646                         if (gpio_unmask & 0x1)
647                                 twl4030_gpio_unmask(irq);
648                 }
649
650                 local_irq_disable();
651                 if (!gpio_pending_unmask)
652                         set_current_state(TASK_INTERRUPTIBLE);
653                 local_irq_enable();
654
655                 schedule();
656         }
657         set_current_state(TASK_RUNNING);
658         return 0;
659 }
660
661 /*
662  * do_twl4030_gpio_irq() is the desc->handle method for each of the twl4030
663  * gpio interrupts.  It executes in kernel thread context.
664  * On entry, cpu interrupts are enabled.
665  */
666 static void do_twl4030_gpio_irq(unsigned int irq, irq_desc_t *desc)
667 {
668         struct irqaction *action;
669         const unsigned int cpu = smp_processor_id();
670
671         desc->status |= IRQ_LEVEL;
672
673         /*
674          * Acknowledge, clear _AND_ disable the interrupt.
675          */
676         twl4030_gpio_mask_and_ack(irq);
677
678         if (!desc->depth) {
679                 kstat_cpu(cpu).irqs[irq]++;
680
681                 action = desc->action;
682                 if (action) {
683                         int ret;
684                         int status = 0;
685                         int retval = 0;
686                         do {
687                                 /* Call the ISR with cpu interrupts enabled. */
688                                 ret = action->handler(irq, action->dev_id);
689                                 if (ret == IRQ_HANDLED)
690                                         status |= action->flags;
691                                 retval |= ret;
692                                 action = action->next;
693                         } while (action);
694
695                         if (retval != IRQ_HANDLED)
696                                 printk(KERN_ERR "ISR for TWL4030 GPIO"
697                                         " irq %d can't handle interrupt\n",
698                                         irq);
699
700                         if (!desc->depth)
701                                 twl4030_gpio_unmask(irq);
702                 }
703         }
704 }
705
706 /*
707  * do_twl4030_gpio_module_irq() is the desc->handle method for the twl4030 gpio
708  * module interrupt.  It executes in kernel thread context.
709  * This is a chained interrupt, so there is no desc->action method for it.
710  * We query the gpio module interrupt controller in the twl4030 to determine
711  * which gpio lines are generating interrupt requests, and then call the
712  * desc->handle method for each gpio that needs service.
713  * On entry, cpu interrupts are disabled.
714  */
715 static void do_twl4030_gpio_module_irq(unsigned int irq, irq_desc_t *desc)
716 {
717         const unsigned int cpu = smp_processor_id();
718
719         desc->status |= IRQ_LEVEL;
720         /*
721         * The desc->handle method would normally call the desc->chip->ack
722         * method here, but we won't bother since our ack method is NULL.
723         */
724         if (!desc->depth) {
725                 int gpio_irq;
726                 unsigned int gpio_isr;
727
728                 kstat_cpu(cpu).irqs[irq]++;
729                 local_irq_enable();
730
731                 down(&gpio_sem);
732                 if (gpio_read_isr(&gpio_isr))
733                         gpio_isr = 0;
734                 up(&gpio_sem);
735
736                 for (gpio_irq = IH_TWL4030_GPIO_BASE; 0 != gpio_isr;
737                         gpio_isr >>= 1, gpio_irq++) {
738                         if (gpio_isr & 0x1) {
739                                 irq_desc_t *d = irq_desc + gpio_irq;
740                                 d->handle_irq(gpio_irq, d);
741                         }
742                 }
743
744                 local_irq_disable();
745                 /*
746                  * Here is where we should call the unmask method, but again we
747                  * won't bother since it is NULL.
748                  */
749         }
750 }
751
752 /* TWL4030 Initialization module */
753 static int __init gpio_twl4030_init(void)
754 {
755         int ret;
756         int irq = 0;
757
758         /* init the global locking sem */
759         sema_init(&gpio_sem, 1);
760
761         /* All GPIO interrupts are initially masked */
762         gpio_pending_unmask = 0;
763         gpio_imr_shadow = GPIO_32_MASK;
764         ret = gpio_write_imr(gpio_imr_shadow);
765         if (!ret) {
766                 /*
767                  * Create a kernel thread to handle deferred unmasking of gpio
768                  * interrupts.
769                  */
770                 gpio_unmask_thread = kthread_create(twl4030_gpio_unmask_thread,
771                         NULL, "twl4030 gpio");
772                 if (!gpio_unmask_thread) {
773                         printk(KERN_ERR
774                                 "%s: could not create twl4030 gpio unmask"
775                                 " thread!\n", __func__);
776                         ret = -ENOMEM;
777                 }
778         }
779
780         if (!ret) {
781                 /* install an irq handler for each of the gpio interrupts */
782                 for (irq = IH_TWL4030_GPIO_BASE; irq < IH_TWL4030_GPIO_END;
783                         irq++) {
784                         set_irq_chip(irq, &twl4030_gpio_irq_chip);
785                         set_irq_handler(irq, do_twl4030_gpio_irq);
786                         set_irq_flags(irq, IRQF_VALID);
787                 }
788
789                 /*
790                  * Install an irq handler to demultiplex the gpio module
791                  * interrupt.
792                  */
793                 set_irq_chip(TWL4030_MODIRQ_GPIO,
794                         &twl4030_gpio_module_irq_chip);
795                 set_irq_chained_handler(TWL4030_MODIRQ_GPIO,
796                         do_twl4030_gpio_module_irq);
797         }
798
799         printk(KERN_INFO "TWL4030 GPIO Demux: IRQ Range %d to %d,"
800                 " Initialization %s\n", IH_TWL4030_GPIO_BASE,
801                 IH_TWL4030_GPIO_END, (ret) ? "Failed" : "Success");
802         return ret;
803 }
804
805 /* TWL GPIO exit module */
806 static void __exit gpio_twl4030_exit(void)
807 {
808         int irq;
809
810         /* uninstall the gpio demultiplexing interrupt handler */
811         set_irq_handler(TWL4030_MODIRQ_GPIO, NULL);
812         set_irq_flags(TWL4030_MODIRQ_GPIO, 0);
813
814         /* uninstall the irq handler for each of the gpio interrupts */
815         for (irq = IH_TWL4030_GPIO_BASE; irq < IH_TWL4030_GPIO_END; irq++) {
816                 set_irq_handler(irq, NULL);
817                 set_irq_flags(irq, 0);
818         }
819
820         /* stop the gpio unmask kernel thread */
821         if (gpio_unmask_thread) {
822                 kthread_stop(gpio_unmask_thread);
823                 gpio_unmask_thread = NULL;
824         }
825 }
826
827 module_init(gpio_twl4030_init);
828 module_exit(gpio_twl4030_exit);
829
830 MODULE_AUTHOR("Texas Instruments, Inc.");
831 MODULE_DESCRIPTION("GPIO interface for TWL4030");
832 MODULE_LICENSE("GPL");