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