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