]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/i2c/chips/twl4030-core.c
Merge current mainline tree into linux-omap tree
[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_TWL4030_BCI_BATTERY) || \
49         defined(CONFIG_TWL4030_BCI_BATTERY_MODUEL)
50 #define twl_has_bci()           true
51 #else
52 #define twl_has_bci()           false
53 #endif
54
55 #if defined(CONFIG_KEYBOARD_TWL4030) || defined(CONFIG_KEYBOARD_TWL4030_MODULE)
56 #define twl_has_keypad()        true
57 #else
58 #define twl_has_keypad()        false
59 #endif
60
61 #if defined(CONFIG_GPIO_TWL4030) || defined(CONFIG_GPIO_TWL4030_MODULE)
62 #define twl_has_gpio()  true
63 #else
64 #define twl_has_gpio()  false
65 #endif
66
67 #if defined(CONFIG_TWL4030_MADC) || defined(CONFIG_TWL4030_MADC_MODULE)
68 #define twl_has_madc()  true
69 #else
70 #define twl_has_madc()  false
71 #endif
72
73 #if defined(CONFIG_RTC_DRV_TWL4030) || defined(CONFIG_RTC_DRV_TWL4030_MODULE)
74 #define twl_has_rtc()   true
75 #else
76 #define twl_has_rtc()   false
77 #endif
78
79 #if defined(CONFIG_TWL4030_USB) || defined(CONFIG_TWL4030_USB_MODULE)
80 #define twl_has_usb()   true
81 #else
82 #define twl_has_usb()   false
83 #endif
84
85 static inline void activate_irq(int irq)
86 {
87 #ifdef CONFIG_ARM
88         /* ARM requires an extra step to clear IRQ_NOREQUEST, which it
89          * sets on behalf of every irq_chip.  Also sets IRQ_NOPROBE.
90          */
91         set_irq_flags(irq, IRQF_VALID);
92 #else
93         /* same effect on other architectures */
94         set_irq_noprobe(irq);
95 #endif
96 }
97
98 /* Primary Interrupt Handler on TWL4030 Registers */
99
100 /* Register Definitions */
101
102 #define REG_PIH_ISR_P1                  (0x1)
103 #define REG_PIH_ISR_P2                  (0x2)
104 #define REG_PIH_SIR                     (0x3)
105
106 /* Triton Core internal information (BEGIN) */
107
108 /* Last - for index max*/
109 #define TWL4030_MODULE_LAST             TWL4030_MODULE_SECURED_REG
110
111 #define TWL4030_NUM_SLAVES              4
112
113 /* Slave address */
114 #define TWL4030_SLAVENUM_NUM0           0x00
115 #define TWL4030_SLAVENUM_NUM1           0x01
116 #define TWL4030_SLAVENUM_NUM2           0x02
117 #define TWL4030_SLAVENUM_NUM3           0x03
118
119 /* Base Address defns */
120 /* USB ID */
121 #define TWL4030_BASEADD_USB             0x0000
122 /* AUD ID */
123 #define TWL4030_BASEADD_AUDIO_VOICE     0x0000
124 #define TWL4030_BASEADD_GPIO            0x0098
125
126 #define TWL4030_BASEADD_INTBR           0x0085
127 #define TWL4030_BASEADD_PIH             0x0080
128 #define TWL4030_BASEADD_TEST            0x004C
129 /* AUX ID */
130 #define TWL4030_BASEADD_INTERRUPTS      0x00B9
131 #define TWL4030_BASEADD_LED             0x00EE
132 #define TWL4030_BASEADD_MADC            0x0000
133 #define TWL4030_BASEADD_MAIN_CHARGE     0x0074
134 #define TWL4030_BASEADD_PRECHARGE       0x00AA
135 #define TWL4030_BASEADD_PWM0            0x00F8
136 #define TWL4030_BASEADD_PWM1            0x00FB
137 #define TWL4030_BASEADD_PWMA            0x00EF
138 #define TWL4030_BASEADD_PWMB            0x00F1
139 #define TWL4030_BASEADD_KEYPAD          0x00D2
140 /* POWER ID */
141 #define TWL4030_BASEADD_BACKUP          0x0014
142 #define TWL4030_BASEADD_INT             0x002E
143 #define TWL4030_BASEADD_PM_MASTER       0x0036
144 #define TWL4030_BASEADD_PM_RECEIVER     0x005B
145 #define TWL4030_BASEADD_RTC             0x001C
146 #define TWL4030_BASEADD_SECURED_REG     0x0000
147
148 /* TWL4030 BCI registers */
149 #define TWL4030_INTERRUPTS_BCIIMR1A     0x2
150 #define TWL4030_INTERRUPTS_BCIIMR2A     0x3
151 #define TWL4030_INTERRUPTS_BCIIMR1B     0x6
152 #define TWL4030_INTERRUPTS_BCIIMR2B     0x7
153 #define TWL4030_INTERRUPTS_BCIISR1A     0x0
154 #define TWL4030_INTERRUPTS_BCIISR2A     0x1
155 #define TWL4030_INTERRUPTS_BCIISR1B     0x4
156 #define TWL4030_INTERRUPTS_BCIISR2B     0x5
157
158 /* TWL4030 keypad registers */
159 #define TWL4030_KEYPAD_KEYP_IMR1        0x12
160 #define TWL4030_KEYPAD_KEYP_IMR2        0x14
161 #define TWL4030_KEYPAD_KEYP_ISR1        0x11
162 #define TWL4030_KEYPAD_KEYP_ISR2        0x13
163
164
165 /* Triton Core internal information (END) */
166
167 /* Few power values */
168 #define R_CFG_BOOT                      0x05
169 #define R_PROTECT_KEY                   0x0E
170
171 /* access control */
172 #define KEY_UNLOCK1                     0xce
173 #define KEY_UNLOCK2                     0xec
174 #define KEY_LOCK                        0x00
175
176 #define HFCLK_FREQ_19p2_MHZ             (1 << 0)
177 #define HFCLK_FREQ_26_MHZ               (2 << 0)
178 #define HFCLK_FREQ_38p4_MHZ             (3 << 0)
179 #define HIGH_PERF_SQ                    (1 << 3)
180
181 /* SIH_CTRL registers that aren't defined elsewhere */
182 #define TWL4030_INTERRUPTS_BCISIHCTRL   0x0d
183 #define TWL4030_MADC_MADC_SIH_CTRL      0x67
184 #define TWL4030_KEYPAD_KEYP_SIH_CTRL    0x17
185
186 #define TWL4030_SIH_CTRL_COR_MASK       (1 << 2)
187
188 /**
189  * struct twl4030_mod_iregs - TWL module IMR/ISR regs to mask/clear at init
190  * @mod_no: TWL4030 module number (e.g., TWL4030_MODULE_GPIO)
191  * @sih_ctrl: address of module SIH_CTRL register
192  * @reg_cnt: number of IMR/ISR regs
193  * @imrs: pointer to array of TWL module interrupt mask register indices
194  * @isrs: pointer to array of TWL module interrupt status register indices
195  *
196  * Ties together TWL4030 modules and lists of IMR/ISR registers to mask/clear
197  * during twl_init_irq().
198  */
199 struct twl4030_mod_iregs {
200         const u8 mod_no;
201         const u8 sih_ctrl;
202         const u8 reg_cnt;
203         const u8 *imrs;
204         const u8 *isrs;
205 };
206
207 /* TWL4030 INT module interrupt mask registers */
208 static const u8 __initconst twl4030_int_imr_regs[] = {
209         TWL4030_INT_PWR_IMR1,
210         TWL4030_INT_PWR_IMR2,
211 };
212
213 /* TWL4030 INT module interrupt status registers */
214 static const u8 __initconst twl4030_int_isr_regs[] = {
215         TWL4030_INT_PWR_ISR1,
216         TWL4030_INT_PWR_ISR2,
217 };
218
219 /* TWL4030 INTERRUPTS module interrupt mask registers */
220 static const u8 __initconst twl4030_interrupts_imr_regs[] = {
221         TWL4030_INTERRUPTS_BCIIMR1A,
222         TWL4030_INTERRUPTS_BCIIMR1B,
223         TWL4030_INTERRUPTS_BCIIMR2A,
224         TWL4030_INTERRUPTS_BCIIMR2B,
225 };
226
227 /* TWL4030 INTERRUPTS module interrupt status registers */
228 static const u8 __initconst twl4030_interrupts_isr_regs[] = {
229         TWL4030_INTERRUPTS_BCIISR1A,
230         TWL4030_INTERRUPTS_BCIISR1B,
231         TWL4030_INTERRUPTS_BCIISR2A,
232         TWL4030_INTERRUPTS_BCIISR2B,
233 };
234
235 /* TWL4030 MADC module interrupt mask registers */
236 static const u8 __initconst twl4030_madc_imr_regs[] = {
237         TWL4030_MADC_IMR1,
238         TWL4030_MADC_IMR2,
239 };
240
241 /* TWL4030 MADC module interrupt status registers */
242 static const u8 __initconst twl4030_madc_isr_regs[] = {
243         TWL4030_MADC_ISR1,
244         TWL4030_MADC_ISR2,
245 };
246
247 /* TWL4030 keypad module interrupt mask registers */
248 static const u8 __initconst twl4030_keypad_imr_regs[] = {
249         TWL4030_KEYPAD_KEYP_IMR1,
250         TWL4030_KEYPAD_KEYP_IMR2,
251 };
252
253 /* TWL4030 keypad module interrupt status registers */
254 static const u8 __initconst twl4030_keypad_isr_regs[] = {
255         TWL4030_KEYPAD_KEYP_ISR1,
256         TWL4030_KEYPAD_KEYP_ISR2,
257 };
258
259 /* TWL4030 GPIO module interrupt mask registers */
260 static const u8 __initconst twl4030_gpio_imr_regs[] = {
261         REG_GPIO_IMR1A,
262         REG_GPIO_IMR1B,
263         REG_GPIO_IMR2A,
264         REG_GPIO_IMR2B,
265         REG_GPIO_IMR3A,
266         REG_GPIO_IMR3B,
267 };
268
269 /* TWL4030 GPIO module interrupt status registers */
270 static const u8 __initconst twl4030_gpio_isr_regs[] = {
271         REG_GPIO_ISR1A,
272         REG_GPIO_ISR1B,
273         REG_GPIO_ISR2A,
274         REG_GPIO_ISR2B,
275         REG_GPIO_ISR3A,
276         REG_GPIO_ISR3B,
277 };
278
279 /* TWL4030 modules that have IMR/ISR registers that must be masked/cleared */
280 static const struct twl4030_mod_iregs __initconst twl4030_mod_regs[] = {
281         {
282                 .mod_no   = TWL4030_MODULE_INT,
283                 .sih_ctrl = TWL4030_INT_PWR_SIH_CTRL,
284                 .reg_cnt  = ARRAY_SIZE(twl4030_int_imr_regs),
285                 .imrs     = twl4030_int_imr_regs,
286                 .isrs     = twl4030_int_isr_regs,
287         },
288         {
289                 .mod_no   = TWL4030_MODULE_INTERRUPTS,
290                 .sih_ctrl = TWL4030_INTERRUPTS_BCISIHCTRL,
291                 .reg_cnt  = ARRAY_SIZE(twl4030_interrupts_imr_regs),
292                 .imrs     = twl4030_interrupts_imr_regs,
293                 .isrs     = twl4030_interrupts_isr_regs,
294         },
295         {
296                 .mod_no   = TWL4030_MODULE_MADC,
297                 .sih_ctrl = TWL4030_MADC_MADC_SIH_CTRL,
298                 .reg_cnt  = ARRAY_SIZE(twl4030_madc_imr_regs),
299                 .imrs     = twl4030_madc_imr_regs,
300                 .isrs     = twl4030_madc_isr_regs,
301         },
302         {
303                 .mod_no   = TWL4030_MODULE_KEYPAD,
304                 .sih_ctrl = TWL4030_KEYPAD_KEYP_SIH_CTRL,
305                 .reg_cnt  = ARRAY_SIZE(twl4030_keypad_imr_regs),
306                 .imrs     = twl4030_keypad_imr_regs,
307                 .isrs     = twl4030_keypad_isr_regs,
308         },
309         {
310                 .mod_no   = TWL4030_MODULE_GPIO,
311                 .sih_ctrl = REG_GPIO_SIH_CTRL,
312                 .reg_cnt  = ARRAY_SIZE(twl4030_gpio_imr_regs),
313                 .imrs     = twl4030_gpio_imr_regs,
314                 .isrs     = twl4030_gpio_isr_regs,
315         },
316 };
317
318
319 /* Data Structures */
320 /* To have info on T2 IRQ substem activated or not */
321 static struct completion irq_event;
322
323 /* Structure to define on TWL4030 Slave ID */
324 struct twl4030_client {
325         struct i2c_client *client;
326         u8 address;
327         bool inuse;
328
329         /* max numb of i2c_msg required is for read =2 */
330         struct i2c_msg xfer_msg[2];
331
332         /* To lock access to xfer_msg */
333         struct mutex xfer_lock;
334 };
335
336 /* Module Mapping */
337 struct twl4030mapping {
338         unsigned char sid;      /* Slave ID */
339         unsigned char base;     /* base address */
340 };
341
342 /* mapping the module id to slave id and base address */
343 static struct twl4030mapping twl4030_map[TWL4030_MODULE_LAST + 1] = {
344         { TWL4030_SLAVENUM_NUM0, TWL4030_BASEADD_USB },
345         { TWL4030_SLAVENUM_NUM1, TWL4030_BASEADD_AUDIO_VOICE },
346         { TWL4030_SLAVENUM_NUM1, TWL4030_BASEADD_GPIO },
347         { TWL4030_SLAVENUM_NUM1, TWL4030_BASEADD_INTBR },
348         { TWL4030_SLAVENUM_NUM1, TWL4030_BASEADD_PIH },
349         { TWL4030_SLAVENUM_NUM1, TWL4030_BASEADD_TEST },
350         { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_KEYPAD },
351         { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_MADC },
352         { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_INTERRUPTS },
353         { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_LED },
354         { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_MAIN_CHARGE },
355         { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_PRECHARGE },
356         { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_PWM0 },
357         { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_PWM1 },
358         { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_PWMA },
359         { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_PWMB },
360         { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_BACKUP },
361         { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_INT },
362         { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_PM_MASTER },
363         { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_PM_RECEIVER },
364         { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_RTC },
365         { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_SECURED_REG },
366 };
367
368 static struct twl4030_client twl4030_modules[TWL4030_NUM_SLAVES];
369
370 /*
371  * TWL4030 doesn't have PIH mask, hence dummy function for mask
372  * and unmask.
373  */
374
375 static void twl4030_i2c_ackirq(unsigned int irq)
376 {
377 }
378
379 static void twl4030_i2c_disableint(unsigned int irq)
380 {
381 }
382
383 static void twl4030_i2c_enableint(unsigned int irq)
384 {
385 }
386
387 /* information for processing in the Work Item */
388 static struct irq_chip twl4030_irq_chip = {
389         .name   = "twl4030",
390         .ack    = twl4030_i2c_ackirq,
391         .mask   = twl4030_i2c_disableint,
392         .unmask = twl4030_i2c_enableint,
393 };
394
395 /* Global Functions */
396
397 /**
398  * twl4030_i2c_write - Writes a n bit register in TWL4030
399  * @mod_no: module number
400  * @value: an array of num_bytes+1 containing data to write
401  * @reg: register address (just offset will do)
402  * @num_bytes: number of bytes to transfer
403  *
404  * IMPORTANT: for 'value' parameter: Allocate value num_bytes+1 and
405  * valid data starts at Offset 1.
406  *
407  * Returns the result of operation - 0 is success
408  */
409 int twl4030_i2c_write(u8 mod_no, u8 *value, u8 reg, u8 num_bytes)
410 {
411         int ret;
412         int sid;
413         struct twl4030_client *twl;
414         struct i2c_msg *msg;
415
416         if (unlikely(mod_no > TWL4030_MODULE_LAST)) {
417                 pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
418                 return -EPERM;
419         }
420         sid = twl4030_map[mod_no].sid;
421         twl = &twl4030_modules[sid];
422
423         if (unlikely(!twl->inuse)) {
424                 pr_err("%s: client %d is not initialized\n", DRIVER_NAME, sid);
425                 return -EPERM;
426         }
427         mutex_lock(&twl->xfer_lock);
428         /*
429          * [MSG1]: fill the register address data
430          * fill the data Tx buffer
431          */
432         msg = &twl->xfer_msg[0];
433         msg->addr = twl->address;
434         msg->len = num_bytes + 1;
435         msg->flags = 0;
436         msg->buf = value;
437         /* over write the first byte of buffer with the register address */
438         *value = twl4030_map[mod_no].base + reg;
439         ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 1);
440         mutex_unlock(&twl->xfer_lock);
441
442         /* i2cTransfer returns num messages.translate it pls.. */
443         if (ret >= 0)
444                 ret = 0;
445         return ret;
446 }
447 EXPORT_SYMBOL(twl4030_i2c_write);
448
449 /**
450  * twl4030_i2c_read - Reads a n bit register in TWL4030
451  * @mod_no: module number
452  * @value: an array of num_bytes containing data to be read
453  * @reg: register address (just offset will do)
454  * @num_bytes: number of bytes to transfer
455  *
456  * Returns result of operation - num_bytes is success else failure.
457  */
458 int twl4030_i2c_read(u8 mod_no, u8 *value, u8 reg, u8 num_bytes)
459 {
460         int ret;
461         u8 val;
462         int sid;
463         struct twl4030_client *twl;
464         struct i2c_msg *msg;
465
466         if (unlikely(mod_no > TWL4030_MODULE_LAST)) {
467                 pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
468                 return -EPERM;
469         }
470         sid = twl4030_map[mod_no].sid;
471         twl = &twl4030_modules[sid];
472
473         if (unlikely(!twl->inuse)) {
474                 pr_err("%s: client %d is not initialized\n", DRIVER_NAME, sid);
475                 return -EPERM;
476         }
477         mutex_lock(&twl->xfer_lock);
478         /* [MSG1] fill the register address data */
479         msg = &twl->xfer_msg[0];
480         msg->addr = twl->address;
481         msg->len = 1;
482         msg->flags = 0; /* Read the register value */
483         val = twl4030_map[mod_no].base + reg;
484         msg->buf = &val;
485         /* [MSG2] fill the data rx buffer */
486         msg = &twl->xfer_msg[1];
487         msg->addr = twl->address;
488         msg->flags = I2C_M_RD;  /* Read the register value */
489         msg->len = num_bytes;   /* only n bytes */
490         msg->buf = value;
491         ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 2);
492         mutex_unlock(&twl->xfer_lock);
493
494         /* i2cTransfer returns num messages.translate it pls.. */
495         if (ret >= 0)
496                 ret = 0;
497         return ret;
498 }
499 EXPORT_SYMBOL(twl4030_i2c_read);
500
501 /**
502  * twl4030_i2c_write_u8 - Writes a 8 bit register in TWL4030
503  * @mod_no: module number
504  * @value: the value to be written 8 bit
505  * @reg: register address (just offset will do)
506  *
507  * Returns result of operation - 0 is success
508  */
509 int twl4030_i2c_write_u8(u8 mod_no, u8 value, u8 reg)
510 {
511
512         /* 2 bytes offset 1 contains the data offset 0 is used by i2c_write */
513         u8 temp_buffer[2] = { 0 };
514         /* offset 1 contains the data */
515         temp_buffer[1] = value;
516         return twl4030_i2c_write(mod_no, temp_buffer, reg, 1);
517 }
518 EXPORT_SYMBOL(twl4030_i2c_write_u8);
519
520 /**
521  * twl4030_i2c_read_u8 - Reads a 8 bit register from TWL4030
522  * @mod_no: module number
523  * @value: the value read 8 bit
524  * @reg: register address (just offset will do)
525  *
526  * Returns result of operation - 0 is success
527  */
528 int twl4030_i2c_read_u8(u8 mod_no, u8 *value, u8 reg)
529 {
530         return twl4030_i2c_read(mod_no, value, reg, 1);
531 }
532 EXPORT_SYMBOL(twl4030_i2c_read_u8);
533
534 /* Helper Functions */
535
536 /*
537  * do_twl4030_module_irq() is the desc->handle method for each of the twl4030
538  * module interrupts.  It executes in kernel thread context.
539  * On entry, cpu interrupts are disabled.
540  */
541 static void do_twl4030_module_irq(unsigned int irq, irq_desc_t *desc)
542 {
543         struct irqaction *action;
544         const unsigned int cpu = smp_processor_id();
545
546         /*
547          * Earlier this was desc->triggered = 1;
548          */
549         desc->status |= IRQ_LEVEL;
550
551         /*
552          * The desc->handle method would normally call the desc->chip->ack
553          * method here, but we won't bother since our ack method is NULL.
554          */
555
556         if (!desc->depth) {
557                 kstat_cpu(cpu).irqs[irq]++;
558
559                 action = desc->action;
560                 if (action) {
561                         int ret;
562                         int status = 0;
563                         int retval = 0;
564
565                         local_irq_enable();
566
567                         do {
568                                 /* Call the ISR with cpu interrupts enabled */
569                                 ret = action->handler(irq, action->dev_id);
570                                 if (ret == IRQ_HANDLED)
571                                         status |= action->flags;
572                                 retval |= ret;
573                                 action = action->next;
574                         } while (action);
575
576                         if (status & IRQF_SAMPLE_RANDOM)
577                                 add_interrupt_randomness(irq);
578
579                         local_irq_disable();
580
581                         if (retval != IRQ_HANDLED)
582                                 printk(KERN_ERR "ISR for TWL4030 module"
583                                         " irq %d can't handle interrupt\n",
584                                         irq);
585
586                         /*
587                          * Here is where we should call the unmask method, but
588                          * again we won't bother since it is NULL.
589                          */
590                 } else
591                         printk(KERN_CRIT "TWL4030 module irq %d has no ISR"
592                                         " but can't be masked!\n", irq);
593         } else
594                 printk(KERN_CRIT "TWL4030 module irq %d is disabled but can't"
595                                 " be masked!\n", irq);
596 }
597
598 static unsigned twl4030_irq_base;
599
600 /*
601  * twl4030_irq_thread() runs as a kernel thread.  It queries the twl4030
602  * interrupt controller to see which modules are generating interrupt requests
603  * and then calls the desc->handle method for each module requesting service.
604  */
605 static int twl4030_irq_thread(void *data)
606 {
607         long irq = (long)data;
608         irq_desc_t *desc = irq_desc + irq;
609         static unsigned i2c_errors;
610         const static unsigned max_i2c_errors = 100;
611
612         daemonize("twl4030-irq");
613         current->flags |= PF_NOFREEZE;
614
615         while (!kthread_should_stop()) {
616                 int ret;
617                 int module_irq;
618                 u8 pih_isr;
619
620                 wait_for_completion_interruptible(&irq_event);
621
622                 ret = twl4030_i2c_read_u8(TWL4030_MODULE_PIH, &pih_isr,
623                                           REG_PIH_ISR_P1);
624                 if (ret) {
625                         printk(KERN_WARNING "I2C error %d while reading TWL4030"
626                                         " PIH ISR register.\n", ret);
627                         if (++i2c_errors >= max_i2c_errors) {
628                                 printk(KERN_ERR "Maximum I2C error count"
629                                                 " exceeded.  Terminating %s.\n",
630                                                 __func__);
631                                 break;
632                         }
633                         continue;
634                 }
635
636                 for (module_irq = twl4030_irq_base; 0 != pih_isr;
637                          pih_isr >>= 1, module_irq++) {
638                         if (pih_isr & 0x1) {
639                                 irq_desc_t *d = irq_desc + module_irq;
640
641                                 local_irq_disable();
642
643                                 d->handle_irq(module_irq, d);
644
645                                 local_irq_enable();
646                         }
647                 }
648
649                 desc->chip->unmask(irq);
650         }
651
652         return 0;
653 }
654
655 /*
656  * do_twl4030_irq() is the desc->handle method for the twl4030 interrupt.
657  * This is a chained interrupt, so there is no desc->action method for it.
658  * Now we need to query the interrupt controller in the twl4030 to determine
659  * which module is generating the interrupt request.  However, we can't do i2c
660  * transactions in interrupt context, so we must defer that work to a kernel
661  * thread.  All we do here is acknowledge and mask the interrupt and wakeup
662  * the kernel thread.
663  */
664 static void do_twl4030_irq(unsigned int irq, irq_desc_t *desc)
665 {
666         const unsigned int cpu = smp_processor_id();
667
668         /*
669          * Earlier this was desc->triggered = 1;
670          */
671         desc->status |= IRQ_LEVEL;
672
673         /*
674          * Acknowledge, clear _AND_ disable the interrupt.
675          */
676         desc->chip->ack(irq);
677
678         if (!desc->depth) {
679                 kstat_cpu(cpu).irqs[irq]++;
680
681                 complete(&irq_event);
682         }
683 }
684
685 static int add_children(struct twl4030_platform_data *pdata)
686 {
687         struct platform_device  *pdev = NULL;
688         struct twl4030_client   *twl = NULL;
689         int                     status = 0;
690
691         if (twl_has_bci() && pdata->bci) {
692                 twl = &twl4030_modules[TWL4030_SLAVENUM_NUM3];
693
694                 pdev = platform_device_alloc("twl4030_bci", -1);
695                 if (!pdev) {
696                         pr_debug("%s: can't alloc bci dev\n", DRIVER_NAME);
697                         status = -ENOMEM;
698                         goto err;
699                 }
700
701                 if (status == 0) {
702                         pdev->dev.parent = &twl->client->dev;
703                         status = platform_device_add_data(pdev, pdata->bci,
704                                         sizeof(*pdata->bci));
705                         if (status < 0) {
706                                 dev_dbg(&twl->client->dev,
707                                         "can't add bci data, %d\n",
708                                         status);
709                                 goto err;
710                         }
711                 }
712
713                 if (status == 0) {
714                         struct resource r = {
715                                 .start = TWL4030_PWRIRQ_CHG_PRES,
716                                 .flags = IORESOURCE_IRQ,
717                         };
718
719                         status = platform_device_add_resources(pdev, &r, 1);
720                 }
721
722                 if (status == 0)
723                         status = platform_device_add(pdev);
724
725                 if (status < 0) {
726                         platform_device_put(pdev);
727                         dev_dbg(&twl->client->dev,
728                                         "can't create bci dev, %d\n",
729                                         status);
730                         goto err;
731                 }
732         }
733
734         if (twl_has_gpio() && pdata->gpio) {
735                 twl = &twl4030_modules[TWL4030_SLAVENUM_NUM1];
736
737                 pdev = platform_device_alloc("twl4030_gpio", -1);
738                 if (!pdev) {
739                         pr_debug("%s: can't alloc gpio dev\n", DRIVER_NAME);
740                         status = -ENOMEM;
741                         goto err;
742                 }
743
744                 /* more driver model init */
745                 if (status == 0) {
746                         pdev->dev.parent = &twl->client->dev;
747                         /* device_init_wakeup(&pdev->dev, 1); */
748
749                         status = platform_device_add_data(pdev, pdata->gpio,
750                                         sizeof(*pdata->gpio));
751                         if (status < 0) {
752                                 dev_dbg(&twl->client->dev,
753                                         "can't add gpio data, %d\n",
754                                         status);
755                                 goto err;
756                         }
757                 }
758
759                 /* GPIO module IRQ */
760                 if (status == 0) {
761                         struct resource r = {
762                                 .start = pdata->irq_base + 0,
763                                 .flags = IORESOURCE_IRQ,
764                         };
765
766                         status = platform_device_add_resources(pdev, &r, 1);
767                 }
768
769                 if (status == 0)
770                         status = platform_device_add(pdev);
771
772                 if (status < 0) {
773                         platform_device_put(pdev);
774                         dev_dbg(&twl->client->dev,
775                                         "can't create gpio dev, %d\n",
776                                         status);
777                         goto err;
778                 }
779         }
780
781         if (twl_has_keypad() && pdata->keypad) {
782                 pdev = platform_device_alloc("twl4030_keypad", -1);
783                 if (pdev) {
784                         twl = &twl4030_modules[TWL4030_SLAVENUM_NUM2];
785                         pdev->dev.parent = &twl->client->dev;
786                         device_init_wakeup(&pdev->dev, 1);
787                         status = platform_device_add_data(pdev, pdata->keypad,
788                                         sizeof(*pdata->keypad));
789                         if (status < 0) {
790                                 dev_dbg(&twl->client->dev,
791                                         "can't add keypad data, %d\n",
792                                         status);
793                                 platform_device_put(pdev);
794                                 goto err;
795                         }
796                         status = platform_device_add(pdev);
797                         if (status < 0) {
798                                 platform_device_put(pdev);
799                                 dev_dbg(&twl->client->dev,
800                                                 "can't create keypad dev, %d\n",
801                                                 status);
802                                 goto err;
803                         }
804                 } else {
805                         pr_debug("%s: can't alloc keypad dev\n", DRIVER_NAME);
806                         status = -ENOMEM;
807                         goto err;
808                 }
809         }
810
811         if (twl_has_madc() && pdata->madc) {
812                 pdev = platform_device_alloc("twl4030_madc", -1);
813                 if (pdev) {
814                         twl = &twl4030_modules[TWL4030_SLAVENUM_NUM2];
815                         pdev->dev.parent = &twl->client->dev;
816                         device_init_wakeup(&pdev->dev, 1);
817                         status = platform_device_add_data(pdev, pdata->madc,
818                                         sizeof(*pdata->madc));
819                         if (status < 0) {
820                                 platform_device_put(pdev);
821                                 dev_dbg(&twl->client->dev,
822                                         "can't add madc data, %d\n",
823                                         status);
824                                 goto err;
825                         }
826                         status = platform_device_add(pdev);
827                         if (status < 0) {
828                                 platform_device_put(pdev);
829                                 dev_dbg(&twl->client->dev,
830                                                 "can't create madc dev, %d\n",
831                                                 status);
832                                 goto err;
833                         }
834                 } else {
835                         pr_debug("%s: can't alloc madc dev\n", DRIVER_NAME);
836                         status = -ENOMEM;
837                         goto err;
838                 }
839         }
840
841         if (twl_has_rtc()) {
842                 twl = &twl4030_modules[TWL4030_SLAVENUM_NUM3];
843
844                 pdev = platform_device_alloc("twl4030_rtc", -1);
845                 if (!pdev) {
846                         pr_debug("%s: can't alloc rtc dev\n", DRIVER_NAME);
847                         status = -ENOMEM;
848                 } else {
849                         pdev->dev.parent = &twl->client->dev;
850                         device_init_wakeup(&pdev->dev, 1);
851                 }
852
853                 /*
854                  * REVISIT platform_data here currently might use of
855                  * "msecure" line ... but for now we just expect board
856                  * setup to tell the chip "we are secure" at all times.
857                  * Eventually, Linux might become more aware of such
858                  * HW security concerns, and "least privilege".
859                  */
860
861                 /* RTC module IRQ */
862                 if (status == 0) {
863                         struct resource r = {
864                                 /* REVISIT don't hard-wire this stuff */
865                                 .start = TWL4030_PWRIRQ_RTC,
866                                 .flags = IORESOURCE_IRQ,
867                         };
868
869                         status = platform_device_add_resources(pdev, &r, 1);
870                 }
871
872                 if (status == 0)
873                         status = platform_device_add(pdev);
874
875                 if (status < 0) {
876                         platform_device_put(pdev);
877                         dev_dbg(&twl->client->dev,
878                                         "can't create rtc dev, %d\n",
879                                         status);
880                         goto err;
881                 }
882         }
883
884         if (twl_has_usb() && pdata->usb) {
885                 twl = &twl4030_modules[TWL4030_SLAVENUM_NUM0];
886
887                 pdev = platform_device_alloc("twl4030_usb", -1);
888                 if (!pdev) {
889                         pr_debug("%s: can't alloc usb dev\n", DRIVER_NAME);
890                         status = -ENOMEM;
891                         goto err;
892                 }
893
894                 if (status == 0) {
895                         pdev->dev.parent = &twl->client->dev;
896                         device_init_wakeup(&pdev->dev, 1);
897                         status = platform_device_add_data(pdev, pdata->usb,
898                                         sizeof(*pdata->usb));
899                         if (status < 0) {
900                                 platform_device_put(pdev);
901                                 dev_dbg(&twl->client->dev,
902                                         "can't add usb data, %d\n",
903                                         status);
904                                 goto err;
905                         }
906                 }
907
908                 if (status == 0) {
909                         struct resource r = {
910                                 .start = TWL4030_PWRIRQ_USB_PRES,
911                                 .flags = IORESOURCE_IRQ,
912                         };
913
914                         status = platform_device_add_resources(pdev, &r, 1);
915                 }
916
917                 if (status == 0)
918                         status = platform_device_add(pdev);
919
920                 if (status < 0) {
921                         platform_device_put(pdev);
922                         dev_dbg(&twl->client->dev,
923                                         "can't create usb dev, %d\n",
924                                         status);
925                 }
926         }
927
928 err:
929         if (status)
930                 pr_err("failed to add twl4030's children (status %d)\n", status);
931         return status;
932 }
933
934 static struct task_struct * __init start_twl4030_irq_thread(long irq)
935 {
936         struct task_struct *thread;
937
938         init_completion(&irq_event);
939         thread = kthread_run(twl4030_irq_thread, (void *)irq,
940                              "twl4030 irq %ld", irq);
941         if (!thread)
942                 pr_err("%s: could not create twl4030 irq %ld thread!\n",
943                        DRIVER_NAME, irq);
944
945         return thread;
946 }
947
948 /*
949  * These three functions should be part of Voltage frame work
950  * added here to complete the functionality for now.
951  */
952 static int __init protect_pm_master(void)
953 {
954         int e = 0;
955
956         e = twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_LOCK,
957                         R_PROTECT_KEY);
958         return e;
959 }
960
961 static int __init unprotect_pm_master(void)
962 {
963         int e = 0;
964
965         e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_UNLOCK1,
966                         R_PROTECT_KEY);
967         e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_UNLOCK2,
968                         R_PROTECT_KEY);
969         return e;
970 }
971
972 static int __init power_companion_init(void)
973 {
974         int e = 0;
975         struct clk *osc;
976         u32 rate;
977         u8 ctrl = HFCLK_FREQ_26_MHZ;
978
979 #if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
980         if (cpu_is_omap2430())
981                 osc = clk_get(NULL, "osc_ck");
982         else
983                 osc = clk_get(NULL, "osc_sys_ck");
984 #else
985         osc = ERR_PTR(-EIO);
986 #endif
987         if (IS_ERR(osc)) {
988                 printk(KERN_WARNING "Skipping twl4030 internal clock init and "
989                                 "using bootloader value (unknown osc rate)\n");
990                 return 0;
991         }
992
993         rate = clk_get_rate(osc);
994         clk_put(osc);
995
996         switch (rate) {
997         case 19200000:
998                 ctrl = HFCLK_FREQ_19p2_MHZ;
999                 break;
1000         case 26000000:
1001                 ctrl = HFCLK_FREQ_26_MHZ;
1002                 break;
1003         case 38400000:
1004                 ctrl = HFCLK_FREQ_38p4_MHZ;
1005                 break;
1006         }
1007
1008         ctrl |= HIGH_PERF_SQ;
1009         e |= unprotect_pm_master();
1010         /* effect->MADC+USB ck en */
1011         e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, ctrl, R_CFG_BOOT);
1012         e |= protect_pm_master();
1013
1014         return e;
1015 }
1016
1017 /**
1018  * twl4030_i2c_clear_isr - clear TWL4030 SIH ISR regs via read + write
1019  * @mod_no: TWL4030 module number
1020  * @reg: register index to clear
1021  * @cor: value of the <module>_SIH_CTRL.COR bit (1 or 0)
1022  *
1023  * Either reads (cor == 1) or writes (cor == 0) to a TWL4030 interrupt
1024  * status register to ensure that any prior interrupts are cleared.
1025  * Returns the status from the I2C read operation.
1026  */
1027 static int __init twl4030_i2c_clear_isr(u8 mod_no, u8 reg, u8 cor)
1028 {
1029         u8 tmp;
1030
1031         return (cor) ? twl4030_i2c_read_u8(mod_no, &tmp, reg) :
1032                 twl4030_i2c_write_u8(mod_no, 0xff, reg);
1033 }
1034
1035 /**
1036  * twl4030_read_cor_bit - are TWL module ISRs cleared by reads or writes?
1037  * @mod_no: TWL4030 module number
1038  * @reg: register index to clear
1039  *
1040  * Returns 1 if the TWL4030 SIH interrupt status registers (ISRs) for
1041  * the specified TWL module are cleared by reads, or 0 if cleared by
1042  * writes.
1043  */
1044 static int twl4030_read_cor_bit(u8 mod_no, u8 reg)
1045 {
1046         u8 tmp = 0;
1047
1048         WARN_ON(twl4030_i2c_read_u8(mod_no, &tmp, reg) < 0);
1049
1050         tmp &= TWL4030_SIH_CTRL_COR_MASK;
1051         tmp >>= __ffs(TWL4030_SIH_CTRL_COR_MASK);
1052
1053         return tmp;
1054 }
1055
1056 /**
1057  * twl4030_mask_clear_intrs - mask and clear all TWL4030 interrupts
1058  * @t: pointer to twl4030_mod_iregs array
1059  * @t_sz: ARRAY_SIZE(t) (starting at 1)
1060  *
1061  * Mask all TWL4030 interrupt mask registers (IMRs) and clear all
1062  * interrupt status registers (ISRs).  No return value, but will WARN if
1063  * any I2C operations fail.
1064  */
1065 static void __init twl4030_mask_clear_intrs(const struct twl4030_mod_iregs *t,
1066                                             const u8 t_sz)
1067 {
1068         int i, j;
1069
1070         /*
1071          * N.B. - further efficiency is possible here.  Eight I2C
1072          * operations on BCI and GPIO modules are avoidable if I2C
1073          * burst read/write transactions were implemented.  Would
1074          * probably save about 1ms of boot time and a small amount of
1075          * power.
1076          */
1077         for (i = 0; i < t_sz; i++) {
1078                 const struct twl4030_mod_iregs tmr = t[i];
1079                 int cor;
1080
1081                 /* Are ISRs cleared by reads or writes? */
1082                 cor = twl4030_read_cor_bit(tmr.mod_no, tmr.sih_ctrl);
1083                 WARN_ON(cor < 0);
1084
1085                 for (j = 0; j < tmr.reg_cnt; j++) {
1086
1087                         /* Mask interrupts at the TWL4030 */
1088                         WARN_ON(twl4030_i2c_write_u8(tmr.mod_no, 0xff,
1089                                                      tmr.imrs[j]) < 0);
1090
1091                         /* Clear TWL4030 ISRs */
1092                         WARN_ON(twl4030_i2c_clear_isr(tmr.mod_no,
1093                                                       tmr.isrs[j], cor) < 0);
1094                 }
1095         }
1096
1097         return;
1098 }
1099
1100
1101 static void twl_init_irq(int irq_num, unsigned irq_base, unsigned irq_end)
1102 {
1103         int     i;
1104         int     res = 0;
1105         char    *msg = "Unable to register interrupt subsystem";
1106
1107         /*
1108          * Mask and clear all TWL4030 interrupts since initially we do
1109          * not have any TWL4030 module interrupt handlers present
1110          */
1111         twl4030_mask_clear_intrs(twl4030_mod_regs,
1112                                  ARRAY_SIZE(twl4030_mod_regs));
1113
1114         twl4030_irq_base = irq_base;
1115
1116         /* install an irq handler for each of the PIH modules */
1117         for (i = irq_base; i < irq_end; i++) {
1118                 set_irq_chip_and_handler(i, &twl4030_irq_chip,
1119                                 do_twl4030_module_irq);
1120                 activate_irq(i);
1121         }
1122
1123         /* install an irq handler to demultiplex the TWL4030 interrupt */
1124         set_irq_data(irq_num, start_twl4030_irq_thread(irq_num));
1125         set_irq_type(irq_num, IRQ_TYPE_EDGE_FALLING);
1126         set_irq_chained_handler(irq_num, do_twl4030_irq);
1127
1128         res = power_companion_init();
1129         if (res < 0)
1130                 pr_err("%s: %s[%d]\n", DRIVER_NAME, msg, res);
1131 }
1132
1133 /*----------------------------------------------------------------------*/
1134
1135 static int twl4030_remove(struct i2c_client *client)
1136 {
1137         unsigned i;
1138
1139         /* FIXME undo twl_init_irq() */
1140         if (twl4030_irq_base) {
1141                 dev_err(&client->dev, "can't yet clean up IRQs?\n");
1142                 return -ENOSYS;
1143         }
1144
1145         for (i = 0; i < TWL4030_NUM_SLAVES; i++) {
1146                 struct twl4030_client   *twl = &twl4030_modules[i];
1147
1148                 if (twl->client && twl->client != client)
1149                         i2c_unregister_device(twl->client);
1150                 twl4030_modules[i].client = NULL;
1151                 twl4030_modules[i].inuse = false;
1152         }
1153         return 0;
1154 }
1155
1156 /* NOTE:  this driver only handles a single twl4030/tps659x0 chip */
1157 static int
1158 twl4030_probe(struct i2c_client *client, const struct i2c_device_id *id)
1159 {
1160         int                             status;
1161         unsigned                        i;
1162         struct twl4030_platform_data    *pdata = client->dev.platform_data;
1163
1164         if (!pdata) {
1165                 dev_dbg(&client->dev, "no platform data?\n");
1166                 return -EINVAL;
1167         }
1168
1169         if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C) == 0) {
1170                 dev_dbg(&client->dev, "can't talk I2C?\n");
1171                 return -EIO;
1172         }
1173
1174         for (i = 0; i < TWL4030_NUM_SLAVES; i++) {
1175                 if (twl4030_modules[i].inuse || twl4030_irq_base) {
1176                         dev_dbg(&client->dev, "driver is already in use\n");
1177                         return -EBUSY;
1178                 }
1179         }
1180
1181         for (i = 0; i < TWL4030_NUM_SLAVES; i++) {
1182                 struct twl4030_client   *twl = &twl4030_modules[i];
1183
1184                 twl->address = client->addr + i;
1185                 if (i == 0)
1186                         twl->client = client;
1187                 else {
1188                         twl->client = i2c_new_dummy(client->adapter,
1189                                         twl->address);
1190                         if (!twl->client) {
1191                                 dev_err(&twl->client->dev,
1192                                         "can't attach client %d\n", i);
1193                                 status = -ENOMEM;
1194                                 goto fail;
1195                         }
1196                         strlcpy(twl->client->name, id->name,
1197                                         sizeof(twl->client->name));
1198                 }
1199                 twl->inuse = true;
1200                 mutex_init(&twl->xfer_lock);
1201         }
1202
1203         /*
1204          * Check if the PIH module is initialized, if yes, then init
1205          * the T2 Interrupt subsystem
1206          */
1207         if (twl4030_modules[twl4030_map[TWL4030_MODULE_PIH].sid].inuse
1208                         && twl4030_irq_base == 0
1209                         && client->irq
1210                         && pdata->irq_base
1211                         && pdata->irq_end > pdata->irq_base) {
1212                 twl_init_irq(client->irq, pdata->irq_base, pdata->irq_end);
1213                 dev_info(&client->dev, "IRQ %d chains IRQs %d..%d\n",
1214                                 client->irq, pdata->irq_base, pdata->irq_end - 1);
1215         }
1216
1217         status = add_children(pdata);
1218 fail:
1219         if (status < 0)
1220                 twl4030_remove(client);
1221         return status;
1222 }
1223
1224 static const struct i2c_device_id twl4030_ids[] = {
1225         { "twl4030", 0 },       /* "Triton 2" */
1226         { "tps65950", 0 },      /* catalog version of twl4030 */
1227         { "tps65930", 0 },      /* fewer LDOs and DACs; no charger */
1228         { "tps65920", 0 },      /* fewer LDOs; no codec or charger */
1229         { /* end of list */ },
1230 };
1231 MODULE_DEVICE_TABLE(i2c, twl4030_ids);
1232
1233 /* One Client Driver , 4 Clients */
1234 static struct i2c_driver twl4030_driver = {
1235         .driver.name    = DRIVER_NAME,
1236         .id_table       = twl4030_ids,
1237         .probe          = twl4030_probe,
1238         .remove         = twl4030_remove,
1239 };
1240
1241 static int __init twl4030_init(void)
1242 {
1243         return i2c_add_driver(&twl4030_driver);
1244 }
1245 subsys_initcall(twl4030_init);
1246
1247 static void __exit twl4030_exit(void)
1248 {
1249         i2c_del_driver(&twl4030_driver);
1250 }
1251 module_exit(twl4030_exit);
1252
1253 MODULE_AUTHOR("Texas Instruments, Inc.");
1254 MODULE_DESCRIPTION("I2C Core interface for TWL4030");
1255 MODULE_LICENSE("GPL");