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