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