]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/i2c/chips/twl4030-core.c
I2C: TWL4030: Fix wrong variable warning
[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         .ack    = twl4030_i2c_ackirq,
242         .mask   = twl4030_i2c_disableint,
243         .unmask = twl4030_i2c_enableint,
244 };
245
246 /* Global Functions */
247 /*
248  * @brief twl4030_i2c_write - Writes a n bit register in TWL4030
249  *
250  * @param mod_no - module number
251  * @param *value - an array of num_bytes+1 containing data to write
252  * IMPORTANT - Allocate value num_bytes+1 and valid data starts at
253  *               Offset 1.
254  * @param reg - register address (just offset will do)
255  * @param num_bytes - number of bytes to transfer
256  *
257  * @return result of operation - 0 is success
258  */
259 int twl4030_i2c_write(u8 mod_no, u8 *value, u8 reg, u8 num_bytes)
260 {
261         int ret;
262         int sid;
263         struct twl4030_client *twl;
264         struct i2c_msg *msg;
265
266         if (unlikely(mod_no > TWL4030_MODULE_LAST)) {
267                 pr_err("Invalid module Number\n");
268                 return -EPERM;
269         }
270         sid = twl4030_map[mod_no].sid;
271         twl = &twl4030_modules[sid];
272
273         if (unlikely(twl->inuse != TWL_CLIENT_USED)) {
274                 pr_err("I2C Client[%d] is not initialized[%d]\n",
275                        sid, __LINE__);
276                 return -EPERM;
277         }
278         mutex_lock(&twl->xfer_lock);
279         /*
280          * [MSG1]: fill the register address data
281          * fill the data Tx buffer
282          */
283         msg = &twl->xfer_msg[0];
284         msg->addr = twl->address;
285         msg->len = num_bytes + 1;
286         msg->flags = 0;
287         msg->buf = value;
288         /* over write the first byte of buffer with the register address */
289         *value = twl4030_map[mod_no].base + reg;
290         ret = i2c_transfer(twl->client.adapter, twl->xfer_msg, 1);
291         mutex_unlock(&twl->xfer_lock);
292
293         /* i2cTransfer returns num messages.translate it pls.. */
294         if (ret >= 0)
295                 ret = 0;
296         return ret;
297 }
298 EXPORT_SYMBOL(twl4030_i2c_write);
299
300 /**
301  * @brief twl4030_i2c_read - Reads a n bit register in TWL4030
302  *
303  * @param mod_no - module number
304  * @param *value - an array of num_bytes containing data to be read
305  * @param reg - register address (just offset will do)
306  * @param num_bytes - number of bytes to transfer
307  *
308  * @return result of operation - num_bytes is success else failure.
309  */
310 int twl4030_i2c_read(u8 mod_no, u8 *value, u8 reg, u8 num_bytes)
311 {
312         int ret;
313         u8 val;
314         int sid;
315         struct twl4030_client *twl;
316         struct i2c_msg *msg;
317
318         if (unlikely(mod_no > TWL4030_MODULE_LAST)) {
319                 pr_err("Invalid module Number\n");
320                 return -EPERM;
321         }
322         sid = twl4030_map[mod_no].sid;
323         twl = &twl4030_modules[sid];
324
325         if (unlikely(twl->inuse != TWL_CLIENT_USED)) {
326                 pr_err("I2C Client[%d] is not initialized[%d]\n", sid,
327                        __LINE__);
328                 return -EPERM;
329         }
330         mutex_lock(&twl->xfer_lock);
331         /* [MSG1] fill the register address data */
332         msg = &twl->xfer_msg[0];
333         msg->addr = twl->address;
334         msg->len = 1;
335         msg->flags = 0; /* Read the register value */
336         val = twl4030_map[mod_no].base + reg;
337         msg->buf = &val;
338         /* [MSG2] fill the data rx buffer */
339         msg = &twl->xfer_msg[1];
340         msg->addr = twl->address;
341         msg->flags = I2C_M_RD;  /* Read the register value */
342         msg->len = num_bytes;   /* only n bytes */
343         msg->buf = value;
344         ret = i2c_transfer(twl->client.adapter, twl->xfer_msg, 2);
345         mutex_unlock(&twl->xfer_lock);
346
347         /* i2cTransfer returns num messages.translate it pls.. */
348         if (ret >= 0)
349                 ret = 0;
350         return ret;
351 }
352 EXPORT_SYMBOL(twl4030_i2c_read);
353
354 /**
355  * @brief twl4030_i2c_write_u8 - Writes a 8 bit register in TWL4030
356  *
357  * @param mod_no - module number
358  * @param value - the value to be written 8 bit
359  * @param reg - register address (just offset will do)
360  *
361  * @return result of operation - 0 is success
362  */
363 int twl4030_i2c_write_u8(u8 mod_no, u8 value, u8 reg)
364 {
365
366         /* 2 bytes offset 1 contains the data offset 0 is used by i2c_write */
367         u8 temp_buffer[2] = { 0 };
368         /* offset 1 contains the data */
369         temp_buffer[1] = value;
370         return twl4030_i2c_write(mod_no, temp_buffer, reg, 1);
371 }
372 EXPORT_SYMBOL(twl4030_i2c_write_u8);
373
374 /**
375  * @brief twl4030_i2c_read_u8 - Reads a 8 bit register from TWL4030
376  *
377  * @param mod_no - module number
378  * @param *value - the value read 8 bit
379  * @param reg - register address (just offset will do)
380  *
381  * @return result of operation - 0 is success
382  */
383 int twl4030_i2c_read_u8(u8 mod_no, u8 *value, u8 reg)
384 {
385         return twl4030_i2c_read(mod_no, value, reg, 1);
386 }
387 EXPORT_SYMBOL(twl4030_i2c_read_u8);
388
389 /* Helper Functions */
390
391 /*
392  * do_twl4030_module_irq() is the desc->handle method for each of the twl4030
393  * module interrupts.  It executes in kernel thread context.
394  * On entry, cpu interrupts are disabled.
395  */
396 static void do_twl4030_module_irq(unsigned int irq, irq_desc_t *desc)
397 {
398         struct irqaction *action;
399         const unsigned int cpu = smp_processor_id();
400
401         /*
402          * Earlier this was desc->triggered = 1;
403          */
404         desc->status |= IRQ_LEVEL;
405
406         /*
407          * The desc->handle method would normally call the desc->chip->ack
408          * method here, but we won't bother since our ack method is NULL.
409          */
410
411         if (!desc->depth) {
412                 kstat_cpu(cpu).irqs[irq]++;
413
414                 action = desc->action;
415                 if (action) {
416                         int ret;
417                         int status = 0;
418                         int retval = 0;
419
420                         local_irq_enable();
421
422                         do {
423                                 /* Call the ISR with cpu interrupts enabled */
424                                 ret = action->handler(irq, action->dev_id);
425                                 if (ret == IRQ_HANDLED)
426                                         status |= action->flags;
427                                 retval |= ret;
428                                 action = action->next;
429                         } while (action);
430
431                         if (status & IRQF_SAMPLE_RANDOM)
432                                 add_interrupt_randomness(irq);
433
434                         local_irq_disable();
435
436                         if (retval != IRQ_HANDLED)
437                                 printk(KERN_ERR "ISR for TWL4030 module"
438                                         " irq %d can't handle interrupt\n",
439                                         irq);
440
441                         /*
442                          * Here is where we should call the unmask method, but
443                          * again we won't bother since it is NULL.
444                          */
445                 } else
446                         printk(KERN_CRIT "TWL4030 module irq %d has no ISR"
447                                         " but can't be masked!\n", irq);
448         } else
449                 printk(KERN_CRIT "TWL4030 module irq %d is disabled but can't"
450                                 " be masked!\n", irq);
451 }
452
453 /*
454  * twl4030_irq_thread() runs as a kernel thread.  It queries the twl4030
455  * interrupt controller to see which modules are generating interrupt requests
456  * and then calls the desc->handle method for each module requesting service.
457  */
458 static int twl4030_irq_thread(void *data)
459 {
460         int irq = (int)data;
461         irq_desc_t *desc = irq_desc + irq;
462         static unsigned i2c_errors;
463         const static unsigned max_i2c_errors = 100;
464
465         daemonize("twl4030-irq");
466         current->flags |= PF_NOFREEZE;
467
468         while (!kthread_should_stop()) {
469                 int ret;
470                 int module_irq;
471                 u8 pih_isr;
472
473                 wait_for_completion_interruptible(&irq_event);
474
475                 ret = twl4030_i2c_read_u8(TWL4030_MODULE_PIH, &pih_isr,
476                                           REG_PIH_ISR_P1);
477                 if (ret) {
478                         printk(KERN_WARNING "I2C error %d while reading TWL4030"
479                                         " PIH ISR register.\n", ret);
480                         if (++i2c_errors >= max_i2c_errors) {
481                                 printk(KERN_ERR "Maximum I2C error count"
482                                                 " exceeded.  Terminating %s.\n",
483                                                 __func__);
484                                 break;
485                         }
486                         continue;
487                 }
488
489                 for (module_irq = IH_TWL4030_BASE; 0 != pih_isr;
490                          pih_isr >>= 1, module_irq++) {
491                         if (pih_isr & 0x1) {
492                                 irq_desc_t *d = irq_desc + module_irq;
493
494                                 local_irq_disable();
495
496                                 d->handle_irq(module_irq, d);
497
498                                 local_irq_enable();
499                         }
500                 }
501
502                 desc->chip->unmask(irq);
503         }
504
505         return 0;
506 }
507
508 /*
509  * do_twl4030_irq() is the desc->handle method for the twl4030 interrupt.
510  * This is a chained interrupt, so there is no desc->action method for it.
511  * Now we need to query the interrupt controller in the twl4030 to determine
512  * which module is generating the interrupt request.  However, we can't do i2c
513  * transactions in interrupt context, so we must defer that work to a kernel
514  * thread.  All we do here is acknowledge and mask the interrupt and wakeup
515  * the kernel thread.
516  */
517 static void do_twl4030_irq(unsigned int irq, irq_desc_t *desc)
518 {
519         const unsigned int cpu = smp_processor_id();
520
521         /*
522          * Earlier this was desc->triggered = 1;
523          */
524         desc->status |= IRQ_LEVEL;
525
526         /*
527          * Acknowledge, clear _AND_ disable the interrupt.
528          */
529         desc->chip->ack(irq);
530
531         if (!desc->depth) {
532                 kstat_cpu(cpu).irqs[irq]++;
533
534                 complete(&irq_event);
535         }
536 }
537
538 /* attach a client to the adapter */
539 static int twl4030_detect_client(struct i2c_adapter *adapter, unsigned char sid)
540 {
541         int err = 0;
542         struct twl4030_client *twl;
543
544         if (unlikely(sid >= TWL4030_NUM_SLAVES)) {
545                 pr_err("sid[%d] > MOD_LAST[%d]\n", sid, TWL4030_NUM_SLAVES);
546                 return -EPERM;
547         }
548
549         /* Check basic functionality */
550         err = i2c_check_functionality(adapter,
551                         I2C_FUNC_SMBUS_WORD_DATA
552                         | I2C_FUNC_SMBUS_WRITE_BYTE);
553         if (!err) {
554                 pr_err("SlaveID=%d functionality check failed\n", sid);
555                 return err;
556         }
557         twl = &twl4030_modules[sid];
558         if (unlikely(twl->inuse)) {
559                 pr_err("Client %s is already in use\n", twl->client_name);
560                 return -EPERM;
561         }
562
563         memset(&twl->client, 0, sizeof(struct i2c_client));
564
565         twl->client.addr        = twl->address;
566         twl->client.adapter     = adapter;
567         twl->client.driver      = &twl4030_driver;
568
569         memcpy(&twl->client.name, twl->client_name,
570                         sizeof(TWL_CLIENT_STRING) + 1);
571
572         pr_info("TWL4030: TRY attach Slave %s on Adapter %s [%x]\n",
573                                 twl->client_name, adapter->name, err);
574
575         err = i2c_attach_client(&twl->client);
576         if (err) {
577                 pr_err("Couldn't attach Slave %s on Adapter"
578                        "%s [%x]\n", twl->client_name, adapter->name, err);
579         } else {
580                 twl->inuse = TWL_CLIENT_USED;
581                 mutex_init(&twl->xfer_lock);
582         }
583
584         return err;
585 }
586
587 /* adapter callback */
588 static int twl4030_attach_adapter(struct i2c_adapter *adapter)
589 {
590         int i;
591         int ret = 0;
592         static int twl_i2c_adapter = 1;
593
594         for (i = 0; i < TWL4030_NUM_SLAVES; i++) {
595                 /* Check if I need to hook on to this adapter or not */
596                 if (twl4030_modules[i].adapter_index == twl_i2c_adapter) {
597                         ret = twl4030_detect_client(adapter, i);
598                         if (ret)
599                                 goto free_client;
600                 }
601         }
602         twl_i2c_adapter++;
603
604         /*
605          * Check if the PIH module is initialized, if yes, then init
606          * the T2 Interrupt subsystem
607          */
608         if ((twl4030_modules[twl4030_map[TWL4030_MODULE_PIH].sid].inuse ==
609                 TWL_CLIENT_USED) && (twl_irq_used != USED)) {
610                 twl_init_irq();
611                 twl_irq_used = USED;
612         }
613         return 0;
614
615 free_client:
616         pr_err("TWL_CLIENT(Idx=%d] registration failed[0x%x]\n", i, ret);
617
618         /* ignore current slave..it never got registered */
619         i--;
620         while (i >= 0) {
621                 /* now remove all those from the current adapter... */
622                 if (twl4030_modules[i].adapter_index == twl_i2c_adapter)
623                         (void)twl4030_detach_client(&twl4030_modules[i].client);
624                 i--;
625         }
626         return ret;
627 }
628
629 /* adapter's callback */
630 static int twl4030_detach_client(struct i2c_client *client)
631 {
632         int err;
633         err = i2c_detach_client(client);
634         if (err) {
635                 pr_err("Client detach failed\n");
636                 return err;
637         }
638         return 0;
639 }
640
641 static struct task_struct *start_twl4030_irq_thread(int irq)
642 {
643         struct task_struct *thread;
644
645         init_completion(&irq_event);
646         thread = kthread_run(twl4030_irq_thread, (void *)irq,
647                              "twl4030 irq %d", irq);
648         if (!thread)
649                 pr_err("%s: could not create twl4030 irq %d thread!\n",
650                        __func__, irq);
651
652         return thread;
653 }
654
655 /*
656  * These three functions should be part of Voltage frame work
657  * added here to complete the functionality for now.
658  */
659 static int protect_pm_master(void)
660 {
661         int e = 0;
662
663         e = twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_LOCK,
664                         R_PROTECT_KEY);
665         return e;
666 }
667
668 static int unprotect_pm_master(void)
669 {
670         int e = 0;
671
672         e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_UNLOCK1,
673                         R_PROTECT_KEY);
674         e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_UNLOCK2,
675                         R_PROTECT_KEY);
676         return e;
677 }
678
679 static int power_companion_init(void)
680 {
681         struct clk *osc;
682         u32 rate;
683         u8 ctrl = HFCLK_FREQ_26_MHZ;
684         int e = 0;
685
686         if (cpu_is_omap2430())
687                 osc = clk_get(NULL, "osc_ck");
688         else
689                 osc = clk_get(NULL, "osc_sys_ck");
690         if (IS_ERR(osc)) {
691                 printk(KERN_WARNING "Skipping twl3040 internal clock init and "
692                                 "using bootloader value (unknown osc rate)\n");
693                 return 0;
694         }
695
696         rate = clk_get_rate(osc);
697         clk_put(osc);
698
699         switch (rate) {
700         case 19200000 : ctrl = HFCLK_FREQ_19p2_MHZ; break;
701         case 26000000 : ctrl = HFCLK_FREQ_26_MHZ; break;
702         case 38400000 : ctrl = HFCLK_FREQ_38p4_MHZ; break;
703         }
704
705         ctrl |= HIGH_PERF_SQ;
706         e |= unprotect_pm_master();
707         /* effect->MADC+USB ck en */
708         e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, ctrl, R_CFG_BOOT);
709         e |= protect_pm_master();
710
711         return e;
712 }
713
714 static void twl_init_irq(void)
715 {
716         int     i = 0;
717         int     res = 0;
718         char    *msg = "Unable to register interrupt subsystem";
719
720         /*
721          * We end up with interrupts from other modules before
722          * they get a chance to handle them...
723          */
724         /* PWR_ISR1 */
725         res = twl4030_i2c_write_u8(TWL4030_MODULE_INT, 0xFF, 0x00);
726         if (res < 0) {
727                 pr_err("%s[%d][%d]\n", msg, res, __LINE__);
728                 return;
729         }
730
731         /* PWR_ISR2 */
732         res = twl4030_i2c_write_u8(TWL4030_MODULE_INT, 0xFF, 0x02);
733         if (res < 0) {
734                 pr_err("%s[%d][%d]\n", msg, res, __LINE__);
735                 return;
736         }
737
738         /* PWR_IMR1 */
739         res = twl4030_i2c_write_u8(TWL4030_MODULE_INT, 0xFF, 0x1);
740         if (res < 0) {
741                 pr_err("%s[%d][%d]\n", msg, res, __LINE__);
742                 return;
743         }
744
745         /* PWR_IMR2 */
746         res = twl4030_i2c_write_u8(TWL4030_MODULE_INT, 0xFF, 0x3);
747         if (res < 0) {
748                 pr_err("%s[%d][%d]\n", msg, res, __LINE__);
749                 return;
750         }
751
752         /* Clear off any other pending interrupts on power */
753         /* PWR_ISR1 */
754         res = twl4030_i2c_write_u8(TWL4030_MODULE_INT, 0xFF, 0x00);
755         if (res < 0) {
756                 pr_err("%s[%d][%d]\n", msg, res, __LINE__);
757                 return;
758         }
759
760         /* PWR_ISR2 */
761         res = twl4030_i2c_write_u8(TWL4030_MODULE_INT, 0xFF, 0x02);
762         if (res < 0) {
763                 pr_err("%s[%d][%d]\n", msg, res, __LINE__);
764                 return;
765         }
766         /* POWER HACK (END) */
767         /* Slave address 0x4A */
768
769         /* BCIIMR1_1 */
770         res = twl4030_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, 0xFF, 0x3);
771         if (res < 0) {
772                 pr_err("%s[%d][%d]\n", msg, res, __LINE__);
773                 return;
774         }
775
776         /* BCIIMR1_2 */
777         res = twl4030_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, 0xFF, 0x4);
778         if (res < 0) {
779                 pr_err("%s[%d][%d]\n", msg, res, __LINE__);
780                 return;
781         }
782
783         /* BCIIMR2_1 */
784         res = twl4030_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, 0xFF, 0x7);
785         if (res < 0) {
786                 pr_err("%s[%d][%d]\n", msg, res, __LINE__);
787                 return;
788         }
789
790         /* BCIIMR2_2 */
791         res = twl4030_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, 0xFF, 0x8);
792         if (res < 0) {
793                 pr_err("%s[%d][%d]\n", msg, res, __LINE__);
794                 return;
795         }
796
797         /* MAD C */
798         /* MADC_IMR1 */
799         res = twl4030_i2c_write_u8(TWL4030_MODULE_MADC, 0xFF, 0x62);
800         if (res < 0) {
801                 pr_err("%s[%d][%d]\n", msg, res, __LINE__);
802                 return;
803         }
804
805         /* MADC_IMR2 */
806         res = twl4030_i2c_write_u8(TWL4030_MODULE_MADC, 0xFF, 0x64);
807         if (res < 0) {
808                 pr_err("%s[%d][%d]\n", msg, res, __LINE__);
809                 return;
810         }
811
812         /* key Pad */
813         /* KEYPAD - IMR1 */
814         res = twl4030_i2c_write_u8(TWL4030_MODULE_KEYPAD, 0xFF, (0x12));
815         if (res < 0) {
816                 pr_err("%s[%d][%d]\n", msg, res, __LINE__);
817                 return;
818         }
819         {
820                 u8 clear;
821                 /* Clear ISR */
822                 twl4030_i2c_read_u8(TWL4030_MODULE_KEYPAD, &clear, 0x11);
823                 twl4030_i2c_read_u8(TWL4030_MODULE_KEYPAD, &clear, 0x11);
824         }
825
826         /* KEYPAD - IMR2 */
827         res = twl4030_i2c_write_u8(TWL4030_MODULE_KEYPAD, 0xFF, (0x14));
828         if (res < 0) {
829                 pr_err("%s[%d][%d]\n", msg, res, __LINE__);
830                 return;
831         }
832
833         /* Slave address 0x49 */
834         /* GPIO_IMR1A */
835         res = twl4030_i2c_write_u8(TWL4030_MODULE_GPIO, 0xFF, (0x1C));
836         if (res < 0) {
837                 pr_err("%s[%d][%d]\n", msg, res, __LINE__);
838                 return;
839         }
840
841         /* GPIO_IMR2A */
842         res = twl4030_i2c_write_u8(TWL4030_MODULE_GPIO, 0xFF, (0x1D));
843         if (res < 0) {
844                 pr_err("%s[%d][%d]\n", msg, res, __LINE__);
845                 return;
846         }
847
848         /* GPIO_IMR3A */
849         res = twl4030_i2c_write_u8(TWL4030_MODULE_GPIO, 0xFF, (0x1E));
850         if (res < 0) {
851                 pr_err("%s[%d][%d]\n", msg, res, __LINE__);
852                 return;
853         }
854
855         /* GPIO_IMR1B */
856         res = twl4030_i2c_write_u8(TWL4030_MODULE_GPIO, 0xFF, (0x22));
857         if (res < 0) {
858                 pr_err("%s[%d][%d]\n", msg, res, __LINE__);
859                 return;
860         }
861
862         /* GPIO_IMR2B */
863         res = twl4030_i2c_write_u8(TWL4030_MODULE_GPIO, 0xFF, (0x23));
864         if (res < 0) {
865                 pr_err("%s[%d][%d]\n", msg, res, __LINE__);
866                 return;
867         }
868
869         /* GPIO_IMR3B */
870         res = twl4030_i2c_write_u8(TWL4030_MODULE_GPIO, 0xFF, (0x24));
871         if (res < 0) {
872                 pr_err("%s[%d][%d]\n", msg, res, __LINE__);
873                 return;
874         }
875
876         /* install an irq handler for each of the PIH modules */
877         for (i = IH_TWL4030_BASE; i < IH_TWL4030_END; i++) {
878                 set_irq_chip(i, &twl4030_irq_chip);
879                 set_irq_handler(i, do_twl4030_module_irq);
880                 set_irq_flags(i, IRQF_VALID);
881         }
882
883         /* install an irq handler to demultiplex the TWL4030 interrupt */
884         set_irq_data(TWL4030_IRQNUM, start_twl4030_irq_thread(TWL4030_IRQNUM));
885         set_irq_type(TWL4030_IRQNUM, IRQT_FALLING);
886         set_irq_chained_handler(TWL4030_IRQNUM, do_twl4030_irq);
887
888         res = power_companion_init();
889         if (res < 0)
890                 pr_err("%s[%d][%d]\n", msg, res, __LINE__);
891 }
892
893 static int __init twl4030_init(void)
894 {
895         return i2c_add_driver(&twl4030_driver);
896 }
897
898 static void __exit twl4030_exit(void)
899 {
900         i2c_del_driver(&twl4030_driver);
901         twl_irq_used = FREE;
902 }
903
904 subsys_initcall(twl4030_init);
905 module_exit(twl4030_exit);
906
907 MODULE_AUTHOR("Texas Instruments, Inc.");
908 MODULE_DESCRIPTION("I2C Core interface for TWL4030");
909 MODULE_LICENSE("GPL");