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