]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/i2c/chips/twl4030_core.c
4f0f4652a1e8a0be6092ff053f0592e87897f3be
[linux-2.6-omap-h63xx.git] / drivers / i2c / chips / twl4030_core.c
1 /*
2  * twl4030_core.c - driver for TWL4030 PM and audio CODEC device
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
31 #include <linux/module.h>
32 #include <linux/kernel_stat.h>
33 #include <linux/init.h>
34 #include <linux/time.h>
35 #include <linux/interrupt.h>
36 #include <linux/random.h>
37 #include <linux/syscalls.h>
38 #include <linux/kthread.h>
39
40 #include <linux/i2c.h>
41 #include <linux/i2c/twl4030.h>
42 #include <linux/slab.h>
43 #include <linux/clk.h>
44 #include <linux/device.h>
45
46 #include <asm/irq.h>
47 #include <asm/mach/irq.h>
48
49 #include <asm/arch/gpio.h>
50 #include <asm/arch/mux.h>
51
52 #define DRIVER_NAME                     "twl4030"
53
54 /* Macro Definitions */
55 #define TWL_CLIENT_STRING               "TWL4030-ID"
56 #define TWL_CLIENT_USED                 1
57 #define TWL_CLIENT_FREE                 0
58
59 /* IRQ Flags */
60 #define FREE                            0
61 #define USED                            1
62
63 /* Primary Interrupt Handler on TWL4030 Registers */
64
65 /* Register Definitions */
66
67 #define REG_PIH_ISR_P1                  (0x1)
68 #define REG_PIH_ISR_P2                  (0x2)
69 #define REG_PIH_SIR                     (0x3)
70
71 /* Triton Core internal information (BEGIN) */
72
73 /* Last - for index max*/
74 #define TWL4030_MODULE_LAST             TWL4030_MODULE_SECURED_REG
75
76 /* Slave address */
77 #define TWL4030_NUM_SLAVES              0x04
78 #define TWL4030_SLAVENUM_NUM0           0x00
79 #define TWL4030_SLAVENUM_NUM1           0x01
80 #define TWL4030_SLAVENUM_NUM2           0x02
81 #define TWL4030_SLAVENUM_NUM3           0x03
82 #define TWL4030_SLAVEID_ID0             0x48
83 #define TWL4030_SLAVEID_ID1             0x49
84 #define TWL4030_SLAVEID_ID2             0x4A
85 #define TWL4030_SLAVEID_ID3             0x4B
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 /* Triton Core internal information (END) */
117
118 /* Few power values */
119 #define R_CFG_BOOT                      0x05
120 #define R_PROTECT_KEY                   0x0E
121
122 /* access control */
123 #define KEY_UNLOCK1                     0xce
124 #define KEY_UNLOCK2                     0xec
125 #define KEY_LOCK                        0x00
126
127 #define HFCLK_FREQ_19p2_MHZ             (1 << 0)
128 #define HFCLK_FREQ_26_MHZ               (2 << 0)
129 #define HFCLK_FREQ_38p4_MHZ             (3 << 0)
130 #define HIGH_PERF_SQ                    (1 << 3)
131
132 /* on I2C-1 for 2430SDP */
133 #define CONFIG_I2C_TWL4030_ID           1
134
135 /* Helper functions */
136 static int
137 twl4030_detect_client(struct i2c_adapter *adapter, unsigned char sid);
138 static int twl4030_attach_adapter(struct i2c_adapter *adapter);
139 static int twl4030_detach_client(struct i2c_client *client);
140 static void do_twl4030_irq(unsigned int irq, irq_desc_t *desc);
141
142 static void twl_init_irq(void);
143
144 /* Data Structures */
145 /* To have info on T2 IRQ substem activated or not */
146 static unsigned char twl_irq_used = FREE;
147 static struct completion irq_event;
148
149 /* Structure to define on TWL4030 Slave ID */
150 struct twl4030_client {
151         struct i2c_client client;
152         const char client_name[sizeof(TWL_CLIENT_STRING) + 1];
153         const unsigned char address;
154         const char adapter_index;
155         unsigned char inuse;
156
157         /* max numb of i2c_msg required is for read =2 */
158         struct i2c_msg xfer_msg[2];
159
160         /* To lock access to xfer_msg */
161         struct semaphore xfer_lock;
162 };
163
164 /* Module Mapping */
165 struct twl4030mapping {
166         unsigned char sid;      /* Slave ID */
167         unsigned char base;     /* base address */
168 };
169
170 /* mapping the module id to slave id and base address */
171 static struct twl4030mapping twl4030_map[TWL4030_MODULE_LAST + 1] = {
172         { TWL4030_SLAVENUM_NUM0, TWL4030_BASEADD_USB },
173         { TWL4030_SLAVENUM_NUM1, TWL4030_BASEADD_AUDIO_VOICE },
174         { TWL4030_SLAVENUM_NUM1, TWL4030_BASEADD_GPIO },
175         { TWL4030_SLAVENUM_NUM1, TWL4030_BASEADD_INTBR },
176         { TWL4030_SLAVENUM_NUM1, TWL4030_BASEADD_PIH },
177         { TWL4030_SLAVENUM_NUM1, TWL4030_BASEADD_TEST },
178         { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_KEYPAD },
179         { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_MADC },
180         { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_INTERRUPTS },
181         { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_LED },
182         { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_MAIN_CHARGE },
183         { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_PRECHARGE },
184         { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_PWM0 },
185         { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_PWM1 },
186         { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_PWMA },
187         { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_PWMB },
188         { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_BACKUP },
189         { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_INT },
190         { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_PM_MASTER },
191         { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_PM_RECEIVER },
192         { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_RTC },
193         { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_SECURED_REG },
194 };
195
196 static struct twl4030_client twl4030_modules[TWL4030_NUM_SLAVES] = {
197         {
198                 .address        = TWL4030_SLAVEID_ID0,
199                 .client_name    = TWL_CLIENT_STRING "0",
200                 .adapter_index  = CONFIG_I2C_TWL4030_ID,
201         },
202         {
203                 .address        = TWL4030_SLAVEID_ID1,
204                 .client_name    = TWL_CLIENT_STRING "1",
205                 .adapter_index  = CONFIG_I2C_TWL4030_ID,
206         },
207         {
208                 .address        = TWL4030_SLAVEID_ID2,
209                 .client_name    = TWL_CLIENT_STRING "2",
210                 .adapter_index  = CONFIG_I2C_TWL4030_ID,
211         },
212         {
213                 .address        = TWL4030_SLAVEID_ID3,
214                 .client_name    = TWL_CLIENT_STRING "3",
215                 .adapter_index  = CONFIG_I2C_TWL4030_ID,
216         },
217 };
218
219 /* One Client Driver , 4 Clients */
220 static struct i2c_driver twl4030_driver = {
221         .driver.name    = "TWL4030 I2C",
222         .attach_adapter = twl4030_attach_adapter,
223         .detach_client  = twl4030_detach_client,
224 };
225
226 /*
227  * TWL4030 doesn't have PIH mask, hence dummy function for mask
228  * and unmask.
229  */
230
231 static void twl4030_i2c_ackirq(unsigned int irq) {}
232 static void twl4030_i2c_disableint(unsigned int irq) {}
233 static void twl4030_i2c_enableint(unsigned int irq) {}
234
235 /* information for processing in the Work Item */
236 static struct irq_chip twl4030_irq_chip = {
237         .ack    = twl4030_i2c_ackirq,
238         .mask   = twl4030_i2c_disableint,
239         .unmask = twl4030_i2c_enableint,
240 };
241
242 /* Global Functions */
243 /*
244  * @brief twl4030_i2c_write - Writes a n bit register in TWL4030
245  *
246  * @param mod_no - module number
247  * @param *value - an array of num_bytes+1 containing data to write
248  * IMPORTANT - Allocate value num_bytes+1 and valid data starts at
249  *               Offset 1.
250  * @param reg - register address (just offset will do)
251  * @param num_bytes - number of bytes to transfer
252  *
253  * @return result of operation - 0 is success
254  */
255 int twl4030_i2c_write(u8 mod_no, u8 * value, u8 reg, u8 num_bytes)
256 {
257         int ret;
258         int sid;
259         struct twl4030_client *client;
260         struct i2c_msg *msg;
261
262         if (unlikely(mod_no > TWL4030_MODULE_LAST)) {
263                 pr_err("Invalid module Number\n");
264                 return -EPERM;
265         }
266         sid = twl4030_map[mod_no].sid;
267         client = &(twl4030_modules[sid]);
268
269         if (unlikely(client->inuse != TWL_CLIENT_USED)) {
270                 pr_err("I2C Client[%d] is not initialized[%d]\n",
271                        sid, __LINE__);
272                 return -EPERM;
273         }
274         down(&(client->xfer_lock));
275         /*
276          * [MSG1]: fill the register address data
277          * fill the data Tx buffer
278          */
279         msg = &(client->xfer_msg[0]);
280         msg->addr = client->address;
281         msg->len = num_bytes + 1;
282         msg->flags = 0;
283         msg->buf = value;
284         /* over write the first byte of buffer with the register address */
285         *value = twl4030_map[mod_no].base + reg;
286         ret = i2c_transfer(client->client.adapter, client->xfer_msg, 1);
287         up(&(client->xfer_lock));
288
289         /* i2cTransfer returns num messages.translate it pls.. */
290         if (ret >= 0)
291                 ret = 0;
292         return ret;
293 }
294
295 /**
296  * @brief twl4030_i2c_read - Reads a n bit register in TWL4030
297  *
298  * @param mod_no - module number
299  * @param *value - an array of num_bytes containing data to be read
300  * @param reg - register address (just offset will do)
301  * @param num_bytes - number of bytes to transfer
302  *
303  * @return result of operation - num_bytes is success else failure.
304  */
305 int twl4030_i2c_read(u8 mod_no, u8 * value, u8 reg, u8 num_bytes)
306 {
307         int ret;
308         u8 val;
309         int sid;
310         struct twl4030_client *client;
311         struct i2c_msg *msg;
312
313         if (unlikely(mod_no > TWL4030_MODULE_LAST)) {
314                 pr_err("Invalid module Number\n");
315                 return -EPERM;
316         }
317         sid = twl4030_map[mod_no].sid;
318         client = &(twl4030_modules[sid]);
319
320         if (unlikely(client->inuse != TWL_CLIENT_USED)) {
321                 pr_err("I2C Client[%d] is not initialized[%d]\n", sid,
322                        __LINE__);
323                 return -EPERM;
324         }
325         down(&(client->xfer_lock));
326         /* [MSG1] fill the register address data */
327         msg = &(client->xfer_msg[0]);
328         msg->addr = client->address;
329         msg->len = 1;
330         msg->flags = 0; /* Read the register value */
331         val = twl4030_map[mod_no].base + reg;
332         msg->buf = &val;
333         /* [MSG2] fill the data rx buffer */
334         msg = &(client->xfer_msg[1]);
335         msg->addr = client->address;
336         msg->flags = I2C_M_RD;  /* Read the register value */
337         msg->len = num_bytes;   /* only n bytes */
338         msg->buf = value;
339         ret = i2c_transfer(client->client.adapter, client->xfer_msg, 2);
340         up(&(client->xfer_lock));
341
342         /* i2cTransfer returns num messages.translate it pls.. */
343         if (ret >= 0)
344                 ret = 0;
345         return ret;
346 }
347
348 /**
349  * @brief twl4030_i2c_write_u8 - Writes a 8 bit register in TWL4030
350  *
351  * @param mod_no - module number
352  * @param value - the value to be written 8 bit
353  * @param reg - register address (just offset will do)
354  *
355  * @return result of operation - 0 is success
356  */
357 int twl4030_i2c_write_u8(u8 mod_no, u8 value, u8 reg)
358 {
359
360         /* 2 bytes offset 1 contains the data offset 0 is used by i2c_write */
361         u8 temp_buffer[2] = { 0 };
362         /* offset 1 contains the data */
363         temp_buffer[1] = value;
364         return twl4030_i2c_write(mod_no, temp_buffer, reg, 1);
365 }
366
367 /**
368  * @brief twl4030_i2c_read_u8 - Reads a 8 bit register from TWL4030
369  *
370  * @param mod_no - module number
371  * @param *value - the value read 8 bit
372  * @param reg - register address (just offset will do)
373  *
374  * @return result of operation - 0 is success
375  */
376 int twl4030_i2c_read_u8(u8 mod_no, u8 * value, u8 reg)
377 {
378         return twl4030_i2c_read(mod_no, value, reg, 1);
379 }
380
381 /* Helper Functions */
382
383 /*
384  * do_twl4030_module_irq() is the desc->handle method for each of the twl4030
385  * module interrupts.  It executes in kernel thread context.
386  * On entry, cpu interrupts are disabled.
387  */
388 static void do_twl4030_module_irq(unsigned int irq, irq_desc_t *desc)
389 {
390         struct irqaction *action;
391         const unsigned int cpu = smp_processor_id();
392
393         /*
394          * Earlier this was desc->triggered = 1;
395          */
396         desc->status |= IRQ_LEVEL;
397
398         /*
399          * The desc->handle method would normally call the desc->chip->ack
400          * method here, but we won't bother since our ack method is NULL.
401          */
402
403         if (!desc->depth) {
404                 kstat_cpu(cpu).irqs[irq]++;
405
406                 action = desc->action;
407                 if (action) {
408                         int ret;
409                         int status = 0;
410                         int retval = 0;
411
412                         local_irq_enable();
413
414                         do {
415                                 /* Call the ISR with cpu interrupts enabled */
416                                 ret = action->handler(irq, action->dev_id);
417                                 if (ret == IRQ_HANDLED)
418                                         status |= action->flags;
419                                 retval |= ret;
420                                 action = action->next;
421                         } while (action);
422
423                         if (status & IRQF_SAMPLE_RANDOM)
424                                 add_interrupt_randomness(irq);
425
426                         local_irq_disable();
427
428                         if (retval != IRQ_HANDLED)
429                                 printk(KERN_ERR "ISR for TWL4030 module"
430                                         " irq %d can't handle interrupt\n", irq);
431
432                         /*
433                          * Here is where we should call the unmask method, but
434                          * again we won't bother since it is NULL.
435                          */
436                 } else
437                         printk(KERN_CRIT "TWL4030 module irq %d has no ISR"
438                                         " but can't be masked!\n", irq);
439         } else
440                 printk(KERN_CRIT "TWL4030 module irq %d is disabled but can't"
441                                 " be masked!\n", irq);
442 }
443
444 /*
445  * twl4030_irq_thread() runs as a kernel thread.  It queries the twl4030
446  * interrupt controller to see which modules are generating interrupt requests
447  * and then calls the desc->handle method for each module requesting service.
448  */
449 static int twl4030_irq_thread(void *data)
450 {
451         int irq = (int)data;
452         irq_desc_t *desc = irq_desc + irq;
453         static unsigned i2c_errors;
454         const static unsigned max_i2c_errors = 100;
455
456         daemonize("twl4030-irq");
457         current->flags |= PF_NOFREEZE;
458
459         while (!kthread_should_stop()) {
460                 int ret;
461                 int module_irq;
462                 u8 pih_isr;
463
464                 wait_for_completion_interruptible(&irq_event);
465
466                 ret = twl4030_i2c_read_u8(TWL4030_MODULE_PIH, &pih_isr,
467                                           REG_PIH_ISR_P1);
468                 if (ret) {
469                         printk(KERN_WARNING "I2C error %d while reading TWL4030"
470                                         " PIH ISR register.\n", ret);
471                         if (++i2c_errors >= max_i2c_errors) {
472                                 printk(KERN_ERR "Maximum I2C error count"
473                                                 " exceeded.  Terminating %s.\n",
474                                                 __FUNCTION__);
475                                 break;
476                         }
477                         continue;
478                 }
479
480                 for (module_irq = IH_TWL4030_BASE; 0 != pih_isr;
481                          pih_isr >>= 1, module_irq++) {
482                         if (pih_isr & 0x1) {
483                                 irq_desc_t *d = irq_desc + module_irq;
484
485                                 local_irq_disable();
486
487                                 d->handle_irq(module_irq, d);
488
489                                 local_irq_enable();
490                         }
491                 }
492
493                 desc->chip->unmask(irq);
494         }
495
496         return 0;
497 }
498
499 /*
500  * do_twl4030_irq() is the desc->handle method for the twl4030 interrupt.
501  * This is a chained interrupt, so there is no desc->action method for it.
502  * Now we need to query the interrupt controller in the twl4030 to determine
503  * which module is generating the interrupt request.  However, we can't do i2c
504  * transactions in interrupt context, so we must defer that work to a kernel
505  * thread.  All we do here is acknowledge and mask the interrupt and wakeup
506  * the kernel thread.
507  */
508 static void do_twl4030_irq(unsigned int irq, irq_desc_t *desc)
509 {
510         const unsigned int cpu = smp_processor_id();
511
512         /*
513          * Earlier this was desc->triggered = 1;
514          */
515         desc->status |= IRQ_LEVEL;
516
517         /*
518          * Acknowledge, clear _AND_ disable the interrupt.
519          */
520         desc->chip->ack(irq);
521
522         if (!desc->depth) {
523                 kstat_cpu(cpu).irqs[irq]++;
524
525                 complete(&irq_event);
526         }
527 }
528
529 /* attach a client to the adapter */
530 static int twl4030_detect_client(struct i2c_adapter *adapter, unsigned char sid)
531 {
532         int err = 0;
533         struct twl4030_client *client;
534
535         if (unlikely(sid >= TWL4030_NUM_SLAVES)) {
536                 pr_err("sid[%d] > MOD_LAST[%d]\n", sid, TWL4030_NUM_SLAVES);
537                 return -EPERM;
538         }
539
540         /* Check basic functionality */
541         if (!(err = i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA |
542                                                 I2C_FUNC_SMBUS_WRITE_BYTE))) {
543                 pr_err("SlaveID=%d functionality check failed\n", sid);
544                 return err;
545         }
546         client = &(twl4030_modules[sid]);
547         if (unlikely(client->inuse)) {
548                 pr_err("Client %s is already in use\n", client->client_name);
549                 return -EPERM;
550         }
551
552         memset(&(client->client), 0, sizeof(struct i2c_client));
553
554         client->client.addr     = client->address;
555         client->client.adapter  = adapter;
556         client->client.driver   = &twl4030_driver;
557
558         memcpy(&(client->client.name), client->client_name,
559                         sizeof(TWL_CLIENT_STRING) + 1);
560
561         pr_info("TWL4030: TRY attach Slave %s on Adapter %s [%x]\n",
562                                 client->client_name, adapter->name, err);
563
564         if ((err = i2c_attach_client(&(client->client)))) {
565                 pr_err("Couldn't attach Slave %s on Adapter"
566                        "%s [%x]\n", client->client_name, adapter->name, err);
567         } else {
568                 client->inuse = TWL_CLIENT_USED;
569                 init_MUTEX(&client->xfer_lock);
570         }
571
572         return err;
573 }
574
575 /* adapter callback */
576 static int twl4030_attach_adapter(struct i2c_adapter *adapter)
577 {
578         int i;
579         int ret = 0;
580         static int twl_i2c_adapter = 1;
581
582         for (i = 0; i < TWL4030_NUM_SLAVES; i++) {
583                 /* Check if I need to hook on to this adapter or not */
584                 if (twl4030_modules[i].adapter_index == twl_i2c_adapter) {
585                         if ((ret = twl4030_detect_client(adapter, i)))
586                                 goto free_client;
587                 }
588         }
589         twl_i2c_adapter++;
590
591         /*
592          * Check if the PIH module is initialized, if yes, then init
593          * the T2 Interrupt subsystem
594          */
595         if ((twl4030_modules[twl4030_map[TWL4030_MODULE_PIH].sid].inuse ==
596                 TWL_CLIENT_USED) && (twl_irq_used != USED)) {
597                 twl_init_irq();
598                 twl_irq_used = USED;
599         }
600         return 0;
601
602 free_client:
603         pr_err("TWL_CLIENT(Idx=%d] registration failed[0x%x]\n",i,ret);
604
605         /* ignore current slave..it never got registered */
606         i--;
607         while (i >= 0) {
608                 /* now remove all those from the current adapter... */
609                 if (twl4030_modules[i].adapter_index == twl_i2c_adapter)
610                         (void)twl4030_detach_client(&(twl4030_modules[i].client));
611                 i--;
612         }
613         return ret;
614 }
615
616 /* adapter's callback */
617 static int twl4030_detach_client(struct i2c_client *iclient)
618 {
619         int err;
620         if ((err = i2c_detach_client(iclient))) {
621                 pr_err("Client detach failed\n");
622                 return err;
623         }
624         return 0;
625 }
626
627 struct task_struct *start_twl4030_irq_thread(int irq)
628 {
629         struct task_struct *thread;
630
631         init_completion(&irq_event);
632         thread = kthread_run(twl4030_irq_thread, (void *)irq,
633                              "twl4030 irq %d", irq);
634         if (!thread)
635                 pr_err("%s: could not create twl4030 irq %d thread!\n",
636                        __FUNCTION__, irq);
637
638         return thread;
639 }
640
641 /*
642  * These three functions should be part of Voltage frame work
643  * added here to complete the functionality for now.
644  */
645 static int protect_pm_master(void)
646 {
647         int e = 0;
648
649         e = twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_LOCK,
650                         R_PROTECT_KEY);
651         return e;
652 }
653
654 static int unprotect_pm_master(void)
655 {
656         int e = 0;
657
658         e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_UNLOCK1,
659                         R_PROTECT_KEY);
660         e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_UNLOCK2,
661                         R_PROTECT_KEY);
662         return e;
663 }
664
665 static int power_companion_init(void)
666 {
667         struct clk *osc;
668         u32 rate, ctrl = HFCLK_FREQ_26_MHZ;
669         int e = 0;
670
671         osc = clk_get(NULL,"osc_ck");
672         rate = clk_get_rate(osc);
673         clk_put(osc);
674
675         switch(rate) {
676                 case 19200000 : ctrl = HFCLK_FREQ_19p2_MHZ; break;
677                 case 26000000 : ctrl = HFCLK_FREQ_26_MHZ; break;
678                 case 38400000 : ctrl = HFCLK_FREQ_38p4_MHZ; break;
679         }
680
681         ctrl |= HIGH_PERF_SQ;
682         e |= unprotect_pm_master();
683                         /* effect->MADC+USB ck en */
684         e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, ctrl, R_CFG_BOOT);
685         e |= protect_pm_master();
686
687         return e;
688 }
689
690 static void twl_init_irq(void)
691 {
692         int     i = 0;
693         int     res = 0;
694         char    *msg = "Unable to register interrupt subsystem";
695
696         /*
697          * We end up with interrupts from other modules before
698          * they get a chance to handle them...
699          */
700         /* PWR_ISR1 */
701         res = twl4030_i2c_write_u8(TWL4030_MODULE_INT, 0xFF, 0x00);
702         if (res < 0) {
703                 pr_err("%s[%d][%d]\n", msg, res, __LINE__);
704                 return;
705         }
706
707         /* PWR_ISR2 */
708         res = twl4030_i2c_write_u8(TWL4030_MODULE_INT, 0xFF, 0x02);
709         if (res < 0) {
710                 pr_err("%s[%d][%d]\n", msg, res, __LINE__);
711                 return;
712         }
713
714         /* PWR_IMR1 */
715         res = twl4030_i2c_write_u8(TWL4030_MODULE_INT, 0xFF, 0x1);
716         if (res < 0) {
717                 pr_err("%s[%d][%d]\n", msg, res, __LINE__);
718                 return;
719         }
720
721         /* PWR_IMR2 */
722         res = twl4030_i2c_write_u8(TWL4030_MODULE_INT, 0xFF, 0x3);
723         if (res < 0) {
724                 pr_err("%s[%d][%d]\n", msg, res, __LINE__);
725                 return;
726         }
727
728         /* Clear off any other pending interrupts on power */
729         /* PWR_ISR1 */
730         res = twl4030_i2c_write_u8(TWL4030_MODULE_INT, 0xFF, 0x00);
731         if (res < 0) {
732                 pr_err("%s[%d][%d]\n", msg, res, __LINE__);
733                 return;
734         }
735
736         /* PWR_ISR2 */
737         res = twl4030_i2c_write_u8(TWL4030_MODULE_INT, 0xFF, 0x02);
738         if (res < 0) {
739                 pr_err("%s[%d][%d]\n", msg, res, __LINE__);
740                 return;
741         }
742         /* POWER HACK (END) */
743         /* Slave address 0x4A */
744
745         /* BCIIMR1_1 */
746         res = twl4030_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, 0xFF, 0x3);
747         if (res < 0) {
748                 pr_err("%s[%d][%d]\n", msg, res, __LINE__);
749                 return;
750         }
751
752         /* BCIIMR1_2 */
753         res = twl4030_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, 0xFF, 0x4);
754         if (res < 0) {
755                 pr_err("%s[%d][%d]\n", msg, res, __LINE__);
756                 return;
757         }
758
759         /* BCIIMR2_1 */
760         res = twl4030_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, 0xFF, 0x7);
761         if (res < 0) {
762                 pr_err("%s[%d][%d]\n", msg, res, __LINE__);
763                 return;
764         }
765
766         /* BCIIMR2_2 */
767         res = twl4030_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, 0xFF, 0x8);
768         if (res < 0) {
769                 pr_err("%s[%d][%d]\n", msg, res, __LINE__);
770                 return;
771         }
772
773         /* MAD C */
774         /* MADC_IMR1 */
775         res = twl4030_i2c_write_u8(TWL4030_MODULE_MADC, 0xFF, 0x62);
776         if (res < 0) {
777                 pr_err("%s[%d][%d]\n", msg, res, __LINE__);
778                 return;
779         }
780
781         /* MADC_IMR2 */
782         res = twl4030_i2c_write_u8(TWL4030_MODULE_MADC, 0xFF, 0x64);
783         if (res < 0) {
784                 pr_err("%s[%d][%d]\n", msg, res, __LINE__);
785                 return;
786         }
787
788         /* key Pad */
789         /* KEYPAD - IMR1 */
790         res = twl4030_i2c_write_u8(TWL4030_MODULE_KEYPAD, 0xFF, (0x12));
791         if (res < 0) {
792                 pr_err("%s[%d][%d]\n", msg, res, __LINE__);
793                 return;
794         }
795         {
796                 u8 clear;
797                 /* Clear ISR */
798                 twl4030_i2c_read_u8(TWL4030_MODULE_KEYPAD, &clear, 0x11);
799                 twl4030_i2c_read_u8(TWL4030_MODULE_KEYPAD, &clear, 0x11);
800         }
801
802         /* KEYPAD - IMR2 */
803         res = twl4030_i2c_write_u8(TWL4030_MODULE_KEYPAD, 0xFF, (0x14));
804         if (res < 0) {
805                 pr_err("%s[%d][%d]\n", msg, res, __LINE__);
806                 return;
807         }
808
809         /* Slave address 0x49 */
810         /* GPIO_IMR1A */
811         res = twl4030_i2c_write_u8(TWL4030_MODULE_GPIO, 0xFF, (0x1C));
812         if (res < 0) {
813                 pr_err("%s[%d][%d]\n", msg, res, __LINE__);
814                 return;
815         }
816
817         /* GPIO_IMR2A */
818         res = twl4030_i2c_write_u8(TWL4030_MODULE_GPIO, 0xFF, (0x1D));
819         if (res < 0) {
820                 pr_err("%s[%d][%d]\n", msg, res, __LINE__);
821                 return;
822         }
823
824         /* GPIO_IMR3A */
825         res = twl4030_i2c_write_u8(TWL4030_MODULE_GPIO, 0xFF, (0x1E));
826         if (res < 0) {
827                 pr_err("%s[%d][%d]\n", msg, res, __LINE__);
828                 return;
829         }
830
831         /* GPIO_IMR1B */
832         res = twl4030_i2c_write_u8(TWL4030_MODULE_GPIO, 0xFF, (0x22));
833         if (res < 0) {
834                 pr_err("%s[%d][%d]\n", msg, res, __LINE__);
835                 return;
836         }
837
838         /* GPIO_IMR2B */
839         res = twl4030_i2c_write_u8(TWL4030_MODULE_GPIO, 0xFF, (0x23));
840         if (res < 0) {
841                 pr_err("%s[%d][%d]\n", msg, res, __LINE__);
842                 return;
843         }
844
845         /* GPIO_IMR3B */
846         res = twl4030_i2c_write_u8(TWL4030_MODULE_GPIO, 0xFF, (0x24));
847         if (res < 0) {
848                 pr_err("%s[%d][%d]\n", msg, res, __LINE__);
849                 return;
850         }
851
852         /* install an irq handler for each of the PIH modules */
853         for (i = IH_TWL4030_BASE; i < IH_TWL4030_END; i++) {
854                 set_irq_chip(i, &twl4030_irq_chip);
855                 set_irq_handler(i, do_twl4030_module_irq);
856                 set_irq_flags(i, IRQF_VALID);
857         }
858
859         /* install an irq handler to demultiplex the TWL4030 interrupt */
860         set_irq_data(TWL4030_IRQNUM, start_twl4030_irq_thread(TWL4030_IRQNUM));
861         set_irq_type(TWL4030_IRQNUM, IRQT_FALLING);
862         set_irq_chained_handler(TWL4030_IRQNUM, do_twl4030_irq);
863
864         res = power_companion_init();
865         if (res < 0) {
866                 pr_err("%s[%d][%d]\n", msg, res, __LINE__);
867         }
868 }
869
870 static int __init twl4030_init(void)
871 {
872         int res;
873
874         if ((res = i2c_add_driver(&twl4030_driver))) {
875                 printk(KERN_ERR "TWL4030: Driver registration failed \n");
876                 return res;
877         }
878
879         pr_info(KERN_INFO "TWL4030: Driver registration complete.\n");
880
881         return 0;
882 }
883
884 static void __exit twl4030_exit(void)
885 {
886         i2c_del_driver(&twl4030_driver);
887         twl_irq_used = FREE;
888 }
889
890 subsys_initcall(twl4030_init);
891 module_exit(twl4030_exit);
892
893 EXPORT_SYMBOL(twl4030_i2c_write_u8);
894 EXPORT_SYMBOL(twl4030_i2c_read_u8);
895 EXPORT_SYMBOL(twl4030_i2c_read);
896 EXPORT_SYMBOL(twl4030_i2c_write);
897
898 MODULE_AUTHOR("Texas Instruments, Inc.");
899 MODULE_DESCRIPTION("I2C Core interface for TWL4030");
900 MODULE_LICENSE("GPL");