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