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