]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/i2c/chips/twl4030-core.c
i2c: move twl4030_keypad to new style registration
[linux-2.6-omap-h63xx.git] / drivers / i2c / chips / twl4030-core.c
1 /*
2  * twl4030_core.c - driver for TWL4030/TPS659x0 PM and audio CODEC devices
3  *
4  * Copyright (C) 2005-2006 Texas Instruments, Inc.
5  *
6  * Modifications to defer interrupt handling to a kernel thread:
7  * Copyright (C) 2006 MontaVista Software, Inc.
8  *
9  * Based on tlv320aic23.c:
10  * Copyright (c) by Kai Svahn <kai.svahn@nokia.com>
11  *
12  * Code cleanup and modifications to IRQ handler.
13  * by syed khasim <x0khasim@ti.com>
14  *
15  * This program is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation; either version 2 of the License, or
18  * (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
28  */
29
30 #include <linux/kernel_stat.h>
31 #include <linux/init.h>
32 #include <linux/mutex.h>
33 #include <linux/interrupt.h>
34 #include <linux/irq.h>
35 #include <linux/random.h>
36 #include <linux/kthread.h>
37 #include <linux/platform_device.h>
38 #include <linux/clk.h>
39
40 #include <linux/i2c.h>
41 #include <linux/i2c/twl4030.h>
42 #include <linux/i2c/twl4030-gpio.h>
43 #include <linux/i2c/twl4030-madc.h>
44 #include <linux/i2c/twl4030-pwrirq.h>
45
46 #define DRIVER_NAME                     "twl4030"
47
48 #if defined(CONFIG_RTC_DRV_TWL4030) || defined(CONFIG_RTC_DRV_TWL4030_MODULE)
49 #define twl_has_rtc()   true
50 #else
51 #define twl_has_rtc()   false
52 #endif
53
54 #if defined(CONFIG_KEYBOARD_TWL4030) || defined(CONFIG_KEYBOARD_TWL4030_MODULE)
55 #define twl_has_keypad()        true
56 #else
57 #define twl_has_keypad()        false
58 #endif
59
60 /* Primary Interrupt Handler on TWL4030 Registers */
61
62 /* Register Definitions */
63
64 #define REG_PIH_ISR_P1                  (0x1)
65 #define REG_PIH_ISR_P2                  (0x2)
66 #define REG_PIH_SIR                     (0x3)
67
68 /* Triton Core internal information (BEGIN) */
69
70 /* Last - for index max*/
71 #define TWL4030_MODULE_LAST             TWL4030_MODULE_SECURED_REG
72
73 #define TWL4030_NUM_SLAVES              4
74
75 /* Slave address */
76 #define TWL4030_SLAVENUM_NUM0           0x00
77 #define TWL4030_SLAVENUM_NUM1           0x01
78 #define TWL4030_SLAVENUM_NUM2           0x02
79 #define TWL4030_SLAVENUM_NUM3           0x03
80
81 /* Base Address defns */
82 /* USB ID */
83 #define TWL4030_BASEADD_USB             0x0000
84 /* AUD ID */
85 #define TWL4030_BASEADD_AUDIO_VOICE     0x0000
86 #define TWL4030_BASEADD_GPIO            0x0098
87
88 #define TWL4030_BASEADD_INTBR           0x0085
89 #define TWL4030_BASEADD_PIH             0x0080
90 #define TWL4030_BASEADD_TEST            0x004C
91 /* AUX ID */
92 #define TWL4030_BASEADD_INTERRUPTS      0x00B9
93 #define TWL4030_BASEADD_LED             0x00EE
94 #define TWL4030_BASEADD_MADC            0x0000
95 #define TWL4030_BASEADD_MAIN_CHARGE     0x0074
96 #define TWL4030_BASEADD_PRECHARGE       0x00AA
97 #define TWL4030_BASEADD_PWM0            0x00F8
98 #define TWL4030_BASEADD_PWM1            0x00FB
99 #define TWL4030_BASEADD_PWMA            0x00EF
100 #define TWL4030_BASEADD_PWMB            0x00F1
101 #define TWL4030_BASEADD_KEYPAD          0x00D2
102 /* POWER ID */
103 #define TWL4030_BASEADD_BACKUP          0x0014
104 #define TWL4030_BASEADD_INT             0x002E
105 #define TWL4030_BASEADD_PM_MASTER       0x0036
106 #define TWL4030_BASEADD_PM_RECEIVER     0x005B
107 #define TWL4030_BASEADD_RTC             0x001C
108 #define TWL4030_BASEADD_SECURED_REG     0x0000
109
110 /* TWL4030 BCI registers */
111 #define TWL4030_INTERRUPTS_BCIIMR1A     0x2
112 #define TWL4030_INTERRUPTS_BCIIMR2A     0x3
113 #define TWL4030_INTERRUPTS_BCIIMR1B     0x6
114 #define TWL4030_INTERRUPTS_BCIIMR2B     0x7
115 #define TWL4030_INTERRUPTS_BCIISR1A     0x0
116 #define TWL4030_INTERRUPTS_BCIISR2A     0x1
117 #define TWL4030_INTERRUPTS_BCIISR1B     0x4
118 #define TWL4030_INTERRUPTS_BCIISR2B     0x5
119
120 /* TWL4030 keypad registers */
121 #define TWL4030_KEYPAD_KEYP_IMR1        0x12
122 #define TWL4030_KEYPAD_KEYP_IMR2        0x14
123 #define TWL4030_KEYPAD_KEYP_ISR1        0x11
124 #define TWL4030_KEYPAD_KEYP_ISR2        0x13
125
126
127 /* Triton Core internal information (END) */
128
129 /* Few power values */
130 #define R_CFG_BOOT                      0x05
131 #define R_PROTECT_KEY                   0x0E
132
133 /* access control */
134 #define KEY_UNLOCK1                     0xce
135 #define KEY_UNLOCK2                     0xec
136 #define KEY_LOCK                        0x00
137
138 #define HFCLK_FREQ_19p2_MHZ             (1 << 0)
139 #define HFCLK_FREQ_26_MHZ               (2 << 0)
140 #define HFCLK_FREQ_38p4_MHZ             (3 << 0)
141 #define HIGH_PERF_SQ                    (1 << 3)
142
143 /* SIH_CTRL registers that aren't defined elsewhere */
144 #define TWL4030_INTERRUPTS_BCISIHCTRL   0x0d
145 #define TWL4030_MADC_MADC_SIH_CTRL      0x67
146 #define TWL4030_KEYPAD_KEYP_SIH_CTRL    0x17
147
148 #define TWL4030_SIH_CTRL_COR_MASK       (1 << 2)
149
150 /**
151  * struct twl4030_mod_iregs - TWL module IMR/ISR regs to mask/clear at init
152  * @mod_no: TWL4030 module number (e.g., TWL4030_MODULE_GPIO)
153  * @sih_ctrl: address of module SIH_CTRL register
154  * @reg_cnt: number of IMR/ISR regs
155  * @imrs: pointer to array of TWL module interrupt mask register indices
156  * @isrs: pointer to array of TWL module interrupt status register indices
157  *
158  * Ties together TWL4030 modules and lists of IMR/ISR registers to mask/clear
159  * during twl_init_irq().
160  */
161 struct twl4030_mod_iregs {
162         const u8 mod_no;
163         const u8 sih_ctrl;
164         const u8 reg_cnt;
165         const u8 *imrs;
166         const u8 *isrs;
167 };
168
169 /* TWL4030 INT module interrupt mask registers */
170 static const u8 __initconst twl4030_int_imr_regs[] = {
171         TWL4030_INT_PWR_IMR1,
172         TWL4030_INT_PWR_IMR2,
173 };
174
175 /* TWL4030 INT module interrupt status registers */
176 static const u8 __initconst twl4030_int_isr_regs[] = {
177         TWL4030_INT_PWR_ISR1,
178         TWL4030_INT_PWR_ISR2,
179 };
180
181 /* TWL4030 INTERRUPTS module interrupt mask registers */
182 static const u8 __initconst twl4030_interrupts_imr_regs[] = {
183         TWL4030_INTERRUPTS_BCIIMR1A,
184         TWL4030_INTERRUPTS_BCIIMR1B,
185         TWL4030_INTERRUPTS_BCIIMR2A,
186         TWL4030_INTERRUPTS_BCIIMR2B,
187 };
188
189 /* TWL4030 INTERRUPTS module interrupt status registers */
190 static const u8 __initconst twl4030_interrupts_isr_regs[] = {
191         TWL4030_INTERRUPTS_BCIISR1A,
192         TWL4030_INTERRUPTS_BCIISR1B,
193         TWL4030_INTERRUPTS_BCIISR2A,
194         TWL4030_INTERRUPTS_BCIISR2B,
195 };
196
197 /* TWL4030 MADC module interrupt mask registers */
198 static const u8 __initconst twl4030_madc_imr_regs[] = {
199         TWL4030_MADC_IMR1,
200         TWL4030_MADC_IMR2,
201 };
202
203 /* TWL4030 MADC module interrupt status registers */
204 static const u8 __initconst twl4030_madc_isr_regs[] = {
205         TWL4030_MADC_ISR1,
206         TWL4030_MADC_ISR2,
207 };
208
209 /* TWL4030 keypad module interrupt mask registers */
210 static const u8 __initconst twl4030_keypad_imr_regs[] = {
211         TWL4030_KEYPAD_KEYP_IMR1,
212         TWL4030_KEYPAD_KEYP_IMR2,
213 };
214
215 /* TWL4030 keypad module interrupt status registers */
216 static const u8 __initconst twl4030_keypad_isr_regs[] = {
217         TWL4030_KEYPAD_KEYP_ISR1,
218         TWL4030_KEYPAD_KEYP_ISR2,
219 };
220
221 /* TWL4030 GPIO module interrupt mask registers */
222 static const u8 __initconst twl4030_gpio_imr_regs[] = {
223         REG_GPIO_IMR1A,
224         REG_GPIO_IMR1B,
225         REG_GPIO_IMR2A,
226         REG_GPIO_IMR2B,
227         REG_GPIO_IMR3A,
228         REG_GPIO_IMR3B,
229 };
230
231 /* TWL4030 GPIO module interrupt status registers */
232 static const u8 __initconst twl4030_gpio_isr_regs[] = {
233         REG_GPIO_ISR1A,
234         REG_GPIO_ISR1B,
235         REG_GPIO_ISR2A,
236         REG_GPIO_ISR2B,
237         REG_GPIO_ISR3A,
238         REG_GPIO_ISR3B,
239 };
240
241 /* TWL4030 modules that have IMR/ISR registers that must be masked/cleared */
242 static const struct twl4030_mod_iregs __initconst twl4030_mod_regs[] = {
243         {
244                 .mod_no   = TWL4030_MODULE_INT,
245                 .sih_ctrl = TWL4030_INT_PWR_SIH_CTRL,
246                 .reg_cnt  = ARRAY_SIZE(twl4030_int_imr_regs),
247                 .imrs     = twl4030_int_imr_regs,
248                 .isrs     = twl4030_int_isr_regs,
249         },
250         {
251                 .mod_no   = TWL4030_MODULE_INTERRUPTS,
252                 .sih_ctrl = TWL4030_INTERRUPTS_BCISIHCTRL,
253                 .reg_cnt  = ARRAY_SIZE(twl4030_interrupts_imr_regs),
254                 .imrs     = twl4030_interrupts_imr_regs,
255                 .isrs     = twl4030_interrupts_isr_regs,
256         },
257         {
258                 .mod_no   = TWL4030_MODULE_MADC,
259                 .sih_ctrl = TWL4030_MADC_MADC_SIH_CTRL,
260                 .reg_cnt  = ARRAY_SIZE(twl4030_madc_imr_regs),
261                 .imrs     = twl4030_madc_imr_regs,
262                 .isrs     = twl4030_madc_isr_regs,
263         },
264         {
265                 .mod_no   = TWL4030_MODULE_KEYPAD,
266                 .sih_ctrl = TWL4030_KEYPAD_KEYP_SIH_CTRL,
267                 .reg_cnt  = ARRAY_SIZE(twl4030_keypad_imr_regs),
268                 .imrs     = twl4030_keypad_imr_regs,
269                 .isrs     = twl4030_keypad_isr_regs,
270         },
271         {
272                 .mod_no   = TWL4030_MODULE_GPIO,
273                 .sih_ctrl = REG_GPIO_SIH_CTRL,
274                 .reg_cnt  = ARRAY_SIZE(twl4030_gpio_imr_regs),
275                 .imrs     = twl4030_gpio_imr_regs,
276                 .isrs     = twl4030_gpio_isr_regs,
277         },
278 };
279
280
281 /* Data Structures */
282 /* To have info on T2 IRQ substem activated or not */
283 static struct completion irq_event;
284
285 /* Structure to define on TWL4030 Slave ID */
286 struct twl4030_client {
287         struct i2c_client *client;
288         u8 address;
289         bool inuse;
290
291         /* max numb of i2c_msg required is for read =2 */
292         struct i2c_msg xfer_msg[2];
293
294         /* To lock access to xfer_msg */
295         struct mutex xfer_lock;
296 };
297
298 /* Module Mapping */
299 struct twl4030mapping {
300         unsigned char sid;      /* Slave ID */
301         unsigned char base;     /* base address */
302 };
303
304 /* mapping the module id to slave id and base address */
305 static struct twl4030mapping twl4030_map[TWL4030_MODULE_LAST + 1] = {
306         { TWL4030_SLAVENUM_NUM0, TWL4030_BASEADD_USB },
307         { TWL4030_SLAVENUM_NUM1, TWL4030_BASEADD_AUDIO_VOICE },
308         { TWL4030_SLAVENUM_NUM1, TWL4030_BASEADD_GPIO },
309         { TWL4030_SLAVENUM_NUM1, TWL4030_BASEADD_INTBR },
310         { TWL4030_SLAVENUM_NUM1, TWL4030_BASEADD_PIH },
311         { TWL4030_SLAVENUM_NUM1, TWL4030_BASEADD_TEST },
312         { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_KEYPAD },
313         { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_MADC },
314         { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_INTERRUPTS },
315         { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_LED },
316         { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_MAIN_CHARGE },
317         { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_PRECHARGE },
318         { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_PWM0 },
319         { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_PWM1 },
320         { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_PWMA },
321         { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_PWMB },
322         { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_BACKUP },
323         { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_INT },
324         { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_PM_MASTER },
325         { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_PM_RECEIVER },
326         { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_RTC },
327         { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_SECURED_REG },
328 };
329
330 static struct twl4030_client twl4030_modules[TWL4030_NUM_SLAVES];
331
332 /*
333  * TWL4030 doesn't have PIH mask, hence dummy function for mask
334  * and unmask.
335  */
336
337 static void twl4030_i2c_ackirq(unsigned int irq)
338 {
339 }
340
341 static void twl4030_i2c_disableint(unsigned int irq)
342 {
343 }
344
345 static void twl4030_i2c_enableint(unsigned int irq)
346 {
347 }
348
349 /* information for processing in the Work Item */
350 static struct irq_chip twl4030_irq_chip = {
351         .name   = "twl4030",
352         .ack    = twl4030_i2c_ackirq,
353         .mask   = twl4030_i2c_disableint,
354         .unmask = twl4030_i2c_enableint,
355 };
356
357 /* Global Functions */
358
359 /**
360  * twl4030_i2c_write - Writes a n bit register in TWL4030
361  * @mod_no: module number
362  * @value: an array of num_bytes+1 containing data to write
363  * @reg: register address (just offset will do)
364  * @num_bytes: number of bytes to transfer
365  *
366  * IMPORTANT: for 'value' parameter: Allocate value num_bytes+1 and
367  * valid data starts at Offset 1.
368  *
369  * Returns the result of operation - 0 is success
370  */
371 int twl4030_i2c_write(u8 mod_no, u8 *value, u8 reg, u8 num_bytes)
372 {
373         int ret;
374         int sid;
375         struct twl4030_client *twl;
376         struct i2c_msg *msg;
377
378         if (unlikely(mod_no > TWL4030_MODULE_LAST)) {
379                 pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
380                 return -EPERM;
381         }
382         sid = twl4030_map[mod_no].sid;
383         twl = &twl4030_modules[sid];
384
385         if (unlikely(!twl->inuse)) {
386                 pr_err("%s: client %d is not initialized\n", DRIVER_NAME, sid);
387                 return -EPERM;
388         }
389         mutex_lock(&twl->xfer_lock);
390         /*
391          * [MSG1]: fill the register address data
392          * fill the data Tx buffer
393          */
394         msg = &twl->xfer_msg[0];
395         msg->addr = twl->address;
396         msg->len = num_bytes + 1;
397         msg->flags = 0;
398         msg->buf = value;
399         /* over write the first byte of buffer with the register address */
400         *value = twl4030_map[mod_no].base + reg;
401         ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 1);
402         mutex_unlock(&twl->xfer_lock);
403
404         /* i2cTransfer returns num messages.translate it pls.. */
405         if (ret >= 0)
406                 ret = 0;
407         return ret;
408 }
409 EXPORT_SYMBOL(twl4030_i2c_write);
410
411 /**
412  * twl4030_i2c_read - Reads a n bit register in TWL4030
413  * @mod_no: module number
414  * @value: an array of num_bytes containing data to be read
415  * @reg: register address (just offset will do)
416  * @num_bytes: number of bytes to transfer
417  *
418  * Returns result of operation - num_bytes is success else failure.
419  */
420 int twl4030_i2c_read(u8 mod_no, u8 *value, u8 reg, u8 num_bytes)
421 {
422         int ret;
423         u8 val;
424         int sid;
425         struct twl4030_client *twl;
426         struct i2c_msg *msg;
427
428         if (unlikely(mod_no > TWL4030_MODULE_LAST)) {
429                 pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
430                 return -EPERM;
431         }
432         sid = twl4030_map[mod_no].sid;
433         twl = &twl4030_modules[sid];
434
435         if (unlikely(!twl->inuse)) {
436                 pr_err("%s: client %d is not initialized\n", DRIVER_NAME, sid);
437                 return -EPERM;
438         }
439         mutex_lock(&twl->xfer_lock);
440         /* [MSG1] fill the register address data */
441         msg = &twl->xfer_msg[0];
442         msg->addr = twl->address;
443         msg->len = 1;
444         msg->flags = 0; /* Read the register value */
445         val = twl4030_map[mod_no].base + reg;
446         msg->buf = &val;
447         /* [MSG2] fill the data rx buffer */
448         msg = &twl->xfer_msg[1];
449         msg->addr = twl->address;
450         msg->flags = I2C_M_RD;  /* Read the register value */
451         msg->len = num_bytes;   /* only n bytes */
452         msg->buf = value;
453         ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 2);
454         mutex_unlock(&twl->xfer_lock);
455
456         /* i2cTransfer returns num messages.translate it pls.. */
457         if (ret >= 0)
458                 ret = 0;
459         return ret;
460 }
461 EXPORT_SYMBOL(twl4030_i2c_read);
462
463 /**
464  * twl4030_i2c_write_u8 - Writes a 8 bit register in TWL4030
465  * @mod_no: module number
466  * @value: the value to be written 8 bit
467  * @reg: register address (just offset will do)
468  *
469  * Returns result of operation - 0 is success
470  */
471 int twl4030_i2c_write_u8(u8 mod_no, u8 value, u8 reg)
472 {
473
474         /* 2 bytes offset 1 contains the data offset 0 is used by i2c_write */
475         u8 temp_buffer[2] = { 0 };
476         /* offset 1 contains the data */
477         temp_buffer[1] = value;
478         return twl4030_i2c_write(mod_no, temp_buffer, reg, 1);
479 }
480 EXPORT_SYMBOL(twl4030_i2c_write_u8);
481
482 /**
483  * twl4030_i2c_read_u8 - Reads a 8 bit register from TWL4030
484  * @mod_no: module number
485  * @value: the value read 8 bit
486  * @reg: register address (just offset will do)
487  *
488  * Returns result of operation - 0 is success
489  */
490 int twl4030_i2c_read_u8(u8 mod_no, u8 *value, u8 reg)
491 {
492         return twl4030_i2c_read(mod_no, value, reg, 1);
493 }
494 EXPORT_SYMBOL(twl4030_i2c_read_u8);
495
496 /* Helper Functions */
497
498 /*
499  * do_twl4030_module_irq() is the desc->handle method for each of the twl4030
500  * module interrupts.  It executes in kernel thread context.
501  * On entry, cpu interrupts are disabled.
502  */
503 static void do_twl4030_module_irq(unsigned int irq, irq_desc_t *desc)
504 {
505         struct irqaction *action;
506         const unsigned int cpu = smp_processor_id();
507
508         /*
509          * Earlier this was desc->triggered = 1;
510          */
511         desc->status |= IRQ_LEVEL;
512
513         /*
514          * The desc->handle method would normally call the desc->chip->ack
515          * method here, but we won't bother since our ack method is NULL.
516          */
517
518         if (!desc->depth) {
519                 kstat_cpu(cpu).irqs[irq]++;
520
521                 action = desc->action;
522                 if (action) {
523                         int ret;
524                         int status = 0;
525                         int retval = 0;
526
527                         local_irq_enable();
528
529                         do {
530                                 /* Call the ISR with cpu interrupts enabled */
531                                 ret = action->handler(irq, action->dev_id);
532                                 if (ret == IRQ_HANDLED)
533                                         status |= action->flags;
534                                 retval |= ret;
535                                 action = action->next;
536                         } while (action);
537
538                         if (status & IRQF_SAMPLE_RANDOM)
539                                 add_interrupt_randomness(irq);
540
541                         local_irq_disable();
542
543                         if (retval != IRQ_HANDLED)
544                                 printk(KERN_ERR "ISR for TWL4030 module"
545                                         " irq %d can't handle interrupt\n",
546                                         irq);
547
548                         /*
549                          * Here is where we should call the unmask method, but
550                          * again we won't bother since it is NULL.
551                          */
552                 } else
553                         printk(KERN_CRIT "TWL4030 module irq %d has no ISR"
554                                         " but can't be masked!\n", irq);
555         } else
556                 printk(KERN_CRIT "TWL4030 module irq %d is disabled but can't"
557                                 " be masked!\n", irq);
558 }
559
560 static unsigned twl4030_irq_base;
561
562 /*
563  * twl4030_irq_thread() runs as a kernel thread.  It queries the twl4030
564  * interrupt controller to see which modules are generating interrupt requests
565  * and then calls the desc->handle method for each module requesting service.
566  */
567 static int twl4030_irq_thread(void *data)
568 {
569         int irq = (int)data;
570         irq_desc_t *desc = irq_desc + irq;
571         static unsigned i2c_errors;
572         const static unsigned max_i2c_errors = 100;
573
574         daemonize("twl4030-irq");
575         current->flags |= PF_NOFREEZE;
576
577         while (!kthread_should_stop()) {
578                 int ret;
579                 int module_irq;
580                 u8 pih_isr;
581
582                 wait_for_completion_interruptible(&irq_event);
583
584                 ret = twl4030_i2c_read_u8(TWL4030_MODULE_PIH, &pih_isr,
585                                           REG_PIH_ISR_P1);
586                 if (ret) {
587                         printk(KERN_WARNING "I2C error %d while reading TWL4030"
588                                         " PIH ISR register.\n", ret);
589                         if (++i2c_errors >= max_i2c_errors) {
590                                 printk(KERN_ERR "Maximum I2C error count"
591                                                 " exceeded.  Terminating %s.\n",
592                                                 __func__);
593                                 break;
594                         }
595                         continue;
596                 }
597
598                 for (module_irq = twl4030_irq_base; 0 != pih_isr;
599                          pih_isr >>= 1, module_irq++) {
600                         if (pih_isr & 0x1) {
601                                 irq_desc_t *d = irq_desc + module_irq;
602
603                                 local_irq_disable();
604
605                                 d->handle_irq(module_irq, d);
606
607                                 local_irq_enable();
608                         }
609                 }
610
611                 desc->chip->unmask(irq);
612         }
613
614         return 0;
615 }
616
617 /*
618  * do_twl4030_irq() is the desc->handle method for the twl4030 interrupt.
619  * This is a chained interrupt, so there is no desc->action method for it.
620  * Now we need to query the interrupt controller in the twl4030 to determine
621  * which module is generating the interrupt request.  However, we can't do i2c
622  * transactions in interrupt context, so we must defer that work to a kernel
623  * thread.  All we do here is acknowledge and mask the interrupt and wakeup
624  * the kernel thread.
625  */
626 static void do_twl4030_irq(unsigned int irq, irq_desc_t *desc)
627 {
628         const unsigned int cpu = smp_processor_id();
629
630         /*
631          * Earlier this was desc->triggered = 1;
632          */
633         desc->status |= IRQ_LEVEL;
634
635         /*
636          * Acknowledge, clear _AND_ disable the interrupt.
637          */
638         desc->chip->ack(irq);
639
640         if (!desc->depth) {
641                 kstat_cpu(cpu).irqs[irq]++;
642
643                 complete(&irq_event);
644         }
645 }
646
647 static int add_children(struct twl4030_platform_data *pdata)
648 {
649         struct platform_device  *pdev = NULL;
650         struct twl4030_client   *twl = NULL;
651         int                     status = 0;
652
653         if (twl_has_rtc()) {
654                 pdev = platform_device_alloc("twl4030_rtc", -1);
655                 if (pdev) {
656                         twl = &twl4030_modules[TWL4030_SLAVENUM_NUM3];
657                         pdev->dev.parent = &twl->client->dev;
658                         device_init_wakeup(&pdev->dev, 1);
659
660                         /*
661                          * FIXME add the relevant IRQ resource, and make the
662                          * rtc driver use it instead of hard-wiring ...
663                          *
664                          * REVISIT platform_data here currently only supports
665                          * setting up the "msecure" line ... which actually
666                          * violates the "princple of least privilege", since
667                          * it's effectively always in "high trust" mode.
668                          *
669                          * For now, expect equivalent treatment at board init:
670                          * setting msecure high.  Eventually, Linux might
671                          * become more aware of those HW security concerns.
672                          */
673
674                         status = platform_device_add(pdev);
675                         if (status < 0)
676                                 platform_device_put(pdev);
677                 } else {
678                         status = -ENOMEM;
679                         goto err;
680                 }
681         }
682
683         if (twl_has_keypad() && pdata->keypad) {
684                 pdev = platform_device_alloc("twl4030_keypad", -1);
685                 if (pdev) {
686                         twl = &twl4030_modules[TWL4030_SLAVENUM_NUM2];
687                         pdev->dev.parent = &twl->client->dev;
688                         device_init_wakeup(&pdev->dev, 1);
689                         status = platform_device_add_data(pdev, pdata->keypad,
690                                         sizeof(*pdata->keypad));
691                         if (status < 0) {
692                                 platform_device_put(pdev);
693                                 goto err;
694                         }
695                         status = platform_device_add(pdev);
696                         if (status < 0)
697                                 platform_device_put(pdev);
698                 } else {
699                         status = -ENOMEM;
700                         goto err;
701                 }
702         }
703
704 err:
705         pr_err("failed to add twl4030's children\n");
706         return status;
707 }
708
709 static struct task_struct * __init start_twl4030_irq_thread(int irq)
710 {
711         struct task_struct *thread;
712
713         init_completion(&irq_event);
714         thread = kthread_run(twl4030_irq_thread, (void *)irq,
715                              "twl4030 irq %d", irq);
716         if (!thread)
717                 pr_err("%s: could not create twl4030 irq %d thread!\n",
718                        DRIVER_NAME, irq);
719
720         return thread;
721 }
722
723 /*
724  * These three functions should be part of Voltage frame work
725  * added here to complete the functionality for now.
726  */
727 static int __init protect_pm_master(void)
728 {
729         int e = 0;
730
731         e = twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_LOCK,
732                         R_PROTECT_KEY);
733         return e;
734 }
735
736 static int __init unprotect_pm_master(void)
737 {
738         int e = 0;
739
740         e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_UNLOCK1,
741                         R_PROTECT_KEY);
742         e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_UNLOCK2,
743                         R_PROTECT_KEY);
744         return e;
745 }
746
747 static int __init power_companion_init(void)
748 {
749         struct clk *osc;
750         u32 rate;
751         u8 ctrl = HFCLK_FREQ_26_MHZ;
752         int e = 0;
753
754         if (cpu_is_omap2430())
755                 osc = clk_get(NULL, "osc_ck");
756         else
757                 osc = clk_get(NULL, "osc_sys_ck");
758         if (IS_ERR(osc)) {
759                 printk(KERN_WARNING "Skipping twl3040 internal clock init and "
760                                 "using bootloader value (unknown osc rate)\n");
761                 return 0;
762         }
763
764         rate = clk_get_rate(osc);
765         clk_put(osc);
766
767         switch (rate) {
768         case 19200000:
769                 ctrl = HFCLK_FREQ_19p2_MHZ;
770                 break;
771         case 26000000:
772                 ctrl = HFCLK_FREQ_26_MHZ;
773                 break;
774         case 38400000:
775                 ctrl = HFCLK_FREQ_38p4_MHZ;
776                 break;
777         }
778
779         ctrl |= HIGH_PERF_SQ;
780         e |= unprotect_pm_master();
781         /* effect->MADC+USB ck en */
782         e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, ctrl, R_CFG_BOOT);
783         e |= protect_pm_master();
784
785         return e;
786 }
787
788 /**
789  * twl4030_i2c_clear_isr - clear TWL4030 SIH ISR regs via read + write
790  * @mod_no: TWL4030 module number
791  * @reg: register index to clear
792  * @cor: value of the <module>_SIH_CTRL.COR bit (1 or 0)
793  *
794  * Either reads (cor == 1) or writes (cor == 0) to a TWL4030 interrupt
795  * status register to ensure that any prior interrupts are cleared.
796  * Returns the status from the I2C read operation.
797  */
798 static int __init twl4030_i2c_clear_isr(u8 mod_no, u8 reg, u8 cor)
799 {
800         u8 tmp;
801
802         return (cor) ? twl4030_i2c_read_u8(mod_no, &tmp, reg) :
803                 twl4030_i2c_write_u8(mod_no, 0xff, reg);
804 }
805
806 /**
807  * twl4030_read_cor_bit - are TWL module ISRs cleared by reads or writes?
808  * @mod_no: TWL4030 module number
809  * @reg: register index to clear
810  *
811  * Returns 1 if the TWL4030 SIH interrupt status registers (ISRs) for
812  * the specified TWL module are cleared by reads, or 0 if cleared by
813  * writes.
814  */
815 static int twl4030_read_cor_bit(u8 mod_no, u8 reg)
816 {
817         u8 tmp = 0;
818
819         WARN_ON(twl4030_i2c_read_u8(mod_no, &tmp, reg) < 0);
820
821         tmp &= TWL4030_SIH_CTRL_COR_MASK;
822         tmp >>= __ffs(TWL4030_SIH_CTRL_COR_MASK);
823
824         return tmp;
825 }
826
827 /**
828  * twl4030_mask_clear_intrs - mask and clear all TWL4030 interrupts
829  * @t: pointer to twl4030_mod_iregs array
830  * @t_sz: ARRAY_SIZE(t) (starting at 1)
831  *
832  * Mask all TWL4030 interrupt mask registers (IMRs) and clear all
833  * interrupt status registers (ISRs).  No return value, but will WARN if
834  * any I2C operations fail.
835  */
836 static void __init twl4030_mask_clear_intrs(const struct twl4030_mod_iregs *t,
837                                             const u8 t_sz)
838 {
839         int i, j;
840
841         /*
842          * N.B. - further efficiency is possible here.  Eight I2C
843          * operations on BCI and GPIO modules are avoidable if I2C
844          * burst read/write transactions were implemented.  Would
845          * probably save about 1ms of boot time and a small amount of
846          * power.
847          */
848         for (i = 0; i < t_sz; i++) {
849                 const struct twl4030_mod_iregs tmr = t[i];
850                 int cor;
851
852                 /* Are ISRs cleared by reads or writes? */
853                 cor = twl4030_read_cor_bit(tmr.mod_no, tmr.sih_ctrl);
854                 WARN_ON(cor < 0);
855
856                 for (j = 0; j < tmr.reg_cnt; j++) {
857
858                         /* Mask interrupts at the TWL4030 */
859                         WARN_ON(twl4030_i2c_write_u8(tmr.mod_no, 0xff,
860                                                      tmr.imrs[j]) < 0);
861
862                         /* Clear TWL4030 ISRs */
863                         WARN_ON(twl4030_i2c_clear_isr(tmr.mod_no,
864                                                       tmr.isrs[j], cor) < 0);
865                 }
866         }
867
868         return;
869 }
870
871
872 static void twl_init_irq(int irq_num, unsigned irq_base, unsigned irq_end)
873 {
874         int     i;
875         int     res = 0;
876         char    *msg = "Unable to register interrupt subsystem";
877
878         /*
879          * Mask and clear all TWL4030 interrupts since initially we do
880          * not have any TWL4030 module interrupt handlers present
881          */
882         twl4030_mask_clear_intrs(twl4030_mod_regs,
883                                  ARRAY_SIZE(twl4030_mod_regs));
884
885         twl4030_irq_base = irq_base;
886
887         /* install an irq handler for each of the PIH modules */
888         for (i = irq_base; i < irq_end; i++) {
889                 set_irq_chip(i, &twl4030_irq_chip);
890                 set_irq_handler(i, do_twl4030_module_irq);
891                 set_irq_flags(i, IRQF_VALID);
892         }
893
894         /* install an irq handler to demultiplex the TWL4030 interrupt */
895         set_irq_data(irq_num, start_twl4030_irq_thread(irq_num));
896         set_irq_type(irq_num, IRQ_TYPE_EDGE_FALLING);
897         set_irq_chained_handler(irq_num, do_twl4030_irq);
898
899         res = power_companion_init();
900         if (res < 0)
901                 pr_err("%s: %s[%d]\n", DRIVER_NAME, msg, res);
902 }
903
904 /*----------------------------------------------------------------------*/
905
906 static int twl4030_remove(struct i2c_client *client)
907 {
908         unsigned i;
909
910         /* FIXME undo twl_init_irq() */
911         if (twl4030_irq_base) {
912                 dev_err(&client->dev, "can't yet clean up IRQs?\n");
913                 return -ENOSYS;
914         }
915
916         for (i = 0; i < TWL4030_NUM_SLAVES; i++) {
917                 struct twl4030_client   *twl = &twl4030_modules[i];
918
919                 if (twl->client && twl->client != client)
920                         i2c_unregister_device(twl->client);
921                 twl4030_modules[i].client = NULL;
922                 twl4030_modules[i].inuse = false;
923         }
924         return 0;
925 }
926
927 /* NOTE:  this driver only handles a single twl4030/tps659x0 chip */
928 static int
929 twl4030_probe(struct i2c_client *client, const struct i2c_device_id *id)
930 {
931         int                             status;
932         unsigned                        i;
933         struct twl4030_platform_data    *pdata = client->dev.platform_data;
934
935         if (!pdata) {
936                 dev_dbg(&client->dev, "no platform data?\n");
937                 return -EINVAL;
938         }
939
940         if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C) == 0) {
941                 dev_dbg(&client->dev, "can't talk I2C?\n");
942                 return -EIO;
943         }
944
945         for (i = 0; i < TWL4030_NUM_SLAVES; i++) {
946                 if (twl4030_modules[i].inuse || twl4030_irq_base) {
947                         dev_dbg(&client->dev, "driver is already in use\n");
948                         return -EBUSY;
949                 }
950         }
951
952         for (i = 0; i < TWL4030_NUM_SLAVES; i++) {
953                 struct twl4030_client   *twl = &twl4030_modules[i];
954
955                 twl->address = client->addr + i;
956                 if (i == 0)
957                         twl->client = client;
958                 else {
959                         twl->client = i2c_new_dummy(client->adapter,
960                                         twl->address);
961                         if (!twl->client) {
962                                 dev_err(&twl->client->dev,
963                                         "can't attach client %d\n", i);
964                                 status = -ENOMEM;
965                                 goto fail;
966                         }
967                         strlcpy(twl->client->name, id->name,
968                                         sizeof(twl->client->name));
969                 }
970                 twl->inuse = true;
971                 mutex_init(&twl->xfer_lock);
972         }
973
974         status = add_children(pdata);
975         if (status < 0)
976                 goto fail;
977
978         /*
979          * Check if the PIH module is initialized, if yes, then init
980          * the T2 Interrupt subsystem
981          */
982         if (twl4030_modules[twl4030_map[TWL4030_MODULE_PIH].sid].inuse
983                         && twl4030_irq_base == 0
984                         && client->irq
985                         && pdata->irq_base
986                         && pdata->irq_end > pdata->irq_base)
987                 twl_init_irq(client->irq, pdata->irq_base, pdata->irq_end);
988
989         dev_info(&client->dev, "chaining %d irqs\n",
990                         twl4030_irq_base
991                                 ? (pdata->irq_end - pdata->irq_base)
992                                 : 0);
993         return 0;
994
995 fail:
996         twl4030_remove(client);
997         return status;
998 }
999
1000 static const struct i2c_device_id twl4030_ids[] = {
1001         { "twl4030", 0 },       /* "Triton 2" */
1002         { "tps65950", 0 },      /* catalog version of twl4030 */
1003         { "tps65930", 0 },      /* fewer LDOs and DACs; no charger */
1004         { "tps65920", 0 },      /* fewer LDOs; no codec or charger */
1005         { /* end of list */ },
1006 };
1007 MODULE_DEVICE_TABLE(i2c, twl4030_ids);
1008
1009 /* One Client Driver , 4 Clients */
1010 static struct i2c_driver twl4030_driver = {
1011         .driver.name    = DRIVER_NAME,
1012         .id_table       = twl4030_ids,
1013         .probe          = twl4030_probe,
1014         .remove         = twl4030_remove,
1015 };
1016
1017 static int __init twl4030_init(void)
1018 {
1019         return i2c_add_driver(&twl4030_driver);
1020 }
1021 subsys_initcall(twl4030_init);
1022
1023 static void __exit twl4030_exit(void)
1024 {
1025         i2c_del_driver(&twl4030_driver);
1026 }
1027 module_exit(twl4030_exit);
1028
1029 MODULE_AUTHOR("Texas Instruments, Inc.");
1030 MODULE_DESCRIPTION("I2C Core interface for TWL4030");
1031 MODULE_LICENSE("GPL");