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